本文朗讀為 AI 合成語音

想讓 Claude 讀完一批檔案之後,被提醒一次「這幾個是 ledger 模組,收工前跑 pytest」。提醒只要一次,不要每個檔案各來一輪。

直覺會掛在 PostToolUse:它在每個工具跑完之後觸發,Read 完就能塞一句話進去。麻煩在於 Claude 會一次平行叫好幾個 Read,「讀完一批」到底是哪一個 Read 之後?

兩次簽收,一次對帳

收貨櫃檯一次進來三箱貨。每箱各自簽收,簽三次,那是 PostToolUse。三箱全到齊之後,櫃檯才對一次帳,只對一次,那是 PostToolBatch。

官方 hooks 文件把這個差別寫得很直接。事件表裡的定位是「After a full batch of parallel tool calls resolves, before the next model call」,詳細段落則寫:

PostToolUse fires once per tool, which means it fires concurrently when Claude makes parallel tool calls. PostToolBatch fires exactly once with the full batch, so it is the right place to inject context that depends on the set of tools that ran rather than on any single tool.

這段我沒打算只靠讀文件就信,所以在本機跑了一次。環境是 Claude Code 2.1.335,用 -p 模式、haiku 模型,設定檔用 --settings 指到獨立的一份,沒動專案或全域設定。同一支腳本同時掛在 PostToolUse 與 PostToolBatch 上,它做兩件事:把收到的事件寫進一個 log,以及在 PostToolBatch 時回傳一段 additionalContext。

設定長這樣:

1
2
3
4
5
6
{
"hooks": {
"PostToolUse": [{"hooks": [{"type": "command", "command": "python3 /你的路徑/ptb-hook.py"}]}],
"PostToolBatch": [{"hooks": [{"type": "command", "command": "python3 /你的路徑/ptb-hook.py"}]}]
}
}

腳本(路徑換成你自己的):

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys, json
d = json.loads(sys.stdin.read())
ev = d.get("hook_event_name")
if ev == "PostToolBatch":
summary = [(c["tool_name"], c["tool_use_id"][-6:], len(str(c.get("tool_response", "")))) for c in d.get("tool_calls", [])]
line = "PostToolBatch calls=%d %s" % (len(summary), summary)
else:
line = "%s tool=%s id=%s" % (ev, d.get("tool_name"), str(d.get("tool_use_id"))[-6:])
with open("/你的路徑/ptb-hook-log.txt", "a") as f:
f.write(line + "\n")
if ev == "PostToolBatch":
print(json.dumps({"hookSpecificOutput": {"hookEventName": "PostToolBatch", "additionalContext": "BATCH-MARKER-7731: 本批共 %d 個工具呼叫" % len(summary)}}))

提示詞要求 Claude 在同一個回合裡平行呼叫 Read 兩次,各讀一個小檔案,讀完之後把含 BATCH-MARKER 的文字原樣告訴我,沒有就回 NONE。log 裡留下的是:

1
2
3
PostToolUse tool=Read id=LXXrza
PostToolUse tool=Read id=K1tz8w
PostToolBatch calls=2 [('Read', 'LXXrza', 16), ('Read', 'K1tz8w', 16)]

兩行 PostToolUse,一行 PostToolBatch,tool_calls 長度是 2,兩個 id 跟前面兩行對得上。模型也確實回報看得到「BATCH-MARKER-7731: 本批共 2 個工具呼叫」,並說那段文字出現在 hook 的附加內容裡,不在檔案內容中。

回到開頭那題。如果我把提醒掛在 PostToolUse 上,從這份 log 推,兩個 Read 會各觸發一次。這是推論:我沒有真的在 PostToolUse 回傳提醒文字去數 Claude 收到幾次。

回傳的東西長得不一樣

接著是文件裡很容易漏看的一段。tool_calls 每一項都有 tool_name、tool_input、tool_use_id、tool_response,而 tool_response 的說明是:

tool_response contains the same content the model receives in the corresponding tool_result block. The value is a serialized string or content-block array, exactly as the tool emitted it. For Read, that means line-number-prefixed text rather than raw file contents. Responses can be large, so parse only the fields you need.

文件還特別補了一個 NOTE,說 tool_response 的形狀跟 PostToolUse 不同:PostToolUse 傳的是工具結構化的 Output 物件,例如 Write 的 {filePath: "...", type: "create"};PostToolBatch 傳的是模型看到的序列化 tool_result 內容。

在我那次實跑裡,兩個 Read 的 tool_response 長度各是 16 個字元。我只量了長度,沒逐字印出內容,所以它實際長什麼樣我不知道,文件說是帶行號前綴的文字。要是你的腳本習慣用 PostToolUse 時 tool_response["filePath"] 那種取法,照文件推,搬到 PostToolBatch 上會踩空。這同樣是從文件推的,我沒試過。

