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

threadWorkflows.test.ts

383 lines11.6 KB

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