diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 89ac5f72..5bbdca5e 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1637,11 +1637,11 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { } else if (cumulativeTotal > 0) { const total = info.total_token_usage if (!total) continue - // A total below the previous one (or, without a previous total, - // every reported field falling) is a counter restart (resume, - // 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. + // A total at most half the previous one (or, without a previous + // total, every reported field at most half) is a counter restart + // (resume, compaction): every counter, reported or not, starts + // again from zero. A smaller drop is a correction: a falling field + // 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], @@ -1649,8 +1649,8 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { ] 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) + ? cumulativeTotal * 2 <= prevTotal + : present.length > 0 && present.every(([now, prev]) => now * 2 <= prev) if (restart) { restartEpoch++; 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) diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index e46fc6f7..37a2750a 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -887,6 +887,20 @@ describe('codex provider - JSONL parsing', () => { expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [3_000, 0], [3_000, 300]]) }) + it('reads a small drop in every field as a correction, not a restart', async () => { + const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-downward.jsonl', [ + sessionMeta({ session_id: 'sess-downward', 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: 7_900, output: 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]]) + }) + 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' }),