@geminix/gxpm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (299) hide show
  1. package/AGENTS.md +148 -0
  2. package/CANON.md +53 -0
  3. package/CLAUDE.md +60 -0
  4. package/CONTEXT.md +49 -0
  5. package/DEBUG.md +59 -0
  6. package/ISSUE_CONTEXT.md +25 -0
  7. package/README.md +143 -0
  8. package/VERSION +1 -0
  9. package/agents/cleanup-auditor/cleanup-auditor.md +56 -0
  10. package/agents/grill-master.md +26 -0
  11. package/agents/implementer.md +32 -0
  12. package/agents/review-army/accessibility-reviewer.md +54 -0
  13. package/agents/review-army/code-quality-reviewer.md +54 -0
  14. package/agents/review-army/security-reviewer.md +56 -0
  15. package/agents/review-army/spec-compliance-reviewer.md +51 -0
  16. package/agents/review-army/test-reviewer.md +55 -0
  17. package/agents/reviewer.md +59 -0
  18. package/agents/ship-audit-army/docs-auditor.md +53 -0
  19. package/agents/ship-audit-army/performance-auditor.md +52 -0
  20. package/agents/ship-audit-army/security-auditor.md +52 -0
  21. package/agents/specifier.md +55 -0
  22. package/agents/triage-officer.md +27 -0
  23. package/bin/gxpm +17 -0
  24. package/bin/gxpm-browser +17 -0
  25. package/bin/gxpm-config +15 -0
  26. package/bin/gxpm-eval +13 -0
  27. package/bin/gxpm-global-discover +15 -0
  28. package/bin/gxpm-init +38 -0
  29. package/bin/gxpm-investigate +194 -0
  30. package/bin/gxpm-uninstall +15 -0
  31. package/bin/gxpm-update-check +165 -0
  32. package/commands/build.md +40 -0
  33. package/commands/help.md +53 -0
  34. package/commands/plan.md +34 -0
  35. package/commands/refine.md +46 -0
  36. package/commands/review.md +34 -0
  37. package/commands/ship.md +37 -0
  38. package/core/ac-check.ts +20 -0
  39. package/core/agent-runtime.ts +363 -0
  40. package/core/artifact-validator.ts +151 -0
  41. package/core/artifacts.ts +313 -0
  42. package/core/autopilot.ts +250 -0
  43. package/core/capabilities.ts +779 -0
  44. package/core/checkpoint.ts +370 -0
  45. package/core/cleanup.ts +32 -0
  46. package/core/command-probe.ts +82 -0
  47. package/core/config.ts +533 -0
  48. package/core/contracts/behavior-spec.schema.ts +38 -0
  49. package/core/contracts/converter.ts +61 -0
  50. package/core/contracts/host.ts +43 -0
  51. package/core/converters/converter.ts +93 -0
  52. package/core/converters/index.ts +8 -0
  53. package/core/converters/managed-artifact.ts +119 -0
  54. package/core/converters/parser.ts +159 -0
  55. package/core/converters/template-renderer.ts +35 -0
  56. package/core/converters/writer.ts +61 -0
  57. package/core/dag-executor.ts +426 -0
  58. package/core/dag-loader.ts +292 -0
  59. package/core/dag-schemas.ts +150 -0
  60. package/core/dispatch.ts +125 -0
  61. package/core/evidence.ts +148 -0
  62. package/core/gate.ts +269 -0
  63. package/core/hook-engine.ts +566 -0
  64. package/core/host-probe.ts +64 -0
  65. package/core/implement.ts +16 -0
  66. package/core/isolation-errors.ts +174 -0
  67. package/core/isolation-resolver.ts +921 -0
  68. package/core/issue-context.ts +381 -0
  69. package/core/issue-readiness.ts +457 -0
  70. package/core/issue-sync.ts +427 -0
  71. package/core/issues.ts +132 -0
  72. package/core/land.ts +108 -0
  73. package/core/orchestrator.ts +54 -0
  74. package/core/phase-artifact.ts +32 -0
  75. package/core/phase-gates.ts +130 -0
  76. package/core/phase-rewind.ts +94 -0
  77. package/core/plan-lint.ts +61 -0
  78. package/core/plan.ts +77 -0
  79. package/core/port-allocation.ts +50 -0
  80. package/core/pr-check.ts +15 -0
  81. package/core/preset-system/preset-resolver.ts +221 -0
  82. package/core/project-init-status.ts +127 -0
  83. package/core/qa.ts +15 -0
  84. package/core/resilience.ts +165 -0
  85. package/core/runs.ts +288 -0
  86. package/core/safe-path.test.ts +80 -0
  87. package/core/safe-path.ts +60 -0
  88. package/core/sdd-gate.test.ts +98 -0
  89. package/core/sdd-gate.ts +134 -0
  90. package/core/self-review.ts +62 -0
  91. package/core/session.ts +70 -0
  92. package/core/ship.ts +86 -0
  93. package/core/specify.ts +173 -0
  94. package/core/state.ts +1002 -0
  95. package/core/template-engine.ts +152 -0
  96. package/core/template-resolver.test.ts +70 -0
  97. package/core/template-resolver.ts +156 -0
  98. package/core/triage.ts +26 -0
  99. package/core/verify.ts +15 -0
  100. package/core/wiki-native.ts +2423 -0
  101. package/core/wiki.ts +27 -0
  102. package/core/workflow-event-emitter.ts +163 -0
  103. package/core/workflows/engine.ts +273 -0
  104. package/core/workflows/expressions.ts +76 -0
  105. package/core/workflows/index.ts +38 -0
  106. package/core/workflows/steps/command.ts +43 -0
  107. package/core/workflows/steps/gate.ts +47 -0
  108. package/core/workflows/steps/gxpm.ts +44 -0
  109. package/core/workflows/steps/linear.ts +31 -0
  110. package/core/workflows/steps/shell.ts +65 -0
  111. package/core/workflows/types.ts +62 -0
  112. package/core/workspace-runtime.ts +227 -0
  113. package/core/worktree-init-steps.ts +647 -0
  114. package/core/worktree-init.ts +330 -0
  115. package/core/worktree-owner.ts +143 -0
  116. package/docs/GXPM_VERIFY.md +98 -0
  117. package/docs/INSTALL_FOR_AGENTS.md +113 -0
  118. package/docs/README.md +57 -0
  119. package/docs/adr/adr-005-multi-platform-skill-converter.md +72 -0
  120. package/docs/agents/domain.md +30 -0
  121. package/docs/agents/issue-tracker.md +30 -0
  122. package/docs/agents/triage-labels.md +32 -0
  123. package/docs/architecture/gxpm-architecture-diagram.md +265 -0
  124. package/docs/architecture/gxpm-current-architecture.md +175 -0
  125. package/docs/architecture/gxpm-current-flow.md +278 -0
  126. package/docs/architecture/gxpm-replacement-architecture.md +211 -0
  127. package/docs/architecture/gxpm-target-architecture.md +449 -0
  128. package/docs/architecture/gxpm-v0-contract.md +311 -0
  129. package/docs/architecture/layered-workflow-boundaries.md +193 -0
  130. package/docs/architecture/preset-system.md +126 -0
  131. package/docs/architecture/scaffold-northstar.md +23 -0
  132. package/docs/brainstorms/2026-05-14-bdd-then-tdd-design.md +320 -0
  133. package/docs/brainstorms/README.md +22 -0
  134. package/docs/brainstorms/docs-knowledge-system-requirements.md +29 -0
  135. package/docs/governance/beta-skill-promotion.md +39 -0
  136. package/docs/governance/development-contract.md +144 -0
  137. package/docs/governance/gherkin-style.md +90 -0
  138. package/docs/governance/host-adapter.md +56 -0
  139. package/docs/governance/skill-authoring.md +87 -0
  140. package/docs/governance/skill-testing.md +356 -0
  141. package/docs/governance/template-authoring.md +53 -0
  142. package/docs/migrations/v0.2.md +51 -0
  143. package/docs/plans/README.md +23 -0
  144. package/docs/plans/bdd-then-tdd-plan.md +1767 -0
  145. package/docs/plans/docs-knowledge-system-plan.md +31 -0
  146. package/docs/plans/spec-kit-sdd-adoption-plan.md +305 -0
  147. package/docs/research/agents-md-best-practices.md +207 -0
  148. package/docs/research/archon-study.md +351 -0
  149. package/docs/research/claude-hooks-study.md +440 -0
  150. package/docs/research/codex-hooks-study.md +624 -0
  151. package/docs/research/everything-claude-code-study.md +252 -0
  152. package/docs/research/from-skills-to-layered-workflow.md +322 -0
  153. package/docs/research/gsd-study.md +69 -0
  154. package/docs/research/kimi-hooks-study.md +274 -0
  155. package/docs/research/mattpocock-skills-comparison.md +429 -0
  156. package/docs/research/mattpocock-skills-study.md +275 -0
  157. package/docs/research/oh-my-codex-study.md +279 -0
  158. package/docs/research/perplexity-agent-skills-design.md +168 -0
  159. package/docs/research/pmc-gstack-skill-study.md +122 -0
  160. package/docs/research/spec-kit-study.md +224 -0
  161. package/docs/research/superpowers-study.md +209 -0
  162. package/docs/roadmap/initial-roadmap.md +53 -0
  163. package/docs/solutions/README.md +45 -0
  164. package/docs/solutions/artifact-nesting-recovery.md +58 -0
  165. package/docs/solutions/session-context-restore-practice.md +67 -0
  166. package/docs/solutions/workflow/version-drift-recovery.md +49 -0
  167. package/docs/solutions/worktree-gate-recovery.md +62 -0
  168. package/docs/specs/README.md +28 -0
  169. package/docs/specs/claude.md +45 -0
  170. package/docs/specs/codex.md +44 -0
  171. package/docs/specs/cursor.md +44 -0
  172. package/hosts/adapters/claude.ts +29 -0
  173. package/hosts/adapters/codex.ts +27 -0
  174. package/hosts/adapters/cursor.ts +27 -0
  175. package/hosts/adapters/kimi.ts +27 -0
  176. package/hosts/claude.ts +23 -0
  177. package/hosts/codex.ts +26 -0
  178. package/hosts/cursor.ts +19 -0
  179. package/hosts/index.ts +33 -0
  180. package/hosts/registry.test.ts +52 -0
  181. package/hosts/registry.ts +57 -0
  182. package/hosts/schema.ts +58 -0
  183. package/package.json +52 -0
  184. package/scripts/browser.ts +185 -0
  185. package/scripts/cleanup.ts +142 -0
  186. package/scripts/commands/artifact.ts +115 -0
  187. package/scripts/commands/autopilot.ts +143 -0
  188. package/scripts/commands/capability.ts +57 -0
  189. package/scripts/commands/config.ts +69 -0
  190. package/scripts/commands/dag.ts +126 -0
  191. package/scripts/commands/feedback.ts +123 -0
  192. package/scripts/commands/gate.ts +291 -0
  193. package/scripts/commands/helpers.ts +126 -0
  194. package/scripts/commands/hook.ts +66 -0
  195. package/scripts/commands/init.ts +515 -0
  196. package/scripts/commands/issue.ts +825 -0
  197. package/scripts/commands/phase.ts +61 -0
  198. package/scripts/commands/preset.ts +159 -0
  199. package/scripts/commands/runtime.ts +199 -0
  200. package/scripts/commands/specify.ts +71 -0
  201. package/scripts/commands/upgrade.ts +243 -0
  202. package/scripts/commands/verify.ts +183 -0
  203. package/scripts/commands/wiki.ts +242 -0
  204. package/scripts/commands/workflow.ts +131 -0
  205. package/scripts/dev-skill.ts +55 -0
  206. package/scripts/discover-skills.ts +116 -0
  207. package/scripts/doctor.ts +410 -0
  208. package/scripts/dogfood-check.ts +125 -0
  209. package/scripts/eval-functional.ts +218 -0
  210. package/scripts/eval.ts +246 -0
  211. package/scripts/gen-skill-docs.ts +201 -0
  212. package/scripts/global-discover.ts +217 -0
  213. package/scripts/governance-check.ts +75 -0
  214. package/scripts/gxpm-check.ts +12 -0
  215. package/scripts/gxpm.ts +216 -0
  216. package/scripts/host-config.ts +62 -0
  217. package/scripts/install-claude-hooks.ts +138 -0
  218. package/scripts/install-codex-hooks.ts +271 -0
  219. package/scripts/install-hooks.ts +128 -0
  220. package/scripts/install-kimi-hooks.ts +92 -0
  221. package/scripts/install-skill.ts +184 -0
  222. package/scripts/phase-artifact-commands.ts +100 -0
  223. package/scripts/post-land-sync.ts +46 -0
  224. package/scripts/scaffold-check.ts +85 -0
  225. package/scripts/skill-naming-check.ts +78 -0
  226. package/scripts/skill-structure-check.ts +157 -0
  227. package/scripts/skills-lock-check.ts +60 -0
  228. package/scripts/sync-markdown-artifacts.ts +172 -0
  229. package/scripts/uninstall.ts +162 -0
  230. package/scripts/version.ts +47 -0
  231. package/scripts/wait-pr-ready.ts +407 -0
  232. package/skills/gxpm/SKILL.md +485 -0
  233. package/skills/gxpm/SKILL.md.tmpl +422 -0
  234. package/skills/gxpm/references/CANON.md +53 -0
  235. package/skills/gxpm/references/key-rules.md +130 -0
  236. package/skills/gxpm-architecture/SKILL.md +106 -0
  237. package/skills/gxpm-architecture/references/DEEPENING.md +37 -0
  238. package/skills/gxpm-architecture/references/INTERFACE-DESIGN.md +44 -0
  239. package/skills/gxpm-autopilot/SKILL.md +116 -0
  240. package/skills/gxpm-autopilot/SKILL.md.tmpl +107 -0
  241. package/skills/gxpm-browser/SKILL.md +105 -0
  242. package/skills/gxpm-browser/SKILL.md.tmpl +41 -0
  243. package/skills/gxpm-browser/references/commands.md +43 -0
  244. package/skills/gxpm-browser/references/evidence-path.md +20 -0
  245. package/skills/gxpm-build/SKILL.md +78 -0
  246. package/skills/gxpm-cleanup/SKILL.md +76 -0
  247. package/skills/gxpm-debug-issue/SKILL.md +39 -0
  248. package/skills/gxpm-diagnose/SKILL.md +220 -0
  249. package/skills/gxpm-diagnose/SKILL.md.tmpl +31 -0
  250. package/skills/gxpm-diagnose/references/feedback-loop.md +34 -0
  251. package/skills/gxpm-diagnose/references/feedback-loops.md +43 -0
  252. package/skills/gxpm-diagnose/references/phases.md +60 -0
  253. package/skills/gxpm-eval/SKILL.md +78 -0
  254. package/skills/gxpm-explore-codebase/SKILL.md +36 -0
  255. package/skills/gxpm-explore-codebase/scripts/summarize-communities.ts +51 -0
  256. package/skills/gxpm-feedback/SKILL.md +122 -0
  257. package/skills/gxpm-grill/SKILL.md +159 -0
  258. package/skills/gxpm-grill/SKILL.md.tmpl +77 -0
  259. package/skills/gxpm-grill/references/documentation-templates.md +56 -0
  260. package/skills/gxpm-grill/references/process.md +25 -0
  261. package/skills/gxpm-handoff/SKILL.md +112 -0
  262. package/skills/gxpm-hygiene/SKILL.md +69 -0
  263. package/skills/gxpm-implementer/SKILL.md +142 -0
  264. package/skills/gxpm-implementer/SKILL.md.tmpl +141 -0
  265. package/skills/gxpm-linear/SKILL.md +282 -0
  266. package/skills/gxpm-linear/SKILL.md.tmpl +86 -0
  267. package/skills/gxpm-linear/references/commands.md +75 -0
  268. package/skills/gxpm-linear/references/workflows.md +120 -0
  269. package/skills/gxpm-planning/SKILL.md +134 -0
  270. package/skills/gxpm-prototype/SKILL.md +64 -0
  271. package/skills/gxpm-refactor-safely/SKILL.md +62 -0
  272. package/skills/gxpm-review-army/SKILL.md +117 -0
  273. package/skills/gxpm-review-changes/SKILL.md +36 -0
  274. package/skills/gxpm-setup/SKILL.md +101 -0
  275. package/skills/gxpm-specifier/SKILL.md +135 -0
  276. package/skills/gxpm-tdd/SKILL.md +187 -0
  277. package/skills/gxpm-tdd/references/interface-design.md +23 -0
  278. package/skills/gxpm-tdd/references/mocking.md +27 -0
  279. package/skills/gxpm-tdd/references/red-green-refactor.md +61 -0
  280. package/skills/gxpm-tdd/references/troubleshooting.md +28 -0
  281. package/skills/gxpm-tdd/references/workflow.md +50 -0
  282. package/skills/gxpm-tdd/testing-anti-patterns.tmpl +304 -0
  283. package/skills/gxpm-triage/SKILL.md +160 -0
  284. package/skills/gxpm-verify/SKILL.md +107 -0
  285. package/skills/gxpm-write-skill/SKILL.md +131 -0
  286. package/skills/gxpm-zoom-out/SKILL.md +69 -0
  287. package/skills/maintain-hygiene-skills-lock/SKILL.md +54 -0
  288. package/skills/maintain-hygiene-skills-lock/SKILL.md.tmpl +53 -0
  289. package/templates/constitution-template.md +63 -0
  290. package/templates/hooks/gxpm-commit-msg +16 -0
  291. package/templates/hooks/gxpm-post-checkout +19 -0
  292. package/templates/hooks/gxpm-post-commit +7 -0
  293. package/templates/hooks/gxpm-post-merge +29 -0
  294. package/templates/hooks/gxpm-pre-commit +39 -0
  295. package/templates/hooks/gxpm-pre-push +33 -0
  296. package/templates/plan-template.md.tmpl +46 -0
  297. package/templates/spec-template.md.tmpl +63 -0
  298. package/templates/specify-stub.tmpl +22 -0
  299. package/templates/tasks-template.md.tmpl +32 -0
