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

threadWorkflows.test.ts

403 lines12.4 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(true);
66 expect(threadSupportsProviderHandoff({ ...projection, providerThreads: [] })).toBe(false);
67 });
68​
69 it("sorts queued messages and gates promotion from capabilities", () => {
70 const state = deriveThreadQueueWorkflowState({
71 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
72 runs: [
73 {
74 id: "active",
75 status: "running",
76 providerThreadId: "provider-thread",
77 activeAttemptId: "attempt-active",
78 ordinal: 1,
79 },
80 { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
81 {
82 id: "first",
83 status: "queued",
84 userMessageId: "message-first",
85 ordinal: 2,
86 queuePosition: 1,
87 },
88 ],
89 messages: [
90 {
91 id: "message-first",
92 text: "First",
93 attachments: [
94 {
95 type: "image",
96 id: "attachment-first",
97 name: "first.png",
98 mimeType: "image/png",
99 sizeBytes: 64,
100 },
101 ],
102 },
103 { id: "message-later", text: "Later" },
104 ],
105 providerTurns: [
106 {
107 id: "provider-turn-active",
108 runAttemptId: "attempt-active",
109 status: "running",
110 },
111 ],
112 providerThreads: [
113 {
114 id: "provider-thread",
115 appThreadId: "thread",
116 providerSessionId: "provider-session",
117 },
118 ],
119 providerSessions: [
120 {
121 id: "provider-session",
122 status: "running",
123 capabilities: capabilities({ queued: true, restartSteer: true }),
124 },
125 ],
126 } as never);
127​
128 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
129 ["first", "First"],
130 ["later", "Later"],
131 ]);
132 expect(state.queuedRuns.map(({ attachments }) => attachments.map(({ id }) => id))).toEqual([
133 ["attachment-first"],
134 [],
135 ]);
136 expect(state.activeRun?.id).toBe("active");
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("offers to skip a running compaction even when the provider cannot steer", () => {
179 const state = deriveThreadQueueWorkflowState({
180 thread: { id: "thread", activeProviderThreadId: null },
181 runs: [
182 { id: "compact", status: "running", userMessageId: "message-compact", ordinal: 1 },
183 { id: "queued", status: "queued", userMessageId: "message-queued", ordinal: 2 },
184 ],
185 messages: [
186 { id: "message-compact", role: "user", text: "/compact", attachments: [] },
187 { id: "message-queued", role: "user", text: "Next", attachments: [] },
188 ],
189 providerThreads: [],
190 providerTurns: [],
191 providerSessions: [],
192 } as never);
193​
194 expect(state.canPromoteToSteer).toBe(true);
195 expect(state.promoteSkipsCompaction).toBe(true);
196 });
197​
198 it("keeps held messages visible and clears the hold when they leave the queue", () => {
199 for (const status of ["queued", "cancelled", "starting"] as const) {
200 const state = deriveThreadQueueWorkflowState({
201 thread: { id: "thread", activeProviderThreadId: null },
202 runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
203 messages: [{ id: "message", text: "Saved message" }],
204 providerThreads: [],
205 providerTurns: [],
206 providerSessions: [],
207 } as never);
208 expect(state.isHeld).toBe(status === "queued");
209 expect(state.queuedRuns.map(({ text }) => text)).toEqual(
210 status === "queued" ? ["Saved message"] : [],
211 );
212 }
213 });
214​
215 it("hides automatic completion delivery from the visible queue", () => {
216 const state = deriveThreadQueueWorkflowState({
217 thread: { id: "thread", activeProviderThreadId: null },
218 runs: [
219 {
220 id: "provider-wake",
221 status: "queued",
222 userMessageId: "provider-wake",
223 ordinal: 4,
224 queuePosition: 3,
225 },
226 {
227 id: "automatic",
228 status: "queued",
229 userMessageId: "message-automatic",
230 ordinal: 2,
231 queuePosition: 1,
232 },
233 {
234 id: "visible",
235 status: "queued",
236 userMessageId: "message-visible",
237 ordinal: 3,
238 queuePosition: 2,
239 },
240 ],
241 messages: [
242 {
243 id: "provider-wake",
244 text: "Model-facing text",
245 notification: {
246 source: { kind: "monitor" },
247 outcome: "updated",
248 summary: "Monitor updated",
249 },
250 },
251 {
252 id: "message-automatic",
253 text: "A delegated task reached a terminal state.",
254 delegatedCompletion: {
255 generation: 1,
256 parentRunId: "run:parent",
257 taskIds: ["task:child"],
258 },
259 },
260 { id: "message-visible", text: "Visible queued message" },
261 ],
262 providerTurns: [],
263 providerThreads: [],
264 providerSessions: [],
265 } as never);
266​
267 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
268 ["visible", "Visible queued message"],
269 ]);
270 });
271​
272 it("removes only the promoted head from the visible queue", () => {
273 const state = deriveThreadQueueWorkflowState({
274 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
275 runs: [
276 {
277 id: "promoted",
278 status: "starting",
279 userMessageId: "message-promoted",
280 providerThreadId: "provider-thread",
281 ordinal: 2,
282 queuePosition: null,
283 },
284 {
285 id: "still-queued",
286 status: "queued",
287 userMessageId: "message-still-queued",
288 providerThreadId: "provider-thread",
289 ordinal: 3,
290 queuePosition: 2,
291 },
292 ],
293 messages: [
294 { id: "message-promoted", text: "Run now" },
295 { id: "message-still-queued", text: "Wait longer" },
296 ],
297 providerTurns: [],
298 providerThreads: [],
299 providerSessions: [],
300 } as never);
301​
302 expect(state.activeRun?.id).toBe("promoted");
303 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
304 ["still-queued", "Wait longer"],
305 ]);
306 });
307​
308 it.each(["preparing", "starting", "waiting"] as const)(
309 "defers steering during setup and blocks it while waiting (%s)",
310 (status) => {
311 const state = deriveThreadQueueWorkflowState({
312 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
313 runs: [
314 {
315 id: "active",
316 status,
317 providerThreadId: "provider-thread",
318 activeAttemptId: "attempt-active",
319 ordinal: 1,
320 },
321 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
322 ],
323 messages: [{ id: "message", text: "Queued" }],
324 providerTurns: [
325 {
326 id: "provider-turn-active",
327 runAttemptId: "attempt-active",
328 status: status === "waiting" ? "completed" : "starting",
329 },
330 ],
331 providerThreads: [
332 {
333 id: "provider-thread",
334 appThreadId: "thread",
335 providerSessionId: "provider-session",
336 },
337 ],
338 providerSessions: [
339 {
340 id: "provider-session",
341 status: "running",
342 capabilities: capabilities({ queued: true, steer: true }),
343 },
344 ],
345 } as never);
346​
347 expect(state.canPromoteToSteer).toBe(status !== "waiting");
348 },
349 );
350​
351 it("allows native, portable, and capability-unknown exact-run forks", () => {
352 const projectedItem = {
353 item: { type: "assistant_message", runId: "run", status: "completed" },
354 } as never;
355​
356 expect(
357 canForkProjectedAssistantItem({
358 projectedItem,
359 capabilities: capabilities({ nativeFork: true }),
360 }),
361 ).toBe(true);
362 expect(
363 canForkProjectedAssistantItem({
364 projectedItem,
365 capabilities: capabilities({ portableFork: true }),
366 }),
367 ).toBe(true);
368 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
369 expect(
370 canForkProjectedAssistantItem({
371 projectedItem: {
372 item: { type: "assistant_message", runId: "run", status: "running" },
373 } as never,
374 }),
375 ).toBe(false);
376 });
377​
378 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
379 const projection = {
380 runs: [
381 { id: "newest-queued", status: "queued", ordinal: 3 },
382 { id: "older-completed", status: "completed", ordinal: 1 },
383 { id: "newest-finished", status: "waiting", ordinal: 2 },
384 ],
385 } as never;
386​
387 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
388 });
389​
390 it.each(["preparing", "starting", "running"] as const)(
391 "does not merge older history while a newer run is %s",
392 (status) => {
393 const projection = {
394 runs: [
395 { id: "older-completed", status: "completed", ordinal: 1 },
396 { id: "newer-active", status, ordinal: 2 },
397 ],
398 } as never;
399​
400 expect(resolveLatestMergeBackRun(projection)).toBeNull();
401 },
402 );
403});