Skip to content

Commit 12f6ce1

Browse files
committed
fix: propagate parent agent permissions to subagent child sessions (anomalyco#12566)
When a parent agent has permissive rules (e.g., allow: *), those rules were not inherited by child sessions created via TaskTool. This caused subagent tool calls to evaluate as 'ask' and block indefinitely with no user to respond. Now the child session's permission includes: 1. The caller agent's permission rules (includes user config) 2. The parent session's accumulated rules (from 'always' replies) 3. Subagent-specific overrides (deny todowrite/todoread/task) Later rules win via findLast semantics, so the subagent deny overrides still take effect for todowrite/todoread/task while allowing everything else the parent permitted.
1 parent 18c5c5a commit 12f6ce1

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export const TaskTool = Tool.define("task", async (ctx) => {
6363

6464
const hasTaskPermission = agent.permission.some((rule) => rule.permission === "task")
6565

66+
// Inherit the caller agent's permission rules so subagents respect
67+
// permissive configs like "allow": "*" (fixes upstream #12566)
68+
const caller = ctx.agent ? await Agent.get(ctx.agent).catch(() => undefined) : undefined
69+
const parentSession = await Session.get(ctx.sessionID).catch(() => undefined)
70+
6671
const session = await iife(async () => {
6772
if (params.task_id) {
6873
const found = await Session.get(params.task_id).catch(() => {})
@@ -73,6 +78,11 @@ export const TaskTool = Tool.define("task", async (ctx) => {
7378
parentID: ctx.sessionID,
7479
title: params.description + ` (@${agent.name} subagent)`,
7580
permission: [
81+
// Inherit caller agent's permission rules (includes user config)
82+
...(caller?.permission ?? []),
83+
// Inherit parent session's accumulated permission rules
84+
...(parentSession?.permission ?? []),
85+
// Subagent-specific overrides (deny todowrite/todoread/task)
7686
{
7787
permission: "todowrite",
7888
pattern: "*",
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { PermissionNext } from "../../src/permission/next"
3+
4+
describe("subagent permission inheritance (#12566)", () => {
5+
test("child session inherits parent agent allow-all permission", () => {
6+
// Parent agent (e.g., "build") configured with "allow": "*"
7+
const parentPermission: PermissionNext.Ruleset = [
8+
{ permission: "*", pattern: "*", action: "allow" },
9+
]
10+
11+
// Parent session may have accumulated rules (e.g., from "always" replies)
12+
const parentSessionPermission: PermissionNext.Ruleset = []
13+
14+
// Subagent-specific overrides
15+
const childOverrides: PermissionNext.Ruleset = [
16+
{ permission: "todowrite", pattern: "*", action: "deny" },
17+
{ permission: "todoread", pattern: "*", action: "deny" },
18+
{ permission: "task", pattern: "*", action: "deny" },
19+
]
20+
21+
// This is how the child session permission should be assembled:
22+
// parent agent rules + parent session rules + child overrides
23+
const childSessionPermission = PermissionNext.merge(
24+
parentPermission,
25+
parentSessionPermission,
26+
childOverrides,
27+
)
28+
29+
// The subagent's own permission (e.g., "general" agent defaults)
30+
const subagentPermission: PermissionNext.Ruleset = [
31+
{ permission: "bash", pattern: "*", action: "ask" },
32+
{ permission: "edit", pattern: "*", action: "ask" },
33+
]
34+
35+
// In the child session, the effective ruleset is:
36+
// merge(subagent.permission, childSession.permission)
37+
const effective = PermissionNext.merge(subagentPermission, childSessionPermission)
38+
39+
// bash should evaluate to "allow" because the parent agent's allow-all
40+
// rule is in the child session's permission and comes after the subagent's "ask"
41+
const bashRule = PermissionNext.evaluate("bash", "ls", effective)
42+
expect(bashRule.action).toBe("allow")
43+
44+
// edit should also be allowed
45+
const editRule = PermissionNext.evaluate("edit", "src/foo.ts", effective)
46+
expect(editRule.action).toBe("allow")
47+
48+
// But todowrite should be denied (child override takes precedence)
49+
const todoRule = PermissionNext.evaluate("todowrite", "*", effective)
50+
expect(todoRule.action).toBe("deny")
51+
52+
// task should be denied (child override)
53+
const taskRule = PermissionNext.evaluate("task", "general", effective)
54+
expect(taskRule.action).toBe("deny")
55+
})
56+
57+
test("child session without parent inheritance falls back to ask", () => {
58+
// This simulates the OLD behavior (bug) where parent rules are not inherited
59+
const childOverrides: PermissionNext.Ruleset = [
60+
{ permission: "todowrite", pattern: "*", action: "deny" },
61+
{ permission: "todoread", pattern: "*", action: "deny" },
62+
{ permission: "task", pattern: "*", action: "deny" },
63+
]
64+
65+
// Subagent's own permission without user config allow-all
66+
const subagentPermission: PermissionNext.Ruleset = [
67+
{ permission: "bash", pattern: "*", action: "ask" },
68+
]
69+
70+
// Without parent inheritance, the merge only has subagent + child overrides
71+
const effective = PermissionNext.merge(subagentPermission, childOverrides)
72+
73+
// bash evaluates to "ask" — this would block in unattended mode
74+
const bashRule = PermissionNext.evaluate("bash", "ls", effective)
75+
expect(bashRule.action).toBe("ask")
76+
})
77+
78+
test("parent session accumulated rules propagate to child", () => {
79+
// Parent agent has default ask-for-bash
80+
const parentPermission: PermissionNext.Ruleset = [
81+
{ permission: "bash", pattern: "*", action: "ask" },
82+
]
83+
84+
// But the user replied "always" for bash during the parent session,
85+
// which gets stored in the parent session's permission
86+
const parentSessionPermission: PermissionNext.Ruleset = [
87+
{ permission: "bash", pattern: "*", action: "allow" },
88+
]
89+
90+
const childOverrides: PermissionNext.Ruleset = [
91+
{ permission: "todowrite", pattern: "*", action: "deny" },
92+
]
93+
94+
const childSessionPermission = PermissionNext.merge(
95+
parentPermission,
96+
parentSessionPermission,
97+
childOverrides,
98+
)
99+
100+
const subagentPermission: PermissionNext.Ruleset = [
101+
{ permission: "bash", pattern: "*", action: "ask" },
102+
]
103+
104+
const effective = PermissionNext.merge(subagentPermission, childSessionPermission)
105+
106+
// bash should be "allow" because the parent session's accumulated
107+
// "always allow" comes after the agent defaults
108+
const bashRule = PermissionNext.evaluate("bash", "ls", effective)
109+
expect(bashRule.action).toBe("allow")
110+
})
111+
112+
test("merge preserves last-wins semantics with findLast", () => {
113+
// PermissionNext.evaluate uses findLast, so later rules win
114+
const rules: PermissionNext.Ruleset = [
115+
{ permission: "bash", pattern: "*", action: "allow" },
116+
{ permission: "bash", pattern: "*", action: "deny" },
117+
{ permission: "bash", pattern: "*", action: "ask" },
118+
]
119+
const result = PermissionNext.evaluate("bash", "ls", rules)
120+
expect(result.action).toBe("ask") // last matching rule wins
121+
})
122+
123+
test("child deny overrides parent allow for specific permissions", () => {
124+
// Parent allows everything
125+
const parentPermission: PermissionNext.Ruleset = [
126+
{ permission: "*", pattern: "*", action: "allow" },
127+
]
128+
129+
// Child explicitly denies task (subagent should not spawn sub-subagents)
130+
const childOverrides: PermissionNext.Ruleset = [
131+
{ permission: "task", pattern: "*", action: "deny" },
132+
]
133+
134+
// Child session permission = parent + child overrides
135+
// The child deny comes AFTER the parent allow
136+
const childSessionPermission = PermissionNext.merge(parentPermission, childOverrides)
137+
138+
const subagentPermission: PermissionNext.Ruleset = []
139+
const effective = PermissionNext.merge(subagentPermission, childSessionPermission)
140+
141+
// task should be denied because the child's deny comes last
142+
const taskRule = PermissionNext.evaluate("task", "general", effective)
143+
expect(taskRule.action).toBe("deny")
144+
145+
// bash should be allowed via the parent's wildcard allow
146+
const bashRule = PermissionNext.evaluate("bash", "ls", effective)
147+
expect(bashRule.action).toBe("allow")
148+
})
149+
})

0 commit comments

Comments
 (0)