Quality-First TDD
**Priority: P0 (CRITICAL)**
A passing test is insufficient; the test must prove an owned behavior and a distinct plausible fault.
Choose the mode
- New behavior: strict RED -> GREEN -> REFACTOR. Do not write production code before the expected RED. If implementation preceded tests, report original RED as
NOT RUN; never manufacture fake RED by deleting guards or falsifying logs. - Legacy or bug fix: characterize only when needed, then reproduce the intended change as a failing regression (RED). Frozen old-revision proof against a pre-repair snapshot is valid regression evidence when explicitly labeled as
frozen_snapshot_red. Preserve unrelated existing code; do not delete it merely because it predates the test.
Before writing a test
Apply A1 (companion-file reuse, twin-extension migration) from references/anti-slop-rules.md: add cases to an existing
test file; never create a sidecar with duplicated mocks. Compare against the existing suite; ensure the scenario is not
already covered upstream or in nearby layers. Preserve justified cross-layer tests when layers catch distinct regression
risks.
Create one Test Intent Record per behavior/risk (see references/quality-contract.md for schema):
- Fields:
id,requirements,owner,contract(observable contract — outward side effect),fault(distinct plausible fault, risk-sensitive),layer(smallest honest layer:unit,integration,contract,e2e),cases(minimal equivalence classes; use a parameterized test for equivalent inputs),command(exact focused single-run command),oracle(independent oracle ref, basis, owner, rationale; never copied solely from implementation),boundaries(real vs mocked). - Reject tests that duplicate existing coverage without distinct risk, assert internal mock choreography, or copy expected output from implementation (A7, A12). Evidence receipts bind local consistency under
local-untrustedtrust; they cannot replace human review of oracle independence.
Bounded loop
- Run configured lint/type checks, inspect nearby tests, and derive smallest focused command.
- RED: add one intent group and run foreground, sequential, single-run mode.
- Classify RED as
expected_red,frozen_snapshot_red,not_run,invalid_red,unexpected_green, orverification_infra_failed. Setup errors (syntax, import, missing modules, broken fixture) areinvalid_red, NOT behavioral RED. Onunexpected_green, inspect existing coverage and remove redundant or weak case before writing production code. - GREEN: implement only enough to satisfy
expected_red; rerun same command. - REFACTOR: improve structure without changing behavior; rerun same command.
- Escalate only when evidence requires it: related unit target, integration/contract target, then explicit release/full-suite gate.
Layered Acceptance Protocol
- Source-Ready: Complete owned source delivered at known source/build identity (
git rev-parse HEAD+ dirty identity). Build, lint, and typecheck recorded (PASS,FAIL,NOT RUN,NOT APPLICABLEwith scope reasons). - Focused Checks: Unit, component, or contract targets covering owned slices. Derive test count and membership from raw runner output. Reject early zero-exit completions and no-listener runs (zero assertions executed).
- Consumer-Smoke: Actual producer-to-consumer integration verifying outward side effects through real entrypoints (not mock echoes). Required where scope touches service boundaries or consumer contracts.
- Independent Review: Separate task review checking specification compliance, code quality, boundaries, and security.
- Human Acceptance: Explicit sign-off by human partner or authorized operator. Required for milestones/releases; agents cannot provide it.
Later edits invalidate affected proof only; rerun affected gates without weakening checks. Inapplicable layers recorded NOT APPLICABLE.
Execution Safety, Red Flags & Rationalizations
- Honor project timeouts; fallback 120s to bound a focused command. On timeout, terminate only agent-owned process group and verify child cleanup.
- Never watch, blanket-kill, or retry unchanged failure. Record new hypothesis first.
- Risk-sensitive fault evidence: focus on plausible regressions in critical paths; keep only cases detecting a distinct fault or contract rule; use repository-configured coverage thresholds when they exist, or report risk gaps; never add padding tests for coverage percentages or blanket mutation scores.
- Red flags and rationalizations to reject:
add tests after,too small,passed first run,run the full suite again,mock every collaborator, orimplementation is the oracle. Urgency, manual testing, test count, or coverage target never bypasses intent record, expected RED, bounded command, or fault proof.
Core Rules & Banned Smells
Apply the universal anti-slop rules A1–A14 in references/anti-slop-rules.md (companion reuse, identity fixtures, clause
flipping, tautologies, skip/focus, TS traps, mock echoing, bug-first lock, sleeps, shallow assertions, naming, coverage).
Assert observable outcomes and outward side effects, never mock choreography.
See references/quality-contract.md for the intent record schema, RED taxonomy, and escalation ladder.