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

threadWorkflows.test.ts

345 lines10.3 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 });
139​
140 it("keeps held messages visible and clears the hold when they leave the queue", () => {
141 for (const status of ["queued", "cancelled", "starting"] as const) {
142 const state = deriveThreadQueueWorkflowState({
143 thread: { id: "thread", activeProviderThreadId: null },
144 runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
145 messages: [{ id: "message", text: "Saved message" }],
146 providerThreads: [],
147 providerTurns: [],
148 providerSessions: [],
149 } as never);
150 expect(state.isHeld).toBe(status === "queued");
151 expect(state.queuedRuns.map(({ text }) => text)).toEqual(
152 status === "queued" ? ["Saved message"] : [],
153 );
154 }
155 });
156​
157 it("hides automatic completion delivery from the visible queue", () => {
158 const state = deriveThreadQueueWorkflowState({
159 thread: { id: "thread", activeProviderThreadId: null },
160 runs: [
161 {
162 id: "provider-wake",
163 status: "queued",
164 userMessageId: "provider-wake",
165 ordinal: 4,
166 queuePosition: 3,
167 },
168 {
169 id: "automatic",
170 status: "queued",
171 userMessageId: "message-automatic",
172 ordinal: 2,
173 queuePosition: 1,
174 },
175 {
176 id: "visible",
177 status: "queued",
178 userMessageId: "message-visible",
179 ordinal: 3,
180 queuePosition: 2,
181 },
182 ],
183 messages: [
184 {
185 id: "provider-wake",
186 text: "Model-facing text",
187 notification: {
188 source: { kind: "monitor" },
189 outcome: "updated",
190 summary: "Monitor updated",
191 },
192 },
193 {
194 id: "message-automatic",
195 text: "A delegated task reached a terminal state.",
196 delegatedCompletion: {
197 generation: 1,
198 parentRunId: "run:parent",
199 taskIds: ["task:child"],
200 },
201 },
202 { id: "message-visible", text: "Visible queued message" },
203 ],
204 providerTurns: [],
205 providerThreads: [],
206 providerSessions: [],
207 } as never);
208​
209 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
210 ["visible", "Visible queued message"],
211 ]);
212 });
213​
214 it("removes only the promoted head from the visible queue", () => {
215 const state = deriveThreadQueueWorkflowState({
216 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
217 runs: [
218 {
219 id: "promoted",
220 status: "starting",
221 userMessageId: "message-promoted",
222 providerThreadId: "provider-thread",
223 ordinal: 2,
224 queuePosition: null,
225 },
226 {
227 id: "still-queued",
228 status: "queued",
229 userMessageId: "message-still-queued",
230 providerThreadId: "provider-thread",
231 ordinal: 3,
232 queuePosition: 2,
233 },
234 ],
235 messages: [
236 { id: "message-promoted", text: "Run now" },
237 { id: "message-still-queued", text: "Wait longer" },
238 ],
239 providerTurns: [],
240 providerThreads: [],
241 providerSessions: [],
242 } as never);
243​
244 expect(state.activeRun?.id).toBe("promoted");
245 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
246 ["still-queued", "Wait longer"],
247 ]);
248 });
249​
250 it.each(["preparing", "starting", "waiting"] as const)(
251 "does not promote queued work into a %s run",
252 (status) => {
253 const state = deriveThreadQueueWorkflowState({
254 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
255 runs: [
256 {
257 id: "active",
258 status,
259 providerThreadId: "provider-thread",
260 activeAttemptId: "attempt-active",
261 ordinal: 1,
262 },
263 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
264 ],
265 messages: [{ id: "message", text: "Queued" }],
266 providerTurns: [
267 {
268 id: "provider-turn-active",
269 runAttemptId: "attempt-active",
270 status: status === "waiting" ? "completed" : "starting",
271 },
272 ],
273 providerThreads: [
274 {
275 id: "provider-thread",
276 appThreadId: "thread",
277 providerSessionId: "provider-session",
278 },
279 ],
280 providerSessions: [
281 {
282 id: "provider-session",
283 status: "running",
284 capabilities: capabilities({ queued: true, steer: true }),
285 },
286 ],
287 } as never);
288​
289 expect(state.canPromoteToSteer).toBe(false);
290 },
291 );
292​
293 it("allows native, portable, and capability-unknown exact-run forks", () => {
294 const projectedItem = {
295 item: { type: "assistant_message", runId: "run", status: "completed" },
296 } as never;
297​
298 expect(
299 canForkProjectedAssistantItem({
300 projectedItem,
301 capabilities: capabilities({ nativeFork: true }),
302 }),
303 ).toBe(true);
304 expect(
305 canForkProjectedAssistantItem({
306 projectedItem,
307 capabilities: capabilities({ portableFork: true }),
308 }),
309 ).toBe(true);
310 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
311 expect(
312 canForkProjectedAssistantItem({
313 projectedItem: {
314 item: { type: "assistant_message", runId: "run", status: "running" },
315 } as never,
316 }),
317 ).toBe(false);
318 });
319​
320 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
321 const projection = {
322 runs: [
323 { id: "newest-queued", status: "queued", ordinal: 3 },
324 { id: "older-completed", status: "completed", ordinal: 1 },
325 { id: "newest-finished", status: "waiting", ordinal: 2 },
326 ],
327 } as never;
328​
329 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
330 });
331​
332 it.each(["preparing", "starting", "running"] as const)(
333 "does not merge older history while a newer run is %s",
334 (status) => {
335 const projection = {
336 runs: [
337 { id: "older-completed", status: "completed", ordinal: 1 },
338 { id: "newer-active", status, ordinal: 2 },
339 ],
340 } as never;
341​
342 expect(resolveLatestMergeBackRun(projection)).toBeNull();
343 },
344 );
345});