From f595cc614dd6e3ab8bdcef7d883e9f573747d40b Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:22:08 -0800 Subject: [PATCH] fix(codex): key a parent on the id forks reference, ahead of session_id Dedup keys took session_id before id, so a parent whose session_id differs from its id kept its own records apart from a fork that names it by id, and replays past the burst double-counted. Prefer id, falling back to session_id and then the file stem. Owner data: all 410 forks with a parent present name the parent's id; 1436 of 1441 metas carry both fields and 5 differ, all forks (keyed on the parent), so no parent here has a differing pair; the test pins the rule. --- src/providers/codex.ts | 8 ++++---- tests/providers/codex.test.ts | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/providers/codex.ts b/src/providers/codex.ts index dc3b4ad0..115a8707 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -1171,10 +1171,10 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { if (entry.type === 'session_meta') { sessionId = entry.payload?.session_id ?? rolloutStem(basename(source.path)) - // Rollouts from before Codex wrote `session_id` carry only `id`, the - // thread UUID a fork names in `forked_from_id`. Dedup keys use it so a - // fork's replayed history collides with the parent's own records. - threadId = entry.payload?.session_id ?? entry.payload?.id ?? sessionId + // `id` is the thread UUID a fork names in `forked_from_id` (older + // rollouts carry only `id`). Dedup keys use it so a fork's replayed + // history collides with the parent's own records. + threadId = entry.payload?.id ?? entry.payload?.session_id ?? sessionId // Guarded for the same reason as discoverSessionFile: parseCodexLine // hands back an unchecked JSON.parse cast, and a non-string cwd would // ride into projectPath/workingDirectory where the parser's path diff --git a/tests/providers/codex.test.ts b/tests/providers/codex.test.ts index 0b617d96..94d29fb5 100644 --- a/tests/providers/codex.test.ts +++ b/tests/providers/codex.test.ts @@ -1607,10 +1607,11 @@ describe('codex provider - forked session dedupe', () => { expect(tokens).toBe(200) }) - // Older Codex builds name the thread only as payload.id. Oversized metas take - // the compact decoder, so pad one past the large-line threshold too. - for (const padded of [false, true]) { - it(`drops a fork replay past the burst when the parent meta carries only payload.id${padded ? ' (oversized meta)' : ''}`, async () => { + // Older Codex builds name the thread only as payload.id; a parent can also + // carry a session_id that differs from the id forks reference. Oversized + // metas take the compact decoder, so pad one past the large-line threshold too. + for (const [padded, parentSessionId] of [[false, undefined], [true, undefined], [false, 'rollout-A']] as const) { + it(`drops a fork replay past the burst when the fork names the parent's payload.id${padded ? ' (oversized meta)' : ''}${parentSessionId ? ' (parent also carries a different session_id)' : ''}`, async () => { const parentId = '0199aaaa-0000-7000-8000-000000000001' const meta = (id: string, timestamp: string, extra: Record = {}) => JSON.stringify({ type: 'session_meta', @@ -1618,7 +1619,7 @@ describe('codex provider - forked session dedupe', () => { payload: { id, cwd: '/x', model: 'gpt-5.5', ...extra, ...(padded ? { base_instructions: { text: 'x'.repeat(40_000) } } : {}) }, }) await writeSession(tmpDir, '2026-10-01', `rollout-2026-10-01T09-00-00-${parentId}.jsonl`, [ - meta(parentId, '2026-10-01T09:00:00Z'), + meta(parentId, '2026-10-01T09:00:00Z', parentSessionId ? { session_id: parentSessionId } : {}), tokenUsageRecord({ timestamp: '2026-10-01T09:00:10Z', responseId: 'resp_1', usage: { input: 10_000, output: 1_000 } }), ]) const forkId = '0199bbbb-0000-7000-8000-000000000002'