/batch 大規模遷移

Claude Code 內建 slash command——把跨百檔的程式碼遷移用幾十個 Sub-agent 平行 worktree 同時處理。一個指令把整個 codebase 從 Solid 換成 React。

是什麼

/batchBoris Cherny 團隊在 2026-02-27 發布的功能,用於自動化大規模程式碼遷移:

> /batch migrate src/ from Solid to React

執行時:

  1. 互動規劃:你跟 Claude 對齊遷移策略(哪些檔、什麼順序、邊界情境)
  2. 派發:Claude 派數十個 sub-agent
  3. 平行作業:每個 sub-agent 在自己的 Git Worktree 工作流 隔離工作(用 isolation: worktree
  4. 獨立驗證:每個 sub-agent 自己跑測試
  5. 獨立 PR:通過測試的 sub-agent 自動開 PR

為什麼比手寫 migration script 強

傳統做法/batch
寫 codemod / regex scriptClaude 看 AST + 語意理解
邊界情境靠人類列舉sub-agent 遇到時自己處理
全部一起跑、出錯難回滾每檔獨立 PR、可單獨 revert
沒測試就 merge每個 sub-agent 自己跑測試才開 PR
序列執行dozens 個 worktree 平行

/simplify 的搭檔

Boris 同 thread 也發布 /simplify

> hey claude make this code change then run /simplify

/simplify 跑平行 sub-agent 做:

/batch(規模化變更) + /simplify(規模化品質) = PR 從草稿到 production-ready 大幅自動化

Boris 的進階用法

用主對話直接派 worktree-isolated sub-agents

> Migrate all sync IO to async. Batch up the changes,
  and launch 10 parallel agents with worktree isolation.
  Make sure each agent tests its changes end to end,
  then have it put up a PR.

→ 不用 /batch,直接在 prompt 描述策略。

自訂 worktree-only agent

# .claude/agents/worktree-worker.md
---
name: worktree-worker
model: haiku
isolation: worktree
---

每次叫 worktree-worker 都會自動隔離。

實際適用場景

場景規模為什麼適合 /batch
框架遷移Solid → React、Vue 2 → Vue 3、AngularJS → Angular重複性高、語意需要理解、可逐檔
型別系統升級Flow → TypeScript、JS → TS同上
API 升級舊 API 全替換成新 API易拆分、可獨立驗證
lint/format 統一例如 Prettier rules 改、所有檔重排純機械可平行
i18n 注入把 hard-coded string 全換成 i18n key可逐檔但需要 context

不適合 /batch 的場景

  • schema 連動的修改:DB migration + ORM model + API + frontend types 必須一致改,分散到多 PR 會破壞一致性
  • 小修小改:開 dozens 個 worktree 的 overhead > 一次性手動改
  • 需要架構決策:應該先 Plan 模式 對齊一個方案,不是讓 sub-agent 各自決策

對 PAM 的應用點子

Use Case 1:把所有 .cs 的 try-catch 吞例外改成 LoggerService

> /batch 找所有 PAM 系統的 .cs 檔,把 catch (Exception) {}
  吞例外的地方改成呼叫 LoggerService.LogException(ex),
  並在 5 個地方手動 throw new BusinessException(...)
  保留原樣(不要動)

Use Case 2:把所有 React class component 改成 hooks

> /batch 把 client-app/src/components 下所有 class component
  改寫成 functional component + hooks,每個檔獨立 PR、
  獨立跑 jest 測試

Use Case 3:把所有 hard-coded SQL 改成 EF Core

> /batch 找所有 PAM 系統還在用 string concat SQL 的地方,
  改成 EF Core LINQ。每改一個檔就自己跑 dotnet test,
  通過才開 PR

反模式

  • 沒先 Plan 模式 對齊就 /batch → dozens 個 sub-agent 各做各的 = chaos
  • 沒測試就讓 sub-agent 開 PR → 必須在 prompt 強制要求 test-pass 才 PR
  • 跨架構層的修改 → 應該整 PR 一次改、不該拆
  • /batch 完不 review → AI 規模化錯誤的代價也是規模化

相關概念

強連結(原文明確提及)

推斷連結(LLM 認為相關,待確認)

深入閱讀

← 回到 wiki