@agenthippo-ai/agenthippo 0.2.3-dataplane → 1.110.9

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 (130) hide show
  1. package/out/analytics/dashboard/provider.js +13 -2
  2. package/out/analytics/dashboard/webview/panel.js +3 -2
  3. package/out/analytics/identity.js +0 -0
  4. package/out/analytics/service.js +23 -25
  5. package/out/analytics/sessionEvents.js +98 -23
  6. package/out/auth/authBaseUrl.js +59 -0
  7. package/out/auth/bootstrapApply.js +9 -10
  8. package/out/auth/brokerClient.js +48 -14
  9. package/out/auth/provider.js +8 -21
  10. package/out/auth/sessionStore.js +3 -40
  11. package/out/auth/signInFlow.js +2 -1
  12. package/out/auth-broker/config.js +50 -5
  13. package/out/auth-broker/server.js +122 -26
  14. package/out/auth-broker/store.js +25 -24
  15. package/out/authority/warrant.js +206 -0
  16. package/out/branding.js +27 -1
  17. package/out/cli/commands/conformance.js +249 -0
  18. package/out/cli/commands/data-plane.js +67 -0
  19. package/out/cli/commands/deploy.js +1 -0
  20. package/out/cli/commands/gateway.js +4 -0
  21. package/out/cli/commands/pack.js +182 -3
  22. package/out/cli/commands/sandbox.js +332 -0
  23. package/out/cli/commands/serve.js +875 -52
  24. package/out/cli/commands/store.js +12 -4
  25. package/out/cli/data-plane/gateway.js +28 -5
  26. package/out/cli/data-plane/index.js +17 -2
  27. package/out/cli/data-plane/registry.js +9 -0
  28. package/out/cli/data-plane/remote.js +106 -0
  29. package/out/cli/data-plane/sidecar.js +161 -0
  30. package/out/cli/deploy/layers/identity.js +85 -4
  31. package/out/cli/deploy-init.js +89 -24
  32. package/out/cli/deps.js +106 -2
  33. package/out/cli/idp-init.js +35 -0
  34. package/out/cli/index.js +155 -16
  35. package/out/cli/pack-install-ref.js +15 -0
  36. package/out/cli/pack-loader.js +8 -1
  37. package/out/cli/pack-runtime.js +61 -0
  38. package/out/cli/runtime.js +35 -1
  39. package/out/cli/serve-hardening.js +80 -3
  40. package/out/cli/utils.js +12 -1
  41. package/out/engines/base.js +32 -3
  42. package/out/engines/claude.js +33 -15
  43. package/out/engines/codex.js +110 -26
  44. package/out/engines/custom.js +8 -0
  45. package/out/evidence/sessionShip.js +610 -0
  46. package/out/extension.js +11 -2
  47. package/out/gateway/bridge.js +40 -2
  48. package/out/gateway/client.js +34 -1
  49. package/out/gateway/installSource.js +68 -0
  50. package/out/gateway/lifecycle.js +185 -51
  51. package/out/gateway/outboundPaths.js +4 -1
  52. package/out/gateway/secretsPreflight.js +90 -0
  53. package/out/gateway/service.js +7 -2
  54. package/out/headless/index.js +17 -1
  55. package/out/live-e2e/mcp-demo/server.mjs +32 -0
  56. package/out/mcp.js +30 -0
  57. package/out/pack/caps.js +271 -0
  58. package/out/pack/governor.js +309 -0
  59. package/out/pack/lease-schema.js +270 -0
  60. package/out/pack/lease.js +640 -0
  61. package/out/pack/reconcile.js +217 -0
  62. package/out/pack/signature.js +839 -0
  63. package/out/remote/handleRemoteChat.js +44 -3
  64. package/out/remote/remoteTrust.js +277 -0
  65. package/out/remote/serveClient.js +52 -5
  66. package/out/runner/host.js +131 -0
  67. package/out/runner/protocol.js +108 -0
  68. package/out/runner/remoteEngine.js +130 -0
  69. package/out/runtime/interfaces/edit-tracker.js +2 -0
  70. package/out/runtime/vscode/vscode-edit-tracker.js +2 -0
  71. package/out/sandbox/containerDriver.js +570 -0
  72. package/out/sandbox/processDriver.js +315 -0
  73. package/out/sandbox/sandboxRunner.js +191 -0
  74. package/out/sandbox/support.js +301 -0
  75. package/out/sandbox/types.js +55 -0
  76. package/out/scripts/agentide-litellm.sh +67 -5
  77. package/out/scripts/agentide_litellm_hooks_v1.py +182 -21
  78. package/out/scripts/analytics/agentide-litellm-otel.config.yaml +94 -132
  79. package/out/scripts/analytics/setup-analytics.ps1 +37 -13
  80. package/out/scripts/analytics/setup-analytics.sh +93 -13
  81. package/out/scripts/analytics/start-analytics.sh +86 -14
  82. package/out/scripts/gateway/setup-gateway.sh +72 -32
  83. package/out/shared/models.js +11 -110
  84. package/out/spotlight/install.js +7 -1
  85. package/out/store/index.js +41 -10
  86. package/out/sync.js +3 -1
  87. package/out/test/analytics.test.js +162 -23
  88. package/out/test/auth-baseurl.test.js +132 -0
  89. package/out/test/auth-bootstrapApply.test.js +27 -6
  90. package/out/test/auth-broker-hardening.test.js +227 -0
  91. package/out/test/auth-broker.test.js +59 -9
  92. package/out/test/branding.test.js +13 -0
  93. package/out/test/caps-governor.test.js +455 -0
  94. package/out/test/codex-new-file-survival.test.js +119 -0
  95. package/out/test/configuration-scopes.test.js +131 -0
  96. package/out/test/data-plane.test.js +67 -0
  97. package/out/test/deploy-init.test.js +40 -0
  98. package/out/test/gateway-client.test.js +28 -0
  99. package/out/test/gateway-install-source.test.js +111 -0
  100. package/out/test/gateway-lifecycle.test.js +1 -1
  101. package/out/test/gateway-outbound.test.js +7 -0
  102. package/out/test/gateway-secrets-preflight.test.js +127 -0
  103. package/out/test/gateway-session-routing.test.js +18 -0
  104. package/out/test/governor-hardening.test.js +217 -0
  105. package/out/test/headless-sync.test.js +22 -0
  106. package/out/test/lease-signature.test.js +357 -0
  107. package/out/test/lease-trust-root.test.js +273 -0
  108. package/out/test/lease-validation.test.js +169 -0
  109. package/out/test/lease.test.js +114 -0
  110. package/out/test/mcp.test.js +27 -1
  111. package/out/test/pack-admission.test.js +216 -0
  112. package/out/test/pack-digest-parity.test.js +140 -0
  113. package/out/test/pack-reconcile.test.js +183 -0
  114. package/out/test/pack-signature-artifact.test.js +103 -0
  115. package/out/test/pack-signature.test.js +176 -0
  116. package/out/test/pack-trust-root.test.js +202 -0
  117. package/out/test/remote-trust.test.js +242 -0
  118. package/out/test/runner-protocol.test.js +249 -0
  119. package/out/test/sandbox-driver.test.js +553 -0
  120. package/out/test/sandbox-hardening.test.js +395 -0
  121. package/out/test/sandbox-runner.test.js +343 -0
  122. package/out/test/serve-hardening.test.js +45 -6
  123. package/out/test/serveClient.test.js +6 -5
  124. package/out/test/sessionShip.test.js +560 -0
  125. package/out/test/spotlightPublish.test.js +28 -0
  126. package/out/test/store-install-safe.test.js +69 -0
  127. package/out/test/tls-bypass.test.js +90 -0
  128. package/out/test/warrant.test.js +138 -0
  129. package/out/utils.js +46 -9
  130. package/package.json +1 -1
@@ -42,18 +42,32 @@ exports.runGatewayTurn = runGatewayTurn;
42
42
  /**
43
43
  * Serve Command - HTTP Server
44
44
  */
45
+ const crypto = __importStar(require("crypto"));
45
46
  const fs = __importStar(require("fs"));
47
+ const os = __importStar(require("os"));
46
48
  const http = __importStar(require("http"));
47
49
  const path = __importStar(require("path"));
48
50
  const node_crypto_1 = require("node:crypto");
51
+ const sandboxRunner_1 = require("../../sandbox/sandboxRunner");
52
+ const deps_1 = require("../deps");
53
+ const signature_1 = require("../../pack/signature");
54
+ const reconcile_1 = require("../../pack/reconcile");
55
+ const index_1 = require("../../store/index");
56
+ const lease_1 = require("../../pack/lease");
57
+ const caps_1 = require("../../pack/caps");
58
+ const governor_1 = require("../../pack/governor");
59
+ const containerDriver_1 = require("../../sandbox/containerDriver");
60
+ const processDriver_1 = require("../../sandbox/processDriver");
61
+ const support_1 = require("../../sandbox/support");
49
62
  const headless_1 = require("../../headless");
50
63
  const pack_loader_1 = require("../pack-loader");
51
64
  const utils_1 = require("../utils");
52
- const runtime_1 = require("../runtime");
65
+ const pack_runtime_1 = require("../pack-runtime");
66
+ const sessionShip_1 = require("../../evidence/sessionShip");
53
67
  const serve_hardening_1 = require("../serve-hardening");
54
68
  const credential_context_1 = require("../credential-context");
55
69
  const data_plane_1 = require("../data-plane");
56
- const runtime_2 = require("../runtime");
70
+ const runtime_1 = require("../runtime");
57
71
  const outboundSend_1 = require("../../gateway/outboundSend");
58
72
  const sessionEvents_1 = require("../../analytics/sessionEvents");
59
73
  const base_1 = require("../../engines/base");
@@ -64,6 +78,11 @@ const customRegistry_1 = require("../../engines/customRegistry");
64
78
  const BANNER_LINE = '-'.repeat(62);
