clanka 0.21.0 → 0.22.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 (45) hide show
  1. package/dist/Acp.test.js +19 -7
  2. package/dist/Acp.test.js.map +1 -1
  3. package/dist/Agent.d.ts +4 -4
  4. package/dist/Agent.d.ts.map +1 -1
  5. package/dist/Agent.js +140 -99
  6. package/dist/Agent.js.map +1 -1
  7. package/dist/Agent.test.js +5 -4
  8. package/dist/Agent.test.js.map +1 -1
  9. package/dist/AgentBashJobs.test.js +47 -32
  10. package/dist/AgentBashJobs.test.js.map +1 -1
  11. package/dist/AgentCallerConfig.test.js +26 -7
  12. package/dist/AgentCallerConfig.test.js.map +1 -1
  13. package/dist/AgentExecutor.d.ts +71 -41
  14. package/dist/AgentExecutor.d.ts.map +1 -1
  15. package/dist/AgentExecutor.js +86 -79
  16. package/dist/AgentExecutor.js.map +1 -1
  17. package/dist/AgentExecutor.test.js +108 -83
  18. package/dist/AgentExecutor.test.js.map +1 -1
  19. package/dist/AgentExecutorImage.test.js +2 -2
  20. package/dist/AgentExecutorImage.test.js.map +1 -1
  21. package/dist/AgentImage.test.js +3 -2
  22. package/dist/AgentImage.test.js.map +1 -1
  23. package/dist/AgentSubagents.test.js +218 -29
  24. package/dist/AgentSubagents.test.js.map +1 -1
  25. package/dist/AgentTools.d.ts +318 -290
  26. package/dist/AgentTools.d.ts.map +1 -1
  27. package/dist/AgentTools.js +218 -199
  28. package/dist/AgentTools.js.map +1 -1
  29. package/dist/AgentTools.test.js +2 -2
  30. package/dist/AgentTools.test.js.map +1 -1
  31. package/dist/bin/cli.mjs +308 -289
  32. package/dist/bin/cli.mjs.map +1 -1
  33. package/package.json +1 -1
  34. package/src/Acp.test.ts +31 -10
  35. package/src/Agent.test.ts +5 -4
  36. package/src/Agent.ts +175 -140
  37. package/src/AgentBashJobs.test.ts +70 -38
  38. package/src/AgentCallerConfig.test.ts +39 -7
  39. package/src/AgentExecutor.test.ts +132 -91
  40. package/src/AgentExecutor.ts +120 -105
  41. package/src/AgentExecutorImage.test.ts +2 -2
  42. package/src/AgentImage.test.ts +4 -3
  43. package/src/AgentSubagents.test.ts +825 -562
  44. package/src/AgentTools.test.ts +2 -2
  45. package/src/AgentTools.ts +331 -288
@@ -1,4 +1,4 @@
1
- import { assert, it } from "@effect/vitest"
1
+ import { assert, describe, it } from "@effect/vitest"
2
2
  import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"
3
3
  import * as NodeServices from "@effect/platform-node/NodeServices"
4
4
  import * as Effect from "effect/Effect"
@@ -26,14 +26,15 @@ import type { Output } from "./AgentOutput.ts"
26
26
  import * as Compaction from "./Compaction.ts"
27
27
 
28
28
  // =============================================================================
29
- // Background sub-agents: lifetime and completion delivery.
29
+ // Agent tasks: lifetime and completion delivery.
30
30
  //
31
31
  // These drive the real Agent loop and a real executor, so the model only
32
- // talks to the script API: `delegate({ prompt, background })`, `waitAgents`,
33
- // `messageAgent` and `stopAgent`. One scripted LanguageModel serves every
34
- // agent; calls are routed by the task marker in each agent's first user
35
- // message. Agent IDs come from the shared counter: the root run is "0" and
36
- // the first sub-agent is "1".
32
+ // talks to the script API: `delegate({ prompt, background })`, `waitTasks`,
33
+ // `messageAgent`, `stopTask`, `listTasks` and `taskOutput`. One scripted
34
+ // LanguageModel serves every agent; calls are routed by the task marker in
35
+ // each agent's first user message. Task IDs come from the executor's counter,
36
+ // shared by bash and agent tasks. Each test gets its own executor, so the
37
+ // first task is "1".
37
38
  //
38
39
  // Sub-agents always finish with `taskComplete`: with the local executor they
39
40
  // currently run outside the parent's ConversationMode, so a text-only turn
