Sub topic 10
Keeping the spec and the code in sync
This chapter assumes you can read Python and Git history, have written a requirements and tasks file as in earlier chapters, and have used an AI coding agent. It covers what happens after the first implementation, when the spec and the code start to disagree.
What spec drift is
Spec drift is the state where the spec says one thing and the code does another. It usually starts small. A reviewer asks for a 60-minute token lifetime, the agent changes a constant, and requirements.md still says 30 minutes. A later agent session reads the stale spec and "fixes" the code back.
Drift matters more with agents than with people. An agent treats the spec as ground truth. If the spec is wrong, the agent reproduces the error with confidence.
Where drift comes from
- Code changed first. A hotfix or a manual edit never reached the spec.
- The agent went beyond the task. It added behavior no requirement asks for.
- The spec was wrong. Implementation revealed a case the requirements missed.
- Tests were loosened. A failing test was edited to pass instead of the code being fixed.
Decide which side is wrong
When code and spec disagree, do not let the agent choose silently. Ask which side is correct, then change only that side.
When the code is right, change the spec first and the tests second. This order keeps the spec as the source of truth. A change that starts in the code makes the spec a record written afterward.
Use tests to detect drift
Tests from the acceptance criteria are your cheapest drift detector. If a criterion says tokens expire after 30 minutes and a test checks it, a code change to 60 minutes fails the test. The failure forces a decision.
from datetime import datetime, timedelta, timezone
TOKEN_TTL = timedelta(minutes=30)
def is_expired(issued, now):
return now - issued > TOKEN_TTL
def test_ttl_matches_requirement_1_1():
# Requirement 1.1: link expires after 30 minutes.
assert TOKEN_TTL == timedelta(minutes=30)
def test_link_valid_at_29_minutes_and_expired_at_31():
t0 = datetime(2026, 1, 1, tzinfo=timezone.utc)
assert not is_expired(t0, t0 + timedelta(minutes=29))
assert is_expired(t0, t0 + timedelta(minutes=31))Name tests after the requirement they cover, as test_ttl_matches_requirement_1_1 does. When a test fails, you can open the exact criterion it protects.
Habits that prevent drift
- Keep the spec in the same repository. Put it under
specs/<feature>/so a pull request shows spec and code changes together. - Review spec changes like code. A diff that touches code but not the spec, for a behavior change, is a warning sign.
- Add a rule to the instruction file. Tell the agent to report a mismatch and stop.
- Close the loop at the end of each task. Ask the agent to list any behavior it added or changed that is not in the spec.
- Record the reason for a change. A one-line note in the design document explains why the value changed.
A rule for the project instruction file:
## Keeping the spec in sync
- If the code and the spec disagree, stop and report the difference.
- Do not edit `requirements.md` or weaken a test without asking.
- When a task is done, list any behavior you added that no requirement covers.Handling a change request mid-project
Suppose the 30-minute expiry must become 60 minutes. The order is:
- Edit Requirement 1.1 to say 60 minutes.
- Update the test to 60 minutes and confirm it fails.
- Ask the agent to change the code until the test passes.
- Review the diff for unrelated changes.
The failing test in step 2 shows the test actually checks the criterion. A test that still passes after the requirement changes was never checking it.
Exercise
A reviewer approves a pull request that changes a token lifetime constant from 30 to 60 minutes. The tests still pass, and requirements.md still says 30 minutes. What two problems does this show, and what should you do?
Show answer
First, the spec and code disagree, which is drift. Second, the tests pass, so no test checks the 30-minute criterion, or a test was loosened. Decide which side is correct. If 60 minutes is intended, update Requirement 1.1 first, then update or add a test that fails against 30 and passes against 60. If 30 is correct, revert the code change.