core/capabilities/execution/tdd/SKILL.md
Enforces test-driven development: write failing test, write minimal code to pass, refactor. Mandatory for all implementation work. Use when writing any production code, implementing features, fixing bugs, refactoring, or when the user says "write code", "implement", "fix this", or "add a feature". Code written before its test is deleted.
npx skillsauth add xoai/sage tddInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Write the test first. Watch it fail. Write minimal code to pass. Refactor. Commit.
The Iron Law: If nobody watched the test fail, you don't know if it tests the right thing. The failure must have been observed, with evidence. Almost always that observer is you, this session — see "Inherited red" for the single narrow case where a prior session already banked the observation. Violating the letter of these rules IS violating the spirit.
ALWAYS. Every time you write production code. Every feature. Every bug fix. Every refactor that changes behavior. The urge to skip "just this once" is a rationalization signal, not a legitimate exception.
RED → VERIFY FAIL → GREEN → VERIFY PASS → REFACTOR → VERIFY STILL PASS → COMMIT → NEXT
Write one minimal test that describes the expected behavior.
Rules:
rejects expired tokens not test authRun the test. It MUST fail. If it passes on first run, the test is suspect — it's either testing something that already works (useless) or testing the wrong thing (dangerous). Investigate before proceeding.
The failure must be the RIGHT failure — the test should fail because the feature doesn't exist yet, not because of a syntax error or wrong import.
Write the SIMPLEST code that makes the test pass. Nothing more.
Rules:
Run ALL tests. The new test must pass. All existing tests must still pass. If any test fails, fix it before proceeding. Never leave tests red.
Lean cadence on resume (resume_test_cadence, default lean): on a resume
close-out only, run the targeted test(s) for the code you just wrote here,
not the whole suite — the whole suite runs once at the close-out verification
(Gate 5), which is where a mid-batch regression is caught. Set
resume_test_cadence: full, or on any first-session build, to run ALL tests at
every GREEN as above. The closing full-suite run is never skipped. See
cycle-protocol.md § Resume close-out economy.
Now — and ONLY now — you may improve the code. Remove duplication, extract helpers, rename for clarity, simplify logic.
Rules:
Commit with a semantic message referencing what behavior was added. Then start the next RED cycle for the next behavior.
If production code was written before its corresponding test existed and was observed failing: DELETE THE CODE. Implement fresh from tests only.
Delete means delete. Not "comment out." Not "keep as reference." Delete. Then write the test. Watch it fail. Then rewrite the code.
This is not punitive. Code written without tests cannot be trusted. The cost of rewriting with TDD is lower than the cost of debugging code you can't verify.
"But I already wrote 200 lines!" — Sunk cost fallacy. The time is gone. Your choice now:
The "waste" is keeping code you can't trust.
Tests written after code answer: "What does this code do?" Tests written before code answer: "What should this code do?"
Tests-after pass immediately. Passing immediately proves nothing — you never see them fail, so you don't know if they test the right thing. Tests-after also test implementation details instead of behavior, making them brittle.
Before declaring implementation complete, verify ALL of these:
Can't check all boxes? You skipped TDD. Start over from the first unchecked item.
| Rationalization | Why It's Wrong | What to Do | |----------------|---------------|------------| | "This is too simple to test" | Simple bugs cause outages. Tests are fast for simple code. | Write the test. It'll take 30 seconds. | | "I'll write tests after" | Tests-after test implementation, not behavior. They prove nothing. | Delete code. Write test first. | | "Just this once" | That's what everyone says every time. It's never just once. | Follow the process. It's faster than debugging. | | "I'm just refactoring" | If behavior changes, you need a test. If it doesn't, existing tests suffice. | Run existing tests. If they break, you're changing behavior — write a test. | | "Manual testing is fine" | You think you tested everything. You didn't. You can't remember what you tested tomorrow. | Automated tests are repeatable. Manual tests are guesses. | | "The deadline is tight" | TDD is faster than debugging untested code under deadline pressure. | Skipping TDD makes deadlines worse, not better. | | "It's just a config change" | Config bugs are the hardest to debug and the easiest to test. | Write a test that verifies the config does what you expect. |
This is NOT a way to skip the red-confirm. It is the recognition that on a resumed cycle, the red-confirm may have already happened, in the session that wrote the test — and re-watching it adds no information, only a full-suite run that the 2026-07-15 resume profile counted as 2.5× the test runs of a bare agent.
The carve-out applies only when ALL of these hold (trust_inherited_red,
default true; set false to always re-confirm):
manifest.py resume produced the brief).
A first-session build is never inherited red — you write the test, you watch
it fail, always.When all three hold: you may go straight to GREEN — write the minimal code, then VERIFY PASS (run the test, watch it pass). You still confirm green; you skip only the re-run whose sole purpose was to re-witness a failure already witnessed.
Fall back to the full RED → VERIFY-FAIL cycle the moment any condition breaks: the named test is absent from the tree, or it is unexpectedly green before you write code (a green inherited test is exactly the "testing something that already works / testing the wrong thing" trap — investigate it, do not trust it), or you find yourself adding a new assertion (that is a new RED, and it is yours to watch). A test THIS session authors always goes through the full cycle. When in doubt, re-confirm — the re-run is cheap insurance against building on a test that never actually failed.
The ONLY way to skip TDD is an explicit human override:
Human: "skip-tdd for this change"
The waiver is logged in .sage/decisions.md with reason and scope.
The resulting code is flagged as untested technical debt for follow-up.
The agent MUST NOT suggest or encourage waivers.
RED: test('login rejects expired JWT tokens', () => { ... })
→ Run → FAILS (expected: 401, got: 200) ✓ Correct failure
GREEN: Add expiry check to auth middleware
→ Run → PASSES ✓
→ All other tests → PASS ✓
REFACTOR: Extract token validation to shared utility
→ Run → All PASS ✓
COMMIT: "fix: reject expired JWT tokens"
Agent writes auth middleware with expiry check → 200 lines
Agent writes tests after → All pass immediately
PROBLEM: Tests pass because they test what the code does, not what it should do.
Edge cases missed. Expiry check has off-by-one error. Nobody will find
it until production.
CORRECT ACTION: Delete the code. Start with the test.
testing
Root cause diagnosis with evidence, Reproducing test, Minimal patch
tools
Session resumption with context
tools
Configure Sage preset and project settings. Switch between base, startup, enterprise, or opensource constitution presets. Use when the user says "configure sage", "change preset", or "sage settings".
development
Brief (medium+ tasks), Spec, Implementation plan