clanka 0.18.2 → 0.19.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/Agent.d.ts.map +1 -1
- package/dist/Agent.js +2 -1
- package/dist/Agent.js.map +1 -1
- package/dist/AgentExecutor.js +6 -1
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +134 -6
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentTools.d.ts +121 -0
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +162 -44
- package/dist/AgentTools.js.map +1 -1
- package/dist/bin/cli.mjs +137 -35
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Agent.ts +2 -1
- package/src/AgentExecutor.test.ts +209 -6
- package/src/AgentExecutor.ts +6 -1
- package/src/AgentTools.ts +230 -73
package/package.json
CHANGED
package/src/Agent.ts
CHANGED
|
@@ -1421,7 +1421,8 @@ const generateSystemTools = (
|
|
|
1421
1421
|
)}${when(
|
|
1422
1422
|
tools.offers("bash"),
|
|
1423
1423
|
`
|
|
1424
|
-
- Avoid passing scripts into the "bash" function, and instead write javascript
|
|
1424
|
+
- Avoid passing scripts into the "bash" function, and instead write javascript.
|
|
1425
|
+
- 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.`,
|
|
1425
1426
|
)}
|
|
1426
1427
|
- Variables are not shared between executions.
|
|
1427
1428
|
- Do not use \`require\`, \`import\`, \`process\`, or any other Node.js apis.${when(
|
|
@@ -356,6 +356,61 @@ const runScript = (script: string) =>
|
|
|
356
356
|
.pipe(Stream.mkString)
|
|
357
357
|
})
|
|
358
358
|
|
|
359
|
+
const readLog = (path: string) =>
|
|
360
|
+
NodeFs.existsSync(path) ? NodeFs.readFileSync(path, "utf8") : ""
|
|
361
|
+
|
|
362
|
+
// A command that records its shell's pid and logs its start, then waits for
|
|
363
|
+
// `openGate` before it logs its finish. The log shows whether it was killed
|
|
364
|
+
// or started twice, without racing a sleep.
|
|
365
|
+
const gatedCommand = (directory: string) => {
|
|
366
|
+
const pid = NodePath.join(directory, "pid")
|
|
367
|
+
const log = NodePath.join(directory, "runs.log")
|
|
368
|
+
const gate = NodePath.join(directory, "gate")
|
|
369
|
+
return `echo $$ > ${pid} && echo start >> ${log} && until [ -f ${gate} ]; do sleep 0.02; done && echo finish >> ${log}`
|
|
370
|
+
}
|
|
371
|
+
const openGate = (directory: string) =>
|
|
372
|
+
NodeFs.writeFileSync(NodePath.join(directory, "gate"), "")
|
|
373
|
+
|
|
374
|
+
const isAlive = (pid: number) => {
|
|
375
|
+
try {
|
|
376
|
+
process.kill(pid, 0)
|
|
377
|
+
return true
|
|
378
|
+
} catch (error) {
|
|
379
|
+
if ((error as NodeJS.ErrnoException).code === "ESRCH") return false
|
|
380
|
+
throw error
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Polls until the gated command's shell has exited. The kill can land after
|
|
385
|
+
// the caller has moved on, as an RPC interrupt does not wait for the server.
|
|
386
|
+
// A command that is never killed waits at its gate, so it fails the deadline.
|
|
387
|
+
const waitForExit = (directory: string) =>
|
|
388
|
+
Effect.gen(function* () {
|
|
389
|
+
const pid = Number(
|
|
390
|
+
NodeFs.readFileSync(NodePath.join(directory, "pid"), "utf8"),
|
|
391
|
+
)
|
|
392
|
+
while (isAlive(pid)) {
|
|
393
|
+
yield* Effect.sleep("20 millis")
|
|
394
|
+
}
|
|
395
|
+
}).pipe(
|
|
396
|
+
Effect.timeoutOrElse({
|
|
397
|
+
duration: "2 seconds",
|
|
398
|
+
orElse: () => Effect.die(new Error("The command was not stopped")),
|
|
399
|
+
}),
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
// Polls the log until `done` holds.
|
|
403
|
+
const waitForLog = (directory: string, done: (log: string) => boolean) =>
|
|
404
|
+
Effect.gen(function* () {
|
|
405
|
+
const path = NodePath.join(directory, "runs.log")
|
|
406
|
+
while (!done(readLog(path))) {
|
|
407
|
+
yield* Effect.sleep("20 millis")
|
|
408
|
+
}
|
|
409
|
+
return readLog(path)
|
|
410
|
+
})
|
|
411
|
+
const started = (log: string) => log !== ""
|
|
412
|
+
const finished = (log: string) => log.includes("finish")
|
|
413
|
+
|
|
359
414
|
// Puts a `gh` that only sleeps first on PATH, so the test needs neither the
|
|
360
415
|
// real CLI nor the network.
|
|
361
416
|
const withSlowGh = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
@@ -502,23 +557,171 @@ await sleep(60000)`,
|
|
|
502
557
|
})
|
|
503
558
|
|
|
504
559
|
describe(`${mode} executor limits`, () => {
|
|
505
|
-
it.live("
|
|
560
|
+
it.live("runs a bash command in the background", () =>
|
|
561
|
+
Effect.gen(function* () {
|
|
562
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
563
|
+
const { environment } = yield* executor.capabilities
|
|
564
|
+
const output = yield* runScript(`
|
|
565
|
+
const result = await bash({
|
|
566
|
+
command: ${JSON.stringify(gatedCommand(environment.cwd))},
|
|
567
|
+
background: true,
|
|
568
|
+
})
|
|
569
|
+
console.log("RETURNED:", result)`)
|
|
570
|
+
assert.include(output, "RETURNED:")
|
|
571
|
+
assert.match(output, /background/i)
|
|
572
|
+
assert.strictEqual(
|
|
573
|
+
yield* waitForLog(environment.cwd, started),
|
|
574
|
+
"start\n",
|
|
575
|
+
"still running after the script ended",
|
|
576
|
+
)
|
|
577
|
+
openGate(environment.cwd)
|
|
578
|
+
assert.strictEqual(
|
|
579
|
+
yield* waitForLog(environment.cwd, finished),
|
|
580
|
+
"start\nfinish\n",
|
|
581
|
+
)
|
|
582
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
583
|
+
)
|
|
584
|
+
|
|
585
|
+
it.live("moves a bash command to the background at its timeout", () =>
|
|
506
586
|
Effect.gen(function* () {
|
|
587
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
588
|
+
const { environment } = yield* executor.capabilities
|
|
507
589
|
const output = yield* runScript(`
|
|
508
590
|
try {
|
|
509
|
-
await bash({
|
|
591
|
+
const result = await bash({
|
|
592
|
+
command: ${JSON.stringify(gatedCommand(environment.cwd))},
|
|
593
|
+
timeoutMs: 60000,
|
|
594
|
+
})
|
|
595
|
+
console.log("RETURNED:", result)
|
|
510
596
|
} catch (error) {
|
|
511
597
|
console.log("REJECTED:", String(error))
|
|
512
598
|
}`)
|
|
513
|
-
assert.include(output, "
|
|
514
|
-
assert.
|
|
515
|
-
assert.
|
|
599
|
+
assert.include(output, "RETURNED:")
|
|
600
|
+
assert.notInclude(output, "REJECTED:")
|
|
601
|
+
assert.match(output, /background/i)
|
|
602
|
+
assert.strictEqual(
|
|
603
|
+
yield* waitForLog(environment.cwd, started),
|
|
604
|
+
"start\n",
|
|
605
|
+
"still running after the script ended",
|
|
606
|
+
)
|
|
607
|
+
openGate(environment.cwd)
|
|
608
|
+
assert.strictEqual(
|
|
609
|
+
yield* waitForLog(environment.cwd, finished),
|
|
610
|
+
"start\nfinish\n",
|
|
611
|
+
)
|
|
516
612
|
}).pipe(
|
|
517
|
-
Effect.timeout("
|
|
613
|
+
Effect.timeout("5 seconds"),
|
|
518
614
|
Effect.provide(executorLayer({ commandTimeout: "300 millis" })),
|
|
519
615
|
),
|
|
520
616
|
)
|
|
521
617
|
|
|
618
|
+
it.live("reports background jobs through jobOutput", () =>
|
|
619
|
+
Effect.gen(function* () {
|
|
620
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
621
|
+
const { environment } = yield* executor.capabilities
|
|
622
|
+
const output = yield* runScript(`
|
|
623
|
+
const done = async (id) => {
|
|
624
|
+
let job = await jobOutput(id)
|
|
625
|
+
while (job.status === "running") {
|
|
626
|
+
await sleep(20)
|
|
627
|
+
job = await jobOutput(id)
|
|
628
|
+
}
|
|
629
|
+
return job
|
|
630
|
+
}
|
|
631
|
+
await bash({ command: ${JSON.stringify(gatedCommand(environment.cwd))}, background: true })
|
|
632
|
+
await bash({ command: "echo out && exit 3", background: true })
|
|
633
|
+
console.log("RUNNING:", JSON.stringify(await jobOutput("1")))
|
|
634
|
+
await writeFile({ path: "gate", content: "" })
|
|
635
|
+
console.log("COMPLETED:", JSON.stringify(await done("1")))
|
|
636
|
+
console.log("FAILED:", JSON.stringify(await done("2")))`)
|
|
637
|
+
assert.include(output, 'RUNNING: {"id":"1","status":"running"')
|
|
638
|
+
assert.include(
|
|
639
|
+
output,
|
|
640
|
+
'COMPLETED: {"id":"1","status":"completed","exitCode":0,"output":""}',
|
|
641
|
+
)
|
|
642
|
+
assert.include(
|
|
643
|
+
output,
|
|
644
|
+
'FAILED: {"id":"2","status":"failed","exitCode":3,"output":"out\\n"}',
|
|
645
|
+
)
|
|
646
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
it.live("stops a background job with stopJob", () =>
|
|
650
|
+
Effect.gen(function* () {
|
|
651
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
652
|
+
const { environment } = yield* executor.capabilities
|
|
653
|
+
const output = yield* runScript(`
|
|
654
|
+
await bash({ command: ${JSON.stringify(gatedCommand(environment.cwd))}, background: true })
|
|
655
|
+
console.log("STOPPED:", JSON.stringify(await stopJob("1")))
|
|
656
|
+
try {
|
|
657
|
+
await stopJob("99")
|
|
658
|
+
} catch (error) {
|
|
659
|
+
console.log("REJECTED:", String(error))
|
|
660
|
+
}`)
|
|
661
|
+
assert.include(output, 'STOPPED: {"id":"1","status":"stopped"')
|
|
662
|
+
assert.match(output, /REJECTED:.*No background job with id "99"/)
|
|
663
|
+
openGate(environment.cwd)
|
|
664
|
+
yield* Effect.sleep("500 millis")
|
|
665
|
+
assert.notInclude(
|
|
666
|
+
readLog(NodePath.join(environment.cwd, "runs.log")),
|
|
667
|
+
"finish",
|
|
668
|
+
"killed",
|
|
669
|
+
)
|
|
670
|
+
}).pipe(Effect.timeout("5 seconds"), Effect.provide(executorLayer({}))),
|
|
671
|
+
)
|
|
672
|
+
|
|
673
|
+
// Interrupting the script ends the call before the command timeout, so
|
|
674
|
+
// the command is stopped rather than handed over.
|
|
675
|
+
it.live(
|
|
676
|
+
"stops a foreground bash command when the script is interrupted",
|
|
677
|
+
() =>
|
|
678
|
+
Effect.gen(function* () {
|
|
679
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
680
|
+
const { environment } = yield* executor.capabilities
|
|
681
|
+
const fiber = yield* runScript(
|
|
682
|
+
`await bash({ command: ${JSON.stringify(gatedCommand(environment.cwd))} })`,
|
|
683
|
+
).pipe(Effect.forkScoped)
|
|
684
|
+
yield* waitForLog(environment.cwd, started)
|
|
685
|
+
yield* Fiber.interrupt(fiber)
|
|
686
|
+
yield* waitForExit(environment.cwd)
|
|
687
|
+
assert.strictEqual(
|
|
688
|
+
readLog(NodePath.join(environment.cwd, "runs.log")),
|
|
689
|
+
"start\n",
|
|
690
|
+
"killed",
|
|
691
|
+
)
|
|
692
|
+
}).pipe(
|
|
693
|
+
Effect.scoped,
|
|
694
|
+
Effect.timeout("5 seconds"),
|
|
695
|
+
Effect.provide(executorLayer({})),
|
|
696
|
+
),
|
|
697
|
+
)
|
|
698
|
+
|
|
699
|
+
// The log sits outside the executor's directory, which is removed with
|
|
700
|
+
// the executor.
|
|
701
|
+
it.live("stops background jobs when the executor shuts down", () =>
|
|
702
|
+
Effect.gen(function* () {
|
|
703
|
+
const fs = yield* FileSystem.FileSystem
|
|
704
|
+
const directory = yield* fs.makeTempDirectoryScoped()
|
|
705
|
+
yield* runScript(
|
|
706
|
+
`await bash({ command: ${JSON.stringify(gatedCommand(directory))}, background: true })`,
|
|
707
|
+
).pipe(
|
|
708
|
+
Effect.andThen(waitForLog(directory, started)),
|
|
709
|
+
Effect.provide(executorLayer({})),
|
|
710
|
+
)
|
|
711
|
+
openGate(directory)
|
|
712
|
+
yield* Effect.sleep("500 millis")
|
|
713
|
+
assert.strictEqual(
|
|
714
|
+
readLog(NodePath.join(directory, "runs.log")),
|
|
715
|
+
"start\n",
|
|
716
|
+
"killed",
|
|
717
|
+
)
|
|
718
|
+
}).pipe(
|
|
719
|
+
Effect.scoped,
|
|
720
|
+
Effect.timeout("5 seconds"),
|
|
721
|
+
Effect.provide(NodeServices.layer),
|
|
722
|
+
),
|
|
723
|
+
)
|
|
724
|
+
|
|
522
725
|
it.live("stops a gh command at the caller's command timeout", () =>
|
|
523
726
|
Effect.gen(function* () {
|
|
524
727
|
const output = yield* runScript(`
|
package/src/AgentExecutor.ts
CHANGED
|
@@ -1257,7 +1257,8 @@ const errorMessage = (error: unknown): string =>
|
|
|
1257
1257
|
? error.message
|
|
1258
1258
|
: String(error)
|
|
1259
1259
|
|
|
1260
|
-
// Leaving out `delegate` leaves out the tools that address its sub-agents
|
|
1260
|
+
// Leaving out `delegate` leaves out the tools that address its sub-agents,
|
|
1261
|
+
// and leaving out `bash` leaves out the tools that address its jobs.
|
|
1261
1262
|
const excludedToolNames = (
|
|
1262
1263
|
excludeTools: ReadonlyArray<ExcludableTool> | undefined,
|
|
1263
1264
|
): ReadonlySet<string> => {
|
|
@@ -1272,6 +1273,10 @@ const excludedToolNames = (
|
|
|
1272
1273
|
names.add(name)
|
|
1273
1274
|
}
|
|
1274
1275
|
}
|
|
1276
|
+
if (names.has("bash")) {
|
|
1277
|
+
names.add("jobOutput")
|
|
1278
|
+
names.add("stopJob")
|
|
1279
|
+
}
|
|
1275
1280
|
return names
|
|
1276
1281
|
}
|
|
1277
1282
|
|
package/src/AgentTools.ts
CHANGED
|
@@ -6,9 +6,13 @@ import { parsePatch, patchChunks } from "./ApplyPatch.ts"
|
|
|
6
6
|
import * as ExaSearch from "./ExaSearch.ts"
|
|
7
7
|
import * as WebToMarkdown from "./WebToMarkdown.ts"
|
|
8
8
|
import type * as HttpClient from "effect/http/HttpClient"
|
|
9
|
+
import type * as PlatformError from "effect/PlatformError"
|
|
9
10
|
import * as Context from "effect/Context"
|
|
10
11
|
import * as Duration from "effect/Duration"
|
|
11
12
|
import * as Effect from "effect/Effect"
|
|
13
|
+
import * as Cause from "effect/Cause"
|
|
14
|
+
import * as Exit from "effect/Exit"
|
|
15
|
+
import * as Fiber from "effect/Fiber"
|
|
12
16
|
import * as Toolkit from "effect/ai/Toolkit"
|
|
13
17
|
import * as Tool from "effect/ai/Tool"
|
|
14
18
|
import * as Schema from "effect/Schema"
|
|
@@ -199,6 +203,43 @@ export class ImageAttacher extends Context.Service<
|
|
|
199
203
|
(image: ImageAttachment) => Effect.Effect<void>
|
|
200
204
|
>()("clanka/AgentTools/ImageAttacher") {}
|
|
201
205
|
|
|
206
|
+
/**
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
* @category Models
|
|
209
|
+
*/
|
|
210
|
+
export const BashJobStatus = Schema.Literals([
|
|
211
|
+
"running",
|
|
212
|
+
"completed",
|
|
213
|
+
"failed",
|
|
214
|
+
"stopped",
|
|
215
|
+
])
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @since 1.0.0
|
|
219
|
+
* @category Models
|
|
220
|
+
*/
|
|
221
|
+
export type BashJobStatus = typeof BashJobStatus.Type
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* A `bash` command running in the background, as returned by `jobOutput`
|
|
225
|
+
* and `stopJob`.
|
|
226
|
+
*
|
|
227
|
+
* @since 1.0.0
|
|
228
|
+
* @category Models
|
|
229
|
+
*/
|
|
230
|
+
export const BashJob = Schema.Struct({
|
|
231
|
+
id: Schema.String,
|
|
232
|
+
status: BashJobStatus,
|
|
233
|
+
exitCode: Schema.optional(Schema.Number),
|
|
234
|
+
output: Schema.String,
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @since 1.0.0
|
|
239
|
+
* @category Models
|
|
240
|
+
*/
|
|
241
|
+
export type BashJob = typeof BashJob.Type
|
|
242
|
+
|
|
202
243
|
/**
|
|
203
244
|
* The `bash` timeout when the caller sets none.
|
|
204
245
|
*
|
|
@@ -245,11 +286,16 @@ export class CommandLimits extends Context.Reference<{
|
|
|
245
286
|
*/
|
|
246
287
|
export const makeBashTool = (timeout: Duration.Duration) =>
|
|
247
288
|
Tool.make("bash", {
|
|
248
|
-
description:
|
|
289
|
+
description:
|
|
290
|
+
'Run a bash command and return the output. With `background: true` it returns straight away with a job id while the command keeps running. A command still running at its timeout is not stopped: it is moved to the background, and the output so far is returned with its job id. Read a job with "jobOutput" and stop it with "stopJob".',
|
|
249
291
|
parameters: Schema.Struct({
|
|
250
292
|
command: Schema.String,
|
|
251
293
|
timeoutMs: Schema.optional(Schema.Finite).annotate({
|
|
252
|
-
documentation: `
|
|
294
|
+
documentation: `How long to wait in ms before moving the command to the background (default and max: ${Duration.toMillis(timeout)})`,
|
|
295
|
+
}),
|
|
296
|
+
background: Schema.optional(Schema.Boolean).annotate({
|
|
297
|
+
documentation:
|
|
298
|
+
"Start the command in the background and return its job id without waiting",
|
|
253
299
|
}),
|
|
254
300
|
}).annotate({
|
|
255
301
|
identifier: "command",
|
|
@@ -388,6 +434,24 @@ export const AgentTools = Toolkit.make(
|
|
|
388
434
|
dependencies: [CurrentDirectory],
|
|
389
435
|
}),
|
|
390
436
|
makeBashTool(defaultBashTimeout),
|
|
437
|
+
Tool.make("jobOutput", {
|
|
438
|
+
description:
|
|
439
|
+
'Return the status and output so far of a background bash job.',
|
|
440
|
+
parameters: Schema.String.annotate({
|
|
441
|
+
identifier: "id",
|
|
442
|
+
}),
|
|
443
|
+
success: BashJob,
|
|
444
|
+
failure: Schema.String,
|
|
445
|
+
}),
|
|
446
|
+
Tool.make("stopJob", {
|
|
447
|
+
description:
|
|
448
|
+
"Stop a background bash job and return its final status and output.",
|
|
449
|
+
parameters: Schema.String.annotate({
|
|
450
|
+
identifier: "id",
|
|
451
|
+
}),
|
|
452
|
+
success: BashJob,
|
|
453
|
+
failure: Schema.String,
|
|
454
|
+
}),
|
|
391
455
|
Tool.make("applyPatch", {
|
|
392
456
|
description:
|
|
393
457
|
"Add, update or remove multiple files with a git diff / unified diff / wrapped patch.",
|
|
@@ -502,45 +566,79 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
502
566
|
const pathService = yield* Path.Path
|
|
503
567
|
const webSearch = yield* ExaSearch.ExaSearch
|
|
504
568
|
const fetchMarkdown = yield* WebToMarkdown.WebToMarkdown
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
new Error(
|
|
523
|
-
`Command failed with exit code ${exitCode}: ${output}`,
|
|
524
|
-
),
|
|
525
|
-
)
|
|
526
|
-
}),
|
|
569
|
+
// Commands run in fibers owned by the executor rather than the tool call,
|
|
570
|
+
// so one that is moved to the background outlives the script that
|
|
571
|
+
// started it. Closing the executor stops them all.
|
|
572
|
+
const commandScope = yield* Effect.scope
|
|
573
|
+
const startCommand = Effect.fnUntraced(function* (
|
|
574
|
+
command: ChildProcess.Command,
|
|
575
|
+
) {
|
|
576
|
+
const { outputChars } = yield* CommandLimits
|
|
577
|
+
const output = makeOutputBuffer(outputChars)
|
|
578
|
+
const fiber = yield* spawner.spawn(command).pipe(
|
|
579
|
+
Effect.flatMap((handle) =>
|
|
580
|
+
handle.all.pipe(
|
|
581
|
+
Stream.decodeText,
|
|
582
|
+
Stream.runForEach((chunk) =>
|
|
583
|
+
Effect.sync(() => output.append(chunk)),
|
|
584
|
+
),
|
|
585
|
+
Effect.andThen(handle.exitCode),
|
|
527
586
|
),
|
|
528
|
-
)
|
|
529
|
-
|
|
530
|
-
|
|
587
|
+
),
|
|
588
|
+
Effect.scoped,
|
|
589
|
+
Effect.forkIn(commandScope),
|
|
590
|
+
)
|
|
591
|
+
return { fiber, output: output.render }
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
const jobs = new Map<string, RunningCommand>()
|
|
595
|
+
let nextJobId = 1
|
|
596
|
+
const addJob = (running: RunningCommand) => {
|
|
597
|
+
const id = String(nextJobId++)
|
|
598
|
+
jobs.set(id, running)
|
|
599
|
+
return id
|
|
600
|
+
}
|
|
601
|
+
const findJob = (id: string) => {
|
|
602
|
+
const running = jobs.get(id)
|
|
603
|
+
return running === undefined
|
|
604
|
+
? Effect.fail(`No background job with id "${id}"`)
|
|
605
|
+
: Effect.succeed(running)
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// A command still running at its timeout is stopped, or with
|
|
609
|
+
// `background` handed over as a job. A caller that is interrupted first
|
|
610
|
+
// stops it either way.
|
|
611
|
+
const runCommand = Effect.fnUntraced(function* (
|
|
612
|
+
command: ChildProcess.Command,
|
|
613
|
+
timeout: Duration.Duration | undefined,
|
|
614
|
+
onTimeout: "stop" | "background",
|
|
615
|
+
) {
|
|
616
|
+
const running = yield* startCommand(command)
|
|
617
|
+
const awaited = Fiber.await(running.fiber)
|
|
618
|
+
const exit = yield* (
|
|
531
619
|
timeout === undefined
|
|
532
|
-
?
|
|
533
|
-
: Effect.
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
620
|
+
? Effect.asSome(awaited)
|
|
621
|
+
: Effect.timeoutOption(awaited, timeout)
|
|
622
|
+
).pipe(Effect.onInterrupt(() => Fiber.interrupt(running.fiber)))
|
|
623
|
+
if (Option.isNone(exit)) {
|
|
624
|
+
const timeoutMs = Duration.toMillis(timeout!)
|
|
625
|
+
if (onTimeout === "stop") {
|
|
626
|
+
yield* Fiber.interrupt(running.fiber)
|
|
627
|
+
return yield* Effect.die(
|
|
628
|
+
new Error(`Command timed out after ${timeoutMs}ms`),
|
|
629
|
+
)
|
|
630
|
+
}
|
|
631
|
+
const id = addJob(running)
|
|
632
|
+
const output = running.output()
|
|
633
|
+
return `${output}${output === "" || output.endsWith("\n") ? "" : "\n"}[Still running after ${timeoutMs}ms, so it was moved to the background as job ${id}. ${jobUsage(id)}]`
|
|
634
|
+
}
|
|
635
|
+
const exitCode = yield* exit.value
|
|
636
|
+
const output = running.output()
|
|
637
|
+
if (exitCode === 0) return output
|
|
638
|
+
return yield* Effect.die(
|
|
639
|
+
new Error(`Command failed with exit code ${exitCode}: ${output}`),
|
|
640
|
+
)
|
|
641
|
+
})
|
|
544
642
|
|
|
545
643
|
/**
|
|
546
644
|
* Image branch of `readFile`: the bytes go to the `ImageAttacher` for
|
|
@@ -758,8 +856,28 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
758
856
|
cwd,
|
|
759
857
|
stdin: "ignore",
|
|
760
858
|
})
|
|
761
|
-
|
|
859
|
+
if (options.background === true) {
|
|
860
|
+
const id = addJob(yield* startCommand(cmd))
|
|
861
|
+
return `Started background job ${id}. ${jobUsage(id)}`
|
|
862
|
+
}
|
|
863
|
+
return yield* runCommand(cmd, Duration.millis(timeoutMs), "background")
|
|
762
864
|
}, Effect.orDie),
|
|
865
|
+
jobOutput: Effect.fn("AgentTools.jobOutput")(function* (input: unknown) {
|
|
866
|
+
const id = yield* decodeJobOutput(input)
|
|
867
|
+
yield* Effect.logInfo(`Calling "jobOutput"`).pipe(
|
|
868
|
+
Effect.annotateLogs({ id }),
|
|
869
|
+
)
|
|
870
|
+
return jobInfo(id, yield* findJob(id))
|
|
871
|
+
}),
|
|
872
|
+
stopJob: Effect.fn("AgentTools.stopJob")(function* (input: unknown) {
|
|
873
|
+
const id = yield* decodeStopJob(input)
|
|
874
|
+
yield* Effect.logInfo(`Calling "stopJob"`).pipe(
|
|
875
|
+
Effect.annotateLogs({ id }),
|
|
876
|
+
)
|
|
877
|
+
const running = yield* findJob(id)
|
|
878
|
+
yield* Fiber.interrupt(running.fiber)
|
|
879
|
+
return jobInfo(id, running)
|
|
880
|
+
}),
|
|
763
881
|
gh: Effect.fn("AgentTools.gh")(function* (args) {
|
|
764
882
|
yield* Effect.logInfo(`Calling "gh"`).pipe(
|
|
765
883
|
Effect.annotateLogs({ args }),
|
|
@@ -770,7 +888,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
770
888
|
stdin: "ignore",
|
|
771
889
|
})
|
|
772
890
|
const { timeout } = yield* CommandLimits
|
|
773
|
-
return yield*
|
|
891
|
+
return yield* runCommand(cmd, timeout, "stop")
|
|
774
892
|
}, Effect.orDie),
|
|
775
893
|
webSearch: Effect.fn("AgentTools.webSearch")(function* (options) {
|
|
776
894
|
yield* Effect.logInfo(`Calling "webSearch"`).pipe(
|
|
@@ -1035,41 +1153,80 @@ const decodeStopAgent = decodeParams(
|
|
|
1035
1153
|
'stopAgent requires an agent id, for example: stopAgent("3").',
|
|
1036
1154
|
)
|
|
1037
1155
|
|
|
1156
|
+
const decodeJobOutput = decodeParams(
|
|
1157
|
+
AgentTools.tools.jobOutput.parametersSchema,
|
|
1158
|
+
'jobOutput requires a job id, for example: jobOutput("1").',
|
|
1159
|
+
)
|
|
1160
|
+
const decodeStopJob = decodeParams(
|
|
1161
|
+
AgentTools.tools.stopJob.parametersSchema,
|
|
1162
|
+
'stopJob requires a job id, for example: stopJob("1").',
|
|
1163
|
+
)
|
|
1164
|
+
|
|
1165
|
+
interface RunningCommand {
|
|
1166
|
+
readonly fiber: Fiber.Fiber<
|
|
1167
|
+
ChildProcessSpawner.ExitCode,
|
|
1168
|
+
PlatformError.PlatformError
|
|
1169
|
+
>
|
|
1170
|
+
readonly output: () => string
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
const jobUsage = (id: string) =>
|
|
1174
|
+
`Use jobOutput("${id}") for its status and output, or stopJob("${id}") to stop it.`
|
|
1175
|
+
|
|
1176
|
+
const jobInfo = (id: string, running: RunningCommand): BashJob => {
|
|
1177
|
+
const exit = running.fiber.pollUnsafe()
|
|
1178
|
+
const output = running.output()
|
|
1179
|
+
if (exit === undefined) return { id, status: "running", output }
|
|
1180
|
+
if (Exit.isSuccess(exit)) {
|
|
1181
|
+
return {
|
|
1182
|
+
id,
|
|
1183
|
+
status: exit.value === 0 ? "completed" : "failed",
|
|
1184
|
+
exitCode: exit.value,
|
|
1185
|
+
output,
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (Cause.hasInterruptsOnly(exit.cause)) {
|
|
1189
|
+
return { id, status: "stopped", output }
|
|
1190
|
+
}
|
|
1191
|
+
return {
|
|
1192
|
+
id,
|
|
1193
|
+
status: "failed",
|
|
1194
|
+
output: `${output}${output === "" ? "" : "\n"}${Cause.pretty(exit.cause)}`,
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1038
1198
|
/**
|
|
1039
|
-
*
|
|
1040
|
-
* end, and a marker in place of the middle. Only the kept text is held in
|
|
1041
|
-
* memory
|
|
1199
|
+
* Accumulates text output keeping at most `maxChars` characters: the start,
|
|
1200
|
+
* the end, and a marker in place of the middle. Only the kept text is held in
|
|
1201
|
+
* memory. Without a limit everything is kept.
|
|
1042
1202
|
*/
|
|
1043
|
-
const
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
)
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
if (omitted <= 0) return head + tail
|
|
1069
|
-
return `${head}\n\n[... ${omitted} of ${total} chars omitted from the middle of the output ...]\n\n${tail}`
|
|
1070
|
-
}),
|
|
1071
|
-
)
|
|
1203
|
+
const makeOutputBuffer = (maxChars: number | undefined) => {
|
|
1204
|
+
const headChars =
|
|
1205
|
+
maxChars === undefined ? Number.POSITIVE_INFINITY : Math.floor(maxChars / 2)
|
|
1206
|
+
const tailChars = maxChars === undefined ? 0 : maxChars - headChars
|
|
1207
|
+
let head = ""
|
|
1208
|
+
let tail = ""
|
|
1209
|
+
let total = 0
|
|
1210
|
+
return {
|
|
1211
|
+
append(chunk: string) {
|
|
1212
|
+
total += chunk.length
|
|
1213
|
+
let rest = chunk
|
|
1214
|
+
if (head.length < headChars) {
|
|
1215
|
+
const take = headChars - head.length
|
|
1216
|
+
head += rest.slice(0, take)
|
|
1217
|
+
rest = rest.slice(take)
|
|
1218
|
+
}
|
|
1219
|
+
if (rest.length > 0 && tailChars > 0) {
|
|
1220
|
+
tail = (tail + rest).slice(-tailChars)
|
|
1221
|
+
}
|
|
1222
|
+
},
|
|
1223
|
+
render() {
|
|
1224
|
+
const omitted = total - head.length - tail.length
|
|
1225
|
+
if (omitted <= 0) return head + tail
|
|
1226
|
+
return `${head}\n\n[... ${omitted} of ${total} chars omitted from the middle of the output ...]\n\n${tail}`
|
|
1227
|
+
},
|
|
1072
1228
|
}
|
|
1229
|
+
}
|
|
1073
1230
|
|
|
1074
1231
|
class ApplyPatchError extends Data.TaggedClass("ApplyPatchError")<{
|
|
1075
1232
|
readonly message: string
|