diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 115a8707..857fc7b0 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1637,8 +1637,8 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { } else if (cumulativeTotal > 0) { const total = info.total_token_usage if (!total) continue - // A total at most half the previous one (or, without a previous - // total, every reported field at most half) is a counter restart + // A total at most a tenth of the previous one (or, without a + // previous total, every reported field at most a tenth) is a 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. @@ -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 * 2 <= prevTotal - : present.length > 0 && present.every(([now, prev]) => now * 2 <= prev) + ? cumulativeTotal * 10 <= prevTotal + : present.length > 0 && present.every(([now, prev]) => now * 10 <= 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 94d29fb5..0fcddda3 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -846,45 +846,45 @@ describe('codex provider - JSONL parsing', () => { const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-reset.jsonl', [ sessionMeta({ session_id: 'sess-reset', model: 'gpt-5.5' }), tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 8_000, output: 800 } }), - tokenCount({ timestamp: '2026-04-14T10:10:10Z', total: { input: 3_000, output: 300 } }), - tokenCount({ timestamp: '2026-04-14T10:10:20Z', total: { input: 6_000, output: 600 } }), + tokenCount({ timestamp: '2026-04-14T10:10:10Z', total: { input: 300, output: 30 } }), + tokenCount({ timestamp: '2026-04-14T10:10:20Z', total: { input: 600, output: 60 } }), ]) 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, 300], [3_000, 300]]) + expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [300, 30], [300, 30]]) }) it('counts a cumulative state repeated after a restart as a new call', async () => { const filePath = await writeSession(tmpDir, '2026-04-14', 'rollout-cumulative-restart-repeat.jsonl', [ sessionMeta({ session_id: 'sess-restart-repeat', model: 'gpt-5.5' }), - tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 100, output: 10 } }), + tokenCount({ timestamp: '2026-04-14T10:00:10Z', total: { input: 1_000, output: 100 } }), tokenCount({ timestamp: '2026-04-14T10:00:20Z', total: { input: 50, output: 5 } }), - tokenCount({ timestamp: '2026-04-14T10:00:30Z', total: { input: 100, output: 10 } }), + tokenCount({ timestamp: '2026-04-14T10:00:30Z', total: { input: 1_000, output: 100 } }), ]) 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([[100, 10], [50, 5], [50, 5]]) + expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[1_000, 100], [50, 5], [950, 95]]) }) 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 } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-04-14T10:00:20Z', payload: { type: 'token_count', info: { total_token_usage: { input_tokens: 300, total_tokens: 300 } } } }), + tokenCount({ timestamp: '2026-04-14T10:00:30Z', total: { input: 600, output: 30 } }), ]) 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]]) + expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [300, 0], [300, 30]]) }) it('keys an omitted cumulative field like the same value written out', async () => { @@ -907,19 +907,21 @@ describe('codex provider - JSONL parsing', () => { expect(keys[0]).toEqual(keys[1]) }) - 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 } }), - ]) + for (const [input, output] of [[7_900, 790], [4_000, 400]]) { + it(`reads a drop of every field to ${input}/${output} as a correction, not a restart`, async () => { + const filePath = await writeSession(tmpDir, '2026-04-14', `rollout-cumulative-downward-${input}.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, output } }), + ]) - 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) + 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]]) - }) + 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', [