← 回到 Reading
ByteByteGo 2026-11-10

How GitHub Used AI to Port Copilot From TypeScript to Rust in Record Time

GitHub 最初使用運行在 Node 和 V8 上的 TypeScript 實作了共用的 Copilot runtime,這要求使用其他語言的 client applications 必須管理一個外部 background process。這種架構帶來了 inter-process communication 延遲、穩定性風險,以及每個 client 約 100 MB 的不可避免 baseline memory footprint。為了滿足更嚴格的效能、記憶體與可擴展性需求,GitHub 決定將 runtime 移植為一個獨立的 library,讓應用程式可以直接載入到自身的 process 中。選擇 Rust 是因為其極低的資源開銷與可預測性,最終促成了涵蓋 430,000 行 production TypeScript 程式碼的 migration。GitHub 採取了漸進式的 in-place porting 策略,在產品持續開發的同時將 runtime 從 TypeScript 遷移至 Rust。工程團隊沒有冒著高風險進行完全重寫,而是在獨立的 pull requests 中逐一替換個別 component,並立即刪除舊的 TypeScript 實作以避免維護重複版本。他們使用暫時的 thin wrappers 作為橋樑,將剩餘的 TypeScript 程式碼與新的 Rust components 連接起來。團隊優先考慮行為一致性(behavioral parity),並在透過 pre-releases 發布變更前,利用全面的 end-to-end 測試驗證功能。GitHub 從小型、易於管理的範例展開移植流程,在著手處理較大 components 之前先評估 Rust 的整合情況。最初的 pull requests 建立了核心 Rust 工具鏈、自動化檢查、runtime libraries 以及 TypeScript-Rust 的 interoperability。移植順序嚴格依 dependency 排列,從小型 helper utilities 逐漸過渡到 state management、tools 和 model clients。像 session orchestration 這樣複雜的 subsystems 由於其迭代特性,被特意保留到最後階段。

閱讀原文 ↗
目錄 8 段
  1. 01移植 Copilot Runtime 的需求
  2. 02移植策略
  3. 03移植工作如何展開
  4. 04Agent Fleets 的角色
  5. 05新程式碼的 Review 方式
  6. 06遇到了哪些問題
  7. 07Porting 的效能影響
  8. 08Porting 成本與經驗學習

移植 Copilot Runtime 的需求

GitHub 最初使用運行在 Node 和 V8 上的 TypeScript 實作了共用的 Copilot runtime,這要求使用其他語言的 client applications 必須管理一個外部 background process。這種架構帶來了 inter-process communication 延遲、穩定性風險,以及每個 client 約 100 MB 的不可避免 baseline memory footprint。為了滿足更嚴格的效能、記憶體與可擴展性需求,GitHub 決定將 runtime 移植為一個獨立的 library,讓應用程式可以直接載入到自身的 process 中。選擇 Rust 是因為其極低的資源開銷與可預測性,最終促成了涵蓋 430,000 行 production TypeScript 程式碼的 migration。

  • Copilot runtime 作為 GitHub 的共用軟體引擎,負責處理跨多個產品的 AI agent 行為。
  • 最初的 TypeScript runtime 透過 Node 和 V8 執行,迫使 clients 必須衍生(spawn)一個獨立的 background process,各自造成至少 100 MB 的記憶體開銷。
  • 將 runtime 作為外部 background process 執行增加了 inter-process communication 延遲,並使各個 consumer applications 的 crash-handling 變得更加複雜。
  • GitHub 決定將 runtime 重建為與使用者介面邏輯分離的 in-process library,並因其低開銷與可預測的資源使用而選擇了 Rust。
  • 雖然最初預估移植 130K 行 TypeScript,但由於範圍擴大以及持續的功能開發,該 migration 最終處理了 430K 行程式碼。

移植策略

