clanka 0.20.0 → 0.21.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.
- package/dist/Acp.test.js +1 -1
- package/dist/Acp.test.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +1 -1
- package/dist/Agent.js.map +1 -1
- package/dist/AgentBashJobs.test.js +6 -2
- package/dist/AgentBashJobs.test.js.map +1 -1
- package/dist/AgentExecutor.js +1 -0
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +87 -11
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentSubagents.test.js +2 -2
- package/dist/AgentSubagents.test.js.map +1 -1
- package/dist/AgentTools.d.ts +92 -8
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +81 -43
- package/dist/AgentTools.js.map +1 -1
- package/dist/bin/cli.mjs +67 -37
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +1 -1
- package/src/Agent.ts +3 -0
- package/src/AgentBashJobs.test.ts +6 -2
- package/src/AgentExecutor.test.ts +127 -11
- package/src/AgentExecutor.ts +1 -0
- package/src/AgentSubagents.test.ts +2 -2
- package/src/AgentTools.ts +102 -53
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -1039,7 +1039,7 @@ await changeDirectory("sub")`),
|
|
|
1039
1039
|
Effect.gen(function* () {
|
|
1040
1040
|
const server = yield* makeServer(
|
|
1041
1041
|
runsScript(
|
|
1042
|
-
`console.log("PWD:" + (await bash({ command: "pwd" })).trim())`,
|
|
1042
|
+
`console.log("PWD:" + (await bash({ command: "pwd" })).output.trim())`,
|
|
1043
1043
|
),
|
|
1044
1044
|
yield* localExecutor(cwd),
|
|
1045
1045
|
)
|
package/src/Agent.ts
CHANGED
|
@@ -1516,6 +1516,9 @@ const generateSystemTools = (
|
|
|
1516
1516
|
`
|
|
1517
1517
|
- Avoid passing scripts into the "bash" function, and instead write javascript.
|
|
1518
1518
|
- Pass \`background: true\` to "bash" for commands that keep running, such as dev servers or watchers. A command still running at its timeout is moved to the background instead of being stopped. Use "jobOutput" and "stopJob" with the returned job id.${when(
|
|
1519
|
+
tools.offers("waitJob"),
|
|
1520
|
+
` "bash" returns a task, and rejects when a command that has not been moved to the background fails. Use "waitJob" to wait for a running task; it returns the task as "running" if its timeout passes first. A background task that fails is returned with status "failed" and its \`exitCode\`.`,
|
|
1521
|
+
)}${when(
|
|
1519
1522
|
tools.jobPushes,
|
|
1520
1523
|
` When a job ends, its status and output are sent to you as a \`<job id="..." status="..." exitCode="...">\` message, unless "jobOutput" or "stopJob" already gave you its final status. You do not need to poll jobs with "jobOutput" to find out when they end.`,
|
|
1521
1524
|
)}`,
|
|
@@ -197,7 +197,7 @@ await sleep(500)`,
|
|
|
197
197
|
)
|
|
198
198
|
|
|
199
199
|
it.live(
|
|
200
|
-
"a job
|
|
200
|
+
"a job whose final state stopJob, jobOutput, waitJob or bash returned is not pushed",
|
|
201
201
|
() =>
|
|
202
202
|
Effect.gen(function* () {
|
|
203
203
|
const { exit, prompts } = yield* runAgent((index) => {
|
|
@@ -215,7 +215,10 @@ while (job.status === "running") {
|
|
|
215
215
|
await sleep(20)
|
|
216
216
|
job = await jobOutput("2")
|
|
217
217
|
}
|
|
218
|
-
await
|
|
218
|
+
await bash({ command: "echo FOREGROUND" })
|
|
219
|
+
await waitJob({ id: (await bash({ command: "echo WAITED", background: true })).id })
|
|
220
|
+
await sleep(500)
|
|
221
|
+
console.log("SCRIPT" + "-DONE")`,
|
|
219
222
|
)
|
|
220
223
|
default:
|
|
221
224
|
return text("done")
|
|
@@ -230,6 +233,7 @@ await sleep(500)`,
|
|
|
230
233
|
pushes[0]!,
|
|
231
234
|
/^<job id="3" status="completed" exitCode="0">\s*UNREAD\s*<\/job>$/,
|
|
232
235
|
)
|
|
236
|
+
assert.include(JSON.stringify(prompts[1]), "SCRIPT-DONE")
|
|
233
237
|
}).pipe(Effect.provide(executorLayer)),
|
|
234
238
|
)
|
|
235
239
|
})
|
|
@@ -566,9 +566,11 @@ const result = await bash({
|
|
|
566
566
|
command: ${JSON.stringify(gatedCommand(environment.cwd))},
|
|
567
567
|
background: true,
|
|
568
568
|
})
|
|
569
|
-
console.log("RETURNED:", result)`)
|
|
570
|
-
assert.include(
|
|
571
|
-
|
|
569
|
+
console.log("RETURNED:", JSON.stringify(result))`)
|
|
570
|
+
assert.include(
|
|
571
|
+
output,
|
|
572
|
+
'RETURNED: {"id":"1","status":"running","output":""}',
|
|
573
|
+
)
|
|
572
574
|
assert.strictEqual(
|
|
573
575
|
yield* waitForLog(environment.cwd, started),
|
|
574
576
|
"start\n",
|
|
@@ -589,16 +591,18 @@ console.log("RETURNED:", result)`)
|
|
|
589
591
|
const output = yield* runScript(`
|
|
590
592
|
try {
|
|
591
593
|
const result = await bash({
|
|
592
|
-
command: ${JSON.stringify(gatedCommand(environment.cwd))},
|
|
594
|
+
command: ${JSON.stringify(`echo early && ${gatedCommand(environment.cwd)}`)},
|
|
593
595
|
timeoutMs: 60000,
|
|
594
596
|
})
|
|
595
|
-
console.log("RETURNED:", result)
|
|
597
|
+
console.log("RETURNED:", JSON.stringify(result))
|
|
596
598
|
} catch (error) {
|
|
597
599
|
console.log("REJECTED:", String(error))
|
|
598
600
|
}`)
|
|
599
|
-
assert.include(
|
|
601
|
+
assert.include(
|
|
602
|
+
output,
|
|
603
|
+
'RETURNED: {"id":"1","status":"running","output":"early\\n"}',
|
|
604
|
+
)
|
|
600
605
|
assert.notInclude(output, "REJECTED:")
|
|
601
|
-
assert.match(output, /background/i)
|
|
602
606
|
assert.strictEqual(
|
|
603
607
|
yield* waitForLog(environment.cwd, started),
|
|
604
608
|
"start\n",
|
|
@@ -615,6 +619,118 @@ try {
|
|
|
615
619
|
),
|
|
616
620
|
)
|
|
617
621
|
|
|
622
|
+
// A foreground command that succeeds returns a task with an id from the
|
|
623
|
+
// same counter as background jobs; one that fails still rejects.
|
|
624
|
+
it.live(
|
|
625
|
+
"returns a task from a foreground bash command, or rejects on failure",
|
|
626
|
+
() =>
|
|
627
|
+
Effect.gen(function* () {
|
|
628
|
+
const output = yield* runScript(`
|
|
629
|
+
try {
|
|
630
|
+
console.log("OK:", JSON.stringify(await bash({ command: "echo hi" })))
|
|
631
|
+
console.log("RETURNED:", JSON.stringify(await bash({ command: "echo out && exit 3" })))
|
|
632
|
+
} catch (error) {
|
|
633
|
+
console.log("REJECTED:", String(error))
|
|
634
|
+
}`)
|
|
635
|
+
assert.include(
|
|
636
|
+
output,
|
|
637
|
+
'OK: {"id":"1","status":"completed","exitCode":0,"output":"hi\\n"}',
|
|
638
|
+
)
|
|
639
|
+
assert.match(
|
|
640
|
+
output,
|
|
641
|
+
/REJECTED:.*Command failed with exit code 3: out/,
|
|
642
|
+
)
|
|
643
|
+
assert.notInclude(output, "RETURNED:")
|
|
644
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
645
|
+
)
|
|
646
|
+
|
|
647
|
+
it.live("waits for a background task with waitJob", () =>
|
|
648
|
+
Effect.gen(function* () {
|
|
649
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
650
|
+
const { environment } = yield* executor.capabilities
|
|
651
|
+
const output = yield* runScript(`
|
|
652
|
+
const task = await bash({
|
|
653
|
+
command: ${JSON.stringify(`${gatedCommand(environment.cwd)} && echo done`)},
|
|
654
|
+
background: true,
|
|
655
|
+
})
|
|
656
|
+
await writeFile({ path: "gate", content: "" })
|
|
657
|
+
console.log("WAITED:", JSON.stringify(await waitJob({ id: task.id })))`)
|
|
658
|
+
assert.include(
|
|
659
|
+
output,
|
|
660
|
+
'WAITED: {"id":"1","status":"completed","exitCode":0,"output":"done\\n"}',
|
|
661
|
+
)
|
|
662
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
663
|
+
)
|
|
664
|
+
|
|
665
|
+
// Without `timeoutMs` the wait uses the caller's command timeout, which
|
|
666
|
+
// also caps it. The test timeout catches a wait that never ends.
|
|
667
|
+
it.live("stops waiting at the timeout and leaves the task running", () =>
|
|
668
|
+
Effect.gen(function* () {
|
|
669
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
670
|
+
const { environment } = yield* executor.capabilities
|
|
671
|
+
const output = yield* runScript(`
|
|
672
|
+
const task = await bash({
|
|
673
|
+
command: ${JSON.stringify(gatedCommand(environment.cwd))},
|
|
674
|
+
background: true,
|
|
675
|
+
})
|
|
676
|
+
const waits = [
|
|
677
|
+
await waitJob({ id: task.id, timeoutMs: 100 }),
|
|
678
|
+
await waitJob({ id: task.id }),
|
|
679
|
+
await waitJob({ id: task.id, timeoutMs: 60000 }),
|
|
680
|
+
]
|
|
681
|
+
console.log("WAITS:", JSON.stringify(waits.map((job) => job.status)))`)
|
|
682
|
+
assert.include(output, 'WAITS: ["running","running","running"]')
|
|
683
|
+
assert.strictEqual(
|
|
684
|
+
yield* waitForLog(environment.cwd, started),
|
|
685
|
+
"start\n",
|
|
686
|
+
"still running after the waits",
|
|
687
|
+
)
|
|
688
|
+
openGate(environment.cwd)
|
|
689
|
+
assert.strictEqual(
|
|
690
|
+
yield* waitForLog(environment.cwd, finished),
|
|
691
|
+
"start\nfinish\n",
|
|
692
|
+
)
|
|
693
|
+
}).pipe(
|
|
694
|
+
Effect.timeout("5 seconds"),
|
|
695
|
+
Effect.provide(executorLayer({ commandTimeout: "300 millis" })),
|
|
696
|
+
),
|
|
697
|
+
)
|
|
698
|
+
|
|
699
|
+
// The watcher returns only after the job has ended, so the job always
|
|
700
|
+
// finishes before `bash` returns. `bash` must return the snapshot it
|
|
701
|
+
// handed over, leaving the final state to the watcher alone. Over RPC the
|
|
702
|
+
// server's watcher does not wait for the client's, so only local mode
|
|
703
|
+
// forces the ordering.
|
|
704
|
+
it.live("returns the task it hands to the job watcher", () =>
|
|
705
|
+
Effect.gen(function* () {
|
|
706
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
707
|
+
const { environment } = yield* executor.capabilities
|
|
708
|
+
const watched: Array<string> = []
|
|
709
|
+
const claims: Array<boolean> = []
|
|
710
|
+
const output = yield* executor
|
|
711
|
+
.execute({
|
|
712
|
+
environment,
|
|
713
|
+
onEnvironment: () => Effect.void,
|
|
714
|
+
script: `
|
|
715
|
+
await bash({ command: "true" })
|
|
716
|
+
console.log("RETURNED:", JSON.stringify(await bash({ command: "true", background: true })))`,
|
|
717
|
+
onTaskComplete: () => Effect.void,
|
|
718
|
+
subagents: subagentsUnavailable,
|
|
719
|
+
onImage: () => Effect.void,
|
|
720
|
+
onJob: (job) =>
|
|
721
|
+
Effect.gen(function* () {
|
|
722
|
+
watched.push(job.id)
|
|
723
|
+
yield* job.await
|
|
724
|
+
claims.push(yield* job.claim)
|
|
725
|
+
}),
|
|
726
|
+
})
|
|
727
|
+
.pipe(Stream.mkString)
|
|
728
|
+
assert.include(output, 'RETURNED: {"id":"2","status":"running"')
|
|
729
|
+
assert.deepStrictEqual(watched, ["2"], "only the running task")
|
|
730
|
+
assert.deepStrictEqual(claims, [true], "reported once, by the watcher")
|
|
731
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
732
|
+
)
|
|
733
|
+
|
|
618
734
|
it.live("reports background jobs through jobOutput", () =>
|
|
619
735
|
Effect.gen(function* () {
|
|
620
736
|
const executor = yield* AgentExecutor.AgentExecutor
|
|
@@ -859,10 +975,10 @@ sleep(100).then(() => console.constructor.constructor("return process")().exit(7
|
|
|
859
975
|
tool: "bash",
|
|
860
976
|
params: { command: "seq 1 1000000" },
|
|
861
977
|
})
|
|
862
|
-
|
|
863
|
-
assert.isBelow(
|
|
864
|
-
assert.isTrue(
|
|
865
|
-
assert.isTrue(
|
|
978
|
+
const { output: text } = output as { readonly output: string }
|
|
979
|
+
assert.isBelow(text.length, 10_000 + 500)
|
|
980
|
+
assert.isTrue(text.startsWith("1\n2\n3\n"), "start kept")
|
|
981
|
+
assert.isTrue(text.trimEnd().endsWith("999999\n1000000"), "end kept")
|
|
866
982
|
}).pipe(
|
|
867
983
|
Effect.timeout("10 seconds"),
|
|
868
984
|
Effect.provide(executorLayer({ commandOutputChars: 10_000 })),
|
package/src/AgentExecutor.ts
CHANGED
|
@@ -798,7 +798,7 @@ console.log(JSON.stringify(result))`,
|
|
|
798
798
|
toolCall(
|
|
799
799
|
`pwd-${name}`,
|
|
800
800
|
`const pwd = await bash({ command: "pwd" })
|
|
801
|
-
await taskComplete("${name.toUpperCase()}:" + pwd.trim())`,
|
|
801
|
+
await taskComplete("${name.toUpperCase()}:" + pwd.output.trim())`,
|
|
802
802
|
),
|
|
803
803
|
),
|
|
804
804
|
),
|
|
@@ -814,7 +814,7 @@ await changeDirectory("a")
|
|
|
814
814
|
await delegate({ prompt: "TASK-CHILD-ONE", background: true })
|
|
815
815
|
await delegate({ prompt: "TASK-CHILD-TWO", background: true })
|
|
816
816
|
console.log(JSON.stringify(await waitAgents()))
|
|
817
|
-
console.log("ROOT:" + (await bash({ command: "pwd" })).trim())`,
|
|
817
|
+
console.log("ROOT:" + (await bash({ command: "pwd" })).output.trim())`,
|
|
818
818
|
),
|
|
819
819
|
)
|
|
820
820
|
case 1:
|
package/src/AgentTools.ts
CHANGED
|
@@ -221,8 +221,10 @@ export const BashJobStatus = Schema.Literals([
|
|
|
221
221
|
export type BashJobStatus = typeof BashJobStatus.Type
|
|
222
222
|
|
|
223
223
|
/**
|
|
224
|
-
* A `bash` command
|
|
225
|
-
* and `stopJob
|
|
224
|
+
* A `bash` command and its state: the task that `bash`, `jobOutput`,
|
|
225
|
+
* `waitJob` and `stopJob` return. `exitCode` is set once the command has
|
|
226
|
+
* exited; a non-zero exit is `failed`. A foreground `bash` command that
|
|
227
|
+
* fails rejects instead of returning a task.
|
|
226
228
|
*
|
|
227
229
|
* @since 1.0.0
|
|
228
230
|
* @category Models
|
|
@@ -260,7 +262,8 @@ export interface BackgroundJob {
|
|
|
260
262
|
|
|
261
263
|
/**
|
|
262
264
|
* Receives every background `bash` job as it starts: those started with
|
|
263
|
-
* `background: true` and those moved to the background at their timeout.
|
|
265
|
+
* `background: true` and those moved to the background at their timeout. A
|
|
266
|
+
* command that `bash` returns finished is never handed over.
|
|
264
267
|
* The executor sets it from `ExecuteOptions.onJob`.
|
|
265
268
|
*
|
|
266
269
|
* @since 1.0.0
|
|
@@ -319,7 +322,7 @@ export class CommandLimits extends Context.Reference<{
|
|
|
319
322
|
export const makeBashTool = (timeout: Duration.Duration) =>
|
|
320
323
|
Tool.make("bash", {
|
|
321
324
|
description:
|
|
322
|
-
'Run a bash command and return
|
|
325
|
+
'Run a bash command and return a task with its job id, status, exit code and output. A command that fails before it is returned rejects with its exit code and output. With `background: true` it returns straight away with status "running" while the command keeps going. A command still running at its timeout is not stopped: it is moved to the background and returned as "running" with the output so far. Wait for a job with "waitJob", read it with "jobOutput" and stop it with "stopJob"; a background job that fails is reported with status "failed" and its exitCode.',
|
|
323
326
|
parameters: Schema.Struct({
|
|
324
327
|
command: Schema.String,
|
|
325
328
|
timeoutMs: Schema.optional(Schema.Finite).annotate({
|
|
@@ -327,12 +330,12 @@ export const makeBashTool = (timeout: Duration.Duration) =>
|
|
|
327
330
|
}),
|
|
328
331
|
background: Schema.optional(Schema.Boolean).annotate({
|
|
329
332
|
documentation:
|
|
330
|
-
|
|
333
|
+
'Start the command in the background and return it as "running" without waiting',
|
|
331
334
|
}),
|
|
332
335
|
}).annotate({
|
|
333
336
|
identifier: "command",
|
|
334
337
|
}),
|
|
335
|
-
success:
|
|
338
|
+
success: BashJob,
|
|
336
339
|
dependencies: [CurrentDirectory],
|
|
337
340
|
})
|
|
338
341
|
|
|
@@ -475,6 +478,19 @@ export const AgentTools = Toolkit.make(
|
|
|
475
478
|
success: BashJob,
|
|
476
479
|
failure: Schema.String,
|
|
477
480
|
}),
|
|
481
|
+
Tool.make("waitJob", {
|
|
482
|
+
description:
|
|
483
|
+
'Wait for a bash job to end and return its task. When the timeout passes first, the task is returned as "running" and the command keeps going.',
|
|
484
|
+
parameters: Schema.Struct({
|
|
485
|
+
id: Schema.String,
|
|
486
|
+
timeoutMs: Schema.optional(Schema.Finite).annotate({
|
|
487
|
+
documentation:
|
|
488
|
+
"How long to wait in ms (default and max: the bash timeout)",
|
|
489
|
+
}),
|
|
490
|
+
}),
|
|
491
|
+
success: BashJob,
|
|
492
|
+
failure: Schema.String,
|
|
493
|
+
}),
|
|
478
494
|
Tool.make("stopJob", {
|
|
479
495
|
description:
|
|
480
496
|
"Stop a background bash job and return its final status and output.",
|
|
@@ -623,14 +639,20 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
623
639
|
return { fiber, output: output.render }
|
|
624
640
|
})
|
|
625
641
|
|
|
642
|
+
// Every `bash` command is a job, so it can be addressed by id. Only
|
|
643
|
+
// the ones still running when `bash` returns go to the `JobWatcher`.
|
|
626
644
|
const jobs = new Map<string, RunningCommand>()
|
|
627
645
|
let nextJobId = 1
|
|
628
|
-
const addJob =
|
|
629
|
-
command: Omit<RunningCommand, "reported">,
|
|
630
|
-
) {
|
|
646
|
+
const addJob = (command: Omit<RunningCommand, "reported">) => {
|
|
631
647
|
const id = String(nextJobId++)
|
|
632
648
|
const running: RunningCommand = { ...command, reported: false }
|
|
633
649
|
jobs.set(id, running)
|
|
650
|
+
return { id, running }
|
|
651
|
+
}
|
|
652
|
+
const watchJob = Effect.fnUntraced(function* (
|
|
653
|
+
id: string,
|
|
654
|
+
running: RunningCommand,
|
|
655
|
+
) {
|
|
634
656
|
const watch = yield* JobWatcher
|
|
635
657
|
yield* watch({
|
|
636
658
|
id,
|
|
@@ -643,7 +665,6 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
643
665
|
return true
|
|
644
666
|
}),
|
|
645
667
|
})
|
|
646
|
-
return id
|
|
647
668
|
})
|
|
648
669
|
const findJob = (id: string) => {
|
|
649
670
|
const running = jobs.get(id)
|
|
@@ -651,41 +672,25 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
651
672
|
? Effect.fail(`No background job with id "${id}"`)
|
|
652
673
|
: Effect.succeed(running)
|
|
653
674
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
675
|
+
// The final state of a job the model has been given is not pushed.
|
|
676
|
+
const reportJob = (id: string, running: RunningCommand) => {
|
|
677
|
+
const job = jobInfo(id, running)
|
|
678
|
+
if (job.status !== "running") running.reported = true
|
|
679
|
+
return job
|
|
680
|
+
}
|
|
681
|
+
// Waits up to `timeout` for a command to exit. A caller that is
|
|
682
|
+
// interrupted first stops the command.
|
|
683
|
+
const awaitCommand = (
|
|
684
|
+
running: RunningCommand | Omit<RunningCommand, "reported">,
|
|
660
685
|
timeout: Duration.Duration | undefined,
|
|
661
|
-
|
|
662
|
-
) {
|
|
663
|
-
const running = yield* startCommand(command)
|
|
686
|
+
) => {
|
|
664
687
|
const awaited = Fiber.await(running.fiber)
|
|
665
|
-
|
|
688
|
+
return (
|
|
666
689
|
timeout === undefined
|
|
667
690
|
? Effect.asSome(awaited)
|
|
668
691
|
: Effect.timeoutOption(awaited, timeout)
|
|
669
692
|
).pipe(Effect.onInterrupt(() => Fiber.interrupt(running.fiber)))
|
|
670
|
-
|
|
671
|
-
const timeoutMs = Duration.toMillis(timeout!)
|
|
672
|
-
if (onTimeout === "stop") {
|
|
673
|
-
yield* Fiber.interrupt(running.fiber)
|
|
674
|
-
return yield* Effect.die(
|
|
675
|
-
new Error(`Command timed out after ${timeoutMs}ms`),
|
|
676
|
-
)
|
|
677
|
-
}
|
|
678
|
-
const id = yield* addJob(running)
|
|
679
|
-
const output = running.output()
|
|
680
|
-
return `${output}${output === "" || output.endsWith("\n") ? "" : "\n"}[Still running after ${timeoutMs}ms, so it was moved to the background as job ${id}. ${jobUsage(id)}]`
|
|
681
|
-
}
|
|
682
|
-
const exitCode = yield* exit.value
|
|
683
|
-
const output = running.output()
|
|
684
|
-
if (exitCode === 0) return output
|
|
685
|
-
return yield* Effect.die(
|
|
686
|
-
new Error(`Command failed with exit code ${exitCode}: ${output}`),
|
|
687
|
-
)
|
|
688
|
-
})
|
|
693
|
+
}
|
|
689
694
|
|
|
690
695
|
/**
|
|
691
696
|
* Image branch of `readFile`: the bytes go to the `ImageAttacher` for
|
|
@@ -889,8 +894,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
889
894
|
)
|
|
890
895
|
}),
|
|
891
896
|
bash: Effect.fn("AgentTools.bash")(function* (options) {
|
|
892
|
-
const
|
|
893
|
-
const maxMs = Duration.toMillis(limits.timeout ?? defaultBashTimeout)
|
|
897
|
+
const maxMs = bashTimeoutMs(yield* CommandLimits)
|
|
894
898
|
const timeoutMs = Math.min(options.timeoutMs ?? maxMs, maxMs)
|
|
895
899
|
yield* Effect.logInfo(`Calling "bash"`).pipe(
|
|
896
900
|
Effect.annotateLogs({
|
|
@@ -903,21 +907,46 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
903
907
|
cwd,
|
|
904
908
|
stdin: "ignore",
|
|
905
909
|
})
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
910
|
+
const { id, running } = addJob(yield* startCommand(cmd))
|
|
911
|
+
if (options.background !== true) {
|
|
912
|
+
const exit = yield* awaitCommand(running, Duration.millis(timeoutMs))
|
|
913
|
+
if (Option.isSome(exit)) {
|
|
914
|
+
// A finished command is reported here, so it is never pushed.
|
|
915
|
+
const exitCode = yield* exit.value
|
|
916
|
+
const job = reportJob(id, running)
|
|
917
|
+
if (exitCode === 0) return job
|
|
918
|
+
return yield* Effect.die(
|
|
919
|
+
new Error(
|
|
920
|
+
`Command failed with exit code ${exitCode}: ${job.output}`,
|
|
921
|
+
),
|
|
922
|
+
)
|
|
923
|
+
}
|
|
909
924
|
}
|
|
910
|
-
|
|
925
|
+
// Snapshot the task before yielding to the watcher: the command can
|
|
926
|
+
// finish while it is being handed over, and its final state must
|
|
927
|
+
// reach the model only once. A finished task is never handed over.
|
|
928
|
+
const job = reportJob(id, running)
|
|
929
|
+
if (job.status === "running") yield* watchJob(id, running)
|
|
930
|
+
return job
|
|
911
931
|
}, Effect.orDie),
|
|
912
932
|
jobOutput: Effect.fn("AgentTools.jobOutput")(function* (input: unknown) {
|
|
913
933
|
const id = yield* decodeJobOutput(input)
|
|
914
934
|
yield* Effect.logInfo(`Calling "jobOutput"`).pipe(
|
|
915
935
|
Effect.annotateLogs({ id }),
|
|
916
936
|
)
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
937
|
+
return reportJob(id, yield* findJob(id))
|
|
938
|
+
}),
|
|
939
|
+
waitJob: Effect.fn("AgentTools.waitJob")(function* (input: unknown) {
|
|
940
|
+
const options = yield* decodeWaitJob(input)
|
|
941
|
+
const maxMs = bashTimeoutMs(yield* CommandLimits)
|
|
942
|
+
const timeoutMs = Math.min(options.timeoutMs ?? maxMs, maxMs)
|
|
943
|
+
yield* Effect.logInfo(`Calling "waitJob"`).pipe(
|
|
944
|
+
Effect.annotateLogs({ id: options.id, timeoutMs }),
|
|
945
|
+
)
|
|
946
|
+
const running = yield* findJob(options.id)
|
|
947
|
+
// Ending the wait leaves the job running.
|
|
948
|
+
yield* Effect.timeoutOption(Fiber.await(running.fiber), timeoutMs)
|
|
949
|
+
return reportJob(options.id, running)
|
|
921
950
|
}),
|
|
922
951
|
stopJob: Effect.fn("AgentTools.stopJob")(function* (input: unknown) {
|
|
923
952
|
const id = yield* decodeStopJob(input)
|
|
@@ -939,7 +968,22 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
939
968
|
stdin: "ignore",
|
|
940
969
|
})
|
|
941
970
|
const { timeout } = yield* CommandLimits
|
|
942
|
-
|
|
971
|
+
const running = yield* startCommand(cmd)
|
|
972
|
+
const exit = yield* awaitCommand(running, timeout)
|
|
973
|
+
if (Option.isNone(exit)) {
|
|
974
|
+
yield* Fiber.interrupt(running.fiber)
|
|
975
|
+
return yield* Effect.die(
|
|
976
|
+
new Error(
|
|
977
|
+
`Command timed out after ${Duration.toMillis(timeout!)}ms`,
|
|
978
|
+
),
|
|
979
|
+
)
|
|
980
|
+
}
|
|
981
|
+
const exitCode = yield* exit.value
|
|
982
|
+
const output = running.output()
|
|
983
|
+
if (exitCode === 0) return output
|
|
984
|
+
return yield* Effect.die(
|
|
985
|
+
new Error(`Command failed with exit code ${exitCode}: ${output}`),
|
|
986
|
+
)
|
|
943
987
|
}, Effect.orDie),
|
|
944
988
|
webSearch: Effect.fn("AgentTools.webSearch")(function* (options) {
|
|
945
989
|
yield* Effect.logInfo(`Calling "webSearch"`).pipe(
|
|
@@ -1208,6 +1252,14 @@ const decodeJobOutput = decodeParams(
|
|
|
1208
1252
|
AgentTools.tools.jobOutput.parametersSchema,
|
|
1209
1253
|
'jobOutput requires a job id, for example: jobOutput("1").',
|
|
1210
1254
|
)
|
|
1255
|
+
/** The caller's `bash` timeout, which also bounds `waitJob`. */
|
|
1256
|
+
const bashTimeoutMs = (limits: typeof CommandLimits.Service) =>
|
|
1257
|
+
Duration.toMillis(limits.timeout ?? defaultBashTimeout)
|
|
1258
|
+
|
|
1259
|
+
const decodeWaitJob = decodeParams(
|
|
1260
|
+
AgentTools.tools.waitJob.parametersSchema,
|
|
1261
|
+
'waitJob requires an options object, for example: waitJob({ id: "1" }) or waitJob({ id: "1", timeoutMs: 60000 }).',
|
|
1262
|
+
)
|
|
1211
1263
|
const decodeStopJob = decodeParams(
|
|
1212
1264
|
AgentTools.tools.stopJob.parametersSchema,
|
|
1213
1265
|
'stopJob requires a job id, for example: stopJob("1").',
|
|
@@ -1226,9 +1278,6 @@ interface RunningCommand {
|
|
|
1226
1278
|
reported: boolean
|
|
1227
1279
|
}
|
|
1228
1280
|
|
|
1229
|
-
const jobUsage = (id: string) =>
|
|
1230
|
-
`Use jobOutput("${id}") for its status and output, or stopJob("${id}") to stop it.`
|
|
1231
|
-
|
|
1232
1281
|
const jobInfo = (id: string, running: RunningCommand): BashJob => {
|
|
1233
1282
|
const exit = running.fiber.pollUnsafe()
|
|
1234
1283
|
const output = running.output()
|