-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathremind-codegraph.sh
More file actions
67 lines (58 loc) · 2.24 KB
/
Copy pathremind-codegraph.sh
File metadata and controls
67 lines (58 loc) · 2.24 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
#!/usr/bin/env bash
# remind-codegraph.sh — PreToolUse hook for Edit|Write
# Reminds the agent to use codegraph before editing a file.
# Only fires once per file per session (tracks in .claude/codegraph-checked.log).
set -euo pipefail
# Extract file_path from tool input
INPUT="${TOOL_INPUT:-}"
FILE_PATH=$(echo "$INPUT" | node -e "
let d=''; process.stdin.on('data',c=>d+=c);
process.stdin.on('end',()=>{
try {
const j=JSON.parse(d);
const p = j.file_path || j.path || '';
// Normalize backslashes
console.log(p.replace(/\\\\/g,'/'));
} catch { console.log(''); }
});
" 2>/dev/null)
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Normalize to relative path
REL_PATH="$FILE_PATH"
if [ -n "${CLAUDE_PROJECT_DIR:-}" ]; then
REL_PATH=$(node -e "
const path = require('path');
const abs = path.resolve(process.argv[1]);
const base = path.resolve(process.argv[2]);
console.log(path.relative(base, abs).replace(/\\\\/g,'/'));
" "$FILE_PATH" "$CLAUDE_PROJECT_DIR" 2>/dev/null) || REL_PATH="$FILE_PATH"
fi
# Skip non-source files (docs, config, etc.)
case "$REL_PATH" in
*.md|*.json|*.yml|*.yaml|*.toml|*.txt|*.lock|*.log|*.env*) exit 0 ;;
esac
# Use git worktree root so each worktree session has its own checked log
WORK_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || WORK_ROOT="${CLAUDE_PROJECT_DIR:-.}"
CHECKED_LOG="$WORK_ROOT/.claude/codegraph-checked.log"
if [ -f "$CHECKED_LOG" ] && grep -qF "$REL_PATH" "$CHECKED_LOG" 2>/dev/null; then
exit 0
fi
# Record that we've reminded for this file
mkdir -p "$(dirname "$CHECKED_LOG")"
echo "$REL_PATH" >> "$CHECKED_LOG"
# Check if graph exists
if [ ! -f "$WORK_ROOT/.codegraph/graph.db" ]; then
exit 0
fi
# Inject reminder as additionalContext
cat <<HOOK_OUTPUT
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "allow",
"additionalContext": "[codegraph reminder] You are about to edit ${REL_PATH}. Did you run codegraph first? Before editing, always: (1) 'codegraph where <name>' to locate the symbol, (2) 'codegraph audit --quick ${REL_PATH}' to understand the file, (3) 'codegraph context <name> -T' for full context, (4) 'codegraph fn-impact <name> -T' to check blast radius. If you already did this, proceed."
}
}
HOOK_OUTPUT