@ftrdotdev/pi-delegates 0.0.0-stage → 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 (138) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/LICENSE +21 -0
  3. package/OPERATIONS.md +130 -0
  4. package/README.md +456 -2
  5. package/ROADMAP.md +179 -0
  6. package/TESTING.md +896 -0
  7. package/agents.ts +8 -0
  8. package/examples/README.md +114 -0
  9. package/examples/gated.ts +28 -0
  10. package/index.ts +9 -0
  11. package/package.json +94 -4
  12. package/skills/pi-delegates/SKILL.md +176 -0
  13. package/skills/pi-delegates/references/control.md +159 -0
  14. package/skills/pi-delegates/references/definitions.md +163 -0
  15. package/skills/pi-delegates/references/gates.md +175 -0
  16. package/skills/pi-delegates/references/launch.md +184 -0
  17. package/src/docs-contract.test.ts +106 -0
  18. package/src/domain/definitions.test.ts +41 -0
  19. package/src/domain/definitions.ts +163 -0
  20. package/src/domain/failure.ts +33 -0
  21. package/src/domain/ids.ts +13 -0
  22. package/src/domain/model-source.ts +30 -0
  23. package/src/domain/names.ts +35 -0
  24. package/src/domain/orchestration/admission.ts +78 -0
  25. package/src/domain/orchestration/control.test.ts +1332 -0
  26. package/src/domain/orchestration/control.ts +457 -0
  27. package/src/domain/orchestration/fanout.test.ts +524 -0
  28. package/src/domain/orchestration/fanout.ts +372 -0
  29. package/src/domain/orchestration/gates.test.ts +685 -0
  30. package/src/domain/orchestration/gates.ts +360 -0
  31. package/src/domain/orchestration/launch.test.ts +372 -0
  32. package/src/domain/orchestration/launch.ts +691 -0
  33. package/src/domain/orchestration/requests.test.ts +695 -0
  34. package/src/domain/orchestration/requests.ts +782 -0
  35. package/src/domain/orchestration/review.test.ts +292 -0
  36. package/src/domain/orchestration/review.ts +168 -0
  37. package/src/domain/orchestration/run-wait.test.ts +265 -0
  38. package/src/domain/orchestration/run-wait.ts +256 -0
  39. package/src/domain/orchestration/workflow-section.test.ts +62 -0
  40. package/src/domain/orchestration/workflow-section.ts +99 -0
  41. package/src/domain/orchestration/workflow.test.ts +1566 -0
  42. package/src/domain/orchestration/workflow.ts +778 -0
  43. package/src/domain/run-records.test.ts +27 -0
  44. package/src/domain/run-records.ts +689 -0
  45. package/src/domain/settlement.test.ts +186 -0
  46. package/src/domain/settlement.ts +137 -0
  47. package/src/domain/shapes.test.ts +185 -0
  48. package/src/domain/skills.test.ts +477 -0
  49. package/src/domain/skills.ts +114 -0
  50. package/src/domain/tool-schema.test.ts +46 -0
  51. package/src/domain/tool-schema.ts +20 -0
  52. package/src/domain/toolkit.test.ts +215 -0
  53. package/src/domain/toolkit.ts +374 -0
  54. package/src/domain/turn-budget.test.ts +37 -0
  55. package/src/domain/turn-budget.ts +23 -0
  56. package/src/domain/view.test.ts +803 -0
  57. package/src/domain/view.ts +491 -0
  58. package/src/example.test.ts +15 -0
  59. package/src/extension/commands.test.ts +485 -0
  60. package/src/extension/commands.ts +230 -0
  61. package/src/extension/entry.test.ts +1276 -0
  62. package/src/extension/entry.ts +380 -0
  63. package/src/extension/gate-alerts.test.ts +237 -0
  64. package/src/extension/gate-alerts.ts +143 -0
  65. package/src/extension/guard.test.ts +48 -0
  66. package/src/extension/guard.ts +31 -0
  67. package/src/extension/host-signals.test.ts +20 -0
  68. package/src/extension/host-signals.ts +13 -0
  69. package/src/extension/inspector-command.test.ts +1343 -0
  70. package/src/extension/inspector-command.ts +685 -0
  71. package/src/extension/prompt.test.ts +108 -0
  72. package/src/extension/prompt.ts +50 -0
  73. package/src/extension/request-alerts.test.ts +183 -0
  74. package/src/extension/request-alerts.ts +181 -0
  75. package/src/extension/retry-observer.test.ts +292 -0
  76. package/src/extension/retry-observer.ts +150 -0
  77. package/src/extension/revocation.test.ts +81 -0
  78. package/src/extension/revocation.ts +84 -0
  79. package/src/extension/settle-alerts.test.ts +579 -0
  80. package/src/extension/settle-alerts.ts +175 -0
  81. package/src/extension/shortcut.test.ts +62 -0
  82. package/src/extension/shortcut.ts +105 -0
  83. package/src/extension/tools.test.ts +1294 -0
  84. package/src/extension/tools.ts +553 -0
  85. package/src/extension/version.test.ts +115 -0
  86. package/src/extension/version.ts +97 -0
  87. package/src/goldens.test.ts +294 -0
  88. package/src/harness-policy.test.ts +76 -0
  89. package/src/layers.test.ts +309 -0
  90. package/src/layers.ts +405 -0
  91. package/src/package-manifest.test.ts +170 -0
  92. package/src/presentation/actions.test.ts +221 -0
  93. package/src/presentation/actions.ts +85 -0
  94. package/src/presentation/activity.test.ts +386 -0
  95. package/src/presentation/activity.ts +66 -0
  96. package/src/presentation/close.test.ts +359 -0
  97. package/src/presentation/inspector.test.ts +682 -0
  98. package/src/presentation/inspector.ts +442 -0
  99. package/src/presentation/overlay.test.ts +553 -0
  100. package/src/presentation/overlay.ts +229 -0
  101. package/src/presentation/repaint.test.ts +365 -0
  102. package/src/presentation/shortcut.ts +2 -0
  103. package/src/presentation/snapshot.test.ts +535 -0
  104. package/src/presentation/snapshot.ts +306 -0
  105. package/src/presentation/status.test.ts +43 -0
  106. package/src/presentation/status.ts +30 -0
  107. package/src/presentation/strip.test.ts +203 -0
  108. package/src/presentation/strip.ts +67 -0
  109. package/src/presentation/wording.test.ts +388 -0
  110. package/src/presentation/wording.ts +294 -0
  111. package/src/runtime/bridge.test.ts +131 -0
  112. package/src/runtime/bridge.ts +128 -0
  113. package/src/runtime/clock.test.ts +31 -0
  114. package/src/runtime/clock.ts +31 -0
  115. package/src/runtime/config.test.ts +63 -0
  116. package/src/runtime/config.ts +92 -0
  117. package/src/runtime/context.test.ts +19 -0
  118. package/src/runtime/context.ts +9 -0
  119. package/src/runtime/execution-env.test.ts +33 -0
  120. package/src/runtime/execution-env.ts +15 -0
  121. package/src/runtime/extensions.test.ts +86 -0
  122. package/src/runtime/extensions.ts +152 -0
  123. package/src/runtime/handle.ts +54 -0
  124. package/src/runtime/log.test.ts +140 -0
  125. package/src/runtime/log.ts +91 -0
  126. package/src/runtime/mcp.test.ts +1450 -0
  127. package/src/runtime/mcp.ts +1005 -0
  128. package/src/runtime/models.test.ts +145 -0
  129. package/src/runtime/models.ts +259 -0
  130. package/src/runtime/observe.test.ts +50 -0
  131. package/src/runtime/observe.ts +150 -0
  132. package/src/runtime/runtime-state.test.ts +25 -0
  133. package/src/runtime/runtime-state.ts +36 -0
  134. package/src/runtime/session-tag.ts +11 -0
  135. package/src/runtime/session.test.ts +352 -0
  136. package/src/runtime/session.ts +263 -0
  137. package/src/runtime/storage.ts +29 -0
  138. package/src/test-hygiene.test.ts +97 -0
