Skip to content

Commit 002ef36

Browse files
fix(server): session import skips Codex subagent sessions (pingdotgg#16662)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 38d50e3 commit 002ef36

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,67 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
15541554
}),
15551555
);
15561556

1557+
it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
1558+
Effect.gen(function* () {
1559+
const path = yield* Path.Path;
1560+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1561+
yield* TestClock.setTime(nowMs);
1562+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1563+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1564+
const workspace = yield* makeTempDir("t3code-workspace-");
1565+
const subagentSources = [
1566+
{ subagent: { thread_spawn: { parent_thread_id: "session-100", depth: 1 } } },
1567+
{ subagent: "review" },
1568+
{ subagent: { other: "guardian" } },
1569+
{ subAgent: "compact" },
1570+
];
1571+
// Newer subagents would exhaust the 100-transcript import budget before any parent.
1572+
const sources = [
1573+
...Array.from({ length: 100 }, (_, index) => subagentSources[index % 4]),
1574+
"vscode",
1575+
undefined,
1576+
null,
1577+
{ custom: "launcher" },
1578+
];
1579+
for (const [index, source] of sources.entries()) {
1580+
yield* writeTranscript({
1581+
filePath: path.join(
1582+
codexHomePath,
1583+
"sessions",
1584+
"2026",
1585+
"08",
1586+
"24",
1587+
`rollout-${index}.jsonl`,
1588+
),
1589+
contents: [
1590+
{ type: "session_meta", payload: { id: `session-${index}`, cwd: workspace, source } },
1591+
// A later cwd must not make an excluded rollout eligible again.
1592+
{ type: "turn_context", payload: { cwd: workspace } },
1593+
{
1594+
type: "event_msg",
1595+
payload: { type: "user_message", message: "Import this prompt" },
1596+
},
1597+
]
1598+
.map((record) => encodeTranscriptRecord(record))
1599+
.join("\n"),
1600+
mtimeMs: nowMs - index * 60_000,
1601+
});
1602+
}
1603+
1604+
yield* Effect.gen(function* () {
1605+
const scanner = yield* AgentSessionScanner.AgentSessionScanner;
1606+
const scan = yield* scanner.scan;
1607+
expect.soft(scan.candidates.map((candidate) => candidate.threadCount)).toEqual([4]);
1608+
const outcomes = yield* Stream.runCollect(scanner.recentThreads(workspace));
1609+
expect(
1610+
outcomes.map((outcome) =>
1611+
outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag,
1612+
),
1613+
).toEqual(["session-100", "session-101", "session-102", "session-103"]);
1614+
}).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })));
1615+
}),
1616+
);
1617+
15571618
it.effect("suppresses duplicate session copies without reporting a skipped import", () =>
15581619
Effect.gen(function* () {
15591620
const path = yield* Path.Path;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ function isT3ManagedWorktree(
525525
);
526526
}
527527

528-
function extractCwd(line: string): string | null {
528+
// false stops metadata scanning for an excluded rollout, even if later records contain a cwd.
529+
function extractCwd(line: string, source: AgentSessionSource): string | null | false {
529530
let parsed: unknown;
530531
try {
531532
parsed = JSON.parse(line);
@@ -540,6 +541,17 @@ function extractCwd(line: string): string | null {
540541
}
541542
const payload = record.payload;
542543
if (typeof payload === "object" && payload !== null) {
544+
if (source === "codex" && record.type === "session_meta") {
545+
const sessionSource = (payload as Record<string, unknown>).source;
546+
if (
547+
typeof sessionSource === "object" &&
548+
sessionSource !== null &&
549+
!Array.isArray(sessionSource) &&
550+
(Object.hasOwn(sessionSource, "subagent") || Object.hasOwn(sessionSource, "subAgent"))
551+
) {
552+
return false;
553+
}
554+
}
543555
const nested = (payload as Record<string, unknown>).cwd;
544556
if (typeof nested === "string" && nested.trim().length > 0) {
545557
return nested;
@@ -674,6 +686,7 @@ export const make = Effect.gen(function* () {
674686
});
675687

676688
const readCwd = Effect.fn("AgentSessionScanner.readCwd")(function* (
689+
source: AgentSessionSource,
677690
transcript: TranscriptCandidate,
678691
budget: MetadataReadBudget,
679692
) {
@@ -710,7 +723,9 @@ export const make = Effect.gen(function* () {
710723
};
711724
const readLastRecord = () => {
712725
const record = remaining + decoder.decode();
713-
return record.length === 0 || !reserveRecord() ? null : extractCwd(record.trim());
726+
return record.length === 0 || !reserveRecord()
727+
? null
728+
: extractCwd(record.trim(), source);
714729
};
715730

716731
while (bytesRead < maxBytes) {
@@ -737,7 +752,7 @@ export const make = Effect.gen(function* () {
737752

738753
for (const line of lines) {
739754
if (!reserveRecord()) return null;
740-
const cwd = extractCwd(line.trim());
755+
const cwd = extractCwd(line.trim(), source);
741756
if (cwd !== null) return cwd;
742757
}
743758
}
@@ -986,8 +1001,8 @@ export const make = Effect.gen(function* () {
9861001
>();
9871002

9881003
for (const transcript of transcripts) {
989-
const cwd = yield* readCwd(transcript, budget);
990-
if (cwd === null) continue;
1004+
const cwd = yield* readCwd(source, transcript, budget);
1005+
if (cwd === null || cwd === false) continue;
9911006
const key = `${transcript.providerInstanceId}\0${cwd}`;
9921007
const existing = byOwnerAndCwd.get(key);
9931008
if (existing) {

0 commit comments

Comments
 (0)