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

threadShell.test.ts

273 lines10.5 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 RuntimeRequestId,
5 ThreadId,
6 type OrchestrationV2ShellSnapshot,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import { Atom, AtomRegistry } from "effect/reactivity";
10import { describe, expect, it } from "vite-plus/test";
11​
12import { PrimaryConnectionTarget } from "../connection/model.ts";
13import { v2Now, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
14import { applyShellStreamEvent } from "./shellReducer.ts";
15import { createEnvironmentThreadShellAtoms } from "./threadShell.ts";
16​
17const environmentId = EnvironmentId.make("environment-v2");
18const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2");
19const otherProjectId = ProjectId.make("other-project");
20​
21function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) {
22 const snapshotAtom = Atom.family((_environmentId: EnvironmentId) =>
23 Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot),
24 );
25 const catalogValueAtom = Atom.make({
26 isReady: true,
27 entries: new Map(
28 environmentIds.map((id) => [
29 id,
30 {
31 target: new PrimaryConnectionTarget({
32 environmentId: id,
33 label: "Environment",
34 httpBaseUrl: "https://example.test",
35 wsBaseUrl: "wss://example.test",
36 }),
37 profile: Option.none(),
38 enabled: true,
39 },
40 ]),
41 ),
42 });
43 return {
44 registry: AtomRegistry.make(),
45 snapshotAtom,
46 catalogValueAtom,
47 threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom }),
48 };
50​
51describe("v2 thread shell lists", () => {
52 it("preserves ordered reference arrays when a middle thread changes", () => {
53 const { registry, threads, snapshotAtom } = makeHarness();
54 const snapshot = {
55 ...v2ShellSnapshot,
56 threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })),
57 };
58 registry.set(snapshotAtom(environmentId), snapshot);
59 const dispose = registry.mount(threads.threadRefsAtom);
60 const before = registry.get(threads.threadRefsAtom);
61 registry.set(
62 snapshotAtom(environmentId),
63 applyShellStreamEvent(snapshot, {
64 kind: "thread.updated",
65 location: "active",
66 sequence: 1,
67 thread: { ...snapshot.threads[1]!, title: "Updated" },
68 }),
69 );
70 expect(registry.get(threads.threadRefsAtom)).toBe(before);
71 dispose();
72 registry.dispose();
73 });
74​
75 it("keeps navigation stable on hidden subagent updates and retains user forks", () => {
76 const { registry, threads, snapshotAtom } = makeHarness();
77 const root = v2ThreadShell;
78 const child = {
79 ...root,
80 id: ThreadId.make("child"),
81 lineage: {
82 ...root.lineage,
83 parentThreadId: root.id,
84 relationshipToParent: "subagent" as const,
85 },
86 };
87 const fork = {
88 ...child,
89 id: ThreadId.make("fork"),
90 lineage: { ...child.lineage, relationshipToParent: "fork" as const },
91 };
92 const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] };
93 registry.set(snapshotAtom(environmentId), snapshot);
94 const dispose = registry.mount(threads.navigationThreadShellsAtom);
95 const before = registry.get(threads.navigationThreadShellsAtom);
96 expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]);
97 registry.set(snapshotAtom(environmentId), {
98 ...snapshot,
99 threads: [root, { ...child, title: "Child streaming" }, fork],
100 });
101 expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before);
102 expect(registry.get(threads.threadShellsAtom)).toHaveLength(3);
103 dispose();
104 registry.dispose();
105 });
106​
107 it("counts a subagent question on its parent only while the subagent runs", () => {
108 const { registry, threads, snapshotAtom } = makeHarness();
109 const root = v2ThreadShell;
110 const child = {
111 ...root,
112 id: ThreadId.make("child"),
113 creationSource: "mcp" as const,
114 status: "running" as const,
115 activityRunStatus: "running" as const,
116 lineage: {
117 ...root.lineage,
118 parentThreadId: root.id,
119 relationshipToParent: "subagent" as const,
120 },
121 pendingRuntimeRequest: {
122 id: RuntimeRequestId.make("question"),
123 kind: "user_input" as const,
124 createdAt: v2Now,
125 },
126 };
127 const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id });
128 const dispose = registry.mount(inputs);
129 registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] });
130 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
131 // A follow-up queued behind the blocked run leaves the question live.
132 registry.set(snapshotAtom(environmentId), {
133 ...v2ShellSnapshot,
134 threads: [root, { ...child, status: "queued" as const }],
135 });
136 expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]);
137 // An async question stays pending after the run that asked it completes.
138 registry.set(snapshotAtom(environmentId), {
139 ...v2ShellSnapshot,
140 threads: [root, { ...child, status: "completed" as const, activityRunStatus: null }],
141 });
142 expect(registry.get(inputs)).toEqual([]);
143 // Older servers omit the activity run, so the latest run's status decides.
144 const { activityRunStatus: _omitted, ...legacyChild } = child;
145 registry.set(snapshotAtom(environmentId), {
146 ...v2ShellSnapshot,
147 threads: [root, { ...legacyChild, status: "completed" as const }],
148 });
149 expect(registry.get(inputs)).toEqual([]);
150 dispose();
151 registry.dispose();
152 });
153​
154 it("leaves switched-off environments out of navigation", () => {
155 const { registry, threads, catalogValueAtom } = makeHarness([
156 environmentId,
157 remoteEnvironmentId,
158 ]);
159 const dispose = registry.mount(threads.navigationThreadShellsAtom);
160 const environmentsOf = () =>
161 new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));
162 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
163​
164 const setRemoteEnabled = (enabled: boolean) => {
165 const catalog = registry.get(catalogValueAtom);
166 const entries = new Map(catalog.entries);
167 entries.set(remoteEnvironmentId, { ...entries.get(remoteEnvironmentId)!, enabled });
168 registry.set(catalogValueAtom, { ...catalog, entries });
169 };
170 setRemoteEnabled(false);
171 expect(environmentsOf()).toEqual(new Set([environmentId]));
172 setRemoteEnabled(true);
173 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
174 dispose();
175 registry.dispose();
176 });
177​
178 it("shares point and list values without retaining an atom for every listed thread", () => {
179 const harness = makeHarness();
180 const snapshot = {
181 ...v2ShellSnapshot,
182 threads: Array.from({ length: 200 }, (_, index) => ({
183 ...v2ThreadShell,
184 id: ThreadId.make(`thread-${index}`),
185 })),
186 };
187 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
188 const listAtom = harness.threads.threadShellsAtom;
189 const projectListAtom = harness.threads.threadShellsForProjectRefsAtom([
190 { environmentId, projectId: v2ThreadShell.projectId },
191 ]);
192 const disposeList = harness.registry.mount(listAtom);
193 const disposeProjectList = harness.registry.mount(projectListAtom);
194 try {
195 const before = harness.registry.get(listAtom);
196 expect(before).toHaveLength(200);
197 expect(harness.registry.get(projectListAtom)).toEqual(before);
198 expect(harness.registry.getNodes().size).toBeLessThan(20);
199 const firstAtom = harness.threads.threadShellAtom({
200 environmentId,
201 threadId: snapshot.threads[0]!.id,
202 });
203 expect(harness.registry.get(firstAtom)).toBe(before[0]);
204​
205 harness.registry.set(harness.snapshotAtom(environmentId), {
206 ...snapshot,
207 snapshotSequence: 1,
208 threads: snapshot.threads.map((thread, index) =>
209 index === 199 ? { ...thread, title: "Updated last thread" } : thread,
210 ),
211 });
212 const after = harness.registry.get(listAtom);
213 expect(after[0]).toBe(before[0]);
214 expect(after.at(-1)).not.toBe(before.at(-1));
215 expect(after.at(-1)?.title).toBe("Updated last thread");
216 expect(harness.registry.get(projectListAtom).at(-1)).toBe(after.at(-1));
217 expect(harness.registry.get(firstAtom)).toBe(after[0]);
218 expect(harness.registry.getNodes().size).toBeLessThan(20);
219 } finally {
220 disposeProjectList();
221 disposeList();
222 harness.registry.dispose();
223 }
224 });
225​
226 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
227 const harness = makeHarness([environmentId, remoteEnvironmentId]);
228 const otherThread = {
229 ...v2ThreadShell,
230 id: ThreadId.make("other-thread"),
231 projectId: otherProjectId,
232 };
233 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
234 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
235 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
236 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
237 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
238 { environmentId, projectId: v2ThreadShell.projectId },
239 ]);
240 const disposeList = harness.registry.mount(listAtom);
241 try {
242 const membership = harness.registry.get(membershipAtom);
243 const before = harness.registry.get(listAtom);
244 expect(before).toHaveLength(2);
245 expect(before[0]).not.toBe(before[1]);
246 expect(before.map((thread) => thread.environmentId)).toEqual([
247 remoteEnvironmentId,
248 environmentId,
249 ]);
250​
251 harness.registry.set(harness.snapshotAtom(environmentId), {
252 ...snapshot,
253 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
254 });
255 expect(harness.registry.get(membershipAtom)).toBe(membership);
256 expect(harness.registry.get(listAtom)).toBe(before);
257​
258 harness.registry.set(harness.snapshotAtom(environmentId), {
259 ...snapshot,
260 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
261 });
262 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
263 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
264 ...v2ShellSnapshot,
265 threads: [],
266 });
267 expect(harness.registry.get(listAtom)).toEqual([]);
268 } finally {
269 disposeList();
270 harness.registry.dispose();
271 }
272 });
273});