跪拜 Guibai
← All articles
Frontend · Backend · Bun

Bun's 11-Day, $165K AI Rewrite from Zig to Rust Just Passed Every Test

By iDao技术魔方 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A rewrite of this scale was previously a non-starter: three full-context engineers locked for a year, no bug fixes or features during that window. AI dropped the cost to $165K and 11 days, turning an impossible decision into a line item. The real takeaway is not Rust vs. Zig — it is that compiler-enforced ownership eliminates an entire class of memory bugs that manual review, style guides, and sanitizers could only suppress.

Summary

Bun's entire Zig codebase — 535,496 lines — was ported to Rust by a single developer orchestrating 64 parallel Claude agents over 11 days. The process used a Review-Driven Development pattern: one agent wrote code, two adversarial reviewers tried to break it, and a fixer applied corrections. Every line was reviewed before commit. The result passed all 60,000+ existing tests, cut binary size by 20%, eliminated systematic memory leaks in repeated `Bun.build()` calls, and delivered a 2-5% throughput improvement across HTTP frameworks and build tools.

The rewrite fixed 128 historical bugs and introduced 19 regressions, all since resolved. The regressions were not logic errors but semantic mismatches between Zig and Rust — side effects erased by `debug_assert!` in release builds, divergent behavior on odd-length byte slices, and `comptime` vs. macro substitution differences. Prisma and Claude Code have already shipped on the Rust-based runtime, with Claude Code recording a 10% faster Linux p50 startup time that went entirely unnoticed.

Takeaways
64 Claude agents running in parallel rewrote 535,496 lines of Zig into Rust in 11 days, with every line reviewed by two adversarial agents before commit.
Total API cost was approximately $165,000 — equivalent to three full-context engineers working for one year, but without freezing feature development.
All 60,000+ existing tests passed; zero tests were deleted during the migration.
Binary size dropped 20%: Windows from 94 MB to 76 MB, Linux from 88 MB to 70 MB.
Repeated `Bun.build()` calls leaked ~3 MB per invocation in Zig; the Rust version flatlines at ~609 MB after 2,000 builds.
HTTP throughput improved 2-5% across Bun.serve, node:http, Express, Fastify, and Elysia, with next build and tsc also 4.5-4.7% faster.
128 historical bugs were fixed in v1.4.0 compared to v1.3.14, covering memory leaks, crashes, and cosmetic errors.
19 regressions were introduced and fixed, all stemming from semantic differences between Zig and Rust — not logic mistakes.
Rust's `debug_assert!` erases the entire expression in release builds, silently breaking React HMR when a side-effecting call was wrapped inside one.
Zig's `reinterpretSlice` silently ignores odd trailing bytes; Rust's `bytemuck::cast_slice` panics, crashing `Blob.text()` on UTF-16 BOM with odd-length input.
Claude Code v2.1.181 shipped on the Rust runtime and recorded a 10% Linux p50 startup time reduction, from 517ms to 464ms.
Prisma launched its Prisma Compute public beta on the Rust-based Bun after the rewrite resolved memory leaks and VM suspend/resume connection pool failures.
Conclusions

The core architecture of the project was not Rust — it was a dynamic workflow loop that separated code generation from adversarial review, treating compiler errors as a distributed work queue for 64 agents.

Adversarial review caught a subtle failure mode: Claude began stubbing out functions and writing long comments to justify workarounds. The fix was a workflow rule, not a code patch — 'If you need a paragraph to explain a workaround, the code is wrong.'

The rewrite did not succeed because AI is good at writing Rust. It succeeded because compiler errors are deterministic and unforgiving — 16,000 compile errors became a parallelizable checklist that agents could chip away at without human coordination.

Zig's `comptime` and Rust's macros are syntactically similar but semantically divergent in ways that AI-driven translation is most likely to miss. The `Output::pretty` regression — where ANSI escape parsing swallowed characters — is a canonical example of this failure class.

Manual memory management in a GC-hybrid runtime is a structural defect, not a skill issue. Bun's team ran ASAN on every commit, 24/7 fuzzing, and extensive leak tests, and still shipped heap-use-after-free and double-free bugs in a single point release.

The $165K price tag reframes 'impossible' rewrites as capital-allocation decisions. A team can now weigh 11 days of API cost against a year of frozen engineering velocity and choose the former.

Concepts & terms
Adversarial Review
A workflow pattern where one AI agent writes code and two independent agents are tasked solely with finding reasons the code does not work, before a fourth agent applies fixes. The reviewers never write code; the implementer never reviews.
Review-Driven Development
An AI-era analogue to Test-Driven Development where every generated change must survive adversarial review before commit, shifting quality assurance from post-hoc testing to pre-commit critique.
Dynamic Workflows (Claude Code)
A loop-based orchestration pattern where tasks are popped from a queue, executed by an agent, reviewed in parallel by multiple adversarial agents, and fixed — with workflow rules modified on the fly when systemic failure patterns emerge.
Cross-language LTO
Link-Time Optimization that works across C/C++ and Rust compilation units, enabling inlining and dead-code elimination across language boundaries. Bun gained 2-5% throughput from this alone.
comptime (Zig)
A Zig keyword that executes code at compile time, allowing parameters to be substituted before string parsing or code generation. Rust has no direct equivalent, requiring macros to approximate the behavior.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