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