@@ -185,10 +186,10 @@ const messageJsons = (prompt: Prompt.Prompt): Array<string> =>
185
186
  const findPush = (
186
187
  prompt: Prompt.Prompt,
187
188
  id: string,
188
- status: "completed" | "failed" | "cancelled",
189
+ status: "completed" | "failed" | "stopped",
189
190
  ): { readonly index: number; readonly text: string } => {
190
191
  const pattern = new RegExp(
191
- `<subagent id="${id}" status="${status}">[\\s\\S]*</subagent>`,
192
+ `<task id="${id}" kind="agent" status="${status}">[\\s\\S]*</task>`,
192
193
  )
193
194
  const index = prompt.content.findIndex(
194
195
  (message) => message.role === "user" && pattern.test(messageText(message)),
@@ -203,11 +204,11 @@ const pushTexts = (prompt: Prompt.Prompt): Array<string> =>
203
204
  prompt.content.flatMap((message) => {
204
205
  if (message.role !== "user") return []
205
206
  const text = messageText(message)
206
- return text.includes("<subagent id=") ? [text] : []
207
+ return text.includes("<task id=") ? [text] : []
207
208
  })
208
209
 
209
210
  const hasAnyPush = (prompt: Prompt.Prompt, id: string): boolean =>
210
- pushTexts(prompt).some((push) => push.includes(`<subagent id="${id}"`))
211
+ pushTexts(prompt).some((push) => push.includes(`<task id="${id}"`))
211
212
 
212
213
  const waitFor = (condition: () => boolean) =>
213
214
  Effect.gen(function* () {
@@ -219,39 +220,43 @@ const unexpected = (call: Call) =>
219
220
 
220
221
  // --- executors ---------------------------------------------------------------
221
222
 
222
- const localExecutorLayer = Layer.unwrap(
223
- Effect.gen(function* () {
224
- const fs = yield* FileSystem.FileSystem
225
- const directory = yield* fs.makeTempDirectoryScoped()
226
- return AgentExecutor.layerLocal({ directory })
227
- }),
228
- ).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)))
223
+ const localExecutorLayer = (limits?: AgentExecutor.Limits) =>
224
+ Layer.unwrap(
225
+ Effect.gen(function* () {
226
+ const fs = yield* FileSystem.FileSystem
227
+ const directory = yield* fs.makeTempDirectoryScoped()
228
+ return AgentExecutor.layerLocal({ directory, limits })
229
+ }),
230
+ ).pipe(
231
+ Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
232
+ )
229
233
 
230
- const rpcExecutorLayer = Layer.unwrap(
231
- Effect.gen(function* () {
232
- const fs = yield* FileSystem.FileSystem
233
- const directory = yield* fs.makeTempDirectoryScoped()
234
- const { protocol, httpEffect } =
235
- yield* RpcServer.makeProtocolWithHttpEffect()
236
- yield* Layer.build(
237
- AgentExecutor.layerRpcServer({ directory }).pipe(
238
- Layer.provide(Layer.succeed(RpcServer.Protocol, protocol)),
234
+ const rpcExecutorLayer = (limits?: AgentExecutor.Limits) =>
235
+ Layer.unwrap(
236
+ Effect.gen(function* () {
237
+ const fs = yield* FileSystem.FileSystem
238
+ const directory = yield* fs.makeTempDirectoryScoped()
239
+ const { protocol, httpEffect } =
240
+ yield* RpcServer.makeProtocolWithHttpEffect()
241
+ yield* Layer.build(
242
+ AgentExecutor.layerRpcServer({ directory, limits }).pipe(
243
+ Layer.provide(Layer.succeed(RpcServer.Protocol, protocol)),
244
+ ),
245
+ )
246
+ yield* HttpServer.serveEffect(httpEffect)
247
+ return AgentExecutor.layerRpc.pipe(
248
+ Layer.provide(RpcClient.layerProtocolHttp({ url: "" })),
249
+ )
250
+ }),
251
+ ).pipe(
252
+ Layer.provide(
253
+ Layer.mergeAll(
254
+ RpcSerialization.layerNdjson,
255
+ NodeHttpServer.layerTest,
256
+ NodeServices.layer,
239
257
  ),
240
- )
241
- yield* HttpServer.serveEffect(httpEffect)
242
- return AgentExecutor.layerRpc.pipe(
243
- Layer.provide(RpcClient.layerProtocolHttp({ url: "" })),
244
- )
245
- }),
246
- ).pipe(
247
- Layer.provide(
248
- Layer.mergeAll(
249
- RpcSerialization.layerNdjson,
250
- NodeHttpServer.layerTest,
251
- NodeServices.layer,
252
258
  ),
253
- ),
254
- )
259
+ )
255
260
 
256
261
  const executors = [
257
262
  ["local", localExecutorLayer],
@@ -261,578 +266,836 @@ const executors = [
261
266
  // --- tests -------------------------------------------------------------------
262
267
 
263
268
  for (const [executorName, executorLayer] of executors) {
264
- it.layer(executorLayer, { excludeTestServices: true })(
265
- `background sub-agents (${executorName} executor)`,
266
- (it) => {
267
- it.effect(
268
- "an idle conversation turn waits for a running child, then gets its capped output",
269
- () =>
270
- Effect.gen(function* () {
271
- const childRelease = Latch.makeUnsafe(false)
272
- const childOutput =
273
- "CHILD-HEAD " + "x".repeat(40_000) + " CHILD-TAIL"
274
- const { exit, calls } = yield* runAgent({
275
- conversationMode: true,
276
- respond: (call) => {
277
- if (call.role === "child") {
278
- // Finishes only after the parent's idle turn is over, so
279
- // the parent has to block on the completion push.
280
- return Stream.fromEffect(
281
- childRelease.await.pipe(
282
- Effect.andThen(Effect.sleep("50 millis")),
283
- ),
284
- ).pipe(Stream.flatMap(() => complete(childOutput)))
285
- }
286
- switch (call.index) {
287
- case 0:
288
- return Stream.make(spawnBackground("TASK-CHILD"))
289
- case 1:
290
- return Stream.fromIterable(
291
- text("waiting for the child"),
269
+ // A fresh executor per test keeps task ids predictable.
270
+ const test = (
271
+ name: string,
272
+ self: () => Effect.Effect<void, unknown, AgentExecutor.AgentExecutor>,
273
+ limits?: AgentExecutor.Limits,
274
+ ) => it.live(name, () => self().pipe(Effect.provide(executorLayer(limits))))
275
+
276
+ describe(`agent tasks (${executorName} executor)`, () => {
277
+ test("an idle conversation turn waits for a running child, then gets its capped output", () =>
278
+ Effect.gen(function* () {
279
+ const childRelease = Latch.makeUnsafe(false)
280
+ const childOutput = "CHILD-HEAD " + "x".repeat(40_000) + " CHILD-TAIL"
281
+ const { exit, calls } = yield* runAgent({
282
+ conversationMode: true,
283
+ respond: (call) => {
284
+ if (call.role === "child") {
285
+ // Finishes only after the parent's idle turn is over, so
286
+ // the parent has to block on the completion push.
287
+ return Stream.fromEffect(
288
+ childRelease.await.pipe(
289
+ Effect.andThen(Effect.sleep("50 millis")),
290
+ ),
291
+ ).pipe(Stream.flatMap(() => complete(childOutput)))
292
+ }
293
+ switch (call.index) {
294
+ case 0:
295
+ return Stream.make(spawnBackground("TASK-CHILD"))
296
+ case 1:
297
+ return Stream.fromIterable(text("waiting for the child")).pipe(
298
+ Stream.ensuring(Effect.sync(() => childRelease.openUnsafe())),
299
+ )
300
+ case 2:
301
+ return Stream.fromIterable(text("all done"))
302
+ default:
303
+ return unexpected(call)
304
+ }
305
+ },
306
+ })
307
+
308
+ assert.deepStrictEqual(exit, Exit.succeed("all done"))
309
+ const root = calls.filter((call) => call.role === "root")
310
+ assert.strictEqual(root.length, 3)
311
+
312
+ const started = lastToolResult(root[1]!.prompt)
313
+ assert.include(started, '"id":"1"')
314
+ assert.include(started, '"kind":"agent"')
315
+ assert.include(started, '"status":"running"')
316
+ assert.include(started, '"description":"TASK-CHILD"')
317
+
318
+ const push = findPush(root[2]!.prompt, "1", "completed")
319
+ assert.notStrictEqual(push.index, -1, "completion push delivered")
320
+ assert.include(push.text, "CHILD-HEAD")
321
+ assert.include(push.text, "CHILD-TAIL")
322
+ assert.isAtMost(
323
+ push.text.length,
324
+ Compaction.executeOutputCapChars + 1_000,
325
+ "push output goes through capOutput",
326
+ )
327
+ }))
328
+
329
+ test("a completion push does not cut off the parent's in-progress response", () =>
330
+ Effect.gen(function* () {
331
+ const childRelease = Latch.makeUnsafe(false)
332
+ const completionDelivered = Latch.makeUnsafe(false)
333
+ const { exit, calls } = yield* runAgent({
334
+ conversationMode: true,
335
+ onOutput: (output) =>
336
+ Effect.sync(() => {
337
+ if (output._tag === "SubagentComplete" && output.id === 1) {
338
+ assert.strictEqual(output.status, "completed")
339
+ assert.strictEqual(output.summary, "CHILD-RESULT")
340
+ completionDelivered.openUnsafe()
341
+ }
342
+ }),
343
+ respond: (call) => {
344
+ if (call.role === "child") {
345
+ return Stream.fromEffect(childRelease.await).pipe(
346
+ Stream.flatMap(() => complete("CHILD-RESULT")),
347
+ )
348
+ }
349
+ switch (call.index) {
350
+ case 0:
351
+ return Stream.make(spawnBackground("TASK-CHILD"))
352
+ case 1:
353
+ // The child completes between two text blocks of one
354
+ // response. The whole response must still be recorded.
355
+ return Stream.fromIterable(text("PART-ONE", "t1")).pipe(
356
+ Stream.concat(
357
+ Stream.fromEffect(
358
+ Effect.gen(function* () {
359
+ childRelease.openUnsafe()
360
+ // Unlike the child model stream ending, this event
361
+ // means cleanup finished and the push was queued.
362
+ yield* completionDelivered.await
363
+ }),
364
+ ).pipe(Stream.drain),
365
+ ),
366
+ Stream.concat(
367
+ Stream.fromIterable([
368
+ ...text("PART-TWO", "t2"),
369
+ toolCall("after", 'console.log("AFTER-SCRIPT-RAN")'),
370
+ ]),
371
+ ),
372
+ )
373
+ default:
374
+ return Stream.fromIterable(text("done"))
375
+ }
376
+ },
377
+ })
378
+
379
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
380
+ const root = calls.filter((call) => call.role === "root")
381
+ const delivery = root.find(
382
+ (call) => findPush(call.prompt, "1", "completed").index !== -1,
383
+ )
384
+ assert.isDefined(delivery, "completion push reached the model")
385
+ const prompt = delivery!.prompt
386
+ const assistantIndex = prompt.content.findIndex(
387
+ (message) =>
388
+ message.role === "assistant" &&
389
+ messageText(message).includes("PART-ONE"),
390
+ )
391
+ assert.notStrictEqual(assistantIndex, -1)
392
+ assert.include(
393
+ messageText(prompt.content[assistantIndex]!),
394
+ "PART-TWO",
395
+ "the response continued past the first text-end",
396
+ )
397
+ assert.include(
398
+ lastToolResult(prompt),
399
+ "AFTER-SCRIPT-RAN",
400
+ "the tool call after the push still ran",
401
+ )
402
+ const push = findPush(prompt, "1", "completed")
403
+ assert.include(push.text, "CHILD-RESULT")
404
+ assert.isAbove(
405
+ push.index,
406
+ assistantIndex,
407
+ "the push is injected after the response, not into it",
408
+ )
409
+ }))
410
+
411
+ test("a failed background child is pushed as failed and the parent keeps going", () =>
412
+ Effect.gen(function* () {
413
+ const childRelease = Latch.makeUnsafe(false)
414
+ const { exit, calls } = yield* runAgent({
415
+ conversationMode: true,
416
+ respond: (call) => {
417
+ if (call.role === "child") {
418
+ return Stream.fromEffect(childRelease.await).pipe(
419
+ Stream.flatMap(() => Stream.fail(invalidRequest)),
420
+ )
421
+ }
422
+ switch (call.index) {
423
+ case 0:
424
+ return Stream.make(spawnBackground("TASK-CHILD"))
425
+ case 1:
426
+ return Stream.fromIterable(text("waiting")).pipe(
427
+ Stream.ensuring(Effect.sync(() => childRelease.openUnsafe())),
428
+ )
429
+ case 2:
430
+ return Stream.fromIterable(text("recovered"))
431
+ default:
432
+ return unexpected(call)
433
+ }
434
+ },
435
+ })
436
+
437
+ assert.deepStrictEqual(exit, Exit.succeed("recovered"))
438
+ const root = calls.filter((call) => call.role === "root")
439
+ const push = findPush(root[2]!.prompt, "1", "failed")
440
+ assert.notStrictEqual(push.index, -1, "failure push delivered")
441
+ assert.include(push.text, "rejected")
442
+ }))
443
+
444
+ test("taskComplete fails while a child is running and leaves it running", () =>
445
+ Effect.gen(function* () {
446
+ const childRelease = Latch.makeUnsafe(false)
447
+ const { exit, calls, history } = yield* runAgent({
448
+ conversationMode: false,
449
+ respond: (call) => {
450
+ if (call.role === "child") {
451
+ return call.index === 0
452
+ ? Stream.fromEffect(childRelease.await).pipe(
453
+ Stream.flatMap(() => complete("CHILD-RESULT")),
454
+ )
455
+ : unexpected(call)
456
+ }
457
+ switch (call.index) {
458
+ case 0:
459
+ return Stream.make(
460
+ toolCall(
461
+ "too-early",
462
+ `await delegate({ prompt: "TASK-CHILD", background: true })
463
+ try {
464
+ await taskComplete("too early")
465
+ console.log("ACCEPTED")
466
+ } catch (error) {
467
+ console.log("REJECTED: " + String(error))
468
+ }`,
469
+ ),
470
+ )
471
+ case 1: {
472
+ const rejected = lastToolResult(call.prompt)
473
+ assert.include(rejected, "REJECTED")
474
+ assert.notInclude(rejected, "ACCEPTED")
475
+ assert.include(
476
+ rejected,
477
+ 'waitTasks({ ids: ["1"] })',
478
+ "quotes the call that waits for the running child",
479
+ )
480
+ assert.include(rejected, "stopTask")
481
+ childRelease.openUnsafe()
482
+ return Stream.make(
483
+ toolCall(
484
+ "wait",
485
+ `const results = await waitTasks({ ids: ["1"] })
486
+ console.log(JSON.stringify(results))
487
+ await taskComplete("done")`,
488
+ ),
489
+ )
490
+ }
491
+ default:
492
+ return unexpected(call)
493
+ }
494
+ },
495
+ })
496
+
497
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
498
+ assert.strictEqual(
499
+ calls.filter((call) => call.role === "root").length,
500
+ 2,
501
+ )
502
+ const waited = lastToolResult(history)
503
+ assert.include(waited, '"id":"1"')
504
+ assert.include(
505
+ waited,
506
+ '"status":"completed"',
507
+ "the child was not cancelled by the refused taskComplete",
508
+ )
509
+ assert.include(waited, "CHILD-RESULT")
510
+ }))
511
+
512
+ test("background children do not outlive the parent run", () =>
513
+ Effect.gen(function* () {
514
+ const childStarted = Latch.makeUnsafe(false)
515
+ let childInterrupted = false
516
+ let interruptedBeforeTeardown = false
517
+ const { exit } = yield* runAgent({
518
+ conversationMode: true,
519
+ respond: (call) => {
520
+ if (call.role === "child") {
521
+ return Stream.fromEffect(
522
+ Effect.sync(() => childStarted.openUnsafe()).pipe(
523
+ Effect.andThen(Effect.never),
524
+ ),
525
+ ).pipe(
526
+ Stream.ensuring(
527
+ Effect.sync(() => {
528
+ childInterrupted = true
529
+ }),
530
+ ),
531
+ )
532
+ }
533
+ switch (call.index) {
534
+ case 0:
535
+ return Stream.make(spawnBackground("TASK-CHILD"))
536
+ case 1:
537
+ // The parent run fails while its child is still running.
538
+ return Stream.fromEffect(childStarted.await).pipe(
539
+ Stream.flatMap(() => Stream.fail(invalidRequest)),
540
+ )
541
+ default:
542
+ return unexpected(call)
543
+ }
544
+ },
545
+ afterExit: waitFor(() => childInterrupted).pipe(
546
+ Effect.map((interrupted) => {
547
+ interruptedBeforeTeardown = interrupted
548
+ }),
549
+ ),
550
+ })
551
+
552
+ assert.isTrue(Exit.isFailure(exit), "the parent run fails")
553
+ assert.isTrue(
554
+ interruptedBeforeTeardown,
555
+ "the child is interrupted when the run ends",
556
+ )
557
+ }))
558
+
559
+ test("stopTask stops the agent and everything under it", () =>
560
+ Effect.gen(function* () {
561
+ const grandchildStarted = Latch.makeUnsafe(false)
562
+ const childBlocked = Latch.makeUnsafe(false)
563
+ let childInterrupted = false
564
+ let grandchildInterrupted = false
565
+ let interruptedBeforeNextTurn = false
566
+ const { exit, calls } = yield* runAgent({
567
+ conversationMode: true,
568
+ respond: (call) => {
569
+ switch (call.role) {
570
+ case "grandchild":
571
+ return Stream.fromEffect(
572
+ Effect.sync(() => grandchildStarted.openUnsafe()).pipe(
573
+ Effect.andThen(Effect.never),
574
+ ),
575
+ ).pipe(
576
+ Stream.ensuring(
577
+ Effect.sync(() => {
578
+ grandchildInterrupted = true
579
+ }),
580
+ ),
581
+ )
582
+ case "child":
583
+ return call.index === 0
584
+ ? Stream.make(spawnBackground("TASK-GRANDCHILD"))
585
+ : Stream.fromEffect(
586
+ Effect.sync(() => childBlocked.openUnsafe()).pipe(
587
+ Effect.andThen(Effect.never),
588
+ ),
292
589
  ).pipe(
293
590
  Stream.ensuring(
294
- Effect.sync(() => childRelease.openUnsafe()),
591
+ Effect.sync(() => {
592
+ childInterrupted = true
593
+ }),
295
594
  ),
296
595
  )
297
- case 2:
298
- return Stream.fromIterable(text("all done"))
299
- default:
300
- return unexpected(call)
301
- }
302
- },
303
- })
304
-
305
- assert.deepStrictEqual(exit, Exit.succeed("all done"))
306
- const root = calls.filter((call) => call.role === "root")
307
- assert.strictEqual(root.length, 3)
308
-
309
- const started = lastToolResult(root[1]!.prompt)
310
- assert.include(started, '"id":"1"')
311
- assert.include(started, '"status":"running"')
312
- assert.notInclude(started, '"output"', "no output while running")
313
-
314
- const push = findPush(root[2]!.prompt, "1", "completed")
315
- assert.notStrictEqual(push.index, -1, "completion push delivered")
316
- assert.include(push.text, "CHILD-HEAD")
317
- assert.include(push.text, "CHILD-TAIL")
318
- assert.isAtMost(
319
- push.text.length,
320
- Compaction.executeOutputCapChars + 1_000,
321
- "push output goes through capOutput",
322
- )
323
- }),
324
- )
325
-
326
- it.effect(
327
- "a completion push does not cut off the parent's in-progress response",
328
- () =>
329
- Effect.gen(function* () {
330
- const childRelease = Latch.makeUnsafe(false)
331
- const completionDelivered = Latch.makeUnsafe(false)
332
- const { exit, calls } = yield* runAgent({
333
- conversationMode: true,
334
- onOutput: (output) =>
335
- Effect.sync(() => {
336
- if (output._tag === "SubagentComplete" && output.id === 1) {
337
- assert.strictEqual(output.status, "completed")
338
- assert.strictEqual(output.summary, "CHILD-RESULT")
339
- completionDelivered.openUnsafe()
340
- }
341
- }),
342
- respond: (call) => {
343
- if (call.role === "child") {
344
- return Stream.fromEffect(childRelease.await).pipe(
345
- Stream.flatMap(() => complete("CHILD-RESULT")),
346
- )
347
- }
596
+ case "root":
348
597
  switch (call.index) {
349
598
  case 0:
350
599
  return Stream.make(spawnBackground("TASK-CHILD"))
351
600
  case 1:
352
- // The child completes between two text blocks of one
353
- // response. The whole response must still be recorded.
354
- return Stream.fromIterable(text("PART-ONE", "t1")).pipe(
355
- Stream.concat(
356
- Stream.fromEffect(
357
- Effect.gen(function* () {
358
- childRelease.openUnsafe()
359
- // Unlike the child model stream ending, this event
360
- // means cleanup finished and the push was queued.
361
- yield* completionDelivered.await
362
- }),
363
- ).pipe(Stream.drain),
364
- ),
365
- Stream.concat(
366
- Stream.fromIterable([
367
- ...text("PART-TWO", "t2"),
368
- toolCall("after", 'console.log("AFTER-SCRIPT-RAN")'),
369
- ]),
601
+ return Stream.fromEffect(grandchildStarted.await).pipe(
602
+ Stream.tap(() => childBlocked.await),
603
+ Stream.as(
604
+ toolCall(
605
+ "stop",
606
+ `const result = await stopTask("1")
607
+ console.log(JSON.stringify(result))`,
608
+ ),
370
609
  ),
371
610
  )
372
611
  default:
373
- return Stream.fromIterable(text("done"))
612
+ if (call.index === 2) {
613
+ interruptedBeforeNextTurn =
614
+ childInterrupted && grandchildInterrupted
615
+ }
616
+ return Stream.fromIterable(text("stopped"))
374
617
  }
375
- },
376
- })
618
+ }
619
+ },
620
+ })
377
621
 
378
- assert.deepStrictEqual(exit, Exit.succeed("done"))
379
- const root = calls.filter((call) => call.role === "root")
380
- const delivery = root.find(
381
- (call) => findPush(call.prompt, "1", "completed").index !== -1,
382
- )
383
- assert.isDefined(delivery, "completion push reached the model")
384
- const prompt = delivery!.prompt
385
- const assistantIndex = prompt.content.findIndex(
386
- (message) =>
387
- message.role === "assistant" &&
388
- messageText(message).includes("PART-ONE"),
389
- )
390
- assert.notStrictEqual(assistantIndex, -1)
391
- assert.include(
392
- messageText(prompt.content[assistantIndex]!),
393
- "PART-TWO",
394
- "the response continued past the first text-end",
395
- )
396
- assert.include(
397
- lastToolResult(prompt),
398
- "AFTER-SCRIPT-RAN",
399
- "the tool call after the push still ran",
400
- )
401
- const push = findPush(prompt, "1", "completed")
402
- assert.include(push.text, "CHILD-RESULT")
403
- assert.isAbove(
404
- push.index,
405
- assistantIndex,
406
- "the push is injected after the response, not into it",
407
- )
408
- }),
409
- )
622
+ assert.deepStrictEqual(exit, Exit.succeed("stopped"))
623
+ const root = calls.filter((call) => call.role === "root")
624
+ const stopped = lastToolResult(root[2]!.prompt)
625
+ assert.include(stopped, '"id":"1"')
626
+ assert.include(stopped, '"status":"stopped"')
627
+ assert.isTrue(
628
+ interruptedBeforeNextTurn,
629
+ "stopTask interrupts the child and its grandchild before returning",
630
+ )
631
+ }))
410
632
 
411
- it.effect(
412
- "a failed background child is pushed as failed and the parent keeps going",
413
- () =>
414
- Effect.gen(function* () {
415
- const childRelease = Latch.makeUnsafe(false)
416
- const { exit, calls } = yield* runAgent({
417
- conversationMode: true,
418
- respond: (call) => {
419
- if (call.role === "child") {
420
- return Stream.fromEffect(childRelease.await).pipe(
421
- Stream.flatMap(() => Stream.fail(invalidRequest)),
633
+ test("messageAgent resumes a finished child on top of its history", () =>
634
+ Effect.gen(function* () {
635
+ let messageCall: number | undefined
636
+ let resumedPrompt: Prompt.Prompt | undefined
637
+ const { exit, calls } = yield* runAgent({
638
+ conversationMode: true,
639
+ respond: (call) => {
640
+ if (call.role === "child") {
641
+ switch (call.index) {
642
+ case 0:
643
+ return complete("FIRST-ANSWER")
644
+ case 1:
645
+ resumedPrompt = call.prompt
646
+ return complete("SECOND-ANSWER")
647
+ default:
648
+ return unexpected(call)
649
+ }
650
+ }
651
+ // Decide from what has been delivered, not from call order:
652
+ // pushes may land before or after an idle turn.
653
+ const pushes = pushTexts(call.prompt)
654
+ if (call.index === 0) {
655
+ return Stream.make(spawnBackground("TASK-CHILD"))
656
+ }
657
+ if (pushes.some((push) => push.includes("SECOND-ANSWER"))) {
658
+ return Stream.fromIterable(text("resumed"))
659
+ }
660
+ if (
661
+ messageCall === undefined &&
662
+ pushes.some((push) => push.includes("FIRST-ANSWER"))
663
+ ) {
664
+ messageCall = call.index
665
+ return Stream.make(
666
+ toolCall(
667
+ "message",
668
+ `const result = await messageAgent({ id: "1", message: "FOLLOW-UP" })
669
+ console.log(JSON.stringify(result))`,
670
+ ),
671
+ )
672
+ }
673
+ return Stream.fromIterable(text("waiting"))
674
+ },
675
+ })
676
+
677
+ assert.deepStrictEqual(exit, Exit.succeed("resumed"))
678
+ assert.isDefined(messageCall, "the first answer was pushed")
679
+ const root = calls.filter((call) => call.role === "root")
680
+ const messaged = lastToolResult(root[messageCall! + 1]!.prompt)
681
+ assert.include(messaged, '"id":"1"')
682
+ assert.include(
683
+ messaged,
684
+ '"status":"running"',
685
+ "messageAgent returns without waiting for the new turn",
686
+ )
687
+
688
+ assert.isDefined(resumedPrompt, "the finished child was resumed")
689
+ const resumed = messageJsons(resumedPrompt!)
690
+ const task = resumed.findIndex((m) => m.includes("TASK-CHILD"))
691
+ const first = resumed.findIndex((m) => m.includes("FIRST-ANSWER"))
692
+ const followUp = resumed.findIndex((m) => m.includes("FOLLOW-UP"))
693
+ assert.notStrictEqual(task, -1, "original task kept")
694
+ assert.isAbove(first, task, "first answer kept")
695
+ assert.isAbove(followUp, first, "message appended after history")
696
+ }))
697
+
698
+ test("pushes go to the agent that spawned the child, at every level", () =>
699
+ Effect.gen(function* () {
700
+ const { exit, calls } = yield* runAgent({
701
+ conversationMode: true,
702
+ respond: (call) => {
703
+ switch (call.role) {
704
+ case "grandchild":
705
+ return complete("GRAND-RESULT")
706
+ case "child":
707
+ if (
708
+ findPush(call.prompt, "2", "completed").text.includes(
709
+ "GRAND-RESULT",
422
710
  )
711
+ ) {
712
+ return complete("CHILD-DONE")
423
713
  }
424
- switch (call.index) {
425
- case 0:
426
- return Stream.make(spawnBackground("TASK-CHILD"))
427
- case 1:
428
- return Stream.fromIterable(text("waiting")).pipe(
429
- Stream.ensuring(
430
- Effect.sync(() => childRelease.openUnsafe()),
714
+ // Keep taking turns until the push is drained into the
715
+ // child's own prompt.
716
+ return call.index === 0
717
+ ? Stream.make(spawnBackground("TASK-GRANDCHILD"))
718
+ : Stream.fromEffect(Effect.sleep("20 millis")).pipe(
719
+ Stream.flatMap(() =>
720
+ Stream.fromIterable(text("child waiting")),
431
721
  ),
432
722
  )
433
- case 2:
434
- return Stream.fromIterable(text("recovered"))
435
- default:
436
- return unexpected(call)
437
- }
438
- },
439
- })
440
-
441
- assert.deepStrictEqual(exit, Exit.succeed("recovered"))
442
- const root = calls.filter((call) => call.role === "root")
443
- const push = findPush(root[2]!.prompt, "1", "failed")
444
- assert.notStrictEqual(push.index, -1, "failure push delivered")
445
- assert.include(push.text, "rejected")
446
- }),
447
- )
448
-
449
- it.effect(
450
- "taskComplete fails while a child is running and leaves it running",
451
- () =>
452
- Effect.gen(function* () {
453
- const childRelease = Latch.makeUnsafe(false)
454
- const { exit, calls, history } = yield* runAgent({
455
- conversationMode: false,
456
- respond: (call) => {
457
- if (call.role === "child") {
458
- return call.index === 0
459
- ? Stream.fromEffect(childRelease.await).pipe(
460
- Stream.flatMap(() => complete("CHILD-RESULT")),
461
- )
462
- : unexpected(call)
463
- }
723
+ case "root":
464
724
  switch (call.index) {
465
725
  case 0:
726
+ // Foreground: blocks until the child is done.
466
727
  return Stream.make(
467
728
  toolCall(
468
- "too-early",
469
- `await delegate({ prompt: "TASK-CHILD", background: true })
470
- try {
471
- await taskComplete("too early")
472
- console.log("ACCEPTED")
473
- } catch (error) {
474
- console.log("REJECTED: " + String(error))
475
- }`,
476
- ),
477
- )
478
- case 1: {
479
- const rejected = lastToolResult(call.prompt)
480
- assert.include(rejected, "REJECTED")
481
- assert.notInclude(rejected, "ACCEPTED")
482
- assert.match(rejected, /\b1\b/, "lists the running child")
483
- assert.include(rejected, "waitAgents")
484
- assert.include(rejected, "stopAgent")
485
- childRelease.openUnsafe()
486
- return Stream.make(
487
- toolCall(
488
- "wait",
489
- `const results = await waitAgents()
490
- console.log(JSON.stringify(results))
491
- await taskComplete("done")`,
729
+ "foreground",
730
+ `const result = await delegate({ prompt: "TASK-CHILD" })
731
+ console.log(JSON.stringify(result))`,
492
732
  ),
493
733
  )
494
- }
734
+ case 1:
735
+ return Stream.fromIterable(text("root done"))
495
736
  default:
496
737
  return unexpected(call)
497
738
  }
498
- },
499
- })
739
+ }
740
+ },
741
+ })
500
742
 
501
- assert.deepStrictEqual(exit, Exit.succeed("done"))
502
- assert.strictEqual(
503
- calls.filter((call) => call.role === "root").length,
504
- 2,
505
- )
506
- const waited = lastToolResult(history)
507
- assert.include(waited, '"id":"1"')
508
- assert.include(
509
- waited,
510
- '"status":"completed"',
511
- "the child was not cancelled by the refused taskComplete",
512
- )
513
- assert.include(waited, "CHILD-RESULT")
514
- }),
515
- )
743
+ assert.deepStrictEqual(exit, Exit.succeed("root done"))
744
+ const root = calls.filter((call) => call.role === "root")
745
+ assert.strictEqual(root.length, 2)
746
+ const foreground = lastToolResult(root[1]!.prompt)
747
+ assert.include(foreground, '"id":"1"')
748
+ assert.include(foreground, '"status":"completed"')
749
+ assert.include(foreground, "CHILD-DONE")
750
+ for (const call of root) {
751
+ assert.isFalse(
752
+ hasAnyPush(call.prompt, "2"),
753
+ "the grandchild's push never reaches the root",
754
+ )
755
+ }
756
+ }))
516
757
 
517
- it.effect("background children do not outlive the parent run", () =>
518
- Effect.gen(function* () {
519
- const childStarted = Latch.makeUnsafe(false)
520
- let childInterrupted = false
521
- let interruptedBeforeTeardown = false
522
- const { exit } = yield* runAgent({
523
- conversationMode: true,
524
- respond: (call) => {
525
- if (call.role === "child") {
526
- return Stream.fromEffect(
527
- Effect.sync(() => childStarted.openUnsafe()).pipe(
528
- Effect.andThen(Effect.never),
758
+ test("each agent keeps its own working directory, starting from its parent's", () =>
759
+ Effect.gen(function* () {
760
+ // Both children change directory before either prints it, so a
761
+ // shared directory shows up as the wrong path.
762
+ const bothChanged = Latch.makeUnsafe(false)
763
+ let changed = 0
764
+ const { exit, calls } = yield* runAgent({
765
+ conversationMode: true,
766
+ respond: (call) => {
767
+ if (call.role === "child") {
768
+ const name = firstUserText(call.prompt).includes("ONE")
769
+ ? "one"
770
+ : "two"
771
+ if (toolResultTexts(call.prompt).length === 0) {
772
+ return Stream.make(
773
+ toolCall(`cd-${name}`, `await changeDirectory("${name}")`),
774
+ )
775
+ }
776
+ if (++changed === 2) bothChanged.openUnsafe()
777
+ return Stream.fromEffect(bothChanged.await).pipe(
778
+ Stream.flatMap(() =>
779
+ Stream.make(
780
+ toolCall(
781
+ `pwd-${name}`,
782
+ `const pwd = await bash({ command: "pwd" })
783
+ await taskComplete("${name.toUpperCase()}:" + pwd.output.trim())`,
784
+ ),
529
785
  ),
530
- ).pipe(
531
- Stream.ensuring(
532
- Effect.sync(() => {
533
- childInterrupted = true
534
- }),
786
+ ),
787
+ )
788
+ }
789
+ switch (call.index) {
790
+ case 0:
791
+ return Stream.make(
792
+ toolCall(
793
+ "spawn",
794
+ `await bash({ command: "mkdir -p a/one a/two" })
795
+ await changeDirectory("a")
796
+ const one = await delegate({ prompt: "TASK-CHILD-ONE", background: true })
797
+ const two = await delegate({ prompt: "TASK-CHILD-TWO", background: true })
798
+ console.log(JSON.stringify(await waitTasks({ ids: [one.id, two.id] })))
799
+ console.log("ROOT:" + (await bash({ command: "pwd" })).output.trim())`,
535
800
  ),
536
801
  )
537
- }
538
- switch (call.index) {
539
- case 0:
540
- return Stream.make(spawnBackground("TASK-CHILD"))
541
- case 1:
542
- // The parent run fails while its child is still running.
543
- return Stream.fromEffect(childStarted.await).pipe(
544
- Stream.flatMap(() => Stream.fail(invalidRequest)),
545
- )
546
- default:
547
- return unexpected(call)
548
- }
549
- },
550
- afterExit: waitFor(() => childInterrupted).pipe(
551
- Effect.map((interrupted) => {
552
- interruptedBeforeTeardown = interrupted
553
- }),
554
- ),
555
- })
802
+ case 1:
803
+ return Stream.fromIterable(text("done"))
804
+ default:
805
+ return unexpected(call)
806
+ }
807
+ },
808
+ })
556
809
 
557
- assert.isTrue(Exit.isFailure(exit), "the parent run fails")
558
- assert.isTrue(
559
- interruptedBeforeTeardown,
560
- "the child is interrupted when the run ends",
561
- )
562
- }),
563
- )
810
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
811
+ const root = calls.filter((call) => call.role === "root")
812
+ const result = lastToolResult(root[1]!.prompt)
813
+ assert.match(result, /ONE:[^"\s]*\/a\/one"/)
814
+ assert.match(result, /TWO:[^"\s]*\/a\/two"/)
815
+ assert.match(result, /ROOT:\S*\/a$/m)
816
+ }))
817
+
818
+ test("waitTasks waits for bash and agent tasks alike, and what it returns is not pushed", () =>
819
+ Effect.gen(function* () {
820
+ const { exit, calls } = yield* runAgent({
821
+ conversationMode: true,
822
+ respond: (call) => {
823
+ if (call.role === "child") return complete("CHILD-OUT")
824
+ switch (call.index) {
825
+ case 0:
826
+ return Stream.make(
827
+ toolCall(
828
+ "mixed",
829
+ `const bashTask = await bash({ command: "echo BASH-OUT", background: true })
830
+ const agentTask = await delegate({ prompt: "TASK-CHILD", background: true })
831
+ const ended = await waitTasks({ ids: [bashTask.id, agentTask.id] })
832
+ console.log(JSON.stringify(ended.map(({ kind, status, output }) => ({ kind, status, output: output.trim() }))))`,
833
+ ),
834
+ )
835
+ case 1:
836
+ return Stream.fromIterable(text("done"))
837
+ default:
838
+ return unexpected(call)
839
+ }
840
+ },
841
+ })
842
+
843
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
844
+ const root = calls.filter((call) => call.role === "root")
845
+ assert.strictEqual(root.length, 2, "no late push wakes the run")
846
+ assert.include(
847
+ lastToolResult(root[1]!.prompt),
848
+ JSON.stringify([
849
+ { kind: "bash", status: "completed", output: "BASH-OUT" },
850
+ { kind: "agent", status: "completed", output: "CHILD-OUT" },
851
+ ]),
852
+ )
853
+ assert.deepStrictEqual(
854
+ pushTexts(root[1]!.prompt),
855
+ [],
856
+ "results returned by waitTasks are not pushed again",
857
+ )
858
+ }))
859
+
860
+ test("an agent only sees and addresses its own tasks", () =>
861
+ Effect.gen(function* () {
862
+ const { exit, calls } = yield* runAgent({
863
+ conversationMode: true,
864
+ respond: (call) => {
865
+ if (call.role === "child") {
866
+ // The parent's bash task id, passed in the task prompt.
867
+ const bashId = /BASH-ID:(\w+)/.exec(
868
+ firstUserText(call.prompt),
869
+ )![1]
870
+ return Stream.make(
871
+ toolCall(
872
+ "peek",
873
+ `const seen = (await listTasks()).map((task) => task.id)
874
+ const attempts = []
875
+ for (const attempt of [() => taskOutput("${bashId}"), () => stopTask("${bashId}")]) {
876
+ try {
877
+ await attempt()
878
+ attempts.push("allowed")
879
+ } catch {
880
+ attempts.push("rejected")
881
+ }
882
+ }
883
+ await taskComplete(JSON.stringify({ seen, attempts }))`,
884
+ ),
885
+ )
886
+ }
887
+ switch (call.index) {
888
+ case 0:
889
+ return Stream.make(
890
+ toolCall(
891
+ "spawn",
892
+ `const server = await bash({ command: "sleep 30", background: true })
893
+ const child = await delegate({ prompt: "TASK-CHILD BASH-ID:" + server.id })
894
+ console.log("CHILD:" + child.output)
895
+ const mine = await listTasks()
896
+ console.log("ROOT:" + JSON.stringify(mine.map((task) => task.kind + ":" + task.status).sort()))`,
897
+ ),
898
+ )
899
+ case 1:
900
+ return Stream.fromIterable(text("done"))
901
+ default:
902
+ return unexpected(call)
903
+ }
904
+ },
905
+ })
906
+
907
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
908
+ const root = calls.filter((call) => call.role === "root")
909
+ const result = lastToolResult(root[1]!.prompt)
910
+ assert.include(
911
+ result,
912
+ 'CHILD:{"seen":[],"attempts":["rejected","rejected"]}',
913
+ "the child neither lists nor addresses its parent's bash task",
914
+ )
915
+ assert.include(
916
+ result,
917
+ 'ROOT:["agent:completed","bash:running"]',
918
+ "the parent lists both of its tasks, and its bash task still runs",
919
+ )
920
+ }))
921
+
922
+ // A bounded wait that passes its deadline returns the agent as running and
923
+ // leaves it running; its result is pushed when it ends.
924
+ test("waitTasks returns a running agent at its deadline and leaves it running", () =>
925
+ Effect.gen(function* () {
926
+ const childRelease = Latch.makeUnsafe(false)
927
+ let childInterrupted = false
928
+ let waited = ""
929
+ const { exit, calls } = yield* runAgent({
930
+ conversationMode: true,
931
+ respond: (call) => {
932
+ if (call.role === "child") {
933
+ return call.index === 0
934
+ ? Stream.fromEffect(
935
+ childRelease.await.pipe(
936
+ Effect.onInterrupt(() =>
937
+ Effect.sync(() => {
938
+ childInterrupted = true
939
+ }),
940
+ ),
941
+ ),
942
+ ).pipe(Stream.flatMap(() => complete("CHILD-LATE")))
943
+ : unexpected(call)
944
+ }
945
+ switch (call.index) {
946
+ case 0:
947
+ return Stream.make(
948
+ toolCall(
949
+ "wait",
950
+ `const child = await delegate({ prompt: "TASK-CHILD", background: true })
951
+ const [task] = await waitTasks({ ids: [child.id], timeoutMs: 200 })
952
+ console.log("WAITED:" + task.status)`,
953
+ ),
954
+ )
955
+ case 1:
956
+ waited = lastToolResult(call.prompt)
957
+ // The child ends only now, after the wait gave up.
958
+ return Stream.fromIterable(text("waiting")).pipe(
959
+ Stream.ensuring(Effect.sync(() => childRelease.openUnsafe())),
960
+ )
961
+ case 2:
962
+ return Stream.fromIterable(text("done"))
963
+ default:
964
+ return unexpected(call)
965
+ }
966
+ },
967
+ })
564
968
 
565
- it.effect("stopAgent cancels the agent and everything under it", () =>
969
+ assert.include(waited, "WAITED:running", "returned at the deadline")
970
+ assert.isFalse(childInterrupted, "the deadline does not stop the agent")
971
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
972
+ const root = calls.filter((call) => call.role === "root")
973
+ assert.strictEqual(root.length, 3)
974
+ assert.include(
975
+ findPush(root[2]!.prompt, "1", "completed").text,
976
+ "CHILD-LATE",
977
+ "its result is pushed once it ends",
978
+ )
979
+ }))
980
+
981
+ // The script times out while `waitTasks` still waits for the slow task,
982
+ // so it never returns the two that ended. They must still be pushed.
983
+ test(
984
+ "tasks a waitTasks call never returned are still pushed",
985
+ () =>
566
986
  Effect.gen(function* () {
567
- const grandchildStarted = Latch.makeUnsafe(false)
568
- const childBlocked = Latch.makeUnsafe(false)
569
- let childInterrupted = false
570
- let grandchildInterrupted = false
571
- let interruptedBeforeNextTurn = false
572
987
  const { exit, calls } = yield* runAgent({
573
988
  conversationMode: true,
574
989
  respond: (call) => {
575
- switch (call.role) {
576
- case "grandchild":
577
- return Stream.fromEffect(
578
- Effect.sync(() => grandchildStarted.openUnsafe()).pipe(
579
- Effect.andThen(Effect.never),
580
- ),
581
- ).pipe(
582
- Stream.ensuring(
583
- Effect.sync(() => {
584
- grandchildInterrupted = true
585
- }),
990
+ if (call.role === "child") return complete("CHILD-OUT")
991
+ switch (call.index) {
992
+ case 0:
993
+ return Stream.make(
994
+ toolCall(
995
+ "wait",
996
+ `const quick = await bash({ command: "echo BASH-OUT", background: true })
997
+ const child = await delegate({ prompt: "TASK-CHILD", background: true })
998
+ const slow = await bash({ command: "sleep 30", background: true })
999
+ await waitTasks({ ids: [quick.id, child.id, slow.id] })
1000
+ console.log("RETURNED")`,
586
1001
  ),
587
1002
  )
588
- case "child":
589
- return call.index === 0
590
- ? Stream.make(spawnBackground("TASK-GRANDCHILD"))
591
- : Stream.fromEffect(
592
- Effect.sync(() => childBlocked.openUnsafe()).pipe(
593
- Effect.andThen(Effect.never),
594
- ),
595
- ).pipe(
596
- Stream.ensuring(
597
- Effect.sync(() => {
598
- childInterrupted = true
599
- }),
600
- ),
601
- )
602
- case "root":
603
- switch (call.index) {
604
- case 0:
605
- return Stream.make(spawnBackground("TASK-CHILD"))
606
- case 1:
607
- return Stream.fromEffect(grandchildStarted.await).pipe(
608
- Stream.tap(() => childBlocked.await),
609
- Stream.as(
610
- toolCall(
611
- "stop",
612
- `const result = await stopAgent("1")
613
- console.log(JSON.stringify(result))`,
614
- ),
615
- ),
616
- )
617
- default:
618
- if (call.index === 2) {
619
- interruptedBeforeNextTurn =
620
- childInterrupted && grandchildInterrupted
621
- }
622
- return Stream.fromIterable(text("stopped"))
623
- }
1003
+ case 1:
1004
+ return Stream.fromIterable(text("done"))
1005
+ default:
1006
+ return unexpected(call)
624
1007
  }
625
1008
  },
626
1009
  })
627
1010
 
628
- assert.deepStrictEqual(exit, Exit.succeed("stopped"))
1011
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
629
1012
  const root = calls.filter((call) => call.role === "root")
630
- const stopped = lastToolResult(root[2]!.prompt)
631
- assert.include(stopped, '"id":"1"')
632
- assert.include(stopped, '"status":"cancelled"')
1013
+ const result = lastToolResult(root[1]!.prompt)
1014
+ assert.include(result, "timed out")
1015
+ assert.notInclude(result, "RETURNED")
633
1016
  assert.isTrue(
634
- interruptedBeforeNextTurn,
635
- "stopAgent interrupts the child and its grandchild before returning",
1017
+ pushTexts(root[1]!.prompt).some((push) =>
1018
+ /^<task id="1" kind="bash" status="completed" exitCode="0">\s*BASH-OUT\s*<\/task>$/.test(
1019
+ push,
1020
+ ),
1021
+ ),
1022
+ "the bash task is pushed",
1023
+ )
1024
+ assert.include(
1025
+ findPush(root[1]!.prompt, "2", "completed").text,
1026
+ "CHILD-OUT",
1027
+ "the agent task is pushed",
636
1028
  )
637
1029
  }),
638
- )
1030
+ { scriptTimeout: "1 second" },
1031
+ )
1032
+ })
1033
+ }
639
1034
 
640
- it.effect(
641
- "messageAgent resumes a finished child on top of its history",
642
- () =>
643
- Effect.gen(function* () {
644
- let messageCall: number | undefined
645
- let resumedPrompt: Prompt.Prompt | undefined
646
- const { exit, calls } = yield* runAgent({
647
- conversationMode: true,
648
- respond: (call) => {
649
- if (call.role === "child") {
650
- switch (call.index) {
651
- case 0:
652
- return complete("FIRST-ANSWER")
653
- case 1:
654
- resumedPrompt = call.prompt
655
- return complete("SECOND-ANSWER")
656
- default:
657
- return unexpected(call)
658
- }
659
- }
660
- // Decide from what has been delivered, not from call order:
661
- // pushes may land before or after an idle turn.
662
- const pushes = pushTexts(call.prompt)
663
- if (call.index === 0) {
664
- return Stream.make(spawnBackground("TASK-CHILD"))
665
- }
666
- if (pushes.some((push) => push.includes("SECOND-ANSWER"))) {
667
- return Stream.fromIterable(text("resumed"))
668
- }
669
- if (
670
- messageCall === undefined &&
671
- pushes.some((push) => push.includes("FIRST-ANSWER"))
672
- ) {
673
- messageCall = call.index
674
- return Stream.make(
675
- toolCall(
676
- "message",
677
- `const result = await messageAgent({ id: "1", message: "FOLLOW-UP" })
678
- console.log(JSON.stringify(result))`,
679
- ),
680
- )
681
- }
682
- return Stream.fromIterable(text("waiting"))
683
- },
684
- })
685
-
686
- assert.deepStrictEqual(exit, Exit.succeed("resumed"))
687
- assert.isDefined(messageCall, "the first answer was pushed")
688
- const root = calls.filter((call) => call.role === "root")
689
- const messaged = lastToolResult(root[messageCall! + 1]!.prompt)
690
- assert.include(messaged, '"id":"1"')
691
- assert.include(
692
- messaged,
693
- '"status":"running"',
694
- "messageAgent returns without waiting for the new turn",
695
- )
1035
+ // Over RPC every task callback is a round trip to the client. Responses that
1036
+ // take 400ms each, against a 1 second script limit, leave no time for one
1037
+ // round trip per agent after the wait: all of the batch must be reported in
1038
+ // one, or results that were marked reported are neither returned nor pushed.
1039
+ const slow = <A, E>(effect: Effect.Effect<A, E>) =>
1040
+ Effect.tap(effect, () => Effect.sleep("400 millis"))
696
1041
 
697
- assert.isDefined(resumedPrompt, "the finished child was resumed")
698
- const resumed = messageJsons(resumedPrompt!)
699
- const task = resumed.findIndex((m) => m.includes("TASK-CHILD"))
700
- const first = resumed.findIndex((m) => m.includes("FIRST-ANSWER"))
701
- const followUp = resumed.findIndex((m) => m.includes("FOLLOW-UP"))
702
- assert.notStrictEqual(task, -1, "original task kept")
703
- assert.isAbove(first, task, "first answer kept")
704
- assert.isAbove(followUp, first, "message appended after history")
705
- }),
706
- )
1042
+ const slowCallbacks = (
1043
+ tasks: AgentExecutor.ExecuteAgentTasks,
1044
+ ): AgentExecutor.ExecuteAgentTasks => ({
1045
+ ...tasks,
1046
+ taskOutput: (id) => slow(tasks.taskOutput(id)),
1047
+ waitTasks: (...args) => slow(tasks.waitTasks(...args)),
1048
+ messageAgent: (options) => slow(tasks.messageAgent(options)),
1049
+ stopTask: (id) => slow(tasks.stopTask(id)),
1050
+ listTasks: slow(tasks.listTasks),
1051
+ })
707
1052
 
708
- it.effect(
709
- "pushes go to the agent that spawned the child, at every level",
710
- () =>
711
- Effect.gen(function* () {
712
- const { exit, calls } = yield* runAgent({
713
- conversationMode: true,
714
- respond: (call) => {
715
- switch (call.role) {
716
- case "grandchild":
717
- return complete("GRAND-RESULT")
718
- case "child":
719
- if (
720
- findPush(call.prompt, "2", "completed").text.includes(
721
- "GRAND-RESULT",
722
- )
723
- ) {
724
- return complete("CHILD-DONE")
725
- }
726
- // Keep taking turns until the push is drained into the
727
- // child's own prompt.
728
- return call.index === 0
729
- ? Stream.make(spawnBackground("TASK-GRANDCHILD"))
730
- : Stream.fromEffect(Effect.sleep("20 millis")).pipe(
731
- Stream.flatMap(() =>
732
- Stream.fromIterable(text("child waiting")),
733
- ),
734
- )
735
- case "root":
736
- switch (call.index) {
737
- case 0:
738
- // Foreground: blocks until the child is done.
739
- return Stream.make(
740
- toolCall(
741
- "foreground",
742
- `const result = await delegate({ prompt: "TASK-CHILD" })
743
- console.log(JSON.stringify(result))`,
744
- ),
745
- )
746
- case 1:
747
- return Stream.fromIterable(text("root done"))
748
- default:
749
- return unexpected(call)
750
- }
751
- }
752
- },
753
- })
754
-
755
- assert.deepStrictEqual(exit, Exit.succeed("root done"))
756
- const root = calls.filter((call) => call.role === "root")
757
- assert.strictEqual(root.length, 2)
758
- const foreground = lastToolResult(root[1]!.prompt)
759
- assert.include(foreground, '"id":"1"')
760
- assert.include(foreground, '"status":"completed"')
761
- assert.include(foreground, "CHILD-DONE")
762
- for (const call of root) {
763
- assert.isFalse(
764
- hasAnyPush(call.prompt, "2"),
765
- "the grandchild's push never reaches the root",
766
- )
767
- }
1053
+ describe("agent tasks (rpc executor, slow callbacks)", () => {
1054
+ it.live("every result of a waitTasks batch reaches the model", () =>
1055
+ Effect.gen(function* () {
1056
+ const executor = yield* AgentExecutor.AgentExecutor
1057
+ const slowExecutor = AgentExecutor.AgentExecutor.of({
1058
+ ...executor,
1059
+ execute: (options) =>
1060
+ executor.execute({
1061
+ ...options,
1062
+ agentTasks: slowCallbacks(options.agentTasks),
768
1063
  }),
769
- )
1064
+ })
1065
+ const { exit, calls } = yield* runAgent({
1066
+ conversationMode: true,
1067
+ respond: (call) => {
1068
+ if (call.role === "child") {
1069
+ return complete(
1070
+ firstUserText(call.prompt).includes("TASK-CHILD-A")
1071
+ ? "CHILD-A-OUT"
1072
+ : "CHILD-B-OUT",
1073
+ )
1074
+ }
1075
+ switch (call.index) {
1076
+ case 0:
1077
+ return Stream.make(
1078
+ toolCall(
1079
+ "wait",
1080
+ `const a = await delegate({ prompt: "TASK-CHILD-A", background: true })
1081
+ const b = await delegate({ prompt: "TASK-CHILD-B", background: true })
1082
+ const ended = await waitTasks({ ids: [a.id, b.id] })
1083
+ console.log(ended.map((task) => task.output).join(" "))`,
1084
+ ),
1085
+ )
1086
+ case 1:
1087
+ return Stream.fromIterable(text("done"))
1088
+ default:
1089
+ return unexpected(call)
1090
+ }
1091
+ },
1092
+ }).pipe(Effect.provideService(AgentExecutor.AgentExecutor, slowExecutor))
770
1093
 
771
- it.effect(
772
- "each agent keeps its own working directory, starting from its parent's",
773
- () =>
774
- Effect.gen(function* () {
775
- // Both children change directory before either prints it, so a
776
- // shared directory shows up as the wrong path.
777
- const bothChanged = Latch.makeUnsafe(false)
778
- let changed = 0
779
- const { exit, calls } = yield* runAgent({
780
- conversationMode: true,
781
- respond: (call) => {
782
- if (call.role === "child") {
783
- const name = firstUserText(call.prompt).includes("ONE")
784
- ? "one"
785
- : "two"
786
- if (toolResultTexts(call.prompt).length === 0) {
787
- return Stream.make(
788
- toolCall(
789
- `cd-${name}`,
790
- `await changeDirectory("${name}")`,
791
- ),
792
- )
793
- }
794
- if (++changed === 2) bothChanged.openUnsafe()
795
- return Stream.fromEffect(bothChanged.await).pipe(
796
- Stream.flatMap(() =>
797
- Stream.make(
798
- toolCall(
799
- `pwd-${name}`,
800
- `const pwd = await bash({ command: "pwd" })
801
- await taskComplete("${name.toUpperCase()}:" + pwd.output.trim())`,
802
- ),
803
- ),
804
- ),
805
- )
806
- }
807
- switch (call.index) {
808
- case 0:
809
- return Stream.make(
810
- toolCall(
811
- "spawn",
812
- `await bash({ command: "mkdir -p a/one a/two" })
813
- await changeDirectory("a")
814
- await delegate({ prompt: "TASK-CHILD-ONE", background: true })
815
- await delegate({ prompt: "TASK-CHILD-TWO", background: true })
816
- console.log(JSON.stringify(await waitAgents()))
817
- console.log("ROOT:" + (await bash({ command: "pwd" })).output.trim())`,
818
- ),
819
- )
820
- case 1:
821
- return Stream.fromIterable(text("done"))
822
- default:
823
- return unexpected(call)
824
- }
825
- },
826
- })
827
-
828
- assert.deepStrictEqual(exit, Exit.succeed("done"))
829
- const root = calls.filter((call) => call.role === "root")
830
- const result = lastToolResult(root[1]!.prompt)
831
- assert.match(result, /ONE:[^"\s]*\/a\/one"/)
832
- assert.match(result, /TWO:[^"\s]*\/a\/two"/)
833
- assert.match(result, /ROOT:\S*\/a$/m)
834
- }),
835
- )
836
- },
1094
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
1095
+ const prompt = calls.filter((call) => call.role === "root")[1]!.prompt
1096
+ const seen = [lastToolResult(prompt), ...pushTexts(prompt)].join("\n")
1097
+ assert.include(seen, "CHILD-A-OUT", "returned or pushed")
1098
+ assert.include(seen, "CHILD-B-OUT", "returned or pushed")
1099
+ }).pipe(Effect.provide(rpcExecutorLayer({ scriptTimeout: "1 second" }))),
837
1100
  )
838
- }
1101
+ })