clanka 0.8.5 → 0.9.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.
Files changed (92) hide show
  1. package/dist/Acp.d.ts.map +1 -1
  2. package/dist/Acp.js +9 -3
  3. package/dist/Acp.js.map +1 -1
  4. package/dist/Acp.test.js +72 -1
  5. package/dist/Acp.test.js.map +1 -1
  6. package/dist/AcpImage.test.js +1 -0
  7. package/dist/AcpImage.test.js.map +1 -1
  8. package/dist/Agent.d.ts +65 -8
  9. package/dist/Agent.d.ts.map +1 -1
  10. package/dist/Agent.js +481 -113
  11. package/dist/Agent.js.map +1 -1
  12. package/dist/Agent.test.js +77 -4
  13. package/dist/Agent.test.js.map +1 -1
  14. package/dist/AgentCallerConfig.test.d.ts +2 -0
  15. package/dist/AgentCallerConfig.test.d.ts.map +1 -0
  16. package/dist/AgentCallerConfig.test.js +327 -0
  17. package/dist/AgentCallerConfig.test.js.map +1 -0
  18. package/dist/AgentExecutor.d.ts +254 -54
  19. package/dist/AgentExecutor.d.ts.map +1 -1
  20. package/dist/AgentExecutor.js +416 -99
  21. package/dist/AgentExecutor.js.map +1 -1
  22. package/dist/AgentExecutor.test.js +201 -31
  23. package/dist/AgentExecutor.test.js.map +1 -1
  24. package/dist/AgentExecutorImage.test.js +2 -1
  25. package/dist/AgentExecutorImage.test.js.map +1 -1
  26. package/dist/AgentImage.test.js +5 -1
  27. package/dist/AgentImage.test.js.map +1 -1
  28. package/dist/AgentOutput.d.ts +54 -2
  29. package/dist/AgentOutput.d.ts.map +1 -1
  30. package/dist/AgentOutput.js +52 -0
  31. package/dist/AgentOutput.js.map +1 -1
  32. package/dist/AgentSkills.d.ts +22 -0
  33. package/dist/AgentSkills.d.ts.map +1 -1
  34. package/dist/AgentSkills.js +36 -0
  35. package/dist/AgentSkills.js.map +1 -1
  36. package/dist/AgentSkills.test.js +2 -0
  37. package/dist/AgentSkills.test.js.map +1 -1
  38. package/dist/AgentSubagents.test.d.ts +2 -0
  39. package/dist/AgentSubagents.test.d.ts.map +1 -0
  40. package/dist/AgentSubagents.test.js +452 -0
  41. package/dist/AgentSubagents.test.js.map +1 -0
  42. package/dist/AgentTools.d.ts +356 -14
  43. package/dist/AgentTools.d.ts.map +1 -1
  44. package/dist/AgentTools.js +213 -34
  45. package/dist/AgentTools.js.map +1 -1
  46. package/dist/CompactionTransport.test.js +1 -1
  47. package/dist/CompactionTransport.test.js.map +1 -1
  48. package/dist/OutputFormatter.d.ts.map +1 -1
  49. package/dist/OutputFormatter.js +16 -2
  50. package/dist/OutputFormatter.js.map +1 -1
  51. package/dist/ToolkitRenderer.d.ts.map +1 -1
  52. package/dist/ToolkitRenderer.js +6 -1
  53. package/dist/ToolkitRenderer.js.map +1 -1
  54. package/dist/bin/{Effect-BEUn4I5E.mjs → Effect-2jPlJ8eu.mjs} +399 -207
  55. package/dist/bin/Effect-2jPlJ8eu.mjs.map +1 -0
  56. package/dist/bin/{McpClient-C0FVuDj2.mjs → McpClient-1W508adc.mjs} +4 -4
  57. package/dist/bin/{McpClient-C0FVuDj2.mjs.map → McpClient-1W508adc.mjs.map} +1 -1
  58. package/dist/bin/{OutputFormatter-DffD5oxX.mjs → OutputFormatter-DXXKo4E8.mjs} +16 -5
  59. package/dist/bin/{OutputFormatter-DffD5oxX.mjs.map → OutputFormatter-DXXKo4E8.mjs.map} +1 -1
  60. package/dist/bin/{Schema-CjUGw2Xh.mjs → Schema-D-7VuCiC.mjs} +640 -87
  61. package/dist/bin/Schema-D-7VuCiC.mjs.map +1 -0
  62. package/dist/bin/{SemanticSearch-CFB73IPb.mjs → SemanticSearch-Cb8aqvGv.mjs} +89 -56
  63. package/dist/bin/SemanticSearch-Cb8aqvGv.mjs.map +1 -0
  64. package/dist/bin/{Stream-Cu6P7ZHo.mjs → Stream-DEu2tfCV.mjs} +81 -51
  65. package/dist/bin/Stream-DEu2tfCV.mjs.map +1 -0
  66. package/dist/bin/cli.mjs +3632 -5538
  67. package/dist/bin/cli.mjs.map +1 -1
  68. package/dist/bin/{turndown.cjs-usmCKZ4b.mjs → turndown.cjs-DK29wZw3.mjs} +2 -2
  69. package/dist/bin/{turndown.cjs-usmCKZ4b.mjs.map → turndown.cjs-DK29wZw3.mjs.map} +1 -1
  70. package/package.json +7 -7
  71. package/src/Acp.test.ts +121 -2
  72. package/src/Acp.ts +10 -3
  73. package/src/AcpImage.test.ts +1 -0
  74. package/src/Agent.test.ts +100 -3
  75. package/src/Agent.ts +752 -142
  76. package/src/AgentCallerConfig.test.ts +514 -0
  77. package/src/AgentExecutor.test.ts +297 -37
  78. package/src/AgentExecutor.ts +707 -159
  79. package/src/AgentExecutorImage.test.ts +2 -1
  80. package/src/AgentImage.test.ts +11 -2
  81. package/src/AgentOutput.ts +74 -1
  82. package/src/AgentSkills.test.ts +2 -0
  83. package/src/AgentSkills.ts +43 -0
  84. package/src/AgentSubagents.test.ts +758 -0
  85. package/src/AgentTools.ts +376 -52
  86. package/src/CompactionTransport.test.ts +1 -1
  87. package/src/OutputFormatter.ts +16 -2
  88. package/src/ToolkitRenderer.ts +7 -1
  89. package/dist/bin/Effect-BEUn4I5E.mjs.map +0 -1
  90. package/dist/bin/Schema-CjUGw2Xh.mjs.map +0 -1
  91. package/dist/bin/SemanticSearch-CFB73IPb.mjs.map +0 -1
  92. package/dist/bin/Stream-Cu6P7ZHo.mjs.map +0 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "clanka",
3
3
  "type": "module",
4
- "version": "0.8.5",
4
+ "version": "0.9.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -46,15 +46,15 @@
46
46
  "devDependencies": {
47
47
  "@changesets/changelog-github": "^1.0.1",
48
48
  "@changesets/cli": "^3.0.3",
49
- "@effect/ai-openai": "4.0.1",
50
- "@effect/ai-openai-compat": "4.0.1",
51
- "@effect/platform-node": "4.0.0",
52
- "@effect/sql-sqlite-node": "4.0.1",
49
+ "@effect/ai-openai": "4.0.4",
50
+ "@effect/ai-openai-compat": "4.0.4",
51
+ "@effect/platform-node": "4.0.4",
52
+ "@effect/sql-sqlite-node": "4.0.4",
53
53
  "@effect/tsgo": "0.48.0",
54
- "@effect/vitest": "4.0.1",
54
+ "@effect/vitest": "4.0.4",
55
55
  "@types/node": "^26.6.4",
56
56
  "@types/turndown": "^5.0.6",
57
- "effect": "4.0.1",
57
+ "effect": "4.0.4",
58
58
  "husky": "^9.1.7",
59
59
  "lint-staged": "^17.6.0",
60
60
  "oxlint": "^1.86.0",
package/src/Acp.test.ts CHANGED
@@ -26,6 +26,7 @@ const capabilities = new AgentExecutor.Capabilities({
26
26
  toolsDts: "",
27
27
  agentsMd: Option.none(),
28
28
  supportsSearch: false,
29
+ tools: [],
29
30
  skills: [],
30
31
  })
31
32
 
@@ -425,8 +426,13 @@ describe("Acp", () => {
425
426
  : assistantSays(summary),
426
427
  {
427
428
  ...executor,
428
- execute: ({ onSubagent }) =>
429
- Stream.fromEffect(onSubagent(prompt)),
429
+ execute: ({ subagents }) =>
430
+ Stream.fromEffect(
431
+ subagents.delegate({ prompt, background: false }),
432
+ ).pipe(
433
+ Stream.map((result) => result.output ?? ""),
434
+ Stream.orDie,
435
+ ),
430
436
  },
431
437
  )
432
438
  const created = yield* server.request(1, "session/new", {
@@ -468,6 +474,119 @@ describe("Acp", () => {
468
474
  ),
469
475
  )
470
476
 
477
+ it.effect(
478
+ "resuming a finished subagent does not create its tool call twice",
479
+ () =>
480
+ withStore(
481
+ Effect.scoped(
482
+ Effect.gen(function* () {
483
+ let childId: string | undefined
484
+ let executions = 0
485
+ const server = yield* makeServer(
486
+ ({ prompt }) => {
487
+ if (JSON.stringify(prompt.content).includes("TASK-CHILD")) {
488
+ const resumed = JSON.stringify(prompt.content).includes(
489
+ "FOLLOW-UP",
490
+ )
491
+ return assistantSays(
492
+ resumed ? "SECOND-RESULT" : "FIRST-RESULT",
493
+ )
494
+ }
495
+ const seen = JSON.stringify(prompt.content)
496
+ if (seen.includes("SECOND-RESULT")) return assistantSays("done")
497
+ if (
498
+ executions === 0 ||
499
+ (executions === 1 && seen.includes("FIRST-RESULT"))
500
+ ) {
501
+ return parts({
502
+ type: "tool-call",
503
+ id: `call-${executions}`,
504
+ name: "execute",
505
+ params: { script: "await work()" },
506
+ })
507
+ }
508
+ return assistantSays("waiting")
509
+ },
510
+ {
511
+ ...executor,
512
+ // The first script starts a background child; the second
513
+ // resumes it once its first result has been delivered.
514
+ execute: ({ subagents }) =>
515
+ Stream.fromEffect(
516
+ executions++ === 0
517
+ ? subagents
518
+ .delegate({ prompt: "TASK-CHILD", background: true })
519
+ .pipe(
520
+ Effect.map((result) => {
521
+ childId = result.id
522
+ return "started"
523
+ }),
524
+ )
525
+ : subagents
526
+ .messageAgent({ id: childId!, message: "FOLLOW-UP" })
527
+ .pipe(Effect.as("resumed")),
528
+ ).pipe(Stream.orDie),
529
+ },
530
+ )
531
+ const created = yield* server.request(1, "session/new", {
532
+ cwd: "/tmp",
533
+ })
534
+ const sessionId: string = created.result.sessionId
535
+ yield* server.request(2, "session/prompt", {
536
+ sessionId,
537
+ prompt: [{ type: "text", text: "delegate and resume" }],
538
+ })
539
+
540
+ assert.strictEqual(executions, 2, "the child was resumed")
541
+ const tools = server
542
+ .updates(sessionId)
543
+ .filter(
544
+ (u) =>
545
+ u.sessionUpdate.startsWith("tool_call") &&
546
+ u.toolCallId.startsWith("subagent-"),
547
+ )
548
+ const creations = tools
549
+ .filter((u) => u.sessionUpdate === "tool_call")
550
+ .map((u) => u.toolCallId)
551
+ assert.deepStrictEqual(
552
+ creations,
553
+ [...new Set(creations)],
554
+ "ACP tool call IDs must be unique within the session",
555
+ )
556
+ for (const u of tools) {
557
+ assert.include(
558
+ creations,
559
+ u.toolCallId,
560
+ "updates refer to a created tool call",
561
+ )
562
+ }
563
+
564
+ const firstDone = tools.findIndex(
565
+ (u) => u.status === "completed" && u.rawOutput === "FIRST-RESULT",
566
+ )
567
+ const secondDone = tools.findIndex(
568
+ (u) =>
569
+ u.status === "completed" && u.rawOutput === "SECOND-RESULT",
570
+ )
571
+ assert.isAbove(firstDone, -1, "first result shown")
572
+ assert.isAbove(secondDone, firstDone, "second result shown")
573
+ assert.isTrue(
574
+ tools
575
+ .slice(firstDone + 1, secondDone)
576
+ .some((u) => u.status === "in_progress"),
577
+ "the resumed turn is shown as running",
578
+ )
579
+ assert.deepStrictEqual(tools[secondDone].content, [
580
+ {
581
+ type: "content",
582
+ content: { type: "text", text: "SECOND-RESULT" },
583
+ },
584
+ ])
585
+ }),
586
+ ),
587
+ ),
588
+ )
589
+
471
590
  it.effect("reports canonical state for new and loaded sessions", () =>
472
591
  withStore(
473
592
  Effect.gen(function* () {
package/src/Acp.ts CHANGED
@@ -297,6 +297,7 @@ interface Session {
297
297
  readonly agent: Agent.Agent
298
298
  running: Fiber.Fiber<void, unknown> | undefined
299
299
  toolCalls: number
300
+ readonly announcedSubagents: Set<number>
300
301
  }
301
302
 
302
303
  /**
@@ -394,6 +395,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
394
395
  agent,
395
396
  running: undefined,
396
397
  toolCalls: 0,
398
+ announcedSubagents: new Set(),
397
399
  }
398
400
  sessions.set(id, session)
399
401
  return session
@@ -669,20 +671,25 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
669
671
  `Retrying after error: ${part.error.message}\n`,
670
672
  ),
671
673
  })
672
- case "SubagentStart":
674
+ // A background agent's tool call stays in progress until the agent
675
+ // finishes, while the parent keeps taking turns.
676
+ case "SubagentStart": {
677
+ const announced = session.announcedSubagents.has(part.id)
678
+ session.announcedSubagents.add(part.id)
673
679
  return update(session.id, {
674
- sessionUpdate: "tool_call",
680
+ sessionUpdate: announced ? "tool_call_update" : "tool_call",
675
681
  toolCallId: `subagent-${part.id}`,
676
682
  title: `delegate: ${part.prompt.replace(/\s+/g, " ").trim().slice(0, 120)}`,
677
683
  kind: "think",
678
684
  status: "in_progress",
679
685
  rawInput: { prompt: part.prompt },
680
686
  })
687
+ }
681
688
  case "SubagentComplete":
682
689
  return update(session.id, {
683
690
  sessionUpdate: "tool_call_update",
684
691
  toolCallId: `subagent-${part.id}`,
685
- status: "completed",
692
+ status: part.status === "completed" ? "completed" : "failed",
686
693
  rawOutput: part.summary,
687
694
  content: [{ type: "content", content: textContent(part.summary) }],
688
695
  })
@@ -42,6 +42,7 @@ const capabilities = new AgentExecutor.Capabilities({
42
42
  toolsDts: "",
43
43
  agentsMd: Option.none(),
44
44
  supportsSearch: false,
45
+ tools: [],
45
46
  skills: [],
46
47
  })
47
48
 
package/src/Agent.test.ts CHANGED
@@ -18,6 +18,7 @@ import type * as Response from "effect/ai/Response"
18
18
  import * as ResponseIdTracker from "effect/ai/ResponseIdTracker"
19
19
  import * as Agent from "./Agent.ts"
20
20
  import * as AgentExecutor from "./AgentExecutor.ts"
21
+ import { subagentsUnavailable } from "./AgentTools.ts"
21
22
  import * as AgentOutput from "./AgentOutput.ts"
22
23
  import * as Compaction from "./Compaction.ts"
23
24
 
@@ -25,6 +26,7 @@ const capabilities = new AgentExecutor.Capabilities({
25
26
  toolsDts: "",
26
27
  agentsMd: Option.none(),
27
28
  supportsSearch: false,
29
+ tools: [],
28
30
  skills: [],
29
31
  })
30
32
 
@@ -114,7 +116,10 @@ describe("Agent", () => {
114
116
  conversationMode: true,
115
117
  executor: makeExecutor(({ onTaskComplete }) =>
116
118
  Stream.fromEffect(
117
- onTaskComplete("done from taskComplete").pipe(Effect.as("")),
119
+ onTaskComplete("done from taskComplete").pipe(
120
+ Effect.as(""),
121
+ Effect.orDie,
122
+ ),
118
123
  ),
119
124
  ),
120
125
  streamText: () =>
@@ -162,6 +167,7 @@ const runAgentCollect = (options: {
162
167
  readonly compaction?: Partial<Compaction.CompactionConfigService> | undefined
163
168
  readonly prompt?: string | undefined
164
169
  readonly turnTimeout?: Duration.Duration | undefined
170
+ readonly maxRetries?: number | undefined
165
171
  readonly respond: ScriptedResponse
166
172
  }) =>
167
173
  Effect.scoped(
@@ -243,6 +249,9 @@ const runAgentCollect = (options: {
243
249
  Agent.TurnTimeout,
244
250
  options.turnTimeout ?? Duration.minutes(5),
245
251
  ),
252
+ options.maxRetries === undefined
253
+ ? Layer.empty
254
+ : Agent.MaxRetries.layer(options.maxRetries),
246
255
  ),
247
256
  ),
248
257
  Effect.exit,
@@ -474,7 +483,7 @@ it.layer(localExecutorLayer, { excludeTestServices: true })(
474
483
  Effect.sync(() => {
475
484
  summaries.push(summary)
476
485
  }),
477
- onSubagent: () => Effect.succeed(""),
486
+ subagents: subagentsUnavailable,
478
487
  onImage: () => Effect.void,
479
488
  })
480
489
  .pipe(Stream.mkString)
@@ -1164,7 +1173,7 @@ describe("Agent overflow compaction", () => {
1164
1173
  })
1165
1174
  assert.isTrue(Exit.isFailure(exit), "turn must fail")
1166
1175
  const error = Option.getOrThrow(Exit.findErrorOption(exit))
1167
- assert.strictEqual(error._tag, "AiError")
1176
+ assert(error._tag === "AiError")
1168
1177
  assert.strictEqual(error.reason._tag, "InvalidRequestError")
1169
1178
  assert.strictEqual(calls.length, 3)
1170
1179
  }),
@@ -1285,3 +1294,91 @@ describe("Agent overflow compaction", () => {
1285
1294
  }),
1286
1295
  )
1287
1296
  })
1297
+
1298
+ describe("Agent retry limit", () => {
1299
+ it.live(
1300
+ "fails with RetriesExhausted once model errors and turn timeouts reach MaxRetries",
1301
+ () =>
1302
+ Effect.gen(function* () {
1303
+ const { exit, calls } = yield* runAgentCollect({
1304
+ maxRetries: 2,
1305
+ turnTimeout: Duration.millis(100),
1306
+ respond: (_call, index) => {
1307
+ switch (index) {
1308
+ case 0:
1309
+ return Stream.fail(providerError)
1310
+ case 1:
1311
+ return Stream.fromEffect(Effect.never)
1312
+ case 2:
1313
+ return Stream.fail(providerError)
1314
+ default:
1315
+ return Stream.die(
1316
+ `unexpected model call #${index}: must stop retrying`,
1317
+ )
1318
+ }
1319
+ },
1320
+ }).pipe(Effect.timeout("3 seconds"))
1321
+ assert.strictEqual(calls.length, 3)
1322
+ const error = Option.getOrThrow(Exit.findErrorOption(exit))
1323
+ assert(error._tag === "RetriesExhausted")
1324
+ assert.strictEqual(error.retries, 2)
1325
+ assert.strictEqual(error.error.reason._tag, "InternalProviderError")
1326
+ }),
1327
+ )
1328
+ })
1329
+
1330
+ describe("Agent history events", () => {
1331
+ it.effect(
1332
+ "rebuild the agent's history from the history it was resumed with",
1333
+ () =>
1334
+ Effect.gen(function* () {
1335
+ const { exit, outputs, history } = yield* runAgentCollect({
1336
+ history: fatHistory,
1337
+ compaction: {
1338
+ contextWindow: 12_000,
1339
+ reserveTokens: 2_000,
1340
+ keepRecentTokens: 9_000,
1341
+ },
1342
+ executor: makeExecutor(() => Stream.succeed("listed")),
1343
+ respond: (call, index) => {
1344
+ switch (index) {
1345
+ case 0:
1346
+ assertSummarizerCall(call)
1347
+ return Stream.fromIterable(text("SUMMARY"))
1348
+ case 1:
1349
+ return Stream.fromIterable([
1350
+ toolCall("call-3", "console.log(await ls('.'))"),
1351
+ finish(500),
1352
+ ])
1353
+ case 2:
1354
+ return Stream.fromIterable([
1355
+ ...text("done"),
1356
+ finish(600, "stop"),
1357
+ ])
1358
+ default:
1359
+ return Stream.die(`unexpected model call #${index}`)
1360
+ }
1361
+ },
1362
+ })
1363
+ assert.deepStrictEqual(exit, Exit.succeed("done"))
1364
+
1365
+ let replayed: Array<Prompt.Message> = [...fatHistory.content]
1366
+ for (const output of outputs) {
1367
+ if (output._tag === "HistoryAppended") {
1368
+ replayed.push(...output.messages)
1369
+ } else if (output._tag === "HistoryReplaced") {
1370
+ replayed = [...output.messages]
1371
+ }
1372
+ }
1373
+ assert.strictEqual(outputsOfTag(outputs, "HistoryReplaced").length, 1)
1374
+ assert.strictEqual(
1375
+ promptJson(Prompt.fromMessages(replayed)),
1376
+ promptJson(
1377
+ Prompt.fromMessages(
1378
+ history.content.filter((message) => message.role !== "system"),
1379
+ ),
1380
+ ),
1381
+ )
1382
+ }),
1383
+ )
1384
+ })