@blackbelt-technology/pi-agent-dashboard 0.10.0 → 0.11.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/AGENTS.md +2 -2
- package/README.md +47 -6
- package/docs/architecture.md +1369 -63
- package/package.json +14 -9
- package/packages/extension/package.json +5 -3
- package/packages/extension/src/bridge-handlers/dispatch.ts +57 -3
- package/packages/extension/src/bridge-handlers/instance.ts +10 -1
- package/packages/extension/src/bridge-handlers/lifecycle.ts +65 -26
- package/packages/extension/src/bridge-ticket-client.ts +61 -7
- package/packages/extension/src/bridge-tls-checks.ts +79 -0
- package/packages/extension/src/bridge.ts +41 -28
- package/packages/extension/src/canvas-tool.ts +2 -0
- package/packages/extension/src/command-handler.ts +80 -6
- package/packages/extension/src/connect-target.ts +23 -0
- package/packages/extension/src/connection.ts +30 -7
- package/packages/extension/src/dashboard-http-origin.ts +88 -0
- package/packages/extension/src/dev-build.ts +11 -1
- package/packages/extension/src/git-link-builder.ts +11 -2
- package/packages/extension/src/inbound-limits.ts +138 -0
- package/packages/extension/src/local-token-header.ts +77 -6
- package/packages/extension/src/model-refresh.ts +69 -0
- package/packages/extension/src/pinned-endpoint.ts +44 -0
- package/packages/extension/src/project-trust-live.ts +143 -0
- package/packages/extension/src/project-trust.ts +159 -20
- package/packages/extension/src/provider-register.ts +59 -18
- package/packages/extension/src/remote-registration-gate.ts +21 -21
- package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
- package/packages/extension/src/role-model-tools.ts +47 -6
- package/packages/extension/src/server-auto-start.ts +1 -1
- package/packages/extension/src/server-proof-probe.ts +117 -0
- package/packages/extension/src/session-sync.ts +42 -12
- package/packages/extension/src/vault-delivery.ts +11 -1
- package/packages/server/package.json +8 -8
- package/packages/server/src/access/access-grants.ts +61 -8
- package/packages/server/src/access/access-health.ts +10 -0
- package/packages/server/src/access/access-plane.ts +5 -1
- package/packages/server/src/access/containment-gate.ts +55 -17
- package/packages/server/src/access/cwd-verdict.ts +3 -3
- package/packages/server/src/access/pi-trust-live.ts +244 -0
- package/packages/server/src/access/pi-trust-mirror-service.ts +44 -11
- package/packages/server/src/access/planes.ts +106 -1
- package/packages/server/src/access/preview-egress-store.ts +153 -0
- package/packages/server/src/access/prompt-channel.ts +5 -0
- package/packages/server/src/access/provisioning-wiring.ts +113 -0
- package/packages/server/src/access/provisioning.ts +763 -0
- package/packages/server/src/access/yolo-session.ts +54 -2
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +8 -3
- package/packages/server/src/agent-events/agent-event-hub.ts +158 -0
- package/packages/server/src/auth/audit-log.ts +80 -0
- package/packages/server/src/auth/auth-plugin.ts +44 -25
- package/packages/server/src/auth/auth.ts +24 -4
- package/packages/server/src/auth/bearer-auth.ts +52 -12
- package/packages/server/src/auth/cors-origin.ts +76 -29
- package/packages/server/src/auth/csp-report-paths.ts +31 -0
- package/packages/server/src/auth/csp-report-sink.ts +302 -0
- package/packages/server/src/auth/csp-report-store.ts +406 -0
- package/packages/server/src/auth/csp.ts +105 -9
- package/packages/server/src/auth/dashboard-credential-store.ts +4 -0
- package/packages/server/src/auth/dashboard-state-caller.ts +29 -0
- package/packages/server/src/auth/local-proof-refusal-log.ts +89 -0
- package/packages/server/src/auth/local-proof.ts +69 -18
- package/packages/server/src/auth/localhost-guard.ts +48 -10
- package/packages/server/src/auth/nav-intent.ts +72 -0
- package/packages/server/src/auth/passkey/passkey-assets.ts +99 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +17 -81
- package/packages/server/src/auth/passkey/passkey-routes.ts +8 -0
- package/packages/server/src/auth/pi-legacy-credentials.ts +3 -0
- package/packages/server/src/auth/plugin-server-trust.ts +29 -0
- package/packages/server/src/auth/plugin-trust-store.ts +103 -0
- package/packages/server/src/auth/proven-tls.ts +45 -0
- package/packages/server/src/auth/provider-auth-storage.ts +6 -1
- package/packages/server/src/auth/radius-mcp.ts +1 -1
- package/packages/server/src/auth/request-principal.ts +32 -0
- package/packages/server/src/auth/secret-rotation.ts +246 -0
- package/packages/server/src/auth/secure-origin.ts +56 -0
- package/packages/server/src/auth/serve-app-host.ts +111 -11
- package/packages/server/src/auth/ws-ticket.ts +39 -1
- package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
- package/packages/server/src/browser-handlers/directory-handler.ts +23 -0
- package/packages/server/src/browser-handlers/folder-gate.ts +55 -0
- package/packages/server/src/browser-handlers/handler-context.ts +25 -2
- package/packages/server/src/browser-handlers/session-action-handler.ts +9 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +100 -2
- package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
- package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
- package/packages/server/src/cli.ts +210 -39
- package/packages/server/src/config-api.ts +23 -1
- package/packages/server/src/config-snapshot.ts +48 -1
- package/packages/server/src/directory-service.ts +160 -54
- package/packages/server/src/event-wiring.ts +143 -108
- package/packages/server/src/git-worktree/git-operations.ts +44 -10
- package/packages/server/src/git-worktree/redact-git-text.ts +124 -0
- package/packages/server/src/git-worktree/ref-arg.ts +39 -0
- package/packages/server/src/git-worktree/worktree-init-trust.ts +43 -1
- package/packages/server/src/identity/http-road-classification.ts +13 -1
- package/packages/server/src/identity/ws-message-scope.ts +1 -0
- package/packages/server/src/identity/ws-road-classification.ts +1 -0
- package/packages/server/src/known-server-moves.ts +36 -15
- package/packages/server/src/lib/browse-roots.ts +118 -17
- package/packages/server/src/lib/client-dist.ts +36 -21
- package/packages/server/src/lib/dashboard-state-guard.ts +230 -0
- package/packages/server/src/lib/diagram-render.ts +4 -6
- package/packages/server/src/lib/eml.ts +3 -8
- package/packages/server/src/lib/file-cwd-admission.ts +145 -0
- package/packages/server/src/lib/grep-worker.ts +23 -0
- package/packages/server/src/lib/grep.ts +93 -7
- package/packages/server/src/lib/index-html.ts +81 -0
- package/packages/server/src/lib/office-preview.ts +2 -3
- package/packages/server/src/lib/path-containment.ts +7 -0
- package/packages/server/src/lib/resolve-file-mention.ts +11 -9
- package/packages/server/src/lib/route-admission.ts +80 -0
- package/packages/server/src/lib/writable-md-target.ts +4 -0
- package/packages/server/src/lifecycle/recovery-server.ts +65 -18
- package/packages/server/src/live-server/capability-redact.ts +15 -0
- package/packages/server/src/live-server/live-browser-gate.ts +22 -0
- package/packages/server/src/live-server/live-capability.ts +224 -17
- package/packages/server/src/live-server/live-csp-report.ts +63 -0
- package/packages/server/src/live-server/live-csp.ts +93 -0
- package/packages/server/src/live-server/live-egress.ts +435 -0
- package/packages/server/src/live-server/live-exposure.ts +149 -0
- package/packages/server/src/live-server/live-log.ts +21 -0
- package/packages/server/src/live-server/live-origin-scan.ts +298 -0
- package/packages/server/src/live-server/live-server-manager.ts +209 -33
- package/packages/server/src/live-server/live-server-proxy.ts +162 -17
- package/packages/server/src/live-server/live-static.ts +228 -0
- package/packages/server/src/openspec/dependencies-file.ts +122 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +3 -1
- package/packages/server/src/openspec/openspec-dependency-inputs.ts +281 -0
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +72 -26
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +48 -12
- package/packages/server/src/openspec/openspec-poll-worker.ts +95 -9
- package/packages/server/src/openspec/workflow-schema-status.ts +379 -0
- package/packages/server/src/openspec/workflow-schema.ts +141 -267
- package/packages/server/src/package/npm-search-proxy.ts +5 -2
- package/packages/server/src/package/provider-probe.ts +8 -2
- package/packages/server/src/pairing/browser-gateway.ts +188 -12
- package/packages/server/src/pairing/device-token-services.ts +26 -0
- package/packages/server/src/pairing/paired-devices.ts +335 -22
- package/packages/server/src/pairing/pairing.ts +34 -4
- package/packages/server/src/pairing/socket-locality.ts +26 -0
- package/packages/server/src/persistence/preferences-store.ts +9 -0
- package/packages/server/src/pi/bridge-channel.ts +33 -0
- package/packages/server/src/pi/bridge-upgrade-auth.ts +18 -3
- package/packages/server/src/pi/gateway-listeners.ts +9 -3
- package/packages/server/src/pi/host-project-trust.ts +14 -6
- package/packages/server/src/pi/pi-gateway.ts +191 -21
- package/packages/server/src/pi/resource-toggle-trust.ts +1 -1
- package/packages/server/src/plugin-discovery-wiring.ts +142 -0
- package/packages/server/src/plugin-event-seam.ts +116 -4
- package/packages/server/src/plugin-runtime-clients.ts +89 -0
- package/packages/server/src/routes/access-routes.ts +159 -9
- package/packages/server/src/routes/agent-event-stream-routes.ts +137 -0
- package/packages/server/src/routes/canvas-types-routes.ts +20 -6
- package/packages/server/src/routes/csp-violations-routes.ts +162 -0
- package/packages/server/src/routes/file-routes.ts +329 -209
- package/packages/server/src/routes/file-write-binary-routes.ts +77 -20
- package/packages/server/src/routes/folder-project-routes.ts +18 -12
- package/packages/server/src/routes/git-routes.ts +319 -203
- package/packages/server/src/routes/grep-routes.ts +38 -14
- package/packages/server/src/routes/known-servers-routes.ts +189 -91
- package/packages/server/src/routes/live-server-routes.ts +378 -15
- package/packages/server/src/routes/local-proof-routes.ts +14 -0
- package/packages/server/src/routes/nav-intent-routes.ts +46 -0
- package/packages/server/src/routes/openspec-group-routes.ts +53 -44
- package/packages/server/src/routes/openspec-routes.ts +379 -28
- package/packages/server/src/routes/package-routes.ts +58 -10
- package/packages/server/src/routes/pairing-routes.ts +146 -10
- package/packages/server/src/routes/pi-trust-query-routes.ts +330 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +59 -0
- package/packages/server/src/routes/plugin-runtime-client-routes.ts +149 -0
- package/packages/server/src/routes/provider-auth-routes.ts +15 -2
- package/packages/server/src/routes/resource-activation-routes.ts +18 -4
- package/packages/server/src/routes/session-routes.ts +51 -4
- package/packages/server/src/routes/system-routes.ts +84 -15
- package/packages/server/src/routes/ui-link-base.ts +88 -0
- package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
- package/packages/server/src/server.ts +648 -137
- package/packages/server/src/session/hydration-state.ts +148 -0
- package/packages/server/src/session/memory-session-manager.ts +7 -0
- package/packages/server/src/session/replay-exit-controller.ts +221 -0
- package/packages/server/src/session/retained-transcript.ts +9 -2
- package/packages/server/src/session/session-load-worker.ts +11 -2
- package/packages/server/src/spawn-process/headless-pid-registry.ts +14 -9
- package/packages/server/src/terminal/terminal-manager.ts +54 -46
- package/packages/server/src/tls/tls-bridge-route.ts +61 -0
- package/packages/server/src/tls/tls-config-validation.ts +2 -0
- package/packages/server/src/vault/device-expiry-sweep.ts +36 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +155 -25
- package/packages/server/src/watch-files-handler.ts +66 -0
- package/packages/shared/package.json +1 -1
- package/packages/shared/src/browser-protocol.ts +148 -2
- package/packages/shared/src/canvas-declare.ts +27 -0
- package/packages/shared/src/config.ts +372 -53
- package/packages/shared/src/containment-guard.ts +35 -0
- package/packages/shared/src/dashboard-plugin/anthropic-peer-names.ts +9 -0
- package/packages/shared/src/dashboard-plugin/event-type-owners.ts +50 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +2 -0
- package/packages/shared/src/dashboard-plugin/host-api.ts +61 -0
- package/packages/shared/src/dashboard-plugin/host-fx-classes.ts +23 -0
- package/packages/shared/src/dashboard-plugin/host-import-map.ts +38 -0
- package/packages/shared/src/dashboard-plugin/index.ts +2 -0
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +54 -1
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +54 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +45 -3
- package/packages/shared/src/dashboard-plugin/slot-types.ts +8 -0
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +219 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +28 -0
- package/packages/shared/src/display-prefs.ts +13 -0
- package/packages/shared/src/doctor-core.ts +94 -27
- package/packages/shared/src/live-server.ts +141 -0
- package/packages/shared/src/local-proof-signal.ts +16 -0
- package/packages/shared/src/loopback-host.ts +26 -0
- package/packages/shared/src/mcp/verb-tiers.generated.ts +178 -0
- package/packages/shared/src/mdns-discovery.ts +25 -6
- package/packages/shared/src/nav-intent.ts +29 -0
- package/packages/shared/src/openspec-dependencies.ts +760 -0
- package/packages/shared/src/openspec-poller.ts +3 -2
- package/packages/shared/src/pairing-knock-client.ts +44 -4
- package/packages/shared/src/pi-trust-live-query.ts +93 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +5 -7
- package/packages/shared/src/pi-trust-mirror-types.ts +36 -3
- package/packages/shared/src/pi-trust-mirror.ts +29 -3
- package/packages/shared/src/pi-trust-resource-digest.ts +255 -0
- package/packages/shared/src/platform/ensure-bundled-git.ts +57 -34
- package/packages/shared/src/platform/git-source.ts +88 -26
- package/packages/shared/src/platform/git.ts +14 -8
- package/packages/shared/src/platform/process.ts +48 -4
- package/packages/shared/src/platform/resolve-pi-shell.ts +76 -0
- package/packages/shared/src/platform/select-git-source.ts +7 -0
- package/packages/shared/src/plugin-route-cwd.ts +95 -0
- package/packages/shared/src/preview-egress.ts +138 -0
- package/packages/shared/src/project-root.ts +16 -0
- package/packages/shared/src/protocol.ts +40 -1
- package/packages/shared/src/recommended-extensions.ts +18 -0
- package/packages/shared/src/recovery-timing.ts +27 -0
- package/packages/shared/src/remote-credentials.ts +63 -0
- package/packages/shared/src/rest-api.ts +55 -8
- package/packages/shared/src/route-tiers.ts +55 -1
- package/packages/shared/src/runtime-overlay/archive-safety.d.mts +2 -0
- package/packages/shared/src/runtime-overlay/archive-safety.mjs +33 -0
- package/packages/shared/src/runtime-overlay/manifest.d.mts +7 -0
- package/packages/shared/src/runtime-overlay/manifest.mjs +22 -2
- package/packages/shared/src/safe-local-path.ts +24 -0
- package/packages/shared/src/sanitize-profiles.ts +98 -0
- package/packages/shared/src/server-launcher.ts +9 -1
- package/packages/shared/src/server-pinning.ts +55 -2
- package/packages/shared/src/server-routes.ts +462 -0
- package/packages/shared/src/session-ui-path.ts +11 -0
- package/packages/shared/src/tls-only-token.ts +16 -0
- package/packages/shared/src/tool-registry/definitions.ts +5 -5
- package/packages/shared/src/tool-registry/strategies.ts +12 -11
- package/packages/shared/src/types.ts +66 -0
- package/packages/shared/src/url-policy.ts +96 -0
- package/packages/shared/src/vault-delivery.ts +15 -0
- package/packages/shared/src/ws-message-tiers.ts +1 -0
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
|
|
28
28
|
import { hasProxyForwardingHeaders } from "../auth/localhost-guard.js";
|
|
29
29
|
import type { TicketConsumption } from "../auth/ws-ticket.js";
|
|
30
|
+
import type { BridgeAdmission } from "./bridge-channel.js";
|
|
30
31
|
|
|
31
32
|
export interface BridgeUpgradeInput {
|
|
32
33
|
transport: "unix" | "tcp";
|
|
@@ -72,7 +73,17 @@ type BridgeRefusalCause =
|
|
|
72
73
|
| "browser-origin";
|
|
73
74
|
|
|
74
75
|
export type BridgeUpgradeVerdict =
|
|
75
|
-
| {
|
|
76
|
+
| {
|
|
77
|
+
allow: true;
|
|
78
|
+
reason: string;
|
|
79
|
+
deprecated?: boolean;
|
|
80
|
+
deviceId?: string;
|
|
81
|
+
viaProof?: boolean;
|
|
82
|
+
/** How the upgrade was admitted (the hub's own observation, see `bridge-channel.ts`). */
|
|
83
|
+
admission: BridgeAdmission;
|
|
84
|
+
/** A consumed ticket was minted on a proven-TLS request. */
|
|
85
|
+
ticketTls?: boolean;
|
|
86
|
+
}
|
|
76
87
|
| { allow: false; reason: string; cause: BridgeRefusalCause };
|
|
77
88
|
|
|
78
89
|
/** Loopback in every form Node reports it, including IPv4-mapped IPv6. */
|
|
@@ -107,7 +118,7 @@ function ticketFrom(input: BridgeUpgradeInput): string | null {
|
|
|
107
118
|
|
|
108
119
|
export function decideBridgeUpgrade(input: BridgeUpgradeInput): BridgeUpgradeVerdict {
|
|
109
120
|
if (input.transport === "unix") {
|
|
110
|
-
return { allow: true, reason: "unix socket: authorised by file mode (D5)" };
|
|
121
|
+
return { allow: true, reason: "unix socket: authorised by file mode (D5)", admission: "unix" };
|
|
111
122
|
}
|
|
112
123
|
|
|
113
124
|
// A bridge never sends `Origin`; a browser always does. So on TCP the mere
|
|
@@ -144,6 +155,8 @@ export function decideBridgeUpgrade(input: BridgeUpgradeInput): BridgeUpgradeVer
|
|
|
144
155
|
allow: true,
|
|
145
156
|
reason: "tcp: valid single-use bridge ticket",
|
|
146
157
|
deviceId: consumption.deviceId,
|
|
158
|
+
admission: consumption.deviceId ? "device-ticket" : "local-ticket",
|
|
159
|
+
ticketTls: consumption.provenTls === true,
|
|
147
160
|
};
|
|
148
161
|
}
|
|
149
162
|
|
|
@@ -154,13 +167,14 @@ export function decideBridgeUpgrade(input: BridgeUpgradeInput): BridgeUpgradeVer
|
|
|
154
167
|
// flagged deprecated. Checked before the grace so an authorised bridge is
|
|
155
168
|
// logged as credentialed, not as a deprecation.
|
|
156
169
|
if (loopback && input.localCredential === "valid") {
|
|
157
|
-
return { allow: true, viaProof: true, reason: "tcp loopback: valid local credential proof (D6)" };
|
|
170
|
+
return { allow: true, viaProof: true, reason: "tcp loopback: valid local credential proof (D6)", admission: "local-proof" };
|
|
158
171
|
}
|
|
159
172
|
if (loopback && input.localCredential === "legacy") {
|
|
160
173
|
return {
|
|
161
174
|
allow: true,
|
|
162
175
|
deprecated: true,
|
|
163
176
|
reason: "tcp loopback: legacy raw local token (deprecated; send X-Pi-Local-Proof)",
|
|
177
|
+
admission: "legacy-token",
|
|
164
178
|
};
|
|
165
179
|
}
|
|
166
180
|
|
|
@@ -171,6 +185,7 @@ export function decideBridgeUpgrade(input: BridgeUpgradeInput): BridgeUpgradeVer
|
|
|
171
185
|
reason:
|
|
172
186
|
`tcp loopback: accepted WITHOUT a bridge ticket (${consumption.reason}) ` +
|
|
173
187
|
`under the deprecation window — this will be refused from 1.0.0`,
|
|
188
|
+
admission: "grace",
|
|
174
189
|
};
|
|
175
190
|
}
|
|
176
191
|
|
|
@@ -31,9 +31,15 @@ export async function startGatewayListeners(
|
|
|
31
31
|
// TCP first: `startOnSocket` installs the shared WebSocketServer, and
|
|
32
32
|
// `start()` refuses to run after it rather than orphan the listener.
|
|
33
33
|
if (policy.tcp) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const { port, host } = policy.tcp;
|
|
35
|
+
try {
|
|
36
|
+
gateway.start(port, host, { kind: policy.socketPath ? "tcp" : "loopback" });
|
|
37
|
+
await gateway.tcpListening();
|
|
38
|
+
} catch (err) {
|
|
39
|
+
// Fail startup like the socket / fallback paths do, instead of running
|
|
40
|
+
// degraded with no bridge listener (#978).
|
|
41
|
+
throw new Error(`gateway TCP port ${host}:${port} could not be bound: ${String(err)}`, { cause: err });
|
|
42
|
+
}
|
|
37
43
|
}
|
|
38
44
|
if (!policy.socketPath) return;
|
|
39
45
|
const socketPath = policy.socketPath;
|
|
@@ -32,6 +32,8 @@ import {
|
|
|
32
32
|
safePiTrustKey,
|
|
33
33
|
} from "@blackbelt-technology/pi-dashboard-shared/pi-trust-mirror-snapshot.js";
|
|
34
34
|
import type { MirrorSourceKind } from "@blackbelt-technology/pi-dashboard-shared/pi-trust-mirror-types.js";
|
|
35
|
+
import { liveLifetimeText } from "../access/pi-trust-live.js";
|
|
36
|
+
import { getPiTrustMirror } from "../access/pi-trust-mirror-service.js";
|
|
35
37
|
import { getPiCore } from "./pi-resource-activation.js";
|
|
36
38
|
|
|
37
39
|
export type { DefaultProjectTrust };
|
|
@@ -45,7 +47,7 @@ export interface HostProjectTrustDeps {
|
|
|
45
47
|
/** Durable dashboard trust. Absent → none (mirror off). */
|
|
46
48
|
mirror?: {
|
|
47
49
|
enabled: () => boolean;
|
|
48
|
-
covering: (cwd: string) => { sourceKind: MirrorSourceKind; key: string } | null;
|
|
50
|
+
covering: (cwd: string) => { sourceKind: MirrorSourceKind; key: string; lifetime?: string } | null;
|
|
49
51
|
};
|
|
50
52
|
}
|
|
51
53
|
|
|
@@ -105,20 +107,26 @@ export function createHostProjectTrust(deps: HostProjectTrustLegacyDeps): (cwd:
|
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
/**
|
|
108
|
-
* The
|
|
109
|
-
*
|
|
110
|
-
*
|
|
110
|
+
* The dashboard-trust entry covering `cwd`, or null — also null when the mirror is
|
|
111
|
+
* disabled. Durable entries first (snapshot bytes + the bridge's rules), then live
|
|
112
|
+
* ones (server-lifetime trust: session worktree trust, session grants, YOLO),
|
|
113
|
+
* evaluated with no folder proof because this server observes its own folders.
|
|
114
|
+
* Shared by the host predicate and the resource-toggle gate (which passes
|
|
115
|
+
* `live: false`: its prompt text describes a durable entry). Never throws.
|
|
111
116
|
*/
|
|
112
117
|
export function coveringDashboardEntry(
|
|
113
118
|
cwd: string,
|
|
114
119
|
home: string = os.homedir(),
|
|
115
|
-
|
|
120
|
+
opts: { live?: boolean } = {},
|
|
121
|
+
): { sourceKind: MirrorSourceKind; key: string; lifetime?: string } | null {
|
|
116
122
|
try {
|
|
117
123
|
if (!loadConfig().piTrustMirror.enabled) return null;
|
|
118
124
|
const key = safePiTrustKey(cwd, home);
|
|
119
125
|
if (key === null) return null;
|
|
120
126
|
const coverage = coverSnapshot(readPiTrustMirrorSnapshot(), key);
|
|
121
|
-
|
|
127
|
+
if (coverage.covered) return { sourceKind: coverage.entry.sourceKind, key: coverage.entry.key };
|
|
128
|
+
const live = opts.live === false ? undefined : getPiTrustMirror()?.live?.covering(key);
|
|
129
|
+
return live ? { sourceKind: live.sourceKind, key: live.key, lifetime: liveLifetimeText(live.lifetime) } : null;
|
|
122
130
|
} catch {
|
|
123
131
|
return null;
|
|
124
132
|
}
|
|
@@ -8,6 +8,7 @@ import type { ExtensionToServerMessage, ServerToExtensionMessage } from "@blackb
|
|
|
8
8
|
import type { DashboardSession } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
9
9
|
import { WebSocket, WebSocketServer } from "ws";
|
|
10
10
|
import type { LocalCredentialVerifier } from "../auth/local-credential.js";
|
|
11
|
+
import { isProvenTls } from "../auth/proven-tls.js";
|
|
11
12
|
import type { TicketConsumption } from "../auth/ws-ticket.js";
|
|
12
13
|
import { classifyCarrierLoss } from "../session/death-reason.js";
|
|
13
14
|
import { createHostPressureTracker, type HostPressure } from "../session/host-pressure-tracker.js";
|
|
@@ -25,6 +26,7 @@ import {
|
|
|
25
26
|
type ProbeableSocket,
|
|
26
27
|
resolveProbe,
|
|
27
28
|
} from "./bridge-contention.js";
|
|
29
|
+
import type { BridgeChannel } from "./bridge-channel.js";
|
|
28
30
|
import { decideBridgeUpgrade, isLoopbackAddress } from "./bridge-upgrade-auth.js";
|
|
29
31
|
import { bindGatewaySocket, unbindGatewaySocket } from "./gateway-socket-bind.js";
|
|
30
32
|
import { createProvisionalRegistry } from "./provisional-registration.js";
|
|
@@ -89,6 +91,13 @@ export interface PiGatewayOptions {
|
|
|
89
91
|
/** Test seams for the pressure thresholds. */
|
|
90
92
|
hostPressureDegradedMs?: number;
|
|
91
93
|
hostPressureUnresponsiveMs?: number;
|
|
94
|
+
/**
|
|
95
|
+
* Fires whenever the routing entry for a session changes to a (possibly different) socket: at
|
|
96
|
+
* `session_register` (with the register's declared `certValidation`) and at move-commit. The vault hub
|
|
97
|
+
* re-evaluates trust here so a socket swap never sees the previous socket's channel.
|
|
98
|
+
* See change: prove-vault-bridge-tls (D3).
|
|
99
|
+
*/
|
|
100
|
+
onRoutingChanged?: (sessionId: string, declared?: { certValidation?: "enforced" }) => void;
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
/** How a bridge connection reached the gateway. `local` = in-process virtual socket. */
|
|
@@ -111,7 +120,18 @@ export interface LocalGatewaySocket {
|
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
export interface PiGateway {
|
|
123
|
+
/**
|
|
124
|
+
* Serve bridges over TCP. Synchronous: the bind outcome is observed via
|
|
125
|
+
* {@link PiGateway.tcpListening}. A failed bind tears the half-started
|
|
126
|
+
* listener down and never escapes as an uncaught exception. See issue #978.
|
|
127
|
+
*/
|
|
114
128
|
start(port: number, host?: string, opts?: { kind?: "tcp" | "loopback" }): void;
|
|
129
|
+
/**
|
|
130
|
+
* Bind outcome of the last `start()`: resolves once listening (or at once when
|
|
131
|
+
* no TCP listener was started); rejects with the bind error, e.g. `EADDRINUSE`.
|
|
132
|
+
* See issue #978.
|
|
133
|
+
*/
|
|
134
|
+
tcpListening(): Promise<void>;
|
|
115
135
|
/**
|
|
116
136
|
* Serve bridges on `127.0.0.1:<port>` because the unix socket cannot be
|
|
117
137
|
* used. Requires the local token or a ticket — no tokenless grace — and
|
|
@@ -138,6 +158,13 @@ export interface PiGateway {
|
|
|
138
158
|
*/
|
|
139
159
|
attachLocalConnection(socket: LocalGatewaySocket): void;
|
|
140
160
|
stop(): void;
|
|
161
|
+
/**
|
|
162
|
+
* Resolves once the async socket teardown handed off by the last `stop()`
|
|
163
|
+
* settles (never rejects; resolves at once when nothing is pending). The
|
|
164
|
+
* async `server.stop()` awaits it so a caller removing HOME right after
|
|
165
|
+
* never races the teardown.
|
|
166
|
+
*/
|
|
167
|
+
stopped(): Promise<void>;
|
|
141
168
|
/**
|
|
142
169
|
* Resolved listening endpoint after start(): a port number for TCP, the
|
|
143
170
|
* socket PATH for a UDS listener, or null when not started. A UDS
|
|
@@ -172,6 +199,19 @@ export interface PiGateway {
|
|
|
172
199
|
clearHostPressure(sessionId: string): void;
|
|
173
200
|
/** Force-close the WebSocket connection for a session */
|
|
174
201
|
closeSession(sessionId: string): boolean;
|
|
202
|
+
/**
|
|
203
|
+
* The channel of the socket the gateway currently ROUTES `sessionId` to (it changes with the routing
|
|
204
|
+
* entry at register and at move-commit); `undefined` when unrouted. See change: prove-vault-bridge-tls (D3).
|
|
205
|
+
*/
|
|
206
|
+
channelOf(sessionId: string): BridgeChannel | undefined;
|
|
207
|
+
/**
|
|
208
|
+
* Admit a `/ws/bridge` upgrade that arrived on the native TLS listener: ticket-only, paired devices
|
|
209
|
+
* only. Hands off to the EXISTING gateway server, or refuses 503 when the gateway has not started.
|
|
210
|
+
* See change: prove-vault-bridge-tls (D2).
|
|
211
|
+
*/
|
|
212
|
+
handleTlsUpgrade(req: IncomingMessage, socket: import("node:stream").Duplex, head: Buffer): void;
|
|
213
|
+
/** Close every socket admitted over the TLS listener (Accept bridges / TLS switched off). */
|
|
214
|
+
closeTlsBridges(): void;
|
|
175
215
|
/**
|
|
176
216
|
* Contention records, cumulative refusal counter and rate limiter.
|
|
177
217
|
* See change: fix-duplicate-bridge-registration (D4, D6).
|
|
@@ -213,14 +253,31 @@ export function createPiGateway(
|
|
|
213
253
|
* `WebSocketServer` (D10), and `verifyClient` sees only the request.
|
|
214
254
|
*/
|
|
215
255
|
const unixUpgrades = new WeakSet<IncomingMessage>();
|
|
256
|
+
/**
|
|
257
|
+
* Upgrades that arrived over the native TLS listener (`handleTlsUpgrade`). `verifyClient` skips them
|
|
258
|
+
* exactly as it skips `unixUpgrades` (the ticket is consumed once, in `handleTlsUpgrade`), and the
|
|
259
|
+
* per-connection transport derivation reads this before the attach-time label, so a TLS connection is
|
|
260
|
+
* never labelled `unix`. See change: prove-vault-bridge-tls (D2).
|
|
261
|
+
*/
|
|
262
|
+
const tlsUpgrades = new WeakMap<IncomingMessage, BridgeChannel>();
|
|
263
|
+
/** Channel evidence of TCP upgrades admitted by `verifyClient`. */
|
|
264
|
+
const tcpChannels = new WeakMap<IncomingMessage, BridgeChannel>();
|
|
265
|
+
/** Per socket: how it was admitted. `channelOf` follows the routing entry to its socket. */
|
|
266
|
+
const socketChannels = new WeakMap<WebSocket, BridgeChannel>();
|
|
267
|
+
/** Sockets admitted over TLS, so `closeTlsBridges()` can close exactly them. */
|
|
268
|
+
const tlsSockets = new Set<WebSocket>();
|
|
216
269
|
/** Upgrades admitted on a verified proof → the data the counter-proof answers. */
|
|
217
270
|
const proofAdmissions = new WeakMap<IncomingMessage, { binding: string; nonce: string; mac: string }>();
|
|
218
271
|
let pingTimer: ReturnType<typeof setInterval> | null = null;
|
|
219
272
|
/** The UDS listener, when this instance serves bridges over a socket (D1). */
|
|
220
273
|
let socketServer: http.Server | null = null;
|
|
221
274
|
let socketPath: string | null = null;
|
|
275
|
+
/** The socket teardown `stop()` hands off; never rejects (see `stopped()`). */
|
|
276
|
+
let pendingTeardown: Promise<void> = Promise.resolve();
|
|
222
277
|
/** What the TCP `wss` is for (health, spawn pinning). See D7. */
|
|
223
278
|
let tcpKind: "tcp" | "loopback" | "loopback-fallback" | null = null;
|
|
279
|
+
/** Bind outcome of the last `start()` (pre-handled; see `tcpListening`). */
|
|
280
|
+
let tcpBound: Promise<void> = Promise.resolve();
|
|
224
281
|
let fallbackReason: "occupied" | "unsupported" | undefined;
|
|
225
282
|
|
|
226
283
|
// Map sessionId → WebSocket
|
|
@@ -464,14 +521,35 @@ export function createPiGateway(
|
|
|
464
521
|
// request exists, so without the explicit label it would fail closed to
|
|
465
522
|
// remote. Same-host by construction, attributed like `unix`.
|
|
466
523
|
// See change: add-acp-session-driver (D1).
|
|
467
|
-
const
|
|
524
|
+
const tlsChannel = req ? tlsUpgrades.get(req) : undefined;
|
|
525
|
+
const connTransport: ConnectionTransport = tlsChannel
|
|
526
|
+
? "tcp"
|
|
527
|
+
: req && unixUpgrades.has(req)
|
|
528
|
+
? "unix"
|
|
529
|
+
: transport;
|
|
530
|
+
// The hub's own observation of how this socket was admitted, on EVERY transport.
|
|
531
|
+
// See change: prove-vault-bridge-tls (D3).
|
|
532
|
+
const channel: BridgeChannel = tlsChannel
|
|
533
|
+
?? (!req
|
|
534
|
+
? { admission: "in-process", provenTls: false, ticketTls: false }
|
|
535
|
+
: unixUpgrades.has(req)
|
|
536
|
+
? { admission: "unix", provenTls: false, ticketTls: false }
|
|
537
|
+
: (tcpChannels.get(req) ?? { admission: "grace", provenTls: false, ticketTls: false }));
|
|
538
|
+
socketChannels.set(ws, channel);
|
|
539
|
+
if (tlsChannel) {
|
|
540
|
+
tlsSockets.add(ws);
|
|
541
|
+
ws.once("close", () => tlsSockets.delete(ws));
|
|
542
|
+
}
|
|
468
543
|
const sessionOrigin = attributeOrigin({
|
|
469
544
|
transport: connTransport === "tcp" ? "tcp" : "unix",
|
|
545
|
+
// A ticket-admitted TLS connection is attributed to its ticket's device as REMOTE, whatever the
|
|
546
|
+
// peer address: a loopback forwarder in front of the TLS port cannot make it hub-local.
|
|
470
547
|
remote:
|
|
471
548
|
connTransport === "tcp" &&
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
549
|
+
(tlsChannel !== undefined ||
|
|
550
|
+
!isLoopbackAddress(
|
|
551
|
+
options?.peerAddressForTest ?? req?.socket?.remoteAddress ?? undefined,
|
|
552
|
+
)),
|
|
475
553
|
deviceId: req ? upgradeDeviceId.get(req) : undefined,
|
|
476
554
|
localInstanceId: options?.instanceId ?? "",
|
|
477
555
|
});
|
|
@@ -487,6 +565,8 @@ export function createPiGateway(
|
|
|
487
565
|
// provisional announces intent and claims nothing, so this socket must
|
|
488
566
|
// stay unrouted for that id until it commits.
|
|
489
567
|
let provisionalSessionId: string | null = null;
|
|
568
|
+
// What THIS socket declared at register (incl. a provisional one), carried to the move-commit.
|
|
569
|
+
let declaredCertValidation: "enforced" | undefined;
|
|
490
570
|
// Serializes this socket's messages. The contention decision is async
|
|
491
571
|
// (it may probe the incumbent), and a bridge sends `session_register`
|
|
492
572
|
// immediately followed by events; without this queue a later message
|
|
@@ -742,6 +822,7 @@ export function createPiGateway(
|
|
|
742
822
|
}
|
|
743
823
|
resetHeartbeat(movedId);
|
|
744
824
|
console.log(`[gateway] session move committed: ${movedId}`);
|
|
825
|
+
options?.onRoutingChanged?.(movedId, { certValidation: declaredCertValidation });
|
|
745
826
|
// Tell the mover the transfer actually happened. It cannot
|
|
746
827
|
// infer this from its own send: every refusal above is silent
|
|
747
828
|
// by design, so no acknowledgement means "assume nothing moved".
|
|
@@ -849,6 +930,7 @@ export function createPiGateway(
|
|
|
849
930
|
},
|
|
850
931
|
});
|
|
851
932
|
provisionalSessionId = msg.sessionId;
|
|
933
|
+
declaredCertValidation = msg.certValidation === "enforced" ? "enforced" : undefined;
|
|
852
934
|
ws.send(
|
|
853
935
|
JSON.stringify({
|
|
854
936
|
type: "provisional_accepted",
|
|
@@ -946,6 +1028,9 @@ export function createPiGateway(
|
|
|
946
1028
|
onConnection?.();
|
|
947
1029
|
onSessionRegistered?.(msg.sessionId, msg.cwd);
|
|
948
1030
|
onEvent?.(msg.sessionId, msg);
|
|
1031
|
+
// The routing entry now points at THIS socket: re-evaluate trust against its own channel.
|
|
1032
|
+
declaredCertValidation = msg.certValidation === "enforced" ? "enforced" : undefined;
|
|
1033
|
+
options?.onRoutingChanged?.(msg.sessionId, { certValidation: declaredCertValidation });
|
|
949
1034
|
} catch {
|
|
950
1035
|
// Ignore malformed messages
|
|
951
1036
|
}
|
|
@@ -1090,7 +1175,7 @@ export function createPiGateway(
|
|
|
1090
1175
|
port: number,
|
|
1091
1176
|
host: string | undefined,
|
|
1092
1177
|
tcpOpts: { kind: "tcp" | "loopback" | "loopback-fallback"; requireLocalCredential?: boolean },
|
|
1093
|
-
) => {
|
|
1178
|
+
): Promise<void> => {
|
|
1094
1179
|
// A listener already exists (socket transport): replacing `wss` here
|
|
1095
1180
|
// would orphan it and silently re-route socket upgrades into the TCP
|
|
1096
1181
|
// server's client set. Both transports are meant to SHARE one
|
|
@@ -1118,7 +1203,7 @@ export function createPiGateway(
|
|
|
1118
1203
|
// written for TCP and hardcodes `transport: "tcp"` — running it
|
|
1119
1204
|
// here would refuse every local bridge in any deployment that also
|
|
1120
1205
|
// enables TCP, which is the shipped container.
|
|
1121
|
-
if (unixUpgrades.has(info.req)) {
|
|
1206
|
+
if (unixUpgrades.has(info.req) || tlsUpgrades.has(info.req)) {
|
|
1122
1207
|
done(true);
|
|
1123
1208
|
return;
|
|
1124
1209
|
}
|
|
@@ -1159,6 +1244,13 @@ export function createPiGateway(
|
|
|
1159
1244
|
proofAdmissions.set(info.req, { binding: stamped.binding, nonce: stamped.nonce, mac: stamped.mac });
|
|
1160
1245
|
}
|
|
1161
1246
|
if (verdict.deviceId) upgradeDeviceId.set(info.req, verdict.deviceId);
|
|
1247
|
+
// The hub's own observation, on the plain TCP listener: never proven TLS.
|
|
1248
|
+
tcpChannels.set(info.req, {
|
|
1249
|
+
admission: verdict.admission,
|
|
1250
|
+
provenTls: false,
|
|
1251
|
+
ticketTls: verdict.ticketTls === true,
|
|
1252
|
+
...(verdict.admission === "device-ticket" && verdict.deviceId ? { ticketDeviceId: verdict.deviceId } : {}),
|
|
1253
|
+
});
|
|
1162
1254
|
done(true);
|
|
1163
1255
|
}
|
|
1164
1256
|
: undefined;
|
|
@@ -1176,6 +1268,28 @@ export function createPiGateway(
|
|
|
1176
1268
|
attachConnectionHandler(wss, "tcp");
|
|
1177
1269
|
tcpKind = tcpOpts.kind;
|
|
1178
1270
|
startHeartbeat();
|
|
1271
|
+
// Bind outcome (#978): without an `error` listener a held port surfaced as an
|
|
1272
|
+
// uncaught exception the crash safety net swallowed, leaving no bridge listener.
|
|
1273
|
+
const server = wss;
|
|
1274
|
+
const bound = new Promise<void>((resolve, reject) => {
|
|
1275
|
+
server.once("listening", () => {
|
|
1276
|
+
server.off("error", reject);
|
|
1277
|
+
// Post-bind errors are reported loudly instead of escaping as uncaught.
|
|
1278
|
+
server.on("error", (err) => console.error(`[pi-gateway] TCP listener error on port ${port}: ${String(err)}`));
|
|
1279
|
+
resolve();
|
|
1280
|
+
});
|
|
1281
|
+
server.once("error", reject);
|
|
1282
|
+
}).catch((err: unknown) => {
|
|
1283
|
+
// A refused bind must leave no half-started listener behind.
|
|
1284
|
+
server.close();
|
|
1285
|
+
if (wss === server) {
|
|
1286
|
+
wss = null;
|
|
1287
|
+
tcpKind = null;
|
|
1288
|
+
}
|
|
1289
|
+
throw err;
|
|
1290
|
+
});
|
|
1291
|
+
bound.catch(() => undefined);
|
|
1292
|
+
return bound;
|
|
1179
1293
|
};
|
|
1180
1294
|
|
|
1181
1295
|
return {
|
|
@@ -1255,6 +1369,67 @@ export function createPiGateway(
|
|
|
1255
1369
|
* upgrade/connection handler with the TCP listener, so the transport is a
|
|
1256
1370
|
* per-bridge property rather than a per-server mode (D10).
|
|
1257
1371
|
*/
|
|
1372
|
+
channelOf(sessionId: string): BridgeChannel | undefined {
|
|
1373
|
+
const ws = connections.get(sessionId);
|
|
1374
|
+
return ws ? socketChannels.get(ws) : undefined;
|
|
1375
|
+
},
|
|
1376
|
+
closeTlsBridges() {
|
|
1377
|
+
for (const ws of [...tlsSockets]) {
|
|
1378
|
+
try {
|
|
1379
|
+
ws.close(1008, "tls-bridges-off");
|
|
1380
|
+
} catch {
|
|
1381
|
+
ws.terminate();
|
|
1382
|
+
}
|
|
1383
|
+
// A peer that ignores the close handshake must not keep a withdrawn channel open.
|
|
1384
|
+
const timer = setTimeout(() => ws.terminate(), 2000);
|
|
1385
|
+
timer.unref?.();
|
|
1386
|
+
ws.once("close", () => clearTimeout(timer));
|
|
1387
|
+
}
|
|
1388
|
+
},
|
|
1389
|
+
handleTlsUpgrade(req, socket, head) {
|
|
1390
|
+
const refuseWith = (status: string, cause: string) => {
|
|
1391
|
+
console.warn(`[pi-gateway] tls bridge upgrade refused (${cause})`);
|
|
1392
|
+
socket.write(`HTTP/1.1 ${status}\r\n\r\n`);
|
|
1393
|
+
socket.destroy();
|
|
1394
|
+
};
|
|
1395
|
+
const target = wss;
|
|
1396
|
+
const bridgeAuth = options?.bridgeAuth;
|
|
1397
|
+
// Nothing is created lazily: the gateway's start order is unaffected. No gateway → no admission.
|
|
1398
|
+
if (!target || !bridgeAuth) {
|
|
1399
|
+
refuseWith("503 Service Unavailable", "tls-gateway-not-ready");
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
// Ticket-only: no loopback grace and no local credential on this path.
|
|
1403
|
+
const verdict = decideBridgeUpgrade({
|
|
1404
|
+
transport: "tcp",
|
|
1405
|
+
remoteAddress: req.socket.remoteAddress ?? undefined,
|
|
1406
|
+
headers: req.headers,
|
|
1407
|
+
url: req.url,
|
|
1408
|
+
secWebSocketProtocol: req.headers["sec-websocket-protocol"],
|
|
1409
|
+
requireTicketOnLoopback: true,
|
|
1410
|
+
consumeTicket: bridgeAuth.consumeTicket,
|
|
1411
|
+
localCredential: "absent",
|
|
1412
|
+
});
|
|
1413
|
+
if (!verdict.allow) {
|
|
1414
|
+
refuseWith("401 Unauthorized", verdict.reason);
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
// The TLS path is for paired devices only: a bridge on the hub host uses its local socket.
|
|
1418
|
+
if (verdict.admission !== "device-ticket" || !verdict.deviceId) {
|
|
1419
|
+
refuseWith("401 Unauthorized", "tls-deviceless-ticket");
|
|
1420
|
+
return;
|
|
1421
|
+
}
|
|
1422
|
+
tlsUpgrades.set(req, {
|
|
1423
|
+
admission: "device-ticket",
|
|
1424
|
+
provenTls: isProvenTls(req),
|
|
1425
|
+
ticketTls: verdict.ticketTls === true,
|
|
1426
|
+
ticketDeviceId: verdict.deviceId,
|
|
1427
|
+
});
|
|
1428
|
+
upgradeDeviceId.set(req, verdict.deviceId);
|
|
1429
|
+
target.handleUpgrade(req, socket as never, head, (ws) => {
|
|
1430
|
+
target.emit("connection", ws, req);
|
|
1431
|
+
});
|
|
1432
|
+
},
|
|
1258
1433
|
attachLocalConnection(socket: LocalGatewaySocket) {
|
|
1259
1434
|
handleConnection(socket as unknown as WebSocket, undefined, "local");
|
|
1260
1435
|
},
|
|
@@ -1295,26 +1470,17 @@ export function createPiGateway(
|
|
|
1295
1470
|
startHeartbeat();
|
|
1296
1471
|
},
|
|
1297
1472
|
start(port: number, host?: string, startOpts?: { kind?: "tcp" | "loopback" }) {
|
|
1298
|
-
startTcp(port, host, { kind: startOpts?.kind ?? "tcp" });
|
|
1473
|
+
tcpBound = startTcp(port, host, { kind: startOpts?.kind ?? "tcp" });
|
|
1474
|
+
},
|
|
1475
|
+
tcpListening() {
|
|
1476
|
+
return tcpBound;
|
|
1299
1477
|
},
|
|
1300
1478
|
async startLoopbackFallback(port: number, reason: "occupied" | "unsupported") {
|
|
1301
1479
|
if (!options?.bridgeAuth) {
|
|
1302
1480
|
// Fail closed: the fallback is only defensible behind an auth gate.
|
|
1303
1481
|
throw new Error("pi-gateway: the loopback fallback requires bridge auth");
|
|
1304
1482
|
}
|
|
1305
|
-
startTcp(port, "127.0.0.1", { kind: "loopback-fallback", requireLocalCredential: true });
|
|
1306
|
-
const server = wss as WebSocketServer;
|
|
1307
|
-
try {
|
|
1308
|
-
await new Promise<void>((resolve, reject) => {
|
|
1309
|
-
server.once("listening", resolve);
|
|
1310
|
-
server.once("error", reject);
|
|
1311
|
-
});
|
|
1312
|
-
} catch (err) {
|
|
1313
|
-
server.close();
|
|
1314
|
-
wss = null;
|
|
1315
|
-
tcpKind = null;
|
|
1316
|
-
throw err;
|
|
1317
|
-
}
|
|
1483
|
+
await startTcp(port, "127.0.0.1", { kind: "loopback-fallback", requireLocalCredential: true });
|
|
1318
1484
|
fallbackReason = reason;
|
|
1319
1485
|
},
|
|
1320
1486
|
bridgeListeners() {
|
|
@@ -1356,7 +1522,7 @@ export function createPiGateway(
|
|
|
1356
1522
|
// would leave the socket path behind with no trace of why.
|
|
1357
1523
|
const pending = unbindGatewaySocket(socketServer, socketPath);
|
|
1358
1524
|
const path = socketPath;
|
|
1359
|
-
pending.catch((err: unknown) => {
|
|
1525
|
+
pendingTeardown = pending.catch((err: unknown) => {
|
|
1360
1526
|
console.error(`[gateway] failed to remove socket ${path}: ${String(err)}`);
|
|
1361
1527
|
});
|
|
1362
1528
|
socketServer = null;
|
|
@@ -1364,6 +1530,10 @@ export function createPiGateway(
|
|
|
1364
1530
|
}
|
|
1365
1531
|
},
|
|
1366
1532
|
|
|
1533
|
+
stopped(): Promise<void> {
|
|
1534
|
+
return pendingTeardown;
|
|
1535
|
+
},
|
|
1536
|
+
|
|
1367
1537
|
sendToSession(sessionId: string, msg: ServerToExtensionMessage): boolean {
|
|
1368
1538
|
const ws = connections.get(sessionId);
|
|
1369
1539
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
@@ -152,7 +152,7 @@ export async function resolveToggleTrust(
|
|
|
152
152
|
// ancestor: prompt (even under `never`) with "Trust" pre-selected. Nothing is
|
|
153
153
|
// recorded until the operator chooses — the decision is a lasting pi record that
|
|
154
154
|
// terminal pi also honours. See change: mirror-dashboard-trust-to-pi.
|
|
155
|
-
const covering = entry === null ? coveringDashboardEntry(cwd) : null;
|
|
155
|
+
const covering = entry === null ? coveringDashboardEntry(cwd, undefined, { live: false }) : null;
|
|
156
156
|
if (covering) {
|
|
157
157
|
const source = MIRROR_SOURCE_LABELS[covering.sourceKind];
|
|
158
158
|
return {
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side wiring of plugin discovery after boot: `refreshPluginDiscovery()` (additive load
|
|
3
|
+
* of new plugins, restart flags, ONE `plugins_changed`) and local-scope detection for active
|
|
4
|
+
* session cwds. Pure composition of the runtime's `createPluginRefresher` +
|
|
5
|
+
* `createLocalScopeDetector` over the loader deps the boot pass used.
|
|
6
|
+
*
|
|
7
|
+
* See change: decouple-plugins-from-core (tasks 5.5, 5.9).
|
|
8
|
+
*/
|
|
9
|
+
import {
|
|
10
|
+
createLocalScopeDetector,
|
|
11
|
+
createPluginRefresher,
|
|
12
|
+
getPluginStatusStore,
|
|
13
|
+
type PluginRefresherDeps,
|
|
14
|
+
} from "@blackbelt-technology/dashboard-plugin-runtime/server";
|
|
15
|
+
import type { PluginStatus } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/plugin-status.js";
|
|
16
|
+
|
|
17
|
+
export interface PluginDiscoveryWiringDeps {
|
|
18
|
+
/** The deps of the boot `loadServerEntries` call. */
|
|
19
|
+
loadDeps: PluginRefresherDeps["loadDeps"];
|
|
20
|
+
/** Re-read the served declaration's `bundledPluginIds` (`undefined` = undeclared). */
|
|
21
|
+
readDeclaredBundledIds: () => readonly string[] | undefined;
|
|
22
|
+
/** Drops the content-hash stat cache: on every discovery refresh (via the refresher) and package-operation completion (task 6.7). */
|
|
23
|
+
clearContentHashCache?: () => void;
|
|
24
|
+
/** After every completed discovery refresh: the structured runtime-admission log (task 6.20). */
|
|
25
|
+
logAdmissions?: () => void;
|
|
26
|
+
/** Cwds of the active (not ended) sessions, live. */
|
|
27
|
+
listActiveCwds: () => readonly string[];
|
|
28
|
+
/** Broadcast one `plugins_changed` to browsers. */
|
|
29
|
+
broadcastPluginsChanged: (plugins: PluginStatus[]) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PluginDiscoveryWiring {
|
|
33
|
+
/** Coalesced: concurrent calls collapse into at most one follow-up run. Always ends in ONE `plugins_changed`. */
|
|
34
|
+
refreshPluginDiscovery(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Rerun local-scope detection (e.g. after a successful `--scope local` package op). Broadcasts one
|
|
37
|
+
* `plugins_changed` only when the detected set changed. Returns whether it changed.
|
|
38
|
+
*/
|
|
39
|
+
rerunLocalDetection(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Completion hook of a package operation (after `package_operation_complete` was broadcast).
|
|
42
|
+
* Failed op → nothing. Global scope (or a `move`, which touches both scopes) → one coalesced
|
|
43
|
+
* discovery refresh. Local scope → local-scope detection only, and exactly ONE `plugins_changed`.
|
|
44
|
+
* Resolves when the refresh finished (local: immediately).
|
|
45
|
+
*/
|
|
46
|
+
onPackageOperationComplete(result: { success: boolean; scope: string; action?: string }): Promise<void>;
|
|
47
|
+
/** A session registered: rerun detection only when its cwd was not already scanned. */
|
|
48
|
+
onSessionRegistered(cwd: string): void;
|
|
49
|
+
/** A session ended (already excluded from `listActiveCwds`): rerun detection for the remaining cwds. */
|
|
50
|
+
onSessionEnded(): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createPluginDiscoveryWiring(deps: PluginDiscoveryWiringDeps): PluginDiscoveryWiring {
|
|
54
|
+
let scannedCwds = new Set<string>();
|
|
55
|
+
const detector = createLocalScopeDetector({
|
|
56
|
+
listCwds: () => {
|
|
57
|
+
const cwds = deps.listActiveCwds();
|
|
58
|
+
scannedCwds = new Set(cwds);
|
|
59
|
+
return cwds;
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const refresher = createPluginRefresher({
|
|
63
|
+
loadDeps: deps.loadDeps,
|
|
64
|
+
readDeclaredBundledIds: deps.readDeclaredBundledIds,
|
|
65
|
+
clearContentHashCache: deps.clearContentHashCache,
|
|
66
|
+
localDetector: detector,
|
|
67
|
+
broadcast: deps.broadcastPluginsChanged,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
async function refreshPluginDiscovery(): Promise<void> {
|
|
71
|
+
await refresher.refreshPluginDiscovery();
|
|
72
|
+
deps.logAdmissions?.();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function rerunLocalDetection(): boolean {
|
|
76
|
+
const changed = detector.rerun();
|
|
77
|
+
if (changed) deps.broadcastPluginsChanged(getPluginStatusStore().listAll());
|
|
78
|
+
return changed;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function onPackageOperationComplete(result: { success: boolean; scope: string; action?: string }): Promise<void> {
|
|
82
|
+
// Every package-operation completion (either scope, even a failed one that may have touched files) drops the
|
|
83
|
+
// content-hash stat cache, so a trust decision after `pi install` always sees a full rehash (design D12, R2).
|
|
84
|
+
deps.clearContentHashCache?.();
|
|
85
|
+
if (!result.success) return;
|
|
86
|
+
if (result.scope === "local" && result.action !== "move") {
|
|
87
|
+
// `rerunLocalDetection` already broadcast when the set changed; otherwise broadcast once anyway.
|
|
88
|
+
if (!rerunLocalDetection()) deps.broadcastPluginsChanged(getPluginStatusStore().listAll());
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await refreshPluginDiscovery();
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error("[plugin-loader] refresh after package operation failed:", e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
refreshPluginDiscovery,
|
|
100
|
+
rerunLocalDetection,
|
|
101
|
+
onPackageOperationComplete,
|
|
102
|
+
onSessionRegistered(cwd) {
|
|
103
|
+
if (!scannedCwds.has(cwd)) rerunLocalDetection();
|
|
104
|
+
},
|
|
105
|
+
onSessionEnded() {
|
|
106
|
+
rerunLocalDetection();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface PackageCompletionDeps<R extends { success: boolean; scope: string; action?: string }> {
|
|
112
|
+
/** Broadcast `package_operation_complete` (always, success or not). */
|
|
113
|
+
broadcastComplete(result: R): void;
|
|
114
|
+
invalidateRecommendedCache(): void;
|
|
115
|
+
/** Late-bound: the wiring exists only after the boot load pass. */
|
|
116
|
+
discovery(): Pick<PluginDiscoveryWiring, "onPackageOperationComplete"> | undefined;
|
|
117
|
+
/** Requirement-probe refresh; runs for every successful op of either scope. */
|
|
118
|
+
refreshProbes(): Promise<void> | void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The package-operation completion listener, as one ordered unit: completion broadcast, then (only on
|
|
123
|
+
* success) recommended-cache invalidation, the discovery refresh / local detection (ONE `plugins_changed`)
|
|
124
|
+
* and the requirement-probe refresh. A failed op does nothing past the broadcast.
|
|
125
|
+
*
|
|
126
|
+
* See change: decouple-plugins-from-core (task 5.7, test-plan #E56).
|
|
127
|
+
*/
|
|
128
|
+
export function createPackageCompletionHandler<R extends { success: boolean; scope: string; action?: string }>(
|
|
129
|
+
deps: PackageCompletionDeps<R>,
|
|
130
|
+
): (result: R) => void {
|
|
131
|
+
return (result) => {
|
|
132
|
+
deps.broadcastComplete(result);
|
|
133
|
+
if (!result.success) return;
|
|
134
|
+
deps.invalidateRecommendedCache();
|
|
135
|
+
deps.discovery()?.onPackageOperationComplete(result).catch((e) => {
|
|
136
|
+
console.error("[plugin-loader] discovery after package operation failed:", e);
|
|
137
|
+
});
|
|
138
|
+
void Promise.resolve(deps.refreshProbes()).catch((e) => {
|
|
139
|
+
console.error("[plugin-loader] requirement-probe refresh after package operation failed:", e);
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
}
|