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

threadShell.test.ts

347 lines13.0 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 { childThreadInputsTitle, createEnvironmentThreadShellAtoms } from "./threadShell.ts";
20​
21const environmentId = EnvironmentId.make("environment-v2");
22const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2");
23const otherProjectId = ProjectId.make("other-project");
24​
25function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) {
26 const snapshotAtom = Atom.family((_environmentId: EnvironmentId) =>
27 Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot),
28 );
29 const catalogValueAtom = Atom.make({
30 isReady: true,
31 entries: new Map(
32 environmentIds.map((id) => [
33 id,
34 {
35 target: new PrimaryConnectionTarget({
36 environmentId: id,
37 label: "Environment",
38 httpBaseUrl: "https://example.test",
39 wsBaseUrl: "wss://example.test",
40 }),
41 profile: Option.none(),
42 enabled: true,
43 },
44 ]),
45 ),
46 });
47 return {
48 registry: AtomRegistry.make(),
49 snapshotAtom,
50 catalogValueAtom,
51 threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom }),
52 };
54​
55describe("v2 thread shell lists", () => {
56 it("preserves ordered reference arrays when a middle thread changes", () => {
57 const { registry, threads, snapshotAtom } = makeHarness();
58 const snapshot = {
59 ...v2ShellSnapshot,
60 threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })),
61 };
62 registry.set(snapshotAtom(environmentId), snapshot);
63 const dispose = registry.mount(threads.threadRefsAtom);
64 const before = registry.get(threads.threadRefsAtom);
65 registry.set(
66 snapshotAtom(environmentId),
67 applyShellStreamEvent(snapshot, {
68 kind: "thread.updated",
69 location: "active",
70 sequence: 1,
71 thread: { ...snapshot.threads[1]!, title: "Updated" },
72 }),
73 );
74 expect(registry.get(threads.threadRefsAtom)).toBe(before);
75 dispose();
76 registry.dispose();
77 });
78​
79 it("keeps navigation stable on hidden subagent updates and retains user forks", () => {
80 const { registry, threads, snapshotAtom } = makeHarness();
81 const root = v2ThreadShell;
82 const child = {
83 ...root,
84 id: ThreadId.make("child"),
85 lineage: {
86 ...root.lineage,
87 parentThreadId: root.id,
88 relationshipToParent: "subagent" as const,
89 },
90 };
91 const fork = {
92 ...child,
93 id: ThreadId.make("fork"),
94 lineage: { ...child.lineage, relationshipToParent: "fork" as const },
95 };
96 const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] };
97 registry.set(snapshotAtom(environmentId), snapshot);
98 const dispose = registry.mount(threads.navigationThreadShellsAtom);
99 const before = registry.get(threads.navigationThreadShellsAtom);
100 expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]);
101 registry.set(snapshotAtom(environmentId), {
102 ...snapshot,
103 threads: [root, { ...child, title: "Child streaming" }, fork],
104 });
105 expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before);
106 expect(registry.get(threads.threadShellsAtom)).toHaveLength(3);
107 dispose();
108 registry.dispose();
109 });
110​
111 it("counts a subagent question on its parent only while the subagent runs", () => {
112 const { registry, threads, snapshotAtom } = makeHarness();
113 const root = v2ThreadShell;
114 const child = {
115 ...root,
116 id: ThreadId.make("child"),
117 creationSource: "mcp" as const,
118 status: "running" as const,
119 activityRunStatus: "running" as const,
120 lineage: {
121 ...root.lineage,
122 parentThreadId: root.id,
123 relationshipToParent: "subagent" as const,
124 },
125 pendingRuntimeRequest: {
126 id: RuntimeRequestId.make("question"),
127 kind: "user_input" as const,
128 createdAt: v2Now,
129 },
130 };
131 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id });
132 const dispose = registry.mount(inputs);
133 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] });
134 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
135 registry.set(snapshotAtom(environmentId), {
136 ...v2ShellSnapshot,
137 threads: [root, { ...child, status: "queued" as const }],
138 });
139 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
140 registry.set(snapshotAtom(environmentId), {
141 ...v2ShellSnapshot,
142 threads: [root, { ...child, status: "completed" as const, activityRunStatus: null }],
143 });
144 expect(registry.get(inputs)).toEqual([]);
145 const { activityRunStatus: _omitted, ...legacyChild } = child;
146 registry.set(snapshotAtom(environmentId), {
147 ...v2ShellSnapshot,
148 threads: [root, { ...legacyChild, status: "completed" as const }],
149 });
150 expect(registry.get(inputs)).toEqual([]);
151 dispose();
152 registry.dispose();
153 });
154​
155 it("brings a parent back from Working while a nested subagent waits for approval", () => {
156 const { registry, threads, snapshotAtom } = makeHarness();
157 const child = {
158 ...v2ThreadShell,
159 id: ThreadId.make("review"),
160 creationSource: "mcp" as const,
161 status: "running" as const,
162 activityRunStatus: "running" as const,
163 lineage: {
164 ...v2ThreadShell.lineage,
165 parentThreadId: v2ThreadShell.id,
166 relationshipToParent: "subagent" as const,
167 },
168 };
169 const grandchild = {
170 ...child,
171 id: ThreadId.make("review-helper"),
172 lineage: { ...child.lineage, parentThreadId: child.id },
173 };
174 const root = {
175 ...v2ThreadShell,
176 latestRunId: RunId.make("root-run"),
177 status: "completed" as const,
178 pendingBackgroundTasks: [
179 { taskId: "review", kind: "subagent" as const, childThreadId: child.id },
180 ],
181 };
182 const approval = {
183 id: RuntimeRequestId.make("cat-file"),
184 kind: "command" as const,
185 createdAt: v2Now,
186 };
187 const parent = threads.threadShellAtom({ environmentId, threadId: root.id });
188 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id });
189 const disposeParent = registry.mount(parent);
190 const disposeInputs = registry.mount(inputs);
191 registry.set(snapshotAtom(environmentId), {
192 ...v2ShellSnapshot,
193 threads: [root, child, grandchild],
194 });
195 expect(isThreadWorking(registry.get(parent)!)).toBe(true);
196​
197 registry.set(snapshotAtom(environmentId), {
198 ...v2ShellSnapshot,
199 threads: [root, child, { ...grandchild, pendingRuntimeRequest: approval }],
200 });
201 expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval");
202 expect(isThreadWorking(registry.get(parent)!)).toBe(false);
203 const asking = registry.get(inputs);
204 expect(asking.map((thread) => thread.id)).toEqual([grandchild.id]);
205 expect(childThreadInputsTitle(asking)).toBe("Subagent needs approval");
206​
207 registry.set(snapshotAtom(environmentId), {
208 ...v2ShellSnapshot,
209 threads: [root, child, grandchild],
210 });
211 expect(registry.get(parent)?.pendingSubagentRequest).toBeNull();
212 expect(isThreadWorking(registry.get(parent)!)).toBe(true);
213 disposeInputs();
214 disposeParent();
215 registry.dispose();
216 });
217​
218 it.each([
219 { reason: "switched-off", state: { enabled: false } },
220 {
221 reason: "unsupported",
222 state: { enabled: true, unsupportedReason: "Use a compatible client." },
223 },
224 ])("leaves $reason environments out of navigation until restored", ({ state }) => {
225 const { registry, threads, catalogValueAtom } = makeHarness([
226 environmentId,
227 remoteEnvironmentId,
228 ]);
229 const dispose = registry.mount(threads.navigationThreadShellsAtom);
230 const environmentsOf = () =>
231 new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));
232 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
233​
234 const setRemotePaused = (paused: boolean) => {
235 const catalog = registry.get(catalogValueAtom);
236 const entries = new Map(catalog.entries);
237 entries.set(remoteEnvironmentId, {
238 ...entries.get(remoteEnvironmentId)!,
239 ...(paused ? state : { enabled: true, unsupportedReason: undefined }),
240 });
241 registry.set(catalogValueAtom, { ...catalog, entries });
242 };
243 setRemotePaused(true);
244 expect(environmentsOf()).toEqual(new Set([environmentId]));
245 setRemotePaused(false);
246 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
247 dispose();
248 registry.dispose();
249 });
250​
251 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
252 const harness = makeHarness([environmentId, remoteEnvironmentId]);
253 const otherThread = {
254 ...v2ThreadShell,
255 id: ThreadId.make("other-thread"),
256 projectId: otherProjectId,
257 };
258 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
259 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
260 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
261 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
262 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
263 { environmentId, projectId: v2ThreadShell.projectId },
264 ]);
265 const disposeList = harness.registry.mount(listAtom);
266 try {
267 const membership = harness.registry.get(membershipAtom);
268 const before = harness.registry.get(listAtom);
269 expect(before).toHaveLength(2);
270 expect(before[0]).not.toBe(before[1]);
271 expect(before.map((thread) => thread.environmentId)).toEqual([
272 remoteEnvironmentId,
273 environmentId,
274 ]);
275​
276 harness.registry.set(harness.snapshotAtom(environmentId), {
277 ...snapshot,
278 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
279 });
280 expect(harness.registry.get(membershipAtom)).toBe(membership);
281 expect(harness.registry.get(listAtom)).toBe(before);
282​
283 harness.registry.set(harness.snapshotAtom(environmentId), {
284 ...snapshot,
285 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
286 });
287 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
288 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
289 ...v2ShellSnapshot,
290 threads: [],
291 });
292 expect(harness.registry.get(listAtom)).toEqual([]);
293 } finally {
294 disposeList();
295 harness.registry.dispose();
296 }
297 });
298​
299 it("keeps the quiet shell through streaming ticks and follows every change once the run stops", () => {
300 const harness = makeHarness();
301 const runId = RunId.make("streaming-run");
302 const running: OrchestrationV2ThreadShell = {
303 ...v2ThreadShell,
304 latestRunId: runId,
305 activeRunId: runId,
306 status: "running",
307 itemCount: 1,
308 visibleItemCount: 1,
309 };
310 const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) =>
311 harness.registry.set(harness.snapshotAtom(environmentId), {
312 ...v2ShellSnapshot,
313 snapshotSequence: sequence,
314 threads: [thread],
315 });
316 const at = (seconds: number) => DateTime.add(v2Now, { seconds });
317 setThread(running, 1);
318 const ref = { environmentId, threadId: v2ThreadShell.id };
319 const quietAtom = harness.threads.quietThreadShellAtom(ref);
320 const dispose = harness.registry.mount(quietAtom);
321 try {
322 const first = harness.registry.get(quietAtom);
323 setThread({ ...running, updatedAt: at(1), itemCount: 4, visibleItemCount: 3 }, 2);
324 expect(harness.registry.get(quietAtom)).toBe(first);
325 expect(harness.registry.get(harness.threads.threadShellAtom(ref))?.itemCount).toBe(4);
326​
327 setThread({ ...running, updatedAt: at(2), itemCount: 5, title: "Renamed" }, 3);
328 const renamed = harness.registry.get(quietAtom);
329 expect(renamed?.title).toBe("Renamed");
330​
331 const stopped: OrchestrationV2ThreadShell = {
332 ...running,
333 activeRunId: null,
334 status: "completed",
335 };
336 setThread({ ...stopped, title: "Renamed", updatedAt: at(3) }, 4);
337 const completed = harness.registry.get(quietAtom);
338 expect(completed).not.toBe(renamed);
339 setThread({ ...stopped, title: "Renamed", updatedAt: at(4) }, 5);
340 expect(harness.registry.get(quietAtom)?.updatedAt).toBe(DateTime.formatIso(at(4)));
341 expect(harness.registry.get(quietAtom)).not.toBe(completed);
342 } finally {
343 dispose();
344 harness.registry.dispose();
345 }
346 });
347});