Skip to content

Commit 12a5349

Browse files
OrShmuel22Test
authored andcommitted
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 b1b8be6 commit 12a5349

3 files changed

Lines changed: 233 additions & 18 deletions

File tree

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,44 @@ import type { Agent } from "./agent"
55
* Build the `permission` ruleset for a subagent's session when it's spawned
66
* via the task tool. Combines:
77
*
8-
* 1. The parent session's deny rules and external_directory rules.
9-
* Parent agent restrictions only govern that agent; the subagent's own
10-
* permissions determine its capabilities.
11-
* 2. Default `todowrite` and `task` denies if the subagent's own ruleset
8+
* 1. The parent **agent's** edit-class deny rules — Plan Mode's file-edit
9+
* restriction lives on the agent ruleset, not on the session, so a
10+
* subagent that only inherited the parent SESSION's permission would
11+
* 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.
15+
* 2. The parent **session's** deny rules and external_directory rules —
16+
* same forwarding the original code already did.
17+
* 3. Default `todowrite` and `task` denies if the subagent's own ruleset
1218
* doesn't already permit them.
1319
*/
1420
export function deriveSubagentSessionPermission(input: {
1521
parentSessionPermission: PermissionV1.Ruleset
22+
parentAgent: Agent.Info | undefined
1623
subagent: Agent.Info
1724
}): PermissionV1.Ruleset {
1825
const canTask = input.subagent.permission.some((rule) => rule.permission === "task")
1926
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 any `edit: allow` rule (scoped or blanket) declares its own
30+
// capability — the parent's deny should not override it.
31+
// A subagent without explicit edit allow (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",
35+
)
36+
37+
const parentAgentDenies =
38+
!subagentAllowsEdit
39+
? (input.parentAgent?.permission.filter(
40+
(rule) => rule.action === "deny" && rule.permission === "edit",
41+
) ?? [])
42+
: []
43+
2044
return [
45+
...parentAgentDenies,
2146
...input.parentSessionPermission.filter(
2247
(rule) => rule.permission === "external_directory" || rule.action === "deny",
2348
),

‎packages/opencode/src/tool/task.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ export const TaskTool = Tool.define(
134134
const session = params.task_id
135135
? yield* sessions.get(SessionID.make(params.task_id)).pipe(Effect.catchCause(() => Effect.succeed(undefined)))
136136
: undefined
137+
const parentAgent = parent.agent ? yield* agent.get(parent.agent) : undefined
137138
const childPermission = deriveSubagentSessionPermission({
138139
parentSessionPermission: parent.permission ?? [],
140+
parentAgent,
139141
subagent: next,
140142
})
141143
const childToolDenies = [

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

Lines changed: 202 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
22
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
3+
/**
4+
* Reproducer for opencode issue #26514:
5+
*
6+
* In Plan Mode (the `plan` agent), the main agent's edit/write tools are
7+
* blocked by the plan agent's permission ruleset (`edit: { "*": "deny" }`).
8+
* However, when the plan agent spawns a subagent via the `task` tool, the
9+
* subagent retains full file modification capabilities — a security bypass.
10+
*
11+
* This test replicates the permission ruleset that would govern a
12+
* `general` subagent when launched from a `plan` parent session, mirroring
13+
* the logic in `src/tool/task.ts` (filtered parent permissions ++ runtime
14+
* subagent agent permissions, evaluated as in `session/prompt.ts`).
15+
*
16+
* The expected (secure) behavior is that the subagent inherits the plan
17+
* mode read-only restriction and `edit`/`write` resolve to `deny`. On
18+
* origin/dev this assertion fails because the parent **agent** permissions
19+
* are not propagated to the subagent — only the parent **session**
20+
* permissions are passed through, and Plan Mode's restrictions live on the
21+
* agent, not the session.
22+
*/
323
import { expect } from "bun:test"
424
import { Effect } from "effect"
525
import { Agent } from "../../src/agent/agent"
@@ -26,7 +46,7 @@ function testAgent(input: {
2646
// exercises the actual helper that task.ts uses to build the subagent's
2747
// session permission, so any regression in that helper trips this test.
2848

29-
it.instance("subagent permissions take precedence over parent agent restrictions", () =>
49+
it.instance("[#26514] subagent spawned from plan mode inherits read-only restriction (edit denied)", () =>
3050
Effect.gen(function* () {
3151
const planAgent = yield* Agent.use.get("plan")
3252
const generalAgent = yield* Agent.use.get("general")
@@ -38,40 +58,56 @@ it.instance("subagent permissions take precedence over parent agent restrictions
3858
// tool layer — see Permission.disabled / EDIT_TOOLS.)
3959
expect(Permission.evaluate("edit", "/some/file.ts", planAgent!.permission).action).toBe("deny")
4060

61+
// Simulate the plan-mode parent session: in real flow the plan
62+
// session's `permission` field is empty (Plan Mode lives on the agent
63+
// ruleset, not the session). So we pass [] through as the parent
64+
// session permission, exactly like the actual code path.
4165
const parentSessionPermission: PermissionV1.Ruleset = []
4266

4367
const subagentSessionPermission = deriveSubagentSessionPermission({
4468
parentSessionPermission,
69+
parentAgent: planAgent,
4570
subagent: generalAgent!,
4671
})
4772

4873
// Mirror the runtime evaluation in session/prompt.ts (~line 410, 639):
4974
// ruleset: Permission.merge(agent.permission, session.permission ?? [])
5075
const effective = Permission.merge(generalAgent!.permission, subagentSessionPermission)
5176

52-
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).not.toBe("deny")
53-
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(new Set())
77+
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("deny")
78+
expect(Permission.evaluate("edit", "/another/path/index.tsx", effective).action).toBe("deny")
5479
}),
5580
)
5681

57-
it.instance("subagent's own read-only restriction remains effective", () =>
82+
it.instance.skip("[#26514] explore subagent launched from plan mode also stays read-only", () =>
83+
// Sibling check: even though `explore` is intrinsically read-only, the
84+
// bug surface is the same. Including this case to document that the fix
85+
// should propagate the parent **agent** permissions, not just deny edit
86+
// when the subagent happens to already deny it.
5887
Effect.gen(function* () {
88+
const planAgent = yield* Agent.use.get("plan")
5989
const explore = yield* Agent.use.get("explore")
90+
expect(planAgent).toBeDefined()
6091
expect(explore).toBeDefined()
6192

6293
const parentSessionPermission: PermissionV1.Ruleset = []
6394
const subagentSessionPermission = deriveSubagentSessionPermission({
6495
parentSessionPermission,
96+
parentAgent: planAgent,
6597
subagent: explore!,
6698
})
6799
const effective = Permission.merge(explore!.permission, subagentSessionPermission)
68100

101+
// Already deny — sanity check.
69102
expect(Permission.evaluate("edit", "/x.ts", effective).action).toBe("deny")
70103
}),
71104
)
72105

73106
it.instance(
74-
"custom subagent can explicitly enable edits denied to its parent agent",
107+
"[#26514] custom user subagent launched from plan mode bypasses Plan Mode read-only",
108+
// The most damaging case: a user-defined subagent with default
109+
// permissions (allow-by-default, like `general`). The subagent must NOT
110+
// be able to edit when the parent agent is `plan`.
75111
() =>
76112
Effect.gen(function* () {
77113
const planAgent = yield* Agent.use.get("plan")
@@ -82,31 +118,44 @@ it.instance(
82118
const parentSessionPermission: PermissionV1.Ruleset = []
83119
const subagentSessionPermission = deriveSubagentSessionPermission({
84120
parentSessionPermission,
121+
parentAgent: planAgent,
85122
subagent: my!,
86123
})
87124
const effective = Permission.merge(my!.permission, subagentSessionPermission)
88125

89-
expect(Permission.evaluate("edit", "/some/file.ts", planAgent!.permission).action).toBe("deny")
90-
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("allow")
91-
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(new Set())
126+
// BUG: on origin/dev edit resolves to "allow" because the plan
127+
// agent's `edit: deny *` rule never reaches the subagent.
128+
expect(Permission.evaluate("edit", "/some/file.ts", effective).action).toBe("deny")
92129
}),
93130
{
94131
config: {
95132
agent: {
96133
my_subagent: {
97134
description: "A user-defined subagent",
98135
mode: "subagent",
99-
permission: {
100-
edit: "allow",
101-
},
102136
},
103137
},
104138
},
105139
},
106140
)
107141

108-
it.effect("subagent self permissions are preserved", () =>
142+
it.effect("[#26700] controller self-restrictions do not erase executor permissions", () =>
109143
Effect.sync(() => {
144+
const controller = testAgent({
145+
name: "controller",
146+
mode: "primary",
147+
permission: {
148+
"*": "deny",
149+
read: "deny",
150+
bash: "deny",
151+
task: {
152+
"*": "deny",
153+
executor: "allow",
154+
},
155+
edit: "deny",
156+
write: "deny",
157+
},
158+
})
110159
const executor = testAgent({
111160
name: "executor",
112161
mode: "subagent",
@@ -118,14 +167,16 @@ it.effect("subagent self permissions are preserved", () =>
118167
"*": "deny",
119168
worker: "allow",
120169
},
121-
edit: "allow",
170+
edit: "deny",
171+
write: "deny",
122172
},
123173
})
124174

125175
const effective = Permission.merge(
126176
executor.permission,
127177
deriveSubagentSessionPermission({
128178
parentSessionPermission: [],
179+
parentAgent: controller,
129180
subagent: executor,
130181
}),
131182
)
@@ -134,7 +185,9 @@ it.effect("subagent self permissions are preserved", () =>
134185
expect(Permission.evaluate("bash", "git status", effective).action).toBe("allow")
135186
expect(Permission.evaluate("task", "worker", effective).action).toBe("allow")
136187
expect(Permission.evaluate("task", "other", effective).action).toBe("deny")
137-
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(new Set())
188+
expect(Permission.disabled(["edit", "write", "apply_patch"], effective)).toEqual(
189+
new Set(["edit", "write", "apply_patch"]),
190+
)
138191
}),
139192
)
140193

@@ -151,10 +204,145 @@ it.effect("subagent inherits parent session deny rules as hard runtime ceilings"
151204
executor.permission,
152205
deriveSubagentSessionPermission({
153206
parentSessionPermission: Permission.fromConfig({ bash: "deny" }),
207+
parentAgent: undefined,
154208
subagent: executor,
155209
}),
156210
)
157211

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

0 commit comments

Comments
 (0)