@blackbelt-technology/pi-agent-dashboard 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -38
- package/docs/architecture.md +148 -2
- package/package.json +6 -4
- package/packages/extension/package.json +4 -3
- package/packages/extension/src/AGENTS.md +1 -0
- package/packages/extension/src/__tests__/bridge-resume-disconnect.test.ts +333 -0
- package/packages/extension/src/__tests__/openspec-cli-shim.test.ts +145 -0
- package/packages/extension/src/__tests__/provider-register-native-models.test.ts +182 -0
- package/packages/extension/src/__tests__/provider-register-thinking-levels.test.ts +54 -1
- package/packages/extension/src/__tests__/single-derivation-guard.test.ts +39 -0
- package/packages/extension/src/bridge-context.ts +21 -0
- package/packages/extension/src/bridge.ts +41 -6
- package/packages/extension/src/openspec-cli-shim.ts +173 -0
- package/packages/extension/src/openspec-cli-shim.ts.AGENTS.md +7 -0
- package/packages/extension/src/provider-register.ts +180 -17
- package/packages/extension/src/provider-register.ts.AGENTS.md +1 -1
- package/packages/extension/src/session-sync.ts +9 -4
- package/packages/server/package.json +8 -8
- package/packages/server/src/__tests__/decorate-goals-spend.test.ts +79 -0
- package/packages/server/src/__tests__/embed-lifecycle-boot-smoke.test.ts +44 -0
- package/packages/server/src/__tests__/goal-routes.test.ts +62 -7
- package/packages/server/src/__tests__/goal-supervisor.test.ts +4 -4
- package/packages/server/src/__tests__/headless-pid-registry.test.ts +7 -3
- package/packages/server/src/__tests__/memory-event-store.test.ts +369 -13
- package/packages/server/src/__tests__/models-introspection-thinking-map.test.ts +67 -0
- package/packages/server/src/__tests__/pi-changelog-routes.test.ts +9 -2
- package/packages/server/src/__tests__/recovery-offer.test.ts +42 -7
- package/packages/server/src/__tests__/recovery-reattach-retraction.test.ts +188 -0
- package/packages/server/src/__tests__/session-tags-persistence.test.ts +29 -0
- package/packages/server/src/browser-handlers/AGENTS.md +1 -1
- package/packages/server/src/browser-handlers/__tests__/session-meta-handler.test.ts +98 -1
- package/packages/server/src/browser-handlers/handler-context.ts +24 -13
- package/packages/server/src/browser-handlers/session-action-handler.ts +12 -1
- package/packages/server/src/browser-handlers/session-meta-handler.ts +29 -0
- package/packages/server/src/embed-lifecycle/AGENTS.md +17 -0
- package/packages/server/src/embed-lifecycle/__tests__/caps.test.ts +129 -0
- package/packages/server/src/embed-lifecycle/__tests__/embed-lifecycle-controller.test.ts +84 -0
- package/packages/server/src/embed-lifecycle/__tests__/identity-key.test.ts +73 -0
- package/packages/server/src/embed-lifecycle/__tests__/idle-reaper.test.ts +173 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-config.test.ts +26 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-event-capture.test.ts +32 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-marker-persistence.test.ts +106 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-metrics.test.ts +39 -0
- package/packages/server/src/embed-lifecycle/__tests__/liveness-probe.test.ts +77 -0
- package/packages/server/src/embed-lifecycle/__tests__/quiescence.test.ts +175 -0
- package/packages/server/src/embed-lifecycle/__tests__/reclamation-soak.test.ts +77 -0
- package/packages/server/src/embed-lifecycle/__tests__/session-lifecycle-policy.test.ts +38 -0
- package/packages/server/src/embed-lifecycle/__tests__/visitor-session-registry.test.ts +140 -0
- package/packages/server/src/embed-lifecycle/caps.ts +88 -0
- package/packages/server/src/embed-lifecycle/cwd-allowlist.ts +50 -0
- package/packages/server/src/embed-lifecycle/embed-lifecycle-controller.ts +90 -0
- package/packages/server/src/embed-lifecycle/identity-key.ts +73 -0
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +161 -0
- package/packages/server/src/embed-lifecycle/lifecycle-event-capture.ts +30 -0
- package/packages/server/src/embed-lifecycle/lifecycle-metrics.ts +70 -0
- package/packages/server/src/embed-lifecycle/liveness-probe.ts +123 -0
- package/packages/server/src/embed-lifecycle/quiescence.ts +159 -0
- package/packages/server/src/embed-lifecycle/session-lifecycle-policy.ts +31 -0
- package/packages/server/src/embed-lifecycle/visitor-session-registry.ts +178 -0
- package/packages/server/src/event-wiring.ts +20 -0
- package/packages/server/src/event-wiring.ts.AGENTS.md +1 -0
- package/packages/server/src/goal/AGENTS.md +1 -0
- package/packages/server/src/goal/decorate-goals-spend.ts +47 -0
- package/packages/server/src/goal/goal-supervisor.ts +5 -3
- package/packages/server/src/model-proxy/AGENTS.md +2 -2
- package/packages/server/src/model-proxy/__tests__/internal-registry-native-merge.test.ts +155 -0
- package/packages/server/src/model-proxy/__tests__/read-native-models.test.ts +92 -0
- package/packages/server/src/model-proxy/internal-registry.ts +49 -23
- package/packages/server/src/model-proxy/registry-singleton.ts +15 -9
- package/packages/server/src/pairing/browser-gateway.ts +40 -11
- package/packages/server/src/pairing/browser-gateway.ts.AGENTS.md +1 -1
- package/packages/server/src/persistence/memory-event-store.ts +369 -23
- package/packages/server/src/persistence/memory-event-store.ts.AGENTS.md +1 -1
- package/packages/server/src/routes/AGENTS.md +2 -2
- package/packages/server/src/routes/goal-routes.ts +14 -9
- package/packages/server/src/routes/models-introspection-routes.ts +10 -0
- package/packages/server/src/routes/system-routes.ts +16 -1
- package/packages/server/src/server.ts +216 -65
- package/packages/server/src/server.ts.AGENTS.md +1 -1
- package/packages/server/src/session/memory-session-manager.ts +4 -0
- package/packages/server/src/session/session-scanner.ts +9 -0
- package/packages/server/src/session/session-to-meta.ts +6 -0
- package/packages/server/src/spawn-process/headless-pid-registry.ts +14 -4
- package/packages/shared/package.json +1 -1
- package/packages/shared/src/AGENTS.md +5 -3
- package/packages/shared/src/__tests__/models-json-reader.test.ts +100 -0
- package/packages/shared/src/__tests__/nightly-workflow-contract.test.ts +125 -0
- package/packages/shared/src/__tests__/openspec-poller-1.6.0-compat.test.ts +112 -0
- package/packages/shared/src/__tests__/pnpm-migration-contract.test.ts +177 -0
- package/packages/shared/src/__tests__/publish-workflow-contract.test.ts +6 -3
- package/packages/shared/src/browser-protocol.ts +22 -0
- package/packages/shared/src/config.ts +70 -0
- package/packages/shared/src/dashboard-plugin/AGENTS.md +1 -1
- package/packages/shared/src/dashboard-plugin/slot-props.ts +9 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +19 -0
- package/packages/shared/src/models-json-reader.ts +98 -0
- package/packages/shared/src/platform/AGENTS.md +1 -1
- package/packages/shared/src/platform/paths.ts +12 -0
- package/packages/shared/src/session-meta.ts +9 -0
- package/packages/shared/src/types.ts +52 -1
- package/docs/AGENTS.md +0 -47
- package/packages/extension/AGENTS.md +0 -7
- package/packages/server/AGENTS.md +0 -7
- package/packages/shared/AGENTS.md +0 -7
- package/scripts/AGENTS.md +0 -38
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* See change: fix-thinking-level-supported-projection.
|
|
10
10
|
*/
|
|
11
11
|
import { describe, expect, it } from "vitest";
|
|
12
|
-
import { toModelInfo } from "../provider-register.js";
|
|
12
|
+
import { deriveSupportedThinkingLevels, toModelInfo } from "../provider-register.js";
|
|
13
13
|
|
|
14
14
|
const ALL_SIX = ["off", "minimal", "low", "medium", "high", "xhigh"];
|
|
15
15
|
|
|
@@ -48,4 +48,57 @@ describe("toModelInfo — supportedThinkingLevels projection", () => {
|
|
|
48
48
|
const info = toModelInfo({ provider: "openai", id: "gpt" });
|
|
49
49
|
expect(info.supportedThinkingLevels).toBeUndefined();
|
|
50
50
|
});
|
|
51
|
+
|
|
52
|
+
it("never projects compat or credential material into ModelInfo (S1/§7.1)", () => {
|
|
53
|
+
const info = toModelInfo({
|
|
54
|
+
provider: "newapi",
|
|
55
|
+
id: "glm-5.2",
|
|
56
|
+
reasoning: true,
|
|
57
|
+
thinkingLevelMap: { xhigh: "xhigh" },
|
|
58
|
+
compat: { thinkingFormat: "deepseek" },
|
|
59
|
+
apiKey: "sk-secret",
|
|
60
|
+
});
|
|
61
|
+
expect(JSON.stringify(info)).not.toMatch(/compat|thinkingFormat|deepseek|apiKey|sk-secret/i);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("native thinkingLevelMap flows through toModelInfo (F1)", () => {
|
|
65
|
+
// Test runtime is pi-ai 0.75.5 (no `max`), so `max` is never surfaced here.
|
|
66
|
+
const info = toModelInfo({
|
|
67
|
+
provider: "newapi",
|
|
68
|
+
id: "glm-5.2",
|
|
69
|
+
reasoning: true,
|
|
70
|
+
thinkingLevelMap: { xhigh: "xhigh" },
|
|
71
|
+
});
|
|
72
|
+
expect(info.supportedThinkingLevels).toEqual(ALL_SIX);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// SYNTHETIC table (NOT the pinned pi-ai 0.75.5 fn, which has no `max` branch):
|
|
77
|
+
// max is opt-in AND runtime-gated, fail-CLOSED. See change:
|
|
78
|
+
// honor-native-models-json-metadata (test-plan E11).
|
|
79
|
+
describe("deriveSupportedThinkingLevels — runtime-gated max (E11)", () => {
|
|
80
|
+
const MAX_ONLY = { minimal: null, low: null, medium: null, high: null, xhigh: null, max: "max" };
|
|
81
|
+
|
|
82
|
+
it("emits max only when maxSupported AND the map opts in", () => {
|
|
83
|
+
expect(deriveSupportedThinkingLevels(true, MAX_ONLY, true)).toEqual(["off", "max"]);
|
|
84
|
+
expect(deriveSupportedThinkingLevels(true, MAX_ONLY, false)).toEqual(["off"]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("never emits max when the map omits it, even on a max-capable runtime", () => {
|
|
88
|
+
expect(deriveSupportedThinkingLevels(true, { xhigh: "xhigh" }, true)).toEqual(ALL_SIX);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("reasoning model with no map supports all levels except xhigh and max", () => {
|
|
92
|
+
expect(deriveSupportedThinkingLevels(true, undefined, true)).toEqual([
|
|
93
|
+
"off",
|
|
94
|
+
"minimal",
|
|
95
|
+
"low",
|
|
96
|
+
"medium",
|
|
97
|
+
"high",
|
|
98
|
+
]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("non-reasoning model supports only off", () => {
|
|
102
|
+
expect(deriveSupportedThinkingLevels(false, MAX_ONLY, true)).toEqual(["off"]);
|
|
103
|
+
});
|
|
51
104
|
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guard: exactly ONE authored `deriveSupportedThinkingLevels` exists (in the
|
|
3
|
+
* bridge extension, parameterized by `maxSupported`), and the dashboard server
|
|
4
|
+
* derives NO supported-thinking-levels list — it passes through the raw
|
|
5
|
+
* `thinkingLevelMap`. Prevents a second derivation copy drifting from the
|
|
6
|
+
* canonical one.
|
|
7
|
+
*
|
|
8
|
+
* See change: honor-native-models-json-metadata (D-X1, test-plan / task §6.1).
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { describe, expect, it } from "vitest";
|
|
13
|
+
|
|
14
|
+
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
15
|
+
const read = (rel: string) => readFileSync(fileURLToPath(new URL(rel, `file://${here}`)), "utf-8");
|
|
16
|
+
|
|
17
|
+
function count(haystack: string, needle: string): number {
|
|
18
|
+
return haystack.split(needle).length - 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe("single supportedThinkingLevels derivation (D-X1)", () => {
|
|
22
|
+
it("the extension declares exactly one deriveSupportedThinkingLevels", () => {
|
|
23
|
+
const src = read("../provider-register.ts");
|
|
24
|
+
expect(count(src, "function deriveSupportedThinkingLevels")).toBe(1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("the server introspection route derives + emits no supportedThinkingLevels", () => {
|
|
28
|
+
const route = read("../../../server/src/routes/models-introspection-routes.ts");
|
|
29
|
+
expect(route).not.toContain("deriveSupportedThinkingLevels");
|
|
30
|
+
// No object-key emission of a supported-levels list (prose mentions are fine).
|
|
31
|
+
expect(route).not.toMatch(/\bsupportedThinkingLevels\s*[:=]/);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("the server registry declares no thinking-level derivation", () => {
|
|
35
|
+
const reg = read("../../../server/src/model-proxy/internal-registry.ts");
|
|
36
|
+
expect(reg).not.toContain("deriveSupportedThinkingLevels");
|
|
37
|
+
expect(reg).not.toMatch(/\bsupportedThinkingLevels\s*[:=]/);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -215,6 +215,27 @@ export function extractFirstAssistantReply(ctx: any): string | undefined {
|
|
|
215
215
|
return undefined;
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Defensive read of pi's `ctx.cwd` guarded getter. That getter throws once the
|
|
220
|
+
* session is replaced (new/fork/resume/reload). During a `session_start` for a
|
|
221
|
+
* replacement, `handleSessionChange(ctx)` runs BEFORE `connection.connect()`;
|
|
222
|
+
* an un-guarded `ctx.cwd` read there throws, the bridge's `safe()` wrapper
|
|
223
|
+
* swallows it, `connect()` is skipped, and the socket the preceding
|
|
224
|
+
* `session_shutdown` closed stays terminally down — the #393 permanent
|
|
225
|
+
* disconnect. Reading through `safeCwd` yields `process.cwd()` on a throw so the
|
|
226
|
+
* re-registration path always reaches `connect()`. See change:
|
|
227
|
+
* fix-bridge-resume-disconnect.
|
|
228
|
+
*/
|
|
229
|
+
export function safeCwd(ctx: unknown): string {
|
|
230
|
+
try {
|
|
231
|
+
const c = (ctx as { cwd?: unknown } | null | undefined)?.cwd;
|
|
232
|
+
if (typeof c === "string") return c;
|
|
233
|
+
} catch {
|
|
234
|
+
/* guarded getter threw — session was replaced */
|
|
235
|
+
}
|
|
236
|
+
return process.cwd();
|
|
237
|
+
}
|
|
238
|
+
|
|
218
239
|
/** Get current model string (provider/id) from cached context */
|
|
219
240
|
export function getCurrentModelString(bc: BridgeContext): string | undefined {
|
|
220
241
|
const model = bc.cachedCtx?.model;
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
import { registerAskUserTool } from "./ask-user-tool.js";
|
|
28
28
|
import { type AutoNamer, createAutoNamer, type StreamSimpleFn } from "./auto-session-namer.js";
|
|
29
29
|
import type { BridgeContext } from "./bridge-context.js";
|
|
30
|
-
import { extractFirstAssistantReply, extractFirstMessage, filterHiddenCommands, getCurrentModelString, isHeadlessRpcSession } from "./bridge-context.js";
|
|
30
|
+
import { extractFirstAssistantReply, extractFirstMessage, filterHiddenCommands, getCurrentModelString, isHeadlessRpcSession, safeCwd } from "./bridge-context.js";
|
|
31
31
|
import { shouldApplyDefaultModel } from "./bridge-default-model-gate.js";
|
|
32
32
|
import { registerCanvasTool } from "./canvas-tool.js";
|
|
33
33
|
import { createCommandHandler, tryExecSlashTemplate } from "./command-handler.js";
|
|
@@ -36,6 +36,7 @@ import { ConnectionManager } from "./connection.js";
|
|
|
36
36
|
import { registerDashboardContextInjector } from "./dashboard-context-injector.js";
|
|
37
37
|
import { DashboardDefaultAdapter } from "./dashboard-default-adapter.js";
|
|
38
38
|
import { runDevBuild } from "./dev-build.js";
|
|
39
|
+
import { provisionOpenspecCli } from "./openspec-cli-shim.js";
|
|
39
40
|
import { EmptyActionableGuard, SURFACE_MESSAGE } from "./empty-actionable-guard.js";
|
|
40
41
|
import { resolveGuardConfig } from "./empty-actionable-guard-config.js";
|
|
41
42
|
import { mapEventToProtocol } from "./event-forwarder.js";
|
|
@@ -2025,6 +2026,32 @@ function initBridge(pi: ExtensionAPI) {
|
|
|
2025
2026
|
cachedCtx = ctx;
|
|
2026
2027
|
sessionId = newSessionId;
|
|
2027
2028
|
|
|
2029
|
+
// Provision the openspec CLI: drop a shim on process.env.PATH so the
|
|
2030
|
+
// generated openspec-* skills' bare `openspec` resolves in-session. Runs on
|
|
2031
|
+
// init AND /reload (re-points the shim on extension upgrade); the PATH
|
|
2032
|
+
// prepend is idempotent. Fail-soft: on hard failure, surface a
|
|
2033
|
+
// dashboard-visible missing-tool signal. See change:
|
|
2034
|
+
// provision-openspec-cli-in-sessions.
|
|
2035
|
+
provisionOpenspecCli({
|
|
2036
|
+
onMissingTool: (reason) => {
|
|
2037
|
+
connection.send({
|
|
2038
|
+
type: "event_forward",
|
|
2039
|
+
sessionId,
|
|
2040
|
+
event: {
|
|
2041
|
+
eventType: "bash_output",
|
|
2042
|
+
timestamp: Date.now(),
|
|
2043
|
+
data: {
|
|
2044
|
+
command: "openspec (session provisioning)",
|
|
2045
|
+
output: reason,
|
|
2046
|
+
exitCode: 127,
|
|
2047
|
+
excludeFromContext: true,
|
|
2048
|
+
missingTool: { kind: "missing-tool", toolName: "openspec" },
|
|
2049
|
+
},
|
|
2050
|
+
},
|
|
2051
|
+
});
|
|
2052
|
+
},
|
|
2053
|
+
});
|
|
2054
|
+
|
|
2028
2055
|
// Wrap sessionManager.appendMessage so that future message_end events can
|
|
2029
2056
|
// recover the just-generated entry id, even when their setTimeout(0)
|
|
2030
2057
|
// fires before pi has finished mutating event.message in place. The
|
|
@@ -2368,10 +2395,15 @@ function initBridge(pi: ExtensionAPI) {
|
|
|
2368
2395
|
// register so server can re-stamp source after restart. Derived from the
|
|
2369
2396
|
// capture-once boolean (token may already be scrubbed). See change:
|
|
2370
2397
|
// fix-spawn-token-env-leak.
|
|
2398
|
+
// ctx.cwd is the guarded getter that throws once the session is replaced.
|
|
2399
|
+
// Read it once, defensively — a throw here would skip session_register +
|
|
2400
|
+
// the heartbeat/git timers, leaving a resumed session dead in the UI.
|
|
2401
|
+
// See change: fix-bridge-resume-disconnect.
|
|
2402
|
+
const startCwd = safeCwd(ctx);
|
|
2371
2403
|
connection.send({
|
|
2372
2404
|
type: "session_register",
|
|
2373
2405
|
sessionId,
|
|
2374
|
-
cwd:
|
|
2406
|
+
cwd: startCwd,
|
|
2375
2407
|
name: lastSessionName || undefined,
|
|
2376
2408
|
source: detectSessionSource(cachedHasUI, sessionFile),
|
|
2377
2409
|
model: initialModel,
|
|
@@ -2383,7 +2415,7 @@ function initBridge(pi: ExtensionAPI) {
|
|
|
2383
2415
|
...(dashboardSpawned ? { dashboardSpawned: true } : {}),
|
|
2384
2416
|
// Tri-state git-repo signal, computed at register time (authority).
|
|
2385
2417
|
// See change: gate-session-worktree-button-on-git.
|
|
2386
|
-
isGitRepo: detectIsGitRepo(
|
|
2418
|
+
isGitRepo: detectIsGitRepo(startCwd),
|
|
2387
2419
|
// Fact-forwarding: server decides auto-hide. See change:
|
|
2388
2420
|
// auto-hide-headless-worker-sessions.
|
|
2389
2421
|
...buildVisibilityRegisterFields(cachedHasUI, process.env),
|
|
@@ -2543,8 +2575,8 @@ function initBridge(pi: ExtensionAPI) {
|
|
|
2543
2575
|
}).catch(() => { stopSpinner(); });
|
|
2544
2576
|
|
|
2545
2577
|
// Send initial git info + the session's pi version
|
|
2546
|
-
sendGitInfoIfChanged(
|
|
2547
|
-
sendCwdMissingIfChanged(
|
|
2578
|
+
sendGitInfoIfChanged(startCwd);
|
|
2579
|
+
sendCwdMissingIfChanged(startCwd);
|
|
2548
2580
|
sendPiVersionIfChanged();
|
|
2549
2581
|
|
|
2550
2582
|
// Start metrics monitor and heartbeat
|
|
@@ -2635,7 +2667,10 @@ function initBridge(pi: ExtensionAPI) {
|
|
|
2635
2667
|
// Caches ctx.cwd (the throwing getter) and clears any prior timer first.
|
|
2636
2668
|
function startGitPollTimer(ctx: any) {
|
|
2637
2669
|
if (gitPollTimer) clearInterval(gitPollTimer);
|
|
2638
|
-
|
|
2670
|
+
// safeCwd: startGitPollTimer runs inside handleSessionChange BEFORE
|
|
2671
|
+
// connection.connect(); an un-guarded ctx.cwd throw here skips connect()
|
|
2672
|
+
// (#393). See change: fix-bridge-resume-disconnect.
|
|
2673
|
+
cachedCwd = safeCwd(ctx);
|
|
2639
2674
|
gitPollTimer = setInterval(() => runGitPollTick({
|
|
2640
2675
|
isActive,
|
|
2641
2676
|
cachedCwd: () => cachedCwd,
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* openspec-cli-shim — make bare `openspec` resolvable inside a pi session.
|
|
3
|
+
*
|
|
4
|
+
* The generated `openspec-*` skills invoke the CLI by bare name. A pi bash
|
|
5
|
+
* child inherits `getShellEnv()`, which reads LIVE `process.env` per call. On a
|
|
6
|
+
* machine with no global `openspec`, bare `openspec` does not resolve and the
|
|
7
|
+
* agent silently degrades to hand-editing artifacts. The only in-session lever
|
|
8
|
+
* is `process.env.PATH`: at bridge init we drop an `openspec` shim into a
|
|
9
|
+
* dedicated dir and prepend that dir to `process.env.PATH`.
|
|
10
|
+
*
|
|
11
|
+
* Design: a DEDICATED dir holding only `openspec` (never `.bin`, which would
|
|
12
|
+
* shadow unrelated binaries); an EXTENSIONLESS `#!/bin/sh` script (Git Bash
|
|
13
|
+
* `bash.exe -c` ignores PATHEXT, so a `.cmd` would not resolve bare `openspec`);
|
|
14
|
+
* the shim invokes the pinned bin via an ABSOLUTE node path (`process.execPath`)
|
|
15
|
+
* so it survives a stripped system PATH; written atomically (temp + rename) and
|
|
16
|
+
* RE-POINTED every init so an extension upgrade refreshes the target. Fail-soft:
|
|
17
|
+
* on any error, log + emit a `missingTool`-style signal, never throw.
|
|
18
|
+
*
|
|
19
|
+
* See change: provision-openspec-cli-in-sessions.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import fs from "node:fs";
|
|
23
|
+
import { createRequire } from "node:module";
|
|
24
|
+
import os from "node:os";
|
|
25
|
+
import path from "node:path";
|
|
26
|
+
|
|
27
|
+
const require = createRequire(import.meta.url);
|
|
28
|
+
|
|
29
|
+
/** The single-source pinned CLI (see `openspec-cli-version-single-source`; 1.6.0). */
|
|
30
|
+
const OPENSPEC_PKG = "@fission-ai/openspec";
|
|
31
|
+
|
|
32
|
+
export interface ProvisionDeps {
|
|
33
|
+
/** Resolve the pinned openspec bin path. Default: real package resolution. */
|
|
34
|
+
resolveBin?: () => string;
|
|
35
|
+
/** Base dir the shim dir lives under. Default: `~/.pi/dashboard`. */
|
|
36
|
+
baseDir?: string;
|
|
37
|
+
/** Absolute node used in the shim shebang line. Default: `process.execPath`. */
|
|
38
|
+
execPath?: string;
|
|
39
|
+
/** Env object whose PATH is prepended in place. Default: `process.env`. */
|
|
40
|
+
env?: NodeJS.ProcessEnv;
|
|
41
|
+
/** Diagnostic logger. Default: `console.warn`. */
|
|
42
|
+
log?: (msg: string, err?: unknown) => void;
|
|
43
|
+
/** Hard-failure surface (bridge injects a dashboard `missingTool` emit). */
|
|
44
|
+
onMissingTool?: (reason: string) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ProvisionResult {
|
|
48
|
+
ok: boolean;
|
|
49
|
+
shimDir?: string;
|
|
50
|
+
binPath?: string;
|
|
51
|
+
reason?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolve the pinned `openspec` bin. `@fission-ai/openspec` exports only `"."`,
|
|
56
|
+
* so `require.resolve("@fission-ai/openspec/bin/openspec.js")` (and even
|
|
57
|
+
* `.../package.json`) throw `ERR_PACKAGE_PATH_NOT_EXPORTED`. Resolve the main
|
|
58
|
+
* entry (the `"."` export) then walk up to the package root and join `bin`.
|
|
59
|
+
*/
|
|
60
|
+
export function resolveOpenspecBin(
|
|
61
|
+
resolveEntry: (spec: string) => string = (spec) => require.resolve(spec),
|
|
62
|
+
fileExists: (p: string) => boolean = fs.existsSync,
|
|
63
|
+
readFile: (p: string) => string = (p) => fs.readFileSync(p, "utf8"),
|
|
64
|
+
): string {
|
|
65
|
+
let dir = path.dirname(resolveEntry(OPENSPEC_PKG));
|
|
66
|
+
for (let i = 0; i < 10; i++) {
|
|
67
|
+
const pj = path.join(dir, "package.json");
|
|
68
|
+
if (fileExists(pj)) {
|
|
69
|
+
const pkg = JSON.parse(readFile(pj)) as { name?: string; bin?: string | Record<string, string> };
|
|
70
|
+
if (pkg.name === OPENSPEC_PKG) {
|
|
71
|
+
const rel = typeof pkg.bin === "string" ? pkg.bin : pkg.bin?.openspec;
|
|
72
|
+
if (!rel) throw new Error(`${OPENSPEC_PKG} package.json has no bin.openspec`);
|
|
73
|
+
return path.join(dir, rel);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const parent = path.dirname(dir);
|
|
77
|
+
if (parent === dir) break;
|
|
78
|
+
dir = parent;
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`could not locate ${OPENSPEC_PKG} package root`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Ensure `dir` exists, owner-only (`0700`) so a world-writable path can't hijack the shim. */
|
|
84
|
+
export function ensureShimDir(dir: string): void {
|
|
85
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
86
|
+
// recursive mkdir does not chmod an existing dir; enforce perms explicitly.
|
|
87
|
+
fs.chmodSync(dir, 0o700);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Write the extensionless `openspec` shim to `shimDir`, atomically (temp +
|
|
92
|
+
* rename) and RE-POINTED to `binPath` every call. Mode `0755` (executable).
|
|
93
|
+
* `execPath` is the absolute node the shim `exec`s, so it needs no node on PATH.
|
|
94
|
+
*/
|
|
95
|
+
export function writeShim(shimDir: string, binPath: string, execPath: string): string {
|
|
96
|
+
const shimPath = path.join(shimDir, "openspec");
|
|
97
|
+
// Single-quote both paths so a path containing `$`, a backtick, whitespace, or
|
|
98
|
+
// a quote cannot expand or break the `exec` line (defense-in-depth: the paths
|
|
99
|
+
// are trusted — require.resolve'd bin + process.execPath — but shell-safe by
|
|
100
|
+
// construction). POSIX single-quote escaping: close, emit \', reopen.
|
|
101
|
+
const script = `#!/bin/sh\nexec ${shSingleQuote(execPath)} ${shSingleQuote(binPath)} "$@"\n`;
|
|
102
|
+
const tmp = path.join(shimDir, `.openspec.${process.pid}.${Date.now()}.tmp`);
|
|
103
|
+
fs.writeFileSync(tmp, script, { mode: 0o755 });
|
|
104
|
+
fs.chmodSync(tmp, 0o755); // writeFileSync mode is umask-masked; enforce explicitly.
|
|
105
|
+
fs.renameSync(tmp, shimPath); // atomic replace
|
|
106
|
+
return shimPath;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Prepend `dir` to `env.PATH` idempotently, comparing CANONICAL (realpath)
|
|
111
|
+
* forms so `/reload` does not duplicate the entry. Returns true if it prepended.
|
|
112
|
+
*/
|
|
113
|
+
export function prependDirToPath(dir: string, env: NodeJS.ProcessEnv): boolean {
|
|
114
|
+
const canonical = canonicalize(dir);
|
|
115
|
+
const current = env.PATH ?? "";
|
|
116
|
+
const entries = current.split(path.delimiter).filter((e) => e.length > 0);
|
|
117
|
+
const already = entries.some((e) => canonicalize(e) === canonical);
|
|
118
|
+
if (already) return false;
|
|
119
|
+
env.PATH = current.length > 0 ? `${canonical}${path.delimiter}${current}` : canonical;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** POSIX single-quote a string for safe embedding in a `/bin/sh` script. */
|
|
124
|
+
function shSingleQuote(s: string): string {
|
|
125
|
+
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function canonicalize(p: string): string {
|
|
129
|
+
try {
|
|
130
|
+
return fs.realpathSync(path.resolve(p));
|
|
131
|
+
} catch {
|
|
132
|
+
return path.resolve(p);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Provision the session so bare `openspec` resolves. Idempotent + fail-soft.
|
|
138
|
+
* Mutates `env.PATH` in place (default `process.env`). Never throws.
|
|
139
|
+
*/
|
|
140
|
+
export function provisionOpenspecCli(deps: ProvisionDeps = {}): ProvisionResult {
|
|
141
|
+
const {
|
|
142
|
+
resolveBin = () => resolveOpenspecBin(),
|
|
143
|
+
baseDir = path.join(os.homedir(), ".pi", "dashboard"),
|
|
144
|
+
execPath = process.execPath,
|
|
145
|
+
env = process.env,
|
|
146
|
+
log = (msg, err) => console.warn(msg, err ?? ""),
|
|
147
|
+
onMissingTool,
|
|
148
|
+
} = deps;
|
|
149
|
+
|
|
150
|
+
let binPath: string;
|
|
151
|
+
try {
|
|
152
|
+
binPath = resolveBin();
|
|
153
|
+
} catch (err) {
|
|
154
|
+
const reason = `[dashboard] openspec CLI provisioning failed: cannot resolve ${OPENSPEC_PKG}`;
|
|
155
|
+
log(reason, err);
|
|
156
|
+
onMissingTool?.(reason);
|
|
157
|
+
return { ok: false, reason };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const shimDir = path.join(baseDir, "openspec-shim");
|
|
161
|
+
try {
|
|
162
|
+
ensureShimDir(shimDir);
|
|
163
|
+
writeShim(shimDir, binPath, execPath);
|
|
164
|
+
prependDirToPath(shimDir, env);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
const reason = `[dashboard] openspec CLI provisioning failed: cannot write shim in ${shimDir}`;
|
|
167
|
+
log(reason, err);
|
|
168
|
+
onMissingTool?.(reason);
|
|
169
|
+
return { ok: false, reason };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { ok: true, shimDir, binPath };
|
|
173
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# openspec-cli-shim.ts — index
|
|
2
|
+
|
|
3
|
+
Provision bare `openspec` inside a pi session so the generated `openspec-*` skills' bare-name CLI calls resolve (the sole in-session lever is `process.env.PATH`, which `getShellEnv()` reads live per bash child). At bridge `session_start` (init AND `/reload`): resolve the pinned `@fission-ai/openspec` bin (`@fission-ai/openspec` exports only `.`, so `require.resolve` the main entry then walk up to the package root and join `bin/openspec.js` — the `/bin/*` and `/package.json` subpaths throw `ERR_PACKAGE_PATH_NOT_EXPORTED`); ensure a `0700` shim dir `~/.pi/dashboard/openspec-shim`; write an EXTENSIONLESS `#!/bin/sh` shim `exec "<process.execPath>" "<bin.js>" "$@"` (absolute node → survives a stripped PATH; extensionless → resolves under Git Bash `bash.exe -c`, which ignores PATHEXT so a `.cmd` would not) atomically (temp+rename) and RE-POINTED every init; idempotently prepend the canonical (realpath) shim dir to `env.PATH` (compare realpath forms, prepend-only, non-destructive). Fail-soft: on resolve/write error, log + invoke `onMissingTool` (bridge sends a `missingTool`-style `bash_output` for `openspec`), never throw.
|
|
4
|
+
|
|
5
|
+
Exports `provisionOpenspecCli(deps)` (orchestrator; `deps` seams `resolveBin`/`baseDir`/`execPath`/`env`/`log`/`onMissingTool` for hermetic tests), `resolveOpenspecBin`, `ensureShimDir`, `writeShim`, `prependDirToPath`. Version single-sourced at 1.6.0 (see `openspec-cli-version-single-source`). Wired in `bridge.ts` `session_start`; covered by `__tests__/openspec-cli-shim.test.ts` (T-E1..E4, X1, X2) + qa `12-openspec-shim.ps1` (T-C1).
|
|
6
|
+
|
|
7
|
+
See change: provision-openspec-cli-in-sessions.
|