Coding Agent Loops
Run AI coding agents in persistent, self-healing sessions with automatic retry and completion notification.
Core Concept
Instead of one long agent session that stalls or dies, run many short sessions in a loop. Each iteration starts fresh — no accumulated context. The agent picks up where it left off via files and git history. This is the "Ralph loop" pattern.
Prerequisites
- -
tmux installed - INLINECODE1 : INLINECODE2
- A coding agent:
codex (Codex CLI) or claude (Claude Code) - Stable tmux socket: always use
~/.tmux/sock (default /tmp socket gets reaped by macOS)
Quick Start
Single Task
CODEBLOCK0
PRD-Based Workflow (Preferred for Multi-Step Work)
CODEBLOCK1
Parallel Agents on Separate Tasks
CODEBLOCK2
Session Management
Check Progress
CODEBLOCK3
List Active Sessions
CODEBLOCK4
Kill a Session
CODEBLOCK5
The Completion Hook (Mandatory)
Always append this to tmux commands:
CODEBLOCK6
Why each part matters:
- -
EXIT_CODE=$? — captures the agent's exit code - INLINECODE8 — visible in tmux pane output
- INLINECODE9 — fires a wake event so OpenClaw notifies you immediately
- INLINECODE10 — keeps the shell alive so output remains readable
PRD Format
Ralph tracks completion via markdown checklists:
CODEBLOCK7
Ralph validates that all items are checked before accepting a completion signal from the agent.
When to Use What
| Scenario | Tool |
|---|
| Multi-step features, PRD checklists | INLINECODE11 |
| Tasks that have stalled before |
ralphy --codex "task" (auto-retry) |
| Tiny focused fixes, one-file changes |
codex exec --full-auto "task" |
| Parallel work on different tasks |
ralphy --codex --parallel --prd PRD.md |
| Skip tests/lint for speed |
ralphy --codex --fast "task" |
| Use Claude Code instead of Codex |
ralphy --claude "task" |
Key Principles
- 1. Always use tmux — background exec processes die on gateway/host restart. tmux sessions persist.
- Always use the stable socket (
~/.tmux/sock) — the default /tmp socket gets cleaned up. - Always add the completion hook — without it you won't know when the agent finishes.
- Log active sessions — record running sessions in daily notes or a tracking file so you don't lose awareness.
- Verify before declaring failure — after a process ends, check
git log, git diff, and process output before concluding it failed. - PATH in tmux — tmux may not inherit your full PATH. Prepend
/opt/homebrew/bin: if tools aren't found.
Troubleshooting
- - Agent exits immediately: Check
~/.codex/log/codex-tui.log for auth errors. May need codex auth login. - Ralph marks tasks done but nothing committed: Ralph can mark PRD tasks complete even when the agent fails silently. Always verify via
git log --oneline -3 and git diff --stat. - API rate limits (429s): Common when running multiple parallel agents. Ralph's retry handles this, but reduce parallelism if persistent.
- Session disappeared: tmux sessions can die from OOM or system restarts. Check with
tmux -S ~/.tmux/sock has-session -t <name> and restart if needed.
Coding Agent Loops
在持久、自愈的会话中运行AI编码代理,具备自动重试和完成通知功能。
核心概念
与其运行一个可能卡住或崩溃的长会话,不如在循环中运行多个短会话。每次迭代都重新开始——没有累积的上下文。代理通过文件和Git历史记录从中断处继续。这就是Ralph循环模式。
前置条件
- - 已安装 tmux
- ralphy-cli:npm install -g ralphy-cli
- 编码代理:codex(Codex CLI)或 claude(Claude Code)
- 稳定的tmux套接字:始终使用 ~/.tmux/sock(默认的 /tmp 套接字会被macOS清理)
快速开始
单个任务
bash
tmux -S ~/.tmux/sock new -d -s my-task \
cd /path/to/repo && ralphy --codex 修复认证漏洞; \
EXIT
CODE=\$?; echo EXITED: \$EXITCODE; \
openclaw system event --text Ralph循环 my-task 已完成 (退出码 \$EXIT_CODE) 在 \$(pwd) --mode now; \
sleep 999999
基于PRD的工作流(多步骤任务首选)
bash
tmux -S ~/.tmux/sock new -d -s feature-build \
cd /path/to/repo && ralphy --codex --prd PRD.md; \
EXIT
CODE=\$?; echo EXITED: \$EXITCODE; \
openclaw system event --text Ralph循环 feature-build 已完成 (退出码 \$EXIT_CODE) 在 \$(pwd) --mode now; \
sleep 999999
并行代理处理独立任务
bash
ralphy --codex --parallel --prd PRD.md
会话管理
检查进度
bash
tmux -S ~/.tmux/sock capture-pane -t my-task -p | tail -20
列出活动会话
bash
tmux -S ~/.tmux/sock list-sessions
终止会话
bash
tmux -S ~/.tmux/sock kill-session -t my-task
完成钩子(必选)
始终在tmux命令后追加以下内容:
bash
; EXITCODE=$?; echo EXITED: $EXITCODE; \
openclaw system event --text Ralph循环 <名称> 已完成 (退出码 $EXIT_CODE) 在 $(pwd) --mode now; \
sleep 999999
各部分的作用:
- - EXITCODE=$? — 捕获代理的退出码
- echo EXITED: $EXITCODE — 在tmux面板输出中可见
- openclaw system event — 触发唤醒事件,OpenClaw立即通知你
- sleep 999999 — 保持shell存活,使输出保持可读
PRD格式
Ralph通过Markdown检查清单跟踪完成状态:
markdown
任务
- - [ ] 创建API端点
- [ ] 添加输入验证
- [ ] 编写测试
- [x] 已完成(已跳过)
Ralph在接受代理的完成信号前,会验证所有项目是否都已勾选。
何时使用何种工具
| 场景 | 工具 |
|---|
| 多步骤功能、PRD检查清单 | ralphy --codex --prd PRD.md |
| 之前卡住的任务 |
ralphy --codex 任务(自动重试) |
| 小型聚焦修复、单文件修改 | codex exec --full-auto 任务 |
| 不同任务的并行工作 | ralphy --codex --parallel --prd PRD.md |
| 跳过测试/代码检查以加快速度 | ralphy --codex --fast 任务 |
| 使用Claude Code替代Codex | ralphy --claude 任务 |
关键原则
- 1. 始终使用tmux — 后台执行进程会在网关/主机重启时终止。tmux会话持久存在。
- 始终使用稳定套接字(~/.tmux/sock)— 默认的 /tmp 套接字会被清理。
- 始终添加完成钩子 — 没有它你无法知道代理何时完成。
- 记录活动会话 — 在每日笔记或跟踪文件中记录运行中的会话,以免遗漏。
- 在声明失败前先验证 — 进程结束后,在断定失败前先检查 git log、git diff 和进程输出。
- tmux中的PATH — tmux可能不会继承你的完整PATH。如果找不到工具,请添加 /opt/homebrew/bin: 前缀。
- 验证后再声明失败 — 进程结束后,检查 git log、git diff 和进程输出,再下结论。
故障排除
- - 代理立即退出:检查 ~/.codex/log/codex-tui.log 中的认证错误。可能需要 codex auth login。
- Ralph标记任务完成但未提交任何内容:即使代理静默失败,Ralph也可能标记PRD任务完成。始终通过 git log --oneline -3 和 git diff --stat 验证。
- API速率限制(429错误):运行多个并行代理时常见。Ralph的重试机制会处理此问题,但如果持续出现,请减少并行度。
- 会话消失:tmux会话可能因OOM或系统重启而终止。使用 tmux -S ~/.tmux/sock has-session -t <名称> 检查,必要时重启。