Sub topic 3
The Spec-Driven Workflow: Requirements, Design, Tasks, Implementation
This chapter assumes you can write and test code in at least one language, use Git, have prompted an AI coding assistant before, and know what an automated test is. Earlier chapters covered why open-ended prompts drift. This chapter covers the workflow that replaces them.
The workflow in one view
Spec-driven development splits a feature into four phases. Each phase produces a file that the next phase reads.
- Requirements state what the system must do, in terms you can observe and test.
- Design states how the system will do it: components, data, interfaces, and the decisions behind them.
- Tasks break the design into small, ordered steps. Each step links back to the requirements it satisfies.
- Implementation is the agent working through one task at a time, with tests and a human checking each result.
The ordering matters because each phase narrows the space of acceptable answers. After requirements, the agent knows the target. After design, it knows the shape of the solution. After tasks, it knows the next small step. A single large prompt asks the agent to make all of those decisions at once and silently.
Where the files live
Keep each feature's spec in version control, next to the code. One directory per feature works well.
specs/
password-reset/
requirements.md
design.md
tasks.md
src/
tests/
AGENTS.mdPutting specs in Git gives you three things. Reviewers can comment on a spec in a pull request. The history shows when and why a requirement changed. The agent can read the files directly, so you do not paste them into every prompt.
Phase 1: Requirements
Requirements describe observable behavior and say nothing about implementation. A requirement has a user story, which gives the purpose, and acceptance criteria, which define done. A common phrasing for criteria is EARS style: WHEN <event> THEN the system SHALL <response>, or IF <condition> THEN the system SHALL <response>.
### Requirement 1: Password reset
**User story:** As a registered user, I want to reset my password by email, so that I can regain access to my account.
#### Acceptance criteria
1. WHEN a user submits a registered email address THEN the system SHALL send a reset link that expires after 30 minutes.
2. WHEN a user submits an unregistered email address THEN the system SHALL return the same response as for a registered address.
3. IF a reset link has already been used THEN the system SHALL reject any further use of that link.Each criterion states one behavior with a concrete value (30 minutes) or a comparison (same response). Criterion 2 is the kind of line an agent would not add on its own. It prevents an attacker from learning which emails have accounts. The requirements phase is where such decisions are made explicitly.
A good test of a criterion is whether you could write a failing test for it before any code exists. If you cannot, the criterion is too vague. "The reset flow should be secure" fails this test. "The link SHALL expire after 30 minutes" passes it.
Phase 2: Design
The design document answers the question "how", within the limits set by the requirements. It is where you record decisions that the agent would otherwise make by default and that you would later have to discover by reading code.
A useful design covers:
- Components and boundaries. Which modules exist and what each is responsible for.
- Data model. What is stored, and which fields enforce a requirement (for example, an issue time and a used flag for reset tokens).
- Interfaces. Function signatures, endpoints, and error behavior.
- Decisions and alternatives. What you chose, what you rejected, and why.
- Testing approach. What is unit tested, what needs integration tests, and what is not tested.
A short excerpt shows the level of detail to aim for:
## Design: Password reset
### Components
- `ResetTokens` (src/reset_tokens.py): issues and redeems tokens. No I/O.
- `reset_request` endpoint (src/api/reset.py): validates the email, calls `ResetTokens`, queues the email.
### Data
- Token: random value, issue time (UTC), used flag.
### Decisions
- Store tokens server-side instead of signing them. Rejected signed tokens because
Requirement 1.3 needs single use, which needs server state anyway.
- The endpoint always returns HTTP 202 with the same body (Requirement 1.2).
### Testing
- `ResetTokens` is tested with an injected clock, so tests do not sleep.Notice the injected clock. A design choice made here (pass now as an argument) is what makes the 30-minute expiry testable later. Design decisions often exist to make requirements verifiable.
Phase 3: Tasks
The task list turns the design into steps small enough for the agent to finish and for you to review in one sitting. Each task names the work, the expected result, and the requirements it covers.
## Tasks
- [ ] 1. Add a reset token store
- Store the token, its issue time, and a used flag.
- Covers: Requirement 1.1, 1.3
- [ ] 2. Implement token redemption
- Reject expired tokens and tokens that were already used.
- Write tests first. Covers: Requirement 1.1, 1.3
- [ ] 3. Add the reset-request endpoint
- Return the same response for registered and unregistered emails.
- Covers: Requirement 1.2Check three properties when you review a task list:
- Ordered by dependency. Task 3 uses the store from task 1, so it comes later.
- Traceable. Every requirement appears in at least one "Covers" line, and every task covers at least one requirement. A task that covers nothing is probably scope creep. A requirement that no task covers will not get built.
- Small enough to verify. A task that touches ten files or mixes unrelated concerns is hard to review. Split it.
Phase 4: Implementation
Implementation is a loop. For each task, in order:
- Tell the agent which task to do, for example "Do task 2 in
specs/password-reset/tasks.md". - The agent reads the requirements, design, and task, and then changes code.
- Run the tests. Read the diff. Compare it with the task and the criteria it covers.
- If it is acceptable, mark the task done, commit, and move to the next one. If it is not, either correct the agent or fix the spec.
Committing after each task gives you a clean revert point. If task 4 goes wrong, you discard one commit and keep the first three.
Doing one task per session or per request keeps the agent's context focused. A request like "implement all the tasks" brings back the problem you were trying to escape: many decisions made at once, with a diff too large to review properly.
Moving backward
The workflow is not a waterfall. Implementation often exposes problems in earlier documents, and the correct response depends on where the problem lives.
| Symptom during implementation | Likely source | Action |
|---|---|---|
| The agent asks what "expired" means | Requirement is ambiguous | Fix the requirement, then check that design and tasks still match |
| The agent builds something that satisfies the text but not your intent | Requirement is incomplete | Add the missing criterion and a test for it |
| The planned data model cannot support a criterion | Design flaw | Update the design, then revise affected tasks |
| The agent changes files outside the task | Task or instructions too loose | Tighten the task scope or add a rule to the project instructions |
| A task is too big to review | Task breakdown | Split it before continuing |
The rule is to change the document that is wrong before changing the code. If you patch the code and leave the spec alone, the next agent session will read the old spec and reproduce the old behavior.
Common errors in applying the workflow
- Requirements that describe implementation. "Use a
tokenstable with three columns" is design. Putting it in requirements locks in a choice before you have weighed alternatives. - Design that restates requirements. If the design adds no decisions, interfaces, or data structures, it is not helping the agent. Cut it or make it specific.
- Skipping approval. If you accept each document without reading it, the agent's assumptions become your spec. Review each document before starting the next phase.
- Tasks without a link to requirements. You lose the ability to check coverage, and tests cannot be traced back to behavior.
- One huge task. The agent works for a long time, produces a large diff, and you review it by skimming.
- Never updating the files. The workflow only helps while the documents describe the current system. Later chapters cover keeping them in sync.
Check your understanding
Exercise
A reviewer reads your tasks.md and finds that Requirement 1.2 (same response for registered and unregistered emails) is not listed in any task's "Covers" line, while task 4 ("Add rate limiting") covers no requirement. Name the problem each finding indicates, and say which document you would change first to resolve them.
Show answer
Requirement 1.2 with no task means the behavior will not be implemented, so the task list has a coverage gap. Task 4 with no requirement means either unrequested scope or a missing requirement. Decide whether rate limiting is wanted. If it is, add a requirement with a testable criterion (for example, a limit on reset requests per address per hour), then update the design if needed, and then link task 4 to it. If it is not wanted, remove task 4. In both cases, add or adjust a task so that Requirement 1.2 is covered, for example the endpoint task. Change the requirements first, because the design and tasks derive from them.
Exercise
During task 2, the agent reports that the design stores only a token and an issue time, so it cannot reject a reused token. Which phase should change, and which other documents must you re-check?
Show answer
Change the design first, because the data model cannot support Requirement 1.3. Add a used flag, or an equivalent, to the stored data. Then re-check tasks.md so that task 1 stores the new field and task 2 tests for it. The requirements stay as they are, since they were correct. Commit the spec change before asking the agent to continue.