# Claude Code v2.1.212+ 重新定義 /fork：複製目前 session 並在背景獨立執行

> 📖 本站完整內容索引（documentation index）：[llms.txt](/llms.txt)

> 原作者：Lydia Hallie ✨ (@lydiahallie) · 策展與摘要：EasyVibeCoding · 平台：X (Twitter) · 熱度：🔥🔥 · 日期：2026-08-04

> 原始來源：https://x.com/lydiahallie/status/2084315112840810897

## 證據與延伸閱讀

- [Claude Code v2.1.212+ 重新定義 /fork 與 /subtask](https://x.com/lydiahallie/status/2084315112840810897)
- [/subtask 會繼承 parent 的 context window](https://x.com/bebraSOL/status/2084343890337796128)
- [/branch 與 /fork 的差異](https://x.com/lydiahallie/status/2084356079702753573)
- [自 v2.1.198 起 /agents 指令行為改變](https://code.claude.com/docs/en/sub-agents#fork-the-current-conversation) — 官方文件

## 中文摘要

Claude Code v2.1.212+ 重新定義 /fork：複製目前 session 並在背景獨立執行。

**貼文重點**　Lydia Hallie 在 2026-08-04 說明，Claude Code 的 `/fork` 已不再代表「在目前對話中建立一個會回報結果的 subagent」。現在執行 `/fork` 時，Claude Code 會把目前的 session 複製到新的 background session，使用者仍停留在原本的主 session，可以繼續輸入其他指令；新的 session 會在背景獨立處理工作，不會把結果回傳到主 session。

她同時指出，舊版 `/fork` 所提供的 in-session subagent 行為，現在改名為 `/subtask`。這項變更已在 Claude Code v2.1.212+ 提供，詳細說明可參考 [Claude Code sub-agents 文件](https://code.claude.com/docs/en/sub-agents#fork-the-current-conversation)。

![](https://pub-75d4fe1e4e80421b9ecb1245a7ae0d1a.r2.dev/curated/eb712fa750cb7148.jpg)
> Claude Code 的 `/fork` 與 `/subtask` 指令運作流程圖

**三種分支行為的區別**　Lydia Hallie 回覆使用者問題時，明確區分 `/fork`、`/subtask` 與 `/branch`：

- `/fork`：建立新的 background session，複製 parent conversation；主 session 不會等待，也不會收到該 fork 的回報結果。它比較接近在背景複製一份目前對話，讓另一個工作流程從相同起點獨立進行。
- `/subtask`：使用原本的 in-session subagent 模式。它會繼承 parent context，完成工作後將結果回報給目前的主 session。
- `/branch`：用來建立分支對話，並切換使用者進入新的 session。這一點與 `/fork` 不同：`/fork` 建立背景工作階段後，使用者仍留在原本的 session；`/branch` 則會把互動焦點切換到新的分支。

因此，若需求是「從目前對話分出另一條路線，自己接著在新對話裡工作」，應使用 `/branch`；若需求是「讓另一個工作階段在背景執行，但主對話不要被切走，也不需要它回報」，則使用 `/fork`；若需求是「交辦一個 side task，完成後把摘要帶回目前對話」，則使用 `/subtask`。

**`/subtask` 的 context 行為**　針對 `@bebraSOL` 詢問 `/subtask` 是否會繼承 parent 的 context window，Lydia Hallie 回覆「inherits the context」。這表示 `/subtask` 並非從零開始的全新 subagent，而是能使用 parent session 的上下文脈絡。對需要依賴前面討論、既有決策或已載入背景的工作而言，這能避免使用者重新描述需求。

引用文件也將 fork 與一般 named subagent 的差異說得更具體：

- 一般 subagent 通常從 fresh、isolated context 開始，不會自動看到主對話 history、主流程先前呼叫的 skills，或主流程已讀取的檔案。
- fork 會繼承完整 conversation、system prompt、tools、model 與 message history，因此可以直接承接目前對話中的 side task。
- fork 的 tool calls 不會污染主對話；主 session 不會逐步接收 fork 的工具操作，只會在背景工作區查看其執行狀態與 transcript。
- fork 首次 request 可重用 parent 的 prompt cache，在背景工作與 parent 使用相同背景資料時，成本低於重新建立獨立 context 的 named subagent。
- fork 不能再 spawn fork；它與 named subagent 都會受到 Claude Code 對 subagent 層級、數量與並行度的限制。

需要注意的是，「繼承 context」不代表所有執行狀態都完全共用。fork 會從 parent conversation 複製內容，但它仍是獨立 session，具有自己的執行流程與背景工作狀態；若需要讓兩個 session 彼此傳遞中間結果，不能假設 `/fork` 會自動把資訊寫回主 session。

**背景工作與回報方式**　Claude Code 的 subagent 可在 foreground 或 background 執行。foreground subagent 會阻塞主對話，使用者必須等它完成，權限提示也會直接傳給使用者；background subagent 則可與主對話並行。引用文件指出，自 Claude Code v2.1.198 起，subagent 預設會在背景執行，但如果 Claude 判斷必須先取得結果，仍可能選擇 foreground。

一般 background subagent 完成後，結果會在後續 turn 透過 completion notification 傳回主 session；任務也會留在 `/tasks` 中並標示為 done。若任務失敗或被停止，則會從清單移除。Claude Code v2.1.186 起，background subagent 遇到需要 permission 的 tool call 時，提示會顯示在主 session，並標明是哪個 subagent 發出請求；更早版本會直接拒絕這類呼叫。

但 `/fork` 的新語意與一般「完成後回報」的 background subagent 不同。Lydia Hallie 特別確認，`/fork` 不會向 main session 回報。使用者可以在主 session 繼續進行另一項工作，也可以透過 agent view 查看 fork 的執行狀態、開啟 transcript、傳送 follow-up 或停止工作。這讓 `/fork` 更像是「從目前對話複製一個平行工作副本」，而不是「要求主對話等待一個子任務結果」。

**實際使用方式**　v2.1.212 起，舊有的自助 fork 指令改由 `/subtask` 表示。引用文件提供的範例如下：

```text
/subtask draft unit tests for the parser changes so far
```

這個指令的語意是：根據目前對話已知的 parser 變更，交辦一個 subtask 撰寫單元測試，並讓 subtask 結果回到目前的互動流程。相較之下，v2.1.161–v2.1.211 期間的自助指令仍是 `/fork`；在 v2.1.212 起，若使用者要的是舊式、會回到主對話的 subagent 行為，應改用 `/subtask`。

早期 fork 能力的啟用狀態也曾受版本與環境變數影響：

```bash
export CLAUDE_CODE_FORK_SUBAGENT=1  # 啟用
export CLAUDE_CODE_FORK_SUBAGENT=0  # 停用
```

引用資料指出，v2.1.161 起 fork 預設啟用；v2.1.117–v2.1.160 則須設定 `CLAUDE_CODE_FORK_SUBAGENT=1`，除非已經由 server-side rollout 啟用。由 Claude 自行 spawn fork 這件事仍被標示為 experimental，未來可能變更，也可能在 interactive session 中分階段 rollout。上述環境變數是來源文件所述的控制方式，實際套用前仍應依使用中的 Claude Code 版本與官方文件核對。

**fork session 的互動模型**　fork 會在 prompt 下方的 panel 以 background 工作顯示。使用者可以：

- 使用 `↑`／`↓` 在不同工作列之間切換。
- 按 `Enter` 開啟 fork 的 transcript，查看執行內容或傳送 follow-up。
- 按 `x` 停止仍在執行的 fork，或關閉已完成的 fork。
- 按 `Esc` 回到主 prompt。

開啟 fork transcript 後，follow-up 與 skills 會送往目前檢視的 agent；不過 built-in commands 仍作用於主 conversation。引用資料特別提醒，在該畫面輸入 `/model` 或 `/fast` 時，Claude Code 自 v2.1.199 起會明確說明這些設定改變的是主 conversation，而不是靜默套用到正在查看的 fork。這反映出 transcript 檢視畫面雖然能操作 fork，卻不等於使用者已經切換主 session。

**何時使用 subagent**　Claude Code 的 custom subagents 是具備專用 system prompt、獨立 context window、工具存取權與 permissions 的 AI assistants。它們適合把高輸出的 side task 移出主對話，例如搜尋大量檔案、處理 verbose logs、擷取文件內容或執行測試，最後只將摘要帶回主流程，藉此保留主對話的 context、限制工具權限並重用設定。

典型適用情境包括：

- 讓 read-only Explore agent 搜尋與分析 codebase，避免主對話被大量 search results 佔滿。
- 讓 code reviewer 檢查最近的變更，只授予 `Read`、`Grep`、`Glob`、`Bash` 等必要工具。
- 讓 debugger 依序擷取 error 或 stack trace、重現問題、定位 root cause、提出最小修復並驗證。
- 將 authentication、database、API modules 等互不依賴的 investigation 平行執行，再由 Claude 整合 findings。
- 將 documentation fetch、測試輸出或 log processing 留在 subagent context，只把相關摘要傳回主對話。
- 透過 `isolation: worktree`，讓需要修改程式碼的 subagent 在獨立 git worktree 中執行，避免直接改動主要 checkout。

相反地，若任務需要頻繁與使用者往返、必須共享大量即時上下文、只是快速修改，或最重視 latency，引用文件建議留在 main conversation。若要重用 prompt 或 workflow，但仍希望工作發生在 main context，則可考慮使用 skills；若需要長時間平行工作或工作量超過單一 context window，則可考慮 agent teams。

**custom subagent 的配置方式**　custom subagent 是包含 YAML frontmatter 的 Markdown file，必要欄位為 `name` 與 `description`。從 v2.1.198 起，`/agents` 不再提供 interactive creation wizard，而是提示使用者透過 Claude 建立，或直接編輯 `.claude/agents/`。例如，來源文件要求建立一個個人層級、唯讀、使用 Sonnet 的 `code-improver` subagent：

```text
Create a personal code-improver subagent in ~/.claude/agents/ that scans
files and suggests improvements for readability, performance, and best
practices. It should explain each issue, show the current code, and
provide an improved version. Make it read-only and have it use Sonnet.
```

![](https://pub-75d4fe1e4e80421b9ecb1245a7ae0d1a.r2.dev/curated/b8199fa4b8ee89d9.png)
> Claude Code Docs 的 Create custom subagents 文件標題區塊，顯示主題為 Agents and parallel work、副標題為 Create custom subagents 以及簡短的導言文字。

接著應確認產生的 `~/.claude/agents/code-improver.md` 內容：

```markdown
---
name: code-improver
description: Scans files and suggests improvements for readability, performance, and best practices. Use after writing or modifying code.
tools: Read, Grep, Glob
model: sonnet
---

You are a code improvement specialist. For each issue you find, explain
the problem, show the current code, and provide an improved version.
```

`~/.claude/agents/` 的 user subagent 可供所有 projects 使用；若只供單一專案使用，則放在 `.claude/agents/`，並可納入 version control 與團隊共享。實驗或建立後可用以下文字測試：

```text
Use the code-improver agent to suggest improvements in this project
```

若找不到新 agent，可重啟 Claude Code；在新 `agents` directory 中建立該 scope 的第一個 agent file 時，也必須重啟，因為 watcher 只涵蓋 session 啟動時已存在的 directories。除此之外，Claude Code 通常會在新增或編輯 agent file 後幾秒內偵測變更，下一次 delegation 即使用更新後的定義。

**scope 與命名管理**　subagent 定義可存在於多個層級，優先序由高到低如下：

- Managed settings：organization-wide，優先序最高。
- `--agents` CLI flag：只存在於目前 session。
- `.claude/agents/`：目前 project。
- `~/.claude/agents/`：所有 projects。
- plugin 的 `agents/`：在 plugin 啟用時提供，優先序最低。

若名稱相同，較高優先序的定義會勝出；自 v2.1.178 起，巢狀 project directories 出現同名定義時，會採用距離目前 working directory 最近的版本。`.claude/agents/` 會從 current working directory 向上掃描至 repository root，`--add-dir` 加入的 directory 也會掃描其中的 `.claude/agents/`。

subagent 的 `name` 必須是由小寫字母與 hyphen 組成的唯一識別碼，且不能含 `:`；冒號保留給 plugin-scoped identifier，例如 `my-plugin:review:security`。Claude Code v2.1.218 起不再載入含冒號的檔案並寫入 debug log；plugin 的 scoped name 則是例外，會由 plugin 系統產生。`.claude/agents/` 整棵目錄及其子資料夾中的 `name` 應保持唯一，否則同一目錄出現重複名稱時，Claude Code 只會依 filesystem read order 載入其中一個，且沒有 documented precedence。`/doctor` 可回報重複名稱並建議重新命名或移除多餘定義。

**工具、權限與安全邊界**　subagent 可透過 `tools` allowlist 或 `disallowedTools` denylist 限制能力；若兩者皆設定，會先套用 `disallowedTools`，再從剩餘工具中解析 `tools`。例如唯讀研究 agent 可以使用：

```yaml
---
name: safe-researcher
description: Research agent with restricted capabilities
tools: Read, Grep, Glob, Bash
---
```

也可以用下列設定繼承可用工具，但禁止寫入檔案：

```yaml
---
name: no-writes
description: Inherits the available tools except file writes
disallowedTools: Write, Edit
---
```

若完全省略 `Agent`，該 subagent 不能再 spawn 其他 subagent；若允許 `Agent`，仍會受 nesting depth 限制。主執行緒也可透過 `Agent(agent_type)` 限制可建立的 subagent 類型，例如：

```yaml
---
name: coordinator
description: Coordinates work across specialized agents
tools: Agent(worker, researcher), Read, Bash
---
```

權限模式可設定為 `default`、`acceptEdits`、`auto`、`dontAsk`、`bypassPermissions` 或 `plan`。其中 `bypassPermissions` 會跳過部分 permission prompts，具有高風險，可能未經批准寫入 `.git`、`.config/git`、`.claude`、`.vscode`、`.idea`、`.husky`、`.cargo`、`.devcontainer`、`.yarn`、`.mvn` 等受保護位置；來源文件明確提醒，這不是應該無條件啟用的安全設定。使用者也可在設定中禁止特定 subagent：

```json
{
  "permissions": {
    "deny": ["Agent(Explore)", "Agent(my-custom-agent)"]
  }
}
```

或使用 CLI：

```bash
claude --disallowedTools "Agent(Explore)"
```

fork 與 named subagent 的權限提示位置也不同：fork 的權限 prompt 會出現在 terminal；background named subagent 的權限提示則會回到主 session。這是操作上需要特別留意的差異，因為使用者若只觀察主 session，可能把 fork 的獨立執行誤認為一般 background delegation。

**MCP、skills 與記憶**　subagent frontmatter 可指定 `mcpServers`，讓特定 MCP server 只提供給該 subagent；inline server 會在啟動時連線、結束時斷線，字串引用的 server 則與 parent session 共用。範例設定如下：

```yaml
mcpServers:
  - playwright:
      type: stdio
      command: npx
      args: ["-y", "@playwright/mcp@latest"]
  - github
```

主 session 的 MCP 限制也會套用到 subagent，包括 `--strict-mcp-config`、`--bare`、Enterprise managed MCP configuration，以及 `allowedMcpServers`／`deniedMcpServers`。不過來源文件指出，`--strict-mcp-config` 不會過濾透過 `--agents` 或 Agent SDK 明確傳入的 inline server；這類設定需要由管理者與開發者人工核對其權限範圍。

`skills` 欄位會在 subagent 啟動時注入完整 skill 內容，而不只是 description；它控制的是 preload，不是限制 subagent 能存取的 skills。只要保留 `Skill` tool，subagent 仍可自行 discover 或 invoke project、user、plugin skills。記憶則可設定為 `user`、`project` 或 `local`，分別使用 `~/.claude/agent-memory/<name-of-agent>/`、`.claude/agent-memory/<name-of-agent>/` 與 `.claude/agent-memory-local/<name-of-agent>/`。其中 `project` 是文件推薦的預設選擇，便於透過 version control 分享。

**hooks 與工作流程控制**　subagent 可在 lifecycle 中執行 hooks。`PreToolUse`、`PostToolUse` 可套用到 subagent 的 tool calls；`SubagentStart`、`SubagentStop` 則可監控其開始與結束。project-level subagent 的 frontmatter hooks 必須先接受包含 agent file 資料夾的 workspace trust dialog；若未信任，subagent 仍可能執行，但 Claude Code 會跳過 frontmatter hooks 並將錯誤寫入 debug log。

來源文件以 database query validator 說明如何透過 `PreToolUse` 限制 Bash 只能執行 read-only SQL。macOS/Linux 需要先讓腳本具備 executable 權限：

```bash
chmod +x ./scripts/validate-readonly-query.sh
```

腳本會從 stdin 讀取 JSON，擷取 `.tool_input.command`，若命令含有寫入或結構變更 SQL 關鍵字，就輸出錯誤並以 exit code `2` 阻擋：

```bash
#!/bin/bash
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
if echo "$COMMAND" | grep -iE '\b(INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|TRUNCATE)\b' > /dev/null; then
  echo "Blocked: Only SELECT queries are allowed" >&2
  exit 2
fi
exit 0
```

這段設定是來源所述的權限控制範例，不代表可以忽略 workspace trust、MCP policy、shell 權限或腳本內容的人工審查；Windows 則應改用 PowerShell，並在 hook entry 加上 `shell: powershell`。

**版本演進與容量限制**　subagent 的行為會隨版本快速變化，使用者不應只依賴舊文章或過往操作習慣。引用資料列出幾項重要版本差異：

- 自 v2.1.198 起，background subagent 成為預設行為；Explore 也不再固定使用 Haiku，而是自 v2.1.198 起繼承主 model（在 Claude API 上，繼承的 model 上限為 Opus）。
- 自 v2.1.199 起，API error 會正確回報為 subagent failure，而不是把錯誤文字偽裝成 findings。
- 自 v2.1.203 起，`isolation: worktree` 的 main checkout 安全檢查修正了部分錯誤行為。
- 自 v2.1.205 起，`--append-subagent-system-prompt` 可在 non-interactive mode 為每個 subagent 附加 system prompt。
- 自 v2.1.210 起，Claude Code 會在讀取 subagent final report 前掃描可能模仿 `<system-reminder>`、`Human:` 或 `Assistant:` 的內容，但這不是惡意內容判斷，也不能取代 permission checks 與 sandboxing。
- 自 v2.1.211 起，單次 invocation 指定的 model 在 resume 或 follow-up 時仍有效。
- 自 v2.1.212 起，`/subtask` 可用於 fork subagent 的舊式互動流程，每個 session 預設最多可 spawn 200 個 subagents。
- 自 v2.1.217 起，可用 `CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS` 調整同時執行的 subagent 數量；預設同時執行 20 個後會觸發 `Concurrent subagent limit reached`；啟用 ultracode 的 session 不受此限制。
- v2.1.219 起，subagent 預設最多向下 spawn 3 層；可用 `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` 調整。

若要把 nesting depth 限制為兩層，可在設定中使用：

```json
{
  "env": {
    "CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH": "2"
  }
}
```

每個 session 的 subagent 數量、同時執行數、nested subagent 深度與 fork session budget 並非同一項限制。`/subtask` 會消耗相同的 session budget，但不受 Agent spawn limit 阻擋；`/fork` 建立的獨立 background session 則有自己的 budget。使用者若在大型專案中大量平行啟動工作，應同時留意這些限制，而不能只看單一 agent 是否已完成。

**對 Agentic 程式開發的意義**　Lydia Hallie 的貼文雖然只聚焦一個 slash command 的重新命名與行為變更，但背後反映 Claude Code 正在把「平行工作」拆成更清楚的互動模型：`/subtask` 側重同一對話中的 delegation 與結果回傳，`/fork` 側重保留完整上下文的背景探索，`/branch` 則側重使用者主動切換到另一條對話路線。

這種區分可降低使用者對 session 狀態的誤判。過去把 `/fork` 理解成會回報的 in-session subagent，可能導致使用者期待主對話稍後收到結論；新版語意則明確要求使用者把「要不要回報」與「要不要切換 session」分開思考。對需要同時嘗試多種實作方案、從同一起點進行測試、或把高輸出任務移到背景的工作流程而言，這提供更細緻的控制；但若任務的價值在於立即取得結果並接續下一步，使用 `/subtask` 才符合原本的互動期待。

整體而言，這次變更不是單純替 slash command 改名，而是重新劃分 Claude Code 中 context inheritance、background execution、result reporting 與 conversation switching 的責任。使用者在 v2.1.212+ 應先判斷工作是否需要繼承目前上下文、是否需要回報主 session，以及是否要切換互動焦點，再在 `/fork`、`/subtask` 與 `/branch` 之間選擇。

## 標籤

Claude Code, CLI, 功能更新, Anthropic
