pi-gauntlet 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +300 -0
  2. package/LICENSE +24 -0
  3. package/README.md +278 -0
  4. package/agents/code-reviewer.md +48 -0
  5. package/agents/conformance-reviewer.md +139 -0
  6. package/agents/implementer.md +40 -0
  7. package/agents/spec-council-member.md +47 -0
  8. package/agents/spec-council-synthesizer.md +39 -0
  9. package/agents/spec-reviewer.md +47 -0
  10. package/agents/spec-summarizer.md +42 -0
  11. package/bin/install-agents.mjs +141 -0
  12. package/extensions/phase-tracker.ts +622 -0
  13. package/extensions/plan-tracker.ts +308 -0
  14. package/extensions/verify-before-ship.ts +132 -0
  15. package/package.json +43 -0
  16. package/skills/brainstorming/SKILL.md +290 -0
  17. package/skills/dispatching-parallel-agents/SKILL.md +192 -0
  18. package/skills/finishing-a-development-branch/SKILL.md +311 -0
  19. package/skills/receiving-code-review/SKILL.md +200 -0
  20. package/skills/requesting-code-review/SKILL.md +115 -0
  21. package/skills/requesting-code-review/code-reviewer.md +166 -0
  22. package/skills/roasting-the-spec/SKILL.md +139 -0
  23. package/skills/subagent-driven-development/SKILL.md +223 -0
  24. package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +25 -0
  25. package/skills/subagent-driven-development/implementer-prompt.md +113 -0
  26. package/skills/subagent-driven-development/spec-reviewer-prompt.md +68 -0
  27. package/skills/systematic-debugging/SKILL.md +151 -0
  28. package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
  29. package/skills/systematic-debugging/condition-based-waiting.md +115 -0
  30. package/skills/systematic-debugging/defense-in-depth.md +122 -0
  31. package/skills/systematic-debugging/find-polluter.sh +63 -0
  32. package/skills/systematic-debugging/reference/rationalizations.md +61 -0
  33. package/skills/systematic-debugging/root-cause-tracing.md +169 -0
  34. package/skills/test-driven-development/SKILL.md +230 -0
  35. package/skills/test-driven-development/reference/examples.md +99 -0
  36. package/skills/test-driven-development/reference/rationalizations.md +65 -0
  37. package/skills/test-driven-development/reference/when-stuck.md +31 -0
  38. package/skills/test-driven-development/testing-anti-patterns.md +299 -0
  39. package/skills/using-git-worktrees/SKILL.md +193 -0
  40. package/skills/verification-before-completion/SKILL.md +169 -0
  41. package/skills/verification-before-completion/reference/conformance-check.md +220 -0
  42. package/skills/writing-plans/SKILL.md +244 -0
  43. package/skills/writing-skills/SKILL.md +429 -0
  44. package/skills/writing-skills/reference/anthropic-best-practices.md +1130 -0
  45. package/skills/writing-skills/reference/persuasion.md +187 -0
  46. package/skills/writing-skills/reference/testing-skills-with-subagents.md +384 -0
