Back to feed
News Story
Simon WillisonT3
1 sources

Rewriting Bun in Rust

Original

Jarred Sumner, creator of Bun, detailed the decision to rewrite the JavaScript runtime from Zig to Rust, citing memory management challenges. The rewrite was largely automated using AI coding agents, leveraging Bun's TypeScript test suite as a conformance suite. This marks a significant shift in software engineering practices, where AI agents enable large-scale rewrites that were previously considered inadvisable.

SynthePulse Insight · AI deep reading

Rewriting Bun with AI: A Disruptive Engineering Migration from Zig to Rust

Version 1 · 1 source

In just 11 days, spending approximately $165,000 in API costs, Jarred Sumner successfully rewrote the Bun runtime from Zig to Rust. This case challenges the software engineering adage of 'never rewrite from scratch,' demonstrating how cutting-edge AI models can fundamentally change the migration paradigm for large projects.

  • Bun's Zig version accumulated a large number of crash-causing defects due to memory management issues (use-after-free, double-free, etc.), leading the team to decide on a rewrite in Rust.
  • Using a TypeScript-written test suite (millions of assertions) as a consistency baseline, an AI agent completed the migration of over a million lines of code in 11 days.
  • The rewrite cost approximately $165,000, consuming 5.9B uncached input tokens, 690M output tokens, and 72B cached input token reads.
  • The new Rust version has been integrated into Claude Code v2.1.181 (released June 17, 2026), with a 10% improvement in startup speed, imperceptible to users.
  • Key innovation: fixing the code generation pipeline rather than manually patching code, and using adversarial code review.
  • This case demonstrates that with a language-agnostic test suite, AI agents can make a 'stop-the-world rewrite' feasible.

Motivation and Challenges for the Rewrite

In his blog post, Jarred Sumner first highly praised the Zig language, stating that without Zig, Bun would not be where it is today. However, as the project evolved, the bug list grew longer—mainly memory management errors (such as use-after-free, double-free, and forgetting to free on error paths). These bugs kept Jarred up at night, worrying about Bun's crash issues. He admitted that the problem is not with Zig, but with Bun's simultaneous use of garbage collection and manual memory management, a hybrid model that no language can perfectly support.

The core reason for choosing Rust is its safe memory management: errors like use-after-free and double-free are caught by the compiler in safe Rust, and the RAII (Resource Acquisition Is Initialization) mechanism provides automatic cleanup through Drop. Additionally, Rust's ownership model can eliminate a large class of memory errors at their source.

AI Agent-Driven Rewrite Process

The key prerequisite for the rewrite was that Bun's test suite is entirely written in TypeScript and is language-agnostic. This means it can serve as a consistency test suite, automatically verifying whether the new implementation matches the original behavior. Jarred initially treated this as an experiment, using an early version of the Fable model (now part of Mythos/Fable) to attempt automatic migration from Zig to Rust. Unexpectedly, within a few days, a high proportion of tests passed, and the newly generated Rust code closely matched the structure of the original Zig code. His attitude shifted from 'worth a try' to 'I'm going to merge this.'

Over 11 days, Jarred primarily monitored the workflow, manually checked outputs, and prompted Claude in an edit loop to fix issues. The entire rewrite consumed 5.9B uncached input tokens, 690M output tokens, and 72B cached input tokens, costing an estimated $165,000 based on API pricing (thanks to being an Anthropic employee with token waivers). The result was a pull request with over +1 million lines of new code.

Merging such a massive PR required an extremely cautious strategy: using the language-agnostic test suite (millions of assertions) for automatic validation, implementing adversarial code review, and prioritizing fixing the code generation pipeline over manually patching code when issues arise. This approach ensures long-term sustainability.

Cost, Benefits, and Security Considerations

Economically, the $165,000 API cost seems high, but considering this is a rewrite from scratch of a mature runtime, traditional labor costs could be much higher. In terms of benefits, after integrating the new Rust version into Claude Code v2.1.181, startup speed on Linux improved by 10%, but it's almost imperceptible to users—which was exactly the design goal: 'boring is good.'

Security was the core driving force. Rust's safety guarantees convert many runtime errors into compile-time errors, significantly reducing the risk of crashes in production. However, Jarred also acknowledged that this rewrite relied on the 'current capabilities' of AI, and the token consumption during the pre-merge phase was enormous, so whether it remains feasible in the future requires further observation.

Implications for the Software Engineering Paradigm

In his classic 2000 article 'Things You Should Never Do, Part I,' Joel Spolsky warned never to rewrite large software. But Jarred's case shows that when AI agents can leverage language-agnostic test suites, dynamic workflows, adversarial review, and pipeline-fixing mechanisms, this old rule may be broken.

Key elements include: a high-quality language-independent test suite, sufficient token budget, systematic review of AI outputs, and the engineering mindset of 'fix the pipeline, not the code.' This provides a reusable methodology for other projects facing similar rewrite dilemmas.

However, the current success remains highly dependent on specific conditions: completeness of the test suite, model capability (here, Fable), and sufficient financial investment. Long-term stability and code maintainability will need to be tested over time.

Credibility boundary

This article is based on Jarred Sumner's official blog post and analysis by Simon Willison, with data directly cited and high reliability. However, the long-term stability and potential issues after the rewrite have not been fully verified.

Insight takeaway

The AI-assisted rewrite of Bun from Zig to Rust demonstrates that, with a language-agnostic test suite, cutting-edge AI models can complete a large-scale system rewrite in a short time at a controllable cost, challenging the traditional 'never rewrite' engineering wisdom. The success or failure of similar future practices will depend on test coverage, model capability evolution, and the degree of process automation.

Sources for this version

  1. Rewriting Bun in Rust

    Simon Willison

Primary report

Simon WillisonT3

Primary source