Jarred Sumner 谈如何交付数百万行智能体编写的 Bun 编译器代码
原题How to Ship Millions of Lines of Agent-Written Code — Jarred Sumner, Anthropic
看前速览
Bun 作者 Jarred Sumner 复盘如何用独立测试验收一套由智能体编写的编译器。
保留独立的测试基准
Bun 的 TypeScript 测试能同时跑在 Zig 版和 Rust 版上。重写 Claude Code 则意味着连它自己的测试也得改。
比对三种运行方式
分别用 JIT、AOT 和移除类型的方式运行同一个应用,再用模糊测试检查理应保持一致的行为。
分开集中写代码和持续验证
集中写完先统一构建,后续交给能保留相关上下文的长时子智能体。定期对比指标,及时发现性能退化。
完整摘要
Jarred Sumner 用一个月做了五轮 Bun 的 JavaScript 提前编译器。他险些交付其中一版,后来因为构建太慢又重写了一次。按他公布的结果,最终版让 Claude Code 的内存用量降到约四分之一,启动速度约翻倍,频繁依赖解释器的程序仍可能变慢。团队没有把 Claude Code 改写成 Rust,因为那样需要迁移它的测试。他们沿用 Bun、WebKit 的既有测试,再比对 JIT、AOT 和移除类型后三种运行结果。Sumner 还讲了第三版如何同时调用 140 个智能体集中写代码,后续如何用长时间运行的子智能体持续修正,并用每小时的指标对比跟进变化。整个复盘贯穿一个决策标准:先弄清楚如何验证结果,再决定怎么构建。
章节
文字稿
简要说明Brief Description
Bun 的创作者 Jarred Sumner 讲述了他的团队如何借助编码代理构建一款提前编译的 JavaScript 编译器。他介绍了编译器的五个版本,并展示了既有测试、差异检查和构建基准测试如何影响他们对可发布版本的取舍。Bun creator Jarred Sumner explains how his team built an ahead-of-time JavaScript compiler with coding agents. He describes five versions of the compiler and shows how preexisting tests, differential checks and build benchmarks shaped what they were willing to ship.
目录Table of Contents
- Bun 的一项数百万行代码 PRA multi-million-line PR for Bun
- 一个月内重写五次Five rewrites in a month
- 测试决定采用哪种方法Testing decides the approach
- 为什么不使用 Rust 重写 Claude Code?Why not rewrite Claude Code in Rust
- 内存占用减少 4 倍,启动速度提升 2 倍4x less memory, 2x faster startup
- 将类型视为预测Types as predictions
- 早于该项目的测试Tests that predate the project
- 必须保持一致的三个版本Three versions that must agree
- 分批编写,然后修正Write in batches, then fix
- 当代码一团糟时When the code was slop
- 同时运行 140 个代理140 agents at once
- 构建时间与第五次重写Build times and the fifth rewrite
- 长期运行的子代理Long-lived subagents
- 每小时评分卡和模糊测试Hourly scorecards and fuzzing
- 你每天实际上都在做什么What you actually do all day
- 大型智能体项目的经验教训Lessons for large agent projects
一个包含数百万行代码的 Bun 拉取请求A multi-million-line PR for Bun
大家好。我叫 Jarred,是 Bun 的创建者,目前在 Anthropic 工作。Bun 是为 Claude Code 提供支持的 JavaScript 运行时。我最近合并了一个包含数百万行代码的 PR,为 Bun 添加了一个提前编译器。它的思路是在运行前提前编译代码,而不是在运行时承担 JIT 的开销。今年夏天,我用 Rust 重写了 Bun。这个编译器项目比那项工作规模更大。大型项目的一项挑战,是如何有把握地发布由智能体编写的数百万行代码。Hello everyone. My name is Jarred. I'm the creator of Bun and I work at Anthropic. Bun is the JavaScript runtime that powers Claude Code. I recently merged a several-million-line PR to add an ahead-of-time compiler to Bun. The idea is to compile the code ahead of time instead of paying the cost of a JIT at runtime. Over the summer, I rewrote Bun in Rust. This compiler is a larger project than that. One of the challenges with a large project is how to confidently ship millions of lines of code written by agents.
经过一番讨论,最初的需求基本上是:我们需要让 Claude Code 运行得更快,并且减少内存占用。我们可以修改 WebKit 或 Bun。Bun 在底层使用 JavaScriptCore,而 JavaScriptCore 是 WebKit 的一部分。After some discussion, the opening prompt was essentially that we needed to make Claude Code faster and use much less memory. We could modify either WebKit or Bun. Bun uses JavaScriptCore, which is part of WebKit, under the hood.
一个月内重写五次Five rewrites in a month
一个多月过去了,编译器已经重写了五次。前两次重写都很短。我差点就把蓝色的那个发布出去,但它编译代码花的时间太长,所以我们又重写了一遍。下一个版本下周就要发布。It's been a little over a month and the compiler has gone through five rewrites. The first couple were pretty short. I nearly shipped the blue one, but it was taking too long to compile the code, so we wrote it again. The next version is shipping next week.
测试决定采用哪种方法Testing decides the approach
你会受到代码测试方式的很大限制。你采用的方法取决于你如何验证代码确实有效,以及如何确保自己可以信任它。You're really constrained by how you test that the code works. The approach you take is driven by how you verify that it works and that you can trust it.
为什么要使用提前编译器?对于典型的 JavaScript 应用,大约有一半的 CPU 时间花在按需编译代码上。人们将 JavaScript 代码库改写为 Rust(有时也改写为 Go),原因之一是 JIT 的开销与应用程序单线程运行这两者叠加。使用 AOT 时,你会在运行前完成编译;使用 JIT 时,则是边运行边编译。这一点尤其重要,因为每次 Claude Code 会话(例如使用 iOS 应用时)原本都要花费 CPU 时间进行编译。Why an ahead-of-time compiler? For a typical JavaScript application, roughly half the CPU time is spent compiling code on demand. When people rewrite JavaScript codebases in Rust or sometimes Go, one reason is the combination of JIT overhead and a single-threaded application. With AOT, you compile before running. With JIT, you compile as you go. That's particularly important when each Claude Code session, such as when using the iOS app, would otherwise spend CPU time compiling.
为什么不使用 Rust 重写 Claude Code?Why not rewrite Claude Code in Rust
我们讨论过是否要用 Rust 重写 Claude Code。我们已经用 Rust 重写了 Bun。Bun 是一个相对容易重写的项目,因为它的测试是用一种独立于实现的语言编写的。我们可以用相同的 TypeScript 测试来测试 Zig 和 Rust 版本。We discussed whether to rewrite Claude Code in Rust. We'd already rewritten Bun in Rust. Bun was a comparatively easy project to rewrite because the tests were written in a language independent of the implementation. We could run the same TypeScript tests against the Zig and Rust versions.
Claude Code 的测试也使用 TypeScript 编写,但如果我们用 Rust 重写应用,就得把它的测试套件也移植过去。一旦开始重写测试,就很难再相信这些测试了。这看起来风险太大。于是,我们转而思考:能不能在不重写 Claude Code 的前提下,让 JavaScript 运行得更快?Claude Code's tests are written in TypeScript too, but we would have to port its test suite if we rewrote the application in Rust. As soon as you're rewriting the tests, you can't really trust the tests anymore. That seemed too risky. Instead, we asked whether we could make JavaScript faster without rewriting Claude Code.
内存占用减少 4 倍,启动速度提升 2 倍4x less memory, 2x faster startup
最终生成的 Claude Code 构建版本使用的内存约为原来的四分之一,启动速度约快一倍。我们正在内部试用,计划很快对外开放。这里有一个有趣的对比:TypeScript 7 是从 JavaScript 和 TypeScript 手动重写为 Go 的。在单线程对比中,使用 Bun 编译的版本现在比单线程的 TypeScript Go 移植版更快。我们也计划为使用 Bun 进行提前编译的版本添加线程支持,但目前还没实现。相关 PR 已经提交。The resulting Claude Code build uses about a quarter of the memory and starts about twice as fast. We're dogfooding it internally and plan to make it available externally soon. There's an interesting comparison with TypeScript 7, which was rewritten by hand from JavaScript and TypeScript into Go. In the single-threaded comparison, the version compiled with Bun is now faster than the single-threaded TypeScript Go port. We also plan to add threads to the ahead-of-time compiled version with Bun, but we haven't done so yet. There's a PR.
将类型视为预测Types as predictions
TypeScript 类型不一定非得正确,因此编译器会把类型视为预测,而不是盲目信任它们。这类问题很适合交给模型处理,因为模型擅长编写编译器。TypeScript types don't necessarily have to be correct, so the compiler treats types as predictions instead of trusting them. This kind of problem works well for models, which are good at writing compilers.
项目创建之前的测试Tests that predate the project
有很多测试无需自己编写就可以运行。我们确实编写了大量测试,但在很大程度上可以信任那些早于该项目的测试。重写 Bun 的 Rust 版本时,我们可以使用现有的 Bun 测试。对于编译器,我们有 Bun 现有的测试套件、WebKit 的测试、Test262,以及 Cloudflare 的测试。我们还让 Bun 自带的测试运行器能够在 AOT 模式下运行。There are many tests you can run without creating them yourself. We do write a lot of tests, but we can largely trust the ones that predate the project. With the Bun Rust rewrite, we could use the existing Bun tests. For the compiler, we have Bun's existing suite, WebKit's tests, Test262 and tests from Cloudflare. We also made it so Bun's own test runner could be run in AOT mode.
AOT 编译器有两项破坏性变更,可以解释幻灯片中那些特定的测试失败。这些变更主要是出于安全考虑,旨在防止原型污染攻击,同时也有助于提升性能。There are two breaking changes with the AOT compiler that explain the particular test failures on the slide. The changes are mostly for security, to prevent prototype pollution attacks, but they also help performance.
必须保持一致的三个版本Three versions that must agree
你可以运行应用程序的三个版本:一个使用 JIT,一个采用预先编译,还有一个不带类型。只要这三个版本的行为一致,你就知道它在这个特定应用程序中能正常工作。然后,你可以对这类问题进行模糊测试,并让 Claude 为不同模式编写生成器。这样能发现很多 bug。You can run all three versions of an application, one with JIT, one compiled ahead of time and one without the types. As long as all three behave the same, you know it works for that specific application. Then you can fuzz that kind of problem and have Claude write generators for different patterns. You can catch a lot of bugs that way.
这是我参与过的代码量最大的项目。其中很大一部分是测试代码,而且我们还把编译器重写了五次。This is the biggest project I've worked on in terms of code volume. Much of the volume is tests, and we also rewrote the compiler five times.
分批编写,然后修复Write in batches, then fix
先从一批需要编写大量代码的任务开始。然后修复所有问题,检查代码能否编译,运行测试并确认它确实有效。在最初这批任务中,运行一次构建即可。否则,你会把所有时间都花在编译和等待构建错误上。初始代码编写完成后,可以安排多个独立代理处理耗时较长的工作。Start with a batch where you need to write a lot of code. Then fix everything, check that it compiles, run the tests and make sure it really works. In the initial batch, run one build. Otherwise you spend all your time compiling and waiting for build errors. After the initial code has been written, you can use many individual agents on longer-running work.
第一个编译器不太理想。Claude Code 占用了 1 GB 内存。我们尽可能复用了 JavaScriptCore,这限制了该版本的能力。The first compiler wasn't very good. Claude Code used a gigabyte of memory. We reused as much of JavaScriptCore as possible, which limited what that version could achieve.
当代码粗制滥造时When the code was slop
接下来,我们给 Bun 添加了一个 TypeScript 类型检查器,叫作 Bun Check,并让 Claude 使用它。Claude Code 的启动速度提高了一倍,内存占用则降到了大约三分之一。但生成的代码简直一团糟。只要任何对象访问了名为 archive 的属性,编译器就会认为可能用到了 Bun.archive API。我看了之后觉得,这东西没法发布。在这种规模下进行规划也有另一个问题。这个代理很喜欢写 Markdown 设计文档,但 1,500 个 Markdown 文件,谁也不可能看得过来。Next we added a TypeScript type checker to Bun, Bun Check, and asked Claude to use it. Claude Code startup became twice as fast and it used about a third of the memory. But the code was slop. If any object accessed a property called archive, the compiler would assume the Bun.archive API might be used. I looked at that and thought we couldn't ship it. There was another problem with planning at that scale. The agent loves writing Markdown design documents, but 1,500 Markdown files isn't reasonable for anyone to read.
140 个智能体同时运行140 agents at once
在第三个版本中,我一次使用了 140 个智能体来编写代码,然后让 Claude Code 运行起来。到了第四个版本,代码质量不错,运行时性能也差不多,但编译速度实在太慢了。On the third version, I used 140 agents at once to write the code and then got Claude Code running. In the fourth version, the code quality was good and runtime performance was about the same, but compilation was far too slow.
构建时间与第五次重写Build times and the fifth rewrite
从 JavaScript 转过来的人不希望有任何编译时间。50 秒是 Claude Code 的情况,它的代码库很大,但这仍意味着其他项目也要花好几秒。没人想等那么久。旧版本的编译器需要 10 到 20 秒。Claude 往往会先评估变更,再决定是否值得实施,但当测量结果并不准确时,人们很容易说服自己它们是可靠的。我们也质疑过,既然编译器可以更小,为什么它有 90 万行代码。People coming from JavaScript don't expect any compile time. Fifty seconds was for Claude Code, which has a large codebase, but that still meant other projects would take several seconds. People don't want to wait that long. Older versions of the compiler took 10 to 20 seconds. Claude tends to size up changes to decide if they're worth doing, but it's easy to persuade yourself that your measurements are good when they aren't. We also questioned why the compiler was 900,000 lines when it could be smaller.
我们又重写了一遍。这次代码不错,构建耗时不到 20 秒。以后或许还会有第六次重写,但第五版已经可以发布了。很多工作都是从让 Claude 尝试一个想法、看看结果如何开始的。我们很容易误判一项改动,进而说服自己放弃一个值得尝试的实验,所以有些旧估算值得重新审视。We rewrote it again. This time, the code was good and the build took under 20 seconds. There might be a sixth rewrite eventually, but the fifth is ready to ship. A lot of the work came from asking Claude to try an idea and see what happened. It is easy to size up a change incorrectly and talk yourself out of a worthwhile experiment, so some of those old estimates deserve another look.
长期运行的子代理Long-lived subagents
在 Bun 运行时环境中,我过去非常依赖 Claude Code 工作流。随着新模型的出现,除了编写代码的第一阶段,我大多会使用长期运行的子代理。它们会在自己的上下文窗口中保留相关代码的上下文,因此可以持续工作,无需反复重建上下文。On the Bun runtime, I used to lean heavily on Claude Code workflows. With newer models, I mostly use long-lived subagents, except in the first phase of writing the code. They keep the context for the relevant code in their context window, so they can keep working without repeatedly rebuilding that context.
在编译器中,如果我们能证明某个值的类型是单态的,该操作就可以变成字段访问。否则,编译器每次都必须进行检查。虽然有一些内联缓存,但我们并没有大量使用。在这么大的项目里,很难跟上所有进展,所以我依赖每小时一次的更新。In the compiler, if we can prove that the type of a value is monomorphic, the operation can become a field access. Otherwise the compiler has to check each time. There is some inline caching, though we don't do much of it. On a project this size it's hard to keep up with everything, so I relied on hourly updates.
每小时评分卡与模糊测试Hourly scorecards and fuzzing
每小时发布到 Slack 的帖子会展示一份优先级指标记分卡,列出先前值和当前值,因此很容易看出数字是上升还是下降。在幻灯片中,这种前后对比的视图用处不大,因为先前版本就是 JIT。大多数测试都在项目开始前编写,这让我们有信心认为代理没有操纵这些测试。我们已经使用了一个 V8 JavaScript 引擎模糊测试器,并对其进行了扩展,使其能够覆盖 TypeScript 类型。检查测试是否真的能发现 bug 很重要。The hourly Slack post showed a scorecard of our priority metrics with previous and current values, so it was easy to tell whether numbers went up or down. That before-and-after view is less useful in the slide, where the prior version is the JIT. Most of the tests came from before the project, giving us confidence that an agent hadn't manipulated them. We already used a V8 JavaScript-engine fuzzer and extended it to cover TypeScript types. It's important to check that your tests actually find bugs.
对于展示的大多数项目,编译器的性能都优于 JIT。UglifyJS 是个例外。如果程序大量依赖解释器,例如频繁使用 `eval` 或 `new Function`,那么 AOT 并不会更快。大多数应用程序不会这样做,因此通常只需改动一行代码就能让应用程序运行得更快,不过除了 UglifyJS 之外也有其他例外。For most of the projects shown, the compiler outperforms the JIT. UglifyJS is an exception. When a program relies on the interpreter a lot, such as when it uses eval or new Function frequently, AOT isn't faster. Most applications don't do that, so it's often close to a one-line change to make an application faster, but there are exceptions beyond UglifyJS.
你一整天实际都在做什么What you actually do all day
编译器禁止对 Object.prototype 进行赋值,以防止原型污染。至于在这样一个项目中,你整天都在做什么,我发出的许多高频消息都是“继续”,部分原因是我的笔记本电脑总是没电。更具体地说,我会提出一些引导性问题:我们为什么要这么做?我们能做到吗?你能做得更快吗?怎么花了这么久?Claude 往往会说服自己编造规则,并低估自己的能力。我问这些问题,是为了让它再努力一些。The compiler bans assignments to Object.prototype as a precaution against prototype pollution. As for what you do all day on a project like this, many of my top messages said “continue”, partly because my laptop kept dying. More concretely, I ask leading questions. Why are we doing this? Can we do this? Can you do it faster? What's taking so long? Claude tends to talk itself into making up rules and can underestimate what it can do. I ask these questions to get it to try harder.
在 JavaScript 引擎基准测试之一 JetStream 3 中,Bun 在大多数测试中都胜过 JavaScriptCore 的 JIT 和 Node.js 中的 V8 JIT。在整个项目过程中,我们使用 Claude Code 构建了 Bun 的编译器,随后又使用由 Bun 本身编译的 Claude Code 构建版本。In JetStream 3, one of the main JavaScript engine benchmarks, Bun outperforms JavaScriptCore's JIT and the V8 JIT in Node.js in most of the tests. Throughout the project, we used Claude Code to build Bun's compiler and then used the build of Claude Code compiled with Bun itself.
大型智能体项目的经验教训Lessons for large agent projects
在使用 Claude 开展大型项目时,可测试性很重要。确保测试确实在测试实际内容。如果你不主动持续复测,情况可能会变糟,而你却毫无察觉。大型项目尚未发布时,重写可能比重构更容易。如果你不采取更大胆的做法,模型往往只会提出 0.1% 的改进,而不是 10% 的改进。你得推动它们再努力一些。Testability matters on a large project with Claude. Make sure the tests actually test things. If you aren't actively remeasuring, things can get worse without you noticing. When a large project hasn't shipped, it can be easier to rewrite than to refactor. If you don't act ambitiously, the models tend to offer 0.1% improvements instead of 10% improvements. You have to nudge them to try harder.
如果你已安装 Bun,现在可以运行 bun upgrade --canary,然后运行 bun compile 试试看。谢谢大家。If you have Bun installed, you can try it now by running bun upgrade --canary and then bun compile. Thanks everyone.