@@ -0,0 +1,122 @@
1
+ # Defense-in-Depth Validation
2
+
3
+ ## Overview
4
+
5
+ When you fix a bug caused by invalid data, adding validation at one place feels sufficient. But that single check can be bypassed by different code paths, refactoring, or mocks.
6
+
7
+ **Core principle:** Validate at EVERY layer data passes through. Make the bug structurally impossible.
8
+
9
+ ## Why Multiple Layers
10
+
11
+ Single validation: "We fixed the bug"
12
+ Multiple layers: "We made the bug impossible"
13
+
14
+ Different layers catch different cases:
15
+ - Entry validation catches most bugs
16
+ - Business logic catches edge cases
17
+ - Environment guards prevent context-specific dangers
18
+ - Debug logging helps when other layers fail
19
+
20
+ ## The Four Layers
21
+
22
+ ### Layer 1: Entry Point Validation
23
+ **Purpose:** Reject obviously invalid input at API boundary
24
+
25
+ ```typescript
26
+ function createProject(name: string, workingDirectory: string) {
27
+ if (!workingDirectory || workingDirectory.trim() === '') {
28
+ throw new Error('workingDirectory cannot be empty');
29
+ }
30
+ if (!existsSync(workingDirectory)) {
31
+ throw new Error(`workingDirectory does not exist: ${workingDirectory}`);
32
+ }
33
+ if (!statSync(workingDirectory).isDirectory()) {
34
+ throw new Error(`workingDirectory is not a directory: ${workingDirectory}`);
35
+ }
36
+ // ... proceed
37
+ }
38
+ ```
39
+
40
+ ### Layer 2: Business Logic Validation
41
+ **Purpose:** Ensure data makes sense for this operation
42
+
43
+ ```typescript
44
+ function initializeWorkspace(projectDir: string, sessionId: string) {
45
+ if (!projectDir) {
46
+ throw new Error('projectDir required for workspace initialization');
47
+ }
48
+ // ... proceed
49
+ }
50
+ ```
51
+
52
+ ### Layer 3: Environment Guards
53
+ **Purpose:** Prevent dangerous operations in specific contexts
54
+
55
+ ```typescript
56
+ async function gitInit(directory: string) {
57
+ // In tests, refuse git init outside temp directories
58
+ if (process.env.NODE_ENV === 'test') {
59
+ const normalized = normalize(resolve(directory));
60
+ const tmpDir = normalize(resolve(tmpdir()));
61
+
62
+ if (!normalized.startsWith(tmpDir)) {
63
+ throw new Error(
64
+ `Refusing git init outside temp dir during tests: ${directory}`
65
+ );
66
+ }
67
+ }
68
+ // ... proceed
69
+ }
70
+ ```
71
+
72
+ ### Layer 4: Debug Instrumentation
73
+ **Purpose:** Capture context for forensics
74
+
75
+ ```typescript
76
+ async function gitInit(directory: string) {
77
+ const stack = new Error().stack;
78
+ logger.debug('About to git init', {
79
+ directory,
80
+ cwd: process.cwd(),
81
+ stack,
82
+ });
83
+ // ... proceed
84
+ }
85
+ ```
86
+
87
+ ## Applying the Pattern
88
+
89
+ When you find a bug:
90
+
91
+ 1. **Trace the data flow** - Where does bad value originate? Where used?
92
+ 2. **Map all checkpoints** - List every point data passes through
93
+ 3. **Add validation at each layer** - Entry, business, environment, debug
94
+ 4. **Test each layer** - Try to bypass layer 1, verify layer 2 catches it
95
+
96
+ ## Example from Session
97
+
98
+ Bug: Empty `projectDir` caused `git init` in source code
99
+
100
+ **Data flow:**
101
+ 1. Test setup → empty string
102
+ 2. `Project.create(name, '')`
103
+ 3. `WorkspaceManager.createWorkspace('')`
104
+ 4. `git init` runs in `process.cwd()`
105
+
106
+ **Four layers added:**
107
+ - Layer 1: `Project.create()` validates not empty/exists/writable
108
+ - Layer 2: `WorkspaceManager` validates projectDir not empty
109
+ - Layer 3: `WorktreeManager` refuses git init outside tmpdir in tests
110
+ - Layer 4: Stack trace logging before git init
111
+
112
+ **Result:** All 1847 tests passed, bug impossible to reproduce
113
+
114
+ ## Key Insight
115
+
116
+ All four layers were necessary. During testing, each layer caught bugs the others missed:
117
+ - Different code paths bypassed entry validation
118
+ - Mocks bypassed business logic checks
119
+ - Edge cases on different platforms needed environment guards
120
+ - Debug logging identified structural misuse
121
+
122
+ **Don't stop at one validation point.** Add checks at every layer.
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+ # Bisection script to find which test creates unwanted files/state
3
+ # Usage: ./find-polluter.sh <file_or_dir_to_check> <test_pattern>
4
+ # Example: ./find-polluter.sh '.git' 'src/**/*.test.ts'
5
+
6
+ set -e
7
+
8
+ if [ $# -ne 2 ]; then
9
+ echo "Usage: $0 <file_to_check> <test_pattern>"
10
+ echo "Example: $0 '.git' 'src/**/*.test.ts'"
11
+ exit 1
12
+ fi
13
+
14
+ POLLUTION_CHECK="$1"
15
+ TEST_PATTERN="$2"
16
+
17
+ echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
18
+ echo "Test pattern: $TEST_PATTERN"
19
+ echo ""
20
+
21
+ # Get list of test files
22
+ TEST_FILES=$(find . -path "$TEST_PATTERN" | sort)
23
+ TOTAL=$(echo "$TEST_FILES" | wc -l | tr -d ' ')
24
+
25
+ echo "Found $TOTAL test files"
26
+ echo ""
27
+
28
+ COUNT=0
29
+ for TEST_FILE in $TEST_FILES; do
30
+ COUNT=$((COUNT + 1))
31
+
32
+ # Skip if pollution already exists
33
+ if [ -e "$POLLUTION_CHECK" ]; then
34
+ echo "⚠️ Pollution already exists before test $COUNT/$TOTAL"
35
+ echo " Skipping: $TEST_FILE"
36
+ continue
37
+ fi
38
+
39
+ echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
40
+
41
+ # Run the test
42
+ npm test "$TEST_FILE" > /dev/null 2>&1 || true
43
+
44
+ # Check if pollution appeared
45
+ if [ -e "$POLLUTION_CHECK" ]; then
46
+ echo ""
47
+ echo "🎯 FOUND POLLUTER!"
48
+ echo " Test: $TEST_FILE"
49
+ echo " Created: $POLLUTION_CHECK"
50
+ echo ""
51
+ echo "Pollution details:"
52
+ ls -la "$POLLUTION_CHECK"
53
+ echo ""
54
+ echo "To investigate:"
55
+ echo " npm test $TEST_FILE # Run just this test"
56
+ echo " cat $TEST_FILE # Review test code"
57
+ exit 1
58
+ fi
59
+ done
60
+
61
+ echo ""
62
+ echo "✅ No polluter found - all tests clean!"
63
+ exit 0
@@ -0,0 +1,61 @@
1
+ # Debugging Rationalizations & Red Flags
2
+
3
+ ## Common Rationalizations
4
+
5
+ | Excuse | Reality |
6
+ |--------|---------|
7
+ | "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |
8
+ | "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |
9
+ | "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |
10
+ | "I'll write test after confirming fix works" | Untested fixes don't stick. Test first proves it. |
11
+ | "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |
12
+ | "Reference too long, I'll adapt the pattern" | Partial understanding guarantees bugs. Read it completely. |
13
+ | "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. |
14
+ | "One more fix attempt" (after 2+ failures) | 3+ failures = architectural problem. Question pattern, don't fix again. |
15
+
16
+ ## Red Flags — STOP and Follow Process
17
+
18
+ If you catch yourself thinking:
19
+ - "Quick fix for now, investigate later"
20
+ - "Just try changing X and see if it works"
21
+ - "Add multiple changes, run tests"
22
+ - "Skip the test, I'll manually verify"
23
+ - "It's probably X, let me fix that"
24
+ - "I don't fully understand but this might work"
25
+ - "Pattern says X but I'll adapt it differently"
26
+ - "Here are the main problems: [lists fixes without investigation]"
27
+ - Proposing solutions before tracing data flow
28
+ - "One more fix attempt" (when already tried 2+)
29
+ - Each fix reveals new problem in different place
30
+
31
+ **ALL of these mean: STOP. Return to Phase 1.**
32
+
33
+ **If 3+ fixes failed:** Question the architecture (see Phase 4.5)
34
+
35
+ ## Your Human Partner's Signals You're Doing It Wrong
36
+
37
+ **Watch for these redirections:**
38
+ - "Is that not happening?" — You assumed without verifying
39
+ - "Will it show us...?" — You should have added evidence gathering
40
+ - "Stop guessing" — You're proposing fixes without understanding
41
+ - "Ultrathink this" — Question fundamentals, not just symptoms
42
+ - "We're stuck?" (frustrated) — Your approach isn't working
43
+
44
+ **When you see these:** STOP. Return to Phase 1.
45
+
46
+ ## Quick Reference
47
+
48
+ | Phase | Key Activities | Success Criteria |
49
+ |-------|---------------|------------------|
50
+ | **1. Root Cause** | Read errors, reproduce, check changes, gather evidence | Understand WHAT and WHY |
51
+ | **2. Pattern** | Find working examples, compare | Identify differences |
52
+ | **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |
53
+ | **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |
54
+
55
+ ## Real-World Impact
56
+
57
+ From debugging sessions:
58
+ - Systematic approach: 15-30 minutes to fix
59
+ - Random fixes approach: 2-3 hours of thrashing
60
+ - First-time fix rate: 95% vs 40%
61
+ - New bugs introduced: Near zero vs common
@@ -0,0 +1,169 @@
1
+ # Root Cause Tracing
2
+
3
+ ## Overview
4
+
5
+ Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.
6
+
7
+ **Core principle:** Trace backward through the call chain until you find the original trigger, then fix at the source.
8
+
9
+ ## When to Use
10
+
11
+ ```dot
12
+ digraph when_to_use {
13
+ "Bug appears deep in stack?" [shape=diamond];
14
+ "Can trace backwards?" [shape=diamond];
15
+ "Fix at symptom point" [shape=box];
16
+ "Trace to original trigger" [shape=box];
17
+ "BETTER: Also add defense-in-depth" [shape=box];
18
+
19
+ "Bug appears deep in stack?" -> "Can trace backwards?" [label="yes"];
20
+ "Can trace backwards?" -> "Trace to original trigger" [label="yes"];
21
+ "Can trace backwards?" -> "Fix at symptom point" [label="no - dead end"];
22
+ "Trace to original trigger" -> "BETTER: Also add defense-in-depth";
23
+ }
24
+ ```
25
+
26
+ **Use when:**
27
+ - Error happens deep in execution (not at entry point)
28
+ - Stack trace shows long call chain
29
+ - Unclear where invalid data originated
30
+ - Need to find which test/code triggers the problem
31
+
32
+ ## The Tracing Process
33
+
34
+ ### 1. Observe the Symptom
35
+ ```
36
+ Error: git init failed in ~/project/packages/core
37
+ ```
38
+
39
+ ### 2. Find Immediate Cause
40
+ **What code directly causes this?**
41
+ ```typescript
42
+ await execFileAsync('git', ['init'], { cwd: projectDir });
43
+ ```
44
+
45
+ ### 3. Ask: What Called This?
46
+ ```typescript
47
+ WorktreeManager.createSessionWorktree(projectDir, sessionId)
48
+ → called by Session.initializeWorkspace()
49
+ → called by Session.create()
50
+ → called by test at Project.create()
51
+ ```
52
+
53
+ ### 4. Keep Tracing Up
54
+ **What value was passed?**
55
+ - `projectDir = ''` (empty string!)
56
+ - Empty string as `cwd` resolves to `process.cwd()`
57
+ - That's the source code directory!
58
+
59
+ ### 5. Find Original Trigger
60
+ **Where did empty string come from?**
61
+ ```typescript
62
+ const context = setupCoreTest(); // Returns { tempDir: '' }
63
+ Project.create('name', context.tempDir); // Accessed before beforeEach!
64
+ ```
65
+
66
+ ## Adding Stack Traces
67
+
68
+ When you can't trace manually, add instrumentation:
69
+
70
+ ```typescript
71
+ // Before the problematic operation
72
+ async function gitInit(directory: string) {
73
+ const stack = new Error().stack;
74
+ console.error('DEBUG git init:', {
75
+ directory,
76
+ cwd: process.cwd(),
77
+ nodeEnv: process.env.NODE_ENV,
78
+ stack,
79
+ });
80
+
81
+ await execFileAsync('git', ['init'], { cwd: directory });
82
+ }
83
+ ```
84
+
85
+ **Critical:** Use `console.error()` in tests (not logger - may not show)
86
+
87
+ **Run and capture:**
88
+ ```bash
89
+ npm test 2>&1 | grep 'DEBUG git init'
90
+ ```
91
+
92
+ **Analyze stack traces:**
93
+ - Look for test file names
94
+ - Find the line number triggering the call
95
+ - Identify the pattern (same test? same parameter?)
96
+
97
+ ## Finding Which Test Causes Pollution
98
+
99
+ If something appears during tests but you don't know which test:
100
+
101
+ Use the bisection script `find-polluter.sh` in this directory:
102
+
103
+ ```bash
104
+ ./find-polluter.sh '.git' 'src/**/*.test.ts'
105
+ ```
106
+
107
+ Runs tests one-by-one, stops at first polluter. See script for usage.
108
+
109
+ ## Real Example: Empty projectDir
110
+
111
+ **Symptom:** `.git` created in `packages/core/` (source code)
112
+
113
+ **Trace chain:**
114
+ 1. `git init` runs in `process.cwd()` ← empty cwd parameter
115
+ 2. WorktreeManager called with empty projectDir
116
+ 3. Session.create() passed empty string
117
+ 4. Test accessed `context.tempDir` before beforeEach
118
+ 5. setupCoreTest() returns `{ tempDir: '' }` initially
119
+
120
+ **Root cause:** Top-level variable initialization accessing empty value
121
+
122
+ **Fix:** Made tempDir a getter that throws if accessed before beforeEach
123
+
124
+ **Also added defense-in-depth:**
125
+ - Layer 1: Project.create() validates directory
126
+ - Layer 2: WorkspaceManager validates not empty
127
+ - Layer 3: NODE_ENV guard refuses git init outside tmpdir
128
+ - Layer 4: Stack trace logging before git init
129
+
130
+ ## Key Principle
131
+
132
+ ```dot
133
+ digraph principle {
134
+ "Found immediate cause" [shape=ellipse];
135
+ "Can trace one level up?" [shape=diamond];
136
+ "Trace backwards" [shape=box];
137
+ "Is this the source?" [shape=diamond];
138
+ "Fix at source" [shape=box];
139
+ "Add validation at each layer" [shape=box];
140
+ "Bug impossible" [shape=doublecircle];
141
+ "NEVER fix just the symptom" [shape=octagon, style=filled, fillcolor=red, fontcolor=white];
142
+
143
+ "Found immediate cause" -> "Can trace one level up?";
144
+ "Can trace one level up?" -> "Trace backwards" [label="yes"];
145
+ "Can trace one level up?" -> "NEVER fix just the symptom" [label="no"];
146
+ "Trace backwards" -> "Is this the source?";
147
+ "Is this the source?" -> "Trace backwards" [label="no - keeps going"];
148
+ "Is this the source?" -> "Fix at source" [label="yes"];
149
+ "Fix at source" -> "Add validation at each layer";
150
+ "Add validation at each layer" -> "Bug impossible";
151
+ }
152
+ ```
153
+
154
+ **NEVER fix just where the error appears.** Trace back to find the original trigger.
155
+
156
+ ## Stack Trace Tips
157
+
158
+ **In tests:** Use `console.error()` not logger - logger may be suppressed
159
+ **Before operation:** Log before the dangerous operation, not after it fails
160
+ **Include context:** Directory, cwd, environment variables, timestamps
161
+ **Capture stack:** `new Error().stack` shows complete call chain
162
+
163
+ ## Real-World Impact
164
+
165
+ From debugging session (2025-10-03):
166
+ - Found root cause through 5-level trace
167
+ - Fixed at source (getter validation)
168
+ - Added 4 layers of defense
169
+ - 1847 tests passed, zero pollution
@@ -0,0 +1,230 @@
1
+ ---
2
+ name: test-driven-development
3
+ description: Use when implementing any feature or bugfix, before writing implementation code
4
+ ---
5
+
6
+ > **Related skills:** Before claiming done, use `/skill:verification-before-completion` to verify tests actually pass.
7
+
8
+ # Test-Driven Development (TDD)
9
+
10
+ ## Overview
11
+
12
+ Write the test first. Watch it fail. Write minimal code to pass.
13
+
14
+ **Core principle:** If you didn't watch the test fail, you don't know if it tests the right thing.
15
+
16
+ **Violating the letter of the rules is violating the spirit of the rules.**
17
+
18
+ ## Prerequisites
19
+ - Active worktree (not the primary checkout on `main`) — see `/skill:using-git-worktrees`
20
+ - Approved plan or clear task scope
21
+
22
+ Before starting implementation, call `phase_tracker({ action: "start", phase: "implement" })`.
23
+
24
+ ## When to Use
25
+
26
+ **Always:**
27
+ - New features
28
+ - Bug fixes
29
+ - Refactoring
30
+ - Behavior changes
31
+
32
+ **Exceptions (ask your human partner):**
33
+ - Throwaway prototypes
34
+ - Generated code
35
+ - Configuration files
36
+
37
+ Thinking "skip TDD just this once"? Stop. That's rationalization.
38
+
39
+ If a tool result contains a ⚠️ workflow warning about TDD, stop immediately and write the failing test before continuing. The warning is not a checkbox to dismiss — it's the gate.
40
+
41
+ ## The Iron Law
42
+
43
+ ```
44
+ NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
45
+ ```
46
+
47
+ Write code before the test? Delete it. Start over.
48
+
49
+ **No exceptions:**
50
+ - Don't keep it as "reference"
51
+ - Don't "adapt" it while writing tests
52
+ - Don't look at it
53
+ - Delete means delete
54
+
55
+ Implement fresh from tests. Period.
56
+
57
+ ## Red-Green-Refactor
58
+
59
+ ### RED — Write Failing Test
60
+
61
+ Write one minimal test showing what should happen.
62
+
63
+ **Requirements:**
64
+ - One behavior per test
65
+ - Clear name describing behavior (if the name contains "and", split it)
66
+ - Real code (no mocks unless unavoidable)
67
+ - Shows desired API — demonstrates how code should be called
68
+
69
+ **Good:**
70
+ ```typescript
71
+ test('retries failed operations 3 times', async () => {
72
+ let attempts = 0;
73
+ const operation = () => {
74
+ attempts++;
75
+ if (attempts < 3) throw new Error('fail');
76
+ return 'success';
77
+ };
78
+ const result = await retryOperation(operation);
79
+ expect(result).toBe('success');
80
+ expect(attempts).toBe(3);
81
+ });
82
+ ```
83
+
84
+ **Bad:**
85
+ ```typescript
86
+ test('retry works', async () => {
87
+ const mock = jest.fn().mockRejectedValueOnce(new Error()).mockResolvedValueOnce('ok');
88
+ await retryOperation(mock);
89
+ expect(mock).toHaveBeenCalledTimes(2);
90
+ });
91
+ ```
92
+
93
+ ### Verify RED — Watch It Fail
94
+
95
+ **MANDATORY. Never skip.**
96
+
97
+ Run the test. Confirm:
98
+ - Test **fails** (not errors from syntax/import issues)
99
+ - Failure message matches expectation
100
+ - Fails because the feature is missing (not because of typos)
101
+
102
+ **Test passes immediately?** You're testing existing behavior. Fix the test.
103
+ **Test errors instead of failing?** Fix the error, re-run until it fails correctly.
104
+
105
+ ### GREEN — Minimal Code
106
+
107
+ Write the simplest code to pass the test. Nothing more.
108
+
109
+ Don't add features, refactor other code, or "improve" beyond what the test requires. If you're writing code that no test exercises, stop.
110
+
111
+ **Good:** Just enough to pass the test.
112
+ **Bad:** Adding options, config, generalization that no test asks for (YAGNI).
113
+
114
+ ### Verify GREEN — Watch It Pass
115
+
116
+ **MANDATORY.**
117
+
118
+ Run the test. Confirm:
119
+ - New test passes
120
+ - All other tests still pass
121
+ - Output is pristine (no errors, no warnings)
122
+
123
+ **Test fails?** Fix code, not test.
124
+ **Other tests fail?** Fix now — don't move on with broken tests.
125
+
126
+ ### REFACTOR — Clean Up
127
+
128
+ Only after green:
129
+ - Remove duplication
130
+ - Improve names
131
+ - Extract helpers
132
+
133
+ Keep tests green throughout. Don't add new behavior during refactor.
134
+
135
+ ### Repeat
136
+
137
+ Next failing test for next behavior.
138
+
139
+ ## Common Rationalizations
140
+
141
+ | Excuse | Reality |
142
+ |--------|---------|
143
+ | "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
144
+ | "I'll test after" | Tests passing immediately prove nothing. |
145
+ | "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
146
+ | "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
147
+ | "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
148
+ | "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
149
+ | "Need to explore first" | Fine. Throw away exploration, start with TDD. |
150
+ | "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
151
+ | "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
152
+ | "Existing code has no tests" | You're improving it. Add tests for the code you're changing. |
153
+ | "This is different because..." | It's not. Follow the process. |
154
+
155
+ ## Red Flags — STOP and Start Over
156
+
157
+ If you catch yourself doing any of these, stop immediately:
158
+
159
+ - Writing production code before the test
160
+ - Writing tests after implementation
161
+ - Test passes immediately (didn't catch the bug)
162
+ - Can't explain why test failed
163
+ - Rationalizing "just this once"
164
+ - "I already manually tested it"
165
+ - "Keep as reference" or "adapt existing code"
166
+ - "Already spent X hours, deleting is wasteful"
167
+ - "TDD is dogmatic, I'm being pragmatic"
168
+
169
+ **All of these mean: Delete code. Start over with TDD.**
170
+
171
+ ## Verification Checklist
172
+
173
+ Before marking work complete:
174
+
175
+ - [ ] Every new function/method has a test
176
+ - [ ] Watched each test fail before implementing
177
+ - [ ] Each test failed for expected reason (feature missing, not typo)
178
+ - [ ] Wrote minimal code to pass each test
179
+ - [ ] All tests pass
180
+ - [ ] Output pristine (no errors, warnings)
181
+ - [ ] Tests use real code (mocks only if unavoidable)
182
+ - [ ] Edge cases and errors covered
183
+
184
+ Can't check all boxes? You skipped TDD. Start over.
185
+
186
+ ## When Stuck
187
+
188
+ | Problem | Solution |
189
+ |---------|----------|
190
+ | Don't know how to test | Write wished-for API. Write assertion first. Ask your human partner. |
191
+ | Test too complicated | Design too complicated. Simplify interface. |
192
+ | Must mock everything | Code too coupled. Use dependency injection. |
193
+ | Test setup huge | Extract helpers. Still complex? Simplify design. |
194
+
195
+ ## Debugging Integration
196
+
197
+ Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression. Never fix bugs without a test.
198
+
199
+ ## Testing Anti-Patterns
200
+
201
+ When adding mocks or test utilities, read `testing-anti-patterns.md` in this skill directory to avoid common pitfalls:
202
+ - Testing mock behavior instead of real behavior
203
+ - Adding test-only methods to production classes
204
+ - Mocking without understanding dependencies
205
+
206
+ ## Reference
207
+
208
+ Read these directly when you need them:
209
+ - `reference/rationalizations.md` — Extended rationalization discussion
210
+ - `reference/examples.md` — More good/bad code examples, bug fix walkthrough
211
+ - `reference/when-stuck.md` — Extended solutions for common blockers
212
+
213
+ ## Final Rule
214
+
215
+ ```
216
+ Production code → test exists and failed first
217
+ Otherwise → not TDD
218
+ ```
219
+
220
+ No exceptions without your human partner's permission.
221
+
222
+ When the TDD implementation cycle is complete (all tests green, code committed), mark the implement phase complete:
223
+
224
+ ```
225
+ phase_tracker({ action: "complete", phase: "implement" })
226
+ ```
227
+
228
+ ## Project overrides
229
+
230
+ If `.pi/gauntlet-overrides.md` exists, read it. Any sections relevant to this skill — by name match, by topic (routing, verification, worktrees, etc.), or by workflow convention — override or extend the instructions above. Project-local `AGENTS.md` is already in context — check it for project-specific routing tables, service paths, and verification commands.