clanka 0.24.5 → 0.25.1

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.
@@ -286,34 +286,45 @@ await taskComplete(missing + " " + call)`,
286
286
 
287
287
  // The shared task functions address bash and agent tasks, so they stay
288
288
  // while either kind can still be started.
289
- it.live("the task functions go only when bash and subagent both go", () =>
290
- Effect.gen(function* () {
291
- const tasks =
292
- "[typeof taskOutput, typeof waitTasks, typeof stopTask, typeof listTasks].join()"
293
- const withBash = yield* runAgent({
294
- mode,
295
- executor: { excludeTools: ["subagent"] },
296
- script: `await taskComplete(${tasks})`,
297
- })
298
- assert.strictEqual(
299
- withBash.summary,
300
- "function,function,function,function",
301
- )
302
- const withNeither = yield* runAgent({
303
- mode,
304
- executor: { excludeTools: ["bash", "subagent"] },
305
- script: `await taskComplete(${tasks})`,
306
- })
307
- assert.strictEqual(
308
- withNeither.summary,
309
- "undefined,undefined,undefined,undefined",
310
- )
311
- assert.notMatch(
312
- withNeither.system,
313
- /\b(taskOutput|waitTasks|stopTask|listTasks)\b/,
314
- )
315
- assert.notInclude(withNeither.system, "<task")
316
- }),
289
+ it.live(
290
+ "the task functions go only when bash, run and subagent all go",
291
+ () =>
292
+ Effect.gen(function* () {
293
+ const tasks =
294
+ "[typeof taskOutput, typeof waitTasks, typeof stopTask, typeof listTasks].join()"
295
+ const withBash = yield* runAgent({
296
+ mode,
297
+ executor: { excludeTools: ["subagent"] },
298
+ script: `await taskComplete(${tasks})`,
299
+ })
300
+ assert.strictEqual(
301
+ withBash.summary,
302
+ "function,function,function,function",
303
+ )
304
+ const withRun = yield* runAgent({
305
+ mode,
306
+ executor: { excludeTools: ["bash", "subagent"] },
307
+ script: `await taskComplete(${tasks})`,
308
+ })
309
+ assert.strictEqual(
310
+ withRun.summary,
311
+ "function,function,function,function",
312
+ )
313
+ const withNeither = yield* runAgent({
314
+ mode,
315
+ executor: { excludeTools: ["bash", "run", "subagent"] },
316
+ script: `await taskComplete(${tasks})`,
317
+ })
318
+ assert.strictEqual(
319
+ withNeither.summary,
320
+ "undefined,undefined,undefined,undefined",
321
+ )
322
+ assert.notMatch(
323
+ withNeither.system,
324
+ /\b(taskOutput|waitTasks|stopTask|listTasks)\b/,
325
+ )
326
+ assert.notInclude(withNeither.system, "<task")
327
+ }),
317
328
  )
318
329
 
319
330
  it.live("a caller tool replaces a left-out built-in of the same name", () =>
@@ -558,6 +558,44 @@ await sleep(60000)`,
558
558
  })
559
559
 
