@zvada/agent-server 0.3.24 → 0.3.26

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 (190) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/README.md +3 -0
  3. package/dist/client/client.js +93 -34
  4. package/dist/core/agents/acp/acp-agent.js +91 -14
  5. package/dist/core/agents/acp/mappings.js +5 -1
  6. package/dist/core/agents/claude-code/approvals.js +2 -1
  7. package/dist/core/agents/claude-code/claude-agent.js +12 -2
  8. package/dist/core/agents/claude-code/mcp.js +9 -1
  9. package/dist/core/agents/claude-code/models.js +16 -10
  10. package/dist/core/agents/claude-code/options.js +23 -2
  11. package/dist/core/agents/claude-code/session.js +21 -1
  12. package/dist/core/agents/claude-code/tools.js +9 -4
  13. package/dist/core/agents/claude-code/turn.js +2 -0
  14. package/dist/core/agents/codex-app-server/adapter.js +2 -0
  15. package/dist/core/agents/codex-app-server/client.js +8 -5
  16. package/dist/core/agents/codex-app-server/codex-agent.js +35 -2
  17. package/dist/core/agents/codex-app-server/effort.js +13 -6
  18. package/dist/core/agents/codex-app-server/mcp.js +35 -18
  19. package/dist/core/agents/codex-app-server/models.js +10 -10
  20. package/dist/core/agents/codex-app-server/options.js +11 -2
  21. package/dist/core/agents/codex-app-server/session.js +11 -4
  22. package/dist/core/agents/codex-app-server/thread-settings.js +30 -11
  23. package/dist/core/agents/host-tools.js +493 -0
  24. package/dist/core/agents/mcp-config.js +6 -2
  25. package/dist/core/agents/scripted/scripted-agent.js +11 -4
  26. package/dist/core/agents/scripted/steps.js +5 -1
  27. package/dist/core/agents/session-agent.js +70 -2
  28. package/dist/core/agents/thinking-levels.js +8 -5
  29. package/dist/core/index.js +1 -0
  30. package/dist/core/presets.js +12 -2
  31. package/dist/core/processes/identity.js +1 -0
  32. package/dist/core/processes/lifeline.js +13 -1
  33. package/dist/core/processes/process-table.js +1 -0
  34. package/dist/core/provision/extract.js +1 -0
  35. package/dist/core/provision/pins.js +7 -16
  36. package/dist/core/provision/releases.js +25 -25
  37. package/dist/core/proxy/loopback.js +22 -4
  38. package/dist/core/runtime/agent-runtime.js +6 -3
  39. package/dist/core/runtime/turn.js +20 -10
  40. package/dist/protocol/config.js +21 -1
  41. package/dist/protocol/credentials.js +2 -1
  42. package/dist/protocol/errors.js +12 -3
  43. package/dist/protocol/model-catalog.js +58 -18
  44. package/dist/protocol/models.js +6 -27
  45. package/dist/protocol/thinking.js +22 -9
  46. package/dist/protocol/wire-api.js +2 -2
  47. package/dist/server/bin.js +6 -4
  48. package/dist/server/routes.js +3 -2
  49. package/dist/types/client/client.d.ts +6 -1
  50. package/dist/types/client/client.d.ts.map +1 -1
  51. package/dist/types/core/agents/acp/acp-agent.d.ts +15 -0
  52. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  53. package/dist/types/core/agents/acp/mappings.d.ts +5 -1
  54. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  55. package/dist/types/core/agents/acp/requests.d.ts +2 -0
  56. package/dist/types/core/agents/acp/requests.d.ts.map +1 -1
  57. package/dist/types/core/agents/claude-code/approvals.d.ts +2 -1
  58. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  59. package/dist/types/core/agents/claude-code/claude-agent.d.ts +1 -0
  60. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  61. package/dist/types/core/agents/claude-code/mcp.d.ts +8 -1
  62. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  63. package/dist/types/core/agents/claude-code/models.d.ts +9 -5
  64. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  65. package/dist/types/core/agents/claude-code/options.d.ts +14 -1
  66. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  67. package/dist/types/core/agents/claude-code/session.d.ts +2 -0
  68. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  69. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  70. package/dist/types/core/agents/claude-code/turn.d.ts +3 -0
  71. package/dist/types/core/agents/claude-code/turn.d.ts.map +1 -1
  72. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  73. package/dist/types/core/agents/codex-app-server/client.d.ts +2 -0
  74. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  75. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +11 -0
  76. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  77. package/dist/types/core/agents/codex-app-server/effort.d.ts +7 -7
  78. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  79. package/dist/types/core/agents/codex-app-server/mcp.d.ts +11 -3
  80. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  81. package/dist/types/core/agents/codex-app-server/models.d.ts +4 -1
  82. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  83. package/dist/types/core/agents/codex-app-server/options.d.ts +9 -2
  84. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  85. package/dist/types/core/agents/codex-app-server/requests.d.ts +2 -0
  86. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  87. package/dist/types/core/agents/codex-app-server/session.d.ts +6 -3
  88. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  89. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  90. package/dist/types/core/agents/host-tools.d.ts +162 -0
  91. package/dist/types/core/agents/host-tools.d.ts.map +1 -0
  92. package/dist/types/core/agents/mcp-config.d.ts +11 -2
  93. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  94. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
  95. package/dist/types/core/agents/scripted/steps.d.ts +1 -1
  96. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -1
  97. package/dist/types/core/agents/session-agent.d.ts +2 -7
  98. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  99. package/dist/types/core/agents/thinking-levels.d.ts +7 -3
  100. package/dist/types/core/agents/thinking-levels.d.ts.map +1 -1
  101. package/dist/types/core/index.d.ts +1 -0
  102. package/dist/types/core/index.d.ts.map +1 -1
  103. package/dist/types/core/presets.d.ts +11 -1
  104. package/dist/types/core/presets.d.ts.map +1 -1
  105. package/dist/types/core/processes/identity.d.ts.map +1 -1
  106. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  107. package/dist/types/core/processes/process-table.d.ts.map +1 -1
  108. package/dist/types/core/provision/extract.d.ts.map +1 -1
  109. package/dist/types/core/provision/pins.d.ts +5 -4
  110. package/dist/types/core/provision/pins.d.ts.map +1 -1
  111. package/dist/types/core/proxy/loopback.d.ts.map +1 -1
  112. package/dist/types/core/runtime/agent-runtime.d.ts +4 -1
  113. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  114. package/dist/types/core/runtime/turn.d.ts +12 -7
  115. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  116. package/dist/types/protocol/clis.d.ts +3 -3
  117. package/dist/types/protocol/config.d.ts +12 -19
  118. package/dist/types/protocol/config.d.ts.map +1 -1
  119. package/dist/types/protocol/conversation.d.ts +10 -42
  120. package/dist/types/protocol/conversation.d.ts.map +1 -1
  121. package/dist/types/protocol/credentials.d.ts +13 -1
  122. package/dist/types/protocol/credentials.d.ts.map +1 -1
  123. package/dist/types/protocol/errors.d.ts +6 -4
  124. package/dist/types/protocol/errors.d.ts.map +1 -1
  125. package/dist/types/protocol/execution.d.ts +2 -18
  126. package/dist/types/protocol/execution.d.ts.map +1 -1
  127. package/dist/types/protocol/lifecycle.d.ts +12 -76
  128. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  129. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  130. package/dist/types/protocol/models.d.ts +4 -42
  131. package/dist/types/protocol/models.d.ts.map +1 -1
  132. package/dist/types/protocol/thinking.d.ts +19 -19
  133. package/dist/types/protocol/thinking.d.ts.map +1 -1
  134. package/dist/types/protocol/wire-api.d.ts +32 -117
  135. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  136. package/dist/types/protocol/wire.d.ts +24 -125
  137. package/dist/types/protocol/wire.d.ts.map +1 -1
  138. package/dist/types/server/routes.d.ts.map +1 -1
  139. package/docs/consuming.md +92 -12
  140. package/docs/deploy.md +8 -0
  141. package/docs/harnesses.md +88 -27
  142. package/package.json +4 -4
  143. package/src/client/client.ts +114 -44
  144. package/src/core/agents/acp/acp-agent.ts +123 -17
  145. package/src/core/agents/acp/mappings.ts +5 -1
  146. package/src/core/agents/acp/requests.ts +2 -0
  147. package/src/core/agents/claude-code/approvals.ts +2 -1
  148. package/src/core/agents/claude-code/claude-agent.ts +14 -2
  149. package/src/core/agents/claude-code/mcp.ts +11 -1
  150. package/src/core/agents/claude-code/models.ts +15 -10
  151. package/src/core/agents/claude-code/options.ts +37 -3
  152. package/src/core/agents/claude-code/session.ts +26 -1
  153. package/src/core/agents/claude-code/tools.ts +10 -4
  154. package/src/core/agents/claude-code/turn.ts +4 -0
  155. package/src/core/agents/codex-app-server/adapter.ts +2 -0
  156. package/src/core/agents/codex-app-server/client.ts +11 -5
  157. package/src/core/agents/codex-app-server/codex-agent.ts +45 -1
  158. package/src/core/agents/codex-app-server/effort.ts +15 -20
  159. package/src/core/agents/codex-app-server/mcp.ts +46 -23
  160. package/src/core/agents/codex-app-server/models.ts +10 -10
  161. package/src/core/agents/codex-app-server/options.ts +16 -2
  162. package/src/core/agents/codex-app-server/requests.ts +2 -0
  163. package/src/core/agents/codex-app-server/session.ts +19 -4
  164. package/src/core/agents/codex-app-server/thread-settings.ts +31 -11
  165. package/src/core/agents/host-tools.ts +649 -0
  166. package/src/core/agents/mcp-config.ts +15 -3
  167. package/src/core/agents/scripted/scripted-agent.ts +11 -3
  168. package/src/core/agents/scripted/steps.ts +5 -1
  169. package/src/core/agents/session-agent.ts +78 -2
  170. package/src/core/agents/thinking-levels.ts +9 -6
  171. package/src/core/index.ts +9 -0
  172. package/src/core/presets.ts +24 -3
  173. package/src/core/processes/identity.ts +1 -0
  174. package/src/core/processes/lifeline.ts +14 -1
  175. package/src/core/processes/process-table.ts +1 -0
  176. package/src/core/provision/extract.ts +1 -0
  177. package/src/core/provision/pins.ts +7 -16
  178. package/src/core/provision/releases.ts +25 -25
  179. package/src/core/proxy/loopback.ts +25 -4
  180. package/src/core/runtime/agent-runtime.ts +6 -3
  181. package/src/core/runtime/turn.ts +21 -9
  182. package/src/protocol/config.ts +22 -1
  183. package/src/protocol/credentials.ts +19 -5
  184. package/src/protocol/errors.ts +12 -3
  185. package/src/protocol/model-catalog.ts +58 -18
  186. package/src/protocol/models.ts +6 -26
  187. package/src/protocol/thinking.ts +27 -14
  188. package/src/protocol/wire-api.ts +2 -2
  189. package/src/server/bin.ts +6 -4
  190. package/src/server/routes.ts +3 -2
