AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # agent-guard |
| 2 | |
| 3 | <p align="center"> |
| 4 | <img src="assets/agent-guard-banner.png" alt="agent-guard — local-first security layer for AI coding agents" width="100%" /> |
| 5 | </p> |
| 6 | |
| 7 | > Give AI agents freedom to code — without giving them unrestricted access. |
| 8 | |
| 9 | AI coding agents can edit files, run shell commands, and touch infrastructure. One bad `rm -rf`, force-push, or `docker compose down -v` can wipe hours of work or production data. |
| 10 | |
| 11 | **agent-guard** is a small, local-first security layer that sits between an AI agent and your system. It evaluates risky operations and returns a clear decision: |
| 12 | |
| 13 | ```text |
| 14 | AI Agent |
| 15 | ↓ |
| 16 | agent-guard |
| 17 | ↓ |
| 18 | ALLOW / WARN / BLOCK |
| 19 | ↓ |
| 20 | System |
| 21 | ``` |
| 22 | |
| 23 | It is **not** an AI assistant. Decisions are deterministic, offline, explainable, and auditable. No LLM. No network. No telemetry. |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | ## Features |
| 28 | |
| 29 | - **Local-first** — config and evaluation stay on your machine |
| 30 | - **Offline** — no cloud APIs or network calls in the core |
| 31 | - **Deterministic** — same input + same policy = same result |
| 32 | - **No LLM required** — rule- and policy-based |
| 33 | - **Command inspection** — parse and analyze without executing |
| 34 | - **Filesystem protection** — recursive deletes, roots, protected paths |
| 35 | - **Git protection** — reset/clean/force-push to protected branches |
| 36 | - **Docker protection** — volume deletes, aggressive prune |
| 37 | - **Secret protection** — `.env`, keys, credentials (values never printed) |
| 38 | - **Database protection** — DROP / TRUNCATE / FLUSH / dropDatabase |
| 39 | - **Network protection** — high-risk methods and production hosts |
| 40 | - **Project policies** — `.agent-guard.yml` with modes |
| 41 | - **JSON output** — machine-readable for agent integrations |
| 42 | - **Cross-platform** — Linux, macOS, Windows |
| 43 | |
| 44 | --- |
| 45 | |
| 46 | ## Installation |
| 47 | |
| 48 | ```bash |
| 49 | pip install agent-guard |
| 50 | ``` |
| 51 | |
| 52 | ### Development install |
| 53 | |
| 54 | ```bash |
| 55 | git clone https://github.com/gitrabbit/agent-guard.git |
| 56 | cd agent-guard |
| 57 | python -m venv .venv |
| 58 | # Linux/macOS |
| 59 | source .venv/bin/activate |
| 60 | # Windows |
| 61 | # .venv\Scripts\activate |
| 62 | pip install -e ".[dev]" |
| 63 | ``` |
| 64 | |
| 65 | --- |
| 66 | |
| 67 | ## Quick Start |
| 68 | |
| 69 | ```bash |
| 70 | agent-guard init |
| 71 | agent-guard check "git reset --hard HEAD" |
| 72 | agent-guard scan |
| 73 | agent-guard run -- npm test |
| 74 | ``` |
| 75 | |
| 76 | --- |
| 77 | |
| 78 | ## Examples |
| 79 | |
| 80 | ### 1. Dangerous filesystem operation |
| 81 | |
| 82 | ```bash |
| 83 | agent-guard check "rm -rf ./src" |
| 84 | ``` |
| 85 | |
| 86 | ```text |
| 87 | BLOCK |
| 88 | Risk: CRITICAL |
| 89 | Reason: Recursive deletion inside the source directory. |
| 90 | ``` |
| 91 | |
| 92 | ### 2. Filesystem root |
| 93 | |
| 94 | ```bash |
| 95 | agent-guard check "rm -rf /" |
| 96 | ``` |
| 97 | |
| 98 | Exit code: `20` (BLOCK) |
| 99 | |
| 100 | ### 3. Git force-push to main |
| 101 | |
| 102 | ```bash |
| 103 | agent-guard check "git push --force origin main" |
| 104 | ``` |
| 105 | |
| 106 | ```text |
| 107 | BLOCK |
| 108 | Protected branch force-push detected. |
| 109 | ``` |
| 110 | |
| 111 | ### 4. Destructive git (warn) |
| 112 | |
| 113 | ```bash |
| 114 | agent-guard check "git reset --hard HEAD" |
| 115 | ``` |
| 116 | |
| 117 | ```text |
| 118 | WARN |
| 119 | Risk: HIGH |
| 120 | Reason: This operation can permanently discard uncommitted changes. |
| 121 | ``` |
| 122 | |
| 123 | ### 5. Docker volume deletion |
| 124 | |
| 125 | ```bash |
| 126 | agent-guard check "docker compose down -v" |
| 127 | ``` |
| 128 | |
| 129 | ```text |
| 130 | BLOCK |
| 131 | Docker volumes may contain persistent application data. |
| 132 | ``` |
| 133 | |
| 134 | ### 6. Secret file access |
| 135 | |
| 136 | ```bash |
| 137 | agent-guard check "cat .env" |
| 138 | ``` |
| 139 | |
| 140 | ```text |
| 141 | BLOCK |
| 142 | Sensitive file access detected. |
| 143 | Target: .env |
| 144 | ``` |
| 145 | |
| 146 | ### 7. Production network |
| 147 | |
| 148 | ```bash |
| 149 | agent-guard check "curl -X DELETE https://production.example.com/api/users/1" |
| 150 | ``` |
| 151 | |
| 152 | ```text |
| 153 | BLOCK |
| 154 | ``` |
| 155 | |
| 156 | ### 8. Safe command |
| 157 | |
| 158 | ```bash |
| 159 | agent-guard check "git status" |
| 160 | ``` |
| 161 | |
| 162 | ```text |
| 163 | ALLOW |
| 164 | ``` |
| 165 | |
| 166 | ### 9. Enforced run with confirmation |
| 167 | |
| 168 | ```bash |
| 169 | agent-guard run -- git reset --hard HEAD |
| 170 | ``` |
| 171 | |
| 172 | Interactive WARN prompt (default **N**). Non-interactive sessions deny WARN unless `--allow-risk` is set. **BLOCK is never overridden by `--allow-risk`.** |
| 173 | |
| 174 | ### 10. JSON output |
| 175 | |
| 176 | ```bash |
| 177 | agent-guard check --json "rm -rf ./" |
| 178 | ``` |
| 179 | |
| 180 | ```json |
| 181 | { |
| 182 | "decision": "BLOCK", |
| 183 | "risk": "CRITICAL", |
| 184 | "operation": "DIRECTORY_DELETE", |
| 185 | "rule_id": "filesystem.recursive_delete", |
| 186 | "reason": "...", |
| 187 | "target": "./" |
| 188 | } |
| 189 | ``` |
| 190 | |
| 191 | ### 11. File operation form |
| 192 | |
| 193 | ```bash |
| 194 | agent-guard check --file-operation delete ./src |
| 195 | ``` |
| 196 | |
| 197 | ### 12. Explicit WARN override (run) |
| 198 | |
| 199 | ```bash |
| 200 | agent-guard run --allow-risk -- git reset --hard HEAD |
| 201 | ``` |
| 202 | |
| 203 | Still cannot run: |
| 204 | |
| 205 | ```bash |
| 206 | agent-guard run --allow-risk -- rm -rf / |
| 207 | # → BLOCK, never executed |
| 208 | ``` |
| 209 | |
| 210 | --- |
| 211 | |
| 212 | ## Configuration |
| 213 | |
| 214 | `agent-guard init` creates `.agent-guard.yml` in the project root (relative paths only — never hardcodes your absolute home path): |
| 215 | |
| 216 | ```yaml |
| 217 | version: 1 |
| 218 | |
| 219 | project_root: . |
| 220 | |
| 221 | mode: default # default | strict | permissive |
| 222 | |
| 223 | protected_paths: |
| 224 | - .env |
| 225 | - .env.* |
| 226 | - .git/ |
| 227 | - ~/.ssh/ |
| 228 | - ~/.aws/ |
| 229 | - secrets/** |
| 230 | |
| 231 | writable_paths: |
| 232 | - src/ |
| 233 | - tests/ |
| 234 | - docs/ |
| 235 | |
| 236 | protected_branches: |
| 237 | - main |
| 238 | - master |
| 239 | - production |
| 240 | - prod |
| 241 | |
| 242 | network: |
| 243 | protected_hosts: |
| 244 | - production.example.com |
| 245 | - api.production.example.com |
| 246 | |
| 247 | rules: |
| 248 | destructive_filesystem: block |
| 249 | destructive_git: warn |
| 250 | protected_branch_force_push: block |
| 251 | secret_access: block |
| 252 | docker_volume_delete: block |
| 253 | production_network: block |
| 254 | database_destructive: block |
| 255 | ``` |
| 256 | |
| 257 | ### Policy modes |
| 258 | |
| 259 | | Mode | Behavior | |
| 260 | |------|----------| |
| 261 | | `default` | Balanced developer protection | |
| 262 | | `strict` | More WARN/BLOCK (HIGH WARN → BLOCK) | |
| 263 | | `permissive` | Softer for non-CRITICAL; **CRITICAL stays blocked** | |
| 264 | |
| 265 | Decision precedence when multiple rules match: **BLOCK > WARN > ALLOW**. |
| 266 | |
| 267 | --- |
| 268 | |
| 269 | ## Rules |
| 270 | |
| 271 | List built-in rules: |
| 272 | |
| 273 | ```bash |
| 274 | agent-guard rules |
| 275 | agent-guard rules --json |
| 276 | ``` |
| 277 | |
| 278 | Categories include: |
| 279 | |
| 280 | | Category | Examples | |
| 281 | |----------|----------| |
| 282 | | `destructive_filesystem` | `rm -rf`, `Remove-Item -Recurse`, `shutil.rmtree` | |
| 283 | | `destructive_git` | `reset --hard`, `clean -fdx`, rebase | |
| 284 | | `protected_branch_force_push` | `git push -f` to main/master/prod | |
| 285 | | `secret_access` | `.env`, PEM keys, credential files | |
| 286 | | `docker_volume_delete` | `compose down -v`, `volume rm`, `prune -a` | |
| 287 | | `database_destructive` | DROP, TRUNCATE, FLUSHALL, dropDatabase | |
| 288 | | `production_network` | DELETE/PUT to production hosts | |
| 289 | | `shell_chain_destructive` | `&&` / `;` chaining signals | |
| 290 | |
| 291 | --- |
| 292 | |
| 293 | ## Risk Levels |
| 294 | |
| 295 | | Score | Level | |
| 296 | |------:|-------| |
| 297 | | 0–19 | LOW | |
| 298 | | 20–49 | MEDIUM | |
| 299 | | 50–79 | HIGH | |
| 300 | | 80+ | CRITICAL | |
| 301 | |
| 302 | Rules may explicitly override the calculated band (e.g. `rm -rf /` is always CRITICAL/BLOCK). |
| 303 | |
| 304 | --- |
| 305 | |
| 306 | ## Decisions |
| 307 | |
| 308 | | Decision | Meaning | Typical exit | |
| 309 | |----------|---------|--------------| |
| 310 | | **ALLOW** | Safe to proceed | `0` | |
| 311 | | **WARN** | Risky; confirm or `--allow-risk` | `10` | |
| 312 | | **BLOCK** | Do not execute | `20` | |
| 313 | |
| 314 | Runtime/config errors use exit code **`30`**. |
| 315 | |
| 316 | --- |
| 317 | |
| 318 | ## Architecture |
| 319 | |
| 320 | ```text |
| 321 | command / file operation |
| 322 | │ |
| 323 | ▼ |
| 324 | normalize ──► parse ──► detect (rules) |
| 325 | │ |
| 326 | ▼ |
| 327 | classify risk |
| 328 | │ |
| 329 | ▼ |
| 330 | evaluate policy |
| 331 | │ |
| 332 | ▼ |
| 333 | ALLOW / WARN / BLOCK |
| 334 | ``` |
| 335 | |
| 336 | Extension-friendly seams (MVP stays simple): policy provider, rule registry, decision engine, execution backend (`shell=False` subprocess). |
| 337 | |
| 338 | Future surfaces can share the same engine: |
| 339 | |
| 340 | ```text |
| 341 | agent-guard |
| 342 | │ |
| 343 | ┌───────────┼───────────┐ |
| 344 | ▼ ▼ ▼ |
| 345 | CLI MCP Agent API |
| 346 | │ │ │ |
| 347 | └───────────┼───────────┘ |
| 348 | ▼ |
| 349 | Policy Engine |
| 350 | ``` |
| 351 | |
| 352 | --- |
| 353 | |
| 354 | ## Security Model |
| 355 | |
| 356 | > **agent-guard is a policy enforcement layer, not a kernel-level sandbox.** |
| 357 | |
| 358 | ### What it does |
| 359 | |
| 360 | - Statically analyzes command strings / argv before execution |
| 361 | - Applies project policy and explainable rules |
| 362 | - Redacts secrets from output |
| 363 | - For `run`: evaluates **before** `subprocess.run(..., shell=False)` |
| 364 | - Never executes during `check` |
| 365 | - Never auto-approves WARN in non-interactive mode |
| 366 | |
| 367 | ### What it does not do |
| 368 | |
| 369 | - Not a kernel sandbox, seccomp, or container isolation |
| 370 | - Not TOCTOU-proof against a malicious process racing the check |
| 371 | - Not a complete shell language parser — sophisticated obfuscation can bypass naive analysis |
| 372 | - Does not follow arbitrary symlinks during scan |
| 373 | - Does not connect to Docker, git remotes, or databases during analysis |
| 374 | |
| 375 | Design for stronger enforcement later (MCP hooks, OS sandboxing, snapshots) without rewriting the policy core. |
| 376 | |
| 377 | --- |
| 378 | |
| 379 | ## Agent Integration |
| 380 | |
| 381 | There is **no native plugin** claimed for Claude Code, Codex, Cursor, or OpenCode in this MVP. Use the CLI as a gate: |
| 382 | |
| 383 | ```text |
| 384 | Agent tool call (bash / shell) |
| 385 | ↓ |
| 386 | agent-guard check|run -- <command> |
| 387 | ↓ |
| 388 | decision (exit code + text/JSON) |
| 389 | ``` |
| 390 | |
| 391 | Patterns: |
| 392 | |
| 393 | 1. **Advisory** — agent runs `agent-guard check "..."` and respects BLOCK/WARN |
| 394 | 2. **Enforcing** — wrap execution: `agent-guard run -- <cmd>` |
| 395 | 3. **JSON** — parse `--json` for structured policy results |
| 396 | |
| 397 | See [`examples/`](examples/) for Claude Code, Codex, and shell wrapper notes. |
| 398 | |
| 399 | --- |
| 400 | |
| 401 | ## JSON API |
| 402 | |
| 403 | ```bash |
| 404 | agent-guard check --json "git push -f origin main" |
| 405 | ``` |
| 406 | |
| 407 | Fields include: `decision`, `risk`, `operation`, `rule_id`, `reason`, `target`, `suggestions`, `matched_policy`, `detected`, `score`, `findings`. |
| 408 | |
| 409 | JSON contains no ANSI codes and never includes raw secret values. |
| 410 | |
| 411 | --- |
| 412 | |
| 413 | ## Exit Codes |
| 414 | |
| 415 | | Code | Meaning | |
| 416 | |-----:|---------| |
| 417 | | 0 | ALLOW | |
| 418 | | 10 | WARN | |
| 419 | | 20 | BLOCK | |
| 420 | | 30 | Runtime / configuration error | |
| 421 | |
| 422 | For `check`, the exit code reflects the security decision. |
| 423 | |
| 424 | --- |
| 425 | |
| 426 | ## CLI Reference |
| 427 | |
| 428 | ```text |
| 429 | agent-guard check "command" # analyze only |
| 430 | agent-guard check --file-operation delete PATH |
| 431 | agent-guard run -- <command> # evaluate then execute |
| 432 | agent-guard run --allow-risk -- ... # override WARN only |
| 433 | agent-guard init # write .agent-guard.yml |
| 434 | agent-guard scan # project risk scan |
| 435 | agent-guard rules [--json] |
| 436 | agent-guard config [--json] |
| 437 | agent-guard version |
| 438 | agent-guard --debug ... # debug with redaction |
| 439 | ``` |
| 440 | |
| 441 | --- |
| 442 | |
| 443 | ## Development |
| 444 | |
| 445 | ```bash |
| 446 | pytest |
| 447 | pytest --cov=agent_guard --cov-report=term-missing |
| 448 | ruff check . |
| 449 | mypy src |
| 450 | ``` |
| 451 | |
| 452 | --- |
| 453 | |
| 454 | ## Adding Rules |
| 455 | |
| 456 | 1. Add a function in `src/agent_guard/rules/<category>.py` |
| 457 | 2. Decorate with `@register_rule(RuleDefinition(...))` |
| 458 | 3. Return `list[Finding]` — never execute commands, never print secrets |
| 459 | 4. Add tests under `tests/rules/` |
| 460 | 5. Document the category in the README if new |
| 461 | |
| 462 | ```python |
| 463 | @register_rule( |
| 464 | RuleDefinition( |
| 465 | id="git.example", |
| 466 | category="destructive_git", |
| 467 | risk=RiskLevel.HIGH, |
| 468 | default_decision=Decision.WARN, |
| 469 | description="Example rule", |
| 470 | ) |
| 471 | ) |
| 472 | def rule_example(parsed: ParsedCommand, config: GuardConfig) -> list[Finding]: |
| 473 | return [] |
| 474 | ``` |
| 475 | |
| 476 | --- |
| 477 | |
| 478 | ## Contributing |
| 479 | |
| 480 | See [CONTRIBUTING.md](CONTRIBUTING.md). |
| 481 | |
| 482 | --- |
| 483 | |
| 484 | ## Roadmap |
| 485 | |
| 486 | Possible future ideas (not implemented): |
| 487 | |
| 488 | - MCP server |
| 489 | - Claude Code / Codex / Cursor native hooks |
| 490 | - Local LLM *explanations* only (not decisions) |
| 491 | - Process sandboxing / filesystem snapshots |
| 492 | - Approval history and organization-signed policies |
| 493 | |
| 494 | --- |
| 495 | |
| 496 | ## License |
| 497 | |
| 498 | MIT. See [LICENSE](LICENSE). |