TypeScript
fix(server): session import skips Codex subagent sessions (#16662)
Igor Makowski002ef36521c1
diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.tsindex 797fdf02925..29093265d15 100644--- a/apps/server/src/project/AgentSessionScanner.test.ts+++ b/apps/server/src/project/AgentSessionScanner.test.ts@@ -1554,6 +1554,67 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {}),);+ it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>+ 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 subagentSources = [+ { subagent: { thread_spawn: { parent_thread_id: "session-100", depth: 1 } } },+ { subagent: "review" },+ { subagent: { other: "guardian" } },+ { subAgent: "compact" },+ ];+ // Newer subagents would exhaust the 100-transcript import budget before any parent.+ const sources = [+ ...Array.from({ length: 100 }, (_, index) => subagentSources[index % 4]),+ "vscode",+ undefined,+ null,+ { custom: "launcher" },+ ];+ for (const [index, source] of sources.entries()) {+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ `rollout-${index}.jsonl`,+ ),+ contents: [+ { type: "session_meta", payload: { id: `session-${index}`, cwd: workspace, source } },+ // A later cwd must not make an excluded rollout eligible again.+ { type: "turn_context", payload: { cwd: workspace } },+ {+ type: "event_msg",+ payload: { type: "user_message", message: "Import this prompt" },+ },+ ]+ .map((record) => encodeTranscriptRecord(record))+ .join("\n"),+ mtimeMs: nowMs - index * 60_000,+ });+ }++ yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const scan = yield* scanner.scan;+ expect.soft(scan.candidates.map((candidate) => candidate.threadCount)).toEqual([4]);+ const outcomes = yield* Stream.runCollect(scanner.recentThreads(workspace));+ expect(+ outcomes.map((outcome) =>+ outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag,+ ),+ ).toEqual(["session-100", "session-101", "session-102", "session-103"]);+ }).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })));+ }),+ );+it.effect("suppresses duplicate session copies without reporting a skipped import", () =>Effect.gen(function* () {const path = yield* Path.Path;diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.tsindex 5b22f874b6e..85fabef9d7e 100644--- a/apps/server/src/project/AgentSessionScanner.ts+++ b/apps/server/src/project/AgentSessionScanner.ts@@ -525,7 +525,8 @@ function isT3ManagedWorktree();}-function extractCwd(line: string): string | null {+// false stops metadata scanning for an excluded rollout, even if later records contain a cwd.+function extractCwd(line: string, source: AgentSessionSource): string | null | false {let parsed: unknown;try {parsed = JSON.parse(line);@@ -540,6 +541,17 @@ function extractCwd(line: string): string | null {}const payload = record.payload;if (typeof payload === "object" && payload !== null) {+ if (source === "codex" && record.type === "session_meta") {+ const sessionSource = (payload as Record<string, unknown>).source;+ if (+ typeof sessionSource === "object" &&+ sessionSource !== null &&+ !Array.isArray(sessionSource) &&+ (Object.hasOwn(sessionSource, "subagent") || Object.hasOwn(sessionSource, "subAgent"))+ ) {+ return false;+ }+ }const nested = (payload as Record<string, unknown>).cwd;if (typeof nested === "string" && nested.trim().length > 0) {return nested;@@ -674,6 +686,7 @@ export const make = Effect.gen(function* () {});const readCwd = Effect.fn("AgentSessionScanner.readCwd")(function* (+ source: AgentSessionSource,transcript: TranscriptCandidate,budget: MetadataReadBudget,) {@@ -710,7 +723,9 @@ export const make = Effect.gen(function* () {};const readLastRecord = () => {const record = remaining + decoder.decode();- return record.length === 0 || !reserveRecord() ? null : extractCwd(record.trim());+ return record.length === 0 || !reserveRecord()+ ? null+ : extractCwd(record.trim(), source);};while (bytesRead < maxBytes) {@@ -737,7 +752,7 @@ export const make = Effect.gen(function* () {for (const line of lines) {if (!reserveRecord()) return null;- const cwd = extractCwd(line.trim());+ const cwd = extractCwd(line.trim(), source);if (cwd !== null) return cwd;}}@@ -986,8 +1001,8 @@ export const make = Effect.gen(function* () {>();for (const transcript of transcripts) {- const cwd = yield* readCwd(transcript, budget);- if (cwd === null) continue;+ const cwd = yield* readCwd(source, transcript, budget);+ if (cwd === null || cwd === false) continue;const key = `${transcript.providerInstanceId}\0${cwd}`;const existing = byOwnerAndCwd.get(key);if (existing) {