@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
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OCI driver (D4) — ONE module for docker and podman; runtime quirks isolated.
|
|
3
|
+
*
|
|
4
|
+
* - Every runtime call runs with `DOCKER_CONFIG` (and podman's
|
|
5
|
+
* `REGISTRY_AUTH_FILE`) pointing at an empty dashboard-owned dir, so a broken
|
|
6
|
+
* `credHelpers` entry in `~/.docker/config.json` can never break a local start.
|
|
7
|
+
* - Presence order: binary → engine (`info`) → image. The image is inspected
|
|
8
|
+
* only after `info` succeeded, so "engine down" is never reported as
|
|
9
|
+
* `image-absent`. Nothing is ever pulled.
|
|
10
|
+
* - Create: `--init`, labels `pi.service` / `pi.owner` / `pi.def-hash`, ports
|
|
11
|
+
* `127.0.0.1::<p>`, secrets as `:ro` file mounts (never `-e`/`--env-file`),
|
|
12
|
+
* `--restart no`, retained after stop (no `--rm`).
|
|
13
|
+
* - Host ports are re-read from `inspect` after every start.
|
|
14
|
+
* - Stop is confirmed by `inspect` showing the container no longer running.
|
|
15
|
+
* - Host VMs (Docker Desktop, podman machine) are reported, never started or
|
|
16
|
+
* stopped.
|
|
17
|
+
* See change: add-service-registry-core.
|
|
18
|
+
*/
|
|
19
|
+
import fs from "node:fs";
|
|
20
|
+
import net from "node:net";
|
|
21
|
+
import { spawn } from "@blackbelt-technology/pi-dashboard-shared/platform/exec.js";
|
|
22
|
+
import { isProcessAlive, killProcess } from "@blackbelt-technology/pi-dashboard-shared/platform/process.js";
|
|
23
|
+
import { readProcessCommandLine } from "@blackbelt-technology/pi-dashboard-shared/platform/process-scan.js";
|
|
24
|
+
import type { DriverName, ServiceDefinition } from "@blackbelt-technology/pi-dashboard-shared/services/schema.js";
|
|
25
|
+
import type { VolumeOutcome } from "@blackbelt-technology/pi-dashboard-shared/services/status.js";
|
|
26
|
+
import type { CommandRunner, RunResult } from "./command-runner.js";
|
|
27
|
+
import type {
|
|
28
|
+
AdoptResult,
|
|
29
|
+
DriverInstance,
|
|
30
|
+
DriverLogs,
|
|
31
|
+
LogsRequest,
|
|
32
|
+
OwnedListing,
|
|
33
|
+
PresenceResult,
|
|
34
|
+
ResetDataOutcome,
|
|
35
|
+
ServiceDriver,
|
|
36
|
+
StartContext,
|
|
37
|
+
StopOutcome,
|
|
38
|
+
} from "./driver.js";
|
|
39
|
+
import { mergeTimestamped } from "./log-text.js";
|
|
40
|
+
import { ensurePrivateDir, readJsonFile, type ServicesPaths, writePrivateFile } from "./paths.js";
|
|
41
|
+
import { allocateLoopbackPort, loopbackEndpoint, type PortAllocator } from "./ports.js";
|
|
42
|
+
import { secretPathEnv, writeSecretMounts } from "./secret-delivery.js";
|
|
43
|
+
|
|
44
|
+
type OciRuntime = "docker" | "podman";
|
|
45
|
+
|
|
46
|
+
export interface OciDriverDeps {
|
|
47
|
+
runtime: OciRuntime;
|
|
48
|
+
run: CommandRunner;
|
|
49
|
+
resolveBinary: (name: string) => string | null;
|
|
50
|
+
paths: ServicesPaths;
|
|
51
|
+
spawn?: typeof spawn;
|
|
52
|
+
killProcess?: typeof killProcess;
|
|
53
|
+
allocatePort?: PortAllocator;
|
|
54
|
+
now?: () => number;
|
|
55
|
+
sleep?: (ms: number) => Promise<void>;
|
|
56
|
+
/** Engine-reachability cache window. Default 30 s. */
|
|
57
|
+
cacheMs?: number;
|
|
58
|
+
/** Wait until a local forward accepts connections (default: TCP poll, 5 s). */
|
|
59
|
+
waitForForward?: (port: number) => Promise<boolean>;
|
|
60
|
+
isProcessAlive?: (pid: number) => boolean;
|
|
61
|
+
readCommandLine?: (pid: number) => Promise<string | null>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Poll a loopback port until it accepts a TCP connection. */
|
|
65
|
+
async function defaultWaitForForward(port: number, timeoutMs = 5_000): Promise<boolean> {
|
|
66
|
+
const deadline = Date.now() + timeoutMs;
|
|
67
|
+
while (Date.now() < deadline) {
|
|
68
|
+
const ok = await new Promise<boolean>((resolve) => {
|
|
69
|
+
const s = net.connect({ host: "127.0.0.1", port });
|
|
70
|
+
const done = (v: boolean) => {
|
|
71
|
+
s.destroy();
|
|
72
|
+
resolve(v);
|
|
73
|
+
};
|
|
74
|
+
s.setTimeout(500, () => done(false));
|
|
75
|
+
s.once("connect", () => done(true));
|
|
76
|
+
s.once("error", () => done(false));
|
|
77
|
+
});
|
|
78
|
+
if (ok) return true;
|
|
79
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The subset of `inspect` this driver reads (identical on docker and podman). */
|
|
85
|
+
export interface ContainerInspect {
|
|
86
|
+
Id: string;
|
|
87
|
+
Config?: { Labels?: Record<string, string> | null };
|
|
88
|
+
State?: { Running?: boolean; Health?: { Status?: string } };
|
|
89
|
+
NetworkSettings?: { Ports?: Record<string, Array<{ HostIp?: string; HostPort?: string }> | null> | null };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const LABEL_SERVICE = "pi.service";
|
|
93
|
+
const LABEL_OWNER = "pi.owner";
|
|
94
|
+
const LABEL_DEF_HASH = "pi.def-hash";
|
|
95
|
+
const STOP_CONFIRM_MARGIN_MS = 5_000;
|
|
96
|
+
|
|
97
|
+
export class OciDriver implements ServiceDriver {
|
|
98
|
+
private readonly deps: OciDriverDeps;
|
|
99
|
+
readonly name: DriverName;
|
|
100
|
+
private engineCache?: { at: number; result: PresenceResult };
|
|
101
|
+
|
|
102
|
+
constructor(deps: OciDriverDeps) {
|
|
103
|
+
this.deps = deps;
|
|
104
|
+
this.name = `oci:${deps.runtime}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private now() {
|
|
108
|
+
return (this.deps.now ?? Date.now)();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private sleep(ms: number) {
|
|
112
|
+
return (this.deps.sleep ?? ((m) => new Promise<void>((r) => setTimeout(r, m))))(ms);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Env for every runtime invocation: isolated, empty registry auth. */
|
|
116
|
+
runtimeEnv(): NodeJS.ProcessEnv {
|
|
117
|
+
const dir = this.deps.paths.emptyDockerConfig;
|
|
118
|
+
ensurePrivateDir(dir);
|
|
119
|
+
return { ...process.env, DOCKER_CONFIG: dir, REGISTRY_AUTH_FILE: `${dir}/auth.json` };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private bin(): string | null {
|
|
123
|
+
return this.deps.resolveBinary(this.deps.runtime);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private exec(args: string[], timeoutMs = 30_000): Promise<RunResult> {
|
|
127
|
+
const bin = this.bin();
|
|
128
|
+
if (!bin) return Promise.resolve({ code: null, stdout: "", stderr: "", timedOut: false, error: "runtime-missing" });
|
|
129
|
+
return this.deps.run(bin, args, { env: this.runtimeEnv(), timeoutMs });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── presence ──────────────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
/** Steps 1–2 (binary, engine), cached for 30 s. */
|
|
135
|
+
async engine(): Promise<PresenceResult> {
|
|
136
|
+
const c = this.engineCache;
|
|
137
|
+
if (c && this.now() - c.at < (this.deps.cacheMs ?? 30_000)) return c.result;
|
|
138
|
+
const result = await this.probeEngine();
|
|
139
|
+
this.engineCache = { at: this.now(), result };
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private async probeEngine(): Promise<PresenceResult> {
|
|
144
|
+
if (!this.bin()) return { ok: false, reason: "runtime-missing", hint: `${this.deps.runtime} is not installed` };
|
|
145
|
+
const info = await this.exec(["info", "--format", "{{json .}}"], 15_000);
|
|
146
|
+
if (info.code !== 0) {
|
|
147
|
+
if (await this.hostVmStopped()) {
|
|
148
|
+
return {
|
|
149
|
+
ok: false,
|
|
150
|
+
reason: "host-vm-stopped",
|
|
151
|
+
hint:
|
|
152
|
+
this.deps.runtime === "podman"
|
|
153
|
+
? "the podman machine is stopped — start it yourself (podman machine start); the dashboard never does"
|
|
154
|
+
: "Docker Desktop is stopped — start it yourself; the dashboard never does",
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
return { ok: false, reason: "runtime-unreachable", hint: `${this.deps.runtime} engine is not reachable` };
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const parsed = JSON.parse(info.stdout) as { OSType?: string; host?: { os?: string } };
|
|
161
|
+
const os = parsed.OSType ?? parsed.host?.os;
|
|
162
|
+
if (os && os.toLowerCase() === "windows") {
|
|
163
|
+
return { ok: false, reason: "unsupported-platform", hint: "Windows-container mode is not supported; switch to Linux containers" };
|
|
164
|
+
}
|
|
165
|
+
} catch {
|
|
166
|
+
/* unparseable info is still a reachable engine */
|
|
167
|
+
}
|
|
168
|
+
return { ok: true };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Host-VM state, report-only. Unknown / unparseable → false (degrades to runtime-unreachable). */
|
|
172
|
+
private async hostVmStopped(): Promise<boolean> {
|
|
173
|
+
if (this.deps.runtime === "podman") {
|
|
174
|
+
const r = await this.exec(["machine", "list", "--format", "json"], 10_000);
|
|
175
|
+
if (r.code !== 0) return false;
|
|
176
|
+
try {
|
|
177
|
+
const machines = JSON.parse(r.stdout) as Array<{ Running?: boolean }>;
|
|
178
|
+
return Array.isArray(machines) && machines.length > 0 && machines.every((m) => m.Running !== true);
|
|
179
|
+
} catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// `docker desktop status` is UNVERIFIED (design D4): parse defensively.
|
|
184
|
+
const r = await this.exec(["desktop", "status", "--format", "json"], 10_000);
|
|
185
|
+
if (r.code !== 0) return false;
|
|
186
|
+
try {
|
|
187
|
+
const s = JSON.parse(r.stdout) as { Status?: string; status?: string };
|
|
188
|
+
return String(s.Status ?? s.status ?? "").toLowerCase() === "stopped";
|
|
189
|
+
} catch {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async presence(def: ServiceDefinition): Promise<PresenceResult> {
|
|
195
|
+
const engine = await this.engine();
|
|
196
|
+
if (!engine.ok) return engine;
|
|
197
|
+
const image = def.oci?.image ?? "";
|
|
198
|
+
const r = await this.exec(["image", "inspect", image], 15_000);
|
|
199
|
+
if (r.code !== 0) {
|
|
200
|
+
return { ok: false, reason: "image-absent", hint: `image ${image} is not present locally — the dashboard never pulls; run: ${this.deps.runtime} pull ${image}` };
|
|
201
|
+
}
|
|
202
|
+
return { ok: true };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
canStart(): boolean {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
canStop(): boolean {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ── inspect helpers ───────────────────────────────────────────────────────
|
|
214
|
+
|
|
215
|
+
/** `inspect` that tells "failed" (null) from "nothing there" ([]). */
|
|
216
|
+
async inspectOrNull(ids: string[]): Promise<ContainerInspect[] | null> {
|
|
217
|
+
if (ids.length === 0) return [];
|
|
218
|
+
const r = await this.exec(["inspect", ...ids], 15_000);
|
|
219
|
+
if (r.code !== 0 && r.stdout.trim() === "") return null;
|
|
220
|
+
try {
|
|
221
|
+
const parsed = JSON.parse(r.stdout) as ContainerInspect[];
|
|
222
|
+
return Array.isArray(parsed) ? parsed : null;
|
|
223
|
+
} catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async inspect(ids: string[]): Promise<ContainerInspect[]> {
|
|
229
|
+
return (await this.inspectOrNull(ids)) ?? [];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* `ps -a -q` with label filters → full inspect records. `null` when the
|
|
234
|
+
* containers cannot be listed OR inspected — never a failed query read as
|
|
235
|
+
* "no containers".
|
|
236
|
+
*/
|
|
237
|
+
async listContainers(filters: string[]): Promise<ContainerInspect[] | null> {
|
|
238
|
+
const args = ["ps", "-a", "-q", "--no-trunc", ...filters.flatMap((f) => ["--filter", `label=${f}`])];
|
|
239
|
+
const r = await this.exec(args, 15_000);
|
|
240
|
+
if (r.code !== 0) return null;
|
|
241
|
+
const ids = r.stdout.split(/\s+/).filter(Boolean);
|
|
242
|
+
return this.inspectOrNull(ids);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Containers carrying BOTH `pi.service=<id>` and `pi.owner=<instanceId>` (labels re-checked client-side). */
|
|
246
|
+
private async ownedContainers(id: string, instanceId: string): Promise<ContainerInspect[] | null> {
|
|
247
|
+
const all = await this.listContainers([`${LABEL_SERVICE}=${id}`, `${LABEL_OWNER}=${instanceId}`]);
|
|
248
|
+
if (all === null) return null;
|
|
249
|
+
return all.filter((c) => c.Config?.Labels?.[LABEL_SERVICE] === id && c.Config?.Labels?.[LABEL_OWNER] === instanceId);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
endpointsOf(def: ServiceDefinition, c: ContainerInspect): Record<string, string> {
|
|
253
|
+
const out: Record<string, string> = {};
|
|
254
|
+
const ports = c.NetworkSettings?.Ports ?? {};
|
|
255
|
+
for (const [name, spec] of Object.entries(def.oci?.ports ?? {})) {
|
|
256
|
+
const binding = ports[`${spec.container}/tcp`]?.[0];
|
|
257
|
+
const hostPort = Number(binding?.HostPort);
|
|
258
|
+
if (Number.isInteger(hostPort) && hostPort > 0) out[name] = loopbackEndpoint(spec.protocol, hostPort);
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private instanceOf(def: ServiceDefinition, c: ContainerInspect): DriverInstance {
|
|
264
|
+
return {
|
|
265
|
+
driver: this.name,
|
|
266
|
+
startedBy: "dashboard",
|
|
267
|
+
containerId: c.Id,
|
|
268
|
+
endpoints: this.endpointsOf(def, c),
|
|
269
|
+
defHash: c.Config?.Labels?.[LABEL_DEF_HASH],
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Classify the containers labelled `pi.service=<id>` (D4 adopt rules). A
|
|
275
|
+
* different `pi.owner` wins over everything: never create beside it.
|
|
276
|
+
*/
|
|
277
|
+
classify(def: ServiceDefinition, all: ContainerInspect[], instanceId: string): AdoptResult {
|
|
278
|
+
const forService = all.filter((c) => c.Config?.Labels?.[LABEL_SERVICE] === def.id);
|
|
279
|
+
const mine = forService.filter((c) => c.Config?.Labels?.[LABEL_OWNER] === instanceId);
|
|
280
|
+
if (forService.length > mine.length) {
|
|
281
|
+
return {
|
|
282
|
+
kind: "unavailable",
|
|
283
|
+
reason: "owner-conflict",
|
|
284
|
+
hint: `containers labelled pi.service=${def.id} belong to another dashboard install (pi.owner differs); remove or re-label them`,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
if (mine.length > 1) {
|
|
288
|
+
return {
|
|
289
|
+
kind: "unavailable",
|
|
290
|
+
reason: "duplicate-instances",
|
|
291
|
+
hint: `${mine.length} containers match ${def.id}: ${mine.map((c) => c.Id.slice(0, 12)).join(", ")} — stop/remove acts on all`,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
if (mine.length === 0) return { kind: "none" };
|
|
295
|
+
return mine[0].State?.Running ? { kind: "running", instance: this.instanceOf(def, mine[0]) } : { kind: "stopped" };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async adopt(def: ServiceDefinition, instanceId: string): Promise<AdoptResult> {
|
|
299
|
+
if (!this.bin()) return { kind: "none" };
|
|
300
|
+
const all = await this.listContainers([`${LABEL_SERVICE}=${def.id}`]);
|
|
301
|
+
if (all === null) return { kind: "none" };
|
|
302
|
+
return this.classify(def, all, instanceId);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Boot adoption for many definitions with ONE `ps -a` (+ one `inspect`). */
|
|
306
|
+
async adoptAll(defs: ServiceDefinition[], instanceId: string): Promise<Map<string, AdoptResult>> {
|
|
307
|
+
const out = new Map<string, AdoptResult>();
|
|
308
|
+
if (defs.length === 0 || !this.bin()) return out;
|
|
309
|
+
const all = await this.listContainers([LABEL_SERVICE]);
|
|
310
|
+
if (all === null) return out;
|
|
311
|
+
for (const def of defs) out.set(def.id, this.classify(def, all, instanceId));
|
|
312
|
+
return out;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ── lifecycle ─────────────────────────────────────────────────────────────
|
|
316
|
+
|
|
317
|
+
createArgs(def: ServiceDefinition, ctx: StartContext, mounts: Array<{ host: string; container: string }>): string[] {
|
|
318
|
+
const oci = def.oci!;
|
|
319
|
+
const args = ["create"];
|
|
320
|
+
if (oci.init !== false) args.push("--init");
|
|
321
|
+
args.push("--restart", "no");
|
|
322
|
+
args.push("--label", `${LABEL_SERVICE}=${def.id}`, "--label", `${LABEL_OWNER}=${ctx.instanceId}`, "--label", `${LABEL_DEF_HASH}=${ctx.defHash}`);
|
|
323
|
+
for (const spec of Object.values(oci.ports)) args.push("-p", `127.0.0.1::${spec.container}`);
|
|
324
|
+
for (const [vol, target] of Object.entries(oci.volumes ?? {})) args.push("-v", `${vol}:${target}`);
|
|
325
|
+
for (const [host, target] of Object.entries(oci.binds ?? {})) args.push("-v", `${host}:${target}`);
|
|
326
|
+
for (const m of mounts) args.push("-v", `${m.host}:${m.container}:ro`);
|
|
327
|
+
// Non-secret env only: literal values and secret MOUNT PATHS, never values.
|
|
328
|
+
for (const [k, v] of Object.entries({ ...(oci.env ?? {}), ...secretPathEnv(def) })) args.push("-e", `${k}=${v}`);
|
|
329
|
+
args.push(oci.image, ...(oci.command ?? []));
|
|
330
|
+
return args;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async start(def: ServiceDefinition, ctx: StartContext): Promise<DriverInstance> {
|
|
334
|
+
const existing = (await this.listContainers([`${LABEL_SERVICE}=${def.id}`, `${LABEL_OWNER}=${ctx.instanceId}`])) ?? [];
|
|
335
|
+
const mounts = writeSecretMounts(this.deps.paths.secretsDir(def.id), ctx.secrets);
|
|
336
|
+
let id: string | undefined;
|
|
337
|
+
const reusable = existing.length === 1 && existing[0].Config?.Labels?.[LABEL_DEF_HASH] === ctx.defHash;
|
|
338
|
+
if (reusable) {
|
|
339
|
+
id = existing[0].Id;
|
|
340
|
+
} else {
|
|
341
|
+
// Definition changed (or duplicates): recreate. Named volumes survive `rm`.
|
|
342
|
+
if (existing.length > 0) await this.exec(["rm", "-f", ...existing.map((c) => c.Id)], 60_000);
|
|
343
|
+
const created = await this.exec(this.createArgs(def, ctx, mounts), 120_000);
|
|
344
|
+
if (created.code !== 0) throw new Error(`${this.deps.runtime} create failed: ${firstLine(created)}`);
|
|
345
|
+
id = created.stdout.trim().split(/\s+/).pop();
|
|
346
|
+
}
|
|
347
|
+
if (!id) throw new Error(`${this.deps.runtime} create returned no container id`);
|
|
348
|
+
const started = await this.exec(["start", id], 120_000);
|
|
349
|
+
if (started.code !== 0) throw new Error(`${this.deps.runtime} start failed: ${firstLine(started)}`);
|
|
350
|
+
// Re-read host ports after EVERY start: the runtime may reassign them.
|
|
351
|
+
const [c] = await this.inspect([id]);
|
|
352
|
+
if (!c) throw new Error(`${this.deps.runtime} inspect returned nothing for ${id}`);
|
|
353
|
+
return this.instanceOf(def, c);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async isAlive(_def: ServiceDefinition, inst: DriverInstance): Promise<boolean> {
|
|
357
|
+
if (!inst.containerId) return false;
|
|
358
|
+
const [c] = await this.inspect([inst.containerId]);
|
|
359
|
+
return c?.State?.Running === true;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async stop(
|
|
363
|
+
def: ServiceDefinition,
|
|
364
|
+
inst: DriverInstance | undefined,
|
|
365
|
+
stopTimeoutMs: number,
|
|
366
|
+
opts: { force?: boolean; instanceId?: string } = {},
|
|
367
|
+
): Promise<StopOutcome> {
|
|
368
|
+
let ids: string[];
|
|
369
|
+
if (inst?.containerId) ids = [inst.containerId];
|
|
370
|
+
// Without a recorded instance act only on containers THIS installation owns:
|
|
371
|
+
// `pi.service` alone could stop another dashboard's container.
|
|
372
|
+
else ids = opts.instanceId ? ((await this.ownedContainers(def.id, opts.instanceId)) ?? []).map((c) => c.Id) : [];
|
|
373
|
+
await this.closeTunnel(def, inst?.tunnelPid);
|
|
374
|
+
if (ids.length === 0) return "stopped";
|
|
375
|
+
const secs = Math.max(1, Math.round(stopTimeoutMs / 1000));
|
|
376
|
+
await this.exec(["stop", "-t", String(secs), ...ids], stopTimeoutMs + 15_000);
|
|
377
|
+
const deadline = this.now() + STOP_CONFIRM_MARGIN_MS;
|
|
378
|
+
for (;;) {
|
|
379
|
+
const running = (await this.inspect(ids)).filter((c) => c.State?.Running === true);
|
|
380
|
+
if (running.length === 0) return "stopped";
|
|
381
|
+
if (this.now() >= deadline) return "stop-failed";
|
|
382
|
+
await this.sleep(500);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
async remove(
|
|
387
|
+
def: ServiceDefinition,
|
|
388
|
+
instanceId: string,
|
|
389
|
+
opts: { purgeData: boolean; sharedVolumes?: ReadonlySet<string> },
|
|
390
|
+
): Promise<{ volumes?: VolumeOutcome[] } | undefined> {
|
|
391
|
+
if (!this.bin()) return undefined;
|
|
392
|
+
const mine = ((await this.ownedContainers(def.id, instanceId)) ?? []).map((c) => c.Id);
|
|
393
|
+
if (mine.length > 0) await this.exec(["rm", "-f", ...mine], 60_000);
|
|
394
|
+
if (!opts.purgeData) return undefined;
|
|
395
|
+
return { volumes: await this.removeVolumesGuarded(def, instanceId, opts.sharedVolumes ?? new Set()) };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The shared volume guard (reset-data and remove-with-purge). Volume names are
|
|
400
|
+
* runtime-global, so a declared volume is KEPT when another defined service
|
|
401
|
+
* declares it (`volume-shared`) or when a container that is not this
|
|
402
|
+
* service's owned one uses it (`volume-in-use`). `volume rm` is never forced:
|
|
403
|
+
* a volume a container started using after the check is refused by the
|
|
404
|
+
* runtime and reported kept.
|
|
405
|
+
*/
|
|
406
|
+
private async removeVolumesGuarded(def: ServiceDefinition, instanceId: string, shared: ReadonlySet<string>): Promise<VolumeOutcome[]> {
|
|
407
|
+
const out: VolumeOutcome[] = [];
|
|
408
|
+
for (const name of Object.keys(def.oci?.volumes ?? {})) {
|
|
409
|
+
if (shared.has(name)) {
|
|
410
|
+
out.push({ name, removed: false, reason: "volume-shared" });
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (await this.foreignContainerUses(name, def.id, instanceId)) {
|
|
414
|
+
out.push({ name, removed: false, reason: "volume-in-use" });
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const rm = await this.exec(["volume", "rm", name], 60_000);
|
|
418
|
+
if (rm.code === 0) {
|
|
419
|
+
out.push({ name, removed: true });
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
// Refused: it vanished already ("no such volume" — nothing to keep), a container grabbed it, or
|
|
423
|
+
// the runtime failed. Only a clear "no such volume" counts as removed; anything else keeps it.
|
|
424
|
+
const still = await this.exec(["volume", "inspect", name], 15_000);
|
|
425
|
+
const gone = still.code !== 0 && /no such volume/i.test(`${still.stderr}${still.stdout}`);
|
|
426
|
+
out.push(gone ? { name, removed: true } : { name, removed: false, reason: "volume-in-use" });
|
|
427
|
+
}
|
|
428
|
+
return out;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Does a container other than this service's owned one use `volume`? Unverifiable → yes (fail closed). */
|
|
432
|
+
private async foreignContainerUses(volume: string, id: string, instanceId: string): Promise<boolean> {
|
|
433
|
+
const r = await this.exec(["ps", "-a", "-q", "--no-trunc", "--filter", `volume=${volume}`], 15_000);
|
|
434
|
+
if (r.code !== 0) return true;
|
|
435
|
+
const ids = r.stdout.split(/\s+/).filter(Boolean);
|
|
436
|
+
if (ids.length === 0) return false;
|
|
437
|
+
const found = await this.inspectOrNull(ids);
|
|
438
|
+
if (found === null || found.length < ids.length) return true;
|
|
439
|
+
return found.some((c) => c.Config?.Labels?.[LABEL_SERVICE] !== id || c.Config?.Labels?.[LABEL_OWNER] !== instanceId);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async resetData(def: ServiceDefinition, instanceId: string, sharedVolumes: ReadonlySet<string>): Promise<ResetDataOutcome> {
|
|
443
|
+
if (!this.bin()) return { kind: "na" };
|
|
444
|
+
const mine = await this.ownedContainers(def.id, instanceId);
|
|
445
|
+
if (mine === null) return { kind: "unreachable" };
|
|
446
|
+
if (mine.some((c) => c.State?.Running === true)) return { kind: "busy" };
|
|
447
|
+
// Plain `rm` (never `-f`): a container that started meanwhile refuses it.
|
|
448
|
+
if (mine.length > 0) {
|
|
449
|
+
const rm = await this.exec(["rm", ...mine.map((c) => c.Id)], 60_000);
|
|
450
|
+
if (rm.code !== 0) return { kind: "rm-failed" };
|
|
451
|
+
}
|
|
452
|
+
return { kind: "ok", volumes: await this.removeVolumesGuarded(def, instanceId, sharedVolumes) };
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
async logs(def: ServiceDefinition, _inst: DriverInstance | undefined, req: LogsRequest): Promise<DriverLogs> {
|
|
456
|
+
if (!this.bin()) return { ok: false, reason: "runtime-error" };
|
|
457
|
+
const mine = await this.ownedContainers(def.id, req.instanceId);
|
|
458
|
+
if (mine === null) return { ok: false, reason: "runtime-error" };
|
|
459
|
+
if (mine.length > 1) return { ok: false, reason: "duplicate-instances" };
|
|
460
|
+
if (mine.length === 0) return { ok: false, reason: "no-output" };
|
|
461
|
+
const r = await this.exec(["logs", "--timestamps", "--since", req.startedAt, "--tail", String(req.lines), mine[0].Id], 30_000);
|
|
462
|
+
// A partial tail (timeout, runner cap, failure) is never served.
|
|
463
|
+
if (r.timedOut || r.truncated || r.error || r.code !== 0) return { ok: false, reason: "runtime-error" };
|
|
464
|
+
return { ok: true, text: mergeTimestamped(r.stdout, r.stderr) };
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async listOwned(instanceId: string): Promise<OwnedListing> {
|
|
468
|
+
if (!this.bin()) return { state: "absent" };
|
|
469
|
+
const all = await this.listContainers([`${LABEL_OWNER}=${instanceId}`]);
|
|
470
|
+
if (all === null) return { state: "unreachable" };
|
|
471
|
+
return {
|
|
472
|
+
state: "ok",
|
|
473
|
+
containers: all
|
|
474
|
+
.filter((c) => c.Config?.Labels?.[LABEL_OWNER] === instanceId && typeof c.Config?.Labels?.[LABEL_SERVICE] === "string")
|
|
475
|
+
.map((c) => ({ serviceId: c.Config?.Labels?.[LABEL_SERVICE] as string, containerId: c.Id, running: c.State?.Running === true })),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async removeContainers(ids: string[]): Promise<boolean> {
|
|
480
|
+
if (ids.length === 0) return true;
|
|
481
|
+
return (await this.exec(["rm", ...ids], 60_000)).code === 0;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async healthcheck(_def: ServiceDefinition, inst: DriverInstance): Promise<boolean> {
|
|
485
|
+
if (!inst.containerId) return false;
|
|
486
|
+
const [c] = await this.inspect([inst.containerId]);
|
|
487
|
+
return c?.State?.Health?.Status === "healthy";
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
async imageHasHealthcheck(def: ServiceDefinition): Promise<boolean | undefined> {
|
|
491
|
+
const engine = await this.engine();
|
|
492
|
+
if (!engine.ok) return undefined;
|
|
493
|
+
const r = await this.exec(["image", "inspect", def.oci?.image ?? ""], 15_000);
|
|
494
|
+
if (r.code !== 0) return undefined;
|
|
495
|
+
try {
|
|
496
|
+
const [img] = JSON.parse(r.stdout) as Array<{ Config?: { Healthcheck?: { Test?: string[] } | null }; Healthcheck?: { Test?: string[] } | null }>;
|
|
497
|
+
const test = img?.Config?.Healthcheck?.Test ?? img?.Healthcheck?.Test;
|
|
498
|
+
return Array.isArray(test) && test.length > 0 && test[0] !== "NONE";
|
|
499
|
+
} catch {
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* podman machine (macOS / Windows): the host cannot reach a published port
|
|
506
|
+
* directly, so open an owned `ssh -N -L` forward per port, without any
|
|
507
|
+
* in-container precondition. Connection identity/port come from
|
|
508
|
+
* `podman system connection list --format json` (shape recorded in the
|
|
509
|
+
* test fixture from a real podman 6.1 machine).
|
|
510
|
+
*/
|
|
511
|
+
/**
|
|
512
|
+
* Terminate this service's tunnel: the live one, or a recorded one a previous
|
|
513
|
+
* server left behind (re-checked to still be an `ssh -L` before signalling).
|
|
514
|
+
*/
|
|
515
|
+
async closeTunnel(def: ServiceDefinition, livePid?: number): Promise<void> {
|
|
516
|
+
const file = this.deps.paths.tunnelFile(def.id);
|
|
517
|
+
const record = readJsonFile<{ pid?: number; forwards?: string[] }>(file);
|
|
518
|
+
const recorded = record?.pid;
|
|
519
|
+
for (const pid of new Set([livePid, recorded].filter((p): p is number => Number.isInteger(p)))) {
|
|
520
|
+
if (!(this.deps.isProcessAlive ?? isProcessAlive)(pid)) continue;
|
|
521
|
+
if (pid !== livePid) {
|
|
522
|
+
// Exact match only: every recorded `-L` spec must still be in this
|
|
523
|
+
// pid's argv, so a reused pid (or the user's own ssh -L) is spared.
|
|
524
|
+
const forwards = record?.forwards ?? [];
|
|
525
|
+
const cmd = await (this.deps.readCommandLine ?? ((p) => readProcessCommandLine(p)))(pid);
|
|
526
|
+
if (!cmd || !/\bssh\b/.test(cmd) || forwards.length === 0 || !forwards.every((f) => cmd.includes(f))) continue;
|
|
527
|
+
}
|
|
528
|
+
await (this.deps.killProcess ?? killProcess)(pid, { timeoutMs: 2_000 });
|
|
529
|
+
}
|
|
530
|
+
fs.rmSync(file, { force: true });
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async onProbeFailed(def: ServiceDefinition, inst: DriverInstance): Promise<DriverInstance | null> {
|
|
534
|
+
if (this.deps.runtime !== "podman" || inst.tunnelPid) return null;
|
|
535
|
+
const conn = await this.machineConnection();
|
|
536
|
+
if (!conn) return null;
|
|
537
|
+
const ssh = this.deps.resolveBinary("ssh");
|
|
538
|
+
if (!ssh) return null;
|
|
539
|
+
const forwards: string[] = [];
|
|
540
|
+
const endpoints: Record<string, string> = {};
|
|
541
|
+
for (const [name, url] of Object.entries(inst.endpoints)) {
|
|
542
|
+
const u = new URL(url);
|
|
543
|
+
const local = await (this.deps.allocatePort ?? allocateLoopbackPort)();
|
|
544
|
+
forwards.push("-L", `127.0.0.1:${local}:127.0.0.1:${u.port}`);
|
|
545
|
+
endpoints[name] = `${u.protocol}//127.0.0.1:${local}${u.pathname === "/" ? "" : u.pathname}`;
|
|
546
|
+
}
|
|
547
|
+
if (forwards.length === 0) return null;
|
|
548
|
+
const args = [
|
|
549
|
+
"-N",
|
|
550
|
+
...forwards,
|
|
551
|
+
"-i", conn.identity,
|
|
552
|
+
"-p", String(conn.port),
|
|
553
|
+
"-o", "BatchMode=yes",
|
|
554
|
+
"-o", "ExitOnForwardFailure=yes",
|
|
555
|
+
"-o", "StrictHostKeyChecking=no",
|
|
556
|
+
"-o", "UserKnownHostsFile=/dev/null",
|
|
557
|
+
`${conn.user}@${conn.host}`,
|
|
558
|
+
];
|
|
559
|
+
await this.closeTunnel(def); // a previous server's forward would hold stale ports
|
|
560
|
+
const child = (this.deps.spawn ?? spawn)(ssh, args, { detached: false, stdio: "ignore", shell: false });
|
|
561
|
+
child.on("error", () => {});
|
|
562
|
+
child.unref();
|
|
563
|
+
if (!child.pid) return null;
|
|
564
|
+
const specs = forwards.filter((_x, i) => i % 2 === 1);
|
|
565
|
+
writePrivateFile(this.deps.paths.tunnelFile(def.id), `${JSON.stringify({ pid: child.pid, forwards: specs })}\n`);
|
|
566
|
+
// Re-probe only once the forward is up: ssh needs a moment to bind it.
|
|
567
|
+
const first = Number(new URL(Object.values(endpoints)[0]).port);
|
|
568
|
+
await (this.deps.waitForForward ?? defaultWaitForForward)(first);
|
|
569
|
+
return { ...inst, endpoints, tunnelPid: child.pid };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
async machineConnection(): Promise<{ user: string; host: string; port: number; identity: string } | null> {
|
|
573
|
+
const r = await this.exec(["system", "connection", "list", "--format", "json"], 10_000);
|
|
574
|
+
if (r.code !== 0) return null;
|
|
575
|
+
try {
|
|
576
|
+
const list = JSON.parse(r.stdout) as Array<{ URI?: string; Identity?: string; IsMachine?: boolean; Default?: boolean }>;
|
|
577
|
+
const c = list.find((x) => x.Default && x.IsMachine) ?? list.find((x) => x.IsMachine);
|
|
578
|
+
if (!c?.URI || !c.Identity || !fs.existsSync(c.Identity)) return null;
|
|
579
|
+
const u = new URL(c.URI);
|
|
580
|
+
if (u.protocol !== "ssh:") return null;
|
|
581
|
+
return { user: decodeURIComponent(u.username), host: u.hostname, port: Number(u.port || 22), identity: c.Identity };
|
|
582
|
+
} catch {
|
|
583
|
+
return null;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function firstLine(r: RunResult): string {
|
|
589
|
+
return (r.stderr || r.stdout || r.error || "").trim().split("\n")[0]?.slice(0, 300) ?? "";
|
|
590
|
+
}
|