Metadata-Version: 2.4
Name: ravenrepo-agent-guard
Version: 0.3.2
Summary: Guardrails for AI coding agents: refuse shortcuts at edit time, gate 'all tests pass' claims on real checks, and prove behaviour for guarded builds.
Author: The agent-guard Authors
License-Expression: Apache-2.0
Project-URL: Homepage, https://ravenrepo.github.io/agent-guard/
Project-URL: Repository, https://github.com/RavenRepo/agent-guard
Project-URL: Documentation, https://github.com/RavenRepo/agent-guard/tree/main/docs
Project-URL: Issues, https://github.com/RavenRepo/agent-guard/issues
Project-URL: Changelog, https://github.com/RavenRepo/agent-guard/blob/main/CHANGELOG.md
Project-URL: Security, https://github.com/RavenRepo/agent-guard/security/policy
Project-URL: Funding, https://github.com/sponsors/RavenRepo
Keywords: ai-agents,coding-agents,guardrails,llm,ai-safety,reward-hacking,specification-gaming,claude-code,codex,hooks,code-quality,testing,ci,sandbox,developer-tools
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Security
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Dynamic: license-file

# agent-guard

**Guardrails for AI coding agents.** agent-guard refuses an agent's shortcuts the moment it tries them: skipped tests, silenced type checkers, `TODO` instead of code, exit codes hidden behind `| tail`, and "all tests pass" with no passing run. It also gates completion claims on real checks, and proves behaviour for guarded builds. Its hooks work with **Claude Code** and **Codex**, and guarded builds run with Claude Code. It needs no dependencies, and runs on your machines.

