Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,11 +1198,8 @@ export const layer = Layer.effect(
permissions.push({ permission: t, action: enabled ? "allow" : "deny", pattern: "*" })
}
if (permissions.length > 0) {
// Merge so per-call tool rules don't clobber inherited session rules
// (e.g. external_directory allows from the parent session).
const merged = Permission.merge(session.permission ?? [], permissions)
session.permission = merged
yield* sessions.setPermission({ sessionID: session.id, permission: merged })
session.permission = permissions
yield* sessions.setPermission({ sessionID: session.id, permission: permissions })
}

if (input.noReply === true) return message
Expand Down
41 changes: 26 additions & 15 deletions packages/opencode/src/tool/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,39 @@ export const TaskTool = Tool.define(
const parentAgent = parent.agent
? yield* agent.get(parent.agent).pipe(Effect.catchCause(() => Effect.succeed(undefined)))
: undefined
const childPermission = deriveSubagentSessionPermission({
parentSessionPermission: parent.permission ?? [],
parentAgent,
subagent: next,
})
const childToolDenies = [
...(next.permission.some((rule) => rule.permission === "todowrite")
? []
: [{ permission: "todowrite" as const, pattern: "*" as const, action: "deny" as const }]),
...(next.permission.some((rule) => rule.permission === id)
? []
: [{ permission: id, pattern: "*" as const, action: "deny" as const }]),
...(cfg.experimental?.primary_tools?.map((permission) => ({
permission,
pattern: "*" as const,
action: "deny" as const,
})) ?? []),
]
const nextSession =
session ??
(yield* sessions.create({
parentID: ctx.sessionID,
title: params.description + ` (@${next.name} subagent)`,
agent: next.name,
permission: [
...deriveSubagentSessionPermission({
parentSessionPermission: parent.permission ?? [],
parentAgent,
subagent: next,
}),
...(cfg.experimental?.primary_tools?.map((item) => ({
pattern: "*",
action: "allow" as const,
permission: item,
})) ?? []),
...childPermission,
...childToolDenies.filter(
(deny) =>
!childPermission.some(
(rule) =>
rule.permission === deny.permission && rule.pattern === deny.pattern && rule.action === deny.action,
),
),
],
}))

Expand Down Expand Up @@ -182,11 +198,6 @@ export const TaskTool = Tool.define(
},
variant: next.model ? undefined : variant,
agent: next.name,
tools: {
...(next.permission.some((rule) => rule.permission === "todowrite") ? {} : { todowrite: false }),
...(next.permission.some((rule) => rule.permission === id) ? {} : { task: false }),
...Object.fromEntries((cfg.experimental?.primary_tools ?? []).map((item) => [item, false])),
},
parts,
})
return result.parts.findLast((item) => item.type === "text")?.text ?? ""
Expand Down
27 changes: 27 additions & 0 deletions packages/opencode/test/session/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,33 @@ it.instance("subtask child inherits parent session external_directory allow", ()
}),
)

noLLMServer.instance("prompt tools replace previous prompt tool rules", () =>
Effect.gen(function* () {
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service
const session = yield* sessions.create({ title: "Prompt tools" })

yield* prompt.prompt({
sessionID: session.id,
agent: "build",
noReply: true,
tools: { bash: false },
parts: [{ type: "text", text: "first" }],
})
yield* prompt.prompt({
sessionID: session.id,
agent: "build",
noReply: true,
tools: { read: true },
parts: [{ type: "text", text: "second" }],
})

const reloaded = yield* sessions.get(session.id)
expect(reloaded.permission).toEqual([{ permission: "read", pattern: "*", action: "allow" }])
expect(Permission.evaluate("bash", "anything", reloaded.permission ?? []).action).toBe("ask")
}),
)

it.instance(
"running subtask preserves metadata after tool-call transition",
() =>
Expand Down
10 changes: 3 additions & 7 deletions packages/opencode/test/tool/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,15 @@ describe("tool.task", () => {
{
permission: "bash",
pattern: "*",
action: "allow",
action: "deny",
},
{
permission: "read",
pattern: "*",
action: "allow",
action: "deny",
},
])
expect(seen?.tools).toEqual({
todowrite: false,
bash: false,
read: false,
})
expect(seen?.tools).toBeUndefined()
}),
{
config: {
Expand Down
Loading