Guardrail testing
in Harness engineering
Your hook, check or sandbox can pass every test you wrote for it and still never fire on the real system.
- How does a guard fail without a failing test?
- How did lane-repeat-guard pass its tests and still miss?
- What does making it fire look like?
- Where else did a green guard do nothing?
- What are the common mistakes?
- What can't a probe prove?
- Summary
How does a guard fail without a failing test?
It fails when its test builds the input itself and the real system never sends that input. A guard here is any hook,
check or sandbox that stops a bad action: a hook that refuses git reset --hard, a test that keeps an install out of
your real home folder. Its test usually hand-writes the event the guard reacts to, feeds it in, and checks the answer.
That proves the guard handles the event. It says nothing about whether the runtime ever emits that event, in that shape,
on that platform.
The rule we now follow: break what the guard protects, in the place it runs, and watch it fire. Then fix it and watch it
pass. The running example is lane-repeat-guard, a Claude Code hook we built on 5 October 2026 in our agent harness.
You need no setup to follow along; the idea carries to any hook, linter rule or sandbox.
The guard fired in the top half. The next case, a failing Edit, is the one it missed.
This is the same habit as a test that fails before its fix, which we apply to app code. This post is about the step after the red test: the runtime.
How did lane-repeat-guard pass its tests and still miss?
It was tested on events we wrote by hand, and for one case the runtime sends none. The guard refuses a subagent's tool
call once that exact call has failed 3 times in its lane, so a stuck agent can't burn its turns retrying. It listens on
PreToolUse to refuse, and on PostToolUseFailure to count failures.
Its docstring, its test and the workflow doc all said an identical failing Edit is refused. The only proof was a
synthetic PostToolUseFailure payload fed to the hook. At the review gate a throwaway subagent ran four identical
failing Edits for real. None was refused.
A second probe the same day found why. We added a temporary capture to the guard that logged every hook payload naming a
probe file. A subagent then made five identical Edits whose old text, "THIS TEXT IS NOT IN THE FILE", was not in the
file. Each came back String to replace not found in file. The capture logged three payloads: PreToolUse for the
Read before, and PreToolUse and PostToolUse for one successful Edit after. None of the five failing Edits fired
any hook event. Claude Code 2.1.284 rejects them before the hook pipeline runs.
- The probe subagent calls a tool.
- If it is an
Editwhose old text is not in the file, Claude Code answers “String to replace not found” and no hook event fires. The guard never hears of it. - Any other call goes to
PreToolUse. If this exact call already failed 3 times in this lane, the guard denies it. - Otherwise the call runs. When it fails,
PostToolUseFailuretells the guard, which adds one to its count.
Dashed arrows are messages. A successful edit resets the count, and the main chat is never counted.
No guard code could fix that. We removed the claim from the docs, wrote the measured fact into the guard's docstring,
and named maxTurns, the per-agent turn cap, as the only bound on that loop. The warning: a test that feeds a hook its
event proves the hook, never the runtime.
What does making it fire look like?
A probe sends the bad case through the real runtime and records the raw result. For the Bash half of the same guard, a
throwaway subagent ran ls /nonexistent-lane-bounds-probe-1. Calls 1 to 3 failed normally. Calls 4 and 5 were refused
with the guard's message: "this exact Bash call has failed 3 times in this lane."
Then the fix half. A second path, ls /nonexistent-lane-bounds-probe-2, failed 3 times; the subagent made one
successful Write; the same ls was allowed again and failed normally. The main chat then ran the first ls and was
not refused, which is correct: the guard only counts subagents.
The settings that wire it up got the same treatment. On a scratch copy of the repo, never in the tree, we made five mutants: a turn cap raised by 10, a cap removed, a cap added where none belongs, one agent's measured run count edited below 20, and the edit matcher narrowed. The tests caught all five.
Paste the probe's raw output into the verification record, next to the claim it proves. A probe nobody wrote down is gone when the chat ends.
Where else did a green guard do nothing?
Two more guards passed their tests and did nothing where it mattered: one under a second tool, one on a second platform.
Cursor. Our shell guards read the command from Claude Code's payload field, tool_. They were
registered for Cursor too, but Cursor sends command at the top level. So under Cursor every guard read an empty command
and stood down, from 31 August 2026. On 22 September an orchestration lane run from Cursor did git reset --hard in a
shared checkout of a product repo. For about 15 minutes main pointed at another run's unreviewed integration branch,
and other sessions' uncommitted work was lost. The fix (4 October) is one payload reader that understands both shapes.
It was proven both ways: a Cursor-shaped git reset --hard beside a live peer got no answer before the fix, and
{"permission": "deny"} from the live Cursor hook after it.
Windows. A test of our install tool, ai-sync, ran a real install into a throwaway home by setting HOME. On
Windows, Python's Path.home() reads USERPROFILE (then HOMEDRIVE plus HOMEPATH) and ignores HOME. There, the
sandbox was a no-op, and the test would have installed into the operator's real profile and overwritten its Claude Code settings. The
fix (26 August) sets all four names. It was caught by ship-check, our pre-ship review, and checked against the ntpath source, not by running on
Windows. Our CI file says it plainly: the two Windows bugs "were both found by reasoning about Windows rather than by running
there."
What are the common mistakes?
| Symptom | Cause | Fix |
|---|---|---|
| The hook's test is green, and the hook never fires live | The test builds the event; the runtime never sends it for that case | Send the case through the runtime once and paste the raw result |
| A guard works under one tool and is silent under another | It reads one tool's payload shape | One shared reader, and a test fed each tool's real payload |
| A sandbox is safe on a Mac and writes your real profile on Windows | Path.home() on Windows ignores HOME |
Set every name the platform reads, and run it there |
| A test passes with and without the fix | The test's input can't show the bug (a sweep whose fallback matched anything; a branch history that pruned itself) | Undo the fix, watch the test go red, then put it back |
The last row is two cases from our lessons file. In one, a worktree cleanup counted unlanded work as landed and would have deleted it; its first test passed either way.
What can't a probe prove?
A probe proves one version on one platform on the day you ran it. The failing-Edit result was measured on Claude Code
2.1.284. No static check can tell whether a later version emits that event, so our lessons ledger marks this rule
"Guard: none". The only check is to run the probe again.
The Windows fix is in the same spot. We added a Windows runner to CI, but as of 14 October 46 of 83 scanned files
fail there on porting issues, so the Windows leg reports and doesn't block. Until it goes green, the USERPROFILE fix is
proven by reading source, not by firing.
Summary
- A test that feeds a hook its event proves the hook, never the runtime: break what the guard protects, where it runs, and watch it fire.
- Each guard in this post was green before it was proven.
lane-repeat-guardcatches the Bash loop and, by measurement, can't see the failing-Editloop.- The Cursor guards now fire on both payload shapes.
- The Windows sandbox is fixed on paper.
- Before a docstring or doc says "the guard catches X", run X once through the real runtime and paste the output in the
verification record. I'd start here: it is the cheapest of the three and would have caught the
Editclaim. - Make the Windows CI leg block again, so the
USERPROFILEfix is proven by a run. - Re-run the failing-
Editprobe after a Claude Code upgrade; if the runtime starts sending the event, the guard needs one more branch.