@blackbelt-technology/pi-agent-dashboard 0.10.0 → 0.12.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.
Files changed (474) hide show
  1. package/AGENTS.md +4 -2
  2. package/README.md +64 -18
  3. package/docs/architecture.md +2325 -270
  4. package/package.json +36 -20
  5. package/packages/extension/package.json +18 -9
  6. package/packages/extension/src/bridge-context.ts +6 -0
  7. package/packages/extension/src/bridge-handlers/dispatch.ts +109 -70
  8. package/packages/extension/src/bridge-handlers/forwarders.ts +19 -42
  9. package/packages/extension/src/bridge-handlers/instance.ts +31 -13
  10. package/packages/extension/src/bridge-handlers/lifecycle.ts +149 -66
  11. package/packages/extension/src/bridge-shutdown.ts +44 -0
  12. package/packages/extension/src/bridge-ticket-client.ts +61 -7
  13. package/packages/extension/src/bridge-tls-checks.ts +79 -0
  14. package/packages/extension/src/bridge.ts +309 -284
  15. package/packages/extension/src/canvas-tool.ts +2 -0
  16. package/packages/extension/src/command-handler.ts +106 -37
  17. package/packages/extension/src/connect-target.ts +23 -0
  18. package/packages/extension/src/connection.ts +153 -38
  19. package/packages/extension/src/credential-guard.ts +300 -0
  20. package/packages/extension/src/custom-entry-forward.ts +27 -18
  21. package/packages/extension/src/dashboard-http-origin.ts +88 -0
  22. package/packages/extension/src/default-model-apply.ts +83 -0
  23. package/packages/extension/src/dev-build.ts +11 -1
  24. package/packages/extension/src/flow-event-wiring.ts +19 -95
  25. package/packages/extension/src/followup-drain.ts +19 -5
  26. package/packages/extension/src/git-link-builder.ts +11 -2
  27. package/packages/extension/src/inbound-limits.ts +138 -0
  28. package/packages/extension/src/local-token-header.ts +77 -6
  29. package/packages/extension/src/model-refresh.ts +69 -0
  30. package/packages/extension/src/path-gate/decide.ts +68 -5
  31. package/packages/extension/src/path-gate/handler.ts +16 -8
  32. package/packages/extension/src/path-gate/index.ts +2 -2
  33. package/packages/extension/src/path-gate/roots.ts +15 -3
  34. package/packages/extension/src/path-gate/suppression.ts +3 -0
  35. package/packages/extension/src/pi-env.d.ts +8 -1
  36. package/packages/extension/src/pi-retry-settings.ts +11 -2
  37. package/packages/extension/src/pinned-endpoint.ts +44 -0
  38. package/packages/extension/src/plugin-event-forward-registry.ts +90 -14
  39. package/packages/extension/src/preview-path-link.ts +151 -0
  40. package/packages/extension/src/project-init/scaffold.ts +98 -35
  41. package/packages/extension/src/project-trust-live.ts +143 -0
  42. package/packages/extension/src/project-trust.ts +178 -30
  43. package/packages/extension/src/provider-model-cache.ts +118 -0
  44. package/packages/extension/src/provider-register.ts +487 -46
  45. package/packages/extension/src/remote-registration-gate.ts +21 -21
  46. package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
  47. package/packages/extension/src/retry-tracker.ts +90 -7
  48. package/packages/extension/src/role-manager.ts +4 -2
  49. package/packages/extension/src/role-model-tools.ts +68 -10
  50. package/packages/extension/src/server-auto-start.ts +69 -3
  51. package/packages/extension/src/server-launcher.ts +26 -8
  52. package/packages/extension/src/server-proof-probe.ts +117 -0
  53. package/packages/extension/src/session-sync.ts +53 -24
  54. package/packages/extension/src/spawn-new-session.ts +22 -0
  55. package/packages/extension/src/steer-buffer.ts +164 -0
  56. package/packages/extension/src/stream-forward-buffer.ts +64 -15
  57. package/packages/extension/src/sudo-askpass/config-reader.ts +32 -0
  58. package/packages/extension/src/sudo-askpass/controller.ts +405 -0
  59. package/packages/extension/src/sudo-askpass/helper-script.ts +69 -0
  60. package/packages/extension/src/sudo-askpass/in-flight.ts +79 -0
  61. package/packages/extension/src/sudo-askpass/index.ts +11 -0
  62. package/packages/extension/src/sudo-askpass/install.ts +143 -0
  63. package/packages/extension/src/sudo-askpass/password-cache.ts +118 -0
  64. package/packages/extension/src/sudo-askpass/prompt-text.ts +55 -0
  65. package/packages/extension/src/sudo-askpass/protocol.ts +18 -0
  66. package/packages/extension/src/sudo-askpass/sudo-mask-registry.ts +52 -0
  67. package/packages/extension/src/terminal-reload.ts +19 -5
  68. package/packages/extension/src/tool-update-filter-registry.ts +210 -0
  69. package/packages/extension/src/vault-delivery.ts +11 -1
  70. package/packages/extension/src/vault-redaction.ts +4 -3
  71. package/packages/server/package.json +17 -14
  72. package/packages/server/src/access/access-denials.ts +8 -0
  73. package/packages/server/src/access/access-grants.ts +303 -64
  74. package/packages/server/src/access/access-health.ts +10 -0
  75. package/packages/server/src/access/access-plane.ts +12 -1
  76. package/packages/server/src/access/containment-gate.ts +113 -30
  77. package/packages/server/src/access/cwd-verdict.ts +3 -3
  78. package/packages/server/src/access/denial-hold.ts +3 -0
  79. package/packages/server/src/access/grant-coordinator.ts +17 -1
  80. package/packages/server/src/access/named-grant.ts +3 -0
  81. package/packages/server/src/access/pending-grant-registry.ts +32 -5
  82. package/packages/server/src/access/pi-trust-live.ts +244 -0
  83. package/packages/server/src/access/pi-trust-mirror-service.ts +50 -11
  84. package/packages/server/src/access/planes.ts +107 -1
  85. package/packages/server/src/access/preview-egress-store.ts +153 -0
  86. package/packages/server/src/access/prompt-channel.ts +5 -0
  87. package/packages/server/src/access/provisioning-wiring.ts +113 -0
  88. package/packages/server/src/access/provisioning.ts +763 -0
  89. package/packages/server/src/access/verified-read.ts +24 -7
  90. package/packages/server/src/access/yolo-session.ts +54 -2
  91. package/packages/server/src/acp-bridge/acp-session-adapter.ts +18 -5
  92. package/packages/server/src/acp-bridge/server-requests.ts +10 -4
  93. package/packages/server/src/agent-events/agent-event-hub.ts +158 -0
  94. package/packages/server/src/auth/audit-log.ts +108 -0
  95. package/packages/server/src/auth/auth-plugin.ts +230 -38
  96. package/packages/server/src/auth/auth.ts +104 -4
  97. package/packages/server/src/auth/bearer-auth.ts +52 -12
  98. package/packages/server/src/auth/bridge-ticket-eligibility.ts +53 -10
  99. package/packages/server/src/auth/cors-origin.ts +90 -30
  100. package/packages/server/src/auth/credential-envelope.ts +233 -0
  101. package/packages/server/src/auth/credential-key-worker.d.mts +16 -0
  102. package/packages/server/src/auth/credential-key-worker.mjs +108 -0
  103. package/packages/server/src/auth/credential-key.ts +479 -0
  104. package/packages/server/src/auth/credential-migration.ts +162 -0
  105. package/packages/server/src/auth/csp-report-paths.ts +26 -0
  106. package/packages/server/src/auth/csp-report-sink.ts +302 -0
  107. package/packages/server/src/auth/csp-report-store.ts +406 -0
  108. package/packages/server/src/auth/csp.ts +145 -11
  109. package/packages/server/src/auth/dashboard-credential-store.ts +4 -0
  110. package/packages/server/src/auth/dashboard-state-caller.ts +29 -0
  111. package/packages/server/src/auth/device-proof.ts +202 -0
  112. package/packages/server/src/auth/duplicate-cookie-gate.ts +271 -0
  113. package/packages/server/src/auth/host-admission.ts +7 -0
  114. package/packages/server/src/auth/host-gate.ts +13 -2
  115. package/packages/server/src/auth/local-proof-refusal-log.ts +89 -0
  116. package/packages/server/src/auth/local-proof.ts +69 -18
  117. package/packages/server/src/auth/localhost-guard.ts +98 -33
  118. package/packages/server/src/auth/locked-json-file.ts +68 -6
  119. package/packages/server/src/auth/models-json-store.ts +269 -0
  120. package/packages/server/src/auth/mutation-origin-gate.ts +5 -2
  121. package/packages/server/src/auth/nav-intent.ts +72 -0
  122. package/packages/server/src/auth/node-guard.ts +36 -49
  123. package/packages/server/src/auth/passkey/passkey-assets.ts +99 -0
  124. package/packages/server/src/auth/passkey/passkey-pages.ts +17 -81
  125. package/packages/server/src/auth/passkey/passkey-routes.ts +8 -0
  126. package/packages/server/src/auth/pi-legacy-credentials.ts +3 -0
  127. package/packages/server/src/auth/plugin-credential-store.ts +81 -10
  128. package/packages/server/src/auth/plugin-server-trust.ts +29 -0
  129. package/packages/server/src/auth/plugin-trust-store.ts +103 -0
  130. package/packages/server/src/auth/proven-tls.ts +45 -0
  131. package/packages/server/src/auth/provider-auth-storage.ts +214 -12
  132. package/packages/server/src/auth/provider-block-checks.ts +321 -0
  133. package/packages/server/src/auth/provider-host-detect.ts +151 -0
  134. package/packages/server/src/auth/provider-key-file.ts +71 -0
  135. package/packages/server/src/auth/provider-readiness.ts +91 -0
  136. package/packages/server/src/auth/radius-mcp.ts +1 -1
  137. package/packages/server/src/auth/request-principal.ts +32 -0
  138. package/packages/server/src/auth/route-tier-gate.ts +3 -2
  139. package/packages/server/src/auth/secret-rotation.ts +246 -0
  140. package/packages/server/src/auth/secure-origin.ts +56 -0
  141. package/packages/server/src/auth/serve-app-host.ts +111 -11
  142. package/packages/server/src/auth/vertex-key-check.ts +140 -0
  143. package/packages/server/src/auth/ws-ticket.ts +40 -2
  144. package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
  145. package/packages/server/src/browse.ts +73 -20
  146. package/packages/server/src/browser-handlers/acp-guard.ts +1 -4
  147. package/packages/server/src/browser-handlers/directory-handler.ts +34 -24
  148. package/packages/server/src/browser-handlers/folder-gate.ts +55 -0
  149. package/packages/server/src/browser-handlers/handler-context.ts +25 -2
  150. package/packages/server/src/browser-handlers/session-action-handler.ts +167 -63
  151. package/packages/server/src/browser-handlers/spawn-choice.ts +138 -1
  152. package/packages/server/src/browser-handlers/subscription-handler.ts +146 -3
  153. package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
  154. package/packages/server/src/build-plugin-send-prompt.ts +18 -0
  155. package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
  156. package/packages/server/src/cli/credentials-command.ts +555 -0
  157. package/packages/server/src/cli.ts +229 -53
  158. package/packages/server/src/config-api.ts +53 -35
  159. package/packages/server/src/config-snapshot.ts +197 -1
  160. package/packages/server/src/directory-service.ts +193 -68
  161. package/packages/server/src/embed-lifecycle/idle-reaper.ts +13 -3
  162. package/packages/server/src/event-wiring.ts +322 -207
  163. package/packages/server/src/git-worktree/ensure-worktree.ts +185 -0
  164. package/packages/server/src/git-worktree/git-operations.ts +45 -11
  165. package/packages/server/src/git-worktree/redact-git-text.ts +124 -0
  166. package/packages/server/src/git-worktree/ref-arg.ts +39 -0
  167. package/packages/server/src/git-worktree/repo-lock.ts +42 -0
  168. package/packages/server/src/git-worktree/start-worktree-init.ts +151 -0
  169. package/packages/server/src/git-worktree/worktree-init-trust.ts +43 -1
  170. package/packages/server/src/git-worktree/worktree-init.ts +5 -1
  171. package/packages/server/src/identity/http-road-classification.ts +18 -2
  172. package/packages/server/src/identity/identity-floor.ts +5 -2
  173. package/packages/server/src/identity/ws-message-scope.ts +11 -3
  174. package/packages/server/src/identity/ws-road-classification.ts +3 -1
  175. package/packages/server/src/known-server-moves.ts +36 -15
  176. package/packages/server/src/lib/browse-roots.ts +129 -17
  177. package/packages/server/src/lib/client-dist.ts +36 -21
  178. package/packages/server/src/lib/content-disposition.ts +20 -0
  179. package/packages/server/src/lib/dashboard-state-guard.ts +513 -0
  180. package/packages/server/src/lib/diagram-render.ts +4 -6
  181. package/packages/server/src/lib/eml.ts +5 -10
  182. package/packages/server/src/lib/file-cwd-admission.ts +145 -0
  183. package/packages/server/src/lib/frame-policy-probe.ts +308 -0
  184. package/packages/server/src/lib/frame-policy-test-seam.ts +99 -0
  185. package/packages/server/src/lib/frame-policy-verdict.ts +125 -0
  186. package/packages/server/src/lib/grep-worker.ts +23 -0
  187. package/packages/server/src/lib/grep.ts +96 -7
  188. package/packages/server/src/lib/index-html.ts +81 -0
  189. package/packages/server/src/lib/mention-fuzzy-index.ts +263 -0
  190. package/packages/server/src/lib/office-preview.ts +49 -13
  191. package/packages/server/src/lib/path-containment.ts +50 -10
  192. package/packages/server/src/lib/resolve-file-mention.ts +234 -40
  193. package/packages/server/src/lib/resolve-mentions-batch.ts +143 -0
  194. package/packages/server/src/lib/route-admission.ts +80 -0
  195. package/packages/server/src/lib/writable-handler-target.ts +4 -0
  196. package/packages/server/src/lib/writable-md-target.ts +26 -0
  197. package/packages/server/src/lifecycle/boot-parent-liveness.ts +10 -25
  198. package/packages/server/src/lifecycle/credential-exit-barrier.ts +207 -0
  199. package/packages/server/src/lifecycle/exit-signal.ts +43 -0
  200. package/packages/server/src/lifecycle/recovery-server.ts +65 -18
  201. package/packages/server/src/live-server/capability-redact.ts +31 -0
  202. package/packages/server/src/live-server/live-browser-gate.ts +22 -0
  203. package/packages/server/src/live-server/live-capability.ts +615 -44
  204. package/packages/server/src/live-server/live-csp-report.ts +63 -0
  205. package/packages/server/src/live-server/live-csp.ts +141 -0
  206. package/packages/server/src/live-server/live-egress.ts +435 -0
  207. package/packages/server/src/live-server/live-exposure.ts +149 -0
  208. package/packages/server/src/live-server/live-log.ts +25 -0
  209. package/packages/server/src/live-server/live-origin-scan.ts +298 -0
  210. package/packages/server/src/live-server/live-server-manager.ts +220 -33
  211. package/packages/server/src/live-server/live-server-proxy.ts +411 -50
  212. package/packages/server/src/live-server/live-static.ts +231 -0
  213. package/packages/server/src/live-server/preview-dispatch.ts +286 -0
  214. package/packages/server/src/live-server/preview-observe.ts +53 -0
  215. package/packages/server/src/live-server/preview-origin.ts +182 -0
  216. package/packages/server/src/live-server/preview-path-bridge.ts +70 -0
  217. package/packages/server/src/live-server/preview-path-store.ts +420 -0
  218. package/packages/server/src/live-server/preview-path.ts +40 -0
  219. package/packages/server/src/live-server/preview-pool.ts +385 -0
  220. package/packages/server/src/live-server/preview-preflight.ts +41 -0
  221. package/packages/server/src/live-server/preview-reserve.ts +44 -0
  222. package/packages/server/src/live-server/preview-tier.ts +217 -0
  223. package/packages/server/src/live-server/preview-viewers.ts +54 -0
  224. package/packages/server/src/live-server/preview-ws-registry.ts +43 -0
  225. package/packages/server/src/model-proxy/custom-provider-discovery.ts +4 -2
  226. package/packages/server/src/model-proxy/internal-auth-storage.ts +5 -1
  227. package/packages/server/src/model-proxy/internal-registry.ts +283 -21
  228. package/packages/server/src/model-proxy/models-block-apply.ts +291 -0
  229. package/packages/server/src/model-proxy/pi-models-validator.ts +164 -0
  230. package/packages/server/src/model-proxy/registry-singleton.ts +44 -9
  231. package/packages/server/src/model-proxy/server-model-runtime.ts +17 -0
  232. package/packages/server/src/openspec/dependencies-file.ts +122 -0
  233. package/packages/server/src/openspec/openspec-change-watcher.ts +3 -1
  234. package/packages/server/src/openspec/openspec-dependency-inputs.ts +281 -0
  235. package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +72 -26
  236. package/packages/server/src/openspec/openspec-poll-worker-pool.ts +48 -12
  237. package/packages/server/src/openspec/openspec-poll-worker.ts +95 -9
  238. package/packages/server/src/openspec/readiness.ts +1 -1
  239. package/packages/server/src/openspec/workflow-schema-status.ts +379 -0
  240. package/packages/server/src/openspec/workflow-schema.ts +141 -267
  241. package/packages/server/src/package/npm-search-proxy.ts +5 -2
  242. package/packages/server/src/package/provider-probe.ts +202 -7
  243. package/packages/server/src/pairing/browser-gateway.ts +325 -51
  244. package/packages/server/src/pairing/device-key-intake.ts +120 -0
  245. package/packages/server/src/pairing/device-token-services.ts +26 -0
  246. package/packages/server/src/pairing/paired-devices.ts +702 -27
  247. package/packages/server/src/pairing/pairing.ts +103 -11
  248. package/packages/server/src/pairing/{subagent-resync-routing.ts → resync-requester-registry.ts} +6 -10
  249. package/packages/server/src/pairing/socket-locality.ts +26 -0
  250. package/packages/server/src/pairing/ws-tier-gate.ts +74 -0
  251. package/packages/server/src/pending/pending-attach-registry.ts +15 -0
  252. package/packages/server/src/pending/pending-initial-prompt-registry.ts +15 -0
  253. package/packages/server/src/persistence/memory-event-store.ts +251 -296
  254. package/packages/server/src/persistence/preferences-store.ts +61 -2
  255. package/packages/server/src/pi/bridge-channel.ts +33 -0
  256. package/packages/server/src/pi/bridge-contention.ts +84 -2
  257. package/packages/server/src/pi/bridge-upgrade-auth.ts +18 -3
  258. package/packages/server/src/pi/gateway-listeners.ts +9 -3
  259. package/packages/server/src/pi/host-project-trust.ts +29 -15
  260. package/packages/server/src/pi/pi-gateway.ts +398 -41
  261. package/packages/server/src/pi/provisional-registration.ts +4 -1
  262. package/packages/server/src/pi/resource-toggle-trust.ts +18 -7
  263. package/packages/server/src/pi-agent-settings.ts +95 -301
  264. package/packages/server/src/pi-default-remirror.ts +89 -0
  265. package/packages/server/src/plugin-append-event.ts +35 -0
  266. package/packages/server/src/plugin-discovery-wiring.ts +142 -0
  267. package/packages/server/src/plugin-entry-stats.ts +61 -0
  268. package/packages/server/src/plugin-event-seam.ts +116 -4
  269. package/packages/server/src/plugin-local-request.ts +207 -0
  270. package/packages/server/src/plugin-ownership.ts +111 -0
  271. package/packages/server/src/plugin-policy-registry.ts +297 -0
  272. package/packages/server/src/plugin-request-lane.ts +92 -16
  273. package/packages/server/src/plugin-runtime-clients.ts +89 -0
  274. package/packages/server/src/preferences-relay.ts +20 -0
  275. package/packages/server/src/project-seed.ts +347 -0
  276. package/packages/server/src/push/push-transports/webhook-url.ts +8 -5
  277. package/packages/server/src/questions/question-store.ts +3 -2
  278. package/packages/server/src/routes/access-routes.ts +182 -15
  279. package/packages/server/src/routes/acme-routes.ts +14 -4
  280. package/packages/server/src/routes/agent-event-stream-routes.ts +137 -0
  281. package/packages/server/src/routes/canvas-types-routes.ts +20 -6
  282. package/packages/server/src/routes/csp-violations-routes.ts +162 -0
  283. package/packages/server/src/routes/file-routes.ts +640 -279
  284. package/packages/server/src/routes/file-write-binary-routes.ts +97 -24
  285. package/packages/server/src/routes/folder-project-routes.ts +18 -12
  286. package/packages/server/src/routes/frame-policy-routes.ts +215 -0
  287. package/packages/server/src/routes/git-routes.ts +340 -273
  288. package/packages/server/src/routes/grep-routes.ts +64 -14
  289. package/packages/server/src/routes/known-servers-routes.ts +189 -91
  290. package/packages/server/src/routes/live-server-routes.ts +572 -15
  291. package/packages/server/src/routes/local-proof-routes.ts +14 -0
  292. package/packages/server/src/routes/models-introspection-routes.ts +1 -1
  293. package/packages/server/src/routes/nav-intent-routes.ts +46 -0
  294. package/packages/server/src/routes/openspec-group-routes.ts +53 -44
  295. package/packages/server/src/routes/openspec-routes.ts +396 -31
  296. package/packages/server/src/routes/package-routes.ts +58 -10
  297. package/packages/server/src/routes/pairing-routes.ts +342 -17
  298. package/packages/server/src/routes/pi-settings-routes.ts +2 -0
  299. package/packages/server/src/routes/pi-trust-query-routes.ts +330 -0
  300. package/packages/server/src/routes/plugin-activation-routes.ts +59 -0
  301. package/packages/server/src/routes/plugin-runtime-client-routes.ts +149 -0
  302. package/packages/server/src/routes/preferences-auto-name-routes.ts +5 -2
  303. package/packages/server/src/routes/preferences-claude-compat-routes.ts +49 -0
  304. package/packages/server/src/routes/provider-auth-routes.ts +181 -10
  305. package/packages/server/src/routes/provider-override-routes.ts +158 -0
  306. package/packages/server/src/routes/provider-routes.ts +348 -86
  307. package/packages/server/src/routes/resource-activation-routes.ts +18 -4
  308. package/packages/server/src/routes/session-routes.ts +75 -25
  309. package/packages/server/src/routes/system-routes.ts +169 -52
  310. package/packages/server/src/routes/ui-link-base.ts +88 -0
  311. package/packages/server/src/rpc-keeper/keeper-manager.ts +196 -25
  312. package/packages/server/src/rpc-keeper/keeper.cjs +37 -23
  313. package/packages/server/src/runtime-overlay/extension-reload.ts +16 -10
  314. package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
  315. package/packages/server/src/runtime-overlay/runtime-stager.ts +4 -2
  316. package/packages/server/src/server.ts +1139 -291
  317. package/packages/server/src/services/cli-service.ts +59 -10
  318. package/packages/server/src/services/secrets-resolver.ts +46 -30
  319. package/packages/server/src/session/custom-event-group-annotation.ts +3 -3
  320. package/packages/server/src/session/custom-event-group-resolver.ts +10 -5
  321. package/packages/server/src/session/death-reason.ts +11 -7
  322. package/packages/server/src/session/event-status-extraction.ts +5 -1
  323. package/packages/server/src/session/hydration-state.ts +148 -0
  324. package/packages/server/src/session/memory-session-manager.ts +31 -2
  325. package/packages/server/src/session/open-tool-calls.ts +81 -47
  326. package/packages/server/src/session/pid-target.ts +127 -0
  327. package/packages/server/src/session/replay-compaction.ts +18 -5
  328. package/packages/server/src/session/replay-exit-controller.ts +221 -0
  329. package/packages/server/src/session/retained-transcript.ts +9 -2
  330. package/packages/server/src/session/seed-discovered-history.ts +85 -0
  331. package/packages/server/src/session/server-pid-context.ts +14 -0
  332. package/packages/server/src/session/session-api.ts +78 -29
  333. package/packages/server/src/session/session-archive.ts +7 -0
  334. package/packages/server/src/session/session-bootstrap.ts +9 -54
  335. package/packages/server/src/session/session-load-worker-pool.ts +11 -1
  336. package/packages/server/src/session/session-load-worker.ts +32 -4
  337. package/packages/server/src/spawn-process/abort-spawned-run.ts +28 -9
  338. package/packages/server/src/spawn-process/acp-spawn.ts +8 -2
  339. package/packages/server/src/spawn-process/headless-pid-registry.ts +393 -77
  340. package/packages/server/src/spawn-process/link-register.ts +42 -0
  341. package/packages/server/src/spawn-process/plugin-spawn-choice.ts +48 -0
  342. package/packages/server/src/spawn-process/plugin-spawn-worktree.ts +290 -0
  343. package/packages/server/src/spawn-process/process-classifier.ts +8 -3
  344. package/packages/server/src/spawn-process/process-manager.ts +61 -6
  345. package/packages/server/src/spawn-process/restored-identity.ts +112 -0
  346. package/packages/server/src/spawn-process/session-terminator.ts +179 -18
  347. package/packages/server/src/spawn-process/spawn-env-contributors.ts +2 -1
  348. package/packages/server/src/spawn-process/spawn-failure-log.ts +9 -0
  349. package/packages/server/src/spawn-process/spawn-register-watchdog.ts +44 -19
  350. package/packages/server/src/sudo-credential/relay.ts +337 -0
  351. package/packages/server/src/terminal/terminal-gateway.ts +10 -0
  352. package/packages/server/src/terminal/terminal-manager.ts +54 -46
  353. package/packages/server/src/tls/sources/acme/credentials.ts +3 -2
  354. package/packages/server/src/tls/sources/acme/runtime.ts +2 -1
  355. package/packages/server/src/tls/tls-bridge-route.ts +61 -0
  356. package/packages/server/src/tls/tls-config-validation.ts +2 -0
  357. package/packages/server/src/vault/device-expiry-sweep.ts +36 -0
  358. package/packages/server/src/vault/vault-delivery-hub.ts +172 -26
  359. package/packages/server/src/vault/vault-routes.ts +9 -2
  360. package/packages/server/src/vault/vault-store.ts +87 -24
  361. package/packages/server/src/watch-files-handler.ts +66 -0
  362. package/packages/server/vendor/xlsx/LICENSE +201 -0
  363. package/packages/server/vendor/xlsx/PROVENANCE.json +13 -0
  364. package/packages/server/vendor/xlsx/xlsx.d.mts +1065 -0
  365. package/packages/server/vendor/xlsx/xlsx.mjs +28228 -0
  366. package/packages/shared/package.json +4 -2
  367. package/packages/shared/src/agent-credential-files.ts +32 -0
  368. package/packages/shared/src/browser-protocol.ts +285 -59
  369. package/packages/shared/src/canvas-declare.ts +27 -0
  370. package/packages/shared/src/canvas-detect.ts +1 -1
  371. package/packages/shared/src/canvas-types.ts +2 -2
  372. package/packages/shared/src/config.ts +443 -187
  373. package/packages/shared/src/containment-guard.ts +45 -0
  374. package/packages/shared/src/credential-encryption.ts +87 -0
  375. package/packages/shared/src/custom-event-groups.ts +2 -0
  376. package/packages/shared/src/dashboard-plugin/claude-compat.ts +159 -0
  377. package/packages/shared/src/dashboard-plugin/event-type-owners.ts +50 -0
  378. package/packages/shared/src/dashboard-plugin/file-handler.ts +2 -0
  379. package/packages/shared/src/dashboard-plugin/host-api.ts +61 -0
  380. package/packages/shared/src/dashboard-plugin/host-fx-classes.ts +23 -0
  381. package/packages/shared/src/dashboard-plugin/host-import-map.ts +38 -0
  382. package/packages/shared/src/dashboard-plugin/index.ts +2 -0
  383. package/packages/shared/src/dashboard-plugin/manifest-types.ts +100 -1
  384. package/packages/shared/src/dashboard-plugin/plugin-status.ts +58 -3
  385. package/packages/shared/src/dashboard-plugin/replay-entry-types.ts +46 -0
  386. package/packages/shared/src/dashboard-plugin/slot-props.ts +47 -3
  387. package/packages/shared/src/dashboard-plugin/slot-types.ts +9 -1
  388. package/packages/shared/src/dashboard-plugin/ui-primitives.ts +39 -3
  389. package/packages/shared/src/device-key-proof.ts +309 -0
  390. package/packages/shared/src/display-prefs.ts +13 -0
  391. package/packages/shared/src/doctor-core.ts +94 -27
  392. package/packages/shared/src/event-forward-declaration.ts +85 -12
  393. package/packages/shared/src/forbidden-subjects.ts +126 -14
  394. package/packages/shared/src/heap-limits.ts +0 -30
  395. package/packages/shared/src/host-admission.ts +2 -1
  396. package/packages/shared/src/index.ts +7 -6
  397. package/packages/shared/src/live-server.ts +161 -0
  398. package/packages/shared/src/local-proof-signal.ts +16 -0
  399. package/packages/shared/src/loopback-host.ts +26 -0
  400. package/packages/shared/src/mcp/verb-tiers.generated.ts +176 -0
  401. package/packages/shared/src/mdns-discovery.ts +25 -6
  402. package/packages/shared/src/models-json-reader.ts +41 -0
  403. package/packages/shared/src/nav-intent.ts +29 -0
  404. package/packages/shared/src/network-denial-signal.ts +15 -0
  405. package/packages/shared/src/node-version.ts +63 -60
  406. package/packages/shared/src/openspec-dependencies.ts +760 -0
  407. package/packages/shared/src/openspec-poller.ts +3 -2
  408. package/packages/shared/src/pairing-knock-client.ts +135 -11
  409. package/packages/shared/src/pi-agent-settings-lock.ts +311 -0
  410. package/packages/shared/src/pi-path-compare.ts +31 -0
  411. package/packages/shared/src/pi-settings-schema.ts +52 -9
  412. package/packages/shared/src/pi-trust-live-query.ts +93 -0
  413. package/packages/shared/src/pi-trust-mirror-snapshot.ts +21 -11
  414. package/packages/shared/src/pi-trust-mirror-types.ts +38 -3
  415. package/packages/shared/src/pi-trust-mirror.ts +47 -11
  416. package/packages/shared/src/pi-trust-resource-digest.ts +261 -0
  417. package/packages/shared/src/pid-namespace.ts +86 -0
  418. package/packages/shared/src/platform/ensure-bundled-git.ts +57 -34
  419. package/packages/shared/src/platform/git-source.ts +88 -26
  420. package/packages/shared/src/platform/git.ts +14 -8
  421. package/packages/shared/src/platform/native-ts-register.mjs +1 -1
  422. package/packages/shared/src/platform/process.ts +452 -5
  423. package/packages/shared/src/platform/resolve-pi-shell.ts +105 -0
  424. package/packages/shared/src/platform/select-git-source.ts +7 -0
  425. package/packages/shared/src/platform/spawn-runtime.ts +193 -7
  426. package/packages/shared/src/plugin-route-cwd.ts +95 -0
  427. package/packages/shared/src/preview-egress.ts +192 -0
  428. package/packages/shared/src/preview-origins.ts +398 -0
  429. package/packages/shared/src/project-root.ts +16 -0
  430. package/packages/shared/src/prompt-answer.ts +9 -5
  431. package/packages/shared/src/protocol.ts +187 -86
  432. package/packages/shared/src/provider-env-catalog.ts +413 -0
  433. package/packages/shared/src/provider-overrides.ts +112 -0
  434. package/packages/shared/src/question-types.ts +6 -0
  435. package/packages/shared/src/recommended-extensions.ts +97 -26
  436. package/packages/shared/src/recovery-timing.ts +27 -0
  437. package/packages/shared/src/remote-credentials.ts +63 -0
  438. package/packages/shared/src/renderer-by-ext.ts +5 -2
  439. package/packages/shared/src/rest-api.ts +148 -11
  440. package/packages/shared/src/role-schema.ts +24 -0
  441. package/packages/shared/src/route-tiers.ts +149 -4
  442. package/packages/shared/src/runtime-overlay/archive-safety.d.mts +2 -0
  443. package/packages/shared/src/runtime-overlay/archive-safety.mjs +33 -0
  444. package/packages/shared/src/runtime-overlay/compat.ts +70 -6
  445. package/packages/shared/src/runtime-overlay/manifest.d.mts +7 -0
  446. package/packages/shared/src/runtime-overlay/manifest.mjs +22 -2
  447. package/packages/shared/src/safe-local-path.ts +24 -0
  448. package/packages/shared/src/sanitize-profiles.ts +98 -0
  449. package/packages/shared/src/secret-patterns.ts +49 -0
  450. package/packages/shared/src/server-launcher.ts +9 -1
  451. package/packages/shared/src/server-pinning.ts +55 -2
  452. package/packages/shared/src/server-routes.ts +462 -0
  453. package/packages/shared/src/session-ui-path.ts +11 -0
  454. package/packages/shared/src/source-matching.ts +2 -2
  455. package/packages/shared/src/state-replay.ts +40 -24
  456. package/packages/shared/src/sudo-credential.ts +71 -0
  457. package/packages/shared/src/tls-only-token.ts +16 -0
  458. package/packages/shared/src/tool-path-resolve.ts +16 -1
  459. package/packages/shared/src/tool-registry/definitions.ts +5 -5
  460. package/packages/shared/src/tool-registry/strategies.ts +13 -12
  461. package/packages/shared/src/tool-workdir.ts +56 -0
  462. package/packages/shared/src/trusted-types-mode.ts +72 -0
  463. package/packages/shared/src/types.ts +134 -109
  464. package/packages/shared/src/url-policy.ts +96 -0
  465. package/packages/shared/src/vault-delivery.ts +15 -0
  466. package/packages/shared/src/vertex-regions.ts +81 -0
  467. package/packages/shared/src/ws-message-tiers.ts +11 -3
  468. package/packages/extension/src/subagent-fanout-admission.ts +0 -234
  469. package/packages/extension/src/subagent-forward-sites.ts +0 -69
  470. package/packages/extension/src/subagent-frame-buffer.ts +0 -245
  471. package/packages/extension/src/subagent-frame-strip.ts +0 -109
  472. package/packages/extension/src/subagent-saturation.ts +0 -196
  473. package/packages/extension/src/subagent-tick-throttle.ts +0 -252
  474. package/packages/shared/src/anthropic-messages-source.ts +0 -8
@@ -0,0 +1,300 @@
1
+ /**
2
+ * Agent credential guard: blocks pi tool calls that read, write or list the
3
+ * dashboard credential files and pi's provider secrets, or that invoke the data key /
4
+ * decrypt. Protected set: `plugin-credentials.json`, `vault.json`, the data-key file and
5
+ * `PI_AGENT_SECRET_FILES` (`auth.json`, `mcp-auth.json`, `providers.json`,
6
+ * `system-one/auth.json`) of `~/.pi/agent` and `PI_CODING_AGENT_DIR`. Always on,
7
+ * no configuration, no switch (a switch is the first thing an injected prompt
8
+ * would ask for). Best-effort protection against ACCIDENTAL disclosure, not a
9
+ * sandbox: relative names from a nearby cwd, variable / string indirection,
10
+ * encoded strings and scripts written to disk are NOT detected.
11
+ *
12
+ * Two rule families, decided before the interactive path gate prompts:
13
+ * - path rules (`read`/`edit`/`write`/`ls`/`find`/`grep`): the canonical target
14
+ * (shared `tool-path-resolve` resolver, follows symlinks) is a protected file
15
+ * or a sibling `<name>.*`; `grep` is also blocked on a directory that DIRECTLY
16
+ * contains a protected file (rg reads hidden files). A missing/empty `path` on
17
+ * `grep`/`find`/`ls` is the session cwd, as in pi.
18
+ * - text rules (EVERY tool, built-in / extension / MCP): string values under the
19
+ * command-bearing fields only (`SCANNED_FIELDS`), never the `write`/`edit`
20
+ * content fields; path-like protected references (no bare basenames), the data
21
+ * key service + a keychain verb, `security dump-keychain`, `credentials decrypt`.
22
+ *
23
+ * Fail closed: an internal error blocks only the call being evaluated. Log lines
24
+ * name the tool and the rule id, never the command text or file content.
25
+ * See change: encrypt-credentials-at-rest (D12); harden-agent-tool-guards (D1).
26
+ */
27
+ import nodePath from "node:path";
28
+ import { PI_AGENT_SECRET_FILES } from "@blackbelt-technology/pi-dashboard-shared/agent-credential-files.js";
29
+ import {
30
+ CREDENTIAL_FILE_BASENAMES,
31
+ CREDENTIAL_KEYCHAIN_SERVICE,
32
+ DATA_KEY_FILE_ENV,
33
+ } from "@blackbelt-technology/pi-dashboard-shared/credential-encryption.js";
34
+ import {
35
+ canonicalizeTarget,
36
+ defaultResolveEnv,
37
+ type ResolveEnv,
38
+ resolveReadTarget,
39
+ resolveToolPath,
40
+ } from "./path-gate/resolve.js";
41
+
42
+ /** Rule ids carried by the `match=` token of the block log line. */
43
+ export type CredentialGuardRule = "path" | "grep-dir" | "text-path" | "text-keychain" | "text-dump" | "text-decrypt" | "depth";
44
+
45
+ export interface CredentialGuardDeps {
46
+ env: NodeJS.ProcessEnv;
47
+ homedir: string;
48
+ /** One line per block / error. Never receives command text or file content. */
49
+ log?: (line: string) => void;
50
+ /** Resolver environment (tests). Default: the real host with `homeDir` = `homedir`. */
51
+ resolveEnv?: ResolveEnv;
52
+ /** Canonicaliser (tests inject a throwing one). Default: shared `canonicalizeTarget`. */
53
+ canonicalize?: (absPath: string, env: ResolveEnv) => string;
54
+ }
55
+
56
+ export interface GuardEvent {
57
+ toolName?: string;
58
+ toolCallId?: string;
59
+ input?: unknown;
60
+ }
61
+
62
+ export interface GuardCtx {
63
+ cwd?: string;
64
+ hasUI?: boolean;
65
+ }
66
+
67
+ export type GuardResult = { block: true; reason: string } | undefined;
68
+
69
+ /** Tools decided by the path rule (their `path` argument is a filesystem target). */
70
+ const PATH_TOOLS = ["read", "edit", "write", "ls", "find", "grep"] as const;
71
+ /** Tools whose missing/empty `path` means the session cwd (pi's behaviour). */
72
+ const CWD_DEFAULT_TOOLS = ["grep", "find", "ls"] as const;
73
+ /** String values are scanned only under these field names (any depth). */
74
+ const SCANNED_FIELDS: ReadonlySet<string> = new Set([
75
+ "command", "script", "code", "args", "url", "path", "file", "filePath", "cwd", "pattern", "glob",
76
+ ]);
77
+ /** `write`/`edit` content fields: never scanned (their targets are covered by the path rule). */
78
+ const CONTENT_FIELDS: ReadonlySet<string> = new Set(["content", "newText", "oldText", "edits"]);
79
+ const CONTENT_TOOLS: ReadonlySet<string> = new Set(["write", "edit"]);
80
+ const MAX_DEPTH = 32;
81
+
82
+ const KEYCHAIN_VERBS = [
83
+ "find-generic-password",
84
+ "delete-generic-password",
85
+ "secret-tool",
86
+ "get-storedcredential",
87
+ "credentialmanager",
88
+ "cmdkey",
89
+ ] as const;
90
+ const DUMP_RE = /security\s+dump-keychain/i;
91
+ const DECRYPT_RE = /credentials\s+decrypt/i;
92
+
93
+ /** Path-like reference form: lowercase, `\` → `/`. */
94
+ const norm = (s: string): string => s.replaceAll("\\", "/").toLowerCase();
95
+ /** Path comparison form: NFC + lowercase (never fail open on a case-folding volume). */
96
+ const fold = (s: string): string => s.normalize("NFC").toLowerCase();
97
+ /** Tool names reach the log: keep only a short, harmless token. */
98
+ const safeName = (s: unknown): string => String(s ?? "unknown").replace(/[^\w.:-]/g, "_").slice(0, 64);
99
+
100
+ /** Collect scanned strings: any depth, only under a scanned field name. False when nested too deep. */
101
+ function collectScanned(value: unknown, scanned: boolean, skipContent: boolean, depth: number, out: string[]): boolean {
102
+ if (depth > MAX_DEPTH) return false;
103
+ if (typeof value === "string") {
104
+ if (scanned) out.push(value);
105
+ return true;
106
+ }
107
+ if (Array.isArray(value)) return value.every((v) => collectScanned(v, scanned, skipContent, depth + 1, out));
108
+ if (value && typeof value === "object") return collectFromObject(value as Record<string, unknown>, scanned, skipContent, depth, out);
109
+ return true;
110
+ }
111
+
112
+ function collectFromObject(obj: Record<string, unknown>, scanned: boolean, skipContent: boolean, depth: number, out: string[]): boolean {
113
+ for (const [k, v] of Object.entries(obj)) {
114
+ if (skipContent && CONTENT_FIELDS.has(k)) continue;
115
+ if (!collectScanned(v, scanned || SCANNED_FIELDS.has(k), skipContent, depth + 1, out)) return false;
116
+ }
117
+ return true;
118
+ }
119
+
120
+ /** The tool's path argument; a missing/empty `path` on grep/find/ls is the session cwd (pi's behaviour). */
121
+ function targetPathOf(tool: string, input: Record<string, unknown>, cwd: string): string | undefined {
122
+ const raw = input.path;
123
+ const missing = raw === undefined || raw === null || raw === "";
124
+ const raw2 = missing && (CWD_DEFAULT_TOOLS as readonly string[]).includes(tool) ? cwd : raw;
125
+ return typeof raw2 === "string" && raw2 !== "" ? raw2 : undefined; // else pi rejects it itself
126
+ }
127
+
128
+ export function createCredentialGuard(deps: CredentialGuardDeps) {
129
+ const log = deps.log ?? (() => {});
130
+ const canonicalize = deps.canonicalize ?? canonicalizeTarget;
131
+ const resolveEnv = (): ResolveEnv => deps.resolveEnv ?? { ...defaultResolveEnv(), homeDir: deps.homedir };
132
+
133
+ /** `PI_CODING_AGENT_DIR` as pi reads it: trimmed, a leading `~` expanded against the injected home. */
134
+ const configuredAgentDir = (): string | undefined => {
135
+ const raw = deps.env.PI_CODING_AGENT_DIR?.trim();
136
+ if (!raw) return undefined;
137
+ if (raw === "~") return deps.homedir;
138
+ return raw.startsWith("~/") || raw.startsWith("~\\") ? nodePath.join(deps.homedir, raw.slice(2)) : raw;
139
+ };
140
+
141
+ /** The files protected under an agent dir: plugin credentials + the provider secrets. */
142
+ const agentDirNames = (): string[] => [CREDENTIAL_FILE_BASENAMES["plugin-credentials"], ...PI_AGENT_SECRET_FILES];
143
+
144
+ /** Protected files under `~/.pi/agent` and `~/.pi/dashboard` (home-relative, always on). */
145
+ const homeProtected = (): string[] => [
146
+ ...agentDirNames().map((n) => nodePath.join(deps.homedir, ".pi", "agent", ...n.split("/"))),
147
+ nodePath.join(deps.homedir, ".pi", "dashboard", CREDENTIAL_FILE_BASENAMES.vault),
148
+ ];
149
+
150
+ /**
151
+ * Protected files named by the environment: `PI_CODING_AGENT_DIR` copies and the data-key
152
+ * file. Built from `deps.env` only, never from the path gate's `res.agentDir` (the guard
153
+ * has no access to it).
154
+ */
155
+ const envProtected = (): string[] => {
156
+ const out: string[] = [];
157
+ const agentDir = configuredAgentDir();
158
+ if (agentDir) for (const n of agentDirNames()) out.push(nodePath.resolve(agentDir, ...n.split("/")));
159
+ const keyFile = deps.env[DATA_KEY_FILE_ENV];
160
+ if (keyFile) out.push(nodePath.resolve(keyFile));
161
+ return out;
162
+ };
163
+
164
+ /** Protected files, lexical (env read per call: cheap, and always current). */
165
+ const protectedLexical = (): string[] => [...homeProtected(), ...envProtected()];
166
+
167
+ /**
168
+ * Canonical forms of the protected files. Each distinct directory is canonicalised once; a file
169
+ * that is not itself a symlink is `canonical(dir)/<name>` (identical to canonicalising the file:
170
+ * a missing or regular file adds nothing to its parent's real path), and a symlinked file takes the
171
+ * full `canonicalize` (it follows the link). Keeps the per-call cost flat as the protected set grows.
172
+ */
173
+ const canonicalProtected = (env: ResolveEnv): string[] => {
174
+ const dirs = new Map<string, string>();
175
+ return protectedLexical().map((file) => {
176
+ // `lexists` (non-throwing) first: most protected files do not exist, and a thrown ENOENT per file is the cost.
177
+ const isLink = env.isSymlink ? (env.lexists ? env.lexists(file) && env.isSymlink(file) : env.isSymlink(file)) : true;
178
+ if (isLink) return canonicalize(file, env);
179
+ const dir = env.path.dirname(file);
180
+ let real = dirs.get(dir);
181
+ if (real === undefined) {
182
+ real = canonicalize(dir, env);
183
+ dirs.set(dir, real);
184
+ }
185
+ return env.path.join(real, env.path.basename(file));
186
+ });
187
+ };
188
+
189
+ /**
190
+ * The env-configured paths AS WRITTEN (not resolved). On win32 a POSIX-style value
191
+ * (`/home/pi/.pi-key/data-key`) resolves to `D:\home\...`, which a command quoting
192
+ * the configured string never contains; text rules match both forms.
193
+ */
194
+ const configuredLexical = (): string[] => {
195
+ const out: string[] = [];
196
+ // Both spellings: a text mention may carry the literal `~/…` form or the expanded one.
197
+ for (const agentDir of new Set([deps.env.PI_CODING_AGENT_DIR?.trim(), configuredAgentDir()])) {
198
+ if (agentDir) for (const n of agentDirNames()) out.push(`${agentDir.replace(/[\\/]+$/, "")}/${n}`);
199
+ }
200
+ const keyFile = deps.env[DATA_KEY_FILE_ENV];
201
+ if (keyFile) out.push(keyFile);
202
+ return out;
203
+ };
204
+
205
+ const block = (tool: string, rule: CredentialGuardRule): GuardResult => {
206
+ log(`[credential-guard] blocked tool=${safeName(tool)} match=${rule}`);
207
+ return { block: true, reason: `credential_guard: ${safeName(tool)} targets dashboard credential storage` };
208
+ };
209
+
210
+ /** Path rule on one canonical target; `isGrep` adds the directory rule. */
211
+ function pathRule(canonical: string, protectedCanon: string[], isGrep: boolean): CredentialGuardRule | undefined {
212
+ const t = fold(canonical);
213
+ const dir = fold(nodePath.dirname(canonical));
214
+ const base = fold(nodePath.basename(canonical));
215
+ for (const p of protectedCanon) {
216
+ const pf = fold(p);
217
+ if (t === pf) return "path";
218
+ // sibling `<name>.*` in the same directory (lock, temp, .corrupt-*, .undecryptable-*)
219
+ if (dir === fold(nodePath.dirname(p)) && base.startsWith(`${fold(nodePath.basename(p))}.`)) return "path";
220
+ if (isGrep && t === fold(nodePath.dirname(p))) return "grep-dir";
221
+ }
222
+ return undefined;
223
+ }
224
+
225
+ function decidePath(tool: string, input: Record<string, unknown>, ctx: GuardCtx): CredentialGuardRule | undefined {
226
+ if (!(PATH_TOOLS as readonly string[]).includes(tool)) return undefined;
227
+ const cwd = ctx.cwd || process.cwd();
228
+ const raw = targetPathOf(tool, input, cwd);
229
+ if (raw === undefined) return undefined;
230
+ const env = resolveEnv();
231
+ const protectedCanon = canonicalProtected(env);
232
+ const resolved = resolveToolPath(raw, cwd, env);
233
+ const targets = [canonicalize(resolved, env)];
234
+ if (tool === "read") targets.push(canonicalize(resolveReadTarget(resolved, env), env));
235
+ for (const t of targets) {
236
+ const rule = pathRule(t, protectedCanon, tool === "grep");
237
+ if (rule) return rule;
238
+ }
239
+ return undefined;
240
+ }
241
+
242
+ function textRule(s: string, needles: string[]): CredentialGuardRule | undefined {
243
+ const n = norm(s);
244
+ if (needles.some((needle) => n.includes(needle))) return "text-path";
245
+ if (n.includes(CREDENTIAL_KEYCHAIN_SERVICE) && KEYCHAIN_VERBS.some((v) => n.includes(v))) return "text-keychain";
246
+ if (DUMP_RE.test(s)) return "text-dump";
247
+ if (DECRYPT_RE.test(s)) return "text-decrypt";
248
+ return undefined;
249
+ }
250
+
251
+ function decideText(tool: string, input: unknown): CredentialGuardRule | undefined {
252
+ const strings: string[] = [];
253
+ if (!collectScanned(input, false, CONTENT_TOOLS.has(tool), 0, strings)) return "depth";
254
+ if (strings.length === 0) return undefined;
255
+ // Explicit list (no index coupling to `protectedLexical()`): the literal `.pi/agent/<name>`
256
+ // and `.pi/dashboard/vault.json` references, the env-named paths, and the env paths as written.
257
+ const needles = [
258
+ ...agentDirNames().map((n) => `.pi/agent/${n}`),
259
+ `.pi/dashboard/${CREDENTIAL_FILE_BASENAMES.vault}`,
260
+ ...envProtected().map(norm),
261
+ ...configuredLexical().map(norm),
262
+ ];
263
+ for (const s of strings) {
264
+ const rule = textRule(s, needles);
265
+ if (rule) return rule;
266
+ }
267
+ return undefined;
268
+ }
269
+
270
+ function handler(event: GuardEvent, ctx: GuardCtx): GuardResult {
271
+ const tool = typeof event?.toolName === "string" ? event.toolName : "";
272
+ try {
273
+ const input = event?.input ?? {};
274
+ const asRecord = (input && typeof input === "object" ? input : {}) as Record<string, unknown>;
275
+ const rule = decidePath(tool, asRecord, ctx ?? {}) ?? decideText(tool, input);
276
+ return rule ? block(tool, rule) : undefined;
277
+ } catch {
278
+ // Fail closed for THIS call only; never log the error text (it may carry a path).
279
+ log(`[credential-guard] error tool=${safeName(tool)}`);
280
+ return { block: true, reason: `credential_guard: ${safeName(tool)} could not be checked` };
281
+ }
282
+ }
283
+
284
+ return { handler };
285
+ }
286
+
287
+ /**
288
+ * The `tool_call` handler `bridge.ts` registers: a no-op for a stale bridge
289
+ * instance (after /reload), otherwise the guard. Deliberately NOT wrapped in the
290
+ * fail-open `safe()`: the guard itself maps an internal error to a block.
291
+ */
292
+ export function createCredentialGuardToolCallHandler(
293
+ guard: ReturnType<typeof createCredentialGuard>,
294
+ isActive: () => boolean,
295
+ ): (event: GuardEvent, ctx: GuardCtx) => GuardResult {
296
+ return (event, ctx) => {
297
+ if (!isActive()) return undefined;
298
+ return guard.handler(event, ctx);
299
+ };
300
+ }
@@ -8,12 +8,15 @@
8
8
  * ONLY `customType` / `data` / `entryId` — the entry's other fields are
