Skip to content

fix(hooks): block() must exit 0 with stdout JSON, not exit 2 (v6.1.3)#47

Merged
intel352 merged 1 commit into
mainfrom
fix/pretool-block-exit-code-2026-05-27T1911
May 27, 2026
Merged

fix(hooks): block() must exit 0 with stdout JSON, not exit 2 (v6.1.3)#47
intel352 merged 1 commit into
mainfrom
fix/pretool-block-exit-code-2026-05-27T1911

Conversation

@intel352
Copy link
Copy Markdown
Contributor

Summary

  • Both Claude Code and Codex ignore stdout entirely when a hook exits with code 2 — the blocking reason must be on stderr. hooks/pre-tool-scope-guard and hooks/subagent-scope-guard were emitting {"decision":"block","reason":"..."} on stdout and then exit 2, which meant Codex surfaced PreToolUse hook exited with code 2 but did not write a blocking reason to stderr and Claude Code silently dropped the reason text.
  • Switched both block() helpers to exit 0 with stdout JSON (the documented decision-control path on both hosts per the May 2026 docs) and mirrored the reason to stderr as belt-and-suspenders. Same pattern already in use by hooks/completion-claim-guard.
  • Switched jq -njq -nc so emitted JSON is compact (matches existing grep-based test expectations; saves bytes).
  • Added two regression tests asserting block() exits 0 + emits stdout JSON + mirrors reason to stderr. Triggers are easily-reproducible: force-push for PreToolUse, manifest drift for SubagentStop.

Note: hooks/scope-lock-{abandon,complete,publish} also use exit 2 but those are CLI scripts invoked via Bash (not registered hooks), where exit 2 is just non-zero error semantics. They already write to stderr first, so they're correct.

Test plan

  • tests/hook-contracts.sh — 49 assertions including 2 new "Codex compat" regressions PASS
  • tests/version-check.sh — manifests agree on 6.1.3
  • Manual reproduction: force-push payload sent to pre-tool-scope-guard now prints JSON to stdout + reason to stderr + exits 0

References

  • Claude Code hooks reference — "Exit 2 means a blocking error. Claude Code ignores stdout and any JSON in it. Instead, stderr text is fed back to Claude as an error message."
  • Codex hooks reference — same contract; legacy {"decision":"block","reason":"..."} JSON on stdout with exit 0 is the supported equivalent.

Followup

Pre-existing bug: pre-tool-scope-guard's force-push regex matches the literal string git push --force anywhere in the command line, including inside heredoc bodies / quoted documentation strings. Encountered while opening this very PR — had to switch to --body-file to avoid a false-positive self-block. Should be fixed by applying the same quote-stripping the SUPERPOWERS_ self-bypass detector already does (line 151). Not bundled into this PR to keep scope tight; will file separately.

🤖 Generated with Claude Code

Both Claude Code and Codex ignore stdout entirely when a hook exits with
code 2 -- the blocking reason must be on stderr. The existing block()
helper in hooks/pre-tool-scope-guard and hooks/subagent-scope-guard was
emitting `{"decision":"block","reason":"..."}` on stdout and then exiting
2, which means:

- Codex surfaced: "PreToolUse hook exited with code 2 but did not write
  a blocking reason to stderr"
- Claude Code silently dropped the reason (the tool call was blocked but
  no reason text reached the agent or user)

Fixed both hooks by switching to `exit 0` with stdout JSON (the documented
decision-control path on both hosts) and mirroring the reason to stderr
as belt + suspenders. This mirrors the pattern already in use in
hooks/completion-claim-guard, which was correct.

Also switched `jq -n` to `jq -nc` so the emitted JSON is compact and
matches what existing grep-based hook tests look for.

Added two new regression tests in tests/hook-contracts.sh that assert
blocks exit 0, emit stdout JSON, AND mirror the reason to stderr. Both
trigger easily-reproducible block paths: force-push (pre-tool) and
manifest drift (subagent-stop).

Version bump 6.1.2 -> 6.1.3 across all four manifests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@intel352 intel352 merged commit cf00d67 into main May 27, 2026
5 checks passed
@intel352 intel352 deleted the fix/pretool-block-exit-code-2026-05-27T1911 branch May 27, 2026 19:14
intel352 added a commit that referenced this pull request May 28, 2026
…nd checks (v6.1.4) (#48)

The force-push, history-rewrite, locked-plan-push, and default-branch-push
checks were scanning the raw tool_input.command instead of the quote-stripped
cmd_no_quotes form (which was already computed for the SUPERPOWERS_ self-
bypass detector). The SUPERPOWERS_ check was correct; the four destructive-
command checks were not.

Real-world false positive encountered during v6.1.3 release: the PR body
heredoc for PR #47 quoted the force-push command verbatim as an example in
the changelog text. The hook scanned the full Bash command (including the
--body arg) and matched the example as a real force push, blocking the
very PR meant to ship the v6.1.3 fix. Worked around with --body-file that
release. Now fixed at the regex level.

Trade-off: an attacker who hides a destructive command inside quoted args
would slip through quote-strip. Acceptable edge case vs the much more
common false-positive on PR bodies and inline documentation. Same trade-off
the SUPERPOWERS_ check already accepted.

Added regression test in tests/hook-contracts.sh that asserts no block
fires when force-push appears inside a quoted string.

Version bump 6.1.3 -> 6.1.4 across all four manifests.

Co-authored-by: Jon Langevin <jon@gocodealone.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant