PostToolUse hook: exit code 2 does not block or undo the preceding write
Resolved 💬 3 comments Opened Apr 11, 2026 by DojoGenesis Closed Apr 14, 2026
Summary
When a PostToolUse hook returns exit code 2, it has no effect on the tool result — the file has already been written. This is surprising behavior that is not documented in the hooks reference.
Steps to reproduce
- Wire a
PostToolUsehook onEditorWriteevents. - Have the hook return
exit 2with a message. - Observe that the edit is persisted on disk regardless.
- Observe that agents reading the hook output may believe their edit failed and retry, causing confusion.
Expected behavior (from docs)
The hooks documentation implies exit code 2 blocks tool execution. This is true for PreToolUse but NOT for PostToolUse — the tool has already completed.
Actual behavior
PostToolUsefires after the write completes. The file is already on disk.exit 2fromPostToolUsedoes not undo the write.- Agents that inspect hook output may interpret it as a failure, leading to retry loops.
Proposed fix
Add a clear note in the hooks documentation for PostToolUse events:
Note:PostToolUsehooks fire after the tool has completed. Returningexit 2will not undo the tool's action. UsePreToolUsewithexit 2to block operations before they run.
Workaround
Use PreToolUse hooks with exit 2 to block writes. Use PostToolUse hooks for informational output only (plain echo, no exit 2).
Environment
Claude Code (CLI), hooks configured in .claude/settings.json.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