Skip to content
mentoor

Topic 5 of 5 · 5 min read

Keeping specs versioned with the code

Goal: Decide when a code change must update its spec and edit the spec so spec, tests and code agree again.

Keep the spec in the repo next to the code, change it in the same pull request as the behaviour, and treat a stale spec as a bug. A spec that no longer matches the code misleads every future agent run.

Check yourself · 1 of 3

A pull request changes a rate limit from 5 attempts to 3. What else should be in the same pull request?

Choose one answer

Why it matters

Agents read files in the repo, so a spec file there becomes input to every later change. If it is accurate, it saves you re-explaining. If it is stale, the agent trusts it and rebuilds behaviour you removed last month. A spec in a chat window or a wiki nobody opens has the same problem faster.

How to do it

  1. Put specs where the code is. For example docs/specs/login-limit.md, one file per feature. Point to the folder from AGENTS.md or CLAUDE.md so every session finds it.
  2. Use a tool if it helps. Spec Kit adds commands for spec, plan and tasks files. Kiro keeps requirements, design and tasks files for each feature. Both are optional. Plain markdown with the same structure works, and Martin Fowler's review of these tools notes that the discipline matters more than the tool.
  3. Change spec and code together. If a pull request changes behaviour, the spec change is in the diff. Reviewers check three things agree: spec, tests and code.
  4. Decide when to update. Update the spec when a requirement, scope line, constraint or acceptance criterion changes. A refactor with identical behaviour needs no spec change.
  5. Version through git. The history of the spec file shows when and why a rule changed. Do not keep spec-v2-final.md copies.

Before and after

An agent changed the lockout from 5 failed logins to 3 at the owner's request. The code and test changed, but the spec still says 5.

--- a/docs/specs/login-limit.md
+++ b/docs/specs/login-limit.md
@@ -9,3 +9,3 @@
 ## Acceptance criteria
-1. Given 5 failed logins for one email in 10 minutes, when a 6th is sent, then the response is 429.
+1. Given 3 failed logins for one email in 10 minutes, when a 4th is sent, then the response is 429.

Now the spec, the test and the code all say 3. The next agent run will not restore 5.

What goes wrong

  • Spec drift. Code changes in a hurry and nobody edits the spec. Months later an agent "fixes" the code to match the old spec.
  • Spec as a one-time form. It is written to get the first version built, then abandoned.
  • Updating the spec to match a bug. Edit the spec only when the behaviour change is intended. If the code is wrong, fix the code.
  • Too many specs. Dozens of overlapping files give the agent conflicting input. Merge or delete what is obsolete.

Examples

When to update the spec

Change Update spec?
Lockout threshold from 5 to 3 Yes, a criterion changed
Rename an internal helper No, behaviour is the same
Add a Retry-After header Yes, a new observable behaviour
Fix a bug where the counter never reset No, the spec was right; the code was wrong

Pointing agents at the specs

# AGENTS.md
## Specs
Feature specs live in docs/specs/. Read the matching file before changing a feature.
If a change alters behaviour, update the spec in the same commit.

Key points

  • Store each spec as a file in the repo and point to the folder from AGENTS.md or CLAUDE.md so agents find it.
  • Change the spec in the same pull request as the behaviour it describes, and review spec, tests and code together.
  • Update the spec when a requirement, scope, constraint or acceptance criterion changes; a pure refactor needs no spec change.
  • Tools such as Spec Kit and Kiro help with structure, but the habit of keeping spec and code in step matters more.
  • Edit the spec only for intended changes; if the code is wrong, fix the code instead.

Common mistakes

  • Letting code change without updating the spec, so the next agent run restores the old behaviour.
  • Editing the spec to match a bug instead of fixing the code.
  • Keeping specs only in chat history or a wiki that agents and reviewers never open.
  • Leaving obsolete and overlapping spec files in the repo, which gives agents conflicting instructions.

Check yourself · 2 of 3

Which change does NOT require a spec update?

Choose one answer

Check yourself · 3 of 3

Why point to the specs folder from AGENTS.md or CLAUDE.md?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic