clanka 0.12.1 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "clanka",
3
3
  "type": "module",
4
- "version": "0.12.1",
4
+ "version": "0.12.2",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
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: "stop" | "tool-calls" = "tool-calls",
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
- let hadToolCall = false
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),