|
| 1 | +# Agent Guidelines for aihook |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +`aihook` is a validator for Claude Code hooks that enforces shell scripting best practices. The primary use case is validating that `cd` commands are always executed within subshells. |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +``` |
| 10 | +aihook/ |
| 11 | +├── main.go # CLI entry point (Cobra setup) |
| 12 | +├── pkg/ |
| 13 | +│ └── validator/ |
| 14 | +│ ├── validator.go # Core validation logic |
| 15 | +│ └── validator_test.go # Comprehensive unit tests |
| 16 | +├── README.md # User documentation |
| 17 | +└── AGENTS.md # This file |
| 18 | +``` |
| 19 | + |
| 20 | +**Separation of concerns:** |
| 21 | +- `main.go`: Cobra CLI setup, flag handling, output formatting |
| 22 | +- `pkg/validator`: Pure validation logic (AST walking, cd detection) |
| 23 | +- Tests are in the validator package, testing the pure logic |
| 24 | + |
| 25 | +## Development Guidelines |
| 26 | + |
| 27 | +### Building and Testing |
| 28 | + |
| 29 | +Always run tests and build from the repository root: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Run tests |
| 33 | +go test ./aihook/... |
| 34 | + |
| 35 | +# Build |
| 36 | +go build ./aihook |
| 37 | + |
| 38 | +# Run |
| 39 | +./aihook stop < script.sh |
| 40 | +``` |
| 41 | + |
| 42 | +### Code Structure |
| 43 | + |
| 44 | +- `main.go` (86 lines) - CLI entry point with Cobra framework |
| 45 | + - Command definitions and flag handling |
| 46 | + - Output formatting (regular and --claude JSON) |
| 47 | + - Calls into validator package for logic |
| 48 | + |
| 49 | +- `pkg/validator/validator.go` (106 lines) - Core validation logic |
| 50 | + - `Validator` type with `ValidateScript()` method |
| 51 | + - AST walking to detect cd commands |
| 52 | + - Tracks subshell context (both `(...)` and `$(...)`) |
| 53 | + - `FormatViolations()` for user-friendly error messages |
| 54 | + |
| 55 | +- `pkg/validator/validator_test.go` (330 lines) - Comprehensive tests |
| 56 | + - 41 test scenarios covering all edge cases |
| 57 | + - Tests the validator package directly |
| 58 | + |
| 59 | +### Key Implementation Details |
| 60 | + |
| 61 | +1. **Shell Parsing**: Uses `mvdan.cc/sh/v3/syntax` for accurate shell script parsing |
| 62 | +2. **AST Walking**: Traverses the syntax tree to find `cd` commands and track subshell context |
| 63 | +3. **Subshell Detection**: Handles both explicit subshells `(...)` and command substitutions `$(...)` |
| 64 | +4. **Output Formats**: Supports both human-readable and Claude Code JSON formats |
| 65 | + |
| 66 | +### Testing Philosophy |
| 67 | + |
| 68 | +Tests cover: |
| 69 | +- Basic cd detection (inside and outside subshells) |
| 70 | +- Complex nesting scenarios |
| 71 | +- Command substitution |
| 72 | +- Edge cases (loops, conditionals, functions) |
| 73 | +- Invalid shell syntax |
| 74 | +- Multiple cd commands in one script |
| 75 | + |
| 76 | +All tests must pass before any changes are committed. |
| 77 | + |
| 78 | +### Common Pitfalls |
| 79 | + |
| 80 | +1. **Arithmetic Expansion**: `$((...))` is NOT a subshell, it's arithmetic expansion |
| 81 | +2. **Command Substitution**: `$(...)` IS a subshell and cd is allowed inside |
| 82 | +3. **Here Documents**: Content inside here-docs should not be parsed as commands |
| 83 | + |
| 84 | +### Claude Code Hook Format |
| 85 | + |
| 86 | +When `--claude` flag is used, output must be JSON with: |
| 87 | +- `exit_code`: Integer (0 for success, 2 for violations, 1 for errors) |
| 88 | +- `message`: String containing the human-readable message |
| 89 | + |
| 90 | +Example: |
| 91 | +```json |
| 92 | +{ |
| 93 | + "exit_code": 2, |
| 94 | + "message": "Found cd commands outside subshells:\n Line 1: 'cd' command found outside subshell\n\nAll 'cd' commands must be in a subshell. Example:\n # Bad: cd /tmp && ls\n # Good: (cd /tmp && ls)\n" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Adding New Hook Types |
| 99 | + |
| 100 | +When adding new hook types in the future: |
| 101 | + |
| 102 | +1. Add a new subcommand in `main.go` |
| 103 | +2. Create a new validator in `pkg/validator` if needed |
| 104 | +3. Add comprehensive unit tests in the validator package |
| 105 | +4. Update README.md with usage examples |
| 106 | +5. Update this AGENTS.md with implementation notes |
| 107 | + |
| 108 | +## Dependencies |
| 109 | + |
| 110 | +- `github.com/spf13/cobra` - CLI framework |
| 111 | +- `mvdan.cc/sh/v3/syntax` - Shell script parser |
| 112 | +- `github.com/neongreen/mono/lib/version` - Shared version command |
| 113 | + |
| 114 | +All dependencies are managed via the repository's root go.mod file. |
0 commit comments