Files
claude-howto/zh/06-hooks/README.md
T
Luong NGUYEN 8aeb5a72dc docs: sync to Claude Code v2.1.278 (#183)
* docs: sync to Claude Code v2.1.278

Type: docs

Audit of the tutorial against Claude Code 2.1.278 (repo baseline was
2.1.263). Fixes claims the repo asserts that are now false; new-feature
coverage is English-only.

P0 - the repo stated the opposite of current behavior:
- AGENTS.md: the claude-md skill said Claude Code "does not read
  AGENTS.md directly" in all 5 languages. It has since v2.1.277, when no
  CLAUDE.md exists at or above the working directory.
- /output-style: listed as "Removed in v2.1.91" in 5 languages and in
  09-advanced-features. Re-added in v2.1.269; row moved out of the
  Deprecated Commands table.
- Permission rules: setup-auto-mode-permissions.py seeded Write(*),
  Glob(*) and NotebookEdit(*), which Claude Code accepts but never
  consults and warns about at startup. Removed; same fix in the
  config-examples.json profile and the 06/09 category tables.
- taskOutputMaxChars: documented as live. The TaskOutput tool was
  removed in v2.1.278 and the key is inert.

P1:
- Dynamic workflow "medium" guideline is fewer than 10 agents, not 15;
  the default is small on Pro plans since v2.1.271.
- Todo-tool availability restated as the official allowlist (Claude 3.x,
  Opus 4-4.7, Sonnet 4-4.6, Haiku 4.5) instead of a denylist; 23 sites
  across 5 languages.
- Memory hierarchy now notes the AGENTS.md fallback.
- Auto-mode classifier cost qualified: not charged on Enterprise plans
  and Claude API accounts since v2.1.278.

P3 (English only): full AGENTS.md section in 02-memory; omitClaudeMd
subagent field; plugin eval / --json / --accept-command / --marketplace;
maxEffortLevel, bashEditDiffEnabled, syncClaudeAi*, 4 env vars,
--system-prompt-snapshot, --plugin-dir folders; send-now key and Monitor
deadlines; checkpoints subagent-edit limitation.

Learning skills audited clean: 100 quiz questions and 100 Review
pointers all valid, 20 self-assessment options correctly mapped.

Cross-file anchors: 15 broken before, 15 after, 0 introduced.

* docs: address review — stale version stamps, /output-style re-add, permission tables
2026-09-20 01:15:34 +02:00

3.6 KiB
Raw Blame History

Claude How To

Hooks 参考

Hooks 是在 Claude Code 事件发生时自动执行的 shell 命令,用来做格式化、校验、通知、审计等自动化工作。

概览

Hooks 是事件驱动的自动化机制。它们会在 Claude Code 发生某些动作时自动运行,不需要你手动触发。

常见用途:

  • 写文件前自动格式化
  • 提交前运行测试
  • 扫描安全问题
  • 记录 bash 命令
  • 校验用户提示词
  • 发送团队通知

Hook 类型

Claude Code 支持 33 个 hook 事件,按用途分为以下 4 组。事件的 handler 类型共 5 种:commandhttpmcp_toolpromptagent

  • Tool HooksPreToolUsePostToolUsePostToolUseFailurePostToolBatchPermissionRequestPermissionDenied
  • Session HooksSessionStartSetupSessionEndStopStopFailureSubagentStartSubagentStop
  • Task HooksUserPromptSubmitUserPromptExpansionMessageDisplayTaskCompletedTaskCreatedTeammateIdleTaskCompletedTaskCreated 仅在启用 todo 工具时触发 —— 默认仅在 Claude 3.x 模型、Opus 4 至 4.7、Sonnet 4 至 4.6 及 Haiku 4.5 上可用;CLAUDE_CODE_ENABLE_TODO_TOOLS=1 可恢复)
  • Lifecycle HooksConfigChangeCwdChangedDirectoryAddedFileChangedPreCompactPostCompactPreModelSwitchPostModelSwitchWorktreeCreateWorktreeRemoveNotificationInstructionsLoadedElicitationElicitationResult

安装

mkdir -p ~/.claude/hooks
cp 06-hooks/*.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/*.sh

然后在 ~/.claude/settings.json 里配置:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/format-code.sh"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/security-scan.sh"
          }
        ]
      }
    ]
  }
}

使用方法

Hooks 会在匹配到事件时自动执行。你可以把它理解成 Claude Code 的事件回调。

常见示例

  • format-code.sh - 写入前自动格式化
  • pre-commit.sh - 提交前跑测试
  • security-scan.sh - 做安全扫描
  • log-bash.sh - 记录 bash 命令
  • validate-prompt.sh - 校验输入
  • notify-team.sh - 发通知

最佳实践

  • 把 hooks 保持短小明确
  • 只做单一职责
  • 先在本地测试
  • 不要在 hook 里放复杂业务逻辑
  • 对副作用保持谨慎

故障排查

  • 检查文件路径和权限
  • 确认脚本可执行
  • 检查 settings.json 语法
  • 查看 Claude Code 版本兼容性

相关概念

更多资源


最后更新: 2026 年 9 月 19 日 Claude Code 版本: 2.1.278 来源: