Open History to see the last commit
1import {
2 EnvironmentId,
3 NodeId,
4 RuntimeRequestId,
5 TurnItemId,
6 type OrchestrationV2ThreadProjection,
7 ThreadId,
8 RunId,
9 MessageId,
10 type ScopedThreadRef,
11} from "@t3tools/contracts";
12import * as Option from "effect/Option";
13import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
14import { describe, expect, it } from "vite-plus/test";
15
16import { v2Projection, v2Now } from "./orchestrationV2TestFixtures.ts";
17import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
18import { createEnvironmentThreadDetailAtoms } from "./threadDetail.ts";
19import type { EnvironmentThreadState } from "./threads.ts";
20
21const ref: ScopedThreadRef = {
22 environmentId: EnvironmentId.make("environment-detail"),
23 threadId: ThreadId.make(v2Projection.thread.id),
24};
25
26function threadState(
27 partial: Pick<EnvironmentThreadState, "data" | "status" | "error"> &
28 Partial<Pick<EnvironmentThreadState, "history">>,
29): EnvironmentThreadState {
30 return { ...partial, history: partial.history ?? EMPTY_THREAD_HISTORY_META };
31}
32
33describe("createEnvironmentThreadDetailAtoms", () => {
34 it("adds environment scope while preserving the pristine projection", () => {
35 const initial: AsyncResult.AsyncResult<EnvironmentThreadState, never> = AsyncResult.success(
36 threadState({
37 data: Option.some(v2Projection),
38 status: "cached",
39 error: Option.none(),
40 }),
41 );
42 const sourceAtom = Atom.make(initial);
43 const details = createEnvironmentThreadDetailAtoms(() => sourceAtom);
44 const registry = AtomRegistry.make();
45
46 const thread = registry.get(details.threadAtom(ref));
47 expect(thread).toEqual({ environmentId: ref.environmentId, projection: v2Projection });
48 expect(thread?.projection).toBe(v2Projection);
49 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
50 expect(registry.get(details.statusAtom(ref))).toBe("cached");
51 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
52
53 registry.set(
54 sourceAtom,
55 AsyncResult.success(
56 threadState({
57 data: Option.some(v2Projection),
58 status: "synchronizing",
59 error: Option.none(),
60 }),
61 ),
62 );
63
64 expect(registry.get(details.threadAtom(ref))).toBe(thread);
65 expect(registry.get(details.statusAtom(ref))).toBe("synchronizing");
66
67 const progressiveHistory = {
68 ...EMPTY_THREAD_HISTORY_META,
69 historyCursor: "cursor-1",
70 hasMoreHistory: true,
71 };
72 registry.set(
73 sourceAtom,
74 AsyncResult.success(
75 threadState({
76 data: Option.some(v2Projection),
77 status: "live",
78 error: Option.some("Stream interrupted."),
79 history: progressiveHistory,
80 }),
81 ),
82 );
83
84 expect(registry.get(details.threadAtom(ref))).toBe(thread);
85 expect(registry.get(details.visibleTurnItemsAtom(ref))).toBe(v2Projection.visibleTurnItems);
86 expect(registry.get(details.statusAtom(ref))).toBe("live");
87 expect(registry.get(details.errorAtom(ref))).toBe("Stream interrupted.");
88 expect(registry.get(details.historyAtom(ref))).toBe(progressiveHistory);
89
90 registry.set(
91 sourceAtom,
92 AsyncResult.success(
93 threadState({
94 data: Option.none(),
95 status: "deleted",
96 error: Option.none(),
97 }),
98 ),
99 );
100
101 expect(registry.get(details.threadAtom(ref))).toBeNull();
102 expect(registry.get(details.visibleTurnItemsAtom(ref))).toEqual([]);
103 expect(registry.get(details.statusAtom(ref))).toBe("deleted");
104 expect(registry.get(details.errorAtom(ref))).toBeNull();
105 expect(registry.get(details.historyAtom(ref))).toBe(EMPTY_THREAD_HISTORY_META);
106 registry.dispose();
107 });
108});
109
110it("does not notify queue consumers for streaming text or execution nodes", () => {
111 const projection = {
112 ...v2Projection,
113 runs: [
114 {
115 id: RunId.make("queued-run"),
116 threadId: v2Projection.thread.id,
117 ordinal: 1,
118 providerInstanceId: v2Projection.thread.providerInstanceId,
119 modelSelection: v2Projection.thread.modelSelection,
120 providerThreadId: null,
121 userMessageId: MessageId.make("queued-message"),
122 rootNodeId: null,
123 activeAttemptId: null,
124 status: "queued" as const,
125 requestedAt: v2Now,
126 startedAt: null,
127 completedAt: null,
128 checkpointId: null,
129 contextHandoffId: null,
130 },
131 ],
132 };
133 const source = Atom.make(
134 AsyncResult.success(
135 threadState({ data: Option.some(projection), status: "live", error: Option.none() }),
136 ),
137 );
138 const details = createEnvironmentThreadDetailAtoms(() => source);
139 const registry = AtomRegistry.make();
140 const dispose = registry.mount(details.queueWorkflowAtom(ref));
141 const before = registry.get(details.queueWorkflowAtom(ref));
142 const disposeCount = registry.mount(details.queuedCountAtom(ref));
143 expect(registry.get(details.queuedCountAtom(ref))).toBe(1);
144 registry.set(
145 source,
146 AsyncResult.success(
147 threadState({
148 data: Option.some({
149 ...projection,
150 nodes: [...v2Projection.nodes],
151 turnItems: [...v2Projection.turnItems],
152 }),
153 status: "live",
154 error: Option.none(),
155 }),
156 ),
157 );
158 expect(registry.get(details.queueWorkflowAtom(ref))).toBe(before);
159 expect(registry.get(details.queuedCountAtom(ref))).toBe(1);
160 registry.set(
161 source,
162 AsyncResult.success(
163 threadState({ data: Option.none(), status: "deleted", error: Option.none() }),
164 ),
165 );
166 expect(registry.get(details.queueWorkflowAtom(ref))).toBeNull();
167 expect(registry.get(details.queuedCountAtom(ref))).toBe(0);
168 disposeCount();
169 dispose();
170 registry.dispose();
171});
172
173it("keeps worktree and pending requests stable during output updates, then reflects resolution", () => {
174 const projection: OrchestrationV2ThreadProjection = {
175 ...v2Projection,
176 thread: { ...v2Projection.thread, worktreePath: "/workspace/task" },
177 runtimeRequests: [
178 {
179 id: RuntimeRequestId.make("approval"),
180 nodeId: NodeId.make("approval-node"),
181 providerTurnId: null,
182 nativeRequestRef: null,
183 kind: "command",
184 status: "pending",
185 responseCapability: { type: "not_resumable", reason: "Provider exited" },
186 createdAt: v2Now,
187 resolvedAt: null,
188 },
189 ],
190 };
191 const source = Atom.make(
192 AsyncResult.success(
193 threadState({ data: Option.some(projection), status: "live", error: Option.none() }),
194 ),
195 );
196 const details = createEnvironmentThreadDetailAtoms(() => source);
197 const registry = AtomRegistry.make();
198 const dispose = registry.mount(details.pendingRequestsAtom(ref));
199 const before = registry.get(details.pendingRequestsAtom(ref));
200 expect(before?.approvals).toHaveLength(1);
201 const streaming: OrchestrationV2ThreadProjection = {
202 ...projection,
203 turnItems: [
204 {
205 id: TurnItemId.make("output"),
206 threadId: projection.thread.id,
207 runId: null,
208 nodeId: null,
209 providerThreadId: null,
210 providerTurnId: null,
211 nativeItemRef: null,
212 parentItemId: null,
213 ordinal: 1,
214 status: "running",
215 title: null,
216 startedAt: v2Now,
217 completedAt: null,
218 updatedAt: v2Now,
219 type: "command_execution",
220 input: "pwd",
221 output: "new output",
222 exitCode: undefined,
223 },
224 ],
225 };
226 registry.set(
227 source,
228 AsyncResult.success(
229 threadState({ data: Option.some(streaming), status: "live", error: Option.none() }),
230 ),
231 );
232 expect(registry.get(details.pendingRequestsAtom(ref))).toBe(before);
233 expect(registry.get(details.worktreePathAtom(ref))).toBe("/workspace/task");
234 registry.set(
235 source,
236 AsyncResult.success(
237 threadState({
238 data: Option.some({
239 ...streaming,
240 thread: { ...projection.thread, worktreePath: "/workspace/moved" },
241 runtimeRequests: projection.runtimeRequests.map((request) => ({
242 ...request,
243 status: "resolved",
244 resolvedAt: v2Now,
245 })),
246 }),
247 status: "live",
248 error: Option.none(),
249 }),
250 ),
251 );
252 expect(registry.get(details.pendingRequestsAtom(ref))?.approvals).toEqual([]);
253 expect(registry.get(details.worktreePathAtom(ref))).toBe("/workspace/moved");
254 dispose();
255 registry.dispose();
256});