Hooks System
AgileFlow leverages Claude Code's official hooks system to enable event-driven automation in your development workflow.
What Are Hooks?
Hooks are automatic triggers that execute commands when specific events occur during your Claude Code session:
| Event | When It Fires | Use Case |
|---|---|---|
SessionStart | When Claude Code session begins | Display status, load context |
PreCompact | Before conversation is compacted | Preserve critical context |
UserPromptSubmit | After you submit a prompt | Activity logging |
Stop | When Claude stops responding | Cleanup, validation |
PreToolUse | Before a tool is used | Block dangerous commands |
PostToolUse | After a tool completes | Validation, logging |
Quick Start
1. Create Configuration Directory
mkdir -p .claude2. Add Hooks to Settings
Create or edit .claude/settings.json:
{
"hooks": {
"SessionStart": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "bash scripts/agileflow-statusline.sh"
}]
}]
}
}3. Restart Claude Code
Hooks take effect on the next session start. Use Cmd/Ctrl + Shift + P and select "Reload Window" in VS Code-based editors.
Example Configurations
Welcome Message on Session Start
Display a helpful welcome message when you start working:
{
"hooks": {
"SessionStart": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "echo '\\n Welcome! Type /agileflow:help for available commands\\n'"
}]
}]
}
}Project Status Display
Show current story, WIP count, and git branch:
{
"hooks": {
"SessionStart": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "bash scripts/agileflow-statusline.sh"
}]
}]
}
}Auto-Archive Completed Stories
Clean up old completed stories automatically:
{
"hooks": {
"SessionStart": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "bash scripts/archive-completed-stories.sh"
}]
}]
}
}Context Preservation During Compaction
Preserve critical state when Claude Code compresses the conversation:
{
"hooks": {
"PreCompact": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "bash scripts/precompact-context.sh"
}]
}]
}
}Activity Logging
Track all prompts for later review:
{
"hooks": {
"UserPromptSubmit": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "echo \"[$(date '+%Y-%m-%d %H:%M:%S')] Prompt submitted\" >> .claude/activity.log"
}]
}]
}
}Block Dangerous Commands
Prevent destructive operations with PreToolUse hooks:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "node scripts/damage-control/bash-tool-damage-control.js"
}]
}]
}
}See Damage Control for more safety options.
Configuration Files
AgileFlow uses a two-tier configuration system:
| File | Purpose | Version Control |
|---|---|---|
.claude/settings.json | Project-level defaults | Committed to git |
.claude/settings.local.json | User-specific overrides | Gitignored |
User-specific settings in settings.local.json override project settings, allowing team members to customize their experience without affecting others.
AgileFlow Hook Scripts
AgileFlow provides ready-to-use scripts in scripts/:
| Script | Purpose |
|---|---|
agileflow-statusline.sh | Display project status (branch, story, WIP count) |
archive-completed-stories.sh | Auto-archive completed stories older than 30 days |
precompact-context.sh | Preserve critical context during conversation compaction |
clear-active-command.js | Clear active command state on session start |
get-env.js | Output environment information for status line |
agileflow-welcome.js | Display welcome message with project info |
Setting Up Hooks with AgileFlow
The easiest way to configure hooks is through AgileFlow's interactive configuration:
/agileflow:configure
Select Hooks to set up event-driven automation with guided prompts.
Or use the specific configuration agent:
/agileflow:agents:configuration:hooks
Hook Configuration Schema
{
"hooks": {
"EventName": [{
"matcher": "ToolName or *",
"hooks": [{
"type": "command",
"command": "shell command to execute"
}]
}]
}
}Fields:
EventName- One of: SessionStart, PreCompact, UserPromptSubmit, Stop, PreToolUse, PostToolUsematcher- Tool name for PreToolUse/PostToolUse, or*for other eventstype- Currently onlycommandis supportedcommand- Shell command to execute
Troubleshooting
Hook Not Running
- Check syntax - Validate JSON in
.claude/settings.json - Restart session - Hooks only load on session start
- Check permissions - Ensure scripts are executable (
chmod +x scripts/*.sh) - Check logs - Look in
.claude/hook.logfor errors
Hook Running Slowly
- Keep hooks fast - SessionStart hooks should complete in under 5 seconds
- Use background execution - For slow operations, run asynchronously
- Cache results - Use file-based caching for expensive operations
PreCompact Not Preserving Context
- Verify script exists - Check
scripts/precompact-context.shis present - Check output format - Script must output valid context format
- Review session-state.json - Ensure active commands are tracked
Related Documentation
- Configuration System - Interactive setup
- Damage Control - PreToolUse safety hooks
- Compact Context - Context preservation details
- Parallel Sessions - Session management
On This Page
Hooks SystemWhat Are Hooks?Quick Start1. Create Configuration Directory2. Add Hooks to Settings3. Restart Claude CodeExample ConfigurationsWelcome Message on Session StartProject Status DisplayAuto-Archive Completed StoriesContext Preservation During CompactionActivity LoggingBlock Dangerous CommandsConfiguration FilesAgileFlow Hook ScriptsSetting Up Hooks with AgileFlowHook Configuration SchemaTroubleshootingHook Not RunningHook Running SlowlyPreCompact Not Preserving ContextRelated Documentation