Skip to content

Commit 27ddf35

Browse files
committed
fix: end the turn when a permission is rejected with feedback
A reject sent WITH a message produces PermissionV1.CorrectedError, but failToolCall only blocked the loop on PermissionV1.RejectedError. So a reject that carried feedback never ended the turn, regardless of experimental.continue_loop_on_deny: the model received the feedback as a tool error and could immediately retry the same call and re-ask, looping tool-call and permission-ask events without user-visible progress. This is the harness loop from anomalyco#31108 as observed via the v1 SDK: kimaki's permission-timeout auto-reject always replies with a message, so its rejections were CorrectedError and never stopped the loop. Reproduced on unpatched dev: 6 queued identical denied retries all executed (7 LLM calls). With this fix the first reject ends the turn (1 LLM call). A reject is the user or a plugin saying no; the turn must end by default, message or not. The feedback is recorded on the errored tool part, so the model reads it on the next turn, or immediately when continue_loop_on_deny is true. Static ruleset denies (PermissionV1.DeniedError) still do not block: they are routine (subagent tool restrictions) and the model should see the error and adapt. Tests: - rejected permission with feedback blocks loop by default (fails on unpatched dev: 7 calls instead of 1) - rejected permission with feedback continues loop when continue_loop_on_deny is true, and the model sees the feedback text Fixes anomalyco#31108 Session: ses_15eee3980ffe5eK2RPweao3nyU
1 parent 6417797 commit 27ddf35

2 files changed