@@ -0,0 +1,440 @@
1
+ # Claude Code CLI Hooks 机制研究
2
+
3
+ > 研究日期:2026-05-04
4
+ > 文档来源:https://code.claude.com/docs/en/hooks.md(Hooks Reference)
5
+ > 版本:Claude Code CLI v2.1.126(2026-04-24)
6
+ > 状态:Stable(生产可用,/hooks 菜单可验证)
7
+
8
+ ---
9
+
10
+ ## 1. 配置格式与位置
11
+
12
+ Claude Code 的 hooks 定义在 **JSON settings 文件**中,通过嵌套三层结构组织:
13
+
14
+ 1. **Hook Event** — 生命周期触发点(如 `PreToolUse`、`SessionStart`)
15
+ 2. **Matcher Group** — 过滤条件(如 `"Bash"`、`"Edit|Write"`)
16
+ 3. **Hook Handler** — 实际执行的命令/HTTP/Agent
17
+
18
+ ```json
19
+ {
20
+ "hooks": {
21
+ "PreToolUse": [
22
+ {
23
+ "matcher": "Bash",
24
+ "hooks": [
25
+ {
26
+ "type": "command",
27
+ "if": "Bash(rm *)",
28
+ "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-rm.sh",
29
+ "timeout": 10
30
+ }
31
+ ]
32
+ }
33
+ ],
34
+ "SessionStart": [
35
+ {
36
+ "matcher": "startup|resume",
37
+ "hooks": [
38
+ {
39
+ "type": "command",
40
+ "command": "echo 'Reminder: use Bun, not npm.'"
41
+ }
42
+ ]
43
+ }
44
+ ]
45
+ }
46
+ }
47
+ ```
48
+
49
+ ### 配置位置与作用域
50
+
51
+ | 位置 | 作用域 | 可提交到仓库 |
52
+ |------|--------|-------------|
53
+ | `~/.claude/settings.json` | 全局所有项目 | ❌ |
54
+ | `.claude/settings.json` | 单个项目 | ✅ |
55
+ | `.claude/settings.local.json` | 单个项目 | ❌(gitignored) |
56
+ | Managed policy settings | 组织级 | ✅(管理员控制) |
57
+ | Plugin `hooks/hooks.json` | Plugin 启用时 | ✅ |
58
+ | Skill/Agent frontmatter | 组件激活期间 | ✅ |
59
+
60
+ Enterprise 管理员可用 `allowManagedHooksOnly: true` 禁用用户级 hooks,仅允许 managed policy hooks。
61
+
62
+ ### 禁用 hooks
63
+
64
+ ```json
65
+ { "disableAllHooks": true }
66
+ ```
67
+
68
+ Managed hooks 只能通过 managed settings 级别的 `disableAllHooks` 禁用。
69
+
70
+ ---
71
+
72
+ ## 2. 事件类型(28 种)
73
+
74
+ Claude Code 支持 **28 个生命周期事件**,是三大 CLI 中最丰富的:
75
+
76
+ | 事件 | 触发时机 | 匹配字段 | 可 Block |
77
+ |------|---------|---------|---------|
78
+ | `SessionStart` | 会话开始或恢复 | `source`: startup/resume/clear/compact | ❌ |
79
+ | `Setup` | `--init-only` 或 `--init` / `--maintenance` 在 `-p` 模式 | `init` / `maintenance` | ❌ |
80
+ | `UserPromptSubmit` | 用户提交 prompt 前 | 无 matcher | ✅ |
81
+ | `UserPromptExpansion` | 用户输入的命令展开为 prompt 前 | command name | ✅ |
82
+ | `PreToolUse` | 工具调用执行前 | tool name | ✅ |
83
+ | `PermissionRequest` | 权限对话框出现时 | tool name | ✅ |
84
+ | `PermissionDenied` | auto mode 拒绝工具调用时 | tool name | ✅ |
85
+ | `PostToolUse` | 工具调用成功后 | tool name | ❌ |
86
+ | `PostToolUseFailure` | 工具调用失败后 | tool name | ❌ |
87
+ | `PostToolBatch` | 一批并行工具调用完成后 | 无 matcher | ❌ |
88
+ | `Notification` | Claude Code 发送通知时 | notification type | ❌ |
89
+ | `SubagentStart` | Subagent 被创建时 | agent type | ❌ |
90
+ | `SubagentStop` | Subagent 完成时 | agent type | ✅ |
91
+ | `TaskCreated` | Task 被创建时 | 无 matcher | ✅ |
92
+ | `TaskCompleted` | Task 被标记完成时 | 无 matcher | ✅ |
93
+ | `Stop` | Claude 完成响应时 | 无 matcher | ✅ |
94
+ | `StopFailure` | 因 API 错误结束 turn 时 | error type | ❌(忽略输出) |
95
+ | `TeammateIdle` | Agent team 队友即将 idle 时 | 无 matcher | ✅ |
96
+ | `InstructionsLoaded` | CLAUDE.md 或 rules 加载时 | load reason | ❌ |
97
+ | `ConfigChange` | 配置文件变更时 | config source | ✅ |
98
+ | `CwdChanged` | 工作目录变更时 | 无 matcher | ❌ |
99
+ | `FileChanged` | 被监视文件变更时 | 文件名列表 | ❌ |
100
+ | `WorktreeCreate` | worktree 被创建时 | 无 matcher | ✅(任何非零退出码) |
101
+ | `WorktreeRemove` | worktree 被移除时 | 无 matcher | ❌ |
102
+ | `PreCompact` | 上下文压缩前 | manual/auto | ❌ |
103
+ | `PostCompact` | 上下文压缩完成后 | manual/auto | ❌ |
104
+ | `Elicitation` | MCP server 请求用户输入时 | MCP server name | ❌ |
105
+ | `ElicitationResult` | 用户响应 MCP elicitation 后 | MCP server name | ❌ |
106
+ | `SessionEnd` | 会话终止时 | end reason | ❌ |
107
+
108
+ ---
109
+
110
+ ## 3. Hook Handler 类型(5 种)
111
+
112
+ | 类型 | 字段 | 说明 |
113
+ |------|------|------|
114
+ | `command` | `command`, `async`, `asyncRewake`, `shell` | 执行 shell 命令,JSON 通过 stdin 传入 |
115
+ | `http` | `url`, `headers`, `allowedEnvVars` | POST JSON 到 HTTP 端点 |
116
+ | `mcp_tool` | `server`, `tool`, `input` | 调用已连接的 MCP server 工具 |
117
+ | `prompt` | `prompt`, `model` | 发送给 Claude 模型做单轮评估(yes/no JSON) |
118
+ | `agent` | `prompt`, `model`, `timeout` | 生成 subagent 用工具验证条件(实验性) |
119
+
120
+ ### 公共字段(所有类型共享)
121
+
122
+ | 字段 | 必需 | 说明 |
123
+ |------|------|------|
124
+ | `type` | ✅ | `"command"` / `"http"` / `"mcp_tool"` / `"prompt"` / `"agent"` |
125
+ | `if` | ❌ | Permission rule 语法过滤(仅工具事件有效) |
126
+ | `timeout` | ❌ | 秒,默认 600(command)、30(prompt)、60(agent) |
127
+ | `statusMessage` | ❌ | Hook 运行时的 spinner 消息 |
128
+ | `once` | ❌ | 仅 skill frontmatter 中有效,每会话只运行一次 |
129
+
130
+ ### Command Hook 特有字段
131
+
132
+ | 字段 | 说明 |
133
+ |------|------|
134
+ | `async` | `true` 时后台运行,不阻塞 |
135
+ | `asyncRewake` | `true` 时后台运行且退出码 2 时唤醒 Claude,stderr/stdout 作为系统提醒 |
136
+ | `shell` | `"bash"`(默认)或 `"powershell"` |
137
+
138
+ ---
139
+
140
+ ## 4. 输入/输出格式
141
+
142
+ ### 公共输入字段
143
+
144
+ 所有事件都包含以下字段(通过 stdin 传入 command hooks,通过 POST body 传入 HTTP hooks):
145
+
146
+ | 字段 | 说明 |
147
+ |------|------|
148
+ | `session_id` | 当前会话标识符 |
149
+ | `transcript_path` | 对话 JSON 路径 |
150
+ | `cwd` | Hook 调用时的工作目录 |
151
+ | `permission_mode` | 当前权限模式 |
152
+ | `hook_event_name` | 触发的事件名 |
153
+
154
+ Subagent / `--agent` 模式下额外包含:
155
+
156
+ | 字段 | 说明 |
157
+ |------|------|
158
+ | `agent_id` | Subagent 唯一标识 |
159
+ | `agent_type` | Agent 名称(如 `"Explore"`) |
160
+
161
+ ### 退出码语义
162
+
163
+ | 退出码 | 含义 |
164
+ |--------|------|
165
+ | `0` | 成功。stdout 解析为 JSON 输出;`UserPromptSubmit`/`UserPromptExpansion`/`SessionStart` 的 stdout 文本作为上下文注入 |
166
+ | `2` | **阻塞错误**。stderr 文本反馈给 Claude;行为取决于事件(见下表) |
167
+ | 其他 | **非阻塞错误**。转录中显示 `<hook> hook error: <stderr 第一行>`,执行继续 |
168
+
169
+ **⚠️ 重要**:只有退出码 2 能阻塞行为。退出码 1 被视为非阻塞错误,执行继续。如果目的是策略执行,必须使用 `exit 2`。
170
+
171
+ ### 退出码 2 的行为(按事件)
172
+
173
+ | 事件 | 可 Block | 退出码 2 效果 |
174
+ |------|---------|--------------|
175
+ | `PreToolUse` | ✅ | 阻止工具调用 |
176
+ | `PermissionRequest` | ✅ | 拒绝权限 |
177
+ | `UserPromptSubmit` | ✅ | 阻止 prompt 处理并擦除 prompt |
178
+ | `UserPromptExpansion` | ✅ | 阻止命令展开 |
179
+ | `Stop` | ✅ | 阻止 Claude 停止,继续对话 |
180
+ | `SubagentStop` | ✅ | 阻止 subagent 停止 |
181
+ | `TeammateIdle` | ✅ | 阻止队友进入 idle |
182
+ | `TaskCreated` | ✅ | 回滚任务创建 |
183
+ | `TaskCompleted` | ✅ | 阻止任务标记完成 |
184
+ | `ConfigChange` | ✅ | 阻止配置变更生效 |
185
+ | `WorktreeCreate` | ✅ | **任何非零退出码** 都中止 worktree 创建 |
186
+ | `SessionStart` / `Setup` / `Notification` / `PostToolUse` / `PostToolBatch` / `StopFailure` / `PreCompact` / `PostCompact` / `Elicitation` / `ElicitationResult` / `InstructionsLoaded` / `CwdChanged` / `FileChanged` / `WorktreeRemove` / `SessionEnd` | ❌ | 退出码 2 显示 stderr 给用户,执行继续 |
187
+
188
+ ### JSON 输出格式
189
+
190
+ **`PreToolUse` / `PermissionDenied`:**
191
+
192
+ ```json
193
+ {
194
+ "hookSpecificOutput": {
195
+ "hookEventName": "PreToolUse",
196
+ "permissionDecision": "deny",
197
+ "permissionDecisionReason": "Destructive command blocked by hook"
198
+ }
199
+ }
200
+ ```
201
+
202
+ `permissionDecision` 值:`"allow"`(跳过交互确认)、`"deny"`(取消)、`"ask"`(正常提示)、`"defer"`(非交互模式 -p 下保留工具调用)
203
+
204
+ **`PermissionRequest`:**
205
+
206
+ ```json
207
+ {
208
+ "hookSpecificOutput": {
209
+ "hookEventName": "PermissionRequest",
210
+ "decision": {
211
+ "behavior": "allow",
212
+ "updatedPermissions": [
213
+ { "type": "setMode", "mode": "acceptEdits", "destination": "session" }
214
+ ]
215
+ }
216
+ }
217
+ }
218
+ ```
219
+
220
+ **`Stop` / `PostToolUse` / `PostToolBatch`:**
221
+
222
+ ```json
223
+ {
224
+ "decision": "block",
225
+ "reason": "Run tests before stopping"
226
+ }
227
+ ```
228
+
229
+ **`UserPromptSubmit`:**
230
+
231
+ ```json
232
+ {
233
+ "additionalContext": "Context to inject into Claude's view"
234
+ }
235
+ ```
236
+
237
+ **`SessionStart`:** 退出码 0 时的 stdout 文本直接作为上下文注入(不支持 JSON `additionalContext`)。
238
+
239
+ ---
240
+
241
+ ## 5. Matcher 模式
242
+
243
+ | Matcher 值 | 评估方式 | 示例 |
244
+ |-----------|---------|------|
245
+ | `"*"`, `""`, 或省略 | 匹配所有 | 每次事件都触发 |
246
+ | 仅字母、数字、`_`、`|` | 精确字符串或 `\|` 分隔列表 | `Bash` 精确匹配 Bash;`Edit\|Write` 匹配两者 |
247
+ | 包含其他字符 | JavaScript 正则表达式 | `^Notebook` 匹配 Notebook 开头;`mcp__memory__.*` 匹配 memory server 所有工具 |
248
+
249
+ ### 各事件的 Matcher 字段
250
+
251
+ | 事件 | Matcher 过滤目标 | 示例 |
252
+ |------|-----------------|------|
253
+ | `PreToolUse`, `PostToolUse`, `PostToolUseFailure`, `PermissionRequest`, `PermissionDenied` | tool name | `Bash`, `Edit\|Write`, `mcp__.*` |
254
+ | `SessionStart` | 会话启动方式 | `startup`, `resume`, `clear`, `compact` |
255
+ | `Setup` | CLI flag | `init`, `maintenance` |
256
+ | `SessionEnd` | 结束原因 | `clear`, `resume`, `logout`, `prompt_input_exit` 等 |
257
+ | `Notification` | 通知类型 | `permission_prompt`, `idle_prompt`, `auth_success` 等 |
258
+ | `SubagentStart`, `SubagentStop` | agent 类型 | `general-purpose`, `Explore`, `Plan` 或自定义名 |
259
+ | `PreCompact`, `PostCompact` | 触发方式 | `manual`, `auto` |
260
+ | `ConfigChange` | 配置来源 | `user_settings`, `project_settings`, `local_settings` 等 |
261
+ | `StopFailure` | 错误类型 | `rate_limit`, `authentication_failed`, `billing_error` 等 |
262
+ | `InstructionsLoaded` | 加载原因 | `session_start`, `nested_traversal`, `path_glob_match` 等 |
263
+ | `Elicitation`, `ElicitationResult` | MCP server 名 | 已配置的 MCP server 名称 |
264
+ | `UserPromptExpansion` | 命令名 | Skill 或 command 名称 |
265
+ | `FileChanged` | 文件名列表(字面量) | `.envrc\|.env` |
266
+ | `UserPromptSubmit`, `PostToolBatch`, `Stop`, `TeammateIdle`, `TaskCreated`, `TaskCompleted`, `WorktreeCreate`, `WorktreeRemove`, `CwdChanged` | **无 matcher 支持** | 每次事件都触发,添加 matcher 被静默忽略 |
267
+
268
+ ### `if` 字段(工具事件专用)
269
+
270
+ `if` 使用 [permission rule syntax](https://code.claude.com/docs/en/permissions) 同时匹配工具名和参数,比 matcher 更精确:
271
+
272
+ ```json
273
+ {
274
+ "matcher": "Bash",
275
+ "hooks": [{
276
+ "type": "command",
277
+ "if": "Bash(git *)",
278
+ "command": "./check-git-policy.sh"
279
+ }]
280
+ }
281
+ ```
282
+
283
+ `if` 仅对工具事件有效:`PreToolUse`, `PostToolUse`, `PostToolUseFailure`, `PermissionRequest`, `PermissionDenied`。其他事件上设置 `if` 会导致 hook 永不运行。
284
+
285
+ ---
286
+
287
+ ## 6. 环境变量
288
+
289
+ | 变量 | 说明 |
290
+ |------|------|
291
+ | `$CLAUDE_PROJECT_DIR` | 项目根目录(建议用 `"$CLAUDE_PROJECT_DIR"/.claude/hooks/...` 引用) |
292
+ | `${CLAUDE_PLUGIN_ROOT}` | Plugin 安装目录 |
293
+ | `${CLAUDE_PLUGIN_DATA}` | Plugin 持久数据目录 |
294
+ | `$CLAUDE_ENV_FILE` | 环境变量文件路径(`SessionStart`/`CwdChanged` 可写入) |
295
+ | `$CLAUDE_CODE_REMOTE` | 远程 web 环境为 `"true"`,本地 CLI 未设置 |
296
+
297
+ ---
298
+
299
+ ## 7. 并行执行与去重
300
+
301
+ 所有匹配的 hooks **并行运行**。相同的 handler 自动去重:
302
+ - Command hooks 按命令字符串去重
303
+ - HTTP hooks 按 URL 去重
304
+
305
+ 当多个 hooks 返回决策时,Claude Code 选择**最严格的答案**:
306
+ - `deny` > `ask` > `allow`
307
+ - `block` 覆盖 `allow`
308
+ - `additionalContext` 文本从所有 hooks 保留并合并传递给 Claude
309
+
310
+ ---
311
+
312
+ ## 8. 对 gxpm 的启示
313
+
314
+ ### 8.1 gxpm 能力映射
315
+
316
+ | gxpm 能力 | Claude Code 实现 | 可行方案 |
317
+ |-----------|-----------------|---------|
318
+ | 会话启动注入上下文 | `SessionStart` stdout 文本注入 | ✅ **最佳**。stdout 直接作为上下文 |
319
+ | Prompt 命中 GXPM-N 注入 issue 状态 | `UserPromptSubmit` 返回 `additionalContext` | ✅ 可用 |
320
+ | 记录 `update_plan` | `PreToolUse` + matcher `"ExitPlanMode"` | ✅ 可用 |
321
+ | Git hooks(pre-commit 等) | 独立机制 | ✅ 通用,不区分 host |
322
+ | 自动 approve ExitPlanMode | `PermissionRequest` + matcher `"ExitPlanMode"` + `decision.behavior: "allow"` | ✅ 可用 |
323
+ | 代码格式化后处理 | `PostToolUse` + matcher `"Edit\|Write"` | ✅ 可用 |
324
+ | 保护敏感文件 | `PreToolUse` + `if: "Edit(*.env)"` | ✅ 可用 |
325
+
326
+ ### 8.2 推荐适配路径
327
+
328
+ 1. **SessionStart 注入**:Claude Code 的 `SessionStart` stdout 注入是三大 CLI 中最直接的上下文注入方式。gxpm 可在 `SessionStart` 中输出项目约定和当前 issue 状态。
329
+
330
+ 2. **UserPromptSubmit 动态上下文**:与 Kimi 不同,Claude Code 的 `UserPromptSubmit` 支持返回 `additionalContext`,是 prompt 命中 GXPM-N 时注入 issue 状态的理想机制。
331
+
332
+ 3. **PreToolUse 审计与拦截**:`PreToolUse` + `if` 字段可以精确拦截特定工具调用(如 `update_plan`),用于记录和审计。
333
+
334
+ 4. **PostToolUse 后处理**:代码格式化、测试运行等后处理任务适合用 `PostToolUse` + `"Edit|Write"` matcher。
335
+
336
+ 5. **PermissionRequest 自动审批**:对于 plan mode 中的 `ExitPlanMode` 等高频确认,可用 `PermissionRequest` 自动审批提升效率。
337
+
338
+ ### 8.3 配置示例(gxpm 用例)
339
+
340
+ ```json
341
+ // .claude/settings.json
342
+ {
343
+ "hooks": {
344
+ "SessionStart": [
345
+ {
346
+ "matcher": "startup|resume",
347
+ "hooks": [
348
+ {
349
+ "type": "command",
350
+ "command": "gxpm hook session-start",
351
+ "timeout": 5
352
+ }
353
+ ]
354
+ }
355
+ ],
356
+ "UserPromptSubmit": [
357
+ {
358
+ "hooks": [
359
+ {
360
+ "type": "command",
361
+ "command": "gxpm hook user-prompt-submit",
362
+ "timeout": 3
363
+ }
364
+ ]
365
+ }
366
+ ],
367
+ "PreToolUse": [
368
+ {
369
+ "matcher": "ExitPlanMode",
370
+ "hooks": [
371
+ {
372
+ "type": "command",
373
+ "command": "gxpm hook pre-tool-use",
374
+ "timeout": 3
375
+ }
376
+ ]
377
+ }
378
+ ],
379
+ "PostToolUse": [
380
+ {
381
+ "matcher": "Edit|Write",
382
+ "hooks": [
383
+ {
384
+ "type": "command",
385
+ "command": "gxpm hook post-tool-use",
386
+ "timeout": 10
387
+ }
388
+ ]
389
+ }
390
+ ]
391
+ }
392
+ }
393
+ ```
394
+
395
+ ---
396
+
397
+ ## 9. 与 Kimi / Codex 的差异矩阵
398
+
399
+ | 维度 | Claude Code | Kimi | Codex |
400
+ |------|------------|------|-------|
401
+ | **事件数量** | 28 种 | 13 种 | 6 种 |
402
+ | **配置格式** | JSON (settings.json) | TOML (config.toml `[[hooks]]`) | JSON (hooks.json) + TOML inline |
403
+ | **Handler 类型** | 5 种(command/http/mcp_tool/prompt/agent) | 1 种(command) | 1 种(command) |
404
+ | **SessionStart 上下文注入** | ✅ stdout 文本直接注入 | ❌ 不支持(返回值被忽略) | ✅ `additionalContext` JSON |
405
+ | **UserPromptSubmit 上下文注入** | ✅ `additionalContext` JSON | ❌ 不支持(仅 block) | ✅ `additionalContext` JSON |
406
+ | **Block 机制** | exit 2 / JSON `permissionDecision: deny` | exit 2 / JSON `permissionDecision: deny` | exit 2 / JSON `permissionDecision: deny` / `decision: block` |
407
+ | **Matcher 语法** | 精确字符串 / `\|` 列表 / JS 正则 | 正则表达式 | 正则表达式 |
408
+ | **`if` 字段** | ✅ Permission rule 语法(仅工具事件) | ❌ | ❌ |
409
+ | **Async 后台运行** | ✅ `async` / `asyncRewake` | ❌ | ❌ |
410
+ | **HTTP hooks** | ✅ | ❌ | ❌ |
411
+ | **MCP tool hooks** | ✅ | ❌ | ❌ |
412
+ | **Prompt/Agent hooks** | ✅ | ❌ | ❌ |
413
+ | **Plugin hooks** | ✅ | ❌ | ❌ |
414
+ | **Skill frontmatter hooks** | ✅ | ❌ | ❌ |
415
+ | **并行执行** | ✅(所有匹配 hooks 并行) | ✅(server-side 并行,wire-side 订阅) | ✅ |
416
+ | **去重** | ✅(按命令字符串 / URL) | ❌ | ❌ |
417
+ | **权限模式集成** | ✅(hooks 在权限检查前运行,`deny` 可覆盖 bypassPermissions) | ❌ | ❌ |
418
+ | **CLI 验证** | ✅ `/hooks` 菜单 | ✅ `/hooks` 命令 | ❌ |
419
+ | **非交互模式** | ✅ `-p` 下 `PermissionRequest` 不触发,用 `PreToolUse` | N/A | ✅ `-p` 下 `PermissionRequest` 不触发 |
420
+
421
+ ---
422
+
423
+ ## 10. 待验证事项
424
+
425
+ - [ ] `SessionStart` stdout 注入是否有长度限制
426
+ - [ ] `additionalContext` 在 `UserPromptSubmit` 中的字符上限
427
+ - [ ] `PreToolUse` 的 `updatedInput` 重写能力(多个 hook 同时修改时的竞争)
428
+ - [ ] Agent hooks(`type: "agent"`)在 gxpm 场景中的实际可用性
429
+ - [ ] Managed policy hooks 在 enterprise 部署中的 gxpm 适配
430
+
431
+ ---
432
+
433
+ ## 参考文档
434
+
435
+ - https://code.claude.com/docs/en/hooks.md — Hooks Reference(完整事件 schema、JSON I/O、退出码)
436
+ - https://code.claude.com/docs/en/hooks-guide.md — Automate workflows with hooks(使用指南与示例)
437
+ - https://code.claude.com/docs/en/settings.md — Settings 配置层级
438
+ - https://code.claude.com/docs/en/permissions.md — Permission rules 语法(`if` 字段)
439
+ - https://code.claude.com/docs/en/plugins-reference.md — Plugin hooks 规范
440
+ - https://code.claude.com/docs/en/skills.md — Skill frontmatter hooks