How the Ralph self-referential loop works
mainRalph creates an autonomous development loop that operates across multiple agent turns using the AfterAgent hook.
The Workflow:
- Initiation: You trigger a loop with
/ralph:loop "task description". - Execution: The agent performs actions (e.g., writing code, running tests).
- Interception: The
AfterAgenthook intercepts the end of the turn. - Reset & Repeat: The hook clears the agent's conversational memory (to prevent stale context/compaction) but keeps the prompt and the file state identical. It then instructs the CLI to start a new turn using the original prompt.
- Termination: The loop continues until the agent meets a
--completion-promise, reaches--max-iterations, or is manually interrupted.
Key Benefits:
- Stable Context: By clearing chat history each turn, the agent focuses on the actual state of the files rather than a long, potentially confusing conversation history.
- Persistent State: Changes made to files and git history persist across iterations.
- Ghost Protection: If you start a new task manually, the hook detects the prompt mismatch and prevents the loop from hijacking your new session.
/ralph:loop "Your task description" --completion-promise "DONE"