From ea69a08ffbd696022d293545ef2746ebda4c2acc Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:17:09 -0800 Subject: [PATCH] fix(codex): key cumulative calls on resolved totals An omitted cumulative field reads as unchanged, but the dedup key still took it as 0, so {input:150,total:160} and the same record with an explicit unchanged output:10 got different keys. Key on the resolved running totals so both forms share one key. --- src/providers/codex.ts | 4 +++- tests/providers/codex.test.ts | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 5bbdca5e..dc3b4ad0 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1704,8 +1704,10 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { // Two rollouts of one thread at identical cumulative totals are the // same calls replayed (resume), so sharing this key is intended. After // a counter restart the same totals are new calls: the epoch tells. + // The prev counters hold the resolved totals (an omitted field reads + // as unchanged), so omitted and explicit forms share one key. const dedupKey = reportsCumulative - ? `codex:${forkedFromId || threadId}:${cumulativeTotal}:${total?.input_tokens ?? 0}:${total?.cached_input_tokens ?? 0}:${total?.output_tokens ?? 0}:${total?.reasoning_output_tokens ?? 0}${restartEpoch ? `:r${restartEpoch}` : ''}` + ? `codex:${forkedFromId || threadId}:${cumulativeTotal}:${prevInput}:${prevCached}:${prevOutput}:${prevReasoning}${restartEpoch ? `:r${restartEpoch}` : ''}` : `codex:record:${JSON.stringify([source.path, tracker.lastCompleteLineOffset])}` accountUsage(entry, { diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index 37a2750a..0b617d96 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -887,6 +887,26 @@ describe('codex provider - JSONL parsing', () => { expect(calls.map(c => [c.inputTokens, c.outputTokens])).toEqual([[8_000, 800], [3_000, 0], [3_000, 300]]) }) + it('keys an omitted cumulative field like the same value written out', async () => { + const total = (timestamp: string, usage: Record) => JSON.stringify({ + type: 'event_msg', timestamp, payload: { type: 'token_count', info: { total_token_usage: usage } }, + }) + const keys: string[][] = [] + for (const [name, second] of [['omitted', { input_tokens: 150, total_tokens: 160 }], ['explicit', { input_tokens: 150, output_tokens: 10, total_tokens: 160 }]] as const) { + const filePath = await writeSession(tmpDir, '2026-04-14', `rollout-key-${name}.jsonl`, [ + sessionMeta({ session_id: 'sess-key-form', model: 'gpt-5.5' }), + total('2026-04-14T10:00:10Z', { input_tokens: 100, output_tokens: 10, total_tokens: 110 }), + total('2026-04-14T10:00:20Z', second), + ]) + const calls: ParsedProviderCall[] = [] + for await (const call of createCodexProvider(tmpDir).createSessionParser({ path: filePath, project: 'test', provider: 'codex' }, new Set()).parse()) calls.push(call) + keys.push(calls.map(c => c.deduplicationKey)) + } + + expect(keys[0]).toHaveLength(2) + 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' }),