@blackbelt-technology/pi-agent-dashboard 0.4.1 → 0.4.3

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 (109) hide show
  1. package/AGENTS.md +80 -32
  2. package/README.md +7 -3
  3. package/docs/architecture.md +361 -12
  4. package/package.json +7 -7
  5. package/packages/extension/package.json +7 -2
  6. package/packages/extension/src/__tests__/ask-user-schema-discriminator.test.ts +141 -0
  7. package/packages/extension/src/__tests__/ask-user-tool.test.ts +51 -7
  8. package/packages/extension/src/__tests__/multiselect-dashboard-routing.test.ts +203 -0
  9. package/packages/extension/src/__tests__/multiselect-polyfill.test.ts +92 -0
  10. package/packages/extension/src/__tests__/no-tui-multiselect-arm-regression.test.ts +81 -0
  11. package/packages/extension/src/__tests__/openspec-activity-detector.test.ts +37 -0
  12. package/packages/extension/src/__tests__/ui-decorators.test.ts +309 -0
  13. package/packages/extension/src/__tests__/ui-modules.test.ts +293 -0
  14. package/packages/extension/src/ask-user-tool.ts +165 -57
  15. package/packages/extension/src/bridge.ts +97 -4
  16. package/packages/extension/src/multiselect-decode.ts +40 -0
  17. package/packages/extension/src/multiselect-polyfill.ts +38 -8
  18. package/packages/extension/src/ui-modules.ts +272 -0
  19. package/packages/server/package.json +9 -3
  20. package/packages/server/src/__tests__/auto-attach.test.ts +61 -8
  21. package/packages/server/src/__tests__/browse-endpoint.test.ts +295 -19
  22. package/packages/server/src/__tests__/cli-bootstrap.test.ts +36 -0
  23. package/packages/server/src/__tests__/directory-service-refresh-force.test.ts +163 -0
  24. package/packages/server/src/__tests__/directory-service-specs-mtime.test.ts +315 -0
  25. package/packages/server/src/__tests__/directory-service-toctou.test.ts +303 -0
  26. package/packages/server/src/__tests__/directory-service.test.ts +174 -0
  27. package/packages/server/src/__tests__/installed-package-enricher.test.ts +225 -0
  28. package/packages/server/src/__tests__/package-manager-wrapper-move.test.ts +414 -0
  29. package/packages/server/src/__tests__/package-routes.test.ts +136 -3
  30. package/packages/server/src/__tests__/package-source-helpers.test.ts +101 -0
  31. package/packages/server/src/__tests__/pending-attach-registry.test.ts +123 -0
  32. package/packages/server/src/__tests__/pending-resume-intent-registry.test.ts +138 -0
  33. package/packages/server/src/__tests__/pi-core-checker.test.ts +73 -30
  34. package/packages/server/src/__tests__/pi-gateway-consume-pending-attach.test.ts +112 -0
  35. package/packages/server/src/__tests__/post-install-openspec-refresh.test.ts +180 -0
  36. package/packages/server/src/__tests__/post-install-rescan.test.ts +134 -0
  37. package/packages/server/src/__tests__/proposal-attach-naming.test.ts +79 -0
  38. package/packages/server/src/__tests__/session-action-handler-spawn-with-attach.test.ts +108 -0
  39. package/packages/server/src/__tests__/session-order-manager.test.ts +55 -0
  40. package/packages/server/src/__tests__/session-order-reboot.test.ts +242 -0
  41. package/packages/server/src/__tests__/session-scanner.test.ts +44 -0
  42. package/packages/server/src/__tests__/subscription-handler.test.ts +40 -0
  43. package/packages/server/src/__tests__/translate-path-source.test.ts +77 -0
  44. package/packages/server/src/__tests__/ui-decorators-replay.test.ts +209 -0
  45. package/packages/server/src/__tests__/ui-modules-replay.test.ts +221 -0
  46. package/packages/server/src/browse.ts +118 -13
  47. package/packages/server/src/browser-gateway.ts +19 -0
  48. package/packages/server/src/browser-handlers/__tests__/session-meta-handler.test.ts +183 -0
  49. package/packages/server/src/browser-handlers/directory-handler.ts +7 -1
  50. package/packages/server/src/browser-handlers/handler-context.ts +15 -0
  51. package/packages/server/src/browser-handlers/session-action-handler.ts +29 -3
  52. package/packages/server/src/browser-handlers/session-meta-handler.ts +46 -12
  53. package/packages/server/src/browser-handlers/subscription-handler.ts +46 -1
  54. package/packages/server/src/cli.ts +5 -6
  55. package/packages/server/src/directory-service.ts +156 -15
  56. package/packages/server/src/event-wiring.ts +111 -10
  57. package/packages/server/src/installed-package-enricher.ts +143 -0
  58. package/packages/server/src/package-manager-wrapper.ts +305 -8
  59. package/packages/server/src/package-source-helpers.ts +104 -0
  60. package/packages/server/src/pending-attach-registry.ts +112 -0
  61. package/packages/server/src/pending-resume-intent-registry.ts +107 -0
  62. package/packages/server/src/pi-core-checker.ts +9 -14
  63. package/packages/server/src/pi-gateway.ts +14 -0
  64. package/packages/server/src/proposal-attach-naming.ts +47 -0
  65. package/packages/server/src/routes/file-routes.ts +29 -3
  66. package/packages/server/src/routes/package-routes.ts +72 -3
  67. package/packages/server/src/routes/plugin-config-routes.ts +129 -0
  68. package/packages/server/src/routes/system-routes.ts +2 -0
  69. package/packages/server/src/server.ts +339 -10
  70. package/packages/server/src/session-api.ts +30 -5
  71. package/packages/server/src/session-order-manager.ts +22 -0
  72. package/packages/server/src/session-scanner.ts +10 -1
  73. package/packages/shared/package.json +9 -2
  74. package/packages/shared/src/__tests__/browser-protocol-types.test.ts +59 -0
  75. package/packages/shared/src/__tests__/config-plugins.test.ts +68 -0
  76. package/packages/shared/src/__tests__/extension-ui-module-shape.test.ts +265 -0
  77. package/packages/shared/src/__tests__/no-raw-openspec-status-in-skills.test.ts +81 -0
  78. package/packages/shared/src/__tests__/openspec-design-evidence.test.ts +288 -0
  79. package/packages/shared/src/__tests__/openspec-effective-status-script.test.ts +174 -0
  80. package/packages/shared/src/__tests__/openspec-poller-design-override.test.ts +225 -0
  81. package/packages/shared/src/__tests__/openspec-poller-specs-override.test.ts +284 -0
  82. package/packages/shared/src/__tests__/openspec-specs-evidence.test.ts +144 -0
  83. package/packages/shared/src/__tests__/platform/is-appimage-self-hit.test.ts +164 -0
  84. package/packages/shared/src/__tests__/plugin-bridge-register-extended.test.ts +72 -0
  85. package/packages/shared/src/__tests__/plugin-bridge-register.test.ts +113 -0
  86. package/packages/shared/src/__tests__/plugin-config-update-protocol.test.ts +41 -0
  87. package/packages/shared/src/__tests__/publish-workflow-contract.test.ts +123 -0
  88. package/packages/shared/src/__tests__/recommended-extensions.test.ts +5 -1
  89. package/packages/shared/src/__tests__/spawn-session-attach-proposal.test.ts +47 -0
  90. package/packages/shared/src/__tests__/tool-registry-strategies-appimage.test.ts +118 -0
  91. package/packages/shared/src/browser-protocol.ts +110 -4
  92. package/packages/shared/src/config.ts +45 -0
  93. package/packages/shared/src/dashboard-plugin/index.ts +11 -0
  94. package/packages/shared/src/dashboard-plugin/manifest-types.ts +58 -0
  95. package/packages/shared/src/dashboard-plugin/plugin-status.ts +26 -0
  96. package/packages/shared/src/dashboard-plugin/slot-props.ts +92 -0
  97. package/packages/shared/src/dashboard-plugin/slot-types.ts +151 -0
  98. package/packages/shared/src/openspec-activity-detector.ts +18 -22
  99. package/packages/shared/src/openspec-design-evidence.ts +109 -0
  100. package/packages/shared/src/openspec-poller.ts +117 -3
  101. package/packages/shared/src/openspec-specs-evidence.ts +79 -0
  102. package/packages/shared/src/platform/binary-lookup.ts +96 -1
  103. package/packages/shared/src/plugin-bridge-register.ts +139 -0
  104. package/packages/shared/src/protocol.ts +56 -2
  105. package/packages/shared/src/recommended-extensions.ts +7 -1
  106. package/packages/shared/src/rest-api.ts +68 -3
  107. package/packages/shared/src/state-replay.ts +11 -1
  108. package/packages/shared/src/tool-registry/strategies.ts +17 -3
  109. package/packages/shared/src/types.ts +160 -0
