claude-hook-advisor 0.2.1

A Claude Code hook that provides intelligent command suggestions and semantic directory aliasing for enhanced AI-assisted development workflows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Hook processing logic

use crate::config::load_config;
use crate::directory::detect_directory_references;
use crate::history;
use crate::types::{Config, HookInput, HookOutput};
use anyhow::{Context, Result};
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::HashMap;
use std::io::{self, Read};
use std::path::PathBuf;
use std::sync::Mutex;

/// Cache for compiled regex patterns to avoid recompilation
static REGEX_CACHE: Lazy<Mutex<HashMap<String, Regex>>> = Lazy::new(|| Mutex::new(HashMap::new()));

/// Runs the application as a Claude Code hook for multiple event types.
/// 
/// Reads JSON input from stdin containing hook event data, loads the project
/// configuration, and processes based on the hook event type:
/// - PreToolUse: Command mapping and replacement suggestions
/// - UserPromptSubmit: Directory reference detection and learning
/// - PostToolUse: Command execution tracking and analysis
/// 
/// # Arguments
/// * `config_path` - Path to the .claude-hook-advisor.toml configuration file
/// * `replace_mode` - If true, returns "replace" decision; if false, returns "block"
/// 
/// # Returns
/// * `Ok(())` - Hook processing completed (may output to stdout)
/// * `Err` - If JSON parsing or configuration loading fails
pub fn run_as_hook(config_path: &str, replace_mode: bool) -> Result<()> {
    // Read configuration
    let config = load_config(config_path)?;

    // Read JSON input from stdin
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer)?;

    let hook_input: HookInput =
        serde_json::from_str(&buffer).context("Failed to parse hook input JSON")?;

    // Route to appropriate handler based on hook event type
    match hook_input.hook_event_name.as_str() {
        "PreToolUse" => handle_pre_tool_use(&config, &hook_input, replace_mode)?,
        "UserPromptSubmit" => handle_user_prompt_submit(&config, &hook_input)?,
        "PostToolUse" => handle_post_tool_use(&config, &hook_input)?,
        _ => {
            // Unknown hook event type, log warning and continue
            eprintln!("Warning: Unknown hook event type: {}", hook_input.hook_event_name);
        }
    }

    Ok(())
}

/// Handles PreToolUse hook events for command mapping and replacement.
/// 
/// Processes Bash commands and checks for configured mappings. If a mapping
/// is found, outputs JSON decision to block or replace the command.
/// 
/// # Arguments
/// * `config` - Configuration containing command mappings
/// * `hook_input` - Hook input data from Claude Code
/// * `replace_mode` - Whether to replace or block commands
/// 
/// # Returns
/// * `Ok(())` - Processing completed (may exit process with JSON output)
/// * `Err` - If command mapping check fails
fn handle_pre_tool_use(config: &Config, hook_input: &HookInput, replace_mode: bool) -> Result<()> {
    // Only process Bash commands
    if hook_input.tool_name.as_deref() != Some("Bash") {
        return Ok(());
    }

    let Some(tool_input) = &hook_input.tool_input else {
        return Ok(());
    };

    let Some(command) = &tool_input.command else {
        return Ok(());
    };

    // Check for command mappings
    if let Some((suggestion, replacement_cmd)) = check_command_mappings(config, command)? {
        let output = if replace_mode {
            HookOutput {
                decision: "replace".to_string(),
                reason: format!("Command mapped: using '{replacement_cmd}' instead"),
                replacement_command: Some(replacement_cmd),
            }
        } else {
            HookOutput {
                decision: "block".to_string(),
                reason: suggestion,
                replacement_command: None,
            }
        };

        println!("{}", serde_json::to_string(&output)?);
        std::process::exit(0);
    }

    Ok(())
}

/// Handles UserPromptSubmit hook events for directory reference detection.
/// 
/// Analyzes user prompts for semantic directory references and outputs
/// resolved canonical paths to help Claude Code understand directory context.
/// 
/// # Arguments
/// * `config` - Configuration containing directory mappings
/// * `hook_input` - Hook input data containing user prompt
/// 
/// # Returns
/// * `Ok(())` - Processing completed (may output directory resolutions)
/// * `Err` - If directory resolution fails
fn handle_user_prompt_submit(config: &Config, hook_input: &HookInput) -> Result<()> {
    let Some(prompt) = &hook_input.prompt else {
        return Ok(());
    };

    // Detect directory references in the prompt
    let directory_refs = detect_directory_references(config, prompt);
    
    if !directory_refs.is_empty() {
        // Output directory resolutions as plain text (not JSON for UserPromptSubmit)
        for resolution in directory_refs {
            println!("Directory reference '{}' resolved to: {}", 
                resolution.alias_used, 
                resolution.canonical_path
            );
            
            if !resolution.variables_substituted.is_empty() {
                println!("  Variables substituted: {:?}", resolution.variables_substituted);
            }
        }
    }

    Ok(())
}

