diff --git a/src/providers/codex.ts b/src/providers/codex.ts index cca9baa8..0d83f338 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1610,9 +1610,9 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { if (!total) continue // 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. + // compaction): every counter, reported or not, starts again from + // zero. 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], @@ -1622,8 +1622,8 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { 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) + if (restart) { prevInput = 0; prevCached = 0; prevCacheWrite = 0; prevOutput = 0; prevReasoning = 0 } + const since = (now: number | undefined, prev: number) => now === undefined ? 0 : 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 bec329d2..f549bc82 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -857,6 +857,21 @@ describe('codex provider - JSONL parsing', () => { expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [3_000, 300], [3_000, 300]]) }) + it('restarts every counter on a restart, including one the restart record omits', async () => { + const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-restart-omitted.jsonl', [ + sessionMeta({ session_id: 'sess-restart-omitted', model: 'gpt-5.5' }), + tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 8_000, output: 800 } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-04-14T10:00:20Z', payload: { type: 'token_count', info: { total_token_usage: { input_tokens: 3_000, total_tokens: 3_000 } } } }), + tokenCount({ timestamp: '2026-04-14T10:00:30Z', total: { input: 6_000, output: 300 } }), + ]) + + 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], [3_000, 0], [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' }),