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

orchestrationV2PendingBackgroundWork.test.ts

442 lines13.4 KB

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