Autonomous iterative loop execution with cross-iteration learning and context rotation
Every Ralph iteration follows this read-work-write cycle. The project's task CLI is the authoritative task tracker — PRD checkboxes are a secondary view. Both must stay in sync.
1. READ: Load .claude/ralph/progress.md (accumulated learnings)
2. READ: Load .claude/ralph/guardrails.md (known pitfalls)
3. SYNC: Check task CLI for in_progress tasks ({{cmd:list_active}})
→ If found, RESUME that task instead of selecting from PRD
4. READ: Load PRD and identify next incomplete task (first unchecked box)
5. TRACK: Look up or create a CLI task for the selected PRD item
→ {{cmd:create_task}} if new, {{cmd:set_status}} to in_progress
→ Append starting note: {{cmd:append_notes}} "-> Starting. Ralph iteration N."
6. BREAK: If task is complex (3+ steps), create sub-tasks via {{cmd:create_subtask}}
7. WORK: Execute the task via the standard pipeline
8. NOTE: Update CLI task notes during work: {{cmd:append_notes}} "[done] ..." or "[x] ..."
9. WRITE: Mark task complete in PRD (check the box)
10. CLOSE: Close the CLI task: {{cmd:close_task}} (and all sub-tasks)
11. VERIFY: Smoke-check work before committing (see Smoke Verification Protocol below)
→ If verification fails, fix wiring issues and re-run tests before proceeding
12. WRITE: Commit changes with passing tests and verified wiring
13. WRITE: Append learnings to progress.md
14. CHECK: All tasks done? → If manifest mode: the manifest-update task (final task in PRD) handles
state tracking. After that task commits, read manifest for next eligible PRD.
If next PRD found with satisfied requires → chain to it (load new PRD, continue iterations).
If no more PRDs or all blocked → output completion promise.
If single-PRD mode → output completion promise. Otherwise → next iteration.
.claude/ralph/progress.md is append-only across iterations:
# Progress Log
## Iteration 1 — [Task title]
- **Status**: Complete
- **Files changed**: path/to/file.ts, path/to/other.ts
- **Patterns discovered**: [Any reusable patterns found]
- **Gotchas**: [Any surprises or issues encountered]
- **Time**: [Timestamp]
## Iteration 2 — [Task title]
...
.claude/ralph/guardrails.md captures failure patterns so future iterations avoid them:
# Guardrails
## Sign: [Descriptive title]
- **Trigger**: [When this applies — e.g., "When modifying auth middleware"]
- **Instruction**: [What to do instead — e.g., "Always run auth tests before committing"]
- **Context**: Iteration [N] — [Brief failure description]
Rules for guardrails:
Ralph mode uses context rotation instead of compression for long-running work:
| Context Level | Zone | Action |
|---|---|---|
| < 60% | Green | Work freely, full pipeline execution |
| 60–80% | Yellow | Wrap up current task, commit work, prepare progress notes |
| > 80% | Red | Forced wrap-up: commit everything, write all learnings, prepare for fresh context |
On rotation:
{{cmd:append_notes}} with [pause] PAUSED AT: [describe exactly where you stopped and what remains]. Context rotating.in_progress so the next context can find and resume itprogress.md, including the task CLI ID for the in_progress taskActive PRD: prd-phases/00-foundation/prd-00b-project-init.md) so the next context knows which file to readOn resuming after rotation:
{{cmd:list_active}} -- the in_progress task from the prior context will appear{{cmd:show_details}} on that task to read the [pause] note with context on where to resumeWhen all PRD tasks are checked off, output the completion promise:
<promise>COMPLETE</promise>
This signals the external loop script (ralph-loop.sh) to stop iterating. The promise text can be customized via the --completion-promise flag.
Each iteration MUST:
in_progress before work begins[done] or [x]){{cmd:close_task}})Each iteration MUST NOT:
.claude/ralph/activity.log is a machine-readable log for external tooling:
[2025-01-15T10:30:00Z] iteration=1 task="Set up project structure" status=complete commit=abc1234
[2025-01-15T10:45:00Z] iteration=2 task="Add auth middleware" status=complete commit=def5678
[2025-01-15T11:00:00Z] iteration=3 task="Add auth tests" status=failed error="timeout in test runner"
Lightweight checks run before every commit (step 11) to verify the app actually works — not just that tests pass. These checks require no E2E infrastructure (no Playwright/Cypress). Verifying before commit ensures broken wiring never enters the repo.
For UI tasks:
For API tasks:
For all tasks:
If smoke verification fails: Do not commit. Fix the wiring issue first — it's usually a missing import, unregistered route, or broken navigation link. Re-run tests after fixing, then commit clean. These are fast fixes that prevent broken code from entering the repo.
The coordinator and architect should proactively identify when a task is a good Ralph candidate. Check these indicators:
| Indicator | Signal |
|---|---|
| Multiple discrete subtasks | Task has 3+ checkbox items or independent steps |
| Machine-verifiable success | Tests, linting, builds, coverage can confirm completion |
| Well-defined scope | Clear acceptance criteria, not open-ended |
| Repetitive pattern | Same type of change across multiple files/components |
| Greenfield work | New feature buildout with structured requirements |
If 3+ indicators match, suggest Ralph mode to the user:
"This task looks like a good candidate for Ralph loop mode — it has [indicators]. Ralph would handle each subtask autonomously with iterative refinement. Would you like to use
/ralphfor this, or continue with interactive mode?"
Only proceed with Ralph if the user explicitly confirms. Standard interactive mode remains the default.