@ftrdotdev/pi-delegates 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/OPERATIONS.md +79 -6
  3. package/README.md +25 -17
  4. package/ROADMAP.md +21 -0
  5. package/TESTING.md +153 -318
  6. package/package.json +4 -3
  7. package/skills/pi-delegates/SKILL.md +29 -0
  8. package/skills/pi-delegates/references/launch.md +28 -0
  9. package/src/docs-contract.test.ts +106 -0
  10. package/src/domain/definitions.ts +3 -1
  11. package/src/domain/failure.ts +33 -0
  12. package/src/domain/model-source.ts +30 -0
  13. package/src/domain/orchestration/control.test.ts +69 -35
  14. package/src/domain/orchestration/control.ts +110 -89
  15. package/src/domain/orchestration/fanout.test.ts +125 -20
  16. package/src/domain/orchestration/fanout.ts +60 -22
  17. package/src/domain/orchestration/gates.test.ts +14 -15
  18. package/src/domain/orchestration/launch.test.ts +210 -11
  19. package/src/domain/orchestration/launch.ts +142 -32
  20. package/src/domain/orchestration/requests.test.ts +11 -16
  21. package/src/domain/orchestration/requests.ts +14 -1
  22. package/src/domain/orchestration/review.test.ts +89 -11
  23. package/src/domain/orchestration/run-wait.test.ts +4 -1
  24. package/src/domain/orchestration/run-wait.ts +1 -1
  25. package/src/domain/orchestration/workflow.test.ts +157 -16
  26. package/src/domain/orchestration/workflow.ts +269 -196
  27. package/src/domain/run-records.ts +214 -52
  28. package/src/domain/settlement.test.ts +186 -0
  29. package/src/domain/settlement.ts +137 -0
  30. package/src/domain/shapes.test.ts +20 -0
  31. package/src/domain/skills.test.ts +31 -20
  32. package/src/domain/skills.ts +36 -19
  33. package/src/domain/toolkit.test.ts +15 -1
  34. package/src/domain/toolkit.ts +8 -13
  35. package/src/domain/turn-budget.test.ts +37 -0
  36. package/src/domain/turn-budget.ts +23 -0
  37. package/src/domain/view.test.ts +78 -14
  38. package/src/domain/view.ts +67 -35
  39. package/src/extension/commands.test.ts +27 -23
  40. package/src/extension/commands.ts +10 -7
  41. package/src/extension/entry.test.ts +214 -56
  42. package/src/extension/entry.ts +78 -40
  43. package/src/extension/gate-alerts.test.ts +10 -8
  44. package/src/extension/inspector-command.test.ts +173 -21
  45. package/src/extension/inspector-command.ts +100 -53
  46. package/src/extension/prompt.test.ts +2 -4
  47. package/src/extension/retry-observer.test.ts +292 -0
  48. package/src/extension/retry-observer.ts +150 -0
  49. package/src/extension/settle-alerts.test.ts +579 -0
  50. package/src/extension/settle-alerts.ts +175 -0
  51. package/src/extension/shortcut.test.ts +6 -8
  52. package/src/extension/tools.test.ts +325 -24
  53. package/src/extension/tools.ts +61 -13
  54. package/src/extension/version.test.ts +21 -29
  55. package/src/goldens.test.ts +294 -0
  56. package/src/harness-policy.test.ts +13 -11
  57. package/src/layers.test.ts +309 -0
  58. package/src/layers.ts +405 -0
  59. package/src/package-manifest.test.ts +32 -19
  60. package/src/presentation/actions.test.ts +8 -8
  61. package/src/presentation/activity.test.ts +7 -8
  62. package/src/presentation/close.test.ts +14 -16
  63. package/src/presentation/inspector.test.ts +14 -11
  64. package/src/presentation/inspector.ts +27 -25
  65. package/src/presentation/overlay.test.ts +1 -3
  66. package/src/presentation/repaint.test.ts +226 -190
  67. package/src/presentation/snapshot.test.ts +5 -0
  68. package/src/presentation/status.test.ts +43 -0
  69. package/src/presentation/status.ts +30 -0
  70. package/src/presentation/strip.test.ts +13 -6
  71. package/src/presentation/strip.ts +7 -14
  72. package/src/presentation/wording.test.ts +153 -3
  73. package/src/presentation/wording.ts +78 -6
  74. package/src/runtime/bridge.ts +38 -44
  75. package/src/runtime/{timers.test.ts → clock.test.ts} +1 -1
  76. package/src/runtime/clock.ts +31 -0
  77. package/src/runtime/config.test.ts +26 -7
  78. package/src/runtime/config.ts +6 -1
  79. package/src/runtime/execution-env.test.ts +6 -8
  80. package/src/runtime/extensions.test.ts +4 -4
  81. package/src/runtime/extensions.ts +23 -11
  82. package/src/runtime/handle.ts +54 -0
  83. package/src/runtime/log.test.ts +19 -7
  84. package/src/runtime/log.ts +23 -0
  85. package/src/runtime/mcp.test.ts +29 -32
  86. package/src/runtime/mcp.ts +70 -3
  87. package/src/runtime/runtime-state.test.ts +25 -0
  88. package/src/runtime/runtime-state.ts +36 -0
  89. package/src/runtime/session-tag.ts +11 -0
  90. package/src/runtime/session.test.ts +24 -31
  91. package/src/runtime/session.ts +99 -261
  92. package/src/runtime/storage.ts +29 -0
  93. package/src/test-hygiene.test.ts +97 -0
  94. package/src/runtime/timers.ts +0 -10
@@ -0,0 +1,579 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { stubHostSource } from "../../test-support/stub-model-source.ts";
3
+ import { makeTempDir } from "../../test-support/platform.ts";
4
+ import { waitFor } from "../../test-support/wait.ts";
5
+ import { fakeDocRuntime } from "../../test-support/fake-doc-runtime.ts";
6
+ import { createCatalogue } from "../domain/definitions.ts";
7
+ import { launch, type HostDefaults } from "../domain/orchestration/launch.ts";
8
+ import { recordAnnouncement, RunDoc } from "../domain/run-records.ts";
9
+ import { SETTLE_MAX_ROWS, type SettleNotice } from "../presentation/wording.ts";
10
+ import { HostModelAdapter } from "../runtime/models.ts";
11
+ import type { LogFields, LogLevel, LogSink } from "../runtime/log.ts";
12
+ import { openTestSession, type TestSession } from "../../test-support/seams.ts";
13
+ import {
14
+ SETTLE_COALESCE_MS,
15
+ SETTLE_SCAN_LIMIT,
16
+ settleHostFor,
17
+ startSettleAlerts,
18
+ type SettleAlertHost,
19
+ } from "./settle-alerts.ts";
20
+
21
+ const host: HostDefaults = {
22
+ model: { provider: "stub", modelId: "stub-1" },
23
+ thinkingLevel: "low",
24
+ };
25
+
26
+ const recordingSink = () => {
27
+ const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
28
+ const sink: LogSink = {
29
+ write: (level, code, fields = {}) => {
30
+ entries.push({ level, code, fields });
31
+ },
32
+ };
33
+ return { sink, entries };
34
+ };
35
+
36
+ const runDoc = (input: Record<string, unknown> = {}): Record<string, unknown> => ({
37
+ definition: "scout",
38
+ mode: "single",
39
+ lane: "default",
40
+ requestedAgent: "scout",
41
+ task: "look",
42
+ requestId: "r",
43
+ attempt: 1,
44
+ childConversationId: null,
45
+ taskId: 7,
46
+ gateRunId: "r#1",
47
+ parentRunId: null,
48
+ childRunIds: [],
49
+ diagnostics: [],
50
+ execution: "detached",
51
+ maxTurns: null,
52
+ resolution: "answered",
53
+ failure: null,
54
+ settledTaskId: null,
55
+ retryCount: 0,
56
+ ...input,
57
+ });
58
+
59
+ const spyHost = (options: { readonly refuse?: boolean } = {}) => {
60
+ const notices: SettleNotice[] = [];
61
+ const omitted: number[] = [];
62
+ const spy: SettleAlertHost = {
63
+ notifySettled: (notice, more = 0) => {
64
+ if (options.refuse === true) throw new Error("the host refused the message");
65
+ notices.push({ rows: [...notice.rows] });
66
+ omitted.push(more);
67
+ },
68
+ };
69
+ return { notices, omitted, host: spy };
70
+ };
71
+
72
+ const announceSpy = () => {
73
+ const announced: [string, number][] = [];
74
+ return {
75
+ announced,
76
+ announce: async (runId: string, taskId: number): Promise<void> => {
77
+ announced.push([runId, taskId]);
78
+ },
79
+ };
80
+ };
81
+
82
+ const flush = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
83
+
84
+ it("one settled detached run is announced once, after the window", async () => {
85
+ const fake = fakeDocRuntime({
86
+ docs: {
87
+ "delegates.run-index": { runIds: ["r#1"] },
88
+ "delegates.run:r#1": runDoc({
89
+ resolution: "failed",
90
+ failure: { reason: "turn_limit", detail: "120 turns" },
91
+ }),
92
+ },
93
+ });
94
+ const recorder = recordingSink();
95
+ const spied = spyHost();
96
+ const announced = announceSpy();
97
+ const watch = startSettleAlerts({
98
+ runtime: fake.runtime,
99
+ host: spied.host,
100
+ announce: announced.announce,
101
+ log: recorder.sink,
102
+ });
103
+ try {
104
+ await flush();
105
+ fake.runtime.settle.emit({ runId: "r#1" });
106
+ await flush();
107
+ assert.strictEqual(spied.notices.length, 0);
108
+ fake.fireDelay(SETTLE_COALESCE_MS);
109
+ await flush();
110
+
111
+ assert.deepStrictEqual(spied.notices, [
112
+ {
113
+ rows: [
114
+ {
115
+ runId: "r#1",
116
+ definition: "scout",
117
+ outcome: "failed",
118
+ failure: { reason: "turn_limit", detail: "120 turns" },
119
+ parentRunId: null,
120
+ },
121
+ ],
122
+ },
123
+ ]);
124
+ assert.deepStrictEqual(announced.announced, [["r#1", 7]]);
125
+ assert.ok(fake.delays().includes(SETTLE_COALESCE_MS), fake.delays().join(","));
126
+ } finally {
127
+ await watch.close();
128
+ }
129
+ });
130
+
131
+ it("two detached runs inside one window arrive as one message", async () => {
132
+ const fake = fakeDocRuntime({
133
+ docs: {
134
+ "delegates.run-index": { runIds: ["r#1", "r#2"] },
135
+ "delegates.run:r#1": runDoc(),
136
+ "delegates.run:r#2": runDoc({
137
+ taskId: 8,
138
+ resolution: "failed",
139
+ failure: { reason: "model_error", detail: "boom" },
140
+ }),
141
+ },
142
+ });
143
+ const spied = spyHost();
144
+ const announced = announceSpy();
145
+ const watch = startSettleAlerts({
146
+ runtime: fake.runtime,
147
+ host: spied.host,
148
+ announce: announced.announce,
149
+ log: recordingSink().sink,
150
+ });
151
+ try {
152
+ await flush();
153
+ fake.runtime.settle.emit({ runId: "r#1" });
154
+ fake.runtime.settle.emit({ runId: "r#2" });
155
+ fake.fireDelay(SETTLE_COALESCE_MS);
156
+ await flush();
157
+
158
+ assert.strictEqual(spied.notices.length, 1);
159
+ assert.deepStrictEqual(
160
+ spied.notices[0]?.rows.map((row) => row.runId),
161
+ ["r#1", "r#2"],
162
+ );
163
+ assert.deepStrictEqual(announced.announced, [
164
+ ["r#1", 7],
165
+ ["r#2", 8],
166
+ ]);
167
+ } finally {
168
+ await watch.close();
169
+ }
170
+ });
171
+
172
+ it("a sync run is never noticed", async () => {
173
+ const fake = fakeDocRuntime({
174
+ docs: {
175
+ "delegates.run-index": { runIds: ["r#1"] },
176
+ "delegates.run:r#1": runDoc({ execution: "wait" }),
177
+ },
178
+ });
179
+ const spied = spyHost();
180
+ const announced = announceSpy();
181
+ const watch = startSettleAlerts({
182
+ runtime: fake.runtime,
183
+ host: spied.host,
184
+ announce: announced.announce,
185
+ log: recordingSink().sink,
186
+ });
187
+ try {
188
+ await flush();
189
+ fake.runtime.settle.emit({ runId: "r#1" });
190
+ fake.fireDelay(SETTLE_COALESCE_MS);
191
+ await flush();
192
+ assert.deepStrictEqual(spied.notices, []);
193
+ assert.strictEqual(announced.announced.length, 0);
194
+ } finally {
195
+ await watch.close();
196
+ }
197
+ });
198
+
199
+ it("an already announced run is not announced again", async () => {
200
+ const fake = fakeDocRuntime({
201
+ docs: {
202
+ "delegates.run-index": { runIds: ["r#1"] },
203
+ "delegates.run:r#1": runDoc({ settledTaskId: 7 }),
204
+ },
205
+ });
206
+ const spied = spyHost();
207
+ const announced = announceSpy();
208
+ const watch = startSettleAlerts({
209
+ runtime: fake.runtime,
210
+ host: spied.host,
211
+ announce: announced.announce,
212
+ log: recordingSink().sink,
213
+ });
214
+ try {
215
+ await flush();
216
+ fake.runtime.settle.emit({ runId: "r#1" });
217
+ fake.fireDelay(SETTLE_COALESCE_MS);
218
+ await flush();
219
+ fake.runtime.settle.emit({ runId: "r#1" });
220
+ fake.fireDelay(SETTLE_COALESCE_MS);
221
+ await flush();
222
+ assert.deepStrictEqual(spied.notices, []);
223
+ assert.strictEqual(announced.announced.length, 0);
224
+ } finally {
225
+ await watch.close();
226
+ }
227
+ });
228
+
229
+ it("a resumed run is announced again because its task changed", async () => {
230
+ const fake = fakeDocRuntime({
231
+ docs: {
232
+ "delegates.run-index": { runIds: ["r#1"] },
233
+ "delegates.run:r#1": runDoc({ settledTaskId: 7 }),
234
+ },
235
+ });
236
+ const spied = spyHost();
237
+ const announced = announceSpy();
238
+ const watch = startSettleAlerts({
239
+ runtime: fake.runtime,
240
+ host: spied.host,
241
+ announce: announced.announce,
242
+ log: recordingSink().sink,
243
+ });
244
+ try {
245
+ await flush();
246
+ fake.commitDoc("delegates.run:r#1", runDoc({ taskId: 9, settledTaskId: 7 }));
247
+ fake.runtime.settle.emit({ runId: "r#1" });
248
+ await flush();
249
+ fake.fireDelay(SETTLE_COALESCE_MS);
250
+ await flush();
251
+ assert.deepStrictEqual(announced.announced, [["r#1", 9]]);
252
+ } finally {
253
+ await watch.close();
254
+ }
255
+ });
256
+
257
+ it("child rows name their parent run id and more than twenty rows are capped", async () => {
258
+ const runIds = Array.from({ length: 25 }, (_, index) => `r#${index + 1}`);
259
+ const docs: Record<string, Record<string, unknown>> = {
260
+ "delegates.run-index": { runIds },
261
+ };
262
+ for (const [index, runId] of runIds.entries()) {
263
+ docs[`delegates.run:${runId}`] = runDoc({
264
+ taskId: index + 1,
265
+ parentRunId: "r#0",
266
+ definition: "builder",
267
+ });
268
+ }
269
+ const fake = fakeDocRuntime({ docs });
270
+ const spied = spyHost();
271
+ const announced = announceSpy();
272
+ const watch = startSettleAlerts({
273
+ runtime: fake.runtime,
274
+ host: spied.host,
275
+ announce: announced.announce,
276
+ log: recordingSink().sink,
277
+ });
278
+ try {
279
+ await flush();
280
+ for (const runId of runIds) fake.runtime.settle.emit({ runId });
281
+ fake.fireDelay(SETTLE_COALESCE_MS);
282
+ await flush();
283
+
284
+ assert.strictEqual(spied.notices.length, 1);
285
+ assert.strictEqual(spied.notices[0]?.rows.length, SETTLE_MAX_ROWS);
286
+ for (const row of spied.notices[0]?.rows ?? []) assert.strictEqual(row.parentRunId, "r#0");
287
+ assert.strictEqual(spied.omitted[0], runIds.length - SETTLE_MAX_ROWS);
288
+ assert.strictEqual(announced.announced.length, SETTLE_MAX_ROWS);
289
+
290
+ fake.fireDelay(SETTLE_COALESCE_MS);
291
+ await flush();
292
+ assert.strictEqual(spied.notices.length, 2);
293
+ assert.strictEqual(spied.notices[1]?.rows.length, runIds.length - SETTLE_MAX_ROWS);
294
+ assert.strictEqual(announced.announced.length, runIds.length);
295
+ } finally {
296
+ await watch.close();
297
+ }
298
+ });
299
+
300
+ it("the start scan reaches a group child through its parent document", async () => {
301
+ const childRunId = "g#1:child:0";
302
+ const fake = fakeDocRuntime({
303
+ docs: {
304
+ "delegates.run-index": { runIds: ["g#1"] },
305
+ "delegates.run:g#1": runDoc({
306
+ definition: "fanout",
307
+ mode: "fanout",
308
+ requestedAgent: "fanout",
309
+ childConversationId: null,
310
+ gateRunId: "g#1",
311
+ taskId: 9,
312
+ childRunIds: [childRunId],
313
+ // The group's own notice already went out; only the child is unannounced.
314
+ settledTaskId: 9,
315
+ }),
316
+ [`delegates.run:${childRunId}`]: runDoc({
317
+ parentRunId: "g#1",
318
+ gateRunId: "g#1",
319
+ taskId: 7,
320
+ }),
321
+ },
322
+ });
323
+ const recorder = recordingSink();
324
+ const spied = spyHost();
325
+ const announced = announceSpy();
326
+ const watch = startSettleAlerts({
327
+ runtime: fake.runtime,
328
+ host: spied.host,
329
+ announce: announced.announce,
330
+ log: recorder.sink,
331
+ });
332
+ try {
333
+ await flush();
334
+ fake.fireDelay(SETTLE_COALESCE_MS);
335
+ await flush();
336
+
337
+ assert.deepStrictEqual(
338
+ spied.notices.flatMap((notice) => notice.rows.map((row) => [row.runId, row.parentRunId])),
339
+ [[childRunId, "g#1"]],
340
+ );
341
+ assert.deepStrictEqual(announced.announced, [[childRunId, 7]]);
342
+ } finally {
343
+ await watch.close();
344
+ }
345
+ });
346
+
347
+ it("a host that refuses the message leaves the run unannounced", async () => {
348
+ const fake = fakeDocRuntime({
349
+ docs: {
350
+ "delegates.run-index": { runIds: ["r#1"] },
351
+ "delegates.run:r#1": runDoc(),
352
+ },
353
+ });
354
+ const recorder = recordingSink();
355
+ const spied = spyHost({ refuse: true });
356
+ const announced = announceSpy();
357
+ const watch = startSettleAlerts({
358
+ runtime: fake.runtime,
359
+ host: spied.host,
360
+ announce: announced.announce,
361
+ log: recorder.sink,
362
+ });
363
+ try {
364
+ await flush();
365
+ fake.runtime.settle.emit({ runId: "r#1" });
366
+ fake.fireDelay(SETTLE_COALESCE_MS);
367
+ await flush();
368
+
369
+ assert.strictEqual(announced.announced.length, 0);
370
+ const refusals = recorder.entries.filter((entry) => entry.code === "settle.notify_failed");
371
+ assert.strictEqual(refusals.length, 1);
372
+ assert.strictEqual(refusals[0]?.level, "warn");
373
+ assert.strictEqual(refusals[0]?.fields.runs, "r#1");
374
+
375
+ // The rows stay pending: the next window retries and still announces nothing.
376
+ fake.fireDelay(SETTLE_COALESCE_MS);
377
+ await flush();
378
+ assert.strictEqual(announced.announced.length, 0);
379
+ } finally {
380
+ await watch.close();
381
+ }
382
+ });
383
+
384
+ it("the start scan reads the newest 200 run ids and logs once at info when it truncates", async () => {
385
+ const runIds = Array.from({ length: 250 }, (_, index) => `r#${index + 1}`);
386
+ const docs: Record<string, Record<string, unknown>> = {
387
+ "delegates.run-index": { runIds },
388
+ };
389
+ const newest = runIds.at(-1) ?? "r#250";
390
+ docs[`delegates.run:${newest}`] = runDoc({ taskId: 250 });
391
+ const fake = fakeDocRuntime({ docs });
392
+ const recorder = recordingSink();
393
+ const spied = spyHost();
394
+ const announced = announceSpy();
395
+ const watch = startSettleAlerts({
396
+ runtime: fake.runtime,
397
+ host: spied.host,
398
+ announce: announced.announce,
399
+ log: recorder.sink,
400
+ });
401
+ try {
402
+ await flush();
403
+ const truncations = recorder.entries.filter((entry) => entry.code === "settle.scan_truncated");
404
+ assert.strictEqual(truncations.length, 1);
405
+ assert.strictEqual(truncations[0]?.level, "info");
406
+ assert.strictEqual(truncations[0]?.fields.omitted, runIds.length - SETTLE_SCAN_LIMIT);
407
+
408
+ fake.fireDelay(SETTLE_COALESCE_MS);
409
+ await flush();
410
+ assert.deepStrictEqual(
411
+ spied.notices[0]?.rows.map((row) => row.runId),
412
+ [newest],
413
+ );
414
+ assert.deepStrictEqual(announced.announced, [[newest, 250]]);
415
+ } finally {
416
+ await watch.close();
417
+ }
418
+ });
419
+
420
+ const open = async (identity: {
421
+ readonly sessionId: string;
422
+ readonly sessionDir: string;
423
+ }): Promise<TestSession> =>
424
+ openTestSession({
425
+ identity: { ...identity, cwd: identity.sessionDir, agentDir: identity.sessionDir },
426
+ models: new HostModelAdapter(stubHostSource({ text: "done" })),
427
+ });
428
+
429
+ it("a detached run that settled while the host was closed is announced at start", async () => {
430
+ const sessionDir = await makeTempDir("delegates-settle-");
431
+ const identity = { sessionId: "s-settle", sessionDir };
432
+ const first = await open(identity);
433
+ let runId: string;
434
+ let taskId: number;
435
+ try {
436
+ const result = await launch(
437
+ { kind: "single", agent: "scout", task: "look", execution: "detached" },
438
+ { runtime: first, catalogue: createCatalogue(), host, waitMs: 0 },
439
+ );
440
+ runId = result.view.runId;
441
+ await waitFor(
442
+ async () => (await first.snapshot(RunDoc, runId))?.resolution === "answered",
443
+ "the detached run settles",
444
+ );
445
+ taskId = (await first.snapshot(RunDoc, runId))?.taskId ?? 0;
446
+ assert.ok(taskId > 0);
447
+ } finally {
448
+ await first.close();
449
+ }
450
+
451
+ const second = await open(identity);
452
+ const spied = spyHost();
453
+ const watch = startSettleAlerts({
454
+ runtime: second,
455
+ host: spied.host,
456
+ announce: (run, task) => recordAnnouncement(second, run, task),
457
+ log: recordingSink().sink,
458
+ intervalMs: 10,
459
+ });
460
+ try {
461
+ await waitFor(() => spied.notices.length === 1, "one notice after start");
462
+ assert.strictEqual(spied.notices[0]?.rows.length, 1);
463
+ assert.strictEqual(spied.notices[0]?.rows[0]?.runId, runId);
464
+ await waitFor(async () => {
465
+ const doc = await second.snapshot(RunDoc, runId ?? "");
466
+ return doc?.settledTaskId === taskId;
467
+ }, "the marker is committed");
468
+ } finally {
469
+ await watch.close();
470
+ }
471
+
472
+ const third = await open(identity);
473
+ const again = spyHost();
474
+ const secondWatch = startSettleAlerts({
475
+ runtime: third,
476
+ host: again.host,
477
+ announce: (run, task) => recordAnnouncement(third, run, task),
478
+ log: recordingSink().sink,
479
+ intervalMs: 10,
480
+ });
481
+ try {
482
+ await new Promise((resolve) => setTimeout(resolve, 100));
483
+ assert.deepStrictEqual(again.notices, []);
484
+ } finally {
485
+ await secondWatch.close();
486
+ await third.close();
487
+ }
488
+ }, 30_000);
489
+
490
+ it("a detached fanout is announced with its group and each child's parent", async () => {
491
+ const sessionDir = await makeTempDir("delegates-settle-group-");
492
+ const runtime = await open({ sessionId: "s-settle-group", sessionDir });
493
+ const spied = spyHost();
494
+ const watch = startSettleAlerts({
495
+ runtime,
496
+ host: spied.host,
497
+ announce: (run, task) => recordAnnouncement(runtime, run, task),
498
+ log: recordingSink().sink,
499
+ intervalMs: 10,
500
+ });
501
+ try {
502
+ const result = await launch(
503
+ {
504
+ kind: "fanout",
505
+ lane: "review",
506
+ agents: [
507
+ { agent: "scout", task: "first" },
508
+ { agent: "scout", task: "second" },
509
+ ],
510
+ execution: "detached",
511
+ },
512
+ { runtime, catalogue: createCatalogue(), host, waitMs: 5_000 },
513
+ );
514
+ const group = result.view.runId;
515
+ const children = result.view.children.map((child) => child.runId);
516
+ const rows = () => spied.notices.flatMap((notice) => notice.rows);
517
+
518
+ // Children and group may land in different windows; the set is what matters.
519
+ await waitFor(
520
+ () => new Set(rows().map((row) => row.runId)).size === 3,
521
+ "the group and its children are announced",
522
+ 20_000,
523
+ );
524
+ const byRun = new Map(rows().map((row) => [row.runId, row]));
525
+ assert.strictEqual(byRun.get(group)?.parentRunId, null);
526
+ assert.strictEqual(byRun.get(group)?.outcome, "answered");
527
+ for (const child of children) {
528
+ assert.strictEqual(byRun.get(child)?.parentRunId, group);
529
+ assert.strictEqual(byRun.get(child)?.outcome, "answered");
530
+ }
531
+
532
+ await waitFor(
533
+ async () => {
534
+ const docs = await Promise.all(
535
+ [group, ...children].map((runId) => runtime.snapshot(RunDoc, runId)),
536
+ );
537
+ return docs.every((doc) => doc !== undefined && doc.settledTaskId === doc.taskId);
538
+ },
539
+ "every marker is committed",
540
+ 10_000,
541
+ );
542
+ } finally {
543
+ await watch.close();
544
+ await runtime.close();
545
+ }
546
+ }, 30_000);
547
+
548
+ it("a headless host writes one info line per batch and calls no surface", async () => {
549
+ const recorder = recordingSink();
550
+ const fake = fakeDocRuntime({
551
+ docs: {
552
+ "delegates.run-index": { runIds: ["r#1", "r#2"] },
553
+ "delegates.run:r#1": runDoc(),
554
+ "delegates.run:r#2": runDoc({ taskId: 8 }),
555
+ },
556
+ });
557
+ const spied = spyHost();
558
+ const announced = announceSpy();
559
+ const watch = startSettleAlerts({
560
+ runtime: fake.runtime,
561
+ host: settleHostFor("json", spied.host, recorder.sink),
562
+ announce: announced.announce,
563
+ log: recorder.sink,
564
+ });
565
+ try {
566
+ await flush();
567
+ fake.runtime.settle.emit({ runId: "r#1" });
568
+ fake.runtime.settle.emit({ runId: "r#2" });
569
+ fake.fireDelay(SETTLE_COALESCE_MS);
570
+ await flush();
571
+ const lines = recorder.entries.filter((entry) => entry.code === "run.settled");
572
+ assert.strictEqual(lines.length, 1);
573
+ assert.strictEqual(lines[0]?.level, "info");
574
+ assert.strictEqual(lines[0]?.fields.runs, "r#1,r#2");
575
+ assert.deepStrictEqual(spied.notices, []);
576
+ } finally {
577
+ await watch.close();
578
+ }
579
+ });