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