Skip to content

Commit c70ab90

Browse files
byapparovclaude
andcommitted
fix(task): apply the caller session's rules to subagent sessions (#147)
A subagent session was created with only the todo/task denies, so a caller's session-level rules never reached the subagent tree. The rules were also overwritten right away by the legacy `tools` prompt option, which replaces the session ruleset. Create the subagent session with merge(caller session rules, subagent denies), and stop passing `tools` to the subagent prompt so the merged ruleset is kept. Primary-only tools stay denied to subagents: the `tools: false` override always replaced the create-time allow, so deny was already the effective rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 12c849d commit c70ab90

2 files changed

Lines changed: 97 additions & 10 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ export const TaskTool = Tool.define("task", async (ctx) => {
9292
if (found) return found
9393
}
9494

95+
const caller = await Session.get(ctx.sessionID)
9596
return await Session.create({
9697
parentID: ctx.sessionID,
9798
title: params.description + ` (@${agent.name} subagent)`,
98-
permission: [
99+
// the caller's session rules apply down the subagent tree; the subagent denies come last and win
100+
permission: PermissionNext.merge(caller.permission ?? [], [
99101
{
100102
permission: "todowrite",
101103
pattern: "*",
@@ -117,10 +119,10 @@ export const TaskTool = Tool.define("task", async (ctx) => {
117119
]),
118120
...(config.experimental?.primary_tools?.map((t) => ({
119121
pattern: "*",
120-
action: "allow" as const,
122+
action: "deny" as const,
121123
permission: t,
122124
})) ?? []),
123-
],
125+
]),
124126
})
125127
})
126128

@@ -163,12 +165,6 @@ export const TaskTool = Tool.define("task", async (ctx) => {
163165
providerID: model.providerID,
164166
},
165167
agent: agent.name,
166-
tools: {
167-
todowrite: false,
168-
todoread: false,
169-
...(hasTaskPermission ? {} : { task: false }),
170-
...Object.fromEntries((config.experimental?.primary_tools ?? []).map((t) => [t, false])),
171-
},
172168
parts: promptParts,
173169
})
174170

‎packages/cli/test/tool/task.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { describe, expect, spyOn, test } from "bun:test"
2+
import { Identifier } from "../../src/id/id"
3+
import type { PermissionNext } from "../../src/permission/next"
4+
import { Instance } from "../../src/project/instance"
5+
import { Session } from "../../src/session"
6+
import { LLM } from "../../src/session/llm"
27
import { MessageV2 } from "../../src/session/message-v2"
38
import { Plugin } from "../../src/plugin"
4-
import { completeTask, taskResultText } from "../../src/tool/task"
9+
import { TaskTool, completeTask, taskResultText } from "../../src/tool/task"
10+
import { tmpdir } from "../fixture/fixture"
511

612
function result(input: { error?: MessageV2.Assistant["error"]; parts?: MessageV2.Part[] }): MessageV2.WithParts {
713
return {
@@ -127,3 +133,88 @@ describe("task tool child result", () => {
127133
).toBe("done")
128134
})
129135
})
136+
137+
describe("task tool subagent session", () => {
138+
test("applies the caller session's rules to the subagent, beneath the subagent denies", async () => {
139+
await using tmp = await tmpdir({
140+
git: true,
141+
config: {
142+
provider: {
143+
fixture: {
144+
npm: "@ai-sdk/google",
145+
options: { apiKey: "fixture" },
146+
models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } },
147+
},
148+
},
149+
experimental: { primary_tools: ["websearch"] },
150+
},
151+
})
152+
await Instance.provide({
153+
directory: tmp.path,
154+
fn: async () => {
155+
const inherited: PermissionNext.Ruleset = [
156+
{ permission: "webfetch", action: "deny", pattern: "*" },
157+
{ permission: "task", action: "allow", pattern: "*" },
158+
]
159+
const parent = await Session.create({ title: "Caller", permission: inherited })
160+
const caller = await Session.updateMessage({
161+
id: Identifier.ascending("message"),
162+
parentID: Identifier.ascending("message"),
163+
role: "assistant",
164+
mode: "build",
165+
agent: "build",
166+
path: { cwd: tmp.path, root: tmp.path },
167+
cost: 0,
168+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
169+
modelID: "gemini-fixture",
170+
providerID: "fixture",
171+
time: { created: Date.now() },
172+
sessionID: parent.id,
173+
})
174+
const requests: LLM.StreamInput[] = []
175+
const stream = spyOn(LLM, "stream").mockImplementation(async (input) => {
176+
requests.push(input)
177+
return {
178+
fullStream: (async function* () {
179+
yield { type: "start-step" }
180+
yield { type: "text-start" }
181+
yield { type: "text-delta", text: "done" }
182+
yield { type: "text-end" }
183+
yield { type: "finish-step", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1 } }
184+
})(),
185+
} as unknown as Awaited<ReturnType<typeof LLM.stream>>
186+
})
187+
try {
188+
const tool = await TaskTool.init()
189+
const result = await tool.execute(
190+
{ description: "Explore", prompt: "Look around", subagent_type: "general" },
191+
{
192+
sessionID: parent.id,
193+
messageID: caller.id,
194+
agent: "build",
195+
abort: new AbortController().signal,
196+
extra: { bypassAgentCheck: true },
197+
messages: [],
198+
metadata() {},
199+
async ask() {},
200+
},
201+
)
202+
const child = await Session.get(result.metadata.sessionId)
203+
const subagent = requests.filter((request) => request.sessionID === child.id)
204+
expect(subagent).toHaveLength(1)
205+
expect(subagent[0].permission).toEqual([
206+
...inherited,
207+
{ permission: "todowrite", action: "deny", pattern: "*" },
208+
{ permission: "todoread", action: "deny", pattern: "*" },
209+
{ permission: "task", action: "deny", pattern: "*" },
210+
// primary-only tools stay unavailable to subagents
211+
{ permission: "websearch", action: "deny", pattern: "*" },
212+
])
213+
} finally {
214+
stream.mockRestore()
215+
await Session.remove(parent.id)
216+
}
217+
},
218+
})
219+
})
220+
})

0 commit comments

Comments
 (0)