clanka 0.12.1 → 0.12.3

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.3",
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,126 @@ 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 failed must be retried instead.
1346
+ describe("Agent incomplete responses", () => {
1347
+ const cases: ReadonlyArray<{
1348
+ readonly name: string
1349
+ readonly response: ReadonlyArray<Response.StreamPartEncoded>
1350
+ }> = [
1351
+ {
1352
+ name: "a stream that ends without a finish part",
1353
+ response: reasoning("Checking the parser"),
1354
+ },
1355
+ {
1356
+ name: "an error response",
1357
+ response: [
1358
+ ...reasoning("Checking the parser"),
1359
+ { type: "error", error: { message: "upstream failed" } },
1360
+ finish(100, "error"),
1361
+ ],
1362
+ },
1363
+ ]
1364
+
1365
+ for (const { name, response } of cases) {
1366
+ it.live(`retries ${name} instead of ending the turn`, () =>
1367
+ Effect.gen(function* () {
1368
+ const { exit, calls, outputs } = yield* runAgentCollect({
1369
+ respond: (_call, index) => {
1370
+ switch (index) {
1371
+ case 0:
1372
+ return Stream.fromIterable(response)
1373
+ case 1:
1374
+ return Stream.fromIterable([
1375
+ ...text("the answer"),
1376
+ finish(100, "stop"),
1377
+ ])
1378
+ default:
1379
+ return Stream.die(`unexpected model call #${index}`)
1380
+ }
1381
+ },
1382
+ })
1383
+ assert.strictEqual(calls.length, 2, "the model is called again")
1384
+ assert.deepStrictEqual(exit, Exit.succeed("the answer"))
1385
+ assert.strictEqual(outputsOfTag(outputs, "ErrorRetry").length, 1)
1386
+ }),
1387
+ )
1388
+ }
1389
+
1390
+ // An empty response is neither an answer nor a failure, so the loop starts
1391
+ // another turn without reporting an error.
1392
+ it.live("starts another turn after a finished response with no text", () =>
1393
+ Effect.gen(function* () {
1394
+ const { exit, calls, outputs } = yield* runAgentCollect({
1395
+ respond: (_call, index) => {
1396
+ switch (index) {
1397
+ case 0:
1398
+ return Stream.fromIterable([
1399
+ ...reasoning("Nothing to do"),
1400
+ finish(100, "stop"),
1401
+ ])
1402
+ case 1:
1403
+ return Stream.fromIterable([
1404
+ ...text("the answer"),
1405
+ finish(100, "stop"),
1406
+ ])
1407
+ default:
1408
+ return Stream.die(`unexpected model call #${index}`)
1409
+ }
1410
+ },
1411
+ })
1412
+ assert.strictEqual(calls.length, 2, "the model is called again")
1413
+ assert.deepStrictEqual(exit, Exit.succeed("the answer"))
1414
+ assert.strictEqual(outputsOfTag(outputs, "ErrorRetry").length, 0)
1415
+ }),
1416
+ )
1417
+
1418
+ it.live("keeps a tool result when the response then reports an error", () =>
1419
+ Effect.gen(function* () {
1420
+ let executions = 0
1421
+ const { exit, calls } = yield* runAgentCollect({
1422
+ executor: makeExecutor(() =>
1423
+ Stream.sync(() => {
1424
+ executions++
1425
+ return "wrote notes.txt"
1426
+ }),
1427
+ ),
1428
+ respond: (call, index) => {
1429
+ if (index === 0) {
1430
+ return Stream.fromIterable([
1431
+ toolCall("call-1", "await writeFile('notes.txt')"),
1432
+ { type: "error", error: { message: "upstream failed" } },
1433
+ finish(100),
1434
+ ])
1435
+ }
1436
+ if (index > 2) return Stream.die(`unexpected model call #${index}`)
1437
+ // Without the result, a model asks for the same write again.
1438
+ return promptJson(call.prompt).includes("wrote notes.txt")
1439
+ ? Stream.fromIterable([...text("the answer"), finish(100, "stop")])
1440
+ : Stream.fromIterable([
1441
+ toolCall(`call-${index + 1}`, "await writeFile('notes.txt')"),
1442
+ finish(100),
1443
+ ])
1444
+ },
1445
+ })
1446
+ assert.deepStrictEqual(
1447
+ {
1448
+ executions,
1449
+ resultSent: promptJson(calls[1]!.prompt).includes("wrote notes.txt"),
1450
+ },
1451
+ { executions: 1, resultSent: true },
1452
+ )
1453
+ assert.deepStrictEqual(exit, Exit.succeed("the answer"))
1454
+ }),
1455
+ )
1456
+ })
1457
+
1338
1458
  describe("Agent history events", () => {
1339
1459
  it.effect(
1340
1460
  "rebuild the agent's history from the history it was resumed with",
package/src/Agent.ts CHANGED
@@ -924,15 +924,53 @@ ${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 complete. One that
943
+ // failed, or stopped before it finished with nothing to return, takes
944
+ // the retry path instead. A finished response with no text starts
945
+ // another turn below.
946
+ // Once a tool call is in the response its tool may have run, so the
947
+ // response is kept and the loop continues with the result.
948
+ const checkResponse = () => {
949
+ if (failure !== undefined && !hadToolCall) {
950
+ return Effect.fail(
951
+ incompleteResponseError(`The model response failed: ${failure}`),
952
+ )
953
+ }
954
+ // The condition that ends the turn after the model call below.
955
+ const endsTurn =
956
+ conversationMode &&
957
+ !hadToolCall &&
958
+ mailbox.messages.size === 0 &&
959
+ runningChildren().length === 0 &&
960
+ !hasUndelivered()
961
+ if (
962
+ !endsTurn ||
963
+ finished ||
964
+ responseToSummary(response).trim() !== ""
965
+ ) {
966
+ return Effect.void
967
+ }
968
+ return Effect.fail(
969
+ incompleteResponseError(
970
+ "The model response ended before it finished",
971
+ ),
972
+ )
973
+ }
936
974
  // Retries of this model call so far, and the error that used up the
937
975
  // last one.
938
976
  let retries = 0
@@ -1003,6 +1041,10 @@ ${Compaction.capOutput(turn.output ?? "").output}
1003
1041
  }
1004
1042
  break
1005
1043
  case "finish":
1044
+ finished = true
1045
+ if (part.reason === "error") {
1046
+ failure ??= "finish reason error"
1047
+ }
1006
1048
  const usage = part.usage
1007
1049
  if (usage.outputTokens.total !== undefined) {
1008
1050
  outputTokens += usage.outputTokens.total
@@ -1031,10 +1073,14 @@ ${Compaction.capOutput(turn.output ?? "").output}
1031
1073
  case "tool-call":
1032
1074
  hadToolCall = true
1033
1075
  break
1076
+ case "error":
1077
+ failure ??= errorMessage(part.error)
1078
+ break
1034
1079
  }
1035
1080
  }
1036
1081
  return Effect.void
1037
1082
  }),