9
9
  * transport trivia the renderer never needs.
10
10
  *
11
- * `customType: "flow-event"` returns null: pi-flows appends those entries
12
- * live itself, so forwarding them would double-render alongside the
13
- * dedicated flow card. Non-`custom` entry types are likewise not ours to
14
- * forward.
11
+ * A plugin-declared entry type (`hidden`, from the plugin's
12
+ * `replayEntryTypes`) returns null: the owning plugin renders it through its
13
+ * own live events, so forwarding would double-render beside its card. Core
14
+ * names no plugin type. Non-`custom` entry types are likewise not ours to
15
+ * forward. See change: update-flows-plugin (D3).
15
16
  */
16
17
 
18
+ const NONE: ReadonlySet<string> = new Set();
19
+
17
20
  export interface CustomEntryForwardData {
18
21
  customType: string;
19
22
  data: unknown;
@@ -28,14 +31,17 @@ export interface CustomEntryForwardData {
28
31
 
29
32
  /**
30
33
  * Map one pi entry to the custom_entry payload, or null when the entry must
31
- * NOT be forwarded (non-custom type, missing/empty customType, flow-event).
34
+ * NOT be forwarded (non-custom type, missing/empty customType, declared type).
32
35
  */
33
- export function toCustomEntryForward(entry: unknown): CustomEntryForwardData | null {
36
+ export function toCustomEntryForward(
37
+ entry: unknown,
38
+ hidden: ReadonlySet<string> = NONE,
39
+ ): CustomEntryForwardData | null {
34
40
  if (!entry || typeof entry !== "object") return null;
35
41
  const e = entry as Record<string, unknown>;
36
42
  if (e.type !== "custom") return null;
37
43
  if (typeof e.customType !== "string" || e.customType === "") return null;
38
- if (e.customType === "flow-event") return null;
44
+ if (hidden.has(e.customType)) return null;
39
45
  return {
40
46
  customType: e.customType,
41
47
  data: e.data,
@@ -66,19 +72,22 @@ export interface CustomMessageForwardData {
66
72
  * RENDERS (absent flag = meant to be seen). Mirrors the state-replay arm so
67
73
  * live and replay forward identically.
68
74
  */
69
- export function toCustomMessageForward(m: {
70
- customType: unknown;
71
- content: unknown;
72
- display?: unknown;
73
- details?: unknown;
74
- entryId?: unknown;
75
- }): CustomMessageForwardData | null {
75
+ export function toCustomMessageForward(
76
+ m: {
77
+ customType: unknown;
78
+ content: unknown;
79
+ display?: unknown;
80
+ details?: unknown;
81
+ entryId?: unknown;
82
+ },
83
+ hidden: ReadonlySet<string> = NONE,
84
+ ): CustomMessageForwardData | null {
76
85
  if (!m || typeof m.customType !== "string" || m.customType === "") return null;
77
86
  if (m.display === false) return null;
78
- // flow-event parity with the entry path: pi-flows does not send custom
79
- // MESSAGES today, but a spoofing customType must not lease the generic
80
- // card either. See change: render-inline-reasoning-and-custom-entries.
81
- if (m.customType === "flow-event") return null;
87
+ // Declared-type parity with the entry path: a plugin does not send custom
88
+ // MESSAGES of its entry type, but a spoofing customType must not lease the
89
+ // generic card either. See change: render-inline-reasoning-and-custom-entries.
90
+ if (hidden.has(m.customType)) return null;
82
91
  return {
83
92
  message: {
84
93
  role: "custom",
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The HTTP origin a REMOTE bridge uses for its `/api/*` calls: ticket mint,
3
+ * server-identity challenge, live trust query.
4
+ *
5
+ * A bridge dials the pi gateway (`PI_DASHBOARD_URL=ws://host:9999`), but the
6
+ * gateway serves bridge WebSockets only; `/api/*` lives on the dashboard HTTP
7
+ * listener (default 8000). Deriving the HTTP origin from the gateway port
8
+ * answered `426` on the mint and read as a revoked credential.
9
+ *
10
+ * Pure: `env` is an argument, never read from `process.env` here, and the
11
+ * bridge machine's own `~/.pi/dashboard/config.json` is deliberately NOT
12
+ * consulted — it describes the local dashboard, not the remote one.
13
+ *
14
+ * Precedence (design D1):
15
+ * 0. `PI_DASHBOARD_HTTP_URL` set but invalid → `bad-http-url`, for every endpoint
16
+ * 1. valid override, in scope (endpoint host == `PI_DASHBOARD_URL` host or override host)
17
+ * 2. plaintext `ws:` with an explicit port → endpoint host + HTTP port env/default
18
+ * 3. `wss:` or port-less `ws:` → endpoint origin with the scheme swapped
19
+ * 4. anything else → `no-http-origin`
20
+ *
21
+ * The bearer therefore only leaves for the endpoint's own host, except where
22
+ * the operator explicitly named another one (step 1, scoped to their hosts).
23
+ *
24
+ * See change: fix-remote-bridge-ticket-origin (D1).
25
+ */
26
+ import { resolveDashboardPorts } from "@blackbelt-technology/pi-dashboard-shared/config.js";
27
+
28
+ /** Env var naming the dashboard's HTTP origin explicitly. */
29
+ const DASHBOARD_HTTP_URL_ENV = "PI_DASHBOARD_HTTP_URL";
30
+
31
+ type HttpOriginSource = "PI_DASHBOARD_HTTP_URL" | "endpoint-host+http-port" | "endpoint-origin";
32
+
33
+ export type HttpOriginResult =
34
+ | { ok: true; origin: string; source: HttpOriginSource }
35
+ | { ok: false; cause: "bad-http-url" | "no-http-origin"; reason: string };
36
+
37
+ function parseUrl(raw: string): URL | null {
38
+ try {
39
+ return new URL(raw);
40
+ } catch {
41
+ return null;
42
+ }
43
+ }
44
+
45
+ /** `origin` of a valid http(s) URL without credentials; otherwise null. */
46
+ function validOverrideOrigin(raw: string): string | null {
47
+ const u = parseUrl(raw);
48
+ if (!u) return null;
49
+ if (u.protocol !== "http:" && u.protocol !== "https:") return null;
50
+ if (u.username !== "" || u.password !== "") return null;
51
+ return u.origin;
52
+ }
53
+
54
+ export function resolveDashboardHttpOrigin(endpoint: string, env: Record<string, string | undefined>): HttpOriginResult {
55
+ const ep = parseUrl(endpoint);
56
+ const override = env[DASHBOARD_HTTP_URL_ENV]?.trim();
57
+
58
+ if (override) {
59
+ const origin = validOverrideOrigin(override);
60
+ if (!origin) {
61
+ return {
62
+ ok: false,
63
+ cause: "bad-http-url",
64
+ reason: `${DASHBOARD_HTTP_URL_ENV} is not an http(s) URL without credentials — fix or unset it`,
65
+ };
66
+ }
67
+ if (ep) {
68
+ const pinnedHost = parseUrl(env.PI_DASHBOARD_URL?.trim() ?? "")?.hostname;
69
+ const overrideHost = parseUrl(override)?.hostname;
70
+ if (ep.hostname === pinnedHost || ep.hostname === overrideHost) {
71
+ return { ok: true, origin, source: "PI_DASHBOARD_HTTP_URL" };
72
+ }
73
+ }
74
+ }
75
+
76
+ if (ep && ep.protocol === "ws:" && ep.port !== "") {
77
+ const { port } = resolveDashboardPorts(env);
78
+ return { ok: true, origin: `http://${ep.hostname}:${port}`, source: "endpoint-host+http-port" };
79
+ }
80
+ if (ep && (ep.protocol === "ws:" || ep.protocol === "wss:")) {
81
+ return {
82
+ ok: true,
83
+ origin: `${ep.protocol === "wss:" ? "https" : "http"}://${ep.host}`,
84
+ source: "endpoint-origin",
85
+ };
86
+ }
87
+ return { ok: false, cause: "no-http-origin", reason: `${endpoint} has no http origin` };
88
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Apply the dashboard Settings default model (and thinking level) to a
3
+ * brand-new pi session. Extracted from bridge.ts so it is unit-testable.
4
+ *
5
+ * `defaultModel` may be a concrete `provider/id` or an `@role[:level]` ref.
6
+ * A role ref is resolved against the CURRENT role map on every call (startup
7
+ * and the pending retry) — no cache, so a preset load changes the next new
8
+ * session's model. Unresolved role → no `setModel`, no pending retry, one log
9
+ * line; pi keeps its own default.
10
+ *
11
+ * Level after `setModel`: non-empty `defaultThinkingLevel` > ref suffix >
12
+ * role level > none. See change: default-model-follows-role (D1, D2, D6).
13
+ */
14
+ import { type RoleConfig, resolveDefaultModel } from "@blackbelt-technology/pi-dashboard-shared/role-schema.js";
15
+
16
+ interface DefaultModelConfig {
17
+ defaultModel?: string;
18
+ defaultThinkingLevel?: string;
19
+ }
20
+
21
+ export interface ApplyDefaultModelDeps {
22
+ loadConfig: () => DefaultModelConfig;
23
+ readRoles: () => Pick<RoleConfig, "roles">;
24
+ registry: { find(provider: string, id: string): unknown } | null | undefined;
25
+ setModel: (model: unknown) => Promise<unknown>;
26
+ setThinkingLevel?: (level: string) => void;
27
+ /** Called after `setModel` (and level) resolve. */
28
+ onApplied?: () => void;
29
+ log: (msg: string) => void;
30
+ }
31
+
32
+ /** Returns the pending model string when the model is not in the registry yet, else `null`. */
33
+ export function applyDefaultModel(deps: ApplyDefaultModelDeps): string | null {
34
+ const cfg = deps.loadConfig();
35
+ const value = cfg.defaultModel;
36
+ if (!value || !deps.registry) return null;
37
+
38
+ let provider: string;
39
+ let modelId: string;
40
+ let roleLevel: string | undefined;
41
+ if (value.startsWith("@")) {
42
+ let roles: Pick<RoleConfig, "roles">;
43
+ try {
44
+ roles = deps.readRoles();
45
+ } catch {
46
+ roles = { roles: {} };
47
+ }
48
+ const r = resolveDefaultModel(value, roles);
49
+ if (r.kind !== "role") {
50
+ deps.log(`[default-model] unresolved role ${value}; using pi default`);
51
+ return null;
52
+ }
53
+ provider = r.provider;
54
+ modelId = r.id;
55
+ roleLevel = r.level;
56
+ } else {
57
+ const slashIdx = value.indexOf("/");
58
+ if (slashIdx <= 0) return null;
59
+ provider = value.slice(0, slashIdx);
60
+ modelId = value.slice(slashIdx + 1);
61
+ }
62
+
63
+ try {
64
+ const found = deps.registry.find(provider, modelId);
65
+ if (found) {
66
+ deps
67
+ .setModel(found)
68
+ .then(() => {
69
+ // setModel preserves the current level, so an explicit call is
70
+ // required. Empty `defaultThinkingLevel` = adopt the role's level
71
+ // (role default) / do not override (concrete default).
72
+ const level = cfg.defaultThinkingLevel || roleLevel;
73
+ if (level) deps.setThinkingLevel?.(level);
74
+ deps.onApplied?.();
75
+ })
76
+ .catch(() => {});
77
+ return null; // applied
78
+ }
79
+ } catch {
80
+ /* ignore */
81
+ }
82
+ return value; // not found yet — pending (re-resolved on retry)
83
+ }
@@ -2,7 +2,9 @@
2
2
  * Dev build-on-reload helper.
3
3
  * Builds the Vite client and requests server shutdown.
4
4
  */
5
+ import { httpProofHeaders } from "@blackbelt-technology/pi-dashboard-shared/local-credential-proof.js";
5
6
  import { execSync as defaultExecSync } from "@blackbelt-technology/pi-dashboard-shared/platform/exec.js";
7
+ import { readLocalToken } from "./local-token-header.js";
6
8
 
7
9
  export interface DevBuildOptions {
8
10
  packageRoot: string;
@@ -11,6 +13,8 @@ export interface DevBuildOptions {
11
13
  _execSync?: typeof defaultExecSync;
12
14
  /** @internal for testing */
13
15
  _fetch?: typeof fetch;
16
+ /** @internal for testing: the local token (default: read from `~/.pi/dashboard/local/token`). */
17
+ _token?: string | undefined;
14
18
  }
15
19
 
16
20
  /**
@@ -31,7 +35,13 @@ export function runDevBuild(opts: DevBuildOptions): void {
31
35
 
32
36
  try {
33
37
  console.log("🛑 Dashboard: stopping server...");
34
- fetchFn(`http://localhost:${opts.serverPort}/api/shutdown`, { method: "POST" }).catch(() => {});
38
+ // `/api/shutdown` is an operate route: under the strict `requireLocalProof` default a bare POST is
39
+ // refused, so dial the loopback literal and carry a proof for this exact request. No token ⇒ no
40
+ // proof header (the refusal is then the honest answer). See change: close-container-loopback-impersonation (D5).
41
+ const url = `http://127.0.0.1:${opts.serverPort}/api/shutdown`;
42
+ const token = "_token" in opts ? opts._token : readLocalToken();
43
+ const headers = token ? httpProofHeaders(token, url, "POST") : {};
44
+ fetchFn(url, { method: "POST", headers }).catch(() => {});
35
45
  console.log("✅ Dashboard: server stopped");
36
46
  } catch {
37
47
  // Server may not be running — that's fine