omnicell-router 0.8.4 → 0.10.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/README.md +26 -7
- package/README.zh-CN.md +18 -5
- package/backend/dist/capture/recorder.d.ts +4 -10
- package/backend/dist/capture/recorder.js +6 -58
- package/backend/dist/cli/clients/base.d.ts +36 -11
- package/backend/dist/cli/clients/base.js +31 -15
- package/backend/dist/cli/clients/claude.d.ts +9 -0
- package/backend/dist/cli/clients/claude.js +6 -0
- package/backend/dist/cli/clients/codex.d.ts +8 -0
- package/backend/dist/cli/clients/codex.js +5 -0
- package/backend/dist/cli/clients/core-adapters.d.ts +1 -1
- package/backend/dist/cli/clients/core-adapters.js +1 -1
- package/backend/dist/cli/clients/dsh.d.ts +8 -0
- package/backend/dist/cli/clients/dsh.js +5 -0
- package/backend/dist/cli/clients/grok.d.ts +15 -1
- package/backend/dist/cli/clients/grok.js +16 -2
- package/backend/dist/cli/clients/index.d.ts +13 -1
- package/backend/dist/cli/clients/index.js +97 -19
- package/backend/dist/cli/clients/kimi.d.ts +6 -0
- package/backend/dist/cli/clients/kimi.js +3 -0
- package/backend/dist/cli/clients/launcher.d.ts +1 -1
- package/backend/dist/cli/clients/model-meta.js +6 -3
- package/backend/dist/cli/clients/omp.d.ts +6 -0
- package/backend/dist/cli/clients/omp.js +3 -0
- package/backend/dist/cli/clients/openclaw.d.ts +6 -0
- package/backend/dist/cli/clients/openclaw.js +3 -0
- package/backend/dist/cli/clients/opencode.d.ts +8 -0
- package/backend/dist/cli/clients/opencode.js +5 -0
- package/backend/dist/cli/clients/pi.d.ts +6 -0
- package/backend/dist/cli/clients/pi.js +3 -0
- package/backend/dist/cli/clients/update-check.d.ts +37 -0
- package/backend/dist/cli/clients/update-check.js +309 -0
- package/backend/dist/cli/clients/workbuddy.d.ts +19 -0
- package/backend/dist/cli/clients/workbuddy.js +66 -0
- package/backend/dist/cli/index.js +5 -4
- package/backend/dist/cli/paths.js +8 -0
- package/backend/dist/cli/types.d.ts +92 -16
- package/backend/dist/config/comments-en.js +31 -26
- package/backend/dist/config/comments-zh.js +29 -24
- package/backend/dist/config/defaults.d.ts +194 -33
- package/backend/dist/config/defaults.js +193 -32
- package/backend/dist/config/env.d.ts +10 -0
- package/backend/dist/config/env.js +3 -2
- package/backend/dist/config/index.d.ts +7 -0
- package/backend/dist/config/index.js +184 -43
- package/backend/dist/config/migrations/v0-to-v1.d.ts +2 -2
- package/backend/dist/config/migrations/v10-to-v11.d.ts +38 -0
- package/backend/dist/config/migrations/v10-to-v11.js +43 -0
- package/backend/dist/config/migrations/v6-to-v7.d.ts +45 -0
- package/backend/dist/config/migrations/v6-to-v7.js +97 -0
- package/backend/dist/config/migrations/v7-to-v8.d.ts +36 -0
- package/backend/dist/config/migrations/v7-to-v8.js +43 -0
- package/backend/dist/config/migrations/v8-to-v9.d.ts +38 -0
- package/backend/dist/config/migrations/v8-to-v9.js +43 -0
- package/backend/dist/config/migrations/v9-to-v10.d.ts +51 -0
- package/backend/dist/config/migrations/v9-to-v10.js +71 -0
- package/backend/dist/config/runtime.js +1 -1
- package/backend/dist/config/schema.d.ts +3 -3
- package/backend/dist/config/schema.js +14 -4
- package/backend/dist/config/types.d.ts +167 -38
- package/backend/dist/egress-proxy/asset-download.d.ts +54 -0
- package/backend/dist/egress-proxy/asset-download.js +420 -0
- package/backend/dist/egress-proxy/manual-node-spec.d.ts +92 -0
- package/backend/dist/egress-proxy/manual-node-spec.js +408 -0
- package/backend/dist/egress-proxy/mihomo-binary.d.ts +54 -0
- package/backend/dist/egress-proxy/mihomo-binary.js +278 -0
- package/backend/dist/egress-proxy/mihomo-client.d.ts +94 -0
- package/backend/dist/egress-proxy/mihomo-client.js +308 -0
- package/backend/dist/egress-proxy/mihomo-config.d.ts +40 -0
- package/backend/dist/egress-proxy/mihomo-config.js +161 -0
- package/backend/dist/egress-proxy/mihomo-geo.d.ts +87 -0
- package/backend/dist/egress-proxy/mihomo-geo.js +213 -0
- package/backend/dist/egress-proxy/mihomo-logs.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-logs.js +84 -0
- package/backend/dist/egress-proxy/mihomo-pac.d.ts +37 -0
- package/backend/dist/egress-proxy/mihomo-pac.js +75 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.js +193 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.d.ts +81 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.js +637 -0
- package/backend/dist/forward-proxy/egress.d.ts +50 -11
- package/backend/dist/forward-proxy/egress.js +50 -17
- package/backend/dist/forward-proxy/h2-forward.js +36 -10
- package/backend/dist/forward-proxy/index.d.ts +11 -8
- package/backend/dist/forward-proxy/index.js +22 -60
- package/backend/dist/forward-proxy/interceptor.d.ts +3 -2
- package/backend/dist/forward-proxy/interceptor.js +4 -4
- package/backend/dist/forward-proxy/relay.js +3 -3
- package/backend/dist/forward-proxy/rules.d.ts +4 -4
- package/backend/dist/forward-proxy/rules.js +25 -4
- package/backend/dist/forward-proxy/server.d.ts +0 -16
- package/backend/dist/forward-proxy/server.js +124 -156
- package/backend/dist/harness/host/hosts.js +3 -2
- package/backend/dist/index.js +53 -18
- package/backend/dist/ingress-proxy/access-log.d.ts +176 -0
- package/backend/dist/ingress-proxy/access-log.js +774 -0
- package/backend/dist/ingress-proxy/ca-inventory.d.ts +106 -0
- package/backend/dist/ingress-proxy/ca-inventory.js +525 -0
- package/backend/dist/ingress-proxy/ca-trust.d.ts +96 -0
- package/backend/dist/ingress-proxy/ca-trust.js +315 -0
- package/backend/dist/ingress-proxy/ca.d.ts +104 -0
- package/backend/dist/ingress-proxy/ca.js +273 -0
- package/backend/dist/ingress-proxy/data-dir.d.ts +28 -0
- package/backend/dist/ingress-proxy/data-dir.js +45 -0
- package/backend/dist/ingress-proxy/egress.d.ts +151 -0
- package/backend/dist/ingress-proxy/egress.js +364 -0
- package/backend/dist/ingress-proxy/h2-forward.d.ts +66 -0
- package/backend/dist/ingress-proxy/h2-forward.js +561 -0
- package/backend/dist/ingress-proxy/index.d.ts +24 -0
- package/backend/dist/ingress-proxy/index.js +144 -0
- package/backend/dist/ingress-proxy/interceptor.d.ts +25 -0
- package/backend/dist/ingress-proxy/interceptor.js +52 -0
- package/backend/dist/ingress-proxy/ordered-relay.d.ts +29 -0
- package/backend/dist/ingress-proxy/ordered-relay.js +46 -0
- package/backend/dist/ingress-proxy/relay.d.ts +54 -0
- package/backend/dist/ingress-proxy/relay.js +188 -0
- package/backend/dist/ingress-proxy/rules.d.ts +195 -0
- package/backend/dist/ingress-proxy/rules.js +348 -0
- package/backend/dist/ingress-proxy/server.d.ts +153 -0
- package/backend/dist/ingress-proxy/server.js +2065 -0
- package/backend/dist/ingress-proxy/sni.d.ts +35 -0
- package/backend/dist/ingress-proxy/sni.js +124 -0
- package/backend/dist/ingress-proxy/status.d.ts +32 -0
- package/backend/dist/ingress-proxy/status.js +15 -0
- package/backend/dist/ingress-proxy/system-proxy.d.ts +112 -0
- package/backend/dist/ingress-proxy/system-proxy.js +481 -0
- package/backend/dist/ingress-proxy/traffic-delete.d.ts +21 -0
- package/backend/dist/ingress-proxy/traffic-delete.js +64 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.d.ts +44 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.js +228 -0
- package/backend/dist/ingress-proxy/traffic-files.d.ts +10 -0
- package/backend/dist/ingress-proxy/traffic-files.js +10 -0
- package/backend/dist/ingress-proxy/upstream-tls.d.ts +46 -0
- package/backend/dist/ingress-proxy/upstream-tls.js +105 -0
- package/backend/dist/ingress-proxy/ws-frames.d.ts +45 -0
- package/backend/dist/ingress-proxy/ws-frames.js +153 -0
- package/backend/dist/ir/adapters/anthropic-messages-stream.js +6 -1
- package/backend/dist/ir/adapters/anthropic-messages.js +12 -5
- package/backend/dist/ir/adapters/google-gemini-stream.js +7 -2
- package/backend/dist/ir/adapters/google-gemini.js +5 -2
- package/backend/dist/ir/adapters/openai-chat-stream.js +5 -1
- package/backend/dist/ir/adapters/openai-chat.d.ts +25 -0
- package/backend/dist/ir/adapters/openai-chat.js +45 -3
- package/backend/dist/ir/adapters/openai-responses-output.d.ts +1 -0
- package/backend/dist/ir/adapters/openai-responses-output.js +67 -21
- package/backend/dist/ir/adapters/openai-responses-stream.d.ts +13 -0
- package/backend/dist/ir/adapters/openai-responses-stream.js +34 -1
- package/backend/dist/ir/adapters/openai-responses.d.ts +82 -24
- package/backend/dist/ir/adapters/openai-responses.js +146 -37
- package/backend/dist/ir/media.d.ts +20 -0
- package/backend/dist/ir/media.js +41 -0
- package/backend/dist/ir/tool-identity.d.ts +35 -0
- package/backend/dist/ir/tool-identity.js +40 -0
- package/backend/dist/ir/types.d.ts +9 -1
- package/backend/dist/local/build-jobs.d.ts +66 -0
- package/backend/dist/local/build-jobs.js +243 -0
- package/backend/dist/local/detect.d.ts +32 -0
- package/backend/dist/local/detect.js +145 -0
- package/backend/dist/local/draft-match.d.ts +58 -0
- package/backend/dist/local/draft-match.js +124 -0
- package/backend/dist/local/engine-runtime.d.ts +522 -0
- package/backend/dist/local/engine-runtime.js +1 -0
- package/backend/dist/local/engines.d.ts +241 -0
- package/backend/dist/local/engines.js +651 -0
- package/backend/dist/local/gguf.d.ts +35 -0
- package/backend/dist/local/gguf.js +314 -0
- package/backend/dist/local/hf.d.ts +120 -0
- package/backend/dist/local/hf.js +304 -0
- package/backend/dist/local/install.d.ts +90 -0
- package/backend/dist/local/install.js +421 -0
- package/backend/dist/local/llamacpp.d.ts +55 -0
- package/backend/dist/local/llamacpp.js +177 -0
- package/backend/dist/local/local-provider.d.ts +81 -0
- package/backend/dist/local/local-provider.js +301 -0
- package/backend/dist/local/machine-stats.d.ts +87 -0
- package/backend/dist/local/machine-stats.js +332 -0
- package/backend/dist/local/model-store.d.ts +347 -0
- package/backend/dist/local/model-store.js +1215 -0
- package/backend/dist/local/part-sweep.d.ts +32 -0
- package/backend/dist/local/part-sweep.js +156 -0
- package/backend/dist/local/plan.d.ts +65 -0
- package/backend/dist/local/plan.js +146 -0
- package/backend/dist/local/resources.d.ts +49 -0
- package/backend/dist/local/resources.js +64 -0
- package/backend/dist/local/run-stats.d.ts +95 -0
- package/backend/dist/local/run-stats.js +195 -0
- package/backend/dist/local/runner.d.ts +235 -0
- package/backend/dist/local/runner.js +1392 -0
- package/backend/dist/local/settings.d.ts +79 -0
- package/backend/dist/local/settings.js +180 -0
- package/backend/dist/local/strata-catalog.d.ts +30 -0
- package/backend/dist/local/strata-catalog.js +511 -0
- package/backend/dist/local/strata-drafting.d.ts +46 -0
- package/backend/dist/local/strata-drafting.js +265 -0
- package/backend/dist/local/strata.d.ts +278 -0
- package/backend/dist/local/strata.js +1392 -0
- package/backend/dist/local/tar.d.ts +20 -0
- package/backend/dist/local/tar.js +367 -0
- package/backend/dist/local/transport.d.ts +35 -0
- package/backend/dist/local/transport.js +89 -0
- package/backend/dist/local/tune.d.ts +58 -0
- package/backend/dist/local/tune.js +324 -0
- package/backend/dist/observability/http-exchange.d.ts +2 -2
- package/backend/dist/opencode/catalog/auto-pull.d.ts +14 -3
- package/backend/dist/opencode/catalog/auto-pull.js +276 -70
- package/backend/dist/opencode/catalog/ocr-store.js +3 -1
- package/backend/dist/opencode/catalog/overrides-store.d.ts +10 -1
- package/backend/dist/opencode/catalog/overrides-store.js +6 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.d.ts +35 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.js +66 -0
- package/backend/dist/opencode/catalog/repository.js +25 -2
- package/backend/dist/opencode/catalog/sources/models-dev.js +13 -3
- package/backend/dist/opencode/catalog/types.d.ts +35 -3
- package/backend/dist/opencode/catalog/types.js +38 -1
- package/backend/dist/opencode/credentials/ocr-store.js +11 -2
- package/backend/dist/opencode/credentials/sync.js +12 -3
- package/backend/dist/opencode/credentials/types.d.ts +31 -4
- package/backend/dist/opencode/credentials/types.js +25 -0
- package/backend/dist/opencode/probe.d.ts +21 -4
- package/backend/dist/opencode/probe.js +34 -10
- package/backend/dist/pipeline/directives.d.ts +9 -4
- package/backend/dist/pipeline/directives.js +14 -10
- package/backend/dist/pipeline/orchestrator.d.ts +42 -13
- package/backend/dist/pipeline/orchestrator.js +497 -165
- package/backend/dist/plugins/environment-ack.d.ts +51 -0
- package/backend/dist/plugins/environment-ack.js +81 -0
- package/backend/dist/plugins/gateways.d.ts +6 -6
- package/backend/dist/plugins/gateways.js +2 -2
- package/backend/dist/plugins/host.d.ts +98 -14
- package/backend/dist/plugins/host.js +622 -87
- package/backend/dist/plugins/install-run.d.ts +27 -0
- package/backend/dist/plugins/install-run.js +99 -0
- package/backend/dist/plugins/manifest.d.ts +84 -32
- package/backend/dist/plugins/manifest.js +149 -45
- package/backend/dist/plugins/market-install.d.ts +18 -0
- package/backend/dist/plugins/market-install.js +111 -0
- package/backend/dist/plugins/market-source.d.ts +100 -0
- package/backend/dist/plugins/market-source.js +449 -0
- package/backend/dist/plugins/market-updates.d.ts +70 -0
- package/backend/dist/plugins/market-updates.js +97 -0
- package/backend/dist/plugins/market.d.ts +677 -0
- package/backend/dist/plugins/market.js +2113 -0
- package/backend/dist/plugins/source-index.d.ts +155 -0
- package/backend/dist/plugins/source-index.js +217 -0
- package/backend/dist/plugins/types.d.ts +402 -37
- package/backend/dist/projects/worktrees.d.ts +88 -0
- package/backend/dist/projects/worktrees.js +220 -0
- package/backend/dist/providers/boot-direct.d.ts +41 -7
- package/backend/dist/providers/boot-direct.js +252 -36
- package/backend/dist/providers/compat/tool-markup-recovery.js +31 -6
- package/backend/dist/providers/executors/dispatch.d.ts +17 -7
- package/backend/dist/providers/executors/dispatch.js +28 -9
- package/backend/dist/providers/executors/openai-chat.js +14 -2
- package/backend/dist/providers/executors/plugin-adapter.d.ts +23 -1
- package/backend/dist/providers/executors/plugin-adapter.js +59 -3
- package/backend/dist/providers/member-match.d.ts +32 -9
- package/backend/dist/providers/member-match.js +65 -21
- package/backend/dist/providers/model-resolution.d.ts +62 -0
- package/backend/dist/providers/model-resolution.js +62 -0
- package/backend/dist/providers/price-order.d.ts +51 -0
- package/backend/dist/providers/price-order.js +54 -0
- package/backend/dist/providers/provider-takedown.d.ts +5 -2
- package/backend/dist/providers/registry.d.ts +84 -25
- package/backend/dist/providers/registry.js +209 -114
- package/backend/dist/providers/selection.d.ts +91 -0
- package/backend/dist/providers/selection.js +158 -0
- package/backend/dist/providers/virtual-meta.d.ts +33 -2
- package/backend/dist/providers/virtual-meta.js +74 -18
- package/backend/dist/quota/cli-login.d.ts +25 -4
- package/backend/dist/quota/cli-login.js +35 -5
- package/backend/dist/quota/coverage-generation.d.ts +24 -0
- package/backend/dist/quota/coverage-generation.js +29 -0
- package/backend/dist/quota/declarations.js +8 -4
- package/backend/dist/quota/engine.js +2 -2
- package/backend/dist/quota/prober.d.ts +43 -2
- package/backend/dist/quota/prober.js +97 -22
- package/backend/dist/ratelimit/rate-limiter.d.ts +1 -1
- package/backend/dist/resilience/error-classifier.js +29 -0
- package/backend/dist/router/index.d.ts +1 -1
- package/backend/dist/router/index.js +17 -1
- package/backend/dist/router/layer2-decision.d.ts +1 -1
- package/backend/dist/router/layer2-decision.js +2 -2
- package/backend/dist/routes/console.js +928 -310
- package/backend/dist/routes/environments.js +1 -1
- package/backend/dist/routes/local-models.d.ts +2 -0
- package/backend/dist/routes/local-models.js +1414 -0
- package/backend/dist/routes/mihomo.d.ts +2 -2
- package/backend/dist/routes/mihomo.js +20 -11
- package/backend/dist/routes/projects.js +121 -0
- package/backend/dist/server.js +318 -13
- package/backend/dist/session/session-manager.d.ts +42 -5
- package/backend/dist/session/session-manager.js +64 -11
- package/backend/dist/session/session-row.d.ts +29 -8
- package/backend/dist/session/session-row.js +18 -13
- package/backend/dist/snapshot/bundle.d.ts +40 -0
- package/backend/dist/snapshot/bundle.js +177 -0
- package/backend/dist/snapshot/handoff/compress.d.ts +46 -0
- package/backend/dist/snapshot/handoff/compress.js +77 -0
- package/backend/dist/snapshot/handoff/index.d.ts +119 -0
- package/backend/dist/snapshot/handoff/index.js +160 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.d.ts +78 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.js +175 -0
- package/backend/dist/snapshot/handoff/opencode-writer.d.ts +51 -0
- package/backend/dist/snapshot/handoff/opencode-writer.js +214 -0
- package/backend/dist/snapshot/handoff/summarize.d.ts +39 -0
- package/backend/dist/snapshot/handoff/summarize.js +118 -0
- package/backend/dist/snapshot/handoff/transcript.d.ts +71 -0
- package/backend/dist/snapshot/handoff/transcript.js +167 -0
- package/backend/dist/snapshot/media-fs.d.ts +10 -0
- package/backend/dist/snapshot/media-fs.js +50 -0
- package/backend/dist/snapshot/media.d.ts +32 -0
- package/backend/dist/snapshot/media.js +87 -0
- package/backend/dist/snapshot/store.d.ts +226 -0
- package/backend/dist/snapshot/store.js +887 -0
- package/backend/dist/snapshot/tree.d.ts +112 -0
- package/backend/dist/snapshot/tree.js +207 -0
- package/backend/dist/snapshot/types.d.ts +212 -0
- package/backend/dist/snapshot/types.js +14 -0
- package/backend/dist/snapshot/view.d.ts +19 -0
- package/backend/dist/snapshot/view.js +57 -0
- package/backend/dist/snapshot/write-worker.d.ts +67 -0
- package/backend/dist/snapshot/write-worker.js +129 -0
- package/backend/dist/surfaces/body-admission.d.ts +67 -2
- package/backend/dist/surfaces/body-admission.js +218 -13
- package/backend/dist/surfaces/chat-pipeline.js +8 -0
- package/backend/dist/surfaces/decision-pipeline.js +10 -7
- package/backend/dist/surfaces/gateway-catalog.d.ts +26 -0
- package/backend/dist/surfaces/gateway-catalog.js +202 -0
- package/backend/dist/surfaces/gateway-chat.d.ts +7 -11
- package/backend/dist/surfaces/gateway-chat.js +50 -12
- package/backend/dist/surfaces/ocr-headers.d.ts +6 -4
- package/backend/dist/surfaces/ocr-headers.js +11 -7
- package/backend/dist/surfaces/systemone.js +1 -1
- package/backend/dist/surfaces/transport-fastify.js +32 -5
- package/backend/dist/surfaces/transport-node-http.d.ts +9 -3
- package/backend/dist/surfaces/transport-node-http.js +6 -19
- package/backend/dist/surfaces/types.d.ts +11 -3
- package/backend/dist/trace/persist.js +21 -1
- package/backend/dist/trace/tracker.d.ts +35 -3
- package/backend/dist/trace/tracker.js +23 -0
- package/backend/dist/types/openai.d.ts +8 -0
- package/backend/dist/types/router.d.ts +17 -6
- package/backend/dist/types/router.js +14 -2
- package/backend/dist/utils/client-families.d.ts +66 -0
- package/backend/dist/utils/client-families.js +127 -0
- package/backend/dist/utils/event-ring.d.ts +1 -1
- package/backend/dist/utils/event-ring.js +1 -1
- package/backend/dist/utils/proxy-node.js +1 -1
- package/backend/dist/utils/proxy.d.ts +135 -11
- package/backend/dist/utils/proxy.js +144 -29
- package/backend/dist/utils/run-command.d.ts +21 -1
- package/backend/dist/utils/run-command.js +64 -4
- package/backend/dist/utils/server-liveness.d.ts +3 -2
- package/backend/dist/utils/server-liveness.js +3 -2
- package/backend/dist/utils/session-file.d.ts +28 -0
- package/backend/dist/utils/session-file.js +34 -0
- package/frontend/dist/assets/antigravity.color-DXFfVr6X.svg +39 -0
- package/frontend/dist/assets/gemini-cli.color-BfquLd1p.svg +1 -0
- package/frontend/dist/assets/index-CTZNvn1d.js +1707 -0
- package/frontend/dist/assets/{index-KI1OW5x6.css → index-CqDSOPQ0.css} +1 -1
- package/frontend/dist/index.html +2 -2
- package/package.json +3 -1
- package/frontend/dist/assets/index-Dk76uc7e.js +0 -1522
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type SnapshotTurn } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Snapshot write WORKER — the per-request assembly that used to run on the
|
|
4
|
+
* gateway's main thread now runs here.
|
|
5
|
+
*
|
|
6
|
+
* What it takes off the loop: base64 decode + sha256 for every inline image,
|
|
7
|
+
* the content-addressed media write (`writeFileSync` + `renameWithRetry`,
|
|
8
|
+
* which busy-waits through Windows AV contention), `JSON.stringify` of the
|
|
9
|
+
* whole turn, and `Buffer.byteLength`. For a 4 MB agent transcript those were
|
|
10
|
+
* 4–30 ms of event-loop time per completed request — paid AFTER the client's
|
|
11
|
+
* last chunk, so it stalled every other in-flight stream instead of that one.
|
|
12
|
+
*
|
|
13
|
+
* Ordering: the worker writes the media payloads BEFORE the transcript line
|
|
14
|
+
* lands (a line must never reference a payload that is not durable yet), and
|
|
15
|
+
* asks the main thread to register the digests first — `pendingMedia` lives
|
|
16
|
+
* there because it guards the hourly sweep, which also lives there. Turns and
|
|
17
|
+
* flush calls are processed strictly in arrival order, so `flush()` observing
|
|
18
|
+
* a `flushed` reply means every turn posted before it is on disk.
|
|
19
|
+
*/
|
|
20
|
+
export interface SnapshotWorkerConfig {
|
|
21
|
+
type: 'config';
|
|
22
|
+
rootDir: string;
|
|
23
|
+
maxTurnBytes: number;
|
|
24
|
+
}
|
|
25
|
+
export interface SnapshotWorkerTurn {
|
|
26
|
+
type: 'turn';
|
|
27
|
+
id: number;
|
|
28
|
+
sessionId: string;
|
|
29
|
+
file: string;
|
|
30
|
+
turn: SnapshotTurn;
|
|
31
|
+
}
|
|
32
|
+
export interface SnapshotWorkerFlush {
|
|
33
|
+
type: 'flush';
|
|
34
|
+
id: number;
|
|
35
|
+
}
|
|
36
|
+
/** Main -> worker: the digests are registered, the payloads may be referenced. */
|
|
37
|
+
export interface SnapshotWorkerMediaAck {
|
|
38
|
+
type: 'media-ack';
|
|
39
|
+
id: number;
|
|
40
|
+
}
|
|
41
|
+
export type SnapshotWorkerRequest = SnapshotWorkerConfig | SnapshotWorkerTurn | SnapshotWorkerFlush | SnapshotWorkerMediaAck;
|
|
42
|
+
export interface SnapshotWorkerMedia {
|
|
43
|
+
type: 'media';
|
|
44
|
+
id: number;
|
|
45
|
+
keys: string[];
|
|
46
|
+
}
|
|
47
|
+
export interface SnapshotWorkerWritten {
|
|
48
|
+
type: 'written';
|
|
49
|
+
id: number;
|
|
50
|
+
}
|
|
51
|
+
export interface SnapshotWorkerHalt {
|
|
52
|
+
type: 'halt';
|
|
53
|
+
id: number;
|
|
54
|
+
sessionId: string;
|
|
55
|
+
refusedBytes: number;
|
|
56
|
+
limitBytes: number;
|
|
57
|
+
}
|
|
58
|
+
export interface SnapshotWorkerFailed {
|
|
59
|
+
type: 'failed';
|
|
60
|
+
id: number;
|
|
61
|
+
message: string;
|
|
62
|
+
}
|
|
63
|
+
export interface SnapshotWorkerFlushed {
|
|
64
|
+
type: 'flushed';
|
|
65
|
+
id: number;
|
|
66
|
+
}
|
|
67
|
+
export type SnapshotWorkerResponse = SnapshotWorkerMedia | SnapshotWorkerWritten | SnapshotWorkerHalt | SnapshotWorkerFailed | SnapshotWorkerFlushed;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isMainThread, parentPort } from 'node:worker_threads';
|
|
4
|
+
import { ensureDirSync, storeMediaPayload } from './media-fs.js';
|
|
5
|
+
import { offloadInputMedia, offloadOutputMedia } from './media.js';
|
|
6
|
+
import { SNAPSHOT_SCHEMA } from './types.js';
|
|
7
|
+
let rootDir = '';
|
|
8
|
+
let maxTurnBytes = Number.POSITIVE_INFINITY;
|
|
9
|
+
const halted = new Set();
|
|
10
|
+
const mediaAcks = new Map();
|
|
11
|
+
function post(message) {
|
|
12
|
+
parentPort?.postMessage(message);
|
|
13
|
+
}
|
|
14
|
+
function appendMarker(file, turn, refusedBytes) {
|
|
15
|
+
const marker = {
|
|
16
|
+
schema: SNAPSHOT_SCHEMA,
|
|
17
|
+
kind: 'incomplete',
|
|
18
|
+
reason: 'turn-cap',
|
|
19
|
+
turnIndex: turn.turnIndex,
|
|
20
|
+
requestId: turn.requestId,
|
|
21
|
+
ts: turn.ts,
|
|
22
|
+
refusedBytes,
|
|
23
|
+
limitBytes: maxTurnBytes,
|
|
24
|
+
};
|
|
25
|
+
try {
|
|
26
|
+
ensureDirSync(path.dirname(file));
|
|
27
|
+
fs.appendFileSync(file, JSON.stringify(marker) + '\n', 'utf8');
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Already halted in memory; the marker is best-effort.
|
|
31
|
+
}
|
|
32
|
+
console.warn(`[snapshot] session ${turn.sessionId} halted (turn-cap): ` +
|
|
33
|
+
`turn ${turn.turnIndex ?? 'none'} needs ${refusedBytes} bytes, limit ${maxTurnBytes} — export will refuse`);
|
|
34
|
+
}
|
|
35
|
+
async function handleTurn(message) {
|
|
36
|
+
const { id, file, turn } = message;
|
|
37
|
+
if (halted.has(message.sessionId)) {
|
|
38
|
+
post({ type: 'written', id });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Pass 1 — PROBE: a sink that returns false keeps `media.ts` from rewriting
|
|
42
|
+
// the payload in place, so this pass only discovers digests. They are
|
|
43
|
+
// registered on the main thread BEFORE any payload file exists, which is
|
|
44
|
+
// what makes the sweep guard airtight for the whole media-write window.
|
|
45
|
+
const probed = new Set();
|
|
46
|
+
const probe = (_raw, sha, ext) => {
|
|
47
|
+
probed.add(`${sha}.${ext}`);
|
|
48
|
+
return false;
|
|
49
|
+
};
|
|
50
|
+
offloadInputMedia(turn.request.input, probe);
|
|
51
|
+
offloadOutputMedia(turn.response.output, probe);
|
|
52
|
+
if (probed.size) {
|
|
53
|
+
post({ type: 'media', id, keys: [...probed] });
|
|
54
|
+
await new Promise((resolve) => mediaAcks.set(id, resolve));
|
|
55
|
+
}
|
|
56
|
+
// Pass 2 — OFFLOAD: writes the payloads and rewrites the sources in place,
|
|
57
|
+
// exactly as the inline path does (a failed write keeps the inline bytes).
|
|
58
|
+
const media = {
|
|
59
|
+
...offloadInputMedia(turn.request.input, (raw, sha, ext) => storeMediaPayload(rootDir, raw, sha, ext)).entries,
|
|
60
|
+
...offloadOutputMedia(turn.response.output, (raw, sha, ext) => storeMediaPayload(rootDir, raw, sha, ext)).entries,
|
|
61
|
+
};
|
|
62
|
+
if (Object.keys(media).length)
|
|
63
|
+
turn.media = media;
|
|
64
|
+
let line;
|
|
65
|
+
let bytes;
|
|
66
|
+
try {
|
|
67
|
+
line = JSON.stringify(turn) + '\n';
|
|
68
|
+
bytes = Buffer.byteLength(line, 'utf8');
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
post({ type: 'failed', id, message: err.message });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (bytes > maxTurnBytes) {
|
|
75
|
+
// Too big to hand over faithfully; halt the session instead of storing a
|
|
76
|
+
// giant line (ADR-0038 §4).
|
|
77
|
+
halted.add(message.sessionId);
|
|
78
|
+
appendMarker(file, turn, bytes);
|
|
79
|
+
post({ type: 'halt', id, sessionId: message.sessionId, refusedBytes: bytes, limitBytes: maxTurnBytes });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
ensureDirSync(path.dirname(file));
|
|
84
|
+
fs.appendFileSync(file, line, 'utf8');
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
post({ type: 'failed', id, message: err.message });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
post({ type: 'written', id });
|
|
91
|
+
}
|
|
92
|
+
const queue = [];
|
|
93
|
+
let pumping = false;
|
|
94
|
+
async function pump() {
|
|
95
|
+
if (pumping)
|
|
96
|
+
return;
|
|
97
|
+
pumping = true;
|
|
98
|
+
while (queue.length) {
|
|
99
|
+
const message = queue.shift();
|
|
100
|
+
if (message.type === 'media-ack') {
|
|
101
|
+
// Answered inline, never queued; kept here for exhaustiveness.
|
|
102
|
+
mediaAcks.get(message.id)?.();
|
|
103
|
+
mediaAcks.delete(message.id);
|
|
104
|
+
}
|
|
105
|
+
else if (message.type === 'config') {
|
|
106
|
+
rootDir = message.rootDir;
|
|
107
|
+
maxTurnBytes = message.maxTurnBytes;
|
|
108
|
+
}
|
|
109
|
+
else if (message.type === 'flush') {
|
|
110
|
+
post({ type: 'flushed', id: message.id });
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
await handleTurn(message);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
pumping = false;
|
|
117
|
+
}
|
|
118
|
+
if (!isMainThread && parentPort) {
|
|
119
|
+
parentPort.on('message', (message) => {
|
|
120
|
+
if (message.type === 'media-ack') {
|
|
121
|
+
// Resolve outside the queue: the queue is parked awaiting this exact ack.
|
|
122
|
+
mediaAcks.get(message.id)?.();
|
|
123
|
+
mediaAcks.delete(message.id);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
queue.push(message);
|
|
127
|
+
void pump();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -151,7 +151,7 @@ export interface ContinueAdmissionRoute {
|
|
|
151
151
|
};
|
|
152
152
|
/**
|
|
153
153
|
* Close the transport after the refusal. On the MITM leg `res.end()` alone leaves
|
|
154
|
-
* the terminated TLS socket open (Bun), so the
|
|
154
|
+
* the terminated TLS socket open (Bun), so the ingress proxy passes the terminator
|
|
155
155
|
* abort hook here; the native leg needs nothing extra (`connection: close`).
|
|
156
156
|
*/
|
|
157
157
|
refuse?: () => void;
|
|
@@ -247,6 +247,71 @@ export declare function logBodyRejection(info: {
|
|
|
247
247
|
}): void;
|
|
248
248
|
/** Narrowing helper for the answering layers on both legs. */
|
|
249
249
|
export declare function isBodyAdmissionError(err: unknown): err is BodyAdmissionError;
|
|
250
|
+
/**
|
|
251
|
+
* Read what the client still has in flight BEFORE writing the answer, so the
|
|
252
|
+
* refusal lands in a readable state on a socket the framework will close as soon
|
|
253
|
+
* as the reply finishes. Used by the Fastify (native) legs and the pre-dispatch
|
|
254
|
+
* gate — every place that still has to WRITE an answer.
|
|
255
|
+
*
|
|
256
|
+
* The budget is TWICE the plane's cap: a body just over the limit (the common
|
|
257
|
+
* case, and the one a client can actually fix by reading the answer) is consumed
|
|
258
|
+
* in full, so the sender finishes its write and the answer is read cleanly. A
|
|
259
|
+
* sender far over the limit is cut there — a server is not obliged to download an
|
|
260
|
+
* upload it already refused (RFC 9110 15.5.14), and `Expect: 100-continue`, which
|
|
261
|
+
* this gateway honours, is how a client learns the limit before sending.
|
|
262
|
+
*
|
|
263
|
+
* The drain NEVER destroys the request: expiry only stops reading, so the caller
|
|
264
|
+
* can still write its 413. (Destruction-before-answer is what produced a bare
|
|
265
|
+
* `200 OK` on Bun.)
|
|
266
|
+
*/
|
|
267
|
+
export declare function settleRefusedBody(req: http.IncomingMessage | undefined, capBytes?: number): Promise<void>;
|
|
268
|
+
/**
|
|
269
|
+
* Same idea AFTER the answer was already flushed (every refusal path). Returns
|
|
270
|
+
* whether the drain took over the request; when it did not, the caller closes the
|
|
271
|
+
* transport itself. `whenDone` runs once the client finished (or the budget
|
|
272
|
+
* expired), so the close still happens exactly once.
|
|
273
|
+
*
|
|
274
|
+
* The byte budget is the PLANE'S cap doubled, not a fixed number: a body just
|
|
275
|
+
* over the limit is the normal case, and cutting the drain short mid-write makes
|
|
276
|
+
* the client see a reset instead of the answer we already wrote. Measured on the
|
|
277
|
+
* admin plane: an 8 MiB upload reset ~20% of the time while the budget was a flat
|
|
278
|
+
* 4 MiB, and stopped resetting once the drain covered the whole body.
|
|
279
|
+
*/
|
|
280
|
+
export declare function drainRefusedBody(req: http.IncomingMessage | undefined, whenDone?: () => void, capBytes?: number): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* Write ONE refusal, then deal with the socket. WHICH order is used depends on
|
|
283
|
+
* the leg, and both orders exist for the same reason — the answer must reach a
|
|
284
|
+
* client that is still uploading:
|
|
285
|
+
*
|
|
286
|
+
* - the RAW leg calls this writer only (answer first, then drain). Answering
|
|
287
|
+
* before reading is what lets a client that ignores the limit still see the
|
|
288
|
+
* 413; the bounded drain only prevents the OS from turning the close into a
|
|
289
|
+
* reset the client never reads past.
|
|
290
|
+
* - the FASTIFY legs drain first (`settleRefusedBody`) and then call this with
|
|
291
|
+
* `drainBody: false`: the framework closes the socket with the reply, so an
|
|
292
|
+
* answer written while the sender is still writing is the reset, and a drain
|
|
293
|
+
* that ran AFTER the answer would only add a second budget on a socket that
|
|
294
|
+
* is already being torn down.
|
|
295
|
+
*
|
|
296
|
+
* Neither path may `destroy()` the request: destroying it before the answer
|
|
297
|
+
* makes Bun reply with a bare `200 OK` and an empty body — a refusal that tells
|
|
298
|
+
* the client the request SUCCEEDED (measured: `+3006 ms`, `HTTP/1.1 200 OK`).
|
|
299
|
+
*
|
|
300
|
+
* `content-length` is declared because Bun 1.4.x otherwise frames the answer as
|
|
301
|
+
* chunked, so a raw-socket client reads hex chunk-size lines instead of JSON.
|
|
302
|
+
* `connection: close` rides only on h1: HTTP/2 forbids connection-specific
|
|
303
|
+
* fields, and on h2 the response completes normally while flow control stops a
|
|
304
|
+
* client that keeps uploading (nothing is buffered).
|
|
305
|
+
*/
|
|
306
|
+
export declare function writeRefusalAnswer(res: http.ServerResponse, status: number, serialized: string, opts?: {
|
|
307
|
+
/** The rejection's own negotiation headers (a 415 names its cause). */
|
|
308
|
+
headers?: Record<string, string>;
|
|
309
|
+
/** The plane's cap, so a post-answer drain knows how much it may read. */
|
|
310
|
+
capBytes?: number;
|
|
311
|
+
/** Drain the refused body after the answer. The raw leg wants it; the
|
|
312
|
+
* Fastify legs already settled the body BEFORE calling this. */
|
|
313
|
+
drainBody?: boolean;
|
|
314
|
+
}): void;
|
|
250
315
|
/** Minimal view of a protocol surface, so this module does not import the table
|
|
251
316
|
* (which would cycle back through the transports). */
|
|
252
317
|
interface EnvelopeOwner {
|
|
@@ -275,5 +340,5 @@ export declare function surfaceBodyAdmissionOptions(surface: EnvelopeOwner): Rou
|
|
|
275
340
|
* second, worse error serializer for validation and 5xx paths it knows nothing
|
|
276
341
|
* about, and it never echoes a stack or an upstream body.
|
|
277
342
|
*/
|
|
278
|
-
export declare function adminBodyErrorHandler(err: FastifyError, request: FastifyRequest, reply: FastifyReply):
|
|
343
|
+
export declare function adminBodyErrorHandler(err: FastifyError, request: FastifyRequest, reply: FastifyReply): Promise<never>;
|
|
279
344
|
export {};
|
|
@@ -487,6 +487,200 @@ export function logBodyRejection(info, log) {
|
|
|
487
487
|
export function isBodyAdmissionError(err) {
|
|
488
488
|
return err instanceof BodyAdmissionError;
|
|
489
489
|
}
|
|
490
|
+
/** How long a refusal may wait for the sender to finish before it is delivered
|
|
491
|
+
* anyway. Generous on purpose: this is bandwidth on ONE refused request, and the
|
|
492
|
+
* alternative is an answer the client never sees. */
|
|
493
|
+
const REFUSAL_DRAIN_BEFORE_ANSWER_MS = 2000;
|
|
494
|
+
/** The idle window a refusal waits for the NEXT chunk before it gives up on the
|
|
495
|
+
* sender. Re-armed per chunk, so a continuous upload is read to its end while a
|
|
496
|
+
* client that stopped (or never started) is answered promptly — a client with
|
|
497
|
+
* `Expect: 100-continue` declares a length and then waits for US. */
|
|
498
|
+
const REFUSAL_DRAIN_IDLE_MS = 250;
|
|
499
|
+
/** How long a refusal already on the wire keeps reading (the raw leg, where the
|
|
500
|
+
* answer is flushed first and only the close is deferred). */
|
|
501
|
+
const REFUSAL_DRAIN_AFTER_ANSWER_MS = 2000;
|
|
502
|
+
/**
|
|
503
|
+
* A refusal is an ANSWER, not a reset — the module's own rule (RFC 9110
|
|
504
|
+
* 15.5.14), and sending only half of it is worse than a bare reset, because the
|
|
505
|
+
* client cannot tell which one it got.
|
|
506
|
+
*
|
|
507
|
+
* The failure this exists for: a client is still uploading when the cap is
|
|
508
|
+
* crossed. The 413 is written, the socket is closed while unread inbound bytes
|
|
509
|
+
* remain, and the OS turns that `close()` into RST — the client's pending write
|
|
510
|
+
* fails, so it never reads the status, the code, or the limit that was named.
|
|
511
|
+
* (Reproduced on the admin plane: the same 8 MiB upload answered 413 four times
|
|
512
|
+
* running and then threw `ECONNRESET` three times, with no code change.)
|
|
513
|
+
*
|
|
514
|
+
* So the body we just refused is read and discarded first, bounded in BYTES and
|
|
515
|
+
* TIME: past either bound the drain STOPS READING but never destroys the request
|
|
516
|
+
* — destroying it before the answer is written makes Bun answer a bare `200 OK`
|
|
517
|
+
* (see `writeRefusalAnswer`), and a refusal that says "OK" is worse than a reset.
|
|
518
|
+
* Nothing here admits the request — the refusal is already decided; this only
|
|
519
|
+
* decides whether the client can hear it.
|
|
520
|
+
*/
|
|
521
|
+
function startRefusalDrain(req, opts) {
|
|
522
|
+
const { maxBytes, maxMs } = opts;
|
|
523
|
+
if (!req || req.destroyed || req.readableEnded || req.complete) {
|
|
524
|
+
return { started: false, done: Promise.resolve() };
|
|
525
|
+
}
|
|
526
|
+
let read = 0;
|
|
527
|
+
const done = new Promise((resolve) => {
|
|
528
|
+
const stop = () => {
|
|
529
|
+
clearTimeout(timer);
|
|
530
|
+
clearTimeout(idle);
|
|
531
|
+
req.removeListener('data', onData);
|
|
532
|
+
req.removeListener('end', onEnd);
|
|
533
|
+
req.removeListener('close', onClose);
|
|
534
|
+
req.removeListener('error', onClose);
|
|
535
|
+
// Never destroy: the caller still has an answer to write on this socket.
|
|
536
|
+
resolve();
|
|
537
|
+
};
|
|
538
|
+
const onData = (chunk) => {
|
|
539
|
+
// Bytes are flowing: this client really is mid-upload, so finish reading
|
|
540
|
+
// its body (bounded) — that is what keeps the refusal readable. The idle
|
|
541
|
+
// timer is re-armed on every chunk, so a client that STOPS is answered
|
|
542
|
+
// within one idle window instead of waiting out the whole budget.
|
|
543
|
+
clearTimeout(idle);
|
|
544
|
+
idle = setTimeout(() => stop(), REFUSAL_DRAIN_IDLE_MS);
|
|
545
|
+
idle.unref?.();
|
|
546
|
+
read += typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
|
|
547
|
+
if (read >= maxBytes)
|
|
548
|
+
stop();
|
|
549
|
+
};
|
|
550
|
+
const onEnd = () => stop();
|
|
551
|
+
const onClose = () => stop();
|
|
552
|
+
const timer = setTimeout(() => stop(), maxMs);
|
|
553
|
+
let idle = setTimeout(() => stop(), REFUSAL_DRAIN_IDLE_MS);
|
|
554
|
+
// Never hold the process open for a client that stopped talking.
|
|
555
|
+
timer.unref?.();
|
|
556
|
+
idle.unref?.();
|
|
557
|
+
req.on('data', onData);
|
|
558
|
+
req.once('end', onEnd);
|
|
559
|
+
req.once('close', onClose);
|
|
560
|
+
req.once('error', onClose);
|
|
561
|
+
req.resume();
|
|
562
|
+
});
|
|
563
|
+
return { started: true, done };
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Read what the client still has in flight BEFORE writing the answer, so the
|
|
567
|
+
* refusal lands in a readable state on a socket the framework will close as soon
|
|
568
|
+
* as the reply finishes. Used by the Fastify (native) legs and the pre-dispatch
|
|
569
|
+
* gate — every place that still has to WRITE an answer.
|
|
570
|
+
*
|
|
571
|
+
* The budget is TWICE the plane's cap: a body just over the limit (the common
|
|
572
|
+
* case, and the one a client can actually fix by reading the answer) is consumed
|
|
573
|
+
* in full, so the sender finishes its write and the answer is read cleanly. A
|
|
574
|
+
* sender far over the limit is cut there — a server is not obliged to download an
|
|
575
|
+
* upload it already refused (RFC 9110 15.5.14), and `Expect: 100-continue`, which
|
|
576
|
+
* this gateway honours, is how a client learns the limit before sending.
|
|
577
|
+
*
|
|
578
|
+
* The drain NEVER destroys the request: expiry only stops reading, so the caller
|
|
579
|
+
* can still write its 413. (Destruction-before-answer is what produced a bare
|
|
580
|
+
* `200 OK` on Bun.)
|
|
581
|
+
*/
|
|
582
|
+
export async function settleRefusedBody(req, capBytes = bodyLimitFor('gateway')) {
|
|
583
|
+
const { done } = startRefusalDrain(req, {
|
|
584
|
+
maxBytes: Math.max(capBytes * 2, 1024 * 1024),
|
|
585
|
+
maxMs: REFUSAL_DRAIN_BEFORE_ANSWER_MS,
|
|
586
|
+
});
|
|
587
|
+
await done;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Same idea AFTER the answer was already flushed (every refusal path). Returns
|
|
591
|
+
* whether the drain took over the request; when it did not, the caller closes the
|
|
592
|
+
* transport itself. `whenDone` runs once the client finished (or the budget
|
|
593
|
+
* expired), so the close still happens exactly once.
|
|
594
|
+
*
|
|
595
|
+
* The byte budget is the PLANE'S cap doubled, not a fixed number: a body just
|
|
596
|
+
* over the limit is the normal case, and cutting the drain short mid-write makes
|
|
597
|
+
* the client see a reset instead of the answer we already wrote. Measured on the
|
|
598
|
+
* admin plane: an 8 MiB upload reset ~20% of the time while the budget was a flat
|
|
599
|
+
* 4 MiB, and stopped resetting once the drain covered the whole body.
|
|
600
|
+
*/
|
|
601
|
+
export function drainRefusedBody(req, whenDone, capBytes = bodyLimitFor('gateway')) {
|
|
602
|
+
const { started, done } = startRefusalDrain(req, {
|
|
603
|
+
maxBytes: Math.max(capBytes * 2, 1024 * 1024),
|
|
604
|
+
maxMs: REFUSAL_DRAIN_AFTER_ANSWER_MS,
|
|
605
|
+
});
|
|
606
|
+
if (!started)
|
|
607
|
+
return false;
|
|
608
|
+
void done.then(() => whenDone?.());
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Write ONE refusal, then deal with the socket. WHICH order is used depends on
|
|
613
|
+
* the leg, and both orders exist for the same reason — the answer must reach a
|
|
614
|
+
* client that is still uploading:
|
|
615
|
+
*
|
|
616
|
+
* - the RAW leg calls this writer only (answer first, then drain). Answering
|
|
617
|
+
* before reading is what lets a client that ignores the limit still see the
|
|
618
|
+
* 413; the bounded drain only prevents the OS from turning the close into a
|
|
619
|
+
* reset the client never reads past.
|
|
620
|
+
* - the FASTIFY legs drain first (`settleRefusedBody`) and then call this with
|
|
621
|
+
* `drainBody: false`: the framework closes the socket with the reply, so an
|
|
622
|
+
* answer written while the sender is still writing is the reset, and a drain
|
|
623
|
+
* that ran AFTER the answer would only add a second budget on a socket that
|
|
624
|
+
* is already being torn down.
|
|
625
|
+
*
|
|
626
|
+
* Neither path may `destroy()` the request: destroying it before the answer
|
|
627
|
+
* makes Bun reply with a bare `200 OK` and an empty body — a refusal that tells
|
|
628
|
+
* the client the request SUCCEEDED (measured: `+3006 ms`, `HTTP/1.1 200 OK`).
|
|
629
|
+
*
|
|
630
|
+
* `content-length` is declared because Bun 1.4.x otherwise frames the answer as
|
|
631
|
+
* chunked, so a raw-socket client reads hex chunk-size lines instead of JSON.
|
|
632
|
+
* `connection: close` rides only on h1: HTTP/2 forbids connection-specific
|
|
633
|
+
* fields, and on h2 the response completes normally while flow control stops a
|
|
634
|
+
* client that keeps uploading (nothing is buffered).
|
|
635
|
+
*/
|
|
636
|
+
export function writeRefusalAnswer(res, status, serialized, opts = {}) {
|
|
637
|
+
const isH2 = res.stream !== undefined;
|
|
638
|
+
res.writeHead(status, {
|
|
639
|
+
'content-type': 'application/json; charset=utf-8',
|
|
640
|
+
'content-length': String(Buffer.byteLength(serialized)),
|
|
641
|
+
...(isH2 ? {} : { connection: 'close' }),
|
|
642
|
+
...(opts.headers ?? {}),
|
|
643
|
+
});
|
|
644
|
+
res.end(serialized);
|
|
645
|
+
if (isH2) {
|
|
646
|
+
// A refused request body cannot be drained here without reading it, and
|
|
647
|
+
// resetting the stream AFTER the response was delivered risks the client
|
|
648
|
+
// discarding an answer we promised it. Neither is needed: HTTP/2 flow
|
|
649
|
+
// control bounds the damage — measured on Node 24 and Bun 1.4.5, a client
|
|
650
|
+
// that keeps uploading after the 413 manages exactly one window (64 KiB)
|
|
651
|
+
// and then stalls, with the response delivered and RSS flat. (The h2
|
|
652
|
+
// compatibility response has no `destroySoon`; an earlier revision called
|
|
653
|
+
// one through an optional chain, which silently did nothing on every
|
|
654
|
+
// runtime — hence this explicit note.)
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
// h1: the connection is not reusable either way, so close it — after the body
|
|
658
|
+
// we just refused has been read (bounded), so the sender finishes its write
|
|
659
|
+
// and sees the answer. `destroySoon` lives on the SOCKET, not on the response
|
|
660
|
+
// (verified on Node 24 and Bun 1.4.5: `res.destroySoon` is undefined, so
|
|
661
|
+
// calling it through an optional chain was a no-op that left the close to the
|
|
662
|
+
// `connection: close` header alone).
|
|
663
|
+
const close = () => {
|
|
664
|
+
const socket = res.socket;
|
|
665
|
+
if (socket?.destroySoon) {
|
|
666
|
+
socket.destroySoon();
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
try {
|
|
670
|
+
res.destroy?.();
|
|
671
|
+
}
|
|
672
|
+
catch {
|
|
673
|
+
// The header still closes it on the runtime's own schedule.
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
if (opts.drainBody === false) {
|
|
677
|
+
close();
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
const draining = drainRefusedBody(res.req, close, opts.capBytes);
|
|
681
|
+
if (!draining)
|
|
682
|
+
close();
|
|
683
|
+
}
|
|
490
684
|
/**
|
|
491
685
|
* Fastify codes for the failures the content parser raises BEFORE a handler runs.
|
|
492
686
|
* They must be answered here because at that point no transport and no capture
|
|
@@ -531,7 +725,7 @@ export function surfaceBodyAdmissionOptions(surface) {
|
|
|
531
725
|
done(err);
|
|
532
726
|
}
|
|
533
727
|
},
|
|
534
|
-
errorHandler(err, request, reply) {
|
|
728
|
+
async errorHandler(err, request, reply) {
|
|
535
729
|
// One translation call covers the framework's own refusals, including
|
|
536
730
|
// FST_ERR_CTP_INVALID_MEDIA_TYPE: Fastify answers that with its own
|
|
537
731
|
// string-`error` envelope, which Anthropic-style clients cannot parse, so it
|
|
@@ -548,10 +742,20 @@ export function surfaceBodyAdmissionOptions(surface) {
|
|
|
548
742
|
capBytes,
|
|
549
743
|
encoding: requestContentEncoding(request.headers),
|
|
550
744
|
}, request.log);
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
745
|
+
// Read the refused body FIRST (bounded, never destroying the request),
|
|
746
|
+
// then answer: Fastify closes the socket with this reply, so an answer
|
|
747
|
+
// written while the sender is still uploading is the reset it never reads.
|
|
748
|
+
// The drain stops at 2× this plane's cap or the time budget — a server is
|
|
749
|
+
// not obliged to download an upload it already refused — and never
|
|
750
|
+
// destroys, which is what keeps the 413 below on the wire (destroying it
|
|
751
|
+
// first made Bun answer a bare `200 OK`).
|
|
752
|
+
await settleRefusedBody(request.raw, capBytes);
|
|
753
|
+
reply.hijack();
|
|
754
|
+
writeRefusalAnswer(reply.raw, admission.statusCode, JSON.stringify(surface.errorBody(admission.statusCode, admission.message, admission.code)),
|
|
755
|
+
// `drainBody: false` — the body was settled above; a second drain would
|
|
756
|
+
// spend another budget on a socket Fastify is already closing.
|
|
757
|
+
{ headers: admission.headers, capBytes, drainBody: false });
|
|
758
|
+
return reply;
|
|
555
759
|
},
|
|
556
760
|
};
|
|
557
761
|
}
|
|
@@ -567,7 +771,7 @@ export function surfaceBodyAdmissionOptions(surface) {
|
|
|
567
771
|
* second, worse error serializer for validation and 5xx paths it knows nothing
|
|
568
772
|
* about, and it never echoes a stack or an upstream body.
|
|
569
773
|
*/
|
|
570
|
-
export function adminBodyErrorHandler(err, request, reply) {
|
|
774
|
+
export async function adminBodyErrorHandler(err, request, reply) {
|
|
571
775
|
const capBytes = bodyLimitFor('admin');
|
|
572
776
|
const admission = isBodyAdmissionError(err)
|
|
573
777
|
? err
|
|
@@ -581,13 +785,14 @@ export function adminBodyErrorHandler(err, request, reply) {
|
|
|
581
785
|
capBytes,
|
|
582
786
|
encoding: requestContentEncoding(request.headers),
|
|
583
787
|
}, request.log);
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
788
|
+
// Same order as the inference planes, with THIS plane's cap: drain the
|
|
789
|
+
// refused body first (never destroying the request), then write the admin
|
|
790
|
+
// envelope. The cap matters — the post-answer drain used to default to the
|
|
791
|
+
// inference cap (32 MiB) on a plane whose own limit is 8 MiB.
|
|
792
|
+
await settleRefusedBody(request.raw, capBytes);
|
|
793
|
+
reply.hijack();
|
|
794
|
+
writeRefusalAnswer(reply.raw, admission.statusCode, JSON.stringify({ success: false, message: admission.message }), { headers: admission.headers, capBytes, drainBody: false });
|
|
795
|
+
return reply;
|
|
591
796
|
}
|
|
592
797
|
/** Translate a framework content-parser failure into this module's error type, so
|
|
593
798
|
* one message builder and one `code` cover both producers. Returns undefined for
|
|
@@ -130,6 +130,14 @@ export async function runChatSurface(t, surface, orchestrator, body) {
|
|
|
130
130
|
// Capture keeps the client's protocol envelope; IR belongs to routing
|
|
131
131
|
// and execution, never to the inbound wire archive.
|
|
132
132
|
clientBody: body,
|
|
133
|
+
// A client that disconnected mid-stream leaves the upstream draining into
|
|
134
|
+
// a dead socket. This probe lets the snapshot plane record such a turn as
|
|
135
|
+
// `aborted` rather than `completed` (ADR-0040). Transport-owned so a
|
|
136
|
+
// non-Fastify leg may omit it.
|
|
137
|
+
isClientGone: () => t.isClientGone(),
|
|
138
|
+
// Sticky variant for the snapshot plane (ADR-0040): "the client left at
|
|
139
|
+
// some point during this turn" — still true after the upstream drained.
|
|
140
|
+
...(t.didClientLeave ? { didClientLeave: () => t.didClientLeave() } : {}),
|
|
133
141
|
...t.captureProcessContext(),
|
|
134
142
|
}, relay);
|
|
135
143
|
if (streamed) {
|
|
@@ -75,8 +75,8 @@ export async function runDecisionSurface(t, orchestrator, body) {
|
|
|
75
75
|
}
|
|
76
76
|
// Chain head of the healthy allowed pool (weight/priority order from
|
|
77
77
|
// buildTierPoolEntries) — deterministic per pool state. The chat pool's
|
|
78
|
-
//
|
|
79
|
-
// decision-surface one.
|
|
78
|
+
// rotation strategies (`weighted_round_robin` / `round_robin`) are a
|
|
79
|
+
// chat-surface nicety, not a decision-surface one.
|
|
80
80
|
const healthyAllowed = registry
|
|
81
81
|
.getCandidateModelsForTier(tier, true, { decisionSurface: true })
|
|
82
82
|
.filter((m) => isModelAllowed(m.id, policy, tier));
|
|
@@ -220,14 +220,17 @@ export async function runDecisionSurface(t, orchestrator, body) {
|
|
|
220
220
|
// this decision (the request slot was already consumed; same honesty as
|
|
221
221
|
// failed chat turns). costUsd approximation: this wire has no cached/
|
|
222
222
|
// reasoning split, so input bills at FULL input price — a deliberate
|
|
223
|
-
// conservative over-count for budgets.
|
|
223
|
+
// conservative over-count for budgets. A plugin that reported the vendor's
|
|
224
|
+
// own committed cost (`vendorCostUsd`, stamped on the IR usage) is the
|
|
225
|
+
// invoice, so it wins over that estimate here exactly as on the chat wire.
|
|
224
226
|
const rlUsage = decision.response.usage
|
|
225
227
|
? {
|
|
226
228
|
tokens: (decision.response.usage.input_tokens ?? 0) + (decision.response.usage.output_tokens ?? 0),
|
|
227
|
-
costUsd:
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
costUsd: decision.response.usage.vendorCostUsd ??
|
|
230
|
+
computeRequestCost({
|
|
231
|
+
inputTokens: decision.response.usage.input_tokens ?? 0,
|
|
232
|
+
outputTokens: decision.response.usage.output_tokens ?? 0,
|
|
233
|
+
}, model.pricing),
|
|
231
234
|
}
|
|
232
235
|
: undefined;
|
|
233
236
|
if (rlUsage) {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ProviderRegistry } from '../providers/registry.js';
|
|
2
|
+
import type { CatalogProviderRecord } from '../opencode/catalog/types.js';
|
|
3
|
+
import type { GatewayCatalog } from '../plugins/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Async-refresh snapshot: `catalog()` is a SYNC host seam called on the
|
|
6
|
+
* request path, so a registry change kicks a background rebuild and the
|
|
7
|
+
* previous snapshot keeps serving until it lands. The first call after boot
|
|
8
|
+
* answers with ids only (the pre-existing contract) — never a blocked request.
|
|
9
|
+
*/
|
|
10
|
+
export declare class GatewayCatalogProjection {
|
|
11
|
+
private readonly registry;
|
|
12
|
+
private readonly listCatalog;
|
|
13
|
+
private snapshot;
|
|
14
|
+
private names;
|
|
15
|
+
private builtSignature;
|
|
16
|
+
private inFlight;
|
|
17
|
+
constructor(registry: ProviderRegistry, listCatalog: () => Promise<CatalogProviderRecord[]>);
|
|
18
|
+
get(): GatewayCatalog;
|
|
19
|
+
/**
|
|
20
|
+
* Catalog display name for a gateway-visible id, read from the SAME
|
|
21
|
+
* snapshot as `get()`. Ensure-first: a caller that only ever resolves
|
|
22
|
+
* models still kicks the (best-effort) build. Undefined until the first
|
|
23
|
+
* build lands (or when the catalog states none) — never a guess.
|
|
24
|
+
*/
|
|
25
|
+
nameOf(id: string): string | undefined;
|
|
26
|
+
}
|