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

orchestrationV2PendingBackgroundWork.test.ts

636 lines19.6 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 type OrchestrationV2PendingBackgroundTask,
4 ProviderInstanceId,
5 ProviderThreadId,
6} from "@t3tools/contracts";
7import {
8 backgroundWorkHoldsCompletion,
9 derivePendingBackgroundWork,
10 turnItemUpdateCanEndBackgroundWork,
11} from "./orchestrationV2PendingBackgroundWork.ts";
12​
13describe("turnItemUpdateCanEndBackgroundWork", () => {
14 it.each([
15 ["a finished subagent", true, { type: "subagent", status: "completed" }],
16 ["a failed background command", true, { type: "command_execution", status: "failed" }],
17 ["an idle dynamic tool", true, { type: "dynamic_tool", status: "idle" }],
18 ["a running command's output", false, { type: "command_execution", status: "running" }],
19 ["a waiting subagent", false, { type: "subagent", status: "waiting" }],
20 ["a finished assistant message", false, { type: "assistant_message", status: "completed" }],
21 ] as const)("%s: %s", (_case, canEnd, item) => {
22 expect(turnItemUpdateCanEndBackgroundWork(item)).toBe(canEnd);
23 });
24});
25​
26describe("backgroundWorkHoldsCompletion", () => {
27 const task = (
28 taskId: string,
29 kind: OrchestrationV2PendingBackgroundTask["kind"],
30 ): OrchestrationV2PendingBackgroundTask => ({ taskId, kind });
31​
32 it.each([
33 ["nothing pending", false, []],
34 ["a dev server", false, [task("dev", "command")]],
35 ["two long-lived shells", false, [task("web", "command"), task("api", "command")]],
36 ["a subagent", true, [task("review", "subagent")]],
37 ["a monitor", true, [task("watch", "monitor")]],
38 ["a command and a monitor", true, [task("dev", "command"), task("watch", "monitor")]],
39 ["a command and a subagent", true, [task("dev", "command"), task("review", "subagent")]],
40 ["work the provider cannot name", true, [task("opaque", "background_task")]],
41 ] as const)("with %s pending, holds completion: %s", (_case, holds, tasks) => {
42 expect(backgroundWorkHoldsCompletion(tasks)).toBe(holds);
43 });
44});
45​
46describe("derivePendingBackgroundWork", () => {
47 it("takes each subagent item's instance from its joined owner", () => {
48 const owner = ProviderInstanceId.make("claude-owner");
49 const tasks = derivePendingBackgroundWork({
50 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
51 providerThreads: [],
52 turnItems: [
53 {
54 id: "owned-item",
55 owningProviderInstanceId: owner,
56 type: "subagent",
57 status: "running",
58 title: null,
59 },
60 { id: "unknown-item", type: "subagent", status: "running", title: null },
61 ],
62 });
63 expect(tasks).toEqual([
64 { taskId: "owned-item", kind: "subagent", providerInstanceId: owner },
65 { taskId: "unknown-item", kind: "subagent" },
66 ]);
67 });
68​
69 it("takes a roster subagent's instance from its owning provider thread", () => {
70 const owner = ProviderInstanceId.make("claude-owner");
71 const tasks = derivePendingBackgroundWork({
72 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
73 providerThreads: [
74 {
75 id: ProviderThreadId.make("pt-owner"),
76 providerInstanceId: owner,
77 pendingBackgroundTasks: [
78 {
79 taskId: "roster-item",
80 kind: "subagent",
81 providerInstanceId: ProviderInstanceId.make("other"),
82 },
83 ],
84 },
85 ],
86 turnItems: [],
87 });
88 expect(tasks).toEqual([{ taskId: "roster-item", kind: "subagent", providerInstanceId: owner }]);
89 });
90​
91 it("returns empty while the latest run is not settled", () => {
92 const tasks = derivePendingBackgroundWork({
93 latestRun: { id: "run-1" as never, ordinal: 1, status: "running" },
94 providerThreads: [
95 {
96 id: "pt-1" as never,
97 pendingBackgroundTasks: [{ taskId: "bg-1", description: "sleep 20", kind: "command" }],
98 },
99 ],
100 turnItems: [
101 {
102 id: "item-1" as never,
103 type: "command_execution",
104 status: "running",
105 title: "npm test",
106 nativeItemRef: null,
107 input: "npm test",
108 },
109 ],
110 });
111 expect(tasks).toEqual([]);
112 });
113​
114 it("does not keep a settled parent waiting for an idle child", () => {
115 expect(
116 derivePendingBackgroundWork({
117 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
118 providerThreads: [],
119 turnItems: [{ id: "idle-child", type: "subagent", status: "idle", title: "Review" }],
120 }),
121 ).toEqual([]);
122 });
123​
124 it("returns pending work when the latest run is waiting (post-success, pre-checkpoint)", () => {
125 const tasks = derivePendingBackgroundWork({
126 latestRun: { id: "run-1" as never, ordinal: 1, status: "waiting" },
127 providerThreads: [{ id: "pt-1" as never }],
128 turnItems: [
129 {
130 id: "item-1" as never,
131 type: "command_execution",
132 status: "running",
133 title: "npm test",
134 nativeItemRef: { nativeId: "cmd-1" },
135 input: "npm test",
136 },
137 ],
138 });
139 expect(tasks).toEqual([{ taskId: "cmd-1", description: "npm test", kind: "command" }]);
140 });
141​
142 it("still returns empty when the latest run is running even with background items", () => {
143 const tasks = derivePendingBackgroundWork({
144 latestRun: { id: "run-1" as never, ordinal: 1, status: "running" },
145 providerThreads: [
146 {
147 id: "pt-1" as never,
148 pendingBackgroundTasks: [{ taskId: "bg-1", description: "sleep 20", kind: "command" }],
149 },
150 ],
151 turnItems: [
152 {
153 id: "item-1" as never,
154 type: "subagent",
155 status: "running",
156 title: "review",
157 nativeItemRef: { nativeId: "sub-1" },
158 prompt: "review",
159 },
160 ],
161 });
162 expect(tasks).toEqual([]);
163 });
164​
165 it("excludes rolled_back items when the latest run is waiting", () => {
166 const tasks = derivePendingBackgroundWork({
167 latestRun: { id: "run-2" as never, ordinal: 2, status: "waiting" },
168 providerThreads: [{ id: "pt-1" as never }],
169 runs: [
170 { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
171 { id: "run-2" as never, ordinal: 2, status: "waiting" },
172 ],
173 turnItems: [
174 {
175 id: "item-old" as never,
176 type: "command_execution",
177 status: "running",
178 title: "from rolled-back run",
179 runId: "run-1",
180 nativeItemRef: { nativeId: "cmd-old" },
181 input: "sleep 99",
182 },
183 {
184 id: "item-new" as never,
185 type: "command_execution",
186 status: "running",
187 title: "still pending",
188 runId: "run-2",
189 nativeItemRef: { nativeId: "cmd-new" },
190 input: "npm test",
191 },
192 ],
193 });
194 expect(tasks).toEqual([
195 {
196 taskId: "cmd-new",
197 description: "still pending",
198 kind: "command",
199 },
200 ]);
201 });
202​
203 it("returns the provider-thread roster after settlement", () => {
204 const tasks = derivePendingBackgroundWork({
205 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
206 providerThreads: [
207 {
208 id: "pt-1" as never,
209 pendingBackgroundTasks: [
210 { taskId: "bg-1", description: "Run Codex review", kind: "command" },
211 ],
212 },
213 ],
214 turnItems: [],
215 activeProviderThreadId: "pt-1",
216 });
217 expect(tasks).toEqual([{ taskId: "bg-1", description: "Run Codex review", kind: "command" }]);
218 });
219​
220 it("includes nonterminal turn items and excludes completed ones", () => {
221 const tasks = derivePendingBackgroundWork({
222 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
223 providerThreads: [{ id: "pt-1" as never }],
224 turnItems: [
225 {
226 id: "item-1" as never,
227 type: "command_execution",
228 status: "running",
229 title: "npm test",
230 nativeItemRef: { nativeId: "cmd-1" },
231 input: "npm test",
232 },
233 {
234 id: "item-2" as never,
235 type: "command_execution",
236 status: "completed",
237 title: "done",
238 nativeItemRef: { nativeId: "cmd-2" },
239 input: "echo done",
240 },
241 ],
242 });
243 expect(tasks).toEqual([{ taskId: "cmd-1", description: "npm test", kind: "command" }]);
244 });
245​
246 it("trims normalized background-work descriptions", () => {
247 const base = {
248 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" as const },
249 providerThreads: [
250 {
251 id: "pt-1" as never,
252 pendingBackgroundTasks: [
253 {
254 taskId: "native-task",
255 description: " native background work ",
256 kind: "background_task" as const,
257 },
258 ],
259 },
260 ],
261 };
262 expect(
263 derivePendingBackgroundWork({
264 ...base,
265 turnItems: [
266 {
267 id: "item-command" as never,
268 type: "command_execution",
269 status: "running",
270 title: " npm test ",
271 nativeItemRef: null,
272 },
273 {
274 id: "item-tool" as never,
275 type: "dynamic_tool",
276 status: "running",
277 title: null,
278 nativeItemRef: null,
279 toolName: " browser.search ",
280 } as never,
281 ],
282 }),
283 ).toEqual([
284 {
285 taskId: "native-task",
286 description: "native background work",
287 kind: "background_task",
288 },
289 {
290 taskId: "item-command",
291 description: "npm test",
292 kind: "command",
293 },
294 {
295 taskId: "item-tool",
296 description: "browser.search",
297 kind: "background_task",
298 },
299 ]);
300 });
301​
302 it("dedupes roster entries against turn items by native task id", () => {
303 const tasks = derivePendingBackgroundWork({
304 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
305 providerThreads: [
306 {
307 id: "pt-1" as never,
308 pendingBackgroundTasks: [
309 { taskId: "task-9", description: "Agent review", kind: "background_task" },
310 ],
311 },
312 ],
313 turnItems: [
314 {
315 id: "item-sub" as never,
316 type: "subagent",
317 status: "running",
318 title: "Agent review",
319 nativeItemRef: { nativeId: "task-9" },
320 prompt: "review the plan",
321 },
322 ],
323 });
324 expect(tasks).toEqual([
325 { taskId: "task-9", description: "Agent review", kind: "background_task" },
326 ]);
327 });
328​
329 it("excludes Grok persistent monitors", () => {
330 const tasks = derivePendingBackgroundWork({
331 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
332 providerThreads: [{ id: "pt-1" as never }],
333 turnItems: [
334 {
335 id: "item-1" as never,
336 type: "dynamic_tool",
337 status: "running",
338 title: "monitor logs",
339 nativeItemRef: { nativeId: "mon-1" },
340 input: { persistent: true, command: "tail -f" },
341 },
342 {
343 id: "item-2" as never,
344 type: "dynamic_tool",
345 status: "running",
346 title: "finite monitor",
347 nativeItemRef: { nativeId: "mon-2" },
348 input: { persistent: false, command: "sleep 5" },
349 },
350 ],
351 });
352 expect(tasks).toEqual([
353 { taskId: "mon-2", description: "finite monitor", kind: "background_task" },
354 ]);
355 });
356​
357 it("returns multiple tasks with stable ordering from insertion", () => {
358 const tasks = derivePendingBackgroundWork({
359 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
360 providerThreads: [
361 {
362 id: "pt-1" as never,
363 pendingBackgroundTasks: [
364 { taskId: "bg-1", description: "first", kind: "command" },
365 { taskId: "bg-2", description: "second", kind: "command" },
366 ],
367 },
368 ],
369 turnItems: [
370 {
371 id: "item-1" as never,
372 type: "command_execution",
373 status: "running",
374 title: "third",
375 nativeItemRef: { nativeId: "cmd-3" },
376 input: "third",
377 },
378 ],
379 });
380 expect(tasks.map((task) => task.taskId)).toEqual(["bg-1", "bg-2", "cmd-3"]);
381 });
382​
383 it("excludes turn items owned by a rolled_back run", () => {
384 const tasks = derivePendingBackgroundWork({
385 latestRun: { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
386 providerThreads: [{ id: "pt-1" as never }],
387 runs: [{ id: "run-1" as never, ordinal: 1, status: "rolled_back" }],
388 turnItems: [
389 {
390 id: "item-1" as never,
391 type: "command_execution",
392 status: "running",
393 title: "abandoned",
394 runId: "run-1",
395 nativeItemRef: { nativeId: "cmd-1" },
396 input: "sleep 99",
397 },
398 ],
399 });
400 expect(tasks).toEqual([]);
401 });
402​
403 it("returns empty when the latest run is rolled_back even with a nonempty roster", () => {
404 const tasks = derivePendingBackgroundWork({
405 latestRun: { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
406 providerThreads: [
407 {
408 id: "pt-1" as never,
409 pendingBackgroundTasks: [{ taskId: "bg-1", description: "sleep 20", kind: "command" }],
410 },
411 ],
412 turnItems: [
413 {
414 id: "item-1" as never,
415 type: "command_execution",
416 status: "running",
417 title: "abandoned",
418 runId: "run-1",
419 nativeItemRef: { nativeId: "cmd-1" },
420 input: "sleep 99",
421 },
422 ],
423 });
424 expect(tasks).toEqual([]);
425 });
426​
427 it("excludes an older rolled_back nonterminal item when the latest run is completed", () => {
428 const tasks = derivePendingBackgroundWork({
429 latestRun: { id: "run-2" as never, ordinal: 2, status: "completed" },
430 providerThreads: [{ id: "pt-1" as never }],
431 runs: [
432 { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
433 { id: "run-2" as never, ordinal: 2, status: "completed" },
434 ],
435 turnItems: [
436 {
437 id: "item-old" as never,
438 type: "command_execution",
439 status: "running",
440 title: "from rolled-back run",
441 runId: "run-1",
442 nativeItemRef: { nativeId: "cmd-old" },
443 input: "sleep 99",
444 },
445 {
446 id: "item-new" as never,
447 type: "command_execution",
448 status: "running",
449 title: "still pending",
450 runId: "run-2",
451 nativeItemRef: { nativeId: "cmd-new" },
452 input: "npm test",
453 },
454 ],
455 });
456 expect(tasks).toEqual([
457 {
458 taskId: "cmd-new",
459 description: "still pending",
460 kind: "command",
461 },
462 ]);
463 });
464​
465 it("does not reclassify an older active run as background work", () => {
466 const tasks = derivePendingBackgroundWork({
467 latestRun: { id: "run-2" as never, ordinal: 2, status: "cancelled" },
468 providerThreads: [
469 {
470 id: "pt-1" as never,
471 pendingBackgroundTasks: [{ taskId: "provider-task", kind: "background_task" }],
472 },
473 ],
474 runs: [
475 { id: "run-1" as never, ordinal: 1, status: "running" },
476 { id: "run-2" as never, ordinal: 2, status: "cancelled" },
477 ],
478 turnItems: [
479 {
480 id: "item-active" as never,
481 type: "command_execution",
482 status: "running",
483 title: "foreground work",
484 runId: "run-1",
485 nativeItemRef: { nativeId: "cmd-active" },
486 input: "vp check",
487 },
488 ],
489 });
490​
491 expect(tasks).toEqual([]);
492 });
493​
494 it("includes items with a null run id even when runs list has rolled_back rows", () => {
495 const tasks = derivePendingBackgroundWork({
496 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
497 providerThreads: [{ id: "pt-1" as never }],
498 runs: [{ id: "run-1" as never, ordinal: 1, status: "rolled_back" }],
499 turnItems: [
500 {
501 id: "item-null-run" as never,
502 type: "command_execution",
503 status: "running",
504 title: "orphan item",
505 runId: null,
506 nativeItemRef: { nativeId: "cmd-null" },
507 input: "echo orphan",
508 },
509 ],
510 });
511 expect(tasks).toEqual([
512 {
513 taskId: "cmd-null",
514 description: "orphan item",
515 kind: "command",
516 },
517 ]);
518 });
519});
520​
521describe("derivePendingBackgroundWork kinds", () => {
522 it("keeps the roster's kinds and names turn items by their type", () => {
523 const tasks = derivePendingBackgroundWork({
524 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
525 providerThreads: [
526 {
527 id: "pt-1" as never,
528 pendingBackgroundTasks: [
529 { taskId: "bash", kind: "command" },
530 { taskId: "watch", kind: "monitor" },
531 { taskId: "workflow", kind: "background_task" },
532 ],
533 },
534 ],
535 turnItems: [
536 {
537 id: "item-sub" as never,
538 type: "subagent",
539 status: "running",
540 title: "Review src/math.ts",
541 nativeItemRef: { nativeId: "sub" },
542 childThreadId: "thread:child" as never,
543 },
544 {
545 id: "item-cmd" as never,
546 type: "command_execution",
547 status: "running",
548 title: null,
549 nativeItemRef: { nativeId: "cmd" },
550 input: "npm test",
551 },
552 ],
553 });
554 expect(tasks).toEqual([
555 { taskId: "bash", kind: "command" },
556 { taskId: "watch", kind: "monitor" },
557 { taskId: "workflow", kind: "background_task" },
558 {
559 taskId: "sub",
560 description: "Review src/math.ts",
561 kind: "subagent",
562 childThreadId: "thread:child",
563 },
564 { taskId: "cmd", description: "npm test", kind: "command" },
565 ]);
566 });
567​
568 describe("pull request watches", () => {
569 const watch = {
570 startedAt: "2026-10-05T00:00:00.000Z",
571 headSha: null,
572 failedChecks: [],
573 passed: false,
574 passedChecks: [],
575 remarksThrough: "2026-10-05T00:00:00.000Z",
576 remarkIds: [],
577 conflicting: false,
578 wakes: 0,
579 };
580 const link = (
581 number: number,
582 extra: { source?: "agent" | "stack-dismissed"; watched?: boolean },
583 ) => ({
584 host: "github.com",
585 repository: "acme/app",
586 number,
587 url: `https://github.com/acme/app/pull/${number}`,
588 source: extra.source ?? "agent",
589 ...(extra.watched === false ? {} : { watch }),
590 });
591 const pullRequests = [
592 link(1, {}),
593 link(2, { watched: false }),
594 link(3, { source: "stack-dismissed" }),
595 ];
596​
597 it("keeps a settled run waiting on each visible watch, as a monitor", () => {
598 const tasks = derivePendingBackgroundWork({
599 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
600 providerThreads: [],
601 turnItems: [],
602 pullRequests,
603 });
604 expect(tasks).toEqual([
605 {
606 taskId: "pull-request-watch:github.com/acme/app#1",
607 description: "Watching pull request #1",
608 kind: "monitor",
609 },
610 ]);
611 expect(backgroundWorkHoldsCompletion(tasks)).toBe(true);
612 });
613​
614 it("keeps a thread that never ran waiting on its watch", () => {
615 expect(
616 derivePendingBackgroundWork({
617 latestRun: null,
618 providerThreads: [],
619 turnItems: [],
620 pullRequests,
621 }).map((task) => task.taskId),
622 ).toEqual(["pull-request-watch:github.com/acme/app#1"]);
623 });
624​
625 it("lists no watch while a run is active", () => {
626 expect(
627 derivePendingBackgroundWork({
628 latestRun: { id: "run-1" as never, ordinal: 1, status: "running" },
629 providerThreads: [],
630 turnItems: [],
631 pullRequests,
632 }),
633 ).toEqual([]);
634 });
635 });
636});