11import { PermissionV1 } from "@opencode-ai/core/v1/permission"
22import { 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+ */
323import { expect } from "bun:test"
424import { Effect } from "effect"
525import { 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
73106it . 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