From 63de339cdc7c3bb60a8a6591e7ed834915a28048 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 05:50:56 -0800 Subject: [PATCH] fix(codex): detect a counter restart from the total, not from any single field Any one cumulative field that fell was read as a restart, so a corrected component counted its whole running value again (output 800 -> 790 under a rising total emitted 790 output). Decide the restart on the total falling, or every reported field falling when no previous total exists; otherwise a falling field is a correction and adds nothing. --- src/providers/codex.ts | 20 +++++++++++++++++--- tests/providers/codex.test.ts | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/providers/codex.ts b/src/providers/codex.ts index af1b429c..cca9baa8 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1589,6 +1589,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { if (infoIdentity === prevInfoIdentity) continue prevInfoIdentity = infoIdentity if (reportsCumulative && prevCumulativeTotal !== null && cumulativeTotal === prevCumulativeTotal) continue + const prevTotal = prevCumulativeTotal prevCumulativeTotal = reportsCumulative ? cumulativeTotal : null const last = info.last_token_usage @@ -1607,9 +1608,22 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { } else if (cumulativeTotal > 0) { const total = info.total_token_usage if (!total) continue - // An omitted counter is unchanged. One below its previous value - // restarted (resume, compaction): it holds the usage since then. - const since = (now: number | undefined, prev: number) => now === undefined ? 0 : now >= prev ? now - prev : now + // A total below the previous one (or, without a previous total, + // every reported field falling) is a counter restart (resume, + // compaction): each field holds the usage since then. Otherwise a + // single falling field is a correction and adds nothing. An + // omitted field is unchanged. + const counters: Array<[number | undefined, number]> = [ + [total.input_tokens, prevInput], [total.cached_input_tokens, prevCached], + [total.cache_write_input_tokens, prevCacheWrite], [total.output_tokens, prevOutput], + [total.reasoning_output_tokens, prevReasoning], + ] + const present = counters.filter(([now]) => now !== undefined) as Array<[number, number]> + const restart = prevTotal !== null + ? cumulativeTotal < prevTotal + : present.length > 0 && present.every(([now, prev]) => now < prev) + const since = (now: number | undefined, prev: number) => + now === undefined ? 0 : restart ? now : Math.max(0, now - prev) inputTokens = since(total.input_tokens, prevInput) cachedInputTokens = since(total.cached_input_tokens, prevCached) cacheWriteTokens = since(total.cache_write_input_tokens, prevCacheWrite) diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index a4837633..bec329d2 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -857,6 +857,20 @@ describe('codex provider - JSONL parsing', () => { expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [3_000, 300], [3_000, 300]]) }) + it('reads one falling field under a rising total as a correction, not a restart', async () => { + const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-correction.jsonl', [ + sessionMeta({ session_id: 'sess-correction', model: 'gpt-5.5' }), + tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 8_000, output: 800 } }), + tokenCount({ timestamp: '2026-04-14T10:00:20Z', total: { input: 9_000, output: 790, total: 9_790 } }), + ]) + + const provider = createCodexProvider(tmpDir) + const calls: ParsedProviderCall[] = [] + for await (const call of provider.createSessionParser({ path: filePath, project: 'test', provider: 'codex' }, new Set()).parse()) calls.push(call) + + expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [1_000, 0]]) + }) + it('reads a cumulative field the record omits as unchanged', async () => { const total = (timestamp: string, usage: Record) => JSON.stringify({ type: 'event_msg', timestamp, payload: { type: 'token_count', info: { total_token_usage: usage } },