ruvnet / ruvnet/ruflo

Claude Flow Hooks System - Automated Lifecycle Management with Claude Code Integration

Open
#145 6 comments 0 reactions 0 assignees View on GitHub
documentation enhancement
Dominant language
TypeScript
Stars
72.7k
Forks
8.6k
Avg merge
3d 3h
Merged PRs (30d)
85

Description

## 🎯 Overview

Claude Flow now includes a comprehensive hooks system that integrates seamlessly with Claude Code's lifecycle events. This system enables automated coordination, memory management, and performance optimization throughout your development workflow.

## 🔗 What Are Hooks?

Hooks are automated commands that execute at specific points in Claude Code's lifecycle. They provide deterministic control over Claude Code's behavior without interrupting your workflow.

## 🚀 Key Features

### Hook Types Available:

1. **PreToolUse Hooks** - Execute BEFORE Claude Code runs a tool:
- **Edit/MultiEdit**: Validates syntax, assigns agents
- **Write**: Creates directories, checks permissions
- **Bash**: Validates command safety
- **Task**: Optimizes topology, spawns agents

2. **PostToolUse Hooks** - Execute AFTER tool completion:
- **File Operations**: Formats code, updates memory
- **Commands**: Tracks metrics, stores results
- **Tasks**: Analyzes performance, updates telemetry
- **Searches**: Caches results, optimizes patterns

3. **Notification Hooks** - Handle system notifications
4. **Stop Hooks** - Execute when session ends
5. **SubagentStop Hooks** - Execute when agents complete

## 📦 Installation & Setup

### Quick Start
Hooks are automatically configured when you initialize a Claude Flow project:

```bash
npx claude-flow init --sparc
```

This creates a `.claude/settings.json` file with pre-configured hooks.

### Manual Configuration
Add to your `.claude/settings.json`:

```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit < /dev/null | MultiEdit",
"hooks": [{
"type": "command",
"command": "npx claude-flow hooks pre-edit --file \"${CLAUDE_FLOW_FILE}\" --validate-syntax --auto-assign-agents",
"blocking": false
}]
}
],
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [{
"type": "command",
"command": "npx claude-flow hooks post-edit --file \"${CLAUDE_FLOW_FILE}\" --format-code --update-memory --train-neural"
}]
}
]
}
}
```

## 🎨 Available Hook Commands

### Pre-Task Hook
```bash
npx claude-flow hooks pre-task --description "Build API" --task-id task-123 --auto-spawn-agents
```

### Post-Task Hook
```bash
npx claude-flow hooks post-task --task-id task-123 --analyze-performance --generate-insights
```

### Pre-Edit Hook
```bash
npx claude-flow hooks pre-edit --file "src/api.js" --validate-syntax
```

### Post-Edit Hook
```bash
npx claude-flow hooks post-edit --file "src/api.js" --memory-key "swarm/edits/123"
```

### Session-End Hook
```bash
npx claude-flow hooks session-end --export-metrics --generate-summary
```

## 🔧 Environment Variables

Hooks have access to context-specific environment variables:
- `${CLAUDE_FLOW_FILE}` - The file being operated on
- `${CLAUDE_FLOW_COMMAND}` - The bash command being executed
- `${CLAUDE_FLOW_TASK}` - The task description
- `${CLAUDE_FLOW_TASK_ID}` - Unique task identifier
- `${CLAUDE_FLOW_PATTERN}` - Search pattern used
- `${CLAUDE_FLOW_MESSAGE}` - Notification message
- `${CLAUDE_FLOW_AGENT_ID}` - Agent identifier

## ⚡ Benefits

1. **Automated Workflows**: No manual intervention needed
2. **Performance Tracking**: Real-time metrics collection
3. **Error Prevention**: Validation before operations
4. **Memory Persistence**: Context maintained across sessions
5. **Neural Learning**: Patterns improve over time
6. **Non-Blocking**: Most hooks run in parallel

## 🛡️ Security & Performance

- **Blocking vs Non-Blocking**: Critical operations (like command validation) can block, while others run in background
- **Full Permissions**: Hooks run with user permissions
- **Caching**: Improves performance over time
- **Async Operations**: Neural training happens asynchronously

## 📚 Documentation

For detailed documentation, see:
- [Hooks Overview](.claude/commands/hooks-overview.md)
- [Hooks Documentation](.claude/commands/hooks/hooks-documentation.md)

## 🚨 Important Notes

1. Hooks are enabled by default in new projects
2. To disable temporarily: `export CLAUDE_FLOW_HOOKS_ENABLED=false`
3. Test individual hooks: `npx claude-flow hooks pre-edit --file test.js`
4. View hook logs: `npx claude-flow logs hooks --tail 50`

## 🎯 Use Cases

- **Auto-format code** on save
- **Validate commands** before execution
- **Auto-spawn agents** for complex tasks
- **Track performance** metrics
- **Persist memory** across sessions
- **Train neural patterns** from successful operations

This hooks system represents a major step forward in automated AI-driven development workflows!

Contributor guide

Open the contributing guide

Research direction

The issue points to .claude/settings.json, .claude/commands/hooks-overview.md, and .claude/commands/hooks/hooks-documentation.md. Start by reading those files and tracing the npx claude-flow init --sparc entry point. The issue describes a broad hooks system but does not define a specific change or completion criteria, so the intended scope needs clarification first.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.