Skip to content

Commit 2b59d9f

Browse files
t3dotggclaude
andcommitted
fix(client): an opened idle subagent reads its family once
Opening a thread sends a visit, so its row comes back as a delta. With its idle parent left out, the row then read the family a second time under the root's id, next to the read the open thread had mounted. On one real database the largest nested family is 432 rows and 811 KB. The read for a missing parent now uses the child's id, one child per root, so it is the read the open thread already made. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent ad29ee5 commit 2b59d9f

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

‎packages/client-runtime/src/state/threadFamilies.test.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,26 @@ describe("createThreadFamilyAtoms", () => {
235235
expect(threadFamilyRead(registry.get(family))).toBe("settled");
236236
expect(mergedIds()).toEqual(["parent", "idle-child"]);
237237

238-
// A child that woke after the shell left out its idle parent reads its family unmounted.
238+
// Children that woke after the shell left out their idle parent arrive as deltas, and
239+
// read their family unmounted, once for the root.
239240
release();
240241
const woken = row("woken", "woken", child.id, parent.id);
241-
registry.set(shellState, AsyncResult.success(shellAt(3, [parent, woken])));
242+
const sibling = row("woken-sibling", "woken-sibling", child.id, parent.id);
243+
registry.set(shellState, AsyncResult.success(shellAt(2, [parent, woken, sibling])));
242244
yield* Queue.offer(
243245
replies,
244-
Effect.succeed({ snapshotSequence: 6, threads: [parent, child, woken] }),
246+
Effect.succeed({ snapshotSequence: 6, threads: [parent, child, woken, sibling] }),
245247
);
246-
yield* settle;
247-
expect(mergedIds()).toEqual(["parent", "woken", "idle-child"]);
248+
const wokenFamily = families.familyAtom(TARGET.environmentId, woken.id);
249+
yield* AtomRegistry.getResult(registry, wokenFamily, { suspendOnWaiting: true }).pipe(
250+
Effect.tap(() => Effect.yieldNow),
251+
);
252+
expect(mergedIds()).toEqual(["parent", "woken", "woken-sibling", "idle-child"]);
248253

249-
// That read is settled, but a caller waiting on its own mount sees it only once mounted.
250-
const mountedRead = families.mountedReadAtom(TARGET.environmentId, parent.id);
251-
expect(threadFamilyRead(registry.get(family))).toBe("settled");
254+
// Opening the woken child mounts that same read. It answers only once it is mounted.
255+
const mountedRead = families.mountedReadAtom(TARGET.environmentId, woken.id);
252256
expect(registry.get(mountedRead)).toBe("pending");
253-
families.mount(registry, TARGET.environmentId, parent.id);
257+
families.mount(registry, TARGET.environmentId, woken.id);
254258
expect(registry.get(mountedRead)).toBe("settled");
255259
}),
256260
),

‎packages/client-runtime/src/state/threadFamilies.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,25 @@ export function threadFamilyRowsMissingFromShell(input: {
6666
}
6767

6868
/**
69-
* Roots of subagents and side chats whose parent row the shell left out. A shell sent with
70-
* `omitIdleSubagentParents` drops idle chains, so a child that wakes later arrives without them.
69+
* Subagents and side chats whose parent row the shell left out, one per root. A shell sent with
70+
* `omitIdleSubagentParents` drops idle chains, so a child that wakes or is opened later arrives
71+
* without them. The family is read by the child, the read an open thread's mount already made.
7172
*/
72-
function rootsWithMissingParents(
73+
function threadsWithMissingParents(
7374
threads: ReadonlyArray<OrchestrationV2ThreadShell>,
7475
): ReadonlyArray<ThreadId> {
7576
const present = new Set<ThreadId>(threads.map((thread) => thread.id));
76-
const roots = new Set<ThreadId>();
77-
for (const { lineage } of threads) {
77+
const byRoot = new Map<ThreadId, ThreadId>();
78+
for (const { id, lineage } of threads) {
7879
if (lineage.parentThreadId === null || present.has(lineage.parentThreadId)) continue;
79-
if (lineage.relationshipToParent === "subagent" || lineage.sideChat === true) {
80-
roots.add(lineage.rootThreadId);
80+
if (
81+
(lineage.relationshipToParent === "subagent" || lineage.sideChat === true) &&
82+
!byRoot.has(lineage.rootThreadId)
83+
) {
84+
byRoot.set(lineage.rootThreadId, id);
8185
}
8286
}
83-
return [...roots];
87+
return [...byRoot.values()];
8488
}
8589

8690
/**
@@ -241,8 +245,8 @@ export function createThreadFamilyAtoms<R, ER, E>(
241245
if (orphans?.threads !== snapshot.threads) {
242246
orphans = {
243247
threads: snapshot.threads,
244-
rows: rootsWithMissingParents(snapshot.threads).map((root) =>
245-
familyRowsAtom(familyKey(environmentId, root)),
248+
rows: threadsWithMissingParents(snapshot.threads).map((threadId) =>
249+
familyRowsAtom(familyKey(environmentId, threadId)),
246250
),
247251
};
248252
}

0 commit comments

Comments
 (0)