1083
+ Effect.andThen(checkResponse),
1038
1084
  modelConfig.systemPromptTransform
1039
1085
  ? (effect) => modelConfig.systemPromptTransform!(system, effect)
1040
1086
  : identity,
@@ -1146,7 +1192,10 @@ ${Compaction.capOutput(turn.output ?? "").output}
1146
1192
  yield* waitForActivity
1147
1193
  continue
1148
1194
  }
1149
- finalSummary = Option.some(responseToSummary(response))
1195
+ const summary = responseToSummary(response)
1196
+ // An empty response is not an answer, so ask the model again.
1197
+ if (summary.trim() === "") continue
1198
+ finalSummary = Option.some(summary)
1150
1199
  }
1151
1200
  }
1152
1201
  }).pipe(
@@ -1263,6 +1312,13 @@ const turnTimeoutError = AiError.make({
1263
1312
  }),
1264
1313
  })
1265
1314
 
1315
+ const incompleteResponseError = (description: string) =>
1316
+ AiError.make({
1317
+ module: "clanka/Agent",
1318
+ method: "send",
1319
+ reason: new AiError.InternalProviderError({ description }),
1320
+ })
1321
+
1266
1322
  const retryPolicy = Schedule.min([
1267
1323
  Schedule.exponential(100, 1.5),
1268
1324
  Schedule.spaced(5000),