From d24c0eab47dbf4c3a53edc9ea1502e0eebdfb82c Mon Sep 17 00:00:00 2001 From: Rock Neurotiko Date: Fri, 20 Mar 2026 01:42:56 +0100 Subject: [PATCH] fix: normalize permission.edit string on opencode plugin (#345) --- apps/opencode-plugin/index.ts | 3 ++- apps/opencode-plugin/plan-mode.test.ts | 36 ++++++++++++++++++++++++++ apps/opencode-plugin/plan-mode.ts | 23 ++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/apps/opencode-plugin/index.ts b/apps/opencode-plugin/index.ts index 1b089c4..7471d10 100644 --- a/apps/opencode-plugin/index.ts +++ b/apps/opencode-plugin/index.ts @@ -41,6 +41,7 @@ import { } from "./commands"; import { planDenyFeedback } from "@plannotator/shared/feedback-templates"; import { + normalizeEditPermission, stripConflictingPlanModeRules, } from "./plan-mode"; @@ -188,7 +189,7 @@ export const PlannotatorPlugin: Plugin = async (ctx) => { opencodeConfig.agent.plan ??= {}; opencodeConfig.agent.plan.permission ??= {}; opencodeConfig.agent.plan.permission.edit = { - ...opencodeConfig.agent.plan.permission.edit, + ...normalizeEditPermission(opencodeConfig.agent.plan.permission.edit), "*.md": "allow", }; }, diff --git a/apps/opencode-plugin/plan-mode.test.ts b/apps/opencode-plugin/plan-mode.test.ts index ec5f896..38af8c4 100644 --- a/apps/opencode-plugin/plan-mode.test.ts +++ b/apps/opencode-plugin/plan-mode.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "os"; import path from "path"; import { getPlanDirectory, + normalizeEditPermission, validatePlanPath, stripConflictingPlanModeRules, } from "./plan-mode"; @@ -144,6 +145,41 @@ describe("validatePlanPath", () => { }); }); +describe("normalizeEditPermission", () => { + test("returns empty object for undefined", () => { + expect(normalizeEditPermission(undefined)).toEqual({}); + }); + + test("converts 'deny' string to wildcard object", () => { + // Triggered by `tools: { edit: false }` or `permission: { edit: "deny" }` + expect(normalizeEditPermission("deny")).toEqual({ "*": "deny" }); + }); + + test("converts 'allow' string to wildcard object", () => { + expect(normalizeEditPermission("allow")).toEqual({ "*": "allow" }); + }); + + test("converts 'ask' string to wildcard object", () => { + expect(normalizeEditPermission("ask")).toEqual({ "*": "ask" }); + }); + + test("passes through an existing object unchanged", () => { + const obj = { "*.ts": "deny", "src/**": "allow" }; + expect(normalizeEditPermission(obj)).toEqual(obj); + }); + + test("merging with '*.md': 'allow' preserves deny-all + md-allow", () => { + // This is the main scenario fixed by this function: + // user has tools: { edit: false } which produces permission.edit = "deny", + // and we need to merge in "*.md": "allow" without string-spreading. + const base = normalizeEditPermission("deny"); + const merged = { ...base, "*.md": "allow" }; + expect(merged).toEqual({ "*": "deny", "*.md": "allow" }); + // Crucially, no char-index keys like "0", "1", "2", "3" + expect(Object.keys(merged)).not.toContain("0"); + }); +}); + describe("stripConflictingPlanModeRules", () => { test("removes OpenCode's blanket file-edit prohibition", () => { expect( diff --git a/apps/opencode-plugin/plan-mode.ts b/apps/opencode-plugin/plan-mode.ts index d8a3232..c94228c 100644 --- a/apps/opencode-plugin/plan-mode.ts +++ b/apps/opencode-plugin/plan-mode.ts @@ -84,6 +84,29 @@ export function validatePlanPath( return { ok: true, content }; } +// ── Permission helpers ──────────────────────────────────────────────────── + +/** + * Normalize an `edit` permission value before merging additional rules. + * + * OpenCode's zod transform converts legacy `tools: { edit: false }` to + * `permission.edit = "deny"` (a plain string) before any plugin sees the + * config. Spreading a string in JS produces char-index keys: + * `{ ..."deny" }` → `{ "0": "d", "1": "e", "2": "n", "3": "y" }` + * which corrupt the permission ruleset and cause zod validation failures. + * + * This function converts a string action to `{ "*": action }` (equivalent + * wildcard object) so the caller can safely spread it and add overrides. + */ +export function normalizeEditPermission( + edit: string | Record | undefined, +): Record { + if (typeof edit === "string") { + return { "*": edit }; + } + return edit ?? {}; +} + // ── Prompt stripping ────────────────────────────────────────────────────── function shouldStripPlanModeLine(line: string): boolean { -- knotmirror 0.0.1