Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Honor policy denies on Claude Code's PermissionRequest hook

Claudee53ffa95a654
diff --git a/cli/beacon-hooks/cmd/policy.go b/cli/beacon-hooks/cmd/policy.go
index 12dd71e7..1a104863 100644
--- a/cli/beacon-hooks/cmd/policy.go
+++ b/cli/beacon-hooks/cmd/policy.go
@@ -254,9 +254,10 @@ func policyDenyFor(reason string, phase policycontract.Phase) *policyDenial {
// policyDenyResponse returns the runtime-specific hook response object that
// denies the tool call. A nil return means the platform has no confirmed
-// object-shaped deny. It is phase-independent: a platform with a confirmed deny
-// shape honors a deny in every phase the seam runs in, so a provider deny is
-// never silently dropped for a platform we enforce on.
+// object-shaped deny. A platform with a confirmed deny shape must honor a deny in
+// every phase the seam runs in, so a provider deny is never silently dropped for a
+// platform we enforce on -- which means the object follows the phase wherever the
+// runtime reads a different one per event (Claude Code, Qwen Code, Muse Code).
func policyDenyResponse(reason string, phase policycontract.Phase) map[string]interface{} {
switch {
case platformFlag == "cursor":
@@ -293,24 +294,35 @@ func policyDenyResponse(reason string, phase policycontract.Phase) map[string]in
// that works without changing that -- the same reasoning as OpenHands above.
case platformFlag == goosePlatform:
return map[string]interface{}{"decision": "block", "reason": reason}
- // Qwen Code shares Claude Code's deny shape exactly: `hookSpecificOutput.permissionDecision`
- // with a `permissionDecisionReason`, both required by its PreToolUse contract. The
- // `hookEventName` stays "PreToolUse" in both phases the seam runs in, matching the existing
- // claude behavior -- Qwen's PermissionRequest event reads a differently shaped
- // `hookSpecificOutput.decision` object, so a deny raised from that phase is not honored there.
- // Stated rather than hidden: the seam fails open, so the cost is a deny that does not take
- // effect on one of two phases, never a tool call blocked for the wrong reason.
- // DeepSeek Harness shares Claude Code's deny shape because its bridge exists to run Claude Code
- // hooks: the decoder reads `hookSpecificOutput.permissionDecision` as allow/deny/ask and
- // `permissionDecisionReason` as the reason, and maps a deny onto the harness's own
- // `tools/pre-execute` rejection with that reason handed to the model.
+ // Claude Code and Qwen Code answer the two phases the seam runs in with two different objects,
+ // and each reads only its own. PreToolUse takes `hookSpecificOutput.permissionDecision` with a
+ // `permissionDecisionReason`; PermissionRequest takes `hookSpecificOutput.decision` with
+ // `behavior: "deny"` and a `message` that Claude is told as the reason
+ // (https://code.claude.com/docs/en/hooks, "PermissionRequest decision control"). Sending the
+ // PreToolUse object from the permission phase is not a weaker deny, it is no deny: the runtime
+ // finds no `decision` and the permission flow carries on to the person, while emitPolicyDenied
+ // has already recorded the call as enforced. Exit code 2 is no fallback either -- Claude Code
+ // documents it as ignored on PermissionRequest.
+ //
+ // That gap is not hypothetical on Claude Code. The PreToolUse matcher Beacon installs names a
+ // fixed list of tools while PermissionRequest matches every tool, so a tool outside the list
+ // reaches the seam only in the permission phase.
+ //
+ // Qwen Code's PermissionRequestHookOutput reads the same `decision.behavior` and `message` and
+ // does not check hookEventName; the name is set anyway so one object is right for both.
+ case platformFlag == "claude" || platformFlag == "qwen":
+ return claudePolicyDenyResponse(reason, phase)
+ // DeepSeek Harness shares Claude Code's PreToolUse deny shape because its bridge exists to run
+ // Claude Code hooks: the decoder reads `hookSpecificOutput.permissionDecision` as
+ // allow/deny/ask and `permissionDecisionReason` as the reason, and maps a deny onto the
+ // harness's own `tools/pre-execute` rejection with that reason handed to the model.
//
// `hookEventName` must equal the firing event or the whole hookSpecificOutput block is
// discarded -- the bridge passes the point name as expectedEventName precisely to stop a hook
// answering for an event that is not running. "PreToolUse" is correct in the only phase that
// can reach this on dsh: Beacon registers no permission-request hook there, because the bridge
// exposes no such event.
- case platformFlag == "claude" || platformFlag == "qwen" || platformFlag == dshPlatform:
+ case platformFlag == dshPlatform:
return map[string]interface{}{
"hookSpecificOutput": map[string]interface{}{
"hookEventName": "PreToolUse",
@@ -341,10 +353,10 @@ func policyDenyResponse(reason string, phase policycontract.Phase) map[string]in
// than blocking a call for the wrong reason. Returning nil would give up the case where the
// inference is right for no safety gain.
//
- // hookEventName is derived from the phase rather than hardcoded, unlike the claude/qwen branch
- // above, because Muse requires it to match the firing event and Beacon binds each phase to a
- // distinct Muse event in the hooks file it writes. That is what lets a deny raised from the
- // PermissionRequest phase be honored here, where on Qwen it cannot be.
+ // hookEventName is derived from the phase rather than hardcoded, because Muse requires it to
+ // match the firing event and Beacon binds each phase to a distinct Muse event in the hooks file
+ // it writes. Unlike Claude Code above, the binary's strings name one object for both events, so
+ // only the event name follows the phase.
case platformFlag == "muse":
return map[string]interface{}{
"hookSpecificOutput": map[string]interface{}{
@@ -358,6 +370,33 @@ func policyDenyResponse(reason string, phase policycontract.Phase) map[string]in
}
}
+// claudePolicyDenyResponse is the Claude Code (and Qwen Code) deny for the phase the seam is
+// answering.
+//
+// The permission phase gets the PermissionRequest `decision` object, and every other phase the
+// PreToolUse object -- pre-tool is the phase the seam runs in on every runtime, so an unrecognized
+// phase degrades to the common case, the same fallback museHookEventNameForPhase takes.
+func claudePolicyDenyResponse(reason string, phase policycontract.Phase) map[string]interface{} {
+ if phase == policycontract.PhasePermissionRequest {
+ return map[string]interface{}{
+ "hookSpecificOutput": map[string]interface{}{
+ "hookEventName": "PermissionRequest",
+ "decision": map[string]interface{}{
+ "behavior": "deny",
+ "message": reason,
+ },
+ },
+ }
+ }
+ return map[string]interface{}{
+ "hookSpecificOutput": map[string]interface{}{
+ "hookEventName": "PreToolUse",
+ "permissionDecision": "deny",
+ "permissionDecisionReason": reason,
+ },
+ }
+}
+
// museHookEventNameForPhase names the Muse Code event the policy seam is answering.
//
// Muse validates that a hook's hookSpecificOutput echoes the event that fired it, so this must
diff --git a/cli/beacon-hooks/cmd/policy_test.go b/cli/beacon-hooks/cmd/policy_test.go
index c254b1cb..e1c3a29a 100644
--- a/cli/beacon-hooks/cmd/policy_test.go
+++ b/cli/beacon-hooks/cmd/policy_test.go
@@ -119,23 +119,56 @@ func TestPreToolPolicyAllowProceedsNormally(t *testing.T) {
func TestPermissionRequestPolicyDenyClaude(t *testing.T) {
// Regression: Claude is a confirmed-deny platform, so a provider deny on the
// permission-request hook must be honored (not silently allowed) just like on
- // pre-tool.
+ // pre-tool. Claude Code's PermissionRequest reads only
+ // hookSpecificOutput.decision.behavior, so the PreToolUse permissionDecision
+ // object would be ignored there while the event still claimed enforcement.
logPath := setupPolicyTest(t, "claude", denyResponse)
out := runHookWithInput(t, runPermissionRequest, map[string]interface{}{
"session_id": "s-claude-perm",
- "tool_name": "Bash",
- "tool_input": map[string]interface{}{"command": "claude --dangerously-skip-permissions"},
+ "tool_name": "NotebookEdit",
+ "tool_input": map[string]interface{}{"notebook_path": "/tmp/n.ipynb"},
})
hso, ok := out["hookSpecificOutput"].(map[string]interface{})
if !ok {
t.Fatalf("claude permission-request deny = %#v, want hookSpecificOutput", out)
}
- if hso["permissionDecision"] != "deny" {
- t.Fatalf("hookSpecificOutput = %#v, want permissionDecision=deny", hso)
+ if hso["hookEventName"] != "PermissionRequest" {
+ t.Fatalf("hookEventName = %v, want PermissionRequest", hso["hookEventName"])
+ }
+ if _, ok := hso["permissionDecision"]; ok {
+ t.Fatalf("hookSpecificOutput = %#v carries the PreToolUse permissionDecision", hso)
+ }
+ decision, ok := hso["decision"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("hookSpecificOutput = %#v, want a decision object", hso)
+ }
+ if decision["behavior"] != "deny" || decision["message"] != "blocked by test" {
+ t.Fatalf("decision = %#v, want behavior=deny message=blocked by test", decision)
}
assertDenialEvent(t, logPath)
}
+func TestPreToolPolicyDenyClaudeKeepsPreToolUseShape(t *testing.T) {
+ // The permission-phase object must not leak into pre-tool, where Claude Code
+ // reads permissionDecision and ignores a decision object.
+ setupPolicyTest(t, "claude", denyResponse)
+ out := runHookWithInput(t, runPreTool, map[string]interface{}{
+ "session_id": "s-claude-pre",
+ "tool_name": "Bash",
+ "tool_input": map[string]interface{}{"command": "codex --full-auto"},
+ })
+ hso, ok := out["hookSpecificOutput"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("claude pre-tool deny = %#v, want hookSpecificOutput", out)
+ }
+ if hso["hookEventName"] != "PreToolUse" {
+ t.Fatalf("hookEventName = %v, want PreToolUse", hso["hookEventName"])
+ }
+ if _, ok := hso["decision"]; ok {
+ t.Fatalf("hookSpecificOutput = %#v carries the PermissionRequest decision object", hso)
+ }
+}
+
func TestPermissionRequestPolicyDenyDevin(t *testing.T) {
logPath := setupPolicyTest(t, "devin", denyResponse)
out := runHookWithInput(t, runPermissionRequest, map[string]interface{}{
diff --git a/cli/beacon-hooks/cmd/qwen_hooks_test.go b/cli/beacon-hooks/cmd/qwen_hooks_test.go
index b686ae3c..0b5c0a99 100644
--- a/cli/beacon-hooks/cmd/qwen_hooks_test.go
+++ b/cli/beacon-hooks/cmd/qwen_hooks_test.go
@@ -140,6 +140,31 @@ func TestQwenPolicyDenyUsesQwensPermissionDecisionShape(t *testing.T) {
}
}
+// Qwen's PermissionRequest event reads `hookSpecificOutput.decision.behavior`, not
+// permissionDecision, so the PreToolUse object sent from the permission phase would be ignored and
+// the deny recorded as enforced would never take effect.
+func TestQwenPermissionRequestPolicyDenyUsesDecisionObject(t *testing.T) {
+ origPlatform := platformFlag
+ t.Cleanup(func() { platformFlag = origPlatform })
+ platformFlag = "qwen"
+
+ deny := policyDenyResponse("Security policy blocks database writes", policycontract.PhasePermissionRequest)
+ specific, ok := deny["hookSpecificOutput"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("deny response = %#v, want hookSpecificOutput", deny)
+ }
+ if _, ok := specific["permissionDecision"]; ok {
+ t.Errorf("hookSpecificOutput = %#v carries the PreToolUse permissionDecision on PermissionRequest", specific)
+ }
+ decision, ok := specific["decision"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("hookSpecificOutput = %#v, want a decision object", specific)
+ }
+ if decision["behavior"] != "deny" || decision["message"] != "Security policy blocks database writes" {
+ t.Errorf("decision = %#v, want behavior=deny with the provider's reason as message", decision)
+ }
+}
+
// Beacon's own default platform is claude, and an unknown --platform value falls through to
// "unknown platform -> allow". Naming qwen explicitly is what keeps it out of that fallback; this
// guards the pairing between the flag value the installer writes and the branch the adapter takes.
diff --git a/docs/runtimes/qwen-code.mdx b/docs/runtimes/qwen-code.mdx
index 32fdc8db..33d1eb58 100644
--- a/docs/runtimes/qwen-code.mdx
+++ b/docs/runtimes/qwen-code.mdx
@@ -159,7 +159,7 @@ Approval telemetry comes from Qwen's `PermissionRequest` event and is recorded a
Beacon's `PreToolUse` hook answers with an empty object. Qwen's `PreToolUse` contract reads a decision from `hookSpecificOutput.permissionDecision`, where `allow` means run the tool without the usual approval prompt. A hook that answered `allow` would disarm the user's own permission prompts for every tool call. An empty object carries no decision, so Qwen's normal permission flow runs untouched.
-Enforcement stays behind the optional, off-by-default policy provider seam (`BEACON_POLICY_PROVIDER`), which is inert unless it names an executable and fails open on any error. When a provider denies a call, Qwen uses Claude Code's deny shape: `hookSpecificOutput.permissionDecision` with a `permissionDecisionReason`.
+Enforcement stays behind the optional, off-by-default policy provider seam (`BEACON_POLICY_PROVIDER`), which is inert unless it names an executable and fails open on any error. When a provider denies a call, Qwen uses Claude Code's deny shapes, one per phase: from `PreToolUse`, `hookSpecificOutput.permissionDecision` with a `permissionDecisionReason`; from `PermissionRequest`, `hookSpecificOutput.decision` with `behavior: "deny"` and the reason as `message`.
## Data handling