@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
@@ -21,6 +21,7 @@ import {
21
21
  type AgentCapabilities,
22
22
  type AgentInput,
23
23
  type ModelDescriptor,
24
+ type PermissionMode,
24
25
  type PermissionToolCall,
25
26
  type RunConfig,
26
27
  inputToText,
@@ -30,6 +31,12 @@ import { onAbort, untilAborted } from "../../utils/deadline.ts";
30
31
  import { AgentServerError, InvalidRequestError } from "../../utils/errors.ts";
31
32
  import type { AgentExecuteOptions, RawAgentEvent } from "../base.ts";
32
33
  import { NO_ANSWERS, questionRequest } from "../elicitation.ts";
34
+ import {
35
+ type HostToolSessions,
36
+ type HostTools,
37
+ hostToolSessions,
38
+ refuseHostName,
39
+ } from "../host-tools.ts";
33
40
  import type { CliLogin } from "../login.ts";
34
41
  import { SessionAgent } from "../session-agent.ts";
35
42
  import type { ScriptedEvent } from "./adapter.ts";
@@ -123,6 +130,12 @@ export interface ScriptedAgentOptions {
123
130
  * restart fences its cut-off sessions.
124
131
  */
125
132
  processes?: NativeProcesses;
133
+ /**
134
+ * The host's tools (`hostTools`): a session's source is a turn's `http`
135
+ * MCP server at the engine's loopback, so a `call` step calls it as an
136
+ * agent would, each call naming the turn running then (LLP 0018).
137
+ */
138
+ hostTools?: HostTools;
126
139
  }
127
140
 
128
141
  /** The one model it lists: it runs none. */
@@ -185,10 +198,17 @@ export class ScriptedAgent extends SessionAgent {
185
198
  private readonly turns = new Map<string, number>();
186
199
 
187
200
  readonly processes?: NativeProcesses;
201
+ private readonly host?: HostToolSessions;
202
+ /** Each session's running turn and its mode, as its host tools' calls read them. */
203
+ private readonly live = new Map<string, { turnId: string; permissionMode: PermissionMode }>();
188
204
 
189
205
  constructor(private readonly options: ScriptedAgentOptions) {
190
206
  super();
191
207
  this.processes = options.processes;
208
+ // A script decides itself whether to ask before a call (`ask`): its writes are not held to full access.
209
+ this.host = hostToolSessions(options.hostTools, this.harness, {
210
+ live: (sessionId) => this.live.get(sessionId),
211
+ });
192
212
  }
193
213
 
194
214
  async listModels(): Promise<ModelDescriptor[]> {
@@ -197,14 +217,19 @@ export class ScriptedAgent extends SessionAgent {
197
217
 
198
218
  protected override async *executeTurn(
199
219
  input: AgentInput,
200
- options: AgentExecuteOptions,
220
+ given: AgentExecuteOptions,
201
221
  controller: AbortController,
202
222
  ): AsyncIterableIterator<RawAgentEvent> {
203
223
  const { signal } = controller;
204
- if (signal.aborted) throw stopped();
224
+ // A turn cancelled before it began is still a turn of its script: it
225
+ // opens it and stops at its first step (or at its end, when it has no
226
+ // step), so a queue skips the turn's steps through its `end` and the
227
+ // next turn plays its own.
205
228
  // A script runs nothing of its own: the abort stops it, so a cancel is confirmed at once.
206
- this.cancelWith(options, async () => true);
207
- const { sessionId, turnId } = options;
229
+ this.cancelWith(given, async () => true);
230
+ const { sessionId, turnId } = given;
231
+ const options = await this.withHostTools(given);
232
+ this.live.set(sessionId, { turnId, permissionMode: options.permissionMode ?? "default" });
208
233
  // A later turn of the session resumes the id its first one reported.
209
234
  options.onNativeSession?.(options.resumeSessionId ?? `scripted-${sessionId}`, {
210
235
  resumed: options.resumeSessionId !== undefined,
@@ -232,13 +257,18 @@ export class ScriptedAgent extends SessionAgent {
232
257
  for (let n = 1; ; n++) {
233
258
  // What the step before did is the value of its `yield` in a generator.
234
259
  const next = await untilAborted(Promise.resolve(iterator.next(outcome)), signal);
235
- if (next.done) return;
260
+ if (next.done) {
261
+ // No step left to stop at: the cancel was confirmed, so the turn ends cancelled.
262
+ if (signal.aborted) throw stopped();
263
+ return;
264
+ }
236
265
  const step = readStep(next.value, `Step ${n} of turn ${turnId}`);
237
266
  const played = yield* this.play(step, turn);
238
267
  if ("stop" in played || "end" in step) return;
239
268
  outcome = played.outcome;
240
269
  }
241
270
  } finally {
271
+ if (this.live.get(sessionId)?.turnId === turnId) this.live.delete(sessionId);
242
272
  // Not awaited: a cancelled turn may have left a `next()` the test never releases.
243
273
  void Promise.resolve(iterator.return?.()).catch(() => {});
244
274
  }
@@ -396,10 +426,26 @@ export class ScriptedAgent extends SessionAgent {
396
426
  return { input: decision.updatedInput ?? input };
397
427
  }
398
428
 
429
+ /** The turn's MCP servers with the host's, by the name its source gives, when it has one. */
430
+ private async withHostTools(options: AgentExecuteOptions): Promise<AgentExecuteOptions> {
431
+ const lease = await this.host?.lease(options.sessionId, options.cwd);
432
+ if (!lease) return options;
433
+ const { source } = lease;
434
+ refuseHostName(options.mcpServers, source.name);
435
+ const server = await lease.http();
436
+ return { ...options, mcpServers: { ...options.mcpServers, [source.name]: server } };
437
+ }
438
+
399
439
  override async release(sessionId: string): Promise<void> {
400
440
  this.turns.delete(sessionId);
441
+ this.host?.release(sessionId);
401
442
  await super.release(sessionId);
402
443
  }
444
+
445
+ override async terminateAll(): Promise<void> {
446
+ await super.terminateAll();
447
+ await this.host?.close();
448
+ }
403
449
  }
404
450
 
405
451
  /** The turn's config, as `echo: "config"` says it: MCP servers by name, `env` by its keys alone. */
@@ -70,7 +70,11 @@ export const ScriptStepSchema = z.union([
70
70
  z.strictObject({ echo: z.enum(["input", "config"]) }),
71
71
  /** A pause of this many ms. */
72
72
  z.strictObject({ wait: Ms }),
73
- /** Runs until the turn is cancelled, then `stopTakes` ms more before it stops, also when the cancel came first. */
73
+ /**
74
+ * Runs until the turn is cancelled, then `stopTakes` ms more before it
75
+ * stops, also when the cancel came first; not in an async generator's
76
+ * script, whose next step a cancel does not wait for.
77
+ */
74
78
  z.strictObject({ hang: z.literal(true), stopTakes: Ms.optional() }),
75
79
  /**
76
80
  * Ends the turn in an error with this message: of `category` when given,
@@ -6,11 +6,14 @@ import {
6
6
  BaseAgent,
7
7
  CANCEL_CONFIRM_MS,
8
8
  type CancelResult,
9
+ type PermissionDecision,
10
+ type PermissionRequestHandler,
9
11
  type RawAgentEvent,
10
12
  type SteerOutcome,
11
13
  type SteerRequest,
12
14
  } from "./base.ts";
13
15
  import { configFingerprint } from "./config-fingerprint.ts";
16
+ import { withoutSecret } from "./host-tools.ts";
14
17
 
15
18
  /** Hands one steer to the harness's native turn. */
16
19
  export type DeliverSteer = (request: SteerRequest) => SteerOutcome | Promise<SteerOutcome>;
@@ -59,6 +62,50 @@ export interface SessionAgentOptions {
59
62
  * exported while its design is a Draft (LLP 0016); a product's own harness
60
63
  * extends `BaseAgent`, the public base.
61
64
  */
65
+ // @ref LLP 0018#who-can-read-the-secret — the broker publishes a request's options too
66
+ /**
67
+ * A permission request as the broker publishes it: its tool call and its
68
+ * options (a harness's own labels and ids) without the secret, and the
69
+ * option picked mapped back to the harness's own id.
70
+ */
71
+ async function permission(
72
+ ask: PermissionRequestHandler,
73
+ scrub: <T>(value: T) => T,
74
+ call: Parameters<PermissionRequestHandler>[0],
75
+ rest: [Parameters<PermissionRequestHandler>[1]?],
76
+ ): Promise<PermissionDecision> {
77
+ const [opts] = rest;
78
+ if (!opts) return ask(scrub(call), ...rest);
79
+ const scrubbed = scrub(opts);
80
+ // A choice whose id held the secret gets an opaque id of its own, unique, so
81
+ // two redacted the same can never be taken for each other.
82
+ const used = new Set(
83
+ (scrubbed.options ?? []).flatMap((option, index) =>
84
+ opts.options?.[index]?.optionId === option.optionId ? [option.optionId] : [],
85
+ ),
86
+ );
87
+ const opaque = (index: number) => {
88
+ let id = `redacted-option-${index}`;
89
+ while (used.has(id)) id += "-";
90
+ used.add(id);
91
+ return id;
92
+ };
93
+ const choices = (scrubbed.options ?? []).map((option, index) => {
94
+ const own = opts.options?.[index]?.optionId ?? option.optionId;
95
+ return {
96
+ option: own === option.optionId ? option : { ...option, optionId: opaque(index) },
97
+ own,
98
+ };
99
+ });
100
+ const native = new Map(choices.map(({ option, own }) => [option.optionId, own]));
101
+ const shown = scrubbed.options
102
+ ? { ...scrubbed, options: choices.map(({ option }) => option) }
103
+ : scrubbed;
104
+ const decision = await ask(scrub(call), shown);
105
+ if (decision.decision === "cancel" || decision.optionId === undefined) return decision;
106
+ return { ...decision, optionId: native.get(decision.optionId) ?? decision.optionId };
107
+ }
108
+
62
109
  export abstract class SessionAgent extends BaseAgent {
63
110
  private terminated = false;
64
111
  /**
@@ -106,8 +153,32 @@ export abstract class SessionAgent extends BaseAgent {
106
153
  try {
107
154
  // The adapter still produces its native cancellation result for an
108
155
  // already-aborted turn. It must not select a conversation or do setup.
109
- const prepared = controller.signal.aborted ? options : this.prepareSessionOptions(options);
110
- yield* this.executeTurn(input, prepared, controller);
156
+ const ready = controller.signal.aborted ? options : this.prepareSessionOptions(options);
157
+ // @ref LLP 0018#who-can-read-the-secret — the session's own secret leaves in no text
158
+ // Kept for the turn once seen: its output may still drain after the session's release.
159
+ let secret: string | undefined;
160
+ const scrub = <T>(value: T): T => {
161
+ secret ??= this.hostSecret(sessionId);
162
+ return withoutSecret(value, secret);
163
+ };
164
+ const { onWarning, onPermissionRequest, onElicitationRequest } = ready;
165
+ const prepared: AgentExecuteOptions = {
166
+ ...ready,
167
+ ...(onWarning && { onWarning: (warning) => onWarning(scrub(warning)) }),
168
+ // What the broker puts on the stream (a request's input, its title) is the agent's text too.
169
+ ...(onPermissionRequest && {
170
+ onPermissionRequest: (call, ...rest) =>
171
+ permission(onPermissionRequest, scrub, call, rest),
172
+ }),
173
+ ...(onElicitationRequest && {
174
+ onElicitationRequest: (request, ...rest) => onElicitationRequest(scrub(request), ...rest),
175
+ }),
176
+ };
177
+ try {
178
+ for await (const event of this.executeTurn(input, prepared, controller)) yield scrub(event);
179
+ } catch (error) {
180
+ throw scrub(error);
181
+ }
111
182
  } finally {
112
183
  this.executed.set(sessionId, options.turnId);
113
184
  if (this.steering.get(sessionId) === steering) this.steering.delete(sessionId);
@@ -227,6 +298,11 @@ export abstract class SessionAgent extends BaseAgent {
227
298
  }
228
299
 
229
300
  /** Native protocol execution; options are prepared and the session is exclusively owned. */
301
+ /** The session's host-tools secret, which no event, warning or error it lets out may hold. */
302
+ protected hostSecret(_sessionId: string): string | undefined {
303
+ return undefined;
304
+ }
305
+
230
306
  protected abstract executeTurn(
231
307
  input: AgentInput,
232
308
  options: AgentExecuteOptions,
@@ -1,9 +1,12 @@
1
- import { THINKING_LEVELS, type ThinkingLevel, isThinkingLevel } from "../../protocol/index.ts";
1
+ import type { ThinkingLevel } from "../../protocol/index.ts";
2
2
 
3
- /** The values that are thinking levels, once each, lowest first. */
4
- export function knownThinkingLevels(values: Iterable<unknown>): ThinkingLevel[] {
3
+ /**
4
+ * The levels a harness lists: each non-blank string once, in the order given.
5
+ * The order is the harness's own (both CLIs list lowest first), and a name
6
+ * this build does not know is kept.
7
+ */
8
+ export function listedThinkingLevels(values: Iterable<unknown>): ThinkingLevel[] {
5
9
  const levels = new Set<ThinkingLevel>();
6
- for (const value of values)
7
- if (typeof value === "string" && isThinkingLevel(value)) levels.add(value);
8
- return THINKING_LEVELS.filter((level) => levels.has(level));
10
+ for (const value of values) if (typeof value === "string" && /\S/.test(value)) levels.add(value);
11
+ return [...levels];
9
12
  }
package/src/core/index.ts CHANGED
@@ -32,6 +32,15 @@ export {
32
32
  } from "./agents/base.ts";
33
33
  export { AgentRegistry } from "./agents/registry.ts";
34
34
  export { type CredentialSource, credentialsFromUrl } from "./agents/credentials.ts";
35
+ export {
36
+ HOST_TOOL_TIMEOUT_MS,
37
+ type HostTool,
38
+ type HostToolCaller,
39
+ type HostToolContent,
40
+ type HostToolResult,
41
+ type HostToolSource,
42
+ type HostTools,
43
+ } from "./agents/host-tools.ts";
35
44
  export type {
36
45
  LoginOptions,
37
46
  LoginStatus,
@@ -11,6 +11,7 @@ import {
11
11
  type CodexAppServerAgentOptions,
12
12
  } from "./agents/codex-app-server/codex-agent.ts";
13
13
  import type { CredentialSource } from "./agents/credentials.ts";
14
+ import type { HostTools } from "./agents/host-tools.ts";
14
15
  import type { InheritConfig } from "./agents/inherit-config.ts";
15
16
  import { AgentRegistry } from "./agents/registry.ts";
16
17
  import { createScriptedTransformer } from "./agents/scripted/adapter.ts";
@@ -31,7 +32,8 @@ type SaidOnce =
31
32
  | "idleTimeoutMs"
32
33
  | "credentials"
33
34
  | "cancelConfirmMs"
34
- | "cancelGraceMs";
35
+ | "cancelGraceMs"
36
+ | "hostTools";
35
37
 
36
38
  export interface CreateRegistryOptions {
37
39
  /**
@@ -70,6 +72,15 @@ export interface CreateRegistryOptions {
70
72
  * Operator-level only.
71
73
  */
72
74
  credentials?: CredentialSource;
75
+ /**
76
+ * The host's own tools (LLP 0018): asked once for each session, at its
77
+ * first turn, for a source of tools the engine gives its agent on every
78
+ * harness, under one server name. Claude runs them in process; Codex, and
79
+ * an ACP agent that takes HTTP MCP servers, reach them at the engine's
80
+ * loopback by a secret of the session's own. Each call names its session,
81
+ * the turn running and the mode. Operator-level only.
82
+ */
83
+ hostTools?: HostTools;
73
84
  /**
74
85
  * How long a session's agent process may wait unused between turns before
75
86
  * it is ended (5 minutes by default); 0 never ends one for idleness, and
@@ -140,6 +151,7 @@ const HARNESSES: Record<
140
151
  onDiagnostic: opts.onDiagnostic,
141
152
  ...opts.claudeCode,
142
153
  credentials: opts.credentials,
154
+ hostTools: opts.hostTools,
143
155
  idleTimeoutMs: opts.idleTimeoutMs,
144
156
  cancelConfirmMs: opts.cancelConfirmMs,
145
157
  cancelGraceMs: opts.cancelGraceMs,
@@ -153,6 +165,7 @@ const HARNESSES: Record<
153
165
  new CodexAppServerAgent({
154
166
  ...opts.codexAppServer,
155
167
  credentials: opts.credentials,
168
+ hostTools: opts.hostTools,
156
169
  idleTimeoutMs: opts.idleTimeoutMs,
157
170
  cancelConfirmMs: opts.cancelConfirmMs,
158
171
  cancelGraceMs: opts.cancelGraceMs,
@@ -166,9 +179,16 @@ const HARNESSES: Record<
166
179
  if (!opts.acp) {
167
180
  throw new Error("the acp harness requires CreateRegistryOptions.acp (resolveLaunch)");
168
181
  }
169
- const { idleTimeoutMs, cancelConfirmMs, cancelGraceMs } = opts;
182
+ const { idleTimeoutMs, cancelConfirmMs, cancelGraceMs, hostTools } = opts;
170
183
  return [
171
- new AcpAgent({ ...opts.acp, idleTimeoutMs, cancelConfirmMs, cancelGraceMs, processes }),
184
+ new AcpAgent({
185
+ ...opts.acp,
186
+ idleTimeoutMs,
187
+ cancelConfirmMs,
188
+ cancelGraceMs,
189
+ hostTools,
190
+ processes,
191
+ }),
172
192
  createAcpTransformer,
173
193
  ];
174
194
  },
@@ -177,7 +197,10 @@ const HARNESSES: Record<
177
197
  throw new Error("the scripted harness requires CreateRegistryOptions.scripted (turn)");
178
198
  }
179
199
  // @ref LLP 0007#what-each-harness-does — it starts none, so a restart vouches for it
180
- return [new ScriptedAgent({ ...opts.scripted, processes }), createScriptedTransformer];
200
+ return [
201
+ new ScriptedAgent({ ...opts.scripted, hostTools: opts.hostTools, processes }),
202
+ createScriptedTransformer,
203
+ ];
181
204
  },
182
205
  };
183
206
 
@@ -190,6 +213,7 @@ const SAID_ONCE: Record<SaidOnce, string> = {
190
213
  cancelConfirmMs: atTheTop("cancelConfirmMs"),
191
214
  cancelGraceMs: atTheTop("cancelGraceMs"),
192
215
  inheritConfig: atTheTop("inheritConfig"),
216
+ hostTools: atTheTop("hostTools"),
193
217
  processes:
194
218
  "the registry gives every harness its processes; say how they are owned with nativeProcesses",
195
219
  resolveCliPath: "the registry's provisioner finds each CLI; name one with provision.overrides",
@@ -118,6 +118,13 @@ interface BunRuntime {
118
118
  */
119
119
  function startShell(ticks: string, exited: () => void, failed: () => void): LifelineShell {
120
120
  const args = ["-c", LIFELINE_SCRIPT, LIFELINE_NAME, ticks];
121
+ // @ref LLP 0007#the-environment-a-process-is-given — only what its script needs
122
+ // It lives as long as the engine, and any process of this user can read its
123
+ // environment (`/proc/<pid>/environ`): `PATH` finds its `sed`, `awk`, `cat`,
124
+ // `tr` and `sleep`. Nothing else of the engine's environment is given;
125
+ // Node itself adds `NODE_V8_COVERAGE` to every child when the engine runs
126
+ // with it set (Bun adds nothing).
127
+ const env = { PATH: process.env.PATH };
121
128
  const bun = (globalThis as { Bun?: BunRuntime }).Bun;
122
129
  if (bun) {
123
130
  // @ref LLP 0007#the-lifeline — registered before the CLI gets input
@@ -129,10 +136,7 @@ function startShell(ticks: string, exited: () => void, failed: () => void): Life
129
136
  stdout: "ignore",
130
137
  stderr: "ignore",
131
138
  detached: true,
132
- // @ref LLP 0007#the-environment-a-process-is-given — this process's environment now
133
- // Without one, Bun gives the environment it was launched with: a
134
- // secret the host deleted from `process.env` would live on here.
135
- env: process.env,
139
+ env,
136
140
  onExit: exited,
137
141
  });
138
142
  child.unref();
@@ -149,7 +153,7 @@ function startShell(ticks: string, exited: () => void, failed: () => void): Life
149
153
  // Node writes a pipe at once when it can (libuv's `uv_try_write`).
150
154
  const child = spawn("/bin/sh", args, {
151
155
  detached: true,
152
- env: process.env,
156
+ env,
153
157
  stdio: ["pipe", "ignore", "ignore"],
154
158
  });
155
159
  child.unref();
@@ -16,22 +16,13 @@ import type { CliTool } from "./tools.ts";
16
16
  * `auto` an installed `@openai/codex` package (a lockfile's) runs instead,
17
17
  * and a `codex` on PATH only in `system` mode.
18
18
  *
19
- * After any bump, run `bun run releases:refresh` (each platform's build and
20
- * its checksums, `releases.ts`) and `bun run models:refresh` (the model
21
- * catalog, which records the versions that listed it); tests fail until both
22
- * match these pins.
19
+ * A bump is `bun run bump -- <tool>`: it writes what changed upstream, moves
20
+ * the pin, and runs both refreshes, `releases:refresh` (each platform's build
21
+ * and its checksums, `releases.ts`) and `models:refresh` (the model catalog,
22
+ * which records the versions that listed it); tests fail until both match
23
+ * these pins. The `bump-clis` skill says what to read and prove.
23
24
  */
24
25
  export const DEFAULT_PINS: Record<CliTool, string> = {
25
- // 0.3.288 verified 2026-10-03: the paired CLI honors `--thinking-display
26
- // summarized`, so reasoning parts carry text (2.1.233 ignored the flag and
27
- // streamed token-estimate placeholders). Re-check thinking summaries on
28
- // every bump (see options.ts extraArgs).
29
- claude: "0.3.288",
30
- // 0.160.0 re-verified 2026-10-03 against generate-ts: thread/turn/item
31
- // methods, token usage, approvals, and host-managed ChatGPT auth changed
32
- // only additively since 0.153.4. Of the GPT-6 family, 0.153.4 lists only
33
- // gpt-6-astra; the rest needs 0.159 or newer.
34
- // Since 0.160 a thread has one writer: resuming it from a second process
35
- // fails until the first releases it.
36
- codex: "0.160.0",
26
+ claude: "0.3.327",
27
+ codex: "0.169.1",
37
28
  };
@@ -9,77 +9,77 @@ import type { CliReleases } from "./checksums.ts";
9
9
  */
10
10
  export const CLI_RELEASES: CliReleases = {
11
11
  claude: {
12
- version: "0.3.288",
13
- cli: "2.1.288",
12
+ version: "0.3.327",
13
+ cli: "2.1.327",
14
14
  builds: {
15
15
  "darwin-arm64": {
16
16
  integrity:
17
- "sha512-85niGScWj8sr7yqOjSfqINaKv/zB47T45o4NBq1BmpaKRYFK7grtK24LA6C7cjx1Pznp+WT68mo373fbbwFW0A==",
18
- sha256: "bbe93063f7a0879a1021b2891e5c9354e5b3b98433e32efe6750f7710afed750",
17
+ "sha512-n+tQOPqz264l12BAKBcBPBbdB/bmZ2SfyONtEXQufTU6EeCoQMPS58RSt51Cxk5sDUme1hTF0vVAqRPdhq8tFg==",
18
+ sha256: "76d48205c926c8a6978cfc2171b631bcd01e20bbadd085638bb2ec57bea91c5b",
19
19
  },
20
20
  "darwin-x64": {
21
21
  integrity:
22
- "sha512-hY8MQtgAi+cRedFb+qSqZzAYXIfjC+ZiXtCxKCs4OqWbqrdVZ0GKcEv4nB6MfQGs/Qr4U24cDNuO2lskBbZAWA==",
23
- sha256: "946acab03a55b60e2016e30964c48b96ef6673cb5da684383f4835da410eede0",
22
+ "sha512-u091X+eQIyqg8BIEYLWJ0o6U3BaivMcf8Im4hI3hti/NGJ6YC5sR+LLEa1RjSIKE+JoOfCxipdCNSKaEI+0d5Q==",
23
+ sha256: "1fe75c05df7be824af1ed80ff9ccdf2be991f3a2212b97e05fc9a3bff2f39ef9",
24
24
  },
25
25
  "linux-arm64": {
26
26
  integrity:
27
- "sha512-CCj0xau64T+Ia+X2wZgjYFSUIWfNJRWIGFbT8+jKiCzQggaBo6OSZ8hAYyME3h13JYrgpyrUTzB5YiwKaw8ZFg==",
28
- sha256: "359ab6a058fcde9741dff54979a212fd134cdf8e8cfc2f8de02bc350b9e2b9d5",
27
+ "sha512-/k6BfV+L2er67UoLUabcp0w3L1DMemJjoijdxvXDiU8w81fGfHaKWKhRuhUQFmIrMLyMyQcBKaoyUNSLWovPnw==",
28
+ sha256: "acf0ef2f9da8d5ab08f193f007e19a06cbfb608b96a2f1f758d63a92a386cff8",
29
29
  },
30
30
  "linux-x64": {
31
31
  integrity:
32
- "sha512-Y5M496UZKttScbzptA45NI+CMHYONnskw0aq3rJ6s+gE6L6Jy8F8UoKt0lQO11R5xTY65TIeCasjQf/bLavuNg==",
33
- sha256: "0298068b686e7fdbaf9402a7a587bb7f49c0b0e084de09f69145a0719207640c",
32
+ "sha512-oAdL7KM9UVAZlATeSjh2haeEjex1N7ZcHzWO1tY2hgyhkyLNVw/tWUhITOucxMwokBx++h2dMVjxiPJiZ5uVOQ==",
33
+ sha256: "343c744baa14bfd38286a7cb90871051f04379956da6c5b803a9e66019a76ac9",
34
34
  },
35
35
  "linux-arm64-musl": {
36
36
  integrity:
37
- "sha512-66MD4hzNTqrTL5MN21bSfxm/fhbnM+ERsmWHGEsJojxAWY5DXCUlDkaAYM0rxDpdvLMVt9PpRVIiWDptaiFPxA==",
38
- sha256: "6b9816b225b303dacc17acc9d83016055274498effdbce6227e62d5dbaa762b9",
37
+ "sha512-oV8/+ZixiPzEW1BStoP9FOXsHcznONaom6S2PwwIfhi5Pxa5fGuloy8TiOB9wK+03zavqt7jmzLgVPF2a2NsTA==",
38
+ sha256: "e44a59ef850788c578adaab7a6487d5d0af9c0844401da9d7263e7b41515754b",
39
39
  },
40
40
  "linux-x64-musl": {
41
41
  integrity:
42
- "sha512-g3HDERLfEamirfp+MJDVl1fT8YYQMILUH65jPzl+SzKLeBu26WmAXAoZFmU4dasEmKZkEnBdL0ZQy3qnDy5EXg==",
43
- sha256: "b4334268a2ce06bf984dab2d6925cc5afe9d2344fa427fc362e266da0d57a894",
42
+ "sha512-O0nDZ1gpXi83qAdd8SM0gTdjlzrqpOlW0M4W7PfHVrMXWLh9I+1tvHat+s17ehIW6A9VQIWIq4oMwq7jb708Dg==",
43
+ sha256: "d2b02e78664c1347aff8fcee045356ee044116be41d7f412721a6c50182b6d98",
44
44
  },
45
45
  "win32-arm64": {
46
46
  integrity:
47
- "sha512-ijVSU5Vl1L1SlEm1djPNEekxHm9FS5GOitz8kL8KLdYDspMA97bgFM4gy/+riA3qn4ERepJkjdDLE+oYMTO+UA==",
48
- sha256: "5015d2b92866232b85384ac6e5eef4ff155418a92123503e52714c29186af8c6",
47
+ "sha512-a5ju83mylDvUwvQyRt1E0TQ0ar2nzVMPeWZhjcmgpB1cNTjf+KT5jgUnLvxPOG2tyjhm8bYGysRMdgB+tVnhvw==",
48
+ sha256: "d2c3395d252007540a5662a2cfd4d8903a0beb5fa32c893cd13b81be427625a7",
49
49
  },
50
50
  "win32-x64": {
51
51
  integrity:
52
- "sha512-zh4yfGduDEBKUqb3BPtq/cLYwHoScqm7X5fJhmkSs0BJiSJGlb38j6FXWDvoPfe+OXNjE5onNx5HlAnDzsQzrA==",
53
- sha256: "84304f7d4b0cd0ebcbe8318695a260151b48991a6659c3366fdd5da290c0ab91",
52
+ "sha512-bf52nw4j5RCvkXI5Poj5Qf0jWvuLQ/UPFqdav86UwaGTZZ0txNV33MKBIUoCyxXV6IC1/p2BNfkwmCAZ+Uc9pw==",
53
+ sha256: "d6c8075bfb3b87f1c1254e64b6c35dadc628d21e75930ae51e47047b3f05a4a2",
54
54
  },
55
55
  },
56
56
  },
57
57
  codex: {
58
- version: "0.160.0",
58
+ version: "0.169.1",
59
59
  builds: {
60
60
  "darwin-arm64": {
61
61
  integrity:
62
- "sha512-aefV6cqZA2REZgR//4McyXlp7zLcTti4CI2v3j9IVgNndPBv2kCeNEcz07qeelXcwOdSFPUKb6roA48vZmDgrQ==",
62
+ "sha512-yOKvCJktlbxdETtXAidz2GcC4z/WMYPmu2plncYJtUI5AhnFIExen8n9eL/El6n9hYbzzZFf1JgyW7yHx6kwTw==",
63
63
  },
64
64
  "darwin-x64": {
65
65
  integrity:
66
- "sha512-ir7cdsPrb9VkqNqpTECoTiomv04xws/FQ968mHQzuL+2J5Da0Dkh2Qlxp7sguDDZJab4YpYmNjQvBVMXbTeQaQ==",
66
+ "sha512-IbHIskdPWGAXFCdGiB8CosfXXtdvMmusrQ2AdW2kFHd6aSJZ+dvgalwjpxmAytalnNM3pQgXwS/6fU1OclFv8Q==",
67
67
  },
68
68
  "linux-arm64": {
69
69
  integrity:
70
- "sha512-VnVdsS06YlDsL8OwaJjQ3xdqdJNG4i+eBJBV3LytddknzSaF/urijLzVg3ptkwvoj8C7Gn7iWpVE+y8WEaPiCg==",
70
+ "sha512-6vRE7ZxkngT6L5JQ9WUQ2UnztS6hqyfcQiFqvk0yhcvWsrZLJNiiDZcKzGbLfJK2sDy7tSt9uWdP1cIgMHIkaw==",
71
71
  },
72
72
  "linux-x64": {
73
73
  integrity:
74
- "sha512-KI/73OqGrHmR18s7ya7E1NqV6rT0y3lxr0s8S1qR2m6zU6QRF/HlR529jALLh5vjdUnsRT4Ahoxt0axb4kY99g==",
74
+ "sha512-KmlHQ/lB9jcn8Yy5z/tWCpSPsqBvI3pU+0PUPccr+pKiiU2NJSCvZcAD7s3dmIFq5ZvGYtGcSn2KdFFdaPQICg==",
75
75
  },
76
76
  "win32-arm64": {
77
77
  integrity:
78
- "sha512-tTvK9ZIGuj9WnYaYOAnfjJVSfPV7dKqWtDHqUPLf0Hadp6SgynTxx7S7iMXRieFeXVOI2XH76KxEHSOTOnz3zw==",
78
+ "sha512-EYXwgEdHHzwgA0nnDcK4pI98IdReB1aDebtOxcYixM1/GY2Y/76oDFYSs/u6DzA27f6Hhf5V92OwQFw/GLICUQ==",
79
79
  },
80
80
  "win32-x64": {
81
81
  integrity:
82
- "sha512-/gCFcuOmGlQkgGivWCtY8BNEDixh70Pue0HZOk9S8bj2vUIE1GLD9zcNB/KqqDJmn0S+lIttauLpzQnw6HOCHA==",
82
+ "sha512-fImXWqxwxqHh9+FhJWsRRFiBzgBrSeN1bYTvtGZtUKGTPzHefYAL1agmlf5XScfwY5i5pHsExfdhFekzRBbqIw==",
83
83
  },
84
84
  },
85
85
  },
@@ -55,31 +55,52 @@ interface BunRuntime {
55
55
  }): { readonly port: number; stop(closeActiveConnections?: boolean): unknown; unref(): void };
56
56
  }
57
57
 
58
+ /**
59
+ * Bun's own `Response`. A product's process may have replaced the global one
60
+ * (Hono's Node server does, with a class of its own), and `Bun.serve` refuses
61
+ * any other ("Expected a Response object"); `fetch` still answers with Bun's.
62
+ */
63
+ async function nativeResponse(): Promise<typeof Response> {
64
+ return (await fetch("data:,")).constructor as typeof Response;
65
+ }
66
+
58
67
  /**
59
68
  * Serve `handle` on 127.0.0.1, on a port of its own: with `Bun.serve` under
60
69
  * Bun, whose `node:http` says nothing of a client that left once its request
61
70
  * body was read (a stopped turn's request would run on upstream), and with
62
71
  * `node:http` under Node.
63
72
  */
64
- export function serveLoopback(handle: (request: Request) => Promise<Response>): Promise<Loopback> {
73
+ export async function serveLoopback(
74
+ handle: (request: Request) => Promise<Response>,
75
+ ): Promise<Loopback> {
65
76
  const bun = (globalThis as { Bun?: BunRuntime }).Bun;
66
77
  if (bun) {
78
+ const Native = await nativeResponse();
79
+ const native = (response: Response): Response =>
80
+ // Not `instanceof`: Hono's class sets Bun's prototype under its own.
81
+ response.constructor === Native
82
+ ? response
83
+ : new Native(response.body, {
84
+ status: response.status,
85
+ statusText: response.statusText,
86
+ headers: [...response.headers],
87
+ });
67
88
  const server = bun.serve({
68
89
  port: 0,
69
90
  hostname: "127.0.0.1",
70
91
  // A model can think longer than any idle limit between the events it streams.
71
92
  idleTimeout: 0,
72
- fetch: (request) => handle(request).catch(() => new Response(null, { status: 502 })),
93
+ fetch: (request) => handle(request).then(native, () => new Native(null, { status: 502 })),
73
94
  });
74
95
  // Nothing on it keeps the process alive.
75
96
  server.unref();
76
- return Promise.resolve({
97
+ return {
77
98
  url: `http://127.0.0.1:${server.port}`,
78
99
  // Not waited for: with a client still connected, Bun's stop can take its time.
79
100
  close: async () => {
80
101
  server.stop(true);
81
102
  },
82
- });
103
+ };
83
104
  }
84
105
  const server = createServer((req, res) => {
85
106
  bridge(handle, req, res).catch(() => {