spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / plugins / hookify / hooks / stop.py
59 lines · 1.5 KBRaw
1#!/usr/bin/env python3
2"""Stop hook executor for hookify plugin.
3
4This script is called by Claude Code when agent wants to stop.
5It reads .claude/hookify.*.local.md files and evaluates stop rules.
6"""
7
8import os
9import sys
10import json
11
12# CRITICAL: Add plugin root to Python path for imports
13PLUGIN_ROOT = os.environ.get('CLAUDE_PLUGIN_ROOT')
14if PLUGIN_ROOT:
15 parent_dir = os.path.dirname(PLUGIN_ROOT)
16 if parent_dir not in sys.path:
17 sys.path.insert(0, parent_dir)
18 if PLUGIN_ROOT not in sys.path:
19 sys.path.insert(0, PLUGIN_ROOT)
20
21try:
22 from hookify.core.config_loader import load_rules
23 from hookify.core.rule_engine import RuleEngine
24except ImportError as e:
25 error_msg = {"systemMessage": f"Hookify import error: {e}"}
26 print(json.dumps(error_msg), file=sys.stdout)
27 sys.exit(0)
28
29
30def main():
31 """Main entry point for Stop hook."""
32 try:
33 # Read input from stdin
34 input_data = json.load(sys.stdin)
35
36 # Load stop rules
37 rules = load_rules(event='stop')
38
39 # Evaluate rules
40 engine = RuleEngine()
41 result = engine.evaluate_rules(rules, input_data)
42
43 # Always output JSON (even if empty)
44 print(json.dumps(result), file=sys.stdout)
45
46 except Exception as e:
47 # On any error, allow the operation
48 error_output = {
49 "systemMessage": f"Hookify error: {str(e)}"
50 }
51 print(json.dumps(error_output), file=sys.stdout)
52
53 finally:
54 # ALWAYS exit 0
55 sys.exit(0)
56
57
58if __name__ == '__main__':
59 main()