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

threadExecution.test.ts

769 lines25.7 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 { usageLimitBlockedRun } from "@t3tools/shared/orchestrationV2ThreadError";
18import { describe, expect, it } from "vite-plus/test";
19​
20import { v2Projection } from "./orchestrationV2TestFixtures.ts";
21import {
22 presentPendingBackgroundWork,
23 presentProviderGoal,
24 deriveReportedModelSelection,
25 deriveLatestThreadRun,
26 deriveProviderSubagentStatus,
27 formatModelSelectionEffort,
28 formatProviderSubagentStatus,
29 deriveRunlessWorkStartedAt,
30 deriveThreadActivityRun,
31 deriveThreadRuntime,
32 threadRuntimeHasInterruptibleRun,
33} from "./threadExecution.ts";
34import { threadRuntimeCanArchive, type ThreadRuntimeSummary } from "./models.ts";
35​
36const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
37​
38function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
39 return {
40 id: RunId.make(id),
41 threadId: v2Projection.thread.id,
42 ordinal,
43 providerInstanceId: v2Projection.thread.providerInstanceId,
44 modelSelection: v2Projection.thread.modelSelection,
45 providerThreadId: null,
46 userMessageId: MessageId.make(`message-${id}`),
47 rootNodeId: null,
48 activeAttemptId: null,
49 status,
50 requestedAt: now,
51 startedAt: status === "queued" ? null : now,
52 completedAt: null,
53 checkpointId: null,
54 contextHandoffId: null,
55 };
57​
58describe("thread execution presentation", () => {
59 it("derives the current root failure without inheriting errors from children or previous runs", () => {
60 const failed = { ...run("limited", 1, "failed"), rootNodeId: NodeId.make("root") };
61 const item = {
62 id: TurnItemId.make("limit-error"),
63 threadId: v2Projection.thread.id,
64 runId: failed.id,
65 nodeId: failed.rootNodeId,
66 providerThreadId: null,
67 providerTurnId: null,
68 nativeItemRef: null,
69 parentItemId: null,
70 ordinal: 1,
71 type: "error" as const,
72 status: "failed" as const,
73 title: "Usage limit reached",
74 startedAt: now,
75 completedAt: now,
76 updatedAt: now,
77 failure: {
78 class: "usage_limit" as const,
79 message: "Plan limit reached",
80 code: "usageLimitExceeded",
81 retryable: null,
82 },
83 };
84 const projection = { ...v2Projection, runs: [failed], turnItems: [item] };
85 expect(deriveThreadRuntime(projection)).toMatchObject({
86 lastError: "Plan limit reached",
87 lastErrorClass: "usage_limit",
88 });
89 expect(
90 deriveThreadRuntime({
91 ...projection,
92 turnItems: [{ ...item, nodeId: NodeId.make("child") }],
93 }),
94 ).toMatchObject({ lastError: null, lastErrorClass: null });
95 expect(
96 deriveThreadRuntime({
97 ...projection,
98 runs: [{ ...failed, rootNodeId: NodeId.make("new-root") }],
99 }),
100 ).toMatchObject({ lastError: null, lastErrorClass: null });
101 expect(
102 deriveThreadRuntime({ ...projection, runs: [failed, run("new", 2, "running")] }),
103 ).toMatchObject({ status: "running", lastError: null, lastErrorClass: null });
104 });
105​
106 it("counts a wake run's activity from the start of the work it continues", () => {
107 const workStartedAt = DateTime.makeUnsafe("2026-07-28T09:20:00.000Z");
108 const wake = { ...run("wake", 2, "running"), workStartedAt };
109 expect(
110 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "completed"), wake] })
111 ?.activityStartedAt,
112 ).toBe("2026-07-28T09:20:00.000Z");
113 expect(
114 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "running")] })
115 ?.activityStartedAt,
116 ).toBe("2026-07-28T10:00:00.000Z");
117 });
118​
119 it("keeps a subscription limit visible while later messages stay queued", () => {
120 const failed = {
121 ...run("limited", 1, "failed"),
122 rootNodeId: NodeId.make("root"),
123 completedAt: now,
124 };
125 const queued = run("queued", 2, "queued");
126 const item = {
127 id: TurnItemId.make("limit-error"),
128 threadId: v2Projection.thread.id,
129 runId: failed.id,
130 nodeId: failed.rootNodeId,
131 providerThreadId: null,
132 providerTurnId: null,
133 nativeItemRef: null,
134 parentItemId: null,
135 ordinal: 1,
136 type: "error" as const,
137 status: "failed" as const,
138 title: "Usage limit reached",
139 startedAt: now,
140 completedAt: now,
141 updatedAt: now,
142 failure: {
143 class: "usage_limit" as const,
144 message: "Plan limit reached",
145 code: "usageLimitExceeded",
146 retryable: null,
147 },
148 };
149 const projection = { ...v2Projection, runs: [failed, queued], turnItems: [item] };
150​
151 expect(deriveLatestThreadRun(projection)?.runId).toBe(failed.id);
152 expect(deriveThreadActivityRun(projection)?.runId).toBe(failed.id);
153 expect(deriveThreadRuntime(projection)).toMatchObject({
154 status: "failed",
155 lastError: "Plan limit reached",
156 lastErrorClass: "usage_limit",
157 });
158 const cancelledQueued = {
159 ...run("cancelled-queued", 3, "cancelled"),
160 startedAt: null,
161 completedAt: now,
162 };
163 expect(
164 usageLimitBlockedRun([failed, queued, cancelledQueued], projection.turnItems, null)?.id,
165 ).toBe(failed.id);
166 expect(
167 deriveThreadRuntime({ ...projection, runs: [failed, queued, cancelledQueued] }),
168 ).toMatchObject({ status: "failed", lastErrorClass: "usage_limit" });
169 expect(
170 deriveThreadRuntime({
171 ...projection,
172 turnItems: [{ ...item, failure: { ...item.failure, class: "provider_error" as const } }],
173 }),
174 ).toMatchObject({ status: "queued", lastErrorClass: null });
175 });
176​
177 it("keeps live activity attached to an executing run when a newer run is queued", () => {
178 const runningRun = run("run-running", 1, "running");
179 const queuedRun = run("run-queued", 2, "queued");
180 const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
181​
182 expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
183 expect(deriveThreadActivityRun(projection)).toMatchObject({
184 runId: runningRun.id,
185 status: "running",
186 });
187​
188 const runtime = deriveThreadRuntime(projection);
189 expect(runtime).toMatchObject({
190 status: "running",
191 activeRunId: runningRun.id,
192 });
193 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
194 });
195​
196 it("marks an open run whose provider turn already ended", () => {
197 const attemptId = RunAttemptId.make("attempt-open");
198 const openRun = { ...run("run-open", 1, "running"), activeAttemptId: attemptId };
199 const providerTurn = (status: "running" | "failed") => ({
200 id: ProviderTurnId.make("provider-turn-open"),
201 providerThreadId: ProviderThreadId.make("provider-thread-open"),
202 nodeId: NodeId.make("node-open"),
203 runAttemptId: attemptId,
204 nativeTurnRef: null,
205 ordinal: 1,
206 status,
207 startedAt: now,
208 completedAt: status === "running" ? null : now,
209 });
210 const projection = (status: "running" | "failed") => ({
211 ...v2Projection,
212 runs: [openRun],
213 providerTurns: [providerTurn(status)],
214 updatedAt: now,
215 });
216​
217 expect(deriveThreadActivityRun(projection("running"))?.turnEnded).toBeUndefined();
218 expect(deriveThreadActivityRun(projection("failed"))).toMatchObject({
219 status: "running",
220 turnEnded: true,
221 });
222 });
223​
224 it("presents a held queue as the stopped run instead of queued work", () => {
225 const interrupted = { ...run("run-interrupted", 1, "interrupted"), completedAt: now };
226 const held = { ...run("run-held", 2, "queued"), queueHeld: true };
227 const projection = { ...v2Projection, runs: [interrupted, held], updatedAt: now };
228​
229 expect(deriveLatestThreadRun(projection)?.runId).toBe(interrupted.id);
230 expect(deriveThreadActivityRun(projection)?.runId).toBe(interrupted.id);
231 expect(deriveThreadRuntime(projection)).toMatchObject({
232 status: "interrupted",
233 activeRunId: null,
234 });
235​
236 const resumed = { ...projection, runs: [interrupted, { ...held, queueHeld: false }] };
237 expect(deriveThreadRuntime(resumed)).toMatchObject({ status: "queued" });
238​
239 const onlyHeld = {
240 ...projection,
241 runs: [held],
242 thread: { ...projection.thread, activeProviderThreadId: null },
243 };
244 expect(deriveLatestThreadRun(onlyHeld)).toBeNull();
245 expect(deriveThreadRuntime(onlyHeld)).toBeNull();
246 });
247​
248 it("does not expose a queued-only run as interruptible", () => {
249 const queuedRun = run("run-queued", 1, "queued");
250 const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
251​
252 expect(deriveThreadActivityRun(projection)).toMatchObject({
253 runId: queuedRun.id,
254 status: "queued",
255 });
256​
257 const runtime = deriveThreadRuntime(projection);
258 expect(runtime).toMatchObject({
259 status: "queued",
260 activeRunId: null,
261 });
262 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
263 });
264​
265 it("keeps checkpoint-wait activity visible without exposing a non-functional interrupt", () => {
266 const waitingRun = run("run-waiting", 1, "waiting");
267 const projection = { ...v2Projection, runs: [waitingRun], updatedAt: now };
268​
269 expect(deriveThreadActivityRun(projection)).toMatchObject({
270 runId: waitingRun.id,
271 status: "waiting",
272 });
273​
274 const runtime = deriveThreadRuntime(projection);
275 expect(runtime).toMatchObject({
276 status: "waiting",
277 activeRunId: null,
278 });
279 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
280 });
281​
282 it("does not expose a stale active run after the runtime parks at idle", () => {
283 const runtime = {
284 status: "idle" as const,
285 activeRunId: RunId.make("run-stale"),
286 providerInstanceId: v2Projection.thread.providerInstanceId,
287 providerName: null,
288 lastError: null,
289 updatedAt: DateTime.formatIso(now),
290 };
291​
292 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
293 });
294​
295 it.each(["preparing", "starting"] as const)("keeps an active %s run interruptible", (status) => {
296 const runtime = {
297 status,
298 activeRunId: RunId.make(`run-${status}`),
299 providerInstanceId: v2Projection.thread.providerInstanceId,
300 providerName: null,
301 lastError: null,
302 updatedAt: DateTime.formatIso(now),
303 };
304​
305 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
306 });
307});
308​
309describe("deriveRunlessWorkStartedAt", () => {
310 const later = DateTime.makeUnsafe("2026-07-28T10:05:00.000Z");
311 const rootTurn = (
312 status: OrchestrationV2ExecutionNode["status"],
313 startedAt = now,
314 ): OrchestrationV2ExecutionNode => ({
315 id: NodeId.make("child-root"),
316 threadId: v2Projection.thread.id,
317 runId: null,
318 parentNodeId: null,
319 rootNodeId: NodeId.make("child-root"),
320 kind: "root_turn",
321 status,
322 countsForRun: false,
323 providerThreadId: null,
324 providerTurnId: null,
325 nativeItemRef: null,
326 runtimeRequestId: null,
327 checkpointScopeId: null,
328 startedAt,
329 completedAt: null,
330 });
331​
332 const nativeChild = {
333 ...v2Projection,
334 thread: {
335 ...v2Projection.thread,
336 creationSource: "provider" as const,
337 lineage: {
338 parentThreadId: ThreadId.make("parent"),
339 relationshipToParent: "subagent" as const,
340 rootThreadId: ThreadId.make("parent"),
341 },
342 },
343 };
344​
345 it("times a provider-native subagent from its runless root turn while it works", () => {
346 const projection = { ...nativeChild, nodes: [rootTurn("running", later)] };
347 expect(deriveRunlessWorkStartedAt(projection)).toBe("2026-07-28T10:05:00.000Z");
348 expect(deriveThreadRuntime(projection)).toBeNull();
349 });
350​
351 it.each(["completed", "cancelled", "failed", "interrupted", "idle"] as const)(
352 "is idle once the subagent is %s",
353 (status) => {
354 expect(deriveRunlessWorkStartedAt({ ...nativeChild, nodes: [rootTurn(status)] })).toBe(null);
355 },
356 );
357​
358 it("ignores root turns that belong to a run, and threads the provider does not run", () => {
359 const owned = { ...rootTurn("running"), runId: RunId.make("run-1") };
360 expect(deriveRunlessWorkStartedAt({ ...nativeChild, nodes: [owned] })).toBeNull();
361 expect(
362 deriveRunlessWorkStartedAt({ ...v2Projection, nodes: [rootTurn("running")] }),
363 ).toBeNull();
364 });
365});
366​
367describe("deriveProviderSubagentStatus", () => {
368 const root = {
369 id: NodeId.make("child-root"),
370 threadId: v2Projection.thread.id,
371 runId: null,
372 parentNodeId: null,
373 rootNodeId: NodeId.make("child-root"),
374 kind: "root_turn" as const,
375 status: "completed" as const,
376 countsForRun: false,
377 providerThreadId: null,
378 providerTurnId: null,
379 nativeItemRef: null,
380 runtimeRequestId: null,
381 checkpointScopeId: null,
382 startedAt: now,
383 completedAt: now,
384 };
385 const child = (creationSource: "provider" | "mcp") => ({
386 ...v2Projection,
387 thread: {
388 ...v2Projection.thread,
389 creationSource,
390 lineage: {
391 parentThreadId: ThreadId.make("parent"),
392 relationshipToParent: "subagent" as const,
393 rootThreadId: ThreadId.make("parent"),
394 },
395 },
396 nodes: [root],
397 });
398​
399 it("reports the provider's own subagent from its runless root turn", () => {
400 expect(deriveProviderSubagentStatus(child("provider"))).toEqual({
401 status: "completed",
402 startedAt: "2026-07-28T10:00:00.000Z",
403 completedAt: "2026-07-28T10:00:00.000Z",
404 });
405 });
406​
407 it("says how long the subagent has worked, or took", () => {
408 const startedAt = "2026-07-28T10:00:00.000Z";
409 const at = (iso: string) => Date.parse(iso);
410 expect(
411 formatProviderSubagentStatus(
412 { status: "running", startedAt, completedAt: null },
413 at("2026-07-28T10:01:05.400Z"),
414 ),
415 ).toBe("Working 1m 5s");
416 expect(
417 formatProviderSubagentStatus(
418 { status: "completed", startedAt, completedAt: "2026-07-28T10:00:34.000Z" },
419 at("2026-07-28T11:00:00.000Z"),
420 ),
421 ).toBe("Completed in 34s");
422 expect(
423 formatProviderSubagentStatus(
424 { status: "cancelled", startedAt, completedAt: "2026-07-28T10:00:34.000Z" },
425 0,
426 ),
427 ).toBe("Cancelled");
428 expect(formatProviderSubagentStatus(null, 0)).toBe("Starting");
429 });
430​
431 it("leaves T3 delegated tasks and ordinary threads alone", () => {
432 expect(deriveProviderSubagentStatus(child("mcp"))).toBeNull();
433 expect(deriveProviderSubagentStatus({ ...v2Projection, nodes: [root] })).toBeNull();
434 });
435});
436​
437describe("formatModelSelectionEffort", () => {
438 const instanceId = ProviderInstanceId.make("claudeAgent");
439 const selection = (options?: ReadonlyArray<{ id: string; value: string }>) => ({
440 instanceId,
441 model: "claude-sonnet-5",
442 ...(options === undefined ? {} : { options }),
443 });
444 const catalog = (descriptor: { currentValue?: string }) => [
445 {
446 slug: "claude-sonnet-5",
447 name: "Claude Sonnet 5",
448 isCustom: false,
449 capabilities: {
450 optionDescriptors: [
451 {
452 id: "effort",
453 label: "Reasoning",
454 type: "select" as const,
455 options: [
456 { id: "medium", label: "Medium" },
457 { id: "high", label: "High", isDefault: true },
458 { id: "xhigh", label: "Extra High" },
459 ],
460 ...descriptor,
461 },
462 ],
463 },
464 },
465 ];
466​
467 it("names stored and matching reported effort, preferring the stored choice", () => {
468 expect(
469 formatModelSelectionEffort(selection([{ id: "effort", value: "xhigh" }]), catalog({})),
470 ).toBe("Extra High");
471 const reported = selection([{ id: "effort", value: "xhigh" }]);
472 expect(formatModelSelectionEffort(selection(), catalog({}), reported)).toBe("Extra High");
473 expect(
474 formatModelSelectionEffort(
475 selection([{ id: "effort", value: "medium" }]),
476 catalog({}),
477 reported,
478 ),
479 ).toBe("Medium");
480 expect(
481 formatModelSelectionEffort(
482 selection([{ id: "effort", value: "retired-effort" }]),
483 catalog({ currentValue: "medium" }),
484 reported,
485 ),
486 ).toBeNull();
487 });
488​
489 it("shows nothing when neither the selection nor the provider states an effort", () => {
490 expect(formatModelSelectionEffort(selection(), catalog({}))).toBeNull();
491 expect(formatModelSelectionEffort(selection(), catalog({ currentValue: "medium" }))).toBeNull();
492 expect(
493 formatModelSelectionEffort(selection(), catalog({}), {
494 ...selection([{ id: "effort", value: "xhigh" }]),
495 model: "claude-opus-5-5",
496 }),
497 ).toBeNull();
498 expect(
499 formatModelSelectionEffort(selection(), catalog({}), {
500 ...selection([{ id: "effort", value: "xhigh" }]),
501 instanceId: ProviderInstanceId.make("claudeAgent-other"),
502 }),
503 ).toBeNull();
504 });
505​
506 it("shows nothing for a model the catalog does not describe", () => {
507 expect(formatModelSelectionEffort(selection([{ id: "effort", value: "high" }]))).toBeNull();
508 expect(
509 formatModelSelectionEffort(
510 { ...selection(), model: "claude-haiku-4-5" },
511 catalog({ currentValue: "medium" }),
512 ),
513 ).toBeNull();
514 });
515});
516​
517describe("threadRuntimeCanArchive", () => {
518 const runtime = (
519 status: ThreadRuntimeSummary["status"],
520 activeRunId: ThreadRuntimeSummary["activeRunId"],
521 ): ThreadRuntimeSummary => ({
522 status,
523 activeRunId,
524 providerInstanceId: v2Projection.thread.providerInstanceId,
525 providerName: null,
526 lastError: null,
527 updatedAt: DateTime.formatIso(now),
528 });
529​
530 it.each(["preparing", "starting", "running"] as const)(
531 "blocks archive while a provider is %s",
532 (status) => {
533 expect(threadRuntimeCanArchive(runtime(status, RunId.make(`run-${status}`)))).toBe(false);
534 },
535 );
536​
537 it("only blocks a queued runtime when a provider run remains attached", () => {
538 expect(threadRuntimeCanArchive(runtime("queued", RunId.make("run-queued")))).toBe(false);
539 expect(threadRuntimeCanArchive(runtime("queued", null))).toBe(true);
540 });
541​
542 it("allows waiting and idle threads", () => {
543 expect(threadRuntimeCanArchive(runtime("waiting", RunId.make("run-finished")))).toBe(true);
544 expect(threadRuntimeCanArchive(runtime("idle", null))).toBe(true);
545 });
546});
547​
548describe("presentPendingBackgroundWork", () => {
549 it.each(["Subagent:", "Subagent: "])(
550 "falls back to the subagent noun when %s has no display name",
551 (description) => {
552 const presentation = presentPendingBackgroundWork([
553 { taskId: "unnamed", kind: "subagent", description },
554 ]);
555​
556 expect(presentation?.title).toBe("Waiting on a subagent");
557 expect(presentation?.items[0]?.label).toBe("subagent");
558 },
559 );
560​
561 it.each([
562 "/root/luna_window_properties",
563 "Subagent: /root/luna_window_properties",
564 "/root/parent/luna_window_properties",
565 ])("uses the subagent display name for %s", (description) => {
566 const childThreadId = ThreadId.make("thread:luna");
567 const presentation = presentPendingBackgroundWork([
568 { taskId: "luna", kind: "subagent", description, childThreadId },
569 ]);
570​
571 expect(presentation).toEqual({
572 title: "Waiting on subagent Luna Window Properties",
573 items: [{ taskId: "luna", kind: "subagent", label: "Luna Window Properties", childThreadId }],
574 waiting: true,
575 });
576 });
577​
578 it("formats subagent names in a mixed roster and preserves command descriptions", () => {
579 const presentation = presentPendingBackgroundWork([
580 { taskId: "cmd", kind: "command", description: "/root/run_tests" },
581 { taskId: "luna", kind: "subagent", description: "/root/luna_window_properties" },
582 { taskId: "review", kind: "subagent", description: "Review src/math.ts" },
583 ]);
584​
585 expect(presentation?.title).toBe("Waiting on 2 subagents and 1 command");
586 expect(presentation?.items.map((item) => item.label)).toEqual([
587 "Luna Window Properties",
588 "Review src/math.ts",
589 "/root/run_tests",
590 ]);
591 });
592​
593 it("names a single piece of work by kind", () => {
594 expect(
595 presentPendingBackgroundWork([
596 { taskId: "a", kind: "subagent", description: "Review src/math.ts" },
597 ])?.title,
598 ).toBe("Waiting on subagent Review src/math.ts");
599 expect(presentPendingBackgroundWork([{ taskId: "a", kind: "monitor" }])?.title).toBe(
600 "Waiting on a monitor",
601 );
602 expect(presentPendingBackgroundWork([])).toBeNull();
603 });
604​
605 it("says only commands are running, not waited on", () => {
606 expect(
607 presentPendingBackgroundWork([
608 { taskId: "dev", kind: "command", description: "Start the shared dev server" },
609 ]),
610 ).toMatchObject({ title: "Running: Start the shared dev server", waiting: false });
611 expect(presentPendingBackgroundWork([{ taskId: "a", kind: "command" }])).toMatchObject({
612 title: "Running a command",
613 waiting: false,
614 });
615 expect(
616 presentPendingBackgroundWork([
617 { taskId: "a", kind: "command", description: "vp run dev" },
618 { taskId: "b", kind: "command", description: "tailscale serve" },
619 ]),
620 ).toMatchObject({ title: "Running 2 commands", waiting: false });
621 expect(
622 presentPendingBackgroundWork([
623 { taskId: "a", kind: "command", description: "vp run dev" },
624 { taskId: "b", kind: "monitor", description: "Watch PR checks" },
625 ]),
626 ).toMatchObject({ title: "Waiting on 1 command and 1 monitor", waiting: true });
627 });
628​
629 it("groups work by kind, subagents first, and keeps each name", () => {
630 const presentation = presentPendingBackgroundWork([
631 { taskId: "cmd", kind: "command", description: "npm test" },
632 {
633 taskId: "b",
634 kind: "subagent",
635 description: "Write tests",
636 childThreadId: ThreadId.make("thread:b"),
637 },
638 { taskId: "a", kind: "subagent", description: "Review src/math.ts" },
639 ]);
640 expect(presentation?.title).toBe("Waiting on 2 subagents and 1 command");
641 expect(presentation?.items.map((item) => [item.kind, item.label, item.childThreadId])).toEqual([
642 ["subagent", "Write tests", "thread:b"],
643 ["subagent", "Review src/math.ts", undefined],
644 ["command", "npm test", undefined],
645 ]);
646 });
647​
648 it("names generic work, including rosters from servers that predate kinds", () => {
649 expect(
650 presentPendingBackgroundWork([
651 { taskId: "bash", kind: "command", description: "Background sleep" },
652 { taskId: "watch", kind: "monitor" },
653 { taskId: "other", kind: "background_task" },
654 ])?.title,
655 ).toBe("Waiting on 1 command, 1 monitor and 1 background task");
656 expect(presentPendingBackgroundWork([{ taskId: "old", kind: "background_task" }])?.title).toBe(
657 "Waiting on a background task",
658 );
659 });
660});
661​
662describe("provider-reported model selection", () => {
663 const selected = v2Projection.thread.modelSelection;
664 const reported = { ...selected, options: [{ id: "reasoningEffort", value: "default" }] };
665 const providerThread = {
666 id: ProviderThreadId.make("active"),
667 driver: ProviderDriverKind.make("codex"),
668 providerInstanceId: selected.instanceId,
669 providerSessionId: ProviderSessionId.make("session"),
670 appThreadId: v2Projection.thread.id,
671 ownerNodeId: null,
672 nativeThreadRef: null,
673 nativeConversationHeadRef: null,
674 status: "idle" as const,
675 firstRunOrdinal: null,
676 lastRunOrdinal: null,
677 handoffIds: [],
678 forkedFrom: null,
679 createdAt: now,
680 updatedAt: now,
681 nativeMetadata: { modelSelection: reported },
682 };
683 const projection = {
684 ...v2Projection,
685 thread: { ...v2Projection.thread, activeProviderThreadId: providerThread.id },
686 providerThreads: [providerThread],
687 };
688​
689 it("reads only the active provider thread's reported selection", () => {
690 expect(deriveReportedModelSelection(projection)).toBe(reported);
691 expect(
692 deriveReportedModelSelection({
693 ...projection,
694 thread: { ...projection.thread, activeProviderThreadId: null },
695 }),
696 ).toBeNull();
697 expect(
698 deriveReportedModelSelection({
699 ...projection,
700 providerThreads: [
701 { ...providerThread, providerInstanceId: ProviderInstanceId.make("other") },
702 ],
703 }),
704 ).toBeNull();
705 });
706​
707 it("shows the reported default in a subagent's effort label", () => {
708 const models = [
709 {
710 slug: selected.model,
711 name: selected.model,
712 isCustom: false,
713 capabilities: {
714 optionDescriptors: [
715 {
716 id: "variant",
717 label: "Reasoning",
718 type: "select" as const,
719 options: [{ id: "high", label: "High" }],
720 },
721 ],
722 },
723 },
724 ];
725 const variantReport = { ...selected, options: [{ id: "variant", value: "default" }] };
726 expect(formatModelSelectionEffort(selected, models, variantReport)).toBe("Default");
727 expect(formatModelSelectionEffort(selected, models)).toBe("Unknown");
728 });
729});
730​
731describe("presentProviderGoal", () => {
732 it("summarizes Codex accounting and offers resume once the goal stops short", () => {
733 expect(
734 presentProviderGoal(
735 {
736 objective: "Ship the feature",
737 status: "paused",
738 tokensUsed: 12_400,
739 tokenBudget: 50_000,
740 timeUsedSeconds: 245,
741 },
742 false,
743 ),
744 ).toEqual({
745 title: "Goal paused",
746 objective: "Ship the feature",
747 usage: "12k / 50k tokens · 4m 5s",
748 canResume: true,
749 });
750 });
751​
752 it("counts Claude evaluator checks and never offers resume", () => {
753 expect(
754 presentProviderGoal({ objective: "All tests pass", status: "active", checks: 1 }, true),
755 ).toEqual({
756 title: "Pursuing goal",
757 objective: "All tests pass",
758 usage: "1 check",
759 canResume: false,
760 });
761 expect(
762 presentProviderGoal({ objective: "All tests pass", status: "complete", checks: 0 }, false)
763 .usage,
764 ).toBeNull();
765 expect(
766 presentProviderGoal({ objective: "All tests pass", status: "active" }, false).title,
767 ).toBe("Goal set");
768 });
769});