@@ -23,12 +23,47 @@
23
23
  import { listOr, statusOr, OPENSPEC_LIST, OPENSPEC_STATUS } from "./platform/openspec.js";
24
24
  import { runAsync, unwrap } from "./platform/runner.js";
25
25
  import type { OpenSpecData, OpenSpecChange, OpenSpecArtifact } from "./types.js";
26
+ import {
27
+ evaluateLocalDesignSatisfaction,
28
+ createFsDesignEvidenceProbe,
29
+ type DesignEvidenceProbe,
30
+ } from "./openspec-design-evidence.js";
31
+ import {
32
+ evaluateLocalSpecsSatisfaction,
33
+ createFsSpecsEvidenceProbe,
34
+ type SpecsEvidenceProbe,
35
+ } from "./openspec-specs-evidence.js";
36
+ import path from "node:path";
26
37
 
27
38
  const EMPTY_DATA: OpenSpecData = { initialized: false, changes: [] };
28
39
 
40
+ /**
41
+ * Factory that returns a probe for a given change name. Production callers
42
+ * pass a closure rooted at `<cwd>/openspec/changes/<name>`. Tests pass an
43
+ * in-memory factory. When omitted, the design override does NOT fire and
44
+ * `buildOpenSpecData` matches today's behavior verbatim.
45
+ *
46
+ * See change: fix-openspec-design-detection.
47
+ */
48
+ export type DesignProbeFactory = (changeName: string) => DesignEvidenceProbe;
49
+
50
+ /**
51
+ * Factory that returns a specs-evidence probe for a given change name.
52
+ * Parallel to `DesignProbeFactory` — production callers pass a closure
53
+ * rooted at `<cwd>/openspec/changes/<name>`; tests pass an in-memory
54
+ * factory. When omitted, the specs override does NOT fire and
55
+ * `buildOpenSpecData` matches today's behavior verbatim for the specs
56
+ * artifact.
57
+ *
58
+ * See change: fix-openspec-specs-mtime-gate-blind-spot.
59
+ */
60
+ export type SpecsProbeFactory = (changeName: string) => SpecsEvidenceProbe;
61
+
29
62
  export function buildOpenSpecData(
30
63
  listResult: { changes?: Array<{ name: string; status: string; completedTasks: number; totalTasks: number }> } | null,
31
64
  statusResults: Map<string, { artifacts?: Array<{ id: string; status: string }>; isComplete?: boolean } | null>,
65
+ probeFactory?: DesignProbeFactory,
66
+ specsProbeFactory?: SpecsProbeFactory,
32
67
  ): OpenSpecData {
33
68
  if (!listResult || !Array.isArray(listResult.changes)) {
34
69
  return EMPTY_DATA;
@@ -41,9 +76,40 @@ export function buildOpenSpecData(
41
76
  status: (a.status === "done" ? "done" : a.status === "ready" ? "ready" : "blocked") as OpenSpecArtifact["status"],
42
77
  }));
