-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpre-tool-use.py
More file actions
238 lines (185 loc) · 7.49 KB
/
Copy pathpre-tool-use.py
File metadata and controls
238 lines (185 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
"""CodingBuddy PreToolUse Hook.
Intercepts Bash tool calls to enforce quality gates on git commit commands.
Detects .ai-rules/config changes via FileWatcher (#823).
Suggests related tests for staged files via SmartTestRunner (#944).
Auto-selects relevant checklists for staged files via ChecklistVerifier (#1001).
Displays active agent status in spinner via statusMessage (#974).
Uses safe_main decorator to ensure Claude Code is never blocked.
"""
import json
import os
import re
import subprocess
import sys
from typing import List, Optional
# Resolve hooks/lib and add to path
_hooks_dir = os.path.dirname(os.path.abspath(__file__))
_lib_dir = os.path.join(_hooks_dir, "lib")
if _lib_dir not in sys.path:
sys.path.insert(0, _lib_dir)
from safe_main import safe_main
from config import get_config
from agent_status import build_status_message
from adaptive_perf import get_monitor
# Pattern to detect git commit in a command string
_GIT_COMMIT_RE = re.compile(r"\bgit\s+commit\b")
QUALITY_GATE_CONTEXT = (
"[CodingBuddy Quality Gate] Before committing, ensure:\n"
"- All tests pass\n"
"- Code follows project conventions\n"
"- Changes are reviewed (self-review at minimum)\n"
"- Commit message follows project convention"
)
FILE_WATCHER_CONTEXT = (
"CodingBuddy: Rules/config changed since last check. Consider reloading."
)
# Snapshot persistence directory
_SNAPSHOT_DIR = os.path.join(
os.environ.get("CLAUDE_PLUGIN_DATA",
os.path.join(os.path.expanduser("~"), ".codingbuddy")),
"snapshots",
)
def _get_hook_config() -> dict:
"""Load config from cwd."""
cwd = os.environ.get("CLAUDE_CWD", os.getcwd())
return get_config(cwd)
def _get_project_root(data: dict) -> str:
"""Get project root from hook input data or environment."""
return data.get("cwd", os.environ.get("CLAUDE_CWD", os.getcwd()))
def _snapshot_path(project_root: str) -> str:
"""Return the path to the snapshot file for a given project root."""
# Use a hash of project root to avoid path separator issues
safe_name = project_root.replace(os.sep, "_").strip("_")
return os.path.join(_SNAPSHOT_DIR, f"{safe_name}.json")
def _load_snapshot(path: str) -> dict:
"""Load a saved snapshot from disk."""
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except (OSError, json.JSONDecodeError, ValueError):
return {}
def _save_snapshot(path: str, snapshot: dict) -> None:
"""Persist a snapshot to disk."""
try:
os.makedirs(os.path.dirname(path), mode=0o700, exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(snapshot, f)
except OSError:
pass
def _check_file_changes(data: dict) -> Optional[str]:
"""Check for .ai-rules/config changes via FileWatcher.
Returns additionalContext string if changes detected, else None.
"""
try:
from file_watcher import FileWatcher
project_root = _get_project_root(data)
config = _get_hook_config()
fw_config = config.get("fileWatcher", {})
if not fw_config.get("enabled", True):
return None
watcher = FileWatcher(project_root)
snap_path = _snapshot_path(project_root)
old_snapshot = _load_snapshot(snap_path)
if old_snapshot:
# Restore previous snapshot for comparison
watcher._last_snapshot = old_snapshot
changes = watcher.detect_changes()
# Take new snapshot for next invocation
new_snapshot = watcher.snapshot()
_save_snapshot(snap_path, new_snapshot)
if changes:
return FILE_WATCHER_CONTEXT
else:
# First invocation — just take initial snapshot
new_snapshot = watcher.snapshot()
_save_snapshot(snap_path, new_snapshot)
return None
except Exception:
return None
def _is_git_commit(command: str) -> bool:
"""Check if a bash command contains a git commit invocation."""
return bool(_GIT_COMMIT_RE.search(command))
def _get_staged_files() -> List[str]:
"""Return list of staged file paths via git diff --cached."""
try:
output = subprocess.check_output(
["git", "diff", "--cached", "--name-only"],
stderr=subprocess.DEVNULL,
timeout=5,
)
return [f for f in output.decode("utf-8", errors="replace").strip().split("\n") if f]
except Exception:
return []
def _get_test_suggestion(staged_files: List[str]) -> Optional[str]:
"""Use SmartTestRunner to build a test-run suggestion for staged files."""
try:
from smart_test_runner import SmartTestRunner
runner = SmartTestRunner()
related = runner.find_related_tests(staged_files)
if not related:
return None
return runner.format_suggestion(related)
except Exception:
return None
def _get_checklist_warning(staged_files: List[str]) -> Optional[str]:
"""Use ChecklistVerifier to build a checklist warning for staged files (#1001)."""
try:
from checklist_verifier import ChecklistVerifier
verifier = ChecklistVerifier()
return verifier.verify(staged_files)
except Exception:
return None
def _handle(data: dict) -> Optional[dict]:
"""Core PreToolUse logic.
Args:
data: Hook input with tool_name and tool_input.
Returns:
Hook output dict or None for no intervention.
"""
# Build agent status message for spinner (#974) — applies to ALL tools
status_msg = build_status_message()
tool_name = data.get("tool_name", "")
contexts = []
# Initialize adaptive performance monitor (#1002)
perf_config = _get_hook_config()
perf_monitor = get_monitor(perf_config)
# Bash-specific checks
if tool_name == "Bash":
# Check for file changes (#823) — skip in lightweight mode (#1002)
if not perf_monitor.should_skip("file_watcher"):
file_change_msg = _check_file_changes(data)
if file_change_msg:
contexts.append(file_change_msg)
command = data.get("tool_input", {}).get("command", "")
# Check git commit quality gates and smart test suggestion (#944)
if _is_git_commit(command):
config = _get_hook_config()
quality_gates = config.get("qualityGates", {})
if quality_gates.get("enabled", False):
contexts.append(QUALITY_GATE_CONTEXT)
# Smart test runner — suggest related tests for staged files
staged = _get_staged_files()
if staged:
suggestion = _get_test_suggestion(staged)
if suggestion:
contexts.append(suggestion)
# Checklist verifier — auto-select relevant checklists (#1001)
checklist_warning = _get_checklist_warning(staged)
if checklist_warning:
contexts.append(checklist_warning)
# Build response — include statusMessage and/or additionalContext
if not status_msg and not contexts:
return None
hook_output: dict = {}
if status_msg:
hook_output["statusMessage"] = status_msg
if contexts:
hook_output["additionalContext"] = "\n\n".join(contexts)
return {"hookSpecificOutput": hook_output}
@safe_main
def handle_pre_tool_use(data: dict) -> Optional[dict]:
"""Entry point for PreToolUse hook."""
return _handle(data)
if __name__ == "__main__":
handle_pre_tool_use()