|
| 1 | +# Using agent-strace with AGENTS.md |
| 2 | + |
| 3 | +## What is AGENTS.md? |
| 4 | + |
| 5 | +`AGENTS.md` is a Markdown file placed at the root of a repository that tells AI coding agents how to work with the project. It specifies tool permissions, constraints, coding conventions, and model hints. Claude Code, Cursor, and other agents read it automatically at session start. |
| 6 | + |
| 7 | +See the [AGENTS.md specification](https://docs.anthropic.com/en/docs/claude-code/memory) for the full format. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## How agent-strace reads AGENTS.md |
| 12 | + |
| 13 | +agent-strace uses `AGENTS.md` as a **behavioral baseline anchor**. When a session starts, it records the current `AGENTS.md` hash alongside the session metadata. This lets it detect when agent behavior changes after an `AGENTS.md` edit — distinguishing intentional config changes from unexpected drift. |
| 14 | + |
| 15 | +Specifically, agent-strace reads: |
| 16 | + |
| 17 | +| Field | How it's used | |
| 18 | +|---|---| |
| 19 | +| Tool permissions (`allowed_tools`, `disallowed_tools`) | Checked against actual tool calls in `agent-strace audit` | |
| 20 | +| File path constraints (`read_only_paths`, `write_paths`) | Checked in `agent-strace audit --policy` | |
| 21 | +| Model hints (`model`) | Compared against `LLM_REQUEST` events in drift detection | |
| 22 | +| Any free-text constraints | Indexed for `agent-strace optimize` suggestions | |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Change detection |
| 27 | + |
| 28 | +`agent-strace drift` computes a behavioral fingerprint for each session (tool mix, error rate, retry rate, blast radius) and compares it against a saved baseline. When `AGENTS.md` changes, the baseline should be updated so drift is measured from the new intended behavior, not the old one. |
| 29 | + |
| 30 | +```bash |
| 31 | +# Check for behavioral drift since the last baseline |
| 32 | +agent-strace drift |
| 33 | + |
| 34 | +# Check drift specifically after an AGENTS.md edit |
| 35 | +agent-strace drift --baseline .agent-strace/baseline.json |
| 36 | +``` |
| 37 | + |
| 38 | +The drift report shows which dimensions changed and by how much: |
| 39 | + |
| 40 | +``` |
| 41 | +Behavioral drift report |
| 42 | +======================= |
| 43 | +Baseline: .agent-strace/baseline.json (saved 2026-05-01) |
| 44 | +Sessions compared: 12 |
| 45 | +
|
| 46 | + error_rate 0.04 → 0.12 ▲ +200% ⚠ DRIFT |
| 47 | + retry_rate 0.08 → 0.09 ▲ +12% OK |
| 48 | + blast_radius 3.2 → 3.1 ▼ -3% OK |
| 49 | + tool_mix 0.91 similarity OK |
| 50 | +
|
| 51 | +Verdict: DRIFT DETECTED — error_rate increased significantly. |
| 52 | +Run `agent-strace explain` on recent sessions to investigate. |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Baseline anchoring |
| 58 | + |
| 59 | +After updating `AGENTS.md`, save a new baseline so future drift is measured from the new intended behavior: |
| 60 | + |
| 61 | +```bash |
| 62 | +# Save a baseline after an AGENTS.md update |
| 63 | +agent-strace drift --save-baseline .agent-strace/baseline.json |
| 64 | + |
| 65 | +# Tag it for traceability |
| 66 | +git add .agent-strace/baseline.json |
| 67 | +git commit -m "chore: update behavioral baseline after AGENTS.md change" |
| 68 | +``` |
| 69 | + |
| 70 | +The baseline file is a small JSON document (< 1 KB) that records the statistical distribution of behavioral metrics across recent sessions. It is safe to commit. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## CI integration |
| 75 | + |
| 76 | +Gate on behavioral regression whenever `AGENTS.md` changes. Add this to your CI workflow: |
| 77 | + |
| 78 | +```yaml |
| 79 | +# .github/workflows/agent-behavior.yml |
| 80 | +name: Agent behavior gate |
| 81 | + |
| 82 | +on: |
| 83 | + push: |
| 84 | + paths: |
| 85 | + - AGENTS.md |
| 86 | + - .agent-strace/baseline.json |
| 87 | + |
| 88 | +jobs: |
| 89 | + drift-check: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + steps: |
| 92 | + - uses: actions/checkout@v4 |
| 93 | + |
| 94 | + - name: Install agent-strace |
| 95 | + run: pip install agent-strace |
| 96 | + |
| 97 | + - name: Check behavioral drift |
| 98 | + run: | |
| 99 | + agent-strace drift \ |
| 100 | + --baseline .agent-strace/baseline.json \ |
| 101 | + --save-baseline /tmp/new-baseline.json |
| 102 | + # Fails with exit code 1 if drift exceeds threshold |
| 103 | +
|
| 104 | + - name: Run eval CI gate |
| 105 | + run: | |
| 106 | + agent-strace eval ci \ |
| 107 | + --baseline .agent-strace/eval-baseline.json \ |
| 108 | + --tolerance 1 |
| 109 | +``` |
| 110 | +
|
| 111 | +The `drift` command exits non-zero when any dimension exceeds its threshold, making it suitable as a CI gate. |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Recommended AGENTS.md snippet |
| 116 | + |
| 117 | +Add this to your project's `AGENTS.md` to tell agents how to work with agent-strace: |
| 118 | + |
| 119 | +```markdown |
| 120 | +## Observability |
| 121 | +
|
| 122 | +This project uses [agent-strace](https://github.com/Siddhant-K-code/agent-trace) to |
| 123 | +capture and analyse agent sessions. |
| 124 | +
|
| 125 | +- Traces are stored in `.agent-traces/` (gitignored) |
| 126 | +- Run `agent-strace replay` after a session to review what happened |
| 127 | +- Run `agent-strace drift` to check for behavioral regression |
| 128 | +- Run `agent-strace retention clean --max-age-days 30` periodically to free disk space |
| 129 | + |
| 130 | +Do not delete `.agent-traces/` manually — use `agent-strace retention clean`. |
| 131 | +``` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Detecting AGENTS.md drift |
| 136 | + |
| 137 | +`agent-strace drift` also detects when the agent's behavior diverges from what `AGENTS.md` specifies — for example, if the agent starts writing to paths that `AGENTS.md` marks as read-only, or calling tools that are listed as disallowed. |
| 138 | + |
| 139 | +```bash |
| 140 | +# Audit the latest session against the current AGENTS.md policy |
| 141 | +agent-strace audit --policy AGENTS.md |
| 142 | + |
| 143 | +# Check all sessions from the last 7 days |
| 144 | +agent-strace audit --policy AGENTS.md --since 7d |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Full workflow |
| 150 | + |
| 151 | +``` |
| 152 | +1. Edit AGENTS.md |
| 153 | +2. Run a few test sessions with the agent |
| 154 | +3. agent-strace drift --save-baseline .agent-strace/baseline.json |
| 155 | +4. git commit -m "chore: update baseline after AGENTS.md change" |
| 156 | +5. CI gates on future drift automatically |
| 157 | +``` |
| 158 | + |
| 159 | +This closes the loop between "I updated the agent's instructions" and "I know the agent is actually following them." |
0 commit comments