fix(codex): start every counter from zero on a restart

A restart record that left out a counter kept that counter's pre-restart
value as the baseline, so the next record's usage for it was swallowed
(8000/800 -> restart 3000 -> 6000/300 counted no output). On a detected
restart every baseline returns to zero, reported or not.
This commit is contained in:
Resham Joshi 2026-11-06 05:53:27 -08:00
parent 63de339cdc
commit deb6b3b297
2 changed files with 20 additions and 5 deletions

View file

@ -1610,9 +1610,9 @@ function createParser(source: SessionSource, seenKeys: Set<string>, 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<string>, 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)

View file

@ -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' }),