/// Handles PostToolUse hook events for command execution tracking.
///
/// Analyzes command execution results and logs them to SQLite database if enabled.
/// Tracks command history, exit codes, and whether commands were replaced.
///
/// # Arguments
/// * `config` - Configuration for tracking settings
/// * `hook_input` - Hook input data containing execution results
///
/// # Returns
/// * `Ok(())` - Processing completed (may output analytics)
/// * `Err` - If execution tracking fails
fn handle_post_tool_use(config: &Config, hook_input: &HookInput) -> Result<()> {
    let Some(tool_name) = &hook_input.tool_name else {
        return Ok(());
    };

    let Some(tool_response) = &hook_input.tool_response else {
        return Ok(());
    };

    // Only track Bash command executions
    if tool_name != "Bash" {
        return Ok(());
    }

    // Check if command history is enabled
    let history_config = match &config.command_history {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(()), // History disabled, skip logging
    };

    // Get command details
    let Some(tool_input) = &hook_input.tool_input else {
        return Ok(());
    };

    let Some(command) = &tool_input.command else {
        return Ok(());
    };

    // Expand tilde in log file path
    let log_path = expand_tilde(&history_config.log_file)?;

    // Initialize database connection
    let conn = history::init_database(&log_path)
        .context("Failed to initialize command history database")?;

    // Check if this command was a replacement
    let (was_replaced, original_command) = if let Some((_, _)) = check_command_mappings(config, command)? {
        // This would have been replaced - find the original
        // For now, we don't have the original in PostToolUse, so we mark it as potentially replaced
        (false, None)
    } else {
        (false, None)
    };

    // Create and log the command record
    let record = history::create_record(
        &hook_input.session_id,
        command,
        tool_response.exit_code,
        hook_input.cwd.as_deref(),
        was_replaced,
        original_command,
    );

    history::log_command(&conn, &record)
        .context("Failed to log command to history")?;

    Ok(())
}

/// Expands tilde (~) in file paths to the user's home directory
fn expand_tilde(path: &str) -> Result<PathBuf> {
    if path.starts_with("~/") {
        let home = std::env::var("HOME")
            .context("HOME environment variable not set")?;
        Ok(PathBuf::from(path.replacen("~", &home, 1)))
    } else {
        Ok(PathBuf::from(path))
    }
}

/// Gets or creates a cached regex for the given pattern
fn get_cached_regex(pattern: &str) -> Result<Regex> {
    let mut cache = REGEX_CACHE.lock()
        .expect("regex cache mutex should not be poisoned");
    
    if let Some(regex) = cache.get(pattern) {
        return Ok(regex.clone());
    }
    
    let regex = Regex::new(pattern)?;
    cache.insert(pattern.to_string(), regex.clone());
    Ok(regex)
}

