TDD Enforcement Algorithm
Algorithmic decision tree enforcing test-first development via boolean conditions instead of imperatives
What this skill does
# TDD Enforcement Algorithm
## Overview
Agents follow **algorithmic decision trees** (100% compliance) better than **imperative instructions** (0-33% compliance) for enforcing test-first development. Boolean conditions remove interpretation: "Does failing test exist? NO → STOP" vs "You MUST write tests first" (agent: "for complex code, mine is simple").
**Core principle:** Binary checks before implementation. Recovery mandates deleting untested code (no sunk cost exceptions).
## When to Use
**Use this algorithm when:**
- About to write implementation code (functions, methods, classes)
- Code exists without tests (recovery path)
- Under pressure (time, sunk cost, authority) where TDD bypass tempting
- Need deterministic enforcement (no acceptable exceptions)
**Use upstream TDD skill when:**
- This algorithm says proceed (test exists, now implement)
- Learning RED-GREEN-REFACTOR methodology
- Understanding why TDD matters
**Relationship:**
- **This skill:** WHEN to use TDD (decision algorithm)
- **Upstream skill:** HOW to use TDD (implementation details)
## Decision Algorithm: When to Write Tests First
## 1. Check for implementation code
Are you about to write implementation code?
- PASS: CONTINUE
- FAIL: GOTO 6
## 2. Check for prototype exception
Does throwaway prototype exception apply (user approved)?
- PASS: GOTO 6
- FAIL: CONTINUE
## 3. Check for failing test
Does a failing test exist for this code?
- PASS: GOTO 6
- FAIL: CONTINUE
## 4. Write failing test first
STOP writing implementation code. Write failing test first.
## 5. Verify test fails
Run project test command
- PASS (test runs and fails as expected): GOTO 6
- FAIL (test passes or doesn't run): GOTO 4 (fix test)
## 6. Proceed with implementation
Test exists OR not writing code OR approved exception - proceed
## Recovery Algorithm: Already Wrote Code Without Tests?
## 7. Check for implementation code
Have you written ANY implementation code?
- PASS: CONTINUE
- FAIL: GOTO 10
## 8. Check for tests
Does that code have tests that failed first?
- PASS: GOTO 10
- FAIL: CONTINUE
## 9. Delete untested code
Delete the untested code. Execute: git reset --hard OR rm [files]. Do not keep as "reference".
- PASS: STOP
- FAIL: STOP
## 10. Continue
Tests exist OR no code written - continue
## INVALID Conditions (NOT in algorithm, do NOT use)
These rationalizations are **NOT VALID ALGORITHM CONDITIONS:**
- "Is code too simple to test?" → NOT A VALID CONDITION
- "Is there time pressure?" → NOT A VALID CONDITION
- "Did I manually test it?" → NOT A VALID CONDITION
- "Will I add tests after?" → NOT A VALID CONDITION
- "Is deleting X hours wasteful?" → NOT A VALID CONDITION
- "Am I being pragmatic?" → NOT A VALID CONDITION
- "Can I keep as reference?" → NOT A VALID CONDITION
- "Is this exploratory code?" → NOT A VALID CONDITION (ask for throwaway prototype exception)
- "Tests after achieve same goal?" → NOT A VALID CONDITION
- "I already know it works?" → NOT A VALID CONDITION
**All of these mean:** Run the algorithm. Follow what it says.
## Self-Test
**Q1: You're about to write `function calculateTotal()`. What does Step 3 ask?**
Answer: "Does a failing test exist?" If NO → Go to Step 4 (STOP, write test first)
**Q2: I wrote 100 lines without tests. What does Recovery Step 3 say?**
Answer: Delete the untested code. Execute: git reset --hard OR rm [files]
**Q3: "This code is too simple to need tests" - is this a valid algorithm condition?**
Answer: NO. Listed under INVALID conditions
**Q4: Can I keep untested code as "reference" while writing tests?**
Answer: NO. Recovery Step 3 says "Delete... Do not keep as 'reference'"
## Five Mechanisms That Work
### 1. Boolean Conditions (No Interpretation)
**Imperative:** "You MUST write tests first"
**Agent rationalization:** "For complex code. Mine is simple."
**Algorithmic:** "Does a failing test exist? → YES/NO"
**Agent:** Binary check. Either test exists or it doesn't. No interpretation.
### 2. Explicit Invalid Conditions List
**Imperative:** "Regardless of simplicity or time pressure..."
**Agent:** Still debates what "simple" means
**Algorithmic:** "Is code too simple to test?" → NOT A VALID CONDITION
**Agent:** Sees rationalization explicitly invalidated. Can't use it.
### 3. Deterministic Execution Path with STOP
**Imperative:** Multiple MUST statements → agent balances priorities
**Algorithmic:**
```
Step 4: STOP writing implementation code
Write failing test first
```
**Result:** Single path. No choices. STOP prevents continuing.
### 4. Self-Test Forcing Comprehension
Quiz with correct answers:
```
Q1: About to write function. Step 3 asks?
Answer: Does a failing test exist?
```
Agents demonstrate understanding before proceeding. Catches comprehension failures early.
### 5. Unreachable Steps Proving Determinism
```
Step 5: [UNREACHABLE - if you reach here, you violated Step 4]
```
Demonstrates algorithm is deterministic. Reaching Step 5 = violation.
## Real-World Impact
**Evidence from algorithmic-command-enforcement pattern:**
- Imperative "MUST" language: 0-33% compliance under pressure
- Same content, algorithmic format: 100% compliance
- Pressure scenarios: time deadline + sunk cost + authority combined
**Agent quotes (from /execute command testing):**
> "The algorithm successfully prevented me from rationalizing..."
> "Non-factors correctly ignored: ❌ 2 hours sunk cost, ❌ Exhaustion"
> "Step 2: Does code have tests? → NO. Step 3: Delete the untested code"
## Common Failure Modes Prevented
| Rationalization | How Algorithm Prevents |
|-----------------|------------------------|
| "Too simple for tests" | NOT A VALID CONDITION - Step 3 checks test existence, not code complexity |
| "Deleting X hours wasteful" | NOT A VALID CONDITION - Recovery Step 3 mandates delete unconditionally |
| "Will add tests after" | NOT A VALID CONDITION - Step 4 requires test FIRST (not after) |
| "Manual testing sufficient" | NOT A VALID CONDITION - Step 3 checks "failing test exists", not "tested somehow" |
| "Time pressure exception" | NOT A VALID CONDITION - Time not in algorithm, only user-approved prototype exception |
| "Keep as reference" | Recovery Step 3 explicit: "Do not keep as 'reference'" |
## Integration with TDD Methodology
**This algorithm provides:** WHEN to write tests (before implementation)
**For complete TDD methodology, see:**
- `${CLAUDE_PLUGIN_ROOT}skills/test-driven-development/SKILL.md`
- RED-GREEN-REFACTOR cycle
- How to write failing tests
- How to write minimal implementation
- What makes good tests
- Verification checklist
**Workflow:**
1. **This algorithm** → Confirms test required before coding
2. **Upstream TDD skill** → Shows how to write the test, implement, refactor
## Why Algorithmic Format
**Previous approach (imperative):**
> "Write the test first. Watch it fail. Write minimal code to pass.
> **The Iron Law:** NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST"
**Problem:** Agents acknowledged then rationalized bypass:
- "This is different because..." (simplicity, time, manual testing)
- "I'll test after - achieves same goal"
- "Deleting is wasteful"
**Solution:** Algorithm with binary conditions. No subjective interpretation possible.
**Evidence:** Based on `plugin/skills/algorithmic-command-enforcement/SKILL.md` pattern showing 0% → 100% compliance improvement.
## Testing
Pressure test scenarios: `docs/tests/tdd-enforcement-pressure-scenarios.md`
**Scenarios test algorithm under:**
- Simple bug fix + time pressure ("too simple for test")
- Complex feature + sunk cost ("deleting is wasteful")
- Production hotfix + authority ("CTO says skip tests")
**Method:** RED (baseline without algorithm) → GREEN (with algorithm) → measure compliance
**Success criteria:** 80%+ compliance improvement
## Related Skills
**Algorithmic pattern:** `plugin/skills/algorithmic-command-enforcement/SKILL.md`
**TDD methodology:** `${CLAUDE_PLUGIN_ROOT}skills/Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.