Ralph Loop (Event-Driven)
Enhanced Ralph pattern with event-driven notifications — Codex/Claude calls OpenClaw when it needs attention instead of polling.
Key Concepts
Clean Sessions
Each iteration spawns a
fresh agent session with clean context. This is intentional:
- - Avoids context window limits
- Each
codex exec is a new process with no memory of previous runs - Memory persists via files:
IMPLEMENTATION_PLAN.md, AGENTS.md, git history
File-Based Notification Fallback
If OpenClaw is rate-limited when Codex sends a wake notification:
- 1. The notification is written to INLINECODE3
- Wake is attempted (may fail)
- When OpenClaw recovers, it checks for pending notifications
- Work is never lost — it's all in git/files
File Structure
CODEBLOCK0
Notification Format
INLINECODE4 :
CODEBLOCK1
Status values:
- -
pending — Wake failed or not attempted - INLINECODE6 — Wake succeeded
OpenClaw Recovery Procedure
When coming back online after rate limit or downtime, check for pending notifications:
CODEBLOCK2
Recovery Actions by Message Prefix
| Prefix | Action |
|---|
| INLINECODE7 | Report completion to user, summarize what was built |
| INLINECODE8 |
Inform user, ask if ready for BUILDING mode |
|
PROGRESS: | Log it, update user if significant |
|
DECISION: | Present options to user, wait for answer, inject into AGENTS.md |
|
ERROR: | Check logs (
.ralph/ralph.log), analyze, help or escalate |
|
BLOCKED: | Escalate to user immediately with full context |
|
QUESTION: | Present to user, get clarification, inject into AGENTS.md |
Injecting Responses
To answer a decision/question for the next iteration:
CODEBLOCK3
The next Codex session will read AGENTS.md and see the answer.
Clearing Notifications
After processing a notification, clear it:
mv .ralph/pending-notification.txt .ralph/last-notification.txt
Workflow
1. Collect Requirements
Ask for (if not provided):
- - Goal/JTBD: What outcome is needed?
- CLI:
codex, claude, opencode, INLINECODE18 - Mode:
PLANNING, BUILDING, or INLINECODE21 - Tech stack: Language, framework, database
- Test command: How to verify correctness
- Max iterations: Default 20
2. Generate Specs
Break the goal into topics of concern → specs/*.md:
CODEBLOCK5
3. Generate AGENTS.md
CODEBLOCK6
4. Generate PROMPT.md (Mode-Specific)
PLANNING Mode
CODEBLOCK7 bash
openclaw gateway wake --text "PLANNING: " --mode now
Use prefixes:
- `DECISION:` — Need human input on a choice
- `QUESTION:` — Requirements unclear
- `DONE:` — Planning complete
## Completion
When plan is complete and ready for building, add to IMPLEMENTATION_PLAN.md:
STATUS: PLANNING_COMPLETE
Then notify:
bash
openclaw gateway wake --text "DONE: Planning complete. X tasks identified." --mode now
CODEBLOCK10
BUILDING Mode
CODEBLOCK11 bash
openclaw gateway wake --text ": " --mode now
Prefixes:
- `DECISION:` — Need human input (e.g., "SQLite vs PostgreSQL?")
- `ERROR:` — Tests failing after 3 attempts
- `BLOCKED:` — Missing dependency, credentials, or unclear spec
- `PROGRESS:` — Major milestone complete (optional)
- `DONE:` — All tasks complete
## Completion
When all tasks are done:
1. Add to IMPLEMENTATION_PLAN.md: `STATUS: COMPLETE`
2. Notify:
bash
openclaw gateway wake --text "DONE: All tasks complete. Summary: " --mode now
CODEBLOCK13
5. Run the Loop
Use the provided scripts/ralph.sh:
CODEBLOCK14
Parallel Execution
For independent tasks, use git worktrees:
CODEBLOCK15
Track sessions:
| Session ID | Worktree | Task | Status |
|---|
| abc123 | /tmp/task-auth | Auth module | running |
| def456 |
/tmp/task-upload | Image upload | running |
Each Codex notifies independently. Check .ralph/pending-notification.txt in each worktree.
CLI-Specific Notes
Codex
- - Requires git repository
- Each
codex exec is a fresh session — no memory between calls - INLINECODE26 : Auto-approve in workspace (sandboxed)
- INLINECODE27 : No sandbox, no approvals (dangerous but fast)
- Default model: gpt-5.2-codex
Claude Code
- -
--dangerously-skip-permissions: Auto-approve (use in sandbox) - No git requirement
- Each invocation is fresh
OpenCode
Goose
Safety
⚠️ Auto-approve flags are dangerous. Always:
- 1. Run in a dedicated directory/branch
- Use a sandbox (Docker/VM) for untrusted projects
- Have
git reset --hard ready as escape hatch - Review commits before pushing
Quick Start
CODEBLOCK16
Example: Antique Catalogue
CODEBLOCK17
The agent will:
- 1. (PLANNING) Break this into 10-15 tasks
- (BUILDING) Implement each task, one per iteration
- Commit after each successful implementation
- Notify on completion or if blocked
Ralph Loop (事件驱动)
增强版Ralph模式,采用事件驱动通知——Codex/Claude在需要关注时主动调用OpenClaw,而非轮询。
核心概念
干净会话
每次迭代都会生成一个
全新的代理会话,上下文完全干净。这是有意为之:
- - 避免上下文窗口限制
- 每次codex exec都是全新进程,不保留之前运行的记忆
- 通过文件持久化记忆:IMPLEMENTATION_PLAN.md、AGENTS.md、git历史
基于文件的通知回退
如果Codex发送唤醒通知时OpenClaw遇到速率限制:
- 1. 通知被写入.ralph/pending-notification.txt
- 尝试唤醒(可能失败)
- 当OpenClaw恢复后,检查待处理通知
- 工作永远不会丢失——全部保存在git/文件中
文件结构
project/
├── PROMPT.md # 每次迭代加载(模式特定)
├── AGENTS.md # 项目上下文、测试命令、经验记录
├── IMPLEMENTATION_PLAN.md # 带状态的任务列表
├── specs/ # 需求规格
│ ├── overview.md
│ └── .md
└── .ralph/
├── ralph.log # 执行日志
├── pending-notification.txt # 当前待处理通知(如有)
└── last-notification.txt # 上一条通知(供参考)
通知格式
.ralph/pending-notification.txt:
json
{
timestamp: 2026-02-07T02:30:00+01:00,
project: /home/user/my-project,
message: DONE: 所有任务完成。,
iteration: 15,
max_iterations: 20,
cli: codex,
status: pending
}
状态值:
- - pending — 唤醒失败或未尝试
- delivered — 唤醒成功
OpenClaw恢复流程
当因速率限制或停机重新上线时,检查待处理通知:
bash
查找所有项目中的待处理通知
find ~/projects -name pending-notification.txt -path
/.ralph/ 2>/dev/null
或检查特定项目
cat /path/to/project/.ralph/pending-notification.txt
按消息前缀的恢复操作
| 前缀 | 操作 |
|---|
| DONE: | 向用户报告完成情况,总结构建内容 |
| PLANNING_COMPLETE: |
通知用户,询问是否准备好进入BUILDING模式 |
| PROGRESS: | 记录进度,如有重大进展则更新用户 |
| DECISION: | 向用户呈现选项,等待回答,注入到AGENTS.md |
| ERROR: | 检查日志(.ralph/ralph.log),分析问题,提供帮助或升级处理 |
| BLOCKED: | 立即向用户升级处理,附带完整上下文 |
| QUESTION: | 向用户呈现问题,获取澄清,注入到AGENTS.md |
注入回复
为下一次迭代回答决策/问题:
bash
echo ## 人工决策
- - [$(date +%Y-%m-%d %H:%M)] 问:<问题>? 答:<答案> >> AGENTS.md
下一次Codex会话将读取AGENTS.md并看到答案。
清除通知
处理完通知后,清除它:
bash
mv .ralph/pending-notification.txt .ralph/last-notification.txt
工作流程
1. 收集需求
询问(如果未提供):
- - 目标/JTBD:需要什么成果?
- CLI:codex、claude、opencode、goose
- 模式:PLANNING、BUILDING或BOTH
- 技术栈:语言、框架、数据库
- 测试命令:如何验证正确性
- 最大迭代次数:默认20
2. 生成规格
将目标分解为关注主题 → specs/*.md:
markdown
specs/overview.md
目标
<一句话描述JTBD>
技术栈
- - 语言:Python 3.11
- 框架:FastAPI
- 数据库:SQLite
- 前端:HTMX + Tailwind
成功标准
3. 生成AGENTS.md
markdown
AGENTS.md
项目
<简要描述>
命令
- - 安装:pip install -e .
- 测试:pytest
- 代码检查:ruff check .
- 运行:python -m app
反向压力
每次实现后运行:
- 1. ruff check . --fix
- pytest
人工决策
经验记录
4. 生成PROMPT.md(模式特定)
PLANNING模式
markdown
Ralph PLANNING循环
目标
上下文
- - 读取:specs/*.md
- 读取:当前代码库结构
- 更新:IMPLEMENTATION_PLAN.md
规则
- 1. 不要实现代码
- 不要提交
- 分析规格与当前状态之间的差距
- 创建/更新IMPLEMENTATION_PLAN.md,包含优先级排序的任务
- 每个任务应较小(< 1小时工作量)
- 如果需求不明确,列出问题
通知
当需要输入或完成规划时:
bash
openclaw gateway wake --text PLANNING: <你的消息> --mode now
使用前缀:
- - DECISION: — 需要人工对某个选择做出决定
- QUESTION: — 需求不明确
- DONE: — 规划完成
完成
当计划完成并准备好构建时,添加到IMPLEMENTATION_PLAN.md:
STATUS: PLANNING_COMPLETE
然后通知:
bash
openclaw gateway wake --text DONE: 规划完成。已识别X个任务。 --mode now
BUILDING模式
markdown
Ralph BUILDING循环
目标
上下文
- - 读取:specs/*.md、IMPLEMENTATION_PLAN.md、AGENTS.md
- 实现:每次迭代一个任务
- 测试:运行AGENTS.md中的反向压力命令
规则
- 1. 从IMPLEMENTATIONPLAN.md中选择优先级最高的未完成任务
- 在修改前调查相关代码
- 实现该任务
- 运行反向压力命令(代码检查、测试)
- 如果测试通过:用清晰消息提交,标记任务完成
- 如果测试失败:尝试修复(最多3次),然后通知
- 用任何操作经验更新AGENTS.md
- 用进度更新IMPLEMENTATIONPLAN.md
通知
在需要时调用OpenClaw:
bash
openclaw gateway wake --text : <消息> --mode now
前缀:
- - DECISION: — 需要人工输入(例如,SQLite还是PostgreSQL?)
- ERROR: — 测试在3次尝试后仍然失败
- BLOCKED: — 缺少依赖、凭据或规格不明确
- PROGRESS: — 完成重大里程碑(可选)
- DONE: — 所有任务完成
完成
当所有任务完成时:
- 1. 添加到IMPLEMENTATION_PLAN.md:STATUS: COMPLETE
- 通知:
bash
openclaw gateway wake --text DONE: 所有任务完成。摘要:<构建了什么> --mode now
5. 运行循环
使用提供的scripts/ralph.sh:
bash
默认:20次迭代,使用Codex
./scripts/ralph.sh 20
使用Claude Code
RALPH_CLI=claude ./scripts/ralph.sh 10
带测试
RALPH_TEST=pytest ./scripts/ralph.sh
并行执行
对于独立任务,使用git工作树:
bash
创建工作树以并行工作
git worktree add /tmp/task-auth main
git worktree add /tmp/task-upload main
生成并行会话(每个都是干净/全新的)
exec pty:true background:true workdir:/tmp/task-auth command:codex exec --full-auto 实现用户认证...
exec pty:true background:true workdir:/tmp/task-upload command:codex exec --full-auto 实现