5 Verbal Corrections Didn't Stick, So a 46-Line Stop Hook Made the Mistake Physically Impossible
说明与用途
46 行 Stop Hook 脚本阻止包含 && 的 ! 交接命令,消除 Claude Code 中的会话冻结。
问题
Claude Code repeatedly generated handoff commands using &&, causing session freezes; verbal corrections never persisted across sessions.
结果
The Stop hook prevented the recurring && mistake, eliminating session freezes and ensuring consistent, correct handoff commands without manual verbal corrections.
工作流
- Register Stop hook in ~/.claude/settings.json under "hooks" → "Stop" with empty matcher.
- Place the 46‑line script ~/.claude/hooks/bang_handoff_amp_guard.sh and make it executable.
- When Claude finishes a response, the hook runs, parses the transcript JSON, extracts the last assistant message, strips code fences, and checks for lines starting with
!that contain&&. - If such a line is found, the script outputs an error message and exits with code 2, blocking the response.
- Claude then re‑issues the response with corrected syntax (using
&instead of&&).
证据
I corrected the same Claude Code mistake more than five times. It never stuck. Then I replaced the correction with a 46-line shell script, and the mistake has not come back once.
Claude Code’s ! command launches a shell and runs the command. If you chain a blocking, process-occupying command using &&, the first command never returns, it sits waiting, and the following commands never execute.
- 记录日期: 2026-09-15T16:15:55.235Z