diff --git a/packages/client-runtime/src/state/threadShell.test.ts b/packages/client-runtime/src/state/threadShell.test.ts index de1bc04aeda8a..9210949afb737 100644 --- a/packages/client-runtime/src/state/threadShell.test.ts +++ b/packages/client-runtime/src/state/threadShell.test.ts @@ -61,6 +61,35 @@ function makeHarness(environmentIds: ReadonlyArray = [environment } describe("v2 thread shell lists", () => { + it("builds no child index while no child asks for input", () => { + const { registry, threads, snapshotAtom } = makeHarness(); + let parentIdReads = 0; + const child = { + ...v2ThreadShell, + id: ThreadId.make("child"), + creationSource: "mcp" as const, + lineage: { + rootThreadId: v2ThreadShell.id, + relationshipToParent: "subagent" as const, + // Nothing is pending, so only the child index would read parent ids. + get parentThreadId() { + parentIdReads++; + return v2ThreadShell.id; + }, + }, + }; + const inputs = threads.childThreadInputsAtom({ environmentId, threadId: v2ThreadShell.id }); + const dispose = registry.mount(inputs); + registry.set(snapshotAtom(environmentId), { + ...v2ShellSnapshot, + threads: [v2ThreadShell, child], + }); + expect(registry.get(inputs)).toEqual([]); + expect(parentIdReads).toBe(0); + dispose(); + registry.dispose(); + }); + it("preserves ordered reference arrays when a middle thread changes", () => { const { registry, threads, snapshotAtom } = makeHarness(); const snapshot = { @@ -160,6 +189,8 @@ describe("v2 thread shell lists", () => { threads: [root, { ...legacyChild, status: "completed" as const }], }); expect(registry.get(inputs)).toEqual([]); + registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] }); + expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]); dispose(); registry.dispose(); }); diff --git a/packages/client-runtime/src/state/threadShell.ts b/packages/client-runtime/src/state/threadShell.ts index d5c60570b0a18..3325346d5d153 100644 --- a/packages/client-runtime/src/state/threadShell.ts +++ b/packages/client-runtime/src/state/threadShell.ts @@ -147,6 +147,9 @@ function sideChatRequest(thread: OrchestrationV2ThreadShell): PendingSubagentReq return kind === "user_input" ? "user_input" : "approval"; } +const asksParent = (thread: OrchestrationV2ThreadShell) => + subagentRequest(thread) !== null || sideChatRequest(thread) !== null; + /** * The threads to flag for a subagent's live approval, up its * subagent chain and through a side chat to its parent. `delegated` are tasks this @@ -496,12 +499,23 @@ export function createEnvironmentThreadShellAtoms(input: { }).pipe(Atom.withLabel(`environment-quiet-thread-shell:${key}`)); }); + const hasChildThreadInputsAtom = Atom.make((get) => { + for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) { + if (get(environmentThreadsAtom(environmentId)).some(asksParent)) return true; + } + return false; + }).pipe(Atom.withLabel("environment-has-child-thread-inputs")); + // Child approvals and questions are hidden with their sidebar rows. Read their // shell summaries, without subscribing to every child transcript. A task // delegated to a linked environment is a child too, with its own subagents. const childThreadInputsAtomFamily = Atom.family((key: string) => { let previous: ReadonlyArray = []; return Atom.make((get) => { + if (!get(hasChildThreadInputsAtom)) { + if (previous.length !== 0) previous = []; + return previous; + } const children = new Map< string, Array<{ environmentId: EnvironmentId; thread: OrchestrationV2ThreadShell }> @@ -537,7 +551,7 @@ export function createEnvironmentThreadShellAtoms(input: { if (seen.has(childKey)) continue; seen.add(childKey); pending.push(childKey); - if (subagentRequest(thread) !== null || sideChatRequest(thread) !== null) { + if (asksParent(thread)) { next.push( scopedThread(environmentId, thread, get(pendingSubagentRequestsAtom(environmentId))), );