@@ -0,0 +1,524 @@
1
+ import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
2
+ import {
3
+ AgentDoc,
4
+ Harness,
5
+ MemoryStorage,
6
+ type HarnessInspection,
7
+ } from "@earendil-works/pi-durable";
8
+ import { assert, it } from "@effect/vitest";
9
+ import { join, makeTempDir, writeText } from "../../../test-support/platform.ts";
10
+ import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
11
+ import { capArbitrary } from "../../../test-support/arbitraries.ts";
12
+ import { stubHostSource } from "../../../test-support/stub-model-source.ts";
13
+ import { createCatalogue, presets } from "../definitions.ts";
14
+ import { DEFAULTS, type DelegatesConfig } from "../../runtime/config.ts";
15
+ import { HostModelAdapter } from "../../runtime/models.ts";
16
+ import { type SessionIdentity } from "../../runtime/session.ts";
17
+
18
+ import { launch, type HostDefaults } from "./launch.ts";
19
+ import { available, liveCounts, type AdmissionHost } from "./admission.ts";
20
+ import { createDelegatesRegistry } from "../../runtime/extensions.ts";
21
+ import { SkillsCatalogue } from "../skills.ts";
22
+ import type { FanoutInput, FanoutState } from "./fanout.ts";
23
+ import { RunDoc, RunIndexDoc } from "../run-records.ts";
24
+ import { viewRun, viewSession } from "../view.ts";
25
+ import { waitFor } from "../../../test-support/wait.ts";
26
+ import { openTestSession } from "../../../test-support/seams.ts";
27
+
28
+ const host: HostDefaults = {
29
+ model: { provider: "stub", modelId: "stub-1" },
30
+ thinkingLevel: "low",
31
+ };
32
+
33
+ const config = (overrides: Partial<Omit<DelegatesConfig, "diagnostics">>): DelegatesConfig => ({
34
+ ...DEFAULTS,
35
+ ...overrides,
36
+ diagnostics: [],
37
+ });
38
+
39
+ const identityFor = async (sessionId: string): Promise<SessionIdentity> => {
40
+ const sessionDir = await makeTempDir("delegates-fanout-");
41
+ return { sessionId, sessionDir, cwd: sessionDir, agentDir: sessionDir };
42
+ };
43
+
44
+ const deferred = (): { readonly promise: Promise<void>; readonly release: () => void } => {
45
+ let release: () => void = () => {};
46
+ const promise = new Promise<void>((resolve) => {
47
+ release = resolve;
48
+ });
49
+ return { promise, release };
50
+ };
51
+
52
+ const six = ["scout", "builder", "reviewer", "scout", "builder", "reviewer"].map(
53
+ (agent, index) => ({
54
+ agent,
55
+ task: `task ${index}`,
56
+ }),
57
+ );
58
+
59
+ it("a fanout of six runs at most four generations at once", async () => {
60
+ const source = stubHostSource({ delayMs: 15 });
61
+ const runtime = await openTestSession({
62
+ identity: await identityFor("s-1"),
63
+ models: new HostModelAdapter(source),
64
+ });
65
+ try {
66
+ const result = await launch(
67
+ { kind: "fanout", lane: "review", agents: six },
68
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
69
+ );
70
+ assert.strictEqual(result.view.status, "answered");
71
+ assert.strictEqual(result.view.children.length, 6);
72
+ assert.deepStrictEqual(
73
+ result.view.children.map((child) => child.status),
74
+ Array.from({ length: 6 }, () => "answered"),
75
+ );
76
+ assert.ok(source.maxConcurrent <= 4, String(source.maxConcurrent));
77
+ } finally {
78
+ await runtime.close();
79
+ }
80
+ });
81
+
82
+ it("the session ceiling throttles two concurrent fanouts", async () => {
83
+ const source = stubHostSource({ delayMs: 15 });
84
+ const runtime = await openTestSession({
85
+ identity: await identityFor("s-1"),
86
+ models: new HostModelAdapter(source),
87
+ config: config({ laneCap: 10, sessionCeiling: 8 }),
88
+ });
89
+ try {
90
+ const deps = { runtime, catalogue: createCatalogue(), host, waitMs: 30_000 };
91
+ await Promise.all([
92
+ launch({ kind: "fanout", lane: "a", agents: six }, deps),
93
+ launch({ kind: "fanout", lane: "b", agents: six }, deps),
94
+ ]);
95
+ assert.ok(source.maxConcurrent <= 8, String(source.maxConcurrent));
96
+ } finally {
97
+ await runtime.close();
98
+ }
99
+ // Both launches share one budget of `waitMs`, so the wall must outlast that
100
+ // budget: a stall then fails on the launch result, not as a bare timeout.
101
+ }, 40_000);
102
+
103
+ it("fail-fast aborts the other children and reports the failing child", async () => {
104
+ const source = stubHostSource({ failAt: [2], delayAt: { 1: 0, 2: 0, 3: 2_000, 4: 2_000 } });
105
+ const runtime = await openTestSession({
106
+ identity: await identityFor("s-1"),
107
+ models: new HostModelAdapter(source),
108
+ });
109
+ try {
110
+ const result = await launch(
111
+ { kind: "fanout", lane: "review", agents: six, policy: "failFast" },
112
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
113
+ );
114
+ assert.strictEqual(result.view.status, "failed");
115
+ assert.ok(result.view.children.length < 6, String(result.view.children.length));
116
+ assert.ok(result.view.diagnostic?.includes("child 1"), result.view.diagnostic ?? "");
117
+ assert.ok(
118
+ result.view.children.some((child) => child.status === "stopped"),
119
+ JSON.stringify(result.view.children),
120
+ );
121
+ } finally {
122
+ await runtime.close();
123
+ }
124
+ });
125
+
126
+ it("caps come from delegates.json and survive a reopen", async () => {
127
+ const identity = await identityFor("s-1");
128
+ await writeText(
129
+ join(identity.agentDir, "delegates.json"),
130
+ JSON.stringify({ caps: { laneCap: 2, sessionCeiling: 3 } }),
131
+ );
132
+ const source = stubHostSource({ delayMs: 15 });
133
+ const first = await openTestSession({
134
+ identity,
135
+ models: new HostModelAdapter(source),
136
+ });
137
+ try {
138
+ assert.strictEqual(first.config.laneCap, 2);
139
+ await launch(
140
+ { kind: "fanout", lane: "review", agents: six },
141
+ { runtime: first, catalogue: createCatalogue(), host, waitMs: 20_000 },
142
+ );
143
+ assert.ok(source.maxConcurrent <= 2, String(source.maxConcurrent));
144
+ } finally {
145
+ await first.close();
146
+ }
147
+ const second = await openTestSession({ identity, models: new HostModelAdapter(source) });
148
+ try {
149
+ assert.strictEqual(second.config.laneCap, 2);
150
+ await launch(
151
+ { kind: "fanout", lane: "review", agents: six },
152
+ { runtime: second, catalogue: createCatalogue(), host, waitMs: 20_000 },
153
+ );
154
+ assert.ok(source.maxConcurrent <= 2, String(source.maxConcurrent));
155
+ } finally {
156
+ await second.close();
157
+ }
158
+ });
159
+
160
+ it("fail-fast admits no child after the failing one", async () => {
161
+ const source = stubHostSource({ failAt: [1] });
162
+ const runtime = await openTestSession({
163
+ identity: await identityFor("s-1"),
164
+ models: new HostModelAdapter(source),
165
+ config: config({ laneCap: 1, sessionCeiling: 8 }),
166
+ });
167
+ try {
168
+ const result = await launch(
169
+ { kind: "fanout", lane: "review", agents: six, policy: "failFast" },
170
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
171
+ );
172
+ assert.strictEqual(result.view.status, "failed");
173
+ assert.strictEqual(result.view.children.length, 1);
174
+ assert.strictEqual(result.view.children[0]?.status, "failed");
175
+ assert.ok(result.view.diagnostic?.includes("child 0"), result.view.diagnostic ?? "");
176
+ } finally {
177
+ await runtime.close();
178
+ }
179
+ });
180
+
181
+ it("the policy defaults to allSettled", async () => {
182
+ const source = stubHostSource({ failAt: [2] });
183
+ const runtime = await openTestSession({
184
+ identity: await identityFor("s-1"),
185
+ models: new HostModelAdapter(source),
186
+ });
187
+ try {
188
+ const result = await launch(
189
+ { kind: "fanout", lane: "review", agents: six },
190
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
191
+ );
192
+ assert.strictEqual(result.view.children.length, 6);
193
+ assert.strictEqual(result.view.children[1]?.status, "failed");
194
+ assert.strictEqual(result.view.status, "failed");
195
+ } finally {
196
+ await runtime.close();
197
+ }
198
+ });
199
+
200
+ it("a single launch over the ceiling parks queued and is admitted later", async () => {
201
+ const hold = deferred();
202
+ const source = stubHostSource({ hold: () => hold.promise });
203
+ const runtime = await openTestSession({
204
+ identity: await identityFor("s-1"),
205
+ models: new HostModelAdapter(source),
206
+ config: config({ sessionCeiling: 1 }),
207
+ });
208
+ try {
209
+ const first = launch(
210
+ { kind: "single", agent: "scout", task: "first" },
211
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
212
+ );
213
+ let firstRunId: string | undefined;
214
+ for (let i = 0; i < 100; i += 1) {
215
+ const index = await runtime.snapshot(RunIndexDoc);
216
+ firstRunId = index?.runIds[0];
217
+ if (firstRunId !== undefined) {
218
+ const viewed = await viewRun(firstRunId, runtime);
219
+ if (viewed.status === "running") break;
220
+ }
221
+ await new Promise((resolve) => setTimeout(resolve, 20));
222
+ }
223
+ assert.ok(firstRunId !== undefined);
224
+ const second = await launch(
225
+ { kind: "single", agent: "scout", task: "second" },
226
+ { runtime, catalogue: createCatalogue(), host, waitMs: 100 },
227
+ );
228
+ assert.strictEqual(second.view.status, "queued");
229
+ const queuedDoc = await runtime.snapshot(RunDoc, second.view.runId);
230
+ assert.notStrictEqual(queuedDoc?.taskId, null);
231
+
232
+ hold.release();
233
+ await first;
234
+ const settled = await launch(
235
+ { kind: "single", agent: "scout", task: "second" },
236
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
237
+ );
238
+ assert.strictEqual(settled.view.status, "answered");
239
+ assert.strictEqual(settled.view.runId, second.view.runId);
240
+ assert.ok(source.maxConcurrent <= 1, String(source.maxConcurrent));
241
+ const index = await runtime.snapshot(RunIndexDoc);
242
+ assert.strictEqual(index?.runIds.length, 2);
243
+ } finally {
244
+ await runtime.close();
245
+ }
246
+ }, 20_000);
247
+
248
+ it.prop(
249
+ "admission never overshoots a cap",
250
+ {
251
+ laneCap: capArbitrary,
252
+ laneLive: capArbitrary,
253
+ sessionCeiling: capArbitrary,
254
+ sessionLive: capArbitrary,
255
+ },
256
+ ({ laneCap, laneLive, sessionCeiling, sessionLive }) => {
257
+ const room = available(laneCap, laneLive, sessionCeiling, sessionLive);
258
+ assert.ok(room >= 0);
259
+ assert.ok(room <= Math.max(0, laneCap - laneLive));
260
+ assert.ok(room <= Math.max(0, sessionCeiling - sessionLive));
261
+ if (laneCap - laneLive <= 0 || sessionCeiling - sessionLive <= 0) {
262
+ assert.strictEqual(room, 0);
263
+ } else {
264
+ assert.strictEqual(room, Math.min(laneCap - laneLive, sessionCeiling - sessionLive));
265
+ }
266
+ },
267
+ { arbitrary: { runs: PROPERTY_RUNS } },
268
+ );
269
+
270
+ it("a fanout reserves another fanout's created-but-pending children", async () => {
271
+ const record = (id: number, status: string) => ({
272
+ id,
273
+ kind: "delegates.run",
274
+ input: { lane: "review" },
275
+ state: { status },
276
+ });
277
+ const admission: AdmissionHost = {
278
+ inspect: async () =>
279
+ ({
280
+ tasks: [{ record: record(1, "pending") }, { record: record(2, "pending") }],
281
+ }) as unknown as HarnessInspection,
282
+ caps: () => ({ laneCap: 2, sessionCeiling: 2 }),
283
+ pollMs: 60_000,
284
+ };
285
+ const { registry, tasks } = createDelegatesRegistry({
286
+ admission,
287
+ skills: SkillsCatalogue.make(),
288
+ });
289
+ const harness = await Harness.open(
290
+ new MemoryStorage(),
291
+ { models: new HostModelAdapter(stubHostSource()), registry },
292
+ BACKGROUND_CONTEXT,
293
+ );
294
+ try {
295
+ const root = await harness.root(BACKGROUND_CONTEXT, {
296
+ agent: { extensions: [], tools: [] },
297
+ });
298
+ harness.resume();
299
+ const input: FanoutInput = {
300
+ runId: "run:probe#1",
301
+ lane: "review",
302
+ children: [
303
+ { agent: "scout", task: "first", definition: presets[0]!, maxTurns: 10 },
304
+ { agent: "scout", task: "second", definition: presets[0]!, maxTurns: 10 },
305
+ ],
306
+ host,
307
+ maxTurns: 10,
308
+ policy: "allSettled",
309
+ review: false,
310
+ };
311
+ const taskId = await harness.commit(
312
+ async (tx) =>
313
+ tx.createTask(tasks.fanout, input, {
314
+ ownership: { kind: "conversation" },
315
+ conversationId: root.id,
316
+ }),
317
+ BACKGROUND_CONTEXT,
318
+ );
319
+ let task = await harness.getTask(taskId, BACKGROUND_CONTEXT);
320
+ for (let i = 0; i < 200; i += 1) {
321
+ if (task?.state.status !== "pending" && task?.state.status !== "running") break;
322
+ await new Promise((resolve) => setTimeout(resolve, 10));
323
+ task = await harness.getTask(taskId, BACKGROUND_CONTEXT);
324
+ }
325
+ assert.ok(task !== undefined);
326
+ assert.strictEqual(task.state.status, "sleeping");
327
+ const checkpoint = task.state.checkpoint as unknown as FanoutState;
328
+ assert.strictEqual(checkpoint.admitted, 0);
329
+ } finally {
330
+ await harness.close(BACKGROUND_CONTEXT);
331
+ }
332
+ }, 20_000);
333
+
334
+ it("live counts merge direct owned tasks without double counting", () => {
335
+ const record = (id: number, lane: string, status: string) => ({
336
+ id,
337
+ kind: "delegates.run",
338
+ input: { lane },
339
+ state: { status, ...(status === "sleeping" ? { signals: [{}] } : {}) },
340
+ });
341
+ const inspection = {
342
+ tasks: [{ record: record(1, "review", "running") }],
343
+ } as unknown as Parameters<typeof liveCounts>[0];
344
+ assert.deepStrictEqual(
345
+ liveCounts(inspection, "review", {
346
+ ownedTasks: [
347
+ record(1, "review", "running"),
348
+ record(2, "review", "sleeping"),
349
+ ] as unknown as NonNullable<NonNullable<Parameters<typeof liveCounts>[2]>["ownedTasks"]>,
350
+ }),
351
+ { laneLive: 2, sessionLive: 2 },
352
+ );
353
+ });
354
+
355
+ it("admission arithmetic never exceeds either difference", () => {
356
+ const values = [0, 1, 2, 3, 5, 8];
357
+ for (const laneCap of values) {
358
+ for (const laneLive of values) {
359
+ for (const sessionCeiling of values) {
360
+ for (const sessionLive of values) {
361
+ const room = available(laneCap, laneLive, sessionCeiling, sessionLive);
362
+ assert.ok(room >= 0);
363
+ assert.ok(room <= Math.max(0, laneCap - laneLive));
364
+ assert.ok(room <= Math.max(0, sessionCeiling - sessionLive));
365
+ if (laneCap - laneLive <= 0 || sessionCeiling - sessionLive <= 0) {
366
+ assert.strictEqual(room, 0);
367
+ } else {
368
+ assert.strictEqual(room, Math.min(laneCap - laneLive, sessionCeiling - sessionLive));
369
+ }
370
+ }
371
+ }
372
+ }
373
+ }
374
+ });
375
+
376
+ it("status reports one row per child and lane counts", async () => {
377
+ const runtime = await openTestSession({
378
+ identity: await identityFor("s-1"),
379
+ models: new HostModelAdapter(stubHostSource()),
380
+ });
381
+ try {
382
+ const result = await launch(
383
+ { kind: "fanout", lane: "review", agents: six },
384
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
385
+ );
386
+ const run = await viewRun(result.view.runId, runtime);
387
+ assert.deepStrictEqual(
388
+ run.children.map((child) => child.status),
389
+ Array.from({ length: 6 }, () => "answered"),
390
+ );
391
+ const session = await viewSession(runtime);
392
+ assert.deepStrictEqual(session.lanes, [{ name: "review", running: 0, queued: 0, answered: 6 }]);
393
+ assert.strictEqual(result.text, `Run ${result.view.runId}: 6 children answered`);
394
+ } finally {
395
+ await runtime.close();
396
+ }
397
+ });
398
+
399
+ it("a run with a missing toolkit name reports the diagnostic", async () => {
400
+ const runtime = await openTestSession({
401
+ identity: await identityFor("s-1"),
402
+ models: new HostModelAdapter(stubHostSource()),
403
+ });
404
+ try {
405
+ const result = await launch(
406
+ { kind: "single", agent: "scout", task: "look" },
407
+ { runtime, catalogue: createCatalogue(), host, waitMs: 10_000 },
408
+ );
409
+ const doc = await runtime.snapshot(RunDoc, result.view.runId);
410
+ const childId = doc!.childConversationId as never;
411
+ await runtime.harness.commit(async (tx) => {
412
+ const agent = await tx.doc(AgentDoc, childId);
413
+ agent.extensions = ["gone-extension"];
414
+ }, BACKGROUND_CONTEXT);
415
+ const view = await viewRun(result.view.runId, runtime);
416
+ assert.deepStrictEqual(view.missing, ["gone-extension"]);
417
+ } finally {
418
+ await runtime.close();
419
+ }
420
+ });
421
+
422
+ it("a detached fanout returns one entry per child with an empty answer", async () => {
423
+ const held = deferred();
424
+ const source = stubHostSource({ text: "done", hold: () => held.promise, holdAt: [1, 2] });
425
+ const runtime = await openTestSession({
426
+ identity: await identityFor("s-detached"),
427
+ models: new HostModelAdapter(source),
428
+ storage: new MemoryStorage(),
429
+ });
430
+ const settled: string[] = [];
431
+ runtime.settle.on((event) => settled.push(event.runId));
432
+ try {
433
+ const result = await launch(
434
+ {
435
+ kind: "fanout",
436
+ lane: "review",
437
+ agents: [
438
+ { agent: "scout", task: "first" },
439
+ { agent: "scout", task: "second" },
440
+ ],
441
+ execution: "detached",
442
+ },
443
+ { runtime, catalogue: createCatalogue(), host, waitMs: 5_000 },
444
+ );
445
+ assert.ok(
446
+ result.view.status === "queued" ||
447
+ result.view.status === "running" ||
448
+ result.view.status === "waiting",
449
+ result.view.status,
450
+ );
451
+ assert.strictEqual(result.view.children.length, 2);
452
+ // A detached group is at work, not blocked on a decision: the launch text says so.
453
+ assert.match(result.text, /^Run .* is (queued|running)\.$/, result.text);
454
+ for (const child of result.view.children) {
455
+ assert.strictEqual(child.answer, null);
456
+ assert.ok(child.status === "queued" || child.status === "running", child.status);
457
+ }
458
+ const parent = await runtime.snapshot(RunDoc, result.view.runId);
459
+ assert.strictEqual(parent?.execution, "detached");
460
+ assert.strictEqual(parent?.childRunIds.length, 2);
461
+ for (const runId of parent?.childRunIds ?? []) {
462
+ const child = await runtime.snapshot(RunDoc, runId);
463
+ assert.strictEqual(child?.execution, "detached");
464
+ // The notice marks a child with ` · parent <run id>`; the group is that parent.
465
+ assert.strictEqual(child?.parentRunId, result.view.runId);
466
+ }
467
+ held.release();
468
+ await waitFor(
469
+ async () => (await viewRun(result.view.runId, runtime)).status === "answered",
470
+ "the detached fanout settles",
471
+ 10_000,
472
+ );
473
+ // The group is a run of its own: its resolution admits it to the settle notice.
474
+ const group = await runtime.snapshot(RunDoc, result.view.runId);
475
+ assert.strictEqual(group?.resolution, "answered");
476
+ assert.strictEqual(group?.failure, null);
477
+ assert.ok(settled.includes(result.view.runId), settled.join(","));
478
+ } finally {
479
+ await runtime.close();
480
+ }
481
+ }, 20_000);
482
+
483
+ it("per-child budgets resolve per child inside one group", async () => {
484
+ const runtime = await openTestSession({
485
+ identity: await identityFor("s-budget"),
486
+ models: new HostModelAdapter(stubHostSource({ text: "ok" })),
487
+ storage: new MemoryStorage(),
488
+ config: config({ maxTurnsCeiling: 150 }),
489
+ });
490
+ try {
491
+ const result = await launch(
492
+ {
493
+ kind: "fanout",
494
+ lane: "review",
495
+ maxTurns: 200,
496
+ agents: [
497
+ { agent: "scout", task: "first", maxTurns: 60 },
498
+ { agent: "scout", task: "second" },
499
+ ],
500
+ },
501
+ { runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
502
+ );
503
+ const parent = await runtime.snapshot(RunDoc, result.view.runId);
504
+ const docs = [];
505
+ for (const runId of parent?.childRunIds ?? []) {
506
+ docs.push(await runtime.snapshot(RunDoc, runId));
507
+ }
508
+ assert.deepStrictEqual(
509
+ docs.map((doc) => doc?.maxTurns),
510
+ [60, 150],
511
+ );
512
+ for (const doc of docs) {
513
+ assert.ok(doc?.childConversationId !== null && doc?.childConversationId !== undefined);
514
+ const agent = await runtime.harness.snapshot(
515
+ AgentDoc,
516
+ doc.childConversationId as never,
517
+ BACKGROUND_CONTEXT,
518
+ );
519
+ assert.strictEqual(agent?.maxTurns, doc.maxTurns);
520
+ }
521
+ } finally {
522
+ await runtime.close();
523
+ }
524
+ }, 20_000);