/// Checks if a command matches any configured mappings and generates suggestions.
///
/// Only matches the primary command at the start of the line (e.g., "npm" matches
/// "npm install" but NOT "my-npm-tool" or "npx npm"). This ensures command mappings
/// only apply to the main command being executed, not subcommands or arguments.
/// Returns the first matching pattern. Uses cached regex compilation for better performance.
///
/// # Arguments
/// * `config` - Configuration containing command mappings
/// * `command` - The bash command to check against mappings
///
/// # Returns
/// * `Ok(Some((suggestion, replacement)))` - If a mapping is found
/// * `Ok(None)` - If no mappings match the command
/// * `Err` - If regex compilation fails
pub fn check_command_mappings(config: &Config, command: &str) -> Result<Option<(String, String)>> {
    for (pattern, replacement) in &config.commands {
        // Create regex pattern that only matches at start of line
        // ^ = start of string (primary command position)
        // Group 1: the pattern to match
        // Group 2: (\s|$) = followed by whitespace or end of string
        // This ensures only the primary command is matched, not subcommands
        let regex_pattern = format!(r"^({})(\s|$)", regex::escape(pattern));
        let regex = get_cached_regex(&regex_pattern)?;

        if regex.is_match(command) {
            // Generate suggested replacement, preserving trailing whitespace
            let suggested_command = regex.replace_all(command, |caps: &regex::Captures| {
                format!("{}{}", replacement, &caps[2])
            });
            let suggestion = format!(
                "Command '{pattern}' is mapped to use '{replacement}' instead. Try: {suggested_command}"
            );
            return Ok(Some((suggestion, suggested_command.to_string())));
        }
    }

    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_command_mapping() {
        let mut commands = HashMap::new();
        commands.insert("npm".to_string(), "bun".to_string());
        commands.insert("yarn".to_string(), "bun".to_string());
        commands.insert("npx".to_string(), "bunx".to_string());

        let config = Config {
            commands,
            semantic_directories: HashMap::new(),
            command_history: None,
        };

        // Test npm mapping
        let result = check_command_mappings(&config, "npm install").unwrap();
        assert!(result.is_some());
        let (suggestion, replacement) = result.unwrap();
        assert!(suggestion.contains("bun install"));
        assert_eq!(replacement, "bun install");

        // Test yarn mapping
        let result = check_command_mappings(&config, "yarn start").unwrap();
        assert!(result.is_some());
        let (suggestion, replacement) = result.unwrap();
        assert!(suggestion.contains("bun start"));
        assert_eq!(replacement, "bun start");
    }

    #[test]
    fn test_command_mapping_edge_cases() {
        let mut commands = HashMap::new();
        commands.insert("npm".to_string(), "bun".to_string());
        let config = Config {
            commands,
            semantic_directories: HashMap::new(),
            command_history: None,
        };

        // Test whitespace boundaries - "npm" in "my-npm-tool" should NOT match
        // because it's not a standalone token (no whitespace separation)
        let result = check_command_mappings(&config, "my-npm-tool install").unwrap();
        assert!(result.is_none(), "npm in 'my-npm-tool' should NOT match");

        // Test empty command
        let result = check_command_mappings(&config, "").unwrap();
        assert!(result.is_none());

        // Test command with multiple spaces - should preserve spacing
        let result = check_command_mappings(&config, "npm   install   --verbose").unwrap();
        assert!(result.is_some());
        let (_, replacement) = result.unwrap();
        assert_eq!(replacement, "bun   install   --verbose");

        // Test npm NOT at start should NOT match (only matches primary command)
        let result = check_command_mappings(&config, "run npm").unwrap();
        assert!(result.is_none(), "'npm' in 'run npm' should NOT match (not primary command)");

        // Test npm-like substring should NOT match
        let result = check_command_mappings(&config, "npmc install").unwrap();
        assert!(result.is_none(), "'npmc' should NOT match 'npm'");

        // Test command by itself (no args)
        let result = check_command_mappings(&config, "npm").unwrap();
        assert!(result.is_some());
        let (_, replacement) = result.unwrap();
        assert_eq!(replacement, "bun");
    }

    #[test]
    fn test_command_mapping_prevents_false_positives() {
        let mut commands = HashMap::new();
        commands.insert("RM".to_string(), "rm -i".to_string());
        let config = Config {
            commands,
            semantic_directories: HashMap::new(),
            command_history: None,
        };

        // Test exact match
        let result = check_command_mappings(&config, "RM file.txt").unwrap();
        assert!(result.is_some());
        let (_, replacement) = result.unwrap();
        assert_eq!(replacement, "rm -i file.txt");

        // Test should NOT match when RM is part of a larger word
        let result = check_command_mappings(&config, "RMm file.txt").unwrap();
        assert!(result.is_none(), "'RMm' should NOT match 'RM'");

        // Test should NOT match when RM has prefix
        let result = check_command_mappings(&config, "gitRM file.txt").unwrap();
        assert!(result.is_none(), "'gitRM' should NOT match 'RM'");

        // Test should NOT match when RM is a subcommand (not at start)
        let result = check_command_mappings(&config, "git RM file.txt").unwrap();
        assert!(result.is_none(), "'RM' in 'git RM' should NOT match (not primary command)");

        // Test should NOT match when RM has hyphen prefix
        let result = check_command_mappings(&config, "git-RM file.txt").unwrap();
        assert!(result.is_none(), "'git-RM' should NOT match 'RM'");

        // Test should NOT match when RM has hyphen suffix
        let result = check_command_mappings(&config, "RM-tool file.txt").unwrap();
        assert!(result.is_none(), "'RM-tool' should NOT match 'RM'");
    }

    #[test]
    fn test_hook_output_serialization() {
        // Test blocking output
        let output = HookOutput {
            decision: "block".to_string(),
            reason: "Test reason".to_string(),
            replacement_command: Some("test command".to_string()),
        };
        
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("\"decision\":\"block\""));
        assert!(json.contains("\"reason\":\"Test reason\""));
        assert!(json.contains("\"replacement_command\":\"test command\""));

        // Test allowing output (no replacement)
        let output = HookOutput {
            decision: "allow".to_string(),
            reason: "No mapping found".to_string(),
            replacement_command: None,
        };
        
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("\"decision\":\"allow\""));
        assert!(json.contains("\"reason\":\"No mapping found\""));
        // Should not include replacement_command field when None due to serde skip
        assert!(!json.contains("replacement_command"));
    }
}