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

threadWorkflows.test.ts

459 lines14.6 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 runs: [
129 {
130 id: "active",
131 status: "running",
132 providerThreadId: "provider-thread",
133 activeAttemptId: "attempt-active",
134 ordinal: 1,
135 },
136 { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
137 {
138 id: "first",
139 status: "queued",
140 userMessageId: "message-first",
141 ordinal: 2,
142 queuePosition: 1,
143 },
144 ],
145 messages: [
146 {
147 id: "message-first",
148 text: "First",
149 attachments: [
150 {
151 type: "image",
152 id: "attachment-first",
153 name: "first.png",
154 mimeType: "image/png",
155 sizeBytes: 64,
156 },
157 ],
158 },
159 { id: "message-later", text: "Later" },
160 ],
161 providerTurns: [
162 {
163 id: "provider-turn-active",
164 runAttemptId: "attempt-active",
165 status: "running",
166 },
167 ],
168 providerThreads: [
169 {
170 id: "provider-thread",
171 appThreadId: "thread",
172 providerSessionId: "provider-session",
173 },
174 ],
175 providerSessions: [
176 {
177 id: "provider-session",
178 status: "running",
179 capabilities: capabilities({ queued: true, interrupt: true, restartSteer: true }),
180 },
181 ],
182 } as never);
183​
184 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
185 ["first", "First"],
186 ["later", "Later"],
187 ]);
188 expect(state.queuedRuns.map(({ attachments }) => attachments.map(({ id }) => id))).toEqual([
189 ["attachment-first"],
190 [],
191 ]);
192 expect(state.activeRun?.id).toBe("active");
193 expect(state.canPromoteToSteer).toBe(true);
194 expect(state.canSendNow).toBe(false);
195 });
196​
197 it.each(["completed", "interrupted", "cancelled", "failed"])(
198 "allows sending a held message after a %s turn without a live session",
199 (status) => {
200 const state = deriveThreadQueueWorkflowState({
201 thread: { id: "thread", activeProviderThreadId: null },
202 runs: [
203 { id: "previous", status, ordinal: 1 },
204 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2, queueHeld: true },
205 ],
206 messages: [{ id: "message", text: "Send this" }],
207 providerThreads: [],
208 providerTurns: [],
209 providerSessions: [],
210 } as never);
211​
212 expect(state.canSendNow).toBe(true);
213 expect(state.canPromoteToSteer).toBe(false);
214 expect(state.isHeld).toBe(true);
215 },
216 );
217​
218 it.each(["preparing", "starting", "running", "waiting"])(
219 "does not offer send now while a turn is %s",
220 (status) => {
221 const state = deriveThreadQueueWorkflowState({
222 thread: { id: "thread", activeProviderThreadId: null },
223 runs: [{ id: "active", status, ordinal: 1 }],
224 messages: [],
225 providerThreads: [],
226 providerTurns: [],
227 providerSessions: [],
228 } as never);
229​
230 expect(state.canSendNow).toBe(false);
231 },
232 );
233​
234 it("offers to skip a running compaction even when the provider cannot steer", () => {
235 const state = deriveThreadQueueWorkflowState({
236 thread: { id: "thread", activeProviderThreadId: null },
237 runs: [
238 { id: "compact", status: "running", userMessageId: "message-compact", ordinal: 1 },
239 { id: "queued", status: "queued", userMessageId: "message-queued", ordinal: 2 },
240 ],
241 messages: [
242 { id: "message-compact", role: "user", text: "/compact", attachments: [] },
243 { id: "message-queued", role: "user", text: "Next", attachments: [] },
244 ],
245 providerThreads: [],
246 providerTurns: [],
247 providerSessions: [],
248 } as never);
249​
250 expect(state.canPromoteToSteer).toBe(true);
251 expect(state.promoteSkipsCompaction).toBe(true);
252 });
253​
254 it("keeps held messages visible and clears the hold when they leave the queue", () => {
255 for (const status of ["queued", "cancelled", "starting"] as const) {
256 const state = deriveThreadQueueWorkflowState({
257 thread: { id: "thread", activeProviderThreadId: null },
258 runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
259 messages: [{ id: "message", text: "Saved message" }],
260 providerThreads: [],
261 providerTurns: [],
262 providerSessions: [],
263 } as never);
264 expect(state.isHeld).toBe(status === "queued");
265 expect(state.queuedRuns.map(({ text }) => text)).toEqual(
266 status === "queued" ? ["Saved message"] : [],
267 );
268 }
269 });
270​
271 it("hides automatic completion delivery from the visible queue", () => {
272 const state = deriveThreadQueueWorkflowState({
273 thread: { id: "thread", activeProviderThreadId: null },
274 runs: [
275 {
276 id: "provider-wake",
277 status: "queued",
278 userMessageId: "provider-wake",
279 ordinal: 4,
280 queuePosition: 3,
281 },
282 {
283 id: "automatic",
284 status: "queued",
285 userMessageId: "message-automatic",
286 ordinal: 2,
287 queuePosition: 1,
288 },
289 {
290 id: "visible",
291 status: "queued",
292 userMessageId: "message-visible",
293 ordinal: 3,
294 queuePosition: 2,
295 },
296 ],
297 messages: [
298 {
299 id: "provider-wake",
300 text: "Model-facing text",
301 notification: {
302 source: { kind: "monitor" },
303 outcome: "updated",
304 summary: "Monitor updated",
305 },
306 },
307 {
308 id: "message-automatic",
309 text: "A delegated task reached a terminal state.",
310 delegatedCompletion: {
311 generation: 1,
312 parentRunId: "run:parent",
313 taskIds: ["task:child"],
314 },
315 },
316 { id: "message-visible", text: "Visible queued message" },
317 ],
318 providerTurns: [],
319 providerThreads: [],
320 providerSessions: [],
321 } as never);
322​
323 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
324 ["visible", "Visible queued message"],
325 ]);
326 });
327​
328 it("removes only the promoted head from the visible queue", () => {
329 const state = deriveThreadQueueWorkflowState({
330 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
331 runs: [
332 {
333 id: "promoted",
334 status: "starting",
335 userMessageId: "message-promoted",
336 providerThreadId: "provider-thread",
337 ordinal: 2,
338 queuePosition: null,
339 },
340 {
341 id: "still-queued",
342 status: "queued",
343 userMessageId: "message-still-queued",
344 providerThreadId: "provider-thread",
345 ordinal: 3,
346 queuePosition: 2,
347 },
348 ],
349 messages: [
350 { id: "message-promoted", text: "Run now" },
351 { id: "message-still-queued", text: "Wait longer" },
352 ],
353 providerTurns: [],
354 providerThreads: [],
355 providerSessions: [],
356 } as never);
357​
358 expect(state.activeRun?.id).toBe("promoted");
359 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
360 ["still-queued", "Wait longer"],
361 ]);
362 });
363​
364 it.each(["preparing", "starting", "waiting"] as const)(
365 "defers steering during setup and blocks it while waiting (%s)",
366 (status) => {
367 const state = deriveThreadQueueWorkflowState({
368 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
369 runs: [
370 {
371 id: "active",
372 status,
373 providerThreadId: "provider-thread",
374 activeAttemptId: "attempt-active",
375 ordinal: 1,
376 },
377 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
378 ],
379 messages: [{ id: "message", text: "Queued" }],
380 providerTurns: [
381 {
382 id: "provider-turn-active",
383 runAttemptId: "attempt-active",
384 status: status === "waiting" ? "completed" : "starting",
385 },
386 ],
387 providerThreads: [
388 {
389 id: "provider-thread",
390 appThreadId: "thread",
391 providerSessionId: "provider-session",
392 },
393 ],
394 providerSessions: [
395 {
396 id: "provider-session",
397 status: "running",
398 capabilities: capabilities({ queued: true, steer: true }),
399 },
400 ],
401 } as never);
402​
403 expect(state.canPromoteToSteer).toBe(status !== "waiting");
404 },
405 );
406​
407 it("allows native, portable, and capability-unknown exact-run forks", () => {
408 const projectedItem = {
409 item: { type: "assistant_message", runId: "run", status: "completed" },
410 } as never;
411​
412 expect(
413 canForkProjectedAssistantItem({
414 projectedItem,
415 capabilities: capabilities({ nativeFork: true }),
416 }),
417 ).toBe(true);
418 expect(
419 canForkProjectedAssistantItem({
420 projectedItem,
421 capabilities: capabilities({ portableFork: true }),
422 }),
423 ).toBe(true);
424 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
425 expect(
426 canForkProjectedAssistantItem({
427 projectedItem: {
428 item: { type: "assistant_message", runId: "run", status: "running" },
429 } as never,
430 }),
431 ).toBe(false);
432 });
433​
434 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
435 const projection = {
436 runs: [
437 { id: "newest-queued", status: "queued", ordinal: 3 },
438 { id: "older-completed", status: "completed", ordinal: 1 },
439 { id: "newest-finished", status: "waiting", ordinal: 2 },
440 ],
441 } as never;
442​
443 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
444 });
445​
446 it.each(["preparing", "starting", "running"] as const)(
447 "does not merge older history while a newer run is %s",
448 (status) => {
449 const projection = {
450 runs: [
451 { id: "older-completed", status: "completed", ordinal: 1 },
452 { id: "newer-active", status, ordinal: 2 },
453 ],
454 } as never;
455​
456 expect(resolveLatestMergeBackRun(projection)).toBeNull();
457 },
458 );
459});