add-coder 0.3.27 → 0.3.28

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 (262) hide show
  1. package/README.md +246 -145
  2. package/package.json +2 -1
  3. package/templates/.add-coder-src-hash.json +126 -148
  4. package/templates/adapters/claude/hooks/doc-format-guard.ts +32 -0
  5. package/templates/adapters/claude/hooks/lib/claude-env.ts +7 -0
  6. package/templates/adapters/claude/hooks/notification.ts +24 -0
  7. package/templates/adapters/claude/hooks/permission-denied.ts +33 -0
  8. package/templates/adapters/claude/hooks/permission-gate.ts +12 -0
  9. package/templates/adapters/claude/hooks/post-tool-failure.ts +4 -0
  10. package/templates/adapters/claude/hooks/post-tool-use.ts +17 -0
  11. package/templates/adapters/claude/hooks/pre-compact.ts +10 -0
  12. package/templates/adapters/claude/hooks/pre-tool-use.ts +81 -0
  13. package/templates/adapters/claude/hooks/prompt-submit.ts +82 -0
  14. package/templates/adapters/claude/hooks/review-checklist.ts +4 -0
  15. package/templates/adapters/claude/hooks/session-end.ts +10 -0
  16. package/templates/adapters/claude/hooks/session-start.ts +24 -0
  17. package/templates/adapters/claude/hooks/stop-check.ts +15 -0
  18. package/templates/adapters/claude/hooks/stop-failure.ts +34 -0
  19. package/templates/adapters/claude/hooks/subagent-guard.ts +25 -0
  20. package/templates/adapters/claude/hooks/subagent-stop.ts +12 -0
  21. package/templates/adapters/claude/settings.json +13 -13
  22. package/templates/adapters/codex/hooks/doc-format-guard.ts +30 -0
  23. package/templates/adapters/codex/hooks/notification.ts +26 -0
  24. package/templates/adapters/codex/hooks/permission-gate.ts +15 -0
  25. package/templates/adapters/codex/hooks/post-tool-failure.ts +14 -0
  26. package/templates/adapters/codex/hooks/post-tool-use.ts +85 -0
  27. package/templates/adapters/codex/hooks/pre-compact.ts +20 -0
  28. package/templates/adapters/codex/hooks/pre-tool-use.ts +141 -0
  29. package/templates/adapters/codex/hooks/prompt-submit.ts +38 -0
  30. package/templates/adapters/codex/hooks/review-checklist.ts +5 -0
  31. package/templates/adapters/codex/hooks/session-end.ts +19 -0
  32. package/templates/adapters/codex/hooks/session-start.ts +45 -0
  33. package/templates/adapters/codex/hooks/stop-check.ts +59 -0
  34. package/templates/adapters/codex/hooks/subagent-guard.ts +22 -0
  35. package/templates/adapters/codex/hooks/subagent-stop.ts +20 -0
  36. package/templates/adapters/codex/hooks.json +5 -5
  37. package/templates/adapters/qoder/hooks/doc-format-guard.ts +34 -0
  38. package/templates/adapters/qoder/hooks/lib/context-inject.ts +61 -0
  39. package/templates/adapters/qoder/hooks/lib/qoder-env.ts +23 -0
  40. package/templates/adapters/qoder/hooks/lib/review-checklist.ts +5 -0
  41. package/templates/adapters/qoder/hooks/notification.ts +34 -0
  42. package/templates/adapters/qoder/hooks/permission-gate.ts +26 -0
  43. package/templates/adapters/qoder/hooks/post-tool-failure.ts +30 -0
  44. package/templates/adapters/qoder/hooks/post-tool-use.ts +32 -0
  45. package/templates/adapters/qoder/hooks/pre-compact.ts +16 -0
  46. package/templates/adapters/qoder/hooks/pre-tool-use.ts +112 -0
  47. package/templates/adapters/qoder/hooks/prompt-submit.ts +66 -0
  48. package/templates/adapters/qoder/hooks/review-checklist.ts +24 -0
  49. package/templates/adapters/qoder/hooks/session-end.ts +27 -0
  50. package/templates/adapters/qoder/hooks/session-start.ts +83 -0
  51. package/templates/adapters/qoder/hooks/stop-check.ts +69 -0
  52. package/templates/adapters/qoder/hooks/subagent-guard.ts +16 -0
  53. package/templates/adapters/qoder/hooks/subagent-stop.ts +33 -0
  54. package/templates/adapters/qoder/settings.json +13 -13
  55. package/templates/adapters/trae/hooks/doc-format-guard.ts +30 -0
  56. package/templates/adapters/trae/hooks/notification.ts +22 -0
  57. package/templates/adapters/trae/hooks/permission-gate.ts +15 -0
  58. package/templates/adapters/trae/hooks/post-tool-failure.ts +14 -0
  59. package/templates/adapters/trae/hooks/post-tool-use.ts +24 -0
  60. package/templates/adapters/trae/hooks/pre-compact.ts +15 -0
  61. package/templates/adapters/trae/hooks/pre-tool-use.ts +40 -0
  62. package/templates/adapters/trae/hooks/prompt-submit.ts +29 -0
  63. package/templates/adapters/trae/hooks/review-checklist.ts +5 -0
  64. package/templates/adapters/trae/hooks/session-end.ts +16 -0
  65. package/templates/adapters/trae/hooks/session-start.ts +15 -0
  66. package/templates/adapters/trae/hooks/stop-check.ts +20 -0
  67. package/templates/adapters/trae/hooks/subagent-guard.ts +22 -0
  68. package/templates/adapters/trae/hooks/subagent-stop.ts +15 -0
  69. package/templates/adapters/trae/hooks.json +1 -1
  70. package/templates/adapters/vscode/hooks/doc-format-guard.ts +27 -0
  71. package/templates/adapters/vscode/hooks/notification.ts +22 -0
  72. package/templates/adapters/vscode/hooks/permission-gate.ts +15 -0
  73. package/templates/adapters/vscode/hooks/post-tool-failure.ts +32 -0
  74. package/templates/adapters/vscode/hooks/post-tool-use.ts +25 -0
  75. package/templates/adapters/vscode/hooks/pre-compact.ts +15 -0
  76. package/templates/adapters/vscode/hooks/pre-tool-use.ts +78 -0
  77. package/templates/adapters/vscode/hooks/prompt-submit.ts +74 -0
  78. package/templates/adapters/vscode/hooks/review-checklist.ts +5 -0
  79. package/templates/adapters/vscode/hooks/session-end.ts +16 -0
  80. package/templates/adapters/vscode/hooks/session-start.ts +24 -0
  81. package/templates/adapters/vscode/hooks/stop-check.ts +21 -0
  82. package/templates/adapters/vscode/hooks/subagent-guard.ts +21 -0
  83. package/templates/adapters/vscode/hooks/subagent-stop.ts +15 -0
  84. package/templates/core/docs/ADD-governance-claude-code.md +3 -3
  85. package/templates/core/docs/ADD-governance-codex.md +5 -5
  86. package/templates/core/docs/ADD-governance-qoder-cn.md +4 -4
  87. package/templates/core/docs/ADD-governance-trae.md +2 -2
  88. package/templates/core/docs/ADD-governance-vscode-copilot.md +21 -22
  89. package/templates/core/governance/audit-bridge.ts +47 -0
  90. package/templates/core/governance/common.ts +299 -0
  91. package/templates/core/governance/context-inject.ts +53 -0
  92. package/templates/core/governance/doc-format-guard.ts +440 -0
  93. package/templates/core/governance/notification-router.ts +62 -0
  94. package/templates/core/governance/notify.ts +66 -0
  95. package/templates/core/governance/permission-gate-router.ts +42 -0
  96. package/templates/core/governance/post-tool-failure-router.ts +56 -0
  97. package/templates/core/governance/post-tool-router.ts +212 -0
  98. package/templates/core/governance/pre-compact-guard.ts +41 -0
  99. package/templates/core/governance/pre-tool-guard.ts +309 -0
  100. package/templates/core/governance/preload-templates.ts +241 -0
  101. package/templates/core/governance/prompt-router.ts +222 -0
  102. package/templates/core/governance/review-checklist-guard.ts +186 -0
  103. package/templates/core/governance/rules.ts +720 -0
  104. package/templates/core/governance/session-end.ts +88 -0
  105. package/templates/core/governance/session-start-guard.ts +99 -0
  106. package/templates/core/governance/state-detect.ts +6 -0
  107. package/templates/core/governance/stop-router.ts +140 -0
  108. package/templates/core/governance/subagent-guard-router.ts +35 -0
  109. package/templates/core/governance/subagent-stop-router.ts +93 -0
  110. package/templates/core/governance/subagent-stop.ts +75 -0
  111. package/templates/core/governance/vocabulary.ts +109 -0
  112. package/templates/core/hooks/doc-format-guard.ts +21 -0
  113. package/templates/core/hooks/notification.ts +11 -0
  114. package/templates/core/hooks/permission-gate.ts +7 -0
  115. package/templates/core/hooks/post-tool-failure.ts +7 -0
  116. package/templates/core/hooks/post-tool-use.ts +23 -0
  117. package/templates/core/hooks/pre-compact.ts +9 -0
  118. package/templates/core/hooks/pre-tool-use.ts +30 -0
  119. package/templates/core/hooks/prompt-submit.ts +11 -0
  120. package/templates/core/hooks/review-checklist.ts +4 -0
  121. package/templates/core/hooks/session-end.ts +10 -0
  122. package/templates/core/hooks/session-start.ts +9 -0
  123. package/templates/core/hooks/stop-check.ts +14 -0
  124. package/templates/core/hooks/subagent-guard.ts +8 -0
  125. package/templates/core/hooks/subagent-stop.ts +9 -0
  126. package/templates/core/scripts/mcp-server/tools/gateway/check_rahs.ts +12 -1
  127. package/templates/core/scripts/mcp-server/tools/plan.ts +12 -2
  128. package/templates/adapters/claude/hooks/doc-format-guard.sh +0 -283
  129. package/templates/adapters/claude/hooks/lib/common.sh +0 -220
  130. package/templates/adapters/claude/hooks/lib/context-inject.sh +0 -96
  131. package/templates/adapters/claude/hooks/lib/notify.sh +0 -36
  132. package/templates/adapters/claude/hooks/lib/preload-templates.sh +0 -212
  133. package/templates/adapters/claude/hooks/lib/session-end.sh +0 -76
  134. package/templates/adapters/claude/hooks/lib/state-detect.sh +0 -13
  135. package/templates/adapters/claude/hooks/lib/subagent-stop.sh +0 -90
  136. package/templates/adapters/claude/hooks/lib/vocabulary.sh +0 -62
  137. package/templates/adapters/claude/hooks/notification.sh +0 -45
  138. package/templates/adapters/claude/hooks/permission-denied.sh +0 -42
  139. package/templates/adapters/claude/hooks/permission-gate.sh +0 -18
  140. package/templates/adapters/claude/hooks/post-tool-failure.sh +0 -10
  141. package/templates/adapters/claude/hooks/post-tool-use.sh +0 -35
  142. package/templates/adapters/claude/hooks/pre-compact.sh +0 -37
  143. package/templates/adapters/claude/hooks/pre-tool-use.sh +0 -153
  144. package/templates/adapters/claude/hooks/prompt-submit.sh +0 -99
  145. package/templates/adapters/claude/hooks/review-checklist.sh +0 -10
  146. package/templates/adapters/claude/hooks/session-end.sh +0 -21
  147. package/templates/adapters/claude/hooks/session-start.sh +0 -36
  148. package/templates/adapters/claude/hooks/stop-check.sh +0 -54
  149. package/templates/adapters/claude/hooks/stop-failure.sh +0 -44
  150. package/templates/adapters/claude/hooks/subagent-guard.sh +0 -36
  151. package/templates/adapters/claude/hooks/subagent-stop.sh +0 -20
  152. package/templates/adapters/codex/hooks/doc-format-guard.sh +0 -343
  153. package/templates/adapters/codex/hooks/lib/common.sh +0 -209
  154. package/templates/adapters/codex/hooks/lib/context-inject.sh +0 -96
  155. package/templates/adapters/codex/hooks/lib/notify.sh +0 -36
  156. package/templates/adapters/codex/hooks/lib/preload-templates.sh +0 -208
  157. package/templates/adapters/codex/hooks/lib/session-end.sh +0 -76
  158. package/templates/adapters/codex/hooks/lib/state-detect.sh +0 -8
  159. package/templates/adapters/codex/hooks/lib/subagent-stop.sh +0 -90
  160. package/templates/adapters/codex/hooks/lib/vocabulary.sh +0 -51
  161. package/templates/adapters/codex/hooks/notification.sh +0 -30
  162. package/templates/adapters/codex/hooks/permission-gate.sh +0 -18
  163. package/templates/adapters/codex/hooks/post-tool-failure.sh +0 -10
  164. package/templates/adapters/codex/hooks/post-tool-use.sh +0 -43
  165. package/templates/adapters/codex/hooks/pre-compact.sh +0 -37
  166. package/templates/adapters/codex/hooks/pre-tool-use.sh +0 -130
  167. package/templates/adapters/codex/hooks/prompt-submit.sh +0 -92
  168. package/templates/adapters/codex/hooks/review-checklist.sh +0 -10
  169. package/templates/adapters/codex/hooks/session-end.sh +0 -38
  170. package/templates/adapters/codex/hooks/session-start.sh +0 -63
  171. package/templates/adapters/codex/hooks/stop-check.sh +0 -71
  172. package/templates/adapters/codex/hooks/subagent-guard.sh +0 -15
  173. package/templates/adapters/codex/hooks/subagent-stop.sh +0 -47
  174. package/templates/adapters/qoder/hooks/doc-format-guard.sh +0 -341
  175. package/templates/adapters/qoder/hooks/lib/common.sh +0 -223
  176. package/templates/adapters/qoder/hooks/lib/context-inject.sh +0 -96
  177. package/templates/adapters/qoder/hooks/lib/notify.sh +0 -36
  178. package/templates/adapters/qoder/hooks/lib/preload-templates.sh +0 -212
  179. package/templates/adapters/qoder/hooks/lib/session-end.sh +0 -76
  180. package/templates/adapters/qoder/hooks/lib/state-detect.sh +0 -13
  181. package/templates/adapters/qoder/hooks/lib/subagent-stop.sh +0 -90
  182. package/templates/adapters/qoder/hooks/lib/vocabulary.sh +0 -62
  183. package/templates/adapters/qoder/hooks/notification.sh +0 -25
  184. package/templates/adapters/qoder/hooks/permission-gate.sh +0 -8
  185. package/templates/adapters/qoder/hooks/post-tool-failure.sh +0 -8
  186. package/templates/adapters/qoder/hooks/post-tool-use.sh +0 -121
  187. package/templates/adapters/qoder/hooks/pre-compact.sh +0 -37
  188. package/templates/adapters/qoder/hooks/pre-tool-use.sh +0 -154
  189. package/templates/adapters/qoder/hooks/prompt-submit.sh +0 -96
  190. package/templates/adapters/qoder/hooks/review-checklist.sh +0 -157
  191. package/templates/adapters/qoder/hooks/session-end.sh +0 -24
  192. package/templates/adapters/qoder/hooks/session-start.sh +0 -45
  193. package/templates/adapters/qoder/hooks/stop-check.sh +0 -59
  194. package/templates/adapters/qoder/hooks/subagent-guard.sh +0 -27
  195. package/templates/adapters/qoder/hooks/subagent-stop.sh +0 -26
  196. package/templates/adapters/trae/hooks/doc-format-guard.sh +0 -343
  197. package/templates/adapters/trae/hooks/lib/common.sh +0 -220
  198. package/templates/adapters/trae/hooks/lib/context-inject.sh +0 -96
  199. package/templates/adapters/trae/hooks/lib/notify.sh +0 -36
  200. package/templates/adapters/trae/hooks/lib/preload-templates.sh +0 -212
  201. package/templates/adapters/trae/hooks/lib/session-end.sh +0 -76
  202. package/templates/adapters/trae/hooks/lib/state-detect.sh +0 -10
  203. package/templates/adapters/trae/hooks/lib/subagent-stop.sh +0 -90
  204. package/templates/adapters/trae/hooks/lib/vocabulary.sh +0 -62
  205. package/templates/adapters/trae/hooks/notification.sh +0 -45
  206. package/templates/adapters/trae/hooks/permission-gate.sh +0 -18
  207. package/templates/adapters/trae/hooks/post-tool-failure.sh +0 -10
  208. package/templates/adapters/trae/hooks/post-tool-use.sh +0 -138
  209. package/templates/adapters/trae/hooks/pre-compact.sh +0 -37
  210. package/templates/adapters/trae/hooks/pre-tool-use.sh +0 -223
  211. package/templates/adapters/trae/hooks/prompt-submit.sh +0 -87
  212. package/templates/adapters/trae/hooks/review-checklist.sh +0 -10
  213. package/templates/adapters/trae/hooks/session-end.sh +0 -38
  214. package/templates/adapters/trae/hooks/session-start.sh +0 -53
  215. package/templates/adapters/trae/hooks/stop-check.sh +0 -54
  216. package/templates/adapters/trae/hooks/subagent-guard.sh +0 -15
  217. package/templates/adapters/trae/hooks/subagent-stop.sh +0 -47
  218. package/templates/adapters/vscode/hooks/doc-format-guard.sh +0 -282
  219. package/templates/adapters/vscode/hooks/lib/common.sh +0 -220
  220. package/templates/adapters/vscode/hooks/lib/context-inject.sh +0 -96
  221. package/templates/adapters/vscode/hooks/lib/notify.sh +0 -36
  222. package/templates/adapters/vscode/hooks/lib/preload-templates.sh +0 -212
  223. package/templates/adapters/vscode/hooks/lib/session-end.sh +0 -76
  224. package/templates/adapters/vscode/hooks/lib/state-detect.sh +0 -13
  225. package/templates/adapters/vscode/hooks/lib/subagent-stop.sh +0 -90
  226. package/templates/adapters/vscode/hooks/lib/vocabulary.sh +0 -62
  227. package/templates/adapters/vscode/hooks/notification.sh +0 -43
  228. package/templates/adapters/vscode/hooks/permission-gate.sh +0 -18
  229. package/templates/adapters/vscode/hooks/post-tool-failure.sh +0 -28
  230. package/templates/adapters/vscode/hooks/post-tool-use.sh +0 -35
  231. package/templates/adapters/vscode/hooks/pre-compact.sh +0 -37
  232. package/templates/adapters/vscode/hooks/pre-tool-use.sh +0 -153
  233. package/templates/adapters/vscode/hooks/prompt-submit.sh +0 -99
  234. package/templates/adapters/vscode/hooks/review-checklist.sh +0 -10
  235. package/templates/adapters/vscode/hooks/session-end.sh +0 -13
  236. package/templates/adapters/vscode/hooks/session-start.sh +0 -35
  237. package/templates/adapters/vscode/hooks/stop-check.sh +0 -62
  238. package/templates/adapters/vscode/hooks/subagent-guard.sh +0 -37
  239. package/templates/adapters/vscode/hooks/subagent-stop.sh +0 -12
  240. package/templates/core/hooks/doc-format-guard.sh +0 -343
  241. package/templates/core/hooks/lib/common.sh +0 -220
  242. package/templates/core/hooks/lib/context-inject.sh +0 -96
  243. package/templates/core/hooks/lib/notify.sh +0 -36
  244. package/templates/core/hooks/lib/preload-templates.sh +0 -212
  245. package/templates/core/hooks/lib/session-end.sh +0 -76
  246. package/templates/core/hooks/lib/state-detect.sh +0 -10
  247. package/templates/core/hooks/lib/subagent-stop.sh +0 -90
  248. package/templates/core/hooks/lib/vocabulary.sh +0 -62
  249. package/templates/core/hooks/notification.sh +0 -45
  250. package/templates/core/hooks/permission-gate.sh +0 -18
  251. package/templates/core/hooks/post-tool-failure.sh +0 -10
  252. package/templates/core/hooks/post-tool-use.sh +0 -138
  253. package/templates/core/hooks/pre-compact.sh +0 -37
  254. package/templates/core/hooks/pre-tool-use.sh +0 -223
  255. package/templates/core/hooks/prompt-submit.sh +0 -87
  256. package/templates/core/hooks/review-checklist.sh +0 -10
  257. package/templates/core/hooks/session-end.sh +0 -38
  258. package/templates/core/hooks/session-start.sh +0 -53
  259. package/templates/core/hooks/stop-check.sh +0 -54
  260. package/templates/core/hooks/subagent-guard.sh +0 -15
  261. package/templates/core/hooks/subagent-stop.sh +0 -47
  262. package/templates/shared/hooks-lib/common.sh +0 -22
