@bitkyc08/opencodex 2.91.0 → 2.93.0-preview.20261027
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/bin/ocx.mjs +47 -8
- package/gui/dist/assets/App-CJZoq-uF.js +58 -0
- package/gui/dist/assets/{Tray-BuLy7hL1.js → Tray-BWQPcgZn.js} +1 -1
- package/gui/dist/assets/index-DRe8Hhfz.css +1 -0
- package/gui/dist/assets/index-Xfct8owH.js +94 -0
- package/gui/dist/assets/quota-summary-DxToX1rV.js +1 -0
- package/gui/dist/index.html +2 -2
- package/gui/dist/provider-icons/workbuddy-color.svg +29 -0
- package/package.json +8 -7
- package/src/adapters/anthropic.ts +28 -5
- package/src/adapters/coding-agent/batch-dispatcher.ts +122 -0
- package/src/adapters/coding-agent/profile.ts +54 -16
- package/src/adapters/coding-agent/turn.ts +30 -2
- package/src/adapters/command-code.ts +114 -7
- package/src/adapters/cursor/native-exec-fs.ts +11 -9
- package/src/adapters/devin/live-models.ts +4 -0
- package/src/adapters/google-vertex-endpoint.ts +9 -1
- package/src/adapters/google-vertex-key-project.ts +223 -0
- package/src/adapters/google-wire-compiler.ts +1 -2
- package/src/adapters/google.ts +13 -3
- package/src/adapters/kiro/reasoning.ts +2 -0
- package/src/adapters/kiro-refusal.ts +6 -1
- package/src/adapters/kiro-wire.ts +1 -1
- package/src/adapters/openai-chat/messages.ts +8 -2
- package/src/adapters/openai-chat/passthrough.ts +35 -14
- package/src/adapters/openai-chat/reasoning-replay.ts +27 -16
- package/src/adapters/openai-chat/reasoning-wire.ts +117 -8
- package/src/adapters/openai-chat/required-tool-result.ts +64 -0
- package/src/adapters/openai-chat/tool-name-registry.ts +3 -1
- package/src/adapters/openai-chat.ts +9 -10
- package/src/adapters/openai-responses/passthrough.ts +7 -4
- package/src/adapters/openai-responses/reasoning.ts +13 -0
- package/src/adapters/openai-responses/request-strips.ts +60 -0
- package/src/adapters/qoder/adapter.ts +129 -8
- package/src/adapters/qoder/scaffold-guard.ts +5 -0
- package/src/adapters/reasoning-controls.ts +16 -0
- package/src/adapters/registry.ts +5 -0
- package/src/adapters/tool-call-id.ts +1 -3
- package/src/adapters/workbuddy-schema.ts +61 -0
- package/src/adapters/workbuddy.ts +68 -0
- package/src/bridge/errors.ts +27 -0
- package/src/bridge/response-json.ts +1 -1
- package/src/bridge/sse.ts +1 -1
- package/src/bridge.ts +1 -1
- package/src/chat/inbound.ts +4 -1
- package/src/chat/outbound.ts +20 -6
- package/src/chat/reasoning-details.ts +28 -5
- package/src/claude/desktop-first-party.ts +54 -4
- package/src/claude/desktop-picker.ts +11 -8
- package/src/claude/first-party-settings.ts +3 -1
- package/src/claude/inbound.ts +2 -1
- package/src/claude/intercept/local-ca.ts +2 -1
- package/src/claude/intercept/picker-ca-cleanup.ts +4 -2
- package/src/claude/intercept/picker-ca-startup.ts +3 -1
- package/src/claude/intercept/picker-models.ts +6 -3
- package/src/claude/intercept/picker-runtime.ts +3 -2
- package/src/claude/intercept/runtime.ts +6 -3
- package/src/claude/model-info.ts +31 -8
- package/src/cli/account-api.ts +11 -0
- package/src/cli/capabilities-agents-routing.ts +3 -3
- package/src/cli/capabilities-base.ts +7 -1
- package/src/cli/capabilities-observe-system.ts +10 -0
- package/src/cli/claude-desktop.ts +9 -8
- package/src/cli/claude.ts +3 -2
- package/src/cli/codex-cli-update.ts +8 -0
- package/src/cli/codex-shim-autorestore.ts +9 -0
- package/src/cli/combo-result.ts +13 -0
- package/src/cli/combo-stats.ts +3 -0
- package/src/cli/combo.ts +83 -41
- package/src/cli/cross-home-owner.ts +13 -1
- package/src/cli/dispatch.ts +25 -6
- package/src/cli/ensure-desired-integrations.ts +8 -6
- package/src/cli/export-command.ts +4 -1
- package/src/cli/help.ts +2 -1
- package/src/cli/index.ts +45 -45
- package/src/cli/integration-plan-dto.ts +4 -0
- package/src/cli/launcher-context.ts +49 -2
- package/src/cli/link.ts +45 -32
- package/src/cli/opencode.ts +20 -9
- package/src/cli/provider.ts +2 -1
- package/src/cli/registry.ts +6 -4
- package/src/cli/root.ts +9 -0
- package/src/cli/status.ts +59 -1
- package/src/cli/stop-restore.ts +113 -0
- package/src/cli/sync-expectation.ts +60 -0
- package/src/cli/uninstall-integrations.ts +41 -7
- package/src/cli/usage-compare.ts +68 -12
- package/src/client/connect.ts +40 -3
- package/src/client/link-home.ts +10 -1
- package/src/client/link-ingress.ts +1 -1
- package/src/client/link-readiness.ts +97 -0
- package/src/client/link-relay-transport.ts +79 -11
- package/src/client/link-relay.ts +74 -15
- package/src/client/link-tunnel.ts +45 -18
- package/src/client/machine-api.ts +3 -1
- package/src/client/machine-listener.ts +20 -2
- package/src/client/runtime.ts +20 -2
- package/src/client/sync-expectation.ts +118 -0
- package/src/clients/config-export/agent-clients.ts +96 -0
- package/src/clients/config-export/contracts.ts +25 -7
- package/src/clients/config-export/qoder.ts +38 -23
- package/src/clients/config-export/reasoning-metadata.ts +55 -8
- package/src/clients/config-export.ts +12 -0
- package/src/clients/lazycodex.ts +7 -2
- package/src/clients/reasoning-controls.ts +49 -0
- package/src/codex/account-store.ts +17 -3
- package/src/codex/app-server-restart-service.ts +6 -5
- package/src/codex/auth-api/account-identity.ts +14 -0
- package/src/codex/auth-api/account-list.ts +29 -2
- package/src/codex/auth-api/http.ts +13 -0
- package/src/codex/auth-api/main-account-probe.ts +2 -2
- package/src/codex/auth-api/pool-mode-gate.ts +16 -3
- package/src/codex/auth-api/pool-quota-probe.ts +15 -0
- package/src/codex/auth-context.ts +15 -12
- package/src/codex/catalog/build-entries.ts +22 -0
- package/src/codex/catalog/configured-tiers.ts +70 -0
- package/src/codex/catalog/effort.ts +52 -11
- package/src/codex/catalog/gather-capture.ts +5 -2
- package/src/codex/catalog/heal-observation.ts +155 -13
- package/src/codex/catalog/metadata.ts +6 -0
- package/src/codex/catalog/model-hints.ts +60 -11
- package/src/codex/catalog/model-visibility.ts +3 -2
- package/src/codex/catalog/native-models.ts +18 -3
- package/src/codex/catalog/parsing.ts +10 -8
- package/src/codex/catalog/pinned-models.ts +2 -1
- package/src/codex/catalog/provider-models.ts +18 -13
- package/src/codex/catalog/retained-sync.ts +2 -1
- package/src/codex/catalog/routed-gather.ts +19 -20
- package/src/codex/catalog/write-audit.ts +3 -2
- package/src/codex/catalog-self-heal.ts +43 -23
- package/src/codex/chatgpt-refresh-failure.ts +6 -2
- package/src/codex/cli-install-provenance.ts +418 -0
- package/src/codex/cli-installation-targets.ts +5 -1
- package/src/codex/cli-update-plan.ts +43 -16
- package/src/codex/collision-identity.ts +30 -5
- package/src/codex/convergence.ts +4 -3
- package/src/codex/data/roster-pinned-models.json +177 -1
- package/src/codex/desired-state.ts +6 -1
- package/src/codex/desktop-app/picker-freshness.ts +10 -8
- package/src/codex/desktop-compatibility/installed-build.ts +88 -12
- package/src/codex/desktop-compatibility/runtime.ts +29 -5
- package/src/codex/desktop-compatibility/usage-activation.ts +2 -1
- package/src/codex/desktop-compatibility/usage-controller.ts +9 -1
- package/src/codex/desktop-compatibility/windows-certificate-trust.ts +46 -12
- package/src/codex/inject/config-toml.ts +0 -18
- package/src/codex/inject/plan.ts +18 -3
- package/src/codex/inject/remove.ts +6 -2
- package/src/codex/inject.ts +1 -1
- package/src/codex/journal.ts +61 -1
- package/src/codex/model-entitlements.ts +1 -1
- package/src/codex/native-main-owner.ts +27 -1
- package/src/codex/native-profile-store.ts +2 -1
- package/src/codex/pool-credit-policy.ts +35 -1
- package/src/codex/pool-refresh-backoff.ts +3 -1
- package/src/codex/quota-types.ts +3 -0
- package/src/codex/quota.ts +24 -4
- package/src/codex/role-auto-assign.ts +6 -2
- package/src/codex/subagent-defaults.ts +49 -4
- package/src/codex/user-identity.ts +35 -6
- package/src/codex/windows-current-sid.ts +162 -0
- package/src/combos/failover.ts +17 -2
- package/src/combos/jev-decision-contract.ts +22 -1
- package/src/combos/jev-quota-activation.ts +5 -1
- package/src/combos/jev-quota-route.ts +1 -1
- package/src/combos/jev-quota.ts +9 -1
- package/src/combos/jev-service-exchange.ts +3 -1
- package/src/combos/jev-session-pin-config.ts +51 -0
- package/src/combos/jev.ts +101 -9
- package/src/combos/resolve.ts +45 -1
- package/src/combos/types.ts +43 -0
- package/src/config/load-degrade.ts +5 -0
- package/src/config/paths.ts +19 -3
- package/src/config/pending-teardown.ts +22 -1
- package/src/config/provider-validation.ts +22 -4
- package/src/config/removed-opencode-free.ts +11 -0
- package/src/config/schema/leaf-validators.ts +6 -0
- package/src/generated/compatibility-version.json +467 -311
- package/src/generated/model-metadata.ts +5 -5
- package/src/images/loop.ts +16 -2
- package/src/integrations/catalog-refresh.ts +1 -1
- package/src/integrations/hermes-profiles.ts +251 -0
- package/src/integrations/merge.ts +8 -0
- package/src/integrations/mutation-plan.ts +19 -4
- package/src/integrations/registry.ts +5 -0
- package/src/integrations/state.ts +36 -1
- package/src/integrations/writer.ts +18 -4
- package/src/lib/bun-path-runtime.mjs +2 -1
- package/src/lib/decision-quota-sink.ts +10 -0
- package/src/lib/errors.ts +62 -0
- package/src/lib/eventstream-decoder.ts +81 -37
- package/src/lib/opaque-proxy-tunnel.ts +32 -8
- package/src/lib/pinned-http.ts +35 -2
- package/src/lib/process-control.ts +13 -1
- package/src/lib/socks5-fetch.ts +59 -64
- package/src/lib/socks5-upload-waiter.ts +50 -0
- package/src/lib/sse-decoder.ts +7 -2
- package/src/lib/state-store-sweeper.ts +40 -14
- package/src/lib/translator-budget.ts +4 -4
- package/src/lib/upstream-retry.ts +4 -1
- package/src/lib/windows-native-principal-worker.ts +14 -0
- package/src/lib/windows-native-principal.ts +188 -0
- package/src/lib/windows-secret-acl.ts +39 -3
- package/src/lib/windows-user-principal.ts +62 -18
- package/src/link/diagnostics.ts +193 -0
- package/src/link/relay-auth.ts +6 -0
- package/src/link/ssh-config.ts +35 -3
- package/src/link/status-projection.ts +39 -0
- package/src/link/supervisor.ts +45 -12
- package/src/link/tunnel-history.ts +109 -0
- package/src/link/tunnel-state.ts +80 -17
- package/src/messaging/remote-files.ts +24 -1
- package/src/oauth/generic-account-failover.ts +91 -7
- package/src/oauth/index.ts +15 -0
- package/src/oauth/model-discovery-auth.ts +5 -0
- package/src/oauth/store.ts +4 -1
- package/src/oauth/token-guardian.ts +7 -0
- package/src/oauth/types.ts +9 -0
- package/src/oauth/workbuddy.ts +139 -0
- package/src/providers/account-quota-disk.ts +12 -4
- package/src/providers/antigravity-quota-identity.ts +37 -0
- package/src/providers/base-url-choices.ts +8 -5
- package/src/providers/codebuddy-models.ts +6 -0
- package/src/providers/codex-capacity.ts +3 -1
- package/src/providers/derive.ts +51 -3
- package/src/providers/kiro-models.ts +3 -0
- package/src/providers/mimo-thinking.ts +12 -0
- package/src/providers/model-discovery.ts +2 -0
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/model-rename-migration.ts +10 -0
- package/src/providers/openai-sidecar-credit.ts +2 -2
- package/src/providers/quota/account-cache.ts +3 -2
- package/src/providers/quota/antigravity.ts +25 -14
- package/src/providers/quota/report-cache.ts +13 -4
- package/src/providers/quota/vendor-probes-key.ts +60 -0
- package/src/providers/quota/workbuddy.ts +83 -0
- package/src/providers/quota-decision-antigravity.ts +64 -0
- package/src/providers/quota-decision-publication.ts +11 -0
- package/src/providers/quota-decision-snapshot.ts +21 -4
- package/src/providers/quota-types.ts +2 -0
- package/src/providers/quota.ts +3 -1
- package/src/providers/reasoning-capabilities.ts +76 -0
- package/src/providers/registry/entries-core.ts +23 -12
- package/src/providers/registry/entries-extended.ts +83 -27
- package/src/providers/registry/model-ids.ts +5 -0
- package/src/providers/registry/model-seeds.ts +120 -56
- package/src/providers/registry/types.ts +29 -2
- package/src/providers/resolved-model-policy.ts +30 -10
- package/src/providers/routed-config.ts +24 -1
- package/src/providers/stale-model-roster-migration.ts +123 -18
- package/src/providers/workbuddy-credit-balance.ts +81 -0
- package/src/providers/workbuddy-models.ts +37 -0
- package/src/providers/workbuddy.ts +60 -0
- package/src/remote-control/workspace-pi-runtime.ts +11 -6
- package/src/responses/compaction-summary.ts +45 -0
- package/src/responses/muse-tool-name-alias.ts +1 -2
- package/src/responses/parser.ts +3 -2
- package/src/responses/reasoning-replay-cache.ts +18 -14
- package/src/responses/schema.ts +1 -0
- package/src/responses/thought-signature-replay.ts +10 -2
- package/src/router.ts +2 -0
- package/src/routing/analytics.ts +2 -3
- package/src/routing/capability.ts +8 -2
- package/src/routing/fallback-policy.ts +3 -3
- package/src/routing/history/ingest.ts +28 -3
- package/src/server/auth-cors.ts +4 -2
- package/src/server/chat-completions.ts +21 -1
- package/src/server/chat-native-eligibility.ts +7 -0
- package/src/server/chat-native-reasoning.ts +24 -7
- package/src/server/chat-native.ts +13 -2
- package/src/server/claude-messages.ts +3 -0
- package/src/server/direct-local-http.ts +5 -2
- package/src/server/effort-policy.ts +5 -1
- package/src/server/index/link-relay-sessions.ts +9 -5
- package/src/server/index/live-sideband.ts +18 -15
- package/src/server/index/optional-listeners.ts +4 -0
- package/src/server/index/serve-options.ts +5 -4
- package/src/server/local-management-read-client.ts +3 -1
- package/src/server/management/agent-settings-routes.ts +27 -15
- package/src/server/management/combo-routes.ts +14 -4
- package/src/server/management/config-routes.ts +2 -2
- package/src/server/management/decision-discovery.ts +12 -2
- package/src/server/management/hermes-profile-routes.ts +156 -0
- package/src/server/management/integration-routes.ts +85 -24
- package/src/server/management/link-routes.ts +5 -2
- package/src/server/management/model-routes.ts +2 -2
- package/src/server/management/model-row-export-metadata.ts +25 -6
- package/src/server/management/model-rows.ts +15 -20
- package/src/server/management/native-integration-routes.ts +11 -8
- package/src/server/management/provider-routes.ts +1 -1
- package/src/server/management/route-registry.ts +4 -0
- package/src/server/management-api.ts +5 -0
- package/src/server/models-capabilities.ts +21 -3
- package/src/server/port-reclaim.ts +51 -9
- package/src/server/request-log-conversation.ts +49 -0
- package/src/server/request-log.ts +2 -0
- package/src/server/responses/adapter-delivery.ts +11 -1
- package/src/server/responses/adapter-dispatch.ts +2 -2
- package/src/server/responses/anthropic-thinking-recovery.ts +1 -0
- package/src/server/responses/codex-ws-exchange.ts +39 -9
- package/src/server/responses/codex-ws-session.ts +38 -4
- package/src/server/responses/codex-ws-wire.ts +6 -1
- package/src/server/responses/combo-required-tool-contract.ts +103 -0
- package/src/server/responses/combo-stream-preflight.ts +8 -1
- package/src/server/responses/compact.ts +14 -5
- package/src/server/responses/context-overflow.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +10 -0
- package/src/server/responses/core-combo.ts +74 -44
- package/src/server/responses/core-lifetime.ts +5 -2
- package/src/server/responses/core-normalize.ts +17 -4
- package/src/server/responses/core-opaque-recovery.ts +1 -0
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-replay.ts +2 -1
- package/src/server/responses/jev-combo-choice.ts +39 -0
- package/src/server/responses/jev-session-pin.ts +203 -0
- package/src/server/responses/main-hard-lock-routing.ts +60 -44
- package/src/server/responses/passthrough-delivery.ts +44 -2
- package/src/server/responses/policy-fallback.ts +6 -1
- package/src/server/responses/request-compression.ts +12 -7
- package/src/server/responses/request-prepare.ts +32 -7
- package/src/server/responses/request-sidecar-auth.ts +2 -0
- package/src/server/responses/request-transport.ts +25 -6
- package/src/server/responses/response-effects.ts +22 -10
- package/src/server/responses/run-turn-execution.ts +2 -4
- package/src/server/responses/sidecar-execution.ts +13 -7
- package/src/server/responses-phase-inference.ts +267 -0
- package/src/server/sse-payload-rewrite.ts +14 -1
- package/src/server/stop-teardown.ts +36 -1
- package/src/server/ws-bridge.ts +1 -1
- package/src/service/definition.ts +20 -8
- package/src/service/desktop-supervision.d.mts +7 -4
- package/src/service/desktop-supervision.mjs +90 -13
- package/src/service/ownership-mutation-lease.mjs +21 -7
- package/src/types/config.ts +14 -2
- package/src/types/provider.ts +29 -7
- package/src/types/request.ts +11 -0
- package/src/types/wire.ts +1 -0
- package/src/types.ts +1 -0
- package/src/update/bind-probe.ts +29 -0
- package/src/update/codex-cli-update-launch-policy.d.mts +9 -0
- package/src/update/codex-cli-update-launch-policy.mjs +30 -0
- package/src/update/job.ts +10 -29
- package/src/update/npm-install-job.ps1 +173 -0
- package/src/update/npm-install.mjs +70 -0
- package/src/update/stop-contract.d.mts +1 -0
- package/src/update/stop-contract.mjs +14 -0
- package/src/update/stop-decision.d.mts +1 -1
- package/src/update/stop-decision.mjs +13 -3
- package/src/update/teardown-handover.ts +166 -0
- package/src/update/transactional-install.d.mts +14 -3
- package/src/update/transactional-install.mjs +33 -7
- package/src/usage/expected-prices.ts +64 -31
- package/src/usage/jev-stats.ts +54 -14
- package/src/usage/timeline.ts +19 -9
- package/src/vision/eligibility.ts +15 -5
- package/src/vision/plan.ts +2 -2
- package/src/web-search/loop.ts +16 -2
- package/gui/dist/assets/App-nZ34cR5G.js +0 -58
- package/gui/dist/assets/index-DGXS51sp.js +0 -94
- package/gui/dist/assets/index-DPeB9HpV.css +0 -1
- package/gui/dist/assets/quota-summary-DO1Sx5TM.js +0 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { chmod, lstat, mkdir, open, rename, unlink } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { assertNotRealHomeUnderTest } from "../lib/test-home-guard";
|
|
5
|
+
import { hardenSecretDirAsync, hardenSecretPathAsync, reattributeHardenedSecretPath, windowsSecretAclApplies } from "../lib/windows-secret-acl";
|
|
6
|
+
import { linkDir } from "./paths";
|
|
7
|
+
import type { TunnelState } from "./tunnel-state";
|
|
8
|
+
|
|
9
|
+
export type LinkDiagnosticRecord = {
|
|
10
|
+
event: "owner_lookup" | "relay_retry" | "relay_failure" | "relay_recovered" | "tunnel_transition";
|
|
11
|
+
outcome: "match" | "mismatch" | "lookup_error" | "deadline" | "stale" | "unavailable"
|
|
12
|
+
| "identity_unknown" | "identity_mismatch" | "auth_timeout" | "auth_failed"
|
|
13
|
+
| "transport_error" | "client_abort" | "header_timeout" | "recovered"
|
|
14
|
+
// tunnel_transition: the Home's -R tunnel changed state, and why (src/link/tunnel-history.ts).
|
|
15
|
+
| "connected" | "spawned" | "stopped" | "auth" | "hostkey" | "forward" | "remote_forward_conflict"
|
|
16
|
+
| "timeout" | "network" | "unknown" | "spawn_failed" | "listener_unavailable";
|
|
17
|
+
requestId?: string;
|
|
18
|
+
checkId?: string;
|
|
19
|
+
generation?: number;
|
|
20
|
+
state?: string;
|
|
21
|
+
previousState?: string;
|
|
22
|
+
elapsedMs?: number;
|
|
23
|
+
attempt?: number;
|
|
24
|
+
tunnelPort?: number;
|
|
25
|
+
retryAfterMs?: number;
|
|
26
|
+
};
|
|
27
|
+
export type LinkDiagnosticSink = (record: LinkDiagnosticRecord) => void;
|
|
28
|
+
|
|
29
|
+
const EVENTS = new Set(["owner_lookup", "relay_retry", "relay_failure", "relay_recovered", "tunnel_transition"]);
|
|
30
|
+
const OUTCOMES = new Set([
|
|
31
|
+
"match", "mismatch", "lookup_error", "deadline", "stale", "unavailable", "identity_unknown",
|
|
32
|
+
"identity_mismatch", "auth_timeout", "auth_failed", "transport_error", "client_abort", "header_timeout", "recovered",
|
|
33
|
+
"connected", "spawned", "stopped", "auth", "hostkey", "forward", "remote_forward_conflict", "timeout", "network",
|
|
34
|
+
"unknown", "spawn_failed", "listener_unavailable",
|
|
35
|
+
]);
|
|
36
|
+
const STATES = new Set<TunnelState["kind"]>(["idle", "connecting", "connected", "reconnecting", "failed"]);
|
|
37
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
38
|
+
const MAX_BYTES = 256 * 1024;
|
|
39
|
+
|
|
40
|
+
/** Copy only bounded enum, UUID and numeric fields; unknown fields never reach disk. */
|
|
41
|
+
function metadata(record: LinkDiagnosticRecord): LinkDiagnosticRecord | null {
|
|
42
|
+
if (!record) return null;
|
|
43
|
+
const { event, outcome, state, previousState } = record;
|
|
44
|
+
if (!EVENTS.has(event) || !OUTCOMES.has(outcome)) return null;
|
|
45
|
+
const result: LinkDiagnosticRecord = { event, outcome };
|
|
46
|
+
for (const key of ["requestId", "checkId"] as const) {
|
|
47
|
+
const value = record[key];
|
|
48
|
+
if (typeof value === "string" && value.length === 36 && UUID.test(value)) result[key] = value;
|
|
49
|
+
}
|
|
50
|
+
if (typeof state === "string" && STATES.has(state as TunnelState["kind"])) result.state = state;
|
|
51
|
+
if (typeof previousState === "string" && STATES.has(previousState as TunnelState["kind"])) result.previousState = previousState;
|
|
52
|
+
for (const key of ["generation", "elapsedMs", "attempt", "retryAfterMs"] as const) {
|
|
53
|
+
const value = record[key];
|
|
54
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= Number.MAX_SAFE_INTEGER
|
|
55
|
+
&& (key === "elapsedMs" || key === "retryAfterMs" || Number.isSafeInteger(value))) result[key] = value;
|
|
56
|
+
}
|
|
57
|
+
const { tunnelPort } = record;
|
|
58
|
+
if (typeof tunnelPort === "number" && Number.isInteger(tunnelPort) && tunnelPort >= 1 && tunnelPort <= 65_535) result.tunnelPort = tunnelPort;
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Append through a private no-follow handle, keeping at most one 256 KiB rotated backup. */
|
|
63
|
+
async function append(dir: string, line: string): Promise<void> {
|
|
64
|
+
assertNotRealHomeUnderTest(dirname(dir));
|
|
65
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
66
|
+
const directory = await lstat(dir);
|
|
67
|
+
if (!directory.isDirectory() || directory.isSymbolicLink()) return;
|
|
68
|
+
const windows = windowsSecretAclApplies();
|
|
69
|
+
if (windows) await hardenSecretDirAsync(dir, { required: true });
|
|
70
|
+
else await chmod(dir, 0o700);
|
|
71
|
+
const path = join(dir, "diagnostics.jsonl");
|
|
72
|
+
const backup = `${path}.1`;
|
|
73
|
+
let file: Awaited<ReturnType<typeof open>> | undefined;
|
|
74
|
+
try {
|
|
75
|
+
// No read of log contents, and no following a file symlink or writing to a pipe.
|
|
76
|
+
const flags = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
77
|
+
file = await open(path, flags, 0o600);
|
|
78
|
+
const current = await file.stat();
|
|
79
|
+
if (!current.isFile() || current.nlink !== 1) return;
|
|
80
|
+
if (windows) await hardenSecretPathAsync(path, { required: true });
|
|
81
|
+
else await file.chmod(0o600);
|
|
82
|
+
const previous = await lstat(backup).catch(error => {
|
|
83
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
84
|
+
return null;
|
|
85
|
+
});
|
|
86
|
+
if (previous) {
|
|
87
|
+
if (!previous.isFile() || previous.isSymbolicLink() || previous.nlink !== 1) return;
|
|
88
|
+
if (previous.size > MAX_BYTES) await unlink(backup);
|
|
89
|
+
else if (windows) await hardenSecretPathAsync(backup, { required: true });
|
|
90
|
+
else await chmod(backup, 0o600);
|
|
91
|
+
}
|
|
92
|
+
if (current.size > MAX_BYTES) await file.truncate(0);
|
|
93
|
+
else if (current.size + Buffer.byteLength(line) > MAX_BYTES) {
|
|
94
|
+
await file.close();
|
|
95
|
+
file = undefined;
|
|
96
|
+
await rename(path, backup);
|
|
97
|
+
file = await open(path, flags, 0o600);
|
|
98
|
+
const fresh = await file.stat();
|
|
99
|
+
if (!fresh.isFile() || fresh.nlink !== 1 || fresh.size + Buffer.byteLength(line) > MAX_BYTES) return;
|
|
100
|
+
if (windows) await hardenSecretPathAsync(path, { required: true });
|
|
101
|
+
else await file.chmod(0o600);
|
|
102
|
+
}
|
|
103
|
+
await file.write(line);
|
|
104
|
+
if (windows) {
|
|
105
|
+
const written = await file.stat({ bigint: true });
|
|
106
|
+
const named = await lstat(path, { bigint: true });
|
|
107
|
+
if (!named.isSymbolicLink() && named.dev === written.dev && named.ino === written.ino) {
|
|
108
|
+
// Windows publishes content-write ChangeTime on close; retain only the same object's ACL memo.
|
|
109
|
+
await file.close();
|
|
110
|
+
file = undefined;
|
|
111
|
+
reattributeHardenedSecretPath(path);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
await file?.close();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create a best-effort metadata sink with one-second event/outcome buckets and a bounded queue.
|
|
121
|
+
* File and ACL work starts on the first eligible record and never blocks admission. flush() waits
|
|
122
|
+
* at most one second; its timeout does not cancel the writer or guarantee durable storage.
|
|
123
|
+
*/
|
|
124
|
+
export function createLinkDiagnosticSink(
|
|
125
|
+
{ configDir, now = Date.now }: { configDir?: string; now?: () => number } = {},
|
|
126
|
+
): LinkDiagnosticSink & { flush: (timeoutMs?: number) => Promise<void> } {
|
|
127
|
+
// At most 56 buckets; correlation IDs never become keys or retained memory.
|
|
128
|
+
const buckets = new Map<string, { at: number; suppressed: number }>();
|
|
129
|
+
const queue: Array<{ dir: string; line: string }> = [];
|
|
130
|
+
let flight: Promise<void> | undefined;
|
|
131
|
+
const drain = async () => {
|
|
132
|
+
while (queue.length) {
|
|
133
|
+
const record = queue.shift()!;
|
|
134
|
+
try { await append(record.dir, record.line); } catch { /* Best effort; never publish raw errors. */ }
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const startWriter = () => {
|
|
138
|
+
if (flight) return;
|
|
139
|
+
flight = drain().finally(() => {
|
|
140
|
+
flight = undefined;
|
|
141
|
+
if (queue.length) startWriter();
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
const sink: LinkDiagnosticSink = record => {
|
|
145
|
+
try {
|
|
146
|
+
const safe = metadata(record);
|
|
147
|
+
if (!safe) return;
|
|
148
|
+
const at = now();
|
|
149
|
+
if (!Number.isSafeInteger(at) || at < 0) return;
|
|
150
|
+
const key = `${safe.event}:${safe.outcome}`;
|
|
151
|
+
const bucket = buckets.get(key);
|
|
152
|
+
// Wall-clock rollback starts a new window, even when the writer is backed up.
|
|
153
|
+
if (bucket && at >= bucket.at && at - bucket.at < 1_000) {
|
|
154
|
+
bucket.suppressed = Math.min(Number.MAX_SAFE_INTEGER, bucket.suppressed + 1);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (queue.length >= 56) {
|
|
158
|
+
buckets.set(key, { at: Math.min(bucket?.at ?? at, at),
|
|
159
|
+
suppressed: Math.min(Number.MAX_SAFE_INTEGER, (bucket?.suppressed ?? 0) + 1) });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const suppressed = bucket?.suppressed ?? 0;
|
|
163
|
+
// Failed writes are rate-limited too; diagnostics cannot hammer an unavailable disk.
|
|
164
|
+
buckets.set(key, { at, suppressed: 0 });
|
|
165
|
+
queue.push({ dir: linkDir(configDir), line: `${JSON.stringify({ ...safe, timestamp: at, ...(suppressed ? { suppressed } : {}) })}\n` });
|
|
166
|
+
startWriter();
|
|
167
|
+
} catch { /* Diagnostics must never change request admission or expose raw errors. */ }
|
|
168
|
+
};
|
|
169
|
+
return Object.assign(sink, { flush: async (timeoutMs = 1_000) => {
|
|
170
|
+
if (!flight) return;
|
|
171
|
+
// Keep shutdown bounded even if an asynchronous filesystem or ACL operation stalls.
|
|
172
|
+
// Timeout leaves the existing writer alone; it may still finish before process exit.
|
|
173
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
174
|
+
try {
|
|
175
|
+
await Promise.race([
|
|
176
|
+
(async () => { while (flight) await flight; })(),
|
|
177
|
+
new Promise<void>(resolve => { timer = setTimeout(resolve, Math.min(1_000, Math.max(0, timeoutMs))); }),
|
|
178
|
+
]);
|
|
179
|
+
} catch { /* Best effort; never publish raw errors. */ }
|
|
180
|
+
finally { clearTimeout(timer); }
|
|
181
|
+
} });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let defaultSink: ReturnType<typeof createLinkDiagnosticSink> | undefined;
|
|
185
|
+
/** Lazily enqueue sanitized evidence in the default sink without propagating diagnostic failures. */
|
|
186
|
+
export function recordLinkDiagnostic(record: LinkDiagnosticRecord): void {
|
|
187
|
+
try { (defaultSink ??= createLinkDiagnosticSink())(record); } catch { /* Best effort. */ }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Drain existing diagnostics for at most one second without activating an unused sink. */
|
|
191
|
+
export async function flushLinkDiagnostics(): Promise<void> {
|
|
192
|
+
await defaultSink?.flush();
|
|
193
|
+
}
|
package/src/link/relay-auth.ts
CHANGED
|
@@ -2,6 +2,12 @@ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
|
2
2
|
|
|
3
3
|
export const LINK_RELAY_AUTH_PATH = "/.well-known/opencodex/link-relay-auth";
|
|
4
4
|
export const LINK_RELAY_SESSION_HEADER = "x-opencodex-link-session";
|
|
5
|
+
/**
|
|
6
|
+
* Marks a challenge that only asks whether the Home answers (the Child's readiness preflight): the
|
|
7
|
+
* Home proves itself without reserving a session, so no data request can follow it and it holds
|
|
8
|
+
* neither session capacity nor a drain lease. An older Home ignores it and reserves as before.
|
|
9
|
+
*/
|
|
10
|
+
export const LINK_RELAY_PROBE_HEADER = "x-opencodex-link-probe";
|
|
5
11
|
export const LINK_RELAY_AUTH_VERSION = "2";
|
|
6
12
|
export const LINK_RELAY_AUTH_TIMEOUT_MS = 5_000;
|
|
7
13
|
|
package/src/link/ssh-config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
4
4
|
import { isSshAlias } from "./ssh-argv";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -117,11 +117,43 @@ export interface LoadHostCandidatesOptions {
|
|
|
117
117
|
glob?: (pattern: string) => string[];
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/** OpenSSH Include uses glob(..., GLOB_TILDE): parentheses and braces are literal. */
|
|
121
|
+
function normalizeIncludeGlob(pattern: string): string {
|
|
122
|
+
let normalized = "";
|
|
123
|
+
for (let i = 0; i < pattern.length; i += 1) {
|
|
124
|
+
const char = pattern[i]!;
|
|
125
|
+
if (char === "\\" && i + 1 < pattern.length) {
|
|
126
|
+
normalized += char + pattern[++i];
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (char === "[") {
|
|
130
|
+
let end = i + 1;
|
|
131
|
+
if (pattern[end] === "!" || pattern[end] === "^") end += 1;
|
|
132
|
+
if (pattern[end] === "]") end += 1;
|
|
133
|
+
for (; end < pattern.length; end += 1) {
|
|
134
|
+
if (pattern[end] === "\\") end += 1;
|
|
135
|
+
else if (pattern[end] === "]") break;
|
|
136
|
+
}
|
|
137
|
+
if (end < pattern.length) {
|
|
138
|
+
normalized += pattern.slice(i, end + 1);
|
|
139
|
+
i = end;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Keep a malformed suffix intact: generated quoting must not close its class.
|
|
143
|
+
return normalized + pattern.slice(i);
|
|
144
|
+
}
|
|
145
|
+
// Character classes work on both Bun versions, without enabling 1.4.4's extglobs.
|
|
146
|
+
normalized += char === "(" ? "[(]" : char === "{" ? "[{]" : char;
|
|
147
|
+
}
|
|
148
|
+
return normalized;
|
|
149
|
+
}
|
|
150
|
+
|
|
120
151
|
function defaultGlob(pattern: string): string[] {
|
|
121
152
|
if (!/[*?[]/.test(pattern)) return [pattern];
|
|
122
153
|
const base = dirname(pattern.slice(0, pattern.search(/[*?[]/) + 1));
|
|
123
|
-
|
|
124
|
-
|
|
154
|
+
// Only native Windows separators become slashes; POSIX backslash escapes stay intact.
|
|
155
|
+
const relative = pattern.slice(base.length + 1).split(sep).join("/");
|
|
156
|
+
return [...new Bun.Glob(normalizeIncludeGlob(relative)).scanSync({ cwd: base, absolute: true, onlyFiles: true })].sort();
|
|
125
157
|
}
|
|
126
158
|
|
|
127
159
|
/**
|
|
@@ -22,6 +22,8 @@ export interface LinkStatusDto {
|
|
|
22
22
|
tunnelPort: number;
|
|
23
23
|
/** Home only: what the link carried from this Child, and the compatibility verdict. */
|
|
24
24
|
peer?: LinkPeerStatus;
|
|
25
|
+
/** Home-initiated links on request (`?diagnostics=1`): retry progress and the last failure. */
|
|
26
|
+
tunnel?: LinkTunnelDiagnostics;
|
|
25
27
|
}>;
|
|
26
28
|
child: null | {
|
|
27
29
|
alias: string;
|
|
@@ -33,6 +35,43 @@ export interface LinkStatusDto {
|
|
|
33
35
|
local?: LinkLocalPeer;
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
/**
|
|
39
|
+
* The Home's account of one `-R` tunnel: ssh attempts since it was last connected, when the next
|
|
40
|
+
* one is due (null while one runs or none is scheduled), the last state change and the last
|
|
41
|
+
* failure. `cause` is a fixed word from `tunnel-history.ts`, never ssh output.
|
|
42
|
+
*/
|
|
43
|
+
export interface LinkTunnelDiagnostics {
|
|
44
|
+
attempt: number;
|
|
45
|
+
nextRetryAt: string | null;
|
|
46
|
+
lastTransition: { from: LinkWireState; to: LinkWireState; at: string; cause: string | null } | null;
|
|
47
|
+
lastFailure: { cause: string; at: string } | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function iso(at: number): string {
|
|
51
|
+
return new Date(at).toISOString();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Add `tunnel` to each Home-initiated link the supervisor reports. */
|
|
55
|
+
export function attachLinkTunnelDiagnostics(dto: LinkStatusDto, supervisorStates: readonly LinkTunnelStatus[]): LinkStatusDto {
|
|
56
|
+
const byId = new Map(supervisorStates.map(status => [status.linkId, status]));
|
|
57
|
+
for (const link of dto.links) {
|
|
58
|
+
const status = byId.get(link.id);
|
|
59
|
+
if (!status || status.direction !== "hub-initiated") continue;
|
|
60
|
+
const { state, history } = status;
|
|
61
|
+
const due = (state.kind === "failed" || state.kind === "reconnecting") && state.retryAt !== undefined && state.inFlight !== true
|
|
62
|
+
? state.retryAt : null;
|
|
63
|
+
link.tunnel = {
|
|
64
|
+
attempt: history?.attempt ?? 0,
|
|
65
|
+
nextRetryAt: due === null ? null : iso(due),
|
|
66
|
+
lastTransition: history?.lastTransition
|
|
67
|
+
? { from: history.lastTransition.from, to: history.lastTransition.to, at: iso(history.lastTransition.at), cause: history.lastTransition.cause }
|
|
68
|
+
: null,
|
|
69
|
+
lastFailure: history?.lastFailure ? { cause: history.lastFailure.cause, at: iso(history.lastFailure.at) } : null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return dto;
|
|
73
|
+
}
|
|
74
|
+
|
|
36
75
|
/** Add this build's identity and, on a Home, each Child's observed peer status. */
|
|
37
76
|
export function attachLinkPeers(dto: LinkStatusDto, local: LinkLocalPeer, observation: (linkId: string) => LinkPeerEntry): LinkStatusDto {
|
|
38
77
|
if (dto.role === "home") {
|
package/src/link/supervisor.ts
CHANGED
|
@@ -4,13 +4,16 @@ import { atomicWriteFile } from "../config/atomic-write";
|
|
|
4
4
|
import { linkDir, linkKnownHostsPath, linkStorePath } from "./paths";
|
|
5
5
|
import { buildTunnelArgv } from "./ssh-argv";
|
|
6
6
|
import { createSshRunner, type SshChild, type SshRunner } from "./ssh-runner";
|
|
7
|
+
import type { LinkDiagnosticSink } from "./diagnostics";
|
|
8
|
+
import { createTunnelHistoryBook, exitCause, type TunnelCause, type TunnelHistory } from "./tunnel-history";
|
|
7
9
|
import {
|
|
8
10
|
classifySshStderr,
|
|
9
11
|
dueForSpawn,
|
|
10
|
-
|
|
12
|
+
failedSpawnTunnel,
|
|
11
13
|
HOME_TUNNEL_RETRY_POLICY,
|
|
12
14
|
IDLE,
|
|
13
15
|
reduceTunnel,
|
|
16
|
+
type TunnelFailure,
|
|
14
17
|
type TunnelState,
|
|
15
18
|
} from "./tunnel-state";
|
|
16
19
|
import { emptyLinkStore, readLinkStore, type LinkRecord, type LinkStore } from "./store";
|
|
@@ -22,6 +25,8 @@ export type LinkTunnelStatus =
|
|
|
22
25
|
state: TunnelState;
|
|
23
26
|
pid: number | null;
|
|
24
27
|
orphan?: "orphan-unverified";
|
|
28
|
+
/** Attempts, last change and last failure cause of this tunnel (`tunnel-history.ts`). */
|
|
29
|
+
history?: TunnelHistory;
|
|
25
30
|
}
|
|
26
31
|
| {
|
|
27
32
|
linkId: string;
|
|
@@ -72,6 +77,10 @@ export interface LinkSupervisorDeps {
|
|
|
72
77
|
apiKeys?: () => readonly { id: string; name: string }[];
|
|
73
78
|
revokeApiKey?: (id: string) => boolean;
|
|
74
79
|
warn?: (message: string) => void;
|
|
80
|
+
/** Where tunnel state changes are recorded; production passes the bounded diagnostics file. */
|
|
81
|
+
diagnostic?: LinkDiagnosticSink;
|
|
82
|
+
/** Bounded drain of `diagnostic` at stop. */
|
|
83
|
+
flushDiagnostics?: () => Promise<void>;
|
|
75
84
|
}
|
|
76
85
|
|
|
77
86
|
const TIMER_MS = 1_000;
|
|
@@ -166,6 +175,10 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
166
175
|
/** The generation of the child whose exit is still being classified, per link. */
|
|
167
176
|
const exiting = new Map<string, number>();
|
|
168
177
|
const recordInstances = new Map<string, string>();
|
|
178
|
+
const history = createTunnelHistoryBook({
|
|
179
|
+
diagnostic: deps.diagnostic,
|
|
180
|
+
tunnelPort: linkId => store.links.find(link => link.id === linkId)?.tunnelPort,
|
|
181
|
+
});
|
|
169
182
|
|
|
170
183
|
const reconcileApiKeys = (): void => {
|
|
171
184
|
const keys = deps.apiKeys?.() ?? [];
|
|
@@ -218,12 +231,22 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
218
231
|
}
|
|
219
232
|
};
|
|
220
233
|
|
|
221
|
-
|
|
222
|
-
|
|
234
|
+
/** Every state change goes through here, so the history sees each transition and its cause. */
|
|
235
|
+
const put = (linkId: string, next: TunnelState, cause: TunnelCause | null = null): TunnelState => {
|
|
236
|
+
const before = states.get(linkId) ?? IDLE;
|
|
223
237
|
states.set(linkId, next);
|
|
238
|
+
history.observe(linkId, before, next, cause, now());
|
|
224
239
|
return next;
|
|
225
240
|
};
|
|
226
241
|
|
|
242
|
+
const setEvent = (linkId: string, event: Parameters<typeof reduceTunnel>[1], cause: TunnelCause | null = null): TunnelState =>
|
|
243
|
+
put(linkId, reduceTunnel(states.get(linkId) ?? IDLE, event, deps.random, HOME_TUNNEL_RETRY_POLICY), cause);
|
|
244
|
+
|
|
245
|
+
/** Fail a link whose attempt could not start; a due retry that fails this way counts as one. */
|
|
246
|
+
const failAttempt = (linkId: string, prior: TunnelState | undefined, reason: TunnelFailure, cause: TunnelCause): void => {
|
|
247
|
+
put(linkId, failedSpawnTunnel(prior, reason, now(), HOME_TUNNEL_RETRY_POLICY), cause);
|
|
248
|
+
};
|
|
249
|
+
|
|
227
250
|
const spawnFor = (record: LinkRecord): void => {
|
|
228
251
|
if (stopping || holds.has(record.id) || record.direction !== "hub-initiated" || children.has(record.id)) return;
|
|
229
252
|
// Only a fresh link or a due attempt spawns. A reload must neither skip the backoff nor start
|
|
@@ -231,7 +254,8 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
231
254
|
const prior = states.get(record.id);
|
|
232
255
|
if (prior && prior.kind !== "idle" && !dueForSpawn(prior, now())) return;
|
|
233
256
|
if (store.listenerPort === null) {
|
|
234
|
-
|
|
257
|
+
// Retried like any forward failure; a reload that brings the listener back starts it fresh.
|
|
258
|
+
failAttempt(record.id, prior, "forward", "listener_unavailable");
|
|
235
259
|
return;
|
|
236
260
|
}
|
|
237
261
|
let argv: string[];
|
|
@@ -250,6 +274,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
250
274
|
exiting.delete(record.id);
|
|
251
275
|
children.set(record.id, { child, argv, spawnedAt: now() });
|
|
252
276
|
orphanUnverified.delete(record.id);
|
|
277
|
+
history.spawned(record.id);
|
|
253
278
|
setEvent(record.id, { type: "spawn", now: now() });
|
|
254
279
|
writePidfile(pidfilePath(record.id), { version: 1, linkId: record.id, pid: child.pid, argv });
|
|
255
280
|
void child.exited.then(async () => {
|
|
@@ -264,7 +289,8 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
264
289
|
try { stderr = child.stderr ? await child.stderr : ""; } catch { /* classified as unknown */ }
|
|
265
290
|
// A reconnect may have started a fresh child meanwhile; its own lifecycle owns the state.
|
|
266
291
|
if (generations.get(record.id) !== generation) return;
|
|
267
|
-
|
|
292
|
+
const stderrClass = classifySshStderr(stderr);
|
|
293
|
+
setEvent(record.id, { type: "exit", now: now(), stderrClass }, exitCause(stderrClass));
|
|
268
294
|
} finally {
|
|
269
295
|
if (exiting.get(record.id) === generation) exiting.delete(record.id);
|
|
270
296
|
}
|
|
@@ -272,11 +298,10 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
272
298
|
if (children.get(record.id)?.child !== child) return;
|
|
273
299
|
children.delete(record.id);
|
|
274
300
|
conditionalRemovePidfile(record.id, child.pid);
|
|
275
|
-
if (!stopping) setEvent(record.id, { type: "exit", now: now(), stderrClass: "network" });
|
|
301
|
+
if (!stopping) setEvent(record.id, { type: "exit", now: now(), stderrClass: "network" }, "network");
|
|
276
302
|
});
|
|
277
303
|
} catch {
|
|
278
|
-
|
|
279
|
-
states.set(record.id, failedTunnel("forward", at, HOME_TUNNEL_RETRY_POLICY, prior?.kind === "failed" ? prior.since : at));
|
|
304
|
+
failAttempt(record.id, prior, "forward", "spawn_failed");
|
|
280
305
|
}
|
|
281
306
|
};
|
|
282
307
|
|
|
@@ -300,7 +325,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
300
325
|
setEvent(record.id, { type: "ready", now: current });
|
|
301
326
|
continue;
|
|
302
327
|
}
|
|
303
|
-
const next = setEvent(record.id, { type: "tick", now: current });
|
|
328
|
+
const next = setEvent(record.id, { type: "tick", now: current }, "timeout");
|
|
304
329
|
if (next.kind === "failed" && !next.inFlight && child && retryInFlight(before)) {
|
|
305
330
|
// A retry started just before the outage limit has not had its grace yet. It carries on as
|
|
306
331
|
// the failed state's retry instead of being killed with a link that may already be back.
|
|
@@ -349,7 +374,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
349
374
|
const stopChild = async (linkId: string): Promise<void> => {
|
|
350
375
|
const current = children.get(linkId);
|
|
351
376
|
if (!current) {
|
|
352
|
-
|
|
377
|
+
put(linkId, IDLE);
|
|
353
378
|
return;
|
|
354
379
|
}
|
|
355
380
|
const generation = generations.get(linkId);
|
|
@@ -358,7 +383,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
358
383
|
await current.child.exited;
|
|
359
384
|
conditionalRemovePidfile(linkId, current.child.pid);
|
|
360
385
|
// A stop or removal reservation made while this child exited owns the next state.
|
|
361
|
-
if (generations.get(linkId) === generation)
|
|
386
|
+
if (generations.get(linkId) === generation) put(linkId, IDLE);
|
|
362
387
|
};
|
|
363
388
|
|
|
364
389
|
const invalidateLink = (linkId: string): void => {
|
|
@@ -407,6 +432,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
407
432
|
if (nextInstances.has(linkId) && !changed.has(linkId)) continue;
|
|
408
433
|
await stopChild(linkId);
|
|
409
434
|
states.delete(linkId);
|
|
435
|
+
history.forget(linkId);
|
|
410
436
|
orphanUnverified.delete(linkId);
|
|
411
437
|
recordInstances.delete(linkId);
|
|
412
438
|
if (stopping) return;
|
|
@@ -414,9 +440,13 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
414
440
|
for (const record of nextStore.links) {
|
|
415
441
|
if (changed.has(record.id)) {
|
|
416
442
|
states.delete(record.id);
|
|
443
|
+
history.forget(record.id);
|
|
417
444
|
orphanUnverified.delete(record.id);
|
|
418
445
|
}
|
|
419
446
|
}
|
|
447
|
+
for (const linkId of [...states.keys()]) {
|
|
448
|
+
if (!nextInstances.has(linkId)) history.forget(linkId);
|
|
449
|
+
}
|
|
420
450
|
recordInstances.clear();
|
|
421
451
|
for (const [linkId, instance] of nextInstances) recordInstances.set(linkId, instance);
|
|
422
452
|
for (const record of nextStore.links) {
|
|
@@ -471,12 +501,14 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
471
501
|
return { linkId: record.id, direction: record.direction, state: "client-owned", pid: null };
|
|
472
502
|
}
|
|
473
503
|
const child = children.get(record.id);
|
|
504
|
+
const recorded = history.get(record.id);
|
|
474
505
|
return {
|
|
475
506
|
linkId: record.id,
|
|
476
507
|
direction: record.direction,
|
|
477
508
|
state: states.get(record.id) ?? IDLE,
|
|
478
509
|
pid: child?.child.pid ?? null,
|
|
479
510
|
...(orphanUnverified.has(record.id) ? { orphan: "orphan-unverified" as const } : {}),
|
|
511
|
+
...(recorded ? { history: recorded } : {}),
|
|
480
512
|
};
|
|
481
513
|
});
|
|
482
514
|
},
|
|
@@ -489,9 +521,10 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
|
|
|
489
521
|
await runLifecycle(async () => {
|
|
490
522
|
await Promise.all([...children.keys()].map(stopChild));
|
|
491
523
|
for (const record of store.links) {
|
|
492
|
-
if (record.direction === "hub-initiated")
|
|
524
|
+
if (record.direction === "hub-initiated") put(record.id, IDLE);
|
|
493
525
|
}
|
|
494
526
|
});
|
|
527
|
+
try { await deps.flushDiagnostics?.(); } catch { /* best effort */ }
|
|
495
528
|
},
|
|
496
529
|
};
|
|
497
530
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the Home remembers about one `-R` tunnel besides its current state: how many ssh attempts
|
|
3
|
+
* the current outage has cost, the last state change and the last failure with its cause. It lives
|
|
4
|
+
* in memory next to the supervisor's state, appears in `GET /api/link/status` as `tunnel`, and
|
|
5
|
+
* each change it records also goes to the bounded diagnostics file (`diagnostics.ts`), so a link
|
|
6
|
+
* that failed for an hour leaves evidence of why after the fact.
|
|
7
|
+
*
|
|
8
|
+
* Nothing here carries ssh output, argv, host names or keys: a cause is one of a fixed set of
|
|
9
|
+
* words, and times are numbers.
|
|
10
|
+
*/
|
|
11
|
+
import type { LinkDiagnosticSink } from "./diagnostics";
|
|
12
|
+
import type { StderrClass, TunnelState } from "./tunnel-state";
|
|
13
|
+
|
|
14
|
+
export type TunnelCause =
|
|
15
|
+
| "auth" | "hostkey" | "forward" | "remote_forward_conflict" | "timeout" | "network" | "unknown"
|
|
16
|
+
| "spawn_failed" | "listener_unavailable";
|
|
17
|
+
|
|
18
|
+
export const TUNNEL_CAUSES: readonly TunnelCause[] = [
|
|
19
|
+
"auth", "hostkey", "forward", "remote_forward_conflict", "timeout", "network", "unknown",
|
|
20
|
+
"spawn_failed", "listener_unavailable",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
export interface TunnelTransitionRecord {
|
|
24
|
+
from: TunnelState["kind"];
|
|
25
|
+
to: TunnelState["kind"];
|
|
26
|
+
at: number;
|
|
27
|
+
cause: TunnelCause | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface TunnelHistory {
|
|
31
|
+
/** ssh attempts started since the link was last connected (0 while connected). */
|
|
32
|
+
attempt: number;
|
|
33
|
+
lastTransition: TunnelTransitionRecord | null;
|
|
34
|
+
lastFailure: { cause: TunnelCause; at: number } | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function exitCause(stderrClass: StderrClass): TunnelCause {
|
|
38
|
+
return stderrClass === "remote_forward" ? "remote_forward_conflict" : stderrClass;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A retry of a failed link ended without connecting: one that ran while the state read failed, or
|
|
43
|
+
* one that could not even start (the retry count still grew).
|
|
44
|
+
*/
|
|
45
|
+
function retryFailedAgain(before: TunnelState, after: TunnelState): boolean {
|
|
46
|
+
if (before.kind !== "failed" || after.kind !== "failed" || after.inFlight === true) return false;
|
|
47
|
+
return before.inFlight === true || (after.retries ?? 0) > (before.retries ?? 0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TunnelHistoryBook {
|
|
51
|
+
/** Count one ssh attempt for the link. */
|
|
52
|
+
spawned(linkId: string): void;
|
|
53
|
+
/** Record a state change; `cause` names why the link left a working state, when known. */
|
|
54
|
+
observe(linkId: string, before: TunnelState, after: TunnelState, cause: TunnelCause | null, at: number): void;
|
|
55
|
+
get(linkId: string): TunnelHistory | undefined;
|
|
56
|
+
forget(linkId: string): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function createTunnelHistoryBook(
|
|
60
|
+
options: { diagnostic?: LinkDiagnosticSink; tunnelPort?: (linkId: string) => number | undefined } = {},
|
|
61
|
+
): TunnelHistoryBook {
|
|
62
|
+
const histories = new Map<string, TunnelHistory>();
|
|
63
|
+
const entry = (linkId: string): TunnelHistory => {
|
|
64
|
+
let history = histories.get(linkId);
|
|
65
|
+
if (!history) {
|
|
66
|
+
history = { attempt: 0, lastTransition: null, lastFailure: null };
|
|
67
|
+
histories.set(linkId, history);
|
|
68
|
+
}
|
|
69
|
+
return history;
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
spawned(linkId) {
|
|
73
|
+
entry(linkId).attempt += 1;
|
|
74
|
+
},
|
|
75
|
+
observe(linkId, before, after, cause, at) {
|
|
76
|
+
const refailed = retryFailedAgain(before, after);
|
|
77
|
+
if (before.kind === after.kind && !refailed) return;
|
|
78
|
+
const history = entry(linkId);
|
|
79
|
+
if (after.kind === "connected" || after.kind === "idle") history.attempt = 0;
|
|
80
|
+
if (cause && (after.kind === "failed" || after.kind === "reconnecting")) history.lastFailure = { cause, at };
|
|
81
|
+
history.lastTransition = { from: before.kind, to: after.kind, at, cause };
|
|
82
|
+
const retryAt = after.kind === "failed" || after.kind === "reconnecting" ? after.retryAt : undefined;
|
|
83
|
+
const tunnelPort = options.tunnelPort?.(linkId);
|
|
84
|
+
try {
|
|
85
|
+
options.diagnostic?.({
|
|
86
|
+
event: "tunnel_transition",
|
|
87
|
+
outcome: cause ?? (after.kind === "connected" ? "connected" : after.kind === "idle" ? "stopped" : "spawned"),
|
|
88
|
+
state: after.kind,
|
|
89
|
+
previousState: before.kind,
|
|
90
|
+
attempt: history.attempt,
|
|
91
|
+
...(tunnelPort !== undefined ? { tunnelPort } : {}),
|
|
92
|
+
...(retryAt !== undefined ? { retryAfterMs: Math.max(0, retryAt - at) } : {}),
|
|
93
|
+
});
|
|
94
|
+
} catch { /* Diagnostics never change the tunnel lifecycle. */ }
|
|
95
|
+
},
|
|
96
|
+
get(linkId) {
|
|
97
|
+
const history = histories.get(linkId);
|
|
98
|
+
if (!history) return undefined;
|
|
99
|
+
return {
|
|
100
|
+
attempt: history.attempt,
|
|
101
|
+
lastTransition: history.lastTransition && { ...history.lastTransition },
|
|
102
|
+
lastFailure: history.lastFailure && { ...history.lastFailure },
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
forget(linkId) {
|
|
106
|
+
histories.delete(linkId);
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|