Skip to content

Commit 8413686

Browse files
fix(server): filter Codex subagents before import candidate selection
1 parent 05d2352 commit 8413686

2 files changed

Lines changed: 54 additions & 60 deletions

File tree

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

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

1557-
it.effect("lists top-level Codex sessions but not the subagents they spawned", () =>
1557+
it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
15581558
Effect.gen(function* () {
15591559
const path = yield* Path.Path;
15601560
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
15611561
yield* TestClock.setTime(nowMs);
15621562
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
15631563
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
15641564
const workspace = yield* makeTempDir("t3code-workspace-");
1565-
const sessions = [
1566-
["parent", "vscode"],
1567-
["no-source", undefined],
1568-
["null-source", null],
1569-
["object-source", { custom: "launcher" }],
1570-
[
1571-
"spawned",
1572-
{
1573-
subagent: {
1574-
thread_spawn: { parent_thread_id: "parent", depth: 1, agent_path: "/root/worker" },
1575-
},
1576-
},
1577-
],
1578-
["review", { subagent: "review" }],
1579-
["guardian", { subagent: { other: "guardian" } }],
1580-
["compact", { subAgent: "compact" }],
1581-
] as const;
1582-
1583-
for (const [index, [id, source]] of sessions.entries()) {
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()) {
15841580
yield* writeTranscript({
15851581
filePath: path.join(
15861582
codexHomePath,
15871583
"sessions",
15881584
"2026",
15891585
"08",
15901586
"24",
1591-
`rollout-${id}.jsonl`,
1587+
`rollout-${index}.jsonl`,
15921588
),
15931589
contents: [
1594-
encodeTranscriptRecord({
1595-
type: "session_meta",
1596-
payload: { id, cwd: workspace, ...(source === undefined ? {} : { source }) },
1597-
}),
1598-
// Codex repeats the cwd on every turn, so the skip must not fall through to it.
1599-
encodeTranscriptRecord({ type: "turn_context", payload: { cwd: workspace } }),
1600-
encodeTranscriptRecord({
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+
{
16011594
type: "event_msg",
1602-
payload: { type: "user_message", message: `Prompt for ${id}` },
1603-
}),
1604-
].join("\n"),
1605-
mtimeMs: nowMs - (index + 1) * 60_000,
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,
16061601
});
16071602
}
16081603

1609-
// One scanner instance, so the import reuses the candidates cached by the scan.
1610-
const { scan, outcomes } = yield* Effect.gen(function* () {
1604+
yield* Effect.gen(function* () {
16111605
const scanner = yield* AgentSessionScanner.AgentSessionScanner;
16121606
const scan = yield* scanner.scan;
1607+
expect.soft(scan.candidates.map((candidate) => candidate.threadCount)).toEqual([4]);
16131608
const outcomes = yield* Stream.runCollect(scanner.recentThreads(workspace));
1614-
return { scan, outcomes: Array.from(outcomes) };
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"]);
16151614
}).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })));
1616-
1617-
expect(scan.candidates.map((candidate) => candidate.threadCount)).toEqual([4]);
1618-
expect(
1619-
outcomes.map((outcome) =>
1620-
outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag,
1621-
),
1622-
).toEqual(["parent", "no-source", "null-source", "object-source"]);
16231615
}),
16241616
);
16251617

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

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

528-
/**
529-
* Returns an empty string for Codex subagent rollouts so the scan stops there
530-
* and skips them: they belong to the parent conversation, not the user.
531-
*/
532-
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 {
533530
let parsed: unknown;
534531
try {
535532
parsed = JSON.parse(line);
@@ -544,15 +541,17 @@ function extractCwd(line: string): string | null {
544541
}
545542
const payload = record.payload;
546543
if (typeof payload === "object" && payload !== null) {
547-
// Top-level Codex sessions record a string source such as "cli" or `{ custom }`.
548-
// Spawned, review, compact and other agents record `{ subagent: ... }` on disk,
549-
// spelled `subAgent` in the app-server schema.
550-
const source = (payload as Record<string, unknown>).source;
551-
const isSubagent =
552-
typeof source === "object" &&
553-
source !== null &&
554-
("subagent" in source || "subAgent" in source);
555-
if (record.type === "session_meta" && isSubagent) return "";
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+
}
556555
const nested = (payload as Record<string, unknown>).cwd;
557556
if (typeof nested === "string" && nested.trim().length > 0) {
558557
return nested;
@@ -687,6 +686,7 @@ export const make = Effect.gen(function* () {
687686
});
688687

689688
const readCwd = Effect.fn("AgentSessionScanner.readCwd")(function* (
689+
source: AgentSessionSource,
690690
transcript: TranscriptCandidate,
691691
budget: MetadataReadBudget,
692692
) {
@@ -723,7 +723,9 @@ export const make = Effect.gen(function* () {
723723
};
724724
const readLastRecord = () => {
725725
const record = remaining + decoder.decode();
726-
return record.length === 0 || !reserveRecord() ? null : extractCwd(record.trim());
726+
return record.length === 0 || !reserveRecord()
727+
? null
728+
: extractCwd(record.trim(), source);
727729
};
728730

729731
while (bytesRead < maxBytes) {
@@ -750,7 +752,7 @@ export const make = Effect.gen(function* () {
750752

751753
for (const line of lines) {
752754
if (!reserveRecord()) return null;
753-
const cwd = extractCwd(line.trim());
755+
const cwd = extractCwd(line.trim(), source);
754756
if (cwd !== null) return cwd;
755757
}
756758
}
@@ -999,8 +1001,8 @@ export const make = Effect.gen(function* () {
9991001
>();
10001002

10011003
for (const transcript of transcripts) {
1002-
const cwd = yield* readCwd(transcript, budget);
1003-
if (!cwd) continue;
1004+
const cwd = yield* readCwd(source, transcript, budget);
1005+
if (cwd === null || cwd === false) continue;
10041006
const key = `${transcript.providerInstanceId}\0${cwd}`;
10051007
const existing = byOwnerAndCwd.get(key);
10061008
if (existing) {

0 commit comments

Comments
 (0)