@zvada/agent-server 0.3.25 → 0.3.27

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 (256) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/README.md +4 -1
  3. package/dist/client/client.js +27 -11
  4. package/dist/client/connection.js +35 -3
  5. package/dist/core/agents/acp/acp-agent.js +91 -14
  6. package/dist/core/agents/acp/mappings.js +5 -1
  7. package/dist/core/agents/claude-code/approvals.js +3 -2
  8. package/dist/core/agents/claude-code/claude-agent.js +12 -2
  9. package/dist/core/agents/claude-code/mcp.js +9 -1
  10. package/dist/core/agents/claude-code/models.js +16 -10
  11. package/dist/core/agents/claude-code/options.js +23 -2
  12. package/dist/core/agents/claude-code/session.js +21 -1
  13. package/dist/core/agents/claude-code/tools.js +9 -4
  14. package/dist/core/agents/claude-code/turn.js +2 -0
  15. package/dist/core/agents/codex-app-server/adapter.js +2 -0
  16. package/dist/core/agents/codex-app-server/client.js +8 -5
  17. package/dist/core/agents/codex-app-server/codex-agent.js +35 -2
  18. package/dist/core/agents/codex-app-server/effort.js +13 -6
  19. package/dist/core/agents/codex-app-server/mcp.js +35 -18
  20. package/dist/core/agents/codex-app-server/models.js +10 -10
  21. package/dist/core/agents/codex-app-server/options.js +11 -2
  22. package/dist/core/agents/codex-app-server/plan-approval.js +1 -1
  23. package/dist/core/agents/codex-app-server/session.js +11 -4
  24. package/dist/core/agents/codex-app-server/thread-settings.js +30 -11
  25. package/dist/core/agents/host-tools.js +493 -0
  26. package/dist/core/agents/mcp-config.js +6 -2
  27. package/dist/core/agents/scripted/scripted-agent.js +39 -6
  28. package/dist/core/agents/scripted/steps.js +5 -1
  29. package/dist/core/agents/session-agent.js +70 -2
  30. package/dist/core/agents/thinking-levels.js +8 -5
  31. package/dist/core/index.js +1 -0
  32. package/dist/core/presets.js +16 -3
  33. package/dist/core/processes/lifeline.js +9 -5
  34. package/dist/core/provision/pins.js +7 -16
  35. package/dist/core/provision/releases.js +25 -25
  36. package/dist/core/proxy/loopback.js +22 -4
  37. package/dist/core/runtime/agent-runtime.js +15 -5
  38. package/dist/core/runtime/event-processor.js +13 -8
  39. package/dist/core/runtime/origin-line.js +42 -0
  40. package/dist/core/runtime/request-broker.js +2 -1
  41. package/dist/core/runtime/steering.js +4 -3
  42. package/dist/core/runtime/turn.js +6 -4
  43. package/dist/operator/errors.js +97 -0
  44. package/dist/operator/index.js +10 -5
  45. package/dist/operator/mcp.js +7 -12
  46. package/dist/operator/operator.js +1336 -380
  47. package/dist/operator/refs.js +59 -0
  48. package/dist/operator/render.js +171 -74
  49. package/dist/operator/session-tools.js +87 -0
  50. package/dist/operator/types.js +30 -0
  51. package/dist/operator/view.js +82 -27
  52. package/dist/protocol/config.js +33 -6
  53. package/dist/protocol/conversation.js +3 -0
  54. package/dist/protocol/history.js +2 -1
  55. package/dist/protocol/index.js +1 -0
  56. package/dist/protocol/lifecycle.js +19 -0
  57. package/dist/protocol/model-catalog.js +58 -18
  58. package/dist/protocol/models.js +6 -27
  59. package/dist/protocol/origin.js +39 -0
  60. package/dist/protocol/reduce.js +2 -0
  61. package/dist/protocol/thinking.js +22 -9
  62. package/dist/protocol/wire-api.js +2 -2
  63. package/dist/protocol/wire.js +17 -0
  64. package/dist/server/bin.js +6 -4
  65. package/dist/server/routes.js +15 -5
  66. package/dist/types/client/client.d.ts.map +1 -1
  67. package/dist/types/client/connection.d.ts +14 -2
  68. package/dist/types/client/connection.d.ts.map +1 -1
  69. package/dist/types/client/turns.d.ts +2 -2
  70. package/dist/types/client/turns.d.ts.map +1 -1
  71. package/dist/types/core/agents/acp/acp-agent.d.ts +15 -0
  72. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  73. package/dist/types/core/agents/acp/mappings.d.ts +5 -1
  74. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  75. package/dist/types/core/agents/acp/requests.d.ts +2 -0
  76. package/dist/types/core/agents/acp/requests.d.ts.map +1 -1
  77. package/dist/types/core/agents/base.d.ts +2 -0
  78. package/dist/types/core/agents/base.d.ts.map +1 -1
  79. package/dist/types/core/agents/claude-code/approvals.d.ts +2 -1
  80. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  81. package/dist/types/core/agents/claude-code/claude-agent.d.ts +1 -0
  82. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  83. package/dist/types/core/agents/claude-code/mcp.d.ts +8 -1
  84. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  85. package/dist/types/core/agents/claude-code/models.d.ts +9 -5
  86. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  87. package/dist/types/core/agents/claude-code/options.d.ts +10 -1
  88. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  89. package/dist/types/core/agents/claude-code/session.d.ts +2 -0
  90. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  91. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  92. package/dist/types/core/agents/claude-code/turn.d.ts +3 -0
  93. package/dist/types/core/agents/claude-code/turn.d.ts.map +1 -1
  94. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  95. package/dist/types/core/agents/codex-app-server/client.d.ts +2 -0
  96. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  97. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +11 -0
  98. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  99. package/dist/types/core/agents/codex-app-server/effort.d.ts +7 -7
  100. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  101. package/dist/types/core/agents/codex-app-server/mcp.d.ts +11 -3
  102. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  103. package/dist/types/core/agents/codex-app-server/models.d.ts +4 -1
  104. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  105. package/dist/types/core/agents/codex-app-server/options.d.ts +9 -2
  106. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  107. package/dist/types/core/agents/codex-app-server/requests.d.ts +2 -0
  108. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  109. package/dist/types/core/agents/codex-app-server/session.d.ts +6 -3
  110. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  111. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  112. package/dist/types/core/agents/host-tools.d.ts +162 -0
  113. package/dist/types/core/agents/host-tools.d.ts.map +1 -0
  114. package/dist/types/core/agents/mcp-config.d.ts +11 -2
  115. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  116. package/dist/types/core/agents/scripted/scripted-agent.d.ts +14 -1
  117. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
  118. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -1
  119. package/dist/types/core/agents/session-agent.d.ts +2 -7
  120. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  121. package/dist/types/core/agents/thinking-levels.d.ts +7 -3
  122. package/dist/types/core/agents/thinking-levels.d.ts.map +1 -1
  123. package/dist/types/core/index.d.ts +1 -0
  124. package/dist/types/core/index.d.ts.map +1 -1
  125. package/dist/types/core/presets.d.ts +11 -1
  126. package/dist/types/core/presets.d.ts.map +1 -1
  127. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  128. package/dist/types/core/provision/pins.d.ts +5 -4
  129. package/dist/types/core/provision/pins.d.ts.map +1 -1
  130. package/dist/types/core/proxy/loopback.d.ts.map +1 -1
  131. package/dist/types/core/runtime/agent-runtime.d.ts +4 -2
  132. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  133. package/dist/types/core/runtime/event-processor.d.ts +2 -2
  134. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  135. package/dist/types/core/runtime/origin-line.d.ts +15 -0
  136. package/dist/types/core/runtime/origin-line.d.ts.map +1 -0
  137. package/dist/types/core/runtime/request-broker.d.ts +1 -1
  138. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  139. package/dist/types/core/runtime/steering.d.ts +7 -4
  140. package/dist/types/core/runtime/steering.d.ts.map +1 -1
  141. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  142. package/dist/types/operator/errors.d.ts +83 -0
  143. package/dist/types/operator/errors.d.ts.map +1 -0
  144. package/dist/types/operator/index.d.ts +10 -5
  145. package/dist/types/operator/index.d.ts.map +1 -1
  146. package/dist/types/operator/mcp.d.ts +1 -1
  147. package/dist/types/operator/mcp.d.ts.map +1 -1
  148. package/dist/types/operator/operator.d.ts +15 -197
  149. package/dist/types/operator/operator.d.ts.map +1 -1
  150. package/dist/types/operator/refs.d.ts +25 -0
  151. package/dist/types/operator/refs.d.ts.map +1 -0
  152. package/dist/types/operator/render.d.ts +59 -19
  153. package/dist/types/operator/render.d.ts.map +1 -1
  154. package/dist/types/operator/session-tools.d.ts +61 -0
  155. package/dist/types/operator/session-tools.d.ts.map +1 -0
  156. package/dist/types/operator/types.d.ts +441 -0
  157. package/dist/types/operator/types.d.ts.map +1 -0
  158. package/dist/types/operator/view.d.ts +19 -29
  159. package/dist/types/operator/view.d.ts.map +1 -1
  160. package/dist/types/protocol/config.d.ts +32 -24
  161. package/dist/types/protocol/config.d.ts.map +1 -1
  162. package/dist/types/protocol/conversation.d.ts +48 -39
  163. package/dist/types/protocol/conversation.d.ts.map +1 -1
  164. package/dist/types/protocol/execution.d.ts +2 -18
  165. package/dist/types/protocol/execution.d.ts.map +1 -1
  166. package/dist/types/protocol/history.d.ts +1 -1
  167. package/dist/types/protocol/history.d.ts.map +1 -1
  168. package/dist/types/protocol/index.d.ts +1 -0
  169. package/dist/types/protocol/index.d.ts.map +1 -1
  170. package/dist/types/protocol/lifecycle.d.ts +48 -74
  171. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  172. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  173. package/dist/types/protocol/models.d.ts +4 -42
  174. package/dist/types/protocol/models.d.ts.map +1 -1
  175. package/dist/types/protocol/origin.d.ts +49 -0
  176. package/dist/types/protocol/origin.d.ts.map +1 -0
  177. package/dist/types/protocol/reduce.d.ts.map +1 -1
  178. package/dist/types/protocol/thinking.d.ts +19 -19
  179. package/dist/types/protocol/thinking.d.ts.map +1 -1
  180. package/dist/types/protocol/wire-api.d.ts +59 -106
  181. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  182. package/dist/types/protocol/wire.d.ts +81 -119
  183. package/dist/types/protocol/wire.d.ts.map +1 -1
  184. package/dist/types/server/routes.d.ts.map +1 -1
  185. package/docs/consuming.md +124 -132
  186. package/docs/deploy.md +6 -5
  187. package/docs/harnesses.md +79 -31
  188. package/docs/sync-history.md +2 -1
  189. package/package.json +4 -4
  190. package/src/client/client.ts +36 -9
  191. package/src/client/connection.ts +31 -1
  192. package/src/client/turns.ts +2 -1
  193. package/src/core/agents/acp/acp-agent.ts +123 -17
  194. package/src/core/agents/acp/mappings.ts +5 -1
  195. package/src/core/agents/acp/requests.ts +2 -0
  196. package/src/core/agents/base.ts +2 -0
  197. package/src/core/agents/claude-code/approvals.ts +4 -3
  198. package/src/core/agents/claude-code/claude-agent.ts +14 -2
  199. package/src/core/agents/claude-code/mcp.ts +11 -1
  200. package/src/core/agents/claude-code/models.ts +15 -10
  201. package/src/core/agents/claude-code/options.ts +33 -3
  202. package/src/core/agents/claude-code/session.ts +26 -1
  203. package/src/core/agents/claude-code/tools.ts +10 -4
  204. package/src/core/agents/claude-code/turn.ts +4 -0
  205. package/src/core/agents/codex-app-server/adapter.ts +2 -0
  206. package/src/core/agents/codex-app-server/client.ts +11 -5
  207. package/src/core/agents/codex-app-server/codex-agent.ts +45 -1
  208. package/src/core/agents/codex-app-server/effort.ts +15 -20
  209. package/src/core/agents/codex-app-server/mcp.ts +46 -23
  210. package/src/core/agents/codex-app-server/models.ts +10 -10
  211. package/src/core/agents/codex-app-server/options.ts +16 -2
  212. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  213. package/src/core/agents/codex-app-server/requests.ts +2 -0
  214. package/src/core/agents/codex-app-server/session.ts +19 -4
  215. package/src/core/agents/codex-app-server/thread-settings.ts +31 -11
  216. package/src/core/agents/host-tools.ts +649 -0
  217. package/src/core/agents/mcp-config.ts +15 -3
  218. package/src/core/agents/scripted/scripted-agent.ts +51 -5
  219. package/src/core/agents/scripted/steps.ts +5 -1
  220. package/src/core/agents/session-agent.ts +78 -2
  221. package/src/core/agents/thinking-levels.ts +9 -6
  222. package/src/core/index.ts +9 -0
  223. package/src/core/presets.ts +28 -4
  224. package/src/core/processes/lifeline.ts +9 -5
  225. package/src/core/provision/pins.ts +7 -16
  226. package/src/core/provision/releases.ts +25 -25
  227. package/src/core/proxy/loopback.ts +25 -4
  228. package/src/core/runtime/agent-runtime.ts +31 -7
  229. package/src/core/runtime/event-processor.ts +13 -6
  230. package/src/core/runtime/origin-line.ts +43 -0
  231. package/src/core/runtime/request-broker.ts +2 -0
  232. package/src/core/runtime/steering.ts +8 -4
  233. package/src/core/runtime/turn.ts +11 -4
  234. package/src/operator/errors.ts +218 -0
  235. package/src/operator/index.ts +18 -23
  236. package/src/operator/mcp.ts +7 -16
  237. package/src/operator/operator.ts +1583 -613
  238. package/src/operator/refs.ts +75 -0
  239. package/src/operator/render.ts +208 -98
  240. package/src/operator/session-tools.ts +126 -0
  241. package/src/operator/types.ts +505 -0
  242. package/src/operator/view.ts +93 -42
  243. package/src/protocol/config.ts +36 -7
  244. package/src/protocol/conversation.ts +3 -0
  245. package/src/protocol/history.ts +2 -1
  246. package/src/protocol/index.ts +1 -0
  247. package/src/protocol/lifecycle.ts +21 -0
  248. package/src/protocol/model-catalog.ts +58 -18
  249. package/src/protocol/models.ts +6 -26
  250. package/src/protocol/origin.ts +47 -0
  251. package/src/protocol/reduce.ts +4 -0
  252. package/src/protocol/thinking.ts +27 -14
  253. package/src/protocol/wire-api.ts +2 -2
  254. package/src/protocol/wire.ts +18 -0
  255. package/src/server/bin.ts +6 -4
  256. package/src/server/routes.ts +16 -5
