Skip to content

Commit aa39d17

Browse files
MarcoDotIOclaude
andcommitted
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 <[email protected]>
1 parent e68e19d commit aa39d17

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
@@ -1337,6 +1337,91 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
13371337
}),
13381338
);
13391339

1340+
it.effect("imports a rewound Codex thread with the history its new rollout inherits", () =>
1341+
Effect.gen(function* () {
1342+
const path = yield* Path.Path;
1343+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1344+
yield* TestClock.setTime(nowMs);
1345+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1346+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1347+
const workspace = yield* makeTempDir("t3code-workspace-");
1348+
const threadId = "019a0000-0000-7000-8000-000000000001";
1349+
const rolloutId = "019a0000-0000-7000-8000-000000000002";
1350+
const sessionsDir = path.join(codexHomePath, "sessions", "2026", "08", "24");
1351+
const prompt = (message: string) =>
1352+
encodeTranscriptRecord({ type: "event_msg", payload: { type: "user_message", message } });
1353+
const reply = (text: string) =>
1354+
encodeTranscriptRecord({
1355+
type: "response_item",
1356+
payload: {
1357+
type: "message",
1358+
role: "assistant",
1359+
content: [{ type: "output_text", text }],
1360+
},
1361+
});
1362+
const keptHistory = [
1363+
encodeTranscriptRecord({
1364+
type: "session_meta",
1365+
payload: { id: threadId, cwd: workspace, history_mode: "paginated" },
1366+
}),
1367+
prompt("Keep the first request"),
1368+
reply("First reply"),
1369+
]
1370+
.map((record) => `${record}\n`)
1371+
.join("");
1372+
1373+
yield* writeTranscript({
1374+
filePath: path.join(sessionsDir, `rollout-2026-08-24T10-00-00-${threadId}.jsonl`),
1375+
contents: `${keptHistory}${prompt("Rewound request")}\n${reply("Rewound reply")}\n`,
1376+
mtimeMs: nowMs - 60_000,
1377+
});
1378+
yield* writeTranscript({
1379+
filePath: path.join(
1380+
sessionsDir,
1381+
`rollout-2026-08-24T11-00-00-${threadId}_${rolloutId}.jsonl`,
1382+
),
1383+
contents: [
1384+
encodeTranscriptRecord({
1385+
type: "session_meta",
1386+
payload: {
1387+
id: threadId,
1388+
cwd: workspace,
1389+
history_mode: "paginated",
1390+
history_base: {
1391+
thread_id: threadId,
1392+
end_ordinal_exclusive: 3,
1393+
end_byte_offset: Buffer.byteLength(keptHistory),
1394+
},
1395+
},
1396+
}),
1397+
prompt("Ask again"),
1398+
reply("Final reply"),
1399+
].join("\n"),
1400+
mtimeMs: nowMs,
1401+
});
1402+
1403+
const threads = yield* runRecentThreads({
1404+
claudeHomePath,
1405+
codexHomePath,
1406+
workspaceRoot: workspace,
1407+
});
1408+
1409+
expect(
1410+
threads.map((thread) => ({
1411+
id: thread.providerSessionId,
1412+
title: thread.title,
1413+
messages: thread.messages.map((message) => message.text),
1414+
})),
1415+
).toEqual([
1416+
{
1417+
id: threadId,
1418+
title: "Keep the first request",
1419+
messages: ["Keep the first request", "First reply", "Ask again", "Final reply"],
1420+
},
1421+
]);
1422+
}),
1423+
);
1424+
13401425
it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
13411426
Effect.gen(function* () {
13421427
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
@@ -120,6 +120,7 @@ const TranscriptRecord = Schema.Struct({
120120
message: Schema.optional(Schema.String),
121121
model: Schema.optional(Schema.String),
122122
cwd: Schema.optional(Schema.String),
123+
history_base: Schema.optional(Schema.Unknown),
123124
content: Schema.optional(Schema.Array(TranscriptContentBlock)),
124125
internal_chat_message_metadata_passthrough: Schema.optional(Schema.Unknown),
125126
}),
@@ -144,6 +145,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption(
144145
);
145146
const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);
146147
const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);
148+
const decodeCodexHistoryBase = Schema.decodeUnknownOption(
149+
Schema.Struct({ thread_id: Schema.String, end_byte_offset: Schema.Number }),
150+
);
151+
// Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
152+
const CODEX_ROLLOUT_ID_PATTERN =
153+
/[-_]([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
147154
const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
148155
Schema.fromJsonString(CodexSessionIndexEntry),
149156
);
@@ -335,6 +342,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
335342
return names;
336343
}
337344

