@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
|
@@ -1,96 +1,374 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* REST routes for known servers management and network discovery.
|
|
3
|
+
*
|
|
4
|
+
* Mutations (add / remove / accept-move) and the reachability probe are
|
|
5
|
+
* OPERATOR-only (login, local proof, local token, genuinely-local request):
|
|
6
|
+
* network trust alone and paired-device bearers are refused. GET stays on the
|
|
7
|
+
* network guard and reports `canManage`.
|
|
8
|
+
*
|
|
9
|
+
* Every known-server write goes through ONE module-level serialized queue
|
|
10
|
+
* ({@link enqueueKnownServersWrite}); identity challenges and probes run
|
|
11
|
+
* BEFORE enqueueing (a hanging target must never block unrelated writes) and
|
|
12
|
+
* each write re-reads config, re-validates, and aborts if its target entry has
|
|
13
|
+
* since been removed.
|
|
14
|
+
*
|
|
15
|
+
* See change: stabilize-mdns-server-identity (D6, D7).
|
|
3
16
|
*/
|
|
4
|
-
import
|
|
17
|
+
import dns from "node:dns";
|
|
18
|
+
import net from "node:net";
|
|
19
|
+
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
5
20
|
import type { NetworkGuard } from "./route-deps.js";
|
|
6
21
|
import type { ApiResponse } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
7
22
|
import type { KnownServer } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
8
23
|
import type { DiscoveredServer } from "@blackbelt-technology/pi-dashboard-shared/mdns-discovery.js";
|
|
9
24
|
import type {
|
|
25
|
+
AcceptKnownServerMoveRequest,
|
|
10
26
|
AddKnownServerRequest,
|
|
11
|
-
RemoveKnownServerRequest,
|
|
12
27
|
DiscoveredServerInfo,
|
|
28
|
+
KnownServerListEntry,
|
|
29
|
+
KnownServerProbeResult,
|
|
30
|
+
RemoveKnownServerRequest,
|
|
13
31
|
} from "@blackbelt-technology/pi-dashboard-shared/rest-api.js";
|
|
14
32
|
import { loadConfig } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
33
|
+
import { formatHostPort, isValidHost, normalizeHostKey } from "@blackbelt-technology/pi-dashboard-shared/host-port.js";
|
|
34
|
+
import { type ProveResult, proveServerIdentity } from "@blackbelt-technology/pi-dashboard-shared/server-pinning.js";
|
|
35
|
+
import type { HostAdmissionOptions } from "../auth/host-admission.js";
|
|
36
|
+
import type { LocalTrustContext } from "../auth/local-proof.js";
|
|
15
37
|
import { writeConfigPartial } from "../config-api.js";
|
|
38
|
+
import type { KnownServerMoves } from "../known-server-moves.js";
|
|
39
|
+
import { createOperatorGuard } from "./pairing-routes.js";
|
|
16
40
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
/** Bounds of the add-time / move identity challenge. */
|
|
42
|
+
const CHALLENGE_TIMEOUT_MS = 2000;
|
|
43
|
+
/**
|
|
44
|
+
* Probe budgets: DNS and HTTP are separate so a slow negative `.local` lookup is not a "timeout".
|
|
45
|
+
* DNS gets 6 s: measured negative `.local` answers take ~4 s in the Docker harness and
|
|
46
|
+
* ~5 s via mDNSResponder, so a 3 s budget would label every dead `.local` name "timeout".
|
|
47
|
+
*/
|
|
48
|
+
const PROBE_DNS_TIMEOUT_MS = 6000;
|
|
49
|
+
const PROBE_HTTP_TIMEOUT_MS = 2000;
|
|
50
|
+
|
|
51
|
+
let writeChain: Promise<unknown> = Promise.resolve();
|
|
52
|
+
|
|
53
|
+
/** The single serialized queue for known-server config writes. */
|
|
54
|
+
function enqueueKnownServersWrite<T>(fn: () => T | Promise<T>): Promise<T> {
|
|
55
|
+
const run = writeChain.then(fn, fn);
|
|
56
|
+
writeChain = run.catch(() => undefined);
|
|
57
|
+
return run;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface KnownServersRouteDeps {
|
|
61
|
+
networkGuard: NetworkGuard;
|
|
62
|
+
getPeerServers: () => Map<string, DiscoveredServer>;
|
|
63
|
+
/** `X-Pi-Local-Token` expected value, when configured. */
|
|
64
|
+
localToken?: string;
|
|
65
|
+
localTrust?: LocalTrustContext;
|
|
66
|
+
hostAdmission: () => HostAdmissionOptions;
|
|
67
|
+
/** Identity-challenge seam (default: {@link proveServerIdentity}, 2 s). */
|
|
68
|
+
prove?: (baseUrl: string) => Promise<ProveResult>;
|
|
69
|
+
/** Name-resolution seam for the probe. */
|
|
70
|
+
lookup?: (host: string) => Promise<unknown>;
|
|
71
|
+
fetchImpl?: typeof fetch;
|
|
72
|
+
moves?: KnownServerMoves;
|
|
73
|
+
log?: (message: string) => void;
|
|
74
|
+
/** Budget overrides (tests shrink them; production uses the defaults). */
|
|
75
|
+
timeouts?: { challengeMs?: number; dnsMs?: number; httpMs?: number };
|
|
76
|
+
}
|
|
35
77
|
|
|
36
|
-
|
|
78
|
+
const sameAddress = (a: { host: string; port: number }, b: { host: string; port: number }) =>
|
|
79
|
+
a.port === b.port && normalizeHostKey(a.host) === normalizeHostKey(b.host);
|
|
80
|
+
|
|
81
|
+
const short = (fingerprint: string) => fingerprint.replace(/^sha256:/, "").slice(0, 16);
|
|
82
|
+
|
|
83
|
+
/** Reject after `ms`, clearing its timer when the race settles. */
|
|
84
|
+
function withTimeout<T>(p: Promise<T>, ms: number, onTimeout: () => T): Promise<T> {
|
|
85
|
+
return new Promise<T>((resolve, reject) => {
|
|
86
|
+
const timer = setTimeout(() => resolve(onTimeout()), ms);
|
|
87
|
+
p.then(
|
|
88
|
+
(v) => {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
resolve(v);
|
|
91
|
+
},
|
|
92
|
+
(e) => {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
reject(e);
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function registerKnownServersRoutes(fastify: FastifyInstance, deps: KnownServersRouteDeps) {
|
|
101
|
+
const { networkGuard, getPeerServers, moves } = deps;
|
|
102
|
+
const log = deps.log ?? ((m: string) => console.log(m));
|
|
103
|
+
const operatorGuard = createOperatorGuard({
|
|
104
|
+
localToken: deps.localToken,
|
|
105
|
+
hostAdmission: deps.hostAdmission,
|
|
106
|
+
localTrust: deps.localTrust,
|
|
107
|
+
});
|
|
108
|
+
const mutationGuard = [networkGuard, operatorGuard];
|
|
109
|
+
|
|
110
|
+
/** Side-effect-free evaluation of the operator guard (same predicate as the mutations). */
|
|
111
|
+
async function isOperator(request: FastifyRequest): Promise<boolean> {
|
|
112
|
+
let denied = false;
|
|
113
|
+
const stub = {
|
|
114
|
+
code: () => stub,
|
|
115
|
+
send: () => {
|
|
116
|
+
denied = true;
|
|
117
|
+
return stub;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
await operatorGuard(request, stub as unknown as FastifyReply);
|
|
121
|
+
return !denied;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const challengeMs = deps.timeouts?.challengeMs ?? CHALLENGE_TIMEOUT_MS;
|
|
125
|
+
const dnsMs = deps.timeouts?.dnsMs ?? PROBE_DNS_TIMEOUT_MS;
|
|
126
|
+
const httpMs = deps.timeouts?.httpMs ?? PROBE_HTTP_TIMEOUT_MS;
|
|
127
|
+
const proveRaw = deps.prove ?? ((baseUrl: string) => proveServerIdentity(baseUrl, { timeoutMs: challengeMs }));
|
|
128
|
+
/** Bounded identity challenge: a hanging target cannot hold the request. */
|
|
129
|
+
const prove = (host: string, port: number): Promise<ProveResult> =>
|
|
130
|
+
withTimeout(proveRaw(`http://${formatHostPort(host, port)}`), challengeMs, () => ({
|
|
131
|
+
refused: "challenge timed out",
|
|
132
|
+
}));
|
|
133
|
+
const provedFingerprint = (r: ProveResult): string | undefined => ("proven" in r ? r.proven : undefined);
|
|
134
|
+
|
|
135
|
+
const readList = (): KnownServer[] => loadConfig().knownServers.map((e) => ({ ...e }));
|
|
136
|
+
const writeList = (list: KnownServer[]): { success: boolean; error?: string } =>
|
|
137
|
+
writeConfigPartial({ knownServers: list });
|
|
138
|
+
|
|
139
|
+
// ── List ─────────────────────────────────────────────────────────────
|
|
140
|
+
fastify.get("/api/known-servers", { preHandler: networkGuard }, async (request) => {
|
|
141
|
+
const data: KnownServerListEntry[] = readList().map((entry) => {
|
|
142
|
+
const movedTo = moves?.suggestionFor(entry);
|
|
143
|
+
return movedTo ? { ...entry, movedTo } : entry;
|
|
144
|
+
});
|
|
145
|
+
return { success: true, data, canManage: await isOperator(request) };
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// ── Add / update ─────────────────────────────────────────────────────
|
|
37
149
|
fastify.post<{ Body: AddKnownServerRequest }>(
|
|
38
150
|
"/api/known-servers",
|
|
39
|
-
{ preHandler:
|
|
40
|
-
async (request): Promise<ApiResponse> => {
|
|
41
|
-
const { host, port
|
|
42
|
-
|
|
151
|
+
{ preHandler: mutationGuard },
|
|
152
|
+
async (request, reply): Promise<ApiResponse<{ updated?: boolean }> & Record<string, unknown>> => {
|
|
153
|
+
const { host, port } = request.body ?? ({} as AddKnownServerRequest);
|
|
154
|
+
const label = typeof request.body?.label === "string" ? request.body.label.trim() : "";
|
|
155
|
+
if (!host || typeof host !== "string" || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
43
156
|
return { success: false, error: "host and port are required" };
|
|
44
157
|
}
|
|
158
|
+
// The host is placed in an outbound URL (identity challenge): admit only DNS-name / IP-literal
|
|
159
|
+
// characters, so no URL-authority trick (`@`, `/`, `#`, `?`) can redirect the request.
|
|
160
|
+
if (!/^[A-Za-z0-9._:[\]-]+$/.test(host) || !isValidHost(host)) return fail(reply, 400, "invalid_host");
|
|
161
|
+
// Snapshot BEFORE the challenge: an entry that existed then but is gone when the
|
|
162
|
+
// write runs was deleted meanwhile and must not be recreated.
|
|
163
|
+
const snapshot = readList();
|
|
164
|
+
// `fingerprint` in the body is a hint only and is never persisted unless proven.
|
|
165
|
+
const proven = provedFingerprint(await prove(host, port));
|
|
166
|
+
|
|
167
|
+
// The holder of (proven, port) at ANOTHER address, and whether it still answers.
|
|
168
|
+
const target0 = snapshot.find((e) => sameAddress(e, { host, port }));
|
|
169
|
+
const holder0 = proven
|
|
170
|
+
? snapshot.find((e) => e.fingerprint === proven && e.port === port && !sameAddress(e, { host, port }))
|
|
171
|
+
: undefined;
|
|
172
|
+
const holderStillProves = holder0 && !target0 ? provedFingerprint(await prove(holder0.host, holder0.port)) === proven : false;
|
|
173
|
+
|
|
174
|
+
return enqueueKnownServersWrite((): ApiResponse<{ updated?: boolean }> & Record<string, unknown> => {
|
|
175
|
+
const list = readList();
|
|
176
|
+
const idx = list.findIndex((e) => sameAddress(e, { host, port }));
|
|
177
|
+
if (target0 && idx < 0) return fail(reply, 409, "entry_removed");
|
|
178
|
+
const target = idx >= 0 ? list[idx] : undefined;
|
|
45
179
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
180
|
+
if (target?.fingerprint && proven && proven !== target.fingerprint) {
|
|
181
|
+
reply.code(409);
|
|
182
|
+
return { success: false, error: "identity_changed", expected: target.fingerprint, proven };
|
|
183
|
+
}
|
|
49
184
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
185
|
+
const holderIdx = proven
|
|
186
|
+
? list.findIndex((e) => e.fingerprint === proven && e.port === port && !sameAddress(e, { host, port }))
|
|
187
|
+
: -1;
|
|
188
|
+
|
|
189
|
+
if (target) {
|
|
190
|
+
// Existing address: refresh label, pin when newly proven, merge a duplicate identity into it.
|
|
191
|
+
if (label) target.label = label;
|
|
192
|
+
if (proven && !target.fingerprint) {
|
|
193
|
+
target.fingerprint = proven;
|
|
194
|
+
if (holderIdx >= 0) {
|
|
195
|
+
const dropped = list.splice(holderIdx, 1)[0];
|
|
196
|
+
if (!target.label && dropped.label) target.label = dropped.label;
|
|
197
|
+
log(`[known-servers] merged duplicate identity: dropped ${dropped.host}:${dropped.port}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return persist(list, reply, false);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (holderIdx >= 0) {
|
|
204
|
+
if (holderStillProves) {
|
|
205
|
+
reply.code(409);
|
|
206
|
+
return { success: false, error: "already_known", host: list[holderIdx].host, port: list[holderIdx].port };
|
|
207
|
+
}
|
|
208
|
+
// The holder's stored address no longer proves the identity: move it here.
|
|
209
|
+
list[holderIdx] = { ...list[holderIdx], host, ...(label ? { label } : {}) };
|
|
210
|
+
return persist(list, reply, true);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
list.push({
|
|
55
214
|
host,
|
|
56
215
|
port,
|
|
57
216
|
...(label ? { label } : {}),
|
|
58
217
|
addedAt: new Date().toISOString(),
|
|
218
|
+
...(proven ? { fingerprint: proven } : {}),
|
|
59
219
|
});
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const result = writeConfigPartial({ knownServers: existing });
|
|
63
|
-
if (!result.success) {
|
|
64
|
-
return { success: false, error: result.error };
|
|
65
|
-
}
|
|
66
|
-
return { success: true };
|
|
220
|
+
return persist(list, reply, false);
|
|
221
|
+
});
|
|
67
222
|
},
|
|
68
223
|
);
|
|
69
224
|
|
|
70
|
-
|
|
225
|
+
function fail(reply: FastifyReply, status: number, error: string) {
|
|
226
|
+
reply.code(status);
|
|
227
|
+
return { success: false as const, error };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function persist(list: KnownServer[], reply: FastifyReply, updated: boolean) {
|
|
231
|
+
const result = writeList(list);
|
|
232
|
+
if (!result.success) return fail(reply, 500, result.error ?? "write failed");
|
|
233
|
+
return updated ? { success: true as const, data: { updated: true } } : { success: true as const };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ── Remove ───────────────────────────────────────────────────────────
|
|
71
237
|
fastify.delete<{ Body: RemoveKnownServerRequest }>(
|
|
72
238
|
"/api/known-servers",
|
|
73
|
-
{ preHandler:
|
|
74
|
-
async (request): Promise<ApiResponse> => {
|
|
75
|
-
const { host, port } = request.body;
|
|
239
|
+
{ preHandler: mutationGuard },
|
|
240
|
+
async (request, reply): Promise<ApiResponse> => {
|
|
241
|
+
const { host, port } = request.body ?? ({} as RemoveKnownServerRequest);
|
|
76
242
|
if (!host || typeof port !== "number") {
|
|
77
243
|
return { success: false, error: "host and port are required" };
|
|
78
244
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
245
|
+
return enqueueKnownServersWrite(() =>
|
|
246
|
+
persist(
|
|
247
|
+
readList().filter((s) => !sameAddress(s, { host, port })),
|
|
248
|
+
reply,
|
|
249
|
+
false,
|
|
250
|
+
),
|
|
83
251
|
);
|
|
252
|
+
},
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
// ── Accept a verified move suggestion ────────────────────────────────
|
|
256
|
+
fastify.post<{ Body: AcceptKnownServerMoveRequest }>(
|
|
257
|
+
"/api/known-servers/accept-move",
|
|
258
|
+
{ preHandler: mutationGuard },
|
|
259
|
+
async (request, reply): Promise<ApiResponse> => {
|
|
260
|
+
const { host, port } = request.body ?? ({} as AcceptKnownServerMoveRequest);
|
|
261
|
+
if (!host || typeof port !== "number") return fail(reply, 400, "host and port are required");
|
|
262
|
+
const entry = readList().find((e) => sameAddress(e, { host, port }));
|
|
263
|
+
const suggestion = entry && moves?.suggestionFor(entry);
|
|
264
|
+
if (!entry?.fingerprint || !suggestion) return fail(reply, 409, "no_suggestion");
|
|
265
|
+
const fingerprint = entry.fingerprint;
|
|
266
|
+
|
|
267
|
+
// Re-verify BOTH halves before touching config.
|
|
268
|
+
if (provedFingerprint(await prove(entry.host, entry.port)) === fingerprint) {
|
|
269
|
+
moves?.withdraw(entry, "stored address answers again");
|
|
270
|
+
return fail(reply, 409, "stale_suggestion");
|
|
271
|
+
}
|
|
272
|
+
if (provedFingerprint(await prove(suggestion.host, suggestion.port)) !== fingerprint) {
|
|
273
|
+
return fail(reply, 409, "stale_suggestion");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return enqueueKnownServersWrite((): ApiResponse => {
|
|
277
|
+
const list = readList();
|
|
278
|
+
const idx = list.findIndex((e) => sameAddress(e, entry) && e.fingerprint === fingerprint);
|
|
279
|
+
if (idx < 0) return fail(reply, 409, "entry_removed");
|
|
280
|
+
list[idx] = { ...list[idx], host: suggestion.host, port: suggestion.port };
|
|
281
|
+
const result = persist(list, reply, false);
|
|
282
|
+
if (result.success) {
|
|
283
|
+
moves?.withdraw(entry, "accepted");
|
|
284
|
+
log(`[known-servers] moved fp=${short(fingerprint)} ${entry.host}:${entry.port} -> ${suggestion.host}:${suggestion.port}`);
|
|
285
|
+
}
|
|
286
|
+
return result;
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
// ── Operator probe of a persisted entry ──────────────────────────────
|
|
292
|
+
const probesInFlight = new Map<string, Promise<KnownServerProbeResult>>();
|
|
293
|
+
const lookup = deps.lookup ?? ((h: string) => dns.promises.lookup(h));
|
|
294
|
+
const doFetch = deps.fetchImpl ?? fetch;
|
|
295
|
+
|
|
296
|
+
async function probeOnce(host: string, port: number): Promise<KnownServerProbeResult> {
|
|
297
|
+
const key = normalizeHostKey(host);
|
|
298
|
+
if (!net.isIP(key)) {
|
|
299
|
+
try {
|
|
300
|
+
const outcome = await withTimeout(lookup(key).then(() => "ok" as const), dnsMs, () => "timeout" as const);
|
|
301
|
+
if (outcome === "timeout") return { status: "timeout" };
|
|
302
|
+
} catch (err) {
|
|
303
|
+
const code = (err as { code?: string })?.code;
|
|
304
|
+
if (code === "ENOTFOUND" || code === "EAI_NONAME") return { status: "name-not-resolved" };
|
|
305
|
+
if (code === "EAI_AGAIN") return { status: "timeout" };
|
|
306
|
+
return { status: "error" };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const res = await doFetch(`http://${formatHostPort(host, port)}/api/health`, {
|
|
311
|
+
redirect: "manual",
|
|
312
|
+
signal: AbortSignal.timeout(httpMs),
|
|
313
|
+
});
|
|
314
|
+
if (res.status !== 200) return { status: "error" };
|
|
315
|
+
const body = (await res.json().catch(() => undefined)) as { ok?: unknown } | undefined;
|
|
316
|
+
return { status: body?.ok === true ? "reachable" : "error" };
|
|
317
|
+
} catch (err) {
|
|
318
|
+
const e = err as { name?: string; cause?: { code?: string } };
|
|
319
|
+
if (e?.name === "AbortError" || e?.name === "TimeoutError") return { status: "timeout" };
|
|
320
|
+
if (e?.cause?.code === "ECONNREFUSED") return { status: "refused" };
|
|
321
|
+
return { status: "error" };
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Pin the identity of an unpinned reachable entry (merging a duplicate holder). */
|
|
326
|
+
async function backfill(entry: KnownServer): Promise<void> {
|
|
327
|
+
if (entry.fingerprint) return;
|
|
328
|
+
const proven = provedFingerprint(await prove(entry.host, entry.port));
|
|
329
|
+
if (!proven) return;
|
|
330
|
+
await enqueueKnownServersWrite(() => {
|
|
331
|
+
const list = readList();
|
|
332
|
+
const idx = list.findIndex((e) => sameAddress(e, entry));
|
|
333
|
+
if (idx < 0 || list[idx].fingerprint) return;
|
|
334
|
+
list[idx].fingerprint = proven;
|
|
335
|
+
const dup = list.findIndex((e, i) => i !== idx && e.fingerprint === proven && e.port === entry.port);
|
|
336
|
+
if (dup >= 0) {
|
|
337
|
+
const dropped = list.splice(dup, 1)[0];
|
|
338
|
+
if (!list.find((e) => e.fingerprint === proven)?.label && dropped.label) {
|
|
339
|
+
list[list.findIndex((e) => e.fingerprint === proven)].label = dropped.label;
|
|
340
|
+
}
|
|
341
|
+
log(`[known-servers] backfill merged duplicate identity: dropped ${dropped.host}:${dropped.port}`);
|
|
342
|
+
}
|
|
343
|
+
const result = writeList(list);
|
|
344
|
+
if (!result.success) log(`[known-servers] backfill write failed: ${result.error ?? "unknown"}`);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
fastify.post<{ Body: { host?: string; port?: number } }>(
|
|
349
|
+
"/api/known-servers/probe",
|
|
350
|
+
{ preHandler: mutationGuard },
|
|
351
|
+
async (request, reply): Promise<ApiResponse<KnownServerProbeResult>> => {
|
|
352
|
+
const { host, port } = request.body ?? {};
|
|
353
|
+
if (typeof host !== "string" || typeof port !== "number") return fail(reply, 400, "host and port are required");
|
|
354
|
+
const entry = readList().find((e) => sameAddress(e, { host, port }));
|
|
355
|
+
if (!entry) return fail(reply, 403, "not_a_known_server");
|
|
84
356
|
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
357
|
+
const key = `${normalizeHostKey(entry.host)}:${entry.port}`;
|
|
358
|
+
let pending = probesInFlight.get(key);
|
|
359
|
+
if (!pending) {
|
|
360
|
+
pending = (async () => {
|
|
361
|
+
const result = await probeOnce(entry.host, entry.port);
|
|
362
|
+
if (result.status === "reachable") await backfill(entry).catch(() => {});
|
|
363
|
+
return result;
|
|
364
|
+
})().finally(() => probesInFlight.delete(key));
|
|
365
|
+
probesInFlight.set(key, pending);
|
|
88
366
|
}
|
|
89
|
-
return { success: true };
|
|
367
|
+
return { success: true, data: await pending };
|
|
90
368
|
},
|
|
91
369
|
);
|
|
92
370
|
|
|
93
|
-
// On-demand network discovery
|
|
371
|
+
// ── On-demand network discovery ──────────────────────────────────────
|
|
94
372
|
fastify.post(
|
|
95
373
|
"/api/discover-servers",
|
|
96
374
|
{ preHandler: networkGuard },
|
|
@@ -103,6 +381,8 @@ export function registerKnownServersRoutes(
|
|
|
103
381
|
version: s.version,
|
|
104
382
|
pid: s.pid,
|
|
105
383
|
isLocal: s.isLocal,
|
|
384
|
+
...(s.fingerprint ? { fingerprint: s.fingerprint } : {}),
|
|
385
|
+
...(s.name ? { name: s.name } : {}),
|
|
106
386
|
}));
|
|
107
387
|
return { success: true, data };
|
|
108
388
|
},
|
|
@@ -5,17 +5,64 @@
|
|
|
5
5
|
*
|
|
6
6
|
* See change: improve-content-editor (live-server-preview §6).
|
|
7
7
|
*/
|
|
8
|
+
|
|
9
|
+
import type { Principal } from "@blackbelt-technology/pi-dashboard-shared/identity.js";
|
|
10
|
+
import type { Tier } from "@blackbelt-technology/pi-dashboard-shared/tiers.js";
|
|
8
11
|
import type { ApiResponse } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
9
|
-
import type { FastifyInstance } from "fastify";
|
|
12
|
+
import type { FastifyInstance, FastifyRequest } from "fastify";
|
|
13
|
+
import { COOKIE_NAME, type TokenPayload } from "../auth/auth.js";
|
|
14
|
+
import { canDiscloseAccessPosture } from "../auth/localhost-guard.js";
|
|
15
|
+
import {
|
|
16
|
+
type CapabilityPrincipal,
|
|
17
|
+
type LiveCapabilityStore,
|
|
18
|
+
sessionKeyOf,
|
|
19
|
+
} from "../live-server/live-capability.js";
|
|
10
20
|
import type { LiveServerManager } from "../live-server/live-server-manager.js";
|
|
11
21
|
import type { NetworkGuard } from "./route-deps.js";
|
|
12
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Who is minting? Read from what the admission hooks already settled on the
|
|
25
|
+
* request: a device credential, a login session, an identity principal, or
|
|
26
|
+
* nothing revocable (a genuine-local caller).
|
|
27
|
+
*/
|
|
28
|
+
function capabilityPrincipalOf(request: FastifyRequest): CapabilityPrincipal {
|
|
29
|
+
const r = request as {
|
|
30
|
+
authVia?: string;
|
|
31
|
+
principalTier?: Tier;
|
|
32
|
+
principalDeviceId?: string;
|
|
33
|
+
sessionPayload?: TokenPayload;
|
|
34
|
+
principal?: Principal | null;
|
|
35
|
+
principalExpiresAt?: number | null;
|
|
36
|
+
cookies?: Record<string, string | undefined>;
|
|
37
|
+
};
|
|
38
|
+
const identity = r.principal ?? undefined;
|
|
39
|
+
const identityFields = identity
|
|
40
|
+
? { identity, ...(typeof r.principalExpiresAt === "number" ? { identityExpiresAt: r.principalExpiresAt } : {}) }
|
|
41
|
+
: {};
|
|
42
|
+
if (r.authVia === "device") {
|
|
43
|
+
return { kind: "device", tier: r.principalTier, deviceId: r.principalDeviceId, ...identityFields };
|
|
44
|
+
}
|
|
45
|
+
if (r.authVia === "session") {
|
|
46
|
+
const cookie = r.cookies?.[COOKIE_NAME];
|
|
47
|
+
return {
|
|
48
|
+
kind: "session",
|
|
49
|
+
tier: r.principalTier,
|
|
50
|
+
session: r.sessionPayload,
|
|
51
|
+
...(cookie ? { sessionKey: sessionKeyOf(cookie) } : {}),
|
|
52
|
+
...identityFields,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// No credential: a genuine same-host caller, or one admitted by network trust
|
|
56
|
+
// alone (`legacy`). The latter's tokens must not outlive a switch to `pair`.
|
|
57
|
+
return { kind: canDiscloseAccessPosture(request) ? "local" : "network", ...identityFields };
|
|
58
|
+
}
|
|
59
|
+
|
|
13
60
|
export function registerLiveServerRoutes(
|
|
14
61
|
fastify: FastifyInstance,
|
|
15
62
|
liveServerManager: LiveServerManager,
|
|
16
|
-
deps: { networkGuard: NetworkGuard },
|
|
63
|
+
deps: { networkGuard: NetworkGuard; capabilities?: LiveCapabilityStore },
|
|
17
64
|
) {
|
|
18
|
-
const { networkGuard } = deps;
|
|
65
|
+
const { networkGuard, capabilities } = deps;
|
|
19
66
|
|
|
20
67
|
// Register (or reuse) a loopback dev-server target. Returns the proxied path.
|
|
21
68
|
fastify.post<{ Body: { host?: unknown; port?: unknown; label?: unknown } }>(
|
|
@@ -40,13 +87,34 @@ export function registerLiveServerRoutes(
|
|
|
40
87
|
},
|
|
41
88
|
);
|
|
42
89
|
|
|
43
|
-
// Remove a target from the allowlist.
|
|
90
|
+
// Remove a target from the allowlist. Its capability tokens die with it.
|
|
44
91
|
fastify.delete<{ Params: { id: string } }>(
|
|
45
92
|
"/api/live-server/:id",
|
|
46
93
|
{ preHandler: networkGuard },
|
|
47
94
|
async (request) => {
|
|
48
95
|
liveServerManager.remove(request.params.id);
|
|
96
|
+
capabilities?.revokeByTarget(request.params.id);
|
|
49
97
|
return { success: true } satisfies ApiResponse;
|
|
50
98
|
},
|
|
51
99
|
);
|
|
100
|
+
|
|
101
|
+
// Mint a per-preview capability path (`control` tier via the route-tier map).
|
|
102
|
+
// The preview frame is sandboxed (opaque origin) and carries no cookie, so the
|
|
103
|
+
// viewer frames this path instead. Unknown target → 404, no token created.
|
|
104
|
+
// See change: retire-trusted-networks-bypass (D5).
|
|
105
|
+
if (capabilities) {
|
|
106
|
+
fastify.post<{ Params: { id: string } }>(
|
|
107
|
+
"/api/live-server/:id/capability",
|
|
108
|
+
{ preHandler: networkGuard },
|
|
109
|
+
async (request, reply) => {
|
|
110
|
+
const target = liveServerManager.get(request.params.id);
|
|
111
|
+
if (!target) {
|
|
112
|
+
reply.code(404);
|
|
113
|
+
return { success: false, error: "live-server target not found" } satisfies ApiResponse;
|
|
114
|
+
}
|
|
115
|
+
const { token, expiresAt } = capabilities.mint(target.id, capabilityPrincipalOf(request));
|
|
116
|
+
return { success: true, path: `/live/${target.id}/~c/${token}/`, expiresAt };
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
}
|
|
52
120
|
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Local-proof bootstrap routes (always registered; strict mode or not).
|
|
3
3
|
*
|
|
4
|
-
* `POST /api/local-proof` — admitted ONLY by `X-Pi-Local-
|
|
4
|
+
* `POST /api/local-proof` — admitted ONLY by a verified local credential (`X-Pi-Local-Proof`, or the legacy raw token); mints a one-time
|
|
5
5
|
* code (60 s TTL, single use).
|
|
6
6
|
* `GET /auth/local-proof` — redeems the code, sets the httpOnly `pi_dash_local`
|
|
7
|
-
* proof cookie, redirects to
|
|
7
|
+
* proof cookie, redirects to `sanitizeReturnUrl(next)`
|
|
8
|
+
* (same-origin path, else `/`).
|
|
8
9
|
*
|
|
9
10
|
* Used by `pi-dashboard open` and Electron so the same-desktop browser can prove
|
|
10
11
|
* it is local under `requireLocalProof` and can approve pairings (D6).
|
|
11
|
-
* See change: harden-trust-and-credential-boundaries (D2/D6).
|
|
12
|
+
* See change: harden-trust-and-credential-boundaries (D2/D6); fix-pairing-approval-credential (D6).
|
|
12
13
|
*/
|
|
13
14
|
import type { FastifyInstance } from "fastify";
|
|
15
|
+
import { sanitizeReturnUrl } from "../auth/auth-plugin.js";
|
|
16
|
+
import { hasLocalCredential } from "../auth/local-credential.js";
|
|
14
17
|
import {
|
|
15
18
|
LOCAL_PROOF_COOKIE,
|
|
16
19
|
LOCAL_PROOF_MAX_AGE_S,
|
|
@@ -18,14 +21,13 @@ import {
|
|
|
18
21
|
type LocalTrustContext,
|
|
19
22
|
signLocalProof,
|
|
20
23
|
} from "../auth/local-proof.js";
|
|
21
|
-
import { verifyLocalToken } from "../auth/local-token.js";
|
|
22
24
|
|
|
23
25
|
export function registerLocalProofRoutes(
|
|
24
26
|
fastify: FastifyInstance,
|
|
25
27
|
deps: { codes: LocalProofCodeStore; ctx: LocalTrustContext },
|
|
26
28
|
): void {
|
|
27
29
|
fastify.post("/api/local-proof", async (request, reply) => {
|
|
28
|
-
if (!
|
|
30
|
+
if (!hasLocalCredential(request.headers)) {
|
|
29
31
|
reply.code(401);
|
|
30
32
|
return { success: false as const, error: "local token required" };
|
|
31
33
|
}
|
|
@@ -33,7 +35,8 @@ export function registerLocalProofRoutes(
|
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
fastify.get("/auth/local-proof", async (request, reply) => {
|
|
36
|
-
const
|
|
38
|
+
const query = (request.query as { code?: unknown; next?: unknown } | undefined) ?? {};
|
|
39
|
+
const code = query.code;
|
|
37
40
|
if (!deps.codes.redeem(code)) {
|
|
38
41
|
reply.code(403);
|
|
39
42
|
return { success: false as const, error: "invalid_or_expired_code" };
|
|
@@ -44,6 +47,8 @@ export function registerLocalProofRoutes(
|
|
|
44
47
|
sameSite: "strict",
|
|
45
48
|
maxAge: LOCAL_PROOF_MAX_AGE_S,
|
|
46
49
|
});
|
|
47
|
-
return
|
|
50
|
+
// `next` is the one same-origin return-path predicate shared with the OAuth
|
|
51
|
+
// `return` param: anything off-origin, with control chars, or non-string → `/`.
|
|
52
|
+
return reply.redirect(sanitizeReturnUrl(query.next));
|
|
48
53
|
});
|
|
49
54
|
}
|