65
79
  async function serveCommand(options) {
66
80
  const apiConfig = (0, pack_loader_1.getApiConfig)();
81
+ if (apiConfig.useLiteLLM && !apiConfig.litellmApiKey) {
82
+ // Loud, because the failure mode it replaces was silent: traffic reached the gateway
83
+ // authenticated with whatever sat in the provider-key slot, with analytics disabled.
84
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} AGENTHIPPO_LITELLM_BASE_URL is set without AGENTHIPPO_LITELLM_API_KEY — gateway calls will authenticate as 'sk-dummy' (a strict gateway will 401). Provider keys are never sent to the gateway.`);
85
+ }
67
86
  // Load agent pack (required)
68
87
  let pack;
69
88
  const syncLogger = options.verbose ? new headless_1.ConsoleLogger('[Sync]') : new headless_1.NoOpLogger();
@@ -97,7 +116,98 @@ async function serveCommand(options) {
97
116
  }
98
117
  process.exit(1);
99
118
  }
100
- // Sync pack content to workspace
119
+ const logger = options.verbose ? new headless_1.ConsoleLogger('[Server]') : new headless_1.NoOpLogger();
120
+ // ---------------------------------------------------------------------------------------
121
+ // Pack admission — BEFORE anything from the pack reaches the workspace.
122
+ //
123
+ // `syncPackToWorkspace` used to run here, hundreds of lines before the signature was checked,
124
+ // so a pack that was about to be refused had already materialised its skills, AGENTS.md and
125
+ // MCP server definitions into the workspace and into the derived .claude/.codex directories.
126
+ // The refusal then stopped the turn while the content it was refusing sat on disk, reachable
127
+ // by anything else pointed at that workspace. Verification moves ahead of the write.
128
+ // ---------------------------------------------------------------------------------------
129
+ let activePackDir = pack.packPath;
130
+ let activeVersion = pack.manifest.metadata.version ?? '0.0.0';
131
+ let packDigestValue;
132
+ /**
133
+ * Trusted pack-signing keys. NOT frozen at boot.
134
+ *
135
+ * A trust root that is CONFIGURED BUT UNREACHABLE (a 503 during a rolling restart, a DNS blip)
136
+ * is now distinguishable from one that was never configured, and it is retried on the lease
137
+ * poll — one transient fetch at the wrong second must fail the turn, not brick the process.
138
+ */
139
+ let packTrust = await (0, signature_1.loadPackTrustRoot)({ leaseUrl: process.env.AGENTHIPPO_LEASE_URL });
140
+ const reportTrustRoot = (t, label = 'pack') => {
141
+ for (const w of t.warnings) {
142
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${w}`);
143
+ }
144
+ if (t.status === 'unavailable') {
145
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${label} trust root is configured but unusable: ${t.reason}`);
146
+ }
147
+ };
148
+ reportTrustRoot(packTrust);
149
+ /**
150
+ * Verified per turn, with a cheap change probe — see `createPackVerifier`. The RESULT is
151
+ * cached only as long as the pack tree looks untouched and never longer than the full-reverify
152
+ * interval; the REQUIREMENT is re-read from the lease on every poll, so an operator can turn
153
+ * enforcement on for a fleet already running.
154
+ */
155
+ const packVerifier = (0, signature_1.createPackVerifier)();
156
+ let packSignature = { ok: false, kind: 'no-pack', reason: 'not verified' };
157
+ const verifyPackAt = (packDir, version) => {
158
+ packSignature = packVerifier.refresh({ packDir, pack: packId, packVersion: version, keys: packTrust.keys });
159
+ packDigestValue = packSignature.ok ? packSignature.claims.digest : undefined;
160
+ logger.info(packSignature.ok
161
+ ? `[Pack] signature verified for ${packId}`
162
+ : `[Pack] unverified — ${packSignature.reason}`);
163
+ return packSignature;
164
+ };
165
+ /** The refusal an operator sees, with the remedy for THIS failure rather than a generic one. */
166
+ const packRefusalText = (result) => {
167
+ if (result.ok) {
168
+ return '';
169
+ }
170
+ // "Configured but unreachable" outranks whatever the verifier concluded: with no usable
171
+ // keys every signature fails as `no-trust-root`, and telling an operator to configure a
172
+ // trust root they already configured is the least useful thing we could say.
173
+ const kind = packTrust.status === 'unavailable' && result.kind === 'no-trust-root' ? 'trust-root-unavailable' : result.kind;
174
+ return (0, signature_1.describePackRefusal)({
175
+ kind,
176
+ detail: kind === 'trust-root-unavailable' ? (packTrust.reason ?? result.reason) : result.reason,
177
+ pack: packId,
178
+ packVersion: activeVersion,
179
+ packDir: activePackDir,
180
+ trustRootHint: packTrust.url ? `Trust root: ${packTrust.url} (${packTrust.reason ?? 'unavailable'})` : packTrust.reason,
181
+ });
182
+ };
183
+ verifyPackAt(activePackDir, activeVersion);
184
+ /**
185
+ * Signing REQUIRED by default.
186
+ *
187
+ * This is a deliberate break with the previous shipped behaviour, which logged
188
+ * `[Pack] unsigned or unverifiable — …` and then ran the pack anyway. Warn-and-run is not a
189
+ * gate; it is a log line next to an ungated code path, and it meant the default deployment
190
+ * shape executed whatever was in the pack directory.
191
+ *
192
+ * Tighten-only survives intact: the escape hatch below relaxes only this INSTANCE's default,
193
+ * and `resolveRequireSignedPack` checks the lease's demand first, so a control plane that sets
194
+ * `requireSignedPack` cannot be opted out of locally.
195
+ */
196
+ const allowUnsignedPack = Boolean(options.allowUnsignedPack) || (0, signature_1.allowUnsignedPackFromEnv)();
197
+ if (!packSignature.ok) {
198
+ if (!allowUnsignedPack) {
199
+ console.error(`${utils_1.COLORS.red}Error: ${packRefusalText(packSignature)}${utils_1.COLORS.reset}`);
200
+ process.exit(1);
201
+ }
202
+ // Unmissable, every boot, at warn level regardless of --verbose.
203
+ console.warn(BANNER_LINE);
204
+ console.warn(`${utils_1.COLORS.yellow}!! RUNNING AN UNVERIFIED PACK — signature checking is DISABLED on this instance.${utils_1.COLORS.reset}`);
205
+ console.warn(`${utils_1.COLORS.yellow}!! ${packSignature.reason}${utils_1.COLORS.reset}`);
206
+ console.warn(`${utils_1.COLORS.yellow}!! Whatever is in ${activePackDir} will be executed, verified by nobody.${utils_1.COLORS.reset}`);
207
+ console.warn(`${utils_1.COLORS.yellow}!! This is a LOCAL DEVELOPMENT setting (AGENTHIPPO_ALLOW_UNSIGNED_PACK). Do not ship it.${utils_1.COLORS.reset}`);
208
+ console.warn(BANNER_LINE);
209
+ }
210
+ // Sync pack content to workspace — only now that the pack has been admitted.
101
211
  const bundledPromptsDir = (0, utils_1.findBundledBasePromptsDir)();
102
212
  await (0, headless_1.syncPackToWorkspace)(options.workspace, pack.packPath, packId, defaultEngine, syncLogger, bundledPromptsDir);
103
213
  console.log(`OK: Loaded agent: ${pack.manifest.metadata.name} v${pack.manifest.metadata.version}`);
@@ -106,7 +216,12 @@ async function serveCommand(options) {
106
216
  // Resolve persistent session state (serve-v3 §A1: replaces the tmpdir default).
107
217
  const { dir: storagePath, ephemeral } = (0, serve_hardening_1.resolveStateDir)(options.stateDir);
108
218
  const bindHost = (0, serve_hardening_1.resolveBindHost)(options.host);
109
- // Passed to createHeadlessRunner below so the runner's own SessionEventWriter uses the same
219
+ // Who serve believes a caller to be decides whose session state and whose data credentials they
220
+ // reach, so an unpinned identity header is not a detail to leave in a docstring.
221
+ for (const line of (0, serve_hardening_1.identityHeaderAdvice)(process.env, bindHost)) {
222
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${line}`);
223
+ }
224
+ // Passed to createPackRunner below so the runner's own SessionEventWriter uses the same
110
225
  // root — a fail-closed warrant denial lands in the same per-session JSONL as the tool events
111
226
  // it precedes. The denial happens before runner.run() is ever called, so it needs its own
112
227
  // writer here too. See resolveAnalyticsSessionsDir's docstring for the precedence.
