TDD Red Green
Implement one observable behavior at a time through a verified RED → GREEN → REFACTOR loop. Treat no change as a correct outcome when the reported bug does not reproduce.
Do not use this workflow for documentation-only, copy-only, formatting-only, or other changes that cannot alter behavior.
Establish Safe Ground
- Read repository instructions, the requested acceptance criterion, test configuration, and project-defined test commands.
- Inspect
git statusand relevant diffs before editing. Separate pre-existing user changes from paths owned by this task; preserve all user work. - Classify the request as a bug, a new behavior, or behavior that may already be correct. Stop and clarify contradictory or untestable criteria.
- Select one observable behavior and the narrowest appropriate test level. Split broader work before writing code.
- Read TDD handoff and checkpoints when handing off a critical acceptance test, recording cycle evidence, recovering from a failed attempt, or pausing work.
Reproduce or Abstain for Bugs
- Translate the report into a relevant automated test that would fail only when the reported defect exists.
- Run the test against the unchanged implementation.
- Continue only when the test fails for the intended behavioral reason.
- If current behavior satisfies the criterion, stop without modifying production code. Report the test or reproduction command, observed output, and why it proves a no-change result.
- If the defect cannot be reproduced reliably, stop and report the missing evidence or environment requirement. Do not invent a patch.
Run One RED → GREEN → REFACTOR Cycle
RED
- Add one focused test for the selected behavior. For a critical acceptance criterion, use a fresh test-author or session when available; provide only the criterion, public contract, repository test conventions, and allowed test paths—not an implementation plan or prior implementation history.
- Run the narrowest command that executes the new test.
- Confirm that the intended assertion fails because the behavior is absent or wrong. Reject syntax errors, import failures, broken fixtures, unrelated failures, and tests that pass immediately as RED evidence.
- Record the command, expected assertion, actual failure, and causal explanation before changing production code.
GREEN
- Implement the smallest change that satisfies the failing test.
- Keep the test unchanged. Never delete, skip, weaken, over-mock, or rewrite it merely to obtain green.
- Run the focused test after each logical implementation attempt until it passes for the intended reason.
- If the test is logically wrong, conflicts with the accepted criterion, or tests an impossible contract, stop and escalate with evidence. Do not edit it just to pass.
REFACTOR
- Refactor only after the focused test is green.
- Remove only duplication or complexity introduced by the owned change; avoid unrelated cleanup and speculative abstractions.
- Re-run the focused test after each structural change. Restore green before continuing.
Verify and Report
- Re-run the focused test from a clean command invocation.
- Run the project-defined broader checks appropriate to the changed surface, such as the related suite, full tests, type checking, linting, and build.
- Compare the final diff with the single behavior and acceptance criterion. Confirm that no test was weakened and no unrelated file changed.
- Report fresh commands, exit status, test counts or key output, the RED failure reason, the GREEN evidence, and any checks not run.
- Start another cycle only for the next independently testable behavior.
Protect Work and History
- Never run
git reset --hard. - Preserve the diagnostic RED command and output before undoing an attempt.
- Undo only changes owned by this task. Never restore, delete, clean, or overwrite a file containing pre-existing user work; use surgical edits or ask for help when ownership overlaps.
- Create checkpoints, commits, branches, pull requests, or pushes only with explicit user authorization and only for owned paths.
- Leave a resumable handoff when interrupted while RED or GREEN is incomplete.