這篇的實測範圍很窄:Claude Code 2.1.335,-p 模式,haiku,平行 2 個 Read。以下這幾種我沒跑過:exit 2 或 decision: "block" 的實際行為、prompt 與 agent 型 hook、被拒絕的呼叫會不會進入 batch、只有單一工具呼叫時 PostToolBatch 還會不會觸發。文件只寫「after every tool call in a batch」,沒有明講單一工具的情況。

注入的字串怎麼送到 Claude 手上

additionalContext 的送達方式,文件講得比我預期的細。Claude Code 把字串包進 system reminder,插在 hook 觸發的位置,Claude 在下一次模型請求時讀到,但不會出現在介面的對話裡。PostToolBatch 屬於「next to the tool result」那一類。幾個條件:

  • 多個 hook 對同一個事件都回 additionalContext,Claude 全部收到。
  • 字串裡若含 <system-reminder> 或 </system-reminder> 標籤,那個標籤的 < 會被換成 &lt;。
  • 超過 10,000 字元時,Claude Code 會把全文寫成 session 目錄下的檔案,只把檔案路徑加上最多前 2,000 字元的預覽交給 Claude。

最後一條對 PostToolBatch 特別有影響,因為這個事件的設計目的就是處理「一整批」,把一批 tool_response 全塞進注入字串,字數可能不少,文件也提醒 responses can be large。我沒有測過超長的情況,這是從文件條文讀來的。

這個事件沒有 matcher。文件的 matcher 表把 UserPromptSubmit、PostToolBatch、Stop 等歸在「no matcher support」,「always fires on every occurrence」。想只對某些工具反應,得在腳本裡自己看 tool_name。

exit 2 停得住它,停不住 PostToolUse

我想把這一段單獨拉出來,因為它改變 hook 能做的事。文件的 exit code 表裡,PostToolUse 那列是「No / Shows stderr to Claude; the tool already ran」,PostToolBatch 那列是「Yes / Stops the agentic loop before the next model call」。

PostToolBatch 回傳 decision: "block" 或 continue: false,會在下一次模型呼叫之前停掉 agentic loop,擋下的訊息來自 JSON 的 reason 或 stopReason,或 exit 2 時的 stderr,訊息會留在對話裡,Claude 在對話繼續時看得到。

另外 prompt 與 agent 型 hook 在 PostToolBatch 上也可以用,文件列的是「all five hook types」。它們回 ok: false 時,文件的說法是 PostToolBatch、UserPromptSubmit、UserPromptExpansion 的回合會結束,理由以警告行顯示,而且這幾個事件在 decision: "block" 時就會結束回合,不看 continue。這句跟前一段講 continue: false 的措辭並不完全一致,我兩處照抄,不替文件調和。

這一整段都是讀文件。我沒有讓 PostToolBatch exit 2 去看循環是怎麼停的。

被拒絕的呼叫

changelog 2.1.328 裡有一句跟這個事件有關:「Changed managed PostToolUse hooks to no longer fire for calls refused before they ran: no log entry, rewrite, block or stop there; PostToolBatch gets most, not marked as refused」。

後半句的 “gets most” 語意含糊,我讀不出「大部分」指什麼,所以不推論。我能確認的版本資訊也只有這麼多:changelog 全文搜尋 PostToolBatch,只在 2.1.328 這一處找到,查不到它是哪個版本首次出現,所以我只敢說它最早在 2.1.328 的說明中被提到,不敢寫成「2.1.328 新增」。

上面那幾種沒跑過的,補法其實不貴:沿用文中的設定與腳本,路徑換成自己的,提示詞改成只呼叫一次 Read,看 log 有沒有出現 PostToolBatch 那一行,比任何推論都準。

我的立場,以及什麼會讓我改變

提醒、檢查、注入的內容取決於「這一批動了哪些東西」,掛 PostToolBatch。內容只取決於單一工具的結果,或需要工具原本結構化的輸出(像 Write 的 filePath),就留在 PostToolUse。

我手上的證據只有那一個實驗:2 個平行 Read,2 次與 1 次。如果之後發現單一工具呼叫時 PostToolBatch 不觸發,我會把「一律掛 PostToolBatch」改成「平行與單一兩種情況各掛一個」,因為這會讓只有一個 Read 的回合漏掉提醒。如果有人實測被拒的呼叫確實會以某種形式混進 tool_calls,那「提醒內容依 tool_name 計數」的寫法也得加防護。這兩件事,我現在都只能說「不知道」。

原文來源:Hooks reference - Claude Code Docs、Claude Code CHANGELOG