iterative-development
alinaqi/maggy/skills/iterative-development/SKILL.md
TDD iteration loops using Claude Code Stop hooks - runs tests after each response, feeds failures back automatically
Skill707 starsChanged 4 months ago
What's in it
- Iterative Development Skill (Stop Hook TDD Loops)
- How It Actually Works
- Setup: Stop Hook Configuration
- The TDD Loop Check Script
- Python Variant
- Additional Hooks for Quality Enforcement
- PreToolUse Hook: Lint Before File Writes
- SessionStart Hook: Auto-Inject Context
- Core Philosophy
- Error Classification
- When to Use TDD Loops
- Good For
- Not Good For
- Disabling the Loop
- Gitignore Additions
---
name: iterative-development
description: TDD iteration loops using Claude Code Stop hooks - runs tests after each response, feeds failures back automatically
when-to-use: When setting up or configuring TDD loops via Stop hooks
user-invocable: false
effort: medium
---
# Iterative Development Skill (Stop Hook TDD Loops)
**Concept:** Claude Code's Stop hook fires right before Claude finishes a response. Exit code 2 feeds stderr back to the model and continues the conversation. This creates a real TDD loop without any plugins.
---
## How It Actually Works
Claude Code has a **Stop hook** that runs when Claude is about to conclude its response. If the hook script exits with code 2, its stderr is shown to the model and the conversation continues automatically.
```
┌─────────────────────────────────────────────────────────────┐
│ 1. User asks Claude to implement a feature │
├─────────────────────────────────────────────────────────────┤
│ 2. Claude writes tests + implementation │
├─────────────────────────────────────────────────────────────┤
│ 3. Claude finishes its response │
├─────────────────────────────────────────────────────────────┤
│ 4. Stop hook runs: executes tests, lint, typecheck │
├─────────────────────────────────────────────────────────────┤
│ 5a. All pass (exit 0) → Claude stops, work is done │
│ 5b. Failures (exit 2) → stderr fed back to Claude │
├─────────────────────────────────────────────────────────────┤
│ 6. Claude sees failures, fixes code, response ends │
├─────────────────────────────────────────────────────────────┤
│ 7. Stop hook runs again → repeat until green or max tries │
└─────────────────────────────────────────────────────────────┘
```
**Key insight:** No fake plugins, no `/ralph-loop` command. The hook is real Claude Code infrastructure that runs automatically.
---
## Setup: Stop Hook Configuration
Add this to your project's `.claude/settings.json`:
```json
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "scripts/tdd-loop-check.sh",
"timeout": 60,
"statusMessage": "Running tests..."
}
]
}
]
}
}
```
### The TDD Loop Check Script
Create `scripts/tdd-loop-check.sh` in your project:
```bash
#!/bin/bash
# TDD Loop Check - runs after each Claude response
# Exit 0 = all good, Claude stops
# Exit 2 = failures, stderr fed back to Claude to fix
MAX_ITERATIONS=25
ITERATION_FILE=".claude/.tdd-iteration-count"
# Track iteration count
if [ -f "$ITERATION_FILE" ]; then
count=$(cat "$ITERATION_FILE")
count=$((count + 1))
else
count=1
fi
echo "$count" > "$ITERATION_FILE"
# Safety: stop after max iterations
if [ "$count" -ge "$MAX_ITERATIONS" ]; then
rm -f "$ITERATION_FILE"
echo "Max iterations ($MAX_ITERATIONS) reached. Stopping loop." >&2
exit 0
fi
# Skip if no test files exist yet
if ! find . -name "*.test.*" -o -name "*.spec.*" -o -name "test_*" 2>/dev/null | grep -q .; then
rm -f "$ITERATION_FILE"
exit 0
fi
# Run tests
TEST_OUTPUT=$(npm test 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Tests failing:" >&2
echo "$TEST_OUTPUT" | tail -30 >&2
echo "" >&2
echo "Fix the failing tests and try again." >&2
exit 2
}
# Run lint (if configured)
if [ -f "package.json" ] && grep -q '"lint"' package.json; then
LINT_OUTPUT=$(npm run lint 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Lint errors:" >&2
echo "$LINT_OUTPUT" | tail -20 >&2
echo "" >&2
echo "Fix lint errors and try again." >&2
exit 2
}
fi
# Run typecheck (if configured)
if [ -f "tsconfig.json" ]; then
TYPE_OUTPUT=$(npx tsc --noEmit 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Type errors:" >&2
echo "$TYPE_OUTPUT" | tail -20 >&2
echo "" >&2
echo "Fix type errors and try again." >&2
exit 2
}
fi
# All green - reset counter and let Claude stop
rm -f "$ITERATION_FILE"
exit 0
```
### Python Variant
```bash
#!/bin/bash
# Python TDD Loop Check
MAX_ITERATIONS=25
ITERATION_FILE=".claude/.tdd-iteration-count"
if [ -f "$ITERATION_FILE" ]; then
count=$(cat "$ITERATION_FILE")
count=$((count + 1))
else
count=1
fi
echo "$count" > "$ITERATION_FILE"
if [ "$count" -ge "$MAX_ITERATIONS" ]; then
rm -f "$ITERATION_FILE"
echo "Max iterations ($MAX_ITERATIONS) reached." >&2
exit 0
fi
if ! find . -name "test_*" -o -name "*_test.py" 2>/dev/null | grep -q .; then
rm -f "$ITERATION_FILE"
exit 0
fi
TEST_OUTPUT=$(pytest -v 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Tests failing:" >&2
echo "$TEST_OUTPUT" | tail -30 >&2
exit 2
}
if command -v ruff &>/dev/null; then
LINT_OUTPUT=$(ruff check . 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Lint errors:" >&2
echo "$LINT_OUTPUT" | tail -20 >&2
exit 2
}
fi
if command -v mypy &>/dev/null; then
TYPE_OUTPUT=$(mypy . 2>&1) || {
echo "ITERATION $count/$MAX_ITERATIONS - Type errors:" >&2
echo "$TYPE_OUTPUT" | tail -20 >&2
exit 2
}
fi
rm -f "$ITERATION_FILE"
exit 0
```
---
## Additional Hooks for Quality Enforcement
### PreToolUse Hook: Lint Before File Writes
Runs a linter before any Write/Edit lands:
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "scripts/pre-write-lint.sh",
"timeout": 10,
"statusMessage": "Checking code quality..."
}
]
}
]
}
}
```
### SessionStart Hook: Auto-Inject Context
Runs at session start to inject project info:
```json
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "echo 'TDD loop active. Tests run automatically after each response. Fix failures to continue.'",
"statusMessage": "Loading project context..."
}
]
}
]
}
}
```
---
## Core Philosophy
```
┌─────────────────────────────────────────────────────────────┐
│ ITERATION > PERFECTION │
│ ───────────────────────────────────────────────────────── │
│ Don't aim for perfect on first try. │
│ Let the loop refine the work. Each iteration builds on │
│ previous attempts visible in files and git history. │
├─────────────────────────────────────────────────────────────┤
│ FAILURES ARE DATA │
│ ───────────────────────────────────────────────────────── │
│ Failed tests, lint errors, type mismatches are signals. │
│ The Stop hook feeds them directly to Claude as context. │
├─────────────────────────────────────────────────────────────┤
│ CLEAR COMPLETION CRITERIA │
│ ───────────────────────────────────────────────────────── │
│ The hook defines "done": tests pass, lint clean, types ok. │
│ No ambiguity about when to stop. │
└─────────────────────────────────────────────────────────────┘
```
---
## Error Classification
Not all failures should loop. The hook script should distinguish:
| Type | Examples | Action |
|------|----------|--------|
| **Code Error** | Logic bug, wrong assertion, type mismatch | Exit 2 → loop continues |
| **Access Error** | Missing API key, DB connection refused | Exit 0 → stop, report to user |
| **Environment Error** | Missing package, wrong runtime version | Exit 0 → stop, report to user |
The sample scripts above handle this — they only exit 2 for test/lint/type failures, not for environment issues.
---
## When to Use TDD Loops
### Good For
| Use Case | Why |
|----------|-----|
| Feature development | Tests provide clear pass/fail signal |
| Bug fixes | Write failing test, fix, loop until green |
| Refactoring | Existing tests catch regressions |
| API development | Each endpoint independently testable |
### Not Good For
| Use Case | Why |
|----------|-----|
| UI/UX work | Requires human judgment |
| One-shot operations | No iteration needed |
| Unclear requirements | No clear "done" criteria |
| Subjective design | No objective success metric |
---
## Disabling the Loop
To temporarily disable the TDD loop for a session:
1. Remove or rename the Stop hook in `.claude/settings.json`
2. Or set `MAX_ITERATIONS=1` in the script
3. Or delete `scripts/tdd-loop-check.sh`
The hook only fires if the script exists and is configured.
---
## Gitignore Additions
```gitignore
# TDD loop state
.claude/.tdd-iteration-count
```
More agent context in alinaqi/maggy
75 other files this repository gives its agents, the first 60 shown.
AGENTS.md
CLAUDE.md
Skill
- aeo-optimizationskills/aeo-optimization/SKILL.md
- agentic-developmentskills/agentic-development/SKILL.md
- agent-teamsskills/agent-teams/SKILL.md
- ai-modelsskills/ai-models/SKILL.md
- android-javaskills/android-java/SKILL.md
- android-kotlinskills/android-kotlin/SKILL.md
- autonomous-testingskills/autonomous-testing/SKILL.md
- aws-auroraskills/aws-aurora/SKILL.md
- aws-dynamodbskills/aws-dynamodb/SKILL.md
- azure-cosmosdbskills/azure-cosmosdb/SKILL.md
- baseskills/base/SKILL.md
- build-in-publicskills/build-in-public/SKILL.md
- cloudflare-d1skills/cloudflare-d1/SKILL.md
- code-deduplicationskills/code-deduplication/SKILL.md
- code-graphskills/code-graph/SKILL.md
- code-reviewskills/code-review/SKILL.md
- codex-reviewskills/codex-review/SKILL.md
- commit-hygieneskills/commit-hygiene/SKILL.md
- context-shuntskills/context-shunt/SKILL.md
- council-reviewskills/council-review/SKILL.md
- cpg-analysisskills/cpg-analysis/SKILL.md
- credentialsskills/credentials/SKILL.md
- cross-agent-delegationskills/cross-agent-delegation/SKILL.md
- database-schemaskills/database-schema/SKILL.md
- dataforseoskills/dataforseo/SKILL.md
- demo-videoskills/demo-video/SKILL.md
- existing-reposkills/existing-repo/SKILL.md
- external-model-delegationskills/external-model-delegation/SKILL.md
- firebaseskills/firebase/SKILL.md
- flutterskills/flutter/SKILL.md
- gemini-reviewskills/gemini-review/SKILL.md
- icpgskills/icpg/SKILL.md
- klaviyoskills/klaviyo/SKILL.md
- llm-patternsskills/llm-patterns/SKILL.md
- maggyskills/maggy/SKILL.md
- medusaskills/medusa/SKILL.md
- mnemosskills/mnemos/SKILL.md
- model-routingskills/model-routing/SKILL.md
- ms-teams-appsskills/ms-teams-apps/SKILL.md
- nodejs-backendskills/nodejs-backend/SKILL.md
- playwright-testingskills/playwright-testing/SKILL.md
- polyphonyskills/polyphony/SKILL.md
- posthog-analyticsskills/posthog-analytics/SKILL.md
- project-toolingskills/project-tooling/SKILL.md
- pwa-developmentskills/pwa-development/SKILL.md
- pythonskills/python/SKILL.md
- react-nativeskills/react-native/SKILL.md
- react-webskills/react-web/SKILL.md
- reddit-adsskills/reddit-ads/SKILL.md
- reddit-apiskills/reddit-api/SKILL.md
- security-auditskills/security-audit/SKILL.md
- securityskills/security/SKILL.md
- session-managementskills/session-management/SKILL.md
- shopify-appsskills/shopify-apps/SKILL.md
- site-architectureskills/site-architecture/SKILL.md
- supabase-nextjsskills/supabase-nextjs/SKILL.md
- supabase-nodeskills/supabase-node/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
Reports can't be read right now.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool public_context_discussion, action report. How to connect one.

