@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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