Skip to content

Commit d0da47a

Browse files
MarcoDotIOclaude
andauthored
fix(server): rewound Codex threads import their history from before the rewind (pingdotgg#30652)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 66e96e5 commit d0da47a

2 files changed

Lines changed: 175 additions & 6 deletions

File tree

‎apps/server/src/project/AgentSessionScanner.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,91 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
13451345
}),
13461346
);
13471347

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+
13481433
it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
13491434
Effect.gen(function* () {
13501435
const path = yield* Path.Path;

‎apps/server/src/project/AgentSessionScanner.ts‎

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const TranscriptRecord = Schema.Struct({
129129
message: Schema.optional(Schema.String),
130130
model: Schema.optional(Schema.String),
131131
cwd: Schema.optional(Schema.String),
132+
history_base: Schema.optional(Schema.Unknown),
132133
content: Schema.optional(Schema.Array(TranscriptContentBlock)),
133134
internal_chat_message_metadata_passthrough: Schema.optional(Schema.Unknown),
134135
}),
@@ -153,6 +154,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption(
153154
);
154155
const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);
155156
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;
156163
const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
157164
Schema.fromJsonString(CodexSessionIndexEntry),
158165
);
@@ -344,6 +351,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
344351
return names;
345352
}
346353

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+
347369
function deriveTitle(source: AgentSessionSource, text: string): string | undefined {
348370
const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text;
349371
return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined;
@@ -1006,8 +1028,10 @@ export const make = Effect.gen(function* () {
10061028
filePath: string,
10071029
expected: ReturnType<typeof transcriptIdentity>,
10081030
source: AgentSessionSource,
1031+
prefix?: { readonly endByteOffset: number; readonly historyBytes: number },
10091032
) {
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;
10111035

10121036
return yield* Effect.scoped(
10131037
fileSystem.open(filePath, { flag: "r" }).pipe(
@@ -1019,7 +1043,7 @@ export const make = Effect.gen(function* () {
10191043
const records: Array<DecodedTranscriptRecord> = [];
10201044
let cwd = Option.none<string>();
10211045
let claudeFields = emptyClaudeFields;
1022-
let historyBytes = 0;
1046+
let historyBytes = prefix?.historyBytes ?? 0;
10231047
let recordBytes = 0;
10241048
let recordCount = 0;
10251049
let bytesRead = 0;
@@ -1071,9 +1095,9 @@ export const make = Effect.gen(function* () {
10711095
return true;
10721096
};
10731097

1074-
while (bytesRead < expected.size) {
1098+
while (bytesRead < readBytes) {
10751099
const next = yield* file.readAlloc(
1076-
Math.min(TRANSCRIPT_PREFIX_BYTES, expected.size - bytesRead),
1100+
Math.min(TRANSCRIPT_PREFIX_BYTES, readBytes - bytesRead),
10771101
);
10781102
if (Option.isNone(next)) {
10791103
return null;
@@ -1108,7 +1132,7 @@ export const make = Effect.gen(function* () {
11081132
)
11091133
return null;
11101134
return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))
1111-
? { records, cwd, claudeFields }
1135+
? { records, cwd, claudeFields, historyBytes }
11121136
: null;
11131137
}),
11141138
),
@@ -1686,6 +1710,56 @@ export const make = Effect.gen(function* () {
16861710
const codexThreadNamesByHome = new Map<string, ReadonlyMap<string, string>>();
16871711
let bytesRemaining = MAX_IMPORT_BYTES;
16881712
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+
});
16891763
return Stream.fromIteratorSucceed(eligibleTranscripts.values(), 1).pipe(
16901764
Stream.mapEffect(({ candidate, transcript }) =>
16911765
Effect.gen(function* () {
@@ -1738,14 +1812,24 @@ export const make = Effect.gen(function* () {
17381812
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
17391813
}
17401814

1815+
const inheritedRecords =
1816+
candidate.source === "codex"
1817+
? yield* readCodexHistoryBase(
1818+
candidate.providerInstanceId,
1819+
snapshot.records,
1820+
snapshot.historyBytes,
1821+
)
1822+
: null;
17411823
const parsedThread = parseAgentSessionRecords(
17421824
{
17431825
source: candidate.source,
17441826
providerInstanceId: candidate.providerInstanceId,
17451827
fallbackSessionId: path.basename(transcript.filePath, ".jsonl"),
17461828
lastActiveAtMs: transcript.mtimeMs,
17471829
},
1748-
snapshot.records,
1830+
inheritedRecords === null
1831+
? snapshot.records
1832+
: [...inheritedRecords, ...snapshot.records],
17491833
snapshot.claudeFields,
17501834
);
17511835
if (parsedThread === null) {

0 commit comments

Comments
 (0)