Skip to main content

Hooks Reference

KMGraph registers shell scripts with Claude Code's hook system to automate knowledge capture across the session lifecycle. Hooks are configured in hooks/hooks.json and executed by the Claude Code harness — not by the agent.

Lifecycle Events

EventWhen it firesMatcher support
SessionStartOnce, when a Claude Code session beginsNo
PreToolUseBefore each tool callYes — regex on tool name + input
PostToolUseAfter each tool call completesYes — regex on tool name
StopWhen the agent stops or the session endsNo
NotificationOn any Claude Code notification eventNo

Hooks with a matcher field fire only when the tool name (and optionally the tool input JSON) matches the provided regex. An empty matcher string ("") matches every invocation of that event.

hooks.json Structure

{
"hooks": {
"<EventName>": [
{
"matcher": "<regex or empty string>",
"hooks": [
{
"type": "command",
"comment": "<human-readable description>",
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/<script>.sh",
"timeout": <seconds>
}
]
}
]
}
}

${CLAUDE_PLUGIN_ROOT} is resolved by the Claude Code harness to the absolute path of the KMGraph plugin directory. All scripts receive tool input JSON on stdin for PreToolUse and PostToolUse events.

Scripts Invoked by Hooks

ScriptEventMatcherEnabled by defaultWhat it does
hooks-master.shSessionStart(all)YesValidates KG config and MCP server build; displays recent lessons (last 7 days) from active and personal KGs; checks MEMORY.md staleness and diffs since last session; auto-switches active KG if autoSwitch: true and CWD matches a different project
post-tool-lesson-check.shPostToolUseWrite|EditYesScans tool input for lesson-worthy keywords (fix, solved, debug, pattern, etc.); suppresses on docs/, .git/, and .md-only writes; surfaces a prompt to run /kmgraph:capture-lesson
platform-file-change-check.shPostToolUseWrite|EditYesDetects writes to platform config files (CLAUDE.md, GEMINI.md, .cursorrules, copilot-instructions.md, etc.); prompts the user to consider syncing other platform files via /kmgraph:setup-platform
plan-mirror.shPostToolUseWriteYesDetects writes to ~/.claude/plans/; copies the file to docs/plans/ in the active KG project root; exits silently if the target directory does not exist
pre-commit-knowledge-gate.shPreToolUseBash.*git commitYesIntercepts plain git commit calls (skips --amend and --no-verify); checks staged files for lesson-worthy source types (src/, *.ts, *.py, *.sh, etc.); advisory prompt only — does not block the commit
session-end-prompt.shStop(all)YesChecks for open plan tasks, draft/proposed ADRs, and recent lesson-worthy commits without a captured lesson; displays a summary prompt; uses a /tmp flag to suppress duplicate output within the same session
notification-dispatch.shNotification(all)No (opt-in)Forwards the notification text to a configured webhookUrl in kg-config.json; exits silently if webhookUrl is absent; network failures never block the hook

Enabled vs Disabled by Default

All scripts are registered in hooks.json and active by default, with one exception:

ScriptDefault stateHow to activate
notification-dispatch.shDisabled (no-op)Add "webhookUrl": "https://..." to ~/.claude/kg-config.json

The other six scripts are always active once KMGraph is installed. Individual hooks can be removed from hooks.json to disable them permanently, or their timeout values can be adjusted to control how long each script may run before the harness terminates it.

Script Locations

All hook scripts reside in scripts/ relative to the plugin root. Additional scripts in that directory (fuzzy-search-archive.sh, install-vscode.sh, prepare-mcp.sh, validate-plugin.sh) are invoked by installer and CLI commands, not by the hook system.

Exit Code Conventions

Exit codeMeaning
0Success or non-blocking warning (session continues normally)
1Blocking error — used only by hooks-master.sh for unrecoverable config failures

PreToolUse hooks that exit 0 allow the tool call to proceed. A non-zero exit from a PreToolUse hook blocks the tool call; KMGraph's pre-commit-knowledge-gate.sh always exits 0 because it is advisory only.