@@ -117,12 +232,42 @@ async function serveCommand(options) {
117
232
  // serve runs the connector broker-side and engines get a per-turn nonce instead of the
118
233
  // credential. The raw data credential never crosses into the agent's trust domain.
119
234
  const dataPlaneCfg = (0, data_plane_1.resolveDataPlaneConfig)(pack.manifest.spec, pack.packPath);
235
+ // EDGE-registration mode (P1 fix, obo-inprogress §2): the trusted edge (IdP Caddy)
236
+ // registers each turn with the sidecar and injects the nonce as a request header; serve
237
+ // merely adopts it. serve holds NO registration credential at all — nothing in this
238
+ // container's /proc can mint a nonce for an arbitrary userId. Takes precedence over the
239
+ // token-based sidecar mode below.
240
+ const edgeCfg = (0, data_plane_1.resolveEdgeDataPlane)();
241
+ const edgeDataPlane = edgeCfg ? new data_plane_1.EdgeDataPlane(edgeCfg) : null;
242
+ // Data-plane SIDECAR mode (secure-obo-design §7.6): when the connector runs in its OWN
243
+ // container, AGENTHIPPO_DATA_PLANE_CONTROL_URL/TOKEN + AGENTHIPPO_DATA_MCP_URL are set and
244
+ // serve holds NO backend base credential — it registers turns with the sidecar and hands the
245
+ // engine only the nonce. Takes precedence over an in-process connector.
246
+ const remoteCfg = edgeDataPlane ? null : (0, data_plane_1.resolveRemoteDataPlane)();
247
+ const remoteDataPlane = remoteCfg ? new data_plane_1.RemoteDataPlane(remoteCfg) : null;
120
248
  // One shared registry: a single per-turn nonce grants BOTH the data-plane and egress
121
- // capabilities (secure-obo-design §7.6 step 2). Created when either broker will run.
122
- const turnRegistry = (dataPlaneCfg || options.gatewayBridge) ? new data_plane_1.TurnRegistry() : null;
249
+ // capabilities (secure-obo-design §7.6 step 2). Created when either LOCAL broker will run.
250
+ const turnRegistry = ((dataPlaneCfg && !remoteDataPlane && !edgeDataPlane) || options.gatewayBridge) ? new data_plane_1.TurnRegistry() : null;
123
251
  const auditEvent = (sessionKey, event, fields) => sessionEventWriter.append(sessionKey, (0, base_1.generateRequestId)(), event, fields);
124
252
  let dataPlane = null;
125
- if (dataPlaneCfg) {
253
+ if (edgeDataPlane) {
254
+ process.env[data_plane_1.DATA_MCP_URL_ENV] = edgeDataPlane.url;
255
+ // Belt-and-braces: edge mode must never see a register token, but if one leaked into the
256
+ // deploy env anyway, strip it before any engine child inherits it.
257
+ delete process.env.AGENTHIPPO_DATA_PLANE_TOKEN;
258
+ delete process.env.AGENTHIPPO_DATA_PLANE_CONTROL_URL;
259
+ console.log(`OK: Data plane EDGE-registered — gateway ${edgeDataPlane.url}; turn nonces are minted at the edge, serve holds no registration credential`);
260
+ }
261
+ else if (remoteDataPlane) {
262
+ process.env[data_plane_1.DATA_MCP_URL_ENV] = remoteDataPlane.url;
263
+ // The control-API token is captured in the RemoteDataPlane (serve memory). Strip it from
264
+ // the process env so no engine child inherits it — otherwise the agent could register a
265
+ // turn as ANY user and read their rows (forge-identity escalation). The engine keeps the
266
+ // gateway URL + its own per-turn nonce, which is all it needs (secure-obo-design §7.6).
267
+ delete process.env.AGENTHIPPO_DATA_PLANE_TOKEN;
268
+ console.log(`OK: Data plane SIDECAR — gateway ${remoteDataPlane.url}; base credentials stay in the connector container, not here`);
269
+ }
270
+ else if (dataPlaneCfg) {
126
271
  try {
127
272
  dataPlane = await data_plane_1.DataPlane.start(dataPlaneCfg, {
128
273
  logger: options.verbose ? new headless_1.ConsoleLogger('[DataPlane]') : new headless_1.NoOpLogger(),
@@ -143,12 +288,42 @@ async function serveCommand(options) {
143
288
  // Egress broker (secure-obo-design §7.6): capture the gateway token broker-side BEFORE any
144
289
  // engine spawns, so it can be stripped from the process env the children inherit. The broker
145
290
  // starts alongside the gateway bridge below; the token is held only in this closure.
146
- const brokerGatewayToken = options.gatewayBridge ? (0, runtime_2.readOpenClawGatewayToken)() : undefined;
291
+ // Captured UNCONDITIONALLY, and the env strip is immediate (serve-v4 §10 hole 2): the strip
292
+ // used to live inside the broker branch, so a serve without --gateway-bridge kept the token
293
+ // in process.env and every engine child (they all spread it) inherited a credential nothing
294
+ // in this deployment would ever legitimately use.
295
+ const brokerGatewayToken = (0, runtime_1.readOpenClawGatewayToken)();
296
+ delete process.env.OPENCLAW_GATEWAY_TOKEN;
147
297
  let egressBroker = null;
148
298
  // Mint one nonce per turn and assemble the per-spawn child env. Returns null outside any
149
299
  // broker (no capability to grant). The URLs ride process.env (boot-stable) for config
150
300
  // invariance; only the nonce is per-spawn.
151
- const registerServeTurn = (reg) => {
301
+ const registerServeTurn = async (reg) => {
302
+ // Edge mode: the trusted edge already registered this turn and injected the nonce as a
303
+ // request header — adopt it. No edge nonce (misrouted request, or a channel turn that
304
+ // never traversed the edge) ⇒ no data capability, fail closed at the gateway.
305
+ if (edgeDataPlane) {
306
+ const edgeNonce = reg.edgeNonce?.trim();
307
+ if (!edgeNonce) {
308
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} edge data plane active but no ${data_plane_1.TURN_NONCE_HEADER} header on this turn — data tools will fail closed (check the edge route's forward_auth)`);
309
+ return null;
310
+ }
311
+ const r = edgeDataPlane.adoptTurn(edgeNonce);
312
+ const env = { ...r.env };
313
+ if (egressBroker) {
314
+ env[data_plane_1.EGRESS_URL_ENV] = egressBroker.url;
315
+ }
316
+ return { nonce: r.nonce, env, release: r.release };
317
+ }
318
+ // Sidecar: register with the connector container; serve never sees a base credential.
319
+ if (remoteDataPlane) {
320
+ const r = await remoteDataPlane.registerTurn(reg);
321
+ const env = { ...r.env };
322
+ if (egressBroker) {
323
+ env[data_plane_1.EGRESS_URL_ENV] = egressBroker.url;
324
+ }
325
+ return { nonce: r.nonce, env, release: r.release };
326
+ }
152
327
  if (!turnRegistry) {
153
328
  return null;
154
329
  }
@@ -162,6 +337,16 @@ async function serveCommand(options) {
162
337
  }
163
338
  return { nonce, env, release };
164
339
  };
340
+ // Edge-registered turn nonce, injected by the trusted edge per chat request (edge mode only —
341
+ // otherwise ignored). Trusted like the pinned identity header: only the in-network edge can
342
+ // reach serve, and a forged value fails closed at the gateway (unknown nonce ⇒ 401).
343
+ const edgeTurnNonce = (headers) => {
344
+ if (!edgeDataPlane) {
345
+ return undefined;
346
+ }
347
+ const v = headers[data_plane_1.TURN_NONCE_HEADER];
348
+ return Array.isArray(v) ? v[0] : v;
349
+ };
165
350
  const turnLimiter = new serve_hardening_1.TurnLimiter(options.maxTurnsPerSession ?? 0);
166
351
  // Per-identity request-rate cap (req/min). Keyed on the trusted user id (falling back to
167
352
  // client IP) so rotating session_id can't bypass it. AGENTHIPPO_SERVE_RATE_LIMIT=0 disables.
@@ -212,18 +397,22 @@ async function serveCommand(options) {
212
397
  await (0, state_sync_1.ensureSessionLocal)(s3State, storagePath, sessionKey, stateLog);
213
398
  }
214
399
  };
215
- const logger = options.verbose ? new headless_1.ConsoleLogger('[Server]') : new headless_1.NoOpLogger();
216
400
  // Find SDK CLI paths
217
401
  const claudeCodePath = (0, utils_1.findClaudeCodePath)();
218
402
  const codexPath = (0, utils_1.findCodexPath)();
219
- // Validate required CLI is available
220
- if (defaultEngine === 'claude' && !claudeCodePath) {
403
+ // Validate required CLI is available — but ONLY when this process will run the engine.
404
+ // A container sandbox runs the engine inside its own image, so requiring the SDK on the host
405
+ // is wrong and refuses to boot on exactly the minimal host a tier-2 deployment wants
406
+ // (observed live: a Linux host with no npm/codex could not start `--sandbox container`).
407
+ // The process driver shares this host, so it still needs the CLI present.
408
+ const engineRunsInSandbox = options.sandbox === 'container';
409
+ if (!engineRunsInSandbox && defaultEngine === 'claude' && !claudeCodePath) {
221
410
  console.error(`${utils_1.COLORS.red}Error: Claude Code CLI not found.${utils_1.COLORS.reset}`);
222
411
  console.error(' Install: npm install -g @anthropic-ai/claude-code');
223
412
  console.error(' Or set CLAUDE_CODE_PATH environment variable');
224
413
  process.exit(1);
225
414
  }
226
- if (defaultEngine === 'codex' && !codexPath) {
415
+ if (!engineRunsInSandbox && defaultEngine === 'codex' && !codexPath) {
227
416
  console.error(`${utils_1.COLORS.red}Error: Codex CLI not found.${utils_1.COLORS.reset}`);
228
417
  console.error(' Install: npm install -g @openai/codex');
229
418
  console.error(' Or set CODEX_PATH environment variable');
@@ -234,38 +423,589 @@ async function serveCommand(options) {
234
423
  const permissions = {
235
424
  fileAccess: (0, pack_loader_1.getFileAccess)(pack),
236
425
  };
237
- const runner = (0, headless_1.createHeadlessRunner)({
426
+ // R-3 policy provenance (PLAN/compliance.md §3.1): remember WHICH policy is in force and
427
+ // announce changes. `policy_applied` marks the boundary; every turn between two boundaries
428
+ // carries the version via the runner's provenance stamp below, so "was the cap enforced?"
429
+ // stays answerable after anyone edits a cap.
430
+ let lastPolicyVersion;
431
+ let provenanceAnnounced = false;
432
+ const notePolicy = (lease) => {
433
+ const v = (0, lease_1.policyVersion)(lease);
434
+ // Announce once at boot even with no lease (unmanaged deployments still have a digest,
435
+ // an engine build and a tier), then again on every policy change. This event is the
436
+ // PROVENANCE DIMENSION: turns don't repeat these facts — the evidence index joins a
437
+ // turn to the latest announcement for its instance at or before the turn's timestamp.
438
+ if (provenanceAnnounced && v === lastPolicyVersion) {
439
+ return;
440
+ }
441
+ provenanceAnnounced = true;
442
+ lastPolicyVersion = v;
443
+ void sessionEventWriter.append('serve', (0, base_1.generateRequestId)(), 'policy_applied', {
444
+ instance_id: instanceId,
445
+ policy_version: v ?? null,
446
+ pack_digest: packDigestValue ?? null,
447
+ engine_version: (0, deps_1.installedEngineVersion)(defaultEngine) ?? null,
448
+ tier: options.sandbox === 'container' ? 2 : options.sandbox ? 0 : null,
449
+ enabled: lease?.enabled ?? null,
450
+ pinned_version: lease?.version ?? null,
451
+ caps: lease?.caps ?? null,
452
+ deny_count: lease?.deny?.length ?? 0,
453
+ });
454
+ };
455
+ // Same factory a sandbox uses (cli/pack-runtime.ts) — built after activeVersion is known.
456
+ let inProcessRunner;
457
+ /**
458
+ * Where sandboxes read the pack from.
459
+ *
460
+ * With --pull-packs this is a SYMLINK (`<state>/packs/current`), never the resolved directory:
461
+ * the sandbox drivers capture this path once at construction, so a stable name whose target is
462
+ * swapped is what lets a later version reach new sandboxes without rebuilding the runner. A
463
+ * sandbox already starting keeps the directory it opened, which is exactly the drain semantics
464
+ * version convergence already has.
465
+ */
466
+ const packsRoot = path.join(storagePath, 'packs');
467
+ const storeDownloader = async (slug, version) => {
468
+ const files = await (0, index_1.downloadFromStore)({ type: 'pack', slug, version });
469
+ return files.map((f) => ({ path: f.path, content: f.content }));
470
+ };
471
+ /** Converge to the pinned version if we can; never take the fleet down trying. */
472
+ const reconcileToPin = async (pinned) => {
473
+ if (!options.pullPacks || !pinned || pinned === activeVersion) {
474
+ return;
475
+ }
476
+ const res = await (0, reconcile_1.reconcilePinnedPack)({
477
+ pack: packId,
478
+ currentVersion: activeVersion,
479
+ pinnedVersion: pinned,
480
+ packsRoot,
481
+ download: storeDownloader,
482
+ publicKeysByKid: packTrust.keys,
483
+ logger: { info: (m) => logger.info(m), warn: (m) => logger.info(m) },
484
+ });
485
+ if (res.ok) {
486
+ activeVersion = res.version;
487
+ activePackDir = res.packDir;
488
+ verifyPackAt(activePackDir, activeVersion);
489
+ logger.info(`[Pack] now serving ${packId}@${activeVersion} from ${res.packDir}`);
490
+ return;
491
+ }
492
+ // Deliberately not fatal: the lease fails OPEN on transport, and a bad tag or a store
493
+ // outage must not become a fleet-wide stop. The instance keeps reporting the version it is
494
+ // really running, so the console shows "pinned to X, running Y" rather than an outage.
495
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} cannot converge to ${packId}@${pinned}: ${res.reason}. Still serving @${activeVersion}.`);
496
+ };
497
+ const warrantKey = (0, support_1.loadWarrantKeypair)(logger);
498
+ // Lease: kill switch + version pin. No URL configured ⇒ unmanaged deployment, always admits.
499
+ /** Set once the sandbox runner exists; the lease-poll identity reads it lazily. */
500
+ let sandboxRunnerRef;
501
+ // Stable across restarts, unique per instance: an operator watching the console must see one
502
+ // row per instance that survives a redeploy, not a new row each boot (and not one row per NAT
503
+ // address for a whole fleet). Persisted beside the instance's own state.
504
+ const instanceId = (() => {
505
+ try {
506
+ const file = path.join((0, serve_hardening_1.resolveStateDir)(options.stateDir).dir, 'instance-id');
507
+ if (fs.existsSync(file)) {
508
+ return fs.readFileSync(file, 'utf-8').trim();
509
+ }
510
+ const id = `${os.hostname()}-${crypto.randomBytes(4).toString('hex')}`;
511
+ fs.mkdirSync(path.dirname(file), { recursive: true });
512
+ fs.writeFileSync(file, id);
513
+ return id;
514
+ }
515
+ catch {
516
+ // Never let bookkeeping stop a boot; a per-process id still beats collapsing by address.
517
+ return `${os.hostname()}-${crypto.randomBytes(4).toString('hex')}`;
518
+ }
519
+ })();
520
+ /** Lazily read by the lease-poll identity, which is built before the pack path is resolved. */
521
+ const activeVersionRef = { get: () => pack?.manifest.metadata.version };
522
+ // ONE constant for the poll interval and everything derived from it. The cache used to default
523
+ // to 60s while every poll defaulted to 30s and the banner printed 30 — so the real kill latency
524
+ // was up to 3x what an operator was told.
525
+ const leasePollSeconds = (0, lease_1.resolveLeasePollSeconds)(options.leasePollSeconds);
526
+ /**
527
+ * Trusted LEASE-signing keys — a separate root from the pack one.
528
+ *
529
+ * These used to be the same keyring (`signingKeys: () => packTrust.keys`), which made signed
530
+ * leases unreachable end to end: the control plane publishes the lease public key under purpose
531
+ * `lease-signing` and refuses to register one kid under two purposes, so a correctly-configured
532
+ * plane and a correctly-configured serve could never name the same key. With `requireSignedLease`
533
+ * sticky and no plane-side undo, that disagreement latches an instance into refusing turns.
534
+ *
535
+ * A DERIVED root that comes back `unavailable` is the normal state of every plane that publishes
536
+ * no lease-signing key, so it is only reported at boot when an operator named one outright.
537
+ * Otherwise `leaseSigningKeys` says everything there is to say, and only once a signed lease has
538
+ * actually arrived.
539
+ */
540
+ let leaseTrust = await (0, signature_1.loadLeaseTrustRoot)({ leaseUrl: process.env.AGENTHIPPO_LEASE_URL });
541
+ if (leaseTrust.explicitlyConfigured) {
542
+ reportTrustRoot(leaseTrust, 'lease');
543
+ }
544
+ let leaseTrustFallbackWarned = false;
545
+ /**
546
+ * BACK-COMPAT, deliberately not a cutover.
547
+ *
548
+ * A deployment that verifies signed leases against the pack keyring today keeps working: with no
549
+ * lease root named outright, the pack keys stay in play and the (possibly empty) derived lease
550
+ * keys are added to them. The warning fires ONCE, and only when a signed lease is actually being
551
+ * verified, so a deployment that never uses signed leases never sees it.
552
+ *
553
+ * Not a hard switch because `requireSignedLease` latches per instance and the plane has no undo:
554
+ * an instance that stopped recognising its lease key would refuse turns and could not be talked
555
+ * out of it from the console.
556
+ */
557
+ const leaseSigningKeys = () => {
558
+ const selected = (0, signature_1.selectLeaseSigningKeys)(leaseTrust, packTrust);
559
+ if (selected.packFallback && !leaseTrustFallbackWarned) {
560
+ leaseTrustFallbackWarned = true;
561
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} verifying signed leases against the PACK trust root — ` +
562
+ 'no lease trust root is configured on this instance. The control plane publishes lease keys under ' +
563
+ 'purpose `lease-signing` and refuses to register one kid under two purposes, so this stops working ' +
564
+ 'the moment the plane rotates to a dedicated lease key — and requireSignedLease is sticky with no ' +
565
+ 'plane-side undo, so that failure cannot be fixed from the console. Set ' +
566
+ `${signature_1.LEASE_JWKS_URL_ENV}=https://<plane>/.well-known/jwks.json?purpose=lease-signing ` +
567
+ `(or ${signature_1.LEASE_SIGNING_KEYS_ENV} with inline {"<kid>":"<public PEM>"} for an air-gapped install).` +
568
+ (leaseTrust.reason ? ` The derived lease trust root is unusable: ${leaseTrust.reason}` : ''));
569
+ }
570
+ return selected.keys;
571
+ };
572
+ const leaseSource = (0, lease_1.createLeaseSource)({
573
+ url: process.env.AGENTHIPPO_LEASE_URL,
574
+ // The poll IS the registration — see LeaseSourceOptions.identity.
575
+ identity: () => ({
576
+ instance: instanceId,
577
+ pack: packId,
578
+ // The version actually being served: after a pull this is the pinned one, and the
579
+ // console's whole convergence view depends on it being the truth rather than boot state.
580
+ version: activeVersionRef.get(),
581
+ tier: options.sandbox === 'container' ? 2 : options.sandbox ? 0 : undefined,
582
+ sandboxes: sandboxRunnerRef?.sandboxes().length,
583
+ // Which engine, and which BUILD of it. Pack version alone cannot explain a fleet-wide
584
+ // behaviour change that came from an engine upgrade, and the CLI is not engine-version
585
+ // agnostic, so the console needs both to answer "what is actually running?".
586
+ engine: defaultEngine,
587
+ engineversion: (0, deps_1.installedEngineVersion)(defaultEngine),
588
+ }),
589
+ // Derived from the poll interval by ONE function, so the two can no longer drift. Worst-case
590
+ // kill latency is poll + cache, and that is the number the boot banner prints.
591
+ cacheSeconds: (0, lease_1.leaseCacheSecondsFor)(leasePollSeconds),
592
+ // Signed-lease verification uses the LEASE trust root, falling back to the pack one while no
593
+ // lease root is configured. Read through a closure so a keyring that arrives on a later poll
594
+ // is picked up without rebuilding this.
595
+ signingKeys: () => leaseSigningKeys(),
596
+ requireSignedLease: options.requireSignedLease,
597
+ instanceId,
598
+ ...(options.leaseMaxStalenessSeconds !== undefined ? { maxStalenessSeconds: options.leaseMaxStalenessSeconds } : {}),
599
+ logger: {
600
+ // Lease warnings are security-relevant (unsigned lease, staleness ceiling, insecure URL)
601
+ // and must not be hidden behind --verbose.
602
+ info: (m) => logger.info(m),
603
+ warn: (m) => console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${m}`),
604
+ },
605
+ });
606
+ /**
607
+ * Retry the trust root while it is missing, and re-verify once it arrives.
608
+ *
609
+ * Only while MISSING: a loaded trust root is not re-fetched, so key rotation still needs the
610
+ * documented restart and a compromised plane cannot swap the trust root under a running
611
+ * instance mid-flight. This exists solely so a transient failure at boot is transient — the
612
+ * `unavailable` status is exactly what makes "transient" distinguishable from "not configured".
613
+ */
614
+ const retrySigningKeys = async () => {
615
+ if (packTrust.status !== 'configured') {
616
+ const next = await (0, signature_1.loadPackTrustRoot)({ leaseUrl: process.env.AGENTHIPPO_LEASE_URL });
617
+ if (next.status === 'configured') {
618
+ packTrust = next;
619
+ reportTrustRoot(next);
620
+ logger.info(`[Pack] signing keys now available (${Object.keys(next.keys).length}); re-verifying`);
621
+ verifyPackAt(activePackDir, activeVersion);
622
+ }
623
+ }
624
+ // The lease root gets the SAME rule, not a better one: retried only while missing, never
625
+ // re-fetched once configured. Improving one and not the other would leave two roots with two
626
+ // rotation stories, which is how the rotation runbook silently stops being true for half of it.
627
+ if (leaseTrust.status !== 'configured') {
628
+ const next = await (0, signature_1.loadLeaseTrustRoot)({ leaseUrl: process.env.AGENTHIPPO_LEASE_URL });
629
+ if (next.status === 'configured') {
630
+ leaseTrust = next;
631
+ if (next.explicitlyConfigured) {
632
+ reportTrustRoot(next, 'lease');
633
+ }
634
+ logger.info(`[Lease] lease-signing keys now available (${Object.keys(next.keys).length})`);
635
+ }
636
+ }
637
+ };
638
+ // Record the boot policy and watch for changes on the lease-poll cadence. The lease source
639
+ // caches, so this adds no extra fetches — it re-reads whatever the poll already trusts.
640
+ void leaseSource.get().then(notePolicy).catch(() => undefined);
641
+ {
642
+ const everyMs = leasePollSeconds * 1000;
643
+ const policyTimer = setInterval(() => {
644
+ void leaseSource.get().then(notePolicy).catch(() => undefined);
645
+ void retrySigningKeys().catch(() => undefined);
646
+ }, everyMs);
647
+ policyTimer.unref?.();
648
+ }
649
+ if (options.pullPacks) {
650
+ fs.mkdirSync(packsRoot, { recursive: true });
651
+ // Materialise whatever we boot with, so `current` is valid before any pin is considered and
652
+ // the drivers can capture one stable path.
653
+ const bootDir = path.join(packsRoot, activeVersion);
654
+ if (!fs.existsSync(bootDir)) {
655
+ fs.cpSync(pack.packPath, bootDir, { recursive: true });
656
+ }
657
+ (0, reconcile_1.promoteCurrent)(packsRoot, bootDir);
658
+ activePackDir = path.join(packsRoot, reconcile_1.CURRENT_LINK);
659
+ await reconcileToPin((await leaseSource.get())?.version);
660
+ // The pack is now read through `current`, which is a different path than the one verified
661
+ // above. Re-verify so `packSignature` describes the tree that will actually run.
662
+ verifyPackAt(activePackDir, activeVersion);
663
+ }
664
+ activeVersionRef.get = () => activeVersion;
665
+ inProcessRunner = (0, pack_runtime_1.createPackRunner)({
238
666
  storagePath,
239
667
  analyticsSessionsDir,
240
668
  packId,
241
- packVersion: pack?.manifest.metadata.version,
242
- bundledPromptsDir,
243
- apiKey: apiConfig.apiKey,
244
- baseUrl: apiConfig.baseUrl,
245
- useLiteLLM: apiConfig.useLiteLLM,
246
- // Per-destination keys so each engine authenticates to the endpoint it actually dials
247
- // (codex→OpenAI, claude→Anthropic, or the LiteLLM proxy) — no provider-blind collapse.
248
- litellmApiKey: apiConfig.litellmApiKey,
249
- openaiApiKey: apiConfig.openaiApiKey,
250
- anthropicApiKey: apiConfig.anthropicApiKey,
669
+ packVersion: activeVersion,
670
+ provenance: () => ({
671
+ policyVersion: lastPolicyVersion,
672
+ packDigest: packDigestValue,
673
+ engineVersion: (0, deps_1.installedEngineVersion)(defaultEngine),
674
+ tier: options.sandbox === 'container' ? 2 : options.sandbox ? 0 : undefined,
675
+ instanceId,
676
+ }),
677
+ workspaceRoot: options.workspace,
678
+ credentials: {
679
+ apiKey: apiConfig.apiKey,
680
+ baseUrl: apiConfig.baseUrl,
681
+ useLiteLLM: apiConfig.useLiteLLM,
682
+ litellmApiKey: apiConfig.litellmApiKey,
683
+ openaiApiKey: apiConfig.openaiApiKey,
684
+ anthropicApiKey: apiConfig.anthropicApiKey,
685
+ },
251
686
  logger,
252
- freshState: false,
253
- claudeCodeExecutablePath: claudeCodePath,
254
- codexExecutablePath: codexPath,
255
687
  permissionHandler,
256
- cliEnvPatch: (0, runtime_1.buildCliEnvPatch)({
257
- cliPath: process.execPath,
258
- workspaceRoot: options.workspace,
259
- agentId: packId,
260
- baseEnv: process.env,
261
- }),
262
- ...(claudeCheapModel ? { engineSettings: { claudeCheapModel } } : {}),
688
+ claudeCheapModel,
689
+ omitGatewayToken: true,
263
690
  });
691
+ // Converge on the same cadence the fleet already polls, so pinning a version in the console
692
+ // reaches instances without a redeploy. Errors are swallowed by reconcileToPin itself.
693
+ if (options.pullPacks) {
694
+ const every = leasePollSeconds * 1000;
695
+ const timer = setInterval(() => {
696
+ void leaseSource.get().then((l) => reconcileToPin(l?.version)).catch(() => undefined);
697
+ }, every);
698
+ timer.unref?.();
699
+ }
700
+ // Container sandboxes got NO filesystem mounts at all — the driver honours `fs`, but serve never
701
+ // passed it, so a sandbox's `/state/workspace` was container-private storage. Everything the
702
+ // agent wrote was invisible on the host and lost when the sandbox died. The process driver needs
703
+ // no mounts: tier 0 shares this host's filesystem already.
704
+ //
705
+ // The workspace is PER SESSION unless --shared-workspace says otherwise. One host directory
706
+ // mounted writable into every session's sandbox is a shared disk between users of the same
707
+ // agent: whatever session A writes (a query export, a downloaded file), session B reads on its
708
+ // next turn. That silently contradicts "one disposable sandbox per session" for everything that
709
+ // matters, since the container boundary does not extend to a directory both containers mount.
710
+ const sharedWorkspace = Boolean(options.sharedWorkspace);
711
+ const sandboxMounts = options.sandbox === 'container'
712
+ ? {
713
+ // serve's resolved pack, not the image's baked copy — the two cannot then disagree
714
+ // about which pack version is running.
715
+ readonly: [`${activePackDir}:${containerDriver_1.SANDBOX_PACK_DIR}`],
716
+ writable: [`${options.workspace}:${containerDriver_1.SANDBOX_WORKSPACE_DIR}`],
717
+ }
718
+ : undefined;
719
+ if (sandboxMounts && sharedWorkspace) {
720
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} --shared-workspace: every session's sandbox mounts ` +
721
+ `${options.workspace} read-write, so sessions can read and overwrite each other's files. ` +
722
+ `Only safe when all sessions belong to one trust domain.`);
723
+ }
724
+ // serve wrote that workspace as root; the sandbox runs non-root and must be able to write it too.
725
+ // Boot-time and once, before the first turn can arrive.
726
+ let sandboxImageUser;
727
+ if (sandboxMounts && options.sandboxImage) {
728
+ await (0, support_1.alignSandboxWorkspaceOwnership)({
729
+ exec: support_1.dockerExec,
730
+ image: options.sandboxImage,
731
+ writable: sandboxMounts.writable,
732
+ // A failure here is what an operator must see even without --verbose; success is noise.
733
+ logger: { info: (m) => logger.info(m), warn: (m) => console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${m}`) },
734
+ });
735
+ sandboxImageUser = await (0, support_1.resolveSandboxImageUser)(support_1.dockerExec, options.sandboxImage);
736
+ }
737
+ // A session's engine state must outlive its sandboxes or the conversation restarts every time a
738
+ // sandbox is reaped (serve-v4-sandbox §8.4). Per session, under serve's own state dir, so the
739
+ // existing per-session sync/prune machinery already covers it.
740
+ const sandboxStateDirFor = (sessionKey) => (0, support_1.ensureSandboxStateDir)(path.join(storagePath, 'sandbox-state', (0, state_sync_1.safeSessionId)(sessionKey)), sandboxImageUser);
741
+ /**
742
+ * Mounts for ONE session's sandbox: the pack read-only, and a workspace only this session sees.
743
+ *
744
+ * The per-session workspace lives beside that session's engine state, so the same reap/prune
745
+ * machinery already covers it and the agent's output stays visible on the host — the property
746
+ * the shared mount was added for — without also being visible to every other session.
747
+ */
748
+ const sandboxFsFor = async (sessionKey) => {
749
+ if (!sandboxMounts) {
750
+ return { readonly: [], writable: [] };
751
+ }
752
+ if (sharedWorkspace) {
753
+ return sandboxMounts;
754
+ }
755
+ const dir = (0, support_1.ensureSandboxStateDir)(path.join(storagePath, 'sandbox-workspace', (0, state_sync_1.safeSessionId)(sessionKey)), sandboxImageUser);
756
+ return { readonly: sandboxMounts.readonly, writable: [`${dir}:${containerDriver_1.SANDBOX_WORKSPACE_DIR}`] };
757
+ };
758
+ /**
759
+ * "May this pack run, at this version, for this session?" — signature requirement, version pin,
760
+ * image digest and scoped deny rules, in ONE place.
761
+ *
762
+ * Both gates call this: sandbox creation (where it decides whether a sandbox may be born) and,
763
+ * on deployments with no sandbox, the per-turn governor gate. Two copies of these four checks
764
+ * is exactly the drift this codebase keeps paying for, and the no-sandbox path having no copy
765
+ * at all is why `requireSignedPack` and a version pin could be switched on in the console and
766
+ * enforce nothing on the default deployment shape.
767
+ */
768
+ const assertPackAdmitted = (currentLease, sessionKey, userId) => {
769
+ const required = (0, signature_1.resolveRequireSignedPack)({
770
+ // The lease's demand is read FIRST and cannot be waived by any local setting — that is
771
+ // what keeps the requirement tighten-only now that the local default is "required".
772
+ lease: currentLease?.requireSignedPack,
773
+ localRequire: options.requireSignedPack,
774
+ localAllowUnsigned: allowUnsignedPack,
775
+ });
776
+ if (required) {
777
+ // RE-VERIFIED, not read off a boot-time boolean. In the default no-sandbox shape the
778
+ // engine runs in THIS process against a pack scaffolded with `fileAccess: full`, so the
779
+ // agent can edit its own pack directory between turns; a cached "verified once at boot"
780
+ // would pass every one of those later turns. `createPackVerifier` keeps this cheap — a
781
+ // stat-only probe in the common case, a full digest when the tree looks touched or the
782
+ // re-verify interval has elapsed.
783
+ const result = packVerifier.verify({
784
+ packDir: activePackDir, pack: packId, packVersion: activeVersion, keys: packTrust.keys,
785
+ });
786
+ if (result !== packSignature) {
787
+ packSignature = result;
788
+ packDigestValue = result.ok ? result.claims.digest : undefined;
789
+ }
790
+ if (!result.ok) {
791
+ throw new lease_1.LeaseDeniedError(packRefusalText(result));
792
+ }
793
+ }
794
+ // sessionKey makes admission SCOPED: a deny rule naming this session or its user refuses
795
+ // at the same gate that enforces the fleet-wide switch.
796
+ (0, lease_1.assertAdmitted)(currentLease, {
797
+ pack: packId,
798
+ version: activeVersion,
799
+ image: options.sandboxImage,
800
+ sessionKey,
801
+ userHash: serve_hardening_1.userNamespaceHash,
802
+ userId,
803
+ });
804
+ };
805
+ // Assigned just below; the identity closure above reads it lazily so a poll can report live
806
+ // sandbox count without forcing the runner to be constructed before the lease source.
807
+ // eslint-disable-next-line prefer-const
808
+ // Sandbox mode: the same HeadlessRunner shape, body relocated into a per-session sandbox.
809
+ // serve's turn loop below is untouched — see sandbox/sandboxRunner.ts.
810
+ const sandboxRunner = options.sandbox
811
+ ? (0, sandboxRunner_1.createSandboxRunner)({
812
+ driver: options.sandbox === 'container'
813
+ // A non-internal route network is a real weakening of the tier-2 claim, so its warning
814
+ // must reach an operator who did not pass --verbose.
815
+ ? new containerDriver_1.ContainerSandboxDriver({
816
+ exec: support_1.dockerExec,
817
+ logger: {
818
+ info: (m) => logger.info(m),
819
+ warn: (m) => console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${m}`),
820
+ },
821
+ })
822
+ : new processDriver_1.ProcessSandboxDriver({
823
+ command: process.execPath,
824
+ // --workspace is REQUIRED, not optional: a tier-0 sandbox shares this host's
825
+ // filesystem, and serve sends this same path as `workspaceRoot` on every run
826
+ // frame. Omitting it left the sandbox on the container-image default
827
+ // (/state/workspace), so a serve-launched process sandbox died at boot with
828
+ // ENOENT: mkdir '/state/workspace'.
829
+ // pack.packPath, not options.agent: serve has ALREADY resolved the pack (a
830
+ // name resolves against ~/.agent-hippo/agents and the workspace), and the
831
+ // sandbox must run that exact directory rather than re-resolve a name from a
832
+ // different cwd and possibly land on a different pack.
833
+ args: [
834
+ // Only forward the script path when the process actually HAS one. A
835
+ // single-file build reports `process.argv[1]` as a path inside its own
836
+ // virtual filesystem (`/$bunfs/root/index.js` for `bun build --compile`,
837
+ // `/snapshot/...` for pkg / node SEA). Passing that made the child parse
838
+ // it as the subcommand and die with `unknown command '/$bunfs/...'`, so
839
+ // every tier-0 sandbox failed with a blank "sandbox channel closed".
840
+ // An existence check is NOT enough to detect this: those runtimes mount
841
+ // the virtual path, so `fs.existsSync` cheerfully returns true for it.
842
+ ...(0, support_1.sandboxScriptArg)(),
843
+ 'sandbox', '--agent', activePackDir, '--stdio',
844
+ '--state-dir', storagePath, '--workspace', options.workspace,
845
+ ],
846
+ // PID/user-namespace wrap where the host allows it (residual P4: on a
847
+ // single-uid host, a concurrent tenant reads a turn's nonce from
848
+ // /proc/<pid>/environ — and the claude CLI's argv from /proc/<pid>/cmdline,
849
+ // which is world-readable). Default 'auto': hardened where possible, honest
850
+ // warning where not; 'require' makes multi-tenant hosts fail closed.
851
+ namespaceIsolation: (0, processDriver_1.namespaceIsolationFromEnv)(),
852
+ logger: {
853
+ info: (m) => logger.info(m),
854
+ warn: (m) => console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${m}`),
855
+ },
856
+ }),
857
+ image: options.sandboxImage ?? '',
858
+ tier: options.sandbox === 'container' ? 2 : 0,
859
+ ...(sandboxMounts ? { fs: sandboxMounts, fsFor: sandboxFsFor, stateDirFor: sandboxStateDirFor } : {}),
860
+ // The only routes a sandbox may reach. Everything else is unroutable, not merely denied.
861
+ routes: [
862
+ ...(process.env.AGENTHIPPO_DATA_MCP_URL ? [{ kind: 'data', target: process.env.AGENTHIPPO_DATA_MCP_URL }] : []),
863
+ ...(apiConfig.baseUrl ? [{ kind: 'inference', target: apiConfig.baseUrl }] : []),
864
+ ],
865
+ limits: { ttlSeconds: options.sandboxTtlSeconds ?? 30 * 60 },
866
+ provenanceEnv: () => (0, runtime_1.buildProvenanceEnv)({
867
+ policyVersion: lastPolicyVersion,
868
+ packDigest: packDigestValue,
869
+ engineVersion: (0, deps_1.installedEngineVersion)(defaultEngine),
870
+ tier: options.sandbox === 'container' ? 2 : options.sandbox ? 0 : undefined,
871
+ instanceId,
872
+ }),
873
+ // Kill switch + rollback, checked before a sandbox can exist. Revoking the lease stops
874
+ // new work fleet-wide immediately; TTL bounds the sandboxes already running.
875
+ admit: async (sessionKey) => {
876
+ const currentLease = await leaseSource.get();
877
+ assertPackAdmitted(currentLease, sessionKey);
878
+ },
879
+ inferenceBaseUrl: apiConfig.baseUrl,
880
+ // Per-sandbox inference warrant — signed, audience-bound, and naming the sandbox it was
881
+ // minted for. The provider key never enters a sandbox; the proxy verifies with a public
882
+ // key and can never mint, so a sandbox cannot re-issue itself more budget.
883
+ //
884
+ // `user` is the verified end user from the turn's credential scope, not the pack: the
885
+ // gateway strips caller-supplied x-litellm-metadata-user-id and re-stamps it from this
886
+ // claim, so the warrant is the only attribution channel that survives it — stamping
887
+ // packId here made every downstream user_id (LiteLLM, spend rows, evidence provenance)
888
+ // the pack. Every mint runs inside runWithCredential (create-time on first-turn
889
+ // acquire, then per-turn re-mints in RemoteEngine.stream), so the ambient credential is
890
+ // the turn's own. An unidentified turn gets a session-scoped marker rather than
891
+ // silently borrowing the pack's name — sessionKey is already user-namespaced, so the
892
+ // marker cannot collide across identified users.
893
+ mintModelToken: (sandboxId, sessionKey) => (0, support_1.mintSandboxInferenceWarrant)(warrantKey, {
894
+ user: (0, credential_context_1.currentCredential)()?.userId?.trim() || `anon:${sessionKey}`,
895
+ pack: packId,
896
+ packVersion: activeVersion,
897
+ sandboxId,
898
+ tier: options.sandbox === 'container' ? 2 : 0,
899
+ // A sandbox outlives one turn, so its model warrant gets the full cap rather than a
900
+ // turn-length TTL; the spend cap, not the clock, is what bounds it.
901
+ ttlSeconds: 300,
902
+ }),
903
+ logger,
904
+ runtime: { logger, editTracker: undefined, configProvider: undefined, permissionHandler },
905
+ })
906
+ : undefined;
907
+ sandboxRunnerRef = sandboxRunner ?? undefined;
908
+ // Caps + kill switch. Enforcement is non-cooperative — it revokes a route or destroys the
909
+ // sandbox — so it exists only where there ARE sandboxes. Without --sandbox we say so rather than
910
+ // pretending a cap is in force.
911
+ // 0 / NaN mean "unlimited" for every one of these — `--max-turns-per-session` has documented
912
+ // that meaning since before caps existed, and passing its default 0 through as a real cap
913
+ // refused the very first turn (`turn cap reached (1/0)`, observed live).
914
+ const positive = (n) => typeof n === 'number' && Number.isFinite(n) && n > 0 ? n : undefined;
915
+ const localCaps = {
916
+ ...(0, caps_1.capsFromEnv)(),
917
+ ...(positive(options.maxTurnsPerSession) !== undefined ? { maxTurnsPerSession: options.maxTurnsPerSession } : {}),
918
+ ...(positive(options.maxSessionSeconds) !== undefined ? { maxSessionSeconds: options.maxSessionSeconds } : {}),
919
+ ...(positive(options.maxConcurrentSandboxes) !== undefined ? { maxConcurrentSandboxes: options.maxConcurrentSandboxes } : {}),
920
+ ...(options.capOnBreach ? { onBreach: options.capOnBreach } : {}),
921
+ };
922
+ const capsRequested = localCaps.maxTurnsPerSession !== undefined
923
+ || localCaps.maxSessionSeconds !== undefined
924
+ || localCaps.maxConcurrentSandboxes !== undefined;
925
+ // A deployment must be governable whether or not it runs sandboxes. Gating the governor on
926
+ // `sandboxRunner` meant a plain `serve` never polled its lease at all: invisible to the control
927
+ // plane AND immune to the kill switch, which is the opposite of what an operator configuring
928
+ // a lease URL is asking for. Without sandboxes the enforcement that needs one degrades (no route
929
+ // revocation, no concurrency bound) but the substance survives — the turn is still refused.
930
+ const nullSandboxes = {
931
+ sandboxes: () => [],
932
+ destroySession: async () => undefined,
933
+ revokeRoute: async () => undefined,
934
+ // There is nothing here that can stop a turn already executing in this process, so a
935
+ // `kill` deny rule degrades to `drain`. The governor now says so loudly instead of
936
+ // sweeping an empty list and reporting the rule as applied.
937
+ supportsInFlightTermination: false,
938
+ };
939
+ const governed = Boolean(sandboxRunner || process.env.AGENTHIPPO_LEASE_URL || capsRequested);
940
+ const governor = governed
941
+ ? (0, governor_1.createGovernor)({
942
+ // The lease's caps bind; local flags may tighten them and never loosen them.
943
+ caps: (0, caps_1.mergeCaps)((await leaseSource.get())?.caps, localCaps),
944
+ sandboxes: sandboxRunner ?? nullSandboxes,
945
+ getLease: () => leaseSource.get(),
946
+ pollSeconds: leasePollSeconds,
947
+ // Scope for deny rules: the pack this instance runs, and the same hash that namespaces
948
+ // session keys — so the console can name a user by email without serve holding a roster.
949
+ pack: packId,
950
+ userHash: serve_hardening_1.userNamespaceHash,
951
+ // Only when there is no sandbox: with one, the same checks gate a sandbox's BIRTH and
952
+ // running sandboxes are meant to drain on TTL. Without one, this is the only gate that
953
+ // exists, so an unsigned pack or an unpinned version would otherwise keep serving.
954
+ ...(sandboxRunner ? {} : {
955
+ admitPack: (sessionKey, lease) => {
956
+ try {
957
+ // No userId here on purpose: the governor has ALREADY evaluated the deny rules for
958
+ // this turn (with its followed-user map, which this call site cannot see), so the
959
+ // only thing left for this gate is the signature/pin/digest half.
960
+ assertPackAdmitted(lease, sessionKey);
961
+ return { allowed: true };
962
+ }
963
+ catch (err) {
964
+ return { allowed: false, reason: err instanceof Error ? err.message : String(err) };
965
+ }
966
+ },
967
+ }),
968
+ logger,
969
+ audit: (event, fields) => auditEvent(String(fields.sessionKey ?? 'instance'), event, fields),
970
+ })
971
+ : undefined;
972
+ if (capsRequested && !sandboxRunner) {
973
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} caps are configured but --sandbox is not set. Turn and duration caps still refuse the turn; the concurrency cap and route revocation need sandboxes, so those are IGNORED.`);
974
+ }
975
+ const baseRunner = sandboxRunner ?? inProcessRunner;
976
+ const runner = governor ? (0, governor_1.withGovernor)(baseRunner, governor) : baseRunner;
977
+ if (governor) {
978
+ governor.start();
979
+ const p = governor.policy();
980
+ // The banner prints the number an operator will be held to: worst-case kill latency is
981
+ // poll + cache, not the poll alone. It used to print "lease poll 30s" while the cache
982
+ // defaulted to 60, so a kill could take three times as long as advertised.
983
+ const killLatency = (0, lease_1.effectiveKillLatencySeconds)(leasePollSeconds);
984
+ logger.info(`[Serve] control plane: caps turns=${p.maxTurnsPerSession ?? '∞'} session=${p.maxSessionSeconds ?? '∞'}s sandboxes=${p.maxConcurrentSandboxes ?? '∞'} onBreach=${p.onBreach ?? 'revoke-inference'}; lease poll ${leasePollSeconds}s (worst-case kill latency ${killLatency}s; an admin refresh is immediate)`);
985
+ if (!sandboxRunner) {
986
+ logger.info('[Serve] no sandbox driver: a deny rule with mode=kill takes effect as DRAIN — the in-flight turn finishes and only the next one is refused.');
987
+ }
988
+ const leaseStatus = leaseSource.status();
989
+ if (process.env.AGENTHIPPO_LEASE_URL && !leaseStatus.requireSignedLease) {
990
+ logger.info('[Serve] lease signature is NOT required — set --require-signed-lease (or lease.requireSignedLease) once the control plane signs leases.');
991
+ }
992
+ }
993
+ if (sandboxRunner) {
994
+ logger.info(`[Serve] sandbox mode: ${options.sandbox} (tier ${options.sandbox === 'container' ? 2 : 0}) — engines run in per-session sandboxes`);
995
+ // A sandbox's ONLY model credential is the per-sandbox warrant, which is meaningful only to an
996
+ // inference proxy. With no proxy configured there is nothing to send it to, so the engine
997
+ // dials the provider bare and the first turn dies with an opaque 401/reconnect loop. Say so
998
+ // at boot — same reason the engine credential check above is a boot check, not a turn check.
999
+ if (!apiConfig.baseUrl) {
1000
+ console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} --sandbox without an inference proxy: a sandbox holds no provider key, so model calls will fail 401. Set AGENTHIPPO_LITELLM_BASE_URL.`);
1001
+ }
1002
+ }
264
1003
  /** Set when `--gateway-bridge` is enabled; cleared on shutdown. */
265
1004
  let gatewayBridge = null;
266
1005
  /** True once the pack is synced and (if bridged) the gateway is connected. Drives GET /ready. */
267
1006
  let ready = false;
268
1007
  // Create HTTP server
1008
+ let evidenceShipHandle;
269
1009
  const server = http.createServer(async (req, res) => {
270
1010
  const cors = (0, serve_hardening_1.corsHeaders)();
271
1011
  for (const [k, v] of Object.entries(cors)) {
@@ -279,8 +1019,63 @@ async function serveCommand(options) {
279
1019
  try {
280
1020
  // Liveness — always open.
281
1021
  if (req.method === 'GET' && req.url === '/health') {
1022
+ const health = { status: 'ok' };
1023
+ // Reported unconditionally: with only `last_ok_at`, "shipping is off" and "shipping
1024
+ // is on but has never once succeeded" look identical to an operator and to alerting.
1025
+ health.evidence_ship_enabled = Boolean(evidenceShipHandle);
1026
+ const lastOk = evidenceShipHandle?.lastOkAt();
1027
+ if (lastOk !== undefined) {
1028
+ health.evidence_ship_last_ok_at = new Date(lastOk).toISOString();
1029
+ }
1030
+ // R-13 gap detection: evidence write failures and permanently-skipped files are
1031
+ // COUNTED now rather than swallowed, but a counter nobody can read is the same as no
1032
+ // counter. `disabledReason` matters most — "shipping is off because
1033
+ // AGENTHIPPO_EVIDENCE_SHIP=0" and "shipping is on and failing" produce the same
1034
+ // `enabled: false` shaped alert otherwise.
1035
+ const ship = (0, sessionShip_1.getEvidenceShipStatus)();
1036
+ health.evidence_ship_disabled_reason = ship.disabledReason ?? null;
1037
+ health.evidence_ship_consecutive_failures = ship.consecutiveFailures;
1038
+ health.evidence_records_staged = ship.recordsStaged;
1039
+ health.evidence_records_shipped = ship.recordsShipped;
1040
+ // Non-zero means the record is known to be INCOMPLETE, not merely late.
1041
+ health.evidence_records_skipped_too_old = ship.recordsSkippedTooOld;
1042
+ health.evidence_files_skipped_too_old = ship.filesSkippedTooOld;
1043
+ health.evidence_content_capture = ship.contentCapture;
1044
+ if (ship.lastHeartbeatAt !== undefined) {
1045
+ health.evidence_ship_last_heartbeat_at = new Date(ship.lastHeartbeatAt).toISOString();
1046
+ }
1047
+ const writes = (0, sessionEvents_1.getSessionEventWriterStats)();
1048
+ health.evidence_writes_ok = writes.written;
1049
+ health.evidence_writes_failed = writes.failed;
1050
+ if (writes.lastError) {
1051
+ health.evidence_write_last_error = {
1052
+ at: new Date(writes.lastError.at).toISOString(),
1053
+ event_type: writes.lastError.eventType,
1054
+ message: writes.lastError.message,
1055
+ };
1056
+ }
1057
+ // Governance surface: an operator asking "is this instance actually governed?" gets
1058
+ // the answer here rather than from boot logs that have long since scrolled away.
1059
+ const leaseState = leaseSource.status();
1060
+ health.lease_configured = Boolean(process.env.AGENTHIPPO_LEASE_URL);
1061
+ health.lease_signed = leaseState.signed;
1062
+ health.lease_signature_required = leaseState.requireSignedLease;
1063
+ health.lease_staleness_seconds = leaseState.stalenessSeconds ?? null;
1064
+ health.lease_fail_closed = leaseState.failClosed;
1065
+ health.pack_signature_ok = packSignature.ok;
1066
+ health.pack_signature_required = (0, signature_1.resolveRequireSignedPack)({
1067
+ localRequire: options.requireSignedPack, localAllowUnsigned: allowUnsignedPack,
1068
+ });
1069
+ health.pack_trust_root = packTrust.status;
1070
+ // Reported separately from the pack root: "signed leases are required and the lease
1071
+ // keyring is unavailable" is an outage in the making, and it is invisible in
1072
+ // `pack_trust_root`. `lease_trust_root_source` distinguishes an operator-named root
1073
+ // from the pack-keyring fallback, which is the state that stops working on rotation.
1074
+ health.lease_trust_root = leaseTrust.status;
1075
+ health.lease_trust_root_source = leaseTrust.explicitlyConfigured ? 'lease' : 'pack-fallback';
1076
+ health.kill_in_flight_supported = Boolean(sandboxRunner);
282
1077
  res.writeHead(200, { 'Content-Type': 'application/json' });
283
- res.end(JSON.stringify({ status: 'ok' }));
1078
+ res.end(JSON.stringify(health));
284
1079
  return;
285
1080
  }
286
1081
  // Readiness — pack synced (+ gateway connected when bridged).
@@ -346,7 +1141,7 @@ async function serveCommand(options) {
346
1141
  }
347
1142
  await prepareSession(sessionKey);
348
1143
  if (userId?.trim()) {
349
- turnReg = registerServeTurn({ userId: userId.trim(), sessionKey, inboundToken: dataToken });
1144
+ turnReg = await registerServeTurn({ userId: userId.trim(), sessionKey, inboundToken: dataToken, edgeNonce: edgeTurnNonce(req.headers) });
350
1145
  }
351
1146
  const result = await (0, credential_context_1.runWithCredential)({ userId, dataToken }, () => handleChat(runner, body, sessionKey, defaultEngine, defaultModel, options.workspace, permissions, userId, turnReg?.env), { onDenied: logWarrantDenied(sessionKey) });
352
1147
  res.writeHead(200, { 'Content-Type': 'application/json' });
@@ -354,7 +1149,7 @@ async function serveCommand(options) {
354
1149
  statePusher?.schedule(sessionKey); // per-session delta push, off the response path
355
1150
  }
356
1151
  finally {
357
- turnReg?.release();
1152
+ await turnReg?.release();
358
1153
  release();
359
1154
  }
360
1155
  return;
@@ -401,7 +1196,7 @@ async function serveCommand(options) {
401
1196
  }
402
1197
  await prepareSession(sessionKey);
403
1198
  if (userId?.trim()) {
404
- turnReg = registerServeTurn({ userId: userId.trim(), sessionKey, inboundToken: dataToken });
1199
+ turnReg = await registerServeTurn({ userId: userId.trim(), sessionKey, inboundToken: dataToken, edgeNonce: edgeTurnNonce(req.headers) });
405
1200
  }
406
1201
  // Fail-closed credential check MUST run before SSE writeHead(200).
407
1202
  // Otherwise a warrant denial lands after headers are committed, the outer
@@ -418,7 +1213,7 @@ async function serveCommand(options) {
418
1213
  statePusher?.schedule(sessionKey); // per-session delta push, off the response path
419
1214
  }
420
1215
  finally {
421
- turnReg?.release();
1216
+ await turnReg?.release();
422
1217
  release();
423
1218
  }
424
1219
  return;
@@ -472,11 +1267,19 @@ async function serveCommand(options) {
472
1267
  console.log('');
473
1268
  console.log('Press Ctrl+C to stop.');
474
1269
  console.log('');
1270
+ const shipConfig = (0, sessionShip_1.resolveSessionShipConfig)(instanceId, analyticsSessionsDir, storagePath);
1271
+ if (shipConfig) {
1272
+ evidenceShipHandle = (0, sessionShip_1.startSessionShipper)({
1273
+ ...shipConfig,
1274
+ logger: { info: (m) => logger.info(m), warn: (m) => console.warn(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} ${m}`) },
1275
+ });
1276
+ }
475
1277
  // Bind channels before bridge connect so config.patch reload does not race registration.
476
1278
  const bindSpec = options.bind?.trim();
477
1279
  if (bindSpec) {
478
1280
  try {
479
- await bindToGatewayChannels(gatewayAgentId, bindSpec);
1281
+ // The captured token — the env var was stripped at boot (§10 hole 2).
1282
+ await bindToGatewayChannels(gatewayAgentId, bindSpec, brokerGatewayToken);
480
1283
  }
481
1284
  catch (err) {
482
1285
  console.error(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} Failed to bind channels: ${err instanceof Error ? err.message : String(err)}`);
@@ -500,13 +1303,15 @@ async function serveCommand(options) {
500
1303
  audit: auditEvent,
501
1304
  });
502
1305
  process.env[data_plane_1.EGRESS_URL_ENV] = egressBroker.url;
503
- // Now that the broker holds the token, strip it from the process env so no
504
- // engine child (all spread process.env) inherits it (§7.4 gap 1 close).
505
- delete process.env.OPENCLAW_GATEWAY_TOKEN;
1306
+ // The token itself was stripped from process.env at boot, broker or not.
506
1307
  console.log('OK: Egress broker on loopback — gateway token stays broker-side; agent sends via turn nonce');
507
1308
  }
508
1309
  catch (err) {
509
- console.error(`${utils_1.COLORS.yellow}Warning:${utils_1.COLORS.reset} Egress broker failed to start: ${err instanceof Error ? err.message : String(err)}`);
1310
+ // Fail CLOSED: without the broker, AGENTHIPPO_EGRESS_URL stays unset and the
1311
+ // runtime would bake the gateway token back into engine children (§7.6 undone
1312
+ // by a port-bind hiccup). Refuse to serve rather than degrade silently.
1313
+ console.error(`${utils_1.COLORS.red}Error:${utils_1.COLORS.reset} Egress broker failed to start: ${err instanceof Error ? err.message : String(err)}`);
1314
+ process.exit(1);
510
1315
  }
511
1316
  }
512
1317
  try {
@@ -580,6 +1385,17 @@ async function serveCommand(options) {
580
1385
  // best-effort
581
1386
  }
582
1387
  }
1388
+ // After the planes are closed, so the tail includes whatever they just wrote. SIGTERM is
1389
+ // how every redeploy ends, and without this each one drops up to one interval of
1390
+ // session evidence — permanently, when the state dir (and its cursor) is ephemeral.
1391
+ if (evidenceShipHandle) {
1392
+ try {
1393
+ await evidenceShipHandle.flush();
1394
+ }
1395
+ catch {
1396
+ // best-effort — never block shutdown on the evidence server being reachable
1397
+ }
1398
+ }
583
1399
  server.close(() => {
584
1400
  runner.dispose();
585
1401
  process.exit(0);
@@ -711,9 +1527,9 @@ async function startServeGatewayBridge(params) {
711
1527
  const { GatewayBridge: Bridge } = await import('../../gateway/bridge.js');
712
1528
  const DEFAULT_GATEWAY_PORT = 18789;
713
1529
  const gatewayUrl = process.env.OPENCLAW_GATEWAY_URL ?? `ws://127.0.0.1:${DEFAULT_GATEWAY_PORT}`;
714
- // Use the captured token — serve strips OPENCLAW_GATEWAY_TOKEN from process.env once the
715
- // egress broker holds it, so the bridge must not depend on the env var (§7.6).
716
- const token = params.brokerGatewayToken ?? process.env.OPENCLAW_GATEWAY_TOKEN;
1530
+ // The captured token — OPENCLAW_GATEWAY_TOKEN is stripped from process.env at boot (§7.6,
1531
+ // §10 hole 2), so the bridge must never depend on the env var.
1532
+ const token = params.brokerGatewayToken;
717
1533
  const bridge = new Bridge({ gatewayUrl, token });
718
1534
  await bridge.connect();
719
1535
  await bridge.registerAgent({
@@ -759,7 +1575,7 @@ async function startServeGatewayBridge(params) {
759
1575
  // destinations server-side (§7.6). userId gates BOTH capabilities: an
760
1576
  // unidentified peer gets neither a nonce nor an egress route.
761
1577
  const turnReg = userId?.trim()
762
- ? params.registerTurn({ userId: userId.trim(), sessionKey, egressStoreKey: outboundKey })
1578
+ ? await params.registerTurn({ userId: userId.trim(), sessionKey, egressStoreKey: outboundKey })
763
1579
  : null;
764
1580
  try {
765
1581
  const result = await (0, credential_context_1.runWithCredential)({ userId }, () => params.runner.run({
@@ -785,7 +1601,7 @@ async function startServeGatewayBridge(params) {
785
1601
  return { success: result.success, error: result.error };
786
1602
  }
787
1603
  finally {
788
- turnReg?.release();
1604
+ await turnReg?.release();
789
1605
  }
790
1606
  },
791
1607
  });
@@ -800,11 +1616,10 @@ async function startServeGatewayBridge(params) {
800
1616
  * Connect to the gateway and set channel bindings for this agent.
801
1617
  * Called when --bind is specified (e.g., --bind "telegram,whatsapp" or --bind "*").
802
1618
  */
803
- async function bindToGatewayChannels(agentId, bindSpec) {
1619
+ async function bindToGatewayChannels(agentId, bindSpec, token) {
804
1620
  const { createAgentIDEGatewayClient } = await import('../../gateway/client.js');
805
1621
  const DEFAULT_GATEWAY_PORT = 18789;
806
1622
  const gatewayUrl = process.env.OPENCLAW_GATEWAY_URL ?? `ws://127.0.0.1:${DEFAULT_GATEWAY_PORT}`;
807
- const token = process.env.OPENCLAW_GATEWAY_TOKEN;
808
1623
  const { client, clientInfo } = createAgentIDEGatewayClient(gatewayUrl, token);
809
1624
  try {
810
1625
  await client.connect(clientInfo);
@@ -901,7 +1716,7 @@ async function handleStreamingChat(runner, body, sessionKey, defaultEngine, defa
901
1716
  },
902
1717
  };
903
1718
  try {
904
- await runner.run({
1719
+ const result = await runner.run({
905
1720
  engineId: resolveRequestedEngineId(body.engine, defaultEngine),
906
1721
  modelId: body.model || defaultModel,
907
1722
  message: body.message,
@@ -913,6 +1728,14 @@ async function handleStreamingChat(runner, body, sessionKey, defaultEngine, defa
913
1728
  permissions,
914
1729
  turnEnv,
915
1730
  });
1731
+ // A policy refusal is a RESULT, not an exception: `withGovernor` returns
1732
+ // `{success:false, error}` without ever touching the emitter. Ignoring it sent the client
1733
+ // HTTP 200, zero events, then `done` — a kill switch, a cap breach or a deny rule looked
1734
+ // exactly like the agent choosing to say nothing, and the operator's reason reached nobody.
1735
+ // /v1/chat already returns the reason in its body; the two endpoints must not disagree.
1736
+ if (!result.success && !streamEnded) {
1737
+ sendEvent('error', { message: result.error ?? 'refused by policy' });
1738
+ }
916
1739
  if (!streamEnded) {
917
1740
  sendEvent('done', {});
918
1741
  endStream();