@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

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