560
560
  describe(`${mode} executor limits`, () => {
561
+ it.live("run accepts only a string and returns command output", () =>
562
+ Effect.gen(function* () {
563
+ const output = yield* runScript(
564
+ 'console.log("OUTPUT:", JSON.stringify(await run("printf hello"))); try { await run({ command: "echo invalid" }) } catch { console.log("INVALID") }',
565
+ )
566
+ assert.include(output, 'OUTPUT: "hello"')
567
+ assert.include(output, "INVALID")
568
+ }).pipe(Effect.provide(executorLayer({}))),
569
+ )
570
+
571
+ it.live(
572
+ "run hands timed-out commands to the background with a string notice",
573
+ () =>
574
+ Effect.gen(function* () {
575
+ const executor = yield* AgentExecutor.AgentExecutor
576
+ const { environment } = yield* executor.capabilities
577
+ const output = yield* runScript(
578
+ `console.log("OUTPUT:", JSON.stringify(await run(${JSON.stringify(gatedCommand(environment.cwd))})))`,
579
+ )
580
+ assert.strictEqual(
581
+ JSON.parse(output.split("OUTPUT:")[1]!),
582
+ 'Task "1" is still running in the background',
583
+ )
584
+ assert.strictEqual(
585
+ yield* waitForLog(environment.cwd, started),
586
+ "start\n",
587
+ )
588
+ openGate(environment.cwd)
589
+ assert.strictEqual(
590
+ yield* waitForLog(environment.cwd, finished),
591
+ "start\nfinish\n",
592
+ )
593
+ }).pipe(
594
+ Effect.timeout("5 seconds"),
595
+ Effect.provide(executorLayer({ commandTimeout: "300 millis" })),
596
+ ),
597
+ )
598
+
561
599
  it.live("runs a bash command in the background", () =>
562
600
  Effect.gen(function* () {
563
601
  const executor = yield* AgentExecutor.AgentExecutor
@@ -1390,7 +1390,7 @@ const excludedToolNames = (
1390
1390
  const names = new Set<string>(excludeTools)
1391
1391
  if (names.has("subagent")) {
1392
1392
  names.add("messageAgent")
1393
- if (names.has("bash")) {
1393
+ if (names.has("bash") && names.has("run")) {
1394
1394
  for (const name of ["taskOutput", "waitTasks", "stopTask", "listTasks"]) {
1395
1395
  names.add(name)
1396
1396
  }
package/src/AgentTools.ts CHANGED
@@ -370,6 +370,13 @@ export const AgentTools = Toolkit.make(
370
370
  success: Schema.NullOr(Schema.String),
371
371
  dependencies: [CurrentDirectory, ImageAttacher],
372
372
  }),
373
+ Tool.make("run", {
374
+ description:
375
+ "Run a shell command and return its output. If it takes too long it will become a background task. Use 'bash' for more control.",
376
+ parameters: Schema.String.annotate({ identifier: "command" }),
377
+ success: Schema.String,
378
+ dependencies: [CurrentDirectory],
379
+ }),
373
380
  Tool.make("rg", {
374
381
  description: "Search for a pattern in files using ripgrep",
375
382
  parameters: Schema.Struct({
@@ -399,23 +406,6 @@ export const AgentTools = Toolkit.make(
399
406
  failure: Schema.String,
400
407
  dependencies: [AgentTaskExecutor],
401
408
  }),
402
- Tool.make("messageAgent", {
403
- description: "Send a message to one of your sub-agents.",
404
- parameters: Schema.Struct({
405
- id: Schema.String,
406
- message: Schema.String,
407
- }),
408
- failure: Schema.String,
409
- dependencies: [AgentTaskExecutor],
410
- }),
411
- Tool.make("glob", {
412
- description: "Find files matching a glob pattern.",
413
- parameters: Schema.String.annotate({
414
- identifier: "pattern",
415
- }),
416
- success: Schema.Array(Schema.String),
417
- dependencies: [CurrentDirectory],
418
- }),
419
409
  Tool.make("gh", {
420
410
  description:
421
411
  "Use the GitHub CLI to run a command - use this instead of the bash tool.",
@@ -426,44 +416,6 @@ export const AgentTools = Toolkit.make(
426
416
  dependencies: [CurrentDirectory],
427
417
  }),
428
418
  makeBashTool(defaultBashTimeout),
429
- Tool.make("taskOutput", {
430
- description:
431
- "Return the status and output so far of one of your tasks: a background bash command or a sub-agent.",
432
- parameters: Schema.String.annotate({
433
- identifier: "id",
434
- }),
435
- success: Task,
436
- failure: Schema.String,
437
- dependencies: [AgentTaskExecutor],
438
- }),
439
- Tool.make("waitTasks", {
440
- description:
441
- "Wait until the given tasks have ended and return them, in the order given.",
442
- parameters: Schema.Struct({
443
- ids: Schema.Array(Schema.String),
444
- timeoutMs: Schema.optional(Schema.Finite).annotate({
445
- documentation:
446
- "How long to wait in ms (default and max: the bash command timeout)",
447
- }),
448
- }),
449
- success: Schema.Array(Task),
450
- failure: Schema.String,
451
- dependencies: [AgentTaskExecutor],
452
- }),
453
- Tool.make("stopTask", {
454
- description: "Stop one of your tasks.",
455
- parameters: Schema.String.annotate({
456
- identifier: "id",
457
- }),
458
- failure: Schema.String,
459
- dependencies: [AgentTaskExecutor],
460
- }),
461
- Tool.make("listTasks", {
462
- description: "List the tasks you have started, with their status.",
463
- parameters: Schema.Void,
464
- success: Schema.Array(Task),
465
- dependencies: [AgentTaskExecutor],
466
- }),
467
419
  Tool.make("applyPatch", {
468
420
  description:
469
421
  "Add, update or remove multiple files with a git diff / unified diff / wrapped patch.",
@@ -527,6 +479,53 @@ export const AgentTools = Toolkit.make(
527
479
  identifier: "ms",
528
480
  }),
529
481
  }),
482
+ Tool.make("taskOutput", {
483
+ description:
484
+ "Return the status and output so far of one of your tasks: a background bash command or a sub-agent.",
485
+ parameters: Schema.String.annotate({
486
+ identifier: "id",
487
+ }),
488
+ success: Task,
489
+ failure: Schema.String,
490
+ dependencies: [AgentTaskExecutor],
491
+ }),
492
+ Tool.make("waitTasks", {
493
+ description:
494
+ "Wait until the given tasks have ended and return them, in the order given.",
495
+ parameters: Schema.Struct({
496
+ ids: Schema.Array(Schema.String),
497
+ timeoutMs: Schema.optional(Schema.Finite).annotate({
498
+ documentation:
499
+ "How long to wait in ms (default and max: the bash command timeout)",
500
+ }),
501
+ }),
502
+ success: Schema.Array(Task),
503
+ failure: Schema.String,
504
+ dependencies: [AgentTaskExecutor],
505
+ }),
506
+ Tool.make("stopTask", {
507
+ description: "Stop one of your tasks.",
508
+ parameters: Schema.String.annotate({
509
+ identifier: "id",
510
+ }),
511
+ failure: Schema.String,
512
+ dependencies: [AgentTaskExecutor],
513
+ }),
514
+ Tool.make("listTasks", {
515
+ description: "List the tasks you have started, with their status.",
516
+ parameters: Schema.Void,
517
+ success: Schema.Array(Task),
518
+ dependencies: [AgentTaskExecutor],
519
+ }),
520
+ Tool.make("messageAgent", {
521
+ description: "Send a message to one of your sub-agents.",
522
+ parameters: Schema.Struct({
523
+ id: Schema.String,
524
+ message: Schema.String,
525
+ }),
526
+ failure: Schema.String,
527
+ dependencies: [AgentTaskExecutor],
528
+ }),
530
529
  Tool.make("taskComplete", {
531
530
  description:
532
531
  "Only call this when the task is fully complete and you have a final output message to send.",
@@ -721,6 +720,53 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
721
720
  return `Image attached: ${fileName}`
722
721
  })
723
722
 
723
+ const bash = Effect.fn("AgentTools.bash")(function* (options: {
724
+ readonly command: string
725
+ readonly timeoutMs?: number | undefined
726
+ readonly background?: boolean | undefined
727
+ }) {
728
+ const maxMs = bashTimeoutMs(yield* CommandLimits)
729
+ const timeoutMs = Math.min(options.timeoutMs ?? maxMs, maxMs)
730
+ yield* Effect.logInfo(`Calling "bash"`).pipe(
731
+ Effect.annotateLogs({
732
+ ...options,
733
+ timeoutMs,
734
+ }),
735
+ )
736
+ const cwd = yield* CurrentDirectory
737
+ const cmd = ChildProcess.make("bash", ["-c", options.command], {
738
+ cwd,
739
+ stdin: "ignore",
740
+ })
741
+ const entry = yield* addBashTask(
742
+ options.command,
743
+ yield* startCommand(cmd, true),
744
+ )
745
+ if (options.background !== true) {
746
+ const exit = yield* awaitCommand(
747
+ entry.command,
748
+ Duration.millis(timeoutMs),
749
+ )
750
+ if (Option.isSome(exit)) {
751
+ // A finished command is reported here, so it is never pushed.
752
+ const exitCode = yield* exit.value
753
+ const task = reportBash(entry)
754
+ if (exitCode === 0) return task
755
+ return yield* Effect.die(
756
+ new Error(
757
+ `Command failed with exit code ${exitCode}: ${task.output}`,
758
+ ),
759
+ )
760
+ }
761
+ }
762
+ // Snapshot the task before yielding to the watcher: the command can
763
+ // finish while it is being handed over, and its final state must
764
+ // reach the model only once. A finished task is never handed over.
765
+ const task = reportBash(entry)
766
+ if (task.status === "running") yield* watchBashTask(entry)
767
+ return options.background === true ? task : backgroundRunningOutput(task)
768
+ }, Effect.orDie)
769
+
724
770
  return AgentToolsWithSearch.of({
725
771
  changeDirectory: Effect.fn("AgentTools.changeDirectory")(
726
772
  function* (directory) {
@@ -882,59 +928,11 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
882
928
  }
883
929
  return yield* run(args.slice(0, -1).concat("-uu", options.pattern))
884
930
  }),
885
- glob: Effect.fn("AgentTools.glob")(function* (pattern) {
886
- yield* Effect.logInfo(`Calling "glob"`).pipe(
887
- Effect.annotateLogs({ pattern }),
888
- )
889
- const cwd = yield* CurrentDirectory
890
- return yield* Effect.promise(() =>
891
- import("glob").then((Glob) => Glob.glob(pattern, { cwd })),
892
- )
931
+ bash,
932
+ run: Effect.fn("AgentTools.run")(function* (command) {
933
+ const task = yield* bash({ command, timeoutMs: 120_000 })
934
+ return task.output
893
935
  }),
894
- bash: Effect.fn("AgentTools.bash")(function* (options) {
895
- const maxMs = bashTimeoutMs(yield* CommandLimits)
896
- const timeoutMs = Math.min(options.timeoutMs ?? maxMs, maxMs)
897
- yield* Effect.logInfo(`Calling "bash"`).pipe(
898
- Effect.annotateLogs({
899
- ...options,
900
- timeoutMs,
901
- }),
902
- )
903
- const cwd = yield* CurrentDirectory
904
- const cmd = ChildProcess.make("bash", ["-c", options.command], {
905
- cwd,
906
- stdin: "ignore",
907
- })
908
- const entry = yield* addBashTask(
909
- options.command,
910
- yield* startCommand(cmd, true),
911
- )
912
- if (options.background !== true) {
913
- const exit = yield* awaitCommand(
914
- entry.command,
915
- Duration.millis(timeoutMs),
916
- )
917
- if (Option.isSome(exit)) {
918
- // A finished command is reported here, so it is never pushed.
919
- const exitCode = yield* exit.value
920
- const task = reportBash(entry)
921
- if (exitCode === 0) return task
922
- return yield* Effect.die(
923
- new Error(
924
- `Command failed with exit code ${exitCode}: ${task.output}`,
925
- ),
926
- )
927
- }
928
- }
929
- // Snapshot the task before yielding to the watcher: the command can
930
- // finish while it is being handed over, and its final state must
931
- // reach the model only once. A finished task is never handed over.
932
- const task = reportBash(entry)
933
- if (task.status === "running") yield* watchBashTask(entry)
934
- return options.background === true
935
- ? task
936
- : backgroundRunningOutput(task)
937
- }, Effect.orDie),
938
936
  taskOutput: Effect.fn("AgentTools.taskOutput")(function* (
939
937
  input: unknown,
940
938
  ) {