Skip to content

Commit d51a3a4

Browse files
fix(hooks): add required hookSpecificOutput fields for context injection
The enrich-context.sh hook was outputting additionalContext without hookEventName and permissionDecision fields, causing Claude Code to silently drop the dependency context. Added both required fields and documented the correct output format in recommended-practices.md. Also fixes CRLF line endings in src/ files.
1 parent db3391b commit d51a3a4

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

.claude/hooks/enrich-context.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ printf '%s' "$DEPS" | node -e "
6969
if(importedBy)ctx+='\n Imported by: '+importedBy;
7070
if(defs)ctx+='\n Defines: '+defs;
7171
console.log(JSON.stringify({
72-
hookSpecificOutput: { additionalContext: ctx }
72+
hookSpecificOutput: {
73+
hookEventName: 'PreToolUse',
74+
permissionDecision: 'allow',
75+
additionalContext: ctx
76+
}
7377
}));
7478
} catch(e) {}
7579
});

docs/recommended-practices.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ You can configure [Claude Code hooks](https://docs.anthropic.com/en/docs/claude-
224224

225225
**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.
226226

227+
> **Important:** For `additionalContext` to surface in the agent's context, the hook output must include all three fields in `hookSpecificOutput`: `hookEventName: "PreToolUse"`, `permissionDecision: "allow"`, and `additionalContext`. Without `hookEventName` and `permissionDecision`, the context is silently dropped. Example output:
228+
>
229+
> ```json
230+
> {
231+
> "hookSpecificOutput": {
232+
> "hookEventName": "PreToolUse",
233+
> "permissionDecision": "allow",
234+
> "additionalContext": "[codegraph] src/builder.js\n Imports: src/config.js, src/db.js"
235+
> }
236+
> }
237+
> ```
238+
227239
**Graph update hook** (PostToolUse on Edit/Write): keeps the graph incrementally updated after each file edit. Only changed files are re-parsed.
228240
229241
> **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.

0 commit comments

Comments
 (0)