mirror of
https://github.com/luongnv89/claude-howto.git
synced 2026-09-17 13:02:14 +02:00
* docs: sync to Claude Code v2.1.257 Audit against Claude Code v2.1.257 (repo baseline was v2.1.245, 12 releases behind). Facts verified against the official docs and cross-checked against the locally installed 2.1.257 binary. Critical (P0): - lesson-quiz L06 Q9 asked how many hook events exist, offering 16/25/31/40. With the real answer now 33, no option was correct. Option C 31 -> 33; answer letter unchanged. - 03-skills taught SKILL.md `name`/`description` as required. Official docs now state all frontmatter fields are optional and `name` defaults to the directory name. Corrected in all five languages. Hook events 31 -> 33 (PreModelSwitch/PostModelSwitch, v2.1.251) across 30 occurrences in 22 files, including three that read 25. Delta proved by set-diff against the official event table: no phantom events. Both events added to the 06-hooks tables with matcher, blocking behavior and 30s timeout, plus detailed per-event sections. CLI corrections verified against the binary: - `claude plugin tag` takes a path, not a version - `claude plugin init` / `validate` require their arguments - `plugin init` scaffolds at ~/.claude/skills/<name>/, not project-local - `--channels` entries must be tagged plugin:<name>@<marketplace> - `--remote` is a deprecated alias for `--cloud` - no `claude remote-control` subcommand exists, only the --remote-control flag Also: CATALOG summary-table totals recomputed in all five languages (column 4 is a derived total, so the count change invalidated it); stale Opus 4.6/4.7 rosters aligned to canonical; self-assessment Advanced Features rescored 0-3 to stop a real gap being graded as mastery; 9 dangling quiz Review pointers repaired; new features documented (--restricted, Fable 5.1, experimental.cacheTtl, CLAUDE_CODE_SUBAGENT_MODEL_FORCE). Deliberately out of scope: pre-existing translation staleness whose fix is translation authorship rather than correction (ja/uk April-2026 root-doc bodies, zh stubs, vi/README stub, translated CATALOG "New Features"). Claude-Session: https://claude.ai/code/session_01128xfEd8W7c2cMRw6N7uiF * fix(hooks): complete the 31 → 33 hook-event propagation across all languages The v2.1.257 sync bumped the hook-event count in the lesson and catalog summaries but left 14 files still asserting 31, four of which the same commit restamped 2.1.257. It also bumped the prose count in claude_concepts_guide.md without adding the two rows, making that file contradict itself where it had been internally consistent before. Adds PreModelSwitch and PostModelSwitch (v2.1.251) everywhere an exhaustive enumeration exists, and corrects the count wherever a list is deliberately partial: - README.md / INDEX.md in en, ja, zh, uk and vi/INDEX.md — count 31 → 33 and both events added to the lifecycle group - resources.md in all five languages — count only; these lists end in "and more" and are partial by design - claude_concepts_guide.md — the two missing table rows Also brings every hook-event table up to the 33 its own summary row claims. The English CATALOG.md listed 29; the translated catalogs listed 25 and the ja/uk concept guides 27 and 28. Completing them required translating six events that had never reached the mirrors: Setup, UserPromptExpansion, PermissionDenied, PostToolBatch, MessageDisplay and DirectoryAdded. Summary-table arithmetic is untouched — those columns count example scripts, not events. Verified: no stale count survives outside CHANGELOG history, every exhaustive enumeration lists exactly 33, and pre-commit passes 23/23. Claude-Session: https://claude.ai/code/session_01UXmqJZNk1BR4uerJA85NiW
Hooks 参考
Hooks 是在 Claude Code 事件发生时自动执行的 shell 命令,用来做格式化、校验、通知、审计等自动化工作。
概览
Hooks 是事件驱动的自动化机制。它们会在 Claude Code 发生某些动作时自动运行,不需要你手动触发。
常见用途:
- 写文件前自动格式化
- 提交前运行测试
- 扫描安全问题
- 记录 bash 命令
- 校验用户提示词
- 发送团队通知
Hook 类型
Claude Code 支持 33 个 hook 事件,按用途分为以下 4 组。事件的 handler 类型共 5 种:command、http、mcp_tool、prompt、agent。
- Tool Hooks:
PreToolUse、PostToolUse、PostToolUseFailure、PostToolBatch、PermissionRequest、PermissionDenied - Session Hooks:
SessionStart、Setup、SessionEnd、Stop、StopFailure、SubagentStart、SubagentStop - Task Hooks:
UserPromptSubmit、UserPromptExpansion、MessageDisplay、TaskCompleted、TaskCreated、TeammateIdle(TaskCompleted和TaskCreated仅在启用 todo 工具时触发 —— 在 Opus 4.8、Sonnet 5、Fable 5、Mythos 5 及更新模型上默认关闭;CLAUDE_CODE_ENABLE_TODO_TOOLS=1可恢复) - Lifecycle Hooks:
ConfigChange、CwdChanged、DirectoryAdded、FileChanged、PreCompact、PostCompact、PreModelSwitch、PostModelSwitch、WorktreeCreate、WorktreeRemove、Notification、InstructionsLoaded、Elicitation、ElicitationResult
安装
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 月 2 日 Claude Code 版本: 2.1.257 来源: