Skip to content

Commit 4987de9

Browse files
docs: update Claude Code hooks section with enrichment pattern and Windows notes
- Add PreToolUse enrichment hook example (codegraph deps → additionalContext) - Document Windows backslash normalization caveat - List all hook files with descriptions - Update parallel session hooks to mention branch name validation
1 parent 631e27a commit 4987de9

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

docs/recommended-practices.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,24 @@ Use `--kind function` to cut noise. Use `--file <pattern>` to scope.
188188

189189
### Claude Code hooks
190190

191-
You can configure [Claude Code hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) to automatically rebuild the graph after file edits:
191+
You can configure [Claude Code hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) to give Claude automatic dependency context and keep the graph fresh as it edits files:
192192

193193
```json
194194
// .claude/settings.json
195195
{
196196
"hooks": {
197+
"PreToolUse": [
198+
{
199+
"matcher": "Read",
200+
"hooks": [
201+
{
202+
"type": "command",
203+
"command": "bash \"$CLAUDE_PROJECT_DIR/.claude/hooks/enrich-context.sh\"",
204+
"timeout": 10
205+
}
206+
]
207+
}
208+
],
197209
"PostToolUse": [
198210
{
199211
"matcher": "Edit|Write",
@@ -210,16 +222,26 @@ You can configure [Claude Code hooks](https://docs.anthropic.com/en/docs/claude-
210222
}
211223
```
212224

213-
This ensures the graph stays fresh as the AI agent modifies files. Incremental builds are automatic — only changed files are re-parsed.
225+
**Enrichment hook** (PreToolUse on Read/Grep): when Claude reads a file, the hook runs `codegraph deps` and injects import/export context into the conversation via `additionalContext`. This means Claude sees "this file imports X, Y and is imported by Z" without having to be told.
226+
227+
**Graph update hook** (PostToolUse on Edit/Write): keeps the graph incrementally updated after each file edit. Only changed files are re-parsed.
228+
229+
> **Windows note:** If your hooks use bash scripts, normalize backslashes inside `node -e` rather than bash (`${VAR//\\//}` fails on Git Bash). See this repo's `.claude/hooks/enrich-context.sh` for the pattern.
230+
231+
See this repo's `.claude/hooks/` directory for working implementations:
232+
- `enrich-context.sh` — dependency context injection
233+
- `update-graph.sh` — incremental graph updates after edits
234+
- `guard-git.sh` — blocks dangerous git commands + validates branch names
235+
- `track-edits.sh` — logs edited files for commit validation
214236

215237
#### Parallel session safety hooks
216238

217239
When multiple AI agents work on the same repo concurrently, add hooks to prevent cross-session interference:
218240

219241
- **Edit tracker** (PostToolUse on Edit|Write): log every file path touched to `.claude/session-edits.log`
220-
- **Git guard** (PreToolUse on Bash): block `git add .`, `git reset`, `git restore`, `git clean`, `git stash`, and validate that `git commit` only includes files from the session edit log
242+
- **Git guard** (PreToolUse on Bash): block `git add .`, `git reset`, `git restore`, `git clean`, `git stash`; validate that `git commit` only includes files from the session edit log; validate branch names match conventional prefixes
221243

222-
See this repo's `.claude/hooks/track-edits.sh` and `guard-git.sh` for a working implementation. Pair with the `/worktree` command so each session gets an isolated copy of the repo.
244+
Pair with the `/worktree` command so each session gets an isolated copy of the repo.
223245

224246
---
225247

0 commit comments

Comments
 (0)