@bitkyc08/opencodex 2.95.0 → 2.97.0

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 (276) hide show
  1. package/bin/ocx.mjs +214 -19
  2. package/gui/dist/assets/{App-8yedBkhm.js → App-DuNhggE1.js} +18 -18
  3. package/gui/dist/assets/{Tray-erSMzxPG.js → Tray-CEw0y0jR.js} +1 -1
  4. package/gui/dist/assets/index-CynE_Rue.js +94 -0
  5. package/gui/dist/assets/{quota-summary-BK4VuQNc.js → quota-summary-VHFSlUuM.js} +1 -1
  6. package/gui/dist/index.html +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/command-code.ts +15 -16
  9. package/src/adapters/cursor/transport-retry.ts +2 -2
  10. package/src/adapters/devin/cloud-direct/chat.ts +15 -6
  11. package/src/adapters/kiro/stream.ts +63 -4
  12. package/src/adapters/kiro-constants.ts +15 -1
  13. package/src/adapters/kiro-events.ts +31 -1
  14. package/src/adapters/kiro-retry.ts +3 -3
  15. package/src/adapters/ollama-native.ts +21 -7
  16. package/src/adapters/openai-responses/canonical-forward.ts +50 -13
  17. package/src/adapters/openai-responses/empty-assistant.ts +15 -3
  18. package/src/adapters/openai-responses/passthrough.ts +8 -8
  19. package/src/adapters/openai-responses/reasoning.ts +9 -4
  20. package/src/adapters/openai-responses/request-strips.ts +1 -1
  21. package/src/adapters/openai-responses/transport.ts +17 -26
  22. package/src/adapters/physical-send.ts +3 -3
  23. package/src/adapters/qoder/adapter.ts +1 -1
  24. package/src/adapters/qoder/image-input.ts +5 -2
  25. package/src/adapters/zed-openai-stream.ts +2 -1
  26. package/src/bridge/errors.ts +11 -2
  27. package/src/bridge/response-json.ts +2 -1
  28. package/src/bridge/sse.ts +14 -6
  29. package/src/chat/failure-verdict.ts +21 -0
  30. package/src/chat/outbound.ts +139 -21
  31. package/src/claude/outbound.ts +49 -2
  32. package/src/cli/agent.ts +44 -15
  33. package/src/cli/capabilities-agents-routing.ts +9 -0
  34. package/src/cli/capabilities-base.ts +25 -3
  35. package/src/cli/capabilities-command.ts +12 -3
  36. package/src/cli/capability-contracts.ts +2 -0
  37. package/src/cli/codex-launch-auth.ts +30 -1
  38. package/src/cli/codex-launcher.ts +12 -5
  39. package/src/cli/codex-shim-autorestore.ts +5 -4
  40. package/src/cli/dispatch.ts +11 -176
  41. package/src/cli/ensure-desired-integrations.ts +4 -3
  42. package/src/cli/help.ts +2 -2
  43. package/src/cli/index.ts +9 -2
  44. package/src/cli/link.ts +1 -1
  45. package/src/cli/message-command.ts +2 -1
  46. package/src/cli/message-mcp-command.ts +29 -0
  47. package/src/cli/registry.ts +12 -4
  48. package/src/cli/root.ts +8 -0
  49. package/src/cli/sync-command.ts +243 -0
  50. package/src/cli/sync-fanout-process.ts +92 -0
  51. package/src/cli/sync-fanout.ts +150 -0
  52. package/src/cli/sync-flags.ts +10 -0
  53. package/src/cli/sync-link-status.ts +39 -0
  54. package/src/cli/sync-update-guard.ts +18 -0
  55. package/src/client/connect.ts +31 -14
  56. package/src/client/link-home.ts +3 -2
  57. package/src/client/machine-listener.ts +3 -0
  58. package/src/client/restore-ownership.ts +19 -1
  59. package/src/client/runtime-restart.ts +12 -4
  60. package/src/client/runtime.ts +40 -11
  61. package/src/client/stop-routing.ts +62 -0
  62. package/src/client/sync-runtime.ts +65 -0
  63. package/src/clients/config-export.ts +32 -3
  64. package/src/codex/account-store.ts +1 -0
  65. package/src/codex/auth-context-provider.ts +15 -0
  66. package/src/codex/auth-context.ts +59 -122
  67. package/src/codex/catalog/antigravity-discovery.ts +16 -5
  68. package/src/codex/catalog/provider-models.ts +6 -8
  69. package/src/codex/forward-transport-headers.ts +65 -36
  70. package/src/codex/inject/config-toml.ts +88 -0
  71. package/src/codex/inject/multi-agent-v2.ts +30 -4
  72. package/src/codex/inject/plan.ts +34 -7
  73. package/src/codex/inject-coordination.ts +11 -0
  74. package/src/codex/inject.ts +18 -7
  75. package/src/codex/installation-id.ts +72 -0
  76. package/src/codex/lineage.ts +13 -7
  77. package/src/codex/quota.ts +22 -6
  78. package/src/codex/request-owned-affinity-key.ts +11 -0
  79. package/src/codex/request-owned-affinity.ts +118 -0
  80. package/src/codex/request-owned-main-pin.ts +86 -0
  81. package/src/codex/role-capability.ts +52 -41
  82. package/src/codex/routing/active-account.ts +13 -5
  83. package/src/codex/routing/selection.ts +40 -1
  84. package/src/codex/routing/thread-affinity-store.ts +916 -0
  85. package/src/codex/routing/thread-affinity.ts +90 -12
  86. package/src/codex/toml-source-lines.ts +72 -13
  87. package/src/combos/failover.ts +6 -109
  88. package/src/combos/request.ts +2 -2
  89. package/src/combos/resolve.ts +29 -11
  90. package/src/config/process-state.ts +5 -0
  91. package/src/config/schema/leaf-validators.ts +1 -0
  92. package/src/generated/compatibility-version.json +496 -192
  93. package/src/grok/sync.ts +10 -1
  94. package/src/images/loop.ts +2 -1
  95. package/src/lib/client-error-body.ts +184 -0
  96. package/src/lib/client-retry-after.ts +66 -0
  97. package/src/lib/config-ownership.ts +4 -0
  98. package/src/lib/context-overflow-verdict.ts +113 -0
  99. package/src/lib/desktop-cli-handoff.d.mts +1 -1
  100. package/src/lib/desktop-cli-handoff.mjs +11 -1
  101. package/src/lib/empty-tool-catalog.ts +25 -3
  102. package/src/lib/error-destination.ts +47 -0
  103. package/src/lib/failure-detail.ts +66 -0
  104. package/src/lib/model-identity.ts +44 -0
  105. package/src/lib/private-sqlite-path.ts +124 -0
  106. package/src/lib/response-service-tier.ts +17 -0
  107. package/src/lib/retry-after.ts +3 -0
  108. package/src/lib/spend-ledger-owner.ts +43 -4
  109. package/src/lib/upstream-condition.ts +142 -0
  110. package/src/lib/upstream-retry.ts +14 -11
  111. package/src/lib/windows-owner-acl.ts +13 -6
  112. package/src/lib/windows-secret-acl.ts +2 -2
  113. package/src/link/remote-platform.ts +14 -0
  114. package/src/link/remote-windows.ts +51 -9
  115. package/src/link/ssh-argv.ts +63 -10
  116. package/src/messaging/mcp-queue-rpc.ts +102 -0
  117. package/src/messaging/mcp-queue-server.ts +42 -0
  118. package/src/messaging/mcp-queue-tools.ts +39 -0
  119. package/src/oauth/anthropic-identity.ts +2 -6
  120. package/src/oauth/generic-account-failover.ts +13 -11
  121. package/src/oauth/index.ts +41 -74
  122. package/src/oauth/oauth-errors.ts +67 -0
  123. package/src/oauth/store.ts +1 -1
  124. package/src/protocols/codecs/ollama.ts +197 -0
  125. package/src/protocols/encoders/adapter-events.ts +4 -0
  126. package/src/protocols/encoders/chat.ts +18 -10
  127. package/src/protocols/trace.ts +18 -0
  128. package/src/providers/anthropic-subscription.ts +25 -0
  129. package/src/providers/claude-cli-usage.ts +15 -67
  130. package/src/providers/fastwire.ts +3 -2
  131. package/src/providers/quota/anthropic-decision-windows.ts +5 -19
  132. package/src/providers/quota/anthropic-usage-windows.ts +65 -0
  133. package/src/providers/quota/vendor-probes-key.ts +60 -24
  134. package/src/providers/request-pacing.ts +3 -2
  135. package/src/responses/apply-patch-envelope.ts +45 -19
  136. package/src/responses/compaction-history-framing.ts +5 -1
  137. package/src/responses/freeform-wrapper-scan.ts +5 -3
  138. package/src/responses/parser-tools.ts +1 -0
  139. package/src/responses/progressive-freeform-input.ts +354 -45
  140. package/src/responses/schema.ts +5 -2
  141. package/src/routing/fallback-policy.ts +3 -1
  142. package/src/server/admission-model-scope.ts +17 -6
  143. package/src/server/admitted-http-turn.ts +56 -0
  144. package/src/server/audio-dictation.ts +3 -0
  145. package/src/server/audio-live.ts +8 -2
  146. package/src/server/audio-upstream.ts +2 -0
  147. package/src/server/auth-cors.ts +5 -0
  148. package/src/server/chat-completions.ts +45 -20
  149. package/src/server/chat-native-sse.ts +52 -7
  150. package/src/server/chat-native.ts +60 -35
  151. package/src/server/claude-bridge-pool-session.ts +59 -0
  152. package/src/server/claude-messages.ts +57 -27
  153. package/src/server/context-history.ts +14 -7
  154. package/src/server/images.ts +14 -5
  155. package/src/server/inbound-body-admission.ts +11 -4
  156. package/src/server/index/live-sideband.ts +16 -2
  157. package/src/server/index/ollama-routes.ts +89 -0
  158. package/src/server/index/serve-options.ts +22 -4
  159. package/src/server/index/websocket-handler.ts +5 -3
  160. package/src/server/index.ts +6 -36
  161. package/src/server/inference/client-encoder-delivery.ts +4 -2
  162. package/src/server/json-serialized-bytes.ts +44 -0
  163. package/src/server/live.ts +30 -5
  164. package/src/server/management/agent-settings-routes.ts +24 -21
  165. package/src/server/management/codex-cli-update-routes.ts +2 -1
  166. package/src/server/management/route-registry.ts +1 -1
  167. package/src/server/messages-native-scope.ts +2 -1
  168. package/src/server/messages-native-tool-guard.ts +1 -1
  169. package/src/server/messages-native.ts +12 -7
  170. package/src/server/messages-shared.ts +6 -2
  171. package/src/server/native-final-log.ts +93 -0
  172. package/src/server/ollama-inbound.ts +349 -0
  173. package/src/server/ollama-native-paths.ts +21 -0
  174. package/src/server/ollama-route-policy.ts +73 -0
  175. package/src/server/relay.ts +22 -3
  176. package/src/server/request-decompress.ts +4 -2
  177. package/src/server/request-log-failure-attribution.ts +22 -0
  178. package/src/server/request-log-failure-detail.ts +80 -0
  179. package/src/server/request-log-terminal-status.ts +4 -2
  180. package/src/server/request-log.ts +79 -42
  181. package/src/server/response-log-body.ts +6 -1
  182. package/src/server/responses/account-change-state.ts +17 -6
  183. package/src/server/responses/adapter-continuation.ts +9 -4
  184. package/src/server/responses/adapter-delivery.ts +5 -4
  185. package/src/server/responses/adapter-dispatch.ts +36 -30
  186. package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
  187. package/src/server/responses/codex-auth-error.ts +22 -17
  188. package/src/server/responses/codex-ws-exchange.ts +11 -8
  189. package/src/server/responses/codex-ws-metadata.ts +94 -21
  190. package/src/server/responses/codex-ws-request.ts +3 -1
  191. package/src/server/responses/combo-required-tool-contract.ts +24 -12
  192. package/src/server/responses/compact.ts +89 -41
  193. package/src/server/responses/compaction-recovery.ts +4 -2
  194. package/src/server/responses/compaction-routing.ts +9 -4
  195. package/src/server/responses/core-auth.ts +32 -14
  196. package/src/server/responses/core-codex-account.ts +34 -12
  197. package/src/server/responses/core-combo-failure.ts +5 -1
  198. package/src/server/responses/core-combo-native.ts +2 -1
  199. package/src/server/responses/core-combo.ts +51 -33
  200. package/src/server/responses/core-errors.ts +22 -20
  201. package/src/server/responses/core-lifetime.ts +6 -3
  202. package/src/server/responses/core-normalize.ts +8 -1
  203. package/src/server/responses/core-options.ts +7 -0
  204. package/src/server/responses/core-pipeline.ts +4 -3
  205. package/src/server/responses/fetch-helpers.ts +7 -1
  206. package/src/server/responses/input-admission.ts +28 -2
  207. package/src/server/responses/native-injection.ts +1 -1
  208. package/src/server/responses/native-response-json.ts +13 -4
  209. package/src/server/responses/native-response-output.ts +1 -2
  210. package/src/server/responses/non-replayable-error.ts +2 -1
  211. package/src/server/responses/pacing-overload.ts +3 -2
  212. package/src/server/responses/passthrough-build-error.ts +36 -0
  213. package/src/server/responses/passthrough-delivery.ts +55 -14
  214. package/src/server/responses/passthrough-dispatch.ts +26 -39
  215. package/src/server/responses/passthrough-error.ts +9 -9
  216. package/src/server/responses/policy-fallback.ts +2 -1
  217. package/src/server/responses/request-prepare.ts +67 -48
  218. package/src/server/responses/request-sidecar-auth.ts +3 -1
  219. package/src/server/responses/request-transport.ts +35 -31
  220. package/src/server/responses/run-turn-execution.ts +6 -6
  221. package/src/server/responses/sidecar-execution.ts +16 -12
  222. package/src/server/responses/spend-storage-error.ts +2 -1
  223. package/src/server/responses/ws-upstream.ts +9 -0
  224. package/src/server/responses-custom-tool-repair.ts +14 -3
  225. package/src/server/responses-terminal-repair.ts +5 -2
  226. package/src/server/responses-undeclared-tool-guard.ts +39 -16
  227. package/src/server/safe-response-headers.ts +162 -13
  228. package/src/server/search.ts +13 -4
  229. package/src/server/turn-state-handles.ts +164 -0
  230. package/src/server/workflow-refusal.ts +2 -1
  231. package/src/server/ws-bridge.ts +5 -2
  232. package/src/service/ownership-mutation-lease.d.mts +11 -2
  233. package/src/service/ownership-mutation-lease.mjs +53 -11
  234. package/src/types/config.ts +2 -0
  235. package/src/update/index.ts +133 -25
  236. package/src/update/npm-install.d.mts +8 -0
  237. package/src/update/npm-install.mjs +20 -1
  238. package/src/update/orchestrated-admission.d.mts +4 -0
  239. package/src/update/orchestrated-admission.mjs +12 -0
  240. package/src/update/orchestrated-arguments.d.mts +12 -0
  241. package/src/update/orchestrated-arguments.mjs +64 -0
  242. package/src/update/orchestrated-child.d.mts +12 -0
  243. package/src/update/orchestrated-child.mjs +70 -0
  244. package/src/update/orchestrated-entry.ts +234 -0
  245. package/src/update/orchestrated-identity-probe-entry.ts +20 -0
  246. package/src/update/orchestrated-identity.ts +53 -0
  247. package/src/update/orchestrated-live-identity.ts +48 -0
  248. package/src/update/orchestrated-node-driver.mjs +99 -0
  249. package/src/update/orchestrated-outcome.d.mts +21 -0
  250. package/src/update/orchestrated-outcome.mjs +40 -0
  251. package/src/update/orchestrated-plan.ts +49 -0
  252. package/src/update/orchestrated-pnpm.d.mts +8 -0
  253. package/src/update/orchestrated-pnpm.mjs +33 -0
  254. package/src/update/orchestrated-session.d.mts +37 -0
  255. package/src/update/orchestrated-session.mjs +264 -0
  256. package/src/update/orchestrated-token.d.mts +10 -0
  257. package/src/update/orchestrated-token.mjs +21 -0
  258. package/src/update/orchestrated-token.ts +3 -0
  259. package/src/update/orchestrated-tree.d.mts +1 -0
  260. package/src/update/orchestrated-tree.mjs +14 -0
  261. package/src/update/pnpm-global-install.d.mts +6 -0
  262. package/src/update/pnpm-global-install.mjs +9 -2
  263. package/src/update/posix-install-supervisor-entry.mjs +6 -0
  264. package/src/update/posix-install-supervisor.d.mts +10 -0
  265. package/src/update/posix-install-supervisor.mjs +88 -0
  266. package/src/update/sync-update-mutex.d.mts +10 -0
  267. package/src/update/sync-update-mutex.mjs +164 -0
  268. package/src/update/sync-update-mutex.ts +1 -0
  269. package/src/update/test-home-guard.mjs +33 -0
  270. package/src/update/transactional-install.mjs +2 -2
  271. package/src/usage/error-detail.ts +225 -0
  272. package/src/usage/log.ts +60 -27
  273. package/src/vision/describe.ts +4 -7
  274. package/src/web-search/executor.ts +4 -7
  275. package/src/web-search/loop.ts +31 -5
  276. package/gui/dist/assets/index-D7DCm5eQ.js +0 -94
