@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.
- package/bin/ocx.mjs +214 -19
- package/gui/dist/assets/{App-8yedBkhm.js → App-DuNhggE1.js} +18 -18
- package/gui/dist/assets/{Tray-erSMzxPG.js → Tray-CEw0y0jR.js} +1 -1
- package/gui/dist/assets/index-CynE_Rue.js +94 -0
- package/gui/dist/assets/{quota-summary-BK4VuQNc.js → quota-summary-VHFSlUuM.js} +1 -1
- package/gui/dist/index.html +1 -1
- package/package.json +1 -1
- package/src/adapters/command-code.ts +15 -16
- package/src/adapters/cursor/transport-retry.ts +2 -2
- package/src/adapters/devin/cloud-direct/chat.ts +15 -6
- package/src/adapters/kiro/stream.ts +63 -4
- package/src/adapters/kiro-constants.ts +15 -1
- package/src/adapters/kiro-events.ts +31 -1
- package/src/adapters/kiro-retry.ts +3 -3
- package/src/adapters/ollama-native.ts +21 -7
- package/src/adapters/openai-responses/canonical-forward.ts +50 -13
- package/src/adapters/openai-responses/empty-assistant.ts +15 -3
- package/src/adapters/openai-responses/passthrough.ts +8 -8
- package/src/adapters/openai-responses/reasoning.ts +9 -4
- package/src/adapters/openai-responses/request-strips.ts +1 -1
- package/src/adapters/openai-responses/transport.ts +17 -26
- package/src/adapters/physical-send.ts +3 -3
- package/src/adapters/qoder/adapter.ts +1 -1
- package/src/adapters/qoder/image-input.ts +5 -2
- package/src/adapters/zed-openai-stream.ts +2 -1
- package/src/bridge/errors.ts +11 -2
- package/src/bridge/response-json.ts +2 -1
- package/src/bridge/sse.ts +14 -6
- package/src/chat/failure-verdict.ts +21 -0
- package/src/chat/outbound.ts +139 -21
- package/src/claude/outbound.ts +49 -2
- package/src/cli/agent.ts +44 -15
- package/src/cli/capabilities-agents-routing.ts +9 -0
- package/src/cli/capabilities-base.ts +25 -3
- package/src/cli/capabilities-command.ts +12 -3
- package/src/cli/capability-contracts.ts +2 -0
- package/src/cli/codex-launch-auth.ts +30 -1
- package/src/cli/codex-launcher.ts +12 -5
- package/src/cli/codex-shim-autorestore.ts +5 -4
- package/src/cli/dispatch.ts +11 -176
- package/src/cli/ensure-desired-integrations.ts +4 -3
- package/src/cli/help.ts +2 -2
- package/src/cli/index.ts +9 -2
- package/src/cli/link.ts +1 -1
- package/src/cli/message-command.ts +2 -1
- package/src/cli/message-mcp-command.ts +29 -0
- package/src/cli/registry.ts +12 -4
- package/src/cli/root.ts +8 -0
- package/src/cli/sync-command.ts +243 -0
- package/src/cli/sync-fanout-process.ts +92 -0
- package/src/cli/sync-fanout.ts +150 -0
- package/src/cli/sync-flags.ts +10 -0
- package/src/cli/sync-link-status.ts +39 -0
- package/src/cli/sync-update-guard.ts +18 -0
- package/src/client/connect.ts +31 -14
- package/src/client/link-home.ts +3 -2
- package/src/client/machine-listener.ts +3 -0
- package/src/client/restore-ownership.ts +19 -1
- package/src/client/runtime-restart.ts +12 -4
- package/src/client/runtime.ts +40 -11
- package/src/client/stop-routing.ts +62 -0
- package/src/client/sync-runtime.ts +65 -0
- package/src/clients/config-export.ts +32 -3
- package/src/codex/account-store.ts +1 -0
- package/src/codex/auth-context-provider.ts +15 -0
- package/src/codex/auth-context.ts +59 -122
- package/src/codex/catalog/antigravity-discovery.ts +16 -5
- package/src/codex/catalog/provider-models.ts +6 -8
- package/src/codex/forward-transport-headers.ts +65 -36
- package/src/codex/inject/config-toml.ts +88 -0
- package/src/codex/inject/multi-agent-v2.ts +30 -4
- package/src/codex/inject/plan.ts +34 -7
- package/src/codex/inject-coordination.ts +11 -0
- package/src/codex/inject.ts +18 -7
- package/src/codex/installation-id.ts +72 -0
- package/src/codex/lineage.ts +13 -7
- package/src/codex/quota.ts +22 -6
- package/src/codex/request-owned-affinity-key.ts +11 -0
- package/src/codex/request-owned-affinity.ts +118 -0
- package/src/codex/request-owned-main-pin.ts +86 -0
- package/src/codex/role-capability.ts +52 -41
- package/src/codex/routing/active-account.ts +13 -5
- package/src/codex/routing/selection.ts +40 -1
- package/src/codex/routing/thread-affinity-store.ts +916 -0
- package/src/codex/routing/thread-affinity.ts +90 -12
- package/src/codex/toml-source-lines.ts +72 -13
- package/src/combos/failover.ts +6 -109
- package/src/combos/request.ts +2 -2
- package/src/combos/resolve.ts +29 -11
- package/src/config/process-state.ts +5 -0
- package/src/config/schema/leaf-validators.ts +1 -0
- package/src/generated/compatibility-version.json +496 -192
- package/src/grok/sync.ts +10 -1
- package/src/images/loop.ts +2 -1
- package/src/lib/client-error-body.ts +184 -0
- package/src/lib/client-retry-after.ts +66 -0
- package/src/lib/config-ownership.ts +4 -0
- package/src/lib/context-overflow-verdict.ts +113 -0
- package/src/lib/desktop-cli-handoff.d.mts +1 -1
- package/src/lib/desktop-cli-handoff.mjs +11 -1
- package/src/lib/empty-tool-catalog.ts +25 -3
- package/src/lib/error-destination.ts +47 -0
- package/src/lib/failure-detail.ts +66 -0
- package/src/lib/model-identity.ts +44 -0
- package/src/lib/private-sqlite-path.ts +124 -0
- package/src/lib/response-service-tier.ts +17 -0
- package/src/lib/retry-after.ts +3 -0
- package/src/lib/spend-ledger-owner.ts +43 -4
- package/src/lib/upstream-condition.ts +142 -0
- package/src/lib/upstream-retry.ts +14 -11
- package/src/lib/windows-owner-acl.ts +13 -6
- package/src/lib/windows-secret-acl.ts +2 -2
- package/src/link/remote-platform.ts +14 -0
- package/src/link/remote-windows.ts +51 -9
- package/src/link/ssh-argv.ts +63 -10
- package/src/messaging/mcp-queue-rpc.ts +102 -0
- package/src/messaging/mcp-queue-server.ts +42 -0
- package/src/messaging/mcp-queue-tools.ts +39 -0
- package/src/oauth/anthropic-identity.ts +2 -6
- package/src/oauth/generic-account-failover.ts +13 -11
- package/src/oauth/index.ts +41 -74
- package/src/oauth/oauth-errors.ts +67 -0
- package/src/oauth/store.ts +1 -1
- package/src/protocols/codecs/ollama.ts +197 -0
- package/src/protocols/encoders/adapter-events.ts +4 -0
- package/src/protocols/encoders/chat.ts +18 -10
- package/src/protocols/trace.ts +18 -0
- package/src/providers/anthropic-subscription.ts +25 -0
- package/src/providers/claude-cli-usage.ts +15 -67
- package/src/providers/fastwire.ts +3 -2
- package/src/providers/quota/anthropic-decision-windows.ts +5 -19
- package/src/providers/quota/anthropic-usage-windows.ts +65 -0
- package/src/providers/quota/vendor-probes-key.ts +60 -24
- package/src/providers/request-pacing.ts +3 -2
- package/src/responses/apply-patch-envelope.ts +45 -19
- package/src/responses/compaction-history-framing.ts +5 -1
- package/src/responses/freeform-wrapper-scan.ts +5 -3
- package/src/responses/parser-tools.ts +1 -0
- package/src/responses/progressive-freeform-input.ts +354 -45
- package/src/responses/schema.ts +5 -2
- package/src/routing/fallback-policy.ts +3 -1
- package/src/server/admission-model-scope.ts +17 -6
- package/src/server/admitted-http-turn.ts +56 -0
- package/src/server/audio-dictation.ts +3 -0
- package/src/server/audio-live.ts +8 -2
- package/src/server/audio-upstream.ts +2 -0
- package/src/server/auth-cors.ts +5 -0
- package/src/server/chat-completions.ts +45 -20
- package/src/server/chat-native-sse.ts +52 -7
- package/src/server/chat-native.ts +60 -35
- package/src/server/claude-bridge-pool-session.ts +59 -0
- package/src/server/claude-messages.ts +57 -27
- package/src/server/context-history.ts +14 -7
- package/src/server/images.ts +14 -5
- package/src/server/inbound-body-admission.ts +11 -4
- package/src/server/index/live-sideband.ts +16 -2
- package/src/server/index/ollama-routes.ts +89 -0
- package/src/server/index/serve-options.ts +22 -4
- package/src/server/index/websocket-handler.ts +5 -3
- package/src/server/index.ts +6 -36
- package/src/server/inference/client-encoder-delivery.ts +4 -2
- package/src/server/json-serialized-bytes.ts +44 -0
- package/src/server/live.ts +30 -5
- package/src/server/management/agent-settings-routes.ts +24 -21
- package/src/server/management/codex-cli-update-routes.ts +2 -1
- package/src/server/management/route-registry.ts +1 -1
- package/src/server/messages-native-scope.ts +2 -1
- package/src/server/messages-native-tool-guard.ts +1 -1
- package/src/server/messages-native.ts +12 -7
- package/src/server/messages-shared.ts +6 -2
- package/src/server/native-final-log.ts +93 -0
- package/src/server/ollama-inbound.ts +349 -0
- package/src/server/ollama-native-paths.ts +21 -0
- package/src/server/ollama-route-policy.ts +73 -0
- package/src/server/relay.ts +22 -3
- package/src/server/request-decompress.ts +4 -2
- package/src/server/request-log-failure-attribution.ts +22 -0
- package/src/server/request-log-failure-detail.ts +80 -0
- package/src/server/request-log-terminal-status.ts +4 -2
- package/src/server/request-log.ts +79 -42
- package/src/server/response-log-body.ts +6 -1
- package/src/server/responses/account-change-state.ts +17 -6
- package/src/server/responses/adapter-continuation.ts +9 -4
- package/src/server/responses/adapter-delivery.ts +5 -4
- package/src/server/responses/adapter-dispatch.ts +36 -30
- package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
- package/src/server/responses/codex-auth-error.ts +22 -17
- package/src/server/responses/codex-ws-exchange.ts +11 -8
- package/src/server/responses/codex-ws-metadata.ts +94 -21
- package/src/server/responses/codex-ws-request.ts +3 -1
- package/src/server/responses/combo-required-tool-contract.ts +24 -12
- package/src/server/responses/compact.ts +89 -41
- package/src/server/responses/compaction-recovery.ts +4 -2
- package/src/server/responses/compaction-routing.ts +9 -4
- package/src/server/responses/core-auth.ts +32 -14
- package/src/server/responses/core-codex-account.ts +34 -12
- package/src/server/responses/core-combo-failure.ts +5 -1
- package/src/server/responses/core-combo-native.ts +2 -1
- package/src/server/responses/core-combo.ts +51 -33
- package/src/server/responses/core-errors.ts +22 -20
- package/src/server/responses/core-lifetime.ts +6 -3
- package/src/server/responses/core-normalize.ts +8 -1
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-pipeline.ts +4 -3
- package/src/server/responses/fetch-helpers.ts +7 -1
- package/src/server/responses/input-admission.ts +28 -2
- package/src/server/responses/native-injection.ts +1 -1
- package/src/server/responses/native-response-json.ts +13 -4
- package/src/server/responses/native-response-output.ts +1 -2
- package/src/server/responses/non-replayable-error.ts +2 -1
- package/src/server/responses/pacing-overload.ts +3 -2
- package/src/server/responses/passthrough-build-error.ts +36 -0
- package/src/server/responses/passthrough-delivery.ts +55 -14
- package/src/server/responses/passthrough-dispatch.ts +26 -39
- package/src/server/responses/passthrough-error.ts +9 -9
- package/src/server/responses/policy-fallback.ts +2 -1
- package/src/server/responses/request-prepare.ts +67 -48
- package/src/server/responses/request-sidecar-auth.ts +3 -1
- package/src/server/responses/request-transport.ts +35 -31
- package/src/server/responses/run-turn-execution.ts +6 -6
- package/src/server/responses/sidecar-execution.ts +16 -12
- package/src/server/responses/spend-storage-error.ts +2 -1
- package/src/server/responses/ws-upstream.ts +9 -0
- package/src/server/responses-custom-tool-repair.ts +14 -3
- package/src/server/responses-terminal-repair.ts +5 -2
- package/src/server/responses-undeclared-tool-guard.ts +39 -16
- package/src/server/safe-response-headers.ts +162 -13
- package/src/server/search.ts +13 -4
- package/src/server/turn-state-handles.ts +164 -0
- package/src/server/workflow-refusal.ts +2 -1
- package/src/server/ws-bridge.ts +5 -2
- package/src/service/ownership-mutation-lease.d.mts +11 -2
- package/src/service/ownership-mutation-lease.mjs +53 -11
- package/src/types/config.ts +2 -0
- package/src/update/index.ts +133 -25
- package/src/update/npm-install.d.mts +8 -0
- package/src/update/npm-install.mjs +20 -1
- package/src/update/orchestrated-admission.d.mts +4 -0
- package/src/update/orchestrated-admission.mjs +12 -0
- package/src/update/orchestrated-arguments.d.mts +12 -0
- package/src/update/orchestrated-arguments.mjs +64 -0
- package/src/update/orchestrated-child.d.mts +12 -0
- package/src/update/orchestrated-child.mjs +70 -0
- package/src/update/orchestrated-entry.ts +234 -0
- package/src/update/orchestrated-identity-probe-entry.ts +20 -0
- package/src/update/orchestrated-identity.ts +53 -0
- package/src/update/orchestrated-live-identity.ts +48 -0
- package/src/update/orchestrated-node-driver.mjs +99 -0
- package/src/update/orchestrated-outcome.d.mts +21 -0
- package/src/update/orchestrated-outcome.mjs +40 -0
- package/src/update/orchestrated-plan.ts +49 -0
- package/src/update/orchestrated-pnpm.d.mts +8 -0
- package/src/update/orchestrated-pnpm.mjs +33 -0
- package/src/update/orchestrated-session.d.mts +37 -0
- package/src/update/orchestrated-session.mjs +264 -0
- package/src/update/orchestrated-token.d.mts +10 -0
- package/src/update/orchestrated-token.mjs +21 -0
- package/src/update/orchestrated-token.ts +3 -0
- package/src/update/orchestrated-tree.d.mts +1 -0
- package/src/update/orchestrated-tree.mjs +14 -0
- package/src/update/pnpm-global-install.d.mts +6 -0
- package/src/update/pnpm-global-install.mjs +9 -2
- package/src/update/posix-install-supervisor-entry.mjs +6 -0
- package/src/update/posix-install-supervisor.d.mts +10 -0
- package/src/update/posix-install-supervisor.mjs +88 -0
- package/src/update/sync-update-mutex.d.mts +10 -0
- package/src/update/sync-update-mutex.mjs +164 -0
- package/src/update/sync-update-mutex.ts +1 -0
- package/src/update/test-home-guard.mjs +33 -0
- package/src/update/transactional-install.mjs +2 -2
- package/src/usage/error-detail.ts +225 -0
- package/src/usage/log.ts +60 -27
- package/src/vision/describe.ts +4 -7
- package/src/web-search/executor.ts +4 -7
- package/src/web-search/loop.ts +31 -5
- 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
|
-
|
|
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
|
-
|
|
138
|
+
headers = mergeCodexSelectedHeaders(candidate.provider.headers, materializeCodexUpstreamAuth(req.headers, authContext, {
|
|
135
139
|
config, modelId: "context_history", admission, substituteMainCredential,
|
|
136
|
-
}))
|
|
137
|
-
|
|
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(
|
|
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
|
|
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.
|
package/src/server/images.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
:
|
|
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
|
-
|
|
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
|
-
|
|
1974
|
-
|
|
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
|
-
|
|
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") {
|
package/src/server/index.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
+
}
|