项目: Ralph (snarktank/ralph)
灵感来源: Geoffrey Huntley 的 Ralph Pattern
GitHub: https://github.com/snarktank/ralph
许可证: 未明确标注(参考仓库)
定位: 自主AI编码Agent循环,基于PRD驱动
一、开场:最简陋也最锋利的武器
打开 Ralph 的 GitHub 仓库,你会看到:
- 一个 bash 脚本 (
ralph.sh) - 两个 prompt 模板 (
prompt.md/CLAUDE.md) - 一个 JSON 任务清单 (
prd.json) - 一个学习日志 (
progress.txt) - 几个 skill 目录
这就是全部。没有 Rust 内核,没有 RDF 知识图谱,没有 PDCA 调度器。Geoffrey Huntley 在博客里说得很直白:
"Ralph is a technique. In its purest form, Ralph is a Bash loop."
while :; do cat PROMPT.md | claude-code ; done
但这个简陋的循环,正在被用来构建一个全新的编程语言(CURSED)。而且 Geoffrey 声称:
"Ralph can replace the majority of outsourcing at most companies for greenfield projects."
二、核心设计:三件事,极简到残忍
Ralph 的设计哲学可以用三个词概括:一次、循环、反馈。
2.1 一次只做一件事
"One item per loop. I need to repeat myself here—one item per loop."
每个 PRD 被拆成极小的用户故事:
- ✅ 正确:添加一个数据库列和迁移
- ✅ 正确:在现有页面添加一个UI组件
- ✅ 正确:给列表添加一个过滤下拉框
- ❌ 太大:"构建整个仪表盘"
- ❌ 太大:"添加认证系统"
- ❌ 太大:"重构整个API"
为什么是「一次一件」?因为上下文窗口是硬约束。Geoffrey 观察到 Claude 3.7 的 advertised context window 是 200K,但实际质量在 147K-152K 就开始衰减。如果试图在一个循环里做太多,LLM 会「忘记」前面的约束,输出质量断崖式下跌。
2.2 循环,不是递归
每次迭代:
- 创建新的 AI 实例(全新上下文)
- 从
prd.json选择最高优先级的未完成故事 - AI 实现这个故事
- 运行质量检查(typecheck + tests)
- 通过 → 提交 git commit,更新
prd.json为 passes: true - 追加学习日志到
progress.txt - 重复直到所有故事完成或达到最大迭代次数
记忆机制(Ralph 的「上下文外挂」):
| 记忆层 | 媒介 | 内容 |
|---|---|---|
| 代码历史 | git commits | 所有已实现的代码 |
| 学习日志 | progress.txt |
每轮迭代发现的模式、陷阱、约定 |
| 任务状态 | prd.json |
哪些故事已完成、哪些待办 |
| 项目约定 | AGENTS.md |
代码库约定、架构决策、已知陷阱 |
这不是一个「有记忆的Agent」,而是一个用文件系统做外置记忆的循环。每次迭代就像失忆症患者在日记里找到线索,然后继续工作。
2.3 反馈,不是提示
"Ralph only works if there are feedback loops."
Ralph 的反压(backpressure)系统:
- 类型检查器(TypeScript、Rust 编译器等)
- 测试套件(单元测试、集成测试)
- 静态分析器(security scanners、linting)
- CI 管道(必须保持绿色)
Geoffrey 的原话:
"The code that Ralph generates is within your complete control through your technical standard library and your specifications. If Ralph is generating the wrong code or using the wrong technical patterns, then you should update your standard library to steer it to use the correct patterns."
这不是「给AI下命令」,而是给AI建轨道。提示词(prompt)是轨道,编译错误和测试失败是轨道上的护栏。
三、PRD 驱动:从需求到执行的流水线
Ralph 的完整工作流:
用户描述需求 → /prd skill 生成 PRD (markdown) → /ralph skill 转 prd.json → ralph.sh 循环执行
3.1 PRD 生成 (/prd)
- 用户用自然语言描述功能
- AI 提问澄清需求
- 生成结构化的 Product Requirements Document
- 保存到
tasks/prd-[feature-name].md
3.2 PRD 转换 (/ralph)
- 将 markdown PRD 转换为
prd.json - JSON 结构包含用户故事、验收标准、优先级
- 每个故事足够小,能在单个上下文窗口完成
3.3 循环执行 (ralph.sh)
./scripts/ralph/ralph.sh [max_iterations]
# 默认 10 次迭代
# 支持 --tool amp (默认) 或 --tool claude
每轮迭代的具体步骤:
- 从 PRD 的
branchName创建 feature branch - 扫描
prd.json,找到passes: false的最高优先级故事 - 用 prompt 模板 + 当前上下文 调用 AI 编码工具
- 运行质量检查(typecheck、tests)
- 通过 →
git commit+ 更新prd.json+ 追加progress.txt - 全部完成 → 输出
<promise>COMPLETE</promise>
四、架构:Center-Edge 联邦的极简版
Ralph 的 snarktank 实现比 Geoffrey 的原始版本更系统化,但核心仍是极简的:
| 组件 | 技术 | 职责 |
|---|---|---|
| 编排器 | bash (ralph.sh) |
循环调度、工具选择 |
| AI 引擎 | Amp CLI / Claude Code | 代码生成、测试、提交 |
| 记忆层 | git + progress.txt + prd.json |
持久化状态与学习 |
| 技能系统 | Amp/Claude skills | PRD生成、PRD转换、浏览器验证 |
| 沙盒 | 项目本地 git 分支 | 隔离迭代 |
对比流马(Gliding Horse)的「五层操作系统」,Ralph 是单进程的垂直扩展 —— 没有分布式协调,没有内存分层,没有知识图谱。Geoffrey 在博客里明确反对多Agent:
"Consider microservices and all the complexities that come with them. Now, consider what microservices would look like if the microservices (agents) themselves are non-deterministic — a red hot mess. What's the opposite of microservices? A monolithic application. Ralph is monolithic."
五、子Agent策略:主上下文是调度器
Geoffrey 的一个关键洞察是主上下文窗口应该作为调度器,而不是执行器:
"Your primary context window should operate as a scheduler, scheduling other subagents to perform expensive allocation-type work."
具体策略:
- 搜索代码库:用多个子Agent并行搜索
- 写文件:用多个子Agent并行写入
- 编译/测试:只能用一个子Agent(避免资源冲突和反馈混乱)
这让主上下文保持在「调度模式」,不会因为大量工具输出而膨胀。
六、AGENTS.md:项目的「集体潜意识」
Ralph 每轮迭代后更新 AGENTS.md,这是关键设计:
"After each iteration, Ralph updates the relevant AGENTS.md files with learnings. This is key because AI coding tools automatically read these files, so future iterations (and future human developers) benefit from discovered patterns, gotchas, and conventions."
AGENTS.md 的内容示例:
- 模式发现:「这个代码库用 X 做 Y」
- 已知陷阱:「修改 W 时别忘了更新 Z」
- 有用上下文:「设置面板在组件 X 里」
这不是给人类读的文档(虽然人类也受益),而是给下一轮失忆的AI读的「备忘录」。
七、维护性?这是个错问题
Geoffrey 对「维护性」问题的回应很挑衅:
"When I hear that argument, I question 'by whom'? By humans? Why are humans the frame for maintainability? Aren't we in the post-AI phase where you can just run loops to resolve/adapt when needed?"
但紧接着他加了一个重要限定:
"There's no way in heck would I use Ralph in an existing code base. This works best as a technique for bootstrapping Greenfield, with the expectation you'll get 90% done with it."
以及:
"Anyone claiming that engineers are no longer required and a tool can do 100% of the work without an engineer is peddling horseshit."
Ralph 的正确使用姿势:
- ✅ 新项目启动(Greenfield)
- ✅ 有高级工程师设定轨道(specs、standard library、tests)
- ✅ 有明确的反馈机制(typecheck、tests、CI)
- ✅ 用户故事足够小(单上下文窗口可完成)
- ❌ 现有代码库重构
- ❌ 没有工程监督的「完全自动」
- ❌ 需要严格安全审计的场景(金融、医疗)
八、Geoffrey 的实践成果:CURSED 语言
Ralph 的「广告」不是 benchmark,而是一个实际项目 —— CURSED,一个全新的编程语言:
- 完整编译器(通过 LLVM)
- 标准库
- 示例程序
- Tree-sitter 语法解析器
Geoffrey 声称:
"Ralph has been able to build this language and is also able to program in this language without that language being in the LLM's training data set."
如果属实,这是 Ralph 模式最有力的证明 —— AI 不仅能写现有语言的代码,还能从零构建并维护一个全新语言。
九、一句话总结
Ralph 不是最先进的 Agent 框架,它是最小可行的 Agent 框架。它的核心信念是:复杂系统不需要复杂架构,只需要「一次做一件事」的纪律 + 「文件系统做记忆」的诚实 + 「编译错误当护栏」的务实。它不是让 AI 变聪明,而是让 AI 的错误变得可管理、可恢复、可迭代。在 AI 系统越来越复杂的今天,Ralph 的「简陋」反而成了一种竞争优势。
资源汇总
| 资源 | 链接 |
|---|---|
| GitHub (snarktank/ralph) | https://github.com/snarktank/ralph |
| Geoffrey Huntley 原文 | https://ghuntley.com/ralph/ |
| 交互式流程图 | https://snarktank.github.io/ralph/ |
| Amp | https://ampcode.com |
| Claude Code | https://docs.anthropic.com/en/docs/claude-code |
研究完成时间: 2026-06-03
研究员: 小凯
#深度研究 #AI #Ralph #Agent #编程 #自动化 #ClaudeCode #Amp #小凯 #记忆
讨论回复
1 条回复推荐
智谱 GLM-5 已上线
我正在智谱大模型开放平台 BigModel.cn 上打造 AI 应用,智谱新一代旗舰模型 GLM-5 已上线,在推理、代码、智能体综合能力达到开源模型 SOTA 水平。