Part 2 of 4
你安装了 Ralph,跑了一个循环,然后……效果一般?也许它卡住了。也许它在兜圈子。也许它构建了点什么,但不太是你想要的。
这很正常。没有结构的 Ralph 就像任何自主代理一样——有能力但没方向。
这一部分介绍三阶段方法论:一个将模糊的项目想法变成可运行代码的实用工作流——而且是在你睡觉时完成的。
核心洞察:将规划与构建分离
Geoffrey Huntley 将其描述为 「一个漏斗,3 个阶段,2 个提示,1 个循环。」 以下是分解:
- 阶段 1:需求 — 你和 Claude 对话,定义规格和验收标准
- 阶段 2:规划 — Claude 分析规格(只读循环),输出优先级排序的 TODO 列表
- 阶段 3:构建 — Claude 逐个实现任务,提交,循环直到完成
这种分离至关重要。正如 The Ralph Playbook 所述:「PLANNING 提示做差距分析并输出优先级排序的 TODO 列表——不实现,不提交。BUILDING 提示选择任务,实现,运行测试,提交。」
为什么有效: 规划模式阻止 Claude 在理解全貌之前就跳进代码。构建模式每次只专注于一个任务,而不是范围蔓延到混乱。
阶段 1:需求(人类参与的部分)
这是唯一需要你主动参与的阶段。也是大多数人偷懒的地方——这正是为什么他们的 Ralph 会话会产出垃圾。
在写任何代码之前,至少花 30 分钟和 Claude 讨论需求。认真的。这个对话就是你捕捉那些「等等,这个怎么办……」时刻的地方——否则这些问题会让你的自主构建脱轨。

