@bitkyc08/opencodex 2.99.0 → 2.100.1
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/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-g3Cm2xhZ.js +59 -0
- package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
- package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
- package/gui/dist/assets/index-Bziu8gTy.css +2 -0
- package/gui/dist/assets/index-OYzhQUNP.js +94 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
- package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
- package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic-image-normalize.ts +1 -1
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/client-fingerprint.ts +1 -1
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +422 -170
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +56 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/claude-cli-usage.ts +3 -2
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +49 -2
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota-types.ts +7 -0
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +42 -8
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/registry/types.ts +6 -0
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/responses/retained-compaction.ts +31 -5
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/compact.ts +13 -1
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +265 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/brand-mark-wzPe3R-J.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -5,7 +5,7 @@ import { upstreamErrorIsLocal } from "./request-log-failure-detail";
|
|
|
5
5
|
import { boundedMessagesDisplayCount, ownedAnthropicErrorPayload, ownedAnthropicErrorResponse, messagesOverflowPresentation, messagesRelayedOverflowPresentation } from "../lib/messages-error-body";
|
|
6
6
|
import { attachComboFailureEvidence, isProxyError, markProxyError } from "../lib/client-error-body";
|
|
7
7
|
import { captureAccountAutoPauseAttempt, wasAccountAutoPaused } from "../oauth/account-auto-pause";
|
|
8
|
-
import { createPhysicalSendReporter } from "../lib/request-execution-budget";
|
|
8
|
+
import { createPhysicalSendReporter, reportDispatchSends, type RequestExecutionBudget, type SingleUseDispatchPermit } from "../lib/request-execution-budget";
|
|
9
9
|
import { createInferenceSendBudget } from "./inference/context";
|
|
10
10
|
/**
|
|
11
11
|
* Managed native Messages lane (PF-08, behind `protocols.rollout.managedMessagesNative`).
|
|
@@ -43,7 +43,7 @@ import {
|
|
|
43
43
|
buildAnthropicMessagesPassthroughRequest,
|
|
44
44
|
type AnthropicMessagesPassthroughRequest,
|
|
45
45
|
} from "../adapters/anthropic/passthrough";
|
|
46
|
-
import { effortFromOutputConfig, resolveInboundModel } from "../claude/inbound";
|
|
46
|
+
import { effortFromOutputConfig, nativeAnthropicProjection, resolveInboundModel } from "../claude/inbound";
|
|
47
47
|
import { anthropicErrorBody, claudeOverflowSsePayload, claudePromptTooLongMessage, collectAnthropicMessage, isContextOverflowText, isThroughputLimitText } from "../claude/outbound";
|
|
48
48
|
import type { AdmissionLease } from "../lib/admission";
|
|
49
49
|
import { readBoundedResponseBody } from "../lib/bounded-body";
|
|
@@ -60,13 +60,14 @@ import {
|
|
|
60
60
|
fetchWithResetRetry,
|
|
61
61
|
fetchWithTransientRetry,
|
|
62
62
|
isNonReplayableResponse,
|
|
63
|
-
markResponseNonReplayable,
|
|
64
63
|
isReplayRefusalResponse,
|
|
65
64
|
isTransientUpstreamStatus,
|
|
65
|
+
markResponseNonReplayable,
|
|
66
66
|
prepareSameTarget429Wait,
|
|
67
67
|
REPLAY_REFUSED_STATUS,
|
|
68
68
|
retainReplayRefusal,
|
|
69
69
|
UpstreamRetryEvidenceError,
|
|
70
|
+
TRANSIENT_RETRY_MAX_ATTEMPTS,
|
|
70
71
|
type UpstreamSendRecovery,
|
|
71
72
|
UPSTREAM_RESET_REPLAY_REFUSED_CODE,
|
|
72
73
|
} from "../lib/upstream-retry";
|
|
@@ -103,7 +104,10 @@ import {
|
|
|
103
104
|
sanitizePassthroughToolCallIds,
|
|
104
105
|
tapAnthropicSseForLog,
|
|
105
106
|
} from "./messages-shared";
|
|
106
|
-
import { beginInferenceAttempt } from "./inference/attempt";
|
|
107
|
+
import { beginInferenceAttempt, type InferenceAttempt } from "./inference/attempt";
|
|
108
|
+
import type { ComboProtocolSource } from "./responses/core-combo-native";
|
|
109
|
+
import type { ProtocolEnvelope } from "../protocols/envelope";
|
|
110
|
+
import { observeNativeMessagesReplayBoundary, preflightNativeMessagesResponse } from "./messages-native-preflight";
|
|
107
111
|
import { createFinalRequestLog, type FinalRequestLogMeta } from "./inference/final-log";
|
|
108
112
|
import { registerTurn, unregisterTurn } from "./lifecycle";
|
|
109
113
|
import { nativeMessagesDeclineReason, type NativeMessagesSelector } from "./messages-native-eligibility";
|
|
@@ -189,6 +193,8 @@ export interface HandleNativeMessagesOptions {
|
|
|
189
193
|
body: Rec;
|
|
190
194
|
/** The selector the client sent, echoed on the request log. */
|
|
191
195
|
requestedModel: string;
|
|
196
|
+
/** The model the answer reports, when it differs from `requestedModel` (a combo the route directive chose). */
|
|
197
|
+
echoModel?: string;
|
|
192
198
|
translatorBudget: TranslatorBudget;
|
|
193
199
|
/** The facts the ingress judged eligibility with; re-applied if key selection changes. */
|
|
194
200
|
selector?: NativeMessagesSelector;
|
|
@@ -202,10 +208,38 @@ export interface HandleNativeMessagesOptions {
|
|
|
202
208
|
modelScope?: AdmissionModelScope;
|
|
203
209
|
}
|
|
204
210
|
|
|
205
|
-
type
|
|
211
|
+
export type NativeMessagesFinishLog = (status: number, message?: string, meta?: FinalRequestLogMeta) => void;
|
|
212
|
+
type FinishLog = NativeMessagesFinishLog;
|
|
206
213
|
|
|
207
|
-
|
|
208
|
-
|
|
214
|
+
interface NativeMessagesExecution extends HandleNativeMessagesOptions {
|
|
215
|
+
finishLog: NativeMessagesFinishLog;
|
|
216
|
+
sendBudget?: RequestExecutionBudget;
|
|
217
|
+
comboDispatchPermit?: SingleUseDispatchPermit;
|
|
218
|
+
turnAdmissionLease?: AdmissionLease;
|
|
219
|
+
onFirstOutput?: () => void;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The combo retains routing, attempts and the final row; the native lane owns only this send. */
|
|
223
|
+
export function createNativeMessagesComboSource(input: Omit<HandleNativeMessagesOptions, "route" | "body"> & {
|
|
224
|
+
envelope: ProtocolEnvelope;
|
|
225
|
+
}): ComboProtocolSource {
|
|
226
|
+
return {
|
|
227
|
+
inbound: "messages",
|
|
228
|
+
envelope: input.envelope,
|
|
229
|
+
selector: input.selector,
|
|
230
|
+
dispatchNativeChild: child => runNativeMessagesAttempt({
|
|
231
|
+
...input, logIds: undefined, logCtx: child.childLog, route: child.route,
|
|
232
|
+
body: nativeAnthropicProjection(child.body, input.translatorBudget),
|
|
233
|
+
selector: { ...input.selector, routeSelector: String(child.body.model ?? "") },
|
|
234
|
+
finishLog: child.finishLog, sendBudget: child.sendBudget,
|
|
235
|
+
comboDispatchPermit: child.comboDispatchPermit, turnAdmissionLease: child.turnAdmissionLease,
|
|
236
|
+
onFirstOutput: child.onFirstOutput,
|
|
237
|
+
}, child.attemptHandle),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Relay an upstream body unchanged, recording first output on its first non-empty chunk once `ready`. */
|
|
242
|
+
function observeFirstChunk(body: ReadableStream<Uint8Array>, onFirst: () => void, ready = () => true): ReadableStream<Uint8Array> {
|
|
209
243
|
const reader = body.getReader();
|
|
210
244
|
let seen = false;
|
|
211
245
|
return new ReadableStream<Uint8Array>({
|
|
@@ -215,7 +249,7 @@ function observeFirstChunk(body: ReadableStream<Uint8Array>, onFirst: () => void
|
|
|
215
249
|
controller.close();
|
|
216
250
|
return;
|
|
217
251
|
}
|
|
218
|
-
if (!seen && value.byteLength > 0) {
|
|
252
|
+
if (!seen && value.byteLength > 0 && ready()) {
|
|
219
253
|
seen = true;
|
|
220
254
|
onFirst();
|
|
221
255
|
}
|
|
@@ -337,9 +371,24 @@ async function prepareNativeBody(body: Rec, signal: AbortSignal): Promise<void>
|
|
|
337
371
|
/**
|
|
338
372
|
* Handle one eligible Messages request natively. Opens the attempt, owns the request's final
|
|
339
373
|
* log row, and answers in the Messages wire (SSE or JSON per the caller's own `stream`).
|
|
374
|
+
* Deliberately not `async`: an extra await turn changes when the relayed stream is first pulled.
|
|
340
375
|
*/
|
|
341
|
-
export
|
|
342
|
-
const {
|
|
376
|
+
export function handleNativeMessages(options: HandleNativeMessagesOptions): Promise<Response> {
|
|
377
|
+
const { logCtx, logIds, route } = options;
|
|
378
|
+
const attemptHandle = beginInferenceAttempt(logCtx, { provider: route.providerName, model: route.modelId, adapter: "anthropic" });
|
|
379
|
+
const finalLog = createFinalRequestLog(logIds, logCtx);
|
|
380
|
+
const finishLog: FinishLog = (status, message, meta = { closeReason: "non_stream" }) => {
|
|
381
|
+
if (finalLog.finished()) return;
|
|
382
|
+
if (message) logCtx.upstreamError = redactSecretString(message).slice(0, 500);
|
|
383
|
+
finalLog.finish(status, meta);
|
|
384
|
+
};
|
|
385
|
+
return runNativeMessagesAttempt({ ...options, finishLog }, attemptHandle);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Execute on the ingress or combo's existing attempt, never opening a second final-row owner. */
|
|
389
|
+
async function runNativeMessagesAttempt(options: NativeMessagesExecution, attemptHandle: InferenceAttempt): Promise<Response> {
|
|
390
|
+
const { req, config, logCtx, logIds, route, body, requestedModel, translatorBudget, finishLog } = options;
|
|
391
|
+
const onFirstOutput = options.onFirstOutput ?? (logIds ? () => recordFirstOutput(logCtx, logIds.start) : undefined);
|
|
343
392
|
const nativeInstance = configuredAnthropicInstance(config, route.providerName);
|
|
344
393
|
const requestedStream = body.stream === true;
|
|
345
394
|
const clientDeclaredEmptyToolCatalog = Array.isArray(body.tools) && body.tools.length === 0;
|
|
@@ -370,20 +419,9 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
370
419
|
logCtx.spendOutputCeilingTokens = Math.trunc(body.max_tokens);
|
|
371
420
|
}
|
|
372
421
|
|
|
373
|
-
const attemptHandle = beginInferenceAttempt(logCtx, {
|
|
374
|
-
provider: route.providerName,
|
|
375
|
-
model: route.modelId,
|
|
376
|
-
adapter: "anthropic",
|
|
377
|
-
});
|
|
378
422
|
attemptHandle.seal(logCtx.accountLogLabel);
|
|
379
423
|
recordAttemptRequestedEffort(logCtx);
|
|
380
424
|
const { attempt } = attemptHandle;
|
|
381
|
-
const finalLog = createFinalRequestLog(logIds, logCtx);
|
|
382
|
-
const finishLog: FinishLog = (status, message, meta = { closeReason: "non_stream" }) => {
|
|
383
|
-
if (finalLog.finished()) return;
|
|
384
|
-
if (message) logCtx.upstreamError = redactSecretString(message).slice(0, 500);
|
|
385
|
-
finalLog.finish(status, meta);
|
|
386
|
-
};
|
|
387
425
|
const bindUsage = (usage: OcxUsage | undefined) => {
|
|
388
426
|
if (!usage) return;
|
|
389
427
|
if (!recordKeyWireAttemptUsage(logCtx, usage)) {
|
|
@@ -391,24 +429,34 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
391
429
|
attempt.usage = usage;
|
|
392
430
|
}
|
|
393
431
|
};
|
|
432
|
+
// A combo child whose preflight accepted has committed: a later failure must not replay,
|
|
433
|
+
// even when no content frame (and so no first output) was seen.
|
|
434
|
+
let committed = false;
|
|
394
435
|
const fail = (status: number, message: string, type?: string, code?: string, originalMessage = message): Response => {
|
|
395
436
|
const safeMessage = redactSecretString(message);
|
|
396
437
|
finishLog(status, safeMessage);
|
|
397
438
|
const legacy = anthropicErrorBody(status, redactSecretString(originalMessage), type, code);
|
|
398
|
-
|
|
439
|
+
const response = ownedAnthropicErrorResponse({ status, type, code, evidence: { status, message: originalMessage },
|
|
399
440
|
localError: markProxyError({ message: safeMessage }), overflow: messagesRelayedOverflowPresentation("", legacy) }, legacy);
|
|
441
|
+
if (committed) markResponseNonReplayable(response);
|
|
442
|
+
return response;
|
|
400
443
|
};
|
|
401
444
|
const failEvidence = (status: number, message: string, type: string, contract?: "transport_failed", overflow?: boolean, rawError?: Rec, legacyBody?: Rec): Response => {
|
|
402
445
|
finishLog(status, message);
|
|
403
446
|
const legacy = legacyBody ?? anthropicErrorBody(status, redactSecretString(message), type);
|
|
404
|
-
|
|
447
|
+
const response = ownedAnthropicErrorResponse({ status, type, evidence: { status, message, type: typeof rawError?.type === "string" ? rawError.type : undefined, code: typeof rawError?.code === "string" ? rawError.code : undefined }, contract,
|
|
405
448
|
overflow: overflow ?? messagesRelayedOverflowPresentation("", legacy) }, legacy);
|
|
449
|
+
if (committed) markResponseNonReplayable(response);
|
|
450
|
+
return response;
|
|
406
451
|
};
|
|
407
452
|
const preparationFailure = (error: unknown): Response => {
|
|
408
453
|
const message = error instanceof Error ? error.message : String(error);
|
|
409
454
|
finishLog(400, message);
|
|
410
455
|
return fail(400, error instanceof Error && isProxyError(error) ? error.message : "The proxy could not translate the Messages request.", "invalid_request_error", undefined, message);
|
|
411
456
|
};
|
|
457
|
+
// After preflight, first output waits for a content frame the log tap has seen.
|
|
458
|
+
const watchFirstOutput = (body: ReadableStream<Uint8Array>) => onFirstOutput
|
|
459
|
+
? observeFirstChunk(body, onFirstOutput, () => logCtx.generationStartedAt !== undefined) : body;
|
|
412
460
|
const localOAuthFailure = (error: unknown): Response | null => {
|
|
413
461
|
if (error instanceof AnthropicAccountCooldownError) {
|
|
414
462
|
const scope = error.routePosition === undefined ? "All Anthropic OAuth accounts" : "Anthropic OAuth accounts for this model route";
|
|
@@ -457,7 +505,7 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
457
505
|
const cleanupAbort = linkAbortSignal(upstream, req.signal);
|
|
458
506
|
let streamTurnRegistered = false;
|
|
459
507
|
const transferTurnToStream = () => {
|
|
460
|
-
const lease = logIds?.turnAdmissionLease;
|
|
508
|
+
const lease = options.turnAdmissionLease ?? logIds?.turnAdmissionLease;
|
|
461
509
|
if (!lease || typeof (lease as { bindAbortController?: unknown }).bindAbortController !== "function") return;
|
|
462
510
|
registerTurn(upstream, lease);
|
|
463
511
|
streamTurnRegistered = true;
|
|
@@ -503,8 +551,8 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
503
551
|
let activeProvider: OcxProviderConfig = oauthBinding ? oauthProvider(oauthBinding) : route.provider;
|
|
504
552
|
stampApiKeyAccountLabel(logCtx, route.providerName, activeProvider);
|
|
505
553
|
if (oauthBinding) logCtx.provider = formatAnthropicProviderForLog(route.providerName, oauthBinding.snapshot.accountId, config);
|
|
506
|
-
const spendTracker = attachRequestSpendTracker(req, logCtx);
|
|
507
|
-
const physicalBudget = createInferenceSendBudget(req, logCtx);
|
|
554
|
+
const spendTracker = options.sendBudget ? undefined : attachRequestSpendTracker(req, logCtx);
|
|
555
|
+
const physicalBudget = options.sendBudget ?? createInferenceSendBudget(req, logCtx);
|
|
508
556
|
// Observe-only spend is already booked by the tracker; count billing sends without booking again.
|
|
509
557
|
let physicalSendsUsed = 0;
|
|
510
558
|
let activeRequest: AnthropicMessagesPassthroughRequest;
|
|
@@ -525,7 +573,9 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
525
573
|
const rejectUnrepresentable = resolveProtocolSettings(config).unrepresentable === "reject";
|
|
526
574
|
const buildActiveRequest = () => {
|
|
527
575
|
recordAttemptCredentialSource(attempt, route.providerName, activeProvider, "anthropic");
|
|
528
|
-
|
|
576
|
+
// Exact combo replay preserves TTL-less breakpoints; an explicit operator TTL still wins.
|
|
577
|
+
const wireConfig = options.sendBudget && config.cacheRetention === undefined ? { ...config, cacheRetention: "short" as const } : config;
|
|
578
|
+
const built = buildAnthropicMessagesPassthroughRequest(activeProvider, route.modelId, body, wireConfig, {
|
|
529
579
|
callerAnthropicBeta: options.callerAnthropicBeta,
|
|
530
580
|
providerAccountUuid: oauthBinding?.providerAccountUuid,
|
|
531
581
|
clientIdentity: options.clientIdentity,
|
|
@@ -568,9 +618,13 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
568
618
|
// One inbound request owns one transient send allowance, captured before any key rotation.
|
|
569
619
|
const requestTransientPolicy = transientRetryPolicyFor(activeProvider);
|
|
570
620
|
let transientSendsUsed = 0;
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
621
|
+
const sharedSendCap = requestTransientPolicy?.attempts ?? TRANSIENT_RETRY_MAX_ATTEMPTS;
|
|
622
|
+
let initialSendStarted = false;
|
|
623
|
+
const remainingTransientSends = (): number => Math.min(
|
|
624
|
+
requestTransientPolicy ? Math.max(0, requestTransientPolicy.attempts - transientSendsUsed) : Number.POSITIVE_INFINITY,
|
|
625
|
+
options.sendBudget ? Math.min(sharedSendCap, options.sendBudget.remainingBaseSends(sharedSendCap)
|
|
626
|
+
+ (!initialSendStarted && options.comboDispatchPermit ? 1 : 0)) : Number.POSITIVE_INFINITY,
|
|
627
|
+
);
|
|
574
628
|
const transientSendAvailable = (): boolean => remainingTransientSends() > 0;
|
|
575
629
|
const selector: NativeMessagesSelector = options.selector ?? {};
|
|
576
630
|
const nativeOAuthRouteIsCurrent = (binding: NativeOAuthBinding): boolean => {
|
|
@@ -595,6 +649,7 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
595
649
|
/** Send the native Messages request under the transient retry policy, re-validating the selected OAuth account or API key at the dispatch boundary. */
|
|
596
650
|
const send = async (recovery?: "rate-limit-429" | "oauth-401" | "oauth-account-403" | "key-429" | "key-401"): Promise<Response> => {
|
|
597
651
|
const remaining = remainingTransientSends();
|
|
652
|
+
if (options.sendBudget && remaining <= 0) throw new SendBudgetExhaustedError();
|
|
598
653
|
if (requestTransientPolicy && remaining <= 0) {
|
|
599
654
|
throw markProxyError(new Error("native Messages transient send budget exhausted before recovery dispatch"));
|
|
600
655
|
}
|
|
@@ -610,6 +665,13 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
610
665
|
providerFetch(activeProvider, undefined, {
|
|
611
666
|
providerName: route.providerName,
|
|
612
667
|
modelId: route.modelId,
|
|
668
|
+
onPhysicalDispatch: options.sendBudget && options.comboDispatchPermit ? () => {
|
|
669
|
+
if (!physicalBudget.spendEnforced) {
|
|
670
|
+
// As native Chat: an observe-only start was already claimed before the send.
|
|
671
|
+
reportDispatchSends(options.sendBudget!, 1, options.comboDispatchPermit, true);
|
|
672
|
+
initialSendStarted = true;
|
|
673
|
+
}
|
|
674
|
+
} : undefined,
|
|
613
675
|
dispatchOverride: async (_input, init, execute, reacquirePacing) => {
|
|
614
676
|
if (oauthBinding) {
|
|
615
677
|
if (!nativeOAuthRouteIsCurrent(oauthBinding)) throw new NativeOAuthSelectionChangedError();
|
|
@@ -669,16 +731,22 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
669
731
|
let dispatched: Response;
|
|
670
732
|
const spendReport = createPhysicalSendReporter(physicalBudget, () => ({
|
|
671
733
|
poolId: logCtx.spendPoolId ?? route.providerName, identityId: logCtx.accountLogLabel,
|
|
672
|
-
}));
|
|
734
|
+
}), options.comboDispatchPermit);
|
|
673
735
|
let sendOwner: AnthropicPhysicalSendOwnership | null = null;
|
|
674
736
|
try {
|
|
675
737
|
if (init.signal?.aborted) throw init.signal.reason;
|
|
676
738
|
// Capture before either accounting path so this request retains its start policy.
|
|
677
739
|
if (spendReport.beforeSend && !spendReport.beforeSend()) {
|
|
678
|
-
if (spendTracker
|
|
740
|
+
if ((logCtx.spendTracker as { refusals?: number } | undefined)?.refusals) throw new NativeMessagesSpendRefusal();
|
|
679
741
|
throw new SendBudgetExhaustedError();
|
|
680
742
|
}
|
|
681
|
-
if (
|
|
743
|
+
if (options.sendBudget) {
|
|
744
|
+
// A combo child shares the request's send budget; a tracker of its own would book twice.
|
|
745
|
+
if (physicalBudget.spendEnforced) initialSendStarted = true;
|
|
746
|
+
else if (initialSendStarted && options.sendBudget.remainingBaseSends(sharedSendCap) <= 0) throw new SendBudgetExhaustedError();
|
|
747
|
+
} else if (spendTracker) {
|
|
748
|
+
if (!physicalBudget.spendEnforced && !spendTracker.charge({ workflowAlreadyCounted: true })) throw new NativeMessagesSpendRefusal();
|
|
749
|
+
}
|
|
682
750
|
noteProviderAttemptSend(logCtx, route.providerName, activeProvider, logCtx.usageLogInputTokens, transportRecovery ?? recovery);
|
|
683
751
|
// Last synchronous ownership reservation before the physical fetch. Retain this
|
|
684
752
|
// exact incarnation across its await; a returned response can never reserve one.
|
|
@@ -941,12 +1009,14 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
941
1009
|
...resolvePassthroughBodyGuard(config, upstream.signal),
|
|
942
1010
|
...(activeProvider.anthropicEofTolerance === true ? { eofTolerant: true } : {}),
|
|
943
1011
|
};
|
|
944
|
-
const
|
|
1012
|
+
const replay = { committed: false, pending: false };
|
|
1013
|
+
const raw = options.sendBudget ? observeNativeMessagesReplayBoundary(response.body, logCtx, replay) : response.body;
|
|
1014
|
+
const observed = logIds && onFirstOutput ? observeFirstChunk(raw, onFirstOutput) : raw;
|
|
945
1015
|
const renamed = activeRequest.oauthToolNames
|
|
946
1016
|
? restoreOAuthToolNamesInSse(observed, activeRequest.oauthToolNames, translatorBudget)
|
|
947
1017
|
: observed;
|
|
948
1018
|
const frames = new MessagesSseFrameContext();
|
|
949
|
-
const echoed = echoRequestedModel(renamed, requestedModel, frames);
|
|
1019
|
+
const echoed = echoRequestedModel(renamed, options.echoModel ?? requestedModel, frames);
|
|
950
1020
|
let toolRefusal: string | undefined;
|
|
951
1021
|
const source = clientDeclaredEmptyToolCatalog
|
|
952
1022
|
? guardNativeMessagesEmptyToolCatalog(echoed, translatorBudget, message => {
|
|
@@ -962,9 +1032,11 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
962
1032
|
cleanupAbort();
|
|
963
1033
|
bindUsage(logCtx.usage);
|
|
964
1034
|
finishLog(toolRefusal ? 502 : status, toolRefusal, toolRefusal ? { ...meta, terminalStatus: "failed" } : meta);
|
|
965
|
-
if (toolRefusal || meta.closeReason !== "terminal") upstream.abort();
|
|
966
1035
|
} finally {
|
|
1036
|
+
// A discarded child cancels its reader, not the shared parent turn. Detach its
|
|
1037
|
+
// controller before aborting local transport work, while the ladder remains bound.
|
|
967
1038
|
releaseStreamTurn();
|
|
1039
|
+
if (toolRefusal || meta.closeReason !== "terminal") upstream.abort();
|
|
968
1040
|
}
|
|
969
1041
|
}, bodyGuard, frames, nativeInstance ? clientFilter : undefined);
|
|
970
1042
|
// A configured Messages provider words an oversized-input refusal its own way; Claude Code
|
|
@@ -972,25 +1044,49 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
972
1044
|
// rewrite buffers whole frames, so it sits after the tap: stall detection keeps timing raw
|
|
973
1045
|
// upstream bytes. Real Anthropic pools refuse pre-stream in that wording and keep one relay.
|
|
974
1046
|
const clientStream = nativeInstance ? relayed : relaySseWithPayloadRewrite(relayed, claudeOverflowSsePayload, translatorBudget, messagesSseFrameHooks(frames), clientFilter);
|
|
975
|
-
|
|
1047
|
+
let delivered = retainUpstreamMessagesRequestId(new Response(clientStream, {
|
|
976
1048
|
status: 200,
|
|
977
1049
|
headers: { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-cache", Connection: "keep-alive" },
|
|
978
1050
|
}), response.headers);
|
|
1051
|
+
if (options.sendBudget) {
|
|
1052
|
+
try {
|
|
1053
|
+
const preflight = await preflightNativeMessagesResponse(delivered, logCtx, translatorBudget, replay);
|
|
1054
|
+
if (preflight.kind === "failed") { bindUsage(logCtx.usage); return preflight.response; }
|
|
1055
|
+
delivered = new Response(watchFirstOutput(preflight.response.body!), { status: preflight.response.status, headers: preflight.response.headers });
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
committed ||= replay.committed || replay.pending;
|
|
1058
|
+
cleanupAbort(); releaseStreamTurn(); upstream.abort();
|
|
1059
|
+
if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
|
|
1060
|
+
if (isTranslatorBudgetExceededError(error)) return fail(502, "upstream translation buffer exceeded the safe limit", "api_error", "translation_buffer_limit");
|
|
1061
|
+
return fail(502, "upstream Messages stream failed before output", "api_error");
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
return delivered;
|
|
979
1065
|
}
|
|
980
1066
|
// A non-streaming caller whose upstream streamed anyway: fold the stream into one message.
|
|
981
1067
|
const tapState: { closeReason?: FinalRequestLogMeta["closeReason"]; meta?: FinalRequestLogMeta } = {};
|
|
982
|
-
|
|
1068
|
+
let tapped = tapAnthropicSseForLog(source, logCtx, (_status, meta) => {
|
|
983
1069
|
tapState.closeReason = meta.closeReason;
|
|
984
1070
|
tapState.meta = meta;
|
|
985
1071
|
}, bodyGuard);
|
|
986
1072
|
const collectorVerdictSink: MessagesFailureVerdictSink = {};
|
|
987
1073
|
try {
|
|
988
|
-
|
|
1074
|
+
if (options.sendBudget) {
|
|
1075
|
+
const preflight = await preflightNativeMessagesResponse(new Response(tapped, { headers: response.headers }), logCtx, translatorBudget, replay);
|
|
1076
|
+
if (preflight.kind === "failed") {
|
|
1077
|
+
cleanupAbort(); upstream.abort(); bindUsage(logCtx.usage);
|
|
1078
|
+
finishLog(preflight.response.status, logCtx.upstreamError);
|
|
1079
|
+
return preflight.response;
|
|
1080
|
+
}
|
|
1081
|
+
committed = true;
|
|
1082
|
+
tapped = watchFirstOutput(preflight.response.body!);
|
|
1083
|
+
}
|
|
1084
|
+
const message = await collectAnthropicMessage(tapped, options.echoModel ?? requestedModel, translatorBudget, collectorVerdictSink);
|
|
989
1085
|
cleanupAbort();
|
|
1086
|
+
bindUsage(logCtx.usage);
|
|
990
1087
|
if (tapState.closeReason === "client_cancel" || req.signal.aborted) {
|
|
991
1088
|
return fail(499, "Client cancelled request", "api_error");
|
|
992
1089
|
}
|
|
993
|
-
bindUsage(logCtx.usage);
|
|
994
1090
|
if (message.type === "error") {
|
|
995
1091
|
const error = isRec(message.error) ? message.error : {};
|
|
996
1092
|
const text = typeof error.message === "string" ? error.message : "upstream stream failed";
|
|
@@ -1033,10 +1129,14 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
1033
1129
|
overflow: messagesRelayedOverflowPresentation(JSON.stringify(collected), collected),
|
|
1034
1130
|
evidence: { status: 200, message: typeof originalError.message === "string" ? originalError.message : undefined },
|
|
1035
1131
|
}) : message;
|
|
1036
|
-
|
|
1132
|
+
const collectedResponse = retainUpstreamMessagesRequestId(Response.json(clientMessage, { status: finalStatus }), response.headers);
|
|
1133
|
+
if (committed && finalStatus >= 400) markResponseNonReplayable(collectedResponse);
|
|
1134
|
+
return collectedResponse;
|
|
1037
1135
|
} catch (error) {
|
|
1136
|
+
committed ||= replay.committed || replay.pending;
|
|
1038
1137
|
cleanupAbort();
|
|
1039
1138
|
upstream.abort();
|
|
1139
|
+
bindUsage(logCtx.usage);
|
|
1040
1140
|
if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
|
|
1041
1141
|
if (isTranslatorBudgetExceededError(error)) {
|
|
1042
1142
|
return fail(413, "upstream translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
|
|
@@ -1083,7 +1183,7 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
1083
1183
|
return fail(502, undeclaredToolCallMessage(), "api_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
|
|
1084
1184
|
}
|
|
1085
1185
|
bindUsage(anthropicUsageToOcx(isRec(message.usage) ? message.usage : undefined));
|
|
1086
|
-
if (
|
|
1186
|
+
if (Array.isArray(message.content) && message.content.length > 0) onFirstOutput?.();
|
|
1087
1187
|
try {
|
|
1088
1188
|
translatorBudget.chargeRetained(Buffer.byteLength(read.text) * 2, { kind: "live_transient" });
|
|
1089
1189
|
} catch (error) {
|
|
@@ -1093,7 +1193,7 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
|
|
|
1093
1193
|
throw error;
|
|
1094
1194
|
}
|
|
1095
1195
|
// The client's selector, not the wire id, as the translated lane answers.
|
|
1096
|
-
message.model = requestedModel;
|
|
1196
|
+
message.model = options.echoModel ?? requestedModel;
|
|
1097
1197
|
const serialized = JSON.stringify(message);
|
|
1098
1198
|
finishLog(200);
|
|
1099
1199
|
if (requestedStream) {
|
|
@@ -28,6 +28,7 @@ import { redactSecretString } from "../lib/redact";
|
|
|
28
28
|
import { sseFieldValue } from "../lib/sse-decoder";
|
|
29
29
|
import { estimateTokens } from "../lib/token-estimate";
|
|
30
30
|
import { isTranslatorBudgetExceededError } from "../lib/translator-budget";
|
|
31
|
+
import { httpStatusFromTerminalError } from "../lib/errors";
|
|
31
32
|
import { AnthropicRequestError, isRec, type Rec } from "../claude/inbound-records";
|
|
32
33
|
import type { RequestLogContext } from "./request-log";
|
|
33
34
|
import { recordGenerationEvent } from "./request-log-generation-window";
|
|
@@ -146,6 +147,7 @@ export function tapAnthropicSseForLog(
|
|
|
146
147
|
let usageAcc: Rec = {};
|
|
147
148
|
// message_stop or an upstream error event: the turn's own terminal already went through.
|
|
148
149
|
let terminalSeen = false;
|
|
150
|
+
let errorSeen = false;
|
|
149
151
|
// stop_reason of the latest message event the client received as a complete SSE event. Any
|
|
150
152
|
// later message event (resumed content) clears it; pings and comments do not.
|
|
151
153
|
let lastDeliveredStop: string | null = null;
|
|
@@ -165,6 +167,7 @@ export function tapAnthropicSseForLog(
|
|
|
165
167
|
if (!dataLine) return;
|
|
166
168
|
let data: unknown;
|
|
167
169
|
try { data = JSON.parse(dataLine); } catch { data = null; }
|
|
170
|
+
if (errorSeen && !(isRec(data) && named(event, data, "ping"))) logCtx.streamErrorFollowed = true;
|
|
168
171
|
if (!isRec(data)) {
|
|
169
172
|
// A malformed event after a stop reason means the turn kept going and was cut.
|
|
170
173
|
if (delivered) lastDeliveredStop = null;
|
|
@@ -178,6 +181,12 @@ export function tapAnthropicSseForLog(
|
|
|
178
181
|
}
|
|
179
182
|
if (named(event, data, "message_stop") || named(event, data, "error")) {
|
|
180
183
|
terminalSeen = true;
|
|
184
|
+
if (data.type === "error") {
|
|
185
|
+
errorSeen = true;
|
|
186
|
+
const error = isRec(data.error) ? data.error : {};
|
|
187
|
+
logCtx.streamErrorStatus = httpStatusFromTerminalError({ type: typeof error.type === "string" ? error.type : undefined,
|
|
188
|
+
code: typeof error.code === "string" ? error.code : undefined, message: typeof error.message === "string" ? error.message : undefined });
|
|
189
|
+
}
|
|
181
190
|
} else if (delivered && !named(event, data, "ping")) {
|
|
182
191
|
const delta = isRec(data.delta) ? data.delta : undefined;
|
|
183
192
|
lastDeliveredStop = named(event, data, "message_delta") && typeof delta?.stop_reason === "string"
|
|
@@ -210,6 +219,9 @@ export function tapAnthropicSseForLog(
|
|
|
210
219
|
const end = text.length - (heldLf ? 1 : 0);
|
|
211
220
|
appendPart(text.slice(start, end));
|
|
212
221
|
}
|
|
222
|
+
// The relay may reframe this read: a combo child judges a retryable error by what followed it here.
|
|
223
|
+
if (errorSeen && !logCtx.streamErrorFollowed
|
|
224
|
+
&& (frameParts.length > 0 || !isHarmlessSseTail(pendingParts.join("")))) logCtx.streamErrorFollowed = true;
|
|
213
225
|
};
|
|
214
226
|
// The last block can sit in the buffer without its blank line: an upstream that stopped after
|
|
215
227
|
// it, or a held trailing CR. Count it before deciding how the turn ended (the Responses relay
|
|
@@ -312,6 +324,7 @@ export function tapAnthropicSseForLog(
|
|
|
312
324
|
settled = true;
|
|
313
325
|
idle.cancel();
|
|
314
326
|
detachAbort();
|
|
327
|
+
recordUsage();
|
|
315
328
|
finalize(499, { closeReason: "client_cancel" });
|
|
316
329
|
clientFilter?.dispose();
|
|
317
330
|
try { tapController?.close(); } catch { /* downstream already torn down */ }
|
|
@@ -457,6 +470,7 @@ export function tapAnthropicSseForLog(
|
|
|
457
470
|
settled = true;
|
|
458
471
|
idle.cancel();
|
|
459
472
|
detachAbort();
|
|
473
|
+
recordUsage();
|
|
460
474
|
finalize(499, { closeReason: "client_cancel" });
|
|
461
475
|
}
|
|
462
476
|
reader.cancel(reason).catch(() => {});
|
|
@@ -13,6 +13,7 @@ import { loadConfig } from "../config";
|
|
|
13
13
|
import { isLoopbackHostname, isWildcardHostname } from "../codex/loopback-target";
|
|
14
14
|
import { normalizeBindHostname } from "./bind-host";
|
|
15
15
|
import { isOcxCommandLine, readAlivePid, readProcessCommandLine, readRuntimePort, verifyPidIdentity } from "../config/process-state";
|
|
16
|
+
import { createServer, isIP } from "node:net";
|
|
16
17
|
import { loopbackFamilyAvailability } from "../lib/loopback-family";
|
|
17
18
|
import {
|
|
18
19
|
LOCAL_ATTESTATION_CHALLENGE_HEADER,
|
|
@@ -46,6 +47,35 @@ export interface HealthzIdentity {
|
|
|
46
47
|
|
|
47
48
|
export type EndpointLiveness = "live" | "dead" | "unknown";
|
|
48
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Whether this process can bind `hostname:port` right now. A successful bind proves no process
|
|
52
|
+
* owns that address, which no probe of it can: connecting to this machine's own non-loopback
|
|
53
|
+
* address (a Tailscale IP, a LAN IP) with nothing listening TIMES OUT on some hosts instead of
|
|
54
|
+
* being refused, and a timeout is indistinguishable from a slow owner. The listener is closed
|
|
55
|
+
* immediately. Any bind error (EADDRINUSE, EADDRNOTAVAIL, EACCES) answers false, so an address
|
|
56
|
+
* we cannot bind stays "unknown" and startup keeps refusing.
|
|
57
|
+
*/
|
|
58
|
+
const BIND_PROOF_TIMEOUT_MS = 1_500;
|
|
59
|
+
|
|
60
|
+
export function canBindAddress(hostname: string, port: number, timeoutMs = BIND_PROOF_TIMEOUT_MS): Promise<boolean> {
|
|
61
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65_535) return Promise.resolve(false);
|
|
62
|
+
return new Promise<boolean>(resolve => {
|
|
63
|
+
const server = createServer();
|
|
64
|
+
let settled = false;
|
|
65
|
+
const finish = (bound: boolean): void => {
|
|
66
|
+
if (settled) return;
|
|
67
|
+
settled = true;
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
try { server.close(); } catch { /* never bound */ }
|
|
70
|
+
resolve(bound);
|
|
71
|
+
};
|
|
72
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
73
|
+
server.once("error", () => finish(false));
|
|
74
|
+
server.once("listening", () => finish(true));
|
|
75
|
+
try { server.listen({ host: hostname, port, exclusive: true }); } catch { finish(false); }
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
49
79
|
export interface LivenessIo {
|
|
50
80
|
fetchFn?: typeof fetch;
|
|
51
81
|
readPidFn?: () => number | null;
|
|
@@ -57,6 +87,12 @@ export interface LivenessIo {
|
|
|
57
87
|
/** Fresh command-line evidence: undefined/empty is unreadable, never confirmed foreign. */
|
|
58
88
|
readCommandLineFn?: typeof readProcessCommandLine;
|
|
59
89
|
loopbackAvailabilityFn?: typeof loopbackFamilyAvailability;
|
|
90
|
+
/**
|
|
91
|
+
* Test seam for the bind proof (see `canBindAddress`): whether `hostname:port` can be bound by
|
|
92
|
+
* this process right now, which proves no process owns that address. `hostname` is the raw
|
|
93
|
+
* socket address (IPv6 unbracketed) and `timeoutMs` the remaining discovery budget.
|
|
94
|
+
*/
|
|
95
|
+
bindProofFn?: (hostname: string, port: number, timeoutMs: number) => Promise<boolean>;
|
|
60
96
|
readRuntimeFn?: (pid?: number) => { pid?: number; port: number; hostname?: string } | null;
|
|
61
97
|
configFn?: () => { port?: number; hostname?: string };
|
|
62
98
|
timeoutMs?: number;
|
|
@@ -479,19 +515,34 @@ export type ProxyLivenessObservation =
|
|
|
479
515
|
|
|
480
516
|
/** Recovery additionally covers both available loopback families and fresh PID identity. */
|
|
481
517
|
export async function observeProxyLiveness(io: LivenessIo = {}): Promise<ProxyLivenessObservation> {
|
|
482
|
-
let alivePid: number | null = null, unknown = false;
|
|
518
|
+
let alivePid: number | null = null, runtimePid: number | undefined, unknown = false;
|
|
483
519
|
const live = await discoverLiveProxy(io, {
|
|
484
520
|
pid: pid => { alivePid = pid; },
|
|
521
|
+
runtimePid: pid => { runtimePid = pid; },
|
|
485
522
|
endpoint: result => { if (result === "unknown") unknown = true; },
|
|
486
523
|
});
|
|
487
524
|
if (live) return { kind: "live", live };
|
|
488
|
-
|
|
525
|
+
const candidates = new Set<number>();
|
|
526
|
+
if (alivePid !== null) candidates.add(alivePid);
|
|
527
|
+
if (runtimePid !== undefined && Number.isSafeInteger(runtimePid) && runtimePid > 0) candidates.add(runtimePid);
|
|
528
|
+
for (const pid of candidates) {
|
|
529
|
+
if (unknown) break;
|
|
530
|
+
if (io.deadlineAt !== undefined && (io.nowFn ?? Date.now)() >= io.deadlineAt) { unknown = true; break; }
|
|
531
|
+
if (pid !== alivePid) {
|
|
532
|
+
try { process.kill(pid, 0); } catch (error) {
|
|
533
|
+
if ((error as NodeJS.ErrnoException).code === "ESRCH") continue;
|
|
534
|
+
// Permission or an indeterminate probe is not proof that the process is dead.
|
|
535
|
+
unknown = true;
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
489
539
|
// A failed verifier alone conflates foreign with unreadable. Only fresh positive
|
|
490
540
|
// evidence of a foreign command line releases a reused PID; never signal it.
|
|
491
|
-
const commandLine = (io.readCommandLineFn ?? readProcessCommandLine)(
|
|
541
|
+
const commandLine = (io.readCommandLineFn ?? readProcessCommandLine)(pid);
|
|
492
542
|
unknown = !commandLine?.trim() || isOcxCommandLine(commandLine);
|
|
493
543
|
if (io.deadlineAt !== undefined && (io.nowFn ?? Date.now)() >= io.deadlineAt) unknown = true;
|
|
494
544
|
}
|
|
545
|
+
if (io.deadlineAt !== undefined && (io.nowFn ?? Date.now)() >= io.deadlineAt) unknown = true;
|
|
495
546
|
return { kind: unknown ? "unknown" : "absent", live: null };
|
|
496
547
|
}
|
|
497
548
|
|
|
@@ -506,6 +557,7 @@ export async function findLiveProxy(io: LivenessIo = {}): Promise<LiveProxy | nu
|
|
|
506
557
|
|
|
507
558
|
async function discoverLiveProxy(io: LivenessIo, observer?: {
|
|
508
559
|
pid: (pid: number | null) => void;
|
|
560
|
+
runtimePid: (pid: number | undefined) => void;
|
|
509
561
|
endpoint: (result: EndpointLiveness) => void;
|
|
510
562
|
}): Promise<LiveProxy | null> {
|
|
511
563
|
// Prefer the cheap alive-pid check: the Windows cmdline probe (WMIC/PowerShell) is too
|
|
@@ -520,18 +572,38 @@ async function discoverLiveProxy(io: LivenessIo, observer?: {
|
|
|
520
572
|
const budgetExhausted = (): boolean =>
|
|
521
573
|
deadlineAt !== undefined && nowFn() >= deadlineAt;
|
|
522
574
|
const expired = (): null => { observer?.endpoint("unknown"); return null; };
|
|
575
|
+
// The proof stays inside the discovery deadline: it is skipped once the budget is spent, its
|
|
576
|
+
// own timeout is capped by what remains, and a proof that finishes past the deadline is
|
|
577
|
+
// discarded, so an exhausted discovery still answers "unknown". A socket takes the raw
|
|
578
|
+
// address, never the bracketed URL spelling `probeHostname` composes for IPv6.
|
|
579
|
+
const bindProof = async (hostname: string, port: number): Promise<boolean> => {
|
|
580
|
+
const remainingMs = deadlineAt === undefined ? BIND_PROOF_TIMEOUT_MS : Math.min(BIND_PROOF_TIMEOUT_MS, deadlineAt - nowFn());
|
|
581
|
+
if (remainingMs <= 0) return false;
|
|
582
|
+
const socketHost = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname;
|
|
583
|
+
// A hostname can resolve to different addresses for the probe and the bind.
|
|
584
|
+
if (isIP(socketHost) === 0) return false;
|
|
585
|
+
const bound = await (io.bindProofFn ?? canBindAddress)(socketHost, port, remainingMs);
|
|
586
|
+
return bound && !budgetExhausted();
|
|
587
|
+
};
|
|
523
588
|
const probe = async (port: number, opts: { hostname?: string; expectedPid?: number }) => {
|
|
524
589
|
// Missing/blank means an actual loopback bind, not a later config's bind scope.
|
|
525
590
|
const bindHostname = opts.hostname?.trim() || "127.0.0.1";
|
|
526
591
|
const hostname = normalizeBindHostname(opts.hostname);
|
|
527
|
-
if (!observer || (!
|
|
528
|
-
|
|
592
|
+
if (!observer || (!recoveryLoopbackFamily(hostname) && !isWildcardHostname(hostname))) {
|
|
593
|
+
// Recovery only (an observer is present): a transport miss on a specific non-loopback bind
|
|
594
|
+
// address is "unknown", and a bind proof is the one thing that turns it into "dead".
|
|
595
|
+
let verdict: EndpointLiveness | undefined;
|
|
596
|
+
const identity = await proxyIdentityAtObserved(port, opts, probeIo, observer ? (r => { verdict = r; }) : undefined);
|
|
597
|
+
if (observer && !identity && verdict !== undefined) {
|
|
598
|
+
if (verdict === "unknown" && await bindProof(hostname, port)) verdict = "dead";
|
|
599
|
+
observer.endpoint(verdict);
|
|
600
|
+
}
|
|
529
601
|
return identity ? { ...identity, hostname: bindHostname,
|
|
530
602
|
...(observer ? { probeHostname: probeHostname(opts.hostname) } : {}) } : null;
|
|
531
603
|
}
|
|
532
604
|
// Parallel families share the existing per-probe attempts/timeout and absolute
|
|
533
605
|
// deadline. A slow family cannot multiply the serial discovery budget.
|
|
534
|
-
const hosts =
|
|
606
|
+
const hosts = recoveryProbeHosts(hostname).filter(host =>
|
|
535
607
|
(io.loopbackAvailabilityFn ?? loopbackFamilyAvailability)(host) !== "unavailable");
|
|
536
608
|
const identities = await Promise.all(hosts.map(async hostname => {
|
|
537
609
|
const identity = await proxyIdentityAtObserved(port, { ...opts, hostname }, probeIo, observer.endpoint);
|
|
@@ -587,6 +659,7 @@ async function discoverLiveProxy(io: LivenessIo, observer?: {
|
|
|
587
659
|
// sibling command, manual deletion). The runtime record still says where it listens —
|
|
588
660
|
// identity-probe it so ensure/update/stop see the live proxy instead of shadowing it.
|
|
589
661
|
const record = readRuntimeFn();
|
|
662
|
+
observer?.runtimePid(record?.pid);
|
|
590
663
|
if (record?.port && record.port !== probedPort) {
|
|
591
664
|
if (budgetExhausted()) return expired();
|
|
592
665
|
const expectedPid = typeof record.pid === "number" ? record.pid : undefined;
|
|
@@ -629,6 +702,26 @@ async function discoverLiveProxy(io: LivenessIo, observer?: {
|
|
|
629
702
|
return null;
|
|
630
703
|
}
|
|
631
704
|
|
|
705
|
+
// Recovery classification is intentionally local: it must not broaden admission or bind policy.
|
|
706
|
+
function recoveryLoopbackFamily(hostname: string): 4 | 6 | null {
|
|
707
|
+
if (isLoopbackHostname(hostname)) return hostname.includes(":") ? 6 : 4;
|
|
708
|
+
const address = hostname.replace(/^\[(.*)\]$/, "$1");
|
|
709
|
+
const family = isIP(address);
|
|
710
|
+
if (family === 4) return address.startsWith("127.") ? 4 : null;
|
|
711
|
+
if (family !== 6) return null;
|
|
712
|
+
// URL serializes validated IPv6 literals canonically, including dotted IPv4-mapped forms.
|
|
713
|
+
const canonical = new URL(`http://[${address.split("%")[0]}]/`).hostname.slice(1, -1);
|
|
714
|
+
if (canonical === "::1") return 6;
|
|
715
|
+
return /^::ffff:7f[0-9a-f]{2}:[0-9a-f]{1,4}$/.test(canonical) ? 4 : null;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function recoveryProbeHosts(hostname: string): string[] {
|
|
719
|
+
if (isWildcardHostname(hostname)) return ["127.0.0.1", "[::1]"];
|
|
720
|
+
const family = recoveryLoopbackFamily(hostname);
|
|
721
|
+
if (family) return [probeHostname(hostname), family === 6 ? "127.0.0.1" : "[::1]"];
|
|
722
|
+
return [probeHostname(hostname)];
|
|
723
|
+
}
|
|
724
|
+
|
|
632
725
|
/**
|
|
633
726
|
* Loopback addresses to ask about a port whose holder must be identified before a
|
|
634
727
|
* decision that is destructive when it answers "nobody".
|