From 0c7f570a0037d39c7f0224d793f53bd551682e45 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 16 Sep 2026 14:28:41 +0800 Subject: [PATCH 1/2] fix(plugin-install): guard opencode non-object mcp in install/remove/status (#836) opencode.json may carry a non-object `mcp` (e.g. a bare string); `"bili" in ` throws TypeError. remove had no guard and crashed, stranding a half-install (broken reversibility); status surfaced "error:". Guard all three sites with isPlainMcpObject: install skips a present non-object mcp, while remove/status degrade to "not installed" so remove always completes cleanly. Found hands-on testing #824 (reported in #809 as finding N4). An equivalent guard also ships inside #824; this PR lands it independently on master, which was still exposed today. Regression test added in tests/plugin-agent.test.ts. Full suite green: 1650 tests / 1648 pass / 0 fail / 2 pre-existing skip. typecheck + build clean. --- src/plugin-install.ts | 13 +++++++++---- tests/plugin-agent.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 6dca5c2d87..730cc4aebf 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -434,11 +434,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 +455,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 +464,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..2576c354a5 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -1000,6 +1000,32 @@ 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 }); +}); + 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"); From 4f9b40c6afc3ea1052b8f63330bc542daae4ec0f Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 16 Sep 2026 14:41:20 +0800 Subject: [PATCH 2/2] fix(plugin-install): guard non-object mcpServers in claude status/remove (#839) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found while reviewing #837: claudeStatus() applied an unguarded 'in' operator to mcpServers from .claude.json — a non-object value crashed pluginRemove('claude') (which calls status first) and surfaced 'error:' rows from pluginStatusAll(), the same broken-reversibility shape as #836 but on the claude side. Reuse the isPlainMcpObject helper introduced by this PR; add a regression test mirroring the opencode one. --- src/plugin-install.ts | 3 ++- tests/plugin-agent.test.ts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 730cc4aebf..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 —————————————————————————————————————————————————————————————— diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index 2576c354a5..8c69d212dd 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -1026,6 +1026,26 @@ test("plugin opencode survives a non-object mcp (issue #836 / #809 N4)", async ( 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");