@@ -1,3 +1,6 @@
1
+ import { formatClientRetryAfter } from "../lib/client-retry-after";
2
+ import { isolateCodexPoolClientMetadata, mergeCodexSelectedHeaders } from "../codex/forward-transport-headers";
3
+ import { CodexInstallationIdUnavailableError } from "../codex/installation-id";
1
4
  /** Native history/notes JSON relay. No interpretation of encrypted tool arguments or retries. */
2
5
  import { formatErrorResponse } from "../bridge";
3
6
  import {
@@ -109,7 +112,8 @@ async function relayContextHistory(
109
112
  "Context account ownership is unavailable; start a new session and preserve needed state before resetting context");
110
113
  }
111
114
  let authContext: Awaited<ReturnType<typeof resolveCodexAuthContext>>;
112
- const headers = new Headers(candidate.provider.headers);
115
+ let upstreamBody: unknown = body;
116
+ let headers = new Headers(candidate.provider.headers);
113
117
  try {
114
118
  authContext = await resolveCodexAuthContext(contextSelectionHeaders(req.headers, sessionId), config, owner.kind === "stored" ? "pool" : "direct", {
115
119
  // The same deadline bounds credential selection and refresh waits.
@@ -131,11 +135,10 @@ async function relayContextHistory(
131
135
  logCtx.provider = formatCodexProviderForLog(candidate.providerName, codexLogAccountId(authContext), config);
132
136
  // Materialization rechecks the current account policy after async selection.
133
137
  // Synthetic lane IDs are local selection metadata, never upstream headers.
134
- for (const [key, value] of materializeCodexUpstreamAuth(req.headers, authContext, {
138
+ headers = mergeCodexSelectedHeaders(candidate.provider.headers, materializeCodexUpstreamAuth(req.headers, authContext, {
135
139
  config, modelId: "context_history", admission, substituteMainCredential,
136
- })) {
137
- headers.set(key, value);
138
- }
140
+ }));
141
+ if (authContext.kind === "pool") upstreamBody = isolateCodexPoolClientMetadata(body, authContext.chatgptAccountId);
139
142
  // Check the assembled outbound headers, including configured provider headers.
140
143
  validateForwardAdmissionCredential(headers, config);
141
144
  // Recheck actual wire identity after async selection/materialization. A replaced
@@ -148,6 +151,7 @@ async function relayContextHistory(
148
151
  } catch (err) {
149
152
  const cancelled = cancellationResponse(req, deadline);
150
153
  if (cancelled) return cancelled;
154
+ if (err instanceof CodexInstallationIdUnavailableError) return formatErrorResponse(503, "server_busy", err.message);
151
155
  if (err instanceof CodexAccountCooldownError) return cooldownErrorResponse(err);
152
156
  if (err instanceof CodexMainProfileDrainingError) return codexMainProfileDrainingResponse();
153
157
  if (err instanceof CodexThreadAffinityExpiredError) return formatErrorResponse(409, "invalid_request_error", "Codex thread account affinity expired; start a new session");
@@ -176,13 +180,16 @@ async function relayContextHistory(
176
180
  let response: Response | undefined;
177
181
  try {
178
182
  response = await fetch(`${candidate.provider.baseUrl}/${endpoint}`, {
179
- method: "POST", headers, body: JSON.stringify(body), signal: deadline.signal, redirect: "manual",
183
+ method: "POST", headers, body: JSON.stringify(upstreamBody), signal: deadline.signal, redirect: "manual",
180
184
  });
181
185
  const result = await readBoundedResponseBytes(response, {maxBytes: MAX_RESPONSE_BYTES, signal: deadline.signal});
182
186
  if (result.oversized) return formatErrorResponse(502, "upstream_error", "Context response exceeded 16 MiB");
183
187
  const outputHeaders = new Headers();
184
188
  for (const key of RESPONSE_HEADERS) {
185
- const value = response.headers.get(key); if (value !== null) outputHeaders.set(key, value);
189
+ const raw = response.headers.get(key);
190
+ // Retry advice leaves as an integer this proxy formats, never the upstream bytes.
191
+ const value = key === "retry-after" ? formatClientRetryAfter(raw) ?? null : raw;
192
+ if (value !== null) outputHeaders.set(key, value);
186
193
  }
187
194
  // A context 403 is not evidence that the model credential is invalid. Don't mutate pool
188
195
  // health/quota or retry writes; preserve the real upstream result for the caller.
@@ -1,3 +1,5 @@
1
+ import { CodexInstallationIdUnavailableError } from "../codex/installation-id";
2
+ import { isolateCodexPoolClientMetadata, mergeCodexSelectedHeaders } from "../codex/forward-transport-headers";
1
3
  /**
2
4
  * /v1/images/{generations,edits} relay (issue #83).
3
5
  *
@@ -730,7 +732,9 @@ export async function handleImages(
730
732
  });
731
733
  if (forward) logCtx.provider = formatCodexProviderForLog(forward.providerName, codexLogAccountId(forward.authContext), config);
732
734
  } catch (err) {
733
- if (err instanceof CodexAccountCooldownError) {
735
+ if (err instanceof CodexInstallationIdUnavailableError) {
736
+ forwardAuthError = formatErrorResponse(503, "server_busy", err.message);
737
+ } else if (err instanceof CodexAccountCooldownError) {
734
738
  forwardAuthError = cooldownErrorResponse(err);
735
739
  } else if (err instanceof CodexMainProfileDrainingError) {
736
740
  forwardAuthError = codexMainProfileDrainingResponse();
@@ -752,7 +756,7 @@ export async function handleImages(
752
756
  }
753
757
  }
754
758
 
755
- const headers = new Headers({ "content-type": "application/json" });
759
+ let headers = new Headers({ "content-type": "application/json" });
756
760
  const releaseForwardProbe = (): void => {
757
761
  try { forward?.releaseProbeLease?.(); }
758
762
  catch { console.error("[images] Failed to release probe lease"); }
@@ -773,8 +777,7 @@ export async function handleImages(
773
777
  }
774
778
  const { provider } = forward;
775
779
  try {
776
- for (const [name, value] of Object.entries(provider.headers ?? {})) headers.set(name, value);
777
- for (const [name, value] of forward.headers) headers.set(name, value);
780
+ headers = mergeCodexSelectedHeaders({ "content-type": "application/json", ...provider.headers }, forward.headers);
778
781
  assertImagesUpstreamAuthorization(headers, config);
779
782
  } catch {
780
783
  releaseForwardProbe();
@@ -866,7 +869,9 @@ export async function handleImages(
866
869
  upstreamResponse = await fetch(url, {
867
870
  method: "POST",
868
871
  headers,
869
- body: JSON.stringify(body),
872
+ body: JSON.stringify(isolateCodexPoolClientMetadata(
873
+ body, forward?.authContext.kind === "pool" ? forward.authContext.chatgptAccountId : undefined,
874
+ )),
870
875
  signal: linkedSignal.signal,
871
876
  // Do not follow a cross-origin 3xx while carrying Codex credentials. Bun strips
872
877
  // `Authorization` across origins but forwards nonstandard headers, so
@@ -911,6 +916,10 @@ export async function handleImages(
911
916
  releaseForwardProbe();
912
917
  return formatErrorResponse(499, "client_closed_request", `image ${endpoint} request canceled by client`);
913
918
  }
919
+ if (err instanceof CodexInstallationIdUnavailableError) {
920
+ releaseForwardProbe();
921
+ return formatErrorResponse(503, "server_busy", err.message);
922
+ }
914
923
  if (linkedSignal.signal.aborted || (err instanceof Error && err.name === "TimeoutError")) {
915
924
  forward?.recordOutcome?.("timeout");
916
925
  // codex retries 5xx up to 4 more times; a retried 504 is acceptable for a transient hang.
@@ -7,6 +7,7 @@ import { codexCompatibleUrl } from "../codex/context-compat";
7
7
  import { addFinalRequestLog, markLocalRequestLogRefusal } from "./request-log";
8
8
  import type { WorkflowRefusalLog } from "./workflow-refusal";
9
9
  import { inboundBodyBudget, bindInboundBodyLease, type InboundBodyBudget } from "./inbound-body-budget";
10
+ import { OLLAMA_JSON_BODY_ROUTES } from "./ollama-native-paths";
10
11
 
11
12
  export { configureInboundBodyBudget, MAX_CONCURRENT_INBOUND_BODY_BYTES } from "./inbound-body-budget";
12
13
 
@@ -145,10 +146,12 @@ export async function withRaisedInboundBodyAdmission(
145
146
  configuredLimit: number | undefined,
146
147
  work: () => Promise<Response>,
147
148
  onRefusal: (response: Response) => Response = response => response,
148
- options?: { budget?: InboundBodyBudget; maxWaitMs?: number },
149
+ options?: { budget?: InboundBodyBudget; maxWaitMs?: number; onAdmissionError?: (error: unknown) => Response },
149
150
  ): Promise<Response> {
150
151
  const maxBytes = resolveInboundBodyLimitBytes(configuredLimit);
151
- if (req.method !== "POST" || !CONFIGURABLE_JSON_BODY_ROUTES.has(pathname)
152
+ // Native Ollama generation routes are served only on the authenticated listener, so they sit in
153
+ // their own set: the loopback oracle checks CONFIGURABLE_JSON_BODY_ROUTES against that allowlist.
154
+ if (req.method !== "POST" || !(CONFIGURABLE_JSON_BODY_ROUTES.has(pathname) || OLLAMA_JSON_BODY_ROUTES.has(pathname))
152
155
  || maxBytes <= MAX_DECOMPRESSED_BODY_BYTES || req.signal.aborted) {
153
156
  return work();
154
157
  }
@@ -164,6 +167,7 @@ export async function withRaisedInboundBodyAdmission(
164
167
  lease = await (options?.budget ?? inboundBodyBudget).acquire(maxBytes, req.signal, options?.maxWaitMs);
165
168
  } catch (error) {
166
169
  cancelUnreadBody(req, error);
170
+ if (options?.onAdmissionError) return options.onAdmissionError(error);
167
171
  throw error;
168
172
  }
169
173
  if (!lease) {
@@ -175,6 +179,7 @@ export async function withRaisedInboundBodyAdmission(
175
179
  if (req.signal.aborted) {
176
180
  lease.release();
177
181
  cancelUnreadBody(req, req.signal.reason);
182
+ if (options?.onAdmissionError) return options.onAdmissionError(req.signal.reason);
178
183
  throw req.signal.reason;
179
184
  }
180
185
  const unbind = bindInboundBodyLease(req, lease);
@@ -198,6 +203,8 @@ export function runAdmittedBodyWork(
198
203
  configuredLimit: number | undefined,
199
204
  work: () => Promise<Response>,
200
205
  refusalLog?: WorkflowRefusalLog,
206
+ mapRefusal?: (response: Response) => Response,
207
+ onAdmissionError?: (error: unknown) => Response,
201
208
  ): Promise<Response> {
202
209
  return withRaisedInboundBodyAdmission(req, codexCompatibleUrl(req.url).pathname, configuredLimit, work, refused => {
203
210
  if (refusalLog) {
@@ -208,6 +215,6 @@ export function runAdmittedBodyWork(
208
215
  });
209
216
  refusalLog.onLogged?.();
210
217
  }
211
- return withCors(refused, req, policy);
212
- });
218
+ return withCors(mapRefusal ? mapRefusal(refused) : refused, req, policy);
219
+ }, onAdmissionError ? { onAdmissionError } : undefined);
213
220
  }
@@ -19,6 +19,7 @@ import {
19
19
  resolveLiveSidebandUpgrade,
20
20
  } from "../live";
21
21
  import { RESPONSE_TTL_MS } from "../../responses/state";
22
+ import { isolateCodexPoolClientMetadata } from "../../codex/forward-transport-headers";
22
23
 
23
24
  export const MAX_WS_FRAME_BYTES = 50 * 1024 * 1024;
24
25
  /**
@@ -124,7 +125,20 @@ function releaseLiveSidebandAdmission(ws: ServerWebSocket<WsData>): void {
124
125
  * `Uint8Array.from` copies into a fresh `ArrayBuffer`-backed view, so a frame
125
126
  * arriving from `node:buffer` still round-trips byte-for-byte.
126
127
  */
127
- export function sendUpstreamFrame(upstream: WebSocket, frame: string | Buffer): void {
128
+ export function sendUpstreamFrame(upstream: WebSocket, frame: string | Buffer, context?: WsData["authContext"]): void {
129
+ if (context?.kind === "pool") {
130
+ let parsed: unknown;
131
+ try { parsed = JSON.parse(typeof frame === "string" ? frame : new TextDecoder("utf-8", { fatal: true }).decode(frame)); }
132
+ catch { /* Non-JSON frames retain their original bytes. */ }
133
+ const account = context.chatgptAccountId;
134
+ let isolated = isolateCodexPoolClientMetadata(parsed, account);
135
+ if (isolated && typeof isolated === "object" && !Array.isArray(isolated)) {
136
+ const record = isolated as Record<string, unknown>;
137
+ const session = isolateCodexPoolClientMetadata(record.session, account);
138
+ if (session !== record.session) isolated = { ...record, session };
139
+ }
140
+ if (isolated !== parsed) frame = typeof frame === "string" ? JSON.stringify(isolated) : Buffer.from(JSON.stringify(isolated));
141
+ }
128
142
  if (typeof frame === "string") {
129
143
  upstream.send(frame);
130
144
  return;
@@ -574,7 +588,7 @@ export function attachLiveSidebandUpstream(
574
588
  ws.data.livePendingBytes = undefined;
575
589
  for (const frame of pending) {
576
590
  try {
577
- sendUpstreamFrame(upstream, frame);
591
+ sendUpstreamFrame(upstream, frame, ws.data.authContext);
578
592
  } catch {
579
593
  closeLiveSideband(ws, 1011, "upstream send failed");
580
594
  return;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Dispatcher arm for the Ollama-compatible inbound API. It runs before the management /api/
3
+ * catch-all and only for the exact native paths; every other /api/ path is untouched. Admission
4
+ * reuses the data-plane gates: generation takes the same key, origin, drain, active-turn,
5
+ * workflow and body admission as /v1/chat/completions, and the read-only routes take the
6
+ * model-discovery admission /v1/models uses. Refusals keep their status and retry advice and are
7
+ * reshaped into the native { "error": string } body.
8
+ */
9
+ import type { Server } from "bun";
10
+ import type { OcxConfig } from "../../types";
11
+ import type { WsData } from "../ws-bridge";
12
+ import { admissionFields, isAllowedRequestOrigin, resolveApiAuth, resolveResponsesApiAuth, withCors, type RequestPolicyView } from "../auth-cors";
13
+ import { withCallerSessionIdentity } from "../caller-session-identity";
14
+ import { captureAdmissionPolicy } from "../capture-admission-policy";
15
+ import { isDraining, type ActiveTurnLease } from "../lifecycle";
16
+ import { handleOllamaGeneration, handleOllamaShow, handleOllamaTags, handleOllamaVersion, ollamaError, toOllamaRefusal } from "../ollama-inbound";
17
+ import { OLLAMA_NATIVE_ROUTES } from "../ollama-native-paths";
18
+ import { nextRequestLogId, type RequestLogContext } from "../request-log";
19
+ import { disableResponsesRequestTimeout } from "../responses";
20
+ import type { AdmittedHttpTurnOptions } from "../admitted-http-turn";
21
+ import type { WorkflowRefusalLog } from "../workflow-refusal";
22
+ import { withRequestLogId } from "./startup-warnings";
23
+
24
+ export interface OllamaRouteDeps {
25
+ config: OcxConfig;
26
+ policy: RequestPolicyView;
27
+ requestServer: Server<WsData>;
28
+ requestMetricsLogContext: Partial<RequestLogContext>;
29
+ drainingResponse: (req: Request, policy: RequestPolicyView) => Response;
30
+ runAdmittedHttpTurn: (
31
+ req: Request,
32
+ policy: RequestPolicyView,
33
+ work: (lease: ActiveTurnLease) => Promise<Response>,
34
+ refusalLog?: WorkflowRefusalLog,
35
+ options?: AdmittedHttpTurnOptions,
36
+ ) => Promise<Response>;
37
+ }
38
+
39
+ /** Returns undefined when the path is not one of the native routes. */
40
+ export async function dispatchOllamaRoute(req: Request, url: URL, deps: OllamaRouteDeps): Promise<Response | undefined> {
41
+ const route = OLLAMA_NATIVE_ROUTES.get(url.pathname);
42
+ if (!route) return undefined;
43
+ const { config, policy } = deps;
44
+ if (req.method !== route.method) {
45
+ const refused = ollamaError(405, `method ${req.method} not allowed`);
46
+ refused.headers.set("Allow", route.method);
47
+ return withCors(refused, req, policy);
48
+ }
49
+ if (route.kind === "version" || route.kind === "tags" || route.kind === "show") {
50
+ // Model discovery never forwards Authorization upstream, so the broader admission set is safe.
51
+ const admission = resolveApiAuth(req, policy);
52
+ if (!admission) return withCors(ollamaError(401, "opencodex API key required"), req, policy);
53
+ if (!isAllowedRequestOrigin(req, policy)) return withCors(ollamaError(403, "cross-origin data-plane request blocked"), req, policy);
54
+ const response = route.kind === "version" ? handleOllamaVersion()
55
+ : route.kind === "tags" ? handleOllamaTags(config, req, admission)
56
+ : await handleOllamaShow(config, req, admission);
57
+ return withCors(response, req, policy);
58
+ }
59
+ const kind = route.kind;
60
+ disableResponsesRequestTimeout(req, deps.requestServer);
61
+ if (isDraining()) return withCors(toOllamaRefusal(deps.drainingResponse(req, policy)), req, policy);
62
+ const admission = resolveResponsesApiAuth(req, policy);
63
+ if (!admission) return withCors(ollamaError(401, "opencodex API key required"), req, policy);
64
+ if (!isAllowedRequestOrigin(req, policy)) return withCors(ollamaError(403, "cross-origin data-plane request blocked"), req, policy);
65
+ const start = Date.now();
66
+ const requestId = nextRequestLogId(start);
67
+ const logCtx: RequestLogContext = {
68
+ model: "unknown",
69
+ provider: "unknown",
70
+ ...deps.requestMetricsLogContext,
71
+ ...admissionFields(admission),
72
+ };
73
+ const sessionReq = withCallerSessionIdentity(req, admission);
74
+ // Expose an id only when the handler or a logged refusal owns the history row.
75
+ let ownsRequestLog = false;
76
+ const advisorAdmissionPolicy = captureAdmissionPolicy(config, admission);
77
+ const response = await deps.runAdmittedHttpTurn(sessionReq, policy, async turnAdmissionLease => {
78
+ ownsRequestLog = true;
79
+ return withCors(
80
+ await handleOllamaGeneration(kind, sessionReq, config, logCtx, { requestId, start, turnAdmissionLease, admission, advisorAdmissionPolicy }),
81
+ req,
82
+ policy,
83
+ );
84
+ }, { requestId, start, logCtx, onLogged: () => { ownsRequestLog = true; } }, {
85
+ mapRefusal: toOllamaRefusal,
86
+ onAdmissionError: () => ollamaError(499, "request cancelled"),
87
+ });
88
+ return ownsRequestLog ? withRequestLogId(response, requestId) : response;
89
+ }
@@ -203,6 +203,10 @@ import type { OcxConfig } from "../../types";
203
203
  import type { PackageTreeIntegrityGuard } from "../../lib/package-tree-integrity";
204
204
  import type { ReadinessGate } from "../readiness";
205
205
  import type { WorkflowRefusalLog } from "../workflow-refusal";
206
+ import type { AdmittedHttpTurnOptions } from "../admitted-http-turn";
207
+ import { isOllamaNativePath } from "../ollama-native-paths";
208
+ import { ollamaError } from "../ollama-inbound";
209
+ import { dispatchOllamaRoute } from "./ollama-routes";
206
210
 
207
211
  import { readyProtocolMetadata } from "../../remote/protocol";
208
212
  import { homeInstanceId, stampServedBy } from "../../remote/served-by";
@@ -212,6 +216,7 @@ import { reasoningArchiveErrorResponse } from "../responses/reasoning-archive-er
212
216
  import { withGrokSessionIdentity } from "../../grok/session-identity";
213
217
  import { withCallerSessionIdentity } from "../caller-session-identity";
214
218
  import { agentAssertionUnsupportedResponse, hasAgentAssertionAuthorization } from "../../lib/authorization-scheme";
219
+ import { runTurnStateCallerScope } from "../turn-state-handles";
215
220
 
216
221
  export type ServerIngress = "public" | "unauthenticated-loopback" | "hub-management" | "claude-intercept" | "hub-link";
217
222
 
@@ -256,6 +261,7 @@ export interface ServeOptionsContext {
256
261
  policy: RequestPolicyView,
257
262
  work: (lease: ActiveTurnLease) => Promise<Response>,
258
263
  refusalLog?: WorkflowRefusalLog,
264
+ options?: AdmittedHttpTurnOptions,
259
265
  ) => Promise<Response>;
260
266
 
261
267
  config: OcxConfig;
@@ -414,6 +420,7 @@ export function createServeOptions(ctx: ServeOptionsContext) {
414
420
  url.pathname === "/healthz"
415
421
  || readyzPath !== undefined
416
422
  || url.pathname.startsWith("/v1/")
423
+ || isOllamaNativePath(url.pathname)
417
424
  )) {
418
425
  const message = "OpenCodex package files changed while this proxy was running; it restarts on its own, or run 'ocx restart' ('ocx service restart' for a background service).";
419
426
  const fencedPort = ctx.boundPort ?? requestServer.port ?? listenPort;
@@ -437,7 +444,9 @@ export function createServeOptions(ctx: ServeOptionsContext) {
437
444
  } : {}),
438
445
  error: { code: "package_tree_changed", message },
439
446
  }, 503, req, policy)
440
- : packageTreeChangedResponse(req, policy, message);
447
+ : isOllamaNativePath(url.pathname)
448
+ ? withCors(ollamaError(503, message), req, policy)
449
+ : packageTreeChangedResponse(req, policy, message);
441
450
  const headers = new Headers(response.headers);
442
451
  headers.set("Retry-After", "5");
443
452
  const challenge = fencedHealth ? req.headers.get(LOCAL_ATTESTATION_CHALLENGE_HEADER) : null;
@@ -453,7 +462,8 @@ export function createServeOptions(ctx: ServeOptionsContext) {
453
462
  if (readyzPath !== undefined) {
454
463
  return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, policy);
455
464
  }
456
- const managementPreflight = url.pathname.startsWith("/api/");
465
+ // The exact Ollama-compatible paths are data-plane routes and take data-plane CORS.
466
+ const managementPreflight = url.pathname.startsWith("/api/") && !isOllamaNativePath(url.pathname);
457
467
  const allowed = managementPreflight
458
468
  ? isAllowedManagementOrigin(req, config)
459
469
  : isAllowedRequestOrigin(req, policy);
@@ -695,6 +705,12 @@ export function createServeOptions(ctx: ServeOptionsContext) {
695
705
  return new Response(resp.body, { status: 503, headers });
696
706
  }
697
707
 
708
+ // Checked synchronously: an await here would delay every later route's admission capture.
709
+ if (isOllamaNativePath(url.pathname)) {
710
+ const native = await dispatchOllamaRoute(req, url, { config, policy, requestServer, requestMetricsLogContext, drainingResponse, runAdmittedHttpTurn });
711
+ if (native) return native;
712
+ }
713
+
698
714
  if (url.pathname.startsWith("/api/")) {
699
715
  const localManagementAuth = {
700
716
  attestationSecret: localAttestationSecret,
@@ -1838,6 +1854,7 @@ export function createServeOptions(ctx: ServeOptionsContext) {
1838
1854
  ...(audioClient?.protocol ? { headers: { "sec-websocket-protocol": audioClient.protocol } } : {}),
1839
1855
  data: {
1840
1856
  kind: "live-sideband",
1857
+ authContext: resolved.authContext,
1841
1858
  liveUpstream: upstreamHandshake.socket,
1842
1859
  liveUpstreamUrl: resolved.upstreamWsUrl,
1843
1860
  liveUpstreamHeaders: resolved.headers,
@@ -1970,6 +1987,7 @@ export function createServeOptions(ctx: ServeOptionsContext) {
1970
1987
  } as const;
1971
1988
  const routeFetch = serveOptions.fetch;
1972
1989
  // x-opencodex-served-by on GET /v1/models, whichever branch answered (src/remote/served-by.ts).
1973
- return { ...serveOptions, fetch: async (req: Request, requestServer: Server<WsData>): Promise<Response> =>
1974
- stampServedBy(req.method, codexCompatibleUrl(req.url).pathname, await routeFetch(req, requestServer)) } as const;
1990
+ // Each request runs with its own caller slot, which admission fills for turn-state handle binding.
1991
+ return { ...serveOptions, fetch: (req: Request, requestServer: Server<WsData>): Promise<Response> => runTurnStateCallerScope(async () =>
1992
+ stampServedBy(req.method, codexCompatibleUrl(req.url).pathname, await routeFetch(req, requestServer))) } as const;
1975
1993
  }
@@ -93,6 +93,7 @@ import type { ServeOptionsContext } from "./serve-options";
93
93
  import type { RequestMetricsRecorder } from "../request-metrics";
94
94
  import { resolveInboundBodyLimitBytes } from "../request-decompress";
95
95
  import { responseToolOwnership } from "../responses/response-tool-ownership";
96
+ import { runTurnStateCallerScope } from "../turn-state-handles";
96
97
 
97
98
  /**
98
99
  * The WebSocket half of the Bun.serve options, split out of serve-options.ts to keep that file
@@ -179,7 +180,7 @@ export function createWebsocketHandler(
179
180
  return;
180
181
  }
181
182
  try {
182
- sendUpstreamFrame(upstream, raw);
183
+ sendUpstreamFrame(upstream, raw, ws.data.authContext);
183
184
  if (ws.data.liveMaxSessionMs !== undefined && upstream.bufferedAmount > MAX_WS_FRAME_BYTES) {
184
185
  closeLiveSideband(ws, 1013, "audio upstream backpressure");
185
186
  }
@@ -310,7 +311,8 @@ export function createWebsocketHandler(
310
311
  const payload: Record<string, unknown> = { ...frame };
311
312
  delete payload.type;
312
313
  turnAdmissionLease.bindAbortController(turnAbort);
313
- void (async () => {
314
+ // The frame runs with its own caller slot, which admission fills for turn-state handle binding.
315
+ void runTurnStateCallerScope(async () => {
314
316
  const start = Date.now();
315
317
  const requestId = nextRequestLogId(start);
316
318
  // Resolved once at the handshake — a frame has no request headers left
@@ -431,7 +433,7 @@ export function createWebsocketHandler(
431
433
  if (!logged && turnAbort.signal.aborted) finalizeLog(499);
432
434
  if (ws.data.cancel === cancelTurn) ws.data.cancel = undefined;
433
435
  }
434
- })();
436
+ });
435
437
  },
436
438
  close(ws: ServerWebSocket<WsData>, code: number, reason: string) {
437
439
  if (ws.data.kind === "remote-workspace-agent") {
@@ -2,7 +2,9 @@ import { remoteWorkspaceEnabled } from "../remote-control/workspace-activation";
2
2
  import { scheduleDesktopCompatibilityStartup } from "./index/desktop-compatibility-startup";
3
3
  import { AuxiliaryListenerBindError } from "./ports";
4
4
  import { normalizeBindHostname } from "./bind-host";
5
- import { configureInboundBodyBudget, runAdmittedBodyWork } from "./inbound-body-admission";
5
+ import { configureInboundBodyBudget } from "./inbound-body-admission";
6
+ import { createAdmittedHttpTurn } from "./admitted-http-turn";
7
+ import { isOllamaNativePath } from "./ollama-native-paths";
6
8
  import {
7
9
  buildWarmupCompletionFrames,
8
10
  buildWsErrorFrame,
@@ -93,9 +95,7 @@ import {
93
95
  runListenerShutdown,
94
96
  setServerRef,
95
97
  trackStreamLifetime,
96
- tryAdmitTurn,
97
98
  unregisterTurn,
98
- type ActiveTurnLease,
99
99
  } from "./lifecycle";
100
100
  export {
101
101
  drainAndShutdown,
@@ -116,9 +116,7 @@ import {
116
116
  type RequestLogContext,
117
117
  type RequestLogEntry,
118
118
  } from "./request-log";
119
- import { sessionLaneIdFromRequest } from "./request-log-conversation";
120
119
  import { setUsageLedgerRetention } from "./usage-ledger-retention";
121
- import { admitHttpWorkflowTurn, workflowDecisionRefusalResponse, type WorkflowRefusalLog } from "./workflow-refusal";
122
120
  export {
123
121
  addFinalRequestLog,
124
122
  filterRequestLogs, queryRequestLogs,
@@ -450,6 +448,8 @@ function startServerWithSpendLedgerOwner(port: number | undefined, deps: StartSe
450
448
  }
451
449
  if (rawPath === "/remote-workspace/pair") return remoteWorkspaceEnabled(config) && req.method === "POST";
452
450
  if (rawPath === "/opencodex-session") return req.method === "GET" || req.method === "POST";
451
+ // Ollama-compatible inference paths are data plane, not management: never via Tailscale Serve.
452
+ if (isOllamaNativePath(rawPath)) return false;
453
453
  if (rawPath.startsWith("/api/")) return true;
454
454
  if (req.method !== "GET" && req.method !== "HEAD") return false;
455
455
  let decodedPath: string;
@@ -500,37 +500,7 @@ function startServerWithSpendLedgerOwner(port: number | undefined, deps: StartSe
500
500
  }), req, policy);
501
501
  }
502
502
 
503
- async function runAdmittedHttpTurn(
504
- req: Request,
505
- policy: RequestPolicyView,
506
- work: (lease: ActiveTurnLease) => Promise<Response>,
507
- refusalLog?: WorkflowRefusalLog,
508
- ): Promise<Response> {
509
- const lease = tryAdmitTurn(sessionLaneIdFromRequest(req.headers), req.signal);
510
- if (!lease) return serverBusyResponse(req, "active turns", policy);
511
- // Root, lane, and the refusal that follows from them, all live in ./workflow-refusal.
512
- let workflow: ReturnType<typeof admitHttpWorkflowTurn>;
513
- try { workflow = admitHttpWorkflowTurn(req.headers); }
514
- catch (error) { lease.release(); throw error; }
515
- if (workflow && !workflow.admitted) {
516
- lease.release();
517
- // withCors, because without Access-Control-Allow-Origin the exposed refusal header is
518
- // still unreadable to a browser dashboard -- which made exposing it pointless.
519
- return withCors(workflowDecisionRefusalResponse(workflow, undefined, refusalLog), req, policy);
520
- }
521
- if (workflow?.admitted) lease.attach(workflow.lease);
522
- let response: Response;
523
- try {
524
- response = await runAdmittedBodyWork(req, policy, config.maxInboundBodyBytes, () => work(lease), refusalLog);
525
- } catch (error) {
526
- lease.release();
527
- throw error;
528
- }
529
- if (!lease.isTransferred()) {
530
- lease.release();
531
- }
532
- return response;
533
- }
503
+ const runAdmittedHttpTurn = createAdmittedHttpTurn(() => config.maxInboundBodyBytes, serverBusyResponse);
534
504
 
535
505
  // Readiness gate: one PRIVATE controller per startServer invocation, captured
536
506
  // by this listener's closure. Starting/failing a second server in the same
@@ -1,3 +1,4 @@
1
+ import type { FailureVerdictSink } from "../../chat/failure-verdict";
1
2
  /**
2
3
  * Adapter delivery straight into a Chat or Messages client's wire (PF-09).
3
4
  *
@@ -215,6 +216,7 @@ export async function deliverClientEncodedResponse(input: ClientEncodedDelivery)
215
216
  recorder?.noteRelayedEvent(observation);
216
217
  },
217
218
  };
219
+ const failureVerdictSink: FailureVerdictSink = {};
218
220
  const encodeOptions = {
219
221
  model: encoder.model,
220
222
  translatorBudget,
@@ -224,7 +226,7 @@ export async function deliverClientEncodedResponse(input: ClientEncodedDelivery)
224
226
  hooks,
225
227
  };
226
228
  const encoded = encoder.protocol === "chat"
227
- ? encodeChatCompletionSse(events, encodeOptions)
229
+ ? encodeChatCompletionSse(events, { ...encodeOptions, failureVerdictSink, proxyErrorContext: logCtx, get localUpstream() { return input.localUpstream; } })
228
230
  : encodeAnthropicMessageSse(events, {
229
231
  ...encodeOptions,
230
232
  get localUpstream() { return input.localUpstream; },
@@ -238,7 +240,7 @@ export async function deliverClientEncodedResponse(input: ClientEncodedDelivery)
238
240
  } else {
239
241
  // The routed turn always streams internally; a non-streaming client gets the fold.
240
242
  response = encoder.protocol === "chat"
241
- ? await collectChatCompletionResponse(tracked, encoder.model, translatorBudget)
243
+ ? await collectChatCompletionResponse(tracked, encoder.model, translatorBudget, failureVerdictSink)
242
244
  : await collectAnthropicMessageResponse(tracked, encoder.model, translatorBudget);
243
245
  }
244
246
  return attachClientWireLog(markClientWire(response, encoder.protocol), log);
@@ -0,0 +1,44 @@
1
+ import { Buffer } from "node:buffer";
2
+
3
+ type Frame =
4
+ | { items: readonly unknown[]; index: number }
5
+ | { record: Record<string, unknown>; keys: readonly string[]; index: number };
6
+
7
+ /**
8
+ * UTF-8 byte length of JSON.stringify(value) for a JSON.parse result, computed without
9
+ * recursion: a valid body nested deeper than the call stack must be measured, not crash.
10
+ * Each open container keeps one cursor frame, so arrays add no per-element queue; an object
11
+ * frame retains its key list, which stays bounded by the input size.
12
+ */
13
+ export function serializedJsonByteLength(value: unknown): number {
14
+ let bytes = 0;
15
+ const stack: Frame[] = [];
16
+ const visit = (item: unknown): void => {
17
+ if (item === null || typeof item !== "object") {
18
+ bytes += Buffer.byteLength(JSON.stringify(item) ?? "null", "utf8");
19
+ } else if (Array.isArray(item)) {
20
+ bytes += 2 + Math.max(0, item.length - 1);
21
+ stack.push({ items: item, index: 0 });
22
+ } else {
23
+ const record = item as Record<string, unknown>;
24
+ const keys = Object.keys(record);
25
+ bytes += 2 + Math.max(0, keys.length - 1);
26
+ stack.push({ record, keys, index: 0 });
27
+ }
28
+ };
29
+ visit(value);
30
+ while (stack.length > 0) {
31
+ const frame = stack[stack.length - 1]!;
32
+ if ("items" in frame) {
33
+ if (frame.index < frame.items.length) visit(frame.items[frame.index++]);
34
+ else stack.pop();
35
+ } else if (frame.index < frame.keys.length) {
36
+ const key = frame.keys[frame.index++]!;
37
+ bytes += Buffer.byteLength(JSON.stringify(key), "utf8") + 1;
38
+ visit(frame.record[key]);
39
+ } else {
40
+ stack.pop();
41
+ }
42
+ }
43
+ return bytes;
44
+ }