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

threadDetail.test.ts

374 lines12.1 KB

1import {
2 CheckpointId,
3 CheckpointRef,
4 CheckpointScopeId,
5 EnvironmentId,
6 NodeId,
7 ProviderDriverKind,
8 RuntimeRequestId,
9 TurnItemId,
10 type OrchestrationV2ThreadProjection,
11 ThreadId,
12 RunId,
13 MessageId,
14 type ScopedThreadRef,
15} from "@t3tools/contracts";
16import * as Option from "effect/Option";
17import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
18import { describe, expect, it } from "vite-plus/test";
19​
20import { v2Projection, v2Now } from "./orchestrationV2TestFixtures.ts";
21import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
22import { createEnvironmentThreadDetailAtoms } from "./threadDetail.ts";
23import type { EnvironmentThreadState } from "./threads.ts";
24​
25const ref: ScopedThreadRef = {
26 environmentId: EnvironmentId.make("environment-detail"),
27 threadId: ThreadId.make(v2Projection.thread.id),
28};
29​
30function threadState(
31 partial: Pick<EnvironmentThreadState, "data" | "status" | "error"> &
32 Partial<Pick<EnvironmentThreadState, "history">>,
33): EnvironmentThreadState {
34 return { ...partial, history: partial.history ?? EMPTY_THREAD_HISTORY_META };
36​
37describe("createEnvironmentThreadDetailAtoms", () => {
38 it("adds environment scope while preserving the pristine projection", () => {
39 const initial: AsyncResult.AsyncResult<EnvironmentThreadState, never> = AsyncResult.success(
40 threadState({
41 data: Option.some(v2Projection),
42 status: "cached",
43 error: Option.none(),
44 }),
45 );
46 const sourceAtom = Atom.make(initial);
47 const details = createEnvironmentThreadDetailAtoms(() => sourceAtom);
48 const registry = AtomRegistry.make();
49​
50 const thread = registry.get(details.threadAtom(ref));
51 expect(thread).toEqual({ environmentId: ref.environmentId, projection: v2Projection });
52 expect(thread?.projection).toBe(v2Projection);
53 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
54 expect(registry.get(details.statusAtom(ref))).toBe("cached");
55 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
56​
57 registry.set(
58 sourceAtom,
59 AsyncResult.success(
60 threadState({
61 data: Option.some(v2Projection),
62 status: "synchronizing",
63 error: Option.none(),
64 }),
65 ),
66 );
67​
68 expect(registry.get(details.threadAtom(ref))).toBe(thread);
69 expect(registry.get(details.statusAtom(ref))).toBe("synchronizing");
70​
71 const progressiveHistory = {
72 ...EMPTY_THREAD_HISTORY_META,
73 historyCursor: "cursor-1",
74 hasMoreHistory: true,
75 };
76 registry.set(
77 sourceAtom,
78 AsyncResult.success(
79 threadState({
80 data: Option.some(v2Projection),
81 status: "live",
82 error: Option.some("Stream interrupted."),
83 history: progressiveHistory,
84 }),
85 ),
86 );
87​
88 expect(registry.get(details.threadAtom(ref))).toBe(thread);
89 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
90 expect(registry.get(details.statusAtom(ref))).toBe("live");
91 expect(registry.get(details.errorAtom(ref))).toBe("Stream interrupted.");
92 expect(registry.get(details.historyAtom(ref))).toBe(progressiveHistory);
93​
94 registry.set(
95 sourceAtom,
96 AsyncResult.success(
97 threadState({
98 data: Option.none(),
99 status: "deleted",
100 error: Option.none(),
101 }),
102 ),
103 );
104​
105 expect(registry.get(details.threadAtom(ref))).toBeNull();
106 expect(registry.get(details.visibleTurnItemsAtom(ref))).toEqual([]);
107 expect(registry.get(details.statusAtom(ref))).toBe("deleted");
108 expect(registry.get(details.errorAtom(ref))).toBeNull();
109 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
110 registry.dispose();
111 });
112});
113​
114it("does not notify queue consumers for streaming text or execution nodes", () => {
115 const projection = {
116 ...v2Projection,
117 runs: [
118 {
119 id: RunId.make("queued-run"),
120 threadId: v2Projection.thread.id,
121 ordinal: 1,
122 providerInstanceId: v2Projection.thread.providerInstanceId,
123 modelSelection: v2Projection.thread.modelSelection,
124 providerThreadId: null,
125 userMessageId: MessageId.make("queued-message"),
126 rootNodeId: null,
127 activeAttemptId: null,
128 status: "queued" as const,
129 requestedAt: v2Now,
130 startedAt: null,
131 completedAt: null,
132 checkpointId: null,
133 contextHandoffId: null,
134 },
135 ],
136 };
137 const source = Atom.make(
138 AsyncResult.success(
139 threadState({ data: Option.some(projection), status: "live", error: Option.none() }),
140 ),
141 );
142 const details = createEnvironmentThreadDetailAtoms(() => source);
143 const registry = AtomRegistry.make();
144 const dispose = registry.mount(details.queueWorkflowAtom(ref));
145 const before = registry.get(details.queueWorkflowAtom(ref));
146 const disposeCount = registry.mount(details.queuedCountAtom(ref));
147 expect(registry.get(details.queuedCountAtom(ref))).toBe(1);
148 registry.set(
149 source,
150 AsyncResult.success(
151 threadState({
152 data: Option.some({
153 ...projection,
154 nodes: [...v2Projection.nodes],
155 turnItems: [...v2Projection.turnItems],
156 }),
157 status: "live",
158 error: Option.none(),
159 }),
160 ),
161 );
162 expect(registry.get(details.queueWorkflowAtom(ref))).toBe(before);
163 expect(registry.get(details.queuedCountAtom(ref))).toBe(1);
164 registry.set(
165 source,
166 AsyncResult.success(
167 threadState({ data: Option.none(), status: "deleted", error: Option.none() }),
168 ),
169 );
170 expect(registry.get(details.queueWorkflowAtom(ref))).toBeNull();
171 expect(registry.get(details.queuedCountAtom(ref))).toBe(0);
172 disposeCount();
173 dispose();
174 registry.dispose();
175});
176​
177it("does not notify the agents pill for unrelated projection updates", () => {
178 const projection: OrchestrationV2ThreadProjection = {
179 ...v2Projection,
180 runs: [
181 {
182 id: RunId.make("active-run"),
183 threadId: v2Projection.thread.id,
184 ordinal: 1,
185 providerInstanceId: v2Projection.thread.providerInstanceId,
186 modelSelection: v2Projection.thread.modelSelection,
187 providerThreadId: null,
188 userMessageId: MessageId.make("message"),
189 rootNodeId: null,
190 activeAttemptId: null,
191 status: "running" as const,
192 requestedAt: v2Now,
193 startedAt: v2Now,
194 completedAt: null,
195 checkpointId: null,
196 contextHandoffId: null,
197 },
198 ],
199 subagents: [
200 {
201 id: NodeId.make("subagent-1"),
202 threadId: v2Projection.thread.id,
203 runId: RunId.make("active-run"),
204 parentNodeId: NodeId.make("root"),
205 origin: "app_owned",
206 createdBy: "agent",
207 driver: ProviderDriverKind.make("codex"),
208 providerInstanceId: v2Projection.thread.providerInstanceId,
209 providerThreadId: null,
210 childThreadId: ThreadId.make("thread-child"),
211 nativeTaskRef: null,
212 prompt: "Do the thing",
213 title: "Worker",
214 model: null,
215 status: "running",
216 result: null,
217 startedAt: v2Now,
218 completedAt: null,
219 updatedAt: v2Now,
220 },
221 ],
222 };
223 const source = Atom.make(
224 AsyncResult.success(
225 threadState({ data: Option.some(projection), status: "live", error: Option.none() }),
226 ),
227 );
228 const details = createEnvironmentThreadDetailAtoms(() => source);
229 const registry = AtomRegistry.make();
230 const dispose = registry.mount(details.turnSubagentsAtom(ref));
231 const before = registry.get(details.turnSubagentsAtom(ref));
232 expect(before?.liveCount).toBe(1);
233 registry.set(
234 source,
235 AsyncResult.success(
236 threadState({
237 data: Option.some({ ...projection, turnItems: [...v2Projection.turnItems] }),
238 status: "live",
239 error: Option.none(),
240 }),
241 ),
242 );
243 expect(registry.get(details.turnSubagentsAtom(ref))).toBe(before);
244 dispose();
245 registry.dispose();
246});
247​
248it("keeps worktree and pending requests stable during output updates, then reflects resolution", () => {
249 const projection: OrchestrationV2ThreadProjection = {
250 ...v2Projection,
251 thread: { ...v2Projection.thread, worktreePath: "/workspace/task" },
252 runtimeRequests: [
253 {
254 id: RuntimeRequestId.make("approval"),
255 nodeId: NodeId.make("approval-node"),
256 providerTurnId: null,
257 nativeRequestRef: null,
258 kind: "command",
259 status: "pending",
260 responseCapability: { type: "not_resumable", reason: "Provider exited" },
261 createdAt: v2Now,
262 resolvedAt: null,
263 },
264 ],
265 };
266 const source = Atom.make(
267 AsyncResult.success(
268 threadState({ data: Option.some(projection), status: "live", error: Option.none() }),
269 ),
270 );
271 const details = createEnvironmentThreadDetailAtoms(() => source);
272 const registry = AtomRegistry.make();
273 const dispose = registry.mount(details.pendingRequestsAtom(ref));
274 const before = registry.get(details.pendingRequestsAtom(ref));
275 expect(before?.approvals).toHaveLength(1);
276 const streaming: OrchestrationV2ThreadProjection = {
277 ...projection,
278 turnItems: [
279 {
280 id: TurnItemId.make("output"),
281 threadId: projection.thread.id,
282 runId: null,
283 nodeId: null,
284 providerThreadId: null,
285 providerTurnId: null,
286 nativeItemRef: null,
287 parentItemId: null,
288 ordinal: 1,
289 status: "running",
290 title: null,
291 startedAt: v2Now,
292 completedAt: null,
293 updatedAt: v2Now,
294 type: "command_execution",
295 input: "pwd",
296 output: "new output",
297 exitCode: undefined,
298 },
299 ],
300 };
301 registry.set(
302 source,
303 AsyncResult.success(
304 threadState({ data: Option.some(streaming), status: "live", error: Option.none() }),
305 ),
306 );
307 expect(registry.get(details.pendingRequestsAtom(ref))).toBe(before);
308 expect(registry.get(details.worktreePathAtom(ref))).toBe("/workspace/task");
309 registry.set(
310 source,
311 AsyncResult.success(
312 threadState({
313 data: Option.some({
314 ...streaming,
315 thread: { ...projection.thread, worktreePath: "/workspace/moved" },
316 runtimeRequests: projection.runtimeRequests.map((request) => ({
317 ...request,
318 status: "resolved",
319 resolvedAt: v2Now,
320 })),
321 }),
322 status: "live",
323 error: Option.none(),
324 }),
325 ),
326 );
327 expect(registry.get(details.pendingRequestsAtom(ref))?.approvals).toEqual([]);
328 expect(registry.get(details.worktreePathAtom(ref))).toBe("/workspace/moved");
329 dispose();
330 registry.dispose();
331});
332​
333it("rebuilds checkpoint summaries only when checkpoints or messages change", () => {
334 const checkpoint: OrchestrationV2ThreadProjection["checkpoints"][number] = {
335 id: CheckpointId.make("checkpoint-1"),
336 threadId: v2Projection.thread.id,
337 scopeId: CheckpointScopeId.make("scope-1"),
338 runId: RunId.make("run-1"),
339 nodeId: NodeId.make("node-1"),
340 parentCheckpointId: null,
341 ordinalWithinScope: 1,
342 appRunOrdinal: 1,
343 ref: CheckpointRef.make("ref-1"),
344 status: "ready",
345 files: [],
346 capturedAt: v2Now,
347 };
348 const projection: OrchestrationV2ThreadProjection = {
349 ...v2Projection,
350 checkpoints: [checkpoint],
351 };
352 const live = (data: OrchestrationV2ThreadProjection | null) =>
353 AsyncResult.success(
354 threadState({ data: Option.fromNullishOr(data), status: "live", error: Option.none() }),
355 );
356 const sourceAtom = Atom.make(live(projection));
357 const details = createEnvironmentThreadDetailAtoms(() => sourceAtom);
358 const registry = AtomRegistry.make();
359 const dispose = registry.mount(details.checkpointSummariesAtom(ref));
360​
361 const summaries = registry.get(details.checkpointSummariesAtom(ref));
362 expect(summaries.map((summary) => summary.runId)).toEqual(["run-1"]);
363​
364 registry.set(sourceAtom, live({ ...projection, runs: [] }));
365 expect(registry.get(details.checkpointSummariesAtom(ref))).toBe(summaries);
366​
367 registry.set(sourceAtom, live({ ...projection, checkpoints: [] }));
368 expect(registry.get(details.checkpointSummariesAtom(ref))).toEqual([]);
369​
370 registry.set(sourceAtom, live(null));
371 expect(registry.get(details.checkpointSummariesAtom(ref))).toEqual([]);
372 dispose();
373 registry.dispose();
374});