Loop Mode
Loop Mode enables autonomous story processing through AgileFlow's Ralph Loop system. Named after the "Ralph Wiggum" pattern from Anthropic's research on agentic workflows, it automatically processes stories in an epic without manual intervention.
Loop Mode is activated through the /agileflow:babysit command with special parameters. Babysit orchestrates the entire process while Ralph Loop handles the automation.
Overview
When enabled, Loop Mode:
- Picks the first "ready" story from an epic
- Marks it "in_progress" and begins implementation
- When Claude stops, runs tests automatically
- If tests pass → marks story complete, loads next story
- Continues until epic complete or max iterations reached
This enables overnight batch processing of well-defined epics with clear acceptance criteria.
Quick Start
# Start loop mode for an epic
/agileflow:babysit EPIC=EP-0042 MODE=loop
# Or initialize directly via script
node .agileflow/scripts/ralph-loop.js --init --epic=EP-0042 --max=20How It Works
The Stop Hook
Loop Mode uses Claude Code's stop hook system. When Claude stops responding:
- The
ralph-loop.jsscript runs automatically - It executes the configured test command (default:
npm test) - Based on results, it either advances to the next story or prompts for fixes
Session State
Loop configuration is stored in docs/09-agents/session-state.json:
{
"ralph_loop": {
"enabled": true,
"epic": "EP-0042",
"current_story": "US-0015",
"iteration": 3,
"max_iterations": 20,
"test_command": "npm test",
"mode": "normal"
}
}Parameters
| Parameter | Required | Default | Description |
|---|---|---|---|
EPIC | Yes | - | Epic ID to process (e.g., EP-0042) |
MODE | Yes | - | Must be loop for autonomous mode |
MAX | No | 20 | Maximum iterations before stopping |
CLI Commands
# Initialize loop for an epic
node .agileflow/scripts/ralph-loop.js --init --epic=EP-0042 --max=20
# Check current loop status
node .agileflow/scripts/ralph-loop.js --status
# Stop the loop gracefully
node .agileflow/scripts/ralph-loop.js --stop
# Reset loop state completely
node .agileflow/scripts/ralph-loop.js --reset
# Run one iteration manually
node .agileflow/scripts/ralph-loop.js --runLoop Modes
Normal Mode (Default)
Standard test-based verification:
/agileflow:babysit EPIC=EP-0042 MODE=loop- Runs
npm testafter each Claude stop - Passes if all tests succeed
- Fails if any test fails
Visual Mode
For UI-focused epics requiring screenshot verification:
/agileflow:babysit EPIC=EP-0042 MODE=loop VISUAL=true- Checks
screenshots/directory for images - All screenshots must have
verified-prefix - Useful for UI component development
See Visual Mode for details.
Coverage Mode
For test coverage requirements:
/agileflow:babysit EPIC=EP-0042 MODE=loop COVERAGE=80- Runs tests with coverage reporting
- Verifies coverage meets threshold (e.g., 80%)
- Fails if coverage drops below threshold
Story Selection
Loop Mode selects stories in this priority order:
- Current story - If one is already in progress
- Ready stories - Stories with status "ready" (all dependencies met)
- Blocked stories - Skipped until unblocked
Stories must have:
- Clear acceptance criteria
- All dependencies resolved
- Status set to "ready" or "pending"
Configuration
Test Command
Configure the test command in agileflow-metadata.json:
{
"loop": {
"test_command": "npm test",
"coverage_threshold": 70,
"max_iterations": 20
}
}Stop Hook Setup
Loop Mode requires the stop hook in .claude/settings.json:
{
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "node .agileflow/scripts/ralph-loop.js --run"
}
]
}
]
}
}When to Use Loop Mode
Good For
- ✅ Well-defined epics with clear stories
- ✅ Test-driven development (tests define "done")
- ✅ Batch processing multiple stories overnight
- ✅ Repetitive implementation tasks (CRUD endpoints, components)
- ✅ Stories with automated acceptance criteria
Not Good For
- ❌ Exploratory work without clear acceptance criteria
- ❌ Stories requiring human review before proceeding
- ❌ Complex multi-domain work needing coordination
- ❌ First-time implementations without patterns
- ❌ Stories with external dependencies (APIs, services)
Example Workflow
1. Prepare the Epic
Ensure all stories have clear acceptance criteria:
## US-0015: Add User List Component
**Acceptance Criteria:**
- [ ] Component renders list of users from API
- [ ] Loading state shown while fetching
- [ ] Error state displayed on failure
- [ ] Empty state when no users
**Tests:** src/components/UserList.test.tsx2. Start Loop Mode
/agileflow:babysit EPIC=EP-0042 MODE=loop MAX=153. Monitor Progress
Check status periodically:
node .agileflow/scripts/ralph-loop.js --statusOutput:
Loop Status:
Epic: EP-0042
Current Story: US-0017
Iteration: 5 of 15
Mode: normal
Stories Completed: 4
Stories Remaining: 3
4. Handle Failures
If tests fail, Loop Mode shows the failures and continues working:
❌ Tests failed for US-0017
Failures:
- UserList.test.tsx: Expected 3 users, got 2
Continue fixing or run /agileflow:babysit --stop to pause.
5. Complete the Epic
When all stories complete:
✅ Epic EP-0042 Complete!
Summary:
- Stories Completed: 7
- Total Iterations: 12
- Time Elapsed: 2h 34m
All tests passing. Ready for review.
Troubleshooting
Loop Not Starting
-
Check session state exists:
cat docs/09-agents/session-state.json -
Verify stop hook is configured:
cat .claude/settings.json | grep -A5 "Stop" -
Initialize manually:
node .agileflow/scripts/ralph-loop.js --init --epic=EP-0042
Tests Always Failing
-
Run tests manually first:
npm test -
Check test command in config:
cat docs/00-meta/agileflow-metadata.json | grep test_command -
Ensure tests exist for current story
Loop Stuck on Same Story
-
Check iteration count:
node .agileflow/scripts/ralph-loop.js --status -
If approaching max iterations, either:
- Fix the blocking issue
- Skip the story: Update status to "blocked"
- Increase max:
--max=30
Stopping Unexpectedly
Loop stops when:
- Max iterations reached
- Epic complete
- Manual stop command
- Critical error in test execution
Check logs for details:
cat .agileflow/logs/ralph-loop.logBest Practices
1. Start with Small Epics
Test Loop Mode on a 3-5 story epic first before larger batches.
2. Write Tests First
Loop Mode works best with TDD - tests define when a story is "done".
3. Keep Stories Focused
Each story should be completable in 1-3 iterations. Large stories → more failures.
4. Monitor Periodically
Check status every 30-60 minutes during long runs.
5. Use Visual Mode for UI
For UI work, enable Visual Mode to catch rendering issues tests might miss.
Related Features
- Visual Mode - Screenshot verification for UI development
- Compact Context - Context preservation during long sessions
/agileflow:babysit- The mentor command that orchestrates Loop Mode/agileflow:verify- Manual test verification
Architecture
Loop Mode consists of:
| Component | Purpose |
|---|---|
ralph-loop.js | Main loop orchestration script |
screenshot-verifier.js | Visual Mode screenshot checking |
session-state.json | Loop configuration and state |
| Stop hook | Claude Code integration point |
The name "Ralph" comes from Anthropic's research paper on agentic workflows, where "Ralph Wiggum" was used as a code name for autonomous task completion patterns.
On This Page
Loop ModeOverviewQuick StartHow It WorksThe Stop HookSession StateParametersCLI CommandsLoop ModesNormal Mode (Default)Visual ModeCoverage ModeStory SelectionConfigurationTest CommandStop Hook SetupWhen to Use Loop ModeGood ForNot Good ForExample Workflow1. Prepare the Epic2. Start Loop Mode3. Monitor Progress4. Handle Failures5. Complete the EpicTroubleshootingLoop Not StartingTests Always FailingLoop Stuck on Same StoryStopping UnexpectedlyBest Practices1. Start with Small Epics2. Write Tests First3. Keep Stories Focused4. Monitor Periodically5. Use Visual Mode for UIRelated FeaturesArchitecture