Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadShell.test.ts

782 lines29.5 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 RunId,
5 RuntimeRequestId,
6 ThreadId,
7 type OrchestrationV2ShellSnapshot,
8 type OrchestrationV2ThreadShell,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11import * as Option from "effect/Option";
12import { Atom, AtomRegistry } from "effect/reactivity";
13import { describe, expect, it } from "vite-plus/test";
14​
15import { PrimaryConnectionTarget } from "../connection/model.ts";
16import { v2Now, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
17import { applyShellStreamEvent } from "./shellReducer.ts";
18import { isThreadWorking } from "./threadInbox.ts";
19import {
20 childThreadInputsTitle,
21 createEnvironmentThreadShellAtoms,
22 resolveListedAncestorThreadRef,
23} from "./threadShell.ts";
24​
25const environmentId = EnvironmentId.make("environment-v2");
26const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2");
27const otherProjectId = ProjectId.make("other-project");
28​
29function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) {
30 const snapshotAtom = Atom.family((_environmentId: EnvironmentId) =>
31 Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot),
32 );
33 const catalogValueAtom = Atom.make({
34 isReady: true,
35 entries: new Map(
36 environmentIds.map((id) => [
37 id,
38 {
39 target: new PrimaryConnectionTarget({
40 environmentId: id,
41 label: "Environment",
42 httpBaseUrl: "https://example.test",
43 wsBaseUrl: "wss://example.test",
44 }),
45 profile: Option.none(),
46 enabled: true,
47 },
48 ]),
49 ),
50 });
51 return {
52 registry: AtomRegistry.make(),
53 snapshotAtom,
54 catalogValueAtom,
55 threads: createEnvironmentThreadShellAtoms({
56 catalogValueAtom,
57 snapshotAtom,
58 hiddenThreadTagsAtom: () => Atom.make(null),
59 }),
60 };
62​
63describe("v2 thread shell lists", () => {
64 it("builds no child index while no child asks for input", () => {
65 const { registry, threads, snapshotAtom } = makeHarness();
66 let parentIdReads = 0;
67 const child = {
68 ...v2ThreadShell,
69 id: ThreadId.make("child"),
70 creationSource: "mcp" as const,
71 lineage: {
72 rootThreadId: v2ThreadShell.id,
73 relationshipToParent: "subagent" as const,
74 // Nothing is pending, so only the child index would read parent ids.
75 get parentThreadId() {
76 parentIdReads++;
77 return v2ThreadShell.id;
78 },
79 },
80 };
81 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: v2ThreadShell.id });
82 const dispose = registry.mount(inputs);
83 registry.set(snapshotAtom(environmentId), {
84 ...v2ShellSnapshot,
85 threads: [v2ThreadShell, child],
86 });
87 expect(registry.get(inputs)).toEqual([]);
88 expect(parentIdReads).toBe(0);
89 dispose();
90 registry.dispose();
91 });
92​
93 it("preserves ordered reference arrays when a middle thread changes", () => {
94 const { registry, threads, snapshotAtom } = makeHarness();
95 const snapshot = {
96 ...v2ShellSnapshot,
97 threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })),
98 };
99 registry.set(snapshotAtom(environmentId), snapshot);
100 const dispose = registry.mount(threads.threadRefsAtom);
101 const before = registry.get(threads.threadRefsAtom);
102 registry.set(
103 snapshotAtom(environmentId),
104 applyShellStreamEvent(snapshot, {
105 kind: "thread.updated",
106 location: "active",
107 sequence: 1,
108 thread: { ...snapshot.threads[1]!, title: "Updated" },
109 }),
110 );
111 expect(registry.get(threads.threadRefsAtom)).toBe(before);
112 dispose();
113 registry.dispose();
114 });
115​
116 it("keeps navigation stable on hidden subagent updates and retains user forks", () => {
117 const { registry, threads, snapshotAtom } = makeHarness();
118 const root = v2ThreadShell;
119 const child = {
120 ...root,
121 id: ThreadId.make("child"),
122 lineage: {
123 ...root.lineage,
124 parentThreadId: root.id,
125 relationshipToParent: "subagent" as const,
126 },
127 };
128 const fork = {
129 ...child,
130 id: ThreadId.make("fork"),
131 lineage: { ...child.lineage, relationshipToParent: "fork" as const },
132 };
133 const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] };
134 registry.set(snapshotAtom(environmentId), snapshot);
135 const dispose = registry.mount(threads.navigationThreadShellsAtom);
136 const before = registry.get(threads.navigationThreadShellsAtom);
137 expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]);
138 registry.set(snapshotAtom(environmentId), {
139 ...snapshot,
140 threads: [root, { ...child, title: "Child streaming" }, fork],
141 });
142 expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before);
143 expect(registry.get(threads.threadShellsAtom)).toHaveLength(3);
144 dispose();
145 registry.dispose();
146 });
147​
148 it("keeps a live subagent question inside the parent without flagging its thread", () => {
149 const { registry, threads, snapshotAtom } = makeHarness();
150 const root = v2ThreadShell;
151 const child = {
152 ...root,
153 id: ThreadId.make("child"),
154 creationSource: "mcp" as const,
155 status: "running" as const,
156 activityRunStatus: "running" as const,
157 lineage: {
158 ...root.lineage,
159 parentThreadId: root.id,
160 relationshipToParent: "subagent" as const,
161 },
162 pendingRuntimeRequest: {
163 id: RuntimeRequestId.make("question"),
164 kind: "user_input" as const,
165 createdAt: v2Now,
166 },
167 };
168 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id });
169 const dispose = registry.mount(inputs);
170 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] });
171 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
172 expect(
173 registry.get(threads.threadShellAtom({ environmentId, threadId: root.id }))
174 ?.pendingSubagentRequest,
175 ).toBeNull();
176 registry.set(snapshotAtom(environmentId), {
177 ...v2ShellSnapshot,
178 threads: [root, { ...child, status: "queued" as const }],
179 });
180 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
181 registry.set(snapshotAtom(environmentId), {
182 ...v2ShellSnapshot,
183 threads: [root, { ...child, status: "completed" as const, activityRunStatus: null }],
184 });
185 expect(registry.get(inputs)).toEqual([]);
186 const { activityRunStatus: _omitted, ...legacyChild } = child;
187 registry.set(snapshotAtom(environmentId), {
188 ...v2ShellSnapshot,
189 threads: [root, { ...legacyChild, status: "completed" as const }],
190 });
191 expect(registry.get(inputs)).toEqual([]);
192 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] });
193 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
194 dispose();
195 registry.dispose();
196 });
197​
198 it("flags the parent while a live subagent waits on a secret card", () => {
199 const { registry, threads, snapshotAtom } = makeHarness();
200 const root = v2ThreadShell;
201 const child = {
202 ...root,
203 id: ThreadId.make("child"),
204 creationSource: "mcp" as const,
205 status: "running" as const,
206 activityRunStatus: "running" as const,
207 lineage: {
208 ...root.lineage,
209 parentThreadId: root.id,
210 relationshipToParent: "subagent" as const,
211 },
212 pendingRuntimeRequest: {
213 id: RuntimeRequestId.make("secret-card"),
214 kind: "user_input" as const,
215 createdAt: v2Now,
216 blocking: true,
217 userOnly: true,
218 },
219 };
220 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] });
221 expect(
222 registry.get(threads.threadShellAtom({ environmentId, threadId: root.id }))
223 ?.pendingSubagentRequest,
224 ).toBe("user_input");
225 registry.dispose();
226 });
227​
228 it("rolls a side chat's questions and approvals up to its parent row", () => {
229 const { registry, threads, snapshotAtom } = makeHarness();
230 const root = v2ThreadShell;
231 const sideChat = {
232 ...root,
233 id: ThreadId.make("side-chat"),
234 status: "running" as const,
235 activityRunStatus: "running" as const,
236 latestRunId: RunId.make("side-chat-run"),
237 lineage: {
238 ...root.lineage,
239 parentThreadId: root.id,
240 relationshipToParent: "fork" as const,
241 sideChat: true,
242 },
243 };
244 const request = { id: RuntimeRequestId.make("ask"), createdAt: v2Now };
245 const parentRef = { environmentId, threadId: root.id };
246 const parent = threads.threadShellAtom(parentRef);
247 const inputs = threads.childThreadInputsAtom(parentRef);
248 const disposeParent = registry.mount(parent);
249 const disposeInputs = registry.mount(inputs);
250 const disposeNavigation = registry.mount(threads.navigationThreadShellsAtom);
251 const withRequest = (
252 kind: "user_input" | "command",
253 options: { readonly blocking?: boolean } = {},
254 ) =>
255 registry.set(snapshotAtom(environmentId), {
256 ...v2ShellSnapshot,
257 threads: [
258 root,
259 {
260 ...sideChat,
261 pendingRuntimeRequest: {
262 ...request,
263 kind,
264 ...(options.blocking === undefined ? {} : { blocking: options.blocking }),
265 },
266 },
267 ],
268 });
269​
270 withRequest("user_input");
271 expect(registry.get(parent)?.pendingSubagentRequest).toBe("user_input");
272 expect(childThreadInputsTitle(registry.get(inputs))).toBe("Side chat needs input");
273 expect(registry.get(threads.navigationThreadShellsAtom).map((thread) => thread.id)).toEqual([
274 root.id,
275 ]);
276​
277 withRequest("user_input", { blocking: false });
278 expect(registry.get(parent)?.pendingSubagentRequest).toBe("user_input");
279​
280 withRequest("command");
281 expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval");
282 expect(childThreadInputsTitle(registry.get(inputs))).toBe("Side chat needs approval");
283​
284 const helper = {
285 ...sideChat,
286 id: ThreadId.make("side-chat-helper"),
287 creationSource: "mcp" as const,
288 lineage: {
289 ...sideChat.lineage,
290 parentThreadId: sideChat.id,
291 relationshipToParent: "subagent" as const,
292 sideChat: false,
293 },
294 pendingRuntimeRequest: { ...request, kind: "command" as const },
295 };
296 const sideChats = threads.sideChatsAtom(parentRef);
297 const disposeSideChats = registry.mount(sideChats);
298 registry.set(snapshotAtom(environmentId), {
299 ...v2ShellSnapshot,
300 threads: [root, sideChat, helper],
301 });
302 expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval");
303 expect(
304 registry.get(sideChats).map((thread) => [thread.id, thread.pendingSubagentRequest]),
305 ).toEqual([[sideChat.id, "approval"]]);
306​
307 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, sideChat] });
308 expect(registry.get(parent)?.pendingSubagentRequest).toBeNull();
309 expect(registry.get(sideChats).map((thread) => thread.pendingSubagentRequest)).toEqual([null]);
310 expect(registry.get(inputs)).toEqual([]);
311 disposeSideChats();
312 disposeNavigation();
313 disposeInputs();
314 disposeParent();
315 registry.dispose();
316 });
317​
318 it("brings a parent back from Working while a nested subagent waits for approval", () => {
319 const { registry, threads, snapshotAtom } = makeHarness();
320 const child = {
321 ...v2ThreadShell,
322 id: ThreadId.make("review"),
323 creationSource: "mcp" as const,
324 status: "running" as const,
325 activityRunStatus: "running" as const,
326 lineage: {
327 ...v2ThreadShell.lineage,
328 parentThreadId: v2ThreadShell.id,
329 relationshipToParent: "subagent" as const,
330 },
331 };
332 const grandchild = {
333 ...child,
334 id: ThreadId.make("review-helper"),
335 lineage: { ...child.lineage, parentThreadId: child.id },
336 };
337 const root = {
338 ...v2ThreadShell,
339 latestRunId: RunId.make("root-run"),
340 status: "completed" as const,
341 pendingBackgroundTasks: [
342 { taskId: "review", kind: "subagent" as const, childThreadId: child.id },
343 ],
344 };
345 const approval = {
346 id: RuntimeRequestId.make("cat-file"),
347 kind: "command" as const,
348 createdAt: v2Now,
349 };
350 const parent = threads.threadShellAtom({ environmentId, threadId: root.id });
351 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id });
352 const disposeParent = registry.mount(parent);
353 const disposeInputs = registry.mount(inputs);
354 registry.set(snapshotAtom(environmentId), {
355 ...v2ShellSnapshot,
356 threads: [root, child, grandchild],
357 });
358 expect(isThreadWorking(registry.get(parent)!)).toBe(true);
359​
360 registry.set(snapshotAtom(environmentId), {
361 ...v2ShellSnapshot,
362 threads: [root, child, { ...grandchild, pendingRuntimeRequest: approval }],
363 });
364 expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval");
365 expect(isThreadWorking(registry.get(parent)!)).toBe(false);
366 const asking = registry.get(inputs);
367 expect(asking.map((thread) => thread.id)).toEqual([grandchild.id]);
368 expect(childThreadInputsTitle(asking)).toBe("Subagent needs approval");
369​
370 registry.set(snapshotAtom(environmentId), {
371 ...v2ShellSnapshot,
372 threads: [root, child, grandchild],
373 });
374 expect(registry.get(parent)?.pendingSubagentRequest).toBeNull();
375 expect(isThreadWorking(registry.get(parent)!)).toBe(true);
376 disposeInputs();
377 disposeParent();
378 registry.dispose();
379 });
380​
381 it("lists a task a linked environment runs for a listed thread under that thread", () => {
382 const { registry, threads, snapshotAtom, catalogValueAtom } = makeHarness([
383 environmentId,
384 remoteEnvironmentId,
385 ]);
386 const parent = v2ThreadShell;
387 const task = {
388 ...v2ThreadShell,
389 id: ThreadId.make("remote-task"),
390 creationSource: "mcp" as const,
391 status: "running" as const,
392 activityRunStatus: "running" as const,
393 delegatedFrom: { environmentId, threadId: parent.id, title: parent.title },
394 };
395 const helper = {
396 ...task,
397 id: ThreadId.make("remote-helper"),
398 delegatedFrom: undefined,
399 lineage: {
400 ...task.lineage,
401 parentThreadId: task.id,
402 relationshipToParent: "subagent" as const,
403 },
404 };
405 const approval = {
406 id: RuntimeRequestId.make("cat-file"),
407 kind: "command" as const,
408 createdAt: v2Now,
409 };
410 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [parent] });
411 registry.set(snapshotAtom(remoteEnvironmentId), {
412 ...v2ShellSnapshot,
413 threads: [task, helper],
414 });
415 const parentRef = { environmentId, threadId: parent.id };
416 const inputs = threads.childThreadInputsAtom(parentRef);
417 const disposeNavigation = registry.mount(threads.navigationThreadShellsAtom);
418 const disposeParent = registry.mount(threads.threadShellAtom(parentRef));
419 const disposeInputs = registry.mount(inputs);
420 const listed = () =>
421 registry
422 .get(threads.navigationThreadShellsAtom)
423 .map((thread) => `${thread.environmentId}/${thread.id}`);
424 expect(listed()).toEqual([`${environmentId}/${parent.id}`]);
425​
426 // The helper the task started there asks for approval: the parent here hears of it.
427 registry.set(snapshotAtom(remoteEnvironmentId), {
428 ...v2ShellSnapshot,
429 threads: [task, { ...helper, pendingRuntimeRequest: approval }],
430 });
431 expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBe(
432 "approval",
433 );
434 expect(registry.get(inputs).map((thread) => `${thread.environmentId}/${thread.id}`)).toEqual([
435 `${remoteEnvironmentId}/${helper.id}`,
436 ]);
437​
438 registry.set(snapshotAtom(remoteEnvironmentId), {
439 ...v2ShellSnapshot,
440 threads: [
441 task,
442 { ...helper, pendingRuntimeRequest: { ...approval, kind: "user_input" as const } },
443 ],
444 });
445 expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBeNull();
446 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([helper.id]);
447​
448 // A secret card needs the user, so it still reaches the parent here.
449 registry.set(snapshotAtom(remoteEnvironmentId), {
450 ...v2ShellSnapshot,
451 threads: [
452 task,
453 {
454 ...helper,
455 pendingRuntimeRequest: { ...approval, kind: "user_input" as const, userOnly: true },
456 },
457 ],
458 });
459 expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBe(
460 "user_input",
461 );
462​
463 // A task whose parent is gone there is listed on its own, as is one whose
464 // parent's environment is switched off.
465 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [] });
466 expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]);
467 // Archiving the parent here leaves the task there unarchived, so it is listed on its own.
468 registry.set(snapshotAtom(environmentId), {
469 ...v2ShellSnapshot,
470 threads: [{ ...parent, archivedAt: v2Now }],
471 });
472 expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]);
473 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [parent] });
474 expect(listed()).toEqual([`${environmentId}/${parent.id}`]);
475 const catalog = registry.get(catalogValueAtom);
476 const entries = new Map(catalog.entries);
477 entries.set(environmentId, { ...entries.get(environmentId)!, enabled: false });
478 registry.set(catalogValueAtom, { ...catalog, entries });
479 expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]);
480 disposeInputs();
481 disposeParent();
482 disposeNavigation();
483 registry.dispose();
484 });
485​
486 it("brings an approval up a chain of tasks across three environments to its root", () => {
487 const thirdEnvironmentId = EnvironmentId.make("third-environment-v2");
488 const { registry, threads, snapshotAtom } = makeHarness([
489 environmentId,
490 remoteEnvironmentId,
491 thirdEnvironmentId,
492 ]);
493 const root = v2ThreadShell;
494 const running = {
495 creationSource: "mcp" as const,
496 status: "running" as const,
497 activityRunStatus: "running" as const,
498 };
499 const task = {
500 ...v2ThreadShell,
501 ...running,
502 id: ThreadId.make("remote-task"),
503 delegatedFrom: { environmentId, threadId: root.id, title: root.title },
504 };
505 const nestedTask = {
506 ...v2ThreadShell,
507 ...running,
508 id: ThreadId.make("third-task"),
509 delegatedFrom: { environmentId: remoteEnvironmentId, threadId: task.id, title: task.title },
510 };
511 // A task the middle one delegates back to the root's own environment.
512 const returningTask = {
513 ...nestedTask,
514 id: ThreadId.make("returning-task"),
515 };
516 const approval = {
517 id: RuntimeRequestId.make("cat-file"),
518 kind: "command" as const,
519 createdAt: v2Now,
520 };
521 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root] });
522 registry.set(snapshotAtom(remoteEnvironmentId), { ...v2ShellSnapshot, threads: [task] });
523 registry.set(snapshotAtom(thirdEnvironmentId), {
524 ...v2ShellSnapshot,
525 threads: [{ ...nestedTask, pendingRuntimeRequest: approval }],
526 });
527 const rootRef = { environmentId, threadId: root.id };
528 const taskRef = { environmentId: remoteEnvironmentId, threadId: task.id };
529 const disposeRoot = registry.mount(threads.threadShellAtom(rootRef));
530 const disposeTask = registry.mount(threads.threadShellAtom(taskRef));
531 expect(registry.get(threads.threadShellAtom(taskRef))?.pendingSubagentRequest).toBe("approval");
532 expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBe("approval");
533​
534 // Once it is answered, neither stays flagged.
535 registry.set(snapshotAtom(thirdEnvironmentId), {
536 ...v2ShellSnapshot,
537 threads: [nestedTask],
538 });
539 expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBeNull();
540​
541 // Environments that delegate to each other still settle on the root's flag.
542 registry.set(snapshotAtom(environmentId), {
543 ...v2ShellSnapshot,
544 threads: [root, { ...returningTask, pendingRuntimeRequest: approval }],
545 });
546 expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBe("approval");
547 disposeTask();
548 disposeRoot();
549 registry.dispose();
550 });
551​
552 it.each([
553 { reason: "switched-off", state: { enabled: false } },
554 {
555 reason: "unsupported",
556 state: { enabled: true, unsupportedReason: "Use a compatible client." },
557 },
558 ])("leaves $reason environments out of navigation until restored", ({ state }) => {
559 const { registry, threads, catalogValueAtom } = makeHarness([
560 environmentId,
561 remoteEnvironmentId,
562 ]);
563 const dispose = registry.mount(threads.navigationThreadShellsAtom);
564 const environmentsOf = () =>
565 new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));
566 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
567​
568 const setRemotePaused = (paused: boolean) => {
569 const catalog = registry.get(catalogValueAtom);
570 const entries = new Map(catalog.entries);
571 entries.set(remoteEnvironmentId, {
572 ...entries.get(remoteEnvironmentId)!,
573 ...(paused ? state : { enabled: true, unsupportedReason: undefined }),
574 });
575 registry.set(catalogValueAtom, { ...catalog, entries });
576 };
577 setRemotePaused(true);
578 expect(environmentsOf()).toEqual(new Set([environmentId]));
579 setRemotePaused(false);
580 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
581 dispose();
582 registry.dispose();
583 });
584​
585 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
586 const harness = makeHarness([environmentId, remoteEnvironmentId]);
587 const otherThread = {
588 ...v2ThreadShell,
589 id: ThreadId.make("other-thread"),
590 projectId: otherProjectId,
591 };
592 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
593 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
594 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
595 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
596 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
597 { environmentId, projectId: v2ThreadShell.projectId },
598 ]);
599 const disposeList = harness.registry.mount(listAtom);
600 try {
601 const membership = harness.registry.get(membershipAtom);
602 const before = harness.registry.get(listAtom);
603 expect(before).toHaveLength(2);
604 expect(before[0]).not.toBe(before[1]);
605 expect(before.map((thread) => thread.environmentId)).toEqual([
606 remoteEnvironmentId,
607 environmentId,
608 ]);
609​
610 harness.registry.set(harness.snapshotAtom(environmentId), {
611 ...snapshot,
612 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
613 });
614 expect(harness.registry.get(membershipAtom)).toBe(membership);
615 expect(harness.registry.get(listAtom)).toBe(before);
616​
617 harness.registry.set(harness.snapshotAtom(environmentId), {
618 ...snapshot,
619 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
620 });
621 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
622 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
623 ...v2ShellSnapshot,
624 threads: [],
625 });
626 expect(harness.registry.get(listAtom)).toEqual([]);
627 } finally {
628 disposeList();
629 harness.registry.dispose();
630 }
631 });
632​
633 it("keeps the quiet shell through streaming ticks and follows every change once the run stops", () => {
634 const harness = makeHarness();
635 const runId = RunId.make("streaming-run");
636 const running: OrchestrationV2ThreadShell = {
637 ...v2ThreadShell,
638 latestRunId: runId,
639 activeRunId: runId,
640 status: "running",
641 itemCount: 1,
642 visibleItemCount: 1,
643 };
644 const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) =>
645 harness.registry.set(harness.snapshotAtom(environmentId), {
646 ...v2ShellSnapshot,
647 snapshotSequence: sequence,
648 threads: [thread],
649 });
650 const at = (seconds: number) => DateTime.add(v2Now, { seconds });
651 setThread(running, 1);
652 const ref = { environmentId, threadId: v2ThreadShell.id };
653 const quietAtom = harness.threads.quietThreadShellAtom(ref);
654 const dispose = harness.registry.mount(quietAtom);
655 try {
656 const first = harness.registry.get(quietAtom);
657 setThread({ ...running, updatedAt: at(1), itemCount: 4, visibleItemCount: 3 }, 2);
658 expect(harness.registry.get(quietAtom)).toBe(first);
659 expect(harness.registry.get(harness.threads.threadShellAtom(ref))?.itemCount).toBe(4);
660​
661 setThread({ ...running, updatedAt: at(2), itemCount: 5, title: "Renamed" }, 3);
662 const renamed = harness.registry.get(quietAtom);
663 expect(renamed?.title).toBe("Renamed");
664​
665 const stopped: OrchestrationV2ThreadShell = {
666 ...running,
667 activeRunId: null,
668 status: "completed",
669 };
670 setThread({ ...stopped, title: "Renamed", updatedAt: at(3) }, 4);
671 const completed = harness.registry.get(quietAtom);
672 expect(completed).not.toBe(renamed);
673 setThread({ ...stopped, title: "Renamed", updatedAt: at(4) }, 5);
674 expect(harness.registry.get(quietAtom)?.updatedAt).toBe(DateTime.formatIso(at(4)));
675 expect(harness.registry.get(quietAtom)).not.toBe(completed);
676 } finally {
677 dispose();
678 harness.registry.dispose();
679 }
680 });
681​
682 it("keeps the quiet shell list through streaming ticks", () => {
683 const harness = makeHarness();
684 const runId = RunId.make("streaming-list-run");
685 const running: OrchestrationV2ThreadShell = {
686 ...v2ThreadShell,
687 latestRunId: runId,
688 activeRunId: runId,
689 status: "running",
690 itemCount: 1,
691 visibleItemCount: 1,
692 };
693 const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) =>
694 harness.registry.set(harness.snapshotAtom(environmentId), {
695 ...v2ShellSnapshot,
696 snapshotSequence: sequence,
697 threads: [thread],
698 });
699 setThread(running, 1);
700 const listAtom = harness.threads.quietThreadShellsAtom;
701 const dispose = harness.registry.mount(listAtom);
702 try {
703 const first = harness.registry.get(listAtom);
704 expect(first).toHaveLength(1);
705 setThread({ ...running, updatedAt: DateTime.add(v2Now, { seconds: 1 }), itemCount: 4 }, 2);
706 expect(harness.registry.get(listAtom)).toBe(first);
707​
708 setThread({ ...running, title: "Renamed" }, 3);
709 expect(harness.registry.get(listAtom)).not.toBe(first);
710 expect(harness.registry.get(listAtom)[0]?.title).toBe("Renamed");
711 } finally {
712 dispose();
713 harness.registry.dispose();
714 }
715 });
716});
717​
718describe("resolveListedAncestorThreadRef", () => {
719 type Thread = Parameters<typeof resolveListedAncestorThreadRef>[1][number];
720 const otherEnvironmentId = EnvironmentId.make("environment-other");
721 const ref = (environment: EnvironmentId, threadId: ThreadId) => ({
722 environmentId: environment,
723 threadId,
724 });
725 const listed = (
726 id: string,
727 environment = environmentId,
728 overrides?: Partial<Thread>,
729 ): Thread => ({
730 id: ThreadId.make(id),
731 environmentId: environment,
732 lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null },
733 archivedAt: null,
734 delegatedFrom: null,
735 ...overrides,
736 });
737 const subagentOf = (id: string, parentId: ThreadId, environment = environmentId) =>
738 listed(id, environment, {
739 lineage: {
740 rootThreadId: parentId,
741 parentThreadId: parentId,
742 relationshipToParent: "subagent",
743 },
744 });
745 const rowOf = (threadId: ThreadId, environment: EnvironmentId, threads: readonly Thread[]) =>
746 resolveListedAncestorThreadRef(ref(environment, threadId), threads);
747​
748 it("maps an open subagent to its nearest listed ancestor", () => {
749 const root = listed("thread-root");
750 const fork = listed("thread-fork", environmentId, {
751 lineage: { rootThreadId: root.id, parentThreadId: root.id, relationshipToParent: "fork" },
752 });
753 const child = subagentOf("thread-child", root.id);
754 const grandchild = subagentOf("thread-grandchild", child.id);
755 const forkChild = subagentOf("thread-fork-child", fork.id);
756 const orphan = subagentOf("thread-orphan", ThreadId.make("thread-missing"));
757 const elsewhere = subagentOf("thread-elsewhere", root.id, otherEnvironmentId);
758 const threads = [root, fork, child, grandchild, forkChild, orphan, elsewhere];
759​
760 expect(rowOf(child.id, environmentId, threads)).toEqual(ref(environmentId, root.id));
761 expect(rowOf(grandchild.id, environmentId, threads)).toEqual(ref(environmentId, root.id));
762 expect(rowOf(forkChild.id, environmentId, threads)).toEqual(ref(environmentId, fork.id));
763 expect(rowOf(elsewhere.id, otherEnvironmentId, threads)).toEqual(
764 ref(otherEnvironmentId, elsewhere.id),
765 );
766 expect(rowOf(orphan.id, environmentId, threads)).toEqual(ref(environmentId, orphan.id));
767 const loopA = subagentOf("thread-loop-a", ThreadId.make("thread-loop-b"));
768 const loopB = subagentOf("thread-loop-b", loopA.id);
769 expect(rowOf(loopA.id, environmentId, [loopA, loopB])).toEqual(ref(environmentId, loopA.id));
770 });
771​
772 it("maps an open task a linked environment delegated to its listed parent", () => {
773 const parent = listed("thread-parent", remoteEnvironmentId);
774 const task = listed("thread-task", environmentId, {
775 delegatedFrom: { environmentId: remoteEnvironmentId, threadId: parent.id, title: "Parent" },
776 });
777​
778 expect(rowOf(task.id, environmentId, [parent, task])).toEqual(
779 ref(remoteEnvironmentId, parent.id),
780 );
781 });
782});