Lines changed: 105 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,25 @@ export const layer = Layer.effect(
238238
time: { start: match.part.state.time.start, end: Date.now() },
239239
},
240240
})
241-
// Only explicit user rejections block the loop (subject to
242-
// experimental.continue_loop_on_deny). Static ruleset denies
243-
// (PermissionV1.DeniedError) and rejections with feedback
244-
// (PermissionV1.CorrectedError) intentionally do NOT block: the model
245-
// should see the error output and adapt or act on the feedback.
241+
// Explicit rejections block the loop (subject to
242+
// experimental.continue_loop_on_deny). This includes rejections with
243+
// feedback (PermissionV1.CorrectedError): a reject is the user or a
244+
// plugin saying no, and the turn must end either way; the feedback is
245+
// recorded on the errored tool part, so the model reads it on the
246+
// next turn (or immediately when continue_loop_on_deny is true).
247+
// Before CorrectedError was included, a reject sent with a message
248+
// (e.g. kimaki's permission-timeout auto-reject) never ended the
249+
// turn, so the model could retry the denied call and re-ask in a
250+
// tight loop. Static ruleset denies (PermissionV1.DeniedError)
251+
// intentionally do NOT block: they are routine (e.g. subagent tool
252+
// restrictions) and the model should see the error and adapt.
246253
// Note: error identity survives Effect.orDie here because runPromise
247254
// rejects with the squashed cause, which is the original error object.
248-
if (error instanceof PermissionV1.RejectedError || error instanceof Question.RejectedError) {
255+
if (
256+
error instanceof PermissionV1.RejectedError ||
257+
error instanceof PermissionV1.CorrectedError ||
258+
error instanceof Question.RejectedError
259+
) {
249260
ctx.blocked = ctx.shouldBreak
250261
}
251262
yield* settleToolCall(toolCallID)

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

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,14 +2290,14 @@ noLLMServer.instance(
22902290
//
22912291
// autoRejectAsks mimics a plugin using the v1 SDK that rejects every
22922292
// permission ask as soon as it is published (e.g. subagent auto-reject).
2293-
const autoRejectAsks = Effect.fn("test.autoRejectAsks")(function* () {
2293+
const autoRejectAsks = Effect.fn("test.autoRejectAsks")(function* (message?: string) {
22942294
const events = yield* EventV2Bridge.Service
22952295
const permission = yield* Permission.Service
22962296
const off = yield* events.listen((event) =>
22972297
Effect.gen(function* () {
22982298
if (event.type !== Permission.Event.Asked.type) return
22992299
const req = event.data as { id: PermissionV1.ID }
2300-
yield* permission.reply({ requestID: req.id, reply: "reject" })
2300+
yield* permission.reply({ requestID: req.id, reply: "reject", ...(message ? { message } : {}) })
23012301
}).pipe(Effect.ignore),
23022302
)
23032303
yield* Effect.addFinalizer(() => off)
@@ -2422,3 +2422,89 @@ unix(
24222422
),
24232423
20_000,
24242424
)
2425+
2426+
unix(
2427+
"rejected permission with feedback blocks loop by default",
2428+
() =>
2429+
withSh(() =>
2430+
Effect.gen(function* () {
2431+
// A reject sent WITH a message produces PermissionV1.CorrectedError.
2432+
// Before CorrectedError joined the blocked check, such a reject never
2433+
// ended the turn: the model received the feedback as a tool error and
2434+
// could immediately retry the same call and re-ask, looping without
2435+
// user-visible progress (e.g. kimaki's permission-timeout auto-reject
2436+
// always sends a message). A reject must end the turn by default,
2437+
// message or not.
2438+
const { llm, dir } = yield* useServerConfig((url) => ({
2439+
...providerCfg(url),
2440+
experimental: { continue_loop_on_deny: false },
2441+
}))
2442+
yield* autoRejectAsks("Permission expired and was rejected.")
2443+
const prompt = yield* SessionPrompt.Service
2444+
const sessions = yield* Session.Service
2445+
const session = yield* sessions.create({
2446+
title: "CorrectedStop",
2447+
permission: [{ permission: "bash", pattern: "*", action: "ask" }],
2448+
})
2449+
yield* prompt.prompt({
2450+
sessionID: session.id,
2451+
agent: "build",
2452+
noReply: true,
2453+
parts: [{ type: "text", text: "run a command" }],
2454+
})
2455+
const call = { command: "rm -rf /tmp/corrected-stop", workdir: dir, description: "Remove dir" }
2456+
for (let i = 0; i < 6; i++) yield* llm.tool("bash", call)
2457+
yield* llm.text("should never be requested")
2458+
2459+
const result = yield* prompt.loop({ sessionID: session.id })
2460+
expect(result.info.role).toBe("assistant")
2461+
expect(yield* llm.calls).toBe(1)
2462+
2463+
const msgs = yield* MessageV2.filterCompactedEffect(session.id)
2464+
const tool = msgs.flatMap((m) => m.parts).find((p): p is SessionV1.ToolPart => p.type === "tool")
2465+
expect(tool?.state.status).toBe("error")
2466+
if (tool?.state.status === "error") expect(tool.state.error).toContain("Permission expired and was rejected.")
2467+
}),
2468+
),
2469+
15_000,
2470+
)
2471+
2472+
unix(
2473+
"rejected permission with feedback continues loop when continue_loop_on_deny is true",
2474+
() =>
2475+
withSh(() =>
2476+
Effect.gen(function* () {
2477+
const { llm, dir } = yield* useServerConfig((url) => ({
2478+
...providerCfg(url),
2479+
experimental: { continue_loop_on_deny: true },
2480+
}))
2481+
yield* autoRejectAsks("try a different approach")
2482+
const prompt = yield* SessionPrompt.Service
2483+
const sessions = yield* Session.Service
2484+
const session = yield* sessions.create({
2485+
title: "CorrectedContinue",
2486+
permission: [{ permission: "bash", pattern: "*", action: "ask" }],
2487+
})
2488+
yield* prompt.prompt({
2489+
sessionID: session.id,
2490+
agent: "build",
2491+
noReply: true,
2492+
parts: [{ type: "text", text: "run a command" }],
2493+
})
2494+
yield* llm.tool("bash", { command: "rm -rf /tmp/corrected-continue", workdir: dir, description: "Remove dir" })
2495+
yield* llm.text("adjusting based on feedback")
2496+
2497+
const result = yield* prompt.loop({ sessionID: session.id })
2498+
expect(result.info.role).toBe("assistant")
2499+
expect(yield* llm.calls).toBe(2)
2500+
2501+
const msgs = yield* MessageV2.filterCompactedEffect(session.id)
2502+
const parts = msgs.flatMap((m) => m.parts)
2503+
const tool = parts.find((p): p is SessionV1.ToolPart => p.type === "tool")
2504+
expect(tool?.state.status).toBe("error")
2505+
if (tool?.state.status === "error") expect(tool.state.error).toContain("try a different approach")
2506+
expect(parts.some((p) => p.type === "text" && p.text === "adjusting based on feedback")).toBe(true)
2507+
}),
2508+
),
2509+
15_000,
2510+
)

0 commit comments

Comments
 (0)