confluence-partners.ai ↗

模組 5 · 進階考點

全考綱補全模組:subagent 委派與診斷/session 管理/batch 與 review pass/MCP 補遺/錯誤回應設計 | 讀完 → 配套 Drill(快檢 10 + 情境 30)

5.1 Subagent 委派策略與 spawn 診斷

委派光譜:procedural(程序式)vs goal-oriented(目標式)

Procedural 指令Goal-oriented 指令
形式逐步腳本(先做 A、再做 B、輸出 C)目標 + 邊界,怎麼做交給 subagent
行為可預測、一致(≈ workflow 思維)adaptive——依中間發現調整做法(≈ agent 思維)
適用well-defined、步驟可事先寫死open-ended、無法預測需要幾步
代價彈性低,遇到沒寫到的情況就卡成本較高 + compounding errors 風險
官方立場是中間解,不是二選一:委派內容必含四件套(objective、輸出格式、工具/來源指引、任務邊界)——定義「什麼算完成」;但逐步規定怎麼執行——那部分用 heuristics 引導(effort scaling、start wide then narrow),讓 subagent 保有 adaptive 空間。原句:"instilling good heuristics rather than rigid rules"
最大陷阱:goal-oriented ≠ 指令越短越好。官方實測反例:只給 "research the semiconductor shortage" 這種一句話目標 → subagent 誤解任務、和別的 agent 做重複搜尋("duplicate work, leave gaps, or fail to find necessary information")。詳細的任務描述(目標+邊界)≠ 詳細的步驟腳本——考題誘答常把兩者混為一談。

Coordinator 保住可見性與控制的手段(選 goal-oriented 不等於放手):stopping conditions / max iterations、checkpoint 暫停等人核准、顯示 planning steps(transparency)、production tracing;驗收看 end state 不看過程——官方原句:"Instead of judging whether the agent followed a specific process, evaluate whether it achieved the correct final state."

約束三件套(#5):tools / context / system prompt

約束面規格考點鑰匙
Tool 限制tools = allowlist,省略 = 繼承全部disallowedTools = denylist,兩者並存時 denylist 先套用「省略 tools 最安全」= 反向陷阱;唯讀審查 = Read/Grep/Glob
Context scopingsubagent 是 fresh context:看不到主對話歷史、已讀檔案;父傳子只有 Agent tool 的 prompt string;子回父只有 final message(中間過程不回流)檔案路徑/錯誤訊息/決策要明寫進 prompt;唯一例外 = fork(繼承整段對話)
System prompt檔案版 = markdown body;SDK = prompt 欄。description 是給協調者決定「何時委派」用的,不是給 subagent 的角色說明description(何時用它)vs prompt(它是誰)分工——對調 = 經典誘答

Spawn 診斷(#6):三大失因 checklist

subagent 沒 spawn / 沒被委派,照序檢查:
① 權限面:coordinator 的 allowedTools 含 Agent 嗎?
   (SDK:缺了 → 呼叫 fall through 到 canUseTool;dontAsk mode → 直接 deny)
   (Task 是 Agent 的舊名,settings 裡寫 Task(...) 仍有效)
② 定義面:AgentDefinition 參數對嗎?
   SDK 必填 description + prompt|檔案版必填 name + description
   tools 全部拼錯 → 直接拒絕啟動並回報錯誤(不是無工具啟動)
   Python:欄位要 camelCase(disallowedTools)——snake_case 直接 TypeError
③ 佈線面:定義有被載入嗎?描述夠清楚嗎?
   新建 agents/ 目錄 → 要重啟 session(最常見原因)
   同名優先序:managed > --agents CLI > project > user > plugin;SDK 程式定義蓋過同名檔案
   SDK 設了 settingSources: [] → .claude/agents/ 完全不載入
   description 太模糊 → Claude 不委派(正解:寫清楚何時用 + "use proactively")
辨識訊號:「定義了卻不委派」→ 先查 allowedTools、再查 prompt 有沒有指名、最後改寫 description(官方 troubleshooting 的順序);「@-mention 指名」才保證執行,自然語言只是「通常會委派」。

5.2 Session 管理與輸出 schema

Session 三選一(#2)

手段機制何時用
Continue--continue:載入當前目錄最近一個 session,不用追 ID單人單線、剛剛才離開
Resume--resume 指定 session ID/name 續同一段對話(state 原封)多 session 場景(每個使用者/任務一條);跑到 max-turns/budget 上限 → 調高再 resume
Forkfork_session / --fork-session(搭配 resume/continue):複製對話史開新分支,原 session 不動同一份前置分析要試多個方向——不重跑上游
新 session + summary開新對話、把結構化 summary(發現/決策/檔案 diff)塞進第一個 prompt——官方:"Don't rely on session resume... This is often more robust"prior tool result 已 stale(檔案 ID 全變、資料過期)、跨機器交接
陷阱三連:① session 持久化的是對話、不是檔案系統(官方原句 "Sessions persist the conversation, not the filesystem")——fork 出來的 agent 改檔案,所有 session 都看得到;檔案回滾靠 checkpointing。② --session-id 是給對話指定 UUID,不是 resume 用的。③ resume 拿到全新空 session 的最常見原因 = cwd 不符(session 綁工作目錄存放)。
改過檔案後的 targeted re-analysis:session 進行中檔案被外部改動(隊友 force-push、generator 重跑)→ 告知 agent 哪些檔變了 + 只 re-read 那些檔,不重跑整個探索、也不假裝沒事。Claude Code 只在 Edit 時偵測 stale 並報錯,不會自動刷新 context——「它會自己發現」是假機制。

Subagent 輸出 schema 設計(#18)與 synthesis 不確定性(#19)

陷阱:「讓 synthesis agent 挑最可信的那個值」= 把衝突塌縮成單一自信陳述,正是 objective 點名的反模式。正解永遠是「並陳 + 標註 + 讓讀者看得到分歧」。

5.3 Batch API 與 review pass 設計

同步 vs 非同步(#23)

Messages API(同步)Message Batches API(非同步)
延遲秒級回應多數 <1 小時,上限 24 小時、無 latency SLA
成本標準價50% 折扣
適用阻塞流程(pre-merge check、互動)非阻塞、可容忍延遲(隔夜報告、週 audit、大量回填)
機制request/responsecustom_id 對應請求與結果;結果順序不保證;部分失敗只重送失敗項(單一 request 失敗不影響其他);不支援單一請求內 multi-turn tool calling、不支援 stream: true
規模/保留上限 100,000 requests 或 256 MB(先到為準);結果保留 29 天(從 created_at 起算);過期未完成項標 expired、不計費、需重送;追蹤靠輪詢或 Console
陷阱:「batch 比較便宜所以全部改 batch」——擋 merge 的檢查走 batch = PR 卡 24 小時。選型鑰匙是阻塞行為,不是成本。反向:「結果照提交順序回來」= 官方明文否定的迷思,對應要靠 custom_id

Review pass 拆分(#24)

準則邊界與 extraction 一致性(#31、#29)

5.4 MCP 與 built-in tools 補遺

Skill 隔離與工具限制(#14)

context: fork(SKILL.md frontmatter):skill 在隔離的 subagent context 執行——冗長中間輸出不污染主對話 session state。工具語意最容易考反allowed-tools 是「授權免確認」(grants permission... It does not restrict which tools are available);要限制disallowed-tools(把工具移出可用池),或 fork + 唯讀 agent type(agent: Explore)。

Built-in tools 選型(#32)

工具本質用在
Grep搜檔案內容(regex)「誰定義/呼叫 X」「error 字串在哪」——找寫在檔案裡的東西
Glob配檔名/路徑 pattern(**/*.test.ts),永不看內文「某類檔案在哪」
Read讀已定位的檔Grep/Glob 找到後讀內容
Bash跑指令git log、跑測試——不是拿來 cat 全部檔案搜內容
Edit / WriteEdit 精準改(唯一 anchor);找不到唯一 anchor → Read + Write 整檔 fallback目標字串出現多次 → fallback

MCP resources vs tools(#34)

ToolsResourcesPrompts
控制者Model(模型自己決定何時呼叫)Application(application-driven:host/使用者決定何時注入)User
性質可執行動作(寫 DB、呼叫 API、改檔)被動、唯讀 context(檔案內容、DB schema、文件)指令模板
識別nameURIresources/list 列目錄、resources/read 取內容;支援參數化 template)name

設計判準:跨系統查詢的「內容目錄型資料」(issue 清單、DB schema、文件層級)→ 曝露為 resources 讓 client 直接列出/注入,省掉模型連環 tool call 摸索;會寫入/觸發動作的才做 tool。Claude Code 引用:@server:protocol://resource/path(如 @github:issue://123),引用後自動抓取當 attachment。

Tool description 寫作(#36)

陷阱:「description 寫短一點省 token」——官方立場相反(詳細描述是第一優先);token 要省在 response 內容工具數量。「用 input_examples 解決選錯工具」也錯位:examples 示範「怎麼填參數」,「何時用這個工具」永遠靠 description。

5.5 Tool 錯誤回應設計

結構化錯誤三件套:isError flag + 錯誤分類(如 errorCategory)+ 可否重試(如 isRetryable)+ 人類可讀說明。絕不回統一的 "Operation failed"——agent 拿到通用錯誤字串就無法決定「重試、修輸入、還是換路徑」。註:errorCategory/isRetryable 是設計 pattern 的自訂欄位,MCP spec 本身只定義 isError
錯誤類別例子Agent 該做的事
Transienttimeout、上游暫時掛掉可 retry(isRetryable: true)
Validation輸入格式錯、缺參數修正輸入再試——原樣 retry 沒有用
Business政策違反(發票總額不符、超退款上限)絕不 retry——永遠會失敗,走專屬處理路徑/升級
Permission憑證過期、權限不足升級處理;絕不能偽裝成空結果

討論題(讀書會 session 用)

  1. 「research the semiconductor shortage」這個委派指令問題在哪?把它改寫成合格的四件套版本。
  2. 你的 subagent 定義了卻從來沒被委派過——照 5.1 的三面 checklist 走一遍,每一步你會檢查哪個檔案/參數?
  3. 兩個可信來源對同一數字給出不同值——synthesis agent 的正確輸出長什麼樣?為什麼「挑一個」是反模式?
  4. 你們公司哪些內部查核適合搬去 Batch API?哪些絕對不行?判準一句話。
  5. 挑一個你實際用過的 MCP server:它的 tool description 有沒有寫「何時不該用我」?resources 和 tools 的分工合理嗎?