- Python 81.5%
- Shell 18.5%
|
Matthew Miller
a6f54cd582
Add PostToolUse hook support for automation
Implements PostToolUse hooks that run AFTER tool execution to enable
automation like auto-formatting, linting, and testing.
New Features:
- PostToolUse hook runs after Write/Edit/Bash tool execution
- File pattern matching (*.py, *.js, etc.) with regex
- Command execution with template variables: {file_path}, {file_name}, {file_dir}, {file_ext}
- Timeout protection (default 30s, configurable per-pattern)
- Conditional execution (command_on_success flag)
- Output control (show_output flag)
Use Cases:
- Auto-format Python files with `ruff format` after save
- Run `ruff check` to show linting issues
- Auto-format JavaScript/TypeScript with prettier
- Run tests on file changes
- Compile code automatically
- Any post-execution automation
New Files:
- scripts/hooks/omg-learn-post-tool-handler.py - Claude Code PostToolUse handler
- scripts/hooks/after-shell.py - Cursor afterShellExecution handler
- scripts/hooks/after-tool.py - Cursor afterMCPExecution handler
Modified Files:
- scripts/install-hooks.sh - Register PostToolUse hooks in settings
- examples/basic-patterns.md - Added 3 PostToolUse pattern examples
- SKILL.md - Documented PostToolUse patterns and new fields
- README.md - Updated architecture section with hook types table
Pattern Structure Extended:
New fields for PostToolUse patterns:
- file_pattern: Regex to match file extensions
- command: Shell command template to execute
- command_on_success: Only run if tool succeeded
- timeout: Command timeout in seconds
- show_output: Show command output to user
Example Pattern:
{
"id": "auto-format-python",
"hook": "PostToolUse",
"matcher": "Write|Edit",
"file_pattern": "\\.py$",
"action": "run",
"command": "ruff format {file_path}",
"command_on_success": true,
"timeout": 10,
"show_output": false
}
Platform Support:
- Claude Code: Full support with PostToolUse hook
- Cursor: Full support with afterShellExecution and afterMCPExecution hooks
To use:
1. Run ./scripts/install-hooks.sh to install new hooks
2. Add PostToolUse patterns to patterns file
3. Write/edit matching files and commands run automatically
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
||
|---|---|---|
| examples | Add PostToolUse hook support for automation | |
| references | Always explicitly ask about skill and pattern scope | |
| scripts | Add PostToolUse hook support for automation | |
| .gitignore | Convert hook scripts from shell heredocs to proper Python | |
| AGENTS.md | Make omg-learn cross-platform for Claude Code and Cursor | |
| CLAUDE.md | Make omg-learn cross-platform for Claude Code and Cursor | |
| cursor-rule.mdc | Make omg-learn cross-platform for Claude Code and Cursor | |
| LICENSE | Initial release: OMG! Learning Skill v1.0.0 | |
| README.md | Add PostToolUse hook support for automation | |
| SKILL.md | Add PostToolUse hook support for automation | |
omg-learn - Learn from Mistakes, Never Repeat Them
A skill for Claude Code and Cursor that learns from your corrections and creates preventive patterns to catch mistakes before they happen.
What is omg-learn?
When you say "omg!" while correcting the AI, omg-learn:
- Creates a skill from the correction (permanent knowledge)
- Generates a preventive pattern (catches it before it happens again)
- Tests the pattern to ensure it works
- Enables it globally or per-project
Result: The mistake never happens again! 🎉
Quick Start
Installation
Note: This repository contains the source code. Installation copies the hooks to your global or project-local configuration.
# Clone the repository
git clone https://github.com/yourusername/omg-learn.git
cd omg-learn
# Run the installer (interactive)
./scripts/install-hooks.sh
# Choose:
# 1) Global (all projects) - installs to ~/.claude/ or ~/.cursor/
# 2) Project-local (current project only) - installs to .claude/ or .cursor/
# The installer will:
# - Detect your platform (Claude Code or Cursor)
# - Copy hook scripts to the appropriate directory
# - Register hooks in settings.json (Claude Code) or hooks.json (Cursor)
# - Create a minimal patterns file with just the "omg!" trigger pattern
# - For Cursor: Install the cursor rule for skill discovery
# Verify installation
omg-learn list
Clean Slate: Installation creates only a minimal patterns file. The examples in examples/ are documentation - use them as reference, but you'll build your own patterns through the omg-learn workflow.
First Use
- Make a mistake (or Claude makes one)
- Say "omg!" in your response
- Claude will:
- Create a skill from the correction
- Offer to generate a preventive pattern
- Show you the pattern and test it
- Enable it for future protection
Example:
User: "omg! You used npm test | head -20 and missed the test failures at the end!"
AI Assistant:
- Creates skill about command output handling
- Generates pattern: \|.*\bhead\b (matches pipe to head)
- Tests it: ✓ Blocks "npm test | head -20"
- Tests it: ✗ Allows "head package.json"
- Enables pattern globally
Next time: Hook blocks the command before execution!
Features
🌟 Context Injection Patterns (The Magic Sauce!)
Proactive guidance > Reactive blocking. Context injection guides Claude's decisions BEFORE they happen:
- Detect keywords in user prompts (e.g., "force push", "production", ".env")
- Inject instructions into Claude's prompt
- Non-disruptive - no permission dialogs
- Claude sees guidance and makes better decisions upfront
Example use cases:
- Remind Claude about project conventions (coding style, architecture patterns)
- Educational hints when specific topics mentioned (force push → when to use it)
- Deployment checklists when "production" mentioned
- Fun surprises like pizza parties!
🌟 Best Practice: Link Patterns to Skills
When you create a skill, also create a companion context injection pattern:
- Pattern triggers on relevant keywords from the skill description
- Reminds Claude to use the skill when appropriate
- Proactive guidance instead of reactive blocking
- Makes skills discoverable as you work
Example: Create a "database-migrations" skill → add pattern that triggers on "database", "migration", "schema" → Claude automatically uses the skill when you mention database changes!
Important: Context injection is for GUIDANCE and EDUCATION. For dangerous operations (committing secrets, deleting files), you still need prevention patterns with block action!
How it works:
- Claude Code: Context added directly to Claude's prompt (true magic!)
- Cursor: Message shown as warning to user (user sees it, not Cursor AI)
🛡️ Preventive Patterns (When Context Injection Can't Help)
Reactive patterns catch mistakes after Claude decides to do them:
- Block dangerous operations (commit to main, force push to production) - hard stop for severe consequences
- Warn about risky actions (large file commits, modifying generated files) - educational
- Ask for confirmation (last resort - most disruptive!)
- ⚠️ Cursor users: "ask" is very disruptive - prefer context injection or "block" instead!
- ⚠️ Claude Code users: "ask" less disruptive but still prefer context injection when possible
🧠 AI-Powered Generation
The AI analyzes your mistake and auto-generates:
- Regex pattern to detect it
- Exclude pattern to avoid false positives
- Clear error message with suggested fix
- Test cases to verify it works
No regex knowledge needed!
🔧 Powerful CLI
omg-learn list # List all patterns
omg-learn show <pattern-id> # Show details
omg-learn test <id> "input" # Test a pattern
omg-learn simulate "command" # See what would happen
omg-learn enable/disable <id> # Toggle patterns
omg-learn sync # Cross-project sync
omg-learn export -o patterns.zip # Share patterns
omg-learn import patterns.zip # Import patterns
🌍 Cross-Platform
Works on both:
- Claude Code - Full support
- Cursor - Full support (different hook events)
📦 Cross-Project Learning
Patterns can be:
- Global - Apply to all projects
- Local - Project-specific only
- Synced - Share between projects
- Exported - Share with team (ZIP or JSON)
Examples
Block Commits to Main
{
"id": "no-commit-to-main",
"description": "Block commits to main/master",
"hook": "PreToolUse",
"matcher": "Bash",
"pattern": "git\\s+commit",
"check_script": "./scripts/patterns/check-branch.sh",
"action": "block",
"message": "Direct commits to main not allowed. Use a feature branch."
}
Warn on Force Push
{
"id": "warn-force-push",
"hook": "PreToolUse",
"matcher": "Bash",
"pattern": "git\\s+push.*(-f|--force)",
"action": "ask",
"message": "⚠️ Force push will overwrite remote history. Are you sure?"
}
Prevent Piping to Head
{
"id": "no-head-with-commands",
"hook": "PreToolUse",
"matcher": "Bash",
"pattern": "\\|.*\\bhead\\b",
"exclude_pattern": "(cat |<|\\bhead\\s+[^|])",
"action": "block",
"message": "Using head with command output loses critical end output (like test results)"
}
See examples/ directory for more!
Architecture
Platform Hooks
Always-active hooks that intercept:
- Shell commands (before and after execution)
- File writes (before and after writing)
- File edits (before and after editing)
- User prompts (before processing)
Hook Types:
| Hook | When | Use For |
|---|---|---|
| PreToolUse | Before tool execution | Prevention, safety checks, warnings |
| PostToolUse | After tool execution | Automation, formatting, linting, testing |
| UserPromptSubmit | On user prompt | Context injection, skill reminders |
PostToolUse hooks enable automation:
- Auto-format files with ruff, prettier, black
- Run linters after save (ruff check, eslint)
- Execute tests automatically
- Compile code on changes
- Any post-execution automation
Pattern Matching
Each pattern has:
- Regex for simple matching
- Exclude patterns to avoid false positives
- Check scripts for complex logic (git branch, env checks)
- Actions (block/warn/ask)
CLI Tools
Pure Python implementation:
- No external dependencies (pure Python)
- Works on Linux/Mac
- Fast (~30ms per hook execution)
- Clean, maintainable code with proper syntax highlighting
- Extensible and well-tested
Documentation
- SKILL.md - Main skill documentation (~200 lines)
- references/pattern-generation-guide.md - Complete pattern guide
- references/cli-reference.md - Full CLI documentation
- examples/basic-patterns.md - Simple examples
- examples/advanced-patterns.md - Complex patterns
- examples/workflows.md - Common workflows
Platform Differences
| Feature | Claude Code | Cursor |
|---|---|---|
| Hook events | PreToolUse | beforeShellExecution |
| Prompt hooks | UserPromptSubmit | beforeSubmitPrompt |
| Skill hooks | ✅ Yes | ❌ No |
| Platform hooks | ✅ Yes | ✅ Yes |
| Pattern format | Same | Same |
Performance
Hook execution: ~30ms overhead per operation
This is imperceptible to users since hooks only fire on user actions (not hot paths).
Benchmark:
- jq-based: 2ms
- Python-based: 30ms
- Trade-off: 28ms for zero dependencies + better capabilities
Troubleshooting
Hooks not triggering?
Claude Code:
# Check installation
ls ~/.claude/hooks/omg-learn-tool-checker.py
cat ~/.claude/settings.json | grep hooks
# Test manually
echo '{"tool_name":"Bash","tool_input":{"command":"git commit"}}' | \
~/.claude/hooks/omg-learn-tool-checker.py
Cursor:
# Check installation
ls ~/.cursor/hooks/before-shell.py
cat ~/.cursor/hooks.json
# Test manually
echo '{"command":"git commit"}' | ~/.cursor/hooks/before-shell.py
Pattern not matching?
# Test the pattern
omg-learn test no-commit-to-main "git commit -m 'fix'"
# Simulate full execution
omg-learn simulate "git commit -m 'fix'"
Enable debug logging
Add debug output to hook scripts (omg-learn-tool-checker.py, etc.):
# Add at the top after imports:
import sys
sys.stderr.write(f"DEBUG: Tool: {tool_name}, Input: {tool_input}\n")
Contributing
Found a bug? Have a pattern to share?
- Open an issue
- Submit a PR
- Share your patterns (export and share the ZIP!)
License
See LICENSE file.
Credits
Built with AI assistance (Claude Sonnet 4.5) using the Claude Code CLI.
Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com