规格文件里写什么
创建 specs/*.md 文件作为唯一的事实来源。不要过度纠结格式——重点是清晰:
提示: 如果你想要一种结构化的需求收集方法,看看 JeredBlu 的 PRD Creator——一个通过对话引导你创建全面规格的提示系统。
specs/├── authentication.md # 登录、JWT、会话├── api-design.md # 接口端点、请求/响应格式├── database.md # 数据库 Schema、关系└── testing.md # 覆盖率要求、测试策略
那场重要的对话
一个好的需求对话是这样进行的:
你: "我想构建一个管理 todo 的 REST API"Claude: "让我们分解一下。你需要什么操作?"你: "CRUD 操作 - 创建、读取、更新、删除 todo"Claude: "明白了。认证怎么处理?用户账户呢?"你: "要的,用户只应该看到自己的 todo"Claude: "有道理。我们应该怎么处理……"
这种来回对话会暴露你独自想不到的边界情况。对话结束后,Claude 生成记录了所有讨论内容的结构化规格。
这笔投入是值得的: 30 分钟的需求对话能防止 Ralph 在模糊目标上转好几个小时。
阶段 2:规划
这是一个独立的模式,使用 PROMPT_plan.md。当你需要生成或重新生成实施计划时,明确运行规划模式。
规划模式在规格和代码之间做差距分析,创建优先级排序的 TODO 列表,不做任何实现或提交。
运行 Ralph 的两种方式
运行 Ralph 循环有两种方法,理解何时使用哪种至关重要:
插件方式(/ralph-loop) — 在单个上下文窗口内运行迭代。更容易设置,适合较短的任务(20-30 次迭代以下)。
Bash 循环方式(loop.sh) — 每次迭代启动一个全新的 Claude 实例。正如 Geoffrey Huntley 的原始指南 所解释的,每次迭代「确定性地加载相同的文件并从磁盘读取当前状态」。这防止了在单个上下文中运行多次迭代时可能出现的上下文膨胀和幻觉。参见社区示例:snarktank/ralph.sh、frankbria/ralph_loop.sh、peteristhegreat 的 gist。
为什么全新上下文很重要: 插件在一个上下文窗口中运行所有内容,随着时间推移会被填满。30-40 次迭代之后,Claude 可能开始忽略你提示的部分内容或做出不一致的决定。Bash 循环方式通过每次重新开始来避免这个问题——唯一共享的状态是写入磁盘的内容(你的代码、
IMPLEMENTATION_PLAN.md和progress.txt)。关于高级技巧,参见 Advanced Context Engineering for Coding Agents。
我们将在下面的 Bash 循环脚本 部分展示如何创建 loop.sh。
如何运行规划模式
对于规划,插件方式就够了,因为你只需要约 5 次迭代(上下文膨胀不是问题):
/ralph-loop "$(cat PROMPT_plan.md)" --max-iterations 5 --completion-promise "PLAN COMPLETE"
或者使用 Bash 循环(参见下面的 Bash 循环脚本):
./loop.sh plan
PROMPT_plan.md 模板
这里是规划模式的完整模板。真实示例请参见 ClaytonFarr 的 PROMPT_plan.md 或 snarktank 的 prompt.md。
# PLANNING MODEYou are in PLANNING mode. Your job is analysis, not implementation.## Your Task1. Read all files in specs/2. Review the existing codebase in src/3. Perform gap analysis between requirements and current implementation4. Create/update IMPLEMENTATION_PLAN.md with prioritized tasks## Task StructureEach task in the plan should include:- Unique ID (e.g., TASK-001, TASK-002)- Priority level (high/medium/low)- Clear description (what needs to be built)- Acceptance criteria (how you'll know it's done)- Required tests (specific test files or patterns)- Dependencies (tasks that must complete first)## File OrganizationOrganize tasks by priority:- High Priority: Critical path items- Medium Priority: Important but not blocking- Low Priority: Nice-to-haves## Critical Rules- DO NOT write any code- DO NOT make any commits- DO NOT implement features- DO NOT run tests or builds- ONLY analyze and planWhen complete, output "PLAN COMPLETE"
查看真实示例: 浏览社区的实际提示文件:
- snarktank/ralph — 带约束结构的完整 prompt.md
- ClaytonFarr/ralph-playbook — PROMPT_plan.md 模板
- frankbria/ralph-claude-code — 带退出检测的实现
规划产出什么
规划模式生成:
IMPLEMENTATION_PLAN.md - 你的活 TODO 列表
示例结构:
# Implementation Plan## High Priority### TASK-001: User Authentication- **Status**: pending- **Description**: Implement JWT-based auth with login/logout- **Acceptance Criteria**:- User can register with email/password- User can login and receive JWT token- Protected routes require valid JWT- **Tests**: `auth.test.ts`- **Dependencies**: none### TASK-002: Todo CRUD Endpoints- **Status**: pending- **Description**: Create POST, GET, PUT, DELETE endpoints for todos- **Acceptance Criteria**:- POST /api/todos creates a todo- GET /api/todos returns user's todos- PUT /api/todos/:id updates a todo- DELETE /api/todos/:id deletes a todo- **Tests**: `todos.test.ts`- **Dependencies**: TASK-001## Medium Priority### TASK-003: Input Validation- **Status**: pending- **Description**: Add Zod validation for all endpoints...
重要: 正如 The Ralph Playbook 所指出的,计划是一次性的。如果它变得过时或不准确,删掉它,重新运行规划模式来重新生成。
阶段 3:构建
这是持续循环模式,使用 PROMPT_build.md。这就是 Ralph 大放异彩的地方——在你睡觉时自主实现任务。
什么是反压(Backpressure)? 在 Ralph 方法论中,反压指的是自动化验证机制——测试、类型检查、Linter、构建——拒绝不合格的工作。与其精确规定 Claude 如何实现某些东西,你创建「门禁」来拒绝不好的输出。失败的测试迫使 Claude 迭代直到代码正确。这就是自主编程循环背后的核心洞察。
构建模式假设计划已经存在,每次选取一个任务,实现并测试,提交,然后用全新的上下文循环。
如何运行构建模式
快速开始(使用插件):
/ralph-loop "$(cat PROMPT_build.md)" --max-iterations 50 --completion-promise "ALL TASKS COMPLETE"
长时间运行的构建(推荐): 使用 Bash 循环方式来获得每次迭代的全新上下文。设置好 loop.sh(如下所示)之后:
./loop.sh # 默认为构建模式./loop.sh build 100 # 自定义迭代限制
PROMPT_build.md 模板
这里是构建模式的完整模板。真实示例请参见 ClaytonFarr 的 PROMPT_build.md 或 frankbria 的 PROMPT.md。
# BUILDING MODEYou are in BUILDING mode. Your job is implementation with quality gates.## Your Task LoopFor each iteration:1. Read IMPLEMENTATION_PLAN.md and progress.txt2. Pick the SINGLE highest priority task with status: pending3. Study existing code before implementing4. Implement the feature completely5. Write comprehensive tests6. Run all tests and type checks: npm test && npm run type-check7. If tests fail, fix them immediately—DO NOT proceed8. When all tests pass:- Commit with descriptive message format: "feat(area): description"- Update task status to completed in IMPLEMENTATION_PLAN.md- Append learnings to progress.txt with timestamp## Critical Rules- Work on ONE task at a time—no exceptions- NEVER commit failing tests- NEVER skip test execution- Tests are your backpressure—respect them- Each commit must be atomic and working- Don't add features not in the plan- Don't refactor unrelated code## Backpressure EnforcementTests MUST pass before committing:```bashnpm test && npm run type-check && npm run lint```If any check fails, fix it in the same iteration.## Progress TrackingAfter completing each task, append to progress.txt:```[YYYY-MM-DD HH:MM] Completed TASK-XXX: Task Title- What was implemented- Key decisions made- Challenges encountered- Learnings for next tasks```When all tasks show status: completed, output "ALL TASKS COMPLETE"
查看真实示例: 浏览实际的 PROMPT_build.md 实现:
- ClaytonFarr/ralph-playbook — PROMPT_build.md 模板
- frankbria/ralph-claude-code — 带智能退出检测的实现
- mikeyobrien/ralph-orchestrator — 增强版编排实现
深度阅读: Geoffrey Huntley 的 Don't Waste Your Back Pressure 解释了为什么验证和拒绝机制对自主循环至关重要。
Bash 循环脚本
这里是一个支持规划和构建两种模式的 loop.sh 脚本。这个模式来自 JeredBlu 的指南 和 Geoffrey Huntley 的原始方法:
#!/bin/bash# loop.sh - 每次迭代全新上下文MODE=${1:-build} # 默认为构建模式MAX_ITERATIONS=${2:-50}if [[ "$MODE" == "plan" ]]; thenPROMPT_FILE="PROMPT_plan.md"COMPLETION="PLAN COMPLETE"MAX_ITERATIONS=${2:-5} # 规划需要更少的迭代elsePROMPT_FILE="PROMPT_build.md"COMPLETION="ALL TASKS COMPLETE"fiecho "Running in $MODE mode with max $MAX_ITERATIONS iterations"echo "Using prompt: $PROMPT_FILE"echo "Completion signal: $COMPLETION"echo "=========================================="for ((i=1; i<=$MAX_ITERATIONS; i++)); doecho "Iteration $i / $MAX_ITERATIONS"echo "--------------------------------"# -p 标志让 Claude 以单次模式运行(参见精通第 1 部分:https://jocv.dev/blog/claude-code-mastery-01-getting-started#one-shot-mode-quick-questions)result=$(claude -p "$(cat $PROMPT_FILE)" --output-format text 2>&1) || trueecho "$result"if [[ "$result" == *"$COMPLETION"* ]]; thenecho "=========================================="echo "$MODE complete after $i iterations."exit 0fiecho "--- End of iteration $i ---"doneecho "Reached max iterations ($MAX_ITERATIONS)"exit 1
设置为可执行:
chmod +x loop.sh
使用方法:
./loop.sh plan # 运行规划模式(默认 5 次迭代)./loop.sh build # 运行构建模式(默认 50 次迭代)./loop.sh # 等同于 ./loop.sh build./loop.sh build 30 # 构建模式,最多 30 次迭代
这给了你每次迭代全新的上下文,防止在长时间运行时使用插件方式可能出现的上下文膨胀。
关于更复杂的实现(错误处理、日志记录和并行执行),请参见 snarktank/ralph 和 mikeyobrien/ralph-orchestrator。
构建循环流程
正如 11 Tips for AI Coding with Ralph Wiggum 所记录的:
- 从
IMPLEMENTATION_PLAN.md中选取最高优先级任务 - 实现功能
- 运行所有测试和类型检查(反压!)
- 只有全部通过才提交
- 更新
progress.txt,记录心得 - 用全新上下文循环 → 重复
关键洞察: 每次迭代在全新的上下文窗口中运行(使用 Bash 循环方式)。这防止了上下文退化,让 Claude 保持专注。
完整的三阶段工作流
以下是所有阶段如何配合工作(使用前面 Bash 循环脚本 部分的 loop.sh 脚本):
# 阶段 1:定义需求(对话式,不是循环)# 和 Claude 对话,创建 specs/*.md 文件# 示例:specs/authentication.md、specs/api-design.md、specs/database.md# 阶段 2:生成计划(运行一次,或在计划需要刷新时运行)./loop.sh plan# 或使用插件:/ralph-loop "$(cat PROMPT_plan.md)" --max-iterations 5 --completion-promise "PLAN COMPLETE"# 创建包含优先级任务的 IMPLEMENTATION_PLAN.md# 阶段 3:自主构建(持续循环)./loop.sh# 或使用插件:/ralph-loop "$(cat PROMPT_build.md)" --max-iterations 50 --completion-promise "ALL TASKS COMPLETE"# 逐个实现任务,提交,循环# 当计划变得过时(功能变更、新需求)时:rm IMPLEMENTATION_PLAN.md./loop.sh plan # 重新生成计划./loop.sh # 继续构建
文件结构
你的项目应该有这样的结构:
your-project/├── specs/│ ├── authentication.md│ ├── api-design.md│ ├── database.md│ └── testing.md├── PROMPT_plan.md # 规划模式提示├── PROMPT_build.md # 构建模式提示├── IMPLEMENTATION_PLAN.md # 由规划生成,由构建更新├── progress.txt # 只追加的心得日志├── loop.sh # Bash 编排脚本└── src/ # 你的实际代码├── api/├── auth/└── tests/
两种模式的关键区别
规划模式:
- 提示文件:
PROMPT_plan.md - 目标:分析和规划
- 会提交吗?不会
- 会写代码吗?不会
- 会运行测试吗?不会
- 会更新计划吗?会(创建/覆盖)
- 典型迭代次数:1-5
- 什么时候运行?一次,或刷新计划时
构建模式:
- 提示文件:
PROMPT_build.md - 目标:实现和测试
- 会提交吗?会
- 会写代码吗?会
- 会运行测试吗?会
- 会更新计划吗?会(标记任务为已完成)
- 典型迭代次数:20-100+
- 什么时候运行?持续到完成
长时间运行 Ralph 循环的关键文件
progress.txt
跟踪跨迭代完成的内容。正如 The Ralph Playbook 所解释的:「progress.txt 是长时间运行代理的标准实践。通过提示将其喂给代理,并使用动词 'append'(追加)来确保它不会更新之前的条目。」
Ralph 读取这个文件来理解上下文,而不需要重新探索整个代码库。
如何使用:
在你的提示中,指示代理:"After each task, APPEND (don't overwrite) your learnings to progress.txt"
progress.txt 条目示例:
[2026-01-16 14:30] Completed TASK-001: User Authentication- Implemented JWT signing/verification using jsonwebtoken- Added httpOnly cookie storage for tokens- All auth tests passing (12/12)- Learning: Cookie sameSite setting needed for cross-origin requests- Challenge: Had to debug token expiry edge case- Next: Tackle TASK-002 (Todo CRUD endpoints)
IMPLEMENTATION_PLAN.md
你的活 TODO 列表,Ralph 在完成任务时更新。这个文件连接规划和构建模式。
结构:
# Implementation Plan## High Priority- [ ] TASK-001: User Authentication (status: pending)- Tests: auth.test.ts- Dependencies: none## Medium Priority- [ ] TASK-002: Todo CRUD (status: pending)- Tests: todos.test.ts- Dependencies: TASK-001## Completed- [x] TASK-000: Project Setup (status: completed)- Completed: 2026-01-15 22:00
编写有效的 Ralph 提示
关键提示要素
正如 11 Tips for AI Coding with Ralph Wiggum 所记录的,每个 Ralph 提示都应包含这些要素:
进度跟踪:
Read progress.txt to see what's been accomplished.After completing each task, APPEND (never overwrite) your progress.
通过测试实现反压:
Each commit MUST pass all tests and type checks.Run: npm test && npm run type-check && npm run lintIf anything fails, fix it before moving on.NEVER commit broken code.
范围控制:
Pick the SINGLE highest priority task from IMPLEMENTATION_PLAN.md.Work ONLY on that task—don't add features or refactor unrelated code.
先探索:
Study the codebase first.Don't assume something isn't implemented—verify by reading files.Use ultrathink before making changes.
有效的语言模式
基于 The Ralph Wiggum Playbook 和 11 Tips 的社区经验,这些短语能改善 Claude 的行为:
- "Study the codebase first" → 减少对已有内容的假设
- "Don't assume not implemented" → 鼓励在编写前先验证
- "Ultrathink before acting" → 在修改前促进仔细规划(
ultrathink为复杂推理分配最大思考预算——参见 精通第 9 部分) - "Capture the why in commits" → 提高 Git 历史质量
- "MUST pass all tests" → 严格执行质量门禁
接下来
你现在掌握了专业方法论:用于需求的规格文件、用于差距分析的规划模式、用于自主执行的构建模式。文件结构。提示模板。
在 第 3 部分:Ralph TUI 监控 中,我们将介绍长时间运行循环的实时可视化——面板、键盘控制和会话管理。
然后在 第 4 部分:高级模式与故障排除 中,我们深入探讨高级提示工程、常见陷阱、全面的故障排除和企业级模式。
阶段 1:需求 — 和 Claude 对话,创建 specs/*.md 文件
阶段 2:规划 — 只读循环:/ralph-loop "$(cat PROMPT_plan.md)" --max-iterations 5 --completion-promise "PLAN COMPLETE"
阶段 3:构建 — 自主循环:/ralph-loop "$(cat PROMPT_build.md)" --max-iterations 50 --completion-promise "ALL TASKS COMPLETE"
当计划变得过时
rm IMPLEMENTATION_PLAN.md 然后重新运行规划模式
关键要点
三个阶段使用不同的提示:规格、规划、构建
规划模式生成路线图——不写代码,不提交
构建模式一次实现一个任务
测试是你的反压——Ralph 不能提交有 Bug 的代码
progress.txt 在迭代间保存心得
计划是一次性的——过时就重新生成
原子化任务效果最好——自包含的任务,2-3 次迭代内完成,有明确的通过/失败验证
Stay Updated
Get notified about new posts on automation, productivity tips, indie hacking, and web3.
No spam, ever. Unsubscribe anytime.
