Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/plugin-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ function claudeRemove(): string {

function claudeStatus(): string {
const data = readJson(claudeMcpJson()) as { mcpServers?: Record<string, unknown> };
return data.mcpServers && "bili" in data.mcpServers ? "installed" : "not installed";
const mcpServers = data.mcpServers;
return isPlainMcpObject(mcpServers) && "bili" in mcpServers ? "installed" : "not installed";
}

// — codex ——————————————————————————————————————————————————————————————
Expand Down Expand Up @@ -434,11 +435,16 @@ function opencodeJson(): string {
return path.join(os.homedir(), ".config", "opencode", "opencode.json");
}

function isPlainMcpObject(v: unknown): v is Record<string, unknown> {
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<string, unknown> | 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 };
Expand All @@ -450,17 +456,17 @@ function opencodeInstall(): string {
function opencodeRemove(): string {
const file = opencodeJson();
const data = readJson(file);
const mcp = data.mcp as Record<string, unknown> | 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);
return `opencode: removed from ${file}`;
}

function opencodeStatus(): string {
const mcp = readJson(opencodeJson()).mcp as Record<string, unknown> | undefined;
return mcp && "bili" in mcp ? "installed" : "not installed";
const mcp = readJson(opencodeJson()).mcp;
return isPlainMcpObject(mcp) && "bili" in mcp ? "installed" : "not installed";
}

// — dispatch ————————————————————————————————————————————————————————————
Expand Down
46 changes: 46 additions & 0 deletions tests/plugin-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading