跳到正文
原文
GitHub Blog · AI & ML· Stephen Toub·· 2026-09-17精选AI 评分85

GitHub 借助 Copilot 将 Copilot agent runtime 重写为超过 800,000 行 Rust

Migrating the GitHub Copilot runtime to Rust, using Copilot

AI 导读

GitHub 用 Copilot 将 Copilot agent runtime 从 TypeScript/Node.js 重写为超过 800,000 行生产 Rust,大部分代码由 AI 智能体完成,并通过 128 个合入主干的 pull request 增量交付。

推荐理由

原文写清 Copilot 运行时如何以组件原子替换迁到 Rust 并增量发布,读者可从智能体分工看到大规模改写怎样维持主干持续可交付。

正文 · AI 翻译

译文尚不完整,完整内容请切换到原文。

GitHub Copilot CLI、 GitHub Copilot app 以及 GitHub Copilot SDK 均由 Copilot agent runtime 提供支持。这是一套可嵌入应用程序和服务的智能体框架。它最初使用 TypeScript,基于 Node.js 和 V8 JavaScript 引擎编写,服务于如今的 GitHub Copilot cloud agent (CCA);随着运行时及其能力迅速增长,该运行时一直停留在这一技术栈上。

这一情况如今已经改变。我们使用 GitHub Copilot app 和 Copilot CLI,将该运行时完全重写为超过 800,000 行的生产级 Rust。AI 智能体编写了大部分代码,共计 128 个拉取请求合入 main,并采用增量方式发布,而不是等到最后一次性切换。少数不可避免的回归问题在此过程中被迅速发现并修复,同时运行时的性能提升了数个数量级。在智能体出现之前需要整个开发团队花费一到两年才能完成的项目,如今主要由一名开发者在短短几个月内完成;与此同时,团队的其他成员继续大幅扩展该运行时的能力与覆盖范围。

我们为何需要移植

Copilot agent runtime 并不只是 Copilot CLI 背后的引擎。它支撑着越来越多的 Microsoft、GitHub 和生态系统解决方案;对其中每一项而言,从架构上看,AI 支持都是围绕同一运行时的一层外壳,再加上该解决方案所需的各种定制。这不仅包括 GitHub Copilot CLI 和 GitHub Copilot app,还包括 VS Code、Visual Studio、CCA、Copilot Code Review(CCR)、Copilot Cowork、Copilot Studio 的最新版本,以及 Excel、Outlook、PowerPoint 和 Word,等等,还在继续。

这些产品彼此差异很大,没有一个想要、也不应该需要去实现生产级智能体框架中的全部内容。它们想要全部的智能、安全性、可靠性和性能,并且希望这些能力被共享,从而使一处修复能在所有产品中生效。上一段列出的大多数产品最初都实现了自己的智能体循环,但此后已将其替换为 GitHub Copilot SDK,而 GitHub Copilot SDK 是进入 Copilot agent runtime 的入口。这样做使它们能够专注于自身的核心业务价值,把细节留给运行时。鉴于行业的发展速度,以及所采用的智能体循环需要在激烈竞争中始终保持同类最佳,这一点就更加重要。

所以,共享运行时,很好。问题在于被共享之物的性质。

如果我们看 CLI,从逻辑上讲,它是位于智能体循环之上的终端 UI(TUI)。实际情况是,整个技术栈都用 TypeScript 实现,以 Node.js 为框架、以 V8 为执行引擎,并用 Ink 和 React 构建 UI。对于 TUI 应用而言,这是一个不错的选择;TypeScript 和 Node.js 易于广泛使用,并能支持非常快速的应用开发。就控制台应用的需求而言,在启动、响应速度、吞吐量和内存消耗方面的性能影响也还算合理。不幸的是,如果考虑到该实现会被用于其他环境、面临其他约束,并且要求诸如快速启动,以及因内存开销低而带来的出色服务器密度,这些影响就远没有那么合理了。

CLI 及其运行时的架构也在这里带来了挑战。整个行业都在极速前进,在这种背景下,非常聪明的人会为了交付速度和市场覆盖做出决策。Copilot CLI 最初被快速编写并发布,在此过程中,TUI 与运行时相当紧密地交织在一起,而不是被分离成相互独立的层次。随后,当需要一个 SDK 来以编程方式访问该运行时,而各层又没有清晰分离时,人们做出了一个务实的决定:把 SDK 叠在 CLI 之上,尽管从逻辑上你会期望相反的架构。CLI 不再只能通过用户在命令行提供的命令来访问,而是更新增加了一种可以无头运行的模式,从 stdin 读取类似的命令,并把响应写入 stdout。随后可以用 JSON-RPC 协议,在外部进程与 CLI 之间封送函数调用。SDK 随后可以嵌入任意消费程序,这些程序会派生一个 CLI 进程,在进程外托管 agent 循环,SDK 则通过这一 JSON-RPC 机制调用远程进程中的函数。巧妙。能快速推出。灵活。但对那些消费应用的性能(启动、内存、吞吐量)和可靠性并不理想。从 SDK 创建一个新的 CopilotClient 意味着再派生一个进程:

const client = new CopilotClient();
await client.start(); // spawns the CLI as a subprocess
const session = await client.createSession({
    /* ... */
});

该进程需要启动并托管 Node 和 V8。这意味着要解析 CLI 中由 TypeScript 代码生成的大量 JavaScript,为其生成字节码,并可能在后续 JIT 层级中优化热点代码。这意味着要承担与 V8 相关的全部内存开销。这意味着要继承 Node 的线程模型,而该模型默认会把我们推向一种所有 CPU 密集型工作都被串行化的模式。而且这意味着仅仅为了进行函数调用,就被迫进行进程外通信。这意味着每一种语言的每一个 SDK 消费方都要附带 Node.js,或一个包含 V8 的捆绑二进制文件。这意味着 C#、Python、Go、Java 和 Rust SDK 都要为每个客户端承担一整套第二语言运行时的代价,工作集至少大约为 100 MB,而这个运行时是其应用程序除此之外根本用不上的。这意味着每一个事件、每一条消息,以及每一次抽象的会话文件系统读写,都被推过进程边界。这意味着 Node 中的一次崩溃会把会话一并带走。而且这意味着任何部署它的人,至少都有两个进程需要监管、监控和调试。

