diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 0fcab0d7ad4c3..7b5cfaae44080 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1766,6 +1766,63 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("imports older history behind 100 unreadable sessions", () => + 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 sessions = path.join(claudeHomePath, "pi-agent", "sessions", "--workspace--"); + // Nesting past the JSON reader's depth limit makes a transcript unreadable. + const tooDeep = "[".repeat(130) + "]".repeat(130); + for (let index = 0; index < 100; index++) { + const header = encodeTranscriptRecord({ + type: "session", + version: 3, + id: `unreadable-${index}`, + cwd: workspace, + }); + yield* writeTranscript({ + filePath: path.join(sessions, `unreadable-${index}.jsonl`), + contents: `${header}\n${tooDeep}\n`, + mtimeMs: nowMs - (index + 1) * 1_000, + }); + } + yield* writeTranscript({ + filePath: path.join(sessions, "older.jsonl"), + contents: + [ + { type: "session", version: 3, id: "older", cwd: workspace }, + { + type: "message", + id: "a1", + parentId: null, + message: { role: "user", content: "Review this change" }, + }, + { + type: "message", + id: "a2", + parentId: "a1", + message: { role: "assistant", content: [{ type: "text", text: "Looks good" }] }, + }, + ] + .map((record) => encodeTranscriptRecord(record)) + .join("\n") + "\n", + mtimeMs: nowMs - 200_000, + }); + + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + }); + + expect(threads.map((thread) => thread.providerSessionId)).toEqual(["older"]); + }), + ); + it.effect("keeps the provider instance that owns a custom session home", () => 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 745d60e6f2c26..b289af999fed2 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -1344,11 +1344,16 @@ export const make = Effect.gen(function* () { filePath: string, expected: ReturnType, source: AgentSessionSource, + decompressionBudget: { bytesRemaining: number }, prefix?: { readonly endByteOffset: number; readonly historyBytes: number }, ) { const compressed = isCompressedTranscript(filePath); + const readToEnd = compressed && prefix === undefined; const readBytes = - prefix?.endByteOffset ?? (compressed ? MAX_IMPORTED_TRANSCRIPT_BYTES : expected.size); + prefix?.endByteOffset ?? + (compressed + ? Math.min(MAX_IMPORTED_TRANSCRIPT_BYTES, decompressionBudget.bytesRemaining) + : expected.size); if ((!compressed && readBytes > expected.size) || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) { return null; } @@ -1422,11 +1427,12 @@ export const make = Effect.gen(function* () { ); if (Option.isNone(next)) { // A compressed rollout's decompressed length is only known at its end. - if (compressed && prefix === undefined) break; + if (readToEnd) break; return null; } bytesRead += next.value.byteLength; + if (compressed) decompressionBudget.bytesRemaining -= next.value.byteLength; const withinBudget = yield* Effect.try({ try: () => { let start = 0; @@ -1446,6 +1452,11 @@ export const make = Effect.gen(function* () { if (!withinBudget) return null; } + // A full rollout that fills the budget is only usable if it ends here. + if (readToEnd && bytesRead === readBytes && Option.isSome(yield* readChunk(1))) { + return null; + } + if ( recordStarted && !(yield* Effect.try({ @@ -2204,6 +2215,7 @@ export const make = Effect.gen(function* () { const importedSessions = new Set(); const codexThreadNamesByHome = new Map>(); let bytesRemaining = MAX_IMPORT_BYTES; + const decompressionBudget = { bytesRemaining: MAX_IMPORT_BYTES }; let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS; const codexRolloutPaths = new Map>(); const codexRolloutPath = (providerInstanceId: ProviderInstanceId, rolloutId: string) => { @@ -2237,15 +2249,22 @@ export const make = Effect.gen(function* () { const filePath = visited.has(base.thread_id) ? undefined : codexRolloutPath(providerInstanceId, base.thread_id); - if (filePath === undefined || base.end_byte_offset > bytesRemaining) return null; + if (filePath === undefined) return null; + const compressed = isCompressedTranscript(filePath); + if ( + base.end_byte_offset > (compressed ? decompressionBudget.bytesRemaining : 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; + if (!compressed) bytesRemaining -= base.end_byte_offset; const inherited = yield* readTranscript( filePath, transcriptIdentity(filePath, stats.value), "codex", + decompressionBudget, { endByteOffset: base.end_byte_offset, historyBytes }, ); if (inherited === null) return null; @@ -2286,13 +2305,19 @@ export const make = Effect.gen(function* () { if ( transcriptsRemaining === 0 || identity.size > MAX_IMPORTED_TRANSCRIPT_BYTES || - identity.size > bytesRemaining + identity.size > bytesRemaining || + (isCompressedTranscript(transcript.filePath) && + decompressionBudget.bytesRemaining === 0) ) { return Option.some({ _tag: "Skipped" }); } - transcriptsRemaining -= 1; bytesRemaining -= identity.size; - const snapshot = yield* readTranscript(transcript.filePath, identity, candidate.source); + const snapshot = yield* readTranscript( + transcript.filePath, + identity, + candidate.source, + decompressionBudget, + ); if (snapshot === null) { return Option.some({ _tag: "Skipped" }); } @@ -2340,13 +2365,14 @@ export const make = Effect.gen(function* () { if (candidate.source === "codex" && inheritedRecords === null) { return Option.some({ _tag: "Skipped" }); } - // Never recorded as imported, so charging them would starve real sessions every run. - transcriptsRemaining += 1; return Option.none(); } if (parsedThread === null) { return Option.some({ _tag: "Skipped" }); } + // Unusable transcripts still cost bytes, but charging a slot would block older + // sessions on every retry because those files are never recorded as imported. + transcriptsRemaining -= 1; let savedTitle: string | undefined; if (candidate.source === "codex") { const names =