From eadfaca49c9723cb60f94ecc2af349a21af337a0 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:02:04 -0800 Subject: [PATCH] fix(codex): drop a ping estimate even when the turn's measured record is a duplicate Measured usage replaced the turn's ping estimate only after the dedup check, so a measured record already counted elsewhere (a fork replaying its parent's response) returned early and left the estimate counted on top. Fold the estimate back before the dedup check. --- src/providers/codex.ts | 3 ++- tests/providers/codex.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 496cfa79..b0c8abe7 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1050,7 +1050,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { const outputTokens = usage.output_tokens ?? 0 const reasoningTokens = usage.reasoning_output_tokens ?? 0 const totalTokens = inputTokens + cachedInputTokens + outputTokens + reasoningTokens - if (totalTokens === 0 || seenKeys.has(dedupKey)) return + if (totalTokens === 0) return const est = pingEstimate pingEstimate = null @@ -1066,6 +1066,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { pendingLocRemoved += est.locRemoved ?? 0 pendingEditFailed += est.editFailed ?? 0 } + if (seenKeys.has(dedupKey)) return // OpenAI includes cached tokens inside input_tokens; Anthropic does not. // Normalize to Anthropic semantics: inputTokens = non-cached only. diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index 3141352c..52203c03 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -1588,6 +1588,33 @@ describe('codex provider - forked session dedupe', () => { } }) +describe('codex provider - ping estimate against a replayed measured record', () => { + it('drops the ping estimate when the turn\'s measured record is a fork replay already counted', async () => { + const parentId = '0199aaaa-0000-7000-8000-000000000011' + const forkId = '0199bbbb-0000-7000-8000-000000000012' + const meta = (id: string, timestamp: string, extra: Record = {}) => + JSON.stringify({ type: 'session_meta', timestamp, payload: { id, cwd: '/x', model: 'gpt-5.5', ...extra } }) + await writeSession(tmpDir, '2026-10-01', `rollout-2026-10-01T09-00-00-${parentId}.jsonl`, [ + meta(parentId, '2026-10-01T09:00:00Z'), + tokenUsageRecord({ timestamp: '2026-10-01T09:00:10Z', responseId: 'resp_1', usage: { input: 10_000, output: 1_000 } }), + ]) + await writeSession(tmpDir, '2026-10-01', `rollout-2026-10-01T10-00-00-${forkId}.jsonl`, [ + meta(forkId, '2026-10-01T10:00:00.000Z', { forked_from_id: parentId }), + JSON.stringify({ type: 'response_item', timestamp: '2026-10-01T10:00:01.600Z', payload: { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'replayed answer text' }] } }), + JSON.stringify({ type: 'event_msg', timestamp: '2026-10-01T10:00:01.700Z', payload: { type: 'token_count', info: null, rate_limits: { primary: { used_percent: 3 } } } }), + tokenUsageRecord({ timestamp: '2026-10-01T10:00:01.800Z', responseId: 'resp_1', usage: { input: 10_000, output: 1_000 } }), + ]) + + const provider = createCodexProvider(tmpDir) + const sessions = (await provider.discoverSessions()).sort((a, b) => (a.path < b.path ? -1 : 1)) + const seen = new Set() + const calls: ParsedProviderCall[] = [] + for (const s of sessions) for await (const c of provider.createSessionParser(s, seen).parse()) calls.push(c) + + expect(calls.map(c => [c.inputTokens, c.outputTokens, c.costIsEstimated ?? false])).toEqual([[10_000, 1_000, false]]) + }) +}) + describe('codex provider - fork replay masking against the parent rollout', () => { const PARENT = '019e0000-0000-7000-8000-000000000001' const CHILD = '019e0000-0000-7000-8000-000000000002'