@bitkyc08/opencodex 2.86.0 → 2.88.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/gui/dist/assets/App-CHC89EXQ.js +58 -0
- package/gui/dist/assets/App-wT3mXqFz.css +1 -0
- package/gui/dist/assets/{Tray-DyR9yBee.js → Tray-B5bC7AWL.js} +1 -1
- package/gui/dist/assets/index-B5bsvxor.js +94 -0
- package/gui/dist/assets/index-DPeB9HpV.css +1 -0
- package/gui/dist/assets/quota-summary-D8y2wTsB.js +1 -0
- package/gui/dist/index.html +2 -2
- package/package.json +1 -1
- package/src/adapters/anthropic/beta-allowlist.ts +9 -8
- package/src/adapters/anthropic/beta-body-pairs.ts +93 -0
- package/src/adapters/anthropic/passthrough.ts +7 -23
- package/src/adapters/anthropic-progress.ts +84 -0
- package/src/adapters/anthropic-rejection-memo.ts +71 -0
- package/src/adapters/anthropic-replay.ts +135 -0
- package/src/adapters/anthropic-tool-catalog.ts +11 -0
- package/src/adapters/anthropic.ts +151 -33
- package/src/adapters/base.ts +2 -0
- package/src/adapters/claude-cli/adapter.ts +111 -23
- package/src/adapters/claude-cli/profiles.ts +2 -1
- package/src/adapters/claude-cli/stable-replay.ts +108 -0
- package/src/adapters/claude-cli/usage-admission.ts +230 -0
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/coding-agent/turn.ts +18 -2
- package/src/adapters/dsh-account.ts +97 -0
- package/src/adapters/google-progress-history.ts +53 -0
- package/src/adapters/google.ts +52 -9
- package/src/adapters/openai-chat/messages.ts +4 -3
- package/src/adapters/openai-chat/passthrough.ts +8 -2
- package/src/adapters/openai-chat/reasoning-replay.ts +52 -0
- package/src/adapters/openai-responses/compaction-search-history.ts +14 -7
- package/src/adapters/openai-responses/passthrough.ts +57 -125
- package/src/adapters/openai-responses/request-strips.ts +25 -3
- package/src/adapters/openai-responses/transport.ts +81 -0
- package/src/adapters/qoder/adapter.ts +4 -2
- package/src/adapters/registry.ts +5 -0
- package/src/adapters/zed.ts +26 -1
- package/src/advisor/activity.ts +55 -0
- package/src/advisor/budget.ts +62 -0
- package/src/advisor/consult.ts +4 -1
- package/src/advisor/context.ts +16 -4
- package/src/advisor/evidence-tool.ts +21 -0
- package/src/advisor/evidence.ts +117 -0
- package/src/advisor/runtime.ts +79 -31
- package/src/advisor/settings.ts +11 -2
- package/src/bridge/errors.ts +19 -1
- package/src/bridge/internal.ts +7 -1
- package/src/bridge/response-json.ts +18 -3
- package/src/bridge/sse.ts +59 -8
- package/src/bridge/terminal-refusal.ts +59 -0
- package/src/claude/desktop-3p-library.ts +32 -7
- package/src/claude/desktop-3p.ts +55 -13
- package/src/claude/desktop-first-party.ts +1 -1
- package/src/claude/desktop-picker-platform.ts +4 -0
- package/src/claude/desktop-picker.ts +15 -8
- package/src/claude/desktop-system-proxy.ts +50 -4
- package/src/claude/inbound-content-options.ts +77 -0
- package/src/claude/inbound.ts +33 -0
- package/src/claude/intercept/picker-ca-startup.ts +2 -1
- package/src/claude/intercept/picker-runtime.ts +6 -5
- package/src/claude/intercept/picker-trust-windows.ts +338 -0
- package/src/claude/intercept/picker-trust.ts +19 -0
- package/src/claude/intercept/port-policy.ts +30 -0
- package/src/claude/intercept/runtime.ts +97 -94
- package/src/claude/outbound.ts +46 -13
- package/src/cli/account-api.ts +1 -0
- package/src/cli/account-policy-dto.ts +6 -0
- package/src/cli/account-policy.ts +34 -5
- package/src/cli/account-preservation.ts +44 -0
- package/src/cli/account.ts +8 -2
- package/src/cli/advisor.ts +17 -3
- package/src/cli/capabilities-accounts.ts +20 -1
- package/src/cli/capabilities-agents-routing.ts +5 -2
- package/src/cli/capabilities-base.ts +38 -10
- package/src/cli/capabilities-observe-system.ts +63 -3
- package/src/cli/capabilities-provider-models.ts +11 -0
- package/src/cli/codex-cli-update.ts +151 -41
- package/src/cli/codex-provider.ts +44 -0
- package/src/cli/combo-input.ts +10 -1
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +2 -1
- package/src/cli/cross-home-owner.ts +33 -16
- package/src/cli/dispatch.ts +25 -4
- package/src/cli/doctor.ts +6 -1
- package/src/cli/ensure-desktop-cold-start.ts +60 -7
- package/src/cli/help.ts +3 -2
- package/src/cli/index.ts +1 -1
- package/src/cli/integration-plan-dto.ts +1 -0
- package/src/cli/log-history.ts +172 -0
- package/src/cli/loopback-family.ts +31 -0
- package/src/cli/models-runtime-subcommands.ts +1 -0
- package/src/cli/models-runtime.ts +72 -8
- package/src/cli/observe-stream.ts +2 -0
- package/src/cli/observe.ts +16 -4
- package/src/cli/provider.ts +27 -5
- package/src/cli/registry.ts +28 -8
- package/src/cli/runtime-api.ts +5 -3
- package/src/cli/system-command.ts +40 -8
- package/src/cli/uninstall-client-state.ts +32 -0
- package/src/cli/usage-compare.ts +309 -0
- package/src/cli/usage-model-search.ts +4 -4
- package/src/cli/usage-report.ts +2 -2
- package/src/cli/v2.ts +1 -1
- package/src/client/machine-listener.ts +1 -3
- package/src/client/runtime.ts +1 -0
- package/src/clients/client-path.ts +51 -0
- package/src/clients/config-export/contracts.ts +2 -1
- package/src/clients/config-export.ts +18 -44
- package/src/clients/gentle-shell-path.ts +87 -0
- package/src/codex/account-auto-switch.ts +2 -0
- package/src/codex/account-identity.ts +29 -0
- package/src/codex/account-label.ts +1 -1
- package/src/codex/account-lifecycle.ts +21 -5
- package/src/codex/account-preservation.ts +28 -0
- package/src/codex/account-use-remaining.ts +23 -0
- package/src/codex/app-server-processes.ts +97 -7
- package/src/codex/app-server-restart-service.ts +55 -4
- package/src/codex/auth-api/account-list.ts +4 -0
- package/src/codex/auth-api/account-pause-group.ts +6 -50
- package/src/codex/auth-api/account-preservation.ts +57 -0
- package/src/codex/auth-api/routes.ts +8 -0
- package/src/codex/auth-api/use-remaining-route.ts +36 -0
- package/src/codex/auth-api.ts +0 -1
- package/src/codex/auth-context.ts +2 -2
- package/src/codex/catalog/auto-review.ts +20 -3
- package/src/codex/catalog/build-entries.ts +9 -1
- package/src/codex/catalog/combo-member.ts +14 -5
- package/src/codex/catalog/configured-tiers.ts +1 -1
- package/src/codex/catalog/gather-capture.ts +2 -0
- package/src/codex/catalog/metadata.ts +11 -9
- package/src/codex/catalog/model-hints.ts +21 -16
- package/src/codex/catalog/model-visibility.ts +3 -1
- package/src/codex/catalog/parsing.ts +30 -86
- package/src/codex/catalog/provider-models.ts +36 -34
- package/src/codex/catalog/routed-gather.ts +14 -13
- package/src/codex/cli-install-provenance.ts +105 -17
- package/src/codex/cli-update-plan.ts +545 -0
- package/src/codex/config-write-lock.ts +23 -2
- package/src/codex/desktop-app/picker-freshness.ts +96 -0
- package/src/codex/desktop-app-restart.ts +21 -0
- package/src/codex/desktop-compatibility/installed-build.ts +24 -6
- package/src/codex/desktop-compatibility/routing-preflight.ts +1 -1
- package/src/codex/desktop-compatibility/runtime.ts +12 -3
- package/src/codex/desktop-compatibility/windows-key-protection.ts +115 -9
- package/src/codex/features.ts +85 -45
- package/src/codex/inject/history-failure.ts +27 -0
- package/src/codex/inject/restore.ts +24 -2
- package/src/codex/inject/routing-classify.ts +4 -3
- package/src/codex/inject.ts +18 -27
- package/src/codex/main-account-hard-lock.ts +30 -1
- package/src/codex/native-profile-startup.ts +1 -1
- package/src/codex/ocx-compaction-history.ts +12 -10
- package/src/codex/paths.ts +86 -1
- package/src/codex/provider-routing.ts +174 -0
- package/src/codex/refresh.ts +2 -2
- package/src/codex/routing/health-store.ts +1 -1
- package/src/codex/routing/selection.ts +4 -3
- package/src/codex/routing/thread-affinity.ts +1 -1
- package/src/codex/signed-in-identity.ts +43 -0
- package/src/codex/subagent-model-fallback.ts +1 -1
- package/src/codex/sync.ts +6 -1
- package/src/codex/user-identity.ts +16 -5
- package/src/codex/websocket-activity.ts +161 -0
- package/src/codex/websocket-registry.ts +25 -1
- package/src/codex/windows-local-appdata.ts +84 -0
- package/src/combos/failover.ts +6 -1
- package/src/combos/index.ts +1 -0
- package/src/combos/resolve.ts +46 -10
- package/src/combos/types.ts +15 -0
- package/src/config/diagnostics.ts +33 -6
- package/src/config/live-reconcile.ts +38 -2
- package/src/config/load-degrade.ts +17 -0
- package/src/config/opencode-wire-upgrade.ts +54 -0
- package/src/config/opencode-zen-upgrade.ts +53 -0
- package/src/config/owner-registry.ts +22 -17
- package/src/config/provider-validation.ts +45 -2
- package/src/config/schema/config-schema.ts +22 -0
- package/src/config/schema/leaf-validators.ts +38 -9
- package/src/config/schema/main-account-hard-lock-fallback.ts +26 -0
- package/src/config/schema/native-reasoning-retention.ts +26 -0
- package/src/config.ts +2 -0
- package/src/generated/compatibility-version.json +657 -297
- package/src/grok/sync.ts +1 -1
- package/src/images/loop.ts +116 -27
- package/src/integrations/catalog-refresh.ts +1 -1
- package/src/integrations/mutation-plan.ts +1 -0
- package/src/integrations/registry.ts +8 -0
- package/src/lib/codex-restart-contract.ts +15 -0
- package/src/lib/desktop-upstream-tunnel.ts +18 -53
- package/src/lib/empty-tool-catalog.ts +62 -0
- package/src/lib/errors.ts +19 -3
- package/src/lib/gcp-adc.ts +96 -9
- package/src/lib/gcp-credential-input.ts +12 -0
- package/src/lib/lab-live-route-production.ts +5 -0
- package/src/lib/opaque-proxy-tunnel.ts +217 -0
- package/src/lib/redact.ts +2 -2
- package/src/lib/request-failure-model.ts +2 -0
- package/src/oauth/account-quota-rank.ts +69 -2
- package/src/oauth/anthropic-account-refusal.ts +20 -2
- package/src/oauth/anthropic-oauth-definitions.ts +3 -1
- package/src/oauth/anthropic-routing.ts +1 -21
- package/src/oauth/anthropic.ts +38 -7
- package/src/oauth/callback-server.ts +27 -1
- package/src/oauth/chatgpt-device.ts +1 -1
- package/src/oauth/chatgpt-shared.ts +83 -0
- package/src/oauth/chatgpt.ts +8 -72
- package/src/oauth/dsh.ts +203 -0
- package/src/oauth/generic-account-failover.ts +40 -11
- package/src/oauth/generic-session-affinity.ts +125 -0
- package/src/oauth/index.ts +184 -109
- package/src/oauth/local-token-detect.ts +21 -1
- package/src/oauth/login-cli-errors.ts +44 -0
- package/src/oauth/login-cli.ts +69 -7
- package/src/oauth/login-flow-state.ts +371 -23
- package/src/oauth/meta-muse-errors.ts +41 -0
- package/src/oauth/meta-muse-keychain.ts +52 -0
- package/src/oauth/meta-muse.ts +51 -84
- package/src/oauth/model-discovery-auth.ts +3 -0
- package/src/oauth/pool-settings-capability.ts +4 -2
- package/src/oauth/refresh-flight-retirement.ts +48 -0
- package/src/oauth/session-affinity-key.ts +23 -0
- package/src/oauth/store-anthropic-instance.ts +9 -2
- package/src/oauth/store.ts +12 -5
- package/src/protocols/encoders/adapter-events.ts +6 -5
- package/src/protocols/encoders/messages.ts +9 -6
- package/src/protocols/provider-summary.ts +2 -1
- package/src/providers/api-key-cooldowns.ts +49 -0
- package/src/providers/api-key-resolve.ts +11 -6
- package/src/providers/api-key-response-ownership.ts +57 -0
- package/src/providers/api-key-selection.ts +31 -6
- package/src/providers/api-keys.ts +214 -3
- package/src/providers/claude-cli-usage.ts +96 -0
- package/src/providers/dsh-account-destination.ts +31 -0
- package/src/providers/dsh-account-models.ts +41 -0
- package/src/providers/gcp-credential-cleanup.ts +59 -0
- package/src/providers/key-failover.ts +54 -50
- package/src/providers/key-store.ts +7 -2
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/model-rename-migration.ts +2 -2
- package/src/providers/openai-tiers-destination.ts +11 -2
- package/src/providers/openai-tiers.ts +5 -12
- package/src/providers/opencode-go-transport.ts +3 -1
- package/src/providers/opencode-zen-rate-limit.ts +7 -2
- package/src/providers/quota/account-cache.ts +6 -5
- package/src/providers/quota/account-row.ts +34 -0
- package/src/providers/quota/antigravity.ts +28 -7
- package/src/providers/quota/dsh-account.ts +70 -0
- package/src/providers/quota/vendor-probes-oauth.ts +1 -93
- package/src/providers/quota-types.ts +2 -0
- package/src/providers/quota.ts +14 -20
- package/src/providers/registry/entries-core.ts +37 -8
- package/src/providers/registry/types.ts +1 -1
- package/src/providers/request-pacing.ts +77 -19
- package/src/providers/routed-config.ts +339 -0
- package/src/providers/service-tier.ts +2 -1
- package/src/quota/window-mapping.ts +5 -2
- package/src/responses/anthropic-progress.ts +34 -0
- package/src/responses/assistant-replay.ts +35 -0
- package/src/responses/compaction-history-framing.ts +65 -0
- package/src/responses/compaction.ts +6 -1
- package/src/responses/incomplete-reason.ts +31 -0
- package/src/responses/parser.ts +50 -5
- package/src/responses/reasoning-envelope.ts +4 -0
- package/src/responses/reasoning-replay-cache.ts +74 -6
- package/src/responses/reasoning-retention.ts +173 -0
- package/src/responses/retained-compaction.ts +46 -0
- package/src/responses/schema.ts +23 -0
- package/src/router.ts +8 -321
- package/src/routing/analytics.ts +7 -15
- package/src/routing/health.ts +37 -22
- package/src/routing/history/indexer.ts +6 -5
- package/src/routing/history/ingest.ts +6 -2
- package/src/routing/profile.ts +1 -1
- package/src/server/admission-model-scope.ts +17 -0
- package/src/server/auth-cors.ts +11 -0
- package/src/server/chat-completions.ts +7 -1
- package/src/server/chat-native-reasoning.ts +123 -0
- package/src/server/chat-native-sse.ts +110 -7
- package/src/server/chat-native.ts +76 -17
- package/src/server/claude-messages.ts +56 -414
- package/src/server/effort-policy.ts +1 -1
- package/src/server/images.ts +15 -2
- package/src/server/index/live-sideband.ts +3 -2
- package/src/server/index/model-alias-id.ts +36 -0
- package/src/server/index/optional-listeners.ts +2 -0
- package/src/server/index/serve-options.ts +20 -6
- package/src/server/index/startup-warnings.ts +2 -0
- package/src/server/index/websocket-handler.ts +18 -4
- package/src/server/index.ts +8 -8
- package/src/server/inference/advisor-diagnostics.ts +31 -0
- package/src/server/lifecycle.ts +13 -1
- package/src/server/management/advisor-routes.ts +15 -4
- package/src/server/management/claude-desktop-picker-routes.ts +1 -1
- package/src/server/management/combo-routes.ts +176 -50
- package/src/server/management/companion-routes.ts +1 -1
- package/src/server/management/config-routes.ts +75 -4
- package/src/server/management/cursor-integration-routes.ts +1 -1
- package/src/server/management/model-routes.ts +11 -6
- package/src/server/management/model-rows.ts +14 -0
- package/src/server/management/native-integration-routes.ts +2 -7
- package/src/server/management/native-reasoning-retention-routes.ts +41 -0
- package/src/server/management/oauth-account-routes.ts +87 -14
- package/src/server/management/provider-gcp-credential-routes.ts +29 -0
- package/src/server/management/provider-key-pacing-routes.ts +31 -0
- package/src/server/management/provider-overwrite-carry.ts +7 -0
- package/src/server/management/provider-routes.ts +25 -30
- package/src/server/management/route-registry.ts +10 -1
- package/src/server/management-api.ts +3 -1
- package/src/server/messages-native-tool-guard.ts +185 -0
- package/src/server/messages-native.ts +117 -24
- package/src/server/messages-shared.ts +431 -0
- package/src/server/relay.ts +1 -1
- package/src/server/request-log-terminal-status.ts +1 -1
- package/src/server/request-log.ts +6 -1
- package/src/server/responses/account-change-state.ts +8 -2
- package/src/server/responses/adapter-continuation.ts +47 -19
- package/src/server/responses/adapter-delivery.ts +72 -12
- package/src/server/responses/adapter-dispatch.ts +46 -3
- package/src/server/responses/advisor-retention.ts +117 -0
- package/src/server/responses/advisor-slot.ts +73 -41
- package/src/server/responses/anthropic-thinking-recovery.ts +153 -0
- package/src/server/responses/codex-ws-wire.ts +15 -8
- package/src/server/responses/combo-stream-preflight.ts +3 -2
- package/src/server/responses/compact.ts +303 -125
- package/src/server/responses/compaction-recovery.ts +20 -10
- package/src/server/responses/compaction-retention.ts +61 -0
- package/src/server/responses/context-overflow.ts +11 -6
- package/src/server/responses/core-auth.ts +6 -0
- package/src/server/responses/core-codex-account.ts +11 -1
- package/src/server/responses/core-combo-failure.ts +2 -0
- package/src/server/responses/core-combo.ts +121 -36
- package/src/server/responses/core-errors.ts +3 -3
- package/src/server/responses/core-lifetime.ts +8 -4
- package/src/server/responses/core-opaque-recovery.ts +35 -0
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-pipeline.ts +141 -0
- package/src/server/responses/core-replay.ts +23 -3
- package/src/server/responses/core.ts +2 -115
- package/src/server/responses/fetch-helpers.ts +73 -19
- package/src/server/responses/main-hard-lock-routing.ts +109 -0
- package/src/server/responses/native-response-control.ts +4 -2
- package/src/server/responses/passthrough-delivery.ts +3 -1
- package/src/server/responses/passthrough-dispatch.ts +15 -13
- package/src/server/responses/passthrough-execution.ts +3 -2
- package/src/server/responses/request-prepare.ts +77 -12
- package/src/server/responses/request-sidecar-auth.ts +2 -2
- package/src/server/responses/request-transport.ts +104 -27
- package/src/server/responses/response-effects.ts +2 -0
- package/src/server/responses/response-tool-ownership.ts +21 -0
- package/src/server/responses/run-turn-execution.ts +96 -30
- package/src/server/responses/sidecar-execution.ts +63 -9
- package/src/server/responses/sidecar-send-budget.ts +2 -2
- package/src/server/responses/terminal-guard.ts +2 -16
- package/src/server/responses-undeclared-tool-guard.ts +1 -1
- package/src/server/ws-bridge.ts +2 -0
- package/src/service/auth-preflight.ts +71 -0
- package/src/service/cli.ts +4 -2
- package/src/service/definition.ts +97 -0
- package/src/service/guards.ts +1 -61
- package/src/service/health-sentinel-linux.ts +112 -0
- package/src/service/health-sentinel-main.ts +76 -57
- package/src/service/health.ts +5 -89
- package/src/service/launchd.ts +27 -7
- package/src/service/logging.ts +72 -9
- package/src/service/repair.ts +2 -1
- package/src/service/state.ts +7 -8
- package/src/service/systemd.ts +1 -1
- package/src/service/windows-guarded-manager.ts +2 -1
- package/src/service/windows-ops.ts +1 -1
- package/src/service/windows-scheduler.ts +2 -7
- package/src/service/windows-taskxml.ts +2 -2
- package/src/service/windows-wscript.ts +14 -0
- package/src/service.ts +4 -2
- package/src/sidecar/auth.ts +2 -0
- package/src/types/config.ts +64 -3
- package/src/types/opencode-go.ts +61 -0
- package/src/types/opencode-zen.ts +35 -0
- package/src/types/provider.ts +32 -6
- package/src/types/request.ts +31 -2
- package/src/types/wire.ts +31 -37
- package/src/types.ts +2 -0
- package/src/update/npm-invocation.mjs +12 -2
- package/src/usage/cost.ts +48 -12
- package/src/usage/expected-prices.ts +3 -0
- package/src/usage/ledger-retention.ts +2 -1
- package/src/usage/ledger-scanner.ts +6 -5
- package/src/usage/log.ts +17 -8
- package/src/usage/summary.ts +30 -11
- package/src/usage/telemetry-contract.ts +1 -0
- package/src/usage/user-cost-overlays.ts +14 -19
- package/src/usage/user-cost-validation.ts +38 -0
- package/src/vision/eligibility.ts +13 -0
- package/src/web-search/anthropic-history.ts +59 -0
- package/src/web-search/loop.ts +236 -73
- package/src/web-search/progress-stream.ts +15 -2
- package/src/web-search/run-turn-loop.ts +9 -1
- package/gui/dist/assets/App-BjlttVLy.js +0 -58
- package/gui/dist/assets/App-CSJvmjNs.css +0 -1
- package/gui/dist/assets/index-BB6uK6tU.css +0 -1
- package/gui/dist/assets/index-BMssP4k3.js +0 -94
- package/gui/dist/assets/quota-summary-D8M1bc_R.js +0 -1
package/src/service/cli.ts
CHANGED
|
@@ -11,8 +11,10 @@ import { statusWinswRaw, type WinswStatus } from "../lib/winsw";
|
|
|
11
11
|
import { withWindowsServiceMutationLock } from "../lib/windows-service-mutation-lock";
|
|
12
12
|
import { maybeShowStarPrompt } from "../cli/star-prompt";
|
|
13
13
|
import { serviceStatusReport } from "./diagnostics";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import { assertServiceAuthEnvironment } from "./auth-preflight";
|
|
15
|
+
import { assertServiceEnvironmentMatchesInstall } from "./guards";
|
|
16
|
+
import { resolveServiceListenPort } from "./definition";
|
|
17
|
+
import { reportServiceServing } from "./health";
|
|
16
18
|
import { platformOps, proxyStillLiveAfterStop, stopTrackedProxyForServiceCommand, installServiceSafely, installFreshWindowsSchedulerSafely, removeServiceInstallState, isServiceInstalled } from "./orchestration";
|
|
17
19
|
import { repairService } from "./repair";
|
|
18
20
|
import type { ServiceRepairVerb } from "./repair";
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of a service definition: the shell command both Unix backends bake into
|
|
3
|
+
* their unit/plist, the port they pin into it, and the proxy environment it carries.
|
|
4
|
+
*
|
|
5
|
+
* Leaf module. It reads config and the token-file path, and asks nothing of the
|
|
6
|
+
* backends that consume it — importing it from `launchd.ts`/`systemd.ts` cannot
|
|
7
|
+
* route back through them (see `installedServiceListenPort` in `health.ts`, which
|
|
8
|
+
* intentionally stays behind because it asks every backend).
|
|
9
|
+
*/
|
|
10
|
+
import { loadConfig } from "../config";
|
|
11
|
+
import { serviceApiTokenFilePath } from "../lib/service-secrets";
|
|
12
|
+
import { PROXY_ENV_KEYS } from "../lib/proxy-env";
|
|
13
|
+
|
|
14
|
+
export function shellQuote(value: string): string {
|
|
15
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Listen port baked into service wrappers / WinSW XML.
|
|
20
|
+
* Priority: explicit override → OCX_BAKE_PORT (update restart) → config.port → 10100.
|
|
21
|
+
* `config.port === 0` means ephemeral for interactive start; services need a stable pin,
|
|
22
|
+
* so treat 0 / invalid like unset (default 10100) instead of baking `--port 0`.
|
|
23
|
+
*/
|
|
24
|
+
export function resolveServiceListenPort(override?: number): number {
|
|
25
|
+
if (typeof override === "number" && Number.isFinite(override) && override > 0 && override <= 65535) {
|
|
26
|
+
return Math.trunc(override);
|
|
27
|
+
}
|
|
28
|
+
const baked = process.env.OCX_BAKE_PORT?.trim();
|
|
29
|
+
if (baked && /^\d+$/.test(baked)) {
|
|
30
|
+
const n = Number(baked);
|
|
31
|
+
if (n > 0 && n <= 65535) return n;
|
|
32
|
+
}
|
|
33
|
+
const configured = loadConfig().port;
|
|
34
|
+
if (typeof configured === "number" && configured > 0 && configured <= 65535) return configured;
|
|
35
|
+
return 10100;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function buildServiceShellCommand(bun: string, cli: string | null, port = resolveServiceListenPort()): string {
|
|
39
|
+
const tokenFile = serviceApiTokenFilePath();
|
|
40
|
+
const args = cli ? `${shellQuote(cli)} start` : "start";
|
|
41
|
+
return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(bun)} ${args} --port ${port}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The same command shape, launched through a stable `ocx` executable instead of an
|
|
46
|
+
* explicit Bun + CLI pair. The token-file preamble is identical and deliberately shared
|
|
47
|
+
* in form: the service still reads the token from disk at start and never carries it in
|
|
48
|
+
* the unit.
|
|
49
|
+
*/
|
|
50
|
+
export function buildServiceLauncherShellCommand(launcher: string, port = resolveServiceListenPort()): string {
|
|
51
|
+
const tokenFile = serviceApiTokenFilePath();
|
|
52
|
+
return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(launcher)} start --port ${port}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Shared tail parser for the baked `--port <n>`.
|
|
57
|
+
*
|
|
58
|
+
* Terminators cover all three artifact shapes: whitespace (batch wrapper, systemd
|
|
59
|
+
* unit), `"` (systemd's quoted ExecStart), `<` (WinSW's `</arguments>`), and `&` (an
|
|
60
|
+
* XML-escaped quote). Matched LAST because every artifact carries the Bun and CLI
|
|
61
|
+
* paths ahead of the argument, and a path containing the literal must not shadow it.
|
|
62
|
+
*/
|
|
63
|
+
export function parseBakedListenPort(read: () => string): number | null {
|
|
64
|
+
try {
|
|
65
|
+
const last = [...read().matchAll(/start --port (\d{1,5})(?:\s|"|&|<|$)/gm)].at(-1);
|
|
66
|
+
if (!last) return null;
|
|
67
|
+
const n = Number(last[1]);
|
|
68
|
+
return n > 0 && n <= 65535 ? n : null;
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Outbound proxy settings the installing shell had, resolved for baking into a service
|
|
76
|
+
* definition.
|
|
77
|
+
*
|
|
78
|
+
* A service manager does not inherit the environment of the shell that installed it, and
|
|
79
|
+
* `ExecStart=/bin/sh -lc` is dash on Ubuntu/WSL — login dash reads `.profile`, not
|
|
80
|
+
* `.bashrc`, which is where proxy exports usually live. So a user who needs a proxy to
|
|
81
|
+
* reach the upstream got a service that dialed direct: the socket was reset, the retry
|
|
82
|
+
* budget drained, and the request surfaced as `502 Provider unreachable` (#2107). The
|
|
83
|
+
* same install driven through `ocx codex-shim` worked, because that path spawns with
|
|
84
|
+
* `{ ...process.env }`.
|
|
85
|
+
*
|
|
86
|
+
* Lower-case variants are honored because curl-style tooling sets them and the runtime's
|
|
87
|
+
* own `applyProxyEnv` already treats both cases as equivalent. Only the canonical
|
|
88
|
+
* upper-case name is baked, so a definition never carries two spellings of one setting.
|
|
89
|
+
*/
|
|
90
|
+
export function resolvedProxyEnv(env: NodeJS.ProcessEnv = process.env): { name: string; value: string }[] {
|
|
91
|
+
const resolved: { name: string; value: string }[] = [];
|
|
92
|
+
for (const key of PROXY_ENV_KEYS) {
|
|
93
|
+
const value = env[key]?.trim() || env[key.toLowerCase()]?.trim();
|
|
94
|
+
if (value) resolved.push({ name: key, value });
|
|
95
|
+
}
|
|
96
|
+
return resolved;
|
|
97
|
+
}
|
package/src/service/guards.ts
CHANGED
|
@@ -2,14 +2,12 @@ import { execSync } from "node:child_process";
|
|
|
2
2
|
import { chmodSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { getConfigDir, loadConfig } from "../config";
|
|
4
4
|
import { withConfigMutationLockSync } from "../config/mutation-lock";
|
|
5
|
-
import { hardenReusedServiceApiToken,
|
|
5
|
+
import { hardenReusedServiceApiToken, serviceApiTokenFilePath } from "../lib/service-secrets";
|
|
6
6
|
import { tokenCollidesWithAdmin } from "../lib/admin-secrets";
|
|
7
7
|
import { randomBytes } from "node:crypto";
|
|
8
8
|
import { hardenSecretDir, hardenSecretPath } from "../lib/windows-secret-acl";
|
|
9
9
|
import { recordOwnedConfigPath } from "../lib/config-ownership";
|
|
10
10
|
import { isTestHomeGuardArmed } from "../lib/test-home-guard";
|
|
11
|
-
import { diagnoseService } from "./diagnostics";
|
|
12
|
-
import type { ServiceDiagnostic } from "./diagnostics";
|
|
13
11
|
import { currentCodexHome, currentOpenCodexHome, resolveServiceState, serviceCodexHomeMatchesInstall, servicePathMatchesInstall } from "./state";
|
|
14
12
|
import { resolveCodexSqliteHome } from "../codex/paths";
|
|
15
13
|
import type { CodexHomeDeps } from "../codex/home";
|
|
@@ -76,20 +74,6 @@ export function assertServiceEnvironmentMatchesInstall(deps: CodexHomeDeps = {})
|
|
|
76
74
|
}
|
|
77
75
|
}
|
|
78
76
|
|
|
79
|
-
/**
|
|
80
|
-
* The `ocx` command a user should rerun for the service state they actually have.
|
|
81
|
-
*
|
|
82
|
-
* `installed` alone is not enough: `repairService()` refuses a Task-Scheduler-plus-WinSW
|
|
83
|
-
* conflict outright, so recommending repair there names a command guaranteed to fail.
|
|
84
|
-
* Install IS the valid conflict recovery, because `installWindows` removes the native
|
|
85
|
-
* backend first. Exported so the guard tests the real selector rather than a copy of it.
|
|
86
|
-
*/
|
|
87
|
-
export function serviceRetryCommand(
|
|
88
|
-
diag: Pick<ServiceDiagnostic, "installed" | "conflict"> = diagnoseService(),
|
|
89
|
-
): string {
|
|
90
|
-
return diag.installed && !diag.conflict ? "ocx service repair" : "ocx service install";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
77
|
/**
|
|
94
78
|
* Refuse a management (admin) token as the data-plane secret.
|
|
95
79
|
*
|
|
@@ -139,50 +123,6 @@ export function assertNotAdminToken(
|
|
|
139
123
|
);
|
|
140
124
|
}
|
|
141
125
|
|
|
142
|
-
/**
|
|
143
|
-
* Preflight for `service install` / `service repair` on the data-plane credential.
|
|
144
|
-
*
|
|
145
|
-
* It used to DEMAND `OPENCODEX_API_AUTH_TOKEN` for a non-loopback hostname, and it threw
|
|
146
|
-
* even when `~/.opencodex/service-api-token` already held a perfectly good token. That is
|
|
147
|
-
* the defect behind the incident this unit exists to close (#4236): an operator exported the
|
|
148
|
-
* ADMIN token as OPENCODEX_API_AUTH_TOKEN because `install` asked for a token, the hub then
|
|
149
|
-
* crash-looped on `assertNotAdminToken`, and `service repair` asked for the same env var
|
|
150
|
-
* again — so the only remembered way to make the command proceed was the thing that broke it.
|
|
151
|
-
*
|
|
152
|
-
* Nobody should have to export a token by hand to run a hub. {@link writeServiceApiTokenFile}
|
|
153
|
-
* provisions one, so the only conditions left that install cannot fix are an admin-token
|
|
154
|
-
* collision in the environment and a token file that exists but cannot be used.
|
|
155
|
-
*/
|
|
156
|
-
export function assertServiceAuthEnvironment(): void {
|
|
157
|
-
const config = loadConfig();
|
|
158
|
-
// Both collision checks come BEFORE the loopback short-circuit, because the launch wrapper
|
|
159
|
-
// exports the token file unconditionally (`buildServiceShellCommand` cats it whenever it
|
|
160
|
-
// exists, whatever the hostname): a management token in either source fences the whole
|
|
161
|
-
// management plane closed at boot, even on a loopback install that needs no admission
|
|
162
|
-
// secret. Returning early is what let that broken state through.
|
|
163
|
-
const present = process.env.OPENCODEX_API_AUTH_TOKEN?.trim();
|
|
164
|
-
if (present) assertNotAdminToken(present);
|
|
165
|
-
const state = readServiceApiTokenState();
|
|
166
|
-
// An existing FILE holding the admin token is the incident shape itself, and the first round
|
|
167
|
-
// only checked the env var — so install/repair reused it and the hub crash-looped at boot.
|
|
168
|
-
// On a machine connected to a hub this same file holds that hub's issued client key, which
|
|
169
|
-
// is never a management token, so the check is a no-op there.
|
|
170
|
-
if (state.kind === "present") assertNotAdminToken(state.token, process.env, "file");
|
|
171
|
-
if (isLoopbackHostname(config.hostname)) return;
|
|
172
|
-
if (present) return;
|
|
173
|
-
// Absent is fine — install/repair generates one below. `unsafe` is not: the writer refuses
|
|
174
|
-
// to replace a path it cannot vouch for, so say so here, where the operator can still act,
|
|
175
|
-
// instead of failing mid-install. Reached from `service repair` as well as `install`, so
|
|
176
|
-
// name a command that can actually succeed (see serviceRetryCommand).
|
|
177
|
-
if (state.kind !== "unsafe") return;
|
|
178
|
-
const diag = diagnoseService();
|
|
179
|
-
throw new Error(
|
|
180
|
-
`The data-plane token file cannot be used (${state.reason}): ${serviceApiTokenFilePath()}. `
|
|
181
|
-
+ `Move it aside, then rerun \`${serviceRetryCommand(diag)}\`; the service provisions a `
|
|
182
|
-
+ "fresh owner-only token and needs nothing from the environment.",
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
126
|
/** How the data-plane token the service will export was obtained. */
|
|
187
127
|
export type ServiceApiTokenOrigin = "env" | "file" | "generated";
|
|
188
128
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** Linux pidfd ownership for the opt-in sentinel. Loaded only on Linux. */
|
|
2
|
+
import { dlopen, FFIType, read } from "bun:ffi";
|
|
3
|
+
import type { TerminateResult } from "./health-sentinel-main";
|
|
4
|
+
|
|
5
|
+
const ESRCH = 3;
|
|
6
|
+
const ENOSYS = 38;
|
|
7
|
+
// Syscalls from 424 onward share one number on every Linux architecture. Binding is limited to
|
|
8
|
+
// x64/arm64 because the FFI signature below assumes their LP64 integer-register calling convention.
|
|
9
|
+
const SYS_PIDFD_OPEN = 434;
|
|
10
|
+
const SYS_PIDFD_SEND_SIGNAL = 424;
|
|
11
|
+
|
|
12
|
+
type CallResult = { value: number; errno: number };
|
|
13
|
+
export type PidfdSyscalls = {
|
|
14
|
+
open(pid: number): CallResult;
|
|
15
|
+
signal(fd: number, signal: number): CallResult;
|
|
16
|
+
close(fd: number): void;
|
|
17
|
+
dispose(): void;
|
|
18
|
+
};
|
|
19
|
+
export type ParentPidfd = { terminate(): TerminateResult; close(): void };
|
|
20
|
+
export type ParentPidfdResult =
|
|
21
|
+
| { kind: "bound"; handle: ParentPidfd }
|
|
22
|
+
| { kind: "unsupported"; reason: string }
|
|
23
|
+
| { kind: "refused"; reason: string };
|
|
24
|
+
|
|
25
|
+
function nativeSyscalls(): PidfdSyscalls {
|
|
26
|
+
const symbols = {
|
|
27
|
+
// syscall is variadic, but these syscalls use integer/pointer arguments only. All supplied
|
|
28
|
+
// slots are machine-word integers (including zero for siginfo and flags), on these LP64 ABIs.
|
|
29
|
+
syscall: { args: [FFIType.i64, FFIType.i64, FFIType.i64, FFIType.i64, FFIType.i64], returns: FFIType.i64 },
|
|
30
|
+
__errno_location: { args: [], returns: FFIType.ptr },
|
|
31
|
+
close: { args: [FFIType.i32], returns: FFIType.i32 },
|
|
32
|
+
} as const;
|
|
33
|
+
const load = (path: string) => dlopen(path, symbols);
|
|
34
|
+
let libc: ReturnType<typeof load>;
|
|
35
|
+
try {
|
|
36
|
+
libc = load("libc.so.6");
|
|
37
|
+
} catch {
|
|
38
|
+
// musl exports libc symbols through its loader; it has no glibc soname or pidfd wrappers.
|
|
39
|
+
libc = load(`/lib/ld-musl-${process.arch === "x64" ? "x86_64" : "aarch64"}.so.1`);
|
|
40
|
+
}
|
|
41
|
+
const call = (nr: number, a: number, b: number, c = 0, d = 0): CallResult => {
|
|
42
|
+
const value = Number(libc.symbols.syscall(nr, a, b, c, d));
|
|
43
|
+
return { value, errno: value === -1 ? read.i32(libc.symbols.__errno_location()!) : 0 };
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
open: pid => call(SYS_PIDFD_OPEN, pid, 0),
|
|
47
|
+
signal: (fd, signal) => call(SYS_PIDFD_SEND_SIGNAL, fd, signal),
|
|
48
|
+
// Never retry close on Linux: even an EINTR has released the descriptor.
|
|
49
|
+
close: fd => { libc.symbols.close(fd); },
|
|
50
|
+
dispose: () => libc.close(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Acquire before arming; reparenting during acquisition invalidates even a successfully opened fd. */
|
|
55
|
+
export function openParentPidfd(
|
|
56
|
+
pid: number,
|
|
57
|
+
isParent: () => boolean,
|
|
58
|
+
options: { arch?: string; load?: () => PidfdSyscalls } = {},
|
|
59
|
+
): ParentPidfdResult {
|
|
60
|
+
if (!isParent()) return { kind: "refused", reason: "the sentinel is not the watched process's child" };
|
|
61
|
+
const arch = options.arch ?? process.arch;
|
|
62
|
+
if (arch !== "x64" && arch !== "arm64") return { kind: "unsupported", reason: `pidfd syscall ABI is unsupported on ${arch}` };
|
|
63
|
+
let api: PidfdSyscalls;
|
|
64
|
+
try { api = (options.load ?? nativeSyscalls)(); }
|
|
65
|
+
catch { return { kind: "refused", reason: "Linux pidfd libc binding is unavailable" }; }
|
|
66
|
+
let fd: number | null = null;
|
|
67
|
+
let closed = false;
|
|
68
|
+
let signalled = false;
|
|
69
|
+
const close = () => {
|
|
70
|
+
if (closed) return;
|
|
71
|
+
closed = true;
|
|
72
|
+
try { if (fd !== null) api.close(fd); } finally { api.dispose(); }
|
|
73
|
+
};
|
|
74
|
+
const refuse = (reason: string): ParentPidfdResult => { close(); return { kind: "refused", reason }; };
|
|
75
|
+
try {
|
|
76
|
+
const opened = api.open(pid);
|
|
77
|
+
if (opened.value < 0) {
|
|
78
|
+
close();
|
|
79
|
+
return opened.errno === ENOSYS
|
|
80
|
+
? { kind: "unsupported", reason: "pidfd_open is unsupported by this kernel" }
|
|
81
|
+
: { kind: "refused", reason: `pidfd_open failed (errno ${opened.errno})` };
|
|
82
|
+
}
|
|
83
|
+
fd = opened.value;
|
|
84
|
+
if (!isParent()) return refuse("the watched parent exited during pidfd acquisition");
|
|
85
|
+
// Check both syscall support and permission before arming, without sending a signal.
|
|
86
|
+
const probe = api.signal(fd, 0);
|
|
87
|
+
if (probe.value < 0) {
|
|
88
|
+
close();
|
|
89
|
+
return probe.errno === ENOSYS
|
|
90
|
+
? { kind: "unsupported", reason: "pidfd_send_signal is unsupported by this kernel" }
|
|
91
|
+
: { kind: "refused", reason: `pidfd signal check failed (errno ${probe.errno})` };
|
|
92
|
+
}
|
|
93
|
+
if (!isParent()) return refuse("the watched parent exited during pidfd verification");
|
|
94
|
+
return {
|
|
95
|
+
kind: "bound",
|
|
96
|
+
handle: {
|
|
97
|
+
close,
|
|
98
|
+
terminate: () => {
|
|
99
|
+
if (closed || signalled) return "not_target";
|
|
100
|
+
// A throwing binding must settle the recorded intent as "failed", like process.kill does.
|
|
101
|
+
let sent: CallResult;
|
|
102
|
+
try { sent = api.signal(fd!, 9); } catch { return "failed"; }
|
|
103
|
+
if (sent.value < 0) return sent.errno === ESRCH ? "not_target" : "failed";
|
|
104
|
+
signalled = true;
|
|
105
|
+
return "terminated";
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
} catch {
|
|
110
|
+
return refuse("Linux pidfd binding failed");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -12,11 +12,10 @@
|
|
|
12
12
|
* - Windows reuses ids quickly, so the sentinel opens a handle on its parent at startup, accepts it
|
|
13
13
|
* only if that process was created before the sentinel, and terminates through the handle
|
|
14
14
|
* (health-sentinel-windows.ts). While the handle is open the id cannot be reused.
|
|
15
|
-
* -
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* residual is accepted and documented in structure/ops/service-health-recovery.md.
|
|
15
|
+
* - Linux holds a pidfd after rechecking the live parent relationship, and signals that fd.
|
|
16
|
+
* Only unsupported kernels/architectures retain the live-ppid fallback used by macOS.
|
|
17
|
+
* - macOS checks the live parent id immediately before SIGKILL; PID reuse between those two
|
|
18
|
+
* syscalls remains a documented residual (structure/ops/service-health-recovery.md).
|
|
20
19
|
*
|
|
21
20
|
* SIGKILL rather than SIGTERM: a proxy whose event loop is stuck cannot run its own SIGTERM
|
|
22
21
|
* handler, and systemd counts a SIGTERM death as a clean exit that `Restart=on-failure` does not
|
|
@@ -80,6 +79,8 @@ export type HealthSentinelDeps = {
|
|
|
80
79
|
isTargetGone(): boolean;
|
|
81
80
|
/** Bind to the target once at startup; a failure stands the sentinel down. */
|
|
82
81
|
bindTarget(): { ok: true } | { ok: false; reason: string };
|
|
82
|
+
/** Release the process owner on every exit, refusal, cancellation or exception. */
|
|
83
|
+
closeTarget(): void;
|
|
83
84
|
/** Terminate the bound target, re-proving it is still the target at the last moment. */
|
|
84
85
|
terminateTarget(): TerminateResult;
|
|
85
86
|
/** Run under the ownership mutation lease, released on return; "busy" when it could not be taken promptly. */
|
|
@@ -149,6 +150,11 @@ function matchesProbeTarget(runtime: SentinelRuntimeRecord | null, pid: number,
|
|
|
149
150
|
}
|
|
150
151
|
|
|
151
152
|
export async function runHealthSentinel(deps: HealthSentinelDeps): Promise<HealthSentinelResult> {
|
|
153
|
+
try { return await observeHealthSentinel(deps); }
|
|
154
|
+
finally { deps.closeTarget(); }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function observeHealthSentinel(deps: HealthSentinelDeps): Promise<HealthSentinelResult> {
|
|
152
158
|
const timing: HealthSentinelTiming = { ...DEFAULT_HEALTH_SENTINEL_TIMING, ...deps.timing };
|
|
153
159
|
const standDown = (reason: string): HealthSentinelResult => {
|
|
154
160
|
deps.log(`standing down: ${reason} (pid ${deps.targetPid})`);
|
|
@@ -407,59 +413,72 @@ export async function runHealthSentinelProcess(env: NodeJS.ProcessEnv = process.
|
|
|
407
413
|
if (opened.ok) windowsHandle = opened.handle;
|
|
408
414
|
else windowsBindFailure = opened.reason;
|
|
409
415
|
}
|
|
416
|
+
let linuxHandle: import("./health-sentinel-linux").ParentPidfd | null = null;
|
|
417
|
+
let linuxBindFailure: string | null = null;
|
|
418
|
+
if (process.platform === "linux") {
|
|
419
|
+
const { openParentPidfd } = await import("./health-sentinel-linux");
|
|
420
|
+
const opened = openParentPidfd(targetPid, () => !gone && process.ppid === targetPid);
|
|
421
|
+
if (opened.kind === "bound") linuxHandle = opened.handle;
|
|
422
|
+
else if (opened.kind === "refused") linuxBindFailure = opened.reason;
|
|
423
|
+
else appendBoundedLog(logPath, `pidfd unavailable: ${opened.reason}; using live parent PID check`);
|
|
424
|
+
}
|
|
410
425
|
const lag = startLagMonitor();
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
return "
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
426
|
+
try {
|
|
427
|
+
await runHealthSentinel({
|
|
428
|
+
targetPid,
|
|
429
|
+
now: Date.now,
|
|
430
|
+
sleep: ms => new Promise(resolve => setTimeout(resolve, ms)),
|
|
431
|
+
probe: async (target, timeoutMs) => {
|
|
432
|
+
lag.reset();
|
|
433
|
+
const outcome = await probeHealth(target, timeoutMs);
|
|
434
|
+
return outcome.kind === "timeout" ? { ...outcome, sentinelLagMs: lag.read() } : outcome;
|
|
435
|
+
},
|
|
436
|
+
readRuntime: () => readSentinelRuntime(runtimePath),
|
|
437
|
+
targetExited,
|
|
438
|
+
closeTarget: () => { linuxHandle?.close(); windowsHandle?.close(); },
|
|
439
|
+
isTargetGone: () => gone || (windowsHandle?.exited() ?? false),
|
|
440
|
+
bindTarget: () => {
|
|
441
|
+
// POSIX: Bun reads the parent id live, and it stops naming the proxy once the proxy exits.
|
|
442
|
+
// Windows: it is the creating parent's id, so the handle below carries the identity.
|
|
443
|
+
if (process.ppid !== targetPid) return { ok: false, reason: "the sentinel is not the watched process's child" };
|
|
444
|
+
if (linuxBindFailure) return { ok: false, reason: linuxBindFailure };
|
|
445
|
+
if (process.platform !== "win32") return { ok: true };
|
|
446
|
+
return windowsHandle ? { ok: true } : { ok: false, reason: windowsBindFailure ?? "the parent process cannot be opened" };
|
|
447
|
+
},
|
|
448
|
+
terminateTarget: () => {
|
|
449
|
+
if (process.platform === "win32") {
|
|
450
|
+
if (!windowsHandle) return "failed";
|
|
451
|
+
return windowsHandle.terminate(1) ? "terminated" : "failed";
|
|
452
|
+
}
|
|
453
|
+
if (gone || process.ppid !== targetPid) return "not_target";
|
|
454
|
+
if (linuxHandle) return linuxHandle.terminate();
|
|
455
|
+
try {
|
|
456
|
+
process.kill(targetPid, "SIGKILL");
|
|
457
|
+
return "terminated";
|
|
458
|
+
} catch {
|
|
459
|
+
return "failed";
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
withOwnershipLease: run => {
|
|
463
|
+
let lease: { release(): void };
|
|
464
|
+
try {
|
|
465
|
+
lease = acquireOwnershipMutationLease(statePaths, { waitMs: LEASE_WAIT_MS, processImage: () => null });
|
|
466
|
+
} catch {
|
|
467
|
+
return "busy";
|
|
468
|
+
}
|
|
469
|
+
try { return run(); } finally { lease.release(); }
|
|
470
|
+
},
|
|
471
|
+
readHistory: () => {
|
|
472
|
+
const entries = readRecoveryEntries(historyPath);
|
|
473
|
+
return entries === null ? null : countedRecoveryTimes(entries);
|
|
474
|
+
},
|
|
475
|
+
writeHistory: entry => writeRecoveryEntry(historyPath, entry),
|
|
476
|
+
log: line => appendBoundedLog(logPath, linuxHandle && line.startsWith("watching pid ") ? `${line} (pidfd)` : line),
|
|
477
|
+
timing: parseTimingOverride(env[HEALTH_SENTINEL_ENV.timing]),
|
|
478
|
+
});
|
|
479
|
+
} finally {
|
|
480
|
+
lag.stop();
|
|
481
|
+
}
|
|
463
482
|
return 0;
|
|
464
483
|
}
|
|
465
484
|
|
package/src/service/health.ts
CHANGED
|
@@ -1,77 +1,18 @@
|
|
|
1
|
-
import { win32 } from "node:path";
|
|
2
1
|
import { loadConfig } from "../config";
|
|
3
|
-
import {
|
|
4
|
-
import { PROXY_ENV_KEYS } from "../lib/proxy-env";
|
|
2
|
+
import { resolveServiceListenPort } from "./definition";
|
|
5
3
|
import { proxyIdentityAt } from "../server/proxy-liveness";
|
|
6
4
|
import { launchdListenPort } from "./launchd";
|
|
7
5
|
import { serviceLogPath } from "./state";
|
|
8
6
|
import { systemdListenPort } from "./systemd";
|
|
9
7
|
import { windowsListenPort, winswListenPort } from "./windows-ops";
|
|
10
8
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Listen port baked into service wrappers / WinSW XML.
|
|
17
|
-
* Priority: explicit override → OCX_BAKE_PORT (update restart) → config.port → 10100.
|
|
18
|
-
* `config.port === 0` means ephemeral for interactive start; services need a stable pin,
|
|
19
|
-
* so treat 0 / invalid like unset (default 10100) instead of baking `--port 0`.
|
|
20
|
-
*/
|
|
21
|
-
export function resolveServiceListenPort(override?: number): number {
|
|
22
|
-
if (typeof override === "number" && Number.isFinite(override) && override > 0 && override <= 65535) {
|
|
23
|
-
return Math.trunc(override);
|
|
24
|
-
}
|
|
25
|
-
const baked = process.env.OCX_BAKE_PORT?.trim();
|
|
26
|
-
if (baked && /^\d+$/.test(baked)) {
|
|
27
|
-
const n = Number(baked);
|
|
28
|
-
if (n > 0 && n <= 65535) return n;
|
|
29
|
-
}
|
|
30
|
-
const configured = loadConfig().port;
|
|
31
|
-
if (typeof configured === "number" && configured > 0 && configured <= 65535) return configured;
|
|
32
|
-
return 10100;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function buildServiceShellCommand(bun: string, cli: string | null, port = resolveServiceListenPort()): string {
|
|
36
|
-
const tokenFile = serviceApiTokenFilePath();
|
|
37
|
-
const args = cli ? `${shellQuote(cli)} start` : "start";
|
|
38
|
-
return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(bun)} ${args} --port ${port}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The same command shape, launched through a stable `ocx` executable instead of an
|
|
43
|
-
* explicit Bun + CLI pair. The token-file preamble is identical and deliberately shared
|
|
44
|
-
* in form: the service still reads the token from disk at start and never carries it in
|
|
45
|
-
* the unit.
|
|
46
|
-
*/
|
|
47
|
-
export function buildServiceLauncherShellCommand(launcher: string, port = resolveServiceListenPort()): string {
|
|
48
|
-
const tokenFile = serviceApiTokenFilePath();
|
|
49
|
-
return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(launcher)} start --port ${port}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Shared tail parser for the baked `--port <n>`.
|
|
54
|
-
*
|
|
55
|
-
* Terminators cover all three artifact shapes: whitespace (batch wrapper, systemd
|
|
56
|
-
* unit), `"` (systemd's quoted ExecStart), `<` (WinSW's `</arguments>`), and `&` (an
|
|
57
|
-
* XML-escaped quote). Matched LAST because every artifact carries the Bun and CLI
|
|
58
|
-
* paths ahead of the argument, and a path containing the literal must not shadow it.
|
|
59
|
-
*/
|
|
60
|
-
export function parseBakedListenPort(read: () => string): number | null {
|
|
61
|
-
try {
|
|
62
|
-
const last = [...read().matchAll(/start --port (\d{1,5})(?:\s|"|&|<|$)/gm)].at(-1);
|
|
63
|
-
if (!last) return null;
|
|
64
|
-
const n = Number(last[1]);
|
|
65
|
-
return n > 0 && n <= 65535 ? n : null;
|
|
66
|
-
} catch {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
9
|
+
export { resolveServiceListenPort };
|
|
10
|
+
export { shellQuote, buildServiceShellCommand, buildServiceLauncherShellCommand, parseBakedListenPort, resolvedProxyEnv } from "./definition";
|
|
70
11
|
|
|
71
12
|
/**
|
|
72
13
|
* The listen port of the INSTALLED service artifact, falling back to the configured
|
|
73
|
-
* one.
|
|
74
|
-
*
|
|
14
|
+
* one. This historical artifact chain can read retained files from another platform;
|
|
15
|
+
* platform-specific launchd command checks instead use only the plist and configured port.
|
|
75
16
|
*/
|
|
76
17
|
export function installedServiceListenPort(): number {
|
|
77
18
|
return launchdListenPort()
|
|
@@ -239,28 +180,3 @@ export async function reportServiceServing(
|
|
|
239
180
|
export function serviceRepairCommand(): string {
|
|
240
181
|
return "ocx service repair";
|
|
241
182
|
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Outbound proxy settings the installing shell had, resolved for baking into a service
|
|
245
|
-
* definition.
|
|
246
|
-
*
|
|
247
|
-
* A service manager does not inherit the environment of the shell that installed it, and
|
|
248
|
-
* `ExecStart=/bin/sh -lc` is dash on Ubuntu/WSL — login dash reads `.profile`, not
|
|
249
|
-
* `.bashrc`, which is where proxy exports usually live. So a user who needs a proxy to
|
|
250
|
-
* reach the upstream got a service that dialed direct: the socket was reset, the retry
|
|
251
|
-
* budget drained, and the request surfaced as `502 Provider unreachable` (#2107). The
|
|
252
|
-
* same install driven through `ocx codex-shim` worked, because that path spawns with
|
|
253
|
-
* `{ ...process.env }`.
|
|
254
|
-
*
|
|
255
|
-
* Lower-case variants are honored because curl-style tooling sets them and the runtime's
|
|
256
|
-
* own `applyProxyEnv` already treats both cases as equivalent. Only the canonical
|
|
257
|
-
* upper-case name is baked, so a definition never carries two spellings of one setting.
|
|
258
|
-
*/
|
|
259
|
-
export function resolvedProxyEnv(env: NodeJS.ProcessEnv = process.env): { name: string; value: string }[] {
|
|
260
|
-
const resolved: { name: string; value: string }[] = [];
|
|
261
|
-
for (const key of PROXY_ENV_KEYS) {
|
|
262
|
-
const value = env[key]?.trim() || env[key.toLowerCase()]?.trim();
|
|
263
|
-
if (value) resolved.push({ name: key, value });
|
|
264
|
-
}
|
|
265
|
-
return resolved;
|
|
266
|
-
}
|