fix(codex): read a small cumulative drop as a correction, not a restart

Any drop in the total counted as a restart, so 8000/800 -> 7900/790 emitted
a second 7900/790 call. A restart now needs the total to fall to at most
half; a smaller drop is a correction and adds nothing.
Rule evidence: owner data has 0 cumulative-only records and 10 total drops,
all with every field falling to <=0.3% of the prior total (4 at a new
task, none at a compaction).
This commit is contained in:
Resham Joshi 2026-11-06 06:16:43 -08:00
parent ca4df66d27
commit bfd3437236
2 changed files with 21 additions and 7 deletions

View file

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

View file

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