Test-Driven Development with PARALLEL subagent delegation + Core Plan v1 evidence tracking. RED-GREEN-REFACTOR cycle with timestamped artifacts and 7-day cleanup.
MANDATORY WORKFLOW: Write tests BEFORE changing code.
Test-Driven Development for new features AND refactoring. Write tests first, then code. Delegates to subagents (tdd-test-writer, tdd-implementer, tdd-refactorer) in PARALLEL for independent tasks.
Integrates with /test (coverage analysis), /verify (run tests), /qa (certification). Part of testing and quality ecosystem.
When /tdd is consuming an existing plan rather than discovering tests from scratch, it should use the same shared plan-consumer validation as /code before trusting the plan:
python - <<'PY'
from contract_primitives import validate_plan_for_execution
result = validate_plan_for_execution(
"plan.md",
consumer="/tdd",
require_implementation_ready=False,
required_phase=1,
)
print(result)
PY
/tdd may proceed from a phased-ready plan only if the validated phase_ready_through threshold actually covers the TDD phase being executed. Without explicit phase context, the local consumer gate defaults to Phase 1. Otherwise route back to /planning.
Before writing tests, search for existing patterns:
/search "{feature} test patterns" --backend chs,cks,code
/search "{language} testing framework" --backend chs,cks,docs
/search "{component} tests" --backend code,cks
Search accelerates TDD by finding existing patterns, frameworks, fixtures, and anti-patterns.
0. DISCOVER -> Understand code (read first, THEN run tests)
1. RED -> Write failing test (PARALLEL tdd-test-writer)
2. GREEN -> Implement minimal code (PARALLEL tdd-implementer)
3. VERIFY -> Run ACTUAL command (not dry-run, not mocks)
4. REGRESSION -> Run related tests (automatic)
5. REFACTOR -> Clean up while tests pass (PARALLEL tdd-refactorer)
See references/parallel-delegation.md for phase-by-phase subagent delegation patterns and examples.
See references/discovery-and-regression.md for DISCOVER phase details and REGRESSION targeting.
See references/verify-phase.md for VERIFY phase requirements, plan discovery, and evidence format.
See references/evidence-collection.md for Core Plan v1 evidence tracking API and per-phase collection.
See references/workflow-variants.md for bug-fixing workflow and completion format.
| Step | New Feature | Refactoring | Agent Pattern |
|---|---|---|---|
| 0. DISCOVER | Read code, then baseline | Read code, then baseline | general-purpose x N (parallel) + You |
| 1. RED | Write test x N (one per case) | Write test x N (one per file) | tdd-test-writer x PARALLEL |
| 2. GREEN | Implement x N (one per file) | Extract/simplify x N (one per file) | tdd-implementer/tdd-refactorer x PARALLEL |
| 3. VERIFY | Run command | Run command | You |
| 4. REGRESSION | Auto-runs | Auto-runs x N (if failures) | Automatic |
| 5. REFACTOR | Clean up x N (one per cleanup) | Clean up x N (one per cleanup) | tdd-refactorer x PARALLEL |
Rule: If tasks are independent, launch ALL in parallel. No upper limit.
Testing Framework Syntax Verification (RED Phase):
Before writing framework-specific tests, invoke /context7 to verify current syntax. This is mandatory for pytest, Django, vitest, jest, and any rapidly-evolving framework.
When: Every RED phase involving framework-specific test code (pytest fixtures, Django test client, React Testing Library, etc.)
Query expansion pattern:
code_only (familiar patterns); full (first contact with framework)Examples:
| Framework | Query Expansion |
|---|---|
| pytest | "pytest fixtures dependency injection with function scope examples" |
| Django | "Django test client assert methods for status codes and JSON responses" |
| vitest | "vitest mocking modules with vi.mock and vi.fn() examples" |
| jest | "jest.spyOn and mock functions for async API testing" |
Before locking in tests or claiming coverage, /tdd should run a short internal test-truth check:
These are internal self-check prompts. They are not default user-facing questions and should only surface to the user when /tdd is genuinely blocked and cannot proceed safely without clarification.
/tdd should run a minimal real execution that proves the tests are attached to actual behavior, not just mocks or overly narrow assertions.
Required for:
The smoke proof should answer:
/tdd may keep this lightweight, but it must not skip it on high-risk behavioral changes.
/tdd should use a critique/review agent when test design is likely to miss the real contract or bless a workaround.
Escalate to a critique agent when:
The critique agent should focus on false confidence, missing failure paths, and tests that would pass while the real bug remains.
Activates for: "implement X", "refactor X", "add feature X", "reduce complexity of X", "X has CC N", "fix X bug", "X broken/error/crash"
Does NOT trigger for: Documentation changes, configuration updates, reading/analyzing code.
| Pattern | Intent | Workflow Variant |
|---|---|---|
| "fix X", "bug in X", "X broken/error/crash" | Bug fix | Bug-fixing workflow (see references/workflow-variants.md) |
| "implement X", "add feature X", "new feature" | New feature | Full RED->GREEN->REFACTOR |
| "refactor X", "simplify X", "reduce complexity" | Refactoring | Refactoring workflow |
| Subagent | Best For | Context Required |
|---|---|---|
tdd-test-writer | RED phase - failing tests | Spec, desired behavior |
tdd-implementer | GREEN phase - minimal code | Failing test, clear spec |
tdd-refactorer | REFACTOR - cleanup while tests pass | Working code + tests |
code-reviewer | Bug detection, security, quality | Code only (fresh eyes) |
code-critic | Root cause analysis | Code + error description |
code-explorer | Architecture, dependency mapping | Module path or pattern |
general-purpose | Multi-step, research | Clear task description |
| Don't | Why |
|---|---|
| Change code without tests | Can't detect behavior changes |
| Skip verification | May have broken something |
| Change logic while refactoring | Mixing refactoring with changes |
| Write tests after code | Defeats TDD philosophy |
| Run subagents sequentially when parallel is possible | Wastes time and token efficiency |
P:/.claude/skills/tdd/gap_loader.py{project_root}/.claude/state/test_gaps/*.json
{terminal_id}_gaps_READY.json_READY.json{terminal_id}_gaps_CONSUMED.jsontests/test_refactor_safety.pytests/test_[feature].pytests/test_[module].pyP:/.claude/tdd-guard/data/test.jsonP:/.claude/tdd-state.jsonEnable TDD enforcement: TDD_ENABLED=1
Bypass TDD (not recommended): TDD_BYPASS=1
Reference files in references/:
| File | Contents |
|---|---|
parallel-delegation.md | Phase-by-phase subagent delegation patterns, strategies, and end-to-end examples |
discovery-and-regression.md | DISCOVER phase details, /t integration, gap loading, REGRESSION test targeting |
evidence-collection.md | Core Plan v1 evidence tracking API, per-phase collection, 7-day cleanup policy |
verify-phase.md | VERIFY phase requirements, plan discovery, evidence format, prohibited responses |
workflow-variants.md | Bug-fixing workflow, closure protocol, completion/summary format |
External: