clanka 0.12.0 → 0.12.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.d.ts.map +1 -1
- package/dist/Acp.js +10 -0
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +62 -0
- package/dist/Acp.test.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +41 -2
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +86 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/bin/cli.mjs +28 -4
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +106 -0
- package/src/Acp.ts +13 -0
- package/src/Agent.test.ts +103 -1
- package/src/Agent.ts +50 -1
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { assert, describe, it } from "@effect/vitest"
|
|
2
2
|
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
3
|
+
import type * as Cause from "effect/Cause"
|
|
3
4
|
import * as Context from "effect/Context"
|
|
5
|
+
import * as Deferred from "effect/Deferred"
|
|
4
6
|
import * as Effect from "effect/Effect"
|
|
5
7
|
import * as FileSystem from "effect/FileSystem"
|
|
6
8
|
import * as Fiber from "effect/Fiber"
|
|
7
9
|
import * as Layer from "effect/Layer"
|
|
10
|
+
import * as Logger from "effect/Logger"
|
|
8
11
|
import * as Option from "effect/Option"
|
|
9
12
|
import * as Path from "effect/Path"
|
|
13
|
+
import * as Queue from "effect/Queue"
|
|
10
14
|
import * as Schedule from "effect/Schedule"
|
|
15
|
+
import * as Sink from "effect/Sink"
|
|
16
|
+
import * as Stdio from "effect/Stdio"
|
|
11
17
|
import * as Stream from "effect/Stream"
|
|
12
18
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
13
19
|
import * as Prompt from "effect/ai/Prompt"
|
|
@@ -952,6 +958,106 @@ await changeDirectory("sub")`),
|
|
|
952
958
|
),
|
|
953
959
|
),
|
|
954
960
|
)
|
|
961
|
+
|
|
962
|
+
it.effect("reports prompts still running when stdin ends", () =>
|
|
963
|
+
withStore(
|
|
964
|
+
Effect.gen(function* () {
|
|
965
|
+
const modelCalled = yield* Deferred.make<void>()
|
|
966
|
+
const languageModel = yield* LanguageModel.make({
|
|
967
|
+
generateText: () => Effect.succeed([]),
|
|
968
|
+
streamText: () =>
|
|
969
|
+
Stream.fromEffect(Deferred.succeed(modelCalled, undefined)).pipe(
|
|
970
|
+
Stream.drain,
|
|
971
|
+
Stream.concat(Stream.never),
|
|
972
|
+
),
|
|
973
|
+
})
|
|
974
|
+
const modelLayer = Layer.mergeAll(
|
|
975
|
+
Layer.succeed(LanguageModel.LanguageModel, languageModel),
|
|
976
|
+
Layer.succeed(Model.ProviderName, "test"),
|
|
977
|
+
Layer.succeed(Model.ModelName, "model"),
|
|
978
|
+
)
|
|
979
|
+
|
|
980
|
+
const decoder = new TextDecoder()
|
|
981
|
+
const decode = (chunk: string | Uint8Array) =>
|
|
982
|
+
typeof chunk === "string" ? chunk : decoder.decode(chunk)
|
|
983
|
+
const stdin = yield* Queue.unbounded<Uint8Array, Cause.Done>()
|
|
984
|
+
const stdout = yield* Queue.unbounded<string>()
|
|
985
|
+
const stdoutLines: Array<string> = []
|
|
986
|
+
// Both the stderr sink and log output reach stderr in the CLI.
|
|
987
|
+
const stderr: Array<string> = []
|
|
988
|
+
const stdio = Stdio.layerTest({
|
|
989
|
+
stdin: Stream.fromQueue(stdin),
|
|
990
|
+
stdout: () =>
|
|
991
|
+
Sink.forEach((chunk) =>
|
|
992
|
+
Effect.sync(() => {
|
|
993
|
+
for (const line of decode(chunk).split("\n")) {
|
|
994
|
+
if (line === "") continue
|
|
995
|
+
stdoutLines.push(line)
|
|
996
|
+
Queue.offerUnsafe(stdout, line)
|
|
997
|
+
}
|
|
998
|
+
}),
|
|
999
|
+
),
|
|
1000
|
+
stderr: () =>
|
|
1001
|
+
Sink.forEach((chunk) =>
|
|
1002
|
+
Effect.sync(() => {
|
|
1003
|
+
stderr.push(decode(chunk))
|
|
1004
|
+
}),
|
|
1005
|
+
),
|
|
1006
|
+
})
|
|
1007
|
+
const logger = Logger.layer([
|
|
1008
|
+
Logger.make((options) => {
|
|
1009
|
+
stderr.push(Logger.formatSimple.log(options))
|
|
1010
|
+
}),
|
|
1011
|
+
])
|
|
1012
|
+
|
|
1013
|
+
const send = (message: object) =>
|
|
1014
|
+
Queue.offer(
|
|
1015
|
+
stdin,
|
|
1016
|
+
new TextEncoder().encode(
|
|
1017
|
+
`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`,
|
|
1018
|
+
),
|
|
1019
|
+
)
|
|
1020
|
+
const response = (id: number) =>
|
|
1021
|
+
Queue.take(stdout).pipe(
|
|
1022
|
+
Effect.map((line): Message => JSON.parse(line)),
|
|
1023
|
+
Effect.repeat({ until: (message) => message.id === id }),
|
|
1024
|
+
)
|
|
1025
|
+
|
|
1026
|
+
const server = yield* Acp.runStdio({
|
|
1027
|
+
version: "test",
|
|
1028
|
+
defaultModel: "test:model",
|
|
1029
|
+
defaultThoughtLevel: "medium",
|
|
1030
|
+
makeAgent: () =>
|
|
1031
|
+
Agent.make.pipe(
|
|
1032
|
+
Effect.provideService(AgentExecutor.AgentExecutor, executor),
|
|
1033
|
+
),
|
|
1034
|
+
makeModel: () =>
|
|
1035
|
+
Option.some(
|
|
1036
|
+
Layer.merge(modelLayer, Agent.layerSubagentModel(modelLayer)),
|
|
1037
|
+
),
|
|
1038
|
+
}).pipe(Effect.provide([stdio, logger]), Effect.forkChild)
|
|
1039
|
+
|
|
1040
|
+
yield* send({ id: 1, method: "session/new", params: { cwd: "/tmp" } })
|
|
1041
|
+
const sessionId: string = (yield* response(1)).result.sessionId
|
|
1042
|
+
yield* send({
|
|
1043
|
+
id: 2,
|
|
1044
|
+
method: "session/prompt",
|
|
1045
|
+
params: { sessionId, prompt: [{ type: "text", text: "hello" }] },
|
|
1046
|
+
})
|
|
1047
|
+
yield* Deferred.await(modelCalled)
|
|
1048
|
+
yield* Queue.end(stdin)
|
|
1049
|
+
yield* Fiber.join(server)
|
|
1050
|
+
|
|
1051
|
+
for (const line of stdoutLines) {
|
|
1052
|
+
assert.strictEqual(JSON.parse(line).jsonrpc, "2.0")
|
|
1053
|
+
}
|
|
1054
|
+
assert.isTrue(
|
|
1055
|
+
stderr.some((line) => line.includes(sessionId)),
|
|
1056
|
+
`expected a stderr diagnostic naming session ${sessionId}, got: ${JSON.stringify(stderr)}`,
|
|
1057
|
+
)
|
|
1058
|
+
}),
|
|
1059
|
+
),
|
|
1060
|
+
)
|
|
955
1061
|
})
|
|
956
1062
|
|
|
957
1063
|
// --- MCP servers ---------------------------------------------------------------
|
package/src/Acp.ts
CHANGED
|
@@ -418,6 +418,19 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
418
418
|
const scope = yield* Effect.scope
|
|
419
419
|
const sessions = new Map<string, Session>()
|
|
420
420
|
|
|
421
|
+
// Whatever ends the server, name the prompts it leaves unfinished. An
|
|
422
|
+
// interrupted prompt keeps its `running` fiber.
|
|
423
|
+
yield* Effect.addFinalizer(() => {
|
|
424
|
+
const running = Array.from(sessions.values())
|
|
425
|
+
.filter((session) => session.running !== undefined)
|
|
426
|
+
.map((session) => session.id)
|
|
427
|
+
return running.length === 0
|
|
428
|
+
? Effect.void
|
|
429
|
+
: Effect.logWarning("ACP server stopping with prompts in progress").pipe(
|
|
430
|
+
Effect.annotateLogs({ sessions: running.join(", ") }),
|
|
431
|
+
)
|
|
432
|
+
})
|
|
433
|
+
|
|
421
434
|
const notify = (method: string, params: object) =>
|
|
422
435
|
options.send({ jsonrpc: "2.0", method, params })
|
|
423
436
|
|
package/src/Agent.test.ts
CHANGED
|
@@ -323,7 +323,7 @@ const toolCall = (id: string, script: string): Response.StreamPartEncoded => ({
|
|
|
323
323
|
|
|
324
324
|
const finish = (
|
|
325
325
|
contextTokens: number,
|
|
326
|
-
reason:
|
|
326
|
+
reason: Response.FinishReason = "tool-calls",
|
|
327
327
|
cache: {
|
|
328
328
|
readonly read?: number | undefined
|
|
329
329
|
readonly write?: number | undefined
|
|
@@ -1335,6 +1335,108 @@ describe("Agent retry limit", () => {
|
|
|
1335
1335
|
)
|
|
1336
1336
|
})
|
|
1337
1337
|
|
|
1338
|
+
const reasoning = (content: string): Array<Response.StreamPartEncoded> => [
|
|
1339
|
+
{ type: "reasoning-start", id: "r" },
|
|
1340
|
+
{ type: "reasoning-delta", id: "r", delta: content },
|
|
1341
|
+
{ type: "reasoning-end", id: "r" },
|
|
1342
|
+
]
|
|
1343
|
+
|
|
1344
|
+
// Conversation mode ends the turn on a response without a tool call, so a
|
|
1345
|
+
// response that never finished or has no answer must be retried instead.
|
|
1346
|
+
describe("Agent incomplete responses", () => {
|
|
1347
|
+
const cases: ReadonlyArray<{
|
|
1348
|
+
readonly name: string
|
|
1349
|
+
readonly response: ReadonlyArray<Response.StreamPartEncoded>
|
|
1350
|
+
readonly reportsError: boolean
|
|
1351
|
+
}> = [
|
|
1352
|
+
{
|
|
1353
|
+
name: "a stream that ends without a finish part",
|
|
1354
|
+
response: reasoning("Checking the parser"),
|
|
1355
|
+
reportsError: true,
|
|
1356
|
+
},
|
|
1357
|
+
{
|
|
1358
|
+
name: "an error response",
|
|
1359
|
+
response: [
|
|
1360
|
+
...reasoning("Checking the parser"),
|
|
1361
|
+
{ type: "error", error: { message: "upstream failed" } },
|
|
1362
|
+
finish(100, "error"),
|
|
1363
|
+
],
|
|
1364
|
+
reportsError: true,
|
|
1365
|
+
},
|
|
1366
|
+
{
|
|
1367
|
+
name: "a finished response with only reasoning",
|
|
1368
|
+
response: [...reasoning("Checking the parser"), finish(100, "stop")],
|
|
1369
|
+
reportsError: false,
|
|
1370
|
+
},
|
|
1371
|
+
]
|
|
1372
|
+
|
|
1373
|
+
for (const { name, response, reportsError } of cases) {
|
|
1374
|
+
it.live(`retries ${name} instead of ending the turn`, () =>
|
|
1375
|
+
Effect.gen(function* () {
|
|
1376
|
+
const { exit, calls, outputs } = yield* runAgentCollect({
|
|
1377
|
+
respond: (_call, index) => {
|
|
1378
|
+
switch (index) {
|
|
1379
|
+
case 0:
|
|
1380
|
+
return Stream.fromIterable(response)
|
|
1381
|
+
case 1:
|
|
1382
|
+
return Stream.fromIterable([
|
|
1383
|
+
...text("the answer"),
|
|
1384
|
+
finish(100, "stop"),
|
|
1385
|
+
])
|
|
1386
|
+
default:
|
|
1387
|
+
return Stream.die(`unexpected model call #${index}`)
|
|
1388
|
+
}
|
|
1389
|
+
},
|
|
1390
|
+
})
|
|
1391
|
+
assert.strictEqual(calls.length, 2, "the model is called again")
|
|
1392
|
+
assert.deepStrictEqual(exit, Exit.succeed("the answer"))
|
|
1393
|
+
if (reportsError) {
|
|
1394
|
+
assert.strictEqual(outputsOfTag(outputs, "ErrorRetry").length, 1)
|
|
1395
|
+
}
|
|
1396
|
+
}),
|
|
1397
|
+
)
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
it.live("keeps a tool result when the response then reports an error", () =>
|
|
1401
|
+
Effect.gen(function* () {
|
|
1402
|
+
let executions = 0
|
|
1403
|
+
const { exit, calls } = yield* runAgentCollect({
|
|
1404
|
+
executor: makeExecutor(() =>
|
|
1405
|
+
Stream.sync(() => {
|
|
1406
|
+
executions++
|
|
1407
|
+
return "wrote notes.txt"
|
|
1408
|
+
}),
|
|
1409
|
+
),
|
|
1410
|
+
respond: (call, index) => {
|
|
1411
|
+
if (index === 0) {
|
|
1412
|
+
return Stream.fromIterable([
|
|
1413
|
+
toolCall("call-1", "await writeFile('notes.txt')"),
|
|
1414
|
+
{ type: "error", error: { message: "upstream failed" } },
|
|
1415
|
+
finish(100),
|
|
1416
|
+
])
|
|
1417
|
+
}
|
|
1418
|
+
if (index > 2) return Stream.die(`unexpected model call #${index}`)
|
|
1419
|
+
// Without the result, a model asks for the same write again.
|
|
1420
|
+
return promptJson(call.prompt).includes("wrote notes.txt")
|
|
1421
|
+
? Stream.fromIterable([...text("the answer"), finish(100, "stop")])
|
|
1422
|
+
: Stream.fromIterable([
|
|
1423
|
+
toolCall(`call-${index + 1}`, "await writeFile('notes.txt')"),
|
|
1424
|
+
finish(100),
|
|
1425
|
+
])
|
|
1426
|
+
},
|
|
1427
|
+
})
|
|
1428
|
+
assert.deepStrictEqual(
|
|
1429
|
+
{
|
|
1430
|
+
executions,
|
|
1431
|
+
resultSent: promptJson(calls[1]!.prompt).includes("wrote notes.txt"),
|
|
1432
|
+
},
|
|
1433
|
+
{ executions: 1, resultSent: true },
|
|
1434
|
+
)
|
|
1435
|
+
assert.deepStrictEqual(exit, Exit.succeed("the answer"))
|
|
1436
|
+
}),
|
|
1437
|
+
)
|
|
1438
|
+
})
|
|
1439
|
+
|
|
1338
1440
|
describe("Agent history events", () => {
|
|
1339
1441
|
it.effect(
|
|
1340
1442
|
"rebuild the agent's history from the history it was resumed with",
|
package/src/Agent.ts
CHANGED
|
@@ -924,15 +924,49 @@ ${Compaction.capOutput(turn.output ?? "").output}
|
|
|
924
924
|
),
|
|
925
925
|
)
|
|
926
926
|
|
|
927
|
+
// Per attempt: the parts received, and what they reported.
|
|
927
928
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
928
929
|
let response = Array.empty<Response.StreamPart<any>>()
|
|
930
|
+
let hadToolCall = false
|
|
931
|
+
let finished = false
|
|
932
|
+
let failure: string | undefined
|
|
929
933
|
const discardAttempt = () => {
|
|
930
934
|
response = []
|
|
931
935
|
pendingImages.length = 0
|
|
936
|
+
hadToolCall = false
|
|
937
|
+
finished = false
|
|
938
|
+
failure = undefined
|
|
932
939
|
}
|
|
933
940
|
let reasoningStarted = false
|
|
934
941
|
let hadReasoningDelta = false
|
|
935
|
-
|
|
942
|
+
// A response that would end the turn must be an answer. One that
|
|
943
|
+
// failed, or has nothing to return, takes the retry path instead.
|
|
944
|
+
// Once a tool call is in the response its tool may have run, so the
|
|
945
|
+
// response is kept and the loop continues with the result.
|
|
946
|
+
const checkResponse = () => {
|
|
947
|
+
if (failure !== undefined && !hadToolCall) {
|
|
948
|
+
return Effect.fail(
|
|
949
|
+
incompleteResponseError(`The model response failed: ${failure}`),
|
|
950
|
+
)
|
|
951
|
+
}
|
|
952
|
+
// The condition that ends the turn after the model call below.
|
|
953
|
+
const endsTurn =
|
|
954
|
+
conversationMode &&
|
|
955
|
+
!hadToolCall &&
|
|
956
|
+
mailbox.messages.size === 0 &&
|
|
957
|
+
runningChildren().length === 0 &&
|
|
958
|
+
!hasUndelivered()
|
|
959
|
+
if (!endsTurn || responseToSummary(response).trim() !== "") {
|
|
960
|
+
return Effect.void
|
|
961
|
+
}
|
|
962
|
+
return Effect.fail(
|
|
963
|
+
incompleteResponseError(
|
|
964
|
+
finished
|
|
965
|
+
? "The model response had no text and no tool call"
|
|
966
|
+
: "The model response ended before it finished",
|
|
967
|
+
),
|
|
968
|
+
)
|
|
969
|
+
}
|
|
936
970
|
// Retries of this model call so far, and the error that used up the
|
|
937
971
|
// last one.
|
|
938
972
|
let retries = 0
|
|
@@ -1003,6 +1037,10 @@ ${Compaction.capOutput(turn.output ?? "").output}
|
|
|
1003
1037
|
}
|
|
1004
1038
|
break
|
|
1005
1039
|
case "finish":
|
|
1040
|
+
finished = true
|
|
1041
|
+
if (part.reason === "error") {
|
|
1042
|
+
failure ??= "finish reason error"
|
|
1043
|
+
}
|
|
1006
1044
|
const usage = part.usage
|
|
1007
1045
|
if (usage.outputTokens.total !== undefined) {
|
|
1008
1046
|
outputTokens += usage.outputTokens.total
|
|
@@ -1031,10 +1069,14 @@ ${Compaction.capOutput(turn.output ?? "").output}
|
|
|
1031
1069
|
case "tool-call":
|
|
1032
1070
|
hadToolCall = true
|
|
1033
1071
|
break
|
|
1072
|
+
case "error":
|
|
1073
|
+
failure ??= errorMessage(part.error)
|
|
1074
|
+
break
|
|
1034
1075
|
}
|
|
1035
1076
|
}
|
|
1036
1077
|
return Effect.void
|
|
1037
1078
|
}),
|
|
1079
|
+
Effect.andThen(checkResponse),
|
|
1038
1080
|
modelConfig.systemPromptTransform
|
|
1039
1081
|
? (effect) => modelConfig.systemPromptTransform!(system, effect)
|
|
1040
1082
|
: identity,
|
|
@@ -1263,6 +1305,13 @@ const turnTimeoutError = AiError.make({
|
|
|
1263
1305
|
}),
|
|
1264
1306
|
})
|
|
1265
1307
|
|
|
1308
|
+
const incompleteResponseError = (description: string) =>
|
|
1309
|
+
AiError.make({
|
|
1310
|
+
module: "clanka/Agent",
|
|
1311
|
+
method: "send",
|
|
1312
|
+
reason: new AiError.InternalProviderError({ description }),
|
|
1313
|
+
})
|
|
1314
|
+
|
|
1266
1315
|
const retryPolicy = Schedule.min([
|
|
1267
1316
|
Schedule.exponential(100, 1.5),
|
|
1268
1317
|
Schedule.spaced(5000),
|