As coding-agent sessions become faster and more autonomous, the cost of one bad command goes up. A guard hook sits in front of tool calls (shell commands, file edits, MCP calls) and answers one question: is this safe to auto-allow, or should it pause and ask the human?
How the guard pipeline works
- Skip list for fast reads: Obviously safe read-only commands (
ls,cat,git status) auto-allow immediately with zero LLM calls. - Protected-path gate: Writes targeting
~/.claude,~/.ssh,~/.aws, shell startup files,/usr/local/bin, or the guard's own folder always prompt the operator (including resolving environment variables and symlinks so indirection can't slip past). - Two-pass LLM classifier: Everything else is rated
SAFE,CAUTION, orUNSAFEagainst a policy file. Pass 1 uses a fast, cheap model; anything flaggedCAUTIONorUNSAFEgets a second-opinion review from a stronger model that can downgrade false alarms toSAFE. - Code-side dangerous-command veto: Even if an LLM rates a command
SAFE(or falls for prompt injection), a code-side check vetoes irreversible utilities (shred,dd,curl | bash, force pushes,find -exec) and forces human confirmation. - Fail closed: If the classifier is unreachable or the hook payload is malformed, the hook falls back to prompting or denying, never silently allowing.
Module layout
hooks/guard/
├── guard.py # Entry point, 7-step pipeline + fail-closed wrapper
├── config.json # Policy & model configuration
├── utils/
│ ├── shell_parse.py # Wrapper-peeling (sudo/env) & shell parsing
│ ├── neo4j_log.py # Background decision audit logging
│ └── code_gates/
│ ├── skip_list.py # Pre-LLM read-only fast allow
│ ├── protected_paths.py # Pre-LLM sensitive-path prompt
│ └── dangerous_bash.py # Post-LLM code-side override
└── classifier/
├── classify.py # Pass 1 (cheap/fast model)
└── reevaluate_non_safe.py # Pass 2 (stronger model, CAUTION/UNSAFE only)
Keeping hard-coded path and command checks in front of a cheap-then-strong LLM classifier keeps read-heavy agent sessions fast while still stopping destructive edge cases for human sign-off.