读见

GitHub 使用 AI 辅助重写,将 Copilot 运行时迁移到 Rust

原题Github Migrates Copilot Runtime to Rust with AI-Assisted Rewrite

约 2 分钟约 460 字原文英文已翻译

读前速览

3 个要点

GitHub 成功地将 Copilot 运行时从 TypeScript 迁移到 Rust,利用 AI 重写了超过 80 万行代码,并将启动性能从 5.25 秒显著提升至 292 毫秒。

  1. 性能提升

    迁移后,启动时间显著下降,从 5.25 秒减至 292 毫秒。这一改善得益于 Rust 的高效性以及将运行时直接嵌入主机进程的能力,消除了进程间通信的开销。

  2. 增量迁移策略

    GitHub 并未采用一次性替换的“大爆炸”方式,而是选择了增量替换策略。具体做法是逐步将 TypeScript 组件换为 Rust 等效组件,并同时保留临时兼容层,从而在整个过渡期内实现持续测试和稳定发布。

  3. AI 辅助开发

    项目大量利用 AI 代理生成初始的 Rust 实现。虽然 AI 编写了大部分代码,但人类工程师仍负责关键的代码审查,识别并修正与行为、状态管理和库语义相关的问题。

完整摘要

GitHub Copilot 运行时的迁移大约在 14.5 周内完成,涉及 128 个拉取请求和 135 个发布版本。新版 Rust 实现将启动时间缩短了 10 倍,并支持直接嵌入到主机应用程序中,从而减少了内存开销。该过程采用增量替换策略,并设置临时兼容层,确保在过渡期间的稳定性。

查看原文

GitHub 已将 GitHub Copilot CLI、Copilot 应用和 Copilot SDK 背后的运行时从 TypeScript 和 Node.js 迁移至 Rust,通过 AI 辅助重写替换了超过 80 万行生产代码。此次迁移耗时约 14.5 周,通过 128 个拉取请求完成,同时 GitHub 仍在持续发布该运行时。GitHub 报告称,在一项测量中,客户端启动、会话创建和单轮交互这一场景的耗时从使用旧运行时的 5.25 秒降至将 Rust 运行时嵌入进程后的 292 毫秒。GitHub has migrated the runtime behind GitHub Copilot CLI, the Copilot app, and Copilot SDK from TypeScript and Node.js to Rust, replacing more than 800,000 lines of production code through an AI-assisted rewrite. The migration took approximately 14.5 weeks and was delivered through 128 pull requests while GitHub continued releasing the runtime. GitHub reports that a measured client startup, session creation, and single-turn scenario fell from 5.25 seconds with the previous runtime to 292 milliseconds when the Rust runtime was embedded in process.

此次迁移也改变了应用与运行时的集成方式。此前的实现依赖 Node.js 和 V8,并通过进程边界与宿主应用通信。GitHub 表示,这使每个客户端的工作集增加了约 100 MB。Rust 实现则可以通过 C ABI 直接嵌入宿主应用,同时仍提供进程外模式。Copilot SDK 目前支持 TypeScript、Python、Go、.NET、Java 和 Rust。The migration also changed how applications integrate with the runtime. The previous implementation required Node.js and V8 and communicated with host applications across a process boundary. GitHub says this added approximately 100 MB of working set per client. The Rust implementation can instead be embedded directly into host applications through a C ABI, while an out-of-process mode remains available. The Copilot SDK currently supports TypeScript, Python, Go, .NET, Java, and Rust.

GitHub Copilot 运行时架构的前后对比(来源:GitHub 博客文章)GitHub’s before and after Copilot runtime architecture (Source: GitHub Blog Post)

GitHub 采用了渐进式替换策略,而不是先并行重写、再一次性切换。各个 TypeScript 组件被逐一替换为 Rust 实现,并通过临时的 N API 互操作机制连接两者。这样一来,在其他组件仍使用 TypeScript 的同时,现有的端到端测试也能对新代码进行测试。迁移期间,GitHub 共发布了 135 个版本,其中包括 35 个稳定版和 100 个预发布版。GitHub used an incremental replacement strategy rather than a parallel rewrite followed by a single cutover. Individual TypeScript components were replaced with Rust implementations, with temporary N API interoperability connecting the two. This allowed existing end-to-end tests to exercise the new code while other components remained in TypeScript. GitHub shipped 135 releases during the migration, including 35 stable and 100 prerelease versions.

兼容层在被移除前,包含 2,019 个内部 N API 导出项和 3,356 处 TypeScript 调用点。截至 8 月 21 日,运行时包含 832,378 行生产环境 Rust 代码和 468,689 行 Rust 单元测试代码。大部分实现由 AI 智能体生成,而编译、测试和人工审查则帮助发现了涉及行为、状态与生命周期处理、库语义以及优化丢失等方面的回归问题。GitHub 记录了 4,478 次直接运行 `cargo check`,其中 87.1% 顺利完成。The compatibility layer reached 2,019 internal N API exports and 3,356 TypeScript call sites before being removed. By August 21, the runtime contained 832,378 lines of production Rust and 468,689 lines of Rust unit tests. AI agents generated most of the implementation, while compilation, testing, and human review helped identify regressions involving behavior, state and lifetime handling, library semantics, and lost optimizations. GitHub recorded 4,478 direct cargo check runs, with 87.1% completing cleanly.

社区回应还着重讨论了 AI 辅助迁移中的验证挑战,并指出取消、重试和背压都是需要在编译之外进行验证的例子。PLBjt 写道,Community responses also focused on the verification challenges of AI-assisted migrations and pointed to cancellation, retries, and backpressure as examples requiring validation beyond compilation. PLBjt wrote that

这里有意思的地方,与其说是 Copilot 写了 Rust,不如说是这次迁移是否让运行时在边界条件下保持了稳定的行为。

The interesting part here is less that Copilot wrote Rust and more whether the migration kept the runtime’s behavior stable at the boundaries.

莱昂纳多公司的 XR 技术负责人 Francesco Pira 强调了这一点。Francesco Pira, XR Tech Lead at Leonardo, highlighted

测试小而易于审查的变更、兼容层以及人类工程判断,将它们视为该方法的重要组成部分。

Tests small reviewable changes, compatibility layers, and human engineering judgment as important elements of the approach. 

微软高级解决方案工程师 Côme Redon 指出了这一点。Côme Redon, Senior Solution Engineer at Microsoft, pointed out

识别遗留系统中未文档化行为约定的挑战。

The challenge of identifying undocumented behavioral contracts in legacy systems.

GitHub 的迁移时间线,展示了逐步推进的拉取请求和版本发布。(来源:GitHub 博客文章)GitHub’s migration timeline showing the incremental pull requests and releases. (Source: GitHub Blog Post)

最终形成的架构提供了原生 Rust 运行时,既可直接嵌入宿主应用,也可独立运行;与此同时,Copilot SDK 在其支持的各种语言中保留了各自的语言专属接口。迁移过程中结合了 AI 生成的实现与渐进式集成、临时兼容边界、自动化验证和人工审核,同时生产运行时也在持续演进。The resulting architecture provides a native Rust runtime that can be embedded directly into host applications or operated separately, while the Copilot SDK maintains language-specific interfaces across its supported languages. The migration combined AI-generated implementation with incremental integration, temporary compatibility boundaries, automated validation, and human review while the production runtime continued to evolve.

值得记下

本次迁移耗时约 14.5 周,并通过 128 个拉取请求来实现,期间 GitHub 继续发布运行时的更新。