Skip to main content

Spec-Driven Development

Why This Matters

The quality of an agent’s work is limited by the quality of the request. For anything bigger than a small change, a short written spec beats a long prompt: it is reviewable before work starts, it gives the agent a stable reference, and it becomes the source for tests and the definition of done.

What a spec for an agent looks like

A spec for the flaky history panel: goal, five numbered acceptance criteria, out of scope and a done-when section that maps criteria to unit and e2e tests

Keep specs in the repository, for example in docs/specs/, and use the same sections every time:

# Spec: <feature>

## Goal
One or two sentences: who gets what, and why.

## Acceptance criteria
Numbered, observable, testable. One behaviour per line.

## Out of scope
What we deliberately do not build now.

## Done when
Which criteria are covered by which tests, and which gates must be green.

And this is that spec implemented, on the solution branch solution/lesson-22-flaky-history:

The real flaky history panel for login.spec.ts: twenty dots with two red failures, pass rate 85.0% and last failure 25 Nov 2026, with a Close button

Write specs an agent can execute reliably

  • Observable criteria: “panel closes with Esc”, not “panel is user-friendly”.
  • Numbers and examples: “last 20 runs”, “pass rate with one decimal”, “No runs recorded yet”.
  • Edges included: empty, error, too much data, no permission.
  • Out of scope stated: the best protection against scope drift (lesson 12, Failure Modes of AI-Generated Code).
  • Tests mapped to criteria: so you can see which criterion is not covered yet.

From spec to tested change

  1. Draft the spec with the agent. Ask it to interview you and propose criteria, then edit them yourself. You own the spec.
  2. Review the spec with a tester or product owner before any code. This is the cheapest review in the whole process.
  3. Plan from the spec (plan mode): “Plan the implementation of docs/specs/flaky-history.md.”
  4. Tests from the criteria first (lesson 13, Test-First with an Agent (TDD)), then implementation.
  5. Close the loop: in the PR, list each criterion with the test that covers it.

Hands-on

  1. Write a spec for your next feature using the template.
  2. Ask the agent which criteria are ambiguous or untestable, and fix them.
  3. Generate tests from the criteria, then implement.
  4. Add the spec template to your repository, and a rule to CLAUDE.md: “For features larger than one file, ask for or write a spec in docs/specs/ first.”

Walkthrough in TestRun Board

  1. Compare the three specs in docs/specs/. Each follows _template.md: goal, numbered acceptance criteria, out of scope, done when, open questions.
  2. Use the write-spec skill for a new feature: “Write a spec for a suite filter (unit, api, e2e) on the runs page.” It should ask you questions first and stop after writing docs/specs/suite-filter.md.
  3. Review the spec: are the criteria observable (“shows Unit (2)”), are the empty and URL cases covered, is “Out of scope” filled?
  4. Ask a second session to find ambiguities: “Which criteria in docs/specs/suite-filter.md are ambiguous or untestable?” Fix them yourself.
  5. Look at how the worked solution for the flaky history panel closes the loop: docs/solutions/lesson-22-flaky-history.md maps every acceptance criterion to the unit, component and e2e tests that cover it.

Key Takeaways

  • A short spec in the repository beats a long prompt for anything non-trivial.
  • Acceptance criteria must be observable, concrete and include edge cases.
  • “Out of scope” prevents scope drift; “Done when” maps criteria to tests.
  • The spec review is the cheapest quality gate you have.