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

threadWorkflows.test.ts

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