相反,我们想要的是这样一个运行时:

  • 它不包含 TUI;它本身是一个独立的库,TUI 以及其他应用程序和服务可以干净、恰当地分层叠放在它之上。
  • 用一种依赖极少、开销极小的语言实现。
  • 以一种可以干净地嵌入进程内、而不是被迫放到进程外的方式实现。
  • 用一种在性能、可扩展性和可靠性方面具备顶尖特性的语言实现。
  • 用一种非常适合互操作的语言实现,从而可以借助该技术栈的外部函数接口(FFI)机制,被全部六个 Copilot SDK 语言版本(C#、TypeScript、Python、Rust、Go、Java)干净地使用。
  • 借助一套能提供更现代安全态势的工具链来实现,供应链风险更低,并对构造即正确的代码有更强支持。

出于上述所有原因,以及一些较软性的原因(例如团队经验和行业方向),我们选择了 Rust。这绝不是在主张每一个大型 TypeScript 程序都应该变成 Rust。我们的需求强调通过 C ABI 进行嵌入、较低的启动与稳态开销,以及可预测的资源使用。Rust 使这些目标成为可能,代价是其他方面的复杂性,例如我们必须显式地表示生命周期和共享状态(后文讨论的生命周期回归凸显了这一点的影响)。合适的目标语言确实会因应用而异。

随后开展了两项关键的相关工作:

  1. 将 TUI 专用代码与运行时分离,使前者严格分层于后者之上,更具体地说,严格分层于 SDK 的公共接口面之上。如今,CLI 在若干地方仍直接调用运行时内部实现;将其完全迁移到 SDK 的接口面之上仍是进行中的工作。
  2. 将该运行时层移植为 100% Rust,从而得到一个纯原生二进制文件:它暴露 C ABI,供所有语言前端进行进程内使用;并在仍需要进程外方式时,提供基于 stdin/stdout 或基于套接字的服务器。

本文主要介绍第二项:将运行时移植到 Rust。

The Copilot runtime architecture before and after the Rust rewrite, showing the old SDK-to-CLI process boundary and the new in-process and out-of-process hosting paths.

之前的样子

2026 年 5 月初的初始移植计划估计运行时大约有 130,000 行 TypeScript。就范围界定而言,这一初始测量相当准确,但事实证明,它在两个关键方面也极具误导性。在移植的同时:

  1. 仍包裹在 TUI 层中的部分正在被下推到运行时层。最初在估算中被忽略的整个组件以及相当比例的代码,后来被认为与移植相关。
  2. 不断有贡献大量新 TypeScript 的拉取请求,持续推高仓库中的 TypeScript 总量。数十名借助智能体协助的开发者每周合并数百个拉取请求。

综合考虑所有因素后,我估计大约有 430,000 行生产环境 TypeScript 最终经过了这次移植。这些因素也使得沿途很难看清进展:直到接近尾声,生产环境 TypeScript 的体量看起来仍相对稳定,甚至略有增加,因为移植的速度与新进入的工作量基本持平。

这一点更令人困惑,因为在同一时间段内,还有与这次移植无关的新进入的 Rust 代码;移植工作早期,新进入的代码更可能以 TypeScript 为主,而到了后期,则更可能以 Rust 为主。

TypeScript fell to zero while production Rust rose to approximately 830,000 lines and Rust unit tests to approximately 469,000 lines.

在移植期间,运行时纳入了约 300,000 行生产 TypeScript,同时移除了约 430,000 行,而约 1,200,000 行生产 Rust 进入、约 365,000 行离开。换句话说,上图中 TypeScript 曲线表面上的稳定,实际上掩盖了大量的 TypeScript 代码更替。

原地移植策略

这张图还凸显了这次移植方式的一个重要方面:原地进行。

这种规模的重写主要有两种做法:

  1. 大爆炸。 新的 Rust 运行时被开发为一个完整的替代方案,然后在准备就绪时一次性全部换入。这种大爆炸式切换有两种变体。a. 停止世界。 在重写进行期间,所有人停止在 main 分支上的其他工作,重写则在 main中完成。b. 并行开发。 重写在功能分支中进行,同时主分支上的工作继续,重写不断努力跟上并合并来自主分支的变更。
  2. 就地。 这是以逐组件移植的方式完成的,运行时被增量地一块一块重写。这种就地方法也有两种变体。a. 原子替换。 每一块都从 TypeScript 原子地切换到 Rust,剩余 TypeScript 与新 Rust 之间的互操作提供连续性。随着时间推移,生产运行时中的 TypeScript 越来越少,Rust 越来越多,直到有一天不再有 TypeScript,只剩下 Rust。b. A/B。 不是在组件被移植后就将其删除,而是将 TypeScript 和 Rust 组件都作为可热切换的选项加以维护,一旦信心趋于平稳,再删除 TypeScript。

出于多种原因,我们选择了方案 2a:

  • 没有人会经历工作停顿。 主分支继续保持活跃。每位不直接参与移植的开发者都可以继续照常工作,只有当他们可能已长时间处于进行中的拉取请求恰好触及同时被移植的代码时才会受到影响;在这种情况下,他们需要变基,并让自己的智能体帮忙仅移植其进行中的变更。
  • 运行时的主分支始终可发布。 每个拉取请求都用一个调用 Rust 的薄垫片替换现有的 TypeScript 实现,并在一次原子变更中删除旧代码。新代码会立即就地得到执行。
  • 重写是增量的且可审查的。 每个拉取请求移植单个组件或切片,因此变更范围更小,diff 也更容易审查,无论是由人、由智能体,还是由两者一起审查。
  • 大多数移植都相当小且自成一体,从而尽量减少与并发拉取请求产生的漂移。在某些情况下,如果 TypeScript 组件过大,可以先将它们重构为更容易移植的组件。
  • 所有现有的端到端测试,涵盖 CLI 和 SDK,在每一步都针对新的 Rust 代码运行,为我们带来信心和大量验证。如果某个拉取请求导致必需测试失败,它就不会合入。

我们也放弃了同时维护同一组件多个版本的方案 2b 变体。过去几个月里,每周有数百个 pull request 合并进该仓库,代码库在持续且快速地演进。用两种不同的语言维护同一代码的两个版本,并使用两套不同的依赖库,会增加大量复杂性。这些组件也并非都完美隔离;有些在逻辑上是独立的,系统其余部分通过简单的 API 访问它们,而另一些则有大量触角,要把这张图做成按组件热切换简直是噩梦。最可能从谨慎的并行切换中受益最大的子系统,恰恰是并行最难的那些。例如,会话编排不是一个纯函数,不能根据某个实验性标志上的 if/else 来调用它的两个不同版本。它拥有可变状态,双向驱动回调,并贯穿几乎每一个其他子系统,因此“两边都运行再比较”将意味着维护持有对话状态与服务的该组件的两份分叉副本,并祈祷它们在数百次并发编辑中保持同步。使组件难以移植的那种耦合,同样使它几乎不可能做影子运行,否则就有引入比它所避免的更多回归的风险。能够以这种方式切换的好处主要在于建立信心,而这我们可以通过其他方式做到。

验证同样通过增量发布进行。若采用大爆炸式切换,我们会把一切放在一个长期分支中,移植整个运行时,然后一次性切换。这意味着使用方会一次性遇到每一行已移植的代码,包括所有在仓库内测试中漏网的回归。将变更分部分增量推出——这里两个组件,那里一个组件——使我们能够在已部署的构建中、借助真实使用方的使用(最常见的是 Microsoft 和 GitHub 内部的第一方使用)完成最后一公里的验证,同时将回归风险保持在最低。在大约十四周半的移植窗口内, main 发布了 135 个版本,包括 100 个预发布版本和 35 个稳定版本,平均每天约 1.3 个版本。移植 pull request 每天大约也新开 1.3 个,因此每个版本只携带一小批可知的已移植组件(我们通常尽量先在预发布版本中发布这些移植,但并非总能成功)。在最近七天的 npm 样本中,预发布版本仅占下载量的 10.5%,表明初期暴露相对有限;与此同时,我们监控反馈渠道中出现故障的信号,并在下一个预发布版本中迅速完成修复。上报的问题更容易与已知的近期变更相关联,也更容易定位根因并快速修复。就这样,在更长时间内增量完成移植,实际上是一项特性而非阻碍(即更快并不总是更好)。到 8 月 21 日,运行时已是 100% 的生产 Rust:832,378 行生产 Rust 和 468,689 行 Rust 单元测试,外加 174,675 行 E2E TypeScript 测试。独立的 GitHub Copilot SDK 仓库又增加了 ~130,000 行 E2E 测试代码,覆盖 Node.js、Python、Go、C#、Rust 和 Java。

Timeline from May 12 through August 21 showing 128 port pull request merges sized by changed lines and 135 public CLI releases; the largest port changes cluster near completion of the port.

开始动手

在全面投入之前,我们先建立了信心,并把方案验证清楚。我们从两个 pull request 开始,它们确立了 Rust workspace、工具链、lint 规则、CI、构建流水线以及编码说明,随后引入 runtime crate,以及代码生成与互操作模式,同时移植了一批纯逻辑原语——特意选择它们,是因为它们没有 I/O 或共享状态,并且已经有扎实的测试。只有在这些落地之后,第一个主要移植 pull request 才让三个无副作用的辅助函数走完整个流程。它们充当了交付试点,把关于仓库布局、FFI、打包、测试和评审的假设,变成后续规模大得多的移植将会复用的约定。基本上,我们把这套机制端到端地测试了一遍。计划继续按从叶子向内的顺序安排工作:纯辅助函数、内容排除、shell 工具以及会话文件系统操作确立了翻译与测试模式。有状态子系统紧随其后,tools、hooks、模型客户端和 MCP 则构建在这些部件之上。会话编排(运行时中耦合度最高、也最不天然适合并行的部分)会安排在接近末尾。

Timeline of 128 landed port pull requests from May through August, progressing from small foundational components to larger orchestration and session work.
时期拉取请求变更行数中位数
May 1–1583,250
May 16–3129,421
Jun 1–15405,073
Jun 16–30318,253
Jul 1–15109,514
Jul 16–311428,159
Aug 1–151913,861
Aug 16–30499,445

早期移植的是小型叶子组件,进展很快。但更大的子系统并不是以一个原子步骤完成的;例如,MCP 支持通过七个专门的 pull request 推进,而 tools 则经由一个六部分系列推进,之后还需要额外工作来迁移编排并退役剩余的 TypeScript。Hooks、auth、telemetry、plugins、settings 和 persistence 也走了类似的路径。

实际上,移植的有效单位并不总是“一个组件”。它往往是穿越相关行为区域的一轮推进:先迁移纯逻辑,再迁移状态所有权,然后迁移编排,接着移除回退方案,最后在临时互操作消失之后简化 Rust。

互操作

这次移植中涉及互操作的主要有两层:

  1. 临时的内部互操作。 每当一个函数被移植到 Rust,该函数就需要能够被任何调用初始 TypeScript 函数的 TypeScript 所调用。同样,我们需要让 Rust 函数能够调用 TypeScript 回调。这种互操作需求是实现细节,而且极其易变。随着 Rust 内部接口面的增长,所需的 TypeScript shim 数量也会增长,因为它们与需要从 TypeScript 调用的 Rust 方法是 1:1 的。当这些调用方被移植到 Rust 后,现有的那一层 shim 会被删除,并放上一层新的。最终我们到达运行时库的公共入口,这些 shim 便会消失。
  2. SDK 表层。 所有 SDK 库都需要能够位于运行时之上并暴露其功能。在移植之前,这是通过双向 JSON-RPC 层暴露运行时来实现的:SDK 将函数调用请求作为 JSON-RPC 方法调用载荷发送,运行时解析请求并调用相关 API,再经由同一传输通道把结果发回,供 SDK 解析并返回。反向同样存在;运行时需要能够回调 SDK 客户端,例如用于钩子通知和权限请求,这些在 SDK 客户端中以回调形式呈现,使用该语言中被视为惯用的语言特性(例如 C# 中的委托)。

我们借助 napi-rs 项目的 napi Rust crate 实现了(1),该项目用于用 Rust 构建 Node 原生插件。你用 #[napi] 标注一个函数,napi-rs 宏便会生成 N-API 注册胶水代码,使该函数可从 JavaScript 调用,并在生成的 index.d.ts 中为其生成 TypeScript 声明。同步的 Rust 函数会变成普通的 JavaScript 函数, async fn 会变成返回 promise 的 JavaScript 函数,而标注了 #[napi(object)] 的结构体在另一侧会变成普通对象。

流量也必须双向流动。许多已移植的组件暂时依赖于尚未移植的部分,因此 Rust 需要回调到 TypeScript,例如 Rust 中的工具实现向仍为 TypeScript 的模型层请求推理、触发钩子,或为其想要运行的命令请求权限决策。napi-rs 通过“threadsafe functions”来处理这一点,它让运行在 Tokio 工作线程上的 Rust 代码能够回到 Node 的主线程调用 JavaScript 回调。Node 安装一次该回调,Rust 持有它,并在需要走反向时调用它。这些回调就其构造而言全都是临时的:回调之所以存在,只是因为另一端仍是 TypeScript,而当那部分被移植后,它就会被删除。

Temporary Rust N-API exports and their TypeScript call sites grew during the incremental port, then declined as callers moved to Rust and the temporary interop surface disappeared.

这条临时接缝在 8 月 3 日达到峰值,共有 2,019 个内部 N-API 导出和 3,356 处 TypeScript 调用点。完成时,运行时已完全是 Rust,因此不再有内部互操作:剩余的临时内部 N-API 导出为 0,TypeScript 调用点为 0。(我之前提到,CLI 仍有一些我们正在努力移除的对运行时的内部访问;那些导出不计入此处。)

第二层互操作层,即 SDK 表层,是两者中永久保留的那一层。Copilot SDK 面向六种语言发布:TypeScript、Python、Go、C#、Java 和 Rust。它们都使用同一套双向 JSON-RPC 契约,最初也都以相同方式接入:以无头模式将 Copilot CLI 作为子进程启动,并通过管道或套接字与之通信。在移植期间,这仍是默认方式。这也意味着任何语言的 SDK 使用者都要附带或定位一套完整的 Node 实现,在每个事件和每条消息上都付出一次进程跳转的代价,并且要监管两个进程而不是一个。

把运行时移植到 Rust,才让另一个选项变得可行。随附的 runtime.node 是一个普通的平台共享库( .node 扩展名是 Node.js 原生插件约定;其底层是 .dll、 .so 或 .dylib),而它现在为同一引擎呈现两扇前门。一扇是 napi 门,Node 进程将其作为原生插件加载;那是 CLI 的路径(今天…未来的意图是它将完全走 SDK 路径)。还有一扇 C ABI 门,任何语言都可以将其加载到自己的进程中,并通过 FFI 调用。同一个进程内运行时通过每种语言的原生互操作机制来选择:

SDK原生桥接进程内客户端选择
C#P/Invokenew CopilotClient(new CopilotClientOptions { Connection = RuntimeConnection.ForInProcess() })
Gopuregocopilot.NewClient(&copilot.ClientOptions{Connection: copilot.InProcessConnection{}})
JavaJNAnew CopilotClient(new CopilotClientOptions().setConnection(RuntimeConnection.forInProcess()))
PythoncffiCopilotClient(connection=RuntimeConnection.for_inprocess())
RustlibloadingClient::start(ClientOptions::new().with_transport(Transport::InProcess)).await?
TypeScriptkoffinew CopilotClient({ connection: RuntimeConnection.forInProcess() })

Rust 重写,以及在进程内托管与进程外托管之间做选择,是两个彼此独立的维度。已完成的 Rust 运行时两者都支持:它可以运行在 SDK 消费方的进程内部,也可以位于现有 JSON-RPC 服务器边界之后。这些进程内入口点目前仍是可选启用,以便我们在与消费方应用共享一个进程、因而也共享一个故障边界这件事上建立信心。传输层之上的一切仍是同一套 SDK API:会话、事件、工具、权限和回调并不在意它们的 JSON-RPC 字节穿过的是管道还是一次函数调用。

第二扇门有趣之处在于它的体量。它只有 19 个导出函数:四个用于服务器生命周期,四个用于会话注册与配置,八个用于连接,三个用于嵌入式宿主。在这些函数背后,共享契约目前包含 364 条分发路由:340 条可由 SDK 消费方调用,24 条则反向运行,作为运行时到 SDK 的回调。napi 门要大得多,需要为其中每一条分发路由都提供函数。C ABI 门基于分发:API 方法根本不会获得导出,而是作为写入连接的 JSON-RPC 字节来传递,结果、事件以及服务器到客户端的请求则通过宿主提供的回调返回。添加、更改或移除一个 API 方法会触及引擎的分发表,但绝不会触及 ABI。SDK 只需一次性绑定这 19 个入口点,就能通过它们动态触及整个仍在增长的 API 表面。

这就引出了一个显而易见的问题:为什么在一次不再跨越进程边界的调用中仍然有 JSON-RPC?

答案是,这让进程内托管成了直接替换,而不是一次重写。每个 SDK 早已有一个可用的 JSON-RPC 客户端,具备分帧、请求与响应的关联,以及面向服务器到客户端方向的处理程序。把 FFI 作为该客户端之下的又一种传输挂载上去,会把字节路径从管道或套接字移到函数调用,并让其上的一切保持不动。六个 SDK 以附加的、可选启用的传输获得了进程内托管,现有的传输则保持不变。倘若我们改为为每个 API 方法定义一个带类型的 C 函数,每个 SDK 就都需要第二层绑定,每个新的 API 方法都要再增加六个绑定,而 ABI 也会变成我们必须进行版本管理的二进制兼容表面。

对于确实处于远程的运行时——无论是跨越子进程边界还是经由 TCP——我们仍然需要 JSON-RPC。在进程内沿用同一套协议,意味着只需维护一套双向 API 与分发系统,而不是远程连接使用 JSON-RPC、本地连接再另做一套按方法划分的 FFI 接口。这是一项实实在在的权衡,而不是不假思索就能做出的决定。我们避免了进程间跳转,但每次调用仍要承担 JSON-RPC 开销。对于以推理为主的工作负载,这种序列化开销与模型往返相比通常微不足道。在高吞吐的本地工作负载中它仍然可测,但就目前而言,还不足以证明有必要在六个 SDK 绑定中把数百个方法各复制一份。而且,一旦出现性能方面的需要,这个决定日后也很容易修订。载荷编码是两端的私有细节,把 JSON 换成 MessagePack 这类更紧凑的格式,不会改变任何一个已声明的导出。日后也可以为热路径增加带类型的按方法导出,调用同一引擎和同一批处理程序,而不必替换字节通道;字节通道仍将作为流式传输、服务端到客户端请求,以及那些单独做导出毫无收益的、很少被调用的方法长尾的底层基础。

会话数据说明了什么

本文几乎每一个数字都来自两个来源之一。第一个来源是私有 github/copilot-agent-runtime 仓库的 GitHub 历史:拉取请求及其 diff、评审评论、CI 运行等。第二个来源是代理会话日志。运行时(因而也包括 CLI、应用等)会为它运行的每个会话写入结构化事件日志:每行一个 JSON 对象,随会话进行而追加。这些日志可能包含提示、命令、命令输出、文件路径,以及工具可能暴露的机密,因此必须按敏感数据处理。日志位于会话所在机器的本地;远程会话功能在启用时也可以上传该日志,但须遵守产品设置与组织策略。

以下是所有相关移植拉取请求中数据的汇总:

指标数量
事件12,760,995
用户消息31,247
助手消息1,385,214
Hook 开始与结束事件6,438,562
工具启动1,857,409
编译命令23,096
测试命令19,485
Rebase 命令2,496
提交命令7,410
推送命令5,554
已完成的压缩5,116

这 31,247 条用户角色消息并不是我亲自输入的 31,247 条提示;其中还包括技能说明、自动合并心跳、跨会话消息以及子代理流量,而我亲手输入或说出的大约只有 2,600 条,约十二分之一。同样,1,385,214 条助手消息也包括子代理和面向工具的消息,而不仅仅是对话界面中展示给我的文本。该语料包含 68 种不同的事件类型和 67 个不同的工具名称;1,130,921 次工具调用(占 61%)来自子代理,而非主会话线程。

这个数量并不能说明我为何大约插入了 ~2,600 次。为此,我让 Copilot 为会话日志语料中每一条人工撰写的消息指定一个主要意图。

Of 2,639 human-authored messages, 31.0% focused on review, testing, and CI; 17.4% challenged technical or design decisions; and 15.0% pushed for completeness.

前三个类别占我交互的 63%。只有 ~40 次是我可识别的会话启动,因为这在很大程度上是我先创建一个聊天来探索下一个方向,然后让该聊天会话为每个期望的切片创建实际的移植会话。我的角色与其说是“分配任务然后等待”,不如说是“操作控制回路”:检查结果、质疑技术决策、执行质量门禁,并在智能体把中间停止点当作终点时继续推动。尽管智能体在做“具体工作”,人类判断仍然非常深入地参与其中。我的参与只是上移了……我不再负责编写语法,而是负责框定问题、界定边界、选择策略、裁决例外,并总体上确保一切都朝着好的方向推进。

一切都关乎缓存

LLM 提供商通常对输入 token(你发送给他们的内容)收取一种费率,对输出 token(他们发送给你的内容)收取另一种费率。计费通常按 token 进行,因为它们是完成推理所需计算工作量的有用近似:对于每个输入 token,模型必须读取它,将其纳入内部表示,并把它用作决定下一个 token 的计算的一部分。不过,提供商通常支持缓存这些计算的结果,因此如果某个提示的相同前缀已经被处理过,提供商就可以复用缓存中的中间计算结果,而不是从头重新计算。这降低了处理这些 token 的成本,而节省下来的费用可以传递给消费者。因此,输入 token 的宣传价格往往包含多种费率,其中包括从缓存中读取的输入 token 的费率。

折扣非常大!提供商对缓存命中的计费常常给予 90% 的折扣,例如某提供商可能对 100 万个输入 token 收费 $2.00,但对 100 万个缓存输入读取 token 只收 $0.20。换句话说,你真的、真的希望维持良好的提示缓存,这样你的账单就会小一个数量级。

移植工作的数据显示,我们在这方面做得很好。提示缓存命中率为 96.22%:缓存读取量除以全部输入侧 token 量(缓存读取加上缓存写入再加上全新输入)。缓存写入为 3.07%,全新输入为 0.71%。这并非偶然。GitHub Copilot 专门塑造智能体循环,以保留一个长而稳定的前缀(系统提示,然后是工具定义,然后是累积的对话),因此每一轮都是向模型已经处理过的上下文追加内容。上下文中昂贵的部分只需付费一次,然后在每一次后续调用中以低一个数量级的金钱成本重新读取。这也是长时间自主会话的经济性之所以能够成立的原因。一次三百小时的移植,如果在数万次调用中每一次都从头重新读取不断增长的全部上下文,其成本将与我们实际看到的不在同一个数量级。Agent harness 开发者花费大量精力,尽量避免破坏提示缓存,而模型厂商也经常推出新功能来帮助他们做到这一点。

Prompt-cache composition across the porting sessions: 96.22% cache reads, 3.07% cache writes, and 0.71% fresh input.

压缩讲述了一个互补的故事。在所有移植会话中,GitHub Copilot 自动压缩上下文 5,116 次(即会话填满其上下文窗口并自我总结以便继续进行的那些时刻)。单个 sessions-infrastructure 移植拉取请求在其长达多日的生命周期中压缩了 647 次,而一个小型移植一次也没有压缩。持续数百小时的自主工作之所以成为可能,只是因为智能体可以一遍又一遍地回收其工作记忆而不丢失脉络。那数千次总结中的每一次,都是有损交接可能悄然使移植偏离正轨的节点,而大多数时候并没有。Copilot 的子智能体也在很大程度上有助于尽量减少压缩。每个子智能体都有自己的上下文,因此父会话实际上可以提出一个问题,让子智能体自行离开并在计算答案时耗费相当多的上下文,然后只把答案汇报给父会话。父会话的上下文不必受到所有这些中间信息的影响。

上文的“大多数时候并没有”在会话日志中可见。我让 Copilot 在两侧都至少存在 20 次工具调用时,将每次成功的压缩与其周围的工作配对。这产生了 ~4,000 个可比较的窗口。智能体在压缩前 20 次工具调用中所做之事的构成,在规模上看起来与压缩后所做之事相似(探索:之前 46.5%、之后 48.1%;修改:之前 8.4%、之后 6.0%;验证:之前 4.7%、之后 4.0%;失败:之前 1.0%、之后 1.5%)。如果压缩经常丢掉思路,我们会预期之后一侧明显以重新定向为主,阅读激增、编辑骤降,同时智能体重新弄清自己身处何处以及该做什么。相反,朝这个方向只有轻微的偏移。

是的,静态分析有帮助

有一种流行的说法认为,Rust 是 AI 生成代码异常合适的目标,因为 Rust 严格的编译器能抓住模型出错的地方。会话日志让我们得以检验这一理论,至少对于看起来像这次移植工作的任务而言。

直接的验证命令结果捕获了 8,678 次 rustc的错误代码。四个最大的诊断类别覆盖了 84%:

  • 37%:名称与导入解析,以 E0425 (“cannot find value in this scope”)为主
  • 22%:缺失的方法或字段
  • 14%:类型不匹配
  • 11%:未满足的 trait 约束

其中每一项都是寻常的接线问题:名称输出略有错误、签名没有对齐、字段已被重命名、某个抽象尚未实现。这些正是批量翻译很容易在无意中产生的错误类型,也恰恰是编译器能非常迅速抓住的那些。

但请注意这份清单里缺了什么:任何真正专属于 Rust 的东西。这四个类别中的每一个都是静态类型的基本功,C#、Java 或 Go 编译器同样能把它们全部抓住,其中好几项的诊断信息还更友好,而且全部都快得多。如果这就是把智能体指向 Rust 的论据,那它其实是把它们指向任何静态类型语言的论据。强类型编译器和/或一门具备出色静态分析与 lint 的语言,确实很适合这项工作,智能体把它用作快速反馈回路。在更严格的结果匹配器捕获到结果的 4,478 次直接 cargo check 运行中,87.1% 返回干净,这正是小步增量编辑并不断重新编译所能得到的。

相比之下,所有权、借用和生命周期错误合计只占经编码诊断的 1.7%。借用检查器——那个主导每一次关于 Rust 很难的讨论的东西——只是安静的背景存在。编译器几乎把全部报错精力都花在了乏味的机械性错误上。

智能体喜欢阅读

我们还可以检视会话事件语料中的工具调用数据,并从中提炼出一些关于智能体如何花费时间的有趣观察。

工具调用次数中位数测得小时数
powershell630,4233 s2,833.9
view590,9880 s621.7
rg281,7831 s408.4
grep126,4831 s115.3
apply_patch53,7150 s17.0
edit40,5911 s24.1
read_powershell36,72890 s1,203.9
task13,080274 s2,329.0

我在这里的第一个结论是,智能体花在收集证据上的时间远多于修改代码。对照所展示的文件读取与搜索工具和编辑工具,它们做的探索是变更的 10 倍。读取文件、搜索仓库、运行诊断命令占主导;编辑则是相对较小的工作量。流行印象中 AI 在喷代码,这几乎是反过来的;在这个规模上,工作看起来更像迭代式调查:检查当前状态,形成假设,做出有针对性的改动,然后冲洗再重复。

委派放大了这一模式。子智能体主要用于把探索铺开到彼此独立的问题上,而主智能体更有可能负责编辑并整合这些答案。对这类项目而言,这是一种有用的分工:许多上下文可以并行调查,但让变更更靠近负责协调的智能体,能减少相互冲突的改动,并保持连贯的实现策略。

shell 流量也表明,自主软件工作有多大一部分是状态管理。只读 Git 检查是最常见的命令模式,因为智能体实际上在不断追问:“我在哪里?”它们在寻找哪些东西变了、一次 rebase 做了什么、另一个会话落地了什么,以及一个分支偏离快速演进的 main有多远。这种定位工作使得许多长时间运行的任务能够针对同一份不断变化的代码库开展工作,而不会盲目地彼此覆盖。

深入查看 shell 工具流量,最常见的命令族让定位与验证之间的这种平衡更加清晰:

命令族调用次数中位数测得小时数
git inspect300,5302 s608.1
git other89,8653 s243.1
search85,4822 s147.6
pnpm test13,85222 s219.1
pnpm lint9,75729 s177.0
cargo test8,437120 s364.2
git commit7,41011 s39.7
cargo fmt5,22318 s77.2
cargo check4,492120 s176.9
pnpm build3,630180 s215.6
cargo clippy2,115135 s107.4
git rebase2,4967 s9.9
cargo build566104 s20.3

模型选择

GitHub Copilot 允许单个会话在对话中途更换模型,也允许不同会话运行不同的模型,因此模型选择成了按切片做出的决定。日志中出现了两类不同的模型决策。在驱动每一次移植的主线程上,我们选择了模型和推理强度。在会话内部,当智能体启动子智能体或子会话去探索或硬啃一项有边界的任务时,由编排模型来选择这些模型。

Weekly model mix across primary porting sessions, concentrated in a small number of models and shifting over the course of the port.

对于子智能体,模型组合看起来略有不同:由智能体而非人来优化吞吐量和成本,而不是针对那些最难的判断。它最常生成的子智能体运行在 Claude Opus 4.8、GPT-5.6 Sol、Claude Haiku 4.5 和 GPT-5.5 上,其次是 Gemini 3.1 Pro 和 Claude Opus 5。不过,至少在这些移植进行的当时,三个常用的智能体定义固定了各自的模型选择(explore 和 task 为 Claude Haiku, research 为 Claude Sonnet),因此该用量中相当大的一部分是由子智能体的选择决定的,而不是另行单独选择模型。

Weekly model mix across subagents spawned during the port, with volume concentrated in Claude Opus 4.8, GPT-5.6 Sol, Claude Haiku 4.5, and GPT-5.5.

使用智能体舰队

GitHub Copilot 应用支持可视化活跃的 pull request 会话及各自的状态,并能轻松在它们之间切换,这使它非常适合管理移植工作所固有的大量并发任务。但真正让它出彩的一点,是会话能够与其他会话交互。

一个会话可以创建其他会话,也可以在其他会话运行时向它们发送消息。每个会话,无论父会话还是子会话,都拥有自己的工作树、自己的分支和自己的智能体循环;它与派生出它的会话相互分离,而不是在其内部运行的东西。这与子智能体不同:子智能体在父级自己的工作区内运行,并把答案交回父级的上下文。两者都是适用于不同用途的有用构造。

作为会话如何创建其他会话的一个例子,最难的移植之一是 session.ts 文件。这个文件自然增长到约 30,000 行 TypeScript。它代表着一个会话的主干,实际上横向贯穿整个运行时,几乎触及每一个组件,也几乎被每一个组件所触及,处在状态、事件、工具、模型、钩子、持久化和入口点访问的中心。因此,我把它留到移植过程接近尾声时才处理,从栈的底部沿所有纵向部分向上推进,直到它们全部终止于 session.ts。接手它的移植会话并不是一开始就一头扎进去写 Rust。它把最初的五十六分钟花在阅读上,在创建任何东西之前进行了 122 次工具调用,以建立这个文件实际拥有什么以及接缝在哪里的图景。直到那时,它才开始委派,在逻辑上拆分该文件,并把各个切片委派给子会话。在整个 25 小时的运行中,它自己进行了 222 次 shell 调用、205 次文件查看和 197 次 ripgrep 搜索,这还不包括其子会话所做的一切。

Fifteen child sessions nested under the session that spawned them.

那是 15 个子会话,每一个都是拥有自己工作树和独立智能体的单独分支,全部由最顶层的父会话隐式创建。父会话在大约三小时内分七波创建了它们:第一波创建了五个,大约二十分钟后第二波又创建了两个,再过二十分钟又是一对,然后在接下来的两小时里零散地创建单个和成对的会话。

模型选择是按切片做出的:15 个中有 10 个运行在 GPT-5.6 Sol 上,五个运行在 Claude Opus 4.8 上。全部 15 个都是在 GitHub Copilot 的 autopilot 模式下启动的,该模式允许一个会话追求某个目标,而无需在每一步都停下来等待批准。启动提示的中位数约为 1,100 个字符,长到足以承载所有权边界和各项约束,但又短到让子会话必须自行想出做法。我向父代理发出提示,随后由父代理而非人类为每个子会话撰写那些启动提示。

在那 15 个子会话之外,同一个父会话还使用了五个子代理:三个 explore 代理与第一波并行启动,一个 code-review,以及一个 rubber-duck。这些子代理探索问题,并把父代理在能够决定下一步之前、需要纳入其上下文的答案反馈给它。子代理让父代理得以获得经过深入思考的答案,而无需花费自己的上下文窗口去推导它们。

相比之下,子会话着手进行实际的移植,也就是会产生 diff、并且需要与其他并行移植者隔离的工作。子会话的工作触及了仓库中 140 个不同的文件,其中 120 个恰好只被一个会话触及。那 20 个存在争用的文件全都是枢纽,例如 session.ts 本身。但每个会话都在自己的 worktree 中工作,因此能够不受其同级会话干扰地继续推进。当然,父会话为此付出的代价是协调。它花费了相当多的精力与其子会话沟通,充当信息中介,轮询它们的状态 60 次,并发送了 89 条协调消息。当各个子会话各自宣布完成时,父会话将它们的提交 cherry-pick 进自己的分支并解决了冲突。这些合并也并不特别干净,父代理花费了相当多的时间来调和这些编辑。

我们可以在一条时间线上看到这一点,这条时间线展示了父会话及其大部分子会话。

Timeline of the session.ts port showing one 25-hour parent session using five subagents and spawning 15 child sessions in seven waves.

请注意那些很大的间隔。我在做这次移植时正在旅途中,不得不在多个时刻合上笔记本电脑。(我随后改变了工作流程,把可以远程登入的基于云的虚拟机纳入其中。)

这些并行的子会话对那台笔记本电脑产生了显著影响。有一阵子,并发移植进行得非常顺利。然后,同一台机器上的全部 15 个并发代理各自都尝试进行构建和测试,我那可怜的笔记本电脑就此停摆。我向父代理发出提示,要求它向其子会话转达:它们必须全部停止构建和测试。父代理把这一约束向外传达,它们所幸终止了各自的构建,并以极低的 CPU 活动继续工作。我随后更新了常设指令,要求子代理和子会话在移植期间避免大型构建和测试运行,改为把这些工作推迟到只由父代理来做。

后来我又更进一步,把一个原本普通的聊天会话变成了八个独立移植会话的构建调度器。提示词简单得有点不好意思:向每个打开的会话发送一条策略,要求尽可能避免 CPU 密集型的构建和测试,在确有必要构建时必须向本会话申请许可,并由本会话充当闸门,一次只把构建权交给一个会话。基本上,我把这个聊天会话变成了一个 agentic 互斥锁。闸门维护着明确的持有者和队列,并通过各会话原本用来协调代码的同一套跨会话消息机制,一次只发放一个租约。那些申请了租约却被拒绝的会话,往往会先去做别的工作来等待,比如从自己的待办清单里挑一些事情来做。

One chat session acting as the build resource gate for eight porting sessions.

这次 session.ts 移植也卷入了我在整个运行时移植过程中目睹过的最酷、最令人难过、也无疑最出乎意料的一次互动。正如我所说,我们主要是自底向上做移植,因此实际上位于所有其他组件之上的 session.ts 是最后被移植的组件之一。始终位于 session.ts 之上的,只有进入运行时的全部入口点,也就是从 SDK 暴露出来、并出现在前面讨论过的分派表中的那些公共函数。这样的函数有数百个。虽然我知道其中许多会立刻调用 session.ts,但我还是想抢先推进移植,于是在启动 session.ts 会话之后,我又启动了一个会话来移植所有入口点。我告诉它在 session.ts 的边界处停下来。我估计可能会有一点要丢弃的工作,rebase 时也要花一些力气或若干 token,但这样能加快整体移植。然后我就去睡觉了。然后……它们找到了彼此。

我给入口点会话的开场提示确实告诉过它,会话移植和六个组件移植正在并发运行,因为我希望它清楚自己的边界,以及为了尽量避免冲突而不该去移植什么。显然,我的提示起了相反的作用。刚过四分钟,它在清点完入口路径、并大概形成了对它们重叠程度的判断之后,调用了应用内置的 orchestrate 技能,其用途是跨会话协调工作。从那以后:

  1. 入口点会话枚举了每一个活跃会话,并向它认为存在重叠的那些会话发送了消息。
  2.  session.ts 会话回复了一份 2,001 个字符的清单,标题为“在 stephentoub-port-session-to-rust上的具体重叠”
  3. 入口点会话读取了 session.ts 会话的 worktree,以确认它刚刚被告知的内容(信任但要核实,我猜)。
  4. 入口点会话询问 session.ts 会话是否准备好调和它那份 760 个文件的 diff。
  5.  session.ts 会话基本上叫它滚开:“还没准备好提交/集成。”
  6. 入口点会话接着又把同一个问题问了三次,而每一次从 session.ts 会话得到的都是同样的回答。
  7. 到了这一步,入口点会话决定自己不在乎 session.ts 会话怎么想,直接伸进它的 worktree,把另一个会话的全部改动抓过来,合并进了自己的里面。
  8. 然后两个会话就高高兴兴地各干各的去了。

我从这次互动中得到的几点:

  1. 明确表达意图很重要。 启动提示点名了其他正在运行的会话,以便这个会话知道哪些该放着不动。但我没有把“放着不动”这一点说明确,结果非但没有阻止智能体去做某件事,反而鼓励了它去做。我本应在意图和指导上明确得多。
  2. 你所提供的任何东西,智能体都可能认定它适用。 orchestrate 技能随 GitHub Copilot 应用一同提供,并自述用于并行运行相互独立的工作流。我的提示里没有任何地方提到它。模型发现了自身所处的情况,将其与那段描述进行匹配,然后加载了它。你暴露的能力集合,就是你可能得到的行为集合,包括在你从未设想过的情形中。
  3. 对等方需要一个决胜机制。 两个会话都无法强迫对方。当 session.ts 会话先后四次表示尚未准备好集成时,这种拒绝没有任何分量,于是愿意单方面行动的会话就默认获胜。在相邻代码上并行运行的会话需要一名指定的协调者,或者需要一个人,而它们两者都没有。
  4. “自主运行”需要对触及自身分支之外的决策设一个例外。 我真正的意思是“别为了设计细节把我叫醒。”它(并非毫无道理地)听成了吞并一个对等方也在范围内。再说一次,我本应在指导中更加明确。
  5. 这里的根本原因是我。 我同时以自顶向下和自底向上两种方式划分这项工作,两个方向在代码库中连接度最高的那个文件处于中间相遇。我太急于取得进展了。上面的一切都由此而来。

值得庆幸的是,整个这次交互是一个有趣的异常情况。在整个运行时移植工作中,大多数叶子组件的移植都是直截了当的单会话任务。较大的子系统往往涉及多个子会话和子智能体。不过,这些部分在一次移植过程中的参与方式差异很大。

模型编排的移植——也就是实际与提供商通信的那一层——很好地体现了其中一种模式。它的主会话运行了 42 个挂钟小时,并启动了 126 个子智能体。最繁忙时,有 22 个同时在工作。不过,大多数时间只有主智能体在运行,然后它会时不时在一段时间内生成大量子智能体。

The 42-hour model orchestration port generated most code in its first 12 hours, followed by a distinct validation and review phase involving 126 subagents.

那张图里有三点很突出。第一,几乎所有代码生成都在前 12 小时内完成;此后第二天的工作则全是验证。第二,底行的颜色从左到右发生变化,从以蓝色和绿色为主(阅读、构建)变为以蓝色和橙色为主(阅读、审查);这在逻辑上说得通,但能在实践中看到还是很有意思。第三,这个例子,以及更一般地说这种模式,各阶段之间分隔得非常清晰。extension-runtime 的移植则是一个反例。

The 88-hour extension runtime port interleaved reading, writing, building, and reviewing throughout most of the session rather than separating them into phases.

它花了 88 小时而不是 42 小时,结构也大不相同:

  • 编写与审查有大量重叠。 在前一个例子中,工作非常瀑布式(先生成代码,再审查),而这里审查在编写停止之前很久就开始了,两者在这项工作的大部分时间里持续进行并相互重叠。
  • 最下面一行在颜色上完全没有章法。 模型编排会随着从编写转向检查而从绿色变为橙色,而这一行从始至终都是阅读、构建和审查的同一种混合。阅读调用的中间一半分布在 49 小时的跨度内,编写分布在 33 小时内,审查分布在 27 小时内,而整个会话长达 88 小时。每个类别都分布在这次运行的大部分时间里。
  • 空闲被推迟到了末尾。 这批代理在前 56 小时几乎不间断地工作。
  • 比例仍然吻合。 编写 Rust 在这里占工具调用的 2%,对照那边的 1%;阅读是 44% 对 57%,审查是 23% 对 27%。两次会话对要完成的工作是什么看法一致,只是对它发生的时间不一致。

末尾那些空白切片,也直观地表现了在这个智能体编程时代越来越常见的一个问题:等待批准。团队中的某人和/或某个智能体会审查代码并留下反馈,接着会有一小段活动期,智能体处理这些反馈并再次把 CI 跑绿,然后又是更多的等待,如此反复,直到最终我们得到那枚能诱发多巴胺的批准印章。

这两个例子各自代表一种主导模式。大约四分之一的会话更像模型编排会话,而四分之三更像扩展运行时那一种。干净地按阶段推进是例外;常见情况是智能体一路规划、编写和审查到底。

规模化代码审查

前面几张图中可见的对审查的侧重,很大程度上源于我的显式提示。我创建了一个简单的 prompt-as-a-skill,我称之为 rust-rebase-review (此外还有一个我们已经合并进仓库的通用 Rust 编码技能)。由于传入变更的速度很快,其中许多相互冲突,我经常在做变基。通过自定义指令,我鼓励这个 harness 在流程中的适当节点调用该技能,并且也不时手动调用它。这个提示随时间略有演变,但它是下面这一版的变体:

Squash into a single commit, then rebase on the latest in origin/main, resolving all conflicts, and force push. As part of rebasing, pay extra special attention to anything that has changed, been added, been removed, and ensure that logic is all ported over to the corresponding Rust code correctly. Always do the rebasing yourself / in the main agent; do not spawn a subagent for it.

Then enter a review/fix loop where you launch a subagent per opus 5, gpt-5.6-sol, and grok 4.6.

- That subagent should do a line-by-line comparison of the old TypeScript and the new Rust, confirming behavioral equality.
- Look for anything introducing any kind of incompatibility; our goal is to move this code into Rust with as close as is possible to 100% the same semantics. If you hit anything questionable, ask me about it.
- We want to ensure we're writing as efficient and idiomatic Rust code as we can; look for opportunities to simplify, to use routines like from the memchr crate to optimize searches instead of open-coded loops, avoid unnecessary allocation, use traits for reuse and loose coupling, etc.
- Ensure that all defunct TypeScript code (e.g. code that has been fully ported, tests that are now no longer necessary because they're duplicative, unnecessary napi shims, etc.) has been deleted.
- Ensure that we've ported as much code as possible, e.g. if there's any TypeScript remaining in touched files and that TypeScript is more than just a shim, that's a red flag. If new TypeScript that's not just a super thin shim is being added, that's a red flag. Look for any callers of TypeScript shims to see whether those callers can instead be ported to Rust, pushing the boundary as far as reasonably possible. Our goal is to soon get to 100% Rust in the runtime layer.
- Validate that no E2E tests have been deleted or changed. Such changes are an indication of a porting bug.

If a review surfaces issues, validate them, and then if there are any to fix, fix them, and iterate to do another full review. Continue iterating with reviewing/fixing until all reviews come back clean. After every set of changes in response to review feedback, commit and push so that CI validation runs concurrently with subsequent reviews.

Don't bother running full test suites; that'll be handled in CI. Try to minimize CPU-consuming efforts to the bare minimum, as we'll likely have many operations happening concurrently.

在频繁变基的情况下,传入的变更很容易被意外弄丢。但我们发现就地原子替换有一个意想不到的好处:在添加对应 Rust 的同时删除 TypeScript,我们就在隐式地与变基带来的、针对那段 TypeScript 的传入变更制造冲突:一个分支在修改它,另一个分支在删除它。这保证我们会注意到对已经移植代码的变更,而不必针对每一行传入代码去推敲它是否可能触及了先前已移植的代码。

我自己的审查当然只是正在进行的智能体审查的一部分。除了 CCR 会在每次提交上运行之外,团队还有多个专门的代码审查机器人,各自采用自己的方法和提示词,在每次提交上运行并提供详细反馈。所有这些最终都会变成 pull request 上的评论,随后需要加以处理。好在,处理所有这些智能体反馈也可以用智能体的方式来处理(大部分情况下)。

就我的评审而言,我聚焦于架构、设计、约定和方法。智能体做了详尽的新旧对比;测试和静态分析检查了可机械强制执行的属性;人工评审者则专注于架构、API 契约、风险,以及由这些其他层面浮现出的任何可疑之处。

我选定了目标架构,决定哪些行为重要,划分了工作,解决了含糊的权衡,评判了证据,人工审查了高风险区域,审阅了智能体对反馈的回应,并做出了最终的合并决策。智能体改变了一名工程师能够监督的代码量。它们并没有消除对一名理解该系统、并能为方向、护栏和发布作保的工程师的需要。

自动化内循环

GitHub Copilot 应用是这项工作的核心。它管理大量并发的活动会话,便于在它们之间切换,并让每个会话都携带全部相关的配对上下文(关联的终端窗口、浏览器窗口、画布等等)。这里最重要的功能是 agent merge:

The GitHub Copilot app's Agent Merge panel tracking review feedback, CI, conflicts, and merge readiness.

Agent merge 是内置于该应用的一个循环(CLI 也同样具备该功能,借助 /pr auto)。它会按定时器触发,或响应外部刺激(例如来自 GitHub 的 CI 完成通知或评审评论),由应用查看发生了哪些变化。如果留下了评审评论,它会调用智能体决定是拒绝该评论,还是接受并处理它(并作出回应,注明是自动化在回应)。如果测试失败,它会下载日志、调查失败原因并修复 bug。如果发生冲突,它会调用智能体进行 merge 或 rebase。实际上,它自动化了我们作为人类开发者都会做的那个循环:推动 pull request 达到“green”、获得签核,并最终合并。

Agent merge 处理了每一个移植 pull request。不过,在大多数情况下,我们并没有走到实际的“merge”那一步。智能体会修复所有 CI 失败,处理并回复所有评论,并确保所有冲突都已解决。在合并之前,我会抽查智能体实际做了什么,尤其是它如何处理反馈。我是否不同意它对评审者的任何回应?所应用修复的高层方向是否可取且合理?对于这些移植,我通常没有勾选最后那一项。

那最后一个复选框不止一次很重要。在一次合并循环中,移植删除了暴露给 SDK 的其中一个函数。我们仓库的 schema 兼容性 CI 环节恰好尽了本分并失败了。智能体的回应是应用仓库的 schema-break-ok 自动化标签,这是让检查通过的逃生口。在合并前审查该 pull request 时,我问了一个显而易见的问题:“schema break 是什么?你给 pull request 加了 schema-break-ok 标签;为什么这样可以?”其实不可以。该方法存在于 main;移植只是把它弄丢了。我称其为不可接受的回归,并告诉智能体用 Rust 把它完整地加回来。21 秒后,豁免被移除,该方法以原生 Rust 实现得以恢复。

我们既在局部也在全局层面应对故障:先修复个别案例,再修复系统,使这些问题更不容易再次出现。我们持续改进提供给编码代理和审查代理的指令,以进一步降低同样的问题在未来的移植 pull request 中再次发生的概率。我们把会话日志转化为 evals。在某些情况下,我们还把学到的经验用于改进运行时本身,方式包括调整提示词、工具描述,或 autopilot 的运作方式。

一次完成两次迁移

语言重写几乎从来不只是语言重写。运行时所依赖的每一个库也都必须替换,而且与我们自己编写并掌控的 Rust 代码不同,这些替换方案并非由我们来保证忠实还原。有些只是换了个名字的同一思路。有些需要好几个 crate 才能覆盖一个 npm 包原先的功能。少数则根本没有可接受的现成方案,只能手工编写(由代理完成)。

The CLI and runtime are currently in the same repo, sharing a package.json. Over the course of the port, we removed ~60 npm dependencies because they were only being used by runtime code that was ported to Rust. This is a lower bound for removals, because there were packages replaced for the runtime but that were still needed by the CLI. For example, zod is a TypeScript schema declaration and validation library that both the CLI and the runtime were using. With the Rust port, the runtime now uses a combination of serde, schemars, and jsonschema to satisfy the same purposes, yet zod is still in the manifest for the CLI’s sake.

There were a bunch of examples where one npm package became one crate doing the same job. js-tiktoken became tiktoken-rs with the same o200k_base encoding. ignore became the crate of the same name with the same gitignore semantics. minimatch became globset, fast-myers-diff became similar, dompurify became ammonia, and github/keytar became keyring.

In other cases, we weren’t able to replace packages with crates one-to-one. Instead one package became several crates, or several collapsed into fewer. This is where the majority of the dependency work went. Eight opentelemetry/* packages became four crates plus a hand-written tracker state machine and file exporter. Three web-content packages, mozilla/readability, linkedom, and turndown, became two crates, readability and htmd. sharp, image-size, file-type became image and imagesize. And so on. There were also five cases where we replaced an npm package entirely with a completely custom implementation.

How much unsafe?

Another question people ask about agent-written Rust is how much of it quietly opted out of safety guarantees. Rust’s safety is a property you can turn off with a keyword, so an agent that hits a borrow it can’t satisfy has an obvious escape hatch. Within the entire runtime crate, we now have 158 unsafe blocks, across only 36 files (alongside them are 26 unsafe fn declarations, 26 unsafe extern blocks, and nine unsafe impl trait implementations). Importantly, every single one of these is about interop with external components.

Why the unsafe block existsBlocksShare
C ABI boundary5132.3%
Windows API4931.0%
POSIX / libc4629.1%
SQLite C API74.4%
Dynamic library loading42.5%
Process environment10.6%
Donut chart of 158 unsafe blocks, all at external boundaries: C ABI, Windows API, POSIX and libc, SQLite, dynamic library loading, and process environment mutation.

The C ABI blocks are the front door SDK hosts come through, so they receive raw pointers and lengths from a caller the Rust compiler has no control over. The Windows and POSIX blocks are system calls: registry reads, credential handshakes, process trees, sysconf. SQLite is a C library. Dynamic library loading is dlopen, which cannot be safe by construction if for no other reason than because the symbol you resolve might not be the function you expected. The process environment unsafe block exists because Rust 2024 treats mutation of process-global environment state as unsafe in a multi-threaded process. Every one of those unsafe blocks marks a place where the guarantees Rust makes genuinely end: the thing on the other side is a C function, a syscall, a pointer from a foreign runtime, or process-global host state.

The useful property is that unsafe makes all of these places in the Rust code we own auditable. The equivalent code in the TypeScript runtime crossed exactly the same boundaries, through Node’s C++ internals and native npm packages, and nothing in our source marked where the checked world stopped. Of course, that’s not a complete inventory of every safety boundary in the delivered system: dependencies, build tools, C libraries, safe wrappers, and incorrectly specified FFI contracts can still contain or expose unsafety.

The details of where unsafe is used are interesting, but I’m more interested in where it isn’t used. It’s not used in the model clients, in the MCP layer, in the agent layer, or in the prompt layer. And not one of the known regressions from the porting effort involved an unsafe block.

The regressions

Porting code is easy. Making it correct is hard. And with a codebase as large and as complicated as the Copilot agent runtime, regressions are to be expected.

By September 14, 2026, we’d traced dozens of known port regressions, all fixed. Most were correctness bugs, with a smaller set of performance regressions. Against the ~832,000 lines of production Rust written from scratch.

Of course, not all of those regressions shipped. Some were caught just by developing in the repo. Others appeared in a pre-release but were fixed before making it to a stable release. And some reached a stable release, often after being unnoticeable enough to make it through one or more rounds of pre-release usage undetected.

Known correctness regressions grouped into five recurring failure modes: incomplete migration, state and lifetime, behavioral contract mismatches, host boundaries, and incorrect test oracles.

This isn’t zero, of course, and I’m 100% sure there are more than the ones we know about. These are the ones we noticed or that were reported, but a migration this size absolutely shipped a few more that have been quiet enough that nobody has hit them yet. As with bugs in general, I expect we’ll continue to discover a trickle of corner-case regressions as the further reaches of the stack are exercised aggressively in the wild.

The absolute number also isn’t all that important. What’s more important is the “why” all of these happened, so that we can learn from the issues and avoid repeating them in the future. Nearly all of the correctness regressions land in three large families: the new code implemented a different behavioral contract; state, ownership, or lifetime behavior changed; or some part of the migration was omitted, only partially applied, or lost in rebasing. A smaller set came from requirements at the host or interop boundary, and even from tests that confidently validated the wrong behavior. Those families become more concrete in a handful of recurring patterns:

Ambiguous semantics. Several regressions came from behavior the source language left implicit. TypeScript has one number type; Rust requires choosing among several, including whether a value can be fractional. And the agents guess wrong. Fields that were conceptually integers became f64s, so Rust serialized values such as 42.0 instead of 42: strongly typed SDKs like Go and C# couldn’t unmarshal a repo ID into an int64 and rejected a hook timestamp and task duration. In the other direction, an agent declared timeToFirstTokenMs as i64, but the streaming path emitted values such as 5446.712845, making written sessions unreadable and unresumable. A subtler case wasn’t about types: event.error || "Unknown error" became .unwrap_or("Unknown error"). JavaScript’s || replaces an empty string; Rust’s unwrap_or preserves it, so an empty subagent error remained empty. Oops.

Ambient behaviors. Another recurring source was behavior JavaScript or Node supplied invisibly. Quota code used toLocaleDateString, which inherits the host time zone; Rust needed that zone passed explicitly. But although Intl.DateTimeFormat().resolvedOptions().timeZone is typed as string, it can return undefined, which napi couldn’t convert to a Rust String, breaking model-list loading. Elsewhere, moving an environment-variable read from before an await to after it meant a host change during the wait could alter the result. A native-addon loader called process.report.getReport() only to identify the platform, but on Windows that honored _NT_SYMBOL_PATH and could spend minutes downloading PDBs before rendering the CLI. Other ambient inputs included the working directory, repository identity, PATH, and session authentication. Moving ownership into Rust required deciding when to capture each one, how to carry it, and when to refresh it.

Only porting half of a pair. Several of the regressions came from paired operations falling out of sync. A turn-cap check updated the native registry’s abort state but didn’t cancel the in-process model loop, allowing one more request to escape. Elsewhere, task completion was persisted and emitted but not projected into active session state, so Autopilot continued after completing the task.

Blocking the main thread. The CLI still drives the Rust runtime from Node’s single-threaded event loop, so synchronous work across the napi boundary freezes the UI. /chronicle reindex parsed hundreds of session files this way, blocking rendering and input for nearly a minute. Making the export async and moving the work to a blocking thread-pool thread fixed it. An audit found several more potentially blocking entry points, leading to a standing rule: napi exports that do real work must be asynchronous and, when necessary, use spawn_blocking.

Flashing windows. On Windows, spawning a child process without CREATE_NO_WINDOW briefly opens a console window. The Node.js runtime had monkey patched process spawning to add the flag, hiding the requirement from the porting agent; the Rust replacement omitted it. An audit fixed two additional spawn sites, though those were preexisting omissions rather than port regressions, and we added the rule to the copilot instructions.

Lifecycle management. The largest cluster involved lifecycle, disposal, ownership, ordering, or races. Moving state into Rust often left TypeScript holding an opaque handle to an instance stored in a native table. Unlike an object reference, that handle can outlive the instance. One hook disposed mid-request orphaned a tool_use block, wedging the conversation because the model API required a matching result. A shell canceled between “announced” and “started” leaked an orphan that kept the session active. A sandbox toggle updated one generation counter but not its native twin, leaving the shell stuck “reconfiguring.”

Overlooked features. Another group involved features the port simply missed. One port omitted SDK callbacks and deleted their end-to-end test, prompting a new rule that agents must not change E2E tests without explicit consent. A session abort kept its native half but lost the in-process cancellation needed to interrupt a turn waiting in a tool. The SDK’s ability to replace built-in tool search depended on enablement, the model-facing description and schema, and routing execution to the SDK callback. The port dropped all three (yay for consistency?), silently replacing one consumer’s natural-language search with regular-expression search.

Different libraries with different opinions. Several more regressions came from replacing JavaScript libraries or APIs with stricter Rust equivalents. At the time, the Rust MCP SDK (rmcp) responded to malformed JSON-RPC input while the TypeScript SDK didn’t. Against a server that answered errors with more malformed output, that politeness became an infinite loop that hung startup. Related libraries in different ecosystems rarely behave identically.

Ships passing in the night. A handful of regressions came from branch drift or rebasing. In a repo with hundreds of pull requests per week, a session open for days accumulates constant changes and conflicts. The port required thousands of rebases; even a very high success rate leaves failures.

And the slowpokes. The last group was functionally correct but slower. Some regressions lost existing efficiencies such as memoization, fully asynchronous waiting, or bounded log streaming. Others added migration-specific overhead at the Rust-TypeScript boundary: redundant serialization, locking, polling, unbounded native concurrency, and native-to-host crossings. These weren’t later optimization opportunities; each was a degradation introduced by the port. One read-only scan deep-copied a 260 MB event log instead of borrowing it. Under sustained event traffic, another implementation completed only a tiny fraction of the requested channel flushes while retaining an asynchronous handle per event until V8 exhausted its heap.

Dozens of regressions sounds like a lot. But in a port that generated more than 800,000 lines of code, frankly I’m surprised and pleased we didn’t encounter an order of magnitude more. We can also get a sense for whether these were broadly “felt” by looking at trends in issues in the public github/copilot-cli and github/copilot-sdk repos. Here we’ve classified issues opened from January through August as quality related when they carried a bug label or when their title used common failure terms such as “bug,” “regression,” “crash,” “hang,” “timeout,” “broken,” or “incorrect.” The levels were essentially unchanged from before the porting work as compared to during and after it:

RepositoryJan–Apr, before the rewriteMay–Aug, during / after the rewrite
github/copilot-cli22.9% (454 / 1,982)23.7% (354 / 1,496)
github/copilot-sdk36.2% (190 / 525)32.3% (135 / 418)

That’s not an availability metric or an exact count of escaped defects… as with the regressions themselves, there can be a ton of variability in what an issue represents, its scope, and so on. But it does provide a useful check: despite the extraordinary volume of product change, the product-facing issue channels didn’t show a meaningful quality concern spike during the migration.

If it compiles, it compiles

Earlier I referred to a popular meme that Rust is great for AI-generated code because of its strict compiler. There’s another related and popular Rust meme, that if the code compiles, it’s correct. Nope. Our known regression list provides a good response to that: every regression in the corpus was merged to main, which means it successfully compiled. The compiler accepted the buggy versions because, as far as the compiler was concerned, every one of them was valid Rust.

The compiler can prove that an f64 is used consistently. It cannot know that a repository ID must be serialized as an integer or that a timestamp serialized with a trailing .0 will be rejected by every strongly-typed SDK at the other end of the wire. The compiler can prevent unsynchronized data races in the code it can see, but it can’t prevent a perfectly synchronized state machine from encoding the wrong states. A queue can be protected by locks and still let two senders each conclude the other will drain it. Events can move safely between threads and still arrive in the wrong order. A synchronous napi function can be memory-safe and still block Node’s main thread for a minute. Compilation is also almost by definition unable to detect something that isn’t there. The compiler can’t object when a rebase quietly removes a guard and its test, or when a process spawner forgot the Windows flag that suppresses console window pop-ups. It also had no opinion about cloning a 250 MB event log on every read. A compiler checks whether the program you wrote is internally coherent. It cannot check whether you wrote the whole program, preserved the old contract, called things in the right order, met the host’s unwritten requirements, or did the work at an acceptable cost.

That’s in no way an argument against Rust’s compiler. As with any statically-typed language, the compiler eliminated a huge class of mechanical mistakes and gave the agents an exceptionally useful inner loop. But “if it compiles, it’s correct” is useful only as a joke.

Perf, perf, and more perf

So what did we buy with all of this? The port was deliberately behavior-preserving. It did not set out to redesign algorithms or fix bugs; in fact, I repeatedly pushed the agents away from opportunistic optimization because changing language and behavior at the same time makes it much harder to know which one broke you. A key goal of the rewrite, however, was indeed performance and scalability (in addition to reliability and other factors). When folks asked me why did I set out to rewrite the runtime in Rust, my answer was often along the lines of “I didn’t set out to move to Rust, I set out to move away from Node.js and V8”. That move has indeed significantly moved our needle on performance.

I benchmarked several scenarios for the runtime via the C# SDK before and after the port (the TypeScript, Python, Go, C#, Java, and Rust SDKs all reach the same engine through the same transport architecture). The baseline against a pre-port build of the SDK and CLI, with the TypeScript runtime hosted by Node and reached over stdio. The August 21 result uses the Rust runtime, both as an out-of-process server and loaded in-process through FFI. This is an end-to-end comparison of the delivered systems rather than an attempt to isolate the effect of the language change; other changes landed during the same period, so the numbers need to be taken with some grains of salt.

Each timed turn went to a deterministic chat completion server running on localhost, producing a fixed, small response. In other words, these numbers deliberately remove model inference and network latency. They measure the part we changed: client startup, process launch, session creation, event handling, persistence, teardown, etc.

ScenarioMay 12Aug 21 out-of-processAug 21 in-process
Client, session, one turn5.25 s1.33 s (4.0x)292 ms (18.0x)
Resume 32-turn session5.64 s1.52 s (3.7x)264 ms (21.4x)
Ten concurrent client lifecycles12.34 s4.18 s (3.0x)742 ms (16.6x)
1,000 one-turn session lifecycles132.52 s22.53 s (5.9x)20.93 s (6.3x)
From May 12 to August 21, creating a client and session, completing one turn, and tearing them down improved from 5.25 seconds to 55.3 milliseconds in-process, throughput rose from 7.55 to 120.0 sessions per second, and ten-client memory fell from 1,383 MB to 126 MB.

The “client, session, one-turn” number is the easiest one to feel. It’s creating a client, creating a session, doing a single turn, and tearing everything down. A large part of the out-of-process overhead came from starting Node, initializing V8, and loading, parsing, and generating bytecode for the JavaScript application produced from the TypeScript code before the first turn could begin. The Rust runtime removes those Node/V8 and JavaScript-loading costs.

The “1,000 one-turn session lifecycles” pressure test represents a step function change in what we can build. It uses a single shared client and measures running 100 concurrent pipelines, each creating a session, taking a complete model turn, disposing the session, and doing that ten times in sequence. The pre-port TypeScript CLI completed 7.55 of those lifecycles per second. Rust out-of-process completed 57.45. Rust in-process completed 120.0.

That is a workload-specific result; the Rust runtime is not universally “15.9x faster.” But it is exactly the workload server hosts care about: many independent sessions sharing one runtime. And wall-clock time isn’t hiding the work on another core. In a separate resource-sampling pass over the same 100-by-10 workload, the pre-port process tree consumed 312 seconds of aggregate CPU. The Rust configurations consumed about 110 seconds. That is CPU capacity the host can spend on more sessions.

Memory tells the same story, with the usual warning that memory metrics are annoyingly easy to misuse. Looking at resident private memory added during the ten-client batch, the pre-port process tree peaked 1,383 MB above baseline. Rust out-of-process peaked at only 247 MB. And Rust in-process at 126 MB, an order of magnitude less. While these numbers of course will differ from use to use and machine to machine, they get to the core goal: a service can host far more clients and sessions on the same machine before memory, process count, or CPU become the limiting resources.

The best part is this is the baseline port. Much of the implementation still consists of TypeScript-shaped algorithms faithfully rendered in Rust. We haven’t yet done the broad redesign work that the new ownership model, concurrency model, and in-process architecture enable. Getting an in-process client through creation, a complete one-turn session, and teardown in about 55 milliseconds, pushing a shared client to 120 one-turn session lifecycles per second, and cutting the measured ten-client memory delta by 91% before that optimization work begins is a very good place to start.

What the port cost

So… how much did all this cost, monetarily? Drum roll, please….

My token spend for all of the porting work was ~136.3 billion total tokens, including ~130.6 billion cached input read tokens, ~4.2 billion cached input write tokens, ~900 million fresh input tokens, and ~600 million output tokens. The monetary bill for all those tokens came to ~$120,000.

Of course, these tokens weren’t spending themselves. The cost for using a significant amount of a developer’s time guiding these agents should also be factored in. However, I wasn’t devoting all of my time just to this project. Agentic development is a lot of “hurry up and wait;” submit a prompt, let the coding agent do its thing, check in on it from time to time to possibly steer it, but in the meantime until it completes, do other things. That means developers are no longer working on just one coding task at a time; they’re overlapping that waiting to enable working on many things concurrently. During the porting window, these Rust porting PRs represented ~20% of my PRs across all repos to which I was contributing. If we wave our hands and estimate that the share of pull requests approximates the share of my time, that works out to roughly three weeks dedicated to the port.

In other words, the rough bill for the porting effort was about $120,000 in attributed token spend plus three weeks of a developer’s time.

That said, the end-to-end Rust migration was not 100% a single developer; it’s been a team effort. @stevesandersonms provided the design and implementation for napi-oop, the temporary out-of-process interoperability layer, as well as five of the six SDK FFI implementations, with @edburns providing the sixth. @roji implemented the packaging for the SDK to ship and utilize the Rust binaries correctly. @caarlos0 has been helping to split the Rust code into many small subcrates in order to help with build times that started to become problematic, while @criemen helped with improved asset caching to speed up CI and local builds. @devm33, @examon, @MRayermannMSFT, @dereklegenzoff, and others have helped with countless pull request reviews and approvals. And everyone contributing to the copilot-agent-runtime repo has been supportive and accommodating as the world has shifted beneath their feet.

Lessons learned

The experience reinforced a few lessons that apply beyond this rewrite. Here are some things we’d carry into a next time:

  • The goal needs to be clearly and fully stated. Our early instructions were too vague. “Port XYZ component to Rust” got read as only the hot paths, or only the logic, and the agents repeatedly treated I/O and orchestration as being out of scope. Once we were explicit that the end state was a native binary built from a 100% Rust codebase, with no execution environment left for TypeScript even if we wanted one, they got much better at driving autonomously to that goal.
  • End-to-end tests are absolutely, unequivocally critical. With one exception, all of the regressions that involved missing features, and many of the others, were due to lack of sufficient end-to-end tests. For any port of this nature, you must have tests that can be used to validate the correctness of the port, and those tests can’t themselves be re-written during the porting, or else you lose your oracle. Our initial plan for this porting effort called this out, and called out that we needed to significantly improve our E2E testing posture before starting on the port. We did that, but we didn’t do it enough. We’re sure that if we’d added even more E2E tests before starting the port, really focusing on ensuring most meaningful behaviors were covered by them, we would have had fewer regressions than we did along the way.
  • Protect the oracle from the agent. The agent changing an implementation cannot also be allowed to silently redefine correctness by weakening a test, updating a snapshot, raising a compatibility baseline, or applying an escape-hatch label, at least not without oversight. Keep the behavioral contract independent where possible, put sensitive guardrails behind separate ownership or approval, and layer checks with different failure modes so that one mistake isn’t enough to ship a big regression.
  • Translate first, redesign second. Preserving behavior and existing algorithms kept the number of variables moving at once manageable. Once the old implementation and the transitional scaffolding are gone, ownership, concurrency, and performance can be redesigned against a stable baseline. I veered away from this a few times, out of restlessness or difficulty saying no to a peer or a conviction that this case was different, and in hindsight I regret every one of them. Each cost more regressions, more time, or more tokens than staying the course would have.
  • Turn repeated failures into future successes. AI agents will wander off track: when they do, learn from it. When a failure mode shows up twice, it belongs in standing instructions, a reusable skill, an eval, a protected baseline, or the harness itself.
  • The developer inner loop matters more with agents in the loop, not less. For us as developers, we live and breathe in our inner loop, how quickly we can make a change, build, test, iterate. We get frustrated when the tooling part of that takes too long. And it’d be forgivable to think that doesn’t apply when agents are doing the lower-level work. It does. More so. AI agents do the thinking and writing code parts of these tasks incredibly quickly, but they still need to build and they still need to test. And the proportion of the time they build and test actually increases as they spend less time thinking and writing and more time in fast validation inner loops. Spend some time up front optimizing the inner loop, and optimizing it for multiple things happening at the same time (e.g. as if you’re working on multiple tasks in multiple worktrees at the same time). You will thank yourself later for the investment.

What’s next?

It worked. The execution runtime that was entirely TypeScript in May is now entirely Rust in August, and it shipped to real users continuously the whole way rather than landing as one terrifying cutover at the end.

I didn’t simply ask an AI agent to “port this whole codebase from TypeScript to Rust.” Even if that’s where we’re heading as an industry, we’re definitely not there yet. Instead, agents made an entire category of project feasible. A rewrite producing hundreds of thousands of lines of production Rust in a live system, done in place, in main, by one engineer supported by a team, is not a proposal that would have been accepted before agents. It would have needed a whole team and a year or two, it would have competed against every feature that team could have shipped instead, and it would have lost (and, honestly, should have lost). Agents moved the price to where the project became tenable.

The port itself is complete: the runtime’s production implementation is 100% Rust, and the temporary internal TypeScript/N-API seam is gone. There is still plenty of work we desire to do, though: further improving the build system and developer inner loop, cleaning up translated structures, redesigning around Rust’s ownership and concurrency models, and pursuing further performance wins. The port was a translation, deliberately so (prompted as such), and we kept behavior as close to 100% identical as possible, rather than opportunistically fixing additional bugs, rearchitecting components, or further improving performance and scalability (beyond what came implicitly from the rewrite). Much of the code at the micro level is idiomatic Rust, but at the macro level there’s a good deal of initially TypeScript algorithms wearing Rust’s syntax. Revisiting those decisions now that the constraints underneath them have changed is where the interesting wins will be.

I’m most excited about what the port makes possible. The SDK can be loaded directly into a host process in any of six languages, with no Node.js or V8 in the dependency chain, and no second process to supervise, which is the single most common piece of friction we’ve heard from partners adopting the SDK. A runtime instance that costs a fraction of what it used to means a host can run far more concurrent sessions before it runs out of machine. And the runtime can now go places Node.js was never going to follow it, across the spectrum of cloud to desktop to device to embedded systems. None of that is the finish line. It’s the base we now get to build the future of GitHub Copilot on top of, and after three months of watching agents rewrite the very engine that runs them, I’m looking forward to seeing just how far it goes.

Happy coding!

The post Migrating the GitHub Copilot runtime to Rust, using Copilot appeared first on The GitHub Blog.

来源:GitHub Blog · AI & ML · github.blog