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

threadWorkflows.test.ts

504 lines16.1 KB

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