@@ -1,71 +0,0 @@
1
- #!/bin/bash
2
- # stop-check.sh — Codex Stop:四象限分流 + 验收检查
3
- set -euo pipefail
4
-
5
- HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
- export CURRENT_MAGIC=".codex"
7
- export MAGIC_DIR=".codex"
8
- export PROJECT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
9
- COMMON_LIB="$HOOK_DIR/lib/common.sh"
10
- [ -f "$COMMON_LIB" ] && source "$COMMON_LIB"
11
-
12
- input=$(cat 2>/dev/null || echo '{}')
13
-
14
- # Stop 已经续跑过一次时不再次阻断,避免递归 continuation。
15
- if echo "$input" | jq -e '.stop_hook_active == true' >/dev/null 2>&1; then
16
- exit 0
17
- fi
18
-
19
- _system_message() {
20
- local message="$1"
21
- jq -nc --arg message "$message" '{systemMessage: $message}'
22
- }
23
-
24
- state=$(detect_active_add 2>/dev/null || true)
25
- has_dev=$(has_dev_action 2>/dev/null && echo "true" || echo "false")
26
-
27
- # DB 是 lifecycle 真相源;不可用时 fail closed,不能谎报“无活跃 Plan”。
28
- if [ "${state%%::*}" = "__STATUS_UNAVAILABLE__" ]; then
29
- reason=$(echo "$state" | awk -F'::' '{print $2}')
30
- printf '%s\n' "[ADD Stop] ⛔ Plan status 暂不可用(${reason})。未回退 Handoff/add-route 猜测,请恢复数据库或 MCP resolver 后重试。" >&2
31
- exit "${EXIT_BLOCK:-2}"
32
- fi
33
-
34
- # Q1: 无 ADD + 无 dev → exit 0 且 stdout 为空。
35
- if [ -z "$state" ] && [ "$has_dev" != "true" ]; then
36
- exit 0
37
- fi
38
-
39
- # Q2: 无 ADD + 有 dev → exit 2,stderr 成为 continuation reason。
40
- if [ -z "$state" ] && [ "$has_dev" = "true" ]; then
41
- build_stop_context "no_add_has_dev" "" >&2
42
- exit "${EXIT_BLOCK:-2}"
43
- fi
44
-
45
- plan=$(echo "$state" | awk -F'::' '{print $1}')
46
- step=$(echo "$state" | awk -F'::' '{print $2}')
47
- approval=$(echo "$state" | awk -F'::' '{print $3}')
48
-
49
- # Q3: 有 ADD + 无 dev → 合法 JSON,不输出纯文本。
50
- if [ "$has_dev" != "true" ]; then
51
- _system_message "[ADD Stop] Plan: ${plan};approval: ${approval};tasks: ${step}。本次无代码改动。"
52
- exit 0
53
- fi
54
-
55
- # Q4: 有 ADD + 有 dev。DB progress 决定本轮是否仍有任务;Handoff 只在
56
- # Step 8 生成,不能参与 active 判定,也不能因缺失而把 ACTIVE Plan 判成无 Plan。
57
- done_tasks=${step%%/*}
58
- total_tasks=${step##*/}
59
- if [[ "$done_tasks" =~ ^[0-9]+$ ]] && [[ "$total_tasks" =~ ^[0-9]+$ ]] && [ "$done_tasks" -lt "$total_tasks" ]; then
60
- cat >&2 <<EOF
61
- [ADD Stop] ⚠️ 当前 DB Plan 仍为 ACTIVE,任务进度 ${done_tasks}/${total_tasks}:${plan}
62
-
63
- 请继续执行当前 Plan 的未完成 Task,并为本轮改动补齐 record_dev_operation 审计。
64
- Handoff 只在 Step 8 生成;本提示没有使用 Handoff/add-route 猜测 active 状态。
65
- EOF
66
- exit "${EXIT_BLOCK:-2}"
67
- fi
68
-
69
- clear_dev_action 2>/dev/null || true
70
- _system_message "[ADD Stop] DB Plan ${plan} 的 tasks 已完成(${done_tasks}/${total_tasks});可进入 Review/closure。"
71
- exit 0
@@ -1,15 +0,0 @@
1
- #!/bin/bash
2
- # SubagentStart/SubagentStop — 子代理门禁(Claude Code 适配)
3
- set -euo pipefail
4
-
5
- HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
- SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
- [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
-
9
- input=$(parse_input)
10
- event=$(echo "$input" | jq -r '.hook_event_name // empty')
11
-
12
- if [ "$event" = "SubagentStart" ]; then
13
- echo "[ADD SubagentGuard] 子代理启动,请确认其遵循 ADD 规范。"
14
- fi
15
- exit 0
@@ -1,47 +0,0 @@
1
- #!/bin/bash
2
- # SubagentStop — 通用版(核心模板,Trae 等适配器继承)
3
- # 治理卡位 #11: 子agent边界校验 + 审计聚合 + 阻断
4
- set -euo pipefail
5
-
6
- HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
7
- COMMON_LIB="$HOOK_DIR/lib/common.sh"
8
- [ -f "$COMMON_LIB" ] && source "$COMMON_LIB"
9
-
10
- export PROJECT_DIR="$PWD"
11
-
12
- input=$(cat 2>/dev/null || echo "{}")
13
- agent_name=$(echo "$input" | jq -r '.agent_type // .subagent_name // "unknown"' 2>/dev/null || echo "unknown")
14
-
15
- # ── ① 边界校验:检查子 agent 交付物是否超出 spec 范围 ──
16
- state=$(detect_active_add 2>/dev/null || true)
17
- if [ -z "$state" ]; then
18
- echo "[ADD SubagentStop] ⚠️ ${agent_name} 已完成,但无活跃 ADD Plan 无法校验边界" >&2
19
- exit 0
20
- fi
21
-
22
- plan_kw=$(echo "$state" | awk -F'::' '{print $1}')
23
- handoff=$(echo "$state" | awk -F'::' '{print $4}')
24
-
25
- echo "[ADD SubagentStop] ${agent_name} 已完成 — 关联 Plan: ${plan_kw}" >&2
26
-
27
- # 如果 handoff 中存在允许的文件清单,检查交付物是否越界
28
- if [ -f "$handoff" ]; then
29
- deliverables=$(echo "$input" | jq -r '.deliverables // ""' 2>/dev/null || echo "")
30
- if [ -n "$deliverables" ]; then
31
- violations=""
32
- for f in $deliverables; do
33
- if ! grep -q "$f" "$handoff" 2>/dev/null; then
34
- violations="${violations} $f"
35
- fi
36
- done
37
- if [ -n "$violations" ]; then
38
- echo "[ADD SubagentStop] ❌ ${agent_name} 交付物超出 spec 边界: ${violations}" >&2
39
- echo "[ADD SubagentStop] 要求重做——请检查这些文件是否属于本轮 spec 范围" >&2
40
- exit "${EXIT_BLOCK:-2}"
41
- fi
42
- fi
43
- fi
44
-
45
- # ── ② 审计聚合 ──
46
- echo "[ADD SubagentStop] ${agent_name} 边界校验通过 — $(date -Iseconds)" >&2
47
- exit 0
@@ -1,341 +0,0 @@
1
- #!/bin/bash
2
- # doc-format-guard.sh — schema.json 驱动的 ADD 文档格式守卫
3
- set -euo pipefail
4
-
5
- input=$(cat)
6
-
7
- # 动态探测 MAGIC_DIR(兼容多 adapter)
8
- HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
9
- PARENT_DIR="$(dirname "$HOOK_DIR")"
10
- MAGIC_DIR="$(basename "$PARENT_DIR")"
11
- PROJECT_DIR="${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}"
12
-
13
- # ── Hook 通知: 拦截事件写入 jsonl(旁路,失败不阻断 exit 2)──
14
- source "${HOOK_DIR}/lib/notify.sh" 2>/dev/null || true
15
- ACTIVE_PLAN=$(detect_active_add 2>/dev/null || true)
16
- if [ -n "$ACTIVE_PLAN" ]; then
17
- PLAN_KEYWORD="${ACTIVE_PLAN%%::*}"
18
- PLAN_STATUS="active"
19
- else
20
- PLAN_KEYWORD="no-active-plan"
21
- PLAN_STATUS="none"
22
- fi
23
-
24
- # DEBUG: dump stdin for investigation
25
- mkdir -p "$MAGIC_DIR/debug-dump"
26
- echo "=== $(date) ===" >> "$MAGIC_DIR/debug-dump/stdin.log"
27
- echo "file_path: $(echo "$input" | jq -r '.tool_input.file_path // "EMPTY"')" >> "$MAGIC_DIR/debug-dump/stdin.log"
28
- echo "has_file_content: $(echo "$input" | jq 'has("tool_input") and (.tool_input | has("file_content"))')" >> "$MAGIC_DIR/debug-dump/stdin.log"
29
- echo "has_replacements: $(echo "$input" | jq 'has("tool_input") and (.tool_input | has("replacements"))')" >> "$MAGIC_DIR/debug-dump/stdin.log"
30
- if echo "$input" | jq -e 'has("tool_input") and (.tool_input | has("file_content"))' > /dev/null 2>&1; then
31
- echo "[file_content[500]]: $(echo "$input" | jq -r '.tool_input.file_content' | head -c 500)" >> "$MAGIC_DIR/debug-dump/stdin.log"
32
- fi
33
- if echo "$input" | jq -e 'has("tool_input") and (.tool_input | has("replacements"))' > /dev/null 2>&1; then
34
- echo "[replacement_new_text[500]]: $(echo "$input" | jq -r '.tool_input.replacements[0].new_text' | head -c 500)" >> "$MAGIC_DIR/debug-dump/stdin.log"
35
- fi
36
- echo "top_keys: $(echo "$input" | jq -r 'keys | join(", ")')" >> "$MAGIC_DIR/debug-dump/stdin.log"
37
- echo "tool_input_keys: $(echo "$input" | jq -r '.tool_input | keys | join(", ") // "NO_TOOL_INPUT"')" >> "$MAGIC_DIR/debug-dump/stdin.log"
38
- echo "=== DONE ===" >> "$MAGIC_DIR/debug-dump/stdin.log"
39
- file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
40
- # L17: 非文件工具事件(空 stdin)→ 不拦截(由 matcher 层过滤)
41
- [ -z "$file_path" ] && exit 0
42
-
43
- if ! echo "$file_path" | grep -qE "$MAGIC_DIR/(plans|specs)/"; then
44
- exit 0
45
- fi
46
-
47
- CONTENT=$(echo "$input" | jq -r '
48
- if .tool_input.file_content then .tool_input.file_content
49
- elif .tool_input.content then .tool_input.content
50
- elif .tool_input.new_string then .tool_input.new_string
51
- elif .tool_input.new_string then .tool_input.new_string
52
- elif .tool_input.replacements then .tool_input.replacements[0].new_text
53
- else "" end')
54
- # L24: PostToolUse 不可阻断(仅反馈),PreToolUse exit 2 + ask 可拦截
55
- # L24: 文件在 .qoder/(plans|specs)/ 下但 Write 工具未传 content → 无法校验,阻断
56
- if [ -z "$CONTENT" ]; then
57
- echo "⛔ 拒绝:Write 工具未传 file_content,无法校验手写文档格式" >&2
58
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"Write 工具未传 file_content,无法校验手写文档。请用 SearchReplace 改写已有文件,或用 Write 工具重试。"}}'
59
- write_hook_event "doc-format-guard" "deny" "Write" "Write 工具未传 file_content" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
60
- exit 2
61
- fi
62
-
63
- PROJECT_DIR="${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}"
64
- TEMPLATES_DIR="$PROJECT_DIR/$MAGIC_DIR/templates"
65
-
66
- # 文件名类型 token 匹配(特异性排序,取代子串 grep;R5/Task 1.4)
67
- # review/handoff 不在此层绑定(需内容特征细分单多轮/运行时),留给下方探测链
68
- TEMPLATE_NAME=""
69
- base=$(basename "$file_path")
70
- base=${base%-v[0-9]*}
71
- case "$base" in
72
- *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
73
- *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
74
- *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
75
- *handoff*) : ;; # R15: 跳过 token 层,交内容探测链;必须在 *report* 前,防含 report 的 handoff 被误吞
76
- *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
77
- *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
78
- *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
79
- *report*) TEMPLATE_NAME="report-template.md" ;;
80
- *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
81
- *spec*) TEMPLATE_NAME="spec-template.md" ;;
82
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
83
- esac
84
-
85
- # 退一步:根据文件内容特征猜测模板类型
86
- if [ -z "$TEMPLATE_NAME" ]; then
87
- if grep -q "## PLAN 元信息" <<< "$CONTENT"; then
88
- TEMPLATE_NAME="standard-plan-template.md"
89
- elif grep -q "## 一、Plan 概述" <<< "$CONTENT"; then
90
- TEMPLATE_NAME="simple-plan-template.md"
91
- elif grep -q "## 四、Handoff" <<< "$CONTENT"; then
92
- TEMPLATE_NAME="simple-plan-template.md"
93
- elif grep -q "## Review 元信息" <<< "$CONTENT"; then
94
- if grep -q "运行时验证" <<< "$CONTENT"; then
95
- TEMPLATE_NAME="review-runtime-template.md"
96
- elif grep -q "跨仓库格式契约" <<< "$CONTENT"; then
97
- TEMPLATE_NAME="review-implementation-template.md"
98
- else
99
- TEMPLATE_NAME="review-template.md"
100
- fi
101
- elif grep -q "## Why" <<< "$CONTENT"; then
102
- TEMPLATE_NAME="spec-template.md"
103
- elif grep -q "## Preconditions" <<< "$CONTENT"; then
104
- TEMPLATE_NAME="tasks-template.md"
105
- elif grep -q "审计链(证据→devlog→checklist)" <<< "$CONTENT"; then
106
- TEMPLATE_NAME="checklist-template.md"
107
- else
108
- case "$file_path" in
109
- *handoff*)
110
- # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
111
- if grep -qF "## 全局元信息" <<< "$CONTENT"; then
112
- TEMPLATE_NAME="handoff-multi-round-template.md"
113
- elif grep -qF "## 1. 交接前状态" <<< "$CONTENT"; then
114
- TEMPLATE_NAME="handoff-single-round-template.md"
115
- else
116
- echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
117
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"handoff 内容不符合 single/multi 模板规范"}}'
118
- write_hook_event "doc-format-guard" "deny" "$file_path" "handoff 模板类型无法识别" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
119
- exit 2
120
- fi
121
- ;;
122
- *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
123
- *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
124
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
125
- *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
126
- *spec*) TEMPLATE_NAME="spec-template.md" ;;
127
- *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
128
- *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
129
- *report*) TEMPLATE_NAME="report-template.md" ;;
130
- *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
131
- *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
132
- *)
133
- # 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
134
- if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
135
- echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
136
- exit 0
137
- fi
138
- echo "⛔ 拒绝:无法识别文档类型 (file_path: $file_path),缺少模板匹配规则" >&2
139
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"无法识别 ADD 文档类型,请联系管理员更新 doc-format-guard.sh"}}'
140
- write_hook_event "doc-format-guard" "deny" "$file_path" "无法识别文档类型" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
141
- exit 2
142
- ;;
143
- esac
144
- fi
145
- fi
146
-
147
- SCHEMA_FILE="$TEMPLATES_DIR/${TEMPLATE_NAME%.md}.schema.json"
148
- # L84: schema 文件不存在 → 无校验规则,阻断(不允许无规则放行)
149
- if [ ! -f "$SCHEMA_FILE" ]; then
150
- echo "⛔ 阻断:模板 ${TEMPLATE_NAME} 缺少对应的 .schema.json 校验规则" >&2
151
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"缺少 .schema.json 校验规则文件,禁止无规则放行"}}'
152
- write_hook_event "doc-format-guard" "deny" "$file_path" "缺少 .schema.json 校验规则" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
153
- exit 2
154
- fi
155
-
156
- # SearchReplace 只传 patch(replacements[].new_text),不传全文件 → 跳过章节校验
157
- IS_SEARCH_REPLACE=$(echo "$input" | jq -r '(.tool_input.replacements | length > 0) or (.tool_input.new_string != null) // false')
158
-
159
- ISSUES=""
160
-
161
- # ── 章节校验 ──
162
- # 使用项目目录下的临时文件,避免 /tmp 在沙箱中不可写导致静默跳过
163
- TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
164
- : > "$TMPFILE"
165
- trap "rm -f $TMPFILE" EXIT
166
-
167
- # ── 锚定校验(锚定制:schema 声明 anchor,参照内容从模板现场提取;Task 1.2)──
168
- # 锚点不在模板 → 跳过该规则 + stderr 告警(不阻塞写入,冒烟巡检兑底)
169
- # within 前缀匹配;heading 缺失 → 跳过规则 + 告警(不误拦)
170
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
171
- jq -r '.sections[] | select(.anchor != null) | .id + "|" + .anchor + "|" + (.within // "")' "$SCHEMA_FILE" 2>/dev/null | while IFS='|' read -r aid anchor within; do
172
- [ -z "$anchor" ] && continue
173
- ref_line=$(grep -m1 -F "$anchor" "$TEMPLATES_DIR/$TEMPLATE_NAME" 2>/dev/null || true)
174
- if [ -z "$ref_line" ]; then
175
- echo "[doc-format-guard] anchor_miss: schema ${aid} 声明的 anchor '${anchor}' 在 ${TEMPLATE_NAME} 中未定位,跳过该规则(冒烟巡检兑底)" >&2
176
- continue
177
- fi
178
- tokens=$(echo "$ref_line" | tr '#*`|(){' ' ' | tr -s ' ' '\n' | grep -v '^$' | grep -vF '{' | sort -u)
179
- [ -z "$tokens" ] && continue
180
- scope="$CONTENT"
181
- if [ -n "$within" ]; then
182
- if grep -q "^${within}" <<< "$CONTENT"; then
183
- scope=$(sed -n "/^${within}/,/^## /p" <<< "$CONTENT")
184
- else
185
- echo "[doc-format-guard] within_miss: schema ${aid} 的 within '${within}' 在文档中未定位,跳过该规则" >&2
186
- continue
187
- fi
188
- fi
189
- # 声明级缺失统计:一个锚点声明的必选 token 有任一缺失即记 1 条规则缺失(struct_score 精确语义)
190
- miss_tokens=""
191
- for tok in $tokens; do
192
- if ! grep -qF "$tok" <<< "$scope"; then
193
- miss_tokens="${miss_tokens}${tok} "
194
- fi
195
- done
196
- # if 语句(非 && 链):miss_tokens 为空时返回 0,避免 while 管道退出码非零触发 set -e
197
- if [ -n "$miss_tokens" ]; then
198
- echo " 缺锚点(${aid}): ${miss_tokens}"
199
- fi
200
- done >> "$TMPFILE"
201
- fi
202
-
203
- # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
204
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
205
- jq -r '.sections[] | select(.required == true) | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
206
- if ! grep -qF "$heading" <<< "$CONTENT"; then
207
- echo " 缺章节: $heading"
208
- fi
209
- done >> "$TMPFILE"
210
-
211
- # 子章节
212
- jq -r '.sections[].subsections[]? | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
213
- if ! grep -qF "$sub" <<< "$CONTENT"; then
214
- echo " 缺子章节: $sub"
215
- fi
216
- done >> "$TMPFILE"
217
- fi
218
-
219
- # ── 占位符校验 ──
220
- jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
221
- if grep -qF "$ph" <<< "$CONTENT"; then
222
- echo " 未替换占位符: $ph"
223
- fi
224
- done >> "$TMPFILE"
225
-
226
- # ── 结构位禁词校验(仅标题行 + schema 声明的 groupColumn;正文叙述免疫;Task 1.2)──
227
- struct_text=$(grep -E '^#{2,}[[:space:]]' <<< "$CONTENT")
228
- col=$(jq -r '.groupColumn // empty' "$SCHEMA_FILE" 2>/dev/null)
229
- if [ -n "$col" ]; then
230
- struct_text="${struct_text}
231
- $(awk -F'|' -v c="$col" 'NF>c {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $(c+1)); print $(c+1)}' <<< "$CONTENT")"
232
- fi
233
- jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
234
- # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
235
- if grep -qF "$term" <<< "$struct_text"; then
236
- echo " 结构位禁词: $term"
237
- fi
238
- done >> "$TMPFILE"
239
-
240
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
241
-
242
- # ── 算法化规则校验(ADD 范式约束下沉为代码)──
243
- ALGO_ISSUES=""
244
-
245
- # 规则1: 精简版 Plan 反作弊
246
- if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
247
- # 1a. 文件数 ≤ 3
248
- file_count=$(grep -cP '^\|\s*`[^`]+`' <<< "$CONTENT" 2>/dev/null || echo "0")
249
- if [ "$file_count" -gt 3 ] 2>/dev/null; then
250
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 涉及 ${file_count} 个文件(超过 3 个限制),应改用 standard-plan-template.md\n"
251
- fi
252
- # 1b. HITL 表不能写 "等 N 个文件"
253
- if grep -qP '等\s*\d*\s*个文件|等\s*若干' <<< "$CONTENT"; then
254
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表文件清单使用模糊描述('等 N 个文件'),必须列出实际完整路径\n"
255
- fi
256
- # 1c. HITL 方案/设计决策不能写 "等若干决策"
257
- if grep -qP '等\s*若干\s*(决策|方案|设计)' <<< "$CONTENT"; then
258
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表方案/设计决策使用模糊描述('等若干决策'),必须逐条列出\n"
259
- fi
260
- # 1d. 不能包含架构设计章节(精简版无架构设计)
261
- if grep -q '## 三、架构设计' <<< "$CONTENT"; then
262
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 包含 '## 三、架构设计' 章节,精简版不应有架构设计——应改用 standard-plan-template.md\n"
263
- fi
264
- fi
265
-
266
- # 规则2: HITL 表非空校验(所有 Plan + Review 模板)
267
- if echo "$TEMPLATE_NAME" | grep -qE 'plan|review'; then
268
- if grep -q '## HITL' <<< "$CONTENT"; then
269
- # HITL 表至少要有 1 行非占位符内容(不包含 { } 占位符)
270
- hitl_rows=$(sed -n '/## HITL/,/^## /p' <<< "$CONTENT" | grep -cP '^\|\s*[^|{]*\s*\|' 2>/dev/null || echo "0")
271
- # rows 包含表头行和分隔行,实际数据行 = rows - 2
272
- hitl_data=$((hitl_rows - 2))
273
- if [ "$hitl_data" -lt 1 ] 2>/dev/null; then
274
- ALGO_ISSUES="${ALGO_ISSUES} ⚠️ HITL 表为空——必须填写至少 1 行实际内容后再提交审核\n"
275
- fi
276
- fi
277
- fi
278
-
279
- # 规则3: 精简版 Plan 禁止同时存在独立 handoff 文件
280
- if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
281
- plan_base=$(basename "$file_path" | sed 's/-plan-v.*//')
282
- plan_dir=$(dirname "$file_path")
283
- handoff_pattern="${plan_dir}/${plan_base}-handoff"
284
- if find "$plan_dir" -name "${plan_base}-handoff*.md" 2>/dev/null | grep -q .; then
285
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版 Handoff 冲突: 检测到独立 handoff 文件(${plan_base}-handoff*.md)。精简版 Plan 的 Handoff 已融合在 §四,不应生成独立文件。请删除独立 handoff 文件或改用 standard-plan-template.md\n"
286
- fi
287
- fi
288
-
289
- if [ -n "$ALGO_ISSUES" ]; then
290
- echo "⛔ 算法化规则校验不通过:" >&2
291
- echo -e "$ALGO_ISSUES" >&2
292
- # 合并到 ISSUES 中一起阻断
293
- echo -e "$ALGO_ISSUES" >> "$TMPFILE"
294
- fi
295
-
296
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
297
-
298
- # ── 判定回流 extra(Task 1.5 数据契约:anchor_hit/struct_score 精确语义;override 由上层 hook 在人类推翻时追加)──
299
- # struct_score = (适用规则数 - 缺失规则数) / 适用规则数 × 100,适用规则从 schema 实况计数
300
- anchor_total=$(jq -r '[.sections[] | select(.anchor != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
301
- heading_total=$(jq -r '[.sections[] | select(.required == true and .heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
302
- sub_total=$(jq -r '[.sections[].subsections[]? | select(.heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
303
- term_total=$(jq -r '.forbidden_terms | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
304
- if [ "$IS_SEARCH_REPLACE" = "true" ]; then
305
- applied=$term_total # patch 路径仅禁词规则适用
306
- else
307
- applied=$(( anchor_total + heading_total + sub_total + term_total ))
308
- fi
309
- missed=$(wc -l < "$TMPFILE" 2>/dev/null | tr -d ' ')
310
- missed=${missed:-0}
311
- anchor_hit=true
312
- grep -q "缺锚点" "$TMPFILE" 2>/dev/null && anchor_hit=false
313
- struct_score=100
314
- if [ "$applied" -gt 0 ] 2>/dev/null && [ "$missed" -gt 0 ] 2>/dev/null; then
315
- struct_score=$(( (applied - missed) * 100 / applied ))
316
- [ "$struct_score" -lt 0 ] && struct_score=0
317
- fi
318
- BACKFLOW_EXTRA="\"anchor_hit\":${anchor_hit},\"struct_score\":${struct_score}"
319
-
320
- # ── 阻断或放行 ──
321
- if [ -n "$ISSUES" ]; then
322
- echo "⛔ $TEMPLATE_NAME 校验不通过:
323
- $ISSUES" >&2
324
- # 明细注入 stdout reason(去引号防 JSON 破坏,截断 180 字),让被拦方第一时间知道改什么
325
- ISSUES_BRIEF=$(printf '%s' "$ISSUES" | tr '\n' ' ' | tr -d '"' | tr -s ' ' | cut -c1-180)
326
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过: ${ISSUES_BRIEF}\"}}"
327
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
328
- exit 2
329
- fi
330
-
331
- # ── 自动更新 index.md ──
332
- if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
333
- if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
334
- "$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
335
- fi
336
- fi
337
-
338
- # 放行回流(Task 1.5:anchor_hit/struct_score 正样本底座)
339
- write_hook_event "doc-format-guard" "allow" "$file_path" "校验通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
340
-
341
- exit 0