Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 canDetachThreadProviderSession,
5 canForkProjectedAssistantItem,
6 deriveThreadQueueWorkflowState,
7 resolveLatestMergeBackRun,
8} from "./threadWorkflows.ts";
9
10const capabilities = (input?: {
11 readonly queued?: boolean;
12 readonly steer?: boolean;
13 readonly restartSteer?: boolean;
14 readonly nativeFork?: boolean;
15 readonly portableFork?: boolean;
16}) =>
17 ({
18 turns: {
19 supportsQueuedMessages: input?.queued ?? false,
20 supportsActiveSteering: input?.steer ?? false,
21 supportsSteeringByInterruptRestart: input?.restartSteer ?? false,
22 },
23 threads: {
24 canForkThread: input?.nativeFork ?? false,
25 canForkFromTurn: input?.nativeFork ?? false,
26 },
27 identity: { nativeThreadIds: input?.nativeFork ? "strong" : "none" },
28 context: { supportsFullThreadHandoff: input?.portableFork ?? false },
29 }) as never;
30
31describe("thread workflows", () => {
32 it("sorts queued messages and gates reorder and promotion from capabilities", () => {
33 const state = deriveThreadQueueWorkflowState({
34 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
35 runs: [
36 {
37 id: "active",
38 status: "running",
39 providerThreadId: "provider-thread",
40 activeAttemptId: "attempt-active",
41 ordinal: 1,
42 },
43 { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
44 {
45 id: "first",
46 status: "queued",
47 userMessageId: "message-first",
48 ordinal: 2,
49 queuePosition: 1,
50 },
51 ],
52 messages: [
53 {
54 id: "message-first",
55 text: "First",
56 attachments: [
57 {
58 type: "image",
59 id: "attachment-first",
60 name: "first.png",
61 mimeType: "image/png",
62 sizeBytes: 64,
63 },
64 ],
65 },
66 { id: "message-later", text: "Later" },
67 ],
68 providerTurns: [
69 {
70 id: "provider-turn-active",
71 runAttemptId: "attempt-active",
72 status: "running",
73 },
74 ],
75 providerThreads: [
76 {
77 id: "provider-thread",
78 appThreadId: "thread",
79 providerSessionId: "provider-session",
80 },
81 ],
82 providerSessions: [
83 {
84 id: "provider-session",
85 status: "running",
86 capabilities: capabilities({ queued: true, restartSteer: true }),
87 },
88 ],
89 } as never);
90
91 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
92 ["first", "First"],
93 ["later", "Later"],
94 ]);
95 expect(state.queuedRuns.map(({ attachments }) => attachments.map(({ id }) => id))).toEqual([
96 ["attachment-first"],
97 [],
98 ]);
99 expect(state.activeRun?.id).toBe("active");
100 expect(state.canReorder).toBe(true);
101 expect(state.canPromoteToSteer).toBe(true);
102 });
103
104 it("keeps held messages visible and clears the hold when they leave the queue", () => {
105 for (const status of ["queued", "cancelled", "starting"] as const) {
106 const state = deriveThreadQueueWorkflowState({
107 thread: { id: "thread", activeProviderThreadId: null },
108 runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
109 messages: [{ id: "message", text: "Saved message" }],
110 providerThreads: [],
111 providerTurns: [],
112 providerSessions: [],
113 } as never);
114 expect(state.isHeld).toBe(status === "queued");
115 expect(state.queuedRuns.map(({ text }) => text)).toEqual(
116 status === "queued" ? ["Saved message"] : [],
117 );
118 }
119 });
120
121 it("hides automatic completion delivery from the visible queue", () => {
122 const state = deriveThreadQueueWorkflowState({
123 thread: { id: "thread", activeProviderThreadId: null },
124 runs: [
125 {
126 id: "provider-wake",
127 status: "queued",
128 userMessageId: "provider-wake",
129 ordinal: 4,
130 queuePosition: 3,
131 },
132 {
133 id: "automatic",
134 status: "queued",
135 userMessageId: "message-automatic",
136 ordinal: 2,
137 queuePosition: 1,
138 },
139 {
140 id: "visible",
141 status: "queued",
142 userMessageId: "message-visible",
143 ordinal: 3,
144 queuePosition: 2,
145 },
146 ],
147 messages: [
148 {
149 id: "provider-wake",
150 text: "Model-facing text",
151 notification: {
152 source: { kind: "monitor" },
153 outcome: "updated",
154 summary: "Monitor updated",
155 },
156 },
157 {
158 id: "message-automatic",
159 text: "A delegated task reached a terminal state.",
160 delegatedCompletion: {
161 generation: 1,
162 parentRunId: "run:parent",
163 taskIds: ["task:child"],
164 },
165 },
166 { id: "message-visible", text: "Visible queued message" },
167 ],
168 providerTurns: [],
169 providerThreads: [],
170 providerSessions: [],
171 } as never);
172
173 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
174 ["visible", "Visible queued message"],
175 ]);
176 });
177
178 it("removes only the promoted head from the visible queue", () => {
179 const state = deriveThreadQueueWorkflowState({
180 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
181 runs: [
182 {
183 id: "promoted",
184 status: "starting",
185 userMessageId: "message-promoted",
186 providerThreadId: "provider-thread",
187 ordinal: 2,
188 queuePosition: null,
189 },
190 {
191 id: "still-queued",
192 status: "queued",
193 userMessageId: "message-still-queued",
194 providerThreadId: "provider-thread",
195 ordinal: 3,
196 queuePosition: 2,
197 },
198 ],
199 messages: [
200 { id: "message-promoted", text: "Run now" },
201 { id: "message-still-queued", text: "Wait longer" },
202 ],
203 providerTurns: [],
204 providerThreads: [],
205 providerSessions: [],
206 } as never);
207
208 expect(state.activeRun?.id).toBe("promoted");
209 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
210 ["still-queued", "Wait longer"],
211 ]);
212 });
213
214 it.each(["preparing", "starting", "waiting"] as const)(
215 "does not promote queued work into a %s run",
216 (status) => {
217 const state = deriveThreadQueueWorkflowState({
218 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
219 runs: [
220 {
221 id: "active",
222 status,
223 providerThreadId: "provider-thread",
224 activeAttemptId: "attempt-active",
225 ordinal: 1,
226 },
227 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
228 ],
229 messages: [{ id: "message", text: "Queued" }],
230 providerTurns: [
231 {
232 id: "provider-turn-active",
233 runAttemptId: "attempt-active",
234 status: status === "waiting" ? "completed" : "starting",
235 },
236 ],
237 providerThreads: [
238 {
239 id: "provider-thread",
240 appThreadId: "thread",
241 providerSessionId: "provider-session",
242 },
243 ],
244 providerSessions: [
245 {
246 id: "provider-session",
247 status: "running",
248 capabilities: capabilities({ queued: true, steer: true }),
249 },
250 ],
251 } as never);
252
253 expect(state.canPromoteToSteer).toBe(false);
254 },
255 );
256
257 it("does not promote queued work until the running provider turn is projected", () => {
258 const state = deriveThreadQueueWorkflowState({
259 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
260 runs: [
261 {
262 id: "active",
263 status: "running",
264 providerThreadId: "provider-thread",
265 activeAttemptId: "attempt-active",
266 ordinal: 1,
267 },
268 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
269 ],
270 messages: [{ id: "message", text: "Queued" }],
271 providerTurns: [],
272 providerThreads: [
273 {
274 id: "provider-thread",
275 appThreadId: "thread",
276 providerSessionId: "provider-session",
277 },
278 ],
279 providerSessions: [
280 {
281 id: "provider-session",
282 status: "running",
283 capabilities: capabilities({ queued: true, steer: true }),
284 },
285 ],
286 } as never);
287
288 expect(state.canPromoteToSteer).toBe(false);
289 });
290
291 it("does not expose known unsupported queue or fork actions", () => {
292 const projection = {
293 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
294 runs: [{ id: "queued", status: "queued", userMessageId: "message", ordinal: 1 }],
295 messages: [],
296 providerThreads: [
297 {
298 id: "provider-thread",
299 appThreadId: "thread",
300 providerSessionId: "provider-session",
301 },
302 ],
303 providerSessions: [
304 {
305 id: "provider-session",
306 status: "ready",
307 capabilities: capabilities(),
308 },
309 ],
310 } as never;
311 const queue = deriveThreadQueueWorkflowState(projection);
312 const projectedItem = {
313 item: { type: "assistant_message", runId: "run", status: "completed" },
314 } as never;
315
316 expect(queue.canReorder).toBe(false);
317 expect(queue.canPromoteToSteer).toBe(false);
318 expect(canForkProjectedAssistantItem({ projectedItem, capabilities: capabilities() })).toBe(
319 false,
320 );
321 expect(canDetachThreadProviderSession(projection)).toBe(true);
322 });
323
324 it("allows native, portable, and capability-unknown exact-run forks", () => {
325 const projectedItem = {
326 item: { type: "assistant_message", runId: "run", status: "completed" },
327 } as never;
328
329 expect(
330 canForkProjectedAssistantItem({
331 projectedItem,
332 capabilities: capabilities({ nativeFork: true }),
333 }),
334 ).toBe(true);
335 expect(
336 canForkProjectedAssistantItem({
337 projectedItem,
338 capabilities: capabilities({ portableFork: true }),
339 }),
340 ).toBe(true);
341 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
342 expect(
343 canForkProjectedAssistantItem({
344 projectedItem: {
345 item: { type: "assistant_message", runId: "run", status: "running" },
346 } as never,
347 }),
348 ).toBe(false);
349 });
350
351 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
352 const projection = {
353 runs: [
354 { id: "newest-queued", status: "queued", ordinal: 3 },
355 { id: "older-completed", status: "completed", ordinal: 1 },
356 { id: "newest-finished", status: "waiting", ordinal: 2 },
357 ],
358 } as never;
359
360 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
361 });
362
363 it("does not let a stale completed run later in storage order hide the waiting checkpoint", () => {
364 const projection = {
365 runs: [
366 { id: "newest-finished", status: "waiting", ordinal: 2 },
367 { id: "older-completed", status: "completed", ordinal: 1 },
368 ],
369 } as never;
370
371 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
372 });
373
374 it.each(["preparing", "starting", "running"] as const)(
375 "does not merge older history while a newer run is %s",
376 (status) => {
377 const projection = {
378 runs: [
379 { id: "older-completed", status: "completed", ordinal: 1 },
380 { id: "newer-active", status, ordinal: 2 },
381 ],
382 } as never;
383
384 expect(resolveLatestMergeBackRun(projection)).toBeNull();
385 },
386 );
387});