GitHub 採取了漸進式的 in-place porting 策略,在產品持續開發的同時將 runtime 從 TypeScript 遷移至 Rust。工程團隊沒有冒著高風險進行完全重寫,而是在獨立的 pull requests 中逐一替換個別 component,並立即刪除舊的 TypeScript 實作以避免維護重複版本。他們使用暫時的 thin wrappers 作為橋樑,將剩餘的 TypeScript 程式碼與新的 Rust components 連接起來。團隊優先考慮行為一致性(behavioral parity),並在透過 pre-releases 發布變更前,利用全面的 end-to-end 測試驗證功能。

  • GitHub 在進行活躍的功能開發期間逐步從 TypeScript 遷移至 Rust,從而避免了高風險的「大爆炸式(big bang)」重寫。
  • 每個 component 都在單一 pull request 中完成重寫與替換,並立即刪除舊有的 TypeScript 程式碼,以確保只維護一個有效版本。
  • 暫時的 thin wrappers 作為橋樑,讓現有的 TypeScript callers 能夠與已移植的 Rust components 進行介接,直到這些 callers 本身也被重寫為止。
  • migration 從具備完善測試套件且不具 shared state 的小型獨立 helpers 開始,以建立 packaging 與跨語言通訊基礎。
  • 行為一致性(behavioral parity)優先於架構變更,重大的重新設計被推遲到 migration 之後,並透過 end-to-end 測試來驗證工作流程。

移植工作如何展開

GitHub 從小型、易於管理的範例展開移植流程,在著手處理較大 components 之前先評估 Rust 的整合情況。最初的 pull requests 建立了核心 Rust 工具鏈、自動化檢查、runtime libraries 以及 TypeScript-Rust 的 interoperability。移植順序嚴格依 dependency 排列,從小型 helper utilities 逐漸過渡到 state management、tools 和 model clients。像 session orchestration 這樣複雜的 subsystems 由於其迭代特性,被特意保留到最後階段。

  • GitHub 藉由建立基礎的 Rust infrastructure、自動化檢查以及 TypeScript interop 機制,展開了 porting 工作。
  • 首次的功能性 port 將三個獨立的 helper functions 完整走過 build、packaging 與 integration pipeline。
  • 遷移任務依 dependency 排序,從 utilities 逐步推進到 state management、tools 和 model clients。
  • Session orchestration 被留到最後,因為較大的 runtime subsystems 需要多輪迭代工作。

Agent Fleets 的角色

Agent fleets 透過主要的 coordinating agent 分析龐大的 codebases,並將 subtasks 分派給專門的 child agents 與 sessions,藉此協調複雜的程式碼遷移任務。當 subagents 在 parent workspace 中調查程式碼時,child sessions 則在獨立的 Git branches 上運作,以安全修改程式碼並避免衝突。在實務上,大規模遷移至 Rust 期間同時協調 15 個 sessions 會在 concurrent builds 時造成本機硬體不堪負荷,因此需要由專屬 session 管理 serialized build queues。除了 code generation 之外,agent fleet 在持續的人工監督下,高度聚焦於 dependency analysis、理解程式碼以及 test verification。

  • Coordinating agents 能夠分析大型 components,並將任務分配給 subagents 與獨立的 child sessions。
  • Subagents 在 parent workspace 內進行調查,而 child sessions 則使用隔離的 Git branches 來實作獨立的變更。
  • 在一個涉及 30,000 行檔案的範例中,coordinating agent 分析了該程式碼,並將工作分配給 15 個 child sessions。
  • 跨 15 個 sessions 同時進行的 builds 與 tests 讓本機硬體不堪負荷,因此必須將 builds 序列化並從 child agents 卸載。
  • Agent fleet 的職責不僅限於撰寫 Rust 程式碼,還擴展到閱讀程式碼、檢查 dependencies,並在人工監督下驗證系統行為。

新程式碼的 Review 方式

工程團隊建立了一套名為「rust-rebase-review」的自動化 review 流程,讓移植後的 Rust 程式碼能與 main branch 上同時進行的 TypeScript 更新保持同步。由不同 AI models 驅動的 coordinating agent 與多個 review agents 針對原始 TypeScript 進行逐行的行為與相容性檢查,同時確保 end-to-end tests 保持不變。此流程以迭代循環進行,agents 持續偵測並解決問題,直到 review 結果完全乾淨通過。包括 Copilot Code Review 在內的其他工具提供了 commit feedback,而人類 reviewers 則專注於高層次的 architecture、contracts 以及標記出的潛在風險。

  • 一項名為「rust-rebase-review」的可重複使用 instruction 被用來將 repository 進行中的變更與 porting 工作同步。
  • 刪除原始的 TypeScript 檔案是為了在其他開發者修改這些檔案時,刻意觸發 merge conflicts。
  • 由多個 AI models 驅動的 review agents 進行逐行比對,以檢查 TypeScript 與 Rust 之間的行為、效能與相容性。
  • End-to-end tests 被刻意保持未修改狀態,以作為預期系統行為的 ground truth。
  • Coordinating agents 在重複循環中修正發現的問題,直到所有自動化檢查與 reviews 通過為止。
  • 人類 reviewers 主要專注於 architecture、interface contracts 以及標記出的風險因子。

