@bitkyc08/opencodex 2.83.0 → 2.86.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 +0 -4
- package/bin/ocx.mjs +61 -3
- package/gui/dist/assets/App-BjlttVLy.js +58 -0
- package/gui/dist/assets/Tray-DyR9yBee.js +1 -0
- package/gui/dist/assets/{index-BAbcj51J.css → index-BB6uK6tU.css} +1 -1
- package/gui/dist/assets/index-BMssP4k3.js +94 -0
- package/gui/dist/assets/quota-summary-D8M1bc_R.js +1 -0
- package/gui/dist/index.html +2 -2
- package/package.json +2 -3
- package/src/adapters/anthropic/beta-allowlist.ts +87 -8
- package/src/adapters/anthropic/client-identity.ts +1 -1
- package/src/adapters/anthropic/passthrough.ts +33 -4
- package/src/adapters/azure.ts +19 -1
- package/src/adapters/client-fingerprint.ts +24 -0
- package/src/adapters/coding-agent/protocol.ts +15 -8
- package/src/adapters/coding-agent/turn.ts +22 -9
- package/src/adapters/cursor/arg-codec.ts +17 -5
- package/src/adapters/cursor/catalog.ts +8 -0
- package/src/adapters/cursor/effort-map.ts +3 -0
- package/src/adapters/cursor/freeform-args.ts +75 -0
- package/src/adapters/cursor/protobuf-events.ts +36 -23
- package/src/adapters/google-antigravity-wire.ts +22 -1
- package/src/adapters/google.ts +2 -2
- package/src/adapters/ollama-native.ts +29 -1
- package/src/adapters/openai-chat/messages.ts +8 -4
- package/src/adapters/openai-chat/response-events.ts +59 -1
- package/src/adapters/openai-chat.ts +8 -5
- package/src/adapters/openai-responses/empty-assistant.ts +32 -0
- package/src/adapters/openai-responses/passthrough.ts +3 -0
- package/src/adapters/openai-responses/request-strips.ts +10 -2
- package/src/adapters/qoder/adapter.ts +56 -15
- package/src/advisor/consult.ts +220 -0
- package/src/advisor/context.ts +226 -0
- package/src/advisor/disclosure.ts +20 -0
- package/src/advisor/runtime.ts +305 -0
- package/src/advisor/settings.ts +135 -0
- package/src/advisor/state.ts +369 -0
- package/src/advisor/synthetic-tool.ts +33 -0
- package/src/bridge/errors.ts +24 -0
- package/src/bridge.ts +1 -1
- package/src/chat/inbound.ts +30 -6
- package/src/chat/outbound.ts +5 -0
- package/src/claude/context-windows.ts +54 -1
- package/src/claude/desktop-3p-library.ts +43 -3
- package/src/claude/desktop-3p.ts +22 -8
- package/src/claude/desktop-picker-profile.ts +11 -4
- package/src/claude/desktop-profile.ts +10 -1
- package/src/claude/desktop-remote-store-io.ts +5 -3
- package/src/claude/inbound.ts +121 -50
- package/src/claude/intercept/connect-proxy.ts +10 -3
- package/src/claude/intercept/local-ca.ts +8 -1
- package/src/claude/intercept/picker-ca-persistence.ts +3 -1
- package/src/claude/intercept/picker-listener.ts +49 -1
- package/src/claude/intercept/settings-no-proxy.ts +146 -0
- package/src/claude/intercept/settings.ts +101 -9
- package/src/claude/model-default-settings.ts +154 -0
- package/src/claude/model-info.ts +7 -7
- package/src/claude/outbound.ts +13 -6
- package/src/cli/account-api.ts +65 -0
- package/src/cli/account-auth.ts +26 -2
- package/src/cli/account.ts +41 -5
- package/src/cli/advisor.ts +151 -0
- package/src/cli/auxiliary-listener-preflight.ts +37 -0
- package/src/cli/capabilities-access-remote.ts +8 -0
- package/src/cli/capabilities-agents-routing.ts +18 -0
- package/src/cli/capabilities-base.ts +14 -2
- package/src/cli/catalog-command-result.ts +2 -1
- package/src/cli/chatgpt-command.ts +35 -14
- package/src/cli/claude.ts +60 -11
- package/src/cli/config-command.ts +8 -2
- package/src/cli/debug.ts +8 -0
- package/src/cli/dispatch.ts +21 -4
- package/src/cli/doctor.ts +9 -0
- package/src/cli/ensure-desktop-cold-start.ts +329 -0
- package/src/cli/export-command.ts +15 -2
- package/src/cli/help.ts +2 -1
- package/src/cli/hub-disable.ts +99 -0
- package/src/cli/hub.ts +4 -2
- package/src/cli/index.ts +26 -8
- package/src/cli/integration-plan-dto.ts +2 -0
- package/src/cli/link-child.ts +104 -0
- package/src/cli/link-enrollment.ts +96 -0
- package/src/cli/link.ts +39 -24
- package/src/cli/observe-stream.ts +6 -1
- package/src/cli/observe.ts +10 -2
- package/src/cli/provider.ts +12 -0
- package/src/cli/registry.ts +20 -7
- package/src/cli/restart-scope.ts +3 -1
- package/src/cli/runtime-api.ts +27 -2
- package/src/cli/status.ts +104 -20
- package/src/client/connect.ts +39 -4
- package/src/client/hub-client.ts +14 -7
- package/src/client/hub-state.ts +25 -14
- package/src/client/link-home.ts +248 -0
- package/src/client/link-management-relay.ts +61 -0
- package/src/client/link-relay.ts +6 -1
- package/src/client/link-status.ts +14 -0
- package/src/client/machine-api.ts +14 -7
- package/src/client/machine-listener.ts +29 -4
- package/src/clients/config-export/contracts.ts +1 -0
- package/src/clients/config-export/model-metadata.ts +3 -2
- package/src/clients/config-export/qoder.ts +93 -0
- package/src/clients/config-export.ts +59 -16
- package/src/clients/omo-role-models.ts +6 -3
- package/src/codex/agent-role-models.ts +26 -8
- package/src/codex/auth-api/account-list.ts +17 -2
- package/src/codex/auth-api/account-selection.ts +17 -7
- package/src/codex/auth-api/pool-mode-gate.ts +5 -2
- package/src/codex/auth-api/routes.ts +5 -1
- package/src/codex/auth-context.ts +79 -5
- package/src/codex/autostart-health.ts +7 -1
- package/src/codex/catalog/build-entries.ts +11 -2
- package/src/codex/catalog/bundled.ts +5 -1
- package/src/codex/catalog/configured-tiers.ts +50 -0
- package/src/codex/catalog/effort.ts +6 -3
- package/src/codex/catalog/metadata.ts +4 -2
- package/src/codex/catalog/model-hints.ts +21 -3
- package/src/codex/catalog/native-models.ts +16 -2
- package/src/codex/catalog/native-ultrafast.ts +106 -0
- package/src/codex/catalog/parsing.ts +5 -0
- package/src/codex/catalog/provider-models.ts +4 -0
- package/src/codex/catalog/retained-sync.ts +2 -0
- package/src/codex/catalog/routed-gather.ts +22 -4
- package/src/codex/catalog/vendor-context.ts +82 -0
- package/src/codex/config-write-lock.ts +40 -2
- package/src/codex/convergence.ts +2 -0
- package/src/codex/desktop-app/types.ts +2 -0
- package/src/codex/desktop-app/windows.ts +29 -16
- package/src/codex/desktop-app-restart.ts +4 -2
- package/src/codex/desktop-compatibility/certificate-service.ts +99 -0
- package/src/codex/desktop-compatibility/certificate-store.ts +193 -0
- package/src/codex/desktop-compatibility/connection-store.ts +87 -0
- package/src/codex/desktop-compatibility/installed-build.ts +55 -0
- package/src/codex/desktop-compatibility/json-body.ts +29 -0
- package/src/codex/desktop-compatibility/native-identity.ts +62 -0
- package/src/codex/desktop-compatibility/relay-listener.ts +93 -0
- package/src/codex/desktop-compatibility/routing-binding.ts +10 -0
- package/src/codex/desktop-compatibility/routing-preflight.ts +69 -0
- package/src/codex/desktop-compatibility/runtime-ownership.ts +28 -0
- package/src/codex/desktop-compatibility/runtime.ts +173 -0
- package/src/codex/desktop-compatibility/service.ts +30 -0
- package/src/codex/desktop-compatibility/startup-settings.ts +50 -0
- package/src/codex/desktop-compatibility/usage-activation.ts +86 -0
- package/src/codex/desktop-compatibility/usage-controlled-fetch.ts +59 -0
- package/src/codex/desktop-compatibility/usage-controller.ts +107 -0
- package/src/codex/desktop-compatibility/usage-policy.ts +59 -0
- package/src/codex/desktop-compatibility/usage-refresh.ts +33 -0
- package/src/codex/desktop-compatibility/usage-sse-controller.ts +86 -0
- package/src/codex/desktop-compatibility/windows-activation-source.ts +44 -0
- package/src/codex/desktop-compatibility/windows-certificate-trust.ts +81 -0
- package/src/codex/desktop-compatibility/windows-key-protection.ts +59 -0
- package/src/codex/desktop-compatibility/windows-package-command.ts +86 -0
- package/src/codex/desktop-compatibility/windows-package-launch.ts +28 -0
- package/src/codex/history-provider.ts +1 -1
- package/src/codex/inject/config-write-section.ts +4 -1
- package/src/codex/inject/remove.ts +18 -6
- package/src/codex/journal.ts +16 -1
- package/src/codex/main-account.ts +99 -15
- package/src/codex/native-main-refresh-mode.ts +217 -0
- package/src/codex/native-profile-startup.ts +4 -1
- package/src/codex/prompt-journal.ts +20 -2
- package/src/codex/prompt-layers/encoding.ts +49 -0
- package/src/codex/prompt-layers/import-source.ts +30 -0
- package/src/codex/prompt-layers/toml-edit.ts +269 -37
- package/src/codex/prompt-layers/toml-read.ts +52 -4
- package/src/codex/prompt-layers.ts +383 -20
- package/src/codex/prompt-lock-claim.ts +13 -3
- package/src/codex/prompt-lock-evidence.ts +3 -3
- package/src/codex/prompt-lock-owner.ts +14 -6
- package/src/codex/prompt-text-probe.ts +34 -5
- package/src/codex/quota.ts +13 -6
- package/src/codex/role-auto-assign.ts +2 -5
- package/src/codex/routing/active-account.ts +21 -0
- package/src/codex/routing/health-store.ts +1 -1
- package/src/codex/routing/pool-recovery.ts +79 -0
- package/src/codex/routing.ts +1 -1
- package/src/codex/runtime.ts +196 -8
- package/src/codex/shim-diagnostics.ts +6 -0
- package/src/codex/shim-overlay.ts +15 -6
- package/src/codex/shim.ts +1 -1
- package/src/combos/failover.ts +3 -0
- package/src/config/api-key-reconcile.ts +34 -0
- package/src/config/diagnostics.ts +18 -1
- package/src/config/feature-flags.ts +5 -0
- package/src/config/live-reconcile.ts +56 -29
- package/src/config/live-topology.ts +29 -0
- package/src/config/load-degrade.ts +8 -2
- package/src/config/paths.ts +13 -3
- package/src/config/salvage.ts +3 -1
- package/src/config/schema/config-schema.ts +19 -0
- package/src/config/schema/desktop-compatibility.ts +9 -0
- package/src/config/schema/leaf-validators.ts +1 -0
- package/src/generated/compatibility-version.json +713 -285
- package/src/generated/model-metadata.ts +1 -1
- package/src/grok/grpc-web.ts +23 -1
- package/src/grok/reset-coupon-ledger.ts +75 -5
- package/src/grok/reset-coupons.ts +4 -2
- package/src/integrations/catalog-refresh.ts +1 -1
- package/src/integrations/config-io.ts +20 -2
- package/src/integrations/journal.ts +15 -1
- package/src/integrations/mutation-plan.ts +14 -7
- package/src/integrations/registry.ts +52 -11
- package/src/integrations/state.ts +19 -7
- package/src/integrations/store.ts +3 -0
- package/src/integrations/target.ts +42 -5
- package/src/integrations/writer.ts +24 -7
- package/src/lib/advisor-activation.ts +16 -0
- package/src/lib/app-owned-memory-stores.ts +4 -0
- package/src/lib/app-owned-memory.ts +11 -0
- package/src/lib/background-work.ts +65 -0
- package/src/lib/bounded-file-read.ts +58 -0
- package/src/lib/bun-path-runtime.d.mts +10 -1
- package/src/lib/bun-path-runtime.mjs +62 -3
- package/src/lib/desktop-proxy-route.ts +30 -0
- package/src/lib/desktop-upstream-tunnel.ts +107 -0
- package/src/lib/errors.ts +7 -0
- package/src/lib/json-byte-size.ts +31 -0
- package/src/lib/local-internal-call-capability.ts +55 -0
- package/src/lib/optional-shutdown-hooks.ts +35 -0
- package/src/lib/outbound-credential-registry.ts +210 -0
- package/src/lib/package-tree-integrity.ts +2 -2
- package/src/lib/passive-read.ts +35 -0
- package/src/lib/shadow-call.ts +44 -5
- package/src/lib/socks5-fetch.ts +48 -71
- package/src/lib/socks5-handshake.ts +112 -0
- package/src/lib/translator-budget.ts +7 -1
- package/src/link/codex-settings.ts +155 -0
- package/src/link/management-routes.ts +89 -0
- package/src/link/remote-probe.ts +38 -0
- package/src/link/ssh-argv.ts +26 -5
- package/src/link/ssh-runner.ts +58 -27
- package/src/link/status-projection.ts +22 -4
- package/src/link/supervisor.ts +144 -49
- package/src/link/tunnel-state.ts +11 -4
- package/src/oauth/anthropic-continuity.ts +4 -3
- package/src/oauth/anthropic-identity.ts +13 -2
- package/src/oauth/anthropic-model-quota.ts +13 -1
- package/src/oauth/anthropic-routing.ts +230 -23
- package/src/oauth/anthropic.ts +18 -4
- package/src/oauth/generic-account-failover.ts +29 -5
- package/src/oauth/google-antigravity.ts +24 -7
- package/src/oauth/index.ts +28 -17
- package/src/oauth/kiro-account-identity.ts +106 -0
- package/src/oauth/kiro-device-login.ts +2 -1
- package/src/oauth/kiro.ts +55 -6
- package/src/oauth/plan.ts +29 -0
- package/src/oauth/store.ts +40 -3
- package/src/oauth/types.ts +2 -0
- package/src/plugins/loader.ts +11 -0
- package/src/protocols/codecs/chat-ir.ts +467 -0
- package/src/protocols/codecs/chat.ts +10 -1
- package/src/protocols/codecs/messages-ir.ts +500 -0
- package/src/protocols/features.ts +10 -2
- package/src/protocols/path.ts +11 -1
- package/src/protocols/plan-snapshot.ts +39 -16
- package/src/protocols/plan.ts +9 -1
- package/src/protocols/settings.ts +8 -0
- package/src/providers/anthropic-billing-ownership.ts +74 -0
- package/src/providers/anthropic-billing-recovery.ts +31 -0
- package/src/providers/anthropic-billing-refusal.ts +55 -0
- package/src/providers/anthropic-reset-admission.ts +62 -0
- package/src/providers/anthropic-reset-grant-ledger.ts +28 -6
- package/src/providers/antigravity-account-roster.ts +102 -0
- package/src/providers/antigravity-models.ts +85 -7
- package/src/providers/api-key-resolve.ts +17 -2
- package/src/providers/key-failover.ts +52 -5
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/model-rename-migration.ts +5 -3
- package/src/providers/model-rename-startup.ts +5 -3
- package/src/providers/quota/account-cache.ts +11 -0
- package/src/providers/quota/anthropic-account-quota.ts +4 -2
- package/src/providers/quota/report-cache.ts +14 -0
- package/src/providers/quota/vendor-probes-oauth.ts +3 -3
- package/src/providers/quota.ts +4 -1
- package/src/providers/registry/entries-core.ts +21 -20
- package/src/providers/registry/entries-extended.ts +19 -1
- package/src/providers/registry/model-seeds.ts +17 -0
- package/src/providers/request-pacing.ts +9 -0
- package/src/providers/stale-no-vision-seed-migration.ts +72 -0
- package/src/responses/compaction.ts +14 -0
- package/src/responses/parser.ts +37 -20
- package/src/responses/state.ts +32 -29
- package/src/routing/history/build-file.ts +92 -0
- package/src/routing/history/indexer.ts +248 -216
- package/src/routing/history/ingest.ts +238 -0
- package/src/routing/history/rebuild-job.ts +111 -0
- package/src/routing/history/rebuild-protocol.ts +15 -0
- package/src/routing/history/rebuild-worker.ts +34 -0
- package/src/server/admission-model-scope.ts +4 -1
- package/src/server/antigravity-direct-eligibility.ts +208 -0
- package/src/server/auth-cors.ts +46 -0
- package/src/server/chat-completions.ts +75 -9
- package/src/server/claude-messages.ts +110 -20
- package/src/server/configured-credentials.ts +194 -0
- package/src/server/index/desktop-compatibility-startup.ts +56 -0
- package/src/server/index/link-management.ts +58 -0
- package/src/server/index/optional-listeners.ts +5 -1
- package/src/server/index/serve-options.ts +15 -2
- package/src/server/index/startup-warnings.ts +9 -0
- package/src/server/index/websocket-handler.ts +9 -0
- package/src/server/index.ts +13 -10
- package/src/server/inference/client-encoder-delivery.ts +6 -5
- package/src/server/lifecycle.ts +2 -1
- package/src/server/login-shell-path.ts +146 -0
- package/src/server/management/advisor-routes.ts +191 -0
- package/src/server/management/agent-settings-routes.ts +50 -14
- package/src/server/management/anthropic-reset-grant-routes.ts +17 -1
- package/src/server/management/codex-agent-role-routes.ts +19 -6
- package/src/server/management/codex-prompt-routes.ts +136 -5
- package/src/server/management/codex-role-auto-assign.ts +1 -1
- package/src/server/management/companion-routes.ts +4 -0
- package/src/server/management/config-routes.ts +2 -2
- package/src/server/management/context.ts +4 -0
- package/src/server/management/desktop-compatibility-routes.ts +46 -0
- package/src/server/management/desktop-compatibility-runtime-routes.ts +46 -0
- package/src/server/management/desktop-compatibility-settings-routes.ts +27 -0
- package/src/server/management/grok-coupon-routes.ts +78 -27
- package/src/server/management/link-enrollment.ts +192 -0
- package/src/server/management/link-route-state.ts +185 -0
- package/src/server/management/link-routes.ts +82 -402
- package/src/server/management/model-routes.ts +2 -2
- package/src/server/management/model-rows.ts +3 -1
- package/src/server/management/protocol-settings-patch.ts +1 -0
- package/src/server/management/provider-overwrite-carry.ts +10 -0
- package/src/server/management/provider-routes.ts +1 -0
- package/src/server/management/route-registry.ts +16 -3
- package/src/server/management/shared.ts +3 -0
- package/src/server/management/sibling-guard.ts +1 -0
- package/src/server/management-api.ts +9 -1
- package/src/server/messages-antigravity-direct.ts +195 -0
- package/src/server/messages-native.ts +73 -43
- package/src/server/relay-eager.ts +2 -0
- package/src/server/request-log-filter.ts +6 -1
- package/src/server/request-log-terminal-status.ts +63 -6
- package/src/server/request-log.ts +7 -0
- package/src/server/responses/adapter-continuation.ts +35 -18
- package/src/server/responses/adapter-delivery.ts +24 -2
- package/src/server/responses/adapter-dispatch.ts +261 -81
- package/src/server/responses/advisor-plan-slot.ts +27 -0
- package/src/server/responses/advisor-slot.ts +336 -0
- package/src/server/responses/code-mode-collab-surface.ts +313 -0
- package/src/server/responses/codex-ws-exchange.ts +147 -6
- package/src/server/responses/codex-ws-wire.ts +203 -0
- package/src/server/responses/collaboration.ts +36 -7
- package/src/server/responses/compact.ts +126 -48
- package/src/server/responses/context-overflow.ts +13 -1
- package/src/server/responses/core-codex-account.ts +105 -1
- package/src/server/responses/core-combo-failure.ts +24 -1
- package/src/server/responses/core-combo.ts +28 -2
- package/src/server/responses/core-errors.ts +29 -1
- package/src/server/responses/core-lifetime.ts +2 -0
- package/src/server/responses/core-normalize.ts +11 -0
- package/src/server/responses/core-options.ts +48 -0
- package/src/server/responses/core-replay.ts +7 -4
- package/src/server/responses/core.ts +1 -1
- package/src/server/responses/deferred-collab-surface.ts +158 -0
- package/src/server/responses/deferred-send-reporter.ts +25 -0
- package/src/server/responses/fetch-helpers.ts +12 -1
- package/src/server/responses/passthrough-delivery.ts +20 -11
- package/src/server/responses/passthrough-dispatch.ts +29 -7
- package/src/server/responses/request-prepare.ts +74 -9
- package/src/server/responses/request-transport.ts +22 -8
- package/src/server/responses/run-turn-execution.ts +70 -8
- package/src/server/responses/sidecar-execution.ts +89 -5
- package/src/server/responses/terminal-guard.ts +2 -1
- package/src/server/responses/ws-upstream.ts +9 -1
- package/src/server/responses-undeclared-tool-guard.ts +30 -9
- package/src/server/startup-action-control.ts +38 -4
- package/src/server/startup-health-cache.ts +5 -0
- package/src/server/system-env-shell.ts +7 -2
- package/src/server/system-env.ts +11 -1
- package/src/service/cli.ts +5 -1
- package/src/service/desktop-startup.ts +161 -34
- package/src/service/desktop-supervision.d.mts +27 -1
- package/src/service/desktop-supervision.mjs +111 -5
- package/src/service/health-sentinel-launch.ts +176 -0
- package/src/service/health-sentinel-main.ts +466 -0
- package/src/service/health-sentinel-policy.ts +241 -0
- package/src/service/health-sentinel-probe.ts +71 -0
- package/src/service/health-sentinel-windows.ts +87 -0
- package/src/service/linux-linger.ts +20 -0
- package/src/service/logging.ts +72 -0
- package/src/tray/windows-tray.ps1 +43 -24
- package/src/types/config.ts +68 -6
- package/src/types/provider.ts +7 -0
- package/src/types/request.ts +15 -0
- package/src/types/tools.ts +2 -0
- package/src/update/async-check.ts +1 -1
- package/src/update/badge.ts +2 -2
- package/src/update/index.ts +88 -15
- package/src/update/job-supervised-restart.ts +107 -0
- package/src/update/job.ts +32 -32
- package/src/update/notify.ts +1 -1
- package/src/update/refresh-scheduler.ts +2 -2
- package/src/update/supervised-recovery.d.mts +27 -0
- package/src/update/supervised-recovery.mjs +157 -0
- package/src/usage/cache-diagnostic.ts +2 -0
- package/src/usage/expected-prices.ts +16 -0
- package/src/usage/log.ts +19 -1
- package/src/web-search/gemini-executor.ts +2 -2
- package/assets/sponsors/packycode.png +0 -0
- package/gui/dist/assets/App-Ba8uG696.js +0 -54
- package/gui/dist/assets/Tray-IImbNPRy.js +0 -1
- package/gui/dist/assets/index-BHRZdJuv.js +0 -94
- package/gui/dist/assets/quota-summary-DiJO6sYY.js +0 -1
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { execFileSync } from "node:child_process";
|
|
14
14
|
import { sep, win32 } from "node:path";
|
|
15
15
|
import { resolveTrustedWindowsPowerShellExe, resolveTrustedWindowsTaskkillExe } from "../../lib/windows-elevation";
|
|
16
|
+
import { activateWindowsCodexCompatibility, assertWindowsCompatibilityContext, captureWindowsCompatibilityContext } from "../desktop-compatibility/windows-package-command";
|
|
16
17
|
import {
|
|
17
18
|
isUnderRoot,
|
|
18
19
|
type DesktopAppAdapter,
|
|
@@ -50,8 +51,7 @@ function isMemberExecutable(executable: string, root: string): boolean {
|
|
|
50
51
|
* changes between builds, so a literal AUMID would silently stop matching and
|
|
51
52
|
* then either do nothing or — worse — match a package we did not mean.
|
|
52
53
|
*/
|
|
53
|
-
|
|
54
|
-
const script = [
|
|
54
|
+
export const WINDOWS_PACKAGE_DISCOVERY_SCRIPT = [
|
|
55
55
|
"$ErrorActionPreference='SilentlyContinue'",
|
|
56
56
|
"Import-Module Appx -ErrorAction SilentlyContinue",
|
|
57
57
|
"$p = Get-AppxPackage -Name OpenAI.Codex",
|
|
@@ -60,12 +60,8 @@ function discoverPackage(exec: DesktopExec): DesktopAppInstall | null {
|
|
|
60
60
|
" $p.PackageFamilyName; $p.InstallLocation; \"$($p.PackageFamilyName)!App\"",
|
|
61
61
|
"}",
|
|
62
62
|
].join("; ");
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
stdout = exec(resolveTrustedWindowsPowerShellExe(), ["-NoProfile", "-NonInteractive", "-Command", script], POWERSHELL_PROBE_OPTIONS);
|
|
66
|
-
} catch {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
63
|
+
|
|
64
|
+
export function parseWindowsDesktopPackage(stdout: string): DesktopAppInstall | null {
|
|
69
65
|
const lines = stdout.split(/\r?\n/).map(line => line.trim()).filter(line => line.length > 0);
|
|
70
66
|
if (lines.length < 3 || lines[0] === "MISS") return null;
|
|
71
67
|
const [family, installLocation, aumid] = lines;
|
|
@@ -73,6 +69,16 @@ function discoverPackage(exec: DesktopExec): DesktopAppInstall | null {
|
|
|
73
69
|
return { id: family, root: installLocation, relaunch: aumid };
|
|
74
70
|
}
|
|
75
71
|
|
|
72
|
+
function discoverPackage(exec: DesktopExec): DesktopAppInstall | null {
|
|
73
|
+
let stdout: string;
|
|
74
|
+
try {
|
|
75
|
+
stdout = exec(resolveTrustedWindowsPowerShellExe(), ["-NoProfile", "-NonInteractive", "-Command", WINDOWS_PACKAGE_DISCOVERY_SCRIPT], POWERSHELL_PROBE_OPTIONS);
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return parseWindowsDesktopPackage(stdout);
|
|
80
|
+
}
|
|
81
|
+
|
|
76
82
|
/**
|
|
77
83
|
* Only `ChatGPT.exe` processes whose image lives under the discovered install
|
|
78
84
|
* location AND owned by the current user. The install location alone is not
|
|
@@ -100,7 +106,8 @@ function listPackageProcesses(exec: DesktopExec, install: DesktopAppInstall): De
|
|
|
100
106
|
" if ($o -and $o.ReturnValue -eq 0 -and $o.User) {",
|
|
101
107
|
" $owner = if ($o.Domain) { \"$($o.Domain)\\$($o.User)\" } else { $o.User }",
|
|
102
108
|
" if ($owner -ieq $me) {",
|
|
103
|
-
"
|
|
109
|
+
" $encoded=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([string]$_.CommandLine))",
|
|
110
|
+
" \"$($_.ProcessId) $($_.ParentProcessId) $($_.CreationDate.ToString('o')) $($_.ExecutablePath)`t$encoded\"",
|
|
104
111
|
" }",
|
|
105
112
|
" }",
|
|
106
113
|
" }",
|
|
@@ -126,7 +133,9 @@ function listPackageProcesses(exec: DesktopExec, install: DesktopAppInstall): De
|
|
|
126
133
|
}
|
|
127
134
|
|
|
128
135
|
function parseProcessLine(line: string, root: string): DesktopProcess | null {
|
|
129
|
-
const
|
|
136
|
+
const [listing, encoded, ...extra] = line.split("\t");
|
|
137
|
+
if (extra.length || (encoded && (encoded.length > 65_536 || !/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)))) return null;
|
|
138
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\S+)(?:\s+(.+))?$/.exec(listing ?? "");
|
|
130
139
|
if (!match) return null;
|
|
131
140
|
const pid = Number(match[1]);
|
|
132
141
|
const parentPid = Number(match[2]);
|
|
@@ -143,7 +152,8 @@ function parseProcessLine(line: string, root: string): DesktopProcess | null {
|
|
|
143
152
|
// Authoritative membership. PowerShell StartsWith already cheap-filtered, but
|
|
144
153
|
// that test is a string prefix and is how a sibling install would sneak in.
|
|
145
154
|
if (!isMemberExecutable(executable, root)) return null;
|
|
146
|
-
return { pid, parentPid, createdAt, executable
|
|
155
|
+
return { pid, parentPid, createdAt, executable,
|
|
156
|
+
...(encoded !== undefined ? { commandLine: Buffer.from(encoded, "base64").toString("utf8") } : {}) };
|
|
147
157
|
}
|
|
148
158
|
|
|
149
159
|
/**
|
|
@@ -215,13 +225,16 @@ export const windowsDesktopAppAdapter: DesktopAppAdapter = {
|
|
|
215
225
|
exec(resolveTrustedWindowsTaskkillExe(), ["/PID", String(root.pid), "/T", "/F"], POWERSHELL_PROBE_OPTIONS);
|
|
216
226
|
},
|
|
217
227
|
|
|
218
|
-
captureRelaunchContext(): Record<string, string> {
|
|
219
|
-
|
|
220
|
-
// carrying forward.
|
|
221
|
-
return {};
|
|
228
|
+
captureRelaunchContext(_exec, _install, processes): Record<string, string> {
|
|
229
|
+
return captureWindowsCompatibilityContext(processes);
|
|
222
230
|
},
|
|
223
231
|
|
|
224
|
-
relaunch(exec, install): void {
|
|
232
|
+
relaunch(exec, install, context): void {
|
|
233
|
+
if (context.codexCompatibilityPacUrl) {
|
|
234
|
+
assertWindowsCompatibilityContext(context);
|
|
235
|
+
activateWindowsCodexCompatibility(exec, install, context.codexCompatibilityPacUrl);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
225
238
|
// Throws on failure so the ladder reports relaunch_failed. The old code
|
|
226
239
|
// returned targets_survived here, which was dishonest: everything HAD died
|
|
227
240
|
// and it was the relaunch that failed.
|
|
@@ -89,6 +89,7 @@ export type DesktopAppRestartReason =
|
|
|
89
89
|
| "self_ancestry"
|
|
90
90
|
| "restart_in_flight"
|
|
91
91
|
| "handoff_started"
|
|
92
|
+
| "relaunch_context_failed"
|
|
92
93
|
| "targets_survived"
|
|
93
94
|
| "relaunch_failed";
|
|
94
95
|
|
|
@@ -263,7 +264,9 @@ export function restartCodexDesktopApp(io: DesktopAppRestartIo = {}): DesktopApp
|
|
|
263
264
|
// graphical session variables, and after termination there is nothing to read them
|
|
264
265
|
// from. Ordering this wrongly works on macOS and Windows and produces a Linux app
|
|
265
266
|
// that cannot reach the compositor.
|
|
266
|
-
|
|
267
|
+
let context: Record<string, string>;
|
|
268
|
+
try { context = adapter.captureRelaunchContext(exec, install, processes); }
|
|
269
|
+
catch { return skipped("relaunch_context_failed"); }
|
|
267
270
|
|
|
268
271
|
const isAlive = io.isAlive ?? defaultIsAlive;
|
|
269
272
|
const sleep = io.sleep ?? defaultSleep;
|
|
@@ -345,4 +348,3 @@ export function restartCodexDesktopApp(io: DesktopAppRestartIo = {}): DesktopApp
|
|
|
345
348
|
if (!handedOff) releaseDesktopRestartLock(io.lock);
|
|
346
349
|
}
|
|
347
350
|
}
|
|
348
|
-
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { getConfigDir } from "../../config/paths";
|
|
3
|
+
import { windowsDefaultExec, windowsDesktopAppAdapter } from "../desktop-app/windows";
|
|
4
|
+
import { ensureDesktopCompatibilityAuthority, inspectDesktopCompatibilityAuthority, loadDesktopCompatibilityAuthority, renewDesktopCompatibilityAuthority, type DesktopAuthorityInspection, type StoredDesktopAuthority } from "./certificate-store";
|
|
5
|
+
import { createWindowsCertificateTrust, inspectWindowsCertificateTrust, type DesktopCertificateTrust } from "./windows-certificate-trust";
|
|
6
|
+
import { acquireDesktopCertificateMutation, desktopCompatibilityRuntimeActive } from "./runtime-ownership";
|
|
7
|
+
|
|
8
|
+
export interface DesktopCertificateStatus {
|
|
9
|
+
supported: boolean;
|
|
10
|
+
state: "missing" | "invalid" | "expired" | "renewal-required" | "prepared" | "trusted" | "unknown";
|
|
11
|
+
fingerprint?: string;
|
|
12
|
+
expiresAt?: number;
|
|
13
|
+
renewalDue?: boolean;
|
|
14
|
+
trust?: DesktopCertificateTrust;
|
|
15
|
+
busy: "prepare" | "trust" | "remove-trust" | "renew" | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DesktopCertificateServiceIo {
|
|
19
|
+
platform?: string;
|
|
20
|
+
inspect?: () => DesktopAuthorityInspection;
|
|
21
|
+
prepare?: () => Promise<StoredDesktopAuthority>;
|
|
22
|
+
load?: (allowExpiredForRemoval: boolean) => Promise<StoredDesktopAuthority>;
|
|
23
|
+
renew?: (fingerprint: string) => Promise<StoredDesktopAuthority>;
|
|
24
|
+
readTrust?: (authority: Extract<DesktopAuthorityInspection, { fingerprint: string }>) => Promise<DesktopCertificateTrust>;
|
|
25
|
+
changeTrust?: (authority: StoredDesktopAuthority, action: "trust" | "remove") => Promise<DesktopCertificateTrust>;
|
|
26
|
+
appRunning?: () => Promise<boolean | null>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class DesktopCertificateServiceError extends Error {
|
|
30
|
+
constructor(readonly code: "unsupported" | "busy" | "not_prepared" | "fingerprint_changed" | "app_running" | "runtime_running" | "app_state_unknown" | "trust_unknown" | "trust_not_applied") {
|
|
31
|
+
super(`desktop_compatibility_${code}`); this.name = "DesktopCertificateServiceError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** This service owns setup only: no proxy listeners, app restart, account or quota writes. */
|
|
36
|
+
export function createDesktopCertificateService(directory = join(getConfigDir(), "codex-desktop-compatibility"), io: DesktopCertificateServiceIo = {}) {
|
|
37
|
+
const platform = io.platform ?? process.platform;
|
|
38
|
+
const inspect = io.inspect ?? (() => inspectDesktopCompatibilityAuthority(directory));
|
|
39
|
+
const prepare = io.prepare ?? (() => ensureDesktopCompatibilityAuthority({ directory }));
|
|
40
|
+
const load = io.load ?? (allowExpired => loadDesktopCompatibilityAuthority({ directory }, allowExpired));
|
|
41
|
+
const renew = io.renew ?? (fingerprint => renewDesktopCompatibilityAuthority({ directory }, fingerprint));
|
|
42
|
+
const readTrust = io.readTrust ?? (value => inspectWindowsCertificateTrust(value.certPem, value.fingerprint));
|
|
43
|
+
const changeTrust = io.changeTrust ?? ((authority, action) => createWindowsCertificateTrust(authority, authority.fingerprint)[action]());
|
|
44
|
+
const appRunning = io.appRunning ?? (async () => {
|
|
45
|
+
const install = windowsDesktopAppAdapter.discover(windowsDefaultExec);
|
|
46
|
+
if (!install) return null; // Unknown installation is not proof that no app uses the authority.
|
|
47
|
+
const processes = windowsDesktopAppAdapter.listProcesses(windowsDefaultExec, install);
|
|
48
|
+
return processes === null ? null : processes.length > 0;
|
|
49
|
+
});
|
|
50
|
+
let busy: DesktopCertificateStatus["busy"] = null;
|
|
51
|
+
async function status(): Promise<DesktopCertificateStatus> {
|
|
52
|
+
if (platform !== "win32") return { supported: false, state: "missing", busy };
|
|
53
|
+
const stored = inspect();
|
|
54
|
+
if (!("fingerprint" in stored)) return { supported: true, state: stored.status, busy };
|
|
55
|
+
const trusted = await readTrust(stored).catch(() => "unknown" as const);
|
|
56
|
+
return { supported: true, state: stored.status === "expired" ? "expired"
|
|
57
|
+
: stored.status === "renewal-required" ? "renewal-required"
|
|
58
|
+
: trusted === "trusted" ? "trusted" : trusted === "not-trusted" ? "prepared" : "unknown",
|
|
59
|
+
fingerprint: stored.fingerprint, expiresAt: stored.expiresAt, renewalDue: stored.renewalDue, trust: trusted, busy };
|
|
60
|
+
}
|
|
61
|
+
async function action(kind: NonNullable<DesktopCertificateStatus["busy"]>, expectedFingerprint?: string): Promise<DesktopCertificateStatus> {
|
|
62
|
+
if (platform !== "win32") throw new DesktopCertificateServiceError("unsupported");
|
|
63
|
+
if (busy !== null) throw new DesktopCertificateServiceError("busy");
|
|
64
|
+
if (desktopCompatibilityRuntimeActive()) throw new DesktopCertificateServiceError("runtime_running");
|
|
65
|
+
let release: () => void;
|
|
66
|
+
try { release = acquireDesktopCertificateMutation(); } catch { throw new DesktopCertificateServiceError("busy"); }
|
|
67
|
+
busy = kind;
|
|
68
|
+
try {
|
|
69
|
+
if (kind !== "prepare") {
|
|
70
|
+
const before = inspect();
|
|
71
|
+
if (!("fingerprint" in before)) throw new DesktopCertificateServiceError("not_prepared");
|
|
72
|
+
if (before.fingerprint !== expectedFingerprint) throw new DesktopCertificateServiceError("fingerprint_changed");
|
|
73
|
+
if (kind === "remove-trust" || kind === "renew") {
|
|
74
|
+
const running = await appRunning();
|
|
75
|
+
if (running === null) throw new DesktopCertificateServiceError("app_state_unknown");
|
|
76
|
+
if (running) throw new DesktopCertificateServiceError("app_running");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const authority = kind === "prepare" ? await prepare() : await load(kind !== "trust");
|
|
80
|
+
if (kind !== "prepare") {
|
|
81
|
+
if (authority.fingerprint !== expectedFingerprint) throw new DesktopCertificateServiceError("fingerprint_changed");
|
|
82
|
+
const current = inspect();
|
|
83
|
+
if (!("fingerprint" in current) || current.fingerprint !== authority.fingerprint) throw new DesktopCertificateServiceError("fingerprint_changed");
|
|
84
|
+
const result = await changeTrust(authority, kind === "trust" ? "trust" : "remove");
|
|
85
|
+
if (result === "unknown") throw new DesktopCertificateServiceError("trust_unknown");
|
|
86
|
+
if (result !== (kind === "trust" ? "trusted" : "not-trusted")) throw new DesktopCertificateServiceError("trust_not_applied");
|
|
87
|
+
// Never discard the only removable old identity until OS trust removal is proven.
|
|
88
|
+
// Publication failure leaves its protected envelope intact for an explicit retry.
|
|
89
|
+
if (kind === "renew") await renew(authority.fingerprint);
|
|
90
|
+
}
|
|
91
|
+
busy = null;
|
|
92
|
+
return status();
|
|
93
|
+
} finally { busy = null; release(); }
|
|
94
|
+
}
|
|
95
|
+
return { status, prepare: () => action("prepare"), trust: (fingerprint: string) => action("trust", fingerprint),
|
|
96
|
+
removeTrust: (fingerprint: string) => action("remove-trust", fingerprint), renew: (fingerprint: string) => action("renew", fingerprint) };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type DesktopCertificateService = ReturnType<typeof createDesktopCertificateService>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, randomUUID, X509Certificate } from "node:crypto";
|
|
2
|
+
import { closeSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, join } from "node:path";
|
|
4
|
+
import { createCertificateAuthority, isServerAuthOnlyCertificate, type LocalInterceptCa } from "../../claude/intercept/local-ca";
|
|
5
|
+
import { withClientLifecycle } from "../../client/lifecycle-lock";
|
|
6
|
+
import { hardenSecretDirAsync, hardenSecretPathAsync } from "../../lib/windows-secret-acl";
|
|
7
|
+
import { windowsAuthorityKeyProtection, type AuthorityKeyProtection } from "./windows-key-protection";
|
|
8
|
+
|
|
9
|
+
const POLICY = "codex-desktop-chatgpt-only/v1";
|
|
10
|
+
const FILE = "authority.json";
|
|
11
|
+
const MAX_FILE = 131_072;
|
|
12
|
+
const VALIDITY_DAYS = 30;
|
|
13
|
+
const DAY = 86_400_000;
|
|
14
|
+
const digest = (value: string) => createHash("sha256").update(value).digest("hex");
|
|
15
|
+
|
|
16
|
+
export class DesktopAuthorityError extends Error {
|
|
17
|
+
constructor(readonly code: "unsafe_path" | "unreadable" | "expired" | "renewal_required" | "protection_failed" | "fingerprint_changed") {
|
|
18
|
+
super(`desktop_compatibility_authority_${code}`); this.name = "DesktopAuthorityError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface DesktopAuthorityStoreOptions {
|
|
23
|
+
/** Explicit feature-owned directory; never an OS trust store or existing Claude CA. */
|
|
24
|
+
directory: string;
|
|
25
|
+
now?: () => number;
|
|
26
|
+
/** Test seam; production always uses Windows CurrentUser DPAPI. */
|
|
27
|
+
protection?: AuthorityKeyProtection;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface StoredDesktopAuthority {
|
|
31
|
+
authority: LocalInterceptCa;
|
|
32
|
+
commonName: string;
|
|
33
|
+
fingerprint: string;
|
|
34
|
+
expiresAt: number;
|
|
35
|
+
renewalDue: boolean;
|
|
36
|
+
reused: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type DesktopAuthorityInspection =
|
|
40
|
+
| { status: "missing" | "invalid" }
|
|
41
|
+
| { status: "present" | "expired" | "renewal-required"; certPem: string; fingerprint: string; expiresAt: number; renewalDue: boolean };
|
|
42
|
+
|
|
43
|
+
/** Read-only public metadata for status: no key decryption, ACL write, lock or generation. */
|
|
44
|
+
export function inspectDesktopCompatibilityAuthority(directory: string, now = Date.now()): DesktopAuthorityInspection {
|
|
45
|
+
if (!isAbsolute(directory) || !Number.isFinite(now)) return { status: "invalid" };
|
|
46
|
+
const path = join(directory, FILE);
|
|
47
|
+
try {
|
|
48
|
+
if (!pathPresent(directory)) return { status: "missing" };
|
|
49
|
+
assertPath(directory, true);
|
|
50
|
+
if (!pathPresent(path)) return { status: "missing" };
|
|
51
|
+
assertPath(path, false);
|
|
52
|
+
const saved = JSON.parse(readFileSync(path, "utf8"));
|
|
53
|
+
if (saved.version !== 1 || saved.protection !== "windows-current-user-dpapi" || typeof saved.certPem !== "string"
|
|
54
|
+
|| typeof saved.sealed !== "string" || !/^[A-Za-z0-9+/]+={0,2}$/.test(saved.sealed) || saved.sealed.length > MAX_FILE) return { status: "invalid" };
|
|
55
|
+
const certificate = new X509Certificate(saved.certPem), expiresAt = Date.parse(certificate.validTo);
|
|
56
|
+
if (!certificate.ca || !Number.isFinite(expiresAt)) return { status: "invalid" };
|
|
57
|
+
return { status: expiresAt <= now ? "expired" : isServerAuthOnlyCertificate(certificate) ? "present" : "renewal-required", certPem: saved.certPem,
|
|
58
|
+
fingerprint: certificate.fingerprint256.replaceAll(":", ""), expiresAt, renewalDue: expiresAt - now <= 7 * DAY };
|
|
59
|
+
} catch { return { status: "invalid" }; }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function assertPath(path: string, directory: boolean): void {
|
|
63
|
+
const stat = lstatSync(path);
|
|
64
|
+
if (stat.isSymbolicLink() || (directory ? !stat.isDirectory() : !stat.isFile() || stat.nlink !== 1 || stat.size > MAX_FILE)) {
|
|
65
|
+
throw new DesktopAuthorityError("unsafe_path");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function pathPresent(path: string): boolean {
|
|
70
|
+
try { lstatSync(path); return true; }
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return false;
|
|
73
|
+
throw new DesktopAuthorityError("unsafe_path");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function validatedAuthority(certPem: string, keyPem: string, commonName: string, now: number, reused: boolean, allowExpired = false): StoredDesktopAuthority {
|
|
78
|
+
try {
|
|
79
|
+
const certificate = new X509Certificate(certPem);
|
|
80
|
+
const privateKey = createPrivateKey(keyPem), publicKey = createPublicKey(keyPem);
|
|
81
|
+
if (!certificate.ca || !certificate.checkPrivateKey(privateKey) || !certificate.verify(publicKey)
|
|
82
|
+
|| certificate.subject !== certificate.issuer
|
|
83
|
+
|| !commonName.startsWith("OpenCodex Codex Desktop ") || !certificate.subject.split("\n").includes(`CN=${commonName}`)) {
|
|
84
|
+
throw new Error("invalid");
|
|
85
|
+
}
|
|
86
|
+
const expiresAt = Date.parse(certificate.validTo), startsAt = Date.parse(certificate.validFrom);
|
|
87
|
+
if (!Number.isFinite(expiresAt) || !Number.isFinite(startsAt) || startsAt > now
|
|
88
|
+
|| expiresAt - startsAt > (VALIDITY_DAYS + 1) * DAY) throw new Error("invalid");
|
|
89
|
+
if (expiresAt <= now && !allowExpired) throw new DesktopAuthorityError("expired");
|
|
90
|
+
if (!allowExpired && !isServerAuthOnlyCertificate(certificate)) throw new DesktopAuthorityError("renewal_required");
|
|
91
|
+
return { authority: { certPem, keyPem, privateKey, publicKey }, commonName,
|
|
92
|
+
fingerprint: certificate.fingerprint256.replaceAll(":", ""), expiresAt,
|
|
93
|
+
renewalDue: expiresAt - now <= 7 * DAY, reused };
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof DesktopAuthorityError) throw error;
|
|
96
|
+
throw new DesktopAuthorityError("unreadable");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function readAuthority(path: string, protection: AuthorityKeyProtection, now: number, allowExpired = false): Promise<StoredDesktopAuthority> {
|
|
101
|
+
assertPath(path, false);
|
|
102
|
+
await hardenSecretPathAsync(path, { required: true });
|
|
103
|
+
assertPath(path, false);
|
|
104
|
+
let cleartext: Uint8Array | undefined;
|
|
105
|
+
try {
|
|
106
|
+
const saved = JSON.parse(readFileSync(path, "utf8"));
|
|
107
|
+
if (saved.version !== 1 || saved.protection !== "windows-current-user-dpapi"
|
|
108
|
+
|| typeof saved.certPem !== "string" || typeof saved.sealed !== "string"
|
|
109
|
+
|| !/^[A-Za-z0-9+/]+={0,2}$/.test(saved.sealed) || saved.sealed.length > MAX_FILE) throw new Error("invalid");
|
|
110
|
+
cleartext = await protection.unprotect(Buffer.from(saved.sealed, "base64"));
|
|
111
|
+
const secret = JSON.parse(Buffer.from(cleartext).toString("utf8"));
|
|
112
|
+
if (secret.policy !== POLICY || secret.certSha256 !== digest(saved.certPem)
|
|
113
|
+
|| typeof secret.keyPem !== "string" || typeof secret.commonName !== "string") throw new Error("invalid");
|
|
114
|
+
return validatedAuthority(saved.certPem, secret.keyPem, secret.commonName, now, true, allowExpired);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error instanceof DesktopAuthorityError) throw error;
|
|
117
|
+
// A corrupt or foreign-user key never silently regenerates a new root or trust prompt.
|
|
118
|
+
throw new DesktopAuthorityError("unreadable");
|
|
119
|
+
} finally { cleartext?.fill(0); }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Loads only existing state. Expired or legacy-purpose keys may be loaded solely for trust removal/renewal. */
|
|
123
|
+
export async function loadDesktopCompatibilityAuthority(options: DesktopAuthorityStoreOptions, allowExpiredForRemoval = false): Promise<StoredDesktopAuthority> {
|
|
124
|
+
if (!isAbsolute(options.directory)) throw new DesktopAuthorityError("unsafe_path");
|
|
125
|
+
const now = options.now?.() ?? Date.now();
|
|
126
|
+
if (!Number.isFinite(now)) throw new DesktopAuthorityError("unreadable");
|
|
127
|
+
assertPath(options.directory, true);
|
|
128
|
+
const path = join(options.directory, FILE);
|
|
129
|
+
if (!pathPresent(path)) throw new DesktopAuthorityError("unreadable");
|
|
130
|
+
return readAuthority(path, options.protection ?? windowsAuthorityKeyProtection, now, allowExpiredForRemoval);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Reuses one user-protected root across restarts; never installs, rotates or removes trust. */
|
|
134
|
+
export async function ensureDesktopCompatibilityAuthority(options: DesktopAuthorityStoreOptions): Promise<StoredDesktopAuthority> {
|
|
135
|
+
return publishAuthority(options);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Deliberate replacement after the caller has removed OS trust and stopped consumers. */
|
|
139
|
+
export async function renewDesktopCompatibilityAuthority(options: DesktopAuthorityStoreOptions, expectedFingerprint: string): Promise<StoredDesktopAuthority> {
|
|
140
|
+
if (!/^[A-F0-9]{64}$/.test(expectedFingerprint)) throw new DesktopAuthorityError("fingerprint_changed");
|
|
141
|
+
return publishAuthority(options, expectedFingerprint);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function publishAuthority(options: DesktopAuthorityStoreOptions, expectedFingerprint?: string): Promise<StoredDesktopAuthority> {
|
|
145
|
+
if (!isAbsolute(options.directory)) throw new DesktopAuthorityError("unsafe_path");
|
|
146
|
+
if (!options.protection && process.platform !== "win32") throw new Error("desktop_compatibility_windows_required");
|
|
147
|
+
const now = options.now?.() ?? Date.now();
|
|
148
|
+
if (!Number.isFinite(now)) throw new DesktopAuthorityError("unreadable");
|
|
149
|
+
const protection = options.protection ?? windowsAuthorityKeyProtection;
|
|
150
|
+
mkdirSync(options.directory, { recursive: true, mode: 0o700 });
|
|
151
|
+
assertPath(options.directory, true);
|
|
152
|
+
await hardenSecretDirAsync(options.directory, { required: true });
|
|
153
|
+
return withClientLifecycle(async () => {
|
|
154
|
+
assertPath(options.directory, true);
|
|
155
|
+
const path = join(options.directory, FILE);
|
|
156
|
+
let original: string | undefined;
|
|
157
|
+
if (expectedFingerprint !== undefined) {
|
|
158
|
+
if (!pathPresent(path)) throw new DesktopAuthorityError("fingerprint_changed");
|
|
159
|
+
const previous = await readAuthority(path, protection, now, true);
|
|
160
|
+
if (previous.fingerprint !== expectedFingerprint) throw new DesktopAuthorityError("fingerprint_changed");
|
|
161
|
+
original = readFileSync(path, "utf8");
|
|
162
|
+
} else if (pathPresent(path)) return readAuthority(path, protection, now);
|
|
163
|
+
const commonName = `OpenCodex Codex Desktop ${randomUUID()}`;
|
|
164
|
+
const authority = createCertificateAuthority({ commonName, validityDays: VALIDITY_DAYS,
|
|
165
|
+
permittedDnsNames: ["chatgpt.com"], excludeAllIpAddresses: true, serverAuthOnly: true });
|
|
166
|
+
const cleartext = Buffer.from(JSON.stringify({ policy: POLICY, certSha256: digest(authority.certPem), commonName, keyPem: authority.keyPem }));
|
|
167
|
+
let sealed: Uint8Array;
|
|
168
|
+
try { sealed = await protection.protect(cleartext); }
|
|
169
|
+
catch { throw new DesktopAuthorityError("protection_failed"); }
|
|
170
|
+
finally { cleartext.fill(0); }
|
|
171
|
+
if (sealed.byteLength === 0 || sealed.byteLength > 65_536) throw new DesktopAuthorityError("protection_failed");
|
|
172
|
+
const contents = JSON.stringify({ version: 1, protection: "windows-current-user-dpapi", certPem: authority.certPem,
|
|
173
|
+
sealed: Buffer.from(sealed).toString("base64") });
|
|
174
|
+
const temporary = join(options.directory, `authority-${randomUUID()}.tmp`);
|
|
175
|
+
let created = false;
|
|
176
|
+
try {
|
|
177
|
+
const fd = openSync(temporary, "wx", 0o600); created = true;
|
|
178
|
+
try { writeFileSync(fd, contents); fsyncSync(fd); } finally { closeSync(fd); }
|
|
179
|
+
await hardenSecretPathAsync(temporary, { required: true });
|
|
180
|
+
assertPath(temporary, false); assertPath(options.directory, true);
|
|
181
|
+
// Verify the new encrypted envelope before replacing the only recovery source.
|
|
182
|
+
await readAuthority(temporary, protection, now);
|
|
183
|
+
if (original !== undefined) {
|
|
184
|
+
assertPath(path, false);
|
|
185
|
+
if (readFileSync(path, "utf8") !== original) throw new DesktopAuthorityError("fingerprint_changed");
|
|
186
|
+
} else if (pathPresent(path)) throw new DesktopAuthorityError("unsafe_path");
|
|
187
|
+
renameSync(temporary, path); created = false;
|
|
188
|
+
// Read back the actual persisted pair before reporting a successful setup.
|
|
189
|
+
const persisted = await readAuthority(path, protection, now);
|
|
190
|
+
return { ...persisted, reused: false };
|
|
191
|
+
} finally { if (created) unlinkSync(temporary); }
|
|
192
|
+
}, { lockPath: join(options.directory, "authority-publication.sqlite") });
|
|
193
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { closeSync, fsyncSync, linkSync, lstatSync, openSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, join } from "node:path";
|
|
4
|
+
import { withClientLifecycle } from "../../client/lifecycle-lock";
|
|
5
|
+
import { hardenSecretDirAsync, hardenSecretPathAsync } from "../../lib/windows-secret-acl";
|
|
6
|
+
|
|
7
|
+
export interface DesktopConnectionIdentity {
|
|
8
|
+
version: 1;
|
|
9
|
+
id: string;
|
|
10
|
+
connectPort: number;
|
|
11
|
+
pacPort: number;
|
|
12
|
+
}
|
|
13
|
+
export interface DesktopConnectionStore {
|
|
14
|
+
read(): DesktopConnectionIdentity | null;
|
|
15
|
+
publish(value: DesktopConnectionIdentity): Promise<DesktopConnectionIdentity>;
|
|
16
|
+
}
|
|
17
|
+
const FILE = "connection.json";
|
|
18
|
+
const fail = (reason: "invalid" | "changed" | "cleanup_required", cause?: unknown) => new Error(`desktop_compatibility_connection_${reason}`, { cause });
|
|
19
|
+
const port = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 1024 && Number(value) <= 65535;
|
|
20
|
+
function validate(value: unknown): DesktopConnectionIdentity {
|
|
21
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw fail("invalid");
|
|
22
|
+
const row = value as Record<string, unknown>;
|
|
23
|
+
if (Object.keys(row).length !== 4 || row.version !== 1 || typeof row.id !== "string"
|
|
24
|
+
|| !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(row.id)
|
|
25
|
+
|| !port(row.connectPort) || !port(row.pacPort) || row.connectPort === row.pacPort) throw fail("invalid");
|
|
26
|
+
return { version: 1, id: row.id, connectPort: row.connectPort, pacPort: row.pacPort };
|
|
27
|
+
}
|
|
28
|
+
function assertPath(path: string, directory: boolean): void {
|
|
29
|
+
const stat = lstatSync(path);
|
|
30
|
+
if (stat.isSymbolicLink() || (directory ? !stat.isDirectory() : !stat.isFile() || stat.nlink !== 1 || stat.size > 4096)) throw fail("invalid");
|
|
31
|
+
}
|
|
32
|
+
function present(path: string): boolean {
|
|
33
|
+
try { lstatSync(path); return true; }
|
|
34
|
+
catch (error) { if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return false; throw fail("invalid"); }
|
|
35
|
+
}
|
|
36
|
+
function same(left: DesktopConnectionIdentity, right: DesktopConnectionIdentity): boolean {
|
|
37
|
+
return left.version === right.version && left.id === right.id && left.connectPort === right.connectPort && left.pacPort === right.pacPort;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Public endpoint identity only. No token, account, private key or expiry is persisted here. */
|
|
41
|
+
export function createDesktopConnectionStore(directory: string): DesktopConnectionStore {
|
|
42
|
+
if (!isAbsolute(directory)) throw fail("invalid");
|
|
43
|
+
const path = join(directory, FILE);
|
|
44
|
+
const read = (): DesktopConnectionIdentity | null => {
|
|
45
|
+
try {
|
|
46
|
+
if (!present(directory)) return null;
|
|
47
|
+
assertPath(directory, true);
|
|
48
|
+
if (!present(path)) return null;
|
|
49
|
+
assertPath(path, false);
|
|
50
|
+
return validate(JSON.parse(readFileSync(path, "utf8")));
|
|
51
|
+
} catch { throw fail("invalid"); }
|
|
52
|
+
};
|
|
53
|
+
const verifyExisting = (value: DesktopConnectionIdentity): DesktopConnectionIdentity | null => {
|
|
54
|
+
const existing = read();
|
|
55
|
+
if (existing && !same(existing, value)) throw fail("changed");
|
|
56
|
+
return existing;
|
|
57
|
+
};
|
|
58
|
+
return { read, async publish(input) {
|
|
59
|
+
const value = validate(input);
|
|
60
|
+
const existing = verifyExisting(value); if (existing) return existing;
|
|
61
|
+
// A trusted authority must already own this directory. Never create a replacement home.
|
|
62
|
+
assertPath(directory, true); await hardenSecretDirAsync(directory, { required: true });
|
|
63
|
+
return withClientLifecycle(async () => {
|
|
64
|
+
const raced = verifyExisting(value); if (raced) return raced;
|
|
65
|
+
const temporary = join(directory, `connection-${randomUUID()}.tmp`);
|
|
66
|
+
let created = false;
|
|
67
|
+
try {
|
|
68
|
+
const fd = openSync(temporary, "wx", 0o600); created = true;
|
|
69
|
+
try { writeFileSync(fd, JSON.stringify(value)); fsyncSync(fd); } finally { closeSync(fd); }
|
|
70
|
+
await hardenSecretPathAsync(temporary, { required: true });
|
|
71
|
+
assertPath(directory, true); assertPath(temporary, false);
|
|
72
|
+
// Create-only publication cannot overwrite another process's endpoint identity.
|
|
73
|
+
try { linkSync(temporary, path); }
|
|
74
|
+
catch (error) {
|
|
75
|
+
const winner = verifyExisting(value); if (!winner) throw error;
|
|
76
|
+
}
|
|
77
|
+
unlinkSync(temporary); created = false;
|
|
78
|
+
const published = verifyExisting(value); if (!published) throw fail("changed");
|
|
79
|
+
return published;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
try { if (created && present(temporary)) unlinkSync(temporary); }
|
|
82
|
+
catch (cleanupError) { throw fail("cleanup_required", new AggregateError([error, cleanupError], "Endpoint publication and cleanup failed")); }
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}, { lockPath: join(directory, "connection-publication.sqlite") });
|
|
86
|
+
} };
|
|
87
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { win32 } from "node:path";
|
|
3
|
+
import { isTestHomeGuardArmed } from "../../lib/test-home-guard";
|
|
4
|
+
import { resolveTrustedWindowsPowerShellExe } from "../../lib/windows-elevation";
|
|
5
|
+
import { parseWindowsDesktopPackage, WINDOWS_PACKAGE_DISCOVERY_SCRIPT } from "../desktop-app/windows";
|
|
6
|
+
import type { DesktopAppInstall } from "../desktop-app/types";
|
|
7
|
+
|
|
8
|
+
export const DESKTOP_COMPATIBILITY_ASSESSED_VERSION = "26.924.2738.0";
|
|
9
|
+
export const DESKTOP_COMPATIBILITY_ASSESSED_FAMILY = "OpenAI.Codex_2p2nqsd0c76g0";
|
|
10
|
+
|
|
11
|
+
function matchesAssessedPackage(install: DesktopAppInstall): boolean {
|
|
12
|
+
const name = win32.basename(install.root);
|
|
13
|
+
const parts = /^OpenAI\.Codex_(\d+\.\d+\.\d+\.\d+)_(x64|arm64|x86|neutral)_([A-Za-z0-9.-]*)_([A-Za-z0-9]+)$/.exec(name);
|
|
14
|
+
return install.id === DESKTOP_COMPATIBILITY_ASSESSED_FAMILY && install.relaunch === `${install.id}!App`
|
|
15
|
+
&& !!parts && parts[1] === DESKTOP_COMPATIBILITY_ASSESSED_VERSION && `OpenAI.Codex_${parts[4]}` === install.id;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Resolve only after the exact asynchronous child has closed, including abort/timeout. */
|
|
19
|
+
function queryInstalledPackage(signal: AbortSignal): Promise<DesktopAppInstall | null> {
|
|
20
|
+
if (process.platform !== "win32" || isTestHomeGuardArmed() || signal.aborted) return Promise.resolve(null);
|
|
21
|
+
return new Promise(resolve => {
|
|
22
|
+
let completed = false, closed = false, result: DesktopAppInstall | null = null;
|
|
23
|
+
const finish = () => { if (completed && closed) { signal.removeEventListener("abort", abort); resolve(result); } };
|
|
24
|
+
const child = execFile(resolveTrustedWindowsPowerShellExe(), ["-NoProfile", "-NonInteractive", "-Command", WINDOWS_PACKAGE_DISCOVERY_SCRIPT], {
|
|
25
|
+
timeout: 10000, maxBuffer: 65536, encoding: "utf8", windowsHide: true,
|
|
26
|
+
}, (error, stdout) => { result = error ? null : parseWindowsDesktopPackage(stdout); completed = true; finish(); });
|
|
27
|
+
const abort = () => { try { child.kill(); } catch { /* Keep ownership until execFile reports close. */ } };
|
|
28
|
+
child.once("close", () => { closed = true; finish(); });
|
|
29
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
30
|
+
if (signal.aborted) abort();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Coalesce concurrent fresh probes; never cache a positive verdict across requests. */
|
|
35
|
+
export function createInstalledBuildProbe(query: (signal: AbortSignal) => Promise<DesktopAppInstall | null> = queryInstalledPackage) {
|
|
36
|
+
let pending: Promise<boolean> | null = null, controller: AbortController | null = null, closed = false;
|
|
37
|
+
return {
|
|
38
|
+
check(): Promise<boolean> {
|
|
39
|
+
if (closed) return Promise.resolve(false);
|
|
40
|
+
if (pending) return pending;
|
|
41
|
+
const current = new AbortController(); controller = current;
|
|
42
|
+
pending = Promise.resolve().then(() => current.signal.aborted ? null : query(current.signal))
|
|
43
|
+
.then(install => !closed && !current.signal.aborted && !!install
|
|
44
|
+
&& matchesAssessedPackage(install))
|
|
45
|
+
.catch(() => false).finally(() => { pending = null; controller = null; });
|
|
46
|
+
return pending;
|
|
47
|
+
},
|
|
48
|
+
async close(): Promise<void> { closed = true; controller?.abort(); await pending; },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function isAssessedDesktopInstalled(): Promise<boolean> {
|
|
53
|
+
const probe = createInstalledBuildProbe();
|
|
54
|
+
try { return await probe.check(); } finally { await probe.close(); }
|
|
55
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Buffer small JSON only; large bodies pass through without collecting the remainder. */
|
|
2
|
+
export async function boundedJsonBody(body: ReadableStream<Uint8Array> | null, maxBytes = 262144): Promise<{ bytes: Buffer } | { stream: ReadableStream<Uint8Array> }> {
|
|
3
|
+
if (!body) return { bytes: Buffer.alloc(0) };
|
|
4
|
+
const reader = body.getReader();
|
|
5
|
+
const chunks: Uint8Array[] = [];
|
|
6
|
+
let count = 0;
|
|
7
|
+
try {
|
|
8
|
+
while (true) {
|
|
9
|
+
const next = await reader.read();
|
|
10
|
+
if (next.done) { reader.releaseLock(); return { bytes: Buffer.concat(chunks, count) }; }
|
|
11
|
+
chunks.push(next.value); count += next.value.byteLength;
|
|
12
|
+
if (count <= maxBytes && chunks.length < 1024) continue;
|
|
13
|
+
let index = 0;
|
|
14
|
+
return { stream: new ReadableStream<Uint8Array>({
|
|
15
|
+
async pull(controller) {
|
|
16
|
+
if (index < chunks.length) {
|
|
17
|
+
const value = chunks[index]!; chunks[index++] = new Uint8Array(0); controller.enqueue(value); return;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const part = await reader.read();
|
|
21
|
+
if (part.done) { reader.releaseLock(); controller.close(); }
|
|
22
|
+
else controller.enqueue(part.value);
|
|
23
|
+
} catch (error) { reader.releaseLock(); controller.error(error); }
|
|
24
|
+
},
|
|
25
|
+
async cancel(reason) { try { await reader.cancel(reason); } finally { reader.releaseLock(); } },
|
|
26
|
+
}) };
|
|
27
|
+
}
|
|
28
|
+
} catch (error) { reader.releaseLock(); throw error; }
|
|
29
|
+
}
|