@@ -169,6 +169,11 @@ import sys
169169settings_file = sys.argv[1]
170170hooks_to_add = sys.argv[2:]
171171
172+ # Hooks that should ONLY be registered in Stop event (not PreToolUse)
173+ STOP_ONLY_HOOKS = {
174+ ".claude/hooks/php-qa-ci__auto-continue.py",
175+ }
176+
172177# Load existing settings
173178try:
174179 with open(settings_file, 'r') as f:
@@ -181,31 +186,58 @@ if 'hooks' not in settings:
181186 settings['hooks'] = {}
182187if 'PreToolUse' not in settings['hooks']:
183188 settings['hooks']['PreToolUse'] = [{'hooks': []}]
189+ if 'Stop' not in settings['hooks']:
190+ settings['hooks']['Stop'] = [{'hooks': []}]
184191
185- # Get existing hooks list
192+ # Get existing PreToolUse hooks list
186193pre_tool_use = settings['hooks']['PreToolUse']
187194if not pre_tool_use:
188195 pre_tool_use = [{'hooks': []}]
189196 settings['hooks']['PreToolUse'] = pre_tool_use
197+ pre_hooks_list = pre_tool_use[0].get('hooks', [])
190198
191- hooks_list = pre_tool_use[0].get('hooks', [])
199+ # Get existing Stop hooks list
200+ stop_hooks = settings['hooks']['Stop']
201+ if not stop_hooks:
202+ stop_hooks = [{'hooks': []}]
203+ settings['hooks']['Stop'] = stop_hooks
204+ stop_hooks_list = stop_hooks[0].get('hooks', [])
192205
193- # Get existing hook commands
194- existing_commands = {h.get('command') for h in hooks_list if isinstance(h, dict)}
206+ # Get existing hook commands (check both raw command and with env prefix)
207+ pre_existing_commands = {h.get('command') for h in pre_hooks_list if isinstance(h, dict)}
208+ stop_existing_commands = {h.get('command') for h in stop_hooks_list if isinstance(h, dict)}
195209
196- # Add new hooks if not already present
210+ # Remove any Stop-only hooks that were incorrectly added to PreToolUse
211+ pre_hooks_list = [h for h in pre_hooks_list if h.get('command') not in STOP_ONLY_HOOKS]
212+
213+ # Add new hooks to appropriate sections
197214for hook_path in hooks_to_add:
198- if hook_path not in existing_commands:
199- hooks_list.append({
200- 'type': 'command',
201- 'command': hook_path,
202- 'timeout': 5
203- })
204- print(f" Registered: {hook_path}")
215+ if hook_path in STOP_ONLY_HOOKS:
216+ # Stop-only hooks get CLAUDE_HOOK_EVENT=Stop prefix
217+ stop_command = f"CLAUDE_HOOK_EVENT=Stop {hook_path}"
218+ if stop_command not in stop_existing_commands and hook_path not in stop_existing_commands:
219+ stop_hooks_list.append({
220+ 'type': 'command',
221+ 'command': stop_command,
222+ 'timeout': 5
223+ })
224+ print(f" Registered (Stop): {stop_command}")
225+ else:
226+ print(f" Already registered (Stop): {hook_path}")
205227 else:
206- print(f" Already registered: {hook_path}")
207-
208- pre_tool_use[0]['hooks'] = hooks_list
228+ # Regular hooks go to PreToolUse
229+ if hook_path not in pre_existing_commands:
230+ pre_hooks_list.append({
231+ 'type': 'command',
232+ 'command': hook_path,
233+ 'timeout': 5
234+ })
235+ print(f" Registered (PreToolUse): {hook_path}")
236+ else:
237+ print(f" Already registered (PreToolUse): {hook_path}")
238+
239+ pre_tool_use[0]['hooks'] = pre_hooks_list
240+ stop_hooks[0]['hooks'] = stop_hooks_list
209241
210242# Write back
211243with open(settings_file, 'w') as f:
0 commit comments