Skip to content

Commit bb4319d

Browse files
committed
fix(task): subagent explicit edit:allow overrides parent edit:deny
When a parent agent (e.g. an orchestrator) has edit:deny and spawns a subagent (e.g. an editor) that has edit:allow, the parent's deny was unconditionally inherited into the subagent's session permission. Because permission evaluation is last-match-wins, the inherited deny overrode the subagent's own allow — removing the edit tool from the subagent's palette. Fix: only inherit parent edit:deny rules when the subagent does NOT explicitly declare edit:allow. If a subagent says it can edit, the parent's self-restriction should not override that declared capability. This preserves Plan Mode security: subagents without explicit edit declarations (like general, explore) still inherit the parent's edit:deny as before. Relates to anomalyco#26700 anomalyco#26747 anomalyco#26758 anomalyco#27123
1 parent 04c4611 commit bb4319d

2 files changed

Lines changed: 153 additions & 1 deletion

File tree

‎packages/opencode/src/agent/subagent-permissions.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import type { Agent } from "./agent"
99
* restriction lives on the agent ruleset, not on the session, so a
1010
* subagent that only inherited the parent SESSION's permission would
1111
* silently bypass it. (#26514)
12+
* Only inherited if the subagent does NOT explicitly allow edit — a
13+
* subagent with `edit: allow` should not have its capability reduced by
14+
* a more-restricted parent.
1215
* 2. The parent **session's** deny rules and external_directory rules —
1316
* same forwarding the original code already did.
1417
* 3. Default `todowrite` and `task` denies if the subagent's own ruleset
@@ -21,8 +24,23 @@ export function deriveSubagentSessionPermission(input: {
2124
}): Permission.Ruleset {
2225
const canTask = input.subagent.permission.some((rule) => rule.permission === "task")
2326
const canTodo = input.subagent.permission.some((rule) => rule.permission === "todowrite")
27+
28+
// Only inherit parent edit:deny if the subagent does NOT explicitly allow edit.
29+
// A subagent with `edit: allow` (or `edit: { "*": "allow" }`) declares its own
30+
// capability — the parent's deny should not override it.
31+
// A subagent without explicit edit declaration (implicit deny) inherits the
32+
// parent's deny as a ceiling.
33+
const subagentAllowsEdit = input.subagent.permission.some(
34+
(rule) => rule.permission === "edit" && rule.action === "allow" && rule.pattern === "*",
35+
)
36+
2437
const parentAgentDenies =
25-
input.parentAgent?.permission.filter((rule) => rule.action === "deny" && rule.permission === "edit") ?? []
38+
!subagentAllowsEdit
39+
? (input.parentAgent?.permission.filter(
40+
(rule) => rule.action === "deny" && rule.permission === "edit",
41+
) ?? [])
42+
: []
43+
2644
return [
2745
...parentAgentDenies,
2846
...input.parentSessionPermission.filter(

‎packages/opencode/test/agent/plan-mode-subagent-bypass.test.ts‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,137 @@ it.effect("subagent inherits parent session deny rules as hard runtime ceilings"
210210
expect(Permission.evaluate("bash", "git status", effective).action).toBe("deny")
211211
}),
212212
)
213+
214+
it.effect("subagent with explicit edit:allow overrides parent edit:deny", () =>
215+
Effect.sync(() => {
216+
const restrictedParent = testAgent({
217+
name: "restricted-parent",
218+
mode: "primary",
219+
permission: {
220+
edit: "deny",
221+
bash: "deny",
222+
read: "allow",
223+
task: {
224+
"*": "deny",
225+
capableChild: "allow",
226+
},
227+
},
228+
})
229+
const capableChild = testAgent({
230+
name: "capable-child",
231+
mode: "subagent",
232+
permission: {
233+
edit: "allow",
234+
write: "allow",
235+
bash: "allow",
236+
read: "allow",
237+
task: "deny",
238+
},
239+
})
240+
241+
const effective = Permission.merge(
242+
capableChild.permission,
243+
deriveSubagentSessionPermission({
244+
parentSessionPermission: [],
245+
parentAgent: restrictedParent,
246+
subagent: capableChild,
247+
}),
248+
)
249+
250+
// Child explicitly allows edit — parent deny should not override
251+
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("allow")
252+
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(new Set())
253+
}),
254+
)
255+
256+
it.effect("subagent without explicit edit permission inherits parent edit:deny", () =>
257+
Effect.sync(() => {
258+
const restrictedParent = testAgent({
259+
name: "restricted-parent",
260+
mode: "primary",
261+
permission: {
262+
edit: "deny",
263+
read: "allow",
264+
task: {
265+
"*": "deny",
266+
silentChild: "allow",
267+
},
268+
},
269+
})
270+
const silentChild = testAgent({
271+
name: "silent-child",
272+
mode: "subagent",
273+
permission: {
274+
read: "allow",
275+
bash: "allow",
276+
task: "deny",
277+
},
278+
})
279+
280+
const effective = Permission.merge(
281+
silentChild.permission,
282+
deriveSubagentSessionPermission({
283+
parentSessionPermission: [],
284+
parentAgent: restrictedParent,
285+
subagent: silentChild,
286+
}),
287+
)
288+
289+
// Child has no explicit edit declaration — parent deny should be inherited
290+
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("deny")
291+
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(
292+
new Set(["edit", "write", "apply_patch"]),
293+
)
294+
}),
295+
)
296+
297+
it.effect("[orchestrator-pattern] parent edit:deny does not override subagent explicit edit:allow", () =>
298+
Effect.sync(() => {
299+
const orchestrator = testAgent({
300+
name: "orchestrator",
301+
mode: "primary",
302+
permission: {
303+
edit: "deny",
304+
bash: "deny",
305+
read: "allow",
306+
glob: "allow",
307+
grep: "allow",
308+
task: {
309+
"*": "deny",
310+
editor: "allow",
311+
},
312+
todowrite: "allow",
313+
question: "allow",
314+
webfetch: "allow",
315+
},
316+
})
317+
const editor = testAgent({
318+
name: "editor",
319+
mode: "subagent",
320+
permission: {
321+
edit: "allow",
322+
write: "allow",
323+
bash: "allow",
324+
read: "allow",
325+
glob: "allow",
326+
grep: "allow",
327+
task: "deny",
328+
todowrite: "allow",
329+
},
330+
})
331+
332+
const effective = Permission.merge(
333+
editor.permission,
334+
deriveSubagentSessionPermission({
335+
parentSessionPermission: [],
336+
parentAgent: orchestrator,
337+
subagent: editor,
338+
}),
339+
)
340+
341+
// Editor should have edit available because it explicitly allows it
342+
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("allow")
343+
// edit/write/apply_patch tools should NOT be in disabled set
344+
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(new Set())
345+
}),
346+
)

0 commit comments

Comments
 (0)