Skip to content
31 changes: 31 additions & 0 deletions packages/client-runtime/src/state/threadShell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [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 = {
Expand Down Expand Up @@ -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();
});
Expand Down
16 changes: 15 additions & 1 deletion packages/client-runtime/src/state/threadShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<EnvironmentThreadShell> = [];
return Atom.make((get) => {
if (!get(hasChildThreadInputsAtom)) {
if (previous.length !== 0) previous = [];
return previous;
}
const children = new Map<
string,
Array<{ environmentId: EnvironmentId; thread: OrchestrationV2ThreadShell }>
Expand Down Expand Up @@ -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))),
);
Expand Down
Loading