@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
@@ -0,0 +1,349 @@
1
+ /**
2
+ * Ollama-compatible inbound API: /api/version, /api/tags, /api/show, /api/chat and /api/generate.
3
+ *
4
+ * The model list describes configured provider routes this proxy can serve, not models stored on
5
+ * this machine. Generation is translated into an internal Chat Completions request and handled by
6
+ * the ordinary Chat path, so routing, admission scope, retries and usage accounting stay shared.
7
+ * The final request-log row is owned here (see ./native-final-log): only this handler knows
8
+ * whether its own conversion succeeded or the client went away.
9
+ */
10
+ import { OllamaCodecError, consumeOllamaChatChunk, createOllamaStreamState, ollamaBufferedResponse, ollamaStreamTerminal, ollamaToChat, type OllamaKind } from "../protocols/codecs/ollama";
11
+ import { createTranslatorBudget, isTranslatorBudgetExceededError } from "../lib/translator-budget";
12
+ import { parseRetryAfterMs } from "../combos";
13
+ import { WORKFLOW_LOCAL_REFUSAL_HEADER } from "../lib/workflow-budget";
14
+ import type { OcxConfig } from "../types";
15
+ import type { DataPlaneAdmission } from "./auth-cors";
16
+ import type { CapturedAdmissionPolicy } from "../types/admission-policy";
17
+ import type { AdmissionLease } from "../lib/admission";
18
+ import { resolveAdmissionModelScope, routeAllowedByScope } from "./admission-model-scope";
19
+ import { handleChatCompletions } from "./chat-completions";
20
+ import { VERSION } from "./management-api";
21
+ import { createNativeFinalLog, type NativeFinalLog } from "./native-final-log";
22
+ import { findOllamaRoute, linkRequestSessionLane, listOllamaRoutes, preflightOllamaRoute } from "./ollama-route-policy";
23
+ import { DecompressedBodyTooLargeError, readJsonRequestBody, resolveInboundBodyLimitBytes } from "./request-decompress";
24
+ import { addFinalRequestLog, type RequestLogContext } from "./request-log";
25
+
26
+ const SHOW_BODY_LIMIT_BYTES = 64 * 1024;
27
+ // Session and workflow identity only: lineage, session lanes and the workflow root's spend
28
+ // ceiling read these from the request Chat sees. Never authorization, cookies or admission keys.
29
+ const IDENTITY_HEADERS = ["session_id", "session-id", "thread-id", "x-session-id", "x-codex-parent-thread-id"] as const;
30
+
31
+ /** Upper bound for the retry advice this surface passes on. */
32
+ export const OLLAMA_MAX_RETRY_AFTER_SECONDS = 3600;
33
+
34
+ /**
35
+ * Retry-After as an integer we format ourselves. The incoming value may come from an upstream,
36
+ * so only the parsed delay crosses this boundary, never its bytes; unparseable values are dropped.
37
+ */
38
+ export function ollamaRetryAfter(value: string | null | undefined, now = Date.now()): string | undefined {
39
+ if (value?.trim() === "0") return "0";
40
+ const ms = parseRetryAfterMs(value, now);
41
+ if (ms === undefined) return undefined;
42
+ return String(Math.min(OLLAMA_MAX_RETRY_AFTER_SECONDS, Math.max(0, Math.ceil(ms / 1000))));
43
+ }
44
+
45
+ /**
46
+ * Every Ollama error response is built from scratch: content type, our own error text, and
47
+ * optionally a reformatted Retry-After. Callers add CORS and the request-log id; nothing is
48
+ * copied from another response.
49
+ */
50
+ export function ollamaError(status: number, message: string, retryAfter?: string | null): Response {
51
+ const headers = new Headers({ "content-type": "application/json" });
52
+ const advice = ollamaRetryAfter(retryAfter);
53
+ if (advice !== undefined) headers.set("Retry-After", advice);
54
+ return new Response(JSON.stringify({ error: message }), { status, headers });
55
+ }
56
+
57
+ /**
58
+ * The local refusal codes this surface passes on, each mapped to a constant defined here. The
59
+ * emitted header value is always the constant; an incoming value only selects it, and any value
60
+ * not in this table is dropped.
61
+ */
62
+ const LOCAL_REFUSAL_CODES: ReadonlyMap<string, string> = new Map([
63
+ ["workflow_sends_exhausted", "workflow_sends_exhausted"],
64
+ ["workflow_children_exhausted", "workflow_children_exhausted"],
65
+ ["workflow_concurrency_exhausted", "workflow_concurrency_exhausted"],
66
+ ["workflow_spend_exhausted", "workflow_spend_exhausted"],
67
+ ["workflow_tracking_exhausted", "workflow_tracking_exhausted"],
68
+ ["workflow_send_replayed", "workflow_send_replayed"],
69
+ ["workflow_pool_history_unresolved", "workflow_pool_history_unresolved"],
70
+ ["workflow_spend_undurable", "workflow_spend_undurable"],
71
+ ]);
72
+
73
+ /** Reshape an existing proxy refusal (capacity, workflow, drain) keeping its status and retry advice. */
74
+ export function toOllamaRefusal(response: Response): Response {
75
+ try { void response.body?.cancel().catch(() => {}); } catch { /* already consumed */ }
76
+ const message = response.status === 429 ? "request budget exhausted; retry later"
77
+ : response.status === 499 ? "request cancelled"
78
+ : response.status === 503 ? "server temporarily unavailable; retry later" : "request refused";
79
+ const native = ollamaError(response.status, message, response.headers.get("retry-after"));
80
+ // The workflow refusal names its local reason; only a recognized code selects our constant.
81
+ const code = LOCAL_REFUSAL_CODES.get(response.headers.get(WORKFLOW_LOCAL_REFUSAL_HEADER) ?? "");
82
+ if (code !== undefined) {
83
+ native.headers.set(WORKFLOW_LOCAL_REFUSAL_HEADER, code);
84
+ native.headers.set("Access-Control-Expose-Headers", WORKFLOW_LOCAL_REFUSAL_HEADER);
85
+ }
86
+ return native;
87
+ }
88
+
89
+ export function handleOllamaVersion(): Response {
90
+ return Response.json({ version: VERSION });
91
+ }
92
+
93
+ function eligibleRoutes(config: OcxConfig, req: Request, admission: DataPlaneAdmission) {
94
+ const scope = resolveAdmissionModelScope(config, admission);
95
+ return listOllamaRoutes(config, req)
96
+ .filter(row => routeAllowedByScope(scope, { providerName: row.route.providerName, modelId: row.route.modelId }));
97
+ }
98
+
99
+ export function handleOllamaTags(config: OcxConfig, req: Request, admission: DataPlaneAdmission): Response {
100
+ return Response.json({ models: eligibleRoutes(config, req, admission).map(row => ({ name: row.selector, model: row.selector })) });
101
+ }
102
+
103
+ export async function handleOllamaShow(config: OcxConfig, req: Request, admission: DataPlaneAdmission): Promise<Response> {
104
+ let body: unknown;
105
+ try { body = await readJsonRequestBody(req, undefined, SHOW_BODY_LIMIT_BYTES); }
106
+ catch (error) { return ollamaError(error instanceof DecompressedBodyTooLargeError ? 413 : 400, "invalid request body"); }
107
+ const record = body && typeof body === "object" && !Array.isArray(body) ? body as Record<string, unknown> : undefined;
108
+ const model = record?.model;
109
+ if (!record || Object.keys(record).some(key => key !== "model" && key !== "verbose") || typeof model !== "string" || !model.trim()) {
110
+ return ollamaError(400, "model is required");
111
+ }
112
+ // Same eligibility as /api/tags: a configured concrete route the caller's key may use.
113
+ // Same eligibility as /api/tags, resolved for this selector only.
114
+ const row = findOllamaRoute(config, model, req);
115
+ const scope = resolveAdmissionModelScope(config, admission);
116
+ if (!row || !routeAllowedByScope(scope, { providerName: row.route.providerName, modelId: row.route.modelId })) return ollamaError(404, "model not found");
117
+ // Only facts this proxy knows. No file size, digest, template, license or local model details.
118
+ return Response.json({ details: {}, model_info: {}, capabilities: ["completion"] });
119
+ }
120
+
121
+ export interface OllamaLogIds {
122
+ requestId: string;
123
+ start: number;
124
+ turnAdmissionLease?: AdmissionLease;
125
+ admission?: DataPlaneAdmission;
126
+ advisorAdmissionPolicy?: CapturedAdmissionPolicy;
127
+ }
128
+
129
+ function failBeforeInner(ids: OllamaLogIds, logCtx: RequestLogContext, status: number, message: string): Response {
130
+ addFinalRequestLog(ids.requestId, ids.start, logCtx, status, { closeReason: "non_stream" });
131
+ return ollamaError(status, message);
132
+ }
133
+
134
+ function internalChatRequest(req: Request, chatJson: string): Request {
135
+ const headers = new Headers({ "content-type": "application/json" });
136
+ for (const name of IDENTITY_HEADERS) {
137
+ const value = req.headers.get(name);
138
+ if (value !== null) headers.set(name, value);
139
+ }
140
+ const internal = new Request(new URL("/v1/chat/completions", req.url), { method: "POST", headers, body: chatJson, signal: req.signal });
141
+ linkRequestSessionLane(req, internal);
142
+ return internal;
143
+ }
144
+
145
+ /**
146
+ * Native error text is chosen by status only. The inner body can carry upstream-authored text,
147
+ * which may echo a credential no pattern recognizes, so it is drained and never forwarded.
148
+ */
149
+ const INNER_ERROR_MESSAGES: Readonly<Record<number, string>> = {
150
+ 400: "request rejected",
151
+ 401: "upstream authentication failed",
152
+ 402: "upstream payment required",
153
+ 403: "request not permitted",
154
+ 404: "model not found",
155
+ 408: "upstream request timed out",
156
+ 409: "request conflict",
157
+ 413: "request too large",
158
+ 422: "request rejected",
159
+ 429: "rate limited; retry later",
160
+ 499: "request cancelled",
161
+ 500: "upstream request failed",
162
+ 502: "upstream request failed",
163
+ 503: "upstream unavailable; retry later",
164
+ 504: "upstream request timed out",
165
+ };
166
+
167
+ export function ollamaInnerErrorMessage(status: number): string {
168
+ return INNER_ERROR_MESSAGES[status] ?? (status >= 500 ? "upstream request failed" : "request failed");
169
+ }
170
+
171
+ async function innerErrorMessage(response: Response): Promise<string> {
172
+ await response.body?.cancel().catch(() => {});
173
+ return ollamaInnerErrorMessage(response.ok ? 502 : response.status);
174
+ }
175
+
176
+ /** /api/chat and /api/generate. `req` is the admitted, session-promoted request. */
177
+ export async function handleOllamaGeneration(
178
+ kind: OllamaKind,
179
+ req: Request,
180
+ config: OcxConfig,
181
+ logCtx: RequestLogContext,
182
+ ids: OllamaLogIds,
183
+ ): Promise<Response> {
184
+ const limit = resolveInboundBodyLimitBytes(config.maxInboundBodyBytes);
185
+ const budget = createTranslatorBudget();
186
+ let chat: Record<string, unknown>;
187
+ try {
188
+ chat = ollamaToChat(kind, await readJsonRequestBody(req, budget, limit));
189
+ } catch (error) {
190
+ if (req.signal.aborted) return failBeforeInner(ids, logCtx, 499, "request cancelled");
191
+ if (error instanceof DecompressedBodyTooLargeError) return failBeforeInner(ids, logCtx, 413, "request body too large");
192
+ if (isTranslatorBudgetExceededError(error)) return failBeforeInner(ids, logCtx, 502, "request translation buffer exceeded the safe limit");
193
+ return failBeforeInner(ids, logCtx, 400, error instanceof OllamaCodecError ? error.message : "invalid JSON body");
194
+ } finally {
195
+ budget.dispose();
196
+ }
197
+ const model = chat.model as string;
198
+ logCtx.requestedModel = model;
199
+ const preflight = preflightOllamaRoute(config, model, req);
200
+ if (!preflight.ok) {
201
+ logCtx.errorCode = preflight.error.code;
202
+ return failBeforeInner(ids, logCtx, preflight.error.status, preflight.error.message);
203
+ }
204
+ // Both Chat delivery paths already report streamed usage on their own; the body stays as a Chat caller sends it.
205
+ const stream = chat.stream === true;
206
+ const chatJson = JSON.stringify(chat);
207
+ if (Buffer.byteLength(chatJson) > limit) return failBeforeInner(ids, logCtx, 413, "request body too large");
208
+ const internal = internalChatRequest(req, chatJson);
209
+ const finalLog = createNativeFinalLog(ids.requestId, ids.start, logCtx);
210
+ const createdAt = new Date().toISOString();
211
+ let inner: Response;
212
+ try {
213
+ inner = await handleChatCompletions(internal, config, logCtx, { ...ids, suppressProtocolTrace: true });
214
+ } catch {
215
+ await finalLog.waitForInner();
216
+ if (req.signal.aborted) return cancelledNonStream(finalLog);
217
+ finalLog.commit({ status: 502, terminalStatus: "failed", closeReason: "non_stream" });
218
+ return ollamaError(502, "request failed");
219
+ }
220
+ if (!inner.ok || !inner.body) {
221
+ const message = await innerErrorMessage(inner);
222
+ await finalLog.waitForInner();
223
+ if (req.signal.aborted) return cancelledNonStream(finalLog);
224
+ finalLog.commit({ status: inner.ok ? 502 : inner.status, terminalStatus: "failed", closeReason: "non_stream" });
225
+ return ollamaError(inner.ok ? 502 : inner.status, message, inner.headers.get("retry-after"));
226
+ }
227
+ return stream
228
+ ? streamOllama(kind, model, createdAt, inner.body, finalLog, req.signal)
229
+ : bufferedOllama(kind, model, createdAt, inner, finalLog, req.signal);
230
+ }
231
+
232
+ /** A client that left before the outer outcome was committed owns a 499 row, whatever the inner path reported. */
233
+ function cancelledNonStream(finalLog: NativeFinalLog): Response {
234
+ finalLog.commit({ status: 499, closeReason: "client_cancel" });
235
+ return ollamaError(499, "request cancelled");
236
+ }
237
+
238
+ /** Exported for the settlement-window cancellation test; production callers go through handleOllamaGeneration. */
239
+ export async function bufferedOllama(kind: OllamaKind, model: string, createdAt: string, inner: Response, finalLog: NativeFinalLog, signal: AbortSignal): Promise<Response> {
240
+ let native: Record<string, unknown>;
241
+ try {
242
+ native = ollamaBufferedResponse(kind, model, JSON.parse(await inner.text()), createdAt);
243
+ } catch {
244
+ await finalLog.waitForInner();
245
+ if (signal.aborted) return cancelledNonStream(finalLog);
246
+ finalLog.commit({ status: 502, terminalStatus: "failed", closeReason: "non_stream" });
247
+ return ollamaError(502, "invalid upstream response");
248
+ }
249
+ const settled = await finalLog.waitForInner();
250
+ if (signal.aborted) return cancelledNonStream(finalLog);
251
+ if (native.done !== true || !settled) {
252
+ finalLog.commit({ status: 502, terminalStatus: "failed", closeReason: "non_stream" });
253
+ return ollamaError(502, typeof native.error === "string" ? native.error : "request did not complete");
254
+ }
255
+ finalLog.commit({ status: 200, terminalStatus: "completed", closeReason: "non_stream" });
256
+ return Response.json(native);
257
+ }
258
+
259
+ const encoder = new TextEncoder();
260
+ const line = (record: unknown) => encoder.encode(`${JSON.stringify(record)}\n`);
261
+
262
+ /** Exported for the settlement-window cancellation test; production callers go through handleOllamaGeneration. */
263
+ export function streamOllama(kind: OllamaKind, model: string, createdAt: string, body: ReadableStream<Uint8Array>, finalLog: NativeFinalLog, signal: AbortSignal): Response {
264
+ const reader = body.getReader();
265
+ const decoder = new TextDecoder();
266
+ const state = createOllamaStreamState(kind, model, createdAt);
267
+ let pending = "";
268
+ // ended: no further reads or records. cancelled: the client went away, possibly while end() waits.
269
+ let ended = false;
270
+ let cancelled = false;
271
+ // The inner Chat path can finalize on EOF after [DONE], so keep draining to EOF before ending.
272
+ let sawDone = false;
273
+ const cancelInner = () => { try { void reader.cancel().catch(() => {}); } catch { /* released */ } };
274
+ const commitCancelled = () => finalLog.commit({ status: 499, closeReason: "client_cancel" });
275
+ const end = async (controller: ReadableStreamDefaultController<Uint8Array>, failure?: string) => {
276
+ if (ended) return;
277
+ ended = true;
278
+ if (failure !== undefined) cancelInner();
279
+ const terminal = failure === undefined ? ollamaStreamTerminal(state) : undefined;
280
+ const settled = await finalLog.waitForInner();
281
+ if (cancelled || signal.aborted) {
282
+ commitCancelled();
283
+ try { controller.close(); } catch { /* already cancelled by the client */ }
284
+ return;
285
+ }
286
+ try {
287
+ if (terminal?.done === true && settled) {
288
+ controller.enqueue(line(terminal));
289
+ finalLog.commit({ status: 200, terminalStatus: "completed", closeReason: "terminal" });
290
+ } else {
291
+ controller.enqueue(line({ error: failure ?? (typeof terminal?.error === "string" ? terminal.error : "request did not complete") }));
292
+ finalLog.commit({ status: 502, terminalStatus: "failed", closeReason: "terminal" });
293
+ }
294
+ controller.close();
295
+ } catch {
296
+ // The client cancelled between settlement and the terminal record; the row still lands once.
297
+ commitCancelled();
298
+ }
299
+ };
300
+ // Returns how many native records were enqueued. Finish and usage frames enqueue nothing.
301
+ const consume = (controller: ReadableStreamDefaultController<Uint8Array>, block: string): number => {
302
+ let emitted = 0;
303
+ for (const raw of block.split("\n")) {
304
+ const text = raw.trimEnd();
305
+ if (!text.startsWith("data:")) continue;
306
+ // Chat ends at [DONE]; any later data (an error frame, say) means the stream did not settle cleanly.
307
+ if (sawDone) throw new Error("data after the Chat stream terminator");
308
+ const payload = text.slice(5).trim();
309
+ if (payload === "[DONE]") { sawDone = true; continue; }
310
+ for (const record of consumeOllamaChatChunk(state, JSON.parse(payload))) { controller.enqueue(line(record)); emitted++; }
311
+ }
312
+ return emitted;
313
+ };
314
+ return new Response(new ReadableStream<Uint8Array>({
315
+ // A pull that resolves without enqueueing is not called again, so keep reading until this
316
+ // pull has produced a record or ended the stream.
317
+ async pull(controller) {
318
+ try {
319
+ while (!ended) {
320
+ const { done, value } = await reader.read();
321
+ if (done) {
322
+ pending += decoder.decode();
323
+ if (pending) consume(controller, pending);
324
+ pending = "";
325
+ await end(controller);
326
+ return;
327
+ }
328
+ pending += decoder.decode(value, { stream: true });
329
+ const boundary = pending.lastIndexOf("\n");
330
+ if (boundary < 0) continue;
331
+ const complete = pending.slice(0, boundary);
332
+ pending = pending.slice(boundary + 1);
333
+ if (consume(controller, complete) > 0) return;
334
+ }
335
+ } catch {
336
+ await end(controller, signal.aborted ? "request cancelled" : "upstream stream failed");
337
+ }
338
+ },
339
+ async cancel() {
340
+ cancelled = true;
341
+ // When end() is already waiting for settlement it sees the flag and writes the 499 row.
342
+ if (ended) return;
343
+ ended = true;
344
+ cancelInner();
345
+ await finalLog.waitForInner();
346
+ commitCancelled();
347
+ },
348
+ }, { highWaterMark: 0 }), { status: 200, headers: { "content-type": "application/x-ndjson" } });
349
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Exact Ollama-compatible paths served on the authenticated data-plane listener. Kept free of
3
+ * handler imports so listener allowlists and the package-tree fence can classify a path cheaply.
4
+ * Anything else under /api/ stays with the management API.
5
+ */
6
+ export type OllamaNativeKind = "version" | "tags" | "show" | "chat" | "generate";
7
+
8
+ export const OLLAMA_NATIVE_ROUTES: ReadonlyMap<string, { method: "GET" | "POST"; kind: OllamaNativeKind }> = new Map([
9
+ ["/api/version", { method: "GET", kind: "version" }],
10
+ ["/api/tags", { method: "GET", kind: "tags" }],
11
+ ["/api/show", { method: "POST", kind: "show" }],
12
+ ["/api/chat", { method: "POST", kind: "chat" }],
13
+ ["/api/generate", { method: "POST", kind: "generate" }],
14
+ ]);
15
+
16
+ /** Generation routes read a JSON body under maxInboundBodyBytes and take the raised-body admission. */
17
+ export const OLLAMA_JSON_BODY_ROUTES: ReadonlySet<string> = new Set(["/api/chat", "/api/generate"]);
18
+
19
+ export function isOllamaNativePath(pathname: string): boolean {
20
+ return OLLAMA_NATIVE_ROUTES.has(pathname);
21
+ }
@@ -0,0 +1,73 @@
1
+ import type { OcxConfig } from "../types";
2
+ import { captureRouteStaticPolicy, previewRouteModel, type RouteResult } from "../router";
3
+ import { POLICY_NAMESPACE, resolvePolicyProfileId } from "../routing/profile";
4
+ import { isShadowSourceModel } from "../lib/shadow-call";
5
+ import { providerConsumesCallerAuthorization } from "../providers/caller-authorization";
6
+ import { resolveOpenCodeGoTransport } from "../providers/opencode-go-transport";
7
+ import { resolveWireProtocolOverride } from "./adapter-resolve";
8
+ import { parseSyntheticRowId } from "./fast-row";
9
+ import { getOrAllocateRequestSessionLane } from "./request-log-conversation";
10
+
11
+ export { linkRequestSessionLane } from "./request-log-conversation";
12
+
13
+ export type OllamaRouteError =
14
+ | { status: 400; code: "unsupported_synthetic_selector" | "unsupported_shadow_rewrite"; message: string }
15
+ | { status: 403; code: "unsupported_credential_domain"; message: string }
16
+ | { status: 404; code: "model_not_found"; message: string };
17
+ export type OllamaRoutePreflight = { ok: true; route: RouteResult } | { ok: false; error: OllamaRouteError };
18
+
19
+ function fail(error: OllamaRouteError): OllamaRoutePreflight { return { ok: false, error }; }
20
+ const credentialError = (): OllamaRoutePreflight => fail({ status: 403, code: "unsupported_credential_domain", message: "This model requires an unsupported credential domain" });
21
+
22
+ /** Resolve exactly the Chat transport before admitting a native request. No send occurs here. */
23
+ export function preflightOllamaRoute(config: OcxConfig, selector: string, req: Request): OllamaRoutePreflight {
24
+ const { fastRow, effortRow } = parseSyntheticRowId(selector, config);
25
+ if (fastRow || effortRow) return fail({ status: 400, code: "unsupported_synthetic_selector", message: "Synthetic model selectors are unsupported" });
26
+ if (config.shadowCallIntercept?.enabled && isShadowSourceModel(selector, config.shadowCallIntercept.sourceModels)) {
27
+ return fail({ status: 400, code: "unsupported_shadow_rewrite", message: "Shadow model rewrites are unsupported" });
28
+ }
29
+ if (resolvePolicyProfileId(config, selector) !== null || selector === POLICY_NAMESPACE || selector.startsWith(`${POLICY_NAMESPACE}/`)) return credentialError();
30
+ let route: RouteResult;
31
+ try { route = previewRouteModel(config, selector); }
32
+ catch { return fail({ status: 404, code: "model_not_found", message: "Model route not found" }); }
33
+ const routedProvider = route.provider;
34
+ route.staticPolicy = captureRouteStaticPolicy(route.providerName, route.modelId, routedProvider, route.staticPolicy.effectiveAlias, "chat");
35
+ const wireProvider = resolveWireProtocolOverride(route.providerName, route.modelId, routedProvider, "chat", route.staticPolicy);
36
+ route.provider = resolveOpenCodeGoTransport(wireProvider, getOrAllocateRequestSessionLane(req), routedProvider);
37
+ if (route.combo || route.routeKind === "combo" || route.routeKind === "policy" || route.credentialDomainRewrite || providerConsumesCallerAuthorization(route.provider)) return credentialError();
38
+ if (!Object.hasOwn(config.providers, route.providerName) || config.providers[route.providerName]?.disabled === true) {
39
+ return fail({ status: 404, code: "model_not_found", message: "Model route not found" });
40
+ }
41
+ return { ok: true, route };
42
+ }
43
+
44
+ /** The configured models one enabled provider offers here; shared by the list and the point lookup. */
45
+ function providerCatalogModels(config: OcxConfig, name: string): string[] {
46
+ const provider = Object.hasOwn(config.providers, name) ? config.providers[name] : undefined;
47
+ if (!provider || provider.disabled === true) return [];
48
+ const models = new Set([...(provider.models ?? []), ...(provider.defaultModel ? [provider.defaultModel] : []),
49
+ ...(provider.retainModels ?? []), ...(provider.selectedModels ?? []),
50
+ ...(config.customModels ?? []).filter(model => model.provider === name).map(model => model.modelId)]);
51
+ return [...models].filter(model => model && !(provider.selectedModels?.length && !provider.selectedModels.includes(model)));
52
+ }
53
+
54
+ function catalogRoute(config: OcxConfig, name: string, model: string, req: Request): { selector: string; route: RouteResult } | undefined {
55
+ const selector = `${name}/${model}`;
56
+ const result = preflightOllamaRoute(config, selector, req);
57
+ return result.ok && result.route.providerName === name && result.route.modelId === model ? { selector, route: result.route } : undefined;
58
+ }
59
+
60
+ /** Configuration-only inventory; this does not imply any locally stored Ollama model. */
61
+ export function listOllamaRoutes(config: OcxConfig, req: Request): Array<{ selector: string; route: RouteResult }> {
62
+ return Object.keys(config.providers).flatMap(name =>
63
+ providerCatalogModels(config, name).flatMap(model => catalogRoute(config, name, model, req) ?? []));
64
+ }
65
+
66
+ /** The listed route for one selector, with the same eligibility as the list but one preflight. */
67
+ export function findOllamaRoute(config: OcxConfig, selector: string, req: Request): { selector: string; route: RouteResult } | undefined {
68
+ const slash = selector.indexOf("/");
69
+ if (slash <= 0) return undefined;
70
+ const name = selector.slice(0, slash);
71
+ const model = selector.slice(slash + 1);
72
+ return providerCatalogModels(config, name).includes(model) ? catalogRoute(config, name, model, req) : undefined;
73
+ }
@@ -1,3 +1,4 @@
1
+ import { carryProxyError } from "../lib/client-error-body";
1
2
  import type { ResponsesTerminalStatus } from "../bridge";
2
3
  import {
3
4
  cyberPolicyErrorType,
@@ -36,6 +37,7 @@ import {
36
37
  import { replaceSseDataPayload, splitSseBlock, sseDataPayload } from "./sse-payload-rewrite";
37
38
  import { createBoundedResponseLogBody } from "./response-log-body";
38
39
  import { clientWireLogOf } from "./inference/client-wire";
40
+ import { knownModelIdentity } from "../lib/model-identity";
39
41
  import { recordClientWireRequestLog } from "./inference/client-wire-log";
40
42
  import { nativeResponseFingerprint } from "./responses/native-response-json";
41
43
  import { nativeResponseOutput } from "./responses/native-response-output";
@@ -903,6 +905,7 @@ export function responseWithDeferredRequestLog(
903
905
  start: number,
904
906
  logCtx: RequestLogContext,
905
907
  addLog: (entry: RequestLogEntry) => void = addRequestLog,
908
+ inspectionSource?: string,
906
909
  ): Response {
907
910
  // A synthetic 429 is a transport verdict, not evidence that the provider declined the turn.
908
911
  if (isReplayRefusalResponse(response)) {
@@ -932,6 +935,7 @@ export function responseWithDeferredRequestLog(
932
935
  && preinspectedJsonResponses.has(response);
933
936
  const body = createBoundedResponseLogBody(response.body, {
934
937
  json: contentType.includes("application/json"),
938
+ ...(inspectionSource !== undefined ? { inspectionSource } : {}),
935
939
  ...(preinspectedJson ? { maxInspectionBytes: 0 } : {}),
936
940
  inspect: preinspectedJson ? () => undefined : text => inspectResponseLogJson(logCtx, text),
937
941
  finalize: reason => {
@@ -949,11 +953,11 @@ export function responseWithDeferredRequestLog(
949
953
  // Logging re-wraps the response, and an in-process verdict does not survive a re-wrap on
950
954
  // its own. A replay refusal that lost it here would read to a later quota recorder or
951
955
  // Retry-After synthesizer as a 429 some upstream produced.
952
- return carryReplayRefusal(response, new Response(body, {
956
+ return carryReplayRefusal(response, carryProxyError(response, new Response(body, {
953
957
  status: response.status,
954
958
  statusText: response.statusText,
955
959
  headers: response.headers,
956
- }));
960
+ })));
957
961
  }
958
962
  if (isUsageDebugEnabled() && logCtx.usageDebugBodyKind === undefined) {
959
963
  logCtx.usageDebugBodyKind = response.body ? "other" : "none";
@@ -1824,13 +1828,28 @@ export function codexSafetyBufferingFilterOptions(
1824
1828
  return { dropCodexSafetyBuffering: config.dropCodexSafetyBuffering === true };
1825
1829
  }
1826
1830
 
1827
- export function sanitizePassthroughHeaders(upstream: Headers, options?: CodexSafetyBufferingFilterOptions): Headers {
1831
+ /** Model ids a passthrough route knows, for the relayed model headers. */
1832
+ export interface PassthroughHeaderOptions extends CodexSafetyBufferingFilterOptions {
1833
+ knownModels?: readonly string[];
1834
+ provider?: string;
1835
+ }
1836
+
1837
+ // Upstream-reported model headers: relayed only as a model id the proxy already knows (the route's
1838
+ // own ids, the bundled Codex catalog, or the selected provider's catalog), never as upstream bytes.
1839
+ const PASSTHROUGH_MODEL_HEADERS: ReadonlySet<string> = new Set(["openai-model", "x-codex-safety-buffering-faster-model"]);
1840
+
1841
+ export function sanitizePassthroughHeaders(upstream: Headers, options?: PassthroughHeaderOptions): Headers {
1828
1842
  const dropSafetyBuffering = options?.dropCodexSafetyBuffering === true;
1829
1843
  const out = new Headers();
1830
1844
  upstream.forEach((value, key) => {
1831
1845
  const lower = key.toLowerCase();
1832
1846
  if (PASSTHROUGH_DROP_HEADERS.has(lower)) return;
1833
1847
  if (dropSafetyBuffering && CODEX_SAFETY_BUFFERING_HEADER_SET.has(lower)) return;
1848
+ if (PASSTHROUGH_MODEL_HEADERS.has(lower)) {
1849
+ const known = knownModelIdentity(value.trim(), options?.knownModels, options?.provider);
1850
+ if (known) out.set(key, known);
1851
+ return;
1852
+ }
1834
1853
  out.set(key, value);
1835
1854
  });
1836
1855
  return out;
@@ -2,6 +2,7 @@ import { Buffer } from "node:buffer";
2
2
  import { gunzipSync, inflateRawSync, inflateSync, zstdDecompressSync } from "node:zlib";
3
3
  import type { TranslatorBudget } from "../lib/translator-budget";
4
4
  import { updateInboundBodyReservation } from "./inbound-body-budget";
5
+ import { serializedJsonByteLength } from "./json-serialized-bytes";
5
6
 
6
7
  /**
7
8
  * Request-body decompression for the /v1/responses data plane.
@@ -333,8 +334,9 @@ export async function readBoundedJsonRequestBody(
333
334
  return options.emptyBodyFallback;
334
335
  }
335
336
  const parsed = JSON.parse(text);
336
- // Keep the serialized-size contract: normalization can expand numeric literals.
337
- const parsedBytes = budget ? Buffer.byteLength(JSON.stringify(parsed), "utf8") : decoded.byteLength;
337
+ // Keep the serialized-size contract: normalization can expand numeric literals. The
338
+ // measurement walks iteratively, so a deeply nested valid body cannot overflow the stack.
339
+ const parsedBytes = serializedJsonByteLength(parsed);
338
340
  budget?.observeAcceptedRequestCopy(parsedBytes);
339
341
  updateInboundBodyReservation(req, Math.max(raw.byteLength, decoded.byteLength, parsedBytes));
340
342
  return parsed;
@@ -100,3 +100,25 @@ export function attributeSealedAttempt(
100
100
  if (attribution?.stage) attempt.failureStage = attribution.stage;
101
101
  if (attribution?.cause) attempt.failureCause = attribution.cause;
102
102
  }
103
+
104
+ /**
105
+ * Re-derive the pair for the physical attempt alone, from its own status and observations.
106
+ *
107
+ * Used when a native ingress owns the request outcome separately (for example an upstream 200
108
+ * whose native conversion then failed, or a client cancel after the send succeeded): the request
109
+ * row carries the outer outcome while the attempt keeps the exchange it actually had.
110
+ */
111
+ export function attributePhysicalAttempt(attempt: PersistedUsageAttempt): void {
112
+ const attribution = deriveRequestFailureAttribution({
113
+ status: attempt.status,
114
+ ...(attempt.streamAborted === true ? { streamAborted: true } : {}),
115
+ outputObserved: attempt.firstOutputMs !== undefined,
116
+ sideEffectObserved: (attempt.deliverySummary?.sideEffectEvents ?? 0) > 0,
117
+ locallyAnswered: false,
118
+ recoveryKinds: attempt.recoveryKinds,
119
+ });
120
+ if (attribution?.stage) attempt.failureStage = attribution.stage;
121
+ else delete attempt.failureStage;
122
+ if (attribution?.cause) attempt.failureCause = attribution.cause;
123
+ else delete attempt.failureCause;
124
+ }
@@ -0,0 +1,80 @@
1
+ import { clientFacingModelText, errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
2
+ import { knownModelIdentity } from "../lib/model-identity";
3
+ /**
4
+ * Proxy-authored failure detail for request logs.
5
+ *
6
+ * Some transports describe a failure themselves (header timeout, body stall, byte cap, client close,
7
+ * a restated upstream status and closed error type). The row keeps a closed FailureDetail code with
8
+ * bounded parameters (src/lib/failure-detail.ts); the English text goes only into the context's
9
+ * upstreamError, which internal classifiers and status heuristics read and which never reaches a row.
10
+ * Only proxy-composed text may be passed here.
11
+ */
12
+ import type { FailureDetail, FailureDetailCode } from "../lib/failure-detail";
13
+
14
+ export type { FailureDetail };
15
+
16
+ export const FAILURE_DETAIL_MAX_LENGTH = 500;
17
+
18
+ /** Contexts whose upstreamError currently holds proxy text rather than upstream evidence. */
19
+ const localOnly = new WeakSet<object>();
20
+
21
+ /**
22
+ * Pass `upstreamEvidence: true` only when the text restates an upstream response (its status and a
23
+ * closed type); a local timeout, cap or cancel is not evidence of an upstream condition.
24
+ */
25
+ export function recordLocalFailureDetail(
26
+ logCtx: { upstreamError?: string; failureDetail?: FailureDetail },
27
+ text: string,
28
+ detail: FailureDetail,
29
+ options: { upstreamEvidence?: boolean } = {},
30
+ ): void {
31
+ // The classifier and status heuristics keep reading upstreamError; the row keeps the closed detail.
32
+ logCtx.upstreamError = text.slice(0, FAILURE_DETAIL_MAX_LENGTH);
33
+ logCtx.failureDetail = detail;
34
+ if (options.upstreamEvidence) localOnly.delete(logCtx);
35
+ else localOnly.add(logCtx);
36
+ }
37
+
38
+ /** True when the context's upstreamError is proxy text; the final row then carries no upstream evidence. */
39
+ export function upstreamErrorIsLocal(logCtx: object): boolean {
40
+ return localOnly.has(logCtx);
41
+ }
42
+
43
+ const INCOMPLETE_REASONS: Readonly<Record<string, readonly [string, FailureDetailCode]>> = {
44
+ max_output_tokens: ["Output reached the requested token limit (max_output_tokens)", "output_limit"],
45
+ upstream_stall_timeout: ["Upstream stalled: no data for the stall-timeout window (upstream_stall_timeout)", "stall_timeout"],
46
+ adapter_eof: ["Upstream stream ended unexpectedly without a terminal event (adapter_eof)", "adapter_eof"],
47
+ };
48
+
49
+ /**
50
+ * A structured `incomplete_details.reason` (emitted by the bridge on a stall timeout or adapter EOF):
51
+ * the context text that explains it, and a closed detail for a known reason.
52
+ */
53
+ export function incompleteReasonDetail(reason: string): { text: string; detail?: FailureDetail } {
54
+ const known = Object.prototype.hasOwnProperty.call(INCOMPLETE_REASONS, reason) ? INCOMPLETE_REASONS[reason] : undefined;
55
+ return known ? { text: known[0], detail: { code: known[1] } } : { text: `Upstream incomplete: ${reason}` };
56
+ }
57
+
58
+ /** Local model refusals keep only client or catalog identities in the request row. */
59
+ export function projectLocalModelRefusalLog(
60
+ logCtx: import("./request-log").RequestLogContext,
61
+ clientModel: import("../lib/error-destination").ClientFacingModelLabel,
62
+ ): void {
63
+ const client = clientFacingModelText(clientModel);
64
+ const known = (value: unknown) => knownModelIdentity(value, [client]);
65
+ const providerKnown = (value: string) => errorDestinationText(providerErrorDestinationLabel(value)) === value;
66
+ if (logCtx.provider && !providerKnown(logCtx.provider)) logCtx.provider = "unknown";
67
+ logCtx.model = known(logCtx.model) ?? "unknown";
68
+ logCtx.requestedModel = known(logCtx.requestedModel);
69
+ logCtx.requestedAlias = known(logCtx.requestedAlias);
70
+ logCtx.wireModel = known(logCtx.wireModel); logCtx.resolvedModel = known(logCtx.resolvedModel);
71
+ logCtx.servedModel = known(logCtx.servedModel); logCtx.responseModelEcho = known(logCtx.responseModelEcho);
72
+ for (const attempt of logCtx.attempts ?? []) {
73
+ attempt.model = known(attempt.model) ?? "unknown";
74
+ if (!providerKnown(attempt.provider)) attempt.provider = "unknown";
75
+ }
76
+ const decision = logCtx.routeDecision;
77
+ if (decision && (!known(decision.requestedModel)
78
+ || decision.candidates.some(candidate => !known(candidate.model) || !providerKnown(candidate.provider))
79
+ || decision.selected && (!known(decision.selected.model) || !providerKnown(decision.selected.provider)))) logCtx.routeDecision = undefined;
80
+ }
@@ -84,8 +84,10 @@ function noteBoundedDiagnostic(
84
84
  logCtx[field] = value;
85
85
  const status = logCtx.terminalHttpStatus;
86
86
  if (status === undefined || status >= 500) {
87
- const { upstreamErrorCode: code, upstreamRequestId: requestId } = upstreamDiagnosticLogFields(logCtx);
88
- console.warn(`[opencodex] upstream failure${status ? ` status=${status}` : ""}${code ? ` code=${code}` : ""}${requestId ? ` request_id=${requestId}` : ""}`);
87
+ // Service logs persist too: print only closed-vocabulary values, never an upstream id or code.
88
+ const { upstreamErrorCode: rawCode, upstreamErrorType: type } = upstreamDiagnosticLogFields(logCtx);
89
+ const code = rawCode !== undefined && KNOWN_BARE_ERROR_CODES.has(rawCode) ? rawCode : undefined;
90
+ console.warn(`[opencodex] upstream failure${status ? ` status=${status}` : ""}${type ? ` type=${type}` : ""}${code ? ` code=${code}` : ""}`);
89
91
  }
90
92
  }
91
93