AI 程式設計教程中文版
官方教程中文版實戰場景

讓 Codex 執行程式碼遷移

說明如何用 Codex 做程式碼遷移:先盤點 legacy system,再按 checkpoint 驗證 parity。

程式碼遷移不要做成一次性大重寫。Codex 適合先盤點 legacy system,再把舊概念對映到新堆疊,按 checkpoint 落地,每個 milestone 後都驗證 parity。

官方頁面:https://developers.openai.com/codex/use-cases/code-migrations

適合什麼任務

場景Codex 應該做什麼
legacy stack 遷到 modern stack盤點舊系統假設,提出分階段遷移計劃
framework、runtime、build system 或平臺約定要變對映舊概念到新概念,標出沒有直接對應物的部分
產品遷移過程中仍要保持可用使用 compatibility layer、module-by-module port、branch-by-abstraction 或 strangler-style replacement
團隊需要明確回復路徑checkpoint 之間保留 rollback 或 fallback options

使用的能力

能力用法連結
$security-best-practices在 merge 前檢查高風險遷移、依賴變更和暴露面https://github.com/openai/skills/tree/main/skills/.curated/security-best-practices
$gh-fix-ci每個 migration milestone 後處理失敗 CI,不把清理拖到最後https://github.com/openai/skills/tree/main/skills/.curated/gh-fix-ci
$aspnet-core當遷移涉及 ASP.NET Core app models、Program.cs、middleware、testing、performance 或 version upgrades 時使用框架指導https://github.com/openai/skills/tree/main/skills/.curated/aspnet-core

相關官方說明:

起始提示詞

请把这个 codebase 从 [legacy stack or system] 迁移到 [target stack or system]。

要求:
- 先盘点 legacy assumptions:routing、data models、auth、configuration、build tooling、tests、deployment 和 external contracts。
- 把旧栈概念映射到新栈,并标出没有直接对应物的部分。
- 提出渐进式迁移计划,使用 compatibility layers 或 checkpoints,不要一次性大重写。
- 除非迁移明确需要 user-visible change,否则保持行为不变。
- 按 milestones 工作;每个 milestone 后运行 lint、type-check 和 focused tests。
- 在迁移完成前,保持 rollback 或 fallback options 清晰可见。
- 如果 validation 失败,先修复再继续。
- 第一步先 mapping migration surface,并提出 checkpoint plan。

這個 prompt 把 Codex 的第一步限制在 mapping 和 checkpoint plan。複雜遷移先畫清楚邊界,再動程式碼。

推薦遷移順序

  1. 盤點 migration surface:legacy packages、framework conventions、routing、data access、auth、configuration、build tooling、tests、deployment assumptions,以及必須保留的 external contracts。
  2. 讓 Codex 把 legacy concepts 對映到 target stack,並標出沒有直接對應物的部分。
  3. 選擇 incremental strategy:compatibility layer、module-by-module port、branch-by-abstraction,或圍繞單一邊界做 strangler-style replacement。
  4. 預設保持 behavior stable。遷移必須帶來的 user-visible change 要單獨命名。
  5. 每個 milestone 後跑最小但有效的驗證:lint、type-check、focused tests、contract tests、smoke tests,或 legacy path 與 new path 的 side-by-side check。
  6. 每個 checkpoint 後 review diff 和剩餘 transition risk,不要等到完整重寫結束才看。

使用 ExecPlans

OpenAI 的 code modernization cookbook 介紹了 ExecPlans:一種讓 Codex 保持全域性檢視、寫清目標狀態、記錄每輪驗證結果的文件。

複雜遷移建議每個系統部分都建立一個 ExecPlan,記錄:

  • 當前 legacy behavior。
  • 目標 stack 的設計。
  • checkpoint 順序。
  • 每個 checkpoint 的驗證命令。
  • 已知風險和回復方案。
  • 每輪 Codex 修改後的 validation log。

這樣後續 review 的不是“Codex 改了很多檔案”,而是一條可以追蹤的遷移鏈路。

驗收重點

  • 行為未變,除非遷移計劃明確要求。
  • checkpoint 能獨立 review。
  • CI 或本地驗證失敗時先修復,再繼續下一步。
  • dependency 和 exposed surface 經過安全檢查。
  • fallback options 在 transition 完成前一直可見。
  • external contracts 沒有被無意破壞。

本頁目錄