@devries-cameron20 / t3code-1 / commits / d0da47a7e9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): rewound Codex threads import their history from before the rewind (#30652)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

MarcoDotIO committed Nov 5, 2026 at 23:33 UTC d0da47a7e9ff0178745185494ea72708aff16052
2 files changed +175 -6
apps/server/src/project/AgentSessionScanner.test.ts
+85
@@ -1345,6 +1345,91 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
1345 }),
1346 );
1347
1348 + it.effect("imports a rewound Codex thread with the history its new rollout inherits", () =>
1349 + Effect.gen(function* () {
1350 + const path = yield* Path.Path;
1351 + const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1352 + yield* TestClock.setTime(nowMs);
1353 + const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1354 + const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1355 + const workspace = yield* makeTempDir("t3code-workspace-");
1356 + const threadId = "019a0000-0000-7000-8000-000000000001";
1357 + const rolloutId = "019a0000-0000-7000-8000-000000000002";
1358 + const sessionsDir = path.join(codexHomePath, "sessions", "2026", "08", "24");
1359 + const prompt = (message: string) =>
1360 + encodeTranscriptRecord({ type: "event_msg", payload: { type: "user_message", message } });
1361 + const reply = (text: string) =>
1362 + encodeTranscriptRecord({
1363 + type: "response_item",
1364 + payload: {
1365 + type: "message",
1366 + role: "assistant",
1367 + content: [{ type: "output_text", text }],
1368 + },
1369 + });
1370 + const keptHistory = [
1371 + encodeTranscriptRecord({
1372 + type: "session_meta",
1373 + payload: { id: threadId, cwd: workspace, history_mode: "paginated" },
1374 + }),
1375 + prompt("Keep the first request"),
1376 + reply("First reply"),
1377 + ]
1378 + .map((record) => `${record}\n`)
1379 + .join("");
1380 +
1381 + yield* writeTranscript({
1382 + filePath: path.join(sessionsDir, `rollout-2026-08-24T10-00-00-${threadId}.jsonl`),
1383 + contents: `${keptHistory}${prompt("Rewound request")}\n${reply("Rewound reply")}\n`,
1384 + mtimeMs: nowMs - 60_000,
1385 + });
1386 + yield* writeTranscript({
1387 + filePath: path.join(
1388 + sessionsDir,
1389 + `rollout-2026-08-24T11-00-00-${threadId}_${rolloutId}.jsonl`,
1390 + ),
1391 + contents: [
1392 + encodeTranscriptRecord({
1393 + type: "session_meta",
1394 + payload: {
1395 + id: threadId,
1396 + cwd: workspace,
1397 + history_mode: "paginated",
1398 + history_base: {
1399 + thread_id: threadId,
1400 + end_ordinal_exclusive: 3,
1401 + end_byte_offset: Buffer.byteLength(keptHistory),
1402 + },
1403 + },
1404 + }),
1405 + prompt("Ask again"),
1406 + reply("Final reply"),
1407 + ].join("\n"),
1408 + mtimeMs: nowMs,
1409 + });
1410 +
1411 + const threads = yield* runRecentThreads({
1412 + claudeHomePath,
1413 + codexHomePath,
1414 + workspaceRoot: workspace,
1415 + });
1416 +
1417 + expect(
1418 + threads.map((thread) => ({
1419 + id: thread.providerSessionId,
1420 + title: thread.title,
1421 + messages: thread.messages.map((message) => message.text),
1422 + })),
1423 + ).toEqual([
1424 + {
1425 + id: threadId,
1426 + title: "Keep the first request",
1427 + messages: ["Keep the first request", "First reply", "Ask again", "Final reply"],
1428 + },
1429 + ]);
1430 + }),
1431 + );
1432 +
1433 it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
1434 Effect.gen(function* () {
1435 const path = yield* Path.Path;
apps/server/src/project/AgentSessionScanner.ts
+90 -6
@@ -129,6 +129,7 @@ const TranscriptRecord = Schema.Struct({
129 message: Schema.optional(Schema.String),
130 model: Schema.optional(Schema.String),
131 cwd: Schema.optional(Schema.String),
132 + history_base: Schema.optional(Schema.Unknown),
133 content: Schema.optional(Schema.Array(TranscriptContentBlock)),
134 internal_chat_message_metadata_passthrough: Schema.optional(Schema.Unknown),
135 }),
@@ -153,6 +154,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption(
154 );
155 const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);
156 const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);
157 +const decodeCodexHistoryBase = Schema.decodeUnknownOption(
158 + Schema.Struct({ thread_id: Schema.String, end_byte_offset: Schema.Number }),
159 +);
160 +// Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
161 +const CODEX_ROLLOUT_ID_PATTERN =
162 + /[-_]([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
163 const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
164 Schema.fromJsonString(CodexSessionIndexEntry),
165 );
@@ -344,6 +351,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
351 return names;
352 }
353
354 +// A rewound paginated Codex thread continues in a new rollout that inherits a
355 +// byte prefix of the rollout it replaced.
356 +function codexHistoryBase(records: ReadonlyArray<DecodedTranscriptRecord>) {
357 + const base = Option.getOrUndefined(
358 + decodeCodexHistoryBase(
359 + records.find((record) => record.type === "session_meta")?.payload?.history_base,
360 + ),
361 + );
362 + return base !== undefined &&
363 + Number.isSafeInteger(base.end_byte_offset) &&
364 + base.end_byte_offset >= 0
365 + ? base
366 + : undefined;
367 +}
368 +
369 function deriveTitle(source: AgentSessionSource, text: string): string | undefined {
370 const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text;
371 return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined;
@@ -1006,8 +1028,10 @@ export const make = Effect.gen(function* () {
1028 filePath: string,
1029 expected: ReturnType<typeof transcriptIdentity>,
1030 source: AgentSessionSource,
1031 + prefix?: { readonly endByteOffset: number; readonly historyBytes: number },
1032 ) {
1010 - if (expected.size > MAX_IMPORTED_TRANSCRIPT_BYTES) return null;
1033 + const readBytes = prefix?.endByteOffset ?? expected.size;
1034 + if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) return null;
1035
1036 return yield* Effect.scoped(
1037 fileSystem.open(filePath, { flag: "r" }).pipe(
@@ -1019,7 +1043,7 @@ export const make = Effect.gen(function* () {
1043 const records: Array<DecodedTranscriptRecord> = [];
1044 let cwd = Option.none<string>();
1045 let claudeFields = emptyClaudeFields;
1022 - let historyBytes = 0;
1046 + let historyBytes = prefix?.historyBytes ?? 0;
1047 let recordBytes = 0;
1048 let recordCount = 0;
1049 let bytesRead = 0;
@@ -1071,9 +1095,9 @@ export const make = Effect.gen(function* () {
1095 return true;
1096 };
1097
1074 - while (bytesRead < expected.size) {
1098 + while (bytesRead < readBytes) {
1099 const next = yield* file.readAlloc(
1076 - Math.min(TRANSCRIPT_PREFIX_BYTES, expected.size - bytesRead),
1100 + Math.min(TRANSCRIPT_PREFIX_BYTES, readBytes - bytesRead),
1101 );
1102 if (Option.isNone(next)) {
1103 return null;
@@ -1108,7 +1132,7 @@ export const make = Effect.gen(function* () {
1132 )
1133 return null;
1134 return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))
1111 - ? { records, cwd, claudeFields }
1135 + ? { records, cwd, claudeFields, historyBytes }
1136 : null;
1137 }),
1138 ),
@@ -1686,6 +1710,56 @@ export const make = Effect.gen(function* () {
1710 const codexThreadNamesByHome = new Map<string, ReadonlyMap<string, string>>();
1711 let bytesRemaining = MAX_IMPORT_BYTES;
1712 let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS;
1713 + const codexRolloutPaths = new Map<ProviderInstanceId, ReadonlyMap<string, string>>();
1714 + const codexRolloutPath = (providerInstanceId: ProviderInstanceId, rolloutId: string) => {
1715 + let paths = codexRolloutPaths.get(providerInstanceId);
1716 + if (paths === undefined) {
1717 + paths = new Map(
1718 + candidates.flatMap((candidate) =>
1719 + candidate.source === "codex" && candidate.providerInstanceId === providerInstanceId
1720 + ? candidate.transcripts.flatMap((transcript) => {
1721 + const id = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1];
1722 + return id === undefined ? [] : [[id, transcript.filePath] as const];
1723 + })
1724 + : [],
1725 + ),
1726 + );
1727 + codexRolloutPaths.set(providerInstanceId, paths);
1728 + }
1729 + return paths.get(rolloutId);
1730 + };
1731 + // Reads the prefixes a rewound rollout inherits, oldest first. Without all of
1732 + // them the rollout is imported on its own, as before.
1733 + const readCodexHistoryBase = Effect.fn("AgentSessionScanner.readCodexHistoryBase")(function* (
1734 + providerInstanceId: ProviderInstanceId,
1735 + records: ReadonlyArray<DecodedTranscriptRecord>,
1736 + historyBytes: number,
1737 + ) {
1738 + const prefixes: Array<ReadonlyArray<DecodedTranscriptRecord>> = [];
1739 + const visited = new Set<string>();
1740 + for (let base = codexHistoryBase(records); base !== undefined;) {
1741 + const filePath = visited.has(base.thread_id)
1742 + ? undefined
1743 + : codexRolloutPath(providerInstanceId, base.thread_id);
1744 + if (filePath === undefined || base.end_byte_offset > bytesRemaining) return null;
1745 + visited.add(base.thread_id);
1746 + const stats = yield* statOption(filePath);
1747 + if (Option.isNone(stats) || stats.value.type !== "File") return null;
1748 + bytesRemaining -= base.end_byte_offset;
1749 + const inherited = yield* readTranscript(
1750 + filePath,
1751 + transcriptIdentity(filePath, stats.value),
1752 + "codex",
1753 + { endByteOffset: base.end_byte_offset, historyBytes },
1754 + );
1755 + if (inherited === null) return null;
1756 + historyBytes = inherited.historyBytes;
1757 + // The rollout's own session_meta names the thread; inherited ones may name a fork parent.
1758 + prefixes.unshift(inherited.records.filter((record) => record.type !== "session_meta"));
1759 + base = codexHistoryBase(inherited.records);
1760 + }
1761 + return prefixes.flat();
1762 + });
1763 return Stream.fromIteratorSucceed(eligibleTranscripts.values(), 1).pipe(
1764 Stream.mapEffect(({ candidate, transcript }) =>
1765 Effect.gen(function* () {
@@ -1738,6 +1812,14 @@ export const make = Effect.gen(function* () {
1812 return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
1813 }
1814
1815 + const inheritedRecords =
1816 + candidate.source === "codex"
1817 + ? yield* readCodexHistoryBase(
1818 + candidate.providerInstanceId,
1819 + snapshot.records,
1820 + snapshot.historyBytes,
1821 + )
1822 + : null;
1823 const parsedThread = parseAgentSessionRecords(
1824 {
1825 source: candidate.source,
@@ -1745,7 +1827,9 @@ export const make = Effect.gen(function* () {
1827 fallbackSessionId: path.basename(transcript.filePath, ".jsonl"),
1828 lastActiveAtMs: transcript.mtimeMs,
1829 },
1748 - snapshot.records,
1830 + inheritedRecords === null
1831 + ? snapshot.records
1832 + : [...inheritedRecords, ...snapshot.records],
1833 snapshot.claudeFields,
1834 );
1835 if (parsedThread === null) {