Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/plugin-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -1905,9 +1927,10 @@ function applyZcodeManagedConfig(doc: Record<string, unknown>, 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;
Expand All @@ -1934,7 +1957,7 @@ function stripZcodeManagedConfig(doc: Record<string, unknown>): { data: Record<s
const mcp = zcodeAsPlain(data.mcp);
if (mcp) {
const servers = zcodeAsPlain(mcp.servers);
if (servers && servers.bili !== undefined && isOursZcodeMcpServer(servers.bili)) {
if (servers && servers.bili !== undefined && isOurZcodeMcpOwnership(servers.bili)) {
removed.push("mcp.servers.bili");
delete servers.bili;
if (Object.keys(servers).length === 0) delete mcp.servers;
Expand Down Expand Up @@ -1987,13 +2010,16 @@ function zcodeRemove(): string {
function zcodeStatus(): string {
let hasHook = false;
let hasMcp = false;
let hasLegacyMcp = false;
try {
const doc = readJson(zcodeUserConfigFile());
const sessionStart = zcodeAsPlain(zcodeAsPlain(doc.hooks)?.events)?.SessionStart;
hasHook = Array.isArray(sessionStart) && (sessionStart as unknown[]).some(isOursZcodeHookEntry);
hasMcp = isOursZcodeMcpServer(zcodeAsPlain(zcodeAsPlain(doc.mcp)?.servers)?.bili);
const mcpServer = zcodeAsPlain(zcodeAsPlain(doc.mcp)?.servers)?.bili;
hasMcp = isOursZcodeMcpServer(mcpServer);
hasLegacyMcp = !hasMcp && isLegacyOursZcodeMcpServer(mcpServer);
} catch {}
let status = hasHook && hasMcp ? "installed" : hasHook || hasMcp ? "partially installed — rerun 'bili plugin install zcode' to fix" : "not installed";
let status = hasHook && hasMcp ? "installed" : hasLegacyMcp ? "partially installed — legacy bili mcp shim, rerun 'bili plugin install zcode'" : hasHook || hasMcp ? "partially installed — rerun 'bili plugin install zcode' to fix" : "not installed";
try {
const routed = inspectZcodeRouting(resolveZcodeDataDir(process.env));
if (routed && routed.wrapped.length > 0) status += ` (routing ${routed.kind} store: ${routed.wrapped.map((w) => w.id).join(", ")})`;
Expand Down Expand Up @@ -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);
Expand Down
82 changes: 82 additions & 0 deletions tests/zcode-legacy-mcp-face.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> {
return JSON.parse(readFileSync(configFile, "utf8")) as Record<string, any>;
}
function managedDoc(mcp?: unknown): Record<string, unknown> {
const doc: Record<string, any> = {
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/);
});
Loading