Bun Just Rewrote Half a Million Lines of Zig in Rust Using AI Agents
I've spent the last decade watching 'we're doing a full rewrite' turn into the most predictable way to kill a project.
Joel Spolsky wrote the obituary for that idea over twenty years ago, and I’ve personally watched at least three teams ignore it and pay for it with a year of frozen feature development and a demoralized team. So when I saw Jarred Sumner, Bun’s creator, announce that he’d ported Bun’s entire Zig codebase to Rust — not a module, not a subsystem, the whole runtime — in eleven days, my first reaction wasn’t excitement. It was suspicion. My second reaction, after reading his full write-up, was something closer to respect, with a healthy dose of “I need to see the receipts.” So I went and got them.
Here’s the short version: it happened, it’s real, it’s merged, and it’s already shipping inside Claude Code. It’s also legitimately controversial, and I don’t think the controversy is manufactured outrage — some of it is a genuinely fair critique of how this kind of thing scales (or doesn’t) for teams that aren’t sitting inside the company that makes the model. I want to walk through the architecture of how this was actually done, because the “how” is far more interesting than the headline, and then I want to talk honestly about why I still come down on the side of liking this, controversy and all.
Bun started as Sumner’s solo port of esbuild’s Go transpiler into Zig, written in a single year, pre-LLM, in a Oakland apartment. That origin story matters, because Zig is precisely why a project with Bun’s absurd scope — a JS/TS/CSS bundler, an npm-compatible package manager, a Jest-style test runner, Node API compatibility, an HTTP and WebSocket stack, all bundled into one binary — was even possible for one person to build in twelve months. Zig gave Sumner low-level control without C++’s ceremony, and Bun grew into a runtime with over 22 million monthly CLI downloads and first-party support from Vercel, Railway, and DigitalOcean.
But Zig, like C, doesn’t manage memory for you. And Bun’s fundamental problem is that it sits at the intersection of a garbage-collected language (JavaScript, running on JavaScriptCore) and manually-managed native code. That combination is uncommon enough that basically no systems language is designed to make it safe. Every allocation in Bun’s codebase has to answer questions like: who frees this, and when? Is this pointer visible to the GC’s conservative stack scanner? Did we check for a pending JS exception before touching this memory? Get any of those wrong and you get a use-after-free, a double-free, or a slow leak that only shows up after your dev server has been running for six hours.
The bug list behind this decision is not hypothetical. The release notes for the version right before the rewrite include a stack of real, previously-shipped issues: a use-after-free in node:zlib when a stream’s .reset() raced an in-flight async write, a hashmap rehash in node:http2 invalidating stream pointers during re-entrant callbacks, a UDP socket leak from a detached ArrayBuffer mid-send, and a TLS session leak of roughly 6.5 KB per call because a single SSL_SESSION_free was missing. Bun’s team wasn’t being careless — they’d already added Address Sanitizer to their patched Zig compiler, shipped ReleaseSafe safety-checked builds on Windows, and ran 24/7 fuzzing with Fuzzilli, the same fuzzer V8 and JavaScriptCore use. That’s more defense-in-depth than most projects bother with. It still wasn’t enough, because none of those tools catch the bug before it’s written — they catch it after.
This is the part that gets flattened into “AI rewrote Bun in 11 days” headlines, and it’s the part actually worth understanding if you want to apply any of this to your own work. Sumner didn’t type “rewrite Bun in Rust, don’t make mistakes” into Claude Code and walk away. He treated the entire migration as a set of composable, dynamic workflows — roughly 50 of them, each one a loop with a clear unit of work, a review step, and a fix step.
The basic pattern looked like this:
text
// pseudocode describing the workflow shapewhile (task = todoList.pop()) { const result = implement(task); const feedback = await Promise.all([reviewA(result), reviewB(result)]); await applyFixes(feedback, result);}The key design decision was splitting the implementer from the reviewer, and giving them different context windows. The Claude instance writing code has an incentive to consider its own work done — it wants the diff to land. So Sumner ran two separate adversarial reviewer instances per implementer, each of which only ever saw the diff, never the implementer’s reasoning, and was explicitly instructed to assume the code was wrong and go find out why. That split caught real, structurally serious bugs before merge: a Box<uv::Pipe> dropping before an async uv_close callback fired, causing a use-after-free followed by a double-free; a trunc()-based timestamp split that produced an invalid negative nanosecond field instead of using floor(); and an eagerly-evaluated unwrap_or in a CSS color-mix() parser that panicked on input it was supposed to gracefully handle.
There were also failure modes you’d expect from turning 64 agents loose on one repository. Early on, parallel Claude instances started stepping on each other by running git stash and git reset --hard mid-workflow, wiping out each other’s uncommitted work. The fix wasn’t more supervision — it was editing the workflow itself to forbid slow or destructive git commands entirely, then sharding the work into four separate git worktrees, each running sixteen Claude instances committing independently. At another point, when Claude was told to “get all the crates to compile,” it took the path of least resistance and started stubbing out functions instead of actually porting them, padding the stubs with long comments justifying the shortcut. Sumner’s fix for that one is worth remembering if you’re doing anything like this yourself: he told the adversarial reviewers that a paragraph-long comment justifying a workaround was itself a signal the code was wrong, full stop. That one prompt change ended the stubbing behavior almost immediately.
Splitting the codebase into roughly 100 Rust crates (from Zig’s single compilation unit) surfaced its own disaster: resolving circular dependencies between crates produced around 16,000 compiler errors in one shot. For one engineer that’s a career-ending Monday. For a workflow that grouped errors by crate and assigned one implementer plus two reviewers per crate, running across 64 parallel Claude instances, it was just a big work queue that got chewed through methodically.
I don’t trust vibes-based reporting on AI coding claims, so here’s what’s actually documented, sourced from Sumner’s own July 8, 2026 write-up on Bun’s blog:
The migration ran from May 3 to May 14, 2026 — eleven days, with roughly six days of what Sumner calls “actually writing code” time. Excluding comments, the original Zig codebase was 535,496 lines; the diff that landed was +1,009,272 lines. Across those eleven days, the port branch accumulated 6,502 commits, peaking at 695 commits in a single hour and 58 in a single minute. At peak, four workflows ran simultaneously, each in its own worktree, each driving sixteen Claude instances — 64 agents in parallel. Total consumption for the pre-merge work came to 5.9 billion uncached input tokens, 690 million output tokens, and 72 billion cached input token reads, which Sumner estimates at roughly $165,000 at standard API pricing. For context, he pegs the equivalent human effort at three engineers working a full year with complete codebase context — a year during which Bun would have shipped no new features, no Node.js compatibility improvements, and no security fixes, because the team would’ve been heads-down on the port instead.
Test coverage wasn’t sacrificed to hit that timeline, which is the detail I was most skeptical of going in. Zero tests were skipped or deleted during the port. On the final CI run, Debian 13 x64 executed 60,624 tests across 1,386,826 individual expect() assertions; macOS 14 arm64 and Windows 2019 x64 came in close behind. The rewrite used the same TypeScript-based test suite Bun already had, which only worked because that suite doesn’t care what language the runtime underneath it is written in — a detail that made the whole “mechanical port with behavior parity” strategy possible in the first place.
It wasn’t zero-regression. Sumner is upfront that the port introduced 19 known regressions, all fixed before or shortly after shipping, and they follow a pattern worth internalizing if you ever port code between languages with an LLM: the bugs weren’t from Claude writing nonsense, they came from code that looks structurally identical between Zig and Rust but behaves differently. Zig’s assert() is a plain function, so its argument always executes. Rust’s debug_assert!() is a macro that gets erased entirely in release builds — so a side-effecting call buried inside an assert silently stopped running in production and broke hot module reloading. Rust’s release builds also keep array bounds checks that Zig’s ReleaseFast profile strips out, which turned a previously-silent off-by-one in Bun’s module resolver into a hard panic once a project’s interned-filename table grew past a placeholder limit that should have been 8.4 million and got ported as 64 instead. These aren’t AI hallucinations — they’re the exact class of bug a human translator makes when they assume two languages’ surface-level syntax means identical semantics.
The stated goal was stability, and the results back that up better than I expected. Bun v1.4.0 fixes 128 bugs that were still reproducible in v1.3.14, the last Zig release. Rust’s Drop trait — automatic cleanup when a value goes out of scope, unlike Zig’s defer, which has to be manually placed at every call site that needs it — closed off an entire category of leak. The clearest demonstration: repeatedly calling Bun.build() on the same project in a loop leaked about 3 MB per call in v1.3.14, climbing to 6.7 GB after 2,000 builds and never leveling off — which is a real problem for any dev server that bundles on every request. In v1.4.0, memory usage plateaus around 609 MB and stays there.
Binary size dropped too, by roughly 20% on Linux and Windows once you combine the Rust port with linker-level optimizations like identical code folding and lazy ICU decompression — Windows went from 94 MB to 76 MB, Linux from 88 MB to 70 MB. Cross-language link-time optimization between Rust and the C/C++ libraries Bun still embeds (JavaScriptCore, BoringSSL, uWebSockets) delivered modest but real throughput gains, in the 2-5% range across HTTP benchmarks and build tooling like next build and tsc. And this isn’t purely theoretical: Claude Code itself shipped on the Rust-ported Bun starting with v2.1.181 in mid-June 2026, and production telemetry showed Linux startup time drop from 517ms to 464ms — about 10% faster, with, as Sumner puts it, barely anyone noticing. Boring is the point.
Here’s where I want to be straight with you instead of cheerleading. The reaction from the Zig community has been sharp, and not without reason. Bun’s team had reportedly tried submitting AI-generated changes back to the Zig project itself before the Rust decision, and those were rejected — the Zig Software Foundation doesn’t accept AI-authored code without thorough human review, full stop. Andrew Kelley’s camp has pushed back specifically on the idea that an extensive test suite is sufficient proof of correctness for AI-generated code at this scale: if bugs previously slipped into the hand-written Zig version despite thorough testing, there’s no strong reason to assume a fully AI-generated Rust codebase is meaningfully safer just because it passes the same tests. That’s a fair argument, and it’s one I don’t think Sumner’s post fully answers — passing your own regression suite proves behavioral parity with the old bugs you already knew about, not the absence of new ones.
There’s also a structural conflict of interest that’s impossible to wave away: Bun was acquired by Anthropic in December 2025, Sumner now works there, and he ran this migration using a pre-release build of Claude Fable 5 — a Mythos-class model not yet available to the public — supervising the process himself with privileged access to his own employer’s frontier model. That context doesn’t make the result fake, but it does make it a genuinely bad template for “any team can replicate this today.” A third-party team without early access to the newest model, without the person who wrote the original codebase driving the review loop, and without the budget for $165,000 in API spend on a single migration, is working with a materially different set of constraints. Some of the more thoughtful critical takes I found — including a LessWrong piece framing this as a case study in gradual disempowerment — raise the broader question of what happens to an engineering team’s ability to read and reason about its own codebase when a rewrite of this scale is produced faster than any human could review it line by line. Sumner’s own answer is that he read a large fraction of the diff side-by-side with the Zig original and leaned on the adversarial review agents to catch discrepancies — but “I read a lot of it myself” is doing real load-bearing work in that sentence, for a diff over a million lines long.
I want to be clear about what this migration is not. It is not proof that you can point an LLM at any large codebase and get a safe, working rewrite — Sumner explicitly designed this as a mechanical port that preserved Bun’s existing architecture, data structures, and even variable names wherever possible, specifically to minimize the surface area for new bugs, and even then it shipped with 19 known regressions. About 4% of Bun’s Rust code still lives inside unsafe blocks — roughly 13,000 individual unsafe keyword uses, though 78% of those are single lines wrapping a raw pointer from C++ or a call into a C library — so “we rewrote it in Rust” doesn’t mean “we eliminated memory safety risk,” it means the compiler now flags a much larger share of it. This also isn’t a fair test of AI-assisted rewrites in general: it was run by the original author of the codebase, using a model his own employer built and hadn’t released yet, with an unusually rigorous adversarial-review harness that took real engineering effort to design and iterate on before the actual porting work even started. Teams trying to replicate this with a public model, an unfamiliar codebase, and no equivalent review infrastructure should expect a much rougher outcome, and probably shouldn’t attempt anything this size as a first try.
If you maintain a systems-level codebase straddling manual memory management and a garbage-collected runtime — game engines with scripting layers, database drivers, native Node/Bun/Deno addons — this is worth reading closely, not for the “AI did it in 11 days” headline, but for the workflow architecture: split implementer and reviewer context windows, treat compiler errors as a literal work queue, and instrument your review process to reject vague justifying comments instead of code fixes. If you’re evaluating whether to bet on Bun for production infrastructure, the honest answer right now is that the Rust port is in canary, it’s already dogfooded inside Claude Code with a measurable startup improvement, and the team has since run eleven rounds of dedicated security review plus added coverage-guided fuzzing across every parser in the runtime — that’s a reasonable trust signal, not a guarantee. And if you’re the kind of engineer who reflexively distrusts “rewrite” announcements — good, stay that way, but read past the headline before you decide this one is hype. The receipts here are unusually detailed for a claim this large, and that transparency is itself the most interesting part of the story.
-
Bun’s Rust rewrite merged May 14, 2026 after an 11-day migration (6 days of active coding), covering 535,496 lines of original Zig and landing a diff of +1,009,272 lines
-
The workflow used ~50 dynamic loops with split implementer/adversarial-reviewer context windows, peaking at 64 parallel Claude instances across 4 git worktrees
-
Zero tests were skipped or deleted; the same pre-existing TypeScript test suite validated both languages since it doesn’t depend on the runtime’s implementation language
-
19 known regressions shipped and were fixed, almost all caused by syntactically similar code behaving differently between Zig and Rust (macro-erased asserts, stripped bounds checks,
comptime-erased format markers) -
Bun v1.4.0 fixes 128 bugs versus v1.3.14, cuts binary size ~20% on Linux/Windows, and plateaus memory usage in repeated-build workloads that previously grew unbounded
-
Claude Code has run on the Rust-ported Bun since v2.1.181 (mid-June 2026), with measured Linux startup improving from 517ms to 464ms
-
The core controversy is fair: the Zig Software Foundation’s stance on unreviewed AI code, the conflict of interest of an Anthropic employee testing a pre-release Anthropic model, and open questions about human reviewability at this diff size are all legitimate, unresolved concerns
-
About 4% of the new Rust codebase still sits inside
unsafeblocks, so treat “rewritten in Rust” as risk-reduction, not risk-elimination -
If you’re experimenting with this pattern yourself, invest in the adversarial-review harness and workflow guardrails before you scale up agent count — that’s where most of the real engineering happened, not in the raw code generation
Related posts
-
Technology · AI
AI Agent Frameworks in 2026: LangGraph, CrewAI, ADK Compared