diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 03b87ed8b7..8fd29a866f 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -1883,6 +1883,28 @@ function isOursZcodeMcpServer(server: unknown): boolean { return !!s && s.type === "stdio" && zcodeArgsMatch(s.args, ZCODE_MCP_ENTRY_RE); } +// Pre-#1145 installs managed a `bili mcp` shim (command bili/bili.exe, args +// ["mcp"]) instead of pointing at dist/zcode/mcp-entry.js. That shim is still +// OURS: without recognizing it the lane reports "MCP face missing" AND +// applyZcodeManagedConfig refuses to touch our own leftover, so an upgrade +// could never repair the face it wrote (the user has to hand-edit the file). +const ZCODE_LEGACY_MCP_COMMAND_RE = /(?:^|[\\/])bili(?:-proxy)?(?:\.exe)?$/i; + +function isLegacyOursZcodeMcpServer(server: unknown): boolean { + const s = zcodeAsPlain(server); + if (!s || s.type !== "stdio" || typeof s.command !== "string") return false; + if (!ZCODE_LEGACY_MCP_COMMAND_RE.test(s.command)) return false; + const args = s.args; + return Array.isArray(args) && args.length === 1 && args[0] === "mcp"; +} + +/** Ownership, not health: the current dist face and the legacy shim were both + * written by this installer, so both may be replaced/removed — while a + * user-authored "bili" server still throws (§7.3). */ +function isOurZcodeMcpOwnership(server: unknown): boolean { + return isOursZcodeMcpServer(server) || isLegacyOursZcodeMcpServer(server); +} + /** Managed-block apply for ~/.zcode/cli/config.json (#1145). Pure over the * parsed doc: enables hooks, upserts our SessionStart entry, and owns ONLY * mcp.servers.bili when it already points at our dist — a user's own "bili" @@ -1905,9 +1927,10 @@ function applyZcodeManagedConfig(doc: Record, opts: { hookEntry data.hooks = hooks; const mcp = zcodeAsPlain(data.mcp) ?? {}; const servers = zcodeAsPlain(mcp.servers) ?? {}; - if (servers.bili !== undefined && !isOursZcodeMcpServer(servers.bili)) { + if (servers.bili !== undefined && !isOurZcodeMcpOwnership(servers.bili)) { throw new Error(`${zcodeUserConfigFile()} already defines mcp.servers.bili owned by something else — rename or remove it first; refusing to overwrite`); } + if (isLegacyOursZcodeMcpServer(servers.bili)) notes.push("replaced the legacy bili mcp shim with this install's zcode/mcp-entry.js"); servers.bili = { type: "stdio", command: "node", args: [opts.mcpEntry] }; mcp.servers = servers; data.mcp = mcp; @@ -1934,7 +1957,7 @@ function stripZcodeManagedConfig(doc: Record): { data: Record 0) status += ` (routing ${routed.kind} store: ${routed.wrapped.map((w) => w.id).join(", ")})`; @@ -2322,9 +2348,10 @@ export function inspectLanePresence(agent: PluginAgent): LanePresence { const hasHook = Array.isArray(sessionStart) && (sessionStart as unknown[]).some(isOursZcodeHookEntry); const mcpBili = zcodeAsPlain(zcodeAsPlain(doc.mcp)?.servers)?.bili; const hasMcp = isOursZcodeMcpServer(mcpBili); - if (!hasHook && !hasMcp) return laneAbsent(); + const legacyMcp = !hasMcp && isLegacyOursZcodeMcpServer(mcpBili); + if (!hasHook && !hasMcp && !legacyMcp) return laneAbsent(); const out: LanePresence = { installed: hasHook && hasMcp, pointers: [], targets: [], form: "managed-block" }; - if (!out.installed) out.pointers.push(hasHook ? "hook present, MCP face missing" : "MCP face present, hook missing"); + if (!out.installed) out.pointers.push(hasHook ? (legacyMcp ? "hook present, legacy bili mcp shim — rerun 'bili plugin install zcode'" : "hook present, MCP face missing") : "MCP face present, hook missing"); const collectArgs = (args: unknown): void => { for (const a of Array.isArray(args) ? (args as unknown[]).map(String) : []) { if (path.isAbsolute(a) && /\.(js|ts)$/.test(a)) out.targets.push(a); diff --git a/tests/zcode-legacy-mcp-face.test.ts b/tests/zcode-legacy-mcp-face.test.ts new file mode 100644 index 0000000000..2fa9f901cc --- /dev/null +++ b/tests/zcode-legacy-mcp-face.test.ts @@ -0,0 +1,82 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { rmrf } from "./tmp-rm.ts"; + +// zcodeUserConfigFile() resolves os.homedir() on every call; isolate it BEFORE +// this module graph loads so a developer's real ~/.zcode/cli/config.json is +// never read or written by the suite. +const home = mkdtempSync(path.join(tmpdir(), "bc-zcode-legacy-")); +const savedEnv = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE }; +process.env.HOME = home; +process.env.USERPROFILE = home; +process.on("exit", () => { + rmrf(home); + for (const [k, v] of Object.entries(savedEnv)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } +}); + +const { pluginInstall, pluginRemove, pluginStatusAll } = await import("../src/plugin-install.ts"); + +const configFile = path.join(home, ".zcode", "cli", "config.json"); +const HOOK = "/opt/bc/dist/zcode/bootstrap-hook.js"; + +function seed(doc: unknown): void { + mkdirSync(path.dirname(configFile), { recursive: true }); + writeFileSync(configFile, JSON.stringify(doc, null, 2)); +} +function read(): Record { + return JSON.parse(readFileSync(configFile, "utf8")) as Record; +} +function managedDoc(mcp?: unknown): Record { + const doc: Record = { + hooks: { enabled: true, events: { SessionStart: [{ hooks: [{ type: "process", command: "node", args: [HOOK] }] }] } }, + }; + if (mcp !== undefined) doc.mcp = { servers: { bili: mcp } }; + return doc; +} + +// #1145 follow-up: pre-#1145 installs wrote a `bili mcp` shim. It is ours, so +// an upgrade must replace it in place — not classify it as foreign. +test("a legacy managed bili mcp shim is upgraded in place instead of blocking install", () => { + seed(managedDoc({ + type: "stdio", + command: "C:\\Users\\me\\.local\\bin\\bili.exe", + args: ["mcp"], + env: { BILLION_CONTEXT_PROXY: "http://127.0.0.1:8787" }, + })); + const msg = pluginInstall("zcode"); + assert.match(msg, /replaced the legacy bili mcp shim/); + const server = read().mcp.servers.bili; + assert.equal(server.type, "stdio"); + assert.equal(server.command, "node"); + assert.match(String(server.args[0]), /zcode[\\/]mcp-entry\.js$/); + assert.equal(server.env, undefined); +}); + +test("the legacy shim is reported as a stale face, not as a missing one", () => { + seed(managedDoc({ type: "stdio", command: "/usr/local/bin/bili", args: ["mcp"] })); + const row = pluginStatusAll().find((r) => r.agent === "zcode"); + assert.ok(row); + assert.match(row.status, /legacy bili mcp shim/); +}); + +test("plugin remove strips a legacy shim this installer wrote", () => { + seed({ mcp: { servers: { bili: { type: "stdio", command: "bili.exe", args: ["mcp"] } } } }); + pluginRemove("zcode"); + assert.equal(read().mcp?.servers?.bili, undefined); +}); + +test("a user-authored bili server still refuses to be overwritten", () => { + seed(managedDoc({ type: "stdio", command: "python", args: ["-m", "my_bili_mcp"] })); + assert.throws(() => pluginInstall("zcode"), /owned by something else/); +}); + +test("lookalike commands with extra args are not claimed as ours", () => { + seed(managedDoc({ type: "stdio", command: "bili", args: ["mcp", "--serve"] })); + assert.throws(() => pluginInstall("zcode"), /owned by something else/); +});