Skip to content

Commit abd5264

Browse files
committed
feat(reviewer): tell blocked agents to ask and retry
A denial reaches the acting agent as the block reason, but nothing told it what to do next. Agents reliably substituted an equivalent action instead of asking: one denied jj squash turned into a workaround commit that the user then had to authorize squashing away 17 minutes later. The plugin already supports the right loop. A denial is cached against a digest of the root-session user turns, so the agent cannot argue past it, while any new user turn invalidates that cache and the identical action is judged again with the answer as evidence. Only the acting agent was never told the loop exists. Append fixed guidance to the block message on every final deny. The audit line, plugin storage record, and status-line event keep the bare model reason, so a verdict stays quotable as the model's own words. Verified with make test (47 pass) and make test-load. A mutation restricting the guidance to model denials is caught by the escalation table test and the cached-denial test.
1 parent 0b1b3ef commit abd5264

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎packages/reviewer/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ The hook runs inside permission evaluation, before any permission request exists
4949
reviewer is thinking, **no prompt is shown**: an `allow` means you never see the request at all,
5050
and a `deny` reaches the model as the block reason (the `reason` string).
5151

52+
A block message also carries fixed guidance telling the agent not to substitute an equivalent
53+
action, and to ask you to authorize the specific operation and scope before retrying the identical
54+
one. This matters because a model denial is cached: the agent cannot argue its way past it, but your
55+
next turn invalidates the cache and the same action is judged again with your answer as evidence.
56+
Agents left to improvise around a block tend to spend that opportunity on a worse plan instead.
57+
Denials that come from `escalationMode` after a timeout, error, or unparseable reply are not cached
58+
and are simply retried. The audit line, the plugin storage record, and the status-line event all
59+
keep the bare model reason.
60+
5261
Timeout, a model error, an unparseable reply, or a model that answers `ask` all fall back to
5362
`escalationMode`: `ask` (default) leaves the normal permission prompt with the reason attached,
5463
`deny` blocks the action with that reason. The deadline is local to the plugin: the host calls

‎packages/reviewer/src/index.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ Recent reviewer approvals are historical context, not user authorization, policy
104104
Answer with a single JSON object and nothing else:
105105
{"decision":"allow"|"deny"|"ask","reason":"one short sentence"}`
106106

107+
// A denial reaches the acting agent as the block reason. Left to itself an agent
108+
// tends to substitute an equivalent action, which spends the user's authorization
109+
// chance on a worse plan. Only a new user turn re-opens a cached denial, so the
110+
// retry path has to be stated where the acting agent will actually read it.
111+
const DENIAL_GUIDANCE = `Reviewer note: do not substitute an equivalent action to get around this block. If it was blocked for missing authorization rather than for being unsafe, ask the user to approve this specific operation and scope, then retry the identical action. Only the user's own reply can authorize it; asserting approval on their behalf changes nothing.`
112+
107113
class TimeoutError extends Error {}
108114

