@blackbelt-technology/pi-agent-dashboard 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -5
- package/docs/architecture.md +1673 -75
- package/package.json +23 -15
- package/packages/extension/package.json +8 -8
- package/packages/extension/src/ask-user-tool.ts +65 -3
- package/packages/extension/src/auto-session-namer.ts +190 -50
- package/packages/extension/src/bridge-handlers/dispatch.ts +450 -0
- package/packages/extension/src/bridge-handlers/forwarders.ts +616 -0
- package/packages/extension/src/bridge-handlers/instance.ts +220 -0
- package/packages/extension/src/bridge-handlers/lifecycle.ts +1030 -0
- package/packages/extension/src/bridge-handlers/ui-prompt-patch.ts +198 -0
- package/packages/extension/src/bridge-identity-log.ts +45 -0
- package/packages/extension/src/bridge.ts +480 -2510
- package/packages/extension/src/command-handler.ts +180 -11
- package/packages/extension/src/connection.ts +265 -18
- package/packages/extension/src/dashboard-context-injector.ts +26 -47
- package/packages/extension/src/empty-actionable-guard.ts +8 -3
- package/packages/extension/src/flow-event-wiring.ts +1 -1
- package/packages/extension/src/followup-buffer.ts +1 -1
- package/packages/extension/src/followup-drain.ts +79 -0
- package/packages/extension/src/local-token-header.ts +161 -17
- package/packages/extension/src/message-update-coalescer.ts +3 -1
- package/packages/extension/src/model-refresh.ts +19 -0
- package/packages/extension/src/multiselect-list.ts +6 -2
- package/packages/extension/src/multiselect-polyfill.ts +2 -2
- package/packages/extension/src/openspec-cli-shim.ts +1 -1
- package/packages/extension/src/path-gate/decide.ts +15 -41
- package/packages/extension/src/path-gate/grant-cache.ts +3 -1
- package/packages/extension/src/path-gate/handler.ts +51 -2
- package/packages/extension/src/path-gate/index.ts +12 -1
- package/packages/extension/src/path-gate/resolve.ts +6 -124
- package/packages/extension/src/path-gate/roots.ts +4 -1
- package/packages/extension/src/path-gate/suppression.ts +4 -1
- package/packages/extension/src/path-gate/yolo-link.ts +104 -0
- package/packages/extension/src/plugin-event-forward-registry.ts +200 -0
- package/packages/extension/src/project-trust.ts +119 -25
- package/packages/extension/src/prompt-expander.ts +5 -1
- package/packages/extension/src/prompt-meta.ts +64 -0
- package/packages/extension/src/provider-register.ts +87 -20
- package/packages/extension/src/role-manager.ts +5 -4
- package/packages/extension/src/server-auto-start.ts +12 -5
- package/packages/extension/src/server-launcher.ts +11 -13
- package/packages/extension/src/session-move.ts +19 -0
- package/packages/extension/src/session-sync.ts +31 -38
- package/packages/extension/src/spawn-token.ts +46 -0
- package/packages/extension/src/stream-forward-buffer.ts +161 -0
- package/packages/extension/src/subagent-frame-buffer.ts +6 -2
- package/packages/extension/src/subagent-tick-throttle.ts +3 -1
- package/packages/extension/src/vault-delivery.ts +394 -0
- package/packages/extension/src/vault-forwarder.ts +128 -0
- package/packages/extension/src/vault-masker.ts +165 -0
- package/packages/extension/src/vault-redaction.ts +181 -0
- package/packages/extension/src/vcs-info.ts +5 -2
- package/packages/server/bin/pi-dashboard.mjs +26 -89
- package/packages/server/package.json +29 -15
- package/packages/server/src/access/access-grants.ts +18 -0
- package/packages/server/src/access/agent-confirm-registry.ts +31 -13
- package/packages/server/src/access/agent-grant.ts +1 -1
- package/packages/server/src/access/agent-yolo.ts +91 -0
- package/packages/server/src/access/containment-gate.ts +28 -1
- package/packages/server/src/access/cwd-admission-log.ts +46 -0
- package/packages/server/src/access/cwd-verdict.ts +144 -0
- package/packages/server/src/access/grant-coordinator.ts +3 -1
- package/packages/server/src/access/pi-trust-mirror-service.ts +374 -0
- package/packages/server/src/access/planes.ts +10 -1
- package/packages/server/src/access/refusal-ledger.ts +6 -6
- package/packages/server/src/access/yolo-session.ts +73 -12
- package/packages/server/src/acp-bridge/acp-connection.ts +225 -0
- package/packages/server/src/acp-bridge/acp-restore.ts +41 -0
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +544 -0
- package/packages/server/src/acp-bridge/child-env.ts +73 -0
- package/packages/server/src/acp-bridge/event-log.ts +194 -0
- package/packages/server/src/acp-bridge/gateway-client.ts +136 -0
- package/packages/server/src/acp-bridge/local-bridge-socket.ts +67 -0
- package/packages/server/src/acp-bridge/local-host.ts +132 -0
- package/packages/server/src/acp-bridge/main.ts +118 -0
- package/packages/server/src/acp-bridge/models.ts +33 -0
- package/packages/server/src/acp-bridge/permission-bridge.ts +106 -0
- package/packages/server/src/acp-bridge/server-requests.ts +136 -0
- package/packages/server/src/acp-bridge/session-state.ts +127 -0
- package/packages/server/src/addons/addon-toggle-restore.ts +110 -0
- package/packages/server/src/addons/addons-catalog.ts +408 -0
- package/packages/server/src/addons/addons-folder-service.ts +347 -0
- package/packages/server/src/addons/addons-folder.ts +310 -0
- package/packages/server/src/addons/addons-service.ts +470 -0
- package/packages/server/src/addons/package-contents.ts +99 -0
- package/packages/server/src/addons/settings-entries.ts +120 -0
- package/packages/server/src/attachments/fit-worker-pool.ts +12 -17
- package/packages/server/src/auth/approval-refusal.ts +87 -0
- package/packages/server/src/auth/auth-plugin.ts +292 -79
- package/packages/server/src/auth/auth.ts +78 -7
- package/packages/server/src/auth/bearer-auth.ts +23 -1
- package/packages/server/src/auth/cors-origin.ts +10 -0
- package/packages/server/src/auth/csp.ts +6 -0
- package/packages/server/src/auth/host-admission.ts +26 -0
- package/packages/server/src/auth/host-gate.ts +6 -1
- package/packages/server/src/auth/local-credential.ts +258 -0
- package/packages/server/src/auth/local-proof.ts +7 -4
- package/packages/server/src/auth/local-token.ts +16 -0
- package/packages/server/src/auth/localhost-guard.ts +251 -8
- package/packages/server/src/auth/network-trust-telemetry.ts +126 -0
- package/packages/server/src/auth/node-guard.ts +43 -0
- package/packages/server/src/auth/operate-required.ts +126 -0
- package/packages/server/src/auth/passkey/challenge-store.ts +90 -0
- package/packages/server/src/auth/passkey/passkey-log.ts +34 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +173 -0
- package/packages/server/src/auth/passkey/passkey-routes.ts +224 -0
- package/packages/server/src/auth/passkey/passkey-service.ts +247 -0
- package/packages/server/src/auth/passkey/phone-signin.ts +211 -0
- package/packages/server/src/auth/passkey/requester.ts +83 -0
- package/packages/server/src/auth/passkey/rp-context.ts +98 -0
- package/packages/server/src/auth/passkey/user-directory.ts +348 -0
- package/packages/server/src/auth/pi-legacy-credentials.ts +321 -0
- package/packages/server/src/auth/provider-auth-registry.ts +35 -1
- package/packages/server/src/auth/provider-auth-storage.ts +55 -8
- package/packages/server/src/auth/radius-override.ts +13 -0
- package/packages/server/src/auth/route-tier-gate.ts +27 -11
- package/packages/server/src/auth/runtime-provider-catalogue.ts +107 -0
- package/packages/server/src/auth/scan-legacy-provider-ids.ts +80 -0
- package/packages/server/src/auth/serve-app-host.ts +97 -0
- package/packages/server/src/auth/trust-mode-boot.ts +93 -0
- package/packages/server/src/auth/trust-mode.ts +83 -0
- package/packages/server/src/auto-name-outcome-store.ts +4 -1
- package/packages/server/src/browse.ts +114 -2
- package/packages/server/src/browser-handlers/acp-guard.ts +79 -0
- package/packages/server/src/browser-handlers/directory-handler.ts +128 -6
- package/packages/server/src/browser-handlers/session-action-handler.ts +129 -62
- package/packages/server/src/browser-handlers/session-meta-handler.ts +16 -6
- package/packages/server/src/browser-handlers/spawn-choice.ts +99 -0
- package/packages/server/src/cli.ts +883 -97
- package/packages/server/src/config-api.ts +143 -1
- package/packages/server/src/config-snapshot.ts +5 -0
- package/packages/server/src/directory-service.ts +435 -32
- package/packages/server/src/embed-lifecycle/caps.ts +35 -4
- package/packages/server/src/embed-lifecycle/embed-lifecycle-controller.ts +45 -4
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +195 -20
- package/packages/server/src/embed-lifecycle/lifecycle-metrics.ts +38 -0
- package/packages/server/src/embed-lifecycle/plugin-session-close.ts +123 -0
- package/packages/server/src/embed-lifecycle/quiescence.ts +29 -3
- package/packages/server/src/embed-lifecycle/reclaim-give-up.ts +54 -0
- package/packages/server/src/event-wiring.ts +208 -35
- package/packages/server/src/file-watch-manager.ts +187 -39
- package/packages/server/src/git-worktree/git-operations.ts +16 -5
- package/packages/server/src/git-worktree/worktree-init-trust.ts +48 -5
- package/packages/server/src/git-worktree/worktree-init.ts +7 -56
- package/packages/server/src/identity/host-resources.ts +3 -0
- package/packages/server/src/identity/http-road-classification.ts +18 -0
- package/packages/server/src/identity/identity-registration-tracker.ts +4 -1
- package/packages/server/src/identity/plugin-identity.ts +19 -0
- package/packages/server/src/identity/policy-registry.ts +4 -1
- package/packages/server/src/identity/resolver-registry.ts +12 -3
- package/packages/server/src/identity/ws-message-scope.ts +9 -0
- package/packages/server/src/identity/ws-road-classification.ts +9 -0
- package/packages/server/src/known-server-moves.ts +175 -0
- package/packages/server/src/lib/browse-roots.ts +36 -0
- package/packages/server/src/lib/digest-gate.ts +82 -0
- package/packages/server/src/lib/file-write-lock.ts +24 -0
- package/packages/server/src/lib/folder-project.ts +439 -0
- package/packages/server/src/lib/purify.ts +13 -5
- package/packages/server/src/lib/writable-handler-target.ts +66 -0
- package/packages/server/src/lib/writable-md-target.ts +12 -1
- package/packages/server/src/lifecycle/classify-dashboard-port.ts +134 -0
- package/packages/server/src/lifecycle/home-lock.ts +344 -130
- package/packages/server/src/lifecycle/home-rendezvous.ts +41 -11
- package/packages/server/src/lifecycle/recovery-server.ts +77 -1
- package/packages/server/src/lifecycle/stop-ownership.ts +97 -11
- package/packages/server/src/live-server/live-capability.ts +254 -0
- package/packages/server/src/live-server/live-server-proxy.ts +173 -26
- package/packages/server/src/metrics/eventloop-spike-metrics.ts +2 -2
- package/packages/server/src/model-proxy/internal-auth-storage.ts +27 -26
- package/packages/server/src/model-proxy/internal-registry.ts +6 -0
- package/packages/server/src/model-proxy/server-model-runtime.ts +6 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +109 -16
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +132 -0
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +90 -35
- package/packages/server/src/openspec/openspec-poll-worker.ts +200 -44
- package/packages/server/src/openspec/proposal-attach-naming.ts +5 -3
- package/packages/server/src/openspec/readiness.ts +9 -0
- package/packages/server/src/openspec/workflow-schema.ts +1128 -0
- package/packages/server/src/package/installed-package-enricher.ts +38 -3
- package/packages/server/src/package/package-health-scanner.ts +133 -0
- package/packages/server/src/package/package-manager-wrapper.ts +18 -2
- package/packages/server/src/package/provider-catalogue-cache.ts +17 -3
- package/packages/server/src/package/provider-probe.ts +5 -3
- package/packages/server/src/pairing/browser-gateway.ts +108 -8
- package/packages/server/src/pairing/knock-admission.ts +277 -0
- package/packages/server/src/pairing/pairing.ts +154 -27
- package/packages/server/src/pairing/subagent-resync-routing.ts +4 -1
- package/packages/server/src/pairing/ws-tier-gate.ts +34 -0
- package/packages/server/src/peer-servers.ts +77 -0
- package/packages/server/src/pending/pending-plugin-ref-registry.ts +57 -2
- package/packages/server/src/persistence/boot-state.ts +48 -1
- package/packages/server/src/persistence/memory-event-store.ts +216 -237
- package/packages/server/src/persistence/meta-persistence.ts +12 -1
- package/packages/server/src/persistence/preferences-store.ts +116 -7
- package/packages/server/src/pi/bridge-upgrade-auth.ts +32 -21
- package/packages/server/src/pi/folder-commands.ts +141 -0
- package/packages/server/src/pi/gateway-socket-bind.ts +9 -3
- package/packages/server/src/pi/gateway-transport-policy.ts +13 -0
- package/packages/server/src/pi/host-project-trust.ts +158 -33
- package/packages/server/src/pi/pi-gateway.ts +93 -9
- package/packages/server/src/pi/pi-resource-scanner.ts +15 -0
- package/packages/server/src/pi/resource-activation-toggle.ts +6 -0
- package/packages/server/src/pi/resource-toggle-trust.ts +34 -2
- package/packages/server/src/pi/session-skill-registry.ts +13 -3
- package/packages/server/src/pi/settings-write-lock.ts +32 -0
- package/packages/server/src/pi/trust-challenges.ts +38 -0
- package/packages/server/src/pi-agent-settings.ts +701 -100
- package/packages/server/src/pi-settings-themes.ts +104 -0
- package/packages/server/src/questions/brief-generator.ts +138 -0
- package/packages/server/src/questions/brief-model.ts +55 -0
- package/packages/server/src/questions/brief-policy.ts +36 -0
- package/packages/server/src/questions/brief-prompt.ts +92 -0
- package/packages/server/src/questions/brief-runtime.ts +50 -0
- package/packages/server/src/questions/question-answer.ts +55 -0
- package/packages/server/src/questions/question-broadcast.ts +106 -0
- package/packages/server/src/questions/question-files.ts +128 -0
- package/packages/server/src/questions/question-routes.ts +126 -0
- package/packages/server/src/questions/question-snapshot.ts +150 -0
- package/packages/server/src/questions/question-stack.ts +57 -0
- package/packages/server/src/questions/question-store.ts +477 -0
- package/packages/server/src/routes/access-prompt-routes.ts +5 -3
- package/packages/server/src/routes/access-routes.ts +123 -5
- package/packages/server/src/routes/acme-routes.ts +190 -0
- package/packages/server/src/routes/acp-routes.ts +16 -0
- package/packages/server/src/routes/addons-routes.ts +221 -0
- package/packages/server/src/routes/file-routes.ts +366 -75
- package/packages/server/src/routes/file-write-binary-routes.ts +473 -0
- package/packages/server/src/routes/folder-project-routes.ts +99 -0
- package/packages/server/src/routes/git-routes.ts +17 -2
- package/packages/server/src/routes/known-servers-routes.ts +333 -53
- package/packages/server/src/routes/live-server-routes.ts +72 -4
- package/packages/server/src/routes/local-proof-routes.ts +12 -7
- package/packages/server/src/routes/mermaid-repair-routes.ts +254 -0
- package/packages/server/src/routes/model-proxy-routes.ts +4 -2
- package/packages/server/src/routes/openspec-routes.ts +270 -40
- package/packages/server/src/routes/package-health-routes.ts +140 -0
- package/packages/server/src/routes/package-routes.ts +28 -1
- package/packages/server/src/routes/pairing-routes.ts +158 -24
- package/packages/server/src/routes/pi-retry-routes.ts +18 -2
- package/packages/server/src/routes/pi-settings-routes.ts +264 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +87 -27
- package/packages/server/src/routes/provider-auth-routes.ts +22 -5
- package/packages/server/src/routes/provider-routes.ts +29 -0
- package/packages/server/src/routes/resource-activation-routes.ts +16 -32
- package/packages/server/src/routes/session-routes.ts +49 -5
- package/packages/server/src/routes/system-routes.ts +207 -23
- package/packages/server/src/routes/tls-routes.ts +55 -0
- package/packages/server/src/routes/user-routes.ts +192 -0
- package/packages/server/src/runtime-overlay/runtime-stager.ts +15 -2
- package/packages/server/src/runtime-overlay/runtime-update-checker.ts +11 -2
- package/packages/server/src/server.ts +919 -139
- package/packages/server/src/services/attached-driver.ts +175 -0
- package/packages/server/src/services/change-notifier.ts +55 -0
- package/packages/server/src/services/cli-service.ts +450 -0
- package/packages/server/src/services/command-runner.ts +92 -0
- package/packages/server/src/services/definitions-store.ts +121 -0
- package/packages/server/src/services/driver.ts +112 -0
- package/packages/server/src/services/health.ts +94 -0
- package/packages/server/src/services/leases.ts +117 -0
- package/packages/server/src/services/log-text.ts +97 -0
- package/packages/server/src/services/native-driver.ts +283 -0
- package/packages/server/src/services/oci-driver.ts +590 -0
- package/packages/server/src/services/output-capture.ts +183 -0
- package/packages/server/src/services/paths.ts +82 -0
- package/packages/server/src/services/ports.ts +26 -0
- package/packages/server/src/services/routes.ts +140 -0
- package/packages/server/src/services/runtime-detect.ts +108 -0
- package/packages/server/src/services/secret-delivery.ts +61 -0
- package/packages/server/src/services/secrets-resolver.ts +145 -0
- package/packages/server/src/services/secrets-store.ts +137 -0
- package/packages/server/src/services/service-manager.ts +1565 -0
- package/packages/server/src/services/start-record.ts +66 -0
- package/packages/server/src/services/state-machine.ts +241 -0
- package/packages/server/src/session/archive-sweeper.ts +92 -3
- package/packages/server/src/session/custom-event-group-matcher.ts +4 -3
- package/packages/server/src/session/in-flight-tool-calls.ts +74 -0
- package/packages/server/src/session/memory-session-manager.ts +53 -0
- package/packages/server/src/session/name-provenance.ts +102 -0
- package/packages/server/src/session/open-tool-calls.ts +15 -6
- package/packages/server/src/session/session-api.ts +47 -12
- package/packages/server/src/session/session-archive.ts +56 -4
- package/packages/server/src/session/session-diff-cache.ts +11 -4
- package/packages/server/src/session/session-load-worker-pool.ts +4 -3
- package/packages/server/src/session/session-scanner.ts +73 -3
- package/packages/server/src/session/session-to-meta.ts +15 -0
- package/packages/server/src/spawn-process/abort-spawned-run.ts +55 -0
- package/packages/server/src/spawn-process/acp-spawn.ts +131 -0
- package/packages/server/src/spawn-process/process-manager.ts +164 -30
- package/packages/server/src/spawn-process/restart-helper.ts +9 -8
- package/packages/server/src/spawn-process/server-pid.ts +16 -7
- package/packages/server/src/spawn-process/session-terminator.ts +218 -0
- package/packages/server/src/spawn-process/spawn-env-contributors.ts +159 -0
- package/packages/server/src/spawn-process/spawn-register-watchdog.ts +10 -1
- package/packages/server/src/terminal/terminal-manager.ts +67 -1
- package/packages/server/src/tls/cert-files.ts +228 -0
- package/packages/server/src/tls/cert-manager.ts +703 -0
- package/packages/server/src/tls/cert-store.ts +69 -0
- package/packages/server/src/tls/source-registry.ts +71 -0
- package/packages/server/src/tls/sources/acme/accounts.ts +76 -0
- package/packages/server/src/tls/sources/acme/acme-source.ts +508 -0
- package/packages/server/src/tls/sources/acme/config-projection.ts +34 -0
- package/packages/server/src/tls/sources/acme/credentials.ts +185 -0
- package/packages/server/src/tls/sources/acme/dns.ts +303 -0
- package/packages/server/src/tls/sources/acme/e2e-overrides.ts +49 -0
- package/packages/server/src/tls/sources/acme/entry.ts +180 -0
- package/packages/server/src/tls/sources/acme/gc.ts +78 -0
- package/packages/server/src/tls/sources/acme/runtime.ts +51 -0
- package/packages/server/src/tls/sources/acme/solvers/acme-dns.ts +154 -0
- package/packages/server/src/tls/sources/acme/solvers/cloudflare.ts +147 -0
- package/packages/server/src/tls/sources/acme/types.ts +72 -0
- package/packages/server/src/tls/sources/tailscale-cert.ts +172 -0
- package/packages/server/src/tls/tls-config-validation.ts +92 -0
- package/packages/server/src/tls/tls-listener.ts +351 -0
- package/packages/server/src/tls/types.ts +80 -0
- package/packages/server/src/tunnel/tunnel-block-events.ts +4 -1
- package/packages/server/src/tunnel/tunnel-core.ts +4 -1
- package/packages/server/src/tunnel/tunnel-endpoints.ts +4 -0
- package/packages/server/src/tunnel-providers/tailscale.ts +27 -6
- package/packages/server/src/vault/vault-bindings.ts +101 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +294 -0
- package/packages/server/src/vault/vault-grants.ts +260 -0
- package/packages/server/src/vault/vault-import.ts +161 -0
- package/packages/server/src/vault/vault-permissions.ts +39 -0
- package/packages/server/src/vault/vault-plugin-read.ts +64 -0
- package/packages/server/src/vault/vault-projects.ts +156 -0
- package/packages/server/src/vault/vault-routes.ts +277 -0
- package/packages/server/src/vault/vault-store.ts +371 -0
- package/packages/shared/package.json +5 -2
- package/packages/shared/src/acp/acp-config.ts +123 -0
- package/packages/shared/src/acp/normalize-v1.ts +92 -0
- package/packages/shared/src/acp/paths.ts +36 -0
- package/packages/shared/src/acp/to-dashboard-events.ts +533 -0
- package/packages/shared/src/acp/update-model.ts +574 -0
- package/packages/shared/src/anthropic-messages-source.ts +8 -0
- package/packages/shared/src/bind-reachability.ts +21 -2
- package/packages/shared/src/boot-state.ts +6 -0
- package/packages/shared/src/browser-host-identity.ts +49 -0
- package/packages/shared/src/browser-host-ipc.ts +107 -0
- package/packages/shared/src/browser-protocol.ts +385 -71
- package/packages/shared/src/canonical-subject.ts +27 -2
- package/packages/shared/src/card-sections.ts +185 -1
- package/packages/shared/src/chrome-profiles.ts +188 -0
- package/packages/shared/src/config.ts +473 -26
- package/packages/shared/src/custom-event-groups.ts +5 -2
- package/packages/shared/src/cwd-guard.ts +381 -1
- package/packages/shared/src/dashboard-paths.ts +12 -0
- package/packages/shared/src/dashboard-plugin/editor-pane-tab.ts +26 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +71 -0
- package/packages/shared/src/dashboard-plugin/index.ts +7 -3
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +106 -2
- package/packages/shared/src/dashboard-plugin/project-identity.ts +42 -0
- package/packages/shared/src/dashboard-plugin/project-markers.ts +60 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +95 -0
- package/packages/shared/src/dashboard-plugin/slot-types.ts +64 -4
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +22 -3
- package/packages/shared/src/dashboard-starter.ts +30 -0
- package/packages/shared/src/display-name.ts +42 -0
- package/packages/shared/src/doctor-core.ts +25 -107
- package/packages/shared/src/enabled-models-match.ts +47 -0
- package/packages/shared/src/event-forward-declaration.ts +187 -0
- package/packages/shared/src/file-kind.ts +54 -2
- package/packages/shared/src/git-worktree-helpers.ts +8 -4
- package/packages/shared/src/grammar-types.ts +3 -1
- package/packages/shared/src/host-admission.ts +2 -1
- package/packages/shared/src/host-port.ts +34 -0
- package/packages/shared/src/launcher-origin.ts +26 -0
- package/packages/shared/src/local-credential-proof.ts +219 -0
- package/packages/shared/src/markup-repair/asciidoc.ts +130 -0
- package/packages/shared/src/markup-repair/types.ts +13 -0
- package/packages/shared/src/md-table-cell.ts +11 -0
- package/packages/shared/src/mdns-discovery.ts +392 -42
- package/packages/shared/src/models-json-reader.ts +6 -0
- package/packages/shared/src/openspec-design-evidence.ts +25 -1
- package/packages/shared/src/openspec-poller.ts +210 -9
- package/packages/shared/src/openspec-status-all.ts +393 -0
- package/packages/shared/src/operate-required.ts +29 -0
- package/packages/shared/src/pairing-knock-client.ts +261 -0
- package/packages/shared/src/pairing-knock.ts +71 -0
- package/packages/shared/src/pairing-refusal.ts +32 -0
- package/packages/shared/src/path-containment-guard-name.ts +8 -0
- package/packages/shared/src/pi-settings-keys.ts +9 -0
- package/packages/shared/src/pi-settings-schema.ts +285 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +200 -0
- package/packages/shared/src/pi-trust-mirror-types.ts +57 -0
- package/packages/shared/src/pi-trust-mirror.ts +328 -0
- package/packages/shared/src/platform/binary-lookup.ts +2 -194
- package/packages/shared/src/platform/git.ts +204 -4
- package/packages/shared/src/platform/native-ts-hooks.mjs +93 -40
- package/packages/shared/src/platform/native-ts-register.mjs +22 -10
- package/packages/shared/src/platform/native-ts-tier.d.mts +1 -0
- package/packages/shared/src/platform/native-ts-tier.mjs +17 -0
- package/packages/shared/src/platform/node-spawn.ts +1 -2
- package/packages/shared/src/platform/openspec.ts +129 -31
- package/packages/shared/src/platform/process-scan.ts +199 -1
- package/packages/shared/src/platform/process.ts +51 -0
- package/packages/shared/src/platform/runner.ts +46 -2
- package/packages/shared/src/platform/spawn-mechanism.ts +14 -2
- package/packages/shared/src/platform/ts-loader-select.d.mts +1 -7
- package/packages/shared/src/platform/ts-loader-select.mjs +14 -43
- package/packages/shared/src/plugin-bridge-register.ts +24 -1
- package/packages/shared/src/project-name.ts +42 -0
- package/packages/shared/src/prompt-answer.ts +183 -0
- package/packages/shared/src/protocol.ts +133 -8
- package/packages/shared/src/question-types.ts +73 -0
- package/packages/shared/src/recommended-extensions.ts +80 -23
- package/packages/shared/src/recovery-timing.ts +12 -0
- package/packages/shared/src/redaction.ts +50 -0
- package/packages/shared/src/rest-api.ts +484 -1
- package/packages/shared/src/role-config-disk.ts +23 -0
- package/packages/shared/src/role-schema.ts +79 -0
- package/packages/shared/src/route-tiers.ts +127 -0
- package/packages/shared/src/runtime-overlay/materialize-plugins.d.mts +17 -0
- package/packages/shared/src/runtime-overlay/materialize-plugins.mjs +119 -8
- package/packages/shared/src/server-identity.ts +51 -15
- package/packages/shared/src/server-launcher.ts +12 -33
- package/packages/shared/src/server-pinning.ts +81 -0
- package/packages/shared/src/services/offers.ts +114 -0
- package/packages/shared/src/services/schema.ts +628 -0
- package/packages/shared/src/services/status.ts +169 -0
- package/packages/shared/src/session-meta.ts +59 -2
- package/packages/shared/src/session-name.ts +8 -0
- package/packages/shared/src/skill-block-parser.ts +14 -0
- package/packages/shared/src/source-matching.ts +5 -1
- package/packages/shared/src/state-replay.ts +19 -0
- package/packages/shared/src/terminal-safe.ts +24 -0
- package/packages/shared/src/tls-status.ts +69 -0
- package/packages/shared/src/tool-path-resolve.ts +243 -0
- package/packages/shared/src/tool-registry/definitions.ts +26 -4
- package/packages/shared/src/tool-registry/index.ts +2 -2
- package/packages/shared/src/tool-registry/pi-tools.ts +32 -3
- package/packages/shared/src/tunnel-provider.ts +3 -2
- package/packages/shared/src/types.ts +104 -9
- package/packages/shared/src/vault-delivery.ts +73 -0
- package/packages/shared/src/vault-origin.ts +51 -0
- package/packages/shared/src/vault-project-key.ts +53 -0
- package/packages/shared/src/worktree-init-hook.ts +69 -0
- package/packages/shared/src/ws-message-tiers.ts +135 -0
- package/tsconfig.base.json +1 -0
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
import {
|
|
26
26
|
type AcquireHooks,
|
|
27
27
|
acquireOrAttach,
|
|
28
|
+
isInstanceUnresponsiveError,
|
|
28
29
|
isLockDisabled,
|
|
29
30
|
type LockMetadata,
|
|
30
31
|
} from "./home-lock.js";
|
|
@@ -39,6 +40,8 @@ export interface EstablishRendezvousConfig {
|
|
|
39
40
|
version: string;
|
|
40
41
|
/** This instance's rendezvous id (see `instance-id.ts`). */
|
|
41
42
|
identity: string;
|
|
43
|
+
/** Host the server binds; recorded in the record when specific and non-loopback. */
|
|
44
|
+
bindHost?: string | null;
|
|
42
45
|
hooks?: AcquireHooks;
|
|
43
46
|
/** Test seam: skip signal-handler installation. Default `true`. */
|
|
44
47
|
installHandlers?: boolean;
|
|
@@ -84,21 +87,45 @@ export async function establishHomeRendezvous(
|
|
|
84
87
|
};
|
|
85
88
|
}
|
|
86
89
|
|
|
90
|
+
let mode: "acquired" | "attach" = "attach";
|
|
91
|
+
let meta: LockMetadata | null = null;
|
|
92
|
+
let release: (() => Promise<void>) | undefined;
|
|
93
|
+
let uninstall: (() => void) | undefined;
|
|
94
|
+
let timer: NodeJS.Timeout | undefined;
|
|
95
|
+
let checking = false;
|
|
96
|
+
|
|
97
|
+
const startPromotionTimer = () => {
|
|
98
|
+
if (intervalMs <= 0 || timer) return;
|
|
99
|
+
timer = setInterval(() => void checkNow(), intervalMs);
|
|
100
|
+
timer.unref?.();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A successor took our lock over (this process was frozen or starved, and
|
|
105
|
+
* noticed late). The lock and the record are the successor's now: stop acting
|
|
106
|
+
* as the HOME default and fall back to attach semantics. The handle already
|
|
107
|
+
* made its own release a no-op.
|
|
108
|
+
*/
|
|
109
|
+
const dropRole = () => {
|
|
110
|
+
if (mode !== "acquired") return;
|
|
111
|
+
log(`[home-lock] lost the rendezvous for this HOME to a successor (${config.identity}); continuing in attach mode.`);
|
|
112
|
+
mode = "attach";
|
|
113
|
+
release = undefined;
|
|
114
|
+
uninstall?.();
|
|
115
|
+
uninstall = undefined;
|
|
116
|
+
startPromotionTimer();
|
|
117
|
+
};
|
|
118
|
+
|
|
87
119
|
const acquireConfig = {
|
|
88
120
|
httpPort: config.httpPort,
|
|
89
121
|
piPort: config.piPort,
|
|
90
122
|
version: config.version,
|
|
91
123
|
identity: config.identity,
|
|
124
|
+
bindHost: config.bindHost,
|
|
92
125
|
hooks: config.hooks,
|
|
126
|
+
onCompromised: dropRole,
|
|
93
127
|
};
|
|
94
128
|
|
|
95
|
-
let mode: "acquired" | "attach" = "attach";
|
|
96
|
-
let meta: LockMetadata | null = null;
|
|
97
|
-
let release: (() => Promise<void>) | undefined;
|
|
98
|
-
let uninstall: (() => void) | undefined;
|
|
99
|
-
let timer: NodeJS.Timeout | undefined;
|
|
100
|
-
let checking = false;
|
|
101
|
-
|
|
102
129
|
const adopt = (result: Awaited<ReturnType<typeof acquireOrAttach>>) => {
|
|
103
130
|
mode = result.mode;
|
|
104
131
|
meta = result.meta;
|
|
@@ -130,6 +157,12 @@ export async function establishHomeRendezvous(
|
|
|
130
157
|
log(`[home-lock] promoted to rendezvous owner for this HOME (${config.identity}).`);
|
|
131
158
|
}
|
|
132
159
|
} catch (err) {
|
|
160
|
+
// An alive-but-silent (or still starting) owner is never taken over; stay
|
|
161
|
+
// attached and look again on the next tick.
|
|
162
|
+
if (isInstanceUnresponsiveError(err)) {
|
|
163
|
+
log(`[home-lock] owner is ${err.reason === "starting" ? "still starting" : "not responding"}; staying attached, retrying next tick.`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
133
166
|
// A mismatch means somebody else holds the HOME on our port; staying in
|
|
134
167
|
// attach mode is the conservative answer, not crashing a running server.
|
|
135
168
|
log(`[home-lock] promotion re-check failed: ${(err as Error).message ?? err}`);
|
|
@@ -138,10 +171,7 @@ export async function establishHomeRendezvous(
|
|
|
138
171
|
}
|
|
139
172
|
};
|
|
140
173
|
|
|
141
|
-
if (mode === "attach"
|
|
142
|
-
timer = setInterval(() => void checkNow(), intervalMs);
|
|
143
|
-
timer.unref?.();
|
|
144
|
-
}
|
|
174
|
+
if (mode === "attach") startPromotionTimer();
|
|
145
175
|
|
|
146
176
|
return {
|
|
147
177
|
get mode() {
|
|
@@ -18,10 +18,25 @@ import { fileURLToPath } from "node:url";
|
|
|
18
18
|
import path from "node:path";
|
|
19
19
|
import os from "node:os";
|
|
20
20
|
import fs from "node:fs";
|
|
21
|
+
import crypto from "node:crypto";
|
|
21
22
|
|
|
22
23
|
export interface RecoveryInfo {
|
|
23
24
|
/** Port to bind. */
|
|
24
25
|
port: number;
|
|
26
|
+
/**
|
|
27
|
+
* Address to bind. Defaults to loopback (`127.0.0.1`): the recovery server is
|
|
28
|
+
* unauthenticated and runs `npm install`, so it must never listen on all
|
|
29
|
+
* interfaces unless the operator explicitly configured the host.
|
|
30
|
+
* See change: harden-server-capability-bounds (B9).
|
|
31
|
+
*/
|
|
32
|
+
host?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The local IPC token (`~/.pi/dashboard/local/token`), when one exists. A caller
|
|
35
|
+
* presenting it in `X-Pi-Local-Token` may run the mutating actions from a
|
|
36
|
+
* non-loopback address. Passed in (not read here) to keep this module
|
|
37
|
+
* node-built-in only.
|
|
38
|
+
*/
|
|
39
|
+
localToken?: string | null;
|
|
25
40
|
/** The original error that prevented startup. */
|
|
26
41
|
error: Error;
|
|
27
42
|
/** Optional: extracted missing module identifier. */
|
|
@@ -30,6 +45,58 @@ export interface RecoveryInfo {
|
|
|
30
45
|
suggestedFix?: string;
|
|
31
46
|
}
|
|
32
47
|
|
|
48
|
+
/** Header carrying the local IPC token (mirrors `LOCAL_TOKEN_HEADER` in `auth/local-token.ts`). */
|
|
49
|
+
const LOCAL_TOKEN_HEADER = "x-pi-local-token";
|
|
50
|
+
|
|
51
|
+
const isLoopbackAddress = (addr: string | undefined): boolean =>
|
|
52
|
+
!!addr && (addr === "::1" || addr === "127.0.0.1" || addr.startsWith("127.") || addr.startsWith("::ffff:127."));
|
|
53
|
+
|
|
54
|
+
/** Hostname part of a `Host` header / Origin URL (no port, no IPv6 brackets). */
|
|
55
|
+
const hostnameOf = (hostPort: string): string => {
|
|
56
|
+
const h = hostPort.trim().toLowerCase();
|
|
57
|
+
if (h.startsWith("[")) return h.slice(1, h.indexOf("]"));
|
|
58
|
+
return h.split(":")[0] ?? "";
|
|
59
|
+
};
|
|
60
|
+
const isLoopbackName = (name: string): boolean => name === "localhost" || name === "::1" || name.startsWith("127.");
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gate for the mutating recovery actions (`retry`, `reinstall`). The recovery
|
|
64
|
+
* server has no auth of its own, so a mutating request is accepted only when it
|
|
65
|
+
* (a) presents the local IPC token, or (b) comes from a loopback peer WITH a
|
|
66
|
+
* loopback `Host` and no cross-site `Origin`. The Host/Origin checks stop a
|
|
67
|
+
* browser page (CSRF), DNS rebinding, and a tunnel forwarding from loopback —
|
|
68
|
+
* all of which reach the socket from 127.0.0.1.
|
|
69
|
+
* See change: harden-server-capability-bounds (B9).
|
|
70
|
+
*/
|
|
71
|
+
export function isRecoveryActionAllowed(
|
|
72
|
+
req: { remoteAddress: string | undefined; headers: Record<string, string | string[] | undefined> },
|
|
73
|
+
expectedToken: string | null | undefined,
|
|
74
|
+
): boolean {
|
|
75
|
+
const first = (v: string | string[] | undefined): string | undefined => (Array.isArray(v) ? v[0] : v);
|
|
76
|
+
|
|
77
|
+
const presented = first(req.headers[LOCAL_TOKEN_HEADER]);
|
|
78
|
+
if (expectedToken && presented) {
|
|
79
|
+
const a = Buffer.from(presented);
|
|
80
|
+
const b = Buffer.from(expectedToken);
|
|
81
|
+
if (a.length === b.length && crypto.timingSafeEqual(a, b)) return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!isLoopbackAddress(req.remoteAddress)) return false;
|
|
85
|
+
const host = first(req.headers.host);
|
|
86
|
+
if (!host || !isLoopbackName(hostnameOf(host))) return false;
|
|
87
|
+
const origin = first(req.headers.origin);
|
|
88
|
+
if (origin !== undefined) {
|
|
89
|
+
let originHost: string;
|
|
90
|
+
try {
|
|
91
|
+
originHost = new URL(origin).hostname.replace(/^\[|\]$/g, "").toLowerCase();
|
|
92
|
+
} catch {
|
|
93
|
+
return false; // includes the opaque `Origin: null`
|
|
94
|
+
}
|
|
95
|
+
if (!isLoopbackName(originHost)) return false;
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
33
100
|
/**
|
|
34
101
|
* Extract the missing-module identifier from an `ERR_MODULE_NOT_FOUND` or
|
|
35
102
|
* legacy `MODULE_NOT_FOUND` error. Returns null if the error isn't of that
|
|
@@ -343,6 +410,15 @@ export async function startRecoveryServer(info: RecoveryInfo): Promise<number> {
|
|
|
343
410
|
);
|
|
344
411
|
return;
|
|
345
412
|
}
|
|
413
|
+
if (
|
|
414
|
+
req.method === "POST" &&
|
|
415
|
+
(url === "/api/recovery/retry" || url === "/api/recovery/reinstall") &&
|
|
416
|
+
!isRecoveryActionAllowed({ remoteAddress: req.socket.remoteAddress, headers: req.headers }, info.localToken)
|
|
417
|
+
) {
|
|
418
|
+
res.writeHead(403, { "content-type": "text/plain" });
|
|
419
|
+
res.end("Recovery actions are only accepted from this machine (loopback) or with the local token.");
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
346
422
|
if (req.method === "POST" && url === "/api/recovery/retry") {
|
|
347
423
|
// Respawn ourselves detached, then exit.
|
|
348
424
|
try {
|
|
@@ -412,7 +488,7 @@ export async function startRecoveryServer(info: RecoveryInfo): Promise<number> {
|
|
|
412
488
|
}
|
|
413
489
|
reject(err);
|
|
414
490
|
});
|
|
415
|
-
server.listen(info.port, () => {
|
|
491
|
+
server.listen(info.port, info.host ?? "127.0.0.1", () => {
|
|
416
492
|
const addr = server.address();
|
|
417
493
|
const bound = typeof addr === "object" && addr ? addr.port : info.port;
|
|
418
494
|
console.error(`[recovery] listening on http://localhost:${bound}`);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* or PID state. Every failure means "not owned".
|
|
5
5
|
* See change: fix-cli-stop-foreign-home-kill.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { getLockPath, getMetaPath, type LockMetadata, readMetadata } from "./home-lock.js";
|
|
8
8
|
import { peekInstanceId } from "./instance-id.js";
|
|
9
9
|
|
|
10
10
|
const HEALTH_PROBE_TIMEOUT_MS = 2000;
|
|
@@ -47,10 +47,26 @@ const defaultDeps: OwnershipDeps = {
|
|
|
47
47
|
fetchHealth: (url) => fetch(url, { signal: AbortSignal.timeout(HEALTH_PROBE_TIMEOUT_MS) }),
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
+
/** Raw `/api/health` fields the stop path reads. */
|
|
51
|
+
type HealthBody = {
|
|
52
|
+
pid?: unknown;
|
|
53
|
+
instanceId?: unknown;
|
|
54
|
+
launchSource?: unknown;
|
|
55
|
+
bootParentAlive?: unknown;
|
|
56
|
+
bootParentPid?: unknown;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** Owner facts copied from a health reply (typed, optional). */
|
|
60
|
+
export interface OwnerFacts {
|
|
61
|
+
launchSource?: string;
|
|
62
|
+
bootParentAlive?: boolean;
|
|
63
|
+
bootParentPid?: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
50
66
|
async function probeHealth(
|
|
51
67
|
url: string,
|
|
52
68
|
fetchHealth: OwnershipDeps["fetchHealth"],
|
|
53
|
-
): Promise<
|
|
69
|
+
): Promise<HealthBody | null> {
|
|
54
70
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
55
71
|
try {
|
|
56
72
|
const timeout = new Promise<null>((resolve) => {
|
|
@@ -60,7 +76,7 @@ async function probeHealth(
|
|
|
60
76
|
const res = await fetchHealth(url);
|
|
61
77
|
if (!res.ok) return null;
|
|
62
78
|
const body = await res.json();
|
|
63
|
-
return body && typeof body === "object" ? (body as
|
|
79
|
+
return body && typeof body === "object" ? (body as HealthBody) : null;
|
|
64
80
|
})();
|
|
65
81
|
attempt.catch(() => {}); // a late rejection after the timeout must not be unhandled
|
|
66
82
|
return await Promise.race([attempt, timeout]);
|
|
@@ -71,18 +87,79 @@ async function probeHealth(
|
|
|
71
87
|
}
|
|
72
88
|
}
|
|
73
89
|
|
|
74
|
-
/**
|
|
75
|
-
|
|
90
|
+
/**
|
|
91
|
+
* The lock-metadata half of ownership proof: the recorded pid, when the record
|
|
92
|
+
* is for `port`. Shared by `stop` (port sweep) and the `start` / `status` port
|
|
93
|
+
* classifier, so the three can never disagree about the same process.
|
|
94
|
+
* See change: harden-dashboard-start-detection (D2).
|
|
95
|
+
*/
|
|
96
|
+
export function lockOwnedPid(meta: LockMetadata | null, port: number): number | null {
|
|
97
|
+
return meta && Number.isInteger(meta.pid) && meta.httpPort === port ? meta.pid : null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The instance-id half of ownership proof: the health reply carries this HOME's id. */
|
|
101
|
+
export function healthMatchesInstance(
|
|
102
|
+
health: { pid?: unknown; instanceId?: unknown } | null,
|
|
103
|
+
localInstanceId: string | null,
|
|
104
|
+
): health is { pid: number; instanceId: string } {
|
|
105
|
+
return (
|
|
106
|
+
!!localInstanceId &&
|
|
107
|
+
!!health &&
|
|
108
|
+
health.instanceId === localInstanceId &&
|
|
109
|
+
typeof health.pid === "number" &&
|
|
110
|
+
Number.isInteger(health.pid)
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Does the current HOME provably own the dashboard that just answered with
|
|
116
|
+
* `health`? Owned iff the health reply carries this HOME's instance id, OR the
|
|
117
|
+
* lock metadata records the replying pid for this port. `server.pid` is never
|
|
118
|
+
* proof (PIDs are reused across HOMEs). Pure.
|
|
119
|
+
*/
|
|
120
|
+
export function isOwnedHealth(
|
|
121
|
+
health: { pid?: unknown; instanceId?: unknown },
|
|
122
|
+
evidence: { meta: LockMetadata | null; localInstanceId: string | null; port: number },
|
|
123
|
+
): boolean {
|
|
124
|
+
if (healthMatchesInstance(health, evidence.localInstanceId)) return true;
|
|
125
|
+
const lockPid = lockOwnedPid(evidence.meta, evidence.port);
|
|
126
|
+
return lockPid !== null && health.pid === lockPid;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* What the current HOME can prove it owns, plus the one pid whose ownership
|
|
131
|
+
* rests on a health reply (instance-id match) with that reply's owner facts.
|
|
132
|
+
* `healthProven` is null whenever no health proof exists (no local instance
|
|
133
|
+
* id, unreachable, or lock-metadata-only ownership). The probe runs only when a
|
|
134
|
+
* local instance id exists, so there is no added latency otherwise.
|
|
135
|
+
* See change: fix-cli-control-electron-server (design D3).
|
|
136
|
+
*/
|
|
137
|
+
export interface Ownership {
|
|
138
|
+
owned: Set<number>;
|
|
139
|
+
healthProven: { pid: number; health: OwnerFacts } | null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function ownerFacts(body: HealthBody): OwnerFacts {
|
|
143
|
+
return {
|
|
144
|
+
...(typeof body.launchSource === "string" ? { launchSource: body.launchSource } : {}),
|
|
145
|
+
...(typeof body.bootParentAlive === "boolean" ? { bootParentAlive: body.bootParentAlive } : {}),
|
|
146
|
+
...(typeof body.bootParentPid === "number" ? { bootParentPid: body.bootParentPid } : {}),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Owned pids (lock metadata + instance-id match) and the health-proven pid. Empty when `config.port <= 0`. */
|
|
151
|
+
export async function collectOwnership(
|
|
76
152
|
config: OwnershipConfig,
|
|
77
153
|
deps: Partial<OwnershipDeps> = {},
|
|
78
|
-
): Promise<
|
|
154
|
+
): Promise<Ownership> {
|
|
79
155
|
const d = { ...defaultDeps, ...deps };
|
|
80
156
|
const owned = new Set<number>();
|
|
81
|
-
|
|
157
|
+
let healthProven: Ownership["healthProven"] = null;
|
|
158
|
+
if (!(config.port > 0)) return { owned, healthProven };
|
|
82
159
|
|
|
83
160
|
try {
|
|
84
|
-
const
|
|
85
|
-
if (
|
|
161
|
+
const lockPid = lockOwnedPid(d.readLockMeta(), config.port);
|
|
162
|
+
if (lockPid !== null) owned.add(lockPid);
|
|
86
163
|
} catch {
|
|
87
164
|
/* unreadable sidecar → no proof */
|
|
88
165
|
}
|
|
@@ -94,14 +171,23 @@ export async function collectOwnedPids(
|
|
|
94
171
|
`http://${healthHost(config.host)}:${config.port}/api/health`,
|
|
95
172
|
d.fetchHealth,
|
|
96
173
|
);
|
|
97
|
-
if (health
|
|
174
|
+
if (healthMatchesInstance(health, localId)) {
|
|
98
175
|
owned.add(health.pid);
|
|
176
|
+
healthProven = { pid: health.pid, health: ownerFacts(health) };
|
|
99
177
|
}
|
|
100
178
|
}
|
|
101
179
|
} catch {
|
|
102
180
|
/* no proof */
|
|
103
181
|
}
|
|
104
|
-
return owned;
|
|
182
|
+
return { owned, healthProven };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Pids the current HOME can prove it owns. Empty when `config.port <= 0`. */
|
|
186
|
+
export async function collectOwnedPids(
|
|
187
|
+
config: OwnershipConfig,
|
|
188
|
+
deps: Partial<OwnershipDeps> = {},
|
|
189
|
+
): Promise<Set<number>> {
|
|
190
|
+
return (await collectOwnership(config, deps)).owned;
|
|
105
191
|
}
|
|
106
192
|
|
|
107
193
|
/** Split holders (pid → ports) into owned vs foreign. Pure. */
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-preview CAPABILITY PATH for `/live/<id>/`.
|
|
3
|
+
*
|
|
4
|
+
* The preview frame is sandboxed without `allow-same-origin` (opaque origin), so
|
|
5
|
+
* its subresource, `fetch` and WebSocket requests carry no dashboard cookie (a
|
|
6
|
+
* real-browser spike confirmed: only the frame navigation carried one). The only
|
|
7
|
+
* same-origin credential such a document can hold is one in its own URL. So an
|
|
8
|
+
* authenticated `control`-tier caller mints `/live/<id>/~c/<token>/`; the token
|
|
9
|
+
* admits ONLY that preview, is stored server-side as a hash, expires after 30
|
|
10
|
+
* minutes idle / 12 hours absolute, and dies with its target, its minting
|
|
11
|
+
* device/session, or a restart.
|
|
12
|
+
*
|
|
13
|
+
* Matching is segment-exact on the RAW pathname: no dot-segment and no
|
|
14
|
+
* percent-encoded `/`, `~` or `.` anywhere, no query-string tricks, and a
|
|
15
|
+
* trailing segment separator is required after the token.
|
|
16
|
+
*
|
|
17
|
+
* See change: retire-trusted-networks-bypass (design D5).
|
|
18
|
+
*/
|
|
19
|
+
import crypto from "node:crypto";
|
|
20
|
+
import type { Principal } from "@blackbelt-technology/pi-dashboard-shared/identity.js";
|
|
21
|
+
import type { Tier } from "@blackbelt-technology/pi-dashboard-shared/tiers.js";
|
|
22
|
+
import type { TokenPayload } from "../auth/auth.js";
|
|
23
|
+
|
|
24
|
+
// Lifetime note: a capability's life is bounded by its own idle/absolute limits and
|
|
25
|
+
// by revocation (target removed, device revoked, logout / passkey revoke, restart).
|
|
26
|
+
// It is deliberately independent of the minting session JWT's natural `exp`.
|
|
27
|
+
export const CAPABILITY_IDLE_MS = 30 * 60_000;
|
|
28
|
+
export const CAPABILITY_MAX_MS = 12 * 60 * 60_000;
|
|
29
|
+
/** Hard cap on live tokens (oldest dropped): a loop minting tokens cannot grow memory. */
|
|
30
|
+
const MAX_TOKENS = 1024;
|
|
31
|
+
/** Live tokens per minting principal: one caller's mint loop evicts only ITS OWN oldest tokens. */
|
|
32
|
+
const MAX_TOKENS_PER_PRINCIPAL = 64;
|
|
33
|
+
/** `/~c/<token>` anywhere in a path or `Referer`. Tokens are base64url. */
|
|
34
|
+
const CAPABILITY_SEGMENT = /\/~c\/[A-Za-z0-9_-]+/g;
|
|
35
|
+
const TOKEN_SHAPE = /^[A-Za-z0-9_-]{22,}$/;
|
|
36
|
+
|
|
37
|
+
/** Who minted a capability — the identity and tier an accepted request carries. */
|
|
38
|
+
export interface CapabilityPrincipal {
|
|
39
|
+
/**
|
|
40
|
+
* `device` (paired device), `session` (login/passkey session), `local` (genuine
|
|
41
|
+
* same-host caller), `network` (admitted by network trust only — `legacy` mode;
|
|
42
|
+
* dies when the mode becomes `pair`).
|
|
43
|
+
*/
|
|
44
|
+
kind: "device" | "session" | "local" | "network";
|
|
45
|
+
/** Tier the minter held at mint time (absent for `local`). */
|
|
46
|
+
tier?: Tier;
|
|
47
|
+
deviceId?: string;
|
|
48
|
+
/** The verified session payload (re-checked for liveness at every use). */
|
|
49
|
+
session?: TokenPayload;
|
|
50
|
+
/** sha256 of the session cookie value — lets logout revoke exactly this session's tokens. */
|
|
51
|
+
sessionKey?: string;
|
|
52
|
+
/** Identity-plane principal, when the minter had one (satisfies the identity floor). */
|
|
53
|
+
identity?: Principal;
|
|
54
|
+
identityExpiresAt?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface Entry {
|
|
58
|
+
targetId: string;
|
|
59
|
+
principal: CapabilityPrincipal;
|
|
60
|
+
mintedAt: number;
|
|
61
|
+
lastUsedAt: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface LiveCapabilityStoreOptions {
|
|
65
|
+
now?: () => number;
|
|
66
|
+
/** Is the minting principal still valid (device not revoked, session alive, identity unexpired)? */
|
|
67
|
+
isPrincipalLive?: (principal: CapabilityPrincipal) => boolean;
|
|
68
|
+
/** Is the target still registered? */
|
|
69
|
+
isTargetLive?: (targetId: string) => boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Stable identity of a minting principal (device id, session key, else the shared "local"/"network" bucket). */
|
|
73
|
+
const principalKey = (p: CapabilityPrincipal): string =>
|
|
74
|
+
p.deviceId ? `device:${p.deviceId}` : p.sessionKey ? `session:${p.sessionKey}` : p.kind;
|
|
75
|
+
|
|
76
|
+
const hash = (token: string): string => crypto.createHash("sha256").update(token).digest("hex");
|
|
77
|
+
|
|
78
|
+
/** sha256 of a session cookie value, for {@link CapabilityPrincipal.sessionKey}. */
|
|
79
|
+
export function sessionKeyOf(cookieValue: string): string {
|
|
80
|
+
return hash(cookieValue);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class LiveCapabilityStore {
|
|
84
|
+
private readonly entries = new Map<string, Entry>();
|
|
85
|
+
private readonly now: () => number;
|
|
86
|
+
private readonly isPrincipalLive: (p: CapabilityPrincipal) => boolean;
|
|
87
|
+
private readonly isTargetLive: (id: string) => boolean;
|
|
88
|
+
|
|
89
|
+
constructor(opts: LiveCapabilityStoreOptions = {}) {
|
|
90
|
+
this.now = opts.now ?? Date.now;
|
|
91
|
+
this.isPrincipalLive = opts.isPrincipalLive ?? (() => true);
|
|
92
|
+
this.isTargetLive = opts.isTargetLive ?? (() => true);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Mint a token for `targetId`. Only its hash is kept. */
|
|
96
|
+
mint(targetId: string, principal: CapabilityPrincipal): { token: string; expiresAt: number } {
|
|
97
|
+
const now = this.now();
|
|
98
|
+
this.sweep(now);
|
|
99
|
+
const token = crypto.randomBytes(32).toString("base64url");
|
|
100
|
+
this.entries.set(hash(token), { targetId, principal, mintedAt: now, lastUsedAt: now });
|
|
101
|
+
// Evict within the minting principal first, so a mint loop cannot push out
|
|
102
|
+
// another user's active preview tokens.
|
|
103
|
+
const who = principalKey(principal);
|
|
104
|
+
let mine = [...this.entries].filter(([, e]) => principalKey(e.principal) === who);
|
|
105
|
+
while (mine.length > MAX_TOKENS_PER_PRINCIPAL) {
|
|
106
|
+
this.entries.delete(mine[0][0]);
|
|
107
|
+
mine = mine.slice(1);
|
|
108
|
+
}
|
|
109
|
+
while (this.entries.size > MAX_TOKENS) {
|
|
110
|
+
const oldest = this.entries.keys().next().value;
|
|
111
|
+
if (oldest === undefined) break;
|
|
112
|
+
this.entries.delete(oldest);
|
|
113
|
+
}
|
|
114
|
+
return { token, expiresAt: now + CAPABILITY_MAX_MS };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Validate `token` for `targetId`. A hit extends the idle expiry. A missing,
|
|
119
|
+
* unknown, wrong-target, expired or no-longer-live token returns `null` — the
|
|
120
|
+
* caller cannot tell which.
|
|
121
|
+
*/
|
|
122
|
+
validate(targetId: string, token: string): CapabilityPrincipal | null {
|
|
123
|
+
if (!TOKEN_SHAPE.test(token)) return null;
|
|
124
|
+
const key = hash(token);
|
|
125
|
+
const entry = this.entries.get(key);
|
|
126
|
+
if (!entry || entry.targetId !== targetId) return null;
|
|
127
|
+
const now = this.now();
|
|
128
|
+
if (now - entry.lastUsedAt >= CAPABILITY_IDLE_MS || now - entry.mintedAt >= CAPABILITY_MAX_MS) {
|
|
129
|
+
this.entries.delete(key);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
if (!this.isTargetLive(entry.targetId) || !this.isPrincipalLive(entry.principal)) {
|
|
133
|
+
this.entries.delete(key);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
entry.lastUsedAt = now;
|
|
137
|
+
return entry.principal;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
revokeByTarget(targetId: string): void {
|
|
141
|
+
for (const [key, e] of this.entries) if (e.targetId === targetId) this.entries.delete(key);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
revokeByDevice(deviceId: string): void {
|
|
145
|
+
for (const [key, e] of this.entries) if (e.principal.deviceId === deviceId) this.entries.delete(key);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
revokeBySessionKey(sessionKey: string): void {
|
|
149
|
+
for (const [key, e] of this.entries) if (e.principal.sessionKey === sessionKey) this.entries.delete(key);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Count of live tokens (tests, diagnostics). Never exposes a token or its hash. */
|
|
153
|
+
size(): number {
|
|
154
|
+
return this.entries.size;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** True when no stored key equals the plaintext token (hash-only storage check). */
|
|
158
|
+
holdsPlaintext(token: string): boolean {
|
|
159
|
+
return this.entries.has(token);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private sweep(now: number): void {
|
|
163
|
+
for (const [key, e] of this.entries) {
|
|
164
|
+
if (now - e.lastUsedAt >= CAPABILITY_IDLE_MS || now - e.mintedAt >= CAPABILITY_MAX_MS) this.entries.delete(key);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A parsed capability URL. `rest` starts with `/` (or is `/`); `prefix` is `/live/<id>/~c/<token>`. */
|
|
170
|
+
export interface CapabilityPath {
|
|
171
|
+
targetId: string;
|
|
172
|
+
token: string;
|
|
173
|
+
prefix: string;
|
|
174
|
+
/** Path (and query) the upstream should see, always beginning with `/`. */
|
|
175
|
+
rest: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Parse `/live/<id>/~c/<token>/<rest…>` from a raw request URL, or `null`.
|
|
180
|
+
*
|
|
181
|
+
* Compared segment by segment on the RAW pathname (never dot-resolved), and any
|
|
182
|
+
* dot-segment or percent-encoded `/`, `~` or `.` anywhere in the pathname makes
|
|
183
|
+
* it not a capability path. The token must be followed by `/`.
|
|
184
|
+
*/
|
|
185
|
+
export function parseCapabilityPath(url: string | undefined): CapabilityPath | null {
|
|
186
|
+
if (typeof url !== "string" || !url.startsWith("/")) return null;
|
|
187
|
+
let end = url.length;
|
|
188
|
+
for (const sep of ["?", "#"] as const) {
|
|
189
|
+
const i = url.indexOf(sep);
|
|
190
|
+
if (i >= 0 && i < end) end = i;
|
|
191
|
+
}
|
|
192
|
+
const pathname = url.slice(0, end);
|
|
193
|
+
const query = url.slice(end);
|
|
194
|
+
if (/%2f|%7e|%2e|%5c/i.test(pathname) || pathname.includes("\\")) return null;
|
|
195
|
+
const segments = pathname.split("/");
|
|
196
|
+
// ["", "live", id, "~c", token, <at least one more: the separator>]
|
|
197
|
+
if (segments.length < 6) return null;
|
|
198
|
+
if (segments[0] !== "" || segments[1] !== "live" || segments[3] !== "~c") return null;
|
|
199
|
+
const [, , targetId, , token] = segments;
|
|
200
|
+
if (!targetId || !TOKEN_SHAPE.test(token)) return null;
|
|
201
|
+
if (segments.some((s) => s === "." || s === "..")) return null;
|
|
202
|
+
const prefix = `/live/${targetId}/~c/${token}`;
|
|
203
|
+
const rest = pathname.slice(prefix.length) + query;
|
|
204
|
+
return { targetId, token, prefix, rest: rest.startsWith("/") ? rest : `/${rest}` };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Replace any `/~c/<token>` in a log field / path with a redaction marker. */
|
|
208
|
+
export function redactCapability(text: string): string {
|
|
209
|
+
return text.replace(CAPABILITY_SEGMENT, "/~c/<redacted>");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Remove any `/~c/<token>` segment (forwarded `Referer`). */
|
|
213
|
+
export function stripCapabilitySegment(text: string): string {
|
|
214
|
+
return text.replace(CAPABILITY_SEGMENT, "");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The slice of a Fastify request the capability hook sets. */
|
|
218
|
+
interface CapabilityRequestState {
|
|
219
|
+
isAuthenticated?: boolean;
|
|
220
|
+
authVia?: string;
|
|
221
|
+
principalTier?: Tier;
|
|
222
|
+
principalDeviceId?: string;
|
|
223
|
+
principal?: Principal | null;
|
|
224
|
+
principalExpiresAt?: number | null;
|
|
225
|
+
liveCapabilityPrefix?: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* `onRequest` hook: authenticate a request whose RAW path carries a valid
|
|
230
|
+
* capability, for that prefix only. Registered BEFORE the bearer / OAuth /
|
|
231
|
+
* identity hooks (they would otherwise refuse a cookie-less preview request),
|
|
232
|
+
* and ahead of the universal guard. A request without a valid capability is
|
|
233
|
+
* left untouched — the normal admission rules decide, and cannot tell a bad
|
|
234
|
+
* token from none.
|
|
235
|
+
*/
|
|
236
|
+
export function createLiveCapabilityHook(store: LiveCapabilityStore) {
|
|
237
|
+
return async function liveCapabilityHook(request: { url: string }): Promise<void> {
|
|
238
|
+
if (!request.url.startsWith("/live/")) return;
|
|
239
|
+
const cap = parseCapabilityPath(request.url);
|
|
240
|
+
if (!cap) return;
|
|
241
|
+
const principal = store.validate(cap.targetId, cap.token);
|
|
242
|
+
if (!principal) return;
|
|
243
|
+
const r = request as unknown as CapabilityRequestState;
|
|
244
|
+
r.isAuthenticated = true;
|
|
245
|
+
r.authVia = "live-capability";
|
|
246
|
+
if (principal.tier) r.principalTier = principal.tier;
|
|
247
|
+
if (principal.deviceId) r.principalDeviceId = principal.deviceId;
|
|
248
|
+
if (principal.identity) {
|
|
249
|
+
r.principal = principal.identity;
|
|
250
|
+
r.principalExpiresAt = principal.identityExpiresAt ?? null;
|
|
251
|
+
}
|
|
252
|
+
r.liveCapabilityPrefix = cap.prefix;
|
|
253
|
+
};
|
|
254
|
+
}
|