@@ -1,6 +1,6 @@
1
1
  import { AsyncQueue, clampThinkingLevel, } from "../../../protocol/index.js";
2
2
  import { onAbort } from "../../utils/deadline.js";
3
- import { knownThinkingLevels } from "../thinking-levels.js";
3
+ import { listedThinkingLevels } from "../thinking-levels.js";
4
4
  import { buildClaudeOptions } from "./options.js";
5
5
  /**
6
6
  * Claude Code's model rows in the shared shape. `default` (the account's
@@ -18,7 +18,7 @@ export function claudeModelDescriptors(models) {
18
18
  ...(model.resolvedModel &&
19
19
  model.resolvedModel !== model.value && { resolvedId: model.resolvedModel }),
20
20
  ...(model.value === "default" && { isDefault: true }),
21
- thinking: { levels: knownThinkingLevels(model.supportedEffortLevels ?? []) },
21
+ thinking: { levels: listedThinkingLevels(model.supportedEffortLevels ?? []) },
22
22
  }));
23
23
  }
24
24
  /**
@@ -26,20 +26,26 @@ export function claudeModelDescriptors(models) {
26
26
  * lowers a level its model lacks on its own and does not say so (`max` and
27
27
  * `xhigh` become `high`), so the level is fitted here, where the model's levels
28
28
  * are known. `off` is sent as it is: some models do not let thinking be
29
- * disabled, and Claude Code then thinks at its default effort; Haiku, which
30
- * Claude Code otherwise starts thinking, does not think with it. A model known
31
- * to have no effort levels (`levels: []`, Haiku) is sent no other level, so it
32
- * is not started on adaptive thinking at an effort it has no control for; a
33
- * model whose levels are unknown (`undefined`) is sent the level as asked.
29
+ * disabled (Haiku 5.5 at `off` still spends thinking tokens, with an empty
30
+ * reasoning part, on Claude Code 2.1.327); Haiku 4.5, which Claude Code
31
+ * otherwise starts thinking, does not think with it. A model known to have no
32
+ * effort levels (`levels: []`, Haiku 4.5) is sent no other level, so it is not
33
+ * started on adaptive thinking at an effort it has no control for: it thinks
34
+ * at its own default, as Claude Code starts it. Neither is a model sent a
35
+ * level that has no fit among the levels it lists. A model whose levels are
36
+ * unknown (`undefined`) is sent the level as asked, but `ultra` as `max`,
37
+ * Claude's ceiling; a model that lists `ultra` is sent it.
34
38
  */
