Test-Driven Development with LLM Agents
Session start
Run the full test suite before any new work. Report the baseline (total tests,
pass/fail counts). If any tests fail, fix those first.
Workflow: red/green/refactor
Execute one cycle at a time. Never batch.
RED — write one failing test
- Write exactly one test for the next behavior.
- Use a descriptive, behavior-focused name
(
should reject emails without @ symbol, not test case 3).
- Follow AAA: Arrange, Act, Assert.
- Run the test. Confirm it fails. If it passes, the test is wrong — it
is not exercising new behavior.
- Do NOT write any implementation code in this phase.
GREEN — minimal implementation
- Write the minimum code to make the failing test pass.
- Do NOT modify, delete, or skip any test. Test files are read-only.
- Do NOT add functionality beyond what the current test requires.
- Run all tests. All must pass.
- If a previously passing test breaks, fix the implementation — never the test.
REFACTOR — clean up under green
- Improve structure: remove duplication, improve naming, simplify logic.
- Do NOT add new behavior.
- Run tests after every change. All must stay green.
- Refactor implementation first (tests are the fixed reference), then
optionally refactor tests (implementation is the fixed reference). Never
both simultaneously.
Repeat
Return to RED with the next behavior. Continue until the feature is complete.
Hard constraints
- NEVER modify or delete a test to make it pass.
- NEVER write implementation without a corresponding failing test.
- NEVER skip the red phase (running the test to see it fail).
- NEVER add functionality beyond what the current test requires.
- NEVER proceed to the next test until the current cycle is complete.
- If you believe a test is wrong, explain why — but do not change it without
explicit approval.
Test quality
- Test the public interface, not implementation details.
- One behavior per test — single, unambiguous target.
- Use concrete input/output values, not abstract descriptions.
- Each test must be independent and self-contained.
- Start with happy-path behavior, then add edge cases in later cycles.
Generating a TDD plan
For larger features, generate a plan before starting cycles:
- Produce a numbered checklist of test descriptions, ordered simplest to most
complex. One behavior per item.
- Do not write any code yet.
- Get approval, then execute one test at a time via red/green/refactor.
Recovering from mistakes
- Agent wrote implementation before test: Stop. Delete the implementation.
Write the failing test first.
- Agent modified a test to pass: Revert the test change. Fix the
implementation instead.
- Test passes unexpectedly (no red): The test does not exercise new
behavior. Rewrite or discard it.
- Context window getting large: Summarize completed work. Re-run the full
test suite to re-anchor.
Reference material
- Principles and context: references/01-principles-and-context.md — why TDD fits agents, structural biases, foundational principles
- Workflow and test specifications: references/02-workflow-and-test-specifications.md — detailed phase descriptions, tests-as-prompts patterns, TDD plan generation
- Prompt templates and enforcement: references/03-prompt-templates-and-enforcement.md — AGENTS.md rules template, prompt templates, hooks, guards, git safety nets
- Anti-patterns and pitfalls: references/04-anti-patterns-and-pitfalls.md — ten common failure modes and their fixes
1---2name: tdd-with-llm-agents3description: Enforces strict red/green/refactor TDD discipline when writing code. Guides the agent through one-test-at-a-time cycles, prevents test subversion, and ensures minimal implementation. Use when writing code with TDD, doing test-driven development, implementing features test-first, or when the user mentions red/green TDD, failing tests first, or test-driven workflow.4---56# Test-Driven Development with LLM Agents78## Session start910Run the full test suite before any new work. Report the baseline (total tests,11pass/fail counts). If any tests fail, fix those first.1213## Workflow: red/green/refactor1415Execute one cycle at a time. Never batch.1617### RED — write one failing test18191. Write exactly **one** test for the next behavior.202. Use a descriptive, behavior-focused name21 (`should reject emails without @ symbol`, not `test case 3`).223. Follow AAA: Arrange, Act, Assert.234. Run the test. **Confirm it fails.** If it passes, the test is wrong — it24 is not exercising new behavior.255. Do NOT write any implementation code in this phase.2627### GREEN — minimal implementation28291. Write the **minimum** code to make the failing test pass.302. Do NOT modify, delete, or skip any test. Test files are read-only.313. Do NOT add functionality beyond what the current test requires.324. Run **all** tests. All must pass.335. If a previously passing test breaks, fix the implementation — never the test.3435### REFACTOR — clean up under green36371. Improve structure: remove duplication, improve naming, simplify logic.382. Do NOT add new behavior.393. Run tests after every change. All must stay green.404. Refactor implementation first (tests are the fixed reference), then41 optionally refactor tests (implementation is the fixed reference). Never42 both simultaneously.4344### Repeat4546Return to RED with the next behavior. Continue until the feature is complete.4748## Hard constraints4950- NEVER modify or delete a test to make it pass.51- NEVER write implementation without a corresponding failing test.52- NEVER skip the red phase (running the test to see it fail).53- NEVER add functionality beyond what the current test requires.54- NEVER proceed to the next test until the current cycle is complete.55- If you believe a test is wrong, explain why — but do not change it without56 explicit approval.5758## Test quality5960- Test the public interface, not implementation details.61- One behavior per test — single, unambiguous target.62- Use concrete input/output values, not abstract descriptions.63- Each test must be independent and self-contained.64- Start with happy-path behavior, then add edge cases in later cycles.6566## Generating a TDD plan6768For larger features, generate a plan before starting cycles:69701. Produce a numbered checklist of test descriptions, ordered simplest to most71 complex. One behavior per item.722. Do not write any code yet.733. Get approval, then execute one test at a time via red/green/refactor.7475## Recovering from mistakes7677- **Agent wrote implementation before test:** Stop. Delete the implementation.78 Write the failing test first.79- **Agent modified a test to pass:** Revert the test change. Fix the80 implementation instead.81- **Test passes unexpectedly (no red):** The test does not exercise new82 behavior. Rewrite or discard it.83- **Context window getting large:** Summarize completed work. Re-run the full84 test suite to re-anchor.8586## Reference material8788- **Principles and context**: [references/01-principles-and-context.md](references/01-principles-and-context.md) — why TDD fits agents, structural biases, foundational principles89- **Workflow and test specifications**: [references/02-workflow-and-test-specifications.md](references/02-workflow-and-test-specifications.md) — detailed phase descriptions, tests-as-prompts patterns, TDD plan generation90- **Prompt templates and enforcement**: [references/03-prompt-templates-and-enforcement.md](references/03-prompt-templates-and-enforcement.md) — AGENTS.md rules template, prompt templates, hooks, guards, git safety nets91- **Anti-patterns and pitfalls**: [references/04-anti-patterns-and-pitfalls.md](references/04-anti-patterns-and-pitfalls.md) — ten common failure modes and their fixes