@zvada/agent-server 0.3.22 → 0.3.23

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 (348) hide show
  1. package/CHANGELOG.md +261 -0
  2. package/dist/types/accounts/chatgpt.d.ts +1 -1
  3. package/dist/types/accounts/chatgpt.d.ts.map +1 -1
  4. package/dist/types/accounts/http.d.ts +2 -4
  5. package/dist/types/accounts/http.d.ts.map +1 -1
  6. package/dist/types/client/client.d.ts +37 -11
  7. package/dist/types/client/client.d.ts.map +1 -1
  8. package/dist/types/client/connection.d.ts +64 -13
  9. package/dist/types/client/connection.d.ts.map +1 -1
  10. package/dist/types/client/index.d.ts +1 -1
  11. package/dist/types/client/index.d.ts.map +1 -1
  12. package/dist/types/client/logins.d.ts +2 -1
  13. package/dist/types/client/logins.d.ts.map +1 -1
  14. package/dist/types/client/streams.d.ts +9 -3
  15. package/dist/types/client/streams.d.ts.map +1 -1
  16. package/dist/types/client/turns.d.ts +1 -1
  17. package/dist/types/client/turns.d.ts.map +1 -1
  18. package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
  19. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  20. package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
  21. package/dist/types/core/agents/acp/client.d.ts +12 -3
  22. package/dist/types/core/agents/acp/client.d.ts.map +1 -1
  23. package/dist/types/core/agents/acp/mappings.d.ts +8 -2
  24. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  25. package/dist/types/core/agents/base.d.ts +22 -8
  26. package/dist/types/core/agents/base.d.ts.map +1 -1
  27. package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
  28. package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
  29. package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
  30. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  31. package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
  32. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  33. package/dist/types/core/agents/claude-code/events.d.ts +9 -1
  34. package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
  35. package/dist/types/core/agents/claude-code/login.d.ts +4 -1
  36. package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
  37. package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
  38. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  39. package/dist/types/core/agents/claude-code/models.d.ts +5 -1
  40. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  41. package/dist/types/core/agents/claude-code/options.d.ts +10 -4
  42. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  43. package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
  44. package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
  45. package/dist/types/core/agents/claude-code/results.d.ts +16 -0
  46. package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
  47. package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
  48. package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
  49. package/dist/types/core/agents/claude-code/session.d.ts +50 -12
  50. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  51. package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
  52. package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
  53. package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
  54. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  55. package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
  56. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  57. package/dist/types/core/agents/codex-app-server/client.d.ts +12 -2
  58. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  59. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +58 -13
  60. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  61. package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
  62. package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
  63. package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
  64. package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
  65. package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
  66. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  67. package/dist/types/core/agents/codex-app-server/homes.d.ts +19 -0
  68. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  69. package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
  70. package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
  71. package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
  72. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
  73. package/dist/types/core/agents/codex-app-server/mcp.d.ts +15 -30
  74. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  75. package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
  76. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  77. package/dist/types/core/agents/codex-app-server/options.d.ts +27 -4
  78. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  79. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
  80. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
  81. package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
  82. package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
  83. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  84. package/dist/types/core/agents/codex-app-server/session.d.ts +68 -40
  85. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  86. package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
  87. package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
  88. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
  89. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  90. package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
  91. package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
  92. package/dist/types/core/agents/codex-app-server/turn.d.ts +31 -8
  93. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
  94. package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
  95. package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
  96. package/dist/types/core/agents/credentials.d.ts +3 -1
  97. package/dist/types/core/agents/credentials.d.ts.map +1 -1
  98. package/dist/types/core/agents/diffs.d.ts +9 -0
  99. package/dist/types/core/agents/diffs.d.ts.map +1 -0
  100. package/dist/types/core/agents/elicitation.d.ts +3 -2
  101. package/dist/types/core/agents/elicitation.d.ts.map +1 -1
  102. package/dist/types/core/agents/login.d.ts +6 -0
  103. package/dist/types/core/agents/login.d.ts.map +1 -1
  104. package/dist/types/core/agents/mcp-config.d.ts +0 -4
  105. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  106. package/dist/types/core/agents/process-pool.d.ts +193 -0
  107. package/dist/types/core/agents/process-pool.d.ts.map +1 -0
  108. package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
  109. package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
  110. package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
  111. package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
  112. package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
  113. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
  114. package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
  115. package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
  116. package/dist/types/core/agents/scripted/steps.d.ts +132 -0
  117. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
  118. package/dist/types/core/agents/session-agent.d.ts +49 -6
  119. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  120. package/dist/types/core/diagnostics.d.ts +4 -1
  121. package/dist/types/core/diagnostics.d.ts.map +1 -1
  122. package/dist/types/core/index.d.ts +5 -0
  123. package/dist/types/core/index.d.ts.map +1 -1
  124. package/dist/types/core/presets.d.ts +32 -6
  125. package/dist/types/core/presets.d.ts.map +1 -1
  126. package/dist/types/core/processes/lifeline.d.ts +0 -20
  127. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  128. package/dist/types/core/provision/index.d.ts +2 -2
  129. package/dist/types/core/provision/index.d.ts.map +1 -1
  130. package/dist/types/core/provision/install-build.d.ts +2 -1
  131. package/dist/types/core/provision/install-build.d.ts.map +1 -1
  132. package/dist/types/core/provision/npm.d.ts +20 -8
  133. package/dist/types/core/provision/npm.d.ts.map +1 -1
  134. package/dist/types/core/provision/provisioner.d.ts +4 -1
  135. package/dist/types/core/provision/provisioner.d.ts.map +1 -1
  136. package/dist/types/core/provision/tools.d.ts +2 -1
  137. package/dist/types/core/provision/tools.d.ts.map +1 -1
  138. package/dist/types/core/runtime/agent-runtime.d.ts +16 -8
  139. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  140. package/dist/types/core/runtime/event-processor.d.ts +5 -1
  141. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  142. package/dist/types/core/runtime/harness-clis.d.ts +9 -0
  143. package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
  144. package/dist/types/core/runtime/harness-logins.d.ts +33 -5
  145. package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
  146. package/dist/types/core/runtime/model-lists.d.ts +2 -0
  147. package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
  148. package/dist/types/core/runtime/permission-policy.d.ts +5 -3
  149. package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
  150. package/dist/types/core/runtime/request-broker.d.ts +2 -1
  151. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  152. package/dist/types/core/runtime/turn.d.ts +12 -1
  153. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  154. package/dist/types/core/utils/deadline.d.ts +6 -0
  155. package/dist/types/core/utils/deadline.d.ts.map +1 -1
  156. package/dist/types/core/utils/errors.d.ts +7 -1
  157. package/dist/types/core/utils/errors.d.ts.map +1 -1
  158. package/dist/types/operator/operator.d.ts.map +1 -1
  159. package/dist/types/operator/render.d.ts +6 -4
  160. package/dist/types/operator/render.d.ts.map +1 -1
  161. package/dist/types/protocol/auth.d.ts +54 -24
  162. package/dist/types/protocol/auth.d.ts.map +1 -1
  163. package/dist/types/protocol/clis.d.ts +14 -7
  164. package/dist/types/protocol/clis.d.ts.map +1 -1
  165. package/dist/types/protocol/config.d.ts +2 -0
  166. package/dist/types/protocol/config.d.ts.map +1 -1
  167. package/dist/types/protocol/conversation.d.ts +40 -3
  168. package/dist/types/protocol/conversation.d.ts.map +1 -1
  169. package/dist/types/protocol/credentials.d.ts +7 -0
  170. package/dist/types/protocol/credentials.d.ts.map +1 -1
  171. package/dist/types/protocol/execution.d.ts +2 -1
  172. package/dist/types/protocol/execution.d.ts.map +1 -1
  173. package/dist/types/protocol/factories.d.ts +12 -2
  174. package/dist/types/protocol/factories.d.ts.map +1 -1
  175. package/dist/types/protocol/harness.d.ts +6 -2
  176. package/dist/types/protocol/harness.d.ts.map +1 -1
  177. package/dist/types/protocol/history.d.ts +19 -0
  178. package/dist/types/protocol/history.d.ts.map +1 -1
  179. package/dist/types/protocol/lifecycle.d.ts +47 -8
  180. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  181. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  182. package/dist/types/protocol/models.d.ts +3 -0
  183. package/dist/types/protocol/models.d.ts.map +1 -1
  184. package/dist/types/protocol/parts.d.ts +14 -0
  185. package/dist/types/protocol/parts.d.ts.map +1 -1
  186. package/dist/types/protocol/tool-state.d.ts +12 -0
  187. package/dist/types/protocol/tool-state.d.ts.map +1 -1
  188. package/dist/types/protocol/wire-api.d.ts +129 -23
  189. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  190. package/dist/types/protocol/wire.d.ts +70 -10
  191. package/dist/types/protocol/wire.d.ts.map +1 -1
  192. package/dist/types/server/acp/binding.d.ts +6 -1
  193. package/dist/types/server/acp/binding.d.ts.map +1 -1
  194. package/dist/types/server/acp/translate.d.ts +33 -7
  195. package/dist/types/server/acp/translate.d.ts.map +1 -1
  196. package/dist/types/server/agent-server.d.ts +6 -0
  197. package/dist/types/server/agent-server.d.ts.map +1 -1
  198. package/dist/types/server/feedback.d.ts +2 -1
  199. package/dist/types/server/feedback.d.ts.map +1 -1
  200. package/dist/types/server/install.d.ts +11 -0
  201. package/dist/types/server/install.d.ts.map +1 -1
  202. package/dist/types/server/session-gate.d.ts +2 -1
  203. package/dist/types/server/session-gate.d.ts.map +1 -1
  204. package/dist/types/server/session-store.d.ts +1 -0
  205. package/dist/types/server/session-store.d.ts.map +1 -1
  206. package/dist/types/server/subscriptions.d.ts +2 -1
  207. package/dist/types/server/subscriptions.d.ts.map +1 -1
  208. package/dist/types/server/transports.d.ts.map +1 -1
  209. package/dist/types/sync/fit.d.ts +8 -7
  210. package/dist/types/sync/fit.d.ts.map +1 -1
  211. package/dist/types/sync/history-sqlite.d.ts.map +1 -1
  212. package/dist/types/sync/index.d.ts +1 -0
  213. package/dist/types/sync/index.d.ts.map +1 -1
  214. package/dist/types/sync/journal-sqlite.d.ts +2 -1
  215. package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
  216. package/dist/types/sync/journal.d.ts +11 -3
  217. package/dist/types/sync/journal.d.ts.map +1 -1
  218. package/dist/types/sync/projection.d.ts +6 -0
  219. package/dist/types/sync/projection.d.ts.map +1 -1
  220. package/dist/types/sync/recovery.d.ts +2 -1
  221. package/dist/types/sync/recovery.d.ts.map +1 -1
  222. package/dist/types/sync/schema.d.ts +10 -0
  223. package/dist/types/sync/schema.d.ts.map +1 -1
  224. package/dist/types/sync/sql.d.ts +2 -1
  225. package/dist/types/sync/sql.d.ts.map +1 -1
  226. package/docs/consuming.md +278 -28
  227. package/docs/deploy.md +36 -8
  228. package/docs/durable-sessions.md +35 -5
  229. package/docs/harnesses.md +385 -24
  230. package/package.json +6 -1
  231. package/src/accounts/chatgpt.ts +2 -2
  232. package/src/accounts/http.ts +2 -16
  233. package/src/client/client.ts +92 -31
  234. package/src/client/connection.ts +103 -22
  235. package/src/client/index.ts +1 -0
  236. package/src/client/logins.ts +1 -1
  237. package/src/client/streams.ts +12 -4
  238. package/src/client/turns.ts +1 -1
  239. package/src/core/agents/acp/acp-agent.ts +181 -52
  240. package/src/core/agents/acp/adapter.ts +9 -1
  241. package/src/core/agents/acp/client.ts +16 -4
  242. package/src/core/agents/acp/mappings.ts +27 -0
  243. package/src/core/agents/base.ts +25 -10
  244. package/src/core/agents/claude-code/adapter.ts +47 -17
  245. package/src/core/agents/claude-code/approvals.ts +6 -5
  246. package/src/core/agents/claude-code/claude-agent.ts +60 -30
  247. package/src/core/agents/claude-code/events.ts +11 -1
  248. package/src/core/agents/claude-code/login.ts +14 -2
  249. package/src/core/agents/claude-code/mcp.ts +31 -15
  250. package/src/core/agents/claude-code/models.ts +7 -2
  251. package/src/core/agents/claude-code/options.ts +11 -3
  252. package/src/core/agents/claude-code/outcome.ts +10 -2
  253. package/src/core/agents/claude-code/results.ts +73 -0
  254. package/src/core/agents/claude-code/sdk-pair.ts +66 -0
  255. package/src/core/agents/claude-code/session.ts +166 -37
  256. package/src/core/agents/claude-code/spend.ts +1 -1
  257. package/src/core/agents/claude-code/tools.ts +108 -12
  258. package/src/core/agents/codex-app-server/adapter.ts +37 -12
  259. package/src/core/agents/codex-app-server/client.ts +37 -8
  260. package/src/core/agents/codex-app-server/codex-agent.ts +115 -50
  261. package/src/core/agents/codex-app-server/commands.ts +2 -2
  262. package/src/core/agents/codex-app-server/credentials.ts +10 -5
  263. package/src/core/agents/codex-app-server/effort.ts +1 -1
  264. package/src/core/agents/codex-app-server/homes.ts +43 -1
  265. package/src/core/agents/codex-app-server/items.ts +219 -31
  266. package/src/core/agents/codex-app-server/login.ts +14 -2
  267. package/src/core/agents/codex-app-server/mcp.ts +47 -32
  268. package/src/core/agents/codex-app-server/models.ts +9 -8
  269. package/src/core/agents/codex-app-server/options.ts +69 -10
  270. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  271. package/src/core/agents/codex-app-server/protocol.ts +14 -4
  272. package/src/core/agents/codex-app-server/requests.ts +14 -6
  273. package/src/core/agents/codex-app-server/session.ts +151 -113
  274. package/src/core/agents/codex-app-server/subagents.ts +21 -14
  275. package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
  276. package/src/core/agents/codex-app-server/turn-items.ts +1 -1
  277. package/src/core/agents/codex-app-server/turn.ts +79 -32
  278. package/src/core/agents/codex-app-server/user-login.ts +107 -0
  279. package/src/core/agents/credentials.ts +5 -2
  280. package/src/core/agents/diffs.ts +25 -0
  281. package/src/core/agents/elicitation.ts +2 -2
  282. package/src/core/agents/login.ts +6 -0
  283. package/src/core/agents/mcp-config.ts +2 -2
  284. package/src/core/agents/process-pool.ts +432 -0
  285. package/src/core/agents/scripted/adapter.ts +142 -0
  286. package/src/core/agents/scripted/mcp-call.ts +134 -0
  287. package/src/core/agents/scripted/scripted-agent.ts +418 -0
  288. package/src/core/agents/scripted/scripts.ts +171 -0
  289. package/src/core/agents/scripted/steps.ts +127 -0
  290. package/src/core/agents/session-agent.ts +105 -8
  291. package/src/core/diagnostics.ts +9 -1
  292. package/src/core/index.ts +11 -0
  293. package/src/core/presets.ts +99 -14
  294. package/src/core/processes/lifeline.ts +99 -25
  295. package/src/core/provision/index.ts +1 -2
  296. package/src/core/provision/install-build.ts +4 -1
  297. package/src/core/provision/npm.ts +309 -97
  298. package/src/core/provision/provisioner.ts +22 -8
  299. package/src/core/provision/tools.ts +44 -19
  300. package/src/core/runtime/agent-runtime.ts +48 -18
  301. package/src/core/runtime/event-processor.ts +18 -2
  302. package/src/core/runtime/harness-clis.ts +36 -1
  303. package/src/core/runtime/harness-logins.ts +82 -31
  304. package/src/core/runtime/model-lists.ts +1 -1
  305. package/src/core/runtime/permission-policy.ts +4 -3
  306. package/src/core/runtime/request-broker.ts +1 -1
  307. package/src/core/runtime/turn.ts +51 -3
  308. package/src/core/utils/deadline.ts +23 -0
  309. package/src/core/utils/errors.ts +11 -4
  310. package/src/operator/operator.ts +8 -4
  311. package/src/operator/render.ts +46 -8
  312. package/src/protocol/auth.ts +30 -23
  313. package/src/protocol/clis.ts +12 -4
  314. package/src/protocol/credentials.ts +19 -0
  315. package/src/protocol/execution.ts +5 -0
  316. package/src/protocol/factories.ts +25 -3
  317. package/src/protocol/harness.ts +11 -1
  318. package/src/protocol/history.ts +20 -0
  319. package/src/protocol/lifecycle.ts +10 -1
  320. package/src/protocol/model-catalog.ts +19 -8
  321. package/src/protocol/parts.ts +12 -1
  322. package/src/protocol/tool-state.ts +14 -1
  323. package/src/protocol/wire.ts +14 -0
  324. package/src/server/acp/binding.ts +113 -23
  325. package/src/server/acp/translate.ts +161 -8
  326. package/src/server/agent-server.ts +10 -1
  327. package/src/server/bin.ts +83 -9
  328. package/src/server/feedback.ts +1 -1
  329. package/src/server/install.ts +82 -4
  330. package/src/server/session-gate.ts +1 -1
  331. package/src/server/session-store.ts +5 -1
  332. package/src/server/subscriptions.ts +1 -1
  333. package/src/server/transports.ts +14 -0
  334. package/src/sync/fit.ts +115 -28
  335. package/src/sync/history-sqlite.ts +6 -2
  336. package/src/sync/index.ts +1 -0
  337. package/src/sync/journal-sqlite.ts +20 -2
  338. package/src/sync/journal.ts +10 -2
  339. package/src/sync/projection.ts +10 -0
  340. package/src/sync/recovery.ts +11 -4
  341. package/src/sync/schema.ts +7 -5
  342. package/src/sync/sql.ts +1 -1
  343. package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
  344. package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
  345. package/dist/types/core/agents/session-pool.d.ts +0 -41
  346. package/dist/types/core/agents/session-pool.d.ts.map +0 -1
  347. package/src/core/agents/claude-code/session-pool.ts +0 -136
  348. package/src/core/agents/session-pool.ts +0 -111
