From aa39d17803651276d8c850edae0b4eef7c200f0b Mon Sep 17 00:00:00 2001 From: MarcoDotIO Date: Wed, 4 Nov 2026 00:40:54 -0500 Subject: [PATCH] fix(server): rewound Codex threads import their history from before the rewind Codex rewinds a paginated thread by writing a new rollout that inherits a byte prefix of the rollout it replaced (session_meta.history_base). The session importer read only the newest rollout, so the imported thread started at the rewind and lost everything before it. It now follows history_base through the earlier rollouts and imports the inherited prefix ahead of the new records. Co-Authored-By: Claude Opus 5.5 --- .../src/project/AgentSessionScanner.test.ts | 85 ++++++++++++++++ .../server/src/project/AgentSessionScanner.ts | 96 +++++++++++++++++-- 2 files changed, 175 insertions(+), 6 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index ce1830b8c5710..390633ad99d8f 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1337,6 +1337,91 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("imports a rewound Codex thread with the history its new rollout inherits", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const workspace = yield* makeTempDir("t3code-workspace-"); + const threadId = "019a0000-0000-7000-8000-000000000001"; + const rolloutId = "019a0000-0000-7000-8000-000000000002"; + const sessionsDir = path.join(codexHomePath, "sessions", "2026", "08", "24"); + const prompt = (message: string) => + encodeTranscriptRecord({ type: "event_msg", payload: { type: "user_message", message } }); + const reply = (text: string) => + encodeTranscriptRecord({ + type: "response_item", + payload: { + type: "message", + role: "assistant", + content: [{ type: "output_text", text }], + }, + }); + const keptHistory = [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: threadId, cwd: workspace, history_mode: "paginated" }, + }), + prompt("Keep the first request"), + reply("First reply"), + ] + .map((record) => `${record}\n`) + .join(""); + + yield* writeTranscript({ + filePath: path.join(sessionsDir, `rollout-2026-08-24T10-00-00-${threadId}.jsonl`), + contents: `${keptHistory}${prompt("Rewound request")}\n${reply("Rewound reply")}\n`, + mtimeMs: nowMs - 60_000, + }); + yield* writeTranscript({ + filePath: path.join( + sessionsDir, + `rollout-2026-08-24T11-00-00-${threadId}_${rolloutId}.jsonl`, + ), + contents: [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { + id: threadId, + cwd: workspace, + history_mode: "paginated", + history_base: { + thread_id: threadId, + end_ordinal_exclusive: 3, + end_byte_offset: Buffer.byteLength(keptHistory), + }, + }, + }), + prompt("Ask again"), + reply("Final reply"), + ].join("\n"), + mtimeMs: nowMs, + }); + + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + }); + + expect( + threads.map((thread) => ({ + id: thread.providerSessionId, + title: thread.title, + messages: thread.messages.map((message) => message.text), + })), + ).toEqual([ + { + id: threadId, + title: "Keep the first request", + messages: ["Keep the first request", "First reply", "Ask again", "Final reply"], + }, + ]); + }), + ); + it.effect("excludes Codex subagents from discovery and the recent-thread budget", () => Effect.gen(function* () { const path = yield* Path.Path; diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 3ed97f8a7d8f4..25ec41cf064c5 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -120,6 +120,7 @@ const TranscriptRecord = Schema.Struct({ message: Schema.optional(Schema.String), model: Schema.optional(Schema.String), cwd: Schema.optional(Schema.String), + history_base: Schema.optional(Schema.Unknown), content: Schema.optional(Schema.Array(TranscriptContentBlock)), internal_chat_message_metadata_passthrough: Schema.optional(Schema.Unknown), }), @@ -144,6 +145,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption( ); const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord); const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata); +const decodeCodexHistoryBase = Schema.decodeUnknownOption( + Schema.Struct({ thread_id: Schema.String, end_byte_offset: Schema.Number }), +); +// Original rollouts end in their thread ID, rewound ones in `_`. +const CODEX_ROLLOUT_ID_PATTERN = + /[-_]([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i; const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption( Schema.fromJsonString(CodexSessionIndexEntry), ); @@ -335,6 +342,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap { return names; } +// A rewound paginated Codex thread continues in a new rollout that inherits a +// byte prefix of the rollout it replaced. +function codexHistoryBase(records: ReadonlyArray) { + const base = Option.getOrUndefined( + decodeCodexHistoryBase( + records.find((record) => record.type === "session_meta")?.payload?.history_base, + ), + ); + return base !== undefined && + Number.isSafeInteger(base.end_byte_offset) && + base.end_byte_offset >= 0 + ? base + : undefined; +} + function deriveTitle(source: AgentSessionSource, text: string): string | undefined { const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text; return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined; @@ -997,8 +1019,10 @@ export const make = Effect.gen(function* () { filePath: string, expected: ReturnType, source: AgentSessionSource, + prefix?: { readonly endByteOffset: number; readonly historyBytes: number }, ) { - if (expected.size > MAX_IMPORTED_TRANSCRIPT_BYTES) return null; + const readBytes = prefix?.endByteOffset ?? expected.size; + if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) return null; return yield* Effect.scoped( fileSystem.open(filePath, { flag: "r" }).pipe( @@ -1010,7 +1034,7 @@ export const make = Effect.gen(function* () { const records: Array = []; let cwd = Option.none(); let claudeFields = emptyClaudeFields; - let historyBytes = 0; + let historyBytes = prefix?.historyBytes ?? 0; let recordBytes = 0; let recordCount = 0; let bytesRead = 0; @@ -1062,9 +1086,9 @@ export const make = Effect.gen(function* () { return true; }; - while (bytesRead < expected.size) { + while (bytesRead < readBytes) { const next = yield* file.readAlloc( - Math.min(TRANSCRIPT_PREFIX_BYTES, expected.size - bytesRead), + Math.min(TRANSCRIPT_PREFIX_BYTES, readBytes - bytesRead), ); if (Option.isNone(next)) { return null; @@ -1089,7 +1113,7 @@ export const make = Effect.gen(function* () { if (recordStarted && !(yield* Effect.try(finishRecord))) return null; return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat)) - ? { records, cwd, claudeFields } + ? { records, cwd, claudeFields, historyBytes } : null; }), ), @@ -1667,6 +1691,56 @@ export const make = Effect.gen(function* () { const codexThreadNamesByHome = new Map>(); let bytesRemaining = MAX_IMPORT_BYTES; let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS; + const codexRolloutPaths = new Map>(); + const codexRolloutPath = (providerInstanceId: ProviderInstanceId, rolloutId: string) => { + let paths = codexRolloutPaths.get(providerInstanceId); + if (paths === undefined) { + paths = new Map( + candidates.flatMap((candidate) => + candidate.source === "codex" && candidate.providerInstanceId === providerInstanceId + ? candidate.transcripts.flatMap((transcript) => { + const id = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1]; + return id === undefined ? [] : [[id, transcript.filePath] as const]; + }) + : [], + ), + ); + codexRolloutPaths.set(providerInstanceId, paths); + } + return paths.get(rolloutId); + }; + // Reads the prefixes a rewound rollout inherits, oldest first. Without all of + // them the rollout is imported on its own, as before. + const readCodexHistoryBase = Effect.fn("AgentSessionScanner.readCodexHistoryBase")(function* ( + providerInstanceId: ProviderInstanceId, + records: ReadonlyArray, + historyBytes: number, + ) { + const prefixes: Array> = []; + const visited = new Set(); + for (let base = codexHistoryBase(records); base !== undefined;) { + const filePath = visited.has(base.thread_id) + ? undefined + : codexRolloutPath(providerInstanceId, base.thread_id); + if (filePath === undefined || base.end_byte_offset > bytesRemaining) return null; + visited.add(base.thread_id); + const stats = yield* statOption(filePath); + if (Option.isNone(stats) || stats.value.type !== "File") return null; + bytesRemaining -= base.end_byte_offset; + const inherited = yield* readTranscript( + filePath, + transcriptIdentity(filePath, stats.value), + "codex", + { endByteOffset: base.end_byte_offset, historyBytes }, + ); + if (inherited === null) return null; + historyBytes = inherited.historyBytes; + // The rollout's own session_meta names the thread; inherited ones may name a fork parent. + prefixes.unshift(inherited.records.filter((record) => record.type !== "session_meta")); + base = codexHistoryBase(inherited.records); + } + return prefixes.flat(); + }); return Stream.fromIteratorSucceed(eligibleTranscripts.values(), 1).pipe( Stream.mapEffect(({ candidate, transcript }) => Effect.gen(function* () { @@ -1719,6 +1793,14 @@ export const make = Effect.gen(function* () { return Option.some({ _tag: "Skipped" }); } + const inheritedRecords = + candidate.source === "codex" + ? yield* readCodexHistoryBase( + candidate.providerInstanceId, + snapshot.records, + snapshot.historyBytes, + ) + : null; const parsedThread = parseAgentSessionRecords( { source: candidate.source, @@ -1726,7 +1808,9 @@ export const make = Effect.gen(function* () { fallbackSessionId: path.basename(transcript.filePath, ".jsonl"), lastActiveAtMs: transcript.mtimeMs, }, - snapshot.records, + inheritedRecords === null + ? snapshot.records + : [...inheritedRecords, ...snapshot.records], snapshot.claudeFields, ); if (parsedThread === null) {