345+
// A rewound paginated Codex thread continues in a new rollout that inherits a
346+
// byte prefix of the rollout it replaced.
347+
function codexHistoryBase(records: ReadonlyArray<DecodedTranscriptRecord>) {
348+
const base = Option.getOrUndefined(
349+
decodeCodexHistoryBase(
350+
records.find((record) => record.type === "session_meta")?.payload?.history_base,
351+
),
352+
);
353+
return base !== undefined &&
354+
Number.isSafeInteger(base.end_byte_offset) &&
355+
base.end_byte_offset >= 0
356+
? base
357+
: undefined;
358+
}
359+
338360
function deriveTitle(source: AgentSessionSource, text: string): string | undefined {
339361
const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text;
340362
return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined;
@@ -997,8 +1019,10 @@ export const make = Effect.gen(function* () {
9971019
filePath: string,
9981020
expected: ReturnType<typeof transcriptIdentity>,
9991021
source: AgentSessionSource,
1022+
prefix?: { readonly endByteOffset: number; readonly historyBytes: number },
10001023
) {
1001-
if (expected.size > MAX_IMPORTED_TRANSCRIPT_BYTES) return null;
1024+
const readBytes = prefix?.endByteOffset ?? expected.size;
1025+
if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) return null;
10021026

10031027
return yield* Effect.scoped(
10041028
fileSystem.open(filePath, { flag: "r" }).pipe(
@@ -1010,7 +1034,7 @@ export const make = Effect.gen(function* () {
10101034
const records: Array<DecodedTranscriptRecord> = [];
10111035
let cwd = Option.none<string>();
10121036
let claudeFields = emptyClaudeFields;
1013-
let historyBytes = 0;
1037+
let historyBytes = prefix?.historyBytes ?? 0;
10141038
let recordBytes = 0;
10151039
let recordCount = 0;
10161040
let bytesRead = 0;
@@ -1062,9 +1086,9 @@ export const make = Effect.gen(function* () {
10621086
return true;
10631087
};
10641088

1065-
while (bytesRead < expected.size) {
1089+
while (bytesRead < readBytes) {
10661090
const next = yield* file.readAlloc(
1067-
Math.min(TRANSCRIPT_PREFIX_BYTES, expected.size - bytesRead),
1091+
Math.min(TRANSCRIPT_PREFIX_BYTES, readBytes - bytesRead),
10681092
);
10691093
if (Option.isNone(next)) {
10701094
return null;
@@ -1089,7 +1113,7 @@ export const make = Effect.gen(function* () {
10891113

10901114
if (recordStarted && !(yield* Effect.try(finishRecord))) return null;
10911115
return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))
1092-
? { records, cwd, claudeFields }
1116+
? { records, cwd, claudeFields, historyBytes }
10931117
: null;
10941118
}),
10951119
),
@@ -1667,6 +1691,56 @@ export const make = Effect.gen(function* () {
16671691
const codexThreadNamesByHome = new Map<string, ReadonlyMap<string, string>>();
16681692
let bytesRemaining = MAX_IMPORT_BYTES;
16691693
let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS;
1694+
const codexRolloutPaths = new Map<ProviderInstanceId, ReadonlyMap<string, string>>();
1695+
const codexRolloutPath = (providerInstanceId: ProviderInstanceId, rolloutId: string) => {
1696+
let paths = codexRolloutPaths.get(providerInstanceId);
1697+
if (paths === undefined) {
1698+
paths = new Map(
1699+
candidates.flatMap((candidate) =>
1700+
candidate.source === "codex" && candidate.providerInstanceId === providerInstanceId
1701+
? candidate.transcripts.flatMap((transcript) => {
1702+
const id = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1];
1703+
return id === undefined ? [] : [[id, transcript.filePath] as const];
1704+
})
1705+
: [],
1706+
),
1707+
);
1708+
codexRolloutPaths.set(providerInstanceId, paths);
1709+
}
1710+
return paths.get(rolloutId);
1711+
};
1712+
// Reads the prefixes a rewound rollout inherits, oldest first. Without all of
1713+
// them the rollout is imported on its own, as before.
1714+
const readCodexHistoryBase = Effect.fn("AgentSessionScanner.readCodexHistoryBase")(function* (
1715+
providerInstanceId: ProviderInstanceId,
1716+
records: ReadonlyArray<DecodedTranscriptRecord>,
1717+
historyBytes: number,
1718+
) {
1719+
const prefixes: Array<ReadonlyArray<DecodedTranscriptRecord>> = [];
1720+
const visited = new Set<string>();
1721+
for (let base = codexHistoryBase(records); base !== undefined;) {
1722+
const filePath = visited.has(base.thread_id)
1723+
? undefined
1724+
: codexRolloutPath(providerInstanceId, base.thread_id);
1725+
if (filePath === undefined || base.end_byte_offset > bytesRemaining) return null;
1726+
visited.add(base.thread_id);
1727+
const stats = yield* statOption(filePath);
1728+
if (Option.isNone(stats) || stats.value.type !== "File") return null;
1729+
bytesRemaining -= base.end_byte_offset;
1730+
const inherited = yield* readTranscript(
1731+
filePath,
1732+
transcriptIdentity(filePath, stats.value),
1733+
"codex",
1734+
{ endByteOffset: base.end_byte_offset, historyBytes },
1735+
);
1736+
if (inherited === null) return null;
1737+
historyBytes = inherited.historyBytes;
1738+
// The rollout's own session_meta names the thread; inherited ones may name a fork parent.
1739+
prefixes.unshift(inherited.records.filter((record) => record.type !== "session_meta"));
1740+
base = codexHistoryBase(inherited.records);
1741+
}
1742+
return prefixes.flat();
1743+
});
16701744
return Stream.fromIteratorSucceed(eligibleTranscripts.values(), 1).pipe(
16711745
Stream.mapEffect(({ candidate, transcript }) =>
16721746
Effect.gen(function* () {
@@ -1719,14 +1793,24 @@ export const make = Effect.gen(function* () {
17191793
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
17201794
}
17211795

1796+
const inheritedRecords =
1797+
candidate.source === "codex"
1798+
? yield* readCodexHistoryBase(
1799+
candidate.providerInstanceId,
1800+
snapshot.records,
1801+
snapshot.historyBytes,
1802+
)
1803+
: null;
17221804
const parsedThread = parseAgentSessionRecords(
17231805
{
17241806
source: candidate.source,
17251807
providerInstanceId: candidate.providerInstanceId,
17261808
fallbackSessionId: path.basename(transcript.filePath, ".jsonl"),
17271809
lastActiveAtMs: transcript.mtimeMs,
17281810
},
1729-
snapshot.records,
1811+
inheritedRecords === null
1812+
? snapshot.records
1813+
: [...inheritedRecords, ...snapshot.records],
17301814
snapshot.claudeFields,
17311815
);
17321816
if (parsedThread === null) {

0 commit comments

Comments
 (0)