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

orchestrationV2PendingBackgroundWork.test.ts

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