Skip to content

Commit 0c4f198

Browse files
radical-beardclaude
andcommitted
fix: filter question tool and respect session permissions in non-interactive mode
The question tool bypassed the permission pipeline by calling Question.ask() directly without going through ctx.ask(). Combined with LLM.resolveTools() only consulting agent-level permissions (where the build agent explicitly allows question), this caused opencode run to hang indefinitely when the model invoked the question tool (anomalyco#11899). Three changes fix this: 1. LLM.resolveTools() now accepts session-level permissions and merges them with agent permissions when computing disabled tools, so session rules like question:deny actually filter the tool from the tool list. 2. SessionPrompt passes the session's permission ruleset through to the LLM layer via the new StreamInput.permission field. 3. The question tool now calls ctx.ask() before Question.ask(), so even if the tool isn't filtered out, the permission pipeline is consulted. On denial, it returns a helpful message instead of hanging. Refs anomalyco#11899 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent a41c81d commit 0c4f198

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎packages/opencode/src/session/llm.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export namespace LLM {
3939
tools: Record<string, Tool>
4040
retries?: number
4141
toolChoice?: "auto" | "required" | "none"
42+
permission?: PermissionNext.Ruleset
4243
}
4344

4445
export type StreamOutput = StreamTextResult<ToolSet, unknown>
@@ -255,8 +256,11 @@ export namespace LLM {
255256
})
256257
}
257258

258-
async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user">) {
259-
const disabled = PermissionNext.disabled(Object.keys(input.tools), input.agent.permission)
259+
async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user" | "permission">) {
260+
const disabled = PermissionNext.disabled(
261+
Object.keys(input.tools),
262+
PermissionNext.merge(input.agent.permission, input.permission ?? []),
263+
)
260264
for (const tool of Object.keys(input.tools)) {
261265
if (input.user.tools?.[tool] === false || disabled.has(tool)) {
262266
delete input.tools[tool]

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ export namespace SessionPrompt {
674674
tools,
675675
model,
676676
toolChoice: format.type === "json_schema" ? "required" : undefined,
677+
permission: session.permission ?? [],
677678
})
678679

679680
// If structured output was captured, save it and exit immediately

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import z from "zod"
22
import { Tool } from "./tool"
33
import { Question } from "../question"
4+
import { PermissionNext } from "../permission/next"
45
import DESCRIPTION from "./question.txt"
56

67
export const QuestionTool = Tool.define("question", {
@@ -9,6 +10,19 @@ export const QuestionTool = Tool.define("question", {
910
questions: z.array(Question.Info.omit({ custom: true })).describe("Questions to ask"),
1011
}),
1112
async execute(params, ctx) {
13+
try {
14+
await ctx.ask({ permission: "question", patterns: ["*"], metadata: {}, always: ["*"] })
15+
} catch (e) {
16+
if (e instanceof PermissionNext.DeniedError || e instanceof PermissionNext.RejectedError) {
17+
return {
18+
title: "Question denied",
19+
output: "Cannot ask questions in this session. Make your best judgment and proceed.",
20+
metadata: { answers: [] },
21+
}
22+
}
23+
throw e
24+
}
25+
1226
const answers = await Question.ask({
1327
sessionID: ctx.sessionID,
1428
questions: params.questions,

0 commit comments

Comments
 (0)