Ralph Loop (Agent Mode)
by @addozhang
Guide OpenClaw agents to execute Ralph Wiggum loops using exec and process tools. Agent orchestrates coding agents (Codex, Claude Code, OpenCode, Goose) with proper TTY support via pty:true. Plans/builds code via PROMPT.md + AGENTS.md, SPECS and IMPLEMENTATION_PLAN.md. Includes PLANNING vs BUILDING modes, backpressure, sandboxing, and completion conditions. Users request loops, agents execute using tools.
clawhub install ralph-loop-agentπ About This Skill
name: ralph-loop description: Guide OpenClaw agents to execute Ralph Wiggum loops using exec and process tools. Agent orchestrates coding agents (Codex, Claude Code, OpenCode, Goose) with proper TTY support via pty:true. Plans/builds code via PROMPT.md + AGENTS.md, SPECS and IMPLEMENTATION_PLAN.md. Includes PLANNING vs BUILDING modes, backpressure, sandboxing, and completion conditions. Users request loops, agents execute using tools. version: 1.1.0 author: OpenClaw Community keywords: [ralph-loop, ai-agent, coding-agent, pty, tty, automation, loop, opencode, codex, claude, goose, exec-tool, process-tool] license: MIT
Ralph Loop
Overview
This skill guides OpenClaw agents to execute Ralph Loop workflows using theexec and process tools. The agent orchestrates AI coding agent sessions following the Ralph playbook flow:1) Define Requirements β JTBD β Focus Topics β specs/*.md
2) PLANNING Loop β Create/update IMPLEMENTATION_PLAN.md (do not implement)
3) BUILDING Loop β Implement tasks, run tests (backpressure), update plan, commit
The loop persists context via PROMPT.md + AGENTS.md (loaded each iteration) and the plan/specs on disk.
How This Skill Works
This skill generates instructions for OpenClaw agents to execute Ralph Loops using the exec and process tools.
exec tool with the coding agent commandpty: true to provide TTY for interactive CLIsbackground: true for monitoring capabilitiesprocess tool to monitor progress and detect completionImportant: Users don't run these scripts directly - the OpenClaw agent executes them using its tool capabilities.
TTY Requirements
Some coding agents require a real terminal (TTY) to work properly, or they will hang:
Interactive CLIs (need TTY):
Non-interactive CLIs (file-based):
Solution: Use exec + process mode for interactive CLIs, simple loops for file-based tools.
Agent Tool Usage Patterns
Interactive CLIs (Recommended Pattern)
For OpenCode, Codex, Claude Code, Pi, and Goose - these require TTY support:
When I (the agent) receive a Ralph Loop request, I will:
1. Use exec tool to launch the coding agent:
exec tool with parameters:
- command: "opencode run --model \"$(cat PROMPT.md)\""
- workdir:
- background: true
- pty: true
- yieldMs: 60000
- timeout: 3600
2. Capture session ID from exec tool response
3. Use process tool to monitor:
process tool with:
- action: "poll"
- sessionId:
process tool with:
- action: "log"
- sessionId:
- offset: -30 (for recent output)
4. Check completion by reading IMPLEMENTATION_PLAN.md for sentinel text
5. Clean up with process kill if needed:
process tool with:
- action: "kill"
- sessionId:
Benefits: TTY support, real-time logs, timeout handling, parallel sessions, workdir isolation
Agent Workflow
1) Gather Inputs
Required:
opencode, codex, claude, goose, pi, other)PLANNING, BUILDING, or BOTH)Optional:
STATUS: COMPLETE in IMPLEMENTATION_PLAN.md)$PWD)--full-auto, --yolo, --dangerously-skip-permissions)Auto-detect:
2) Requirements β Specs (Optional)
If requirements are unclear:
specs/.md for each3) PROMPT.md + AGENTS.md
PROMPT.md references:
specs/*.mdIMPLEMENTATION_PLAN.mdAGENTS.md includes:
4) Prompt Templates
PLANNING Prompt (no implementation):
You are running a Ralph PLANNING loop for this goal: .Read specs/* and the current codebase. Only update IMPLEMENTATION_PLAN.md.
Rules:
Do not implement
Do not commit
Create a prioritized task list
Write down questions if unclear Completion:
When plan is ready, add: STATUS: PLANNING_COMPLETE
BUILDING Prompt:
You are running a Ralph BUILDING loop for this goal: .Context: specs/*, IMPLEMENTATION_PLAN.md, AGENTS.md
Tasks:
1) Pick the most important task
2) Investigate code
3) Implement
4) Run backpressure commands from AGENTS.md
5) Update IMPLEMENTATION_PLAN.md
6) Update AGENTS.md with learnings
7) Commit with clear message
Completion:
When all done, add: STATUS: COMPLETE
5) CLI Command Reference
The agent constructs command strings using these patterns:
| CLI | Command String Pattern |
|-----|----------------------|
| OpenCode | opencode run --model |
| Codex | codex exec (requires git) |
| Claude Code | claude |
| Pi | pi --provider |
| Goose | goose run "$(cat PROMPT.md)" |
Common flags:
--full-auto, --yolo, --model --dangerously-skip-permissionsDetailed Agent Tool Usage Examples
Example 1: OpenCode Ralph Loop
Agent executes this sequence:
Step 1: Launch OpenCode with exec tool
{
command: "opencode run --model github-copilot/claude-opus-4.5 \"$(cat PROMPT.md)\"",
workdir: "/path/to/project",
background: true,
pty: true,
timeout: 3600,
yieldMs: 60000
}Step 2: Capture session ID from response
sessionId: "abc123"
Step 3: Monitor with process tool every 10-30 seconds
{
action: "poll",
sessionId: "abc123"
}
Step 4: Check recent logs
{
action: "log",
sessionId: "abc123",
offset: -30
}
Step 5: Read IMPLEMENTATION_PLAN.md to check for completion
Look for: "STATUS: COMPLETE" or "STATUS: PLANNING_COMPLETE" Step 6: If complete or timeout, cleanup
{
action: "kill",
sessionId: "abc123"
}
Example 2: Codex with Full Auto
Agent tool calls:
exec tool:
{
command: "codex exec --full-auto --model anthropic/claude-opus-4 \"$(cat PROMPT.md)\"",
workdir: "/path/to/project",
background: true,
pty: true,
timeout: 3600
}Then monitor with process tool as above
Completion Detection
Use flexible regex to match variations:
grep -Eq "STATUS:?\s*(PLANNING_)?COMPLETE" IMPLEMENTATION_PLAN.md
Matches:
STATUS: COMPLETESTATUS:COMPLETESTATUS: PLANNING_COMPLETE## Status: PLANNING_COMPLETESafety & Safeguards
Auto-Approval Flags (Risky!)
--full-auto (sandboxed, auto-approve) or --yolo (no sandbox!)--dangerously-skip-permissionsEscape Hatches
Ctrl+Cgit reset --hard HEAD~NBest Practices
1. Start small: Test with 1-2 iterations first 2. Workdir isolation: Prevent reading unrelated files 3. Set timeouts: Default 1h may not fit all tasks 4. Monitor actively: Check logs, don't terminate prematurely 5. Requirements first: Clear specs before building 6. Backpressure early: Add tests from the startTroubleshooting
| Problem | Solution | |---------|----------| | OpenCode hangs | Ensure agent uses exec tool with pty: true | | Session won't start | Check CLI path, git repo, command syntax | | Completion not detected | Verify sentinel format in IMPLEMENTATION_PLAN.md | | Process timeout | Agent should increase timeout parameter or simplify tasks | | Parallel conflicts | Agent should use git worktrees for isolation | | Can't see progress | Agent should use process tool with action: "log" |
License
MIT
Credits
This skill builds upon work by:
Key improvement: Uses OpenClaw's exec tool with pty: true to provide TTY for interactive CLIs, solving the hanging issue that occurs with simple background bash execution.
π Tips & Best Practices
1. Start small: Test with 1-2 iterations first 2. Workdir isolation: Prevent reading unrelated files 3. Set timeouts: Default 1h may not fit all tasks 4. Monitor actively: Check logs, don't terminate prematurely 5. Requirements first: Clear specs before building 6. Backpressure early: Add tests from the start