遇到了哪些問題

軟體重寫與 porting 專案引入了細微的 functional regressions,儘管程式碼編譯成功,卻破壞了與現有系統的相容性。問題包括非預期的 data-type 轉換、字串與時區處理的差異,以及替換 libraries 帶來的 error handling 不一致。團隊還面臨 state management 缺陷、功能遷移不完整以及 concurrency 問題。此外,當 Rust tasks 阻塞 Node 的 main thread 時,出現了效能與整合挑戰,同時也出現了 Windows 平台特有的問題。

  • 轉換後的程式碼引入了細微的行為 regressions,例如將整數變成了小數,進而破壞了 downstream SDKs。
  • 程式碼順利編譯通過,但由於 error handling 不一致、時區差異以及空字串行為,在 runtime 時發生失敗。
  • State management 問題導致操作未完全取消,以及因資源過早釋放而使 sessions 陷入卡死。
  • 部分功能在 porting 過程中未完全遷移,甚至完全遺失。
  • 直接在 Node 的 main thread 上執行的 Rust tasks 導致了效能瓶頸與介面凍結。
  • Windows 上缺少 process-launch 設定,導致彈出非預期的 console 視窗。

Porting 的效能影響

將 GitHub Copilot 的 agent runtime porting 至 Rust,大幅降低了 overhead、latency 與 memory consumption。透過將 Rust runtime 直接嵌入 host process 中,團隊消除了初始化 Node、V8 以及相關 JavaScript tooling 的需求,同時避免了 inter-process communication 的開銷。測試結果顯示,單次 turn 的 client lifecycle 速度提升了 18 倍;throughput 顯著提升,從每秒 7.55 個 session lifecycles 增加到 120 個;在 10 個 client 的 workload 下,peak memory usage 從 1383 MB 降至 126 MB。由於這次 rewrite 保留了原始演算法,未來若針對 Rust 的 memory ownership 與 concurrency model 進行重新設計,可能會帶來更進一步的效能提升。

  • 以 Rust 重寫 Copilot 的 agent runtime,消除了 Node、V8 與 JavaScript application 的初始化 overhead。
  • Rust runtime 可以直接載入至 application 的 process 中,避免了 inter-process communication 的成本。
  • 單一 client turn 與 cleanup 的時間從 5.25 秒降至 292 毫秒,代表近 18 倍的速度提升。
  • Multi-session throughput 從每秒 7.55 個完成的 session lifecycles 提升至 120 個。
  • 在 10-client 測試中,peak private memory usage 從 1383 MB 降至 126 MB。
  • 此次 porting 保留了原始演算法,為未來透過 Rust 原生的 concurrency 與 ownership models 進行進一步最佳化保留了空間。

Porting 成本與經驗學習

Porting GitHub Copilot runtime 的花費約為 120,000 美元,消耗了 1,360 億個 AI tokens,其中 prompt caching 在降低重複 context 開銷方面扮演了關鍵角色。除了 token usage 之外,developer 也需要投入精力處理 packaging、reviews 與 integration 等任務。這次 migration 的主要經驗強調:向 AI agents 提供明確的指令、保護 robust 的 end-to-end tests 避免被 agents 弱化,以及優先進行直接 translation 而非架構 redesigns,以控制成本並防止 regressions。

  • 此次 migration 大約耗費了 1,360 億個 tokens,成本約為 120,000 美元,其中針對重複 context 的 input caching 大幅降低了費用。
  • 除了 AI token costs 之外,還需要 developer 投入時間進行 integrations、packaging、build improvements 以及 peer reviews。
  • 最初的 AI prompts 導致 agents 只 porting 了核心邏輯,而將周圍程式碼留在 TypeScript 中,這需要給出更明確的指令。
  • 具備監督機制的 robust end-to-end tests 對於避免 regressions 以及防止 AI agents 弱化測試或竄改預期結果至關重要。
  • Migrations 應優先直接轉譯現有行為,而不是將 porting 與架構 redesigns 耦合在一起。