35
39
  export function fitClaudeThinking(level, levels) {
36
40
  if (level === undefined)
37
41
  return {};
38
- if (level === "off" || levels === undefined)
42
+ if (level === "off")
39
43
  return { send: level };
40
- if (!levels.length)
44
+ if (levels === undefined)
45
+ return { send: level === "ultra" ? "max" : level };
46
+ const fitted = clampThinkingLevel(level, levels);
47
+ if (fitted === undefined)
41
48
  return {};
42
- const fitted = clampThinkingLevel(level, levels) ?? level;
43
49
  return { send: fitted, applied: fitted };
44
50
  }
45
51
  /**
@@ -149,6 +149,10 @@ function buildEnv(config, level) {
149
149
  }),
150
150
  };
151
151
  }
152
+ /** Whether Claude Code reads a flag as on: `1`, `true`, `yes` or `on`, in any case (2.1.327). */
153
+ function envFlagOn(value) {
154
+ return ["1", "true", "yes", "on"].includes((value ?? "").trim().toLowerCase());
155
+ }
152
156
  /** The environment Claude Code runs a turn with this `env` in, which decides whose login it runs on. */
153
157
  export function claudeEnv(config) {
154
158
  return buildEnv(config, inherited(config));
@@ -191,8 +195,10 @@ export function buildClaudeOptions(config, sdkMcpServers, overrides, spawn) {
191
195
  }
192
196
  else {
193
197
  options.thinking = { type: "adaptive" };
194
- // Claude's ceiling is `max`.
195
- options.effort = config.thinkingLevel === "ultra" ? "max" : config.thinkingLevel;
198
+ // The level as `fitClaudeThinking` gave it. The SDK types `effort` as
199
+ // the levels it knows; one beyond them (a level the model lists, or
200
+ // any when its levels are unknown) goes to the CLI as it is named.
201
+ options.effort = config.thinkingLevel;
196
202
  }
197
203
  }
