Jarred Sumner, the creator of the widely used JavaScript runtime environment Bun, recently detailed the ambitious rewrite of a significant portion of the project from Zig to Rust. This move was initially shrouded in secrecy due to temporary export restrictions on the advanced tool used for the migration. However, the situation has been resolved, and the details of this remarkable project are now public.
Bun is a complex and critically important project that many production software solutions rely on. Its functionality includes transpiling, minifying, and bundling JavaScript, TypeScript, and CSS; it also serves as a test runner and an npm-compatible package manager. Furthermore, it provides module resolution, a WebSocket client, and Node.js module implementations. With 22 million monthly downloads, Bun is a cornerstone of many modern development ecosystems.
The main reason for the rewrite was persistent stability and memory safety issues in Zig, the language originally used for Bun. Zig is not a garbage-collected language, which led to numerous errors such as memory leaks, crashes caused by memory issues, and heap overflows. Despite efforts by the Bun team to patch the Zig compiler and implement end-to-end memory leak tests, problems continued to arise. As Jarred Sumner himself noted, managing the lifecycle of automatically managed values (from JavaScript) and manually managed values was a constant source of trouble. Every memory allocation required detailed inspection, which was tedious and error-prone.
Transitioning to a language with inherent memory safety while maintaining high performance seemed like an ideal solution. Rust meets these requirements. A large percentage of previous errors – such as use-after-free, double-free, or forgetting to free memory during error handling – are eliminated in safe Rust due to compiler checks and automatic resource management via the RAII principle and the Drop trait. These errors are caught during compilation, providing much more effective feedback than manual code reviews.
Historically, complete rewrites of software projects are considered very risky and are generally not recommended. The main problem is their immense time consumption. Estimates that start at several months often stretch into years because the original codebase continuously evolves, adding new features that then need to be implemented in the rewrite as well. A manual rewrite of Bun, which consists of over half a million lines of Zig code, would have taken a small team of engineers an entire year, according to Jarred Sumner's estimates. Such a year-long pause in development without new features for users was unacceptable to the team.
Instead of lengthy manual modifications, an idea emerged: what if an advanced artificial intelligence tool could be used for the rewrite? Jarred Sumner decided to dedicate a week to testing the capabilities of a new large language model for rewriting Bun into Rust.
The rewrite itself, of course, was not as simple as issuing one concise request. The process began with a three-hour intensive preparation where Jarred discussed with the AI model how best to map patterns from Zig code to Rust. The result of this collaboration was a detailed document titled PORTING.md, containing approximately 600 lines of migration instructions, which subsequently appeared on a popular technology forum. These guidelines specified strict rules, such as prohibiting the use of certain Rust libraries (like tokio or std::fs), preferring callbacks and state machines over async fn, and instructions for resolving borrowing check issues in Rust.
Following the preparatory phase, a trial rewrite of three files out of a total of 1,448 took place. The results were subjected to a dual 'adversarial' review, where the AI model, in separate sessions, criticized and sought errors in its own output. Once the viability of the approach was demonstrated, the work was distributed among 64 AI agents, who worked in parallel on independent files.

Planning a new website or app?
Tell me what you have in mind — I will take a look and advise on the best way forward.
Initial attempts to run parallel work encountered problems where agents interfered with each other, for instance, when working with the Git version control system. Jarred had to adjust the workflow so that agents performed only specific operations, such as committing individual files, avoiding slow or conflicting commands. After optimization, the work was divided into four worktrees, each serving 16 agents who efficiently contributed code.
Over the next two days, the AI agents rewrote all 535,496 lines of Zig code. Each commit was verified by two independent 'adversarial' reviews before merging. Upon completion of the rewrite, approximately 16,000 compilation errors appeared (after uncovering circular dependencies, the number settled at about 1,600 errors), which would pose an immense challenge for a human engineer. However, for 64 AI agents working in parallel, this was a manageable task. Errors were fixed incrementally, 'crate-by-crate' (a crate in Rust is a compilation unit), with automated cargo check runs followed by error corrections. This phase lasted approximately 12 hours and proceeded autonomously while the team slept.
After fixing the compilation errors, the phase of running local tests followed (two days), and then ensuring the extensive test suite passed in CI/CD (three days). Once all tests passed and Jarred verified correct functionality, the changes were merged into the main branch. The entire process, from planning to completion, took only 11 days.
This lightning-fast migration of approximately 550,000 lines of code, split into 6,500 commits over 11 days, came at a cost. With the AI tool's API pricing, the rewrite expenses reached 165,000 dollars. This amount, equivalent to the annual base salary of a software engineer at a mid-sized American company, while high, is considered an 'incredible bargain'. A human engineer could not achieve such milestones in 11 days, even if there were more of them. AI thus opens doors to rewrites and migrations that were previously considered infeasible.
However, the success of such a project requires several key prerequisites. Essential is a highly motivated engineer with a deep understanding of the codebase who can guide and correct the AI. Furthermore, an extremely robust test suite is crucial to reliably verify functionality after the rewrite. And, of course, one must be willing to invest significant resources into AI tokens, albeit with uncertainty about how well the entire process will work. However, with a good test suite and a committed engineer, success is highly probable. Costs can also be optimized by strategically utilizing models – for example, using more expensive and advanced models for planning, while employing cheaper variants for the actual coding and reviews. AI-assisted migrations are undoubtedly accelerating, but projects that are well-designed and thoroughly tested from the ground up, like Bun, benefit the most.
