### 总体概览
此次文档更新主要集中在优化非交互模式下的工具延迟与恢复机制,新增了针对插件通知的 Hook 支持,并完善了 MCP 工具加载行为及 API 缓存错误的诊断说明。同时包含若干重要的 Bug 修复,涉及云会话的登录状态及多 MCP 工具场景下的错误处理。
### 核心主题
* **Hook 与工具编排增强**:大幅细化了 `PreToolUse` Hook 中 `"defer"` 决策的行为描述。明确指出 `defer` 是结束当前“轮次”而非直接退出进程,并增加了恢复会话时若工具不可用(如 MCP 服务器断开)的处理逻辑(返回 `tool_deferred_unavailable`)。
* **插件生态系统扩展**:在 `Notification` Hook 类型中新增了 `plugin_notification`,允许 Mods 通过 `$.ui.notify` 触发通知事件。
* **MCP 与工具搜索逻辑澄清**:详细解释了当 `ENABLE_TOOL_SEARCH` 设置为 `auto` 阈值模式时,工具为何会在定义总大小低于阈值时仍保持延迟加载的状态。
* **API 诊断可观测性**:为 Messages API 中的各类 `cache_miss_reason`(如 `model_changed`, `tools_changed` 等)补充了详细描述和外部文档链接,便于调试缓存失效问题。
* **Bug 修复**:修复了在云会话中使用 `claude -p` 时因环境变量缺失而误报“Not logged in”的问题,以及修复了在 MCP 工具较多时 API 错误被错误替换为“prompt is too long”的缺陷。
### 影响等级:中等
**理由**:虽然大部分变动属于文档澄清,但对于构建基于 Agent SDK 的复杂封装或非交互式自动化工作流的开发者来说,`defer` 机制的语义变化及错误处理逻辑更新至关重要。插件通知的 Hook 也为扩展开发提供了新能力。
### 开发者行动项
1. **检查 Defer 处理逻辑**:如果你的应用使用了 `permissionDecision: "defer"`,请务必更新代码以处理新的 `stop_reason: "tool_deferred_unavailable"` 状态,并理解恢复时发送 Prompt 与不发送 Prompt 的区别。
2. **适配新 Hook 类型**:在 Hook 监听器中增加对 `plugin_notification` 类型的处理,以支持插件的 UI 通知功能。
3. **审查 MCP 工具加载配置**:如果你的环境依赖 `ENABLE_TOOL_SEARCH=auto`,请查阅更新后的文档,理解工具在会话恢复或重连时可能保持延迟加载的行为。
4. **利用缓存诊断信息**:在调试 API 缓存命中率时,参考新增的 `cache_miss_reason` 详细说明来优化 Prompt 构建策略。
5. **更新环境变量配置**:针对云会话或无写入权限目录下的沙盒配置,参考文档中的 `~/.claude/settings.json` 配置方案进行修正。
💭 Model Reasoning
1. **Analyze the Request**:
* **Role**: World-class programmer analyzing documentation changes.
* **Input**: 12 pages of documentation diffs (hooks, hooks-guide, mcp, model-config, sandboxing, setup, api/messages, api/models/list, api/beta/files/upload, claude-in-microsoft-foundry, mcp-connector, CHANGELOG).
* **Task**: Summarize what changed, why it matters, key themes, impact level, and action items.
* **Format**: Markdown.
* **Language**: Chinese.
2. **Process the Diffs (Page by Page Analysis)**:
* **Page: `hooks.md`**:
* *Change 1*: Added `plugin_notification` to the `Notification` hook types.
* *Change 2*: Updated `permissionDecision` for `"defer"`. Now clarifies that it ends the turn so the tool can be resumed later, and requires the `-p` flag (non-interactive).
* *Change 3*: Refined the `PreToolUse` -> `defer` flow description.
* *Change 4*: Added handling for when a deferred tool is unavailable upon resume (`tool_deferred_unavailable`). Clarified behavior when sending prompts vs. no prompts during resume.
* *Theme*: Hook extensibility (plugins), Tool deferral/resume logic refinements.
* **Page: `hooks-guide.md`**:
* *Change 1*: Added `plugin_notification` description (Mod raising notifications).
* *Change 2*: Updated description of `"defer"` to mention it ends the turn (not just "exits the process").
* *Change 3*: Added `plugin_notification` to the notification types table.
* *Theme*: Consistency with `hooks.md`.
* **Page: `mcp.md`**:
* *Change 1*: Clarified `ENABLE_TOOL_SEARCH=auto` behavior. Specifically, tools stay deferred below the threshold if tool search was already on.
* *Theme*: Tool loading and MCP configuration clarity.
* **Page: `model-config.md`**:
* *Change 1*: Clarified warnings for `maxEffortLevel` caps. Warnings go to stderr for `--print`, debug log otherwise.
* *Change 2*: Minor text correction regarding 200K limit warnings.
* *Theme*: Effort level cap warnings/logging.
* **Page: `sandboxing.md`**:
* *Change 1*: Added a workaround for when no writable project folder is available. Suggests enabling sandbox via `~/.claude/settings.json` user settings.
* *Theme*: Sandbox configuration edge cases.
* **Page: `setup.md`**:
* *Change 1*: Removed duplicate `theme={null}` attributes from code blocks (cosmetic cleanup).
* *Theme*: Documentation hygiene.
* **Page: `api/messages.md`**:
* *Change 1*: Added detailed descriptions for various `cache_miss_reason` types (e.g., `model_changed`, `system_changed`, etc.) and linked to the cache diagnostics guide.
* *Theme*: API diagnostics and caching clarity.
* **Page: `api/models/list.md`** & `api/beta/files/upload.md`**:
* *Change 1*: Added `"webhooks-2026-10-07"` to headers.
* *Theme*: API versioning/headers update.
* **Page: `claude-in-microsoft-foundry.md`** & `agents-and-tools/mcp-connector.md`**:
* *Change 1*: Minor text corrections/formatting (e.g., Markdown code backticks in URL text).
* *Theme*: Documentation hygiene/formatting.
* **Page: `CHANGELOG.md.md`**:
* *Change 1*: Added entry for v2.1.306.
* *Fixes*: Fixed `claude -p` inside cloud sessions reporting "Not logged in". Fixed API error being replaced by "prompt is too long" in sessions with many MCP tools.
* *Theme*: Bug fixes.
3. **Synthesize and Structure**:
* **Overall Summary**: The changes focus on refining the "defer" mechanism for tools in non-interactive modes, adding hook support for plugin notifications, clarifying tool loading behaviors with MCP, and adding diagnostic details to the API cache miss reasons. There are also minor bug fixes and documentation cleanups.
* **Key Themes**:
* **Hook & Tool Orchestration**: Significant updates to how the `"defer"` permission decision works, specifically regarding tool unavailability during resume and the state of the turn (ending vs. exiting). Support for plugin notifications via hooks.
* **MCP & Tool Search**: Clarification on the `auto` threshold for tool loading and when tools stay deferred.
* **API Diagnostics**: Enhanced documentation for `cache_miss_reason` types in the Messages API.
* **Bug Fixes & Stability**: Fixes related to cloud sessions (`-p` flag) and error handling with many MCP tools.
* **Impact Level**: **Medium**.
* *Reasoning*: The changes to the `defer` logic and tool unavailability are critical for developers building advanced Agent SDK wrappers or non-interactive workflows. The plugin notification hook enables new integration capabilities. Other changes are informational or cosmetic.
* **Action Items**:
* Review Agent SDK implementations handling the `defer` logic to account for the new `tool_deferred_unavailable` state and the distinction between sending prompts vs. resuming without prompts.
* Update hook listeners to handle the new `plugin_notification` type if plugin integration is relevant.
* Check `tool_deferred_unavailable` handling in custom integrations.
* Consult the new cache diagnostics guide if implementing caching strategies with the API.
4. **Drafting the Response (in Chinese)**:
* *Summary*: 此次更新主要优化了非交互模式下的工具延迟机制,增加了插件通知的 Hook 支持,并明确了 MCP 工具加载逻辑及 API 缓存诊断细节。
* *Themes*:
* Hook 与工具编排:细化了 `defer` 权限决策的行为,特别是恢复会话时工具不可用的处理逻辑;新增 `plugin_notification` Hook 类型。
* MCP 与工具搜索:阐明了 `ENABLE_TOOL_SEARCH=auto` 阈值下的工具加载行为。
* API 诊断:为 Messages API 的 `cache_miss_reason` 类型补充了详细说明。
* Bug 修复:解决了云会话中 `claude -p` 的登录状态报错及多 MCP 工具场景下的错误处理问题。
* *Impact*: Medium (主要影响构建 Agent SDK 封装或高级自动化工作流的开发者)。
* *Actions*:
* 调整处理 `defer` 逻辑的代码,以应对新增的 `tool_deferred_unavailable` 状态。
* 在 Hook 监听器中添加对 `plugin_notification` 的处理。
* 参考新的缓存诊断指南优化 API 缓存策略。
5. **Refining Language**: Ensure technical terms are accurate (e.g., "defer", "non-interactive", "Agent SDK", "MCP").
6. **Final Polish**: (Self-correction: Ensure I mention the change from "process exits" to "turn ends" for defer, as that's a nuance).
7. **Final Output Generation**. (Matches the thought process above).