Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts";
3
4import {
5 canForkProjectedAssistantItem,
6 deriveThreadQueueWorkflowState,
7 getUserQueuedThreadRuns,
8 resolveLatestMergeBackRun,
9 threadCanSteer,
10 threadSupportsProviderHandoff,
11} from "./threadWorkflows.ts";
12
13const capabilities = (input?: {
14 readonly queued?: boolean;
15 readonly steer?: boolean;
16 readonly restartSteer?: boolean;
17 readonly interrupt?: boolean;
18 readonly nativeFork?: boolean;
19 readonly portableFork?: boolean;
20}) =>
21 ({
22 turns: {
23 supportsQueuedMessages: input?.queued ?? false,
24 supportsActiveSteering: input?.steer ?? false,
25 supportsSteeringByInterruptRestart: input?.restartSteer ?? false,
26 supportsInterrupt: input?.interrupt ?? false,
27 },
28 threads: {
29 canForkThread: input?.nativeFork ?? false,
30 canForkFromTurn: input?.nativeFork ?? false,
31 },
32 identity: { nativeThreadIds: input?.nativeFork ? "strong" : "none" },
33 context: { supportsFullThreadHandoff: input?.portableFork ?? false },
34 }) as never;
35
36describe("thread workflows", () => {
37 it.each([
38 ["queue only", { queued: true }, false, false],
39 ["session still attaching", undefined, false, true],
40 ["native steering", { steer: true }, false, true],
41 ["interrupt and restart", { interrupt: true, restartSteer: true }, false, true],
42 ["restart without interrupt", { restartSteer: true }, false, false],
43 ["explicit compaction", { steer: true }, true, false],
44 ] as const)("gates composer steering for %s", (_, turns, compactCommandIsActive, expected) => {
45 const projection = {
46 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
47 runs: [{ id: "active", status: "running", providerThreadId: "provider-thread" }],
48 providerThreads: [{ id: "provider-thread", providerSessionId: "session" }],
49 providerSessions: [
50 ...(turns === undefined
51 ? []
52 : [{ id: "session", status: "running", capabilities: capabilities(turns) }]),
53 { id: "other", status: "ready", capabilities: capabilities({ steer: true }) },
54 ],
55 messages: [],
56 } as unknown as OrchestrationV2ThreadProjection;
57 expect(
58 threadCanSteer(projection, {
59 compactCommandIsActive,
60 compactionRunning: false,
61 activeRunId: projection.runs[0]!.id,
62 }),
63 ).toBe(expected);
64 });
65
66 it("skips message history classification when no runs are queued", () => {
67 let metadataReads = 0;
68 const message = {
69 id: "message",
70 get delegatedCompletion() {
71 metadataReads++;
72 return undefined;
73 },
74 };
75 const projection = {
76 runs: [{ id: "active", status: "running", userMessageId: "message", ordinal: 1 }],
77 messages: [message],
78 } as never;
79
80 expect(getUserQueuedThreadRuns(projection)).toEqual([]);
81 expect(metadataReads).toBe(0);
82 });
83
84 it("allows a completed thread to switch providers after its session detaches", () => {
85 const projection = {
86 thread: {
87 id: "thread",
88 activeProviderThreadId: "provider-thread",
89 modelSelection: { instanceId: "cursor", model: "grok-4.7" },
90 },
91 runs: [{ id: "run", status: "completed" }],
92 providerSessions: [],
93 providerThreads: [
94 {
95 id: "provider-thread",
96 appThreadId: "thread",
97 providerInstanceId: "cursor",
98 providerSessionId: "detached-session",
99 nativeThreadRef: { driver: "cursor", nativeId: "agent-123", strength: "strong" },
100 },
101 ],
102 } as unknown as OrchestrationV2ThreadProjection;
103
104 expect(threadSupportsProviderHandoff(projection)).toBe(true);
105 expect(
106 threadSupportsProviderHandoff({
107 ...projection,
108 runs: [{ ...projection.runs[0]!, status: "running" }],
109 }),
110 ).toBe(false);
111 expect(
112 threadSupportsProviderHandoff({
113 ...projection,
114 providerThreads: [{ ...projection.providerThreads[0]!, nativeThreadRef: null }],
115 }),
116 ).toBe(true);
117 expect(threadSupportsProviderHandoff({ ...projection, providerThreads: [] })).toBe(false);
118 });
119
120 it("sorts queued messages and gates promotion from capabilities", () => {
121 const state = deriveThreadQueueWorkflowState({
122 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
123 runs: [
124 {
125 id: "active",
126 status: "running",
127 providerThreadId: "provider-thread",
128 activeAttemptId: "attempt-active",
129 ordinal: 1,
130 },
131 { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
132 {
133 id: "first",
134 status: "queued",
135 userMessageId: "message-first",
136 ordinal: 2,
137 queuePosition: 1,
138 },
139 ],
140 messages: [
141 {
142 id: "message-first",
143 text: "First",
144 attachments: [
145 {
146 type: "image",
147 id: "attachment-first",
148 name: "first.png",
149 mimeType: "image/png",
150 sizeBytes: 64,
151 },
152 ],
153 },
154 { id: "message-later", text: "Later" },
155 ],
156 providerTurns: [
157 {
158 id: "provider-turn-active",
159 runAttemptId: "attempt-active",
160 status: "running",
161 },
162 ],
163 providerThreads: [
164 {
165 id: "provider-thread",
166 appThreadId: "thread",
167 providerSessionId: "provider-session",
168 },
169 ],
170 providerSessions: [
171 {
172 id: "provider-session",
173 status: "running",
174 capabilities: capabilities({ queued: true, interrupt: true, restartSteer: true }),
175 },
176 ],
177 } as never);
178
179 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
180 ["first", "First"],
181 ["later", "Later"],
182 ]);
183 expect(state.queuedRuns.map(({ attachments }) => attachments.map(({ id }) => id))).toEqual([
184 ["attachment-first"],
185 [],
186 ]);
187 expect(state.activeRun?.id).toBe("active");
188 expect(state.canPromoteToSteer).toBe(true);
189 expect(state.canSendNow).toBe(false);
190 });
191
192 it.each(["completed", "interrupted", "cancelled", "failed"])(
193 "allows sending a held message after a %s turn without a live session",
194 (status) => {
195 const state = deriveThreadQueueWorkflowState({
196 thread: { id: "thread", activeProviderThreadId: null },
197 runs: [
198 { id: "previous", status, ordinal: 1 },
199 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2, queueHeld: true },
200 ],
201 messages: [{ id: "message", text: "Send this" }],
202 providerThreads: [],
203 providerTurns: [],
204 providerSessions: [],
205 } as never);
206
207 expect(state.canSendNow).toBe(true);
208 expect(state.canPromoteToSteer).toBe(false);
209 expect(state.isHeld).toBe(true);
210 },
211 );
212
213 it.each(["preparing", "starting", "running", "waiting"])(
214 "does not offer send now while a turn is %s",
215 (status) => {
216 const state = deriveThreadQueueWorkflowState({
217 thread: { id: "thread", activeProviderThreadId: null },
218 runs: [{ id: "active", status, ordinal: 1 }],
219 messages: [],
220 providerThreads: [],
221 providerTurns: [],
222 providerSessions: [],
223 } as never);
224
225 expect(state.canSendNow).toBe(false);
226 },
227 );
228
229 it("offers to skip a running compaction even when the provider cannot steer", () => {
230 const state = deriveThreadQueueWorkflowState({
231 thread: { id: "thread", activeProviderThreadId: null },
232 runs: [
233 { id: "compact", status: "running", userMessageId: "message-compact", ordinal: 1 },
234 { id: "queued", status: "queued", userMessageId: "message-queued", ordinal: 2 },
235 ],
236 messages: [
237 { id: "message-compact", role: "user", text: "/compact", attachments: [] },
238 { id: "message-queued", role: "user", text: "Next", attachments: [] },
239 ],
240 providerThreads: [],
241 providerTurns: [],
242 providerSessions: [],
243 } as never);
244
245 expect(state.canPromoteToSteer).toBe(true);
246 expect(state.promoteSkipsCompaction).toBe(true);
247 });
248
249 it("keeps held messages visible and clears the hold when they leave the queue", () => {
250 for (const status of ["queued", "cancelled", "starting"] as const) {
251 const state = deriveThreadQueueWorkflowState({
252 thread: { id: "thread", activeProviderThreadId: null },
253 runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
254 messages: [{ id: "message", text: "Saved message" }],
255 providerThreads: [],
256 providerTurns: [],
257 providerSessions: [],
258 } as never);
259 expect(state.isHeld).toBe(status === "queued");
260 expect(state.queuedRuns.map(({ text }) => text)).toEqual(
261 status === "queued" ? ["Saved message"] : [],
262 );
263 }
264 });
265
266 it("hides automatic completion delivery from the visible queue", () => {
267 const state = deriveThreadQueueWorkflowState({
268 thread: { id: "thread", activeProviderThreadId: null },
269 runs: [
270 {
271 id: "provider-wake",
272 status: "queued",
273 userMessageId: "provider-wake",
274 ordinal: 4,
275 queuePosition: 3,
276 },
277 {
278 id: "automatic",
279 status: "queued",
280 userMessageId: "message-automatic",
281 ordinal: 2,
282 queuePosition: 1,
283 },
284 {
285 id: "visible",
286 status: "queued",
287 userMessageId: "message-visible",
288 ordinal: 3,
289 queuePosition: 2,
290 },
291 ],
292 messages: [
293 {
294 id: "provider-wake",
295 text: "Model-facing text",
296 notification: {
297 source: { kind: "monitor" },
298 outcome: "updated",
299 summary: "Monitor updated",
300 },
301 },
302 {
303 id: "message-automatic",
304 text: "A delegated task reached a terminal state.",
305 delegatedCompletion: {
306 generation: 1,
307 parentRunId: "run:parent",
308 taskIds: ["task:child"],
309 },
310 },
311 { id: "message-visible", text: "Visible queued message" },
312 ],
313 providerTurns: [],
314 providerThreads: [],
315 providerSessions: [],
316 } as never);
317
318 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
319 ["visible", "Visible queued message"],
320 ]);
321 });
322
323 it("removes only the promoted head from the visible queue", () => {
324 const state = deriveThreadQueueWorkflowState({
325 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
326 runs: [
327 {
328 id: "promoted",
329 status: "starting",
330 userMessageId: "message-promoted",
331 providerThreadId: "provider-thread",
332 ordinal: 2,
333 queuePosition: null,
334 },
335 {
336 id: "still-queued",
337 status: "queued",
338 userMessageId: "message-still-queued",
339 providerThreadId: "provider-thread",
340 ordinal: 3,
341 queuePosition: 2,
342 },
343 ],
344 messages: [
345 { id: "message-promoted", text: "Run now" },
346 { id: "message-still-queued", text: "Wait longer" },
347 ],
348 providerTurns: [],
349 providerThreads: [],
350 providerSessions: [],
351 } as never);
352
353 expect(state.activeRun?.id).toBe("promoted");
354 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
355 ["still-queued", "Wait longer"],
356 ]);
357 });
358
359 it.each(["preparing", "starting", "waiting"] as const)(
360 "defers steering during setup and blocks it while waiting (%s)",
361 (status) => {
362 const state = deriveThreadQueueWorkflowState({
363 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
364 runs: [
365 {
366 id: "active",
367 status,
368 providerThreadId: "provider-thread",
369 activeAttemptId: "attempt-active",
370 ordinal: 1,
371 },
372 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
373 ],
374 messages: [{ id: "message", text: "Queued" }],
375 providerTurns: [
376 {
377 id: "provider-turn-active",
378 runAttemptId: "attempt-active",
379 status: status === "waiting" ? "completed" : "starting",
380 },
381 ],
382 providerThreads: [
383 {
384 id: "provider-thread",
385 appThreadId: "thread",
386 providerSessionId: "provider-session",
387 },
388 ],
389 providerSessions: [
390 {
391 id: "provider-session",
392 status: "running",
393 capabilities: capabilities({ queued: true, steer: true }),
394 },
395 ],
396 } as never);
397
398 expect(state.canPromoteToSteer).toBe(status !== "waiting");
399 },
400 );
401
402 it("allows native, portable, and capability-unknown exact-run forks", () => {
403 const projectedItem = {
404 item: { type: "assistant_message", runId: "run", status: "completed" },
405 } as never;
406
407 expect(
408 canForkProjectedAssistantItem({
409 projectedItem,
410 capabilities: capabilities({ nativeFork: true }),
411 }),
412 ).toBe(true);
413 expect(
414 canForkProjectedAssistantItem({
415 projectedItem,
416 capabilities: capabilities({ portableFork: true }),
417 }),
418 ).toBe(true);
419 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
420 expect(
421 canForkProjectedAssistantItem({
422 projectedItem: {
423 item: { type: "assistant_message", runId: "run", status: "running" },
424 } as never,
425 }),
426 ).toBe(false);
427 });
428
429 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
430 const projection = {
431 runs: [
432 { id: "newest-queued", status: "queued", ordinal: 3 },
433 { id: "older-completed", status: "completed", ordinal: 1 },
434 { id: "newest-finished", status: "waiting", ordinal: 2 },
435 ],
436 } as never;
437
438 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
439 });
440
441 it.each(["preparing", "starting", "running"] as const)(
442 "does not merge older history while a newer run is %s",
443 (status) => {
444 const projection = {
445 runs: [
446 { id: "older-completed", status: "completed", ordinal: 1 },
447 { id: "newer-active", status, ordinal: 2 },
448 ],
449 } as never;
450
451 expect(resolveLatestMergeBackRun(projection)).toBeNull();
452 },
453 );
454});