From d56c5bf2cb228a1142bb034cded84ce99bba3490 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 04:46:32 -0800 Subject: [PATCH] fix(codex): keep the file stem as the session id, use the thread id only in dedup keys The previous commit also moved the visible session id of payload.id-only rollouts from the file stem to the UUID, which changes their label and identity in session views. Only the dedup keys need the thread id, so keep sessionId as it was and carry the thread id separately, across append resumes too. --- src/providers/codex.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/providers/codex.ts b/src/providers/codex.ts index 21702ab5..586ee3e6 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -733,6 +733,7 @@ function isValidCodexTokenUsage(value: unknown): value is CodexTokenUsage { type CodexResumeState = { sessionModel?: string sessionId: string + threadId?: string sessionCwd?: string forkedFromId: string forkReplayState?: CodexForkReplayState @@ -811,6 +812,7 @@ function isResumeState(value: unknown): value is CodexResumeState { }) ) return typeof v['sessionId'] === 'string' + && (v['threadId'] === undefined || typeof v['threadId'] === 'string') && typeof v['forkedFromId'] === 'string' && (v['forkReplayState'] === undefined || isCodexForkReplayState(v['forkReplayState'])) && (v['prevCumulativeTotal'] === null || typeof v['prevCumulativeTotal'] === 'number') @@ -869,6 +871,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { let sessionModel: string | undefined = resume?.state.sessionModel let sessionId = resume?.state.sessionId ?? '' + let threadId = resume?.state.threadId ?? sessionId let sessionCwd: string | undefined = resume?.state.sessionCwd let forkedFromId = resume?.state.forkedFromId ?? '' let forkReplayState = resume?.state.forkReplayState ? { ...resume.state.forkReplayState } : undefined @@ -1143,10 +1146,11 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { if (!entry) continue 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 - // same thread UUID a fork names in `forked_from_id`; keying on it lets - // a fork's replayed history collide with the parent's own records. - sessionId = entry.payload?.session_id ?? entry.payload?.id ?? rolloutStem(basename(source.path)) + // 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 // 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 @@ -1279,6 +1283,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { resumeState = { ...(sessionModel !== undefined ? { sessionModel } : {}), sessionId, + threadId, ...(sessionCwd !== undefined ? { sessionCwd } : {}), forkedFromId, ...(forkReplayState ? { forkReplayState: { ...forkReplayState } } : {}), @@ -1490,7 +1495,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { hasTokenUsageRecord = true const dedupKey = typeof responseId === 'string' && responseId - ? `codex:${forkedFromId || sessionId}:response:${responseId}` + ? `codex:${forkedFromId || threadId}:response:${responseId}` : `codex:usage-record:${JSON.stringify([source.path, tracker.lastCompleteLineOffset])}` accountUsage(entry, usage, dedupKey) continue @@ -1647,7 +1652,7 @@ function createParser(source: SessionSource, seenKeys: Set, capture?: { // burst (accepted trade-off: the alternative collapsed // distinct requests wholesale). const dedupKey = reportsCumulative - ? `codex:${forkedFromId || sessionId}:${cumulativeTotal}:${total?.input_tokens ?? 0}:${total?.cached_input_tokens ?? 0}:${total?.output_tokens ?? 0}:${total?.reasoning_output_tokens ?? 0}` + ? `codex:${forkedFromId || threadId}:${cumulativeTotal}:${total?.input_tokens ?? 0}:${total?.cached_input_tokens ?? 0}:${total?.output_tokens ?? 0}:${total?.reasoning_output_tokens ?? 0}` : `codex:record:${JSON.stringify([source.path, tracker.lastCompleteLineOffset])}` accountUsage(entry, {