clanka 0.23.0 → 0.23.2
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 +5 -4
- package/dist/Acp.test.js.map +1 -1
- package/dist/Agent.js +2 -2
- package/dist/Agent.js.map +1 -1
- package/dist/AgentBashJobs.test.js +5 -4
- package/dist/AgentBashJobs.test.js.map +1 -1
- package/dist/AgentExecutor.d.ts +5 -3
- package/dist/AgentExecutor.d.ts.map +1 -1
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +2 -3
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentSubagents.test.js +19 -16
- package/dist/AgentSubagents.test.js.map +1 -1
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +32 -7
- package/dist/AgentTools.js.map +1 -1
- package/dist/bin/cli.mjs +25 -10
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +8 -4
- package/src/Agent.ts +2 -2
- package/src/AgentBashJobs.test.ts +16 -5
- package/src/AgentExecutor.test.ts +2 -3
- package/src/AgentExecutor.ts +5 -3
- package/src/AgentSubagents.test.ts +25 -23
- package/src/AgentTools.ts +32 -8
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -740,11 +740,15 @@ describe("Acp", () => {
|
|
|
740
740
|
resumed ? "SECOND-RESULT" : "FIRST-RESULT",
|
|
741
741
|
)
|
|
742
742
|
}
|
|
743
|
-
const
|
|
744
|
-
|
|
743
|
+
const completions = prompt.content.filter(
|
|
744
|
+
(message) =>
|
|
745
|
+
message.role === "user" &&
|
|
746
|
+
JSON.stringify(message.content).includes("<task "),
|
|
747
|
+
).length
|
|
748
|
+
if (completions === 2) return assistantSays("done")
|
|
745
749
|
if (
|
|
746
750
|
executions === 0 ||
|
|
747
|
-
(executions === 1 &&
|
|
751
|
+
(executions === 1 && completions === 1)
|
|
748
752
|
) {
|
|
749
753
|
return parts({
|
|
750
754
|
type: "tool-call",
|
|
@@ -758,7 +762,7 @@ describe("Acp", () => {
|
|
|
758
762
|
{
|
|
759
763
|
...executor,
|
|
760
764
|
// The first script starts a background child; the second
|
|
761
|
-
// resumes it once its first
|
|
765
|
+
// resumes it once its first completion has been delivered.
|
|
762
766
|
execute: ({ agentTasks, environment }) =>
|
|
763
767
|
Stream.fromEffect(
|
|
764
768
|
executions++ === 0
|
package/src/Agent.ts
CHANGED
|
@@ -1614,7 +1614,7 @@ const taskMessage = (task: Task): string => {
|
|
|
1614
1614
|
const exitCode =
|
|
1615
1615
|
task.exitCode === undefined ? "" : ` exitCode="${task.exitCode}"`
|
|
1616
1616
|
return `<task id="${task.id}" kind="${task.kind}" status="${task.status}"${exitCode}>
|
|
1617
|
-
${
|
|
1617
|
+
To read the output, call taskOutput(${JSON.stringify(task.id)}).
|
|
1618
1618
|
</task>`
|
|
1619
1619
|
}
|
|
1620
1620
|
|
|
@@ -1771,7 +1771,7 @@ const taskInstructions = (tools: PromptTools): string => {
|
|
|
1771
1771
|
? ""
|
|
1772
1772
|
: ` ${functions.length === 1 ? functions[0]! : `${functions.slice(0, -1).join(", ")} and ${functions.at(-1)!}`}.`
|
|
1773
1773
|
const pushes = tools.taskPushes
|
|
1774
|
-
? ` When a task ends, its status and output are sent to you as a \`<task id="..." kind="..." status="...">\` message, unless a task function already gave you its final status, so you do not need to poll.`
|
|
1774
|
+
? ` When a task ends, its status and a call to retrieve its output are sent to you as a \`<task id="..." kind="..." status="...">\` message, unless a task function already gave you its final status, so you do not need to poll.`
|
|
1775
1775
|
: bash && tools.offers("taskOutput")
|
|
1776
1776
|
? ` Background commands are not sent to you when they end: check them with "taskOutput".`
|
|
1777
1777
|
: ""
|
|
@@ -24,7 +24,7 @@ import * as AgentExecutor from "./AgentExecutor.ts"
|
|
|
24
24
|
//
|
|
25
25
|
// These drive the real Agent loop and a real executor. When a background bash
|
|
26
26
|
// task exits, the next model call gets a user message
|
|
27
|
-
// `<task id="…" kind="bash" status="…" exitCode="…">output</task>`, unless the
|
|
27
|
+
// `<task id="…" kind="bash" status="…" exitCode="…">output retrieval hint</task>`, unless the
|
|
28
28
|
// task was already reported by `stopTask`, `waitTasks` or a `taskOutput` call
|
|
29
29
|
// that saw its final status. Task IDs come from the executor's counter: "1",
|
|
30
30
|
// "2", ...
|
|
@@ -161,7 +161,7 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
161
161
|
// Each test gets its own executor, so task IDs start at "1".
|
|
162
162
|
describe(`background bash tasks (${executorName} executor)`, () => {
|
|
163
163
|
it.live(
|
|
164
|
-
"a finished bash task is pushed once with its id, kind, status, exit code and output",
|
|
164
|
+
"a finished bash task is pushed once with its id, kind, status, exit code and an output retrieval call",
|
|
165
165
|
() =>
|
|
166
166
|
Effect.gen(function* () {
|
|
167
167
|
const { exit, prompts } = yield* runAgent((index) => {
|
|
@@ -175,7 +175,10 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
175
175
|
await sleep(500)`,
|
|
176
176
|
)
|
|
177
177
|
case 1:
|
|
178
|
-
return execute(
|
|
178
|
+
return execute(
|
|
179
|
+
"again",
|
|
180
|
+
`console.log((await taskOutput("1")).output)`,
|
|
181
|
+
)
|
|
179
182
|
default:
|
|
180
183
|
return text("done")
|
|
181
184
|
}
|
|
@@ -187,7 +190,15 @@ await sleep(500)`,
|
|
|
187
190
|
assert.strictEqual(pushes.length, 1, "pushed before the next call")
|
|
188
191
|
assert.match(
|
|
189
192
|
pushes[0]!,
|
|
190
|
-
/^<task id="1" kind="bash" status="failed" exitCode="3">\s*
|
|
193
|
+
/^<task id="1" kind="bash" status="failed" exitCode="3">\s*To read the output, call taskOutput\("1"\)\.\s*<\/task>$/,
|
|
194
|
+
)
|
|
195
|
+
assert.include(
|
|
196
|
+
JSON.stringify(
|
|
197
|
+
prompts[2]!.content.findLast(
|
|
198
|
+
(message) => message.role === "tool",
|
|
199
|
+
),
|
|
200
|
+
),
|
|
201
|
+
"JOB-OUTPUT",
|
|
191
202
|
)
|
|
192
203
|
assert.deepStrictEqual(
|
|
193
204
|
taskPushes(prompts[2]!),
|
|
@@ -232,7 +243,7 @@ console.log("SCRIPT" + "-DONE")`,
|
|
|
232
243
|
assert.strictEqual(pushes.length, 1)
|
|
233
244
|
assert.match(
|
|
234
245
|
pushes[0]!,
|
|
235
|
-
/^<task id="3" kind="bash" status="completed" exitCode="0">\s*
|
|
246
|
+
/^<task id="3" kind="bash" status="completed" exitCode="0">\s*To read the output, call taskOutput\("3"\)\.\s*<\/task>$/,
|
|
236
247
|
)
|
|
237
248
|
assert.include(JSON.stringify(prompts[1]), "SCRIPT-DONE")
|
|
238
249
|
}).pipe(Effect.provide(executorLayer)),
|
|
@@ -1011,7 +1011,7 @@ sleep(100).then(() => console.constructor.constructor("return process")().exit(7
|
|
|
1011
1011
|
}).pipe(Effect.timeout("3 seconds"), Effect.provide(executorLayer({}))),
|
|
1012
1012
|
)
|
|
1013
1013
|
|
|
1014
|
-
it.live("
|
|
1014
|
+
it.live("returns only the file notice for long bash output", () =>
|
|
1015
1015
|
Effect.gen(function* () {
|
|
1016
1016
|
const executor = yield* AgentExecutor.AgentExecutor
|
|
1017
1017
|
const output = yield* executor.executeUnsafe({
|
|
@@ -1020,8 +1020,7 @@ sleep(100).then(() => console.constructor.constructor("return process")().exit(7
|
|
|
1020
1020
|
})
|
|
1021
1021
|
const { output: text } = output as { readonly output: string }
|
|
1022
1022
|
assert.isBelow(text.length, 10_000 + 500)
|
|
1023
|
-
assert.
|
|
1024
|
-
assert.isTrue(text.trimEnd().endsWith("999999\n1000000"), "end kept")
|
|
1023
|
+
assert.match(text, /^Output was too large\. Saved to: [^\n]+$/)
|
|
1025
1024
|
}).pipe(
|
|
1026
1025
|
Effect.timeout("10 seconds"),
|
|
1027
1026
|
Effect.provide(executorLayer({ commandOutputChars: 10_000 })),
|
package/src/AgentExecutor.ts
CHANGED
|
@@ -293,10 +293,12 @@ export interface Limits {
|
|
|
293
293
|
readonly scriptTimeout?: Duration.Input | undefined
|
|
294
294
|
/**
|
|
295
295
|
* The most characters of `bash` and `gh` output held in memory per command.
|
|
296
|
-
*
|
|
297
|
-
*
|
|
296
|
+
* Bash output exceeding this limit is saved to a temporary file; only a
|
|
297
|
+
* message with the file path is returned. Files are removed when the
|
|
298
|
+
* executor closes. Longer gh output keeps its start and end, with a marker
|
|
299
|
+
* in place of the middle.
|
|
298
300
|
*
|
|
299
|
-
* Defaults to no limit.
|
|
301
|
+
* Defaults to 30,000 characters for bash and no limit for gh.
|
|
300
302
|
*/
|
|
301
303
|
readonly commandOutputChars?: number | undefined
|
|
302
304
|
}
|
|
@@ -23,7 +23,6 @@ import * as RpcServer from "effect/rpc/RpcServer"
|
|
|
23
23
|
import * as Agent from "./Agent.ts"
|
|
24
24
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
25
25
|
import type { Output } from "./AgentOutput.ts"
|
|
26
|
-
import * as Compaction from "./Compaction.ts"
|
|
27
26
|
|
|
28
27
|
// =============================================================================
|
|
29
28
|
// Agent tasks: lifetime and completion delivery.
|
|
@@ -274,7 +273,7 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
274
273
|
) => it.live(name, () => self().pipe(Effect.provide(executorLayer(limits))))
|
|
275
274
|
|
|
276
275
|
describe(`agent tasks (${executorName} executor)`, () => {
|
|
277
|
-
test("an idle conversation turn waits for a running child, then gets
|
|
276
|
+
test("an idle conversation turn waits for a running child, then gets an output retrieval call", () =>
|
|
278
277
|
Effect.gen(function* () {
|
|
279
278
|
const childRelease = Latch.makeUnsafe(false)
|
|
280
279
|
const childOutput = "CHILD-HEAD " + "x".repeat(40_000) + " CHILD-TAIL"
|
|
@@ -298,6 +297,13 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
298
297
|
Stream.ensuring(Effect.sync(() => childRelease.openUnsafe())),
|
|
299
298
|
)
|
|
300
299
|
case 2:
|
|
300
|
+
return Stream.make(
|
|
301
|
+
toolCall(
|
|
302
|
+
"read",
|
|
303
|
+
`console.log((await taskOutput("1")).output)`,
|
|
304
|
+
),
|
|
305
|
+
)
|
|
306
|
+
case 3:
|
|
301
307
|
return Stream.fromIterable(text("all done"))
|
|
302
308
|
default:
|
|
303
309
|
return unexpected(call)
|
|
@@ -307,7 +313,7 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
307
313
|
|
|
308
314
|
assert.deepStrictEqual(exit, Exit.succeed("all done"))
|
|
309
315
|
const root = calls.filter((call) => call.role === "root")
|
|
310
|
-
assert.strictEqual(root.length,
|
|
316
|
+
assert.strictEqual(root.length, 4)
|
|
311
317
|
|
|
312
318
|
const started = lastToolResult(root[1]!.prompt)
|
|
313
319
|
assert.include(started, '"id":"1"')
|
|
@@ -317,13 +323,11 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
317
323
|
|
|
318
324
|
const push = findPush(root[2]!.prompt, "1", "completed")
|
|
319
325
|
assert.notStrictEqual(push.index, -1, "completion push delivered")
|
|
320
|
-
assert.include(push.text, "
|
|
321
|
-
assert.
|
|
322
|
-
assert.
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
"push output goes through capOutput",
|
|
326
|
-
)
|
|
326
|
+
assert.include(push.text, 'taskOutput("1")')
|
|
327
|
+
assert.notInclude(push.text, "CHILD-HEAD")
|
|
328
|
+
assert.notInclude(push.text, "CHILD-TAIL")
|
|
329
|
+
assert.include(lastToolResult(root[3]!.prompt), "CHILD-HEAD")
|
|
330
|
+
assert.include(lastToolResult(root[3]!.prompt), "CHILD-TAIL")
|
|
327
331
|
}))
|
|
328
332
|
|
|
329
333
|
test("a completion push does not cut off the parent's in-progress response", () =>
|
|
@@ -400,7 +404,7 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
400
404
|
"the tool call after the push still ran",
|
|
401
405
|
)
|
|
402
406
|
const push = findPush(prompt, "1", "completed")
|
|
403
|
-
assert.include(push.text, "
|
|
407
|
+
assert.include(push.text, 'taskOutput("1")')
|
|
404
408
|
assert.isAbove(
|
|
405
409
|
push.index,
|
|
406
410
|
assistantIndex,
|
|
@@ -438,7 +442,8 @@ for (const [executorName, executorLayer] of executors) {
|
|
|
438
442
|
const root = calls.filter((call) => call.role === "root")
|
|
439
443
|
const push = findPush(root[2]!.prompt, "1", "failed")
|
|
440
444
|
assert.notStrictEqual(push.index, -1, "failure push delivered")
|
|
441
|
-
assert.include(push.text, "
|
|
445
|
+
assert.include(push.text, 'taskOutput("1")')
|
|
446
|
+
assert.notInclude(push.text, "rejected")
|
|
442
447
|
}))
|
|
443
448
|
|
|
444
449
|
test("taskComplete fails while a child is running and leaves it running", () =>
|
|
@@ -700,13 +705,10 @@ console.log(JSON.stringify(await taskOutput("1")))`,
|
|
|
700
705
|
if (call.index === 0) {
|
|
701
706
|
return Stream.make(spawnBackground("TASK-CHILD"))
|
|
702
707
|
}
|
|
703
|
-
if (pushes.
|
|
708
|
+
if (pushes.length === 2) {
|
|
704
709
|
return Stream.fromIterable(text("resumed"))
|
|
705
710
|
}
|
|
706
|
-
if (
|
|
707
|
-
messageCall === undefined &&
|
|
708
|
-
pushes.some((push) => push.includes("FIRST-ANSWER"))
|
|
709
|
-
) {
|
|
711
|
+
if (messageCall === undefined && pushes.length === 1) {
|
|
710
712
|
messageCall = call.index
|
|
711
713
|
return Stream.make(
|
|
712
714
|
toolCall(
|
|
@@ -721,7 +723,7 @@ console.log("MESSAGE-VOID:", result === undefined)`,
|
|
|
721
723
|
})
|
|
722
724
|
|
|
723
725
|
assert.deepStrictEqual(exit, Exit.succeed("resumed"))
|
|
724
|
-
assert.isDefined(messageCall, "the first
|
|
726
|
+
assert.isDefined(messageCall, "the first completion was pushed")
|
|
725
727
|
const root = calls.filter((call) => call.role === "root")
|
|
726
728
|
const messaged = lastToolResult(root[messageCall! + 1]!.prompt)
|
|
727
729
|
assert.include(messaged, "MESSAGE-VOID: true")
|
|
@@ -747,7 +749,7 @@ console.log("MESSAGE-VOID:", result === undefined)`,
|
|
|
747
749
|
case "child":
|
|
748
750
|
if (
|
|
749
751
|
findPush(call.prompt, "2", "completed").text.includes(
|
|
750
|
-
"
|
|
752
|
+
'taskOutput("2")',
|
|
751
753
|
)
|
|
752
754
|
) {
|
|
753
755
|
return complete("CHILD-DONE")
|
|
@@ -1014,8 +1016,8 @@ console.log("WAITED:" + task.status)`,
|
|
|
1014
1016
|
assert.strictEqual(root.length, 3)
|
|
1015
1017
|
assert.include(
|
|
1016
1018
|
findPush(root[2]!.prompt, "1", "completed").text,
|
|
1017
|
-
"
|
|
1018
|
-
"its
|
|
1019
|
+
'taskOutput("1")',
|
|
1020
|
+
"its completion is pushed once it ends",
|
|
1019
1021
|
)
|
|
1020
1022
|
}))
|
|
1021
1023
|
|
|
@@ -1056,7 +1058,7 @@ console.log("RETURNED")`,
|
|
|
1056
1058
|
assert.notInclude(result, "RETURNED")
|
|
1057
1059
|
assert.isTrue(
|
|
1058
1060
|
pushTexts(root[1]!.prompt).some((push) =>
|
|
1059
|
-
/^<task id="1" kind="bash" status="completed" exitCode="0">\s*
|
|
1061
|
+
/^<task id="1" kind="bash" status="completed" exitCode="0">\s*To read the output, call taskOutput\("1"\)\.\s*<\/task>$/.test(
|
|
1060
1062
|
push,
|
|
1061
1063
|
),
|
|
1062
1064
|
),
|
|
@@ -1064,7 +1066,7 @@ console.log("RETURNED")`,
|
|
|
1064
1066
|
)
|
|
1065
1067
|
assert.include(
|
|
1066
1068
|
findPush(root[1]!.prompt, "2", "completed").text,
|
|
1067
|
-
"
|
|
1069
|
+
'taskOutput("2")',
|
|
1068
1070
|
"the agent task is pushed",
|
|
1069
1071
|
)
|
|
1070
1072
|
}),
|
package/src/AgentTools.ts
CHANGED
|
@@ -17,6 +17,7 @@ import * as Fiber from "effect/Fiber"
|
|
|
17
17
|
import * as Toolkit from "effect/ai/Toolkit"
|
|
18
18
|
import * as Tool from "effect/ai/Tool"
|
|
19
19
|
import * as Schema from "effect/Schema"
|
|
20
|
+
import * as Scope from "effect/Scope"
|
|
20
21
|
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"
|
|
21
22
|
import * as FileSystem from "effect/FileSystem"
|
|
22
23
|
import * as Path from "effect/Path"
|
|
@@ -581,23 +582,46 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
581
582
|
const commandScope = yield* Effect.scope
|
|
582
583
|
const startCommand = Effect.fnUntraced(function* (
|
|
583
584
|
command: ChildProcess.Command,
|
|
585
|
+
saveLargeOutput = false,
|
|
584
586
|
) {
|
|
585
587
|
const { outputChars } = yield* CommandLimits
|
|
586
|
-
const
|
|
588
|
+
const maxChars = saveLargeOutput ? (outputChars ?? 30_000) : outputChars
|
|
589
|
+
const output = makeOutputBuffer(maxChars)
|
|
590
|
+
let total = 0
|
|
591
|
+
let outputFile: string | undefined
|
|
592
|
+
const append = Effect.fnUntraced(function* (chunk: string) {
|
|
593
|
+
if (saveLargeOutput) {
|
|
594
|
+
if (outputFile !== undefined) {
|
|
595
|
+
yield* fs.writeFileString(outputFile, chunk, { flag: "a" })
|
|
596
|
+
} else if (total + chunk.length > maxChars!) {
|
|
597
|
+
const file = yield* fs
|
|
598
|
+
.makeTempFileScoped({ prefix: "clanka-bash-" })
|
|
599
|
+
.pipe(Effect.provideService(Scope.Scope, commandScope))
|
|
600
|
+
yield* fs.writeFileString(file, output.render() + chunk)
|
|
601
|
+
outputFile = file
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
total += chunk.length
|
|
605
|
+
output.append(chunk)
|
|
606
|
+
})
|
|
587
607
|
const fiber = yield* spawner.spawn(command).pipe(
|
|
588
608
|
Effect.flatMap((handle) =>
|
|
589
609
|
handle.all.pipe(
|
|
590
610
|
Stream.decodeText,
|
|
591
|
-
Stream.runForEach(
|
|
592
|
-
Effect.sync(() => output.append(chunk)),
|
|
593
|
-
),
|
|
611
|
+
Stream.runForEach(append),
|
|
594
612
|
Effect.andThen(handle.exitCode),
|
|
595
613
|
),
|
|
596
614
|
),
|
|
597
615
|
Effect.scoped,
|
|
598
616
|
Effect.forkIn(commandScope),
|
|
599
617
|
)
|
|
600
|
-
return {
|
|
618
|
+
return {
|
|
619
|
+
fiber,
|
|
620
|
+
output: () =>
|
|
621
|
+
outputFile === undefined
|
|
622
|
+
? output.render()
|
|
623
|
+
: `Output was too large. Saved to: ${outputFile}`,
|
|
624
|
+
}
|
|
601
625
|
})
|
|
602
626
|
|
|
603
627
|
// Every task started on this executor, bash and agent alike, so both
|
|
@@ -605,7 +629,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
605
629
|
// started it.
|
|
606
630
|
const tasks = new Map<string, TaskEntry>()
|
|
607
631
|
let nextTaskId = 1
|
|
608
|
-
const registerAgentTask = Effect.
|
|
632
|
+
const registerAgentTask = Effect.gen(function* () {
|
|
609
633
|
const id = String(nextTaskId++)
|
|
610
634
|
tasks.set(id, { kind: "agent", id, owner: yield* TaskOwner })
|
|
611
635
|
return id
|
|
@@ -883,7 +907,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
883
907
|
})
|
|
884
908
|
const entry = yield* addBashTask(
|
|
885
909
|
options.command,
|
|
886
|
-
yield* startCommand(cmd),
|
|
910
|
+
yield* startCommand(cmd, true),
|
|
887
911
|
)
|
|
888
912
|
if (options.background !== true) {
|
|
889
913
|
const exit = yield* awaitCommand(
|
|
@@ -1179,7 +1203,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
1179
1203
|
yield* Effect.logInfo(`Calling "subagent"`).pipe(
|
|
1180
1204
|
Effect.annotateLogs({ background }),
|
|
1181
1205
|
)
|
|
1182
|
-
const id = yield* registerAgentTask
|
|
1206
|
+
const id = yield* registerAgentTask
|
|
1183
1207
|
const agentTasks = yield* AgentTaskExecutor
|
|
1184
1208
|
return yield* agentTasks.subagent({ id, prompt, background })
|
|
1185
1209
|
}),
|