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