From 137decbcd59051c854a8e392f2a24521b5c91cc4 Mon Sep 17 00:00:00 2001 From: stevejkang Date: Mon, 11 May 2026 09:52:13 +0900 Subject: [PATCH] Fix session usage showing 100% when actual usage is 1% The normalizeUsedPercent boundary condition <= 1 incorrectly caught usedPercent values of exactly 1 (meaning 1% in 0-100 scale) and multiplied them to 100. This only manifested in session windows where low usage values hit the boundary. Change to < 1 so that integer percentage values pass through without normalization. --- src/__tests__/fetcher.test.ts | 24 ++++++++++++++++++++++++ src/fetcher.ts | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/__tests__/fetcher.test.ts b/src/__tests__/fetcher.test.ts index 9e420c3..a109db2 100644 --- a/src/__tests__/fetcher.test.ts +++ b/src/__tests__/fetcher.test.ts @@ -142,6 +142,30 @@ describe("fetchRateLimits", () => { expect(result.primary!.usedPercent).toBe(75) expect(result.secondary!.usedPercent).toBe(30) }) + + it("does not normalize usedPercent of exactly 1 (already 0-100 scale)", async () => { + const proc = createMockProcess() + ;(spawn as unknown as Mock).mockReturnValue(proc) + + const promise = fetchRateLimits("/usr/local/bin/codex") + + await new Promise((r) => setTimeout(r, 0)) + proc.stdout.emit("data", Buffer.from(JSON.stringify({ id: 1, result: {} }) + "\n")) + await new Promise((r) => setTimeout(r, 0)) + proc.stdout.emit( + "data", + Buffer.from( + JSON.stringify({ + id: 2, + result: { rateLimits: { primary: { usedPercent: 1 }, secondary: { usedPercent: 1 } } }, + }) + "\n" + ) + ) + + const result = await promise + expect(result.primary!.usedPercent).toBe(1) + expect(result.secondary!.usedPercent).toBe(1) + }) }) describe("readCache", () => { diff --git a/src/fetcher.ts b/src/fetcher.ts index 65bcbcf..95ac348 100644 --- a/src/fetcher.ts +++ b/src/fetcher.ts @@ -99,12 +99,12 @@ export function fetchRateLimits(codexBinary: string): Promise function normalizeUsedPercent(snapshot: RateLimitSnapshot): void { if (snapshot.primary && snapshot.primary.usedPercent != null) { - if (snapshot.primary.usedPercent > 0 && snapshot.primary.usedPercent <= 1) { + if (snapshot.primary.usedPercent > 0 && snapshot.primary.usedPercent < 1) { snapshot.primary.usedPercent *= 100 } } if (snapshot.secondary && snapshot.secondary.usedPercent != null) { - if (snapshot.secondary.usedPercent > 0 && snapshot.secondary.usedPercent <= 1) { + if (snapshot.secondary.usedPercent > 0 && snapshot.secondary.usedPercent < 1) { snapshot.secondary.usedPercent *= 100 } }