43
78
 
44
- const isComplete =
79
+ // Design-artifact override: promote-only, design-only. See change:
80
+ // fix-openspec-design-detection.
81
+ if (probeFactory) {
82
+ const designIdx = artifacts.findIndex((a) => a.id === "design");
83
+ if (designIdx !== -1 && artifacts[designIdx].status === "ready") {
84
+ const probe = probeFactory(c.name);
85
+ if (evaluateLocalDesignSatisfaction("", probe)) {
86
+ artifacts[designIdx] = { ...artifacts[designIdx], status: "done" };
87
+ }
88
+ }
89
+ }
90
+
91
+ // Specs-artifact override: promote-only, specs-only. See change:
92
+ // fix-openspec-specs-mtime-gate-blind-spot.
93
+ if (specsProbeFactory) {
94
+ const specsIdx = artifacts.findIndex((a) => a.id === "specs");
95
+ if (specsIdx !== -1 && artifacts[specsIdx].status === "ready") {
96
+ const probe = specsProbeFactory(c.name);
97
+ if (evaluateLocalSpecsSatisfaction("", probe)) {
98
+ artifacts[specsIdx] = { ...artifacts[specsIdx], status: "done" };
99
+ }
100
+ }
101
+ }
102
+
103
+ const cliIsComplete =
45
104
  typeof statusResult?.isComplete === "boolean" ? statusResult.isComplete : undefined;
46
105
 
