@bitkyc08/opencodex 2.100.5 → 2.100.6-preview.20261119.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/README.md +1 -1
- package/bin/ocx.mjs +2 -0
- package/gui/dist/assets/App-BI8LQ7Qk.css +1 -0
- package/gui/dist/assets/App-D5qUBOoF.js +60 -0
- package/gui/dist/assets/Tray-DMwQSAli.js +1 -0
- package/gui/dist/assets/brand-mark-ycThul7d.js +1 -0
- package/gui/dist/assets/index-Bp_GaFFu.js +138 -0
- package/gui/dist/assets/index-BtwVlpeQ.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-Bquysdn5.js → segment-thumb-features-BLUW2Fb1.js} +1 -1
- package/gui/dist/classic/assets/App-CoiVLRW3.css +1 -0
- package/gui/dist/classic/assets/App-D39mkP2w.js +59 -0
- package/gui/dist/classic/assets/Tray-nUmdL7K9.js +1 -0
- package/gui/dist/classic/assets/index-BJ4uxZk0.js +138 -0
- package/gui/dist/classic/assets/usage-companion-chart-DFoYm2RY.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +62 -0
- package/native/ocx-child/Cargo.toml +1 -0
- package/native/ocx-child/src/journal.rs +32 -10
- package/native/ocx-child/src/state.rs +2 -1
- package/native/ocx-child/src/toml_surgeon.rs +183 -72
- package/package.json +2 -2
- package/src/adapters/agy-acp/acp.ts +202 -0
- package/src/adapters/agy-acp/adapter.ts +554 -0
- package/src/adapters/agy-acp/binary.ts +79 -0
- package/src/adapters/agy-acp/options.ts +163 -0
- package/src/adapters/agy-acp/output-redaction.ts +270 -0
- package/src/adapters/agy-acp/policy.ts +105 -0
- package/src/adapters/agy-acp/process.ts +218 -0
- package/src/adapters/anthropic/account-metadata.ts +4 -1
- package/src/adapters/anthropic/advisor-tool.ts +43 -9
- package/src/adapters/anthropic/client-identity.ts +2 -0
- package/src/adapters/anthropic.ts +102 -14
- package/src/adapters/antigravity-refusal.ts +5 -1
- package/src/adapters/base.ts +25 -2
- package/src/adapters/claude-cli/adapter.ts +1 -0
- package/src/adapters/claude-cli/usage-admission.ts +12 -9
- package/src/adapters/codebuddy/adapter.ts +1 -0
- package/src/adapters/command-code.ts +2 -2
- package/src/adapters/cursor/http1-bidi.ts +7 -2
- package/src/adapters/cursor/live-transport.ts +2 -0
- package/src/adapters/cursor/request-builder.ts +4 -4
- package/src/adapters/cursor/tool-naming.ts +5 -5
- package/src/adapters/cursor/transport-retry.ts +14 -8
- package/src/adapters/cursor/transport.ts +2 -0
- package/src/adapters/cursor.ts +8 -2
- package/src/adapters/google-error-details.ts +105 -0
- package/src/adapters/google-errors.ts +183 -31
- package/src/adapters/google-http.ts +66 -22
- package/src/adapters/google-truncation.ts +48 -3
- package/src/adapters/google-vertex-endpoint.ts +12 -1
- package/src/adapters/google.ts +54 -26
- package/src/adapters/kiro/adapter.ts +39 -38
- package/src/adapters/kiro/stream.ts +59 -9
- package/src/adapters/kiro-retry.ts +5 -3
- package/src/adapters/m365-copilot/LICENSE +21 -0
- package/src/adapters/m365-copilot/fenced.ts +745 -0
- package/src/adapters/m365-copilot/images.ts +89 -0
- package/src/adapters/m365-copilot/tools.ts +284 -0
- package/src/adapters/m365-copilot/transport.ts +232 -0
- package/src/adapters/m365-copilot.ts +143 -0
- package/src/adapters/openai-chat/tool-schema.ts +2 -2
- package/src/adapters/openai-responses/passthrough.ts +1 -0
- package/src/adapters/openai-responses/reasoning.ts +23 -3
- package/src/adapters/physical-send.ts +11 -4
- package/src/adapters/qoder/adapter.ts +17 -3
- package/src/adapters/registry.ts +15 -1
- package/src/adapters/zed.ts +2 -1
- package/src/advisor/consult.ts +1 -0
- package/src/bridge/response-json.ts +6 -2
- package/src/bridge/sse.ts +21 -9
- package/src/chat/inbound.ts +11 -8
- package/src/chat/outbound.ts +28 -7
- package/src/claude/context-windows.ts +32 -8
- package/src/claude/desktop-3p.ts +18 -14
- package/src/claude/desktop-picker-platform.ts +1 -1
- package/src/claude/desktop-picker.ts +3 -3
- package/src/claude/inbound-content-options.ts +11 -1
- package/src/claude/intercept/picker-ca-cleanup.ts +17 -5
- package/src/claude/intercept/picker-ca.ts +162 -27
- package/src/claude/intercept/picker-trust-linux.ts +411 -0
- package/src/claude/intercept/picker-trust.ts +35 -3
- package/src/claude/intercept/runtime.ts +2 -2
- package/src/claude/subagent-force-status.ts +24 -5
- package/src/claude/subscription-reserve.ts +87 -0
- package/src/cli/account-api.ts +3 -0
- package/src/cli/account-auth.ts +65 -11
- package/src/cli/account-extended.ts +1 -1
- package/src/cli/account-next-actions.ts +4 -1
- package/src/cli/account-policy-dto.ts +1 -1
- package/src/cli/account-policy.ts +6 -3
- package/src/cli/account-probe.ts +66 -0
- package/src/cli/account.ts +8 -2
- package/src/cli/capabilities-accounts.ts +8 -1
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-base.ts +4 -4
- package/src/cli/capabilities-integrations.ts +44 -4
- package/src/cli/capabilities-provider-models.ts +6 -0
- package/src/cli/claude-context-settings.ts +60 -0
- package/src/cli/claude-desktop.ts +2 -2
- package/src/cli/claude.ts +15 -7
- package/src/cli/client-start-fence.ts +64 -1
- package/src/cli/combo-result.ts +1 -0
- package/src/cli/combo-stats.ts +1 -1
- package/src/cli/combo.ts +19 -2
- package/src/cli/config-command.ts +21 -8
- package/src/cli/connect.ts +16 -0
- package/src/cli/cross-home-owner.ts +65 -18
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/gateway-client-command.ts +128 -0
- package/src/cli/index.ts +98 -97
- package/src/cli/init.ts +20 -4
- package/src/cli/integrations.ts +23 -3
- package/src/cli/launcher-context.ts +12 -1
- package/src/cli/link.ts +24 -5
- package/src/cli/models-runtime-subcommands.ts +1 -0
- package/src/cli/models-runtime.ts +32 -0
- package/src/cli/start-owner-fence.ts +5 -2
- package/src/cli/status.ts +3 -3
- package/src/cli/stop-client-process.ts +51 -0
- package/src/cli/stop-restore.ts +383 -36
- package/src/cli/sync-command.ts +2 -2
- package/src/cli/uninstall-client-state.ts +7 -0
- package/src/client/connect.ts +72 -13
- package/src/client/hub-client.ts +38 -0
- package/src/client/hub-relay.ts +61 -36
- package/src/client/hub-session.ts +106 -0
- package/src/client/lifecycle-lock.ts +6 -1
- package/src/client/link-relay.ts +2 -1
- package/src/client/machine-listener.ts +60 -1
- package/src/client/runtime-restart.ts +1 -0
- package/src/client/runtime.ts +104 -34
- package/src/client/state.ts +11 -0
- package/src/client/stop-darwin-process.ts +73 -0
- package/src/client/stop-file-routing.ts +49 -0
- package/src/client/stop-native-handoff.ts +108 -0
- package/src/client/stop-routing.ts +41 -7
- package/src/client/stop-teardown-owner.ts +173 -0
- package/src/client/sync-runtime.ts +23 -4
- package/src/clients/config-export/opencode-wire.ts +24 -0
- package/src/clients/config-export.ts +28 -19
- package/src/codex/auth-api/login-flow.ts +68 -33
- package/src/codex/auth-api/main-account-probe.ts +3 -3
- package/src/codex/auth-api/pool-quota-probe.ts +8 -7
- package/src/codex/auth-api/routes.ts +1 -1
- package/src/codex/catalog/aggregation.ts +22 -10
- package/src/codex/catalog/gather-capture.ts +11 -2
- package/src/codex/catalog/m365-copilot-models.ts +52 -0
- package/src/codex/catalog/provider-models.ts +126 -53
- package/src/codex/catalog/routed-gather.ts +26 -12
- package/src/codex/catalog-admission.ts +4 -6
- package/src/codex/catalog-auto-refresh.ts +4 -2
- package/src/codex/catalog-self-heal.ts +4 -2
- package/src/codex/control-plane-models.ts +6 -0
- package/src/codex/convergence.ts +10 -0
- package/src/codex/desktop-app/picker-freshness.ts +3 -14
- package/src/codex/desktop-app/types.ts +6 -0
- package/src/codex/desktop-app/windows-command-payload.ts +9 -0
- package/src/codex/desktop-app/windows.ts +20 -12
- package/src/codex/desktop-compatibility/installed-build.ts +61 -58
- package/src/codex/desktop-compatibility/runtime-ownership.ts +11 -5
- package/src/codex/desktop-compatibility/runtime.ts +51 -14
- package/src/codex/desktop-compatibility/windows-activation-source.ts +28 -13
- package/src/codex/desktop-compatibility/windows-package-command.ts +73 -33
- package/src/codex/desktop-compatibility/windows-package-launch.ts +41 -9
- package/src/codex/desktop-compatibility/windows-package-proof.ts +104 -0
- package/src/codex/inject/config-toml.ts +67 -73
- package/src/codex/inject/plan.ts +11 -10
- package/src/codex/inject/remove.ts +4 -1
- package/src/codex/inject.ts +1 -0
- package/src/codex/journal-config-restore.ts +31 -4
- package/src/codex/journal.ts +16 -4
- package/src/codex/model-cache.ts +98 -27
- package/src/codex/native-residue.ts +14 -13
- package/src/codex/paths.ts +65 -28
- package/src/codex/quota-observation-freshness.ts +4 -0
- package/src/codex/quota.ts +71 -8
- package/src/codex/routing/selection.ts +32 -8
- package/src/combos/capability-policy.ts +31 -0
- package/src/combos/failover.ts +82 -74
- package/src/combos/identifiers.ts +2 -2
- package/src/combos/index.ts +3 -0
- package/src/combos/jev-model-backend.ts +12 -4
- package/src/combos/jev-session-pin-config.ts +22 -9
- package/src/combos/jev.ts +15 -4
- package/src/combos/request-capability.ts +214 -0
- package/src/combos/request.ts +22 -15
- package/src/combos/resolve.ts +5 -2
- package/src/combos/suggestion-channel.ts +89 -0
- package/src/combos/types.ts +14 -1
- package/src/config/account-auto-pause-revisions.ts +33 -10
- package/src/config/atomic-write.ts +1 -1
- package/src/config/codex-ws-incremental-setting.ts +34 -0
- package/src/config/diagnostics.ts +47 -8
- package/src/config/live-topology.ts +4 -2
- package/src/config/load-degrade.ts +96 -2
- package/src/config/mutation-lock.ts +7 -3
- package/src/config/paths.ts +3 -0
- package/src/config/pending-teardown.ts +19 -3
- package/src/config/process-state.ts +30 -12
- package/src/config/provider-validation.ts +10 -0
- package/src/config/salvage.ts +2 -2
- package/src/config/schema/account-auto-pause.ts +15 -1
- package/src/config/schema/agy-acp-references.ts +257 -0
- package/src/config/schema/config-schema.ts +32 -0
- package/src/config/schema/leaf-validators.ts +42 -0
- package/src/config/schema/subscription-reserve.ts +33 -0
- package/src/config/windows-process-command-line.ts +27 -9
- package/src/config.ts +6 -0
- package/src/generated/compatibility-version.json +806 -402
- package/src/images/loop.ts +79 -39
- package/src/integrations/catalog-refresh.ts +19 -1
- package/src/integrations/gateway-clients/agy.ts +340 -0
- package/src/integrations/gateway-clients/antigravity.ts +224 -0
- package/src/integrations/gateway-clients/index.ts +89 -0
- package/src/integrations/gateway-clients/io.ts +278 -0
- package/src/integrations/gateway-clients/types.ts +80 -0
- package/src/integrations/route-intent-disconnect.ts +37 -21
- package/src/lib/app-owned-memory-stores.ts +9 -0
- package/src/lib/cloud-code-appeal-link.ts +30 -0
- package/src/lib/errors.ts +32 -0
- package/src/lib/executor-physical-send-receipt.ts +42 -0
- package/src/lib/failure-detail.ts +1 -0
- package/src/lib/gcp-adc.ts +5 -0
- package/src/lib/http-date.ts +91 -0
- package/src/lib/lab-passive-linker-registration.ts +2 -1
- package/src/lib/local-chat-completion.ts +13 -3
- package/src/lib/local-internal-call-capability.ts +1 -3
- package/src/lib/messages-error-body.ts +2 -1
- package/src/lib/outbound-credential-registry.ts +40 -4
- package/src/lib/pinned-http.ts +7 -2
- package/src/lib/process-control.ts +1 -0
- package/src/lib/provider-egress.ts +16 -0
- package/src/lib/provider-outbound.ts +3 -0
- package/src/lib/redact-folding.ts +12 -3
- package/src/lib/redact.ts +37 -19
- package/src/lib/request-execution-budget.ts +241 -32
- package/src/lib/request-peer-scope.ts +64 -0
- package/src/lib/response-send-readings.ts +41 -0
- package/src/lib/state-store-registrations.ts +5 -0
- package/src/lib/tool-envelope-echo-filter.ts +33 -5
- package/src/lib/upstream-condition.ts +10 -3
- package/src/lib/upstream-retry.ts +95 -20
- package/src/lib/windows-secret-acl.ts +24 -7
- package/src/link/child-activity.ts +60 -4
- package/src/link/codex-settings.ts +2 -1
- package/src/link/relay-auth.ts +1 -0
- package/src/link/route-rounds.ts +32 -9
- package/src/link/status-projection.ts +42 -5
- package/src/link/supervisor.ts +96 -11
- package/src/link/tunnel-history.ts +5 -0
- package/src/oauth/account-auto-pause-policy.ts +72 -19
- package/src/oauth/account-auto-pause.ts +40 -16
- package/src/oauth/anthropic-account-refusal.ts +7 -0
- package/src/oauth/anthropic-continuity.ts +70 -9
- package/src/oauth/anthropic-identity.ts +75 -11
- package/src/oauth/anthropic-login-slot.ts +258 -0
- package/src/oauth/anthropic-routing.ts +24 -9
- package/src/oauth/anthropic-send-ownership.ts +4 -3
- package/src/oauth/anthropic.ts +30 -11
- package/src/oauth/health.ts +7 -2
- package/src/oauth/index.ts +50 -48
- package/src/oauth/key-providers.ts +2 -0
- package/src/oauth/login-cli.ts +11 -0
- package/src/oauth/login-flow-state.ts +10 -4
- package/src/oauth/m365-copilot-browser.ts +222 -0
- package/src/oauth/m365-copilot-web-session.ts +25 -0
- package/src/oauth/m365-copilot.ts +57 -0
- package/src/oauth/model-discovery-auth.ts +5 -0
- package/src/oauth/plan.ts +44 -0
- package/src/oauth/pool-kernel.ts +22 -8
- package/src/oauth/pool-settings-capability.ts +14 -3
- package/src/oauth/reauth-precheck.ts +27 -0
- package/src/oauth/store-anthropic-instance.ts +3 -1
- package/src/oauth/store.ts +121 -23
- package/src/oauth/types.ts +27 -2
- package/src/protocols/codecs/chat-ir.ts +7 -4
- package/src/protocols/codecs/gemini-interactions.ts +770 -0
- package/src/protocols/codecs/gemini.ts +470 -0
- package/src/protocols/contract.ts +2 -0
- package/src/protocols/encoders/adapter-events.ts +5 -3
- package/src/protocols/encoders/chat.ts +23 -7
- package/src/protocols/settings.ts +3 -0
- package/src/providers/account-quota-disk.ts +26 -8
- package/src/providers/api-key-pause.ts +2 -2
- package/src/providers/api-key-response-ownership.ts +14 -4
- package/src/providers/claude-cli-usage-state.ts +1 -0
- package/src/providers/claude-cli-usage.ts +24 -7
- package/src/providers/default-aliases.ts +11 -5
- package/src/providers/derive.ts +5 -1
- package/src/providers/fastwire.ts +6 -5
- package/src/providers/free-directory.ts +2 -2
- package/src/providers/google-vertex-location.ts +2 -1
- package/src/providers/google-vertex-model-discovery.ts +22 -0
- package/src/providers/key-failover.ts +18 -23
- package/src/providers/m365-copilot-discovery-fetch.ts +17 -0
- package/src/providers/m365-copilot-discovery.ts +68 -0
- package/src/providers/m365-copilot-models.ts +32 -0
- package/src/providers/model-rename-fields.ts +6 -0
- package/src/providers/new-model-policy-runtime.ts +27 -6
- package/src/providers/openai-forward-destination.ts +19 -0
- package/src/providers/openai-tiers-destination.ts +3 -19
- package/src/providers/quota/account-cache.ts +183 -27
- package/src/providers/quota/anthropic-account-quota.ts +21 -17
- package/src/providers/quota/anthropic-cooldown-recovery.ts +57 -27
- package/src/providers/quota/anthropic-decision-windows.ts +7 -3
- package/src/providers/quota/anthropic-usage-windows.ts +34 -4
- package/src/providers/quota/antigravity-account-probe.ts +165 -0
- package/src/providers/quota/antigravity.ts +71 -3
- package/src/providers/quota/vendor-probes-oauth.ts +52 -28
- package/src/providers/quota-decision-publication.ts +1 -1
- package/src/providers/quota-decision-snapshot.ts +4 -1
- package/src/providers/quota-types.ts +2 -0
- package/src/providers/quota.ts +8 -7
- package/src/providers/registry/agy-acp-models.ts +3 -0
- package/src/providers/registry/entries-core.ts +9 -0
- package/src/providers/registry/entries-extended.ts +60 -0
- package/src/providers/registry/model-seeds.ts +51 -0
- package/src/providers/request-pacing.ts +37 -7
- package/src/providers/service-tier.ts +2 -0
- package/src/providers/xai-privacy-mode.ts +52 -0
- package/src/remote/hub-management-relay.ts +74 -0
- package/src/responses/id-limits.ts +2 -0
- package/src/responses/input-media.ts +15 -3
- package/src/responses/parser-tools.ts +74 -11
- package/src/responses/parser.ts +16 -14
- package/src/responses/provider-opaque-metadata.ts +10 -0
- package/src/responses/reasoning-envelope.ts +3 -0
- package/src/responses/schema.ts +2 -3
- package/src/responses/spill-store.ts +5 -3
- package/src/responses/state/connection-scope.ts +232 -0
- package/src/responses/state/entry-types.ts +47 -0
- package/src/responses/state/snapshot-codec.ts +75 -14
- package/src/responses/state/snapshot-select.ts +2 -0
- package/src/responses/state/spill-codec.ts +59 -0
- package/src/responses/state/spill-queue.ts +11 -2
- package/src/responses/state.ts +178 -159
- package/src/router.ts +2 -6
- package/src/routing/compatibility/behavior.ts +6 -2
- package/src/routing/compatibility/subject.ts +17 -12
- package/src/routing/machine-weighted-routing.ts +58 -0
- package/src/server/advisor-admission-scope.ts +15 -7
- package/src/server/auth-cors.ts +27 -0
- package/src/server/chat-completions.ts +37 -17
- package/src/server/chat-native-eligibility.ts +21 -0
- package/src/server/chat-native.ts +15 -6
- package/src/server/claude-messages.ts +54 -6
- package/src/server/credential-recovery-attempt.ts +32 -0
- package/src/server/effort-policy.ts +6 -3
- package/src/server/gemini-inbound.ts +267 -0
- package/src/server/gemini-interactions-inbound.ts +229 -0
- package/src/server/gemini-interactions-store.ts +129 -0
- package/src/server/gemini-native-catalog.ts +64 -0
- package/src/server/gemini-native-log.ts +33 -0
- package/src/server/gemini-native-paths.ts +88 -0
- package/src/server/gemini-native-wire.ts +201 -0
- package/src/server/gemini-native.ts +218 -0
- package/src/server/grok-upstream-envelope-echo.ts +8 -2
- package/src/server/hub-relay-auth.ts +87 -0
- package/src/server/hub-usage-windows.ts +135 -0
- package/src/server/hub-usage.ts +16 -3
- package/src/server/inbound-body-admission.ts +2 -1
- package/src/server/index/anthropic-model-listing.ts +75 -0
- package/src/server/index/gemini-routes.ts +147 -0
- package/src/server/index/link-management.ts +3 -22
- package/src/server/index/link-relay-sessions.ts +71 -13
- package/src/server/index/link-websocket.ts +2 -0
- package/src/server/index/passive-management-read.ts +55 -0
- package/src/server/index/serve-options.ts +93 -107
- package/src/server/index/websocket-handler.ts +15 -2
- package/src/server/index.ts +10 -0
- package/src/server/inference/client-encoder-delivery.ts +2 -1
- package/src/server/inference/context.ts +33 -6
- package/src/server/management/agent-settings-routes.ts +33 -16
- package/src/server/management/claude-code-settings-patch.ts +6 -0
- package/src/server/management/combo-routes.ts +21 -1
- package/src/server/management/combo-suggestion-routes.ts +212 -0
- package/src/server/management/config-routes.ts +12 -4
- package/src/server/management/context.ts +2 -0
- package/src/server/management/gateway-client-routes.ts +62 -0
- package/src/server/management/integration-routes.ts +5 -0
- package/src/server/management/link-routes.ts +5 -1
- package/src/server/management/m365-copilot-provider-probe.ts +23 -0
- package/src/server/management/model-routes.ts +32 -2
- package/src/server/management/model-rows.ts +36 -3
- package/src/server/management/oauth-account-routes.ts +68 -15
- package/src/server/management/protocol-settings-patch.ts +1 -0
- package/src/server/management/provider-omitted-fields.ts +25 -0
- package/src/server/management/provider-overwrite-carry.ts +33 -8
- package/src/server/management/provider-routes.ts +10 -16
- package/src/server/management/request-history-routes.ts +2 -2
- package/src/server/management/route-registry.ts +10 -0
- package/src/server/management/shared.ts +118 -30
- package/src/server/management/sibling-guard.ts +2 -1
- package/src/server/management/system-routes.ts +6 -0
- package/src/server/management-auth.ts +8 -1
- package/src/server/messages-native-scope.ts +19 -0
- package/src/server/messages-native.ts +156 -59
- package/src/server/messages-shared.ts +6 -0
- package/src/server/models-capabilities.ts +24 -0
- package/src/server/native-chat-bridge.ts +259 -0
- package/src/server/ollama-inbound.ts +36 -184
- package/src/server/passive-route-linker.ts +3 -1
- package/src/server/port-reclaim.ts +48 -4
- package/src/server/proxy-liveness.ts +79 -22
- package/src/server/relay.ts +16 -9
- package/src/server/request-log-account-rotation.ts +2 -1
- package/src/server/request-log-attempt-recovery.ts +24 -2
- package/src/server/request-log-attempt-usage.ts +5 -4
- package/src/server/request-log-committed-terminal.ts +87 -0
- package/src/server/request-log-failure-attribution.ts +3 -3
- package/src/server/request-log-failure-detail.ts +34 -0
- package/src/server/request-log-key-failure.ts +27 -0
- package/src/server/request-log-terminal-status.ts +25 -12
- package/src/server/request-log.ts +59 -63
- package/src/server/responses/adapter-continuation.ts +77 -44
- package/src/server/responses/adapter-delivery.ts +47 -22
- package/src/server/responses/adapter-dispatch.ts +150 -149
- package/src/server/responses/advisor-retention.ts +12 -4
- package/src/server/responses/advisor-slot.ts +97 -2
- package/src/server/responses/agent-task-recovery.ts +44 -11
- package/src/server/responses/agy-acp-preflight.ts +41 -0
- package/src/server/responses/anthropic-image-context.ts +44 -0
- package/src/server/responses/antigravity-validation-refusal.ts +25 -1
- package/src/server/responses/buffered-sse-json.ts +18 -1
- package/src/server/responses/codex-ws-correlation.ts +5 -2
- package/src/server/responses/codex-ws-exchange.ts +91 -8
- package/src/server/responses/codex-ws-incremental.ts +136 -0
- package/src/server/responses/codex-ws-pool.ts +22 -8
- package/src/server/responses/codex-ws-request.ts +5 -0
- package/src/server/responses/codex-ws-send-accounting.ts +8 -0
- package/src/server/responses/codex-ws-session.ts +14 -0
- package/src/server/responses/codex-ws-wire.ts +44 -22
- package/src/server/responses/combo-admission-snapshot.ts +93 -0
- package/src/server/responses/combo-required-tool-stream.ts +16 -4
- package/src/server/responses/combo-selection-keepalive.ts +5 -0
- package/src/server/responses/combo-stream-preflight.ts +182 -73
- package/src/server/responses/compact-response-body.ts +27 -0
- package/src/server/responses/compact.ts +174 -121
- package/src/server/responses/compaction-recovery.ts +7 -0
- package/src/server/responses/core-codex-account.ts +88 -62
- package/src/server/responses/core-combo-failure.ts +15 -0
- package/src/server/responses/core-combo-native.ts +12 -1
- package/src/server/responses/core-combo.ts +340 -94
- package/src/server/responses/core-normalize.ts +19 -12
- package/src/server/responses/core-opaque-recovery.ts +15 -4
- package/src/server/responses/core-options.ts +50 -5
- package/src/server/responses/core-pipeline.ts +11 -0
- package/src/server/responses/core.ts +7 -1
- package/src/server/responses/empty-completion-guard.ts +21 -7
- package/src/server/responses/estimated-usage.ts +2 -1
- package/src/server/responses/fetch-helpers.ts +95 -19
- package/src/server/responses/history-slim.ts +204 -0
- package/src/server/responses/hosted-advisor-spend.ts +49 -0
- package/src/server/responses/image-context-recovery.ts +199 -0
- package/src/server/responses/input-admission.ts +36 -11
- package/src/server/responses/jev-model-invoke.ts +23 -16
- package/src/server/responses/non-replayable-error.ts +12 -2
- package/src/server/responses/passthrough-build-error.ts +5 -3
- package/src/server/responses/passthrough-delivery.ts +74 -33
- package/src/server/responses/passthrough-dispatch.ts +170 -197
- package/src/server/responses/policy-fallback.ts +7 -2
- package/src/server/responses/policy-request-scope.ts +36 -2
- package/src/server/responses/replay-owner.ts +20 -0
- package/src/server/responses/request-prepare.ts +97 -16
- package/src/server/responses/request-send-budget.ts +97 -25
- package/src/server/responses/request-sidecar-auth.ts +138 -65
- package/src/server/responses/request-spend-target.ts +13 -1
- package/src/server/responses/request-spend.ts +228 -47
- package/src/server/responses/request-transport.ts +395 -70
- package/src/server/responses/run-turn-execution.ts +40 -9
- package/src/server/responses/sidecar-execution.ts +214 -67
- package/src/server/responses/sidecar-send-budget.ts +9 -9
- package/src/server/responses/terminal-guard.ts +35 -9
- package/src/server/responses/ws-upstream.ts +27 -6
- package/src/server/responses-phase-inference.ts +32 -9
- package/src/server/responses-terminal-repair.ts +14 -35
- package/src/server/system-env.ts +8 -5
- package/src/server/ws-bridge.ts +23 -17
- package/src/service/health-sentinel-linux.ts +16 -7
- package/src/service/systemd.ts +11 -2
- package/src/service/windows-checked-stop-live.ts +60 -55
- package/src/service/windows-checked-stop.ts +29 -11
- package/src/service/windows-guarded-manager.ts +14 -26
- package/src/service/windows-process-evidence.ts +82 -74
- package/src/service/windows-process-native.ts +69 -20
- package/src/service/windows-service-stop.ts +14 -12
- package/src/service/windows-stop-binding.ts +14 -5
- package/src/service/windows-stop-confirm.ts +1 -1
- package/src/service/windows-task-instance-stop.ts +98 -0
- package/src/service/windows-taskxml.ts +30 -7
- package/src/sidecar/anthropic-binding.ts +1 -1
- package/src/stall-timeout.ts +30 -0
- package/src/telemetry/aggregate.ts +6 -3
- package/src/types/account-auto-pause.ts +19 -1
- package/src/types/accounts.ts +21 -0
- package/src/types/anthropic-account-pool.ts +1 -1
- package/src/types/combo-capability-policy.ts +19 -0
- package/src/types/config.ts +22 -2
- package/src/types/provider.ts +42 -8
- package/src/types/request.ts +15 -0
- package/src/types/tools.ts +22 -5
- package/src/types/wire.ts +1 -0
- package/src/types.ts +4 -0
- package/src/update/check-types.ts +2 -0
- package/src/update/job.ts +8 -3
- package/src/update/managed-release.ts +6 -0
- package/src/update/orchestrated-entry.ts +13 -4
- package/src/update/refresh-scheduler.ts +17 -7
- package/src/usage/attempt-delivery.ts +6 -0
- package/src/usage/cache-provenance.ts +71 -0
- package/src/usage/cost.ts +216 -27
- package/src/usage/credit-billing.ts +88 -0
- package/src/usage/jev-stats.ts +13 -9
- package/src/usage/log.ts +5 -33
- package/src/usage/summary.ts +8 -43
- package/src/usage/user-cost-overlays.ts +13 -1
- package/src/vision/index.ts +8 -3
- package/src/vision/plan.ts +28 -14
- package/src/vision/routed-describe.ts +24 -36
- package/src/web-search/header-deadline.ts +14 -3
- package/src/web-search/index.ts +6 -4
- package/src/web-search/loop.ts +240 -78
- package/src/web-search/progress-stream.ts +29 -2
- package/src/web-search/run-turn-loop.ts +5 -1
- package/gui/dist/assets/App-BGP8I0V4.css +0 -1
- package/gui/dist/assets/App-BRExzhwL.js +0 -60
- package/gui/dist/assets/Tray-DHCWAn8V.js +0 -1
- package/gui/dist/assets/brand-mark-C70PuQ3j.js +0 -1
- package/gui/dist/assets/index-DJxM-imn.css +0 -2
- package/gui/dist/assets/index-Dy1jIGty.js +0 -138
- package/gui/dist/classic/assets/App-B4j-Txmb.css +0 -1
- package/gui/dist/classic/assets/App-CaC-E6Vl.js +0 -59
- package/gui/dist/classic/assets/Tray-BG253fiL.js +0 -1
- package/gui/dist/classic/assets/index-CR_AThOp.js +0 -138
- package/gui/dist/classic/assets/quota-summary-B7KQPpEC.js +0 -1
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { X509Certificate } from "node:crypto";
|
|
3
|
+
import { lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync, type BigIntStats } from "node:fs";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
|
+
import { dirname, isAbsolute, join, sep } from "node:path";
|
|
6
|
+
import { readBoundedResponseBytes } from "../../lib/bounded-body";
|
|
7
|
+
import { acceptsPickerAuthority, PICKER_CA_COMMON_NAME, sameLinuxPickerTrustStore, type LinuxPickerTrustStore } from "./picker-ca";
|
|
8
|
+
import type { PickerTrustState, SecurityResult } from "./picker-trust";
|
|
9
|
+
|
|
10
|
+
export type LinuxTrustRunner = (args: readonly string[]) => Promise<SecurityResult>;
|
|
11
|
+
|
|
12
|
+
export const LINUX_TRUST_TIMEOUT_MS = 15_000;
|
|
13
|
+
const OUTPUT_LIMIT = 4 * 1024 * 1024;
|
|
14
|
+
export const LINUX_TRUST_TRUNCATED = "certutil_output_truncated";
|
|
15
|
+
const MISSING = "certutil_missing";
|
|
16
|
+
const NSS_FILES = ["cert9.db", "key4.db", "pkcs11.txt", "cert8.db", "key3.db", "secmod.db",
|
|
17
|
+
...["cert9.db", "key4.db"].flatMap(db => ["-journal", "-shm", "-wal"].map(suffix => db + suffix))];
|
|
18
|
+
|
|
19
|
+
function statIfPresent(path: string): BigIntStats | undefined {
|
|
20
|
+
try {
|
|
21
|
+
return lstatSync(path, { bigint: true });
|
|
22
|
+
} catch (error) {
|
|
23
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
24
|
+
if (code === "ENOENT" || code === "ENOTDIR") return undefined;
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function legacyDbDir(home: string): string {
|
|
30
|
+
if (!isAbsolute(home) || home.split(sep).includes("..")) throw new Error("unsafe NSS home");
|
|
31
|
+
return join(home, ".pki", "nssdb");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function xdgDbDir(home: string, xdgDataHome?: string): string {
|
|
35
|
+
const data = xdgDataHome || join(home, ".local", "share");
|
|
36
|
+
if (!isAbsolute(data) || data.split(sep).includes("..")) throw new Error("unsafe NSS data home");
|
|
37
|
+
return join(data, "pki", "nssdb");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function linuxNssDbDir(home = homedir(), xdgDataHome?: string): string {
|
|
41
|
+
// Chromium crypto/nss_util.cc:GetDefaultNSSConfigDirectory prefers an existing legacy
|
|
42
|
+
// directory, then uses ${XDG_DATA_HOME:-$HOME/.local/share}/pki/nssdb.
|
|
43
|
+
// https://chromium.googlesource.com/chromium/src/+/main/crypto/nss_util.cc
|
|
44
|
+
const legacy = legacyDbDir(home);
|
|
45
|
+
const stat = statIfPresent(legacy);
|
|
46
|
+
// A legacy symlink may be Chromium's active store. Select it so the safety check refuses it.
|
|
47
|
+
if (stat?.isDirectory() || stat?.isSymbolicLink()) return legacy;
|
|
48
|
+
return xdgDbDir(home, xdgDataHome);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function linuxPickerNickname(sha1: string): string {
|
|
52
|
+
return `${PICKER_CA_COMMON_NAME} ${sha1}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Check ancestors as well, since a writable parent can replace a safe directory. */
|
|
56
|
+
function safeDirectory(path: string, database: boolean, uid: bigint | undefined): BigIntStats | undefined {
|
|
57
|
+
const parent = dirname(path);
|
|
58
|
+
if (parent !== path) safeDirectory(parent, false, uid);
|
|
59
|
+
const stat = statIfPresent(path);
|
|
60
|
+
if (!stat) return undefined;
|
|
61
|
+
if (!database && uid !== undefined && stat.uid === 0n && stat.isSymbolicLink()) {
|
|
62
|
+
// Trusted system aliases (for example /var) may lead to an otherwise safe ancestor.
|
|
63
|
+
return safeDirectory(realpathSync(path), false, uid);
|
|
64
|
+
}
|
|
65
|
+
if (!stat.isDirectory() || (uid !== undefined && (
|
|
66
|
+
(stat.uid !== uid && (database || stat.uid !== 0n))
|
|
67
|
+
|| ((stat.mode & 0o022n) !== 0n && (database || (stat.mode & 0o1000n) === 0n))
|
|
68
|
+
))) throw new Error("unsafe NSS directory");
|
|
69
|
+
return stat;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function openNssDb(dir: string) {
|
|
73
|
+
const uid = process.platform === "win32" ? undefined : BigInt(process.getuid!());
|
|
74
|
+
const directory = safeDirectory(dir, true, uid);
|
|
75
|
+
const files = new Map<string, BigIntStats>();
|
|
76
|
+
if (directory) {
|
|
77
|
+
for (const name of NSS_FILES) {
|
|
78
|
+
const stat = statIfPresent(join(dir, name));
|
|
79
|
+
if (!stat) continue;
|
|
80
|
+
if (!stat.isFile() || stat.nlink !== 1n || (uid !== undefined && (stat.uid !== uid || (stat.mode & 0o022n)))) {
|
|
81
|
+
throw new Error("unsafe NSS database file");
|
|
82
|
+
}
|
|
83
|
+
files.set(name, stat);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { dir, db: `sql:${dir}`, directory, files };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type NssDb = ReturnType<typeof openNssDb>;
|
|
90
|
+
|
|
91
|
+
function sameDirectory(before: NssDb, after: NssDb): boolean {
|
|
92
|
+
return before.dir === after.dir && before.directory !== undefined && after.directory !== undefined
|
|
93
|
+
&& before.directory.ino !== 0n
|
|
94
|
+
&& before.directory.dev === after.directory.dev && before.directory.ino === after.directory.ino;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function identifyStore(db: NssDb): LinuxPickerTrustStore | undefined {
|
|
98
|
+
const certDb = db.files.get("cert9.db");
|
|
99
|
+
if (!db.directory?.ino || !certDb?.ino) return undefined;
|
|
100
|
+
return { dir: realpathSync(db.dir), directoryId: `${db.directory.dev}:${db.directory.ino}`,
|
|
101
|
+
certDbId: `${certDb.dev}:${certDb.ino}` };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function unchanged(db: NssDb, store: LinuxPickerTrustStore | undefined): boolean {
|
|
105
|
+
const current = openNssDb(db.dir);
|
|
106
|
+
return sameDirectory(db, current) && sameLinuxPickerTrustStore(store, identifyStore(current));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function resolveNssDb(home?: string, xdgDataHome = home === undefined ? process.env.XDG_DATA_HOME : undefined): NssDb {
|
|
110
|
+
return openNssDb(linuxNssDbDir(home, xdgDataHome));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** certutil runner with a bounded runtime and output, code null when the executable is absent. */
|
|
114
|
+
export function createLinuxCertutilRunner(exe: string | null = Bun.which("certutil"), env?: NodeJS.ProcessEnv): LinuxTrustRunner {
|
|
115
|
+
return args => runCertutil(exe, args, env);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const defaultLinuxCertutilRunner: LinuxTrustRunner = args => runCertutil(Bun.which("certutil"), args);
|
|
119
|
+
|
|
120
|
+
async function runCertutil(exe: string | null, args: readonly string[], env = process.env): Promise<SecurityResult> {
|
|
121
|
+
if (!exe) return { code: null, stdout: "", stderr: MISSING };
|
|
122
|
+
const abort = new AbortController();
|
|
123
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
124
|
+
try {
|
|
125
|
+
const proc = Bun.spawn([exe, ...args], { stdin: "ignore", stdout: "pipe", stderr: "pipe",
|
|
126
|
+
env: { ...env, LC_ALL: "C" } });
|
|
127
|
+
const capture = async (pipe: ReadableStream<Uint8Array>): Promise<string> => {
|
|
128
|
+
const result = await readBoundedResponseBytes(new Response(pipe), { maxBytes: OUTPUT_LIMIT, signal: abort.signal });
|
|
129
|
+
if (result.oversized) throw new Error(LINUX_TRUST_TRUNCATED);
|
|
130
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(result.bytes);
|
|
131
|
+
};
|
|
132
|
+
try {
|
|
133
|
+
const deadline = new Promise<never>((_, reject) => {
|
|
134
|
+
timer = setTimeout(() => reject(new Error("certutil timed out")), LINUX_TRUST_TIMEOUT_MS);
|
|
135
|
+
});
|
|
136
|
+
const [stdout, stderr, code] = await Promise.race([
|
|
137
|
+
Promise.all([capture(proc.stdout), capture(proc.stderr), proc.exited]),
|
|
138
|
+
deadline,
|
|
139
|
+
]);
|
|
140
|
+
return { code, stdout, stderr };
|
|
141
|
+
} catch (error) {
|
|
142
|
+
try { proc.kill("SIGKILL"); } catch { /* The child may already have exited. */ }
|
|
143
|
+
abort.abort(error);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return { code: null, stdout: "", stderr: error instanceof Error ? error.message : String(error) };
|
|
148
|
+
} finally {
|
|
149
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function normalizeSha1(value: string): string | null {
|
|
154
|
+
const normalized = value.replace(/[:\s]/g, "").toUpperCase();
|
|
155
|
+
return /^[0-9A-F]{40}$/.test(normalized) ? normalized : null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function missingTool(result: SecurityResult): boolean {
|
|
159
|
+
return result.code === null && result.stderr === MISSING;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function certificate(pem: string): X509Certificate | null {
|
|
163
|
+
if (!/^\s*-----BEGIN CERTIFICATE-----[\sA-Za-z0-9+/=]+-----END CERTIFICATE-----\s*$/.test(pem)) return null;
|
|
164
|
+
try { return new X509Certificate(pem); }
|
|
165
|
+
catch { return null; }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
type Listing = { kind: "listed"; entries: Map<string, string> } | { kind: "missing_tool" | "failed" };
|
|
169
|
+
|
|
170
|
+
async function listCertificates(run: LinuxTrustRunner, db: string): Promise<Listing> {
|
|
171
|
+
const result = await run(["-L", "-d", db]);
|
|
172
|
+
if (missingTool(result)) return { kind: "missing_tool" };
|
|
173
|
+
if (result.code !== 0 || result.stderr.trim()) return { kind: "failed" };
|
|
174
|
+
const entries = new Map<string, string>();
|
|
175
|
+
const lines = result.stdout.trim().split(/\r?\n/);
|
|
176
|
+
if (!/^Certificate Nickname\s+Trust Attributes\s*$/.test(lines.shift() ?? "")
|
|
177
|
+
|| lines.shift()?.trim() !== "SSL,S/MIME,JAR/XPI") return { kind: "failed" };
|
|
178
|
+
for (const line of lines) {
|
|
179
|
+
if (!line.trim()) continue;
|
|
180
|
+
const match = /^(\S(?:.*\S)?)\s+([A-Za-z]*,[A-Za-z]*,[A-Za-z]*)\s*$/.exec(line);
|
|
181
|
+
if (!match || entries.has(match[1]!)) return { kind: "failed" };
|
|
182
|
+
entries.set(match[1]!, match[2]!);
|
|
183
|
+
}
|
|
184
|
+
return { kind: "listed", entries };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function exportCertificate(run: LinuxTrustRunner, db: NssDb, nickname: string): Promise<X509Certificate | null> {
|
|
188
|
+
const result = await run(["-L", "-d", db.db, "-n", nickname, "-a"]);
|
|
189
|
+
if (result.code !== 0 || result.stderr.trim()) return null;
|
|
190
|
+
const text = result.stdout.trim();
|
|
191
|
+
const blocks = text.match(/-----BEGIN CERTIFICATE-----[\sA-Za-z0-9+/=]+-----END CERTIFICATE-----/g);
|
|
192
|
+
if (!blocks || text.replace(/-----BEGIN CERTIFICATE-----[\sA-Za-z0-9+/=]+-----END CERTIFICATE-----/g, "").trim()) return null;
|
|
193
|
+
const certificates = blocks.map(certificate);
|
|
194
|
+
if (certificates.some(cert => cert === null)) return null;
|
|
195
|
+
if (certificates.length === 1) return certificates[0]!;
|
|
196
|
+
|
|
197
|
+
// Both -a and -r enumerate the entire subject group. Bind the result to the internal
|
|
198
|
+
// SQL token's exact CKA_LABEL and CKA_VALUE (NSS columns a3 and a11). Read only and
|
|
199
|
+
// fail closed on an unexpected schema, a non-local token or an ambiguous label.
|
|
200
|
+
if (!db.files.has("cert9.db")) return null;
|
|
201
|
+
using sql = new Database(join(db.dir, "cert9.db"), { readonly: true });
|
|
202
|
+
const rows = sql.query<{ der: Uint8Array }, [Buffer, number]>(
|
|
203
|
+
"SELECT a11 AS der FROM nssPublic WHERE a0 = X'00000001' AND a3 = ? AND length(a11) <= ? LIMIT 2",
|
|
204
|
+
).all(Buffer.from(nickname), OUTPUT_LIMIT);
|
|
205
|
+
return rows.length === 1 ? certificates.find(cert => cert?.raw.equals(rows[0]!.der)) ?? null : null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
interface NssEntry { fingerprint: string; flags: string }
|
|
209
|
+
|
|
210
|
+
async function inventory(run: LinuxTrustRunner, db: NssDb): Promise<Map<string, NssEntry> | null> {
|
|
211
|
+
const start = performance.now();
|
|
212
|
+
const listing = await listCertificates(run, db.db);
|
|
213
|
+
if (listing.kind !== "listed") return null;
|
|
214
|
+
const certs = new Map<string, NssEntry>();
|
|
215
|
+
for (const [nickname, flags] of listing.entries) {
|
|
216
|
+
if (performance.now() - start >= LINUX_TRUST_TIMEOUT_MS) return null;
|
|
217
|
+
const cert = await exportCertificate(run, db, nickname);
|
|
218
|
+
if (!cert) return null;
|
|
219
|
+
certs.set(nickname, { fingerprint: cert.fingerprint256, flags });
|
|
220
|
+
}
|
|
221
|
+
if (performance.now() - start >= LINUX_TRUST_TIMEOUT_MS) return null;
|
|
222
|
+
const after = await listCertificates(run, db.db);
|
|
223
|
+
if (after.kind !== "listed" || after.entries.size !== listing.entries.size) return null;
|
|
224
|
+
for (const [nickname, flags] of listing.entries) {
|
|
225
|
+
if (after.entries.get(nickname) !== flags) return null;
|
|
226
|
+
}
|
|
227
|
+
return certs;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export async function inspectPickerTrustLinux(
|
|
231
|
+
caSha1: string,
|
|
232
|
+
run: LinuxTrustRunner = defaultLinuxCertutilRunner,
|
|
233
|
+
home?: string,
|
|
234
|
+
xdgDataHome?: string,
|
|
235
|
+
): Promise<PickerTrustState> {
|
|
236
|
+
const sha1 = normalizeSha1(caSha1);
|
|
237
|
+
if (!sha1) return "untrusted";
|
|
238
|
+
try {
|
|
239
|
+
const selected = resolveNssDb(home, xdgDataHome);
|
|
240
|
+
if (!selected.directory) return "untrusted";
|
|
241
|
+
const store = identifyStore(selected);
|
|
242
|
+
const listing = await listCertificates(run, selected.db);
|
|
243
|
+
if (listing.kind === "missing_tool") return "unsupported";
|
|
244
|
+
if (listing.kind !== "listed") return "unknown";
|
|
245
|
+
for (const nickname of [linuxPickerNickname(sha1), PICKER_CA_COMMON_NAME]) {
|
|
246
|
+
const flags = listing.entries.get(nickname);
|
|
247
|
+
if (flags === undefined) continue;
|
|
248
|
+
const cert = await exportCertificate(run, selected, nickname);
|
|
249
|
+
if (!cert || !unchanged(selected, store)) return "unknown";
|
|
250
|
+
if (normalizeSha1(cert.fingerprint) === sha1) return flags.split(",")[0]!.includes("C") ? "trusted" : "untrusted";
|
|
251
|
+
}
|
|
252
|
+
return "untrusted";
|
|
253
|
+
} catch { // no-excuse-ok: catch -- OS read unavailable or denied; never claim trust.
|
|
254
|
+
return "unknown";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export async function trustPickerCaLinux(
|
|
259
|
+
_caPath: string,
|
|
260
|
+
run: LinuxTrustRunner = defaultLinuxCertutilRunner,
|
|
261
|
+
cert?: { pem?: string },
|
|
262
|
+
home?: string,
|
|
263
|
+
xdgDataHome?: string,
|
|
264
|
+
bindStore?: (store: LinuxPickerTrustStore) => void,
|
|
265
|
+
recordedStore?: LinuxPickerTrustStore,
|
|
266
|
+
): Promise<{ ok: boolean; reason?: "unsupported" | "declined_or_failed" }> {
|
|
267
|
+
const failed = { ok: false, reason: "declined_or_failed" } as const;
|
|
268
|
+
let privateDir: string | undefined;
|
|
269
|
+
try {
|
|
270
|
+
const pem = cert?.pem;
|
|
271
|
+
const root = pem && certificate(pem);
|
|
272
|
+
const sha1 = root && normalizeSha1(root.fingerprint);
|
|
273
|
+
if (!pem || !sha1 || !acceptsPickerAuthority(pem)) return failed;
|
|
274
|
+
const selected = resolveNssDb(home, xdgDataHome);
|
|
275
|
+
const tool = await run(["--build-flags"]);
|
|
276
|
+
if (missingTool(tool)) return { ok: false, reason: "unsupported" };
|
|
277
|
+
if (tool.code !== 0) return failed;
|
|
278
|
+
|
|
279
|
+
let current = resolveNssDb(home, xdgDataHome);
|
|
280
|
+
if (selected.dir !== current.dir || (selected.directory && !sameDirectory(selected, current))) return failed;
|
|
281
|
+
if (recordedStore && !sameLinuxPickerTrustStore(recordedStore, identifyStore(current))) return failed;
|
|
282
|
+
if (!current.directory) {
|
|
283
|
+
mkdirSync(current.dir, { recursive: true, mode: 0o700 });
|
|
284
|
+
const created = resolveNssDb(home, xdgDataHome);
|
|
285
|
+
if (created.dir !== current.dir || !created.directory) return failed;
|
|
286
|
+
current = created;
|
|
287
|
+
}
|
|
288
|
+
if (!current.files.has("cert9.db")) {
|
|
289
|
+
// Initialize an empty directory only. An incomplete database needs owner intervention.
|
|
290
|
+
if (current.files.size !== 0) return failed;
|
|
291
|
+
const initialized = await run(["-N", "-d", current.db, "--empty-password"]);
|
|
292
|
+
if (initialized.code !== 0) return failed;
|
|
293
|
+
const ready = resolveNssDb(home, xdgDataHome);
|
|
294
|
+
if (!sameDirectory(current, ready)) return failed;
|
|
295
|
+
current = ready;
|
|
296
|
+
}
|
|
297
|
+
const store = identifyStore(current);
|
|
298
|
+
if (!store || !unchanged(current, store)) return failed;
|
|
299
|
+
privateDir = mkdtempSync(join(tmpdir(), "ocx-picker-ca-"));
|
|
300
|
+
const installPath = join(privateDir, "ca.pem");
|
|
301
|
+
writeFileSync(installPath, pem, { mode: 0o600 });
|
|
302
|
+
bindStore?.(store);
|
|
303
|
+
const added = await run(["-A", "-d", current.db, "-n", linuxPickerNickname(sha1), "-t", "C,,", "-a", "-i", installPath]);
|
|
304
|
+
return added.code === 0 && unchanged(current, store) ? { ok: true } : failed;
|
|
305
|
+
} catch { // no-excuse-ok: catch -- directory or command failure is reported.
|
|
306
|
+
return failed;
|
|
307
|
+
} finally {
|
|
308
|
+
if (privateDir) rmSync(privateDir, { recursive: true, force: true });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
interface NssRemoval {
|
|
313
|
+
selected: NssDb;
|
|
314
|
+
store: LinuxPickerTrustStore | undefined;
|
|
315
|
+
before: Map<string, NssEntry>;
|
|
316
|
+
nickname: string;
|
|
317
|
+
}
|
|
318
|
+
export type LinuxPickerUntrustResult = { ok: boolean; reason?: "original_store_unknown" };
|
|
319
|
+
|
|
320
|
+
async function prepareRemoval(run: LinuxTrustRunner, selected: NssDb, sha1: string, expected: X509Certificate): Promise<NssRemoval | "absent" | "failed"> {
|
|
321
|
+
const store = identifyStore(selected);
|
|
322
|
+
const before = await inventory(run, selected);
|
|
323
|
+
if (!before || !unchanged(selected, store)) return "failed";
|
|
324
|
+
|
|
325
|
+
const nickname = linuxPickerNickname(sha1);
|
|
326
|
+
const target = before.get(nickname);
|
|
327
|
+
if (!target) return [...before.values()].some(cert => cert.fingerprint === expected.fingerprint256) ? "failed" : "absent";
|
|
328
|
+
if (target.fingerprint !== expected.fingerprint256
|
|
329
|
+
|| [...before].some(([name, cert]) => name !== nickname && cert.fingerprint === target.fingerprint)) return "failed";
|
|
330
|
+
return { selected, store, before, nickname };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function removeFromStore(
|
|
334
|
+
run: LinuxTrustRunner, { selected, store, before, nickname }: NssRemoval, expected: X509Certificate,
|
|
335
|
+
bindStore?: (store: LinuxPickerTrustStore) => void,
|
|
336
|
+
): Promise<boolean> {
|
|
337
|
+
const fresh = await listCertificates(run, selected.db);
|
|
338
|
+
if (fresh.kind !== "listed" || !fresh.entries.has(nickname)) return false;
|
|
339
|
+
const verified = await exportCertificate(run, selected, nickname);
|
|
340
|
+
if (!verified?.raw.equals(expected.raw) || !unchanged(selected, store)) return false;
|
|
341
|
+
|
|
342
|
+
if (bindStore) {
|
|
343
|
+
if (!store) return false;
|
|
344
|
+
bindStore(store);
|
|
345
|
+
if (!unchanged(selected, store)) return false;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// -D resolves the nickname again. Keep the pre-delete check and verify that the exact
|
|
349
|
+
// predecessor is gone and the surviving entries kept their certificate and trust flags.
|
|
350
|
+
const deleted = await run(["-D", "-d", selected.db, "-n", nickname]);
|
|
351
|
+
if (deleted.code !== 0 || !unchanged(selected, store)) return false;
|
|
352
|
+
const after = await inventory(run, selected);
|
|
353
|
+
if (!after || !unchanged(selected, store)
|
|
354
|
+
|| [...after.values()].some(cert => cert.fingerprint === expected.fingerprint256)) return false;
|
|
355
|
+
before.delete(nickname);
|
|
356
|
+
return [...before].every(([name, cert]) => {
|
|
357
|
+
const survivor = after.get(name);
|
|
358
|
+
return survivor?.fingerprint === cert.fingerprint && survivor.flags === cert.flags;
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export async function untrustPickerCaLinux(
|
|
363
|
+
fingerprintSha1: string,
|
|
364
|
+
run: LinuxTrustRunner = defaultLinuxCertutilRunner,
|
|
365
|
+
home?: string,
|
|
366
|
+
authority?: { pem: string; linuxStore?: LinuxPickerTrustStore; bindStore?: (store: LinuxPickerTrustStore) => void },
|
|
367
|
+
xdgDataHome = home === undefined ? process.env.XDG_DATA_HOME : undefined,
|
|
368
|
+
): Promise<LinuxPickerUntrustResult> {
|
|
369
|
+
const failed = { ok: false };
|
|
370
|
+
const sha1 = normalizeSha1(fingerprintSha1);
|
|
371
|
+
try {
|
|
372
|
+
const expected = authority && certificate(authority.pem);
|
|
373
|
+
if (!sha1 || !authority || !expected || normalizeSha1(expected.fingerprint) !== sha1
|
|
374
|
+
|| !acceptsPickerAuthority(authority.pem)) return failed;
|
|
375
|
+
if (authority.linuxStore) {
|
|
376
|
+
const recorded = openNssDb(authority.linuxStore.dir);
|
|
377
|
+
if (!sameLinuxPickerTrustStore(authority.linuxStore, identifyStore(recorded))) return failed;
|
|
378
|
+
const removal = await prepareRemoval(run, recorded, sha1, expected);
|
|
379
|
+
if (removal === "failed") return failed;
|
|
380
|
+
return { ok: removal === "absent" || await removeFromStore(run, removal, expected) };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Pre-identity records may refer to either Chromium path. Check existing stores
|
|
384
|
+
// even when Chromium now prefers a different one. Absence alone cannot retire
|
|
385
|
+
// an unbound pending record because a previous custom XDG store may be off this list.
|
|
386
|
+
const userHome = home ?? homedir();
|
|
387
|
+
const dirs = new Set([legacyDbDir(userHome), xdgDbDir(userHome), xdgDbDir(userHome, xdgDataHome)]);
|
|
388
|
+
const candidates = [...dirs].map(dir => ({ dir, db: statIfPresent(dir) ? openNssDb(dir) : undefined }));
|
|
389
|
+
const removals = new Map<string, NssRemoval>();
|
|
390
|
+
for (const { db } of candidates) {
|
|
391
|
+
if (!db) continue;
|
|
392
|
+
const removal = await prepareRemoval(run, db, sha1, expected);
|
|
393
|
+
if (removal === "failed") return failed;
|
|
394
|
+
if (removal !== "absent") removals.set(removal.store?.dir ?? db.dir, removal);
|
|
395
|
+
}
|
|
396
|
+
const candidatesUnchanged = () => candidates.every(({ dir, db }) => db ? unchanged(db, identifyStore(db)) : !statIfPresent(dir));
|
|
397
|
+
if (!candidatesUnchanged()) return failed;
|
|
398
|
+
if (authority.bindStore) {
|
|
399
|
+
if (!removals.size) return { ok: false, reason: "original_store_unknown" };
|
|
400
|
+
// The pending record can carry one store identity. Scan before deleting so an
|
|
401
|
+
// ambiguous match cannot strand trust in a second store after a partial failure.
|
|
402
|
+
if (removals.size !== 1) return failed;
|
|
403
|
+
}
|
|
404
|
+
for (const removal of removals.values()) {
|
|
405
|
+
if (!await removeFromStore(run, removal, expected, authority.bindStore)) return failed;
|
|
406
|
+
}
|
|
407
|
+
return { ok: candidatesUnchanged() };
|
|
408
|
+
} catch { // no-excuse-ok: catch -- any uncertainty keeps the pending record.
|
|
409
|
+
return failed;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir, tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import { PICKER_CA_COMMON_NAME, PICKER_HOST } from "./picker-ca";
|
|
4
|
+
import { PICKER_CA_COMMON_NAME, PICKER_HOST, withPublishedPickerCaLinuxTrust, type LinuxPickerTrustStore } from "./picker-ca";
|
|
5
5
|
import {
|
|
6
6
|
inspectPickerTrustWindows,
|
|
7
7
|
trustPickerCaWindows,
|
|
8
8
|
untrustPickerCaWindows,
|
|
9
9
|
} from "./picker-trust-windows";
|
|
10
|
+
import { inspectPickerTrustLinux, trustPickerCaLinux, untrustPickerCaLinux, type LinuxPickerUntrustResult } from "./picker-trust-linux";
|
|
10
11
|
|
|
11
12
|
/** Trust is scoped to the current root fingerprint and a verified persisted leaf. */
|
|
12
13
|
export type PickerTrustState = "trusted" | "untrusted" | "unsupported" | "unknown";
|
|
@@ -65,12 +66,16 @@ export async function inspectPickerTrust(
|
|
|
65
66
|
caSha1: string,
|
|
66
67
|
run: SecurityRunner = defaultSecurityRunner,
|
|
67
68
|
platform: NodeJS.Platform = process.platform,
|
|
69
|
+
linuxHome?: string,
|
|
68
70
|
): Promise<PickerTrustState> {
|
|
69
71
|
if (platform === "win32") {
|
|
70
72
|
// The macOS default runner is never passed down: only an explicitly injected
|
|
71
73
|
// runner crosses over, otherwise the Windows default powershell.exe runner runs.
|
|
72
74
|
return inspectPickerTrustWindows(leafPath, caSha1, run === defaultSecurityRunner ? undefined : run);
|
|
73
75
|
}
|
|
76
|
+
if (platform === "linux") {
|
|
77
|
+
return inspectPickerTrustLinux(caSha1, run === defaultSecurityRunner ? undefined : run, linuxHome);
|
|
78
|
+
}
|
|
74
79
|
if (platform !== "darwin") return "unsupported";
|
|
75
80
|
const keychain = loginKeychainPath();
|
|
76
81
|
try {
|
|
@@ -94,11 +99,22 @@ export async function trustPickerCa(
|
|
|
94
99
|
caPath: string,
|
|
95
100
|
run: SecurityRunner = defaultSecurityRunner,
|
|
96
101
|
platform: NodeJS.Platform = process.platform,
|
|
97
|
-
cert?: { pem?: string },
|
|
102
|
+
cert?: { pem?: string; configDir?: string },
|
|
103
|
+
linuxHome?: string,
|
|
98
104
|
): Promise<{ ok: boolean; reason?: "unsupported" | "declined_or_failed" }> {
|
|
99
105
|
if (platform === "win32") {
|
|
100
106
|
return trustPickerCaWindows(caPath, run === defaultSecurityRunner ? undefined : run, cert);
|
|
101
107
|
}
|
|
108
|
+
if (platform === "linux") {
|
|
109
|
+
const runner = run === defaultSecurityRunner ? undefined : run;
|
|
110
|
+
if (!cert?.configDir || !cert.pem) return trustPickerCaLinux(caPath, runner, cert, linuxHome);
|
|
111
|
+
try {
|
|
112
|
+
return await withPublishedPickerCaLinuxTrust(cert.configDir, cert.pem, (store, save) =>
|
|
113
|
+
trustPickerCaLinux(caPath, runner, cert, linuxHome, undefined, save, store));
|
|
114
|
+
} catch {
|
|
115
|
+
return { ok: false, reason: "declined_or_failed" };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
102
118
|
if (platform !== "darwin") return { ok: false, reason: "unsupported" };
|
|
103
119
|
// `cert.pem` is the caller-verified certificate bytes: the shared on-disk ca.pem is writable by
|
|
104
120
|
// same-user processes and could be swapped between fingerprint inspection and this install, so
|
|
@@ -129,13 +145,29 @@ export async function untrustPickerCa(
|
|
|
129
145
|
fingerprintSha1: string,
|
|
130
146
|
run: SecurityRunner = defaultSecurityRunner,
|
|
131
147
|
platform: NodeJS.Platform = process.platform,
|
|
132
|
-
|
|
148
|
+
linuxHome?: string,
|
|
149
|
+
linux?: { store?: LinuxPickerTrustStore; configDir?: string; bindStore?: (store: LinuxPickerTrustStore) => void },
|
|
150
|
+
): Promise<LinuxPickerUntrustResult> {
|
|
133
151
|
if (platform === "win32") {
|
|
134
152
|
try {
|
|
135
153
|
return await untrustPickerCaWindows(fingerprintSha1, run === defaultSecurityRunner ? undefined : run,
|
|
136
154
|
{ pem: readFileSync(caPath, "utf8") });
|
|
137
155
|
} catch { return { ok: false }; }
|
|
138
156
|
}
|
|
157
|
+
if (platform === "linux") {
|
|
158
|
+
try {
|
|
159
|
+
const pem = readFileSync(caPath, "utf8");
|
|
160
|
+
const remove = (linuxStore?: LinuxPickerTrustStore) => untrustPickerCaLinux(
|
|
161
|
+
fingerprintSha1, run === defaultSecurityRunner ? undefined : run, linuxHome,
|
|
162
|
+
{ pem, linuxStore, bindStore: linux?.bindStore });
|
|
163
|
+
if (!linux?.configDir) return await remove(linux?.store);
|
|
164
|
+
return await withPublishedPickerCaLinuxTrust(linux.configDir, pem, async (store, _save, clear) => {
|
|
165
|
+
const result = await remove(store);
|
|
166
|
+
if (result.ok) clear();
|
|
167
|
+
return result;
|
|
168
|
+
});
|
|
169
|
+
} catch { return { ok: false }; }
|
|
170
|
+
}
|
|
139
171
|
if (platform !== "darwin") return { ok: false };
|
|
140
172
|
const keychain = loginKeychainPath();
|
|
141
173
|
// Listed means the current picker CA is in the login keychain; unlisted means nothing to remove,
|
|
@@ -216,8 +216,8 @@ export async function startClaudeIntercept<T>(options: StartClaudeInterceptOptio
|
|
|
216
216
|
const fresh = options.config;
|
|
217
217
|
const platform = options.pickerPlatform ?? process.platform;
|
|
218
218
|
const pickerRequired = pickerProfileApplied || pickerDesired(fresh, resolveClaudeDesktopMode(fresh, observeClaudeDesktopMode(fresh)), platform);
|
|
219
|
-
// Preserve macOS's dormant relay; Windows
|
|
220
|
-
pickerWired = platform
|
|
219
|
+
// Preserve macOS's dormant relay; Windows and Linux defer binding until enable or profile recovery.
|
|
220
|
+
pickerWired = platform === "darwin" || pickerRequired;
|
|
221
221
|
if (pickerRequired) {
|
|
222
222
|
await preparePersistentPickerAuthority({ configDir, store: options.pickerCaStore, security: options.pickerSecurity, platform });
|
|
223
223
|
}
|
|
@@ -4,6 +4,10 @@ import { join } from "node:path";
|
|
|
4
4
|
import { claudeConfigDir } from "./gateway-cache";
|
|
5
5
|
import { commandInvocation } from "../lib/win-exec";
|
|
6
6
|
|
|
7
|
+
const VERSION_PROBE_TIMEOUT_MS = 1_500;
|
|
8
|
+
const VERSION_PROBE_CACHE_MS = 30_000;
|
|
9
|
+
let cachedProbe: { key: string; expiresAt: number; result: Promise<string | null> } | undefined;
|
|
10
|
+
|
|
7
11
|
export interface SubagentForceStatus {
|
|
8
12
|
targetValid: boolean;
|
|
9
13
|
version: string | null;
|
|
@@ -19,20 +23,35 @@ export function subagentForceSupport(version: string | null): SubagentForceStatu
|
|
|
19
23
|
return major! > 2 || (major === 2 && (minor! > 1 || (minor === 1 && patch! >= 257))) ? "supported" : "unsupported";
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
function
|
|
26
|
+
export function probeInstalledClaudeCodeVersion(): Promise<string | null> {
|
|
27
|
+
// Reuse the in-flight or recent result (including unknown) within the same executable lookup context.
|
|
28
|
+
const { PATH, PATHEXT, ComSpec } = process.env;
|
|
29
|
+
const key = JSON.stringify([process.platform, process.cwd(), PATH, PATHEXT, ComSpec]);
|
|
30
|
+
const now = performance.now();
|
|
31
|
+
if (cachedProbe?.key === key && cachedProbe.expiresAt > now) return cachedProbe.result;
|
|
32
|
+
|
|
23
33
|
const invocation = commandInvocation("claude", ["--version"]);
|
|
24
|
-
|
|
25
|
-
execFile(invocation.file, invocation.args, { ...invocation.options,
|
|
34
|
+
const result = new Promise<string | null>(resolve => {
|
|
35
|
+
const child = execFile(invocation.file, invocation.args, { ...invocation.options, maxBuffer: 4096 }, (error, stdout) => {
|
|
36
|
+
clearTimeout(deadline);
|
|
26
37
|
resolve(error ? null : /^\s*(\d+\.\d+\.\d+)(?:\s|$)/.exec(String(stdout))?.[1] ?? null);
|
|
27
38
|
});
|
|
28
|
-
|
|
39
|
+
const deadline = setTimeout(() => {
|
|
40
|
+
resolve(null);
|
|
41
|
+
try { child.kill("SIGKILL"); } catch { /* child already exited */ }
|
|
42
|
+
child.stdout?.destroy();
|
|
43
|
+
child.stderr?.destroy();
|
|
44
|
+
}, VERSION_PROBE_TIMEOUT_MS);
|
|
45
|
+
}).catch(() => null);
|
|
46
|
+
cachedProbe = { key, expiresAt: now + VERSION_PROBE_CACHE_MS, result };
|
|
47
|
+
return result;
|
|
29
48
|
}
|
|
30
49
|
|
|
31
50
|
/** Server-local observation only: never returns settings values or modifies settings. */
|
|
32
51
|
export async function inspectSubagentForceStatus(
|
|
33
52
|
targetValid: boolean,
|
|
34
53
|
configDir = claudeConfigDir(),
|
|
35
|
-
versionProbe: () => Promise<string | null> =
|
|
54
|
+
versionProbe: () => Promise<string | null> = probeInstalledClaudeCodeVersion,
|
|
36
55
|
): Promise<SubagentForceStatus> {
|
|
37
56
|
const version = await versionProbe().catch(() => null);
|
|
38
57
|
let settingsOverride = false;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Weekly quota steering for caller-forward Claude subscriptions.
|
|
3
|
+
* Policy comes from the loaded config. Quota observations are process-local and scoped to
|
|
4
|
+
* the exact upstream and caller credential. A managed route uses its own billing identity.
|
|
5
|
+
*
|
|
6
|
+
* Pattern adopted from Hermes-agent (MIT, NousResearch): reserve quota for interactive
|
|
7
|
+
* use and record the request, destination and reason for a downgrade.
|
|
8
|
+
*/
|
|
9
|
+
import { SUBSCRIPTION_RESERVE_HARD, SUBSCRIPTION_RESERVE_SOFT } from "../config/schema/subscription-reserve";
|
|
10
|
+
import { previewRouteModel, type RouteResult } from "../router";
|
|
11
|
+
import type { OcxConfig } from "../types";
|
|
12
|
+
|
|
13
|
+
export const SUBSCRIPTION_PRIORITY_HEADER = "x-ocx-priority";
|
|
14
|
+
const OBSERVATION_MAX_AGE_MS = 30 * 60_000;
|
|
15
|
+
const OBSERVATION_LIMIT = 128;
|
|
16
|
+
|
|
17
|
+
interface WeeklyObservation {
|
|
18
|
+
utilization: number;
|
|
19
|
+
observedAt: number;
|
|
20
|
+
resetAt?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const observations = new WeakMap<OcxConfig, Map<string, WeeklyObservation>>();
|
|
24
|
+
|
|
25
|
+
export function nativeSubscriptionObservationKey(upstream: string, bearer: string): string | undefined {
|
|
26
|
+
if (!bearer.startsWith("sk-ant-oat")) return undefined;
|
|
27
|
+
return Bun.CryptoHasher.hash("sha256", JSON.stringify([upstream, bearer]), "hex");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function finite(value: string | null): number | undefined {
|
|
31
|
+
if (value === null || !/^\d+(?:\.\d+)?$/.test(value.trim())) return undefined;
|
|
32
|
+
const n = Number(value);
|
|
33
|
+
return Number.isFinite(n) ? n : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function observeWeeklyHeaders(config: OcxConfig, key: string, headers: Headers, now = Date.now()): void {
|
|
37
|
+
const utilization = finite(headers.get("anthropic-ratelimit-unified-7d-utilization"));
|
|
38
|
+
if (utilization === undefined || utilization > 1.5) return;
|
|
39
|
+
const reset = finite(headers.get("anthropic-ratelimit-unified-7d-reset"));
|
|
40
|
+
let seen = observations.get(config);
|
|
41
|
+
if (!seen) {
|
|
42
|
+
seen = new Map();
|
|
43
|
+
observations.set(config, seen);
|
|
44
|
+
}
|
|
45
|
+
seen.delete(key);
|
|
46
|
+
if (seen.size >= OBSERVATION_LIMIT) seen.delete(seen.keys().next().value!);
|
|
47
|
+
seen.set(key, {
|
|
48
|
+
utilization: Math.min(utilization, 1),
|
|
49
|
+
observedAt: now,
|
|
50
|
+
...(reset !== undefined && reset > 0 ? { resetAt: reset * 1000 } : {}),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Only an explicit caller signal can classify Haiku, subagents or short requests as background. */
|
|
55
|
+
export function isSubscriptionBackground(headers: Headers): boolean {
|
|
56
|
+
return headers.get(SUBSCRIPTION_PRIORITY_HEADER)?.trim().toLowerCase() === "background";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function decideReserve(
|
|
60
|
+
config: OcxConfig,
|
|
61
|
+
key: string,
|
|
62
|
+
isEligible: (target: string, route: RouteResult) => boolean,
|
|
63
|
+
now = Date.now(),
|
|
64
|
+
): { target: string; reason: string } | undefined {
|
|
65
|
+
const policy = config.subscriptionReserve;
|
|
66
|
+
const seen = observations.get(config)?.get(key);
|
|
67
|
+
if (policy?.enabled !== true || !seen || seen.observedAt > now || now - seen.observedAt >= OBSERVATION_MAX_AGE_MS
|
|
68
|
+
|| (seen.resetAt !== undefined && seen.resetAt <= now)) return undefined;
|
|
69
|
+
|
|
70
|
+
const soft = policy.soft ?? SUBSCRIPTION_RESERVE_SOFT;
|
|
71
|
+
const hard = policy.hard ?? SUBSCRIPTION_RESERVE_HARD;
|
|
72
|
+
if (seen.utilization < soft) return undefined;
|
|
73
|
+
const tier = seen.utilization >= hard ? "hard" : "soft";
|
|
74
|
+
const threshold = tier === "hard" ? hard : soft;
|
|
75
|
+
for (const target of policy.targets ?? []) {
|
|
76
|
+
try {
|
|
77
|
+
// An unknown prefix can otherwise fall through to the default provider as a literal model.
|
|
78
|
+
const route = previewRouteModel(config, target);
|
|
79
|
+
if (route.routeKind !== "explicit-provider" || route.routeReason !== "explicit-provider-namespace"
|
|
80
|
+
|| route.providerName !== target.slice(0, target.indexOf("/")) || !isEligible(target, route)) continue;
|
|
81
|
+
} catch {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
return { target, reason: `weekly Claude ${Math.round(seen.utilization * 100)}% >= ${Math.round(threshold * 100)}% (${tier} reserve)` };
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
package/src/cli/account-api.ts
CHANGED
|
@@ -43,6 +43,7 @@ export interface AccountRow {
|
|
|
43
43
|
/** Explicit paid-credit consent, reported only when supplied by the Codex API. */
|
|
44
44
|
creditsAfterLimit?: boolean;
|
|
45
45
|
needsReauthReason?: "verify_account";
|
|
46
|
+
needsReauthCause?: "tos_violation";
|
|
46
47
|
autoSelectable?: boolean;
|
|
47
48
|
skipReason?: "paused" | "needs_reauth" | "suspended" | "cooldown" | "quota_exhausted";
|
|
48
49
|
selectionExcludedReason?: "plan_excluded";
|
|
@@ -475,6 +476,7 @@ interface OAuthAccountDto {
|
|
|
475
476
|
pauseDetails?: import("../types/account-auto-pause").AccountPauseDetails;
|
|
476
477
|
autoSwitchThresholdOverride?: number | null;
|
|
477
478
|
needsReauthReason?: "verify_account";
|
|
479
|
+
needsReauthCause?: "tos_violation";
|
|
478
480
|
autoSelectable?: boolean;
|
|
479
481
|
skipReason?: unknown;
|
|
480
482
|
/** Always sent by the management route; explicitly `null` when the tier is unknown. */
|
|
@@ -521,6 +523,7 @@ async function fetchOAuthRows(
|
|
|
521
523
|
...(a.paused === true ? { paused: true, pauseDetails: accountPauseDetailsSchema.safeParse(a.pauseDetails).data } : {}),
|
|
522
524
|
...(isAnthropicOAuthInstance(name) && Object.hasOwn(a, "autoSwitchThresholdOverride") ? { autoSwitchThresholdOverride: a.autoSwitchThresholdOverride } : {}),
|
|
523
525
|
...(a.needsReauthReason === "verify_account" ? { needsReauthReason: a.needsReauthReason } : {}),
|
|
526
|
+
...(a.needsReauthReason === "verify_account" && a.needsReauthCause === "tos_violation" ? { needsReauthCause: a.needsReauthCause } : {}),
|
|
524
527
|
...(name === "kiro" && typeof a.autoSelectable === "boolean"
|
|
525
528
|
? { autoSelectable: a.autoSelectable } : {}),
|
|
526
529
|
...(name === "kiro" && a.autoSelectable === false && isKiroSkipReason(a.skipReason)
|