Hooks System
Hooks are lifecycle event handlers that execute at specific points in the platform’s lifecycle, allowing Superpowers to inject context, run setup scripts, or perform initialization tasks.What Are Hooks?
Hooks are automatic scripts that run when certain events occur in your AI coding platform. They enable:- Context Injection: Adding information to the agent’s context at session start
- Setup Tasks: Running initialization scripts before work begins
- Environment Configuration: Setting up project-specific requirements
- Custom Workflows: Triggering platform-specific automation
Hooks are transparent to users - they run automatically without user intervention.
Hook Configuration
Hooks are defined inhooks/hooks.json:
Configuration Anatomy
Hook Event Types
Hook Event Types
Different platforms support different hook events:
SessionStart: Runs when a new session begins (supported by Claude Code, Cursor)SessionResume: Runs when resuming a saved sessionProjectOpen: Runs when opening a project- Custom events: Platform-specific hooks
Matcher Pattern
Matcher Pattern
The This regex matches session states:
matcher field filters when hooks run:startup: New sessionresume: Resumed sessionclear: Cleared sessioncompact: Compacted session
Hook Commands
Hook Commands
Each hook has:
type: Command type (usually"command")command: Shell command to executeasync: Whether to run asynchronously (false= blocking)
${CLAUDE_PLUGIN_ROOT}: Path to plugin root directory${CURSOR_PLUGIN_ROOT}: Path to plugin root (Cursor)- Standard shell variables (
$HOME,$USER, etc.)
SessionStart Hook
The SessionStart hook is the most important hook in Superpowers - it injects theusing-superpowers skill content into every session.
Hook Script: hooks/session-start
What the Hook Does
Escape for JSON
Properly escapes content for JSON embedding (critical for special characters):Performance note: Uses bash parameter substitution instead of character-by-character loops for 10-100x speedup.
Cross-Platform Hook Wrapper
Thehooks/run-hook.cmd script is a polyglot (runs on both Windows and Unix):
How the Polyglot Works
Windows Execution
Windows Execution
When
cmd.exe runs the script:- The
: << 'CMDBLOCK'is ignored (:is a no-op in batch) - Batch portion executes normally
- Searches for Git Bash in standard locations
- Falls back to
bashon PATH - If no bash found, exits silently (plugin still works without hooks)
Unix Execution
Unix Execution
When bash/sh runs the script:
- The
: << 'CMDBLOCK'starts a heredoc that ignores the batch portion CMDBLOCKcloses the heredoc- Unix portion executes normally
- Directly calls bash with the hook script
Why extensionless filenames? Claude Code’s Windows auto-detection prepends
bash to any command with .sh, which would double-invoke bash.Creating Custom Hooks
Step 1: Define Hook in hooks.json
Step 2: Create Hook Script
Createhooks/my-custom-hook (no extension):
Step 3: Make Executable
Step 4: Test Locally
Hook Output Format
Hooks must output valid JSON to stdout:Field Descriptions
Field Descriptions
additional_context: Text injected into agent context (Cursor format)hookSpecificOutput.hookEventName: Name of triggering eventhookSpecificOutput.additionalContext: Text injected into agent context (Claude Code format)- Custom fields: Platform-specific extensions
Platform Support
| Platform | Hook Support | SessionStart | Custom Hooks |
|---|---|---|---|
| Claude Code | ✅ Yes | ✅ Yes | ✅ Yes |
| Cursor | ✅ Yes | ✅ Yes | ✅ Yes |
| OpenCode | ❌ No | ✅ Via Plugin | ❌ No |
| Codex | ❌ No | ❌ No | ❌ No |
OpenCode alternative: Uses plugin system with
experimental.chat.system.transform hook to inject context at session start. See Plugin System.Debugging Hooks
Enable Verbose Logging
Add debugging to your hook script:Check Platform Logs
- Claude Code: Check debug logs via
/debug logs - Cursor: Check console in developer tools
- Manual test: Run hook directly:
./hooks/run-hook.cmd session-start
Common Issues
Hook doesn't run
Hook doesn't run
- Verify
hooks.jsonsyntax withjq - Check file permissions:
chmod +x hooks/session-start - Ensure matcher pattern matches session state
- Verify plugin is properly installed
Invalid JSON output
Invalid JSON output
- Test JSON with:
./hooks/run-hook.cmd session-start | jq . - Check for unescaped special characters
- Verify all quotes are properly escaped
- Ensure heredoc EOF is on its own line
Context not injected
Context not injected
- Verify both
additional_contextANDhookSpecificOutput.additionalContextare set - Check platform-specific field requirements
- Test with minimal content first
- Review platform logs for errors
Best Practices
Next Steps
Architecture
Understand overall system architecture
Plugin System
Learn how plugins integrate hooks
Creating Skills
Write custom skills that hooks can inject
Contributing
Contribute hook improvements