106
+ // Re-derive isComplete from post-override artifacts. Promote false→true
107
+ // only when every artifact is done; never demote CLI true.
108
+ let isComplete = cliIsComplete;
109
+ if (artifacts.length > 0 && artifacts.every((a) => a.status === "done")) {
110
+ isComplete = true;
111
+ }
112
+
47
113
  const change: OpenSpecChange = {
48
114
  name: c.name,
49
115
  status: (c.status === "complete" ? "complete" : c.status === "in-progress" ? "in-progress" : "no-tasks") as OpenSpecChange["status"],
@@ -58,6 +124,44 @@ export function buildOpenSpecData(
58
124
  return { initialized: true, changes };
59
125
  }
60
126
 
127
+ /**
128
+ * Build a real-fs probe factory rooted at `<cwd>/openspec/changes/<name>`.
129
+ * Production callers (`pollOpenSpec`, `pollOpenSpecAsync`,
130
+ * `directory-service.ts`) use this to wire the override. Tests inject
131
+ * their own factory.
132
+ */
133
+ export function createFsProbeFactory(cwd: string): DesignProbeFactory {
134
+ const probe = createFsDesignEvidenceProbe();
135
+ const changesRoot = path.join(cwd, "openspec", "changes");
136
+ return (changeName) => {
137
+ const changeDir = path.join(changesRoot, changeName);
138
+ return {
139
+ hasDesignFile: () => probe.hasDesignFile(changeDir),
140
+ hasDesignDirWithMd: () => probe.hasDesignDirWithMd(changeDir),
141
+ tasksHasCheckboxes: () => probe.tasksHasCheckboxes(changeDir),
142
+ };
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Build a real-fs specs-probe factory rooted at `<cwd>/openspec/changes/<name>`.
148
+ * Parallel to `createFsProbeFactory` — production callers (`pollOpenSpec`,
149
+ * `pollOpenSpecAsync`, `directory-service.ts`) use this to wire the specs
150
+ * override. Tests inject their own factory.
151
+ *
152
+ * See change: fix-openspec-specs-mtime-gate-blind-spot.
153
+ */
154
+ export function createFsSpecsProbeFactory(cwd: string): SpecsProbeFactory {
155
+ const probe = createFsSpecsEvidenceProbe();
156
+ const changesRoot = path.join(cwd, "openspec", "changes");
157
+ return (changeName) => {
158
+ const changeDir = path.join(changesRoot, changeName);
159
+ return {
160
+ hasAnySpecFile: () => probe.hasAnySpecFile(changeDir),
161
+ };
162
+ };
163
+ }
164
+
61
165
  /**
62
166
  * Synchronous poll — blocks the event loop. Used by the bridge extension
63
167
  * where async isn't practical (some pi extension hooks are sync).
@@ -70,7 +174,12 @@ export function pollOpenSpec(cwd: string): OpenSpecData {
70
174
  for (const c of listResult.changes) {
71
175
  statusResults.set(c.name, statusOr({ cwd, change: c.name }));
72
176
  }
73
- return buildOpenSpecData(listResult, statusResults);
177
+ return buildOpenSpecData(
178
+ listResult,
179
+ statusResults,
180
+ createFsProbeFactory(cwd),
181
+ createFsSpecsProbeFactory(cwd),
182
+ );
74
183
  }
75
184
 
76
185
  /**
@@ -114,5 +223,10 @@ export async function pollOpenSpecAsync(cwd: string): Promise<OpenSpecData> {
114
223
  }),
115
224
  );
116
225
  const statusResults = new Map<string, any>(statusEntries);
117
- return buildOpenSpecData(listResult, statusResults);
226
+ return buildOpenSpecData(
227
+ listResult,
228
+ statusResults,
229
+ createFsProbeFactory(cwd),
230
+ createFsSpecsProbeFactory(cwd),
231
+ );
118
232
  }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Local-evidence override for the OpenSpec `specs` artifact.
3
+ *
4
+ * The `spec-driven` schema declares `specs/**\/*.md` as the `generates`
5
+ * pattern for the `specs` artifact, and the openspec CLI marks the
6
+ * artifact `done` whenever that glob matches anything. The dashboard's
7
+ * mtime-gated cache, however, can momentarily stale on `specs: ready`
8
+ * for multi-spec changes (see change:
9
+ * fix-openspec-specs-mtime-gate-blind-spot — the watch set is now
10
+ * extended to cover `specs/**`, but this override is the second line
11
+ * of defence).
12
+ *
13
+ * This module computes a boolean "is specs satisfied locally?" from
14
+ * file-system evidence the dashboard's cache might miss between polls.
15
+ * It is consumed by:
16
+ *
17
+ * 1. `buildOpenSpecData` in `openspec-poller.ts` — promotes
18
+ * `artifacts[specs].status` from "ready" to "done" when at least
19
+ * one `specs/**\/*.md` file exists. Promote-only; specs-only;
20
+ * never demotes; never touches other artifacts.
21
+ *
22
+ * One rule:
23
+ *
24
+ * any file matching `specs/**\/*.md` exists in the change folder
25
+ *
26
+ * The probe walks the `specs/` subtree once and short-circuits on the
27
+ * first `*.md` it finds. Defensive: every fs call is wrapped in
28
+ * try/catch and treated as "no match" on error.
29
+ *
30
+ * See change: fix-openspec-specs-mtime-gate-blind-spot.
31
+ */
32
+
33
+ import { readdirSync } from "node:fs";
34
+ import path from "node:path";
35
+
36
+ /** Probe surface — kept tiny so unit tests can pass an in-memory stub. */
37
+ export interface SpecsEvidenceProbe {
38
+ /** Returns true iff at least one `*.md` file exists under `<changeDir>/specs/`. */
39
+ hasAnySpecFile(changeDir: string): boolean;
40
+ }
41
+
42
+ /** Pure rule evaluator. Single rule; short-circuits on first match. */
43
+ export function evaluateLocalSpecsSatisfaction(
44
+ changeDir: string,
45
+ probe: SpecsEvidenceProbe,
46
+ ): boolean {
47
+ return probe.hasAnySpecFile(changeDir);
48
+ }
49
+
50
+ /**
51
+ * Production probe — backed by the real filesystem. Walks `<changeDir>/specs/`
52
+ * iteratively, short-circuits on the first `*.md` file encountered. Every
53
+ * `readdirSync` is wrapped in try/catch (handles ENOENT, permission errors,
54
+ * symlink loops, and any unexpected fs error) and treated as "no match".
55
+ */
56
+ export function createFsSpecsEvidenceProbe(): SpecsEvidenceProbe {
57
+ return {
58
+ hasAnySpecFile(changeDir: string): boolean {
59
+ const root = path.join(changeDir, "specs");
60
+ // Iterative DFS — no recursion to avoid stack overflow on pathological trees.
61
+ const stack: string[] = [root];
62
+ while (stack.length > 0) {
63
+ const dir = stack.pop()!;
64
+ let entries: import("node:fs").Dirent[];
65
+ try {
66
+ entries = readdirSync(dir, { withFileTypes: true });
67
+ } catch {
68
+ // Missing dir, permission denied, or any other fs error — skip.
69
+ continue;
70
+ }
71
+ for (const e of entries) {
72
+ if (e.isFile() && e.name.endsWith(".md")) return true;
73
+ if (e.isDirectory()) stack.push(path.join(dir, e.name));
74
+ }
75
+ }
76
+ return false;
77
+ },
78
+ };
79
+ }
@@ -4,11 +4,106 @@
4
4
  * with a single configurable resolver.
5
5
  */
6
6
  import { execSync, spawnSync, buildSafeArgv } from "./exec.js";
7
- import { existsSync } from "node:fs";
7
+ import { existsSync, realpathSync } from "node:fs";
8
8
  import path from "node:path";
9
9
  import os from "node:os";
10
10
  import { MANAGED_BIN, MANAGED_DIR } from "../managed-paths.js";
11
11
 
12
+ // ── AppImage self-hit guard (Linux power-user mode safety) ────────────────
13
+
14
+ /**
15
+ * Optional environment overrides for {@link isAppImageSelfHit}. Tests
16
+ * inject explicit values so the helper can exercise both branches
17
+ * without mutating `process.env` or `process.execPath`. Production
18
+ * callers omit `opts` and the helper reads from the live process.
19
+ *
20
+ * See change: fix-electron-appimage-cli-self-detection (D1).
21
+ */
22
+ export interface AppImageSelfHitOpts {
23
+ /** Override `process.execPath`. Default: `process.execPath`. */
24
+ execPath?: string;
25
+ /** Override `process.env.APPDIR`. Default: `process.env.APPDIR`. */
26
+ appDir?: string | undefined;
27
+ /** Override `process.env.APPIMAGE`. Default: `process.env.APPIMAGE`. */
28
+ appImage?: string | undefined;
29
+ }
30
+
31
+ /** Defensive realpath — returns the input on any error (broken symlink / ENOENT). */
32
+ function safeRealpath(p: string): string {
33
+ try {
34
+ return realpathSync(p);
35
+ } catch {
36
+ return p;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Returns `true` when `candidatePath` is the running process's own
42
+ * Electron launcher binary — the bug class that motivates this helper:
43
+ * AppImage's runtime prepends its squashfs mount (`/tmp/.mount_*`) to
44
+ * `PATH` of the Electron child, and `packagerConfig.executableName =
45
+ * "pi-dashboard"` makes the launcher a name-collision with the dashboard
46
+ * CLI. Trusting the first `which pi-dashboard` hit therefore spawns the
47
+ * Electron app recursively as if it were the CLI.
48
+ *
49
+ * A path is considered a self-hit when ANY of the following is true:
50
+ * - `realpath(candidatePath) === realpath(execPath)`, OR
51
+ * - `candidatePath` lives under the directory named by `appDir`, OR
52
+ * - `realpath(candidatePath) === realpath(appImage)`.
53
+ *
54
+ * `realpath` calls are wrapped in try/catch so broken symlinks / ENOENT
55
+ * fall back to literal string comparisons. The helper never throws.
56
+ *
57
+ * Production callers (`whereStrategy`, `detectPiDashboardCli`,
58
+ * `detectPi`, `detectSystemNode`) omit `opts`. Tests pass explicit
59
+ * overrides via `opts`.
60
+ *
61
+ * See change: fix-electron-appimage-cli-self-detection (D1).
62
+ */
63
+ export function isAppImageSelfHit(
64
+ candidatePath: string,
65
+ opts?: AppImageSelfHitOpts,
66
+ ): boolean {
67
+ if (!candidatePath) return false;
68
+
69
+ const execPath = opts && "execPath" in opts ? opts.execPath : process.execPath;
70
+ const appDir = opts && "appDir" in opts ? opts.appDir : process.env.APPDIR;
71
+ const appImage = opts && "appImage" in opts ? opts.appImage : process.env.APPIMAGE;
72
+
73
+ const realCandidate = safeRealpath(candidatePath);
74
+
75
+ // Rule 1: realpath equals process.execPath
76
+ if (execPath) {
77
+ const realExec = safeRealpath(execPath);
78
+ if (realCandidate === realExec) return true;
79
+ if (candidatePath === execPath) return true;
80
+ }
81
+
82
+ // Rule 2: candidate lives under APPDIR (the AppImage squashfs mount).
83
+ // We compare the candidate's realpath against APPDIR's realpath so a
84
+ // symlink under the mount is still recognized as a self-hit.
85
+ if (appDir) {
86
+ const realAppDir = safeRealpath(appDir);
87
+ const sep = path.sep;
88
+ // Append separator so /tmp/.mount_PI doesn't accidentally match
89
+ // /tmp/.mount_PIxx-elsewhere via prefix.
90
+ const prefix = realAppDir.endsWith(sep) ? realAppDir : realAppDir + sep;
91
+ if (realCandidate === realAppDir || realCandidate.startsWith(prefix)) return true;
92
+ // Literal fallback (broken symlinks / ENOENT keep a useful answer).
93
+ const litPrefix = appDir.endsWith(sep) ? appDir : appDir + sep;
94
+ if (candidatePath === appDir || candidatePath.startsWith(litPrefix)) return true;
95
+ }
96
+
97
+ // Rule 3: realpath equals APPIMAGE (the .AppImage file the user clicked).
98
+ if (appImage) {
99
+ const realAppImage = safeRealpath(appImage);
100
+ if (realCandidate === realAppImage) return true;
101
+ if (candidatePath === appImage) return true;
102
+ }
103
+
104
+ return false;
105
+ }
106
+
12
107
  /**
13
108
  * Well-known globalThis symbol for the default `ToolRegistry`.
14
109
  *
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Plugin bridge entry management in pi's settings.json.
3
+ *
4
+ * Manages `dashboard-<plugin-id>` keys in a dedicated
5
+ * `dashboardPluginBridges` object inside settings.json.
6
+ *
7
+ * Rules:
8
+ * - Only touches entries under the `dashboardPluginBridges` key.
9
+ * - NEVER modifies user-owned `packages[]` entries.
10
+ * - Uses atomic write (tmp + rename) for all updates.
11
+ * - Detects path conflicts (existing entry with mismatched path).
12
+ */
13
+ import fs from "node:fs";
14
+ import path from "node:path";
15
+ import os from "node:os";
16
+
17
+ export interface PluginBridgeRegisterOptions {
18
+ homedir?: string;
19
+ }
20
+
21
+ export type PluginBridgeConflict =
22
+ | { type: "ok" }
23
+ | { type: "conflict"; existingPath: string; newPath: string };
24
+
25
+ function getSettingsPath(homedir?: string): string {
26
+ const home = homedir ?? process.env.HOME ?? process.env.USERPROFILE ?? os.homedir();
27
+ return path.join(home, ".pi", "agent", "settings.json");
28
+ }
29
+
30
+ function readSettings(settingsPath: string): Record<string, unknown> {
31
+ try {
32
+ if (!fs.existsSync(settingsPath)) return {};
33
+ const raw = fs.readFileSync(settingsPath, "utf-8").trim();
34
+ if (!raw) return {};
35
+ return JSON.parse(raw);
36
+ } catch {
37
+ return {};
38
+ }
39
+ }
40
+
41
+ function writeSettings(settingsPath: string, settings: Record<string, unknown>): void {
42
+ fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
43
+ const tmp = settingsPath + ".tmp." + process.pid;
44
+ fs.writeFileSync(tmp, JSON.stringify(settings, null, 2) + "\n");
45
+ fs.renameSync(tmp, settingsPath);
46
+ }
47
+
48
+ function getManagedBridges(
49
+ settings: Record<string, unknown>,
50
+ ): Record<string, string> {
51
+ const val = settings.dashboardPluginBridges;
52
+ if (val && typeof val === "object" && !Array.isArray(val)) {
53
+ return val as Record<string, string>;
54
+ }
55
+ return {};
56
+ }
57
+
58
+ const MANAGED_PREFIX = "dashboard-";
59
+
60
+ /**
61
+ * Register a plugin's bridge entry in pi's settings.json.
62
+ *
63
+ * Returns { type: "conflict", existingPath, newPath } if a
64
+ * `dashboard-<pluginId>` key already exists but points to a different path.
65
+ * In that case the settings.json is NOT modified.
66
+ *
67
+ * Returns { type: "ok" } on success (including when the entry already matches).
68
+ */
69
+ export function registerPluginBridge(
70
+ pluginId: string,
71
+ bridgePath: string,
72
+ opts: PluginBridgeRegisterOptions = {},
73
+ ): PluginBridgeConflict {
74
+ const settingsPath = getSettingsPath(opts.homedir);
75
+ const settings = readSettings(settingsPath);
76
+ const managed = getManagedBridges(settings);
77
+ const key = MANAGED_PREFIX + pluginId;
78
+
79
+ const existing = managed[key];
80
+ if (existing) {
81
+ if (existing === bridgePath) return { type: "ok" }; // already registered
82
+ return { type: "conflict", existingPath: existing, newPath: bridgePath };
83
+ }
84
+
85
+ managed[key] = bridgePath;
86
+ settings.dashboardPluginBridges = managed;
87
+ writeSettings(settingsPath, settings);
88
+ console.info(`[plugin-bridge] Registered bridge for plugin "${pluginId}": ${bridgePath}`);
89
+ return { type: "ok" };
90
+ }
91
+
92
+ /**
93
+ * Remove a plugin's bridge entry from pi's settings.json.
94
+ * No-op if the entry does not exist.
95
+ * NEVER touches entries without the `dashboard-` prefix.
96
+ */
97
+ export function deregisterPluginBridge(
98
+ pluginId: string,
99
+ opts: PluginBridgeRegisterOptions = {},
100
+ ): void {
101
+ const settingsPath = getSettingsPath(opts.homedir);
102
+ const settings = readSettings(settingsPath);
103
+ const managed = getManagedBridges(settings);
104
+ const key = MANAGED_PREFIX + pluginId;
105
+
106
+ if (!(key in managed)) return; // nothing to remove
107
+
108
+ delete managed[key];
109
+ settings.dashboardPluginBridges = managed;
110
+ writeSettings(settingsPath, settings);
111
+ console.info(`[plugin-bridge] Deregistered bridge for plugin "${pluginId}"`);
112
+ }
113
+
114
+ /**
115
+ * Register all plugins with bridge entries from the discovery list.
116
+ * Returns a map of pluginId → conflict/ok result.
117
+ * Plugins with conflicts are NOT registered; caller should surface via /api/health.
118
+ */
119
+ export function registerAllPluginBridges(
120
+ plugins: Array<{ pluginId: string; bridgePath: string }>,
121
+ opts: PluginBridgeRegisterOptions = {},
122
+ ): Record<string, PluginBridgeConflict> {
123
+ const results: Record<string, PluginBridgeConflict> = {};
124
+ for (const { pluginId, bridgePath } of plugins) {
125
+ results[pluginId] = registerPluginBridge(pluginId, bridgePath, opts);
126
+ }
127
+ return results;
128
+ }
129
+
130
+ /**
131
+ * List all currently managed plugin bridge entries.
132
+ */
133
+ export function listManagedBridges(
134
+ opts: PluginBridgeRegisterOptions = {},
135
+ ): Record<string, string> {
136
+ const settingsPath = getSettingsPath(opts.homedir);
137
+ const settings = readSettings(settingsPath);
138
+ return getManagedBridges(settings);
139
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Extension ↔ Server WebSocket protocol messages.
3
3
  */
4
- import type { DashboardEvent, CommandInfo, FlowInfo, SessionSource, ImageContent, FileEntry, TurnUsage, ContextUsage, ModelInfo, PiSessionInfo, OpenSpecPhase, RoleInfo } from "./types.js";
4
+ import type { DashboardEvent, CommandInfo, FlowInfo, SessionSource, ImageContent, FileEntry, TurnUsage, ContextUsage, ModelInfo, PiSessionInfo, OpenSpecPhase, RoleInfo, ExtensionUiModule, DecoratorDescriptor } from "./types.js";
5
5
 
6
6
  // ── Extension → Server ──────────────────────────────────────────────
7
7
 
@@ -228,6 +228,39 @@ export interface ProcessListMessage {
228
228
 
229
229
  // LoadSessionEventsResultMessage and LoadSessionEventsErrorMessage removed — server loads directly
230
230
 
231
+ // ── Extension UI System (Phase 1) ──
232
+ // Pull-discovered, schema-driven UI modules. See change: add-extension-ui-modal.
233
+
234
+ export interface UiModulesListMessage {
235
+ type: "ui_modules_list";
236
+ sessionId: string;
237
+ modules: ExtensionUiModule[];
238
+ }
239
+
240
+ export interface UiDataListMessage {
241
+ type: "ui_data_list";
242
+ sessionId: string;
243
+ /** Matches some `module.view.dataEvent`. */
244
+ event: string;
245
+ items: unknown[];
246
+ }
247
+
248
+ // ── Extension UI System (Phase 2: live in-page decorations) ──
249
+ // See change: add-extension-ui-decorations.
250
+
251
+ /**
252
+ * Extension → server: a single live decorator descriptor. Cache key
253
+ * `${kind}:${namespace}:${id}` MUST be unique within a session; `removed: true`
254
+ * deletes the cache entry instead of upserting.
255
+ */
256
+ export interface ExtUiDecoratorMessage {
257
+ type: "ext_ui_decorator";
258
+ sessionId: string;
259
+ descriptor: DecoratorDescriptor;
260
+ /** When true, server deletes the cached descriptor under the matching key. */
261
+ removed?: boolean;
262
+ }
263
+
231
264
  export type ExtensionToServerMessage =
232
265
  | SessionRegisterMessage
233
266
  | SessionUnregisterMessage
@@ -250,7 +283,10 @@ export type ExtensionToServerMessage =
250
283
  | FirstMessageUpdateMessage
251
284
  | RolesListMessage
252
285
  | SpawnNewSessionMessage
253
- | ProcessListMessage;
286
+ | ProcessListMessage
287
+ | UiModulesListMessage
288
+ | UiDataListMessage
289
+ | ExtUiDecoratorMessage;
254
290
 
255
291
  // ── Server → Extension ──────────────────────────────────────────────
256
292
 
@@ -401,6 +437,23 @@ export interface ExtensionUiResponseMessage {
401
437
  cancelled?: boolean;
402
438
  }
403
439
 
440
+ /**
441
+ * Server → extension: a browser invoked an action / requested data on a
442
+ * Phase-1 management-modal module. The bridge re-emits this on `pi.events`
443
+ * as `pi.events.emit(msg.event, { ...msg.params, action: msg.action, _reply })`.
444
+ * Extensions either populate `data.items` synchronously (for `action: "list"`
445
+ * data fetches) or perform side-effects and emit `ui:invalidate` to refresh.
446
+ */
447
+ export interface UiManagementMessage {
448
+ type: "ui_management";
449
+ sessionId: string;
450
+ /** Action id (matches some `UiAction.id`) or `"list"` for data fetch. */
451
+ action: string;
452
+ /** Event name to emit (matches `view.dataEvent` or `UiAction.event`). */
453
+ event: string;
454
+ params?: Record<string, unknown>;
455
+ }
456
+
404
457
  export interface PromptResponseServerMessage {
405
458
  type: "prompt_response";
406
459
  sessionId: string;
@@ -435,4 +488,5 @@ export type ServerToExtensionMessage =
435
488
  | RolePresetSaveExtensionMessage
436
489
  | RolePresetDeleteExtensionMessage
437
490
  | RequestRolesMessage
491
+ | UiManagementMessage
438
492
  | KillProcessMessage;
@@ -180,7 +180,13 @@ export const RECOMMENDED_EXTENSIONS: readonly RecommendedExtension[] = [
180
180
  */
181
181
  export const BUNDLED_EXTENSION_IDS: readonly string[] = [
182
182
  "pi-anthropic-messages",
183
- "pi-flows",
183
+ // "pi-flows" is intentionally NOT bundled until the upstream repo declares
184
+ // an SPDX-conformant license (`LICENSE` file or `package.json#license`).
185
+ // The bundle-recommended-extensions.sh license allowlist enforcement
186
+ // (MIT/Apache-2.0/BSD-2-Clause/BSD-3-Clause/ISC) correctly rejects it.
187
+ // Re-add this entry once https://github.com/BlackBeltTechnology/pi-flows
188
+ // has a license declared. See: openspec/changes/archive/
189
+ // 2026-04-21-bundle-first-party-extensions/design.md §"License blockers".
184
190
  ];
185
191
 
186
192
  /** Retrieve a recommended entry by id, or `undefined`. */
@@ -64,17 +64,35 @@ export type FileReadResponse = ApiResponse<FileReadResult>;
64
64
  export interface BrowseEntry {
65
65
  name: string;
66
66
  path: string;
67
- isGit: boolean;
68
- isPi: boolean;
67
+ /**
68
+ * Set only when the request used `detect=1`. When the response was
69
+ * produced without `detect=1`, this field is absent (undefined) —
70
+ * meaning "not classified", NOT "classified as not-git". Consumers
71
+ * that need badges SHOULD call `GET /api/browse/flags` to fill in
72
+ * the flags lazily.
73
+ *
74
+ * See change: split-browse-flags.
75
+ */
76
+ isGit?: boolean;
77
+ /** See `isGit` — same opt-in / detect-gated semantics. */
78
+ isPi?: boolean;
69
79
  }
70
80
 
71
81
  /**
72
- * Response shape for `GET /api/browse?path=<dir>&q=<query>`.
82
+ * Response shape for `GET /api/browse?path=<dir>&q=<query>&detect=<0|1>`.
73
83
  *
74
84
  * The optional `q` query parameter, when present and non-empty, causes the
75
85
  * server to filter entries by case-insensitive substring on `name` and rank
76
86
  * them (exact → prefix → word-boundary → substring) before the 200-entry cap.
77
87
  * When omitted or whitespace-only, entries are sorted alphabetically.
88
+ *
89
+ * The optional `detect` query parameter (only the literal string `"1"` is
90
+ * truthy) opts into eager `.git` / `.pi` classification on every entry. When
91
+ * absent (the default), per-entry `isGit` / `isPi` are omitted and no
92
+ * filesystem probes run — use the bulk `GET /api/browse/flags` endpoint to
93
+ * classify entries lazily.
94
+ *
95
+ * See change: split-browse-flags.
78
96
  */
79
97
  export interface BrowseResult {
80
98
  entries: BrowseEntry[];
@@ -94,6 +112,43 @@ export interface BrowseResult {
94
112
 
95
113
  export type BrowseResponse = ApiResponse<BrowseResult>;
96
114
 
115
+ // ── Browse flags (bulk classifier) ──────────────────────────────────
116
+
117
+ /**
118
+ * Per-path classification record returned by `GET /api/browse/flags`.
119
+ * Booleans only — any probe failure (ENOENT, EACCES, ELOOP, race-on-
120
+ * deletion, …) maps to `false` for that flag, never an error.
121
+ *
122
+ * See change: split-browse-flags.
123
+ */
124
+ export interface BrowseFlagEntry {
125
+ isGit: boolean;
126
+ isPi: boolean;
127
+ }
128
+
129
+ /**
130
+ * Wire shape passed via the `paths` query parameter on
131
+ * `GET /api/browse/flags?paths=<json-array>`. The value MUST be a
132
+ * URL-encoded JSON array of absolute path strings (length ≤ 100).
133
+ * Provided here for type-only documentation — the request itself is a
134
+ * GET, so this interface is not serialized as a body.
135
+ */
136
+ export interface BrowseFlagsRequest {
137
+ paths: string[];
138
+ }
139
+
140
+ /** Successful response payload for `GET /api/browse/flags`. */
141
+ export interface BrowseFlagsResult {
142
+ /**
143
+ * Map keyed by the absolute paths that were requested. The key set
144
+ * SHALL equal the input `paths` set — one classification per input
145
+ * path, no extras, no omissions.
146
+ */
147
+ flags: Record<string, BrowseFlagEntry>;
148
+ }
149
+
150
+ export type BrowseFlagsResponse = ApiResponse<BrowseFlagsResult>;
151
+
97
152
  /** Request body for `POST /api/browse/mkdir`. */
98
153
  export interface MkdirRequest {
99
154
  parent: string;
@@ -254,6 +309,16 @@ export interface InstalledPackage {
254
309
  installedPath?: string;
255
310
  /** Set after check-updates: true if newer version available */
256
311
  updateAvailable?: boolean;
312
+ /** Version read from `<installedPath>/package.json#version`. Undefined if missing/unreadable. */
313
+ version?: string;
314
+ /** Description read from `<installedPath>/package.json#description`. */
315
+ description?: string;
316
+ /** Friendly name. From RECOMMENDED_EXTENSIONS displayName when matched, else basename of source. */
317
+ displayName?: string;
318
+ /** True when this row matches a RECOMMENDED_EXTENSIONS entry (via sourcesMatch). */
319
+ isRecommended?: boolean;
320
+ /** True when isRecommended AND id is in BUNDLED_EXTENSION_IDS AND bundle subtree exists. */
321
+ isBundled?: boolean;
257
322
  }
258
323
 
259
324
  export type InstalledPackagesResponse = ApiResponse<InstalledPackage[]>;