Ralph Loop Runner
Overview
The Ralph Loop is an autonomous execution protocol that drives an implementation plan from start to finish. Each iteration implements one increment, validates it against its test gate, commits the change, and advances to the next increment.
The loop follows the principle: Perceive → Implement → Validate → Commit → Repeat.
A fresh agent instance handles each iteration, ensuring clean context and preventing drift. The loop terminates when all increments reach **Status:** done.
Prerequisites
Before starting the Ralph Loop:
- PRD exists in
docs/product-specs/with status fields on user stories. - Execution plan exists in
docs/exec-plans/active/with**Status:** pendingon every increment. - Peer review recommended — run
spec-peer-revieweron both documents first. - Test infrastructure works — verify the project's test commands run successfully.
Workspace Setup Protocol
Before the first iteration, prepare the workspace:
- Create workspace directory:
docs/exec-plans/active/NNNN_feature-name/ - Move artifacts into workspace:
- Copy PRD from
docs/product-specs/NNNN_prd_feature-name.mdinto the workspace - Move exec plan from
docs/exec-plans/active/NNNN_exec_feature-name.mdinto the workspace
- Copy PRD from
- Create progress log:
docs/exec-plans/active/NNNN_feature-name/progress.txt - Create feature branch:
git checkout -b ralph/NNNN-feature-name
Workspace structure after setup:
docs/exec-plans/active/NNNN_feature-name/
NNNN_prd_feature-name.md # Copy of PRD
NNNN_exec_feature-name.md # Execution plan (source of truth)
progress.txt # Iteration log
Iteration Protocol
Each iteration follows this exact sequence:
- Read workspace: Load the exec plan and find the next
**Status:** pendingincrement. - Set status: Change that increment's status from
pendingtoin-progress. Update**Current Increment:**in the plan header. - Implement: Execute the increment's scope items. Follow the primary files list.
- Run test gate: Execute every command listed in the increment's test gate section.
- Pottery wheel: If any test gate fails, fix the issue and re-run. Maximum 3 retries per increment.
- Mark done: Change the increment's status from
in-progresstodone. - Update PRD: Check off any acceptance criteria (
- [ ]→- [x]) satisfied by this increment. - Commit: Stage and commit with the convention below.
- Log progress: Append an entry to
progress.txt. - Exit or continue: If more
pendingincrements remain and running interactively, continue to step 1. If running viaralph.sh, exit withRALPH_COMPLETEsignal so the script can spawn a fresh agent.
Marking Convention
Status values for increments:
**Status:** pending— not yet started**Status:** in-progress— currently being implemented**Status:** done— implemented and test gate passed
Status values for milestones (in the Milestone Chunks table):
pending— no increments in this milestone are donein-progress— at least one increment is done, others remaindone— all increments in this milestone are done
Overall plan status:
**Overall Status:** pending— no work started**Overall Status:** in-progress— at least one increment done**Overall Status:** done— all increments done
PRD user story status:
**Status:** pending— not started**Status:** in-progress— some acceptance criteria checked**Status:** done— all acceptance criteria checked
Progress Log Format
Each entry in progress.txt follows this format:
## Iteration N
- Increment: XX — [Title]
- Status: done | blocked
- Files changed: [list]
- Test gate: passed | failed (retry M)
- Learnings: [any notable observations]
- Commit: [hash]
- Timestamp: [ISO 8601]
Commit Convention
Use conventional commits with the plan ID as scope:
feat|fix|refactor(NNNN): increment XX — [title]
Examples:
feat(0001): increment 01 — create database schemafix(0001): increment 03 — fix validation edge caserefactor(0001): increment 05 — extract shared utilities
Completion Detection
The loop is complete when:
- No increments have
**Status:** pendingor**Status:** in-progressin the exec plan. - All milestone statuses in the table are
done. **Overall Status:**is set todone.
Archival Protocol
When all increments are done:
- Move PRD: Copy from workspace back to
docs/product-specs/(overwrite with updated checkboxes). Set PRD status tocomplete. - Move exec plan: Move from workspace to
docs/exec-plans/completed/. - Delete progress log: Remove
progress.txt. - Remove workspace: Delete the empty
NNNN_feature-name/directory. - Optional: Invoke
git-pr-creatorto open a pull request for the feature branch.
Error Recovery
Blocked increment
If an increment cannot be completed after 3 pottery-wheel retries:
- Set its status to
**Status:** blocked. - Log the blocker in
progress.txtwith details. - Stop the loop — do not skip increments (they may have dependencies).
- Notify the user with the blocker details.
Crash recovery
If the agent or script crashes mid-iteration:
- Check
git statusandgit logto determine what was committed. - Read
progress.txtto find the last completed iteration. - Find the first
**Status:** pendingor**Status:** in-progressincrement. - If an increment is
in-progressbut not committed, reset it topendingand restart.
Max retries
- Per-increment pottery wheel: 3 attempts maximum.
- Per-loop max iterations: configurable via
ralph.sh --max-iterations(default: 50).