diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 6dca5c2d87..8855fea617 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -360,7 +360,8 @@ function claudeRemove(): string { function claudeStatus(): string { const data = readJson(claudeMcpJson()) as { mcpServers?: Record }; - return data.mcpServers && "bili" in data.mcpServers ? "installed" : "not installed"; + const mcpServers = data.mcpServers; + return isPlainMcpObject(mcpServers) && "bili" in mcpServers ? "installed" : "not installed"; } // — codex —————————————————————————————————————————————————————————————— @@ -434,11 +435,16 @@ function opencodeJson(): string { return path.join(os.homedir(), ".config", "opencode", "opencode.json"); } +function isPlainMcpObject(v: unknown): v is Record { + return v !== null && typeof v === "object" && !Array.isArray(v); +} + function opencodeInstall(): string { const file = opencodeJson(); const mcpJs = path.join(selfPackageRoot(), "dist", "mcp.js"); requireDistFile(mcpJs); const data = readJson(file); + if (data.mcp != null && !isPlainMcpObject(data.mcp)) return `opencode: mcp.bili skipped ("mcp" is not an object) -> ${file}`; const mcp = (data.mcp as Record | undefined) ?? {}; if ("bili" in mcp) return `opencode: already installed (${file})`; mcp.bili = { type: "local", command: [process.execPath, mcpJs], environment: { BILI_MCP_PROXY: proxyOriginForInstall() }, enabled: true }; @@ -450,8 +456,8 @@ function opencodeInstall(): string { function opencodeRemove(): string { const file = opencodeJson(); const data = readJson(file); - const mcp = data.mcp as Record | undefined; - if (!mcp || !("bili" in mcp)) return `opencode: not installed (${file})`; + const mcp = data.mcp; + if (!isPlainMcpObject(mcp) || !("bili" in mcp)) return `opencode: not installed (${file})`; delete mcp.bili; if (Object.keys(mcp).length === 0) delete data.mcp; writeJson(file, data); @@ -459,8 +465,8 @@ function opencodeRemove(): string { } function opencodeStatus(): string { - const mcp = readJson(opencodeJson()).mcp as Record | undefined; - return mcp && "bili" in mcp ? "installed" : "not installed"; + const mcp = readJson(opencodeJson()).mcp; + return isPlainMcpObject(mcp) && "bili" in mcp ? "installed" : "not installed"; } // — dispatch ———————————————————————————————————————————————————————————— diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index 77c6a5ed36..8c69d212dd 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -1000,6 +1000,52 @@ test("plugin install refuses to touch broken or non-object configs", async () => fs.rmSync(home, { recursive: true, force: true }); }); +// #836 (found in #809 N4): a non-object `mcp` (e.g. bare string) made +// `"bili" in mcp` throw — crashing remove (stranding a half-install) and +// surfacing "error:" from status. All three opencode sites must degrade +// gracefully instead of throwing. +test("plugin opencode survives a non-object mcp (issue #836 / #809 N4)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-plugin-home-")); + const piAgentDir = path.join(home, ".pi/agent"); + await withEnv(hintEnv(home, piAgentDir), async () => { + const ocDir = path.join(home, ".config/opencode"); + fs.mkdirSync(ocDir, { recursive: true }); + const ocFile = path.join(ocDir, "opencode.json"); + const malformed = JSON.stringify({ mcp: "bogus-string", other: 1 }); + fs.writeFileSync(ocFile, malformed); + + assert.doesNotThrow(() => pluginRemove("opencode")); + assert.match(pluginRemove("opencode"), /not installed/); + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")!.status, "not installed"); + assert.equal(fs.readFileSync(ocFile, "utf8"), malformed); + + assert.doesNotThrow(() => pluginInstall("opencode")); + assert.match(pluginInstall("opencode"), /skipped/i); + assert.equal(fs.readFileSync(ocFile, "utf8"), malformed); + }); + fs.rmSync(home, { recursive: true, force: true }); +}); + +// #839 (found while reviewing #837): same bug class as #836 on the claude side +// — a non-object `mcpServers` in .claude.json made `"bili" in mcpServers` throw +// in claudeStatus(), crashing remove (which calls status first) and surfacing +// "error:" from status. +test("plugin claude survives a non-object mcpServers (issue #839)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-plugin-home-")); + const piAgentDir = path.join(home, ".pi/agent"); + await withEnv(hintEnv(home, piAgentDir), async () => { + const cFile = path.join(home, ".claude.json"); + const malformed = JSON.stringify({ mcpServers: "bogus-string", other: 1 }); + fs.writeFileSync(cFile, malformed); + + assert.doesNotThrow(() => pluginRemove("claude")); + assert.match(pluginRemove("claude"), /not installed/); + assert.equal(pluginStatusAll().find((r) => r.agent === "claude")!.status, "not installed"); + assert.equal(fs.readFileSync(cFile, "utf8"), malformed); + }); + fs.rmSync(home, { recursive: true, force: true }); +}); + test("plugin list survives a broken host config (per-row error, no crash)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-plugin-home-")); const piAgentDir = path.join(home, ".pi/agent");