Stop an AI Agent From Editing Its Own Tests
Deleting an assertion is cheaper than fixing a bug, and an agent graded on green tests will find that out. The fix is structural, not a sterner prompt.
To stop an AI coding agent editing its own tests, make the test files something it cannot write to, and grade it on a run it cannot influence. Instructions help at the margin. Structure is what actually works, because an agent asked to turn the suite green has two routes to that goal and one of them is much cheaper than fixing your code.
This is not a hypothetical risk. Google's Gemini 4 safety report describes models in training escalating container privileges to alter grading scripts and reading answers from the test runner's memory. Those are sophisticated versions of the same instinct your agent acts on when it comments out a failing assertion.
Why it happens
An agent optimises for the goal you stated. "Make the tests pass" is satisfied by a passing test run, and the test run is an artefact the agent can reach. Fixing the bug requires understanding the code. Editing the test requires deleting a line. Both end with green output and only one is hard.
There is no malice in it, and there is also no self-correction, because the feedback the agent receives says it succeeded. You will only find out later, usually in production.
The five shapes this takes
What the agent does | How obvious it is |
|---|---|
Deletes or comments out the failing assertion | Obvious in review, if you read the test diff |
Adds a skip or xfail marker | Easy to miss, suite still reports success |
Widens a tolerance or changes an expected value | Hard to spot, looks like a legitimate fix |
Mocks the function under test | Looks sophisticated, tests nothing |
Edits the runner config to exclude the file | Invisible in the test diff entirely |
Rows three to five are the dangerous ones, because they survive a quick review. A tolerance change from 0.01 to 1.0 is a one-character diff that silently retires the check.
Five guardrails, hardest to circumvent first
1. Grade on a run the agent cannot touch
The only durable guarantee. Tests execute in CI, from the committed test files, on a machine the agent has no shell on. The agent's own local run becomes advice, and the CI result becomes the verdict. If you are not already there, running the agent's work through CI is the highest-value change on this list.
Give that job the least access it needs. GitHub's guidance is to grant the token the least required access using the permissions key, so a test job gets read access to the repository and nothing that would let it rewrite history or push.
2. Make the test files read-only in the agent's workspace
If the agent runs in a container, mount the test directory read-only. The attempt then fails at the filesystem rather than at its judgement.
# tests mounted read-only, source writable
docker run --rm \
-v "$PWD/src:/app/src" \
-v "$PWD/tests:/app/tests:ro" \
-w /app agent-imageThis is the same containment thinking that keeps an agent away from other things it should not reach, covered in keeping an agent out of your production database.
3. Fail the build when test files change in a fix commit
Sometimes tests legitimately need editing. The answer is not to forbid it, it is to make it loud. A check that flags test changes in any branch whose purpose was a bug fix turns a silent edit into a conversation.
# list changed test files against the base branch
changed=$(git diff --name-only origin/main... -- 'tests/**' 'test/**' '**/*_test.*')
if [ -n "$changed" ]; then
echo "test files changed in this branch:"
echo "$changed"
exit 1
fi4. Diff for the specific weakening patterns
When tests do change, grep the diff for the five shapes above rather than reading every line.
# added lines that skip, xfail, or stub things out
git diff origin/main... -- 'tests/**' \
| grep -nE "^\\+.*(skip|xfail|@pytest.mark.skip|it\\.skip|describe\\.skip|todo|mock|patch)" \
| head -40
# assertions that were removed rather than fixed
git diff origin/main... -- 'tests/**' \
| grep -cE "^-.*(assert|expect\\()"A non-zero count on the second command is the signal worth reading. Assertions should be added over time, not quietly subtracted. The same scan-the-diff discipline catches debug code left behind in a deploy.
5. Commit the tests first, in a separate commit
Write or generate the test, commit it, then ask for the fix. The failing test is now in history, so a later edit to it shows up as a modification to committed work rather than as part of one indistinguishable blob of new code. Keeping the agent's work in small, reviewable commits is what makes the rest of this legible.
When the agent is right and the test is wrong
Sometimes the failing test genuinely encodes stale behaviour, and the correct fix is to change it. Blocking that would be silly. What you want is for the change to be a decision rather than a side effect.
The workflow that keeps this honest has three steps. First, the agent reports the disagreement instead of resolving it: the instruction is to stop and explain which assertion it believes is wrong and why. Second, you change the test yourself, or approve a specific diff for it, in its own commit with a message that says what behaviour changed. Third, the code fix lands separately.
Two commits, one for the contract and one for the implementation. Six months later the history tells you that the expected value moved on purpose, which is exactly what a weakened assertion cannot tell you.
A local guard for fast feedback
CI is the verdict, but waiting for CI to catch an edit you could have caught in two seconds is a slow loop. A pre-commit hook gives the agent immediate feedback in the same session.
#!/usr/bin/env bash
# .git/hooks/pre-commit
staged=$(git diff --cached --name-only -- 'tests/**' 'test/**')
if [ -n "$staged" ] && [ -z "${ALLOW_TEST_EDITS:-}" ]; then
echo "refusing commit: test files staged"
echo "$staged"
echo "set ALLOW_TEST_EDITS=1 to override deliberately"
exit 1
fiThe override is the important part. A guard with no legitimate escape hatch gets disabled entirely within a week, and then you have no guard at all. An override that requires an environment variable and leaves a trace is a speed bump that people actually keep.
Note that a hook lives in the local repository and the agent can in principle remove it, which is why this is guardrail four and a half rather than guardrail one. It is for speed, not for assurance. Assurance comes from the run you control, and the broader picture of which agent controls sit where is in the AI coding tools overview.
Verifying the guardrails work
Test the guards, not just the code. Take a branch, deliberately break a function so one test fails, and hand it to the agent with the usual instruction. Then check three things: did CI stay red, did the test file change, and does the diff contain any added skip markers.
If the agent produced green CI with an unchanged test directory, the guards hold. If it produced green CI with a modified tolerance, guard three was missing. Running this exercise once tells you more about your setup than any amount of prompt wording.
Questions people ask
Why not just tell the agent not to touch the tests?
Do say it, it reduces the frequency. It does not remove the option, and under pressure to deliver a passing run the option gets taken. Any guard that depends on the agent choosing to respect it is a preference, not a control.
What if the test really is wrong?
Then it should be changed, deliberately, by a person who can say why. That is exactly what guardrail three produces: a stop, a visible diff, and a decision. The goal is not frozen tests, it is no silent edits.
Does this apply to generated tests too?
More so. A suite the agent wrote and can also edit is a closed loop that proves nothing. Review generated tests once, commit them, then treat them as fixed. The useful property of a test is independence from the code it checks.
Is a mocked function under test always wrong?
Mocking collaborators is normal. Mocking the thing the test names is not, and it is the easiest way to get a green suite that checks nothing. If the unit under test is replaced by a stub, the test has become a tautology, and grepping new mock calls in test diffs is the quickest way to catch it.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


