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

orchestrationV2PendingBackgroundWork.test.ts

448 lines13.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import { ProviderInstanceId, ProviderThreadId } from "@t3tools/contracts";
3import {
4 backgroundWorkHoldsCompletion,
5 derivePendingBackgroundWork,
6} from "./orchestrationV2PendingBackgroundWork.ts";
7​
8describe("backgroundWorkHoldsCompletion", () => {
9 it.each([
10 ["a dev server", false, [{ taskId: "dev", kind: "command" }]],
11 ["a dev server's watchdog monitor", false, [{ taskId: "watch", kind: "monitor" }]],
12 ["a command the agent waits on", true, [{ taskId: "tests", kind: "command", awaited: true }]],
13 ["a subagent", true, [{ taskId: "review", kind: "subagent" }]],
14 ] as const)("with %s pending, holds completion: %s", (_case, holds, tasks) => {
15 expect(backgroundWorkHoldsCompletion(tasks)).toBe(holds);
16 });
17});
18​
19describe("derivePendingBackgroundWork", () => {
20 it("takes each subagent item's instance from its joined owner", () => {
21 const owner = ProviderInstanceId.make("claude-owner");
22 const tasks = derivePendingBackgroundWork({
23 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
24 providerThreads: [],
25 turnItems: [
26 {
27 id: "owned-item",
28 owningProviderInstanceId: owner,
29 type: "subagent",
30 status: "running",
31 title: null,
32 },
33 { id: "unknown-item", type: "subagent", status: "running", title: null },
34 ],
35 });
36 expect(tasks).toEqual([
37 { taskId: "owned-item", kind: "subagent", providerInstanceId: owner },
38 { taskId: "unknown-item", kind: "subagent" },
39 ]);
40 });
41​
42 it("takes a roster subagent's instance from its owning provider thread", () => {
43 const owner = ProviderInstanceId.make("claude-owner");
44 const tasks = derivePendingBackgroundWork({
45 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
46 providerThreads: [
47 {
48 id: ProviderThreadId.make("pt-owner"),
49 providerInstanceId: owner,
50 pendingBackgroundTasks: [
51 {
52 taskId: "roster-item",
53 kind: "subagent",
54 providerInstanceId: ProviderInstanceId.make("other"),
55 },
56 ],
57 },
58 ],
59 turnItems: [],
60 });
61 expect(tasks).toEqual([{ taskId: "roster-item", kind: "subagent", providerInstanceId: owner }]);
62 });
63​
64 it("returns empty while the latest run is not settled", () => {
65 const tasks = derivePendingBackgroundWork({
66 latestRun: { id: "run-1" as never, ordinal: 1, status: "running" },
67 providerThreads: [
68 {
69 id: "pt-1" as never,
70 pendingBackgroundTasks: [{ taskId: "bg-1", description: "sleep 20", kind: "command" }],
71 },
72 ],
73 turnItems: [
74 {
75 id: "item-1" as never,
76 type: "command_execution",
77 status: "running",
78 title: "npm test",
79 nativeItemRef: null,
80 input: "npm test",
81 },
82 ],
83 });
84 expect(tasks).toEqual([]);
85 });
86​
87 it("does not keep a settled parent waiting for an idle child", () => {
88 expect(
89 derivePendingBackgroundWork({
90 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
91 providerThreads: [],
92 turnItems: [{ id: "idle-child", type: "subagent", status: "idle", title: "Review" }],
93 }),
94 ).toEqual([]);
95 });
96​
97 it("returns pending work when the latest run is waiting (post-success, pre-checkpoint)", () => {
98 const tasks = derivePendingBackgroundWork({
99 latestRun: { id: "run-1" as never, ordinal: 1, status: "waiting" },
100 providerThreads: [{ id: "pt-1" as never }],
101 turnItems: [
102 {
103 id: "item-1" as never,
104 type: "command_execution",
105 status: "running",
106 title: "npm test",
107 nativeItemRef: { nativeId: "cmd-1" },
108 input: "npm test",
109 },
110 ],
111 });
112 expect(tasks).toEqual([{ taskId: "cmd-1", description: "npm test", kind: "command" }]);
113 });
114​
115 it("excludes rolled_back items when the latest run is waiting", () => {
116 const tasks = derivePendingBackgroundWork({
117 latestRun: { id: "run-2" as never, ordinal: 2, status: "waiting" },
118 providerThreads: [{ id: "pt-1" as never }],
119 runs: [
120 { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
121 { id: "run-2" as never, ordinal: 2, status: "waiting" },
122 ],
123 turnItems: [
124 {
125 id: "item-old" as never,
126 type: "command_execution",
127 status: "running",
128 title: "from rolled-back run",
129 runId: "run-1",
130 nativeItemRef: { nativeId: "cmd-old" },
131 input: "sleep 99",
132 },
133 {
134 id: "item-new" as never,
135 type: "command_execution",
136 status: "running",
137 title: "still pending",
138 runId: "run-2",
139 nativeItemRef: { nativeId: "cmd-new" },
140 input: "npm test",
141 },
142 ],
143 });
144 expect(tasks).toEqual([
145 {
146 taskId: "cmd-new",
147 description: "still pending",
148 kind: "command",
149 },
150 ]);
151 });
152​
153 it("includes nonterminal turn items and excludes completed ones", () => {
154 const tasks = derivePendingBackgroundWork({
155 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
156 providerThreads: [{ id: "pt-1" as never }],
157 turnItems: [
158 {
159 id: "item-1" as never,
160 type: "command_execution",
161 status: "running",
162 title: "npm test",
163 nativeItemRef: { nativeId: "cmd-1" },
164 input: "npm test",
165 },
166 {
167 id: "item-2" as never,
168 type: "command_execution",
169 status: "completed",
170 title: "done",
171 nativeItemRef: { nativeId: "cmd-2" },
172 input: "echo done",
173 },
174 ],
175 });
176 expect(tasks).toEqual([{ taskId: "cmd-1", description: "npm test", kind: "command" }]);
177 });
178​
179 it("dedupes roster entries against turn items by native task id", () => {
180 const tasks = derivePendingBackgroundWork({
181 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
182 providerThreads: [
183 {
184 id: "pt-1" as never,
185 pendingBackgroundTasks: [
186 { taskId: "task-9", description: "Agent review", kind: "background_task" },
187 ],
188 },
189 ],
190 turnItems: [
191 {
192 id: "item-sub" as never,
193 type: "subagent",
194 status: "running",
195 title: "Agent review",
196 nativeItemRef: { nativeId: "task-9" },
197 prompt: "review the plan",
198 },
199 ],
200 });
201 expect(tasks).toEqual([
202 { taskId: "task-9", description: "Agent review", kind: "background_task" },
203 ]);
204 });
205​
206 it("excludes Grok persistent monitors", () => {
207 const tasks = derivePendingBackgroundWork({
208 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
209 providerThreads: [{ id: "pt-1" as never }],
210 turnItems: [
211 {
212 id: "item-1" as never,
213 type: "dynamic_tool",
214 status: "running",
215 title: "monitor logs",
216 nativeItemRef: { nativeId: "mon-1" },
217 input: { persistent: true, command: "tail -f" },
218 },
219 {
220 id: "item-2" as never,
221 type: "dynamic_tool",
222 status: "running",
223 title: "finite monitor",
224 nativeItemRef: { nativeId: "mon-2" },
225 input: { persistent: false, command: "sleep 5" },
226 },
227 ],
228 });
229 expect(tasks).toEqual([
230 { taskId: "mon-2", description: "finite monitor", kind: "background_task" },
231 ]);
232 });
233​
234 it("returns empty when the latest run is rolled_back even with a nonempty roster", () => {
235 const tasks = derivePendingBackgroundWork({
236 latestRun: { id: "run-1" as never, ordinal: 1, status: "rolled_back" },
237 providerThreads: [
238 {
239 id: "pt-1" as never,
240 pendingBackgroundTasks: [{ taskId: "bg-1", description: "sleep 20", kind: "command" }],
241 },
242 ],
243 turnItems: [
244 {
245 id: "item-1" as never,
246 type: "command_execution",
247 status: "running",
248 title: "abandoned",
249 runId: "run-1",
250 nativeItemRef: { nativeId: "cmd-1" },
251 input: "sleep 99",
252 },
253 ],
254 });
255 expect(tasks).toEqual([]);
256 });
257​
258 it("does not reclassify an older active run as background work", () => {
259 const tasks = derivePendingBackgroundWork({
260 latestRun: { id: "run-2" as never, ordinal: 2, status: "cancelled" },
261 providerThreads: [
262 {
263 id: "pt-1" as never,
264 pendingBackgroundTasks: [{ taskId: "provider-task", kind: "background_task" }],
265 },
266 ],
267 runs: [
268 { id: "run-1" as never, ordinal: 1, status: "running" },
269 { id: "run-2" as never, ordinal: 2, status: "cancelled" },
270 ],
271 turnItems: [
272 {
273 id: "item-active" as never,
274 type: "command_execution",
275 status: "running",
276 title: "foreground work",
277 runId: "run-1",
278 nativeItemRef: { nativeId: "cmd-active" },
279 input: "vp check",
280 },
281 ],
282 });
283​
284 expect(tasks).toEqual([]);
285 });
286​
287 it("includes items with a null run id even when runs list has rolled_back rows", () => {
288 const tasks = derivePendingBackgroundWork({
289 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
290 providerThreads: [{ id: "pt-1" as never }],
291 runs: [{ id: "run-1" as never, ordinal: 1, status: "rolled_back" }],
292 turnItems: [
293 {
294 id: "item-null-run" as never,
295 type: "command_execution",
296 status: "running",
297 title: "orphan item",
298 runId: null,
299 nativeItemRef: { nativeId: "cmd-null" },
300 input: "echo orphan",
301 },
302 ],
303 });
304 expect(tasks).toEqual([
305 {
306 taskId: "cmd-null",
307 description: "orphan item",
308 kind: "command",
309 },
310 ]);
311 });
312});
313​
314describe("derivePendingBackgroundWork kinds", () => {
315 it("keeps the roster's kinds and names turn items by their type", () => {
316 const tasks = derivePendingBackgroundWork({
317 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
318 providerThreads: [
319 {
320 id: "pt-1" as never,
321 pendingBackgroundTasks: [
322 { taskId: "bash", kind: "command" },
323 { taskId: "tests", kind: "command", awaited: true },
324 { taskId: "watch", kind: "monitor" },
325 { taskId: "workflow", kind: "background_task" },
326 ],
327 },
328 ],
329 turnItems: [
330 {
331 id: "item-sub" as never,
332 type: "subagent",
333 status: "running",
334 title: "Review src/math.ts",
335 nativeItemRef: { nativeId: "sub" },
336 childThreadId: "thread:child" as never,
337 },
338 {
339 id: "item-cmd" as never,
340 type: "command_execution",
341 status: "running",
342 title: null,
343 nativeItemRef: { nativeId: "cmd" },
344 input: "npm test",
345 },
346 ],
347 });
348 expect(tasks).toEqual([
349 { taskId: "bash", kind: "command" },
350 { taskId: "tests", kind: "command", awaited: true },
351 { taskId: "watch", kind: "monitor" },
352 { taskId: "workflow", kind: "background_task" },
353 {
354 taskId: "sub",
355 description: "Review src/math.ts",
356 kind: "subagent",
357 childThreadId: "thread:child",
358 },
359 { taskId: "cmd", description: "npm test", kind: "command" },
360 ]);
361 });
362​
363 describe("pull request watches", () => {
364 const watch = {
365 startedAt: "2026-10-05T00:00:00.000Z",
366 headSha: null,
367 failedChecks: [],
368 passed: false,
369 passedChecks: [],
370 remarksThrough: "2026-10-05T00:00:00.000Z",
371 remarkIds: [],
372 conflicting: false,
373 wakes: 0,
374 };
375 const link = (
376 number: number,
377 extra: {
378 source?: "agent" | "stack-dismissed";
379 watched?: boolean;
380 state?: "open" | "closed" | "merged";
381 },
382 ) => ({
383 host: "github.com",
384 repository: "acme/app",
385 number,
386 url: `https://github.com/acme/app/pull/${number}`,
387 source: extra.source ?? "agent",
388 snapshot: extra.state === undefined ? null : { state: extra.state },
389 ...(extra.watched === false ? {} : { watch }),
390 });
391 const pullRequests = [
392 link(1, {}),
393 link(2, { watched: false }),
394 link(3, { source: "stack-dismissed" }),
395 link(4, { state: "merged" }),
396 link(5, { state: "closed" }),
397 ];
398​
399 it("keeps a settled run waiting on each visible watch, as an awaited monitor", () => {
400 const tasks = derivePendingBackgroundWork({
401 latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" },
402 providerThreads: [],
403 turnItems: [],
404 pullRequests,
405 });
406 expect(tasks).toEqual([
407 {
408 taskId: "pull-request-watch:github.com/acme/app#1",
409 description: "Watching pull request #1",
410 kind: "monitor",
411 awaited: true,
412 },
413 {
414 taskId: "pull-request-watch:github.com/acme/app#5",
415 description: "Watching pull request #5",
416 kind: "monitor",
417 awaited: true,
418 },
419 ]);
420 expect(backgroundWorkHoldsCompletion(tasks)).toBe(true);
421 });
422​
423 it("keeps a thread that never ran waiting on its watch", () => {
424 expect(
425 derivePendingBackgroundWork({
426 latestRun: null,
427 providerThreads: [],
428 turnItems: [],
429 pullRequests,
430 }).map((task) => task.taskId),
431 ).toEqual([
432 "pull-request-watch:github.com/acme/app#1",
433 "pull-request-watch:github.com/acme/app#5",
434 ]);
435 });
436​
437 it("lists no watch while a run is active", () => {
438 expect(
439 derivePendingBackgroundWork({
440 latestRun: { id: "run-1" as never, ordinal: 1, status: "running" },
441 providerThreads: [],
442 turnItems: [],
443 pullRequests,
444 }),
445 ).toEqual([]);
446 });
447 });
448});