198
204
  if (config.resumeSessionId) {
@@ -217,8 +223,23 @@ export function buildClaudeOptions(config, sdkMcpServers, overrides, spawn) {
217
223
  // ClaudeSdkOptionOverrides type excludes them anyway).
218
224
  if (overrides)
219
225
  Object.assign(options, overrides);
226
+ // @ref LLP 0001#d4--permission-requests-are-first-class-protocol — plan mode asks through the broker
227
+ // On a model with auto mode, plan mode takes auto mode's semantics: calls run that the broker
228
+ // never sees. Off beneath the operator's own settings object; a path, or `extraArgs.settings`, stays theirs.
229
+ if (typeof options.settings !== "string" && options.extraArgs?.settings === undefined)
230
+ options.settings = { useAutoModeDuringPlan: false, ...options.settings };
220
231
  if (config.subagentMode === "turn")
221
232
  options.forwardSubagentText = true;
233
+ // @ref LLP 0001#d14--nothing-the-agent-does-is-off-the-stream — a watch outlives its turn
234
+ // Claude Code 2.1.327 offers `Monitor` with background tasks off: its watch
235
+ // runs past the turn and Claude answers each line with a query of its own,
236
+ // which no turn shows. With background tasks off it is off too, beside the
237
+ // operator's own denials; an env that turns them on leaves it alone.
238
+ if (envFlagOn(options.env?.CLAUDE_CODE_DISABLE_BACKGROUND_TASKS)) {
239
+ const denied = options.disallowedTools ?? [];
240
+ if (!denied.includes("Monitor"))
241
+ options.disallowedTools = [...denied, "Monitor"];
242
+ }
222
243
  // Engine wiring over the operator's: their spawner still runs, as theirs.
223
244
  if (spawn)
224
245
  options.spawnClaudeCodeProcess = claudeSpawner(spawn, overrides?.spawnClaudeCodeProcess, overrides?.stderr);
@@ -170,12 +170,25 @@ export class ClaudeSession {
170
170
  }
171
171
  return true;
172
172
  }
173
+ /** The turn running now and the mode the process runs in, for a host tool's caller. */
174
+ liveTurn() {
175
+ return this.turn
176
+ ? {
177
+ turnId: this.turn.id,
178
+ permissionMode: this.config.permissionMode ?? "default",
179
+ signal: this.turn.signal,
180
+ }
181
+ : undefined;
182
+ }
173
183
  async start() {
174
184
  if (this.state !== "starting")
175
185
  throw new Error(`Cannot start in state: ${this.state}`);
176
186
  const sdk = await import("@anthropic-ai/claude-agent-sdk");
177
187
  const spawn = { sessionId: this.id, cwd: this.config.cwd };
178
- const hostServers = await this.settings.sdkMcpServers?.(spawn);
188
+ const source = await this.settings.host?.source(this.id, this.config.cwd);
189
+ const hostServers = { ...(await this.settings.sdkMcpServers?.(spawn)) };
190
+ if (source && this.settings.host)
191
+ hostServers[source.name] = this.settings.host.inProcess(this.id, source, () => this.liveTurn());
179
192
  this.mcp.useHostServers(hostServers);
180
193
  const overrides = await this.settings.sdkOptions?.(spawn);
181
194
  this.thinkingOverridden = Boolean(overrides && ("effort" in overrides || "thinking" in overrides));
@@ -186,6 +199,13 @@ export class ClaudeSession {
186
199
  this.native = native;
187
200
  },
188
201
  });
