Lifecycle Hooks
Massu AI includes 17 MCP lifecycle hooks and 11 shell utility hooks that run automatically during Claude Code sessions. Together they capture observations, inject context, enforce rules, filter output, preserve state, and drive the auto-learning pipeline -- all without any manual effort from the developer.
How Hooks Work
Hooks are compiled TypeScript files that Claude Code executes at specific lifecycle events. Each hook:
- Receives JSON on stdin with session context (session ID, transcript path, tool details)
- Processes the input (reads databases, analyzes tool calls, generates context)
- Outputs result on stdout (plain text for context injection, JSON for structured responses)
- Exits within 5 seconds to avoid blocking Claude Code
Hooks are compiled with esbuild into standalone JavaScript files bundled inside the @massu/core npm package (dist/hooks/). They are automatically configured in .claude/settings.local.json when you run npx massu init. They have no runtime dependencies beyond Node.js built-ins and the bundled Massu AI modules.
The 17-Hook Lifecycle
| Hook | Event | Purpose | Timing |
|---|---|---|---|
| session-start | Session begins | Inject memory context | Startup, resume, compact |
| session-end | Session ends | Generate summary, archive state | Session stop |
| pre-tool-use-gate | Before tool execution | Consolidated security-gate + pre-delete-check (single-spawn) | Before every tool |
| post-tool-use | After each tool call | Capture observations | After every tool |
| user-prompt | User submits prompt | Capture prompts, link tasks | Every prompt |
| pre-compact | Before compaction | Preserve session state | Before context window compaction |
| pre-delete-check | Before file deletion | Feature impact check (back-compat; folded into pre-tool-use-gate) | Before rm/delete operations |
| post-edit-context | After file edit | Surface applicable rules | After Edit/Write on src/ files |
| security-gate | Before tool execution | Validate security policies (back-compat; folded into pre-tool-use-gate) | Before dangerous operations |
| cost-tracker | After each tool call | Track token usage and costs | After every tool |
| quality-event | After each tool call | Detect quality signals | After every tool |
| intent-suggester | User submits prompt | Suggest relevant commands | Every prompt |
| fix-detector | After Edit/Write to source | Detect bug-fix shape via git diff; arm auto-learning pipeline | After every code edit |
| classify-failure | After Edit/Write to source | Score detected fix against known failure classes (KNOWN / SIMILAR / NEW) | After every code edit |
| incident-pipeline | After Write to docs/incidents/*.md | Trigger rule derivation from new incident report | When incident written |
| rule-enforcement-pipeline | After Write to memory/feedback_*.md | Trigger enforcement placement for new rule | When feedback rule written |
| auto-learning-pipeline | Session ends with armed fix | Forcing function: block session-end until incident → rule → enforcement complete | Stop hook with pending fix |
Session Lifecycle Flow
Session Start
|
v
[session-start hook] --> Injects memory context
|
v
User writes prompt
|
v
[user-prompt hook] --> Captures prompt, links to task
[intent-suggester hook] --> Suggests relevant commands
|
v
Claude Code prepares a tool call
|
v
[pre-tool-use-gate hook] --> Single-spawn check: security policies +
feature-impact protection on deletions
|
v
Claude Code processes (reads, edits, runs commands)
|
v
[post-tool-use hook] --> Captures observation per tool call
[post-edit-context hook] --> Surfaces rules on file edits
[cost-tracker hook] --> Records token usage
[quality-event hook] --> Detects quality signals
[fix-detector hook] --> Detects fix-shape diffs (arms auto-learning)
[classify-failure hook] --> Scores fix as KNOWN/SIMILAR/NEW
[incident-pipeline hook] --> Triggers when incidents/*.md written
[rule-enforcement-pipeline hook] --> Triggers when feedback_*.md written
|
v
(if context window fills up)
[pre-compact hook] --> Snapshots state before compaction
[session-start hook] --> Re-injects context after compact
|
v
Session ends
|
v
[auto-learning-pipeline hook] --> Forcing function: blocks Stop until
fix-detector arm has produced an
incident report + rule + enforcement
[session-end hook] --> Generates summary, archives CURRENT.mdZero Effort for Users
You do not need to interact with hooks directly. They run silently in the background:
- No commands to remember: Hooks trigger automatically at the right lifecycle events
- No performance impact: Each hook completes in under 500ms
- No configuration needed: Hooks use your
massu.config.yamlautomatically - Best-effort execution: If a hook fails, it exits silently without blocking Claude Code
Hook Compilation
Hooks are written in TypeScript and compiled with esbuild:
cd packages/core && npm run build:hooksThis produces standalone JavaScript files in packages/core/dist/hooks/ that are referenced by .claude/settings.local.json during installation. Run npx massu install-hooks to update the hooks configuration.
Detailed Hook Documentation
Click any hook below for complete documentation including input/output formats, configuration, and examples:
- session-start -- Context injection at session start
- session-end -- Summary generation and archival
- pre-tool-use-gate -- Consolidated PreToolUse single-spawn check (security + delete-protection)
- post-tool-use -- Observation capture after tool calls
- user-prompt -- Prompt capture and task linking
- pre-compact -- State preservation before compaction
- pre-delete-check -- Deletion protection via feature impact (back-compat; folded into pre-tool-use-gate)
- post-edit-context -- Rule surfacing after file edits
- security-gate -- Security policy validation before tool execution (back-compat; folded into pre-tool-use-gate)
- cost-tracker -- Token usage tracking per tool call
- quality-event -- Quality signal detection after tool calls
- intent-suggester -- Command suggestion based on prompt intent
- fix-detector -- Fix-shape detection via git diff (arms auto-learning pipeline)
- classify-failure -- Scores detected fix against known failure classes
- incident-pipeline -- Triggers rule derivation when incident reports are written
- rule-enforcement-pipeline -- Triggers enforcement placement when feedback rules are written
- auto-learning-pipeline -- Stop-hook forcing function for the incident → rule → enforcement chain
Shell Hooks (11)
In addition to the 11 MCP lifecycle hooks, Massu AI includes 11 shell utility hooks located in scripts/hooks/. These hooks are invoked at specific points to enforce policies, filter output, and automate routine tasks.
| Hook | Trigger | Purpose |
|---|---|---|
output-secret-filter.sh | After every tool output | Strip potential secrets (API keys, tokens, passwords) from Claude Code output before display |
memory-integrity-check.sh | Session start | Verify SQLite memory database integrity; alert if corruption detected |
loop-circuit-breaker.sh | During /massu-loop | Detect infinite loops and halt with a structured error after configurable iteration limit |
auto-ingest-incident.sh | Session end | Automatically ingest any incidents detected during the session into the knowledge system |
auto-review-on-stop.sh | Session stop | Trigger an automated code review of uncommitted changes when the session stops |
validate-deliverables.sh | During /massu-loop | Verify each plan deliverable is actually complete before marking it done |
surface-review-findings.sh | Post-review | Surface /massu-review findings inline as Claude Code context for the next prompt |
pattern-feedback.sh | After tool calls | Capture pattern usage signals and feed them back to the knowledge system |
mcp-rate-limiter.sh | Before each MCP tool call | Enforce per-tool rate limits to prevent runaway tool usage in long sessions |
mcp-usage-tracker.sh | After each MCP tool call | Record tool invocation counts and latency for the observability dashboard |
cost-tracker.sh | After each MCP tool call | Track estimated token costs per tool invocation for the cost intelligence dashboard |
Shell Hook Configuration
Shell hooks are configured in .claude/settings.local.json alongside the MCP lifecycle hooks. They are installed by npx massu init and updated by npx massu install-hooks.
{
"hooks": {
"PostToolUse": [
{ "matcher": "*", "hooks": [{ "type": "command", "command": "node dist/hooks/post-tool-use.js" }] },
{ "matcher": "*", "hooks": [{ "type": "command", "command": "bash dist/hooks/mcp-usage-tracker.sh" }] }
]
}
}Shell hooks follow the same stdin/stdout contract as MCP hooks: they receive JSON on stdin, write output on stdout, and must exit within 5 seconds.