[![CI](https://github.com/RavenRepo/agent-guard/actions/workflows/ci.yml/badge.svg)](https://github.com/RavenRepo/agent-guard/actions/workflows/ci.yml)
[![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/RavenRepo/agent-guard/blob/main/LICENSE)
![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)
![Dependencies: none](https://img.shields.io/badge/dependencies-none-brightgreen.svg)
![Status: alpha](https://img.shields.io/badge/status-alpha-orange.svg)
[![Sponsor](https://img.shields.io/badge/sponsor-%E2%9D%A4-db61a2.svg?logo=githubsponsors)](https://github.com/sponsors/RavenRepo)
[![Website](https://img.shields.io/badge/website-ravenrepo.github.io%2Fagent--guard-f0b429.svg)](https://ravenrepo.github.io/agent-guard/)

```text
agent-guard refused this action:
- [masked-exit] /work/api 'pnpm -F @acme/api typecheck 2>&1 | tail -3'
  This hides the check's exit code. Use `set -o pipefail;` before piping, and never `|| true` a check.
These rules apply inside repositories only: put probes and scratch scripts in your scratch folder outside the repository.
Do the real work instead. Don't work around this check through another tool or command: that is refused too, and it is logged. If you believe this is a false positive, stop and report the rule and the line to the coordinator.
```

## Why

Coding agents optimise for the check they can see. Under pressure they **game the test instead of fixing the code**, a failure mode called reward hacking or specification gaming:
- weakening an assertion, or skipping the failing test;
- casting to `any`, or adding `@ts-ignore`;
- swallowing the error;
- writing a check that always returns `true`;
- copying the repo to `/tmp` to test outside the gate;
- reporting success without having run anything.

Code review catches some of it, late. agent-guard catches it **before the edit or command happens**, tells the agent what to do instead, and keeps the evidence.

## What it does

| Layer | When it runs | What you get |
|---|---|---|
| **Hooks** | Before every file edit and shell command, and when the agent stops (Claude Code and Codex hook events) | Shortcuts are refused with a reason the agent reads. A success claim needs a passing run of the *matching* check after the last code edit; an unbacked claim is refused, then recorded as an unresolved verdict. |
| **Runner scan** | In CI or your full check, before merge | `agent-guard scan BASE` applies the content rules, and the rules about test, gate and guard files, to the branch diff: additions, edits, deletions and renames. The shell and claim rules run only in the hooks. |
| **Guarded builds** | For tasks you define with a contract | The agent can only read granted files and submit edits. A change is accepted only if, in a sandbox, the base reproduces the defect, the change passes every behaviour case, and a planted bad implementation is caught. The check is re-run on the exact commit before landing. |

## What it catches

There are 23 data-driven rules, plus 8 built-in rules; run `agent-guard rules` for the full list.

| Family | Rules |
|---|---|
| Silencing the checker | `suppress-checker` (`@ts-ignore`, `eslint-disable`, `# type: ignore`, `# noqa`…), `type-escape` (`as any`, `as unknown as`), `ts-expect-error`, `checker-weakened` (`--ignoreConfig`, `--passWithNoTests`, `vitest -u`) |
| Placeholders instead of code | `placeholder` (TODO, "for now", "in production this would…"), `placeholder-code` (`throw new Error('not implemented')`), `elision` (`// ... existing code ...`), `commented-out-code`, `shortcut-unreferenced` |
| Gaming tests | `skipped-test` (`.skip`, `.only`, `xit`, `@pytest.mark.skip`), `assertion-drop`, `test-removed`, `test-lock` (builders may not touch tests), `test-file-mutation` (rewriting committed tests from the shell) |
| Fake implementations | `vacuous-guard` (`verify…() { return true }`), `vacuous-gate-list` (`…_CHECKS = []`), `swallowed-error`, `test-env-branch` (`if (NODE_ENV === 'test')`), `sandbox-probe` |
| Getting around the gate | `masked-exit` (`\|\| true`, `\| tail` without `pipefail`), `hook-bypass` (`--no-verify`, `core.hooksPath`), `gate-config` (CI, lint, tsconfig and dependency files), `guard-tamper`, `force-push`, `repo-copy` |
| Claims | `unbacked-claim` ("all tests pass" with no passing test run since the last edit), `forbidden-claim` ("production-ready") |

**Design choices that keep the agent from gaming the guard itself:**
- **Content rules check only the lines the agent adds,** so existing code is never blocked. The test, gate and guard rules also look at what an edit removes, or at which file it touches.
- **No inline overrides.** An override written in a comment is just another comment an agent can write. Exceptions live in the operator's policy: an `allow` entry carries an expiry date, a reason and an approver, and `disabled_rules` turns a rule off for every path. Neither can turn off `guard-tamper`.
- **The policy belongs to the operator.** The system policy wins over anything in the agent's environment, and the hooks pin it.
- **Evidence is a check the agent ran, not text it printed.** A claim needs a matching check whose result shows no failure: for Claude Code, a tool result that isn't flagged `is_error` and doesn't open with failure text such as `Exit code 1`; for Codex, tool output that holds none of the failure markers the hook recognises, such as `Process exited with code 1`. Printed text like `rc=0` does not count.

## Quick start

```sh
git clone --branch v0.3.2 https://github.com/RavenRepo/agent-guard.git && cd agent-guard
tests/run.sh                                  # rule self-test plus the test suite
bin/agent-guard rules                         # what it refuses
echo '{"tool_name":"Bash","cwd":".","tool_input":{"command":"pnpm test || true"}}' | bin/agent-guard hook pre; echo "exit $?"
```

**See it work with your real clients,** without changing anything on the machine. Each client runs against a scripted model on loopback; no account and no network are used.

```sh
bin/agent-guard prove --disposable enforce
```

**Install as a package** (Python 3.10+). On PyPI it is `ravenrepo-agent-guard`; the command it installs is `agent-guard`:

```sh
pipx install ravenrepo-agent-guard
agent-guard selftest
```

The `agent-guard` package on PyPI is a different project. To install from this repository instead, use a release tag: `pipx install git+https://github.com/RavenRepo/agent-guard.git@v0.3.2`.

**Upgrading from 0.3.1 or earlier,** whose package was named `agent-guard`: remove it first, because both packages install the `agent-guard` command. With pipx, run `pipx uninstall agent-guard` before `pipx install ravenrepo-agent-guard`; otherwise pipx leaves the command pointing at the old version. In a shared virtual environment, run `pip uninstall agent-guard` before installing the new package; uninstalling it afterwards removes the command the two share. Then check that `agent-guard version` prints the new version.

agent-guard runs inside every agent session, so check what you install: see [Verifying what you install](https://github.com/RavenRepo/agent-guard/blob/main/SECURITY.md#verifying-what-you-install).

## Set up the hooks

### One developer

**Claude Code:** add to `~/.claude/settings.json`.

```json
{
  "hooks": {
    "PreToolUse": [{"matcher": "Edit|Write|MultiEdit|NotebookEdit|Bash",
                    "hooks": [{"type": "command", "command": "agent-guard hook pre", "timeout": 10}]}],
    "Stop": [{"hooks": [{"type": "command", "command": "agent-guard hook stop", "timeout": 10}]}],
    "SubagentStop": [{"hooks": [{"type": "command", "command": "agent-guard hook stop", "timeout": 10}]}]
  }
}
```

**Codex:** add the same `hooks` object, without `SubagentStop` and with `"matcher": "*"` for `PreToolUse`, to `~/.codex/hooks.json`. Then trust the hooks in `/hooks`. Codex skips user hooks until they are reviewed.

### A team host or a fleet of agents

This is the recommended setup. It is a root-owned install with system hooks (Claude Code managed settings, and `/etc/codex/hooks.json`, which Codex trusts by policy). It runs an activation proof with the real clients and rolls back the hook registrations if the proof fails.

```sh
sudo bash install/install.sh install --agent-user agent --enforce-paths /srv/work
```

Start in `warn` mode, read the event log, then switch to `enforce`. In `warn` mode the hooks refuse nothing, but an unbacked success claim found by a `block` rule is still recorded as an unresolved verdict when the event has a session id, and the user sees a message. See [docs/DEPLOY.md](https://github.com/RavenRepo/agent-guard/blob/main/docs/DEPLOY.md).

### CI

Add the runner scan to your full check, so a shortcut that slipped past the hooks is caught before merge. The scan diffs against the base branch, so the checkout needs its history (`fetch-depth: 0`):

```yaml
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  with:
    fetch-depth: 0
    persist-credentials: false
- run: pipx run --spec ravenrepo-agent-guard==0.3.2 agent-guard scan origin/main
```

## Grow the rules

A rule is data: a pattern, plus examples of the shortcut and of the honest code next to it. To add one:

1. Write the rule, with both kinds of example.
2. Run `agent-guard rules check pack.json` to prove it refuses the faults and allows the clean twins.
3. Run `agent-guard replay` to measure it against recorded sessions.
4. Ship it in `warn` mode, and promote it to blocking.

See [docs/ADDING-RULES.md](https://github.com/RavenRepo/agent-guard/blob/main/docs/ADDING-RULES.md).

## Documentation

| Guide | Covers |
|---|---|
| [docs/DEPLOY.md](https://github.com/RavenRepo/agent-guard/blob/main/docs/DEPLOY.md) | Install, activation proof, configuration, rollout from warn to enforce, completion verdicts, undo |
| [docs/ADDING-RULES.md](https://github.com/RavenRepo/agent-guard/blob/main/docs/ADDING-RULES.md) | Rule format, self-test, replay, exceptions |
| [docs/CONTRACTS.md](https://github.com/RavenRepo/agent-guard/blob/main/docs/CONTRACTS.md) | Guarded-build contracts, the sandbox, landing checks |
| [CHANGELOG.md](https://github.com/RavenRepo/agent-guard/blob/main/CHANGELOG.md) | Release history |

## FAQ

**Does it send anything anywhere?**
Not from the hooks or the runner scan: they make no network calls. `prove` runs the clients against scripted models on loopback. A guarded build launches Claude Code, which talks to whatever model you have configured for it. The event log holds rule ids, paths, line numbers and hashes, and each event's host name, session id, working directory, tool and, for a subagent, agent id, never your code.

**How much does it slow the agent down?**
One short-lived process per tool call: about 120 ms median on a laptop, mostly Python start-up.

**What if the guard itself has a bug?**
It fails open. The action proceeds and the error is logged, so a guard bug never stops every agent. The runner scan is the backstop.

**How do I stop an agent from claiming "all tests pass" when they don't?**
The Stop hook checks the final message against what actually ran. A tests claim needs a passing test run after the last code edit; printed text such as `rc=0` doesn't count. A claim that stays unbacked is recorded as unresolved, so your controller can refuse to mark the work done.

**Can it judge code quality?**
No. It refuses named, mechanical shortcuts, and proves behaviour you specify. Design and readability still need review.

## Limits

- **The pattern rules are scanners, not parsers.** A determined agent can still hide a change; the runner scan and human review stay.
- **Guarded builds suit bounded tasks** (functions, scripts, CLIs). A probe gets no network, 384 MiB, one CPU and 6 seconds.
- **The Stop hook doesn't decide completion.** It records verdicts; your controller decides, using the verdict and the runner's own result.
- **Tested on Linux** with Claude Code 2.1.332 and Codex 0.168.0.

## Author

agent-guard is built and maintained by [Amit Kumar](https://iamamitkumar.dev) ([@growthperclick](https://x.com/growthperclick)), a founder and AI engineer who builds open tooling around AI coding agents: guardrails, model routing, evals and governance. Also by the author: [ladder](https://github.com/RavenRepo/ladder), which runs recurring LLM work on the cheapest model that still passes a cross-family verifier. More at [growthperclick.com](https://growthperclick.com).

## Sponsor the project

agent-guard is free and open source. Sponsorship pays for the work that keeps it useful:
- growing and tuning the rule catalogue as new shortcuts turn up;
- re-testing the hooks against each new release of the supported clients;
- triaging false positives quickly.

If it keeps your agents honest, please consider [sponsoring on GitHub](https://github.com/sponsors/RavenRepo).

## Contributing and security

Contributions are welcome. False positives and shortcuts no rule catches help most. See [CONTRIBUTING.md](https://github.com/RavenRepo/agent-guard/blob/main/CONTRIBUTING.md). To report a way around the guard, follow [SECURITY.md](https://github.com/RavenRepo/agent-guard/blob/main/SECURITY.md).

## License

[Apache License 2.0](https://github.com/RavenRepo/agent-guard/blob/main/LICENSE)