202
+ // @ref LLP 0018#permissions — a read-only host tool runs unasked, as on Codex; any other asks
203
+ const readOnly = (source?.tools ?? []).filter((tool) => tool.annotations?.readOnly);
204
+ if (source && readOnly.length)
205
+ options.allowedTools = [
206
+ ...(options.allowedTools ?? []),
207
+ ...readOnly.map((tool) => `mcp__${source.name}__${tool.name}`),
208
+ ];
189
209
  if (this.settings.hooks) {
190
210
  options.hooks = this.settings.hooks({
191
211
  sessionId: this.id,
@@ -11,12 +11,14 @@
11
11
  * CLI offers that is not here, so a pin bump decides it.
12
12
  */
13
13
  export const CLAUDE_TOOL_KINDS = new Map([
14
- // What the pinned CLI (2.1.288) offers, with the engine's options or a product's.
14
+ // What the pinned CLI offers, with the engine's options or a product's.
15
15
  ["Read", "read"],
16
16
  ["Edit", "edit"],
17
17
  ["Write", "edit"],
18
18
  ["NotebookEdit", "edit"],
19
19
  ["Bash", "execute"],
20
+ // Runs a shell command and reports its output as it comes.
21
+ ["Monitor", "execute"],
20
22
  // Stops a background shell or sub-agent the agent started.
21
23
  ["TaskStop", "execute"],
22
24
  // Runs a script the agent wrote, which starts sub-agents of its own.
@@ -25,6 +27,8 @@ export const CLAUDE_TOOL_KINDS = new Map([
25
27
  ["WebSearch", "fetch"],
26
28
  // Loads a skill's instructions into the conversation.
27
29
  ["Skill", "read"],
30
+ // Loads the definition of a tool the CLI held back: nothing of the person's.
31
+ ["ToolSearch", "other"],
28
32
  // The sub-agent tool: `init` names it `Task`, the model `Agent`. A spawn is `task` by its subagent.
29
33
  ["Task", "think"],
30
34
  ["Agent", "think"],
@@ -71,8 +75,8 @@ function toolLocationsFromInput(input) {
71
75
  }
72
76
  const MCP_PREFIX = "mcp__";
73
77
  /**
74
- * A server's name as Claude Code spells it in a tool name (the pinned CLI
75
- * 2.1.288, SDK 0.3.288, and 2.1.325 alike):
78
+ * A server's name as Claude Code spells it in a tool name (the pinned CLI;
79
+ * 2.1.288 and 2.1.325 spell it alike, observed 2026-11-16):
76
80
  * other than letters, digits, `_` and `-` become `_`, and a claude.ai
77
81
  * connector's runs of `_` collapse.
78
82
  */
@@ -119,7 +123,8 @@ function textOf(input, key) {
119
123
  */
120
124
  export function claudeToolMeta(toolName, input, servers) {
121
125
  const mcp = claudeMcpTool(toolName, servers);
122
- const command = toolName === "Bash" ? textOf(input, "command") : undefined;
126
+ // A `Monitor` on a WebSocket has no command.
127
+ const command = toolName === "Bash" || toolName === "Monitor" ? textOf(input, "command") : undefined;
123
128
  const url = toolName === "WebFetch" ? textOf(input, "url") : undefined;
124
129
  return {
125
130
  kind: CLAUDE_TOOL_KINDS.get(toolName) ?? "other",
@@ -13,6 +13,7 @@ const STEER_QUERY_TIMEOUT_MS = 60_000;
13
13
  */
14
14
  export class ClaudeTurn {
15
15
  id;
16
+ signal;
16
17
  permissions;
17
18
  elicitation;
18
19
  /** What the turn yields, until its result or its failure. */
@@ -48,6 +49,7 @@ export class ClaudeTurn {
48
49
  watchdog;
49
50
  constructor(start, prompt) {
50
51
  this.id = start.turnId;
52
+ this.signal = start.signal;
51
53
  this.permissions = start.permissions;
52
54
  this.elicitation = start.elicitation;
53
55
  this.prompt = prompt;
@@ -442,6 +442,8 @@ export function codexErrorCategory(info) {
442
442
  case "unauthorized":
443
443
  return "auth";
444
444
  case "badRequest":
445
+ // A model the provider does not have, or the account cannot run.
446
+ case "modelNotFound":
445
447
  return "invalid_request";
446
448
  case "internalServerError":
447
449
  return "internal";
@@ -44,6 +44,13 @@ export class CodexRpcError extends Error {
44
44
  this.name = "CodexRpcError";
45
45
  }
46
46
  }
47
+ /** The `-c key=value` overrides the app-server starts with, for these options. */
48
+ export function configArgs(opts) {
49
+ return [
50
+ ...(opts.ephemeralCredentials ? ["-c", 'cli_auth_credentials_store="ephemeral"'] : []),
51
+ ...(opts.pluginsOff ? ["-c", "features.plugins=false"] : []),
52
+ ];
53
+ }
47
54
  /**
48
55
  * Newline-delimited (JSONL) JSON-RPC client over a `codex app-server` subprocess.
49
56
  * Requests get monotonic ids resolved against a pending map; notifications fan
@@ -203,11 +210,7 @@ export class CodexAppServerClient {
203
210
  this.nativeShutdownFailed = false;
204
211
  const native = this.opts.processes.spawn({
205
212
  command: this.opts.codexPath ?? "codex",
206
- args: [
207
- "app-server",
208
- ...(this.opts.ephemeralCredentials ? ["-c", 'cli_auth_credentials_store="ephemeral"'] : []),
209
- ...(this.opts.pluginsOff ? ["-c", "features.plugins=false"] : []),
210
- ],
213
+ args: ["app-server", ...configArgs(this.opts)],
211
214
  cwd: this.opts.cwd,
212
215
  env: this.opts.env ?? process.env,
213
216
  harness: "codex-app-server",
@@ -1,6 +1,8 @@
1
1
  import { sharedNativeProcesses } from "../../processes/native-processes.js";
2
2
  import { onAbort, untilAborted } from "../../utils/deadline.js";
3
+ import { McpUnavailableError } from "../../utils/errors.js";
3
4
  import { configFingerprint } from "../config-fingerprint.js";
5
+ import { hostToolSessions, refuseHostName, } from "../host-tools.js";
4
6
  import { IDLE_TIMEOUT_MS } from "../process-pool.js";
5
7
  import { SessionAgent } from "../session-agent.js";
6
8
  import { CodexAppServerClient, } from "./client.js";
@@ -55,6 +57,7 @@ export class CodexAppServerAgent extends SessionAgent {
55
57
  processes;
56
58
  timing;
57
59
  sessions;
60
+ host;
58
61
  login = new CodexCliLogin({
59
62
  connect: (env) => this.connect({ cwd: process.cwd(), ...(env && { env }) }, { experimentalApi: false }),
60
63
  cliPath: async () => (await this.cliPath()) ?? "codex",
@@ -87,6 +90,9 @@ export class CodexAppServerAgent extends SessionAgent {
87
90
  inheritConfig: agentOptions.inheritConfig,
88
91
  builtInTools: agentOptions.builtInTools,
89
92
  });
93
+ this.host = hostToolSessions(agentOptions.hostTools, this.harness, {
94
+ live: (sessionId) => this.sessions.live(sessionId),
95
+ });
90
96
  }
91
97
  /** On a product's credentials, each session may list as a different account. */
92
98
  get modelsPerSession() {
@@ -100,6 +106,7 @@ export class CodexAppServerAgent extends SessionAgent {
100
106
  signal,
101
107
  items: new TurnItems(),
102
108
  mode: options.permissionMode ?? "default",
109
+ turnId: options.turnId,
103
110
  };
104
111
  let session;
105
112
  let stop;
@@ -129,7 +136,8 @@ export class CodexAppServerAgent extends SessionAgent {
129
136
  const predecessor = this.sessions.stopUnsettled(options.sessionId);
130
137
  if (predecessor && !(await predecessor))
131
138
  throw new Error("Previous Codex execution has not stopped");
132
- const mcp = this.sessions.mcpFor(options);
139
+ const host = await untilAborted(this.hostServer(options), signal);
140
+ const mcp = this.sessions.mcpFor(options, host);
133
141
  scope.auth = await authFor({
134
142
  credentials: this.agentOptions.credentials,
135
143
  sessionId: options.sessionId,
@@ -188,6 +196,29 @@ export class CodexAppServerAgent extends SessionAgent {
188
196
  this.sessions.leave(options.sessionId, session, scope);
189
197
  }
190
198
  }
199
+ hostSecret(sessionId) {
200
+ return this.host?.secretOf(sessionId);
201
+ }
202
+ /** The host's server for the session, by the name its source gives, when it has one. */
203
+ async hostServer(options) {
204
+ const lease = await this.host?.lease(options.sessionId, options.cwd);
205
+ if (!lease)
206
+ return undefined;
207
+ refuseHostName(options.mcpServers, lease.source.name);
208
+ try {
209
+ return { [lease.source.name]: await lease.http() };
210
+ }
211
+ catch (error) {
212
+ // @ref LLP 0018#codex — a loopback that cannot start: the turn goes on without the host's tools, and says so
213
+ if (!(error instanceof McpUnavailableError))
214
+ throw error;
215
+ await options.onWarning?.({
216
+ category: "mcp_unavailable",
217
+ message: `${error.message.split(";")[0]}; the turn goes on without the host's tools`,
218
+ });
219
+ return undefined;
220
+ }
221
+ }
191
222
  /**
192
223
  * The user's own login, for a turn (or a model list) whose `env` names a
193
224
  * home, when the operator set `userLogin`: read again for each, so a login
@@ -261,10 +292,12 @@ export class CodexAppServerAgent extends SessionAgent {
261
292
  await super.release(sessionId);
262
293
  // @ref LLP 0001#d10--built-in-turns-own-their-logical-session-through-cleanup — released once its process has exited, as a Claude session is
263
294
  await this.sessions.release(sessionId);
295
+ this.host?.release(sessionId);
264
296
  }
265
297
  async terminateAll() {
266
298
  await super.terminateAll();
267
299
  await this.sessions.terminateAll();
300
+ await this.host?.close();
268
301
  }
269
302
  /**
270
303
  * The CLI to run: the provisioner's, else `$CODEX_CLI_PATH`; PATH's `codex`
@@ -285,7 +318,7 @@ export class CodexAppServerAgent extends SessionAgent {
285
318
  const codexPath = (await this.cliPath(signal)) ?? "codex";
286
319
  signal?.throwIfAborted();
287
320
  const clientOptions = {
288
- ...clientOptionsFor(codexPath, options, auth, experimentalApi, this.agentOptions.inheritConfig),
321
+ ...clientOptionsFor(codexPath, options, auth, experimentalApi, this.agentOptions.inheritConfig, this.host !== undefined),
289
322
  processes: this.processes,
290
323
  ...(sessionId !== undefined && { sessionId }),
291
324
  };
@@ -2,12 +2,19 @@ import { clampThinkingLevel } from "../../../protocol/index.js";
2
2
  /**
3
3
  * The effort Codex runs for `level`. `supported` is what the model accepts, in
4
4
  * this vocabulary, where `off` stands for its least effort: `offEffort`, the
5
- * native `none` or `minimal` the model advertises. A level it lacks becomes the
6
- * nearest one it has. Without `supported`, `off` becomes `low`: no model the
7
- * pinned CLI lists accepts `none` or `minimal`, and every one accepts `low`.
5
+ * native `none` or `minimal` the model advertises. A level it lists is sent as
6
+ * it is, and one it lacks is fitted to the nearest level this build can order;
7
+ * when there is none (a level this build does not know, or a model listing
8
+ * none it knows), the level is not applied: `undefined`. Without `supported`,
9
+ * the level is sent as asked, and `off` becomes `low`: no model the pinned CLI
10
+ * lists accepts `none` or `minimal`, and every one accepts `low`.
8
11
  */
9
12
  export function codexReasoningEffort(level, supported, offEffort) {
10
- const clamped = supported?.length ? clampThinkingLevel(level, supported) : undefined;
11
- const effective = clamped ?? (level === "off" ? "low" : level);
12
- return effective === "off" ? (offEffort ?? "minimal") : effective;
13
+ if (supported?.length) {
14
+ const clamped = clampThinkingLevel(level, supported);
15
+ if (clamped === undefined)
16
+ return undefined;
17
+ return clamped === "off" ? (offEffort ?? "minimal") : clamped;
18
+ }
19
+ return level === "off" ? "low" : level;
13
20
  }
@@ -5,7 +5,7 @@ import { configFingerprint } from "../config-fingerprint.js";
5
5
  import { UnavailableMcpServers, mcpConnectionErrors, settleMcpFailures, snapshotMcpServers, } from "../mcp-config.js";
6
6
  import { pages } from "./client.js";
7
7
  /** Codex supports stdio and Streamable HTTP, not the legacy SSE transport. */
8
- function codexMcpServers(servers) {
8
+ export function codexMcpServers(servers) {
9
9
  return Object.fromEntries(Object.entries(servers).map(([name, server]) => {
10
10
  if (name === "codex_apps")
11
11
  throw new Error("MCP server name codex_apps is reserved by Codex");
@@ -14,13 +14,22 @@ function codexMcpServers(servers) {
14
14
  }
15
15
  return [
16
16
  name,
17
- server.type === "stdio"
18
- ? {
19
- command: server.command,
20
- ...(server.args && { args: server.args }),
21
- ...(server.env && { env: server.env }),
22
- }
23
- : { url: server.url, ...(server.headers && { http_headers: server.headers }) },
17
+ {
18
+ ...(server.type === "stdio"
19
+ ? {
20
+ command: server.command,
21
+ ...(server.args && { args: server.args }),
22
+ ...(server.env && { env: server.env }),
23
+ }
24
+ : { url: server.url, ...(server.headers && { http_headers: server.headers }) }),
25
+ // Whole seconds, rounded up (1118 ms becomes 2 s): Codex reads a fraction
26
+ // back as a float (1.118 as 1.1179999999999999, Codex 0.160.0), which a
27
+ // home's read-back takes for an override; an integer reads back unchanged.
28
+ // The thread's config and its fingerprint take this same value.
29
+ ...(server.timeoutMs !== undefined && {
30
+ tool_timeout_sec: Math.ceil(server.timeoutMs / 1000),
31
+ }),
32
+ },
24
33
  ];
25
34
  }));
26
35
  }
@@ -177,13 +186,21 @@ async function settleCodexMcp(client, threadId, servers, causes) {
177
186
  updates.end();
178
187
  }
179
188
  }
180
- /** The turn's servers, snapshotted before anything asynchronous can let the caller change them. */
181
- export function codexTurnMcp(options, homes) {
182
- const wire = snapshotMcpServers(options.mcpServers);
183
- const servers = codexMcpServers(wire);
189
+ // @ref LLP 0018#codex — the host's server rides the thread, never a home's config file
190
+ /**
191
+ * The turn's servers, snapshotted before anything asynchronous can let the
192
+ * caller change them. The host's server goes in the thread's own config
193
+ * whether or not the turn names a home: its secret is never written to disk.
194
+ */
195
+ export function codexTurnMcp(options, homes, host = {}) {
196
+ const turn = snapshotMcpServers(options.mcpServers);
197
+ const wire = { ...turn, ...host };
198
+ const servers = codexMcpServers(turn);
199
+ const own = codexMcpServers(host);
184
200
  if (homes.claim(options))
185
- return { wire, servers };
186
- return Object.keys(servers).length ? { wire, thread: servers } : { wire };
201
+ return Object.keys(own).length ? { wire, servers, thread: own } : { wire, servers };
202
+ const thread = { ...servers, ...own };
203
+ return Object.keys(thread).length ? { wire, thread } : { wire };
187
204
  }
188
205
  // @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — a thread's config is fixed once it is open
189
206
  /**
@@ -257,9 +274,9 @@ export class CodexMcpState {
257
274
  * Read before the thread opens, so no connection is made first, and
258
275
  * before each turn.
259
276
  */
260
- async threadServers({ servers, thread, }) {
261
- // A home the turn names holds the turn's servers, written there.
262
- if (servers !== undefined || !thread)
277
+ async threadServers({ thread }) {
278
+ // A home the turn names holds the turn's servers, written there; the thread only the host's.
279
+ if (!thread)
263
280
  return undefined;
264
281
  const result = (await this.client.request("config/read", { cwd: this.cwd }));
265
282
  assertCodexThreadMcp(result.config?.mcp_servers ?? {}, thread);
@@ -276,7 +293,7 @@ export class CodexMcpState {
276
293
  // down), or at once when this turn requires it: its old failure must
277
294
  // not decide. So is every server after a turn whose servers did not settle.
278
295
  const again = this.unavailable.retryNow(mcp.wire).length > 0 || this.unsettled;
279
- const started = mcp.thread
296
+ const started = mcp.servers === undefined
280
297
  ? await this.startThreadServers(again)
281
298
  : await this.startHomeServers(mcp, again);
282
299
  let unavailable;
@@ -1,17 +1,17 @@
1
- import { isThinkingLevel } from "../../../protocol/index.js";
2
1
  import { onAbort } from "../../utils/deadline.js";
3
- import { knownThinkingLevels } from "../thinking-levels.js";
2
+ import { listedThinkingLevels } from "../thinking-levels.js";
4
3
  import { pages } from "./client.js";
5
4
  const MODEL_LIST_TIMEOUT_MS = 5_000;
6
5
  /** How many pages of `model/list` are read; Codex lists a dozen models in one. */
7
6
  const MODEL_LIST_PAGES = 10;
8
- /** The level a native Codex effort is: `none` and `minimal` are `off`. */
7
+ /**
8
+ * The level a native Codex effort is: `none` and `minimal` are `off`, and any
9
+ * other effort is a level of its own name, known to this build or not.
10
+ */
9
11
  export function codexThinkingLevel(effort) {
10
12
  if (effort === "none" || effort === "minimal")
11
13
  return "off";
12
- return typeof effort === "string" && effort !== "off" && isThinkingLevel(effort)
13
- ? effort
14
- : undefined;
14
+ return typeof effort === "string" && effort !== "off" && /\S/.test(effort) ? effort : undefined;
15
15
  }
16
16
  function efforts(model) {
17
17
  return Array.isArray(model.supportedReasoningEfforts)
@@ -29,8 +29,8 @@ async function readCodexModels(client) {
29
29
  * What each model accepts, by its `model` and `id`. Codex's `none` or
30
30
  * `minimal` is `off`, remembered so `off` sends the value the model
31
31
  * advertised (`none` when it offers both). A model that lists no efforts at
32
- * all has none (`levels: []`); one that lists only efforts this build does not
33
- * know, or says nothing, is left out, so what it accepts stays unknown.
32
+ * all has none (`levels: []`); one that says nothing is left out, so what it
33
+ * accepts stays unknown.
34
34
  */
35
35
  function codexModelThinking(models) {
36
36
  const thinking = new Map();
@@ -41,7 +41,7 @@ function codexModelThinking(models) {
41
41
  : native.includes("minimal")
42
42
  ? "minimal"
43
43
  : undefined;
44
- const levels = knownThinkingLevels(native.map(codexThinkingLevel));
44
+ const levels = listedThinkingLevels(native.map(codexThinkingLevel));
45
45
  const none = Array.isArray(model.supportedReasoningEfforts) && native.length === 0;
46
46
  if (!levels.length && !none)
47
47
  continue;
@@ -110,7 +110,7 @@ export function codexModelDescriptors(models, harness, configuredModel) {
110
110
  const id = typeof model.model === "string" ? model.model : model.id;
111
111
  if (typeof id !== "string" || model.hidden === true)
112
112
  return [];
113
- const levels = knownThinkingLevels(efforts(model).map(codexThinkingLevel));
113
+ const levels = listedThinkingLevels(efforts(model).map(codexThinkingLevel));
114
114
  const fallback = codexThinkingLevel(model.defaultReasoningEffort);
115
115
  return [
116
116
  {
@@ -1,4 +1,5 @@
1
1
  import { configFingerprint, directoriesFingerprint } from "../config-fingerprint.js";
2
+ import { withLoopbackDirect } from "../host-tools.js";
2
3
  /**
3
4
  * Why a turn without tools (`disableTools`) fails on Codex. With every
4
5
  * feature that offers one turned off, Codex 0.160 still gives a code-mode
@@ -67,11 +68,15 @@ export function codexSessionCompatible(existing, options) {
67
68
  * How the harness starts `codex app-server` for a folder, its `env` and a
68
69
  * login, at the operator's `inheritConfig`.
69
70
  */
70
- export function clientOptionsFor(codexPath, { cwd, env }, auth, experimentalApi, inheritConfig = "all") {
71
+ export function clientOptionsFor(codexPath, { cwd, env }, auth, experimentalApi, inheritConfig = "all",
72
+ /** The engine serves host tools on loopback: reach it directly, past any proxy. */
73
+ loopback = false) {
74
+ const base = env ? { ...process.env, ...env } : process.env;
71
75
  return {
72
76
  codexPath,
73
77
  cwd,
74
- env: env ? { ...process.env, ...env } : process.env,
78
+ // @ref LLP 0018#codex — the bearer never travels through a proxy
79
+ env: loopback ? withLoopbackDirect(base) : base,
75
80
  ...(experimentalApi && { experimentalApi: true }),
76
81
  ...(auth && { ephemeralCredentials: true }),
77
82
  // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — a session's home loads no plugins
@@ -110,6 +115,10 @@ export function threadParams(options, policy = {}, mcpServers) {
110
115
  if (tools.local === false) {
111
116
  features.shell_tool = false;
112
117
  features.unified_exec = false;
118
+ // @ref LLP 0017#each-turn-names-its-environments — view_image has a switch, apply_patch none
119
+ // Since 0.164 Codex lists `view_image` and `apply_patch` whatever a turn's environments:
120
+ // this takes the first away, and `environments: []` refuses a call to the second.
121
+ features.view_image = false;
113
122
  }
114
123
  if (tools.imageGeneration === false)
115
124
  features.image_generation = false;
@@ -60,7 +60,7 @@ export async function* askPlan(turn, native) {
60
60
  kind: CODEX_TOOL_KINDS.plan,
61
61
  title: PLAN_TITLE,
62
62
  rawInput: { plan: offered.text },
63
- }, { signal: asking.signal, options: PLAN_OPTIONS });
63
+ }, { signal: asking.signal, options: PLAN_OPTIONS, purpose: "plan" });
64
64
  }
65
65
  finally {
66
66
  offAbort();
@@ -4,7 +4,7 @@ import { ProcessPool } from "../process-pool.js";
4
4
  import { authFingerprint, logIn } from "./credentials.js";
5
5
  import { CodexHomes, holdHome, homeIdentity } from "./homes.js";
6
6
  import { codexLoginMark, codexLoginStore } from "./login.js";
7
- import { CodexMcpState, codexTurnMcp, threadServersFingerprint } from "./mcp.js";
7
+ import { CodexMcpState, codexTurnMcp, threadServersFingerprint, } from "./mcp.js";
8
8
  import { codexSessionCompatible, processIdentity, threadParams, } from "./options.js";
9
9
  import { bindServerRequests } from "./requests.js";
10
10
  import { CodexThread } from "./thread-settings.js";
@@ -76,9 +76,16 @@ export class CodexSessions {
76
76
  get(sessionId) {
77
77
  return this.pool.get(sessionId);
78
78
  }
79
- /** The turn's MCP servers, in the `CODEX_HOME` the session claims when the turn names one. */
80
- mcpFor(options) {
81
- return codexTurnMcp(options, this.homes);
79
+ /** The turn's MCP servers, in the `CODEX_HOME` the session claims when the turn names one, and the host's. */
80
+ mcpFor(options, host) {
81
+ return codexTurnMcp(options, this.homes, host);
82
+ }
83
+ /** The turn the session runs now, and its mode, for a host tool's caller. */
84
+ live(sessionId) {
85
+ const turn = this.pool.get(sessionId)?.turn;
86
+ return turn?.turnId
87
+ ? { turnId: turn.turnId, permissionMode: turn.mode, signal: turn.signal }
88
+ : undefined;
82
89
  }
83
90
  /**
84
91
  * The session's process for the turn, which takes it (the pool's rule):