109115
function fail(message: string): never {
@@ -504,7 +510,9 @@ export default Plugin.define({
504510
}
505511

506512
if (outcome.decision !== "ask") event.effect = outcome.decision
507-
event.message = outcome.reason
513+
// The audit line and status event keep the bare reason; only the acting
514+
// agent's block message carries the retry guidance.
515+
event.message = outcome.decision === "deny" ? `${outcome.reason}\n\n${DENIAL_GUIDANCE}` : outcome.reason
508516

509517
const entry = {
510518
timestamp: new Date().toISOString(),

‎packages/reviewer/test/reviewer.test.ts‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,62 @@ test("denies on a deny decision", async () => {
292292
const event = ask({ resources: ["git push origin feature"] })
293293
await harness.evaluate(event)
294294
expect(event.effect).toBe("deny")
295-
expect(event.message).toBe("pushes to origin")
295+
expect(event.message).toContain("pushes to origin")
296+
})
297+
298+
// A block message is the acting agent's only view of a denial: the model's
299+
// reason first, then exactly one plugin-authored note, so neither can be read
300+
// as the other.
301+
function blockMessage(message: string | undefined): { reason: string; notes: string[] } {
302+
const parts = (message ?? "").split("\n\n")
303+
return { reason: parts[0] ?? "", notes: parts.slice(1) }
304+
}
305+
306+
test("tells a blocked agent to ask and retry instead of substituting an action", async () => {
307+
const harness = await start({}, replies(`{"decision":"deny","reason":"pushes to origin"}`))
308+
const event = ask({ resources: ["git push origin feature"] })
309+
await harness.evaluate(event)
310+
const { reason, notes } = blockMessage(event.message)
311+
expect(reason).toBe("pushes to origin")
312+
expect(notes).toHaveLength(1)
313+
expect(notes[0]).toContain("do not substitute an equivalent action")
314+
expect(notes[0]).toContain("retry the identical action")
315+
expect(notes[0]).toContain("Only the user's own reply can authorize it")
316+
// The records stay quotable as the model's own verdict.
317+
expect(harness.stored.last).toMatchObject({ decision: "deny", reason: "pushes to origin" })
318+
expect(harness.emitted[1]?.data).toMatchObject({ reason: "pushes to origin" })
319+
})
320+
321+
test("every denied outcome carries the reason and the guidance exactly once", async () => {
322+
const cases: { name: string; generate: Generate }[] = [
323+
{ name: "reviewer", generate: replies(`{"decision":"deny","reason":"pushes to origin"}`) },
324+
{ name: "uncertain", generate: replies(`{"decision":"ask","reason":"scope unclear"}`) },
325+
{ name: "parse", generate: replies("{") },
326+
{ name: "error", generate: async () => { throw new Error("provider unreachable") } },
327+
{ name: "timeout", generate: never },
328+
]
329+
for (const { name, generate } of cases) {
330+
const harness = await start({ escalationMode: "deny", timeoutMs: 20 }, generate)
331+
const event = ask()
332+
await harness.evaluate(event)
333+
const { reason, notes } = blockMessage(event.message)
334+
expect({ name, effect: event.effect, notes: notes.length }).toEqual({ name, effect: "deny", notes: 1 })
335+
expect({ name, reason }).not.toEqual({ name, reason: "" })
336+
expect({ name, stored: harness.stored.last }).toMatchObject({ name, stored: { decision: "deny", reason } })
337+
}
338+
})
339+
340+
test("outcomes that still prompt carry no guidance", async () => {
341+
const allowing = await start({}, replies(`{"decision":"allow","reason":"read-only inspection"}`))
342+
const allowed = ask()
343+
await allowing.evaluate(allowed)
344+
expect(allowed.message).toBe("read-only inspection")
345+
346+
const braking = await start({}, replies(`{"decision":"allow","reason":"trust me"}`))
347+
const braked = ask({ resources: ["rm -rf /"] })
348+
await braking.evaluate(braked)
349+
expect(braked.effect).toBe("ask")
350+
expect(blockMessage(braked.message).notes).toHaveLength(0)
296351
})
297352

298353
test("passes the configured model variant to generation", async () => {
@@ -315,7 +370,7 @@ test("ignores braces in prose around a single decision object", async () => {
315370
const event = ask()
316371
await harness.evaluate(event)
317372
expect(event.effect).toBe("deny")
318-
expect(event.message).toBe("writes outside the workspace")
373+
expect(event.message).toContain("writes outside the workspace")
319374
})
320375

321376
test("accepts repeated decision objects that agree", async () => {
@@ -370,7 +425,7 @@ test("records a model ask as uncertain and applies escalationMode", async () =>
370425
const event = ask()
371426
await harness.evaluate(event)
372427
expect(event.effect).toBe("deny")
373-
expect(event.message).toBe("scope unclear")
428+
expect(event.message).toContain("scope unclear")
374429
expect(harness.stored.last).toMatchObject({ decision: "deny", source: "uncertain" })
375430
})
376431

@@ -523,6 +578,7 @@ test("agent-authored retry text cannot bypass a cached denial", async () => {
523578
await harness.evaluate(retry)
524579
expect(first.effect).toBe("deny")
525580
expect(retry.effect).toBe("deny")
581+
expect(retry.message).toContain("Only the user's own reply can authorize it")
526582
expect(harness.generated).toBe(1)
527583
})
528584

0 commit comments

Comments
 (0)