Sub topic 10
Keeping the agent from editing the tests
This chapter assumes you can write and run unit tests, read a Git diff, and have used a coding agent. It builds on the failure modes from the previous chapter. Here you will see how to stop an agent from changing the tests it is supposed to satisfy.
Why tests need protection
In test-driven development (TDD), the test is the specification. The agent's job is to change the implementation until the specification passes. An agent measures success by the test runner's output. The shortest route to a green result is sometimes to change the test instead of the code.
Common ways this happens:
- Editing an assertion so it matches whatever the code now returns.
- Adding a skip marker such as
@pytest.mark.skip. - Deleting a failing test.
- Loosening a check, for example replacing
==with a membership test.
All of these produce a passing run, and none of them solve the problem. If you do not look at the diff, the green output looks the same as a real fix.
Layer 1: state the rule in the instructions
Start with a written rule. The working agreement from earlier in this tutorial already contains it:
5. Do not edit, skip, or delete existing tests to make them pass. If a test seems wrong, stop and explain why.
6. Do not hardcode expected values from tests into the implementation.The second sentence of rule 5 matters. A test can be wrong, and the agent needs a legitimate way to say so. If the only options are "pass" or "break the rule", the agent has no honest path. Telling it to stop and explain gives it one, and you make the decision.
Instructions are a request, not a guarantee. An agent can overlook them in a long session, so add checks that do not depend on its cooperation.
Layer 2: make the tests hard to change
Use file permissions or tool settings so the agent cannot write to the test files during the green phase. Options include:
- Marking test files read-only for the session.
- Configuring your agent tool to deny writes to a
tests/directory. - Giving the agent a separate step for writing tests, and a different step for implementation that allows edits only under
src/.
The last option follows the cycle naturally. You write or approve the red test, lock it, then let the agent work on the implementation.
Layer 3: detect changes in the diff
Even with rules and permissions, check what changed. After each cycle, list which files the agent touched:
git diff --statDuring a green or refactor step, no test file should appear in this output. You can turn that into an automatic check:
git diff --name-only | grep -E '^tests?/|test_.*\.py$' && echo "Test files changed" || echo "No test files changed"The grep command exits with success when it finds a match, so the message after && prints only when a test file changed. If you see that message during a green step, read the test diff before accepting anything.
You can also look for the telltale words in the diff itself:
git diff | grep -E '^\+.*(skip|xfail|pytest\.mark)'The protection flow
When the test really is wrong
Sometimes the agent is right. A test may contain a typo, or the requirement may have changed. Handle this as its own step:
- The agent stops and explains why the test is wrong.
- You confirm the explanation against the requirement.
- The test change is made and committed on its own, separate from any implementation change.
- The cycle restarts from red.
A separate commit keeps the history honest. A reviewer can see that a test changed on purpose, instead of finding it buried in a large implementation diff.
Another safeguard: independent runs
Your local checks protect against mistakes you can see. A continuous integration (CI) job that runs the suite from a clean checkout adds a check the agent did not control. A later chapter covers that setup.
Exercise
During a green step, the agent reports all tests pass, but git diff --stat shows a changed file tests/test_slugify.py. What should you do, and what should the agent do when it believes a test is wrong?
Show answer
Do not accept the result yet. Read the diff of the test file to see whether an assertion was weakened, skipped, or removed, and revert the change if so. When the agent believes a test is wrong, it should stop and explain why instead of editing the test. You then decide, make any test change in a separate commit, and restart the cycle from red.