ralph-loop
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRalph Wiggum Loop Mode
Ralph Wiggum循环模式
Named after the Simpsons character who "never stops despite being confused," this technique runs Claude Code in a loop where the prompt stays the same but the codebase accumulates changes. Each iteration reads previous work and continues until completion.
该模式以《辛普森一家》中“即便困惑也永不停止”的角色Ralph Wiggum命名,它会让Claude Code在循环中运行,提示语保持不变,但代码库会累积变更。每次迭代都会读取之前的工作内容并继续执行,直至任务完成。
When to Use Ralph Mode
何时使用Ralph模式
Ideal for:
- Well-defined implementation tasks with clear completion criteria
- Refactoring or migration work (e.g., React v16 to v19)
- Test-driven development cycles (run until tests pass)
- Batch processing or repetitive tasks
- Overnight autonomous work sessions
Not ideal for:
- Tasks requiring design decisions or human judgment
- Exploratory work without clear end states
- Tasks where requirements may change mid-execution
- First-time implementations where you need to learn the code
适用场景:
- 定义明确、具备清晰完成标准的实现任务
- 重构或迁移工作(例如React v16升级到v19)
- 测试驱动开发循环(运行直至测试通过)
- 批处理或重复性任务
- 夜间自主工作会话
不适用场景:
- 需要设计决策或人工判断的任务
- 无明确结束状态的探索性工作
- 执行过程中需求可能变更的任务
- 需要学习代码库的首次实现任务
Activation Protocol
激活流程
Step 1: Validate Task Suitability
步骤1:验证任务适用性
Before activating, confirm:
- Task has clear, measurable completion criteria
- Success can be verified programmatically (tests, build, specific file state)
- The work is in a git-tracked directory
- You understand what success looks like
激活前,请确认:
- 任务具备清晰、可衡量的完成标准
- 可通过编程方式验证成功(测试、构建、特定文件状态)
- 工作目录已通过git追踪
- 你清楚成功的具体表现
Step 2: Create State File
步骤2:创建状态文件
Create with the following structure:
.claude/ralph-loop.local.mdmarkdown
---
active: true
iteration: 0
max_iterations: 20
completion_promise: null
---创建文件,结构如下:
.claude/ralph-loop.local.mdmarkdown
---
active: true
iteration: 0
max_iterations: 20
completion_promise: null
---Your Task Prompt Here
你的任务提示
Objective
目标
[Clear statement of what needs to be accomplished]
[清晰说明需要完成的内容]
Completion Criteria
完成标准
Complete when TODO.md shows [x] ALL_TASKS_COMPLETE
当TODO.md显示[x] ALL_TASKS_COMPLETE时即完成
Verification Commands
验证命令
Run these to check progress:
[test command][build command]
运行以下命令检查进度:
[测试命令][构建命令]
Context
上下文
- Read [relevant files] for specifications
- Follow [conventions file] for code style
undefined- 阅读[相关文件]以获取规格说明
- 遵循[约定文件]的代码风格
undefinedStep 3: Create TODO.md (Recommended Completion Method)
步骤3:创建TODO.md(推荐的完成方式)
Create in your project root:
TODO.mdmarkdown
undefined在项目根目录创建:
TODO.mdmarkdown
undefinedTask Checklist
任务检查清单
Tasks
任务
- Task 1
- Task 2
- Task 3
- 任务1
- 任务2
- 任务3
Completion
完成标记
- ALL_TASKS_COMPLETE
undefined- ALL_TASKS_COMPLETE
undefinedStep 4: Start the Loop
步骤4:启动循环
Simply run Claude normally. The Stop hook will detect the state file and keep the loop running until completion is detected.
bash
claude直接正常运行Claude即可。Stop钩子会检测状态文件并保持循环运行,直至检测到任务完成。
bash
claudeTwo Completion Methods
两种完成方式
Method 1: TODO.md Markers (Recommended)
方式1:TODO.md标记(推荐)
The hook checks for . This is more reliable because:
TODO.md[x] ALL_TASKS_COMPLETE- It's visible in the file system
- It can be tracked in git
- Claude can easily update it
- You can see progress (X/Y tasks complete)
钩子会检查中是否存在。这种方式更可靠,因为:
TODO.md[x] ALL_TASKS_COMPLETE- 可在文件系统中直观查看
- 可通过git追踪
- Claude可轻松更新
- 你能看到任务进度(已完成X/Y项任务)
Method 2: Promise Tags (Legacy)
方式2:Promise标签(旧版)
Set in the state file and output when complete.
completion_promise<promise>YOUR_TEXT</promise>markdown
---
active: true
iteration: 0
max_iterations: 20
completion_promise: "feature implemented"
---When Claude outputs , the loop ends.
<promise>feature implemented</promise>在状态文件中设置,并在完成时输出。
completion_promise<promise>YOUR_TEXT</promise>markdown
---
active: true
iteration: 0
max_iterations: 20
completion_promise: "feature implemented"
---当Claude输出时,循环将结束。
<promise>feature implemented</promise>Configuration Options
配置选项
In frontmatter:
.claude/ralph-loop.local.md| Option | Default | Description |
|---|---|---|
| | Set to |
| | Current iteration count (auto-incremented) |
| | Safety cap (0 = unlimited) |
| | Text to match for promise completion |
在的前置元数据中:
.claude/ralph-loop.local.md| 选项 | 默认值 | 描述 |
|---|---|---|
| | 设置为 |
| | 当前迭代次数(自动递增) |
| | 安全上限(0表示无限制) |
| | 用于匹配完成状态的文本 |
During Execution
执行期间
Iteration Status
迭代状态
Every iteration shows:
- Current iteration number
- Task progress (from TODO.md)
- Completion criteria
- Max iterations remaining
每次迭代都会显示:
- 当前迭代次数
- 任务进度(来自TODO.md)
- 完成标准
- 剩余最大迭代次数
Checkpoint Notifications
检查点通知
Every 5 iterations, you'll see a checkpoint reminder to:
- Review changes:
git log --oneline -10 - Verify progress is on track
- Consider adjusting the prompt if stuck
每5次迭代,你会收到检查点提醒,需:
- 查看变更:
git log --oneline -10 - 验证进度是否符合预期
- 若陷入停滞,考虑调整提示语
Manual Intervention
人工干预
To pause the loop:
bash
undefined暂停循环:
bash
undefinedEdit the state file
编辑状态文件
Change active: true → active: false
将active: true改为active: false
**To stop immediately:**
```bash
rm .claude/ralph-loop.local.mdTo resume:
bash
undefined
**立即停止:**
```bash
rm .claude/ralph-loop.local.md恢复运行:
bash
undefinedRe-create or edit the state file
重新创建或编辑状态文件
Set active: true
设置active: true
claude
undefinedclaude
undefinedSafety Features
安全特性
- Iteration cap: Prevents infinite loops (default: 20)
- Git tracking: Every change is revertible
- Checkpoint notifications: Reminders to review progress
- Clear completion criteria: Loop only exits on explicit success
- Cost awareness: Track iterations to estimate API costs
- 迭代上限:防止无限循环(默认值:20)
- Git追踪:所有变更均可回滚
- 检查点通知:提醒你查看进度
- 清晰的完成标准:仅在明确成功时退出循环
- 成本意识:追踪迭代次数以估算API成本
Example: MetricFlow Phase 7-8
示例:MetricFlow第7-8阶段
markdown
---
active: true
iteration: 0
max_iterations: 25
completion_promise: null
---markdown
---
active: true
iteration: 0
max_iterations: 25
completion_promise: null
---MetricFlow Phase 7-8: Educator Agent
MetricFlow第7-8阶段:Educator Agent
Objective
目标
Implement the Educator Agent that uses Claude API to generate educational
explanations for code metrics.
实现Educator Agent,该Agent使用Claude API为代码指标生成教学解释。
Completion Criteria
完成标准
Complete when TODO.md shows [x] ALL_TASKS_COMPLETE
当TODO.md显示[x] ALL_TASKS_COMPLETE时即完成
Verification Commands
验证命令
cd backend && python -m pytest tests/test_educator.py -vcd backend && python -c "from app.agents.educator import EducatorAgent; print('OK')"
cd backend && python -m pytest tests/test_educator.py -vcd backend && python -c "from app.agents.educator import EducatorAgent; print('OK')"
Context
上下文
- Read docs/plans/MASTER_PLAN.md sections 5.3 (Educator Agent)
- Follow CLAUDE.md for project conventions
- Analyzer and Pattern agents already complete (use their output formats)
- 阅读docs/plans/MASTER_PLAN.md的5.3节(Educator Agent)
- 遵循CLAUDE.md的项目约定
- Analyzer和Pattern Agent已完成(可参考它们的输出格式)
Instructions
说明
- Check TODO.md for current task list
- Implement next incomplete task
- Write tests as you go
- Run verification after each change
- Mark [x] ALL_TASKS_COMPLETE when done
And corresponding `TODO.md`:
```markdown- 查看TODO.md中的当前任务列表
- 实现下一个未完成的任务
- 边开发边编写测试
- 每次变更后运行验证命令
- 完成后标记[x] ALL_TASKS_COMPLETE
对应的`TODO.md`:
```markdownPhase 7-8: Educator Agent
第7-8阶段:Educator Agent
Tasks
任务
- Create EducatorAgent class skeleton in backend/app/agents/educator.py
- Add Claude API client initialization
- Implement explain_complexity() method
- Implement explain_maintainability() method
- Implement explain_code_smells() method
- Add course concept mapping
- Write unit tests for all methods
- Integration test with Analyzer output
- 在backend/app/agents/educator.py中创建EducatorAgent类骨架
- 添加Claude API客户端初始化逻辑
- 实现explain_complexity()方法
- 实现explain_maintainability()方法
- 实现explain_code_smells()方法
- 添加课程概念映射
- 为所有方法编写单元测试
- 与Analyzer输出进行集成测试
Completion
完成标记
- ALL_TASKS_COMPLETE
undefined- ALL_TASKS_COMPLETE
undefinedTroubleshooting
故障排除
Loop won't start:
- Check exists
.claude/ralph-loop.local.md - Verify is set in frontmatter
active: true
Loop won't stop:
- Ensure TODO.md contains exactly (case-insensitive)
[x] ALL_TASKS_COMPLETE - Or check matches your output tag
completion_promise - Check isn't set to 0 (unlimited)
max_iterations - Manual stop:
rm .claude/ralph-loop.local.md
Stuck on same error:
- Review the error pattern
- Adjust the prompt with more specific guidance
- Consider breaking task into smaller subtasks
Costs too high:
- Reduce
max_iterations - Use shorter checkpoint intervals for early review
- Consider if task is too complex for Ralph mode
循环无法启动:
- 检查是否存在
.claude/ralph-loop.local.md - 确认前置元数据中设置了
active: true
循环无法停止:
- 确保TODO.md中包含确切的(不区分大小写)
[x] ALL_TASKS_COMPLETE - 或检查是否与输出标签匹配
completion_promise - 检查是否未设置为0(无限制)
max_iterations - 手动停止:
rm .claude/ralph-loop.local.md
卡在同一错误:
- 分析错误模式
- 调整提示语,提供更具体的指导
- 考虑将任务拆分为更小的子任务
成本过高:
- 降低的值
max_iterations - 缩短检查点间隔以便尽早查看进度
- 评估任务是否过于复杂,不适合使用Ralph模式
Tips for Success
成功技巧
- Clear prompts reduce iterations by 40-60% - be specific
- Start with small max_iterations (5-10) until confident
- Git commit after every checkpoint - easy rollback
- Use TODO.md completion - more reliable than promise tags
- Monitor first few iterations - catch bad patterns early
- Supervised autonomy - review at checkpoints for course projects
- 清晰的提示可减少40-60%的迭代次数——请尽可能具体
- 初始设置较小的最大迭代次数(5-10次),直至你对模式有信心
- 每次检查点后提交git——便于回滚
- 使用TODO.md完成方式——比promise标签更可靠
- 监控前几次迭代——尽早发现不良模式
- 监督式自主——针对课程项目,在检查点进行审核
Cost Estimates
成本估算
| Task Complexity | Iterations | Estimated Cost |
|---|---|---|
| Simple (single feature) | 5-10 | $5-15 |
| Medium (multi-file changes) | 10-20 | $15-30 |
| Complex (full phase) | 20-50 | $30-75 |
| 任务复杂度 | 迭代次数 | 预估成本 |
|---|---|---|
| 简单(单一功能) | 5-10 | $5-15 |
| 中等(多文件变更) | 10-20 | $15-30 |
| 复杂(完整阶段) | 20-50 | $30-75 |
Quick Start Template
快速启动模板
bash
undefinedbash
undefined1. Create state file
1. 创建状态文件
mkdir -p .claude cat > .claude/ralph-loop.local.md << 'EOF'
active: true iteration: 0 max_iterations: 20 completion_promise: null
mkdir -p .claude cat > .claude/ralph-loop.local.md << 'EOF'
active: true iteration: 0 max_iterations: 20 completion_promise: null
Your Task
你的任务
Objective
目标
[What you want to accomplish]
[你想要完成的内容]
Completion
完成标记
Check TODO.md for [x] ALL_TASKS_COMPLETE
EOF
检查TODO.md中的[x] ALL_TASKS_COMPLETE
EOF
2. Create TODO.md
2. 创建TODO.md
cat > TODO.md << 'EOF'
cat > TODO.md << 'EOF'
Tasks
任务
- First task
- Second task
- 第一项任务
- 第二项任务
Completion
完成标记
- ALL_TASKS_COMPLETE EOF
- ALL_TASKS_COMPLETE EOF
3. Start Ralph loop
3. 启动Ralph循环
claude
undefinedclaude
undefined