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

threadExecution.test.ts

648 lines21.6 KB

1import {
2 TurnItemId,
3 NodeId,
4 MessageId,
5 ProviderInstanceId,
6 ProviderThreadId,
7 ProviderSessionId,
8 ProviderDriverKind,
9 ProviderTurnId,
10 RunAttemptId,
11 RunId,
12 ThreadId,
13 type OrchestrationV2ExecutionNode,
14 type OrchestrationV2RunStatus,
15} from "@t3tools/contracts";
16import * as DateTime from "effect/DateTime";
17import { createModelCapabilities, createModelSelection } from "@t3tools/shared/model";
18import { usageLimitBlockedRun } from "@t3tools/shared/orchestrationV2ThreadError";
19import { describe, expect, it } from "vite-plus/test";
20​
21import { v2Projection } from "./orchestrationV2TestFixtures.ts";
22import {
23 deriveReportedModelSelection,
24 deriveLatestThreadRun,
25 deriveProviderSubagentStatus,
26 deriveProviderExtensionStatuses,
27 deriveResumableRunId,
28 deriveRunlessWorkStartedAt,
29 deriveThreadActivityRun,
30 deriveThreadRuntime,
31 formatModelSelectionEffort,
32 isBackgroundWorkStopPending,
33 presentPendingBackgroundWork,
34 presentProviderGoal,
35 presentRunStop,
36 threadRuntimeHasInterruptibleRun,
37} from "./threadExecution.ts";
38​
39const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
40​
41function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
42 return {
43 id: RunId.make(id),
44 threadId: v2Projection.thread.id,
45 ordinal,
46 providerInstanceId: v2Projection.thread.providerInstanceId,
47 modelSelection: v2Projection.thread.modelSelection,
48 providerThreadId: null,
49 userMessageId: MessageId.make(`message-${id}`),
50 rootNodeId: null,
51 activeAttemptId: null,
52 status,
53 requestedAt: now,
54 startedAt: status === "queued" ? null : now,
55 completedAt: null,
56 checkpointId: null,
57 contextHandoffId: null,
58 };
60​
61describe("thread execution presentation", () => {
62 it("shows extension statuses only from the selected provider's current live session", () => {
63 const instanceId = v2Projection.thread.modelSelection.instanceId;
64 const providerThreadId = ProviderThreadId.make("native-status-thread");
65 const sessionId = ProviderSessionId.make("native-status-session");
66 const statuses = [{ key: "watch", text: "Watching" }];
67 const projection = {
68 thread: { ...v2Projection.thread, activeProviderThreadId: providerThreadId },
69 providerThreads: [
70 { id: providerThreadId, providerSessionId: sessionId, providerInstanceId: instanceId },
71 ],
72 providerSessions: [
73 {
74 id: sessionId,
75 providerInstanceId: instanceId,
76 status: "ready" as const,
77 extensionStatuses: statuses,
78 },
79 ],
80 };
81 expect(deriveProviderExtensionStatuses(projection)).toEqual(statuses);
82 expect(
83 deriveProviderExtensionStatuses({
84 ...projection,
85 providerSessions: [
86 ...projection.providerSessions,
87 {
88 ...projection.providerSessions[0]!,
89 id: ProviderSessionId.make("unrelated"),
90 extensionStatuses: [{ key: "wrong", text: "Wrong session" }],
91 },
92 ],
93 }),
94 ).toEqual(statuses);
95 expect(
96 deriveProviderExtensionStatuses({
97 ...projection,
98 providerSessions: [{ ...projection.providerSessions[0]!, status: "stopped" }],
99 }),
100 ).toEqual([]);
101 expect(
102 deriveProviderExtensionStatuses({
103 ...projection,
104 providerSessions: [{ ...projection.providerSessions[0]!, status: "error" }],
105 }),
106 ).toEqual(statuses);
107 expect(
108 deriveProviderExtensionStatuses({
109 ...projection,
110 providerSessions: [
111 { ...projection.providerSessions[0]!, status: "error", extensionStatuses: [] },
112 ],
113 }),
114 ).toEqual([]);
115 expect(
116 deriveProviderExtensionStatuses({
117 ...projection,
118 providerSessions: [{ ...projection.providerSessions[0]!, extensionStatuses: undefined }],
119 }),
120 ).toEqual([]);
121 expect(
122 deriveProviderExtensionStatuses({
123 ...projection,
124 thread: {
125 ...projection.thread,
126 modelSelection: { instanceId: ProviderInstanceId.make("other"), model: "default" },
127 },
128 }),
129 ).toEqual([]);
130 expect(deriveProviderExtensionStatuses(null)).toEqual([]);
131 });
132​
133 it("derives the current root failure without inheriting errors from children or previous runs", () => {
134 const failed = { ...run("limited", 1, "failed"), rootNodeId: NodeId.make("root") };
135 const item = {
136 id: TurnItemId.make("limit-error"),
137 threadId: v2Projection.thread.id,
138 runId: failed.id,
139 nodeId: failed.rootNodeId,
140 providerThreadId: null,
141 providerTurnId: null,
142 nativeItemRef: null,
143 parentItemId: null,
144 ordinal: 1,
145 type: "error" as const,
146 status: "failed" as const,
147 title: "Usage limit reached",
148 startedAt: now,
149 completedAt: now,
150 updatedAt: now,
151 failure: {
152 class: "usage_limit" as const,
153 message: "Plan limit reached",
154 code: "usageLimitExceeded",
155 retryable: null,
156 },
157 };
158 const projection = { ...v2Projection, runs: [failed], turnItems: [item] };
159 expect(deriveThreadRuntime(projection)).toMatchObject({
160 lastError: "Plan limit reached",
161 lastErrorClass: "usage_limit",
162 });
163 expect(deriveResumableRunId(projection, deriveThreadRuntime(projection))).toBe(failed.id);
164 expect(
165 deriveThreadRuntime({
166 ...projection,
167 turnItems: [{ ...item, nodeId: NodeId.make("child") }],
168 }),
169 ).toMatchObject({ lastError: null, lastErrorClass: null });
170 expect(
171 deriveThreadRuntime({
172 ...projection,
173 runs: [{ ...failed, rootNodeId: NodeId.make("new-root") }],
174 }),
175 ).toMatchObject({ lastError: null, lastErrorClass: null });
176 expect(
177 deriveThreadRuntime({ ...projection, runs: [failed, run("new", 2, "running")] }),
178 ).toMatchObject({ status: "running", lastError: null, lastErrorClass: null });
179 });
180​
181 // regression: #15029 working timers no longer reset on every background wake
182 it("counts a wake run's activity from the start of the work it continues", () => {
183 const workStartedAt = DateTime.makeUnsafe("2026-07-28T09:20:00.000Z");
184 const wake = { ...run("wake", 2, "running"), workStartedAt };
185 expect(
186 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "completed"), wake] })
187 ?.activityStartedAt,
188 ).toBe("2026-07-28T09:20:00.000Z");
189 expect(
190 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "running")] })
191 ?.activityStartedAt,
192 ).toBe("2026-07-28T10:00:00.000Z");
193 });
194​
195 it("keeps a subscription limit visible while later messages stay queued", () => {
196 const failed = {
197 ...run("limited", 1, "failed"),
198 rootNodeId: NodeId.make("root"),
199 completedAt: now,
200 };
201 const queued = run("queued", 2, "queued");
202 const item = {
203 id: TurnItemId.make("limit-error"),
204 threadId: v2Projection.thread.id,
205 runId: failed.id,
206 nodeId: failed.rootNodeId,
207 providerThreadId: null,
208 providerTurnId: null,
209 nativeItemRef: null,
210 parentItemId: null,
211 ordinal: 1,
212 type: "error" as const,
213 status: "failed" as const,
214 title: "Usage limit reached",
215 startedAt: now,
216 completedAt: now,
217 updatedAt: now,
218 failure: {
219 class: "usage_limit" as const,
220 message: "Plan limit reached",
221 code: "usageLimitExceeded",
222 retryable: null,
223 },
224 };
225 const projection = { ...v2Projection, runs: [failed, queued], turnItems: [item] };
226​
227 expect(deriveLatestThreadRun(projection)?.runId).toBe(failed.id);
228 expect(deriveThreadActivityRun(projection)?.runId).toBe(failed.id);
229 expect(deriveThreadRuntime(projection)).toMatchObject({
230 status: "failed",
231 lastError: "Plan limit reached",
232 lastErrorClass: "usage_limit",
233 });
234 expect(deriveResumableRunId(projection, deriveThreadRuntime(projection))).toBe(failed.id);
235 const cancelledQueued = {
236 ...run("cancelled-queued", 3, "cancelled"),
237 startedAt: null,
238 completedAt: now,
239 };
240 expect(
241 usageLimitBlockedRun([failed, queued, cancelledQueued], projection.turnItems, null)?.id,
242 ).toBe(failed.id);
243 expect(
244 deriveThreadRuntime({ ...projection, runs: [failed, queued, cancelledQueued] }),
245 ).toMatchObject({ status: "failed", lastErrorClass: "usage_limit" });
246 const providerError = {
247 ...projection,
248 turnItems: [{ ...item, failure: { ...item.failure, class: "provider_error" as const } }],
249 };
250 expect(deriveThreadRuntime(providerError)).toMatchObject({
251 status: "queued",
252 lastErrorClass: null,
253 });
254 expect(deriveResumableRunId(providerError, deriveThreadRuntime(providerError))).toBeNull();
255 });
256​
257 it("keeps live activity attached to an executing run when a newer run is queued", () => {
258 const runningRun = run("run-running", 1, "running");
259 const queuedRun = run("run-queued", 2, "queued");
260 const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
261​
262 expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
263 expect(deriveThreadActivityRun(projection)).toMatchObject({
264 runId: runningRun.id,
265 status: "running",
266 });
267​
268 const runtime = deriveThreadRuntime(projection);
269 expect(runtime).toMatchObject({
270 status: "running",
271 activeRunId: runningRun.id,
272 });
273 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
274 });
275​
276 it("marks an open run whose provider turn already ended", () => {
277 const attemptId = RunAttemptId.make("attempt-open");
278 const openRun = { ...run("run-open", 1, "running"), activeAttemptId: attemptId };
279 const providerTurn = (status: "running" | "failed") => ({
280 id: ProviderTurnId.make("provider-turn-open"),
281 providerThreadId: ProviderThreadId.make("provider-thread-open"),
282 nodeId: NodeId.make("node-open"),
283 runAttemptId: attemptId,
284 nativeTurnRef: null,
285 ordinal: 1,
286 status,
287 startedAt: now,
288 completedAt: status === "running" ? null : now,
289 });
290 const projection = (status: "running" | "failed") => ({
291 ...v2Projection,
292 runs: [openRun],
293 providerTurns: [providerTurn(status)],
294 updatedAt: now,
295 });
296​
297 expect(deriveThreadActivityRun(projection("running"))?.turnEnded).toBeUndefined();
298 expect(deriveThreadActivityRun(projection("failed"))).toMatchObject({
299 status: "running",
300 turnEnded: true,
301 });
302 });
303​
304 it("presents a held queue as the stopped run instead of queued work", () => {
305 const interrupted = { ...run("run-interrupted", 1, "interrupted"), completedAt: now };
306 const held = { ...run("run-held", 2, "queued"), queueHeld: true };
307 const projection = { ...v2Projection, runs: [interrupted, held], updatedAt: now };
308​
309 expect(deriveLatestThreadRun(projection)?.runId).toBe(interrupted.id);
310 expect(deriveThreadActivityRun(projection)?.runId).toBe(interrupted.id);
311 expect(deriveThreadRuntime(projection)).toMatchObject({
312 status: "interrupted",
313 activeRunId: null,
314 });
315 expect(deriveResumableRunId(projection, deriveThreadRuntime(projection))).toBe(interrupted.id);
316​
317 const resumed = { ...projection, runs: [interrupted, { ...held, queueHeld: false }] };
318 expect(deriveThreadRuntime(resumed)).toMatchObject({ status: "queued" });
319​
320 const onlyHeld = {
321 ...projection,
322 runs: [held],
323 thread: { ...projection.thread, activeProviderThreadId: null },
324 };
325 expect(deriveLatestThreadRun(onlyHeld)).toBeNull();
326 expect(deriveThreadRuntime(onlyHeld)).toBeNull();
327 });
328​
329 it("does not expose a queued-only run as interruptible", () => {
330 const queuedRun = run("run-queued", 1, "queued");
331 const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
332​
333 expect(deriveThreadActivityRun(projection)).toMatchObject({
334 runId: queuedRun.id,
335 status: "queued",
336 });
337​
338 const runtime = deriveThreadRuntime(projection);
339 expect(runtime).toMatchObject({
340 status: "queued",
341 activeRunId: null,
342 });
343 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
344 });
345});
346​
347describe("presentRunStop", () => {
348 const signal = (type: "run_interrupt_request" | "run_interrupt_result", runId: RunId) => ({
349 id: TurnItemId.make(`${type}:${runId}`),
350 threadId: v2Projection.thread.id,
351 runId,
352 nodeId: null,
353 providerThreadId: null,
354 providerTurnId: null,
355 nativeItemRef: null,
356 parentItemId: null,
357 ordinal: 1,
358 status: type === "run_interrupt_result" ? ("interrupted" as const) : ("completed" as const),
359 title: "Interrupt",
360 startedAt: now,
361 completedAt: now,
362 updatedAt: now,
363 type,
364 message: "Stopped by user",
365 });
366 const running = run("run-stopping", 1, "running");
367 const request = signal("run_interrupt_request", running.id);
368​
369 it.each([
370 { name: "no Stop", runs: [running], turnItems: [], state: null },
371 {
372 name: "a Stop the agent is winding down",
373 runs: [running],
374 turnItems: [request],
375 state: "stopping",
376 },
377 {
378 name: "a Stop the provider never confirmed",
379 runs: [running],
380 turnItems: [{ ...request, unresponsiveAt: now }],
381 state: "unresponsive",
382 },
383 {
384 name: "a Stop that ended the run",
385 runs: [running],
386 turnItems: [request, signal("run_interrupt_result", running.id)],
387 state: null,
388 },
389 {
390 name: "a settled run's background work still stopping",
391 runs: [{ ...running, status: "completed" as const, completedAt: now }],
392 turnItems: [request],
393 state: null,
394 },
395 {
396 name: "a settled run whose background work ignored Stop",
397 runs: [{ ...running, status: "completed" as const, completedAt: now }],
398 turnItems: [{ ...request, unresponsiveAt: now }],
399 state: "unresponsive",
400 },
401 {
402 name: "a settled run whose background work ignored Stop, with a queued follow-up",
403 runs: [
404 { ...running, status: "completed" as const, completedAt: now },
405 run("run-queued", 2, "queued"),
406 ],
407 turnItems: [{ ...request, unresponsiveAt: now }],
408 state: "unresponsive",
409 },
410 ])("presents $name", ({ runs, turnItems, state }) => {
411 expect(presentRunStop({ runs, turnItems })?.state ?? null).toBe(state);
412 });
413});
414​
415describe("deriveRunlessWorkStartedAt", () => {
416 const later = DateTime.makeUnsafe("2026-07-28T10:05:00.000Z");
417 const rootTurn = (
418 status: OrchestrationV2ExecutionNode["status"],
419 startedAt = now,
420 ): OrchestrationV2ExecutionNode => ({
421 id: NodeId.make("child-root"),
422 threadId: v2Projection.thread.id,
423 runId: null,
424 parentNodeId: null,
425 rootNodeId: NodeId.make("child-root"),
426 kind: "root_turn",
427 status,
428 countsForRun: false,
429 providerThreadId: null,
430 providerTurnId: null,
431 nativeItemRef: null,
432 runtimeRequestId: null,
433 checkpointScopeId: null,
434 startedAt,
435 completedAt: null,
436 });
437​
438 const nativeChild = {
439 ...v2Projection,
440 thread: {
441 ...v2Projection.thread,
442 creationSource: "provider" as const,
443 lineage: {
444 parentThreadId: ThreadId.make("parent"),
445 relationshipToParent: "subagent" as const,
446 rootThreadId: ThreadId.make("parent"),
447 },
448 },
449 };
450​
451 it("times a provider-native subagent from its runless root turn while it works", () => {
452 const projection = { ...nativeChild, nodes: [rootTurn("running", later)] };
453 expect(deriveRunlessWorkStartedAt(projection)).toBe("2026-07-28T10:05:00.000Z");
454 expect(deriveThreadRuntime(projection)).toBeNull();
455 });
456​
457 it("ignores root turns that belong to a run, and threads the provider does not run", () => {
458 const owned = { ...rootTurn("running"), runId: RunId.make("run-1") };
459 expect(deriveRunlessWorkStartedAt({ ...nativeChild, nodes: [owned] })).toBeNull();
460 expect(
461 deriveRunlessWorkStartedAt({ ...v2Projection, nodes: [rootTurn("running")] }),
462 ).toBeNull();
463 });
464});
465​
466describe("deriveProviderSubagentStatus", () => {
467 const root = {
468 id: NodeId.make("child-root"),
469 threadId: v2Projection.thread.id,
470 runId: null,
471 parentNodeId: null,
472 rootNodeId: NodeId.make("child-root"),
473 kind: "root_turn" as const,
474 status: "completed" as const,
475 countsForRun: false,
476 providerThreadId: null,
477 providerTurnId: null,
478 nativeItemRef: null,
479 runtimeRequestId: null,
480 checkpointScopeId: null,
481 startedAt: now,
482 completedAt: now,
483 };
484 const child = (creationSource: "provider" | "mcp") => ({
485 ...v2Projection,
486 thread: {
487 ...v2Projection.thread,
488 creationSource,
489 lineage: {
490 parentThreadId: ThreadId.make("parent"),
491 relationshipToParent: "subagent" as const,
492 rootThreadId: ThreadId.make("parent"),
493 },
494 },
495 nodes: [root],
496 });
497​
498 it("leaves T3 delegated tasks and ordinary threads alone", () => {
499 expect(deriveProviderSubagentStatus(child("mcp"))).toBeNull();
500 expect(deriveProviderSubagentStatus({ ...v2Projection, nodes: [root] })).toBeNull();
501 });
502});
503​
504describe("provider-reported model selection", () => {
505 const selected = v2Projection.thread.modelSelection;
506 const reported = { ...selected, options: [{ id: "reasoningEffort", value: "default" }] };
507 const providerThread = {
508 id: ProviderThreadId.make("active"),
509 driver: ProviderDriverKind.make("codex"),
510 providerInstanceId: selected.instanceId,
511 providerSessionId: ProviderSessionId.make("session"),
512 appThreadId: v2Projection.thread.id,
513 ownerNodeId: null,
514 nativeThreadRef: null,
515 nativeConversationHeadRef: null,
516 status: "idle" as const,
517 firstRunOrdinal: null,
518 lastRunOrdinal: null,
519 handoffIds: [],
520 forkedFrom: null,
521 createdAt: now,
522 updatedAt: now,
523 nativeMetadata: { modelSelection: reported },
524 };
525 const projection = {
526 ...v2Projection,
527 thread: { ...v2Projection.thread, activeProviderThreadId: providerThread.id },
528 providerThreads: [providerThread],
529 };
530​
531 it("reads only the active provider thread's reported selection", () => {
532 expect(deriveReportedModelSelection(projection)).toBe(reported);
533 expect(
534 deriveReportedModelSelection({
535 ...projection,
536 thread: { ...projection.thread, activeProviderThreadId: null },
537 }),
538 ).toBeNull();
539 expect(
540 deriveReportedModelSelection({
541 ...projection,
542 providerThreads: [
543 { ...providerThread, providerInstanceId: ProviderInstanceId.make("other") },
544 ],
545 }),
546 ).toBeNull();
547 });
548});
549​
550describe("formatModelSelectionEffort", () => {
551 it("labels an old Ultracode effort by the effort it runs at", () => {
552 const models = [
553 {
554 slug: "claude-opus-5-5",
555 name: "Claude Opus 5.5",
556 isCustom: false,
557 capabilities: createModelCapabilities({
558 optionDescriptors: [
559 {
560 id: "effort",
561 label: "Reasoning",
562 type: "select",
563 options: [
564 { id: "high", label: "High", isDefault: true },
565 { id: "xhigh", label: "Extra High" },
566 ],
567 },
568 { id: "ultracode", label: "Ultracode", type: "boolean" },
569 ],
570 }),
571 },
572 ];
573 const selection = createModelSelection(
574 ProviderInstanceId.make("claudeAgent"),
575 "claude-opus-5-5",
576 [{ id: "effort", value: "ultracode" }],
577 );
578​
579 expect(formatModelSelectionEffort(selection, models)).toBe("Extra High");
580 });
581});
582​
583describe("isBackgroundWorkStopPending", () => {
584 const tasks = [{ taskId: "dev-server" }];
585 const sent = { threadKey: "env:thread", taskIds: ["dev-server"], retryAt: null };
586 const accepted = { ...sent, retryAt: 10_000 };
587​
588 it("stays pending after acceptance while a stopped task is still listed", () => {
589 expect(isBackgroundWorkStopPending(sent, "env:thread", tasks, false)).toBe(true);
590 expect(isBackgroundWorkStopPending(accepted, "env:thread", tasks, false)).toBe(true);
591 });
592​
593 it("ends once the stopped tasks leave, even if new work starts", () => {
594 expect(isBackgroundWorkStopPending(accepted, "env:thread", [], false)).toBe(false);
595 expect(isBackgroundWorkStopPending(accepted, "env:thread", [{ taskId: "next" }], false)).toBe(
596 false,
597 );
598 });
599​
600 it("allows retry at the deadline even if a stopped task remains", () => {
601 const request = { ...accepted, retryAt: 10_000 };
602 expect(isBackgroundWorkStopPending(request, "env:thread", tasks, false)).toBe(true);
603 expect(isBackgroundWorkStopPending(request, "env:thread", tasks, true)).toBe(false);
604 });
605​
606 it("only applies to the thread that sent the Stop", () => {
607 expect(isBackgroundWorkStopPending(null, "env:thread", tasks, false)).toBe(false);
608 expect(isBackgroundWorkStopPending(sent, "env:other", tasks, false)).toBe(false);
609 });
610});
611​
612describe("presentProviderGoal", () => {
613 it("rolls token counts that round to 1000k over to 1m", () => {
614 const usage = (tokensUsed: number) =>
615 presentProviderGoal(
616 { objective: "Ship the feature", status: "active", tokensUsed, tokenBudget: 2_000_000 },
617 true,
618 ).usage;
619 expect(usage(999_499)).toBe("999k / 2m tokens");
620 expect(usage(999_500)).toBe("1m / 2m tokens");
621 expect(usage(999_999)).toBe("1m / 2m tokens");
622 expect(usage(1_000_000)).toBe("1m / 2m tokens");
623 expect(
624 presentProviderGoal(
625 {
626 objective: "Ship the feature",
627 status: "paused",
628 tokensUsed: 999_500,
629 tokenBudget: 999_500,
630 },
631 false,
632 ).usage,
633 ).toBe("1m / 1m tokens");
634 });
635});
636​
637describe("presentPendingBackgroundWork", () => {
638 it("names a command by its script, not the shell that wraps it", () => {
639 const presentation = presentPendingBackgroundWork([
640 {
641 taskId: "call_1",
642 kind: "command",
643 description: `/bin/zsh -lc "mv a.ts b.ts\ncat > b.ts <<'EOF'\nexport {};\nEOF"`,
644 },
645 ]);
646 expect(presentation?.items[0]?.label).toBe("mv a.ts b.ts\ncat > b.ts <<'EOF'\nexport {};\nEOF");
647 });
648});