From c24e76b7bb262265579d129e2966f377be8b32a9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sat, 18 Jul 2026 10:10:06 -0700 Subject: [PATCH 1/3] fix(cli): stop read-only agent bash denies from blocking delegated subagents When a read-only or delegating agent (plan, ask, orchestrator) delegated to a custom subagent via the task tool, the subagent was blocked from running commands its own config explicitly allowed (e.g. `git status`) with errors citing deny rules the user never wrote (`git *`, `*`, shell-operator guards). Those phantom rules are the calling agent's `readOnlyBash` allowlist. Its deny rules exist only to shape that allowlist; `KiloTask.inherited` was extracting them (deny-only) and appending them as subagent ceilings, where last-match-wins resolution made `git * deny` / `* deny` override the subagent's own allows. Drop `bash` from the inherited-ceiling set so the calling agent's own bash policy no longer caps a writable subagent. Edit, notebook, and MCP denials remain hard ceilings, and an explicit session-scoped bash lockdown still reaches the child via `deriveSubagentSessionPermission`, which inherits session denies. Fixes #11523 --- .../fix-subagent-phantom-bash-denies.md | 6 +++ packages/opencode/src/kilocode/tool/task.ts | 18 +++++-- .../agent/plan-mode-subagent-bypass.test.ts | 49 +++++++++++++++++-- .../test/kilocode/task-nesting.test.ts | 4 +- 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 .changeset/fix-subagent-phantom-bash-denies.md diff --git a/.changeset/fix-subagent-phantom-bash-denies.md b/.changeset/fix-subagent-phantom-bash-denies.md new file mode 100644 index 000000000000..d8d356ed35f5 --- /dev/null +++ b/.changeset/fix-subagent-phantom-bash-denies.md @@ -0,0 +1,6 @@ +--- +"@kilocode/cli": patch +"kilo-code": patch +--- + +Fix subagent permission errors that referenced phantom deny rules and blocked commands the subagent's own config explicitly allowed. A read-only or delegating agent's `readOnlyBash` allowlist is no longer projected onto a writable subagent as a bash ceiling, so a delegated subagent can run its own allowed commands (e.g. `git status`). Edit, notebook, and MCP denials are still inherited as hard ceilings. diff --git a/packages/opencode/src/kilocode/tool/task.ts b/packages/opencode/src/kilocode/tool/task.ts index c73774e4311b..a52fdb843bd9 100644 --- a/packages/opencode/src/kilocode/tool/task.ts +++ b/packages/opencode/src/kilocode/tool/task.ts @@ -49,9 +49,18 @@ export namespace KiloTask { * overriding the selected subagent's own allowlist with parent ask/allow rules. * * OpenCode removed parent-agent inheritance entirely in anomalyco/opencode#31696. - * Kilo intentionally differs: parent denials remain hard ceilings for Plan Mode - * and MCP restrictions, while parent ask/allow rules must not replace the - * selected subagent's policy. Preserve this distinction during upstream merges. + * Kilo intentionally differs: parent edit/notebook/MCP denials remain hard ceilings + * for Plan Mode and MCP restrictions, while parent ask/allow rules must not replace + * the selected subagent's policy. Preserve this distinction during upstream merges. + * + * Bash is deliberately NOT inherited from the calling agent. A read-only/delegating + * agent (plan, ask, orchestrator) carries a `readOnlyBash` allowlist whose deny rules + * (`*`, `git *`, shell-operator guards) exist only to shape that allowlist. Projecting + * those denies onto a writable subagent capped commands the subagent's own config + * explicitly allows (e.g. `git status`), surfacing phantom deny rules the user never + * wrote (#11523). The subagent's own bash policy governs its bash capabilities; an + * explicit session-scoped bash lockdown (sandbox / session deny) still reaches the + * child via `deriveSubagentSessionPermission`, which inherits session deny rules. * * The caller must resolve `caller` (Agent.Info) and `session` (Session.Info) * before calling. This function is pure/synchronous. @@ -64,7 +73,8 @@ export namespace KiloTask { const rules = Permission.merge(input.caller.permission ?? [], input.session.permission ?? []) const prefixes = Object.keys(input.mcp ?? {}).map((k) => k.replace(/[^a-zA-Z0-9_-]/g, "_") + "_") const isMcp = (p: string) => prefixes.some((prefix) => p.startsWith(prefix)) - const mutation = new Set(["edit", "bash", "notebook_edit", "notebook_execute"]) + // `bash` is intentionally excluded — see the doc comment above (#11523). + const mutation = new Set(["edit", "notebook_edit", "notebook_execute"]) const inherited = rules.filter( (r: Permission.Rule) => r.action === "deny" && (mutation.has(r.permission) || isMcp(r.permission)), ) diff --git a/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts b/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts index a88964ecede9..c4a880fbaeae 100644 --- a/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts +++ b/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts @@ -159,8 +159,9 @@ it.effect("subagent inherits parent session deny rules as hard runtime ceilings" }), ) -// kilocode_change start - preserve Plan mutation ceilings across Kilo task delegation -it.instance("Plan delegation preserves notebook and process mutation ceilings", () => +// kilocode_change start - preserve Plan edit/notebook ceilings across Kilo task delegation, +// but do NOT project the caller's read-only bash allowlist onto a writable subagent (#11523) +it.instance("Plan delegation preserves notebook mutation ceilings", () => Effect.gen(function* () { const caller = yield* Agent.use.get("plan") expect(caller).toBeDefined() @@ -172,7 +173,49 @@ it.instance("Plan delegation preserves notebook and process mutation ceilings", expect(Permission.evaluate("notebook_edit", "notebook.ipynb", rules).action).toBe("deny") expect(Permission.evaluate("notebook_execute", "notebook.ipynb", rules).action).toBe("deny") - expect(Permission.evaluate("bash", "bun run server.ts", rules).action).toBe("deny") + // The caller's own readOnlyBash denies are no longer inherited as subagent ceilings; + // the subagent's own bash policy governs its bash capabilities (#11523). + expect(rules.some((rule) => rule.permission === "bash" && rule.action === "deny")).toBe(false) }), ) + +it.instance( + "read-only caller does not cap a writable subagent's own bash allowlist", + () => + Effect.gen(function* () { + const caller = yield* Agent.use.get("plan") + const worker = yield* Agent.use.get("git_worker") + expect(caller).toBeDefined() + expect(worker).toBeDefined() + + const rules = KiloTask.inherited({ + caller: caller!, + session: { permission: [] } as unknown as Parameters[0]["session"], + mcp: {}, + }) + // The phantom deny rules the issue reports must not leak from the read-only caller. + expect(rules).not.toContainEqual({ permission: "bash", pattern: "git *", action: "deny" }) + expect(rules).not.toContainEqual({ permission: "bash", pattern: "*", action: "deny" }) + + // Mirror task.ts: the subagent runs with its own permission plus the inherited ceilings. + const effective = Permission.merge(worker!.permission, KiloTask.permissions(rules)) + expect(Permission.evaluate("bash", "git status", effective).action).toBe("allow") + }), + { + config: { + agent: { + git_worker: { + description: "A writable subagent that runs git", + mode: "subagent", + permission: { + bash: { + "*": "ask", + "git *": "allow", + }, + }, + }, + }, + }, + }, +) // kilocode_change end diff --git a/packages/opencode/test/kilocode/task-nesting.test.ts b/packages/opencode/test/kilocode/task-nesting.test.ts index 44708219c845..3e8e30946000 100644 --- a/packages/opencode/test/kilocode/task-nesting.test.ts +++ b/packages/opencode/test/kilocode/task-nesting.test.ts @@ -354,7 +354,9 @@ describe("Kilo task nesting", () => { KiloSessionPrompt.guardPermissions({ agent: validator, session: child }), ) expect(child.permission).not.toContainEqual({ permission: "bash", pattern: "*", action: "ask" }) - expect(child.permission).toContainEqual({ permission: "bash", pattern: "rm -rf *", action: "deny" }) + // The calling agent's own bash policy is no longer projected onto the subagent as a + // ceiling (#11523); the subagent's own `*: deny` policy is what keeps `rm -rf` denied. + expect(child.permission).not.toContainEqual({ permission: "bash", pattern: "rm -rf *", action: "deny" }) expect({ allowed: Permission.evaluate("bash", "ansible-lint --version", effective).action, denied: Permission.evaluate("bash", "rm -rf build", effective).action, From 72fe8e69140d20944cbe579a562394030fe2f0c9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Tue, 11 Aug 2026 21:48:04 -0700 Subject: [PATCH 2/3] fix(cli): keep plan's destructive-command denies as subagent ceilings Removing bash from the inherited ceilings globally made the default Plan->Explore path writable: Plan can delegate to Explore without asking, and the built-in Explore agent declares bash: "allow", so rm -rf and git push both resolved to allow on the merged tree. Re-inherit plan's concrete destructive-command denies (rm -rf *, git push*) as ceilings while leaving the broad allowlist-shaping denies uninherited, so a writable subagent keeps its own bash policy and the read-only boundary holds. --- packages/opencode/src/kilocode/tool/task.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/kilocode/tool/task.ts b/packages/opencode/src/kilocode/tool/task.ts index a52fdb843bd9..e48e385f5899 100644 --- a/packages/opencode/src/kilocode/tool/task.ts +++ b/packages/opencode/src/kilocode/tool/task.ts @@ -53,14 +53,16 @@ export namespace KiloTask { * for Plan Mode and MCP restrictions, while parent ask/allow rules must not replace * the selected subagent's policy. Preserve this distinction during upstream merges. * - * Bash is deliberately NOT inherited from the calling agent. A read-only/delegating + * Broad bash denies are deliberately NOT inherited from the calling agent. A read-only/delegating * agent (plan, ask, orchestrator) carries a `readOnlyBash` allowlist whose deny rules * (`*`, `git *`, shell-operator guards) exist only to shape that allowlist. Projecting * those denies onto a writable subagent capped commands the subagent's own config * explicitly allows (e.g. `git status`), surfacing phantom deny rules the user never * wrote (#11523). The subagent's own bash policy governs its bash capabilities; an * explicit session-scoped bash lockdown (sandbox / session deny) still reaches the - * child via `deriveSubagentSessionPermission`, which inherits session deny rules. + * child via `deriveSubagentSessionPermission`, which inherits session deny rules. Plan's + * concrete destructive-command denies remain ceilings so its built-in Explore delegation + * cannot bypass the read-only boundary. * * The caller must resolve `caller` (Agent.Info) and `session` (Session.Info) * before calling. This function is pure/synchronous. @@ -82,6 +84,12 @@ export namespace KiloTask { if (Permission.evaluate(permission, "*", rules).action !== "deny") continue inherited.push({ permission, pattern: "*", action: "deny" }) } + if (input.caller.name === "plan") { + for (const pattern of ["rm -rf *", "git push*"]) { + if (Permission.evaluate("bash", pattern, rules).action !== "deny") continue + inherited.push({ permission: "bash", pattern, action: "deny" }) + } + } return merge(inherited) } From e36dce10474986d1f605e92158a6c4249645eeb5 Mon Sep 17 00:00:00 2001 From: mvanhorn Date: Thu, 13 Aug 2026 09:39:17 -0700 Subject: [PATCH 3/3] fix: implemented the review fixes Implemented the review fixes. - Built-in Explore now has an enforcement-level read-only bash policy, even if user config sets `bash: allow`. - Plan and orchestrator delegation deny mutations while allowing `git status` and `rg`. - `gh` and raw `find` are denied for delegated Explore. - Custom writable subagents retain their own bash allowlists. - Removed the incomplete Plan-only `rm -rf`/`git push` ceilings. - Added focused regression coverage. ```text --- packages/opencode/src/agent/agent.ts | 1 + packages/opencode/src/kilocode/agent/index.ts | 26 +++++++- packages/opencode/src/kilocode/tool/task.ts | 12 +--- .../agent/plan-mode-subagent-bypass.test.ts | 65 +++++++++++++++++-- 4 files changed, 90 insertions(+), 14 deletions(-) diff --git a/packages/opencode/src/agent/agent.ts b/packages/opencode/src/agent/agent.ts index a80f81cd8b4a..4ba811299b0a 100644 --- a/packages/opencode/src/agent/agent.ts +++ b/packages/opencode/src/agent/agent.ts @@ -383,6 +383,7 @@ const layer = Layer.effect( // kilocode_change start KiloAgent.processConfigItem(item) KiloAgent.hardenPlan(key, item, ctx.worktree, user, Permission.fromConfig(value.permission ?? {})) + KiloAgent.hardenExplore(key, item, user, Permission.fromConfig(value.permission ?? {})) } function referencePrompt(reference: KiloReference.Resolved) { diff --git a/packages/opencode/src/kilocode/agent/index.ts b/packages/opencode/src/kilocode/agent/index.ts index afaec2ca4cfe..3b56ecc44791 100644 --- a/packages/opencode/src/kilocode/agent/index.ts +++ b/packages/opencode/src/kilocode/agent/index.ts @@ -143,6 +143,14 @@ export const readOnlyBash: Record = { "man *-H*": "deny", } +const exploreBash: Record = { + ...readOnlyBash, + // Explore runs as a delegated agent, so it cannot answer permission prompts. + "gh *": "deny", + // `find` can mutate through `-delete` and `-exec`; use glob/list instead. + "find *": "deny", +} + function askGuard(mcp: Record = {}) { return Permission.fromConfig({ "*": "deny", @@ -224,6 +232,20 @@ export function hardenPlan( item.permission = Permission.merge(item.permission, planEditGuard(worktree), ...edit) } +export function hardenExplore( + key: string, + item: { permission: Permission.Ruleset }, + ...explicit: Permission.Ruleset[] +) { + if (key !== "explore") return + item.permission = Permission.merge( + item.permission, + Permission.fromConfig({ bash: exploreBash }), + // Hardening is a ceiling, so retain any stricter user-authored denies. + ...explicit.map(denies), + ) +} + function planGuard(worktree: string, mcp: Record = {}) { return Permission.fromConfig({ "*": "deny", @@ -447,7 +469,6 @@ export function patchAgents( grep: "allow", glob: "allow", list: "allow", - bash: "allow", skill: "allow", webfetch: "allow", websearch: "allow", @@ -465,6 +486,9 @@ export function patchAgents( }, }), user, + // Explore is always delegated, so user allows cannot make its shell writable. + Permission.fromConfig({ bash: exploreBash }), + denies(user), ), prompt: cfg.experimental?.codebase_search ? `Prefer using the codebase_search tool for codebase searches — it performs intelligent multi-step code search and returns the most relevant code spans.\n\n${PROMPT_EXPLORE}` diff --git a/packages/opencode/src/kilocode/tool/task.ts b/packages/opencode/src/kilocode/tool/task.ts index 537a10552c0a..f7b351461800 100644 --- a/packages/opencode/src/kilocode/tool/task.ts +++ b/packages/opencode/src/kilocode/tool/task.ts @@ -55,9 +55,9 @@ export namespace KiloTask { * explicitly allows (e.g. `git status`), surfacing phantom deny rules the user never * wrote (#11523). The subagent's own bash policy governs its bash capabilities; an * explicit session-scoped bash lockdown (sandbox / session deny) still reaches the - * child via `deriveSubagentSessionPermission`, which inherits session deny rules. Plan's - * concrete destructive-command denies remain ceilings so its built-in Explore delegation - * cannot bypass the read-only boundary. + * child via `deriveSubagentSessionPermission`, which inherits session deny rules. Built-in + * Explore has its own enforcement-level read-only bash policy, so every caller retains that + * boundary without projecting a delegator's bash rules onto custom writable subagents. * * The caller must resolve `caller` (Agent.Info) and `session` (Session.Info) * before calling. This function is pure/synchronous. @@ -79,12 +79,6 @@ export namespace KiloTask { if (Permission.evaluate(permission, "*", rules).action !== "deny") continue inherited.push({ permission, pattern: "*", action: "deny" }) } - if (input.caller.name === "plan") { - for (const pattern of ["rm -rf *", "git push*"]) { - if (Permission.evaluate("bash", pattern, rules).action !== "deny") continue - inherited.push({ permission: "bash", pattern, action: "deny" }) - } - } return merge(inherited) } diff --git a/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts b/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts index a8905452b4c2..9d9a1b4eb50a 100644 --- a/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts +++ b/packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts @@ -162,7 +162,7 @@ it.effect("subagent inherits parent session deny rules as hard runtime ceilings" // kilocode_change start - preserve Plan edit/notebook ceilings across Kilo task delegation, // but do NOT project the caller's read-only bash allowlist onto a writable subagent (#11523) -it.instance("Plan delegation preserves notebook mutation ceilings", () => +it.instance("Plan delegation preserves notebook ceilings without projecting bash denies", () => Effect.gen(function* () { const caller = yield* Agent.use.get("plan") expect(caller).toBeDefined() @@ -174,12 +174,67 @@ it.instance("Plan delegation preserves notebook mutation ceilings", () => expect(Permission.evaluate("notebook_edit", "notebook.ipynb", rules).action).toBe("deny") expect(Permission.evaluate("notebook_execute", "notebook.ipynb", rules).action).toBe("deny") - // The caller's own readOnlyBash denies are no longer inherited as subagent ceilings; - // the subagent's own bash policy governs its bash capabilities (#11523). - expect(rules.some((rule) => rule.permission === "bash" && rule.action === "deny")).toBe(false) + expect(rules.filter((rule) => rule.permission === "bash")).toEqual([]) }), ) +it.instance( + "built-in Explore enforces read-only bash for Plan and orchestrator delegation", + () => + Effect.gen(function* () { + const plan = yield* Agent.use.get("plan") + const orchestrator = yield* Agent.use.get("orchestrator") + const explore = yield* Agent.use.get("explore") + expect(plan).toBeDefined() + expect(orchestrator).toBeDefined() + expect(explore).toBeDefined() + + const inherited = (caller: Agent.Info) => + KiloTask.inherited({ + caller, + session: { permission: [] } as unknown as Parameters[0]["session"], + mcp: {}, + }) + const effective = (caller: Agent.Info) => + Permission.merge(explore!.permission, KiloTask.permissions(inherited(caller))) + const rules = effective(plan!) + + expect(Permission.evaluate("bash", "git status", rules).action).toBe("allow") + expect(Permission.evaluate("bash", "rg TODO src", rules).action).toBe("allow") + + const denied = [ + "rm -rf src", + "git push origin main", + "git -c user.name=test push origin main", + "git commit -m test", + "touch output.txt", + "mv source target", + "cp source target", + "mkdir output", + "npm install", + ] + for (const command of denied) { + expect(Permission.evaluate("bash", command, rules).action).toBe("deny") + } + + // Delegated agents cannot answer an `ask`, and raw find can mutate via -exec/-delete. + expect(Permission.evaluate("bash", "gh repo view", rules).action).toBe("deny") + expect(Permission.evaluate("bash", "find . -name '*.ts'", rules).action).toBe("deny") + expect(Permission.evaluate("bash", "touch output.txt", effective(orchestrator!)).action).toBe("deny") + }), + { + config: { + agent: { + explore: { + permission: { + bash: "allow", + }, + }, + }, + }, + }, +) + it.instance( "read-only caller does not cap a writable subagent's own bash allowlist", () => @@ -201,6 +256,7 @@ it.instance( // Mirror task.ts: the subagent runs with its own permission plus the inherited ceilings. const effective = Permission.merge(worker!.permission, KiloTask.permissions(rules)) expect(Permission.evaluate("bash", "git status", effective).action).toBe("allow") + expect(Permission.evaluate("bash", "touch output.txt", effective).action).toBe("allow") }), { config: { @@ -212,6 +268,7 @@ it.instance( bash: { "*": "ask", "git *": "allow", + "touch *": "allow", }, }, },