@@ -26,7 +26,8 @@ import type { AdapterEvent, EventTransformer, TransformResult } from "../types.t
26
26
  import type { ClaudeQuerySettled, ClaudeRawEvent } from "./events.ts";
27
27
  import { fromSdkPermissionMode } from "./options.ts";
28
28
  import { ClaudeTurnOutcome, claudeErrorCategory } from "./outcome.ts";
29
- import { claudeToolMeta, toolLocationsFromInput } from "./tools.ts";
29
+ import { claudeResultView } from "./results.ts";
30
+ import { claudeToolMeta } from "./tools.ts";
30
31
  import { ClaudeTurnUsage } from "./turn-usage.ts";
31
32
 
32
33
  /**
@@ -159,6 +160,8 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
159
160
  private settled?: ClaudeQuerySettled;
160
161
  private readonly usage = new ClaudeTurnUsage();
161
162
  private readonly outcome = new ClaudeTurnOutcome();
163
+ /** The MCP servers the session knows, which name its MCP tools. */
164
+ private mcpServers = new Set<string>();
162
165
 
163
166
  constructor(ctx: { sessionId: string }) {
164
167
  this.ctx = { sessionId: ctx.sessionId, messageId: "" };
@@ -173,7 +176,10 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
173
176
  case "assistant":
174
177
  return this.handleAssistant(event);
175
178
  case "user":
176
- return this.handleToolResults(event.message?.content);
179
+ return this.handleToolResults(event.message?.content, event.tool_use_result);
180
+ case "agentServer/mcpServers":
181
+ for (const name of event.names) this.mcpServers.add(name);
182
+ return [];
177
183
  case "agentServer/querySettled":
178
184
  this.settled = event;
179
185
  return [];
@@ -210,6 +216,8 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
210
216
  switch (event.subtype) {
211
217
  case "init":
212
218
  if (typeof event.model === "string") this.usage.requested(event.model);
219
+ for (const server of Array.isArray(event.mcp_servers) ? event.mcp_servers : [])
220
+ this.mcpServers.add(server.name);
213
221
  return [];
214
222
  case "api_retry": {
215
223
  const message = `Claude API ${event.error_status ? `error ${event.error_status}` : "connection failed"}${event.error ? ` (${event.error})` : ""}`;
@@ -308,7 +316,7 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
308
316
  const input = isRecord(block.input) ? block.input : {};
309
317
  const subagent = subagentFromInput(name, input);
310
318
  const part = createToolPart(this.ctxFor(), block.id, name, input, {
311
- ...claudeToolMeta(name, input),
319
+ ...claudeToolMeta(name, input, this.mcpServers),
312
320
  ...(subagent && { subagent }),
313
321
  });
314
322
  this.toolPartsById.set(block.id, part);
@@ -376,7 +384,12 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
376
384
  }
377
385
  if (cb.type === "tool_use") {
378
386
  const name = cb.name ?? "tool";
379
- const part = createPendingToolPart(this.ctxFor(), cb.id ?? "", name, claudeToolMeta(name));
387
+ const part = createPendingToolPart(
388
+ this.ctxFor(),
389
+ cb.id ?? "",
390
+ name,
391
+ claudeToolMeta(name, undefined, this.mcpServers),
392
+ );
380
393
  this.blocks.set(index, { partId: part.id, kind: "tool", part });
381
394
  if (cb.id) this.toolPartsById.set(cb.id, part);
382
395
  return [{ kind: "part-open", part }];
@@ -439,7 +452,7 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
439
452
  if (tool?.state.status !== "in_progress") continue;
440
453
  if (JSON.stringify(tool.state.input) === JSON.stringify(block.input)) continue;
441
454
  tool.state = { ...tool.state, input: block.input };
442
- setToolMeta(tool, claudeToolMeta(tool.toolName, block.input));
455
+ setToolMeta(tool, claudeToolMeta(tool.toolName, block.input, this.mcpServers));
443
456
  events.push({ kind: "part-update", part: tool });
444
457
  }
445
458
  return events;
@@ -462,30 +475,47 @@ export class ClaudeCodeTransformer implements EventTransformer<unknown> {
462
475
  const input = this.completeInputs.get(tool.toolCallId) ?? safeParseJson(partial);
463
476
  this.completeInputs.delete(tool.toolCallId);
464
477
  startToolPart(tool, input);
465
- // The streamed open carried no input, so subagent metadata can only be
466
- // read now that the partial JSON has parsed.
467
- const locations = toolLocationsFromInput(input);
478
+ // The streamed open carried no input, so what the call acts on and its
479
+ // subagent can only be read now that the partial JSON has parsed.
468
480
  const subagent = subagentFromInput(tool.toolName, input);
469
- if (locations || subagent) {
470
- setToolMeta(tool, { ...(locations && { locations }), ...(subagent && { subagent }) });
471
- }
481
+ setToolMeta(tool, {
482
+ ...claudeToolMeta(tool.toolName, input, this.mcpServers),
483
+ ...(subagent && { subagent }),
484
+ });
472
485
  return [{ kind: "part-update", part: tool }];
473
486
  }
474
487
 
488
+ /**
489
+ * Tool results complete their parts. The message's `tool_use_result` is the
490
+ * structured result of its one tool result: the CLI sends each in a
491
+ * message of its own, so a message with several has none that is theirs.
492
+ */
475
493
  private handleToolResults(
476
494
  content: Extract<SDKMessage, { type: "user" }>["message"]["content"] | undefined,
495
+ structured: unknown,
477
496
  ): AdapterEvent[] {
478
497
  if (!Array.isArray(content)) return [];
498
+ const results = content.filter((block) => block.type === "tool_result");
479
499
  const events: AdapterEvent[] = [];
480
- for (const block of content) {
481
- if (block.type !== "tool_result" || !block.tool_use_id) continue;
500
+ for (const block of results) {
501
+ if (!block.tool_use_id) continue;
482
502
  const tool = this.toolPartsById.get(block.tool_use_id);
483
503
  if (!tool) continue;
484
- const content = toolResultContent(block.content);
504
+ const output = stringifyToolResult(block.content);
505
+ const isError = block.is_error === true;
506
+ const view = claudeResultView(
507
+ tool.toolName,
508
+ results.length === 1 ? structured : undefined,
509
+ output,
510
+ isError,
511
+ );
512
+ const shown = [...(toolResultContent(block.content) ?? []), ...(view.content ?? [])];
513
+ if (view.locations) setToolMeta(tool, { locations: view.locations });
485
514
  completeToolPart(tool, {
486
- output: stringifyToolResult(block.content),
487
- isError: block.is_error === true,
488
- ...(content && { content }),
515
+ output,
516
+ isError,
517
+ ...(shown.length && { content: shown }),
518
+ ...(view.metadata && { metadata: view.metadata }),
489
519
  });
490
520
  events.push({ kind: "part-update", part: tool });
491
521
  }
@@ -19,10 +19,7 @@ import { claudeToolMeta } from "./tools.ts";
19
19
  import type { ClaudeTurn } from "./turn.ts";
20
20
 
21
21
  /** What a request needs of the turn it is asked in. */
22
- export type ClaudeTurnRequests = Pick<
23
- ClaudeTurn,
24
- "permissions" | "elicitation" | "cancelByRequest"
25
- >;
22
+ type ClaudeTurnRequests = Pick<ClaudeTurn, "permissions" | "elicitation" | "cancelByRequest">;
26
23
 
27
24
  /** What Claude is told when it asks while no turn runs. */
28
25
  const NO_TURN = "No turn is running, so nobody can answer this.";
@@ -67,6 +64,8 @@ export function createCanUseTool(deps: {
67
64
  policy?: ClaudeToolPolicy;
68
65
  /** The turn running now; null between turns. */
69
66
  turn: () => ClaudeTurnRequests | null;
67
+ /** The MCP servers the session knows, which name its MCP tools. */
68
+ mcpServers?: () => readonly string[];
70
69
  }): CanUseTool {
71
70
  return async (toolName, input, options) => {
72
71
  const { signal, toolUseID, agentID, suggestions, blockedPath, decisionReason } = options;
@@ -100,12 +99,14 @@ export function createCanUseTool(deps: {
100
99
  opts: { options?: PermissionOption[]; always?: { rule: string } | null },
101
100
  ): Promise<PermissionDecision | undefined> => {
102
101
  if (!turn.permissions) return undefined;
102
+ // @ref LLP 0002#a-tool-call-says-what-it-acts-on — why Claude asks is `reason`, in the CLI's words
103
103
  const toolCall: PermissionToolCall = {
104
104
  toolCallId: toolUseID,
105
105
  toolName,
106
106
  ...(title && { title }),
107
+ ...(decisionReason && { reason: decisionReason }),
107
108
  rawInput: input,
108
- ...claudeToolMeta(toolName, input),
109
+ ...claudeToolMeta(toolName, input, deps.mcpServers?.()),
109
110
  };
110
111
  const decision = await turn.permissions(toolCall, { signal, ...opts });
111
112
  // A request Claude withdrew itself (a hook decided first) is not a cancel.
@@ -8,21 +8,26 @@ import type {
8
8
  AgentInput,
9
9
  McpServerConfig,
10
10
  ModelDescriptor,
11
+ RunConfig,
11
12
  } from "../../../protocol/index.ts";
12
13
  import { emitDiagnostic } from "../../diagnostics.ts";
13
14
  import { type NativeProcesses, sharedNativeProcesses } from "../../processes/native-processes.ts";
14
- import { onAbort } from "../../utils/deadline.ts";
15
+ import { onAbort, untilAborted } from "../../utils/deadline.ts";
15
16
  import { SessionResumeError } from "../../utils/errors.ts";
16
17
  import { UnsupportedAttachmentError, attachedText, isTextMedia } from "../attachments.ts";
17
- import type { AgentExecuteOptions, CancelResult, ListModelsOptions } from "../base.ts";
18
+ import type { AgentExecuteOptions, ListModelsOptions } from "../base.ts";
18
19
  import { SessionAgent } from "../session-agent.ts";
19
20
  import { ClaudeCredentials, type ClaudeDelivery, ON_PLACEHOLDER } from "./credentials.ts";
20
21
  import type { ClaudeRawEvent } from "./events.ts";
21
22
  import { ClaudeCliLogin } from "./login.ts";
22
23
  import { claudeModelDescriptors, fitClaudeThinking, readClaudeModels } from "./models.ts";
23
24
  import { type ClaudeCodeAgentOptions, claudeSessionConfig } from "./options.ts";
24
- import { ClaudeSessionPool } from "./session-pool.ts";
25
- import type { ClaudeSession } from "./session.ts";
25
+ import {
26
+ type ClaudePoolTurn,
27
+ type ClaudeSession,
28
+ type ClaudeSessionPool,
29
+ claudeSessionPool,
30
+ } from "./session.ts";
26
31
 
27
32
  type ClaudeContent = SDKUserMessage["message"]["content"];
28
33
 
@@ -40,6 +45,8 @@ const CAPABILITIES: AgentCapabilities = {
40
45
  rejectionMessages: true,
41
46
  turnScopedSubagents: true,
42
47
  steering: true,
48
+ // Every turn setting reaches the SDK (`buildClaudeOptions`).
49
+ ignoredSettings: [],
43
50
  };
44
51
 
45
52
  /**
@@ -112,9 +119,9 @@ export class ClaudeCodeAgent extends SessionAgent {
112
119
  private readonly credentials?: ClaudeCredentials;
113
120
 
114
121
  constructor(private readonly agentOptions: ClaudeCodeAgentOptions = {}) {
115
- super();
122
+ super({ cancelConfirmMs: agentOptions.cancelConfirmMs });
116
123
  this.processes = agentOptions.processes ?? sharedNativeProcesses();
117
- this.pool = new ClaudeSessionPool({ ...agentOptions, processes: this.processes });
124
+ this.pool = claudeSessionPool({ ...agentOptions, processes: this.processes });
118
125
  this.login = new ClaudeCliLogin({
119
126
  resolveCliPath: agentOptions.resolveCliPath,
120
127
  processes: this.processes,
@@ -134,8 +141,8 @@ export class ClaudeCodeAgent extends SessionAgent {
134
141
  : options;
135
142
  }
136
143
 
137
- loginChanged(): void {
138
- this.pool.loginChanged();
144
+ loginChanged(store?: string): void {
145
+ this.pool.loginChanged(store);
139
146
  }
140
147
 
141
148
  protected override async *executeTurn(
@@ -143,11 +150,17 @@ export class ClaudeCodeAgent extends SessionAgent {
143
150
  options: AgentExecuteOptions,
144
151
  controller: AbortController,
145
152
  ): AsyncIterableIterator<ClaudeRawEvent> {
153
+ let held: ClaudeSession | undefined;
146
154
  let unconfirmedSession: ClaudeSession | undefined;
147
155
  let removeInterrupt: (() => void) | undefined;
156
+ // @ref LLP 0001#d10--built-in-turns-own-their-logical-session-through-cleanup — a start under way ends at the abort, at once
157
+ // Until the turn has its process, the abort is the whole stop.
158
+ this.cancelWith(options, async () => (held ? this.interrupt(held, options.sessionId) : true));
148
159
  try {
149
160
  controller.signal.throwIfAborted();
150
- const cliPath = await this.agentOptions.resolveCliPath?.();
161
+ // @ref LLP 0010#a-turn-waits-for-its-cli-until-it-is-cancelled — the install is the server's
162
+ const { resolveCliPath } = this.agentOptions;
163
+ const cliPath = resolveCliPath && (await untilAborted(resolveCliPath(), controller.signal));
151
164
  const content = toClaudeContent(input);
152
165
  controller.signal.throwIfAborted();
153
166
  const thinking = fitClaudeThinking(options.thinkingLevel, options.modelThinking);
@@ -156,16 +169,26 @@ export class ClaudeCodeAgent extends SessionAgent {
156
169
  await this.credentials?.forTurn(options.sessionId, options.turnId, controller.signal),
157
170
  );
158
171
  controller.signal.throwIfAborted();
159
- const { session, spawned } = await this.pool.getOrCreate(
160
- options.sessionId,
161
- claudeSessionConfig(
172
+ const wanted: ClaudePoolTurn = {
173
+ sessionId: options.sessionId,
174
+ config: claudeSessionConfig(
162
175
  { ...turnOptions, thinkingLevel: thinking.send },
163
176
  cliPath,
164
177
  this.agentOptions.inheritConfig,
165
178
  ),
166
- );
179
+ };
180
+ const session = await this.pool.acquire(options.sessionId, wanted, controller.signal);
181
+ held = session;
182
+ const spawned = wanted.spawned === true;
167
183
  if (thinking.applied && !session.operatorThinking)
168
184
  options.onExecution?.({ thinkingLevel: thinking.applied });
185
+ const notApplied: Array<keyof RunConfig> = [];
186
+ // A model known to have no effort levels is sent no level (`fitClaudeThinking`).
187
+ if (options.thinkingLevel !== undefined && thinking.send === undefined)
188
+ notApplied.push("thinkingLevel");
189
+ // A message to resume at goes only with a resume (`buildClaudeOptions`).
190
+ if (options.resumeSessionAt && !options.resumeSessionId) notApplied.push("resumeSessionAt");
191
+ if (notApplied.length) options.onExecution?.({ notApplied });
169
192
  const attemptedResume = spawned && Boolean(options.resumeSessionId);
170
193
  if (spawned) unconfirmedSession = session;
171
194
  // A cancel during connection repair must never submit the prompt.
@@ -195,6 +218,8 @@ export class ClaudeCodeAgent extends SessionAgent {
195
218
  : "no_active_turn",
196
219
  );
197
220
  removeInterrupt = onAbort(controller.signal, () => void session.interruptTurn());
221
+ const mcpServers = session.mcp.names;
222
+ if (mcpServers.length) yield { type: "agentServer/mcpServers", names: mcpServers };
198
223
  let sawOutput = false;
199
224
  for await (const event of turn.events) {
200
225
  if (event.type === "assistant" || event.type === "stream_event") sawOutput = true;
@@ -245,6 +270,8 @@ export class ClaudeCodeAgent extends SessionAgent {
245
270
  // A cancelled/failed preparation must not leave an unconfirmed query
246
271
  // looking like a healthy warm conversation on the next attempt.
247
272
  await unconfirmedSession?.terminate();
273
+ // A warm one waits for the next turn, at most the idle time.
274
+ if (held) this.pool.leave(options.sessionId, held);
248
275
  }
249
276
  }
250
277
 
@@ -274,31 +301,32 @@ export class ClaudeCodeAgent extends SessionAgent {
274
301
 
275
302
  /**
276
303
  * Hot-swap the live session's wire MCP servers without a restart (context
277
- * intact). Returns false when the session isn't live. In-process servers
278
- * are preserved across swaps.
304
+ * intact). A session still starting takes it once ready, after the servers
305
+ * its turn attaches. Returns undefined when the session isn't live. In-process servers
306
+ * are preserved across swaps. The next turn's own `mcpServers` swap back
307
+ * when they still name the old set: keep them in step.
279
308
  */
280
309
  async setMcpServers(
281
310
  sessionId: string,
282
311
  servers: Record<string, McpServerConfig>,
283
312
  ): Promise<McpSetServersResult | undefined> {
284
- return this.pool.setMcpServers(sessionId, servers);
313
+ return this.pool.client(sessionId)?.setMcpServers(servers);
285
314
  }
286
315
 
287
316
  /** Resume saved history in a fresh subprocess on the next turn after host suspension. */
288
317
  invalidateMcpConnections(): void {
289
- this.pool.invalidateMcpConnections();
318
+ this.pool.invalidate();
290
319
  }
291
320
 
292
- override async cancel(sessionId: string): Promise<CancelResult> {
293
- // Abort FIRST: the controller ends the engine's execute loop, so this
294
- // turn classifies as cancelled even when the generator drains to a
295
- // natural end before the SDK interrupt lands (the abort-order race that
296
- // could surface a clean user cancel as `error`/`end_turn`). The SDK
297
- // interrupt then stops the underlying turn; its round-trip is what
298
- // confirms the cancel.
299
- const session = this.pool.get(sessionId);
300
- const base = await super.cancel(sessionId);
301
- if (!session || !base.hadTurn) return base;
321
+ /**
322
+ * Interrupt the turn's process for a cancel, which has already aborted the
323
+ * turn: the controller ends the engine's execute loop, so this turn
324
+ * classifies as cancelled even when the generator drains to a natural end
325
+ * before the SDK interrupt lands (the abort-order race that could surface
326
+ * a clean user cancel as `error`/`end_turn`). The SDK interrupt's
327
+ * round-trip is what confirms the cancel.
328
+ */
329
+ private async interrupt(session: ClaudeSession, sessionId: string): Promise<boolean> {
302
330
  const interrupted = await session.interruptTurn();
303
331
  if (!interrupted) {
304
332
  emitDiagnostic(this.agentOptions.onDiagnostic, {
@@ -307,18 +335,20 @@ export class ClaudeCodeAgent extends SessionAgent {
307
335
  message: "cancel dispatched but the SDK interrupt was not acknowledged in time",
308
336
  });
309
337
  }
310
- return { confirmed: interrupted, hadTurn: true };
338
+ return interrupted;
311
339
  }
312
340
 
313
341
  override async release(sessionId: string): Promise<void> {
314
342
  await super.release(sessionId);
315
- await this.pool.terminate(sessionId);
343
+ await this.pool.release(sessionId);
316
344
  this.credentials?.release(sessionId);
317
345
  }
318
346
 
319
347
  override async terminateAll(): Promise<void> {
348
+ // Before the turns are aborted: a start under way ends for shutdown, not as a cancel.
349
+ const closed = this.pool.closeAll();
320
350
  await super.terminateAll();
321
- await this.pool.terminateAll();
351
+ await closed;
322
352
  await this.credentials?.close();
323
353
  }
324
354
  }
@@ -14,7 +14,17 @@ export type ClaudeRawEvent =
14
14
  | SDKMessage
15
15
  | ClaudeTurnInterrupted
16
16
  | ClaudeSteerRead
17
- | ClaudeQuerySettled;
17
+ | ClaudeQuerySettled
18
+ | ClaudeMcpServerNames;
19
+
20
+ /**
21
+ * The MCP servers the session knows as the turn begins, which name its MCP
22
+ * tools (`mcp__<server>__<tool>`): a server's name may hold `__` itself.
23
+ */
24
+ export interface ClaudeMcpServerNames {
25
+ type: "agentServer/mcpServers";
26
+ names: string[];
27
+ }
18
28
 
19
29
  /**
20
30
  * The turn was asked to stop. An interrupted query and one that failed end on
@@ -47,7 +47,7 @@ type ClaudeQuery = (params: {
47
47
  options: CCOptions;
48
48
  }) => Query & ClaudeLoginControl;
49
49
 
50
- export interface ClaudeCliLoginOptions {
50
+ interface ClaudeCliLoginOptions {
51
51
  resolveCliPath?: () => Promise<string | undefined>;
52
52
  processes: NativeProcesses;
53
53
  inheritConfig?: InheritConfig;
@@ -113,10 +113,22 @@ function httpsUrl(value: unknown): string {
113
113
  return value;
114
114
  }
115
115
 
116
+ /** The variable that picks Claude Code's login store. */
117
+ const STORE_VARIABLE = "CLAUDE_CONFIG_DIR";
118
+
119
+ /** The login store a turn or a login with this `env` uses: its `CLAUDE_CONFIG_DIR`, empty when unset. */
120
+ export function claudeLoginStore(env: Record<string, string> | undefined): string {
121
+ return env?.[STORE_VARIABLE] ?? "";
122
+ }
123
+
116
124
  /** Claude Code's login: `claude auth status`, its OAuth flows over the SDK, `claude auth logout`. */
117
125
  export class ClaudeCliLogin implements CliLogin {
118
126
  constructor(private readonly options: ClaudeCliLoginOptions) {}
119
127
 
128
+ store(env: Record<string, string> | undefined): string {
129
+ return claudeLoginStore(env);
130
+ }
131
+
120
132
  /** The CLI to run a command of: the one turns run, else `$CLAUDE_CLI_PATH`, else `claude` on PATH. */
121
133
  private async commandPath(): Promise<string> {
122
134
  return (await this.options.resolveCliPath?.()) ?? process.env.CLAUDE_CLI_PATH ?? "claude";
@@ -134,7 +146,7 @@ export class ClaudeCliLogin implements CliLogin {
134
146
  const cliPath = await this.commandPath();
135
147
  return [
136
148
  ...LOGINS,
137
- terminalMethod([cliPath, "auth", "login"], this.env(options), "CLAUDE_CONFIG_DIR"),
149
+ terminalMethod([cliPath, "auth", "login"], this.env(options), STORE_VARIABLE),
138
150
  ];
139
151
  }
140
152
 
@@ -11,7 +11,7 @@ import {
11
11
  import type { SdkMcpServers } from "./options.ts";
12
12
 
13
13
  /** What an MCP update asks of the live query. */
14
- export type McpControl = Pick<Query, "setMcpServers" | "mcpServerStatus">;
14
+ type McpControl = Pick<Query, "setMcpServers" | "mcpServerStatus">;
15
15
 
16
16
  function remoteMcpServer(server: McpServerConfig): boolean {
17
17
  return server.type === "http" || server.type === "sse";
@@ -33,6 +33,7 @@ function transientMcpError(error: string): boolean {
33
33
  export class ClaudeMcpConnections {
34
34
  private wire: Record<string, McpServerConfig>;
35
35
  private host: SdkMcpServers = {};
36
+ private loaded: string[] = [];
36
37
  private updates = Promise.resolve();
37
38
  /**
38
39
  * Set while an update has not finished, with the servers that failed in it.
@@ -41,12 +42,18 @@ export class ClaudeMcpConnections {
41
42
  * uncertainty holds until a verified reattachment succeeds.
42
43
  */
43
44
  private failed?: Set<string>;
44
- private invalidated = false;
45
45
  /** Servers not required that could not connect: the session's turns go on without them. */
46
46
  readonly unavailable = new UnavailableMcpServers();
47
47
 
48
- /** `servers`: the spawning turn's, attached before its prompt. */
49
- constructor(servers: Record<string, McpServerConfig> | undefined) {
48
+ /**
49
+ * `servers`: the spawning turn's, attached before its prompt.
50
+ * `hostResumed`: whether a host resume came since the process began to
51
+ * start (the pool's `startedBeforeResume`).
52
+ */
53
+ constructor(
54
+ servers: Record<string, McpServerConfig> | undefined,
55
+ private readonly hostResumed: () => boolean = () => false,
56
+ ) {
50
57
  this.wire = snapshotMcpServers(servers);
51
58
  }
52
59
 
@@ -55,6 +62,20 @@ export class ClaudeMcpConnections {
55
62
  return this.wire;
56
63
  }
57
64
 
65
+ /**
66
+ * Every server name the session knows: the wire's, the host's, and those
67
+ * Claude loaded from the person's configuration (above `inheritConfig:
68
+ * "none"`), which its `system/init` lists. A tool name is matched against them.
69
+ */
70
+ get names(): string[] {
71
+ return [...new Set([...Object.keys(this.wire), ...Object.keys(this.host), ...this.loaded])];
72
+ }
73
+
74
+ /** The servers Claude's `system/init` listed. */
75
+ sawLoaded(names: readonly string[]): void {
76
+ this.loaded = [...names];
77
+ }
78
+
58
79
  /** The last update did not finish: what the CLI holds now is not known. */
59
80
  get uncertain(): boolean {
60
81
  return this.failed !== undefined;
@@ -62,17 +83,12 @@ export class ClaudeMcpConnections {
62
83
 
63
84
  /**
64
85
  * The process must be replaced: host suspension invalidated its
65
- * subprocess-wide HTTP pool, which replacing MCP clients cannot clear.
86
+ * subprocess-wide HTTP pool, which replacing MCP clients cannot clear. A
87
+ * thawed VM can retain clients whose sockets still appear connected, so it
88
+ * holds until the process is replaced, whatever an update says after.
66
89
  */
67
90
  get respawnRequired(): boolean {
68
- return this.invalidated;
69
- }
70
-
71
- /** A thawed VM can retain clients whose sockets still appear connected. */
72
- invalidate(): void {
73
- // Latch until process replacement, even if an in-flight MCP update
74
- // succeeds or the current config no longer contains remote servers.
75
- this.invalidated = true;
91
+ return this.hostResumed();
76
92
  }
77
93
 
78
94
  /** The host's in-process servers, resolved at spawn: every update keeps them, and they win a name. */
@@ -84,7 +100,7 @@ export class ClaudeMcpConnections {
84
100
  needsUpdate(servers: Record<string, McpServerConfig> | undefined): boolean {
85
101
  return (
86
102
  this.uncertain ||
87
- this.invalidated ||
103
+ this.respawnRequired ||
88
104
  // A server the last turn went without is tried again once due, or at
89
105
  // once when this turn requires it.
90
106
  this.unavailable.retryNow(servers ?? {}).length > 0 ||
@@ -118,7 +134,7 @@ export class ClaudeMcpConnections {
118
134
  query: McpControl,
119
135
  servers: Record<string, McpServerConfig>,
120
136
  ): Promise<McpSetServersResult> {
121
- if (this.invalidated) throw new Error("Claude process must resume after host suspension");
137
+ if (this.respawnRequired) throw new Error("Claude process must resume after host suspension");
122
138
  const reconnect = this.failed !== undefined;
123
139
  // What failed last time stays uncertain through this update, with what fails in it.
124
140
  const failed = new Set(this.failed);
@@ -35,14 +35,19 @@ export function claudeModelDescriptors(models: readonly ClaudeModelInfo[]): Mode
35
35
  * lowers a level its model lacks on its own and does not say so (`max` and
36
36
  * `xhigh` become `high`), so the level is fitted here, where the model's levels
37
37
  * are known. `off` is sent as it is: some models do not let thinking be
38
- * disabled, and Claude Code then thinks at its default effort.
38
+ * disabled, and Claude Code then thinks at its default effort; Haiku, which
39
+ * Claude Code otherwise starts thinking, does not think with it. A model known
40
+ * to have no effort levels (`levels: []`, Haiku) is sent no other level, so it
41
+ * is not started on adaptive thinking at an effort it has no control for; a
42
+ * model whose levels are unknown (`undefined`) is sent the level as asked.
39
43
  */
40
44
  export function fitClaudeThinking(
41
45
  level: ThinkingLevel | undefined,
42
46
  levels: readonly ThinkingLevel[] | undefined,
43
47
  ): { send?: ThinkingLevel; applied?: ThinkingLevel } {
44
48
  if (level === undefined) return {};
45
- if (level === "off" || !levels?.length) return { send: level };
49
+ if (level === "off" || levels === undefined) return { send: level };
50
+ if (!levels.length) return {};
46
51
  const fitted = clampThinkingLevel(level, levels) ?? level;
47
52
  return { send: fitted, applied: fitted };
48
53
  }
@@ -110,6 +110,14 @@ export interface ClaudeCodeAgentOptions {
110
110
  credentials?: CredentialSource;
111
111
  /** Idle time before an unused session's subprocess is shut down (default 5 minutes). */
112
112
  idleTimeoutMs?: number;
113
+ /**
114
+ * How long a cancel waits for Claude to acknowledge its interrupt before
115
+ * it answers `confirmed: false` (`CANCEL_CONFIRM_MS`, 2 s, by default),
116
+ * and how long a turn the runtime stopped reading has to end before its
117
+ * process is replaced. An interrupt Claude never acknowledges leaves the
118
+ * process and the turn running (LLP 0016, decision 11).
119
+ */
120
+ cancelConfirmMs?: number;
113
121
  /**
114
122
  * In-process MCP servers to attach to every session (the SDK's
115
123
  * `createSdkMcpServer` output). Called once per session spawn. Operator
@@ -167,12 +175,12 @@ export interface ClaudeCodeAgentOptions {
167
175
  /** Embed-tier extras threaded from ClaudeCodeAgentOptions into each session. */
168
176
  export type ClaudeSessionExtras = Pick<
169
177
  ClaudeCodeAgentOptions,
170
- "sdkMcpServers" | "toolPolicy" | "hooks" | "sdkOptions" | "processes"
178
+ "sdkMcpServers" | "toolPolicy" | "hooks" | "sdkOptions" | "processes" | "onDiagnostic"
171
179
  >;
172
180
 
173
181
  /** What every session of an agent runs with: fixed when the agent is made. */
174
182
  export type ClaudeSessionSettings = ClaudeSessionExtras &
175
- Pick<ClaudeCodeAgentOptions, "idleTimeoutMs"> & {
183
+ Pick<ClaudeCodeAgentOptions, "idleTimeoutMs" | "cancelConfirmMs"> & {
176
184
  /** The agent's: where its sessions' `claude` processes start. */
177
185
  processes: NativeProcesses;
178
186
  };
@@ -238,7 +246,7 @@ export function claudeSessionConfig(
238
246
  }
239
247
 
240
248
  /** How many model turns a query may take unless the turn says otherwise. */
241
- export const DEFAULT_MAX_TURNS = 100;
249
+ const DEFAULT_MAX_TURNS = 100;
242
250
 
243
251
  /** The settings sources each level loads; built-in plugins and skills load whatever they say. */
244
252
  const SETTING_SOURCES: Record<InheritConfig, readonly SettingSource[]> = {
@@ -3,7 +3,7 @@ import { type ErrorCategory, type StopReason, classifyError } from "../../../pro
3
3
  import type { AdapterEvent, TransformResult } from "../types.ts";
4
4
 
5
5
  /** Map Anthropic stop reasons / result subtypes onto the normalized enum. */
6
- export function mapClaudeStopReason(
6
+ function mapClaudeStopReason(
7
7
  stopReason: string | null | undefined,
8
8
  subtype: string | undefined,
9
9
  ): StopReason | undefined {
@@ -70,7 +70,7 @@ export function planUsedUp(info: SDKRateLimitInfo): boolean {
70
70
  }
71
71
 
72
72
  /** A `rate_limit_event`: one window of the claude.ai plan (`resetsAt` is in seconds). */
73
- export function claudeLimits(info: SDKRateLimitInfo | undefined): AdapterEvent[] {
73
+ function claudeLimits(info: SDKRateLimitInfo | undefined): AdapterEvent[] {
74
74
  if (!info) return [];
75
75
  // The status is the reported window's own: another window may still be limited.
76
76
  const status =
@@ -101,6 +101,8 @@ export class ClaudeTurnOutcome {
101
101
  private error?: string;
102
102
  /** The typed code of the last failed API request the turn reported. */
103
103
  private errorCode?: string;
104
+ /** The HTTP status of the API failure its result reported (`api_error_status`). */
105
+ private errorStatus?: number;
104
106
  /** Plan windows Claude reports used up, with no extra usage serving requests. */
105
107
  private readonly usedUpWindows = new Set<string>();
106
108
  private interrupted = false;
@@ -140,6 +142,7 @@ export class ClaudeTurnOutcome {
140
142
  if (this.steersPending) {
141
143
  this.error = undefined;
142
144
  this.errorCode = undefined;
145
+ this.errorStatus = undefined;
143
146
  this.execFailure = false;
144
147
  }
145
148
  this.steersPending = steersPending;
@@ -163,6 +166,7 @@ export class ClaudeTurnOutcome {
163
166
  // provider) ends the turn as a "success" whose text is the error. The
164
167
  // text names the failure, so the runtime classifies it (401 -> auth).
165
168
  this.error = result.result || "Claude turn ended with an API error";
169
+ if (typeof result.api_error_status === "number") this.errorStatus = result.api_error_status;
166
170
  }
167
171
  }
168
172
 
@@ -204,6 +208,10 @@ export class ClaudeTurnOutcome {
204
208
  // `invalid_request` is any 400, a prompt too long for the model among them.
205
209
  if (typed === "invalid_request" && this.error && classifyError(this.error) === "context_limit")
206
210
  return "context_limit";
211
+ // `server_error` is any 5xx, an overloaded API (529, 503) among them;
212
+ // the result's status says which.
213
+ if (typed === "internal" && this.errorStatus !== undefined)
214
+ return claudeErrorCategory(undefined, this.errorStatus) ?? typed;
207
215
  return typed;
208
216
  }
209
217
  }