@@ -14,6 +14,8 @@ const STEER_QUERY_TIMEOUT_MS = 60_000;
14
14
  /** What a turn starts with: its id, and the brokers its requests go to. */
15
15
  export interface ClaudeTurnStart {
16
16
  turnId: string;
17
+ /** The turn's cancel: a host tool's call under way ends with it. */
18
+ signal?: AbortSignal;
17
19
  /**
18
20
  * The engine's permission broker for this turn. Absent (a turn driven
19
21
  * outside the runtime), Claude's permission mode alone decides.
@@ -35,6 +37,7 @@ export interface ClaudeTurnStart {
35
37
  */
36
38
  export class ClaudeTurn {
37
39
  readonly id: string;
40
+ readonly signal?: AbortSignal;
38
41
  readonly permissions?: PermissionRequestHandler;
39
42
  readonly elicitation?: ElicitationRequestHandler;
40
43
  /** What the turn yields, until its result or its failure. */
@@ -71,6 +74,7 @@ export class ClaudeTurn {
71
74
 
72
75
  constructor(start: ClaudeTurnStart, prompt: string) {
73
76
  this.id = start.turnId;
77
+ this.signal = start.signal;
74
78
  this.permissions = start.permissions;
75
79
  this.elicitation = start.elicitation;
76
80
  this.prompt = prompt;
@@ -493,6 +493,8 @@ export function codexErrorCategory(info: unknown): ErrorCategory | undefined {
493
493
  case "unauthorized":
494
494
  return "auth";
495
495
  case "badRequest":
496
+ // A model the provider does not have, or the account cannot run.
497
+ case "modelNotFound":
496
498
  return "invalid_request";
497
499
  case "internalServerError":
498
500
  return "internal";
@@ -123,6 +123,16 @@ export class CodexRpcError extends Error {
123
123
  }
124
124
  }
125
125
 
126
+ /** The `-c key=value` overrides the app-server starts with, for these options. */
127
+ export function configArgs(
128
+ opts: Pick<CodexAppServerClientOptions, "ephemeralCredentials" | "pluginsOff">,
129
+ ): string[] {
130
+ return [
131
+ ...(opts.ephemeralCredentials ? ["-c", 'cli_auth_credentials_store="ephemeral"'] : []),
132
+ ...(opts.pluginsOff ? ["-c", "features.plugins=false"] : []),
133
+ ];
134
+ }
135
+
126
136
  export interface CodexAppServerClientOptions {
127
137
  codexPath?: string;
128
138
  cwd?: string;
@@ -324,11 +334,7 @@ export class CodexAppServerClient implements CodexConnection {
324
334
  this.nativeShutdownFailed = false;
325
335
  const native = this.opts.processes.spawn({
326
336
  command: this.opts.codexPath ?? "codex",
327
- args: [
328
- "app-server",
329
- ...(this.opts.ephemeralCredentials ? ["-c", 'cli_auth_credentials_store="ephemeral"'] : []),
330
- ...(this.opts.pluginsOff ? ["-c", "features.plugins=false"] : []),
331
- ],
337
+ args: ["app-server", ...configArgs(this.opts)],
332
338
  cwd: this.opts.cwd,
333
339
  env: this.opts.env ?? process.env,
334
340
  harness: "codex-app-server",
@@ -6,9 +6,16 @@ import type {
6
6
  } from "../../../protocol/index.ts";
7
7
  import { type NativeProcesses, sharedNativeProcesses } from "../../processes/native-processes.ts";
8
8
  import { onAbort, untilAborted } from "../../utils/deadline.ts";
9
+ import { McpUnavailableError } from "../../utils/errors.ts";
9
10
  import type { AgentExecuteOptions, ListModelsOptions, RawAgentEvent } from "../base.ts";
10
11
  import { configFingerprint } from "../config-fingerprint.ts";
11
12
  import type { CredentialSource } from "../credentials.ts";
13
+ import {
14
+ type HostToolSessions,
15
+ type HostTools,
16
+ hostToolSessions,
17
+ refuseHostName,
18
+ } from "../host-tools.ts";
12
19
  import type { InheritConfig } from "../inherit-config.ts";
13
20
  import { IDLE_TIMEOUT_MS } from "../process-pool.ts";
14
21
  import { type DeliverSteer, SessionAgent } from "../session-agent.ts";
@@ -136,6 +143,12 @@ export interface CodexAppServerAgentOptions {
136
143
  * default. Operator-level only.
137
144
  */
138
145
  userLogin?: boolean;
146
+ /**
147
+ * The host's tools for each session (LLP 0018): Codex reaches them at the
148
+ * engine's loopback, by a secret of the session's own in its thread's
149
+ * config. Operator-level only.
150
+ */
151
+ hostTools?: HostTools;
139
152
  }
140
153
 
141
154
  /**
@@ -151,6 +164,7 @@ export class CodexAppServerAgent extends SessionAgent {
151
164
  readonly processes: NativeProcesses;
152
165
  private readonly timing: CodexTurnTiming;
153
166
  private readonly sessions: CodexSessions;
167
+ private readonly host?: HostToolSessions;
154
168
  readonly login = new CodexCliLogin({
155
169
  connect: (env) =>
156
170
  this.connect({ cwd: process.cwd(), ...(env && { env }) }, { experimentalApi: false }),
@@ -190,6 +204,9 @@ export class CodexAppServerAgent extends SessionAgent {
190
204
  builtInTools: agentOptions.builtInTools,
191
205
  },
192
206
  );
207
+ this.host = hostToolSessions(agentOptions.hostTools, this.harness, {
208
+ live: (sessionId) => this.sessions.live(sessionId),
209
+ });
193
210
  }
194
211
 
195
212
  /** On a product's credentials, each session may list as a different account. */
@@ -209,6 +226,7 @@ export class CodexAppServerAgent extends SessionAgent {
209
226
  signal,
210
227
  items: new TurnItems(),
211
228
  mode: options.permissionMode ?? "default",
229
+ turnId: options.turnId,
212
230
  };
213
231
  let session: CodexSession | undefined;
214
232
  let stop: TurnStop | undefined;
@@ -236,7 +254,8 @@ export class CodexAppServerAgent extends SessionAgent {
236
254
  const predecessor = this.sessions.stopUnsettled(options.sessionId);
237
255
  if (predecessor && !(await predecessor))
238
256
  throw new Error("Previous Codex execution has not stopped");
239
- const mcp = this.sessions.mcpFor(options);
257
+ const host = await untilAborted(this.hostServer(options), signal);
258
+ const mcp = this.sessions.mcpFor(options, host);
240
259
  scope.auth = await authFor({
241
260
  credentials: this.agentOptions.credentials,
242
261
  sessionId: options.sessionId,
@@ -288,6 +307,28 @@ export class CodexAppServerAgent extends SessionAgent {
288
307
  }
289
308
  }
290
309
 
310
+ protected override hostSecret(sessionId: string): string | undefined {
311
+ return this.host?.secretOf(sessionId);
312
+ }
313
+
314
+ /** The host's server for the session, by the name its source gives, when it has one. */
315
+ private async hostServer(options: AgentExecuteOptions) {
316
+ const lease = await this.host?.lease(options.sessionId, options.cwd);
317
+ if (!lease) return undefined;
318
+ refuseHostName(options.mcpServers, lease.source.name);
319
+ try {
320
+ return { [lease.source.name]: await lease.http() };
321
+ } catch (error) {
322
+ // @ref LLP 0018#codex — a loopback that cannot start: the turn goes on without the host's tools, and says so
323
+ if (!(error instanceof McpUnavailableError)) throw error;
324
+ await options.onWarning?.({
325
+ category: "mcp_unavailable",
326
+ message: `${error.message.split(";")[0]}; the turn goes on without the host's tools`,
327
+ });
328
+ return undefined;
329
+ }
330
+ }
331
+
291
332
  /**
292
333
  * The user's own login, for a turn (or a model list) whose `env` names a
293
334
  * home, when the operator set `userLogin`: read again for each, so a login
@@ -376,11 +417,13 @@ export class CodexAppServerAgent extends SessionAgent {
376
417
  await super.release(sessionId);
377
418
  // @ref LLP 0001#d10--built-in-turns-own-their-logical-session-through-cleanup — released once its process has exited, as a Claude session is
378
419
  await this.sessions.release(sessionId);
420
+ this.host?.release(sessionId);
379
421
  }
380
422
 
381
423
  override async terminateAll(): Promise<void> {
382
424
  await super.terminateAll();
383
425
  await this.sessions.terminateAll();
426
+ await this.host?.close();
384
427
  }
385
428
 
386
429
  /**
@@ -418,6 +461,7 @@ export class CodexAppServerAgent extends SessionAgent {
418
461
  auth,
419
462
  experimentalApi,
420
463
  this.agentOptions.inheritConfig,
464
+ this.host !== undefined,
421
465
  ),
422
466
  processes: this.processes,
423
467
  ...(sessionId !== undefined && { sessionId }),
@@ -1,29 +1,24 @@
1
1
  import { type ThinkingLevel, clampThinkingLevel } from "../../../protocol/index.ts";
2
2
 
3
- /** Codex reasoning-effort string for a level (matches the `ReasoningEffort` enum). */
4
- type CodexReasoningEffort =
5
- | "none"
6
- | "minimal"
7
- | "low"
8
- | "medium"
9
- | "high"
10
- | "xhigh"
11
- | "max"
12
- | "ultra";
13
-
14
3
  /**
15
4
  * The effort Codex runs for `level`. `supported` is what the model accepts, in
16
5
  * this vocabulary, where `off` stands for its least effort: `offEffort`, the
17
- * native `none` or `minimal` the model advertises. A level it lacks becomes the
18
- * nearest one it has. Without `supported`, `off` becomes `low`: no model the
19
- * pinned CLI lists accepts `none` or `minimal`, and every one accepts `low`.
6
+ * native `none` or `minimal` the model advertises. A level it lists is sent as
7
+ * it is, and one it lacks is fitted to the nearest level this build can order;
8
+ * when there is none (a level this build does not know, or a model listing
9
+ * none it knows), the level is not applied: `undefined`. Without `supported`,
10
+ * the level is sent as asked, and `off` becomes `low`: no model the pinned CLI
11
+ * lists accepts `none` or `minimal`, and every one accepts `low`.
20
12
  */
21
- export function codexReasoningEffort<Off extends "none" | "minimal" = "minimal">(
13
+ export function codexReasoningEffort(
22
14
  level: ThinkingLevel,
23
15
  supported?: readonly ThinkingLevel[],
24
- offEffort?: Off,
25
- ): Exclude<CodexReasoningEffort, "none" | "minimal"> | Off {
26
- const clamped = supported?.length ? clampThinkingLevel(level, supported) : undefined;
27
- const effective = clamped ?? (level === "off" ? "low" : level);
28
- return effective === "off" ? (offEffort ?? ("minimal" as Off)) : effective;
16
+ offEffort?: "none" | "minimal",
17
+ ): string | undefined {
18
+ if (supported?.length) {
19
+ const clamped = clampThinkingLevel(level, supported);
20
+ if (clamped === undefined) return undefined;
21
+ return clamped === "off" ? (offEffort ?? "minimal") : clamped;
22
+ }
23
+ return level === "off" ? "low" : level;
29
24
  }
@@ -14,7 +14,7 @@ import type { CodexHomes } from "./homes.ts";
14
14
  import type { CodexNotification } from "./protocol.ts";
15
15
 
16
16
  /** Codex supports stdio and Streamable HTTP, not the legacy SSE transport. */
17
- function codexMcpServers(servers: Record<string, McpServerConfig>): Record<string, unknown> {
17
+ export function codexMcpServers(servers: Record<string, McpServerConfig>): Record<string, unknown> {
18
18
  return Object.fromEntries(
19
19
  Object.entries(servers).map(([name, server]) => {
20
20
  if (name === "codex_apps") throw new Error("MCP server name codex_apps is reserved by Codex");
@@ -23,13 +23,22 @@ function codexMcpServers(servers: Record<string, McpServerConfig>): Record<strin
23
23
  }
24
24
  return [
25
25
  name,
26
- server.type === "stdio"
27
- ? {
28
- command: server.command,
29
- ...(server.args && { args: server.args }),
30
- ...(server.env && { env: server.env }),
31
- }
32
- : { url: server.url, ...(server.headers && { http_headers: server.headers }) },
26
+ {
27
+ ...(server.type === "stdio"
28
+ ? {
29
+ command: server.command,
30
+ ...(server.args && { args: server.args }),
31
+ ...(server.env && { env: server.env }),
32
+ }
33
+ : { url: server.url, ...(server.headers && { http_headers: server.headers }) }),
34
+ // Whole seconds, rounded up (1118 ms becomes 2 s): Codex reads a fraction
35
+ // back as a float (1.118 as 1.1179999999999999, Codex 0.160.0), which a
36
+ // home's read-back takes for an override; an integer reads back unchanged.
37
+ // The thread's config and its fingerprint take this same value.
38
+ ...(server.timeoutMs !== undefined && {
39
+ tool_timeout_sec: Math.ceil(server.timeoutMs / 1000),
40
+ }),
41
+ },
33
42
  ];
34
43
  }),
35
44
  );
@@ -248,12 +257,28 @@ export interface CodexTurnMcp {
248
257
  thread?: Record<string, unknown>;
249
258
  }
250
259
 
251
- /** The turn's servers, snapshotted before anything asynchronous can let the caller change them. */
252
- export function codexTurnMcp(options: AgentExecuteOptions, homes: CodexHomes): CodexTurnMcp {
253
- const wire = snapshotMcpServers(options.mcpServers);
254
- const servers = codexMcpServers(wire);
255
- if (homes.claim(options)) return { wire, servers };
256
- return Object.keys(servers).length ? { wire, thread: servers } : { wire };
260
+ /** The host's tools, as the session's turns name them (`HostToolSessions.http`). */
261
+ export type CodexHostServer = Record<string, McpServerConfig>;
262
+
263
+ // @ref LLP 0018#codex — the host's server rides the thread, never a home's config file
264
+ /**
265
+ * The turn's servers, snapshotted before anything asynchronous can let the
266
+ * caller change them. The host's server goes in the thread's own config
267
+ * whether or not the turn names a home: its secret is never written to disk.
268
+ */
269
+ export function codexTurnMcp(
270
+ options: AgentExecuteOptions,
271
+ homes: CodexHomes,
272
+ host: CodexHostServer = {},
273
+ ): CodexTurnMcp {
274
+ const turn = snapshotMcpServers(options.mcpServers);
275
+ const wire = { ...turn, ...host };
276
+ const servers = codexMcpServers(turn);
277
+ const own = codexMcpServers(host);
278
+ if (homes.claim(options))
279
+ return Object.keys(own).length ? { wire, servers, thread: own } : { wire, servers };
280
+ const thread = { ...servers, ...own };
281
+ return Object.keys(thread).length ? { wire, thread } : { wire };
257
282
  }
258
283
 
259
284
  // @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
@@ -325,12 +350,9 @@ export class CodexMcpState {
325
350
  * Read before the thread opens, so no connection is made first, and
326
351
  * before each turn.
327
352
  */
328
- async threadServers({
329
- servers,
330
- thread,
331
- }: CodexTurnMcp): Promise<Record<string, unknown> | undefined> {
332
- // A home the turn names holds the turn's servers, written there.
333
- if (servers !== undefined || !thread) return undefined;
353
+ async threadServers({ thread }: CodexTurnMcp): Promise<Record<string, unknown> | undefined> {
354
+ // A home the turn names holds the turn's servers, written there; the thread only the host's.
355
+ if (!thread) return undefined;
334
356
  const result = (await this.client.request("config/read", { cwd: this.cwd })) as {
335
357
  config?: { mcp_servers?: Record<string, unknown> };
336
358
  };
@@ -353,9 +375,10 @@ export class CodexMcpState {
353
375
  // down), or at once when this turn requires it: its old failure must
354
376
  // not decide. So is every server after a turn whose servers did not settle.
355
377
  const again = this.unavailable.retryNow(mcp.wire).length > 0 || this.unsettled;
356
- const started = mcp.thread
357
- ? await this.startThreadServers(again)
358
- : await this.startHomeServers(mcp, again);
378
+ const started =
379
+ mcp.servers === undefined
380
+ ? await this.startThreadServers(again)
381
+ : await this.startHomeServers(mcp, again);
359
382
  let unavailable: Record<string, string>;
360
383
  try {
361
384
  unavailable = await settleCodexMcp(this.client, threadId, mcp.wire, this.causes);
@@ -1,7 +1,6 @@
1
1
  import type { AgentHarness, ModelDescriptor, ThinkingLevel } from "../../../protocol/index.ts";
2
- import { isThinkingLevel } from "../../../protocol/index.ts";
3
2
  import { onAbort } from "../../utils/deadline.ts";
4
- import { knownThinkingLevels } from "../thinking-levels.ts";
3
+ import { listedThinkingLevels } from "../thinking-levels.ts";
5
4
  import { type CodexConnection, pages } from "./client.ts";
6
5
 
7
6
  const MODEL_LIST_TIMEOUT_MS = 5_000;
@@ -20,12 +19,13 @@ export interface CodexModelThinking {
20
19
  defaultEffort?: string;
21
20
  }
22
21
 
23
- /** The level a native Codex effort is: `none` and `minimal` are `off`. */
22
+ /**
23
+ * The level a native Codex effort is: `none` and `minimal` are `off`, and any
24
+ * other effort is a level of its own name, known to this build or not.
25
+ */
24
26
  export function codexThinkingLevel(effort: unknown): ThinkingLevel | undefined {
25
27
  if (effort === "none" || effort === "minimal") return "off";
26
- return typeof effort === "string" && effort !== "off" && isThinkingLevel(effort)
27
- ? effort
28
- : undefined;
28
+ return typeof effort === "string" && effort !== "off" && /\S/.test(effort) ? effort : undefined;
29
29
  }
30
30
 
31
31
  function efforts(model: CodexModel): unknown[] {
@@ -53,8 +53,8 @@ async function readCodexModels(client: Pick<CodexConnection, "request">): Promis
53
53
  * What each model accepts, by its `model` and `id`. Codex's `none` or
54
54
  * `minimal` is `off`, remembered so `off` sends the value the model
55
55
  * advertised (`none` when it offers both). A model that lists no efforts at
56
- * all has none (`levels: []`); one that lists only efforts this build does not
57
- * know, or says nothing, is left out, so what it accepts stays unknown.
56
+ * all has none (`levels: []`); one that says nothing is left out, so what it
57
+ * accepts stays unknown.
58
58
  */
59
59
  function codexModelThinking(models: readonly CodexModel[]): Map<string, CodexModelThinking> {
60
60
  const thinking = new Map<string, CodexModelThinking>();
@@ -65,7 +65,7 @@ function codexModelThinking(models: readonly CodexModel[]): Map<string, CodexMod
65
65
  : native.includes("minimal")
66
66
  ? "minimal"
67
67
  : undefined;
68
- const levels = knownThinkingLevels(native.map(codexThinkingLevel));
68
+ const levels = listedThinkingLevels(native.map(codexThinkingLevel));
69
69
  const none = Array.isArray(model.supportedReasoningEfforts) && native.length === 0;
70
70
  if (!levels.length && !none) continue;
71
71
  const defaultEffort =
@@ -156,7 +156,7 @@ export function codexModelDescriptors(
156
156
  return models.flatMap((model) => {
157
157
  const id = typeof model.model === "string" ? model.model : model.id;
158
158
  if (typeof id !== "string" || model.hidden === true) return [];
159
- const levels = knownThinkingLevels(efforts(model).map(codexThinkingLevel));
159
+ const levels = listedThinkingLevels(efforts(model).map(codexThinkingLevel));
160
160
  const fallback = codexThinkingLevel(model.defaultReasoningEffort);
161
161
  return [
162
162
  {
@@ -1,6 +1,7 @@
1
1
  import type { PermissionMode } from "../../../protocol/index.ts";
2
2
  import type { AgentExecuteOptions } from "../base.ts";
3
3
  import { configFingerprint, directoriesFingerprint } from "../config-fingerprint.ts";
4
+ import { withLoopbackDirect } from "../host-tools.ts";
4
5
  import type { InheritConfig } from "../inherit-config.ts";
5
6
  import type { CodexAppServerClientOptions } from "./client.ts";
6
7
  import type { CodexAuth } from "./credentials.ts";
@@ -102,11 +103,15 @@ export function clientOptionsFor(
102
103
  auth: CodexAuth | undefined,
103
104
  experimentalApi: boolean,
104
105
  inheritConfig: InheritConfig = "all",
106
+ /** The engine serves host tools on loopback: reach it directly, past any proxy. */
107
+ loopback = false,
105
108
  ): Omit<CodexAppServerClientOptions, "processes"> {
109
+ const base = env ? { ...process.env, ...env } : process.env;
106
110
  return {
107
111
  codexPath,
108
112
  cwd,
109
- env: env ? { ...process.env, ...env } : process.env,
113
+ // @ref LLP 0018#codex — the bearer never travels through a proxy
114
+ env: loopback ? withLoopbackDirect(base) : base,
110
115
  ...(experimentalApi && { experimentalApi: true }),
111
116
  ...(auth && { ephemeralCredentials: true }),
112
117
  // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — a session's home loads no plugins
@@ -127,7 +132,12 @@ const MANAGED_SUBAGENTS =
127
132
  * each, Codex's own behaviour) or turns off for every thread (LLP 0017).
128
133
  */
129
134
  export interface CodexBuiltInTools {
130
- /** The local environment: the shell, unified exec, `apply_patch`, `view_image`, `write_stdin`. */
135
+ /**
136
+ * The local environment: the shell, unified exec, `apply_patch`,
137
+ * `view_image`, `write_stdin`. Off, Codex (since 0.164) still lists
138
+ * `apply_patch` to the model and refuses a call to it, so nothing the agent
139
+ * calls reads or writes the disk.
140
+ */
131
141
  local?: boolean;
132
142
  webSearch?: boolean;
133
143
  imageGeneration?: boolean;
@@ -173,6 +183,10 @@ export function threadParams(
173
183
  if (tools.local === false) {
174
184
  features.shell_tool = false;
175
185
  features.unified_exec = false;
186
+ // @ref LLP 0017#each-turn-names-its-environments — view_image has a switch, apply_patch none
187
+ // Since 0.164 Codex lists `view_image` and `apply_patch` whatever a turn's environments:
188
+ // this takes the first away, and `environments: []` refuses a call to the second.
189
+ features.view_image = false;
176
190
  }
177
191
  if (tools.imageGeneration === false) features.image_generation = false;
178
192
  // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — the account's connectors
@@ -34,6 +34,8 @@ export interface CodexTurnScope {
34
34
  items: TurnItems;
35
35
  /** The mode the turn runs in now: its own, until the user approves a plan. */
36
36
  mode: PermissionMode;
37
+ /** The engine's id of the turn, for a host tool's caller. */
38
+ turnId?: string;
37
39
  }
38
40
 
39
41
  /** One question of Codex's request_user_input. */
@@ -1,12 +1,19 @@
1
1
  import { onAbort } from "../../utils/deadline.ts";
2
2
  import { SessionResumeError } from "../../utils/errors.ts";
3
3
  import type { AgentExecuteOptions } from "../base.ts";
4
+ import type { LiveTurn } from "../host-tools.ts";
4
5
  import { ProcessPool } from "../process-pool.ts";
5
6
  import type { CodexConnection } from "./client.ts";
6
7
  import { type CodexAuth, authFingerprint, logIn } from "./credentials.ts";
7
8
  import { CodexHomes, holdHome, homeIdentity } from "./homes.ts";
8
9
  import { codexLoginMark, codexLoginStore } from "./login.ts";
9
- import { CodexMcpState, type CodexTurnMcp, codexTurnMcp, threadServersFingerprint } from "./mcp.ts";
10
+ import {
11
+ type CodexHostServer,
12
+ CodexMcpState,
13
+ type CodexTurnMcp,
14
+ codexTurnMcp,
15
+ threadServersFingerprint,
16
+ } from "./mcp.ts";
10
17
  import type { CodexModelThinking } from "./models.ts";
11
18
  import {
12
19
  type CodexProcessIdentity,
@@ -151,9 +158,17 @@ export class CodexSessions {
151
158
  return this.pool.get(sessionId);
152
159
  }
153
160
 
154
- /** The turn's MCP servers, in the `CODEX_HOME` the session claims when the turn names one. */
155
- mcpFor(options: AgentExecuteOptions): CodexTurnMcp {
156
- return codexTurnMcp(options, this.homes);
161
+ /** The turn's MCP servers, in the `CODEX_HOME` the session claims when the turn names one, and the host's. */
162
+ mcpFor(options: AgentExecuteOptions, host?: CodexHostServer): CodexTurnMcp {
163
+ return codexTurnMcp(options, this.homes, host);
164
+ }
165
+
166
+ /** The turn the session runs now, and its mode, for a host tool's caller. */
167
+ live(sessionId: string): ReturnType<LiveTurn> {
168
+ const turn = this.pool.get(sessionId)?.turn;
169
+ return turn?.turnId
170
+ ? { turnId: turn.turnId, permissionMode: turn.mode, signal: turn.signal }
171
+ : undefined;
157
172
  }
158
173
 
159
174
  /**
@@ -96,18 +96,30 @@ async function modelThinking(
96
96
  return levels && { levels };
97
97
  }
98
98
 
99
- /** An effort fitted to what `model` accepts, so Codex is not asked for one it lacks. */
99
+ /**
100
+ * An effort fitted to what `model` accepts, so Codex is not asked for one it
101
+ * lacks. When the model's levels are known, the answer is always an effort it
102
+ * lists: the fit; else the model's own default effort; else the first level
103
+ * it lists, as Codex names it (its `none` or `minimal` for `off`). The effort
104
+ * as it was only when the model's levels are unknown or it lists none.
105
+ */
100
106
  async function fitEffort(
101
107
  session: CodexSession,
102
108
  model: string | undefined,
103
109
  effort: string,
104
110
  known: readonly ThinkingLevel[] | undefined,
105
111
  ): Promise<string> {
106
- const level = codexThinkingLevel(effort);
107
112
  const accepts = await modelThinking(session, model, known);
108
- return level && accepts?.levels.length
109
- ? codexReasoningEffort(level, accepts.levels, accepts.off)
110
- : effort;
113
+ const first = accepts?.levels[0];
114
+ if (!accepts || first === undefined) return effort;
115
+ const level = codexThinkingLevel(effort);
116
+ return (
117
+ (level && codexReasoningEffort(level, accepts.levels, accepts.off)) ??
118
+ accepts.defaultEffort ??
119
+ // A level the model lists always fits; `off` becomes its native effort.
120
+ codexReasoningEffort(first, accepts.levels, accepts.off) ??
121
+ first
122
+ );
111
123
  }
112
124
 
113
125
  /** One logical session's thread settings, which outlive its processes. */
@@ -174,15 +186,22 @@ export class CodexThread {
174
186
  // A model known to have no reasoning control is sent no effort, and the
175
187
  // turn reports no level: the thread's effort does not apply to it.
176
188
  let noThinking = false;
189
+ // A level with no fit among the levels the model lists is not sent: the
190
+ // turn runs as one that names no level.
191
+ let unlisted = false;
177
192
  if (options.thinkingLevel) {
178
193
  const accepts = await modelThinking(session, next.model, known);
179
194
  if (accepts && !accepts.levels.length) noThinking = true;
180
195
  else {
181
196
  const effort = codexReasoningEffort(options.thinkingLevel, accepts?.levels, accepts?.off);
182
- params.effort = effort;
183
- next = { ...next, effort, effortSet: true };
197
+ if (effort === undefined) unlisted = true;
198
+ else {
199
+ params.effort = effort;
200
+ next = { ...next, effort, effortSet: true };
201
+ }
184
202
  }
185
- } else {
203
+ }
204
+ if (!options.thinkingLevel || unlisted) {
186
205
  // Once a turn set the thread's effort, the default is worked out for
187
206
  // this turn's model. Otherwise, and when Codex cannot say what the
188
207
  // default is, the thread keeps its own effort, fitted to a model the
@@ -203,12 +222,13 @@ export class CodexThread {
203
222
  const collaboration = inCollaborationMode(next, mode);
204
223
  return {
205
224
  params: { ...params, ...collaboration.params },
206
- ...((level || next.reportedModel || noThinking) && {
225
+ ...((level || next.reportedModel || noThinking || unlisted) && {
207
226
  applied: {
208
227
  ...(level && { thinkingLevel: level }),
209
228
  ...(next.reportedModel && { model: next.reportedModel }),
210
- // The turn's level was not sent: its model has no reasoning control.
211
- ...(noThinking && { notApplied: ["thinkingLevel" as const] }),
229
+ // The turn's level was not sent: its model has no reasoning
230
+ // control, or does not list that level.
231
+ ...((noThinking || unlisted) && { notApplied: ["thinkingLevel" as const] }),
212
232
  },
213
233
  }),
214
234
  next: collaboration.next,