Claude Hook Advisor
A Rust CLI tool that integrates with Claude Code using a triple-hook architecture to provide intelligent command suggestions and semantic directory aliasing. Enhance your development workflow with automatic command mapping and natural language directory references.
🎬 What You'll Experience
Once installed, claude-hook-advisor works invisibly in your Claude Code conversations:
Directory Aliasing Magic ✨
You type: "What files are in my docs?" Claude responds: "I'll check what files are in your docs directory at /Users/you/Documents/Documentation."
Behind the scenes, you'll see:
<user-prompt-submit-hook>Directory reference 'docs' resolved to: /Users/you/Documents/Documentation</user-prompt-submit-hook>
You type: "Check the project_docs for API documentation"
Claude automatically knows: Uses /Users/you/Documents/Documentation/my-project/ without you typing the full path
Command Intelligence in Action 🚀
Claude tries to run: npm install
Tool intervenes: Suggests bun install based on your configuration
Claude automatically runs: bun install with no manual intervention
You see: Claude seamlessly uses your preferred tools without you having to correct it every time.
The Magic is Invisible
- No extra commands to remember
- No interruptions to your workflow
- Natural language directory references just work
- Your preferred tools are used automatically
- All happens transparently in Claude Code conversations
Features
🎯 Command Intelligence
- Smart command mapping: Map any command to preferred alternatives with regex support
- Per-project configuration: Each project can have its own
.claude-hook-advisor.tomlfile - Triple-hook integration: PreToolUse, UserPromptSubmit, and PostToolUse hooks
📁 Semantic Directory Aliasing
- Natural language directory references: Use "docs", "central_docs", "project_docs" in conversations
- Simple path mapping: Direct alias-to-path mapping with tilde expansion
- Automatic resolution: Claude Code automatically resolves semantic references to canonical paths
- TOML configuration: Simple configuration file-based setup
📊 Command History Tracking
- Persistent Bash history: All commands Claude runs are logged to SQLite database
- Never lose a command: Commands run by Claude don't appear in your shell history, but now you can retrieve them
- Powerful querying: Filter by session, command pattern, exit code, or time range
- Audit trail: Track what Claude actually executed for debugging and compliance
🚀 Performance & Security
- Fast and lightweight: Built in Rust for optimal performance
- Path canonicalization: Security against directory traversal attacks
- Graceful error handling: Robust fallback mechanisms
Installation
From crates.io (Recommended)
Install directly from crates.io using cargo:
This installs the binary to ~/.cargo/bin/claude-hook-advisor (make sure ~/.cargo/bin is in your PATH).
From Source
Quick Start
1. Install and Configure Hooks
# Install the binary
# Automatically install hooks into Claude Code (creates backups)
# Remove hooks if needed (with backup)
2. Configure Directory Aliases
Edit your .claude-hook-advisor.toml file to set up directory aliases:
# Semantic directory aliases - use natural language!
[]
= "~/Documents/Documentation/my-project"
= "~/Documents/Documentation"
= "~/Documents/Documentation/claude"
= "~/Documents/test-data"
Pro tip: Use quoted, space-separated aliases for natural conversation:
- "Check the project docs folder" → matches
"project docs" - "Look in test data directory" → matches
"test data"
3. Configure Command Mappings
Create a .claude-hook-advisor.toml file in your project root:
# Command mappings
[]
= "bun"
= "bun"
= "bunx"
= "wget --verbose"
# Semantic directory aliases - natural language
[]
= "~/Documents/Documentation/my-project"
= "~/Documents/Documentation"
= "~/Documents/Documentation/claude"
4. (Optional) Enable Command History Tracking
Track all commands Claude runs to a SQLite database:
[]
= true
= "~/.claude-hook-advisor/bash-history.db"
Then view history anytime with: claude-hook-advisor --history
See the Command History Tracking section for full details.
Example Configurations
Node.js project (prefer bun):
[]
= "bun"
= "bun"
= "bunx"
Python project (prefer uv):
[]
= "uv pip"
= "uv add"
General preferences:
[]
= "wget --verbose"
= "bat"
= "eza"
Claude Code Integration
Automatic Installation (Recommended)
This automatically configures all three hooks:
- PreToolUse: Command suggestion and blocking
- UserPromptSubmit: Directory reference detection
- PostToolUse: Analytics and execution tracking
Manual Configuration
If you prefer manual setup, add to your .claude/settings.json:
Note: This assumes claude-hook-advisor is in your PATH. After cargo install, the binary is typically located at ~/.cargo/bin/claude-hook-advisor.
How It Works
Command Intelligence (PreToolUse Hook) 🚦
The Flow:
- Command Detection: When Claude Code tries to run a Bash command, the hook receives JSON input
- Configuration Loading: The tool loads
.claude-hook-advisor.tomlfrom the current directory - Pattern Matching: Matches only the primary command at the start of the line (e.g.,
npmmatchesnpm installbut notnpx npmormy-npm-tool) - Suggestion Generation: If a match is found, returns a blocking response with the suggested replacement
- Claude Integration: Claude receives the suggestion and automatically retries with the correct command
Behind the Scenes:
// Simplified code flow
let config = load_config?;
let command = parse_bash_command;
if let Some = config.commands.get
What makes it smart:
- Start-of-line matching ensures only primary commands are replaced (e.g.,
npm install→bun install, butnpx npmis unchanged) - Prevents false positives with substrings (e.g.,
npmwon't matchnpm-checkormy-npm-tool) - Doesn't interfere with subcommands (e.g.,
git rmwon't trigger anrmmapping) - Preserves command arguments (
npm install --save→bun install --save) - Fast regex-based pattern matching (~1ms response time)
Directory Aliasing (UserPromptSubmit Hook) 📁
The Flow:
- Text Analysis: Scans user prompts for semantic directory references (e.g., "docs", "project_docs")
- Pattern Recognition: Uses regex to detect directory aliases in natural language
- Path Expansion: Expands tilde (~) to user home directory
- Path Resolution: Converts semantic references to canonical filesystem paths
- Security Validation: Performs path canonicalization to prevent traversal attacks
Behind the Scenes:
// Pattern detection
let patterns = ;
// Tilde expansion
let resolved = expand_tilde?;
// Security canonicalization
let canonical = canonicalize?;
What makes it secure:
- Path canonicalization prevents
../../../etc/passwdattacks - Only resolves to configured directories
- Validates paths exist before resolution
Analytics (PostToolUse Hook) 📊
The Flow:
- Execution Tracking: Receives command results with success/failure data
- Performance Monitoring: Tracks command success rates and execution patterns
- Analytics Logging: Provides insights for optimization and monitoring
Behind the Scenes:
// Success/failure tracking
match hook_data.tool_response.exit_code
Future possibilities:
- Command success rate analytics
- Performance optimization suggestions
- Usage pattern insights
Command History Tracking
Track every Bash command Claude runs in a SQLite database. Commands executed by Claude don't show up in your shell's history, but now you can retrieve them anytime.
Setup
1. Install/Update the binary:
If you just cloned or pulled the latest code:
If you installed from crates.io, the feature is already available in v0.2.0+.
2. Enable command history:
Edit your .claude-hook-advisor.toml and add:
[]
= true
= "~/.claude-hook-advisor/bash-history.db"
Or if you don't have a config file yet, run:
This creates a config file with command history as a commented example you can uncomment.
3. That's it!
The PostToolUse hook (already installed if you ran --install before) will automatically start logging commands. No restart needed!
Viewing History
Show recent commands:
Show last 50 commands:
Show only failed commands:
Show git commands only:
Show commands from a specific session:
What Gets Logged
Each command record includes:
- Timestamp: When the command was executed
- Command: The exact command that ran
- Exit code: Success (0) or failure code
- Working directory: Where the command was executed
- Session ID: Link commands to Claude Code sessions
Example Output
Command History (5 records)
================================================================================
2025-11-10T14:30:22Z ✓
Command: git status
CWD: /home/user/my-project
Session: abc123-def456
2025-11-10T14:30:25Z ✓
Command: cargo test
CWD: /home/user/my-project
Session: abc123-def456
2025-11-10T14:30:30Z ✗ (exit: 1)
Command: npm test
CWD: /home/user/my-project
Session: abc123-def456
Use Cases
- Retrieve that perfect command: "What was that complex curl command Claude ran yesterday?"
- Debugging failures: "Which commands failed in this session?"
- Audit trail: Track all commands for compliance or security review
- Learning: See what commands Claude uses to solve problems
Example Output
Real Claude Code Conversation
Here's what an actual conversation looks like with claude-hook-advisor working:
🗣️ You: "What files are in my docs?"
🤖 Claude: "⏺ I'll check what files are in your docs directory at /Users/you/Documents/Documentation."
Behind the scenes:
[DEBUG] UserPromptSubmit hook triggered
[DEBUG] Pattern matched: 'docs' -> '~/Documents/Documentation'
[DEBUG] Path resolved: /Users/you/Documents/Documentation
Hook message in Claude:
<user-prompt-submit-hook>Directory reference 'docs' resolved to: /Users/you/Documents/Documentation</user-prompt-submit-hook>
🗣️ You: "Install the dependencies for this project"
🤖 Claude: "I'll install the dependencies using npm install."
(Claude attempts: npm install)
Hook intercepts:
🤖 Claude: "I'll use bun install instead based on your project preferences."
(Claude runs: bun install)
Result: Your preferred package manager is used automatically, no manual correction needed!
Command Line Testing
Directory Resolution:
# Test directory resolution via hook
|
# Expected output:
# Directory reference 'docs' resolved to: /Users/you/Documents/Documentation
Hook Simulation:
|
<user-prompt-submit-hook>Directory
|
{
}
Development
Available Make Targets
Testing
# Run unit tests
# Test with example npm command
# Manual testing - Command mapping (PreToolUse)
|
# Manual testing - Directory detection (UserPromptSubmit)
|
# Manual testing - Analytics (PostToolUse)
|
# Test directory resolution with existing config
|
🔧 Troubleshooting & Debug
Understanding Hook Messages
When claude-hook-advisor is working correctly, you'll see these messages in Claude Code:
Directory Resolution:
<user-prompt-submit-hook>Directory reference 'docs' resolved to: /Users/you/Documents/Documentation</user-prompt-submit-hook>
Command Suggestions:
<pre-tool-use-hook>Command 'npm' mapped to 'bun'. Suggested: bun install</pre-tool-use-hook>
Execution Tracking:
<post-tool-use-hook>Command 'bun install' completed successfully (exit code: 0)</post-tool-use-hook>
Debug Mode
Enable detailed logging to see what's happening behind the scenes:
# Add RUST_LOG=debug to your Claude Code settings
{
}
Debug output shows:
- Configuration file loading
- Pattern matching details
- Path resolution steps
- Variable substitution
- Security validation
Common Issues & Solutions
🚫 Hooks Not Triggering
Problem: No hook messages appear in Claude Code conversations
Solutions:
- Verify hook installation by checking your Claude Code settings file
- Check
.claude/settings.jsonor.claude/settings.local.json: - Ensure
claude-hook-advisoris in your PATH:which claude-hook-advisor - Test manually:
echo '{"session_id":"test","hook_event_name":"UserPromptSubmit","prompt":"check docs"}' | claude-hook-advisor --hook
📁 Directory Not Resolved
Problem: "docs" doesn't resolve to the expected path
Solutions:
- Check configuration file exists:
ls .claude-hook-advisor.toml - Verify alias configuration:
[] = "~/Documents/Documentation" - Test resolution via hook:
echo '{"session_id":"test","hook_event_name":"UserPromptSubmit","prompt":"check docs"}' | claude-hook-advisor --hook - Check file permissions:
ls -la .claude-hook-advisor.toml
⚙️ Commands Not Being Mapped
Problem: npm still runs instead of bun
Solutions:
- Verify command mapping in config:
[] = "bun" - Test mapping:
echo '{"session_id":"test","hook_event_name":"PreToolUse","tool_name":"Bash","tool_input":{"command":"npm install"}}' | claude-hook-advisor --hook - Remember: Commands only match at the start of the line (by design):
- ✅
npm installmatches and becomesbun install - ❌
npx npmwon't match (npm is not the primary command) - ❌
npm-checkwon't match (different command)
- ✅
- Add debug logging to see pattern matching
🔒 Permission Issues
Problem: Hook fails with permission errors
Solutions:
- Make binary executable:
chmod +x ~/.cargo/bin/claude-hook-advisor - Check file ownership:
ls -la ~/.cargo/bin/claude-hook-advisor - Verify PATH includes
~/.cargo/bin:echo $PATH
🐛 Debugging Your Configuration
Test each component individually:
# Test directory resolution via hook
|
# Test command mapping
|
# Test user prompt analysis
|
# Check configuration by testing resolution
|
Performance Notes
- Startup time: ~1-5ms per hook call
- Memory usage: ~2-3MB per process
- File watching: Configuration is loaded on each hook call (no caching)
- Path resolution: Uses filesystem canonicalization for security
Configuration File Lookup
The tool looks for configuration files in this order:
- Custom path specified with
-c/--configflag .claude-hook-advisor.tomlin current directory- If no config found, allows all commands (no mappings)
Use Cases
Command Intelligence
- Package Manager Consistency: Enforce use of
buninstead ofnpm/yarn - Tool Preferences: Replace
curlwithwget,catwithbat, etc. - Project Standards: Ensure consistent tooling across team members
- Legacy Migration: Gradually move from old tools to new ones
- Security Policies: Block dangerous commands or redirect to safer alternatives
Directory Aliasing
- Documentation Management: Use "docs" instead of typing full paths
- Project Organization: Reference "project_docs", "central_docs" naturally
- Cross-Platform Paths: Abstract away platform-specific directory structures
- Team Collaboration: Shared semantic directory references across team members
- Workflow Automation: Natural language directory references in Claude conversations
Similar Tools
This tool is inspired by and similar to:
- Shell aliases (but works at the Claude Code level)
- Git hooks (but for command execution)
- Package manager configuration files
Support
If you find this tool useful, consider supporting its development: