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
@@ -0,0 +1,11 @@
1
+ // notification.ts — Notification 入口薄壳(Task 2.2/3.1 继承体系)
2
+ // 治理逻辑: governance/notification-router.js(NotificationRouter 探测循环 + reviews 提醒)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { readHookInput } from "../governance/common.js"
6
+ import { NotificationRouter } from "../governance/notification-router.js"
7
+
8
+ const input = readHookInput()
9
+ const PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
10
+
11
+ process.exit(new NotificationRouter(PROJECT_DIR, ".add").run(input))
@@ -0,0 +1,7 @@
1
+ // permission-gate.ts — PermissionRequest 入口薄壳(Task 5.1 继承体系)
2
+ // 治理逻辑: governance/permission-gate-router.js(PermissionGateRouter——jq 语义解析 + emit 输出)
3
+ // 入口职责: 仅进程语义(<50 行),治理 0 复制。
4
+
5
+ import { runPermissionGate } from "../governance/permission-gate-router.js"
6
+
7
+ runPermissionGate()
@@ -0,0 +1,7 @@
1
+ // post-tool-failure.ts — PostToolUseFailure 入口薄壳(Task 5.1 继承体系)
2
+ // 治理逻辑: governance/post-tool-failure-router.js(PostToolFailureRouter——jq 语义解析 + emit 输出)
3
+ // 入口职责: 仅进程语义(<50 行),治理 0 复制。
4
+
5
+ import { runPostToolFailure } from "../governance/post-tool-failure-router.js"
6
+
7
+ runPostToolFailure()
@@ -0,0 +1,23 @@
1
+ // post-tool-use.ts — PostToolUse 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/post-tool-router.ts(PostToolRouter §1 DPS 哨兵 / §2 文档守卫 / §3 Bash 增强)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { jsonGet, readHookInput } from "../governance/common.js"
6
+ import { PostToolRouter } from "../governance/post-tool-router.js"
7
+
8
+ const input = readHookInput()
9
+ let toolName = jsonGet(input, "tool_name")
10
+ if (toolName === "") {
11
+ // bash 版此处 grep 兜底失败触发 set -e 崩溃(exit 1)——缺陷行为,
12
+ // TS 版修复:无 tool_name 的 PostToolUse 事件静默放行(冒烟契约 ∈ {0,2})。
13
+ const fallback = input.match(/"tool_name"\s*:\s*"([^"]*)"/)?.[1] ?? ""
14
+ if (fallback === "") {
15
+ process.exit(0)
16
+ }
17
+ toolName = fallback
18
+ }
19
+
20
+ const PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
21
+ const MAGIC_DIR = process.env.MAGIC_DIR || ".qoder"
22
+
23
+ process.exit(new PostToolRouter(PROJECT_DIR, MAGIC_DIR).run(input, toolName))
@@ -0,0 +1,9 @@
1
+ // pre-compact.ts — 上下文压缩前 ADD 状态保存(入口薄壳,Task 2.2/3.1 继承体系)
2
+ // 治理逻辑: governance/pre-compact-guard.js(PreCompactGuard 状态保存 + tpl 标记清理)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { PreCompactGuard } from "../governance/pre-compact-guard.js"
6
+
7
+ process.env.PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
8
+
9
+ process.exit(new PreCompactGuard().run())
@@ -0,0 +1,30 @@
1
+ // pre-tool-use.ts — PreToolUse 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/pre-tool-guard.ts(PreToolUseGuard 服务类,规则消费 hook-guard-rules.toml)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { jsonGet, readHookInput, tryResolveMagicDir } from "../governance/common.js"
6
+ import { PreToolUseGuard } from "../governance/pre-tool-guard.js"
7
+
8
+ const input = readHookInput()
9
+ const PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
10
+
11
+ // 探测 MAGIC_DIR(唯一解析链:注入优先 → 物理位置推导)
12
+ let MAGIC_DIR = process.env.MAGIC_DIR
13
+ if (!MAGIC_DIR) {
14
+ MAGIC_DIR = tryResolveMagicDir() || ".qoder"
15
+ }
16
+
17
+ const guard = new PreToolUseGuard(PROJECT_DIR, MAGIC_DIR)
18
+
19
+ const toolName = jsonGet(input, "tool_name")
20
+ const command = jsonGet(input, "command")
21
+
22
+ let exitCode = 0
23
+ if (command !== "") {
24
+ exitCode = guard.runSectionA(input)
25
+ if (exitCode !== 0) process.exit(exitCode)
26
+ } else {
27
+ exitCode = guard.runSectionB(input, toolName)
28
+ if (exitCode !== 0) process.exit(exitCode)
29
+ }
30
+ process.exit(0)
@@ -0,0 +1,11 @@
1
+ // prompt-submit.ts — UserPromptSubmit 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/prompt-router.ts(PromptRouter L1/L2/L3 分流,日报阈值消费 hook-event-rules.toml)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { readHookInput } from "../governance/common.js"
6
+ import { PromptRouter } from "../governance/prompt-router.js"
7
+
8
+ const input = readHookInput()
9
+ const MAGIC_DIR = process.env.MAGIC_DIR || ".qoder"
10
+
11
+ process.exit(new PromptRouter(MAGIC_DIR).run(input))
@@ -0,0 +1,4 @@
1
+ // review-checklist.ts — Review 检查清单校验(bash 版 review-checklist.sh 的 TS 同语义实现)
2
+
3
+ process.stdout.write("[ADD ReviewChecklist] 请在提交前确认 checklist.md 全部项已勾选。\n")
4
+ process.exit(0)
@@ -0,0 +1,10 @@
1
+ // session-end.ts — SessionEnd 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/session-end.ts(SessionEndGuard 清理 + 结算 + 兜底)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { SessionEndGuard } from "../governance/session-end.js"
6
+
7
+ const PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
8
+ process.env.PROJECT_DIR = PROJECT_DIR
9
+
10
+ process.exit(new SessionEndGuard(PROJECT_DIR).run())
@@ -0,0 +1,9 @@
1
+ // session-start.ts — SessionStart 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/session-start-guard.ts(SessionStartGuard 状态恢复 + 模板索引 + 代办 + HITL)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { SessionStartGuard } from "../governance/session-start-guard.js"
6
+
7
+ const PROJECT_DIR = process.env.PROJECT_DIR || process.cwd()
8
+
9
+ process.exit(new SessionStartGuard(PROJECT_DIR).run())
@@ -0,0 +1,14 @@
1
+ // stop-check.ts — Stop 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/stop-router.ts(StopRouter 四象限分流,象限文本消费 hook-context-rules.toml)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { tryResolveMagicDir } from "../governance/common.js"
6
+ import { StopRouter } from "../governance/stop-router.js"
7
+
8
+ // 对齐 bash: MAGIC_DIR 从物理位置推导并 export(入口注入优先)
9
+ const inferredMagicDir = tryResolveMagicDir()
10
+ if (inferredMagicDir && !process.env.MAGIC_DIR) {
11
+ process.env.MAGIC_DIR = inferredMagicDir
12
+ }
13
+
14
+ process.exit(new StopRouter().run())
@@ -0,0 +1,8 @@
1
+ // subagent-guard.ts — SubagentStart 入口薄壳(Task 3.1 能力对齐)
2
+ // 治理逻辑: governance/subagent-guard-router.js(SubagentGuardRouter 状态注入——对齐强版语义)
3
+ // 能力对齐注: 原 core 为弱版(事件提示空话),已对齐 claude/vscode/qoder 强版(状态注入)
4
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
5
+
6
+ import { SubagentGuardRouter } from "../governance/subagent-guard-router.js"
7
+
8
+ process.exit(new SubagentGuardRouter().run())
@@ -0,0 +1,9 @@
1
+ // subagent-stop.ts — SubagentStop 入口薄壳(Task 2.2 薄壳化)
2
+ // 治理逻辑: lib/subagent-stop-router.ts(SubagentStopRouter 边界校验 + 审计聚合)
3
+ // 入口职责: 仅解析 + 进程语义(<50 行),治理 0 复制。
4
+
5
+ import { readHookInput } from "../governance/common.js"
6
+ import { SubagentStopRouter } from "../governance/subagent-stop-router.js"
7
+
8
+ const input = readHookInput()
9
+ process.exit(new SubagentStopRouter().run(input))
@@ -109,7 +109,18 @@ export function registerCheckRahs(server: ToolRegistrar) {
109
109
  try {
110
110
  // Spec 合规:checklist [T] 项勾选率([R] 运行时项不计入)
111
111
  const specDir = join(PROJECT_ROOT, MAGIC_DIR, "specs");
112
- const sn = basename(pm, ".md").replace(/-plan-v\d+$/i, "");
112
+ // spec 目录名可能带 -vN 版本后缀:优先带版本匹配,回退不带版本 [Task 1.8 修复]
113
+ const rahsPlanBase = basename(pm, ".md").replace(/-plan-v\d+$/i, "");
114
+ const rahsVersionSuffix = /-plan-(v\d+)$/i.exec(basename(pm, ".md"))?.[1] ?? "";
115
+ const rahsCandidates = rahsVersionSuffix
116
+ ? [`${rahsPlanBase}-${rahsVersionSuffix}`, rahsPlanBase]
117
+ : [rahsPlanBase];
118
+ const planContent = (await readFileSafe(join(plansDir, pm))) || "";
119
+ const rahsSpecRef = planContent.match(/specs\/([^/`\s]+)/);
120
+ if (rahsSpecRef) rahsCandidates.push(rahsSpecRef[1]);
121
+ const sn =
122
+ rahsCandidates.find((d) => existsSync(join(specDir, d))) ??
123
+ rahsCandidates[0];
113
124
  const clPath = join(specDir, sn, "checklist.md");
114
125
  if (existsSync(clPath)) {
115
126
  const cl = (await readFileSafe(clPath)) || "";
@@ -75,8 +75,18 @@ export function registerPlanTools(server: ToolRegistrar) {
75
75
  // 提取 planKeyword(关键词: xxx)
76
76
  const kwMatch = content.match(/关键词[::]\s*(.+)/) || content.match(/planKeyword[::]\s*"?([^"\n]+)"?/)
77
77
  const keyword = kwMatch?.[1]?.trim() || t.name
78
- // 定位 spec dir
79
- const specDirName = basename(t.name).replace(/-plan-v\d+$/, "")
78
+ // 定位 spec dir(目录名可能带 -vN 版本后缀:优先带版本匹配,再回退不带版本,
79
+ // 最后回退解析 Plan §六 关联文档 Spec 路径真源)[Task 1.8 修复]
80
+ const planBase = basename(t.name).replace(/-plan-v\d+$/, "")
81
+ const versionSuffix = /-plan-(v\d+)$/.exec(basename(t.name))?.[1] ?? ""
82
+ const specCandidates = versionSuffix
83
+ ? [`${planBase}-${versionSuffix}`, planBase]
84
+ : [planBase]
85
+ const specRef = content.match(/specs\/([^/`\s]+)/)
86
+ if (specRef) specCandidates.push(specRef[1])
87
+ const specDirName =
88
+ specCandidates.find((d) => existsSync(join(specsDir, d))) ??
89
+ specCandidates[0]
80
90
  const tasksPath = join(specsDir, specDirName, "tasks.md")
81
91
  const checklistPath = join(specsDir, specDirName, "checklist.md")
82
92
  const specPath = join(specsDir, specDirName, "spec.md")
@@ -1,283 +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="$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="$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 "## 四、Handoff" <<< "$CONTENT"; then
88
- TEMPLATE_NAME="simple-standard-plan-template.md"
89
- elif grep -q "## PLAN 元信息" <<< "$CONTENT"; then
90
- TEMPLATE_NAME="standard-plan-template.md"
91
- elif grep -q "## Review 元信息" <<< "$CONTENT"; then
92
- if grep -q "运行时验证" <<< "$CONTENT"; then
93
- TEMPLATE_NAME="review-runtime-template.md"
94
- elif grep -q "跨仓库格式契约" <<< "$CONTENT"; then
95
- TEMPLATE_NAME="review-implementation-template.md"
96
- else
97
- TEMPLATE_NAME="review-template.md"
98
- fi
99
- elif grep -q "## Why" <<< "$CONTENT"; then
100
- TEMPLATE_NAME="spec-template.md"
101
- elif grep -q "## Preconditions" <<< "$CONTENT"; then
102
- TEMPLATE_NAME="tasks-template.md"
103
- elif grep -q "审计链(证据→devlog→checklist)" <<< "$CONTENT"; then
104
- TEMPLATE_NAME="checklist-template.md"
105
- else
106
- case "$file_path" in
107
- *handoff*)
108
- # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
109
- if grep -qF "## 全局元信息" <<< "$CONTENT"; then
110
- TEMPLATE_NAME="handoff-multi-round-template.md"
111
- elif grep -qF "## 1. 交接前状态" <<< "$CONTENT"; then
112
- TEMPLATE_NAME="handoff-single-round-template.md"
113
- else
114
- echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
115
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"handoff 内容不符合 single/multi 模板规范"}}'
116
- write_hook_event "doc-format-guard" "deny" "$file_path" "handoff 模板类型无法识别" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
117
- exit 2
118
- fi
119
- ;;
120
- *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
121
- *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
122
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
123
- *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
124
- *spec*) TEMPLATE_NAME="spec-template.md" ;;
125
- *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
126
- *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
127
- *report*) TEMPLATE_NAME="report-template.md" ;;
128
- *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
129
- *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
130
- *)
131
- # 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
132
- if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
133
- echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
134
- exit 0
135
- fi
136
- echo "⛔ 拒绝:无法识别文档类型 (file_path: $file_path),缺少模板匹配规则" >&2
137
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"无法识别 ADD 文档类型,请联系管理员更新 doc-format-guard.sh"}}'
138
- write_hook_event "doc-format-guard" "deny" "$file_path" "无法识别文档类型" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
139
- exit 2
140
- ;;
141
- esac
142
- fi
143
- fi
144
-
145
- SCHEMA_FILE="$TEMPLATES_DIR/${TEMPLATE_NAME%.md}.schema.json"
146
- # L84: schema 文件不存在 → 无校验规则,阻断(不允许无规则放行)
147
- if [ ! -f "$SCHEMA_FILE" ]; then
148
- echo "⛔ 阻断:模板 ${TEMPLATE_NAME} 缺少对应的 .schema.json 校验规则" >&2
149
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"缺少 .schema.json 校验规则文件,禁止无规则放行"}}'
150
- write_hook_event "doc-format-guard" "deny" "$file_path" "缺少 .schema.json 校验规则" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
151
- exit 2
152
- fi
153
-
154
- # SearchReplace 只传 patch(replacements[].new_text),不传全文件 → 跳过章节校验
155
- IS_SEARCH_REPLACE=$(echo "$input" | jq -r '(.tool_input.replacements | length > 0) or (.tool_input.new_string != null) // false')
156
-
157
- ISSUES=""
158
-
159
- # ── 章节校验 ──
160
- # 使用项目目录下的临时文件,避免 /tmp 在沙箱中不可写导致静默跳过
161
- TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
162
- : > "$TMPFILE"
163
- trap "rm -f $TMPFILE" EXIT
164
-
165
- # ── 锚定校验(锚定制:schema 声明 anchor,参照内容从模板现场提取;Task 1.2)──
166
- # 锚点不在模板 → 跳过该规则 + stderr 告警(不阻塞写入,冒烟巡检兑底)
167
- # within 前缀匹配;heading 缺失 → 跳过规则 + 告警(不误拦)
168
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
169
- jq -r '.sections[] | select(.anchor != null) | .id + "|" + .anchor + "|" + (.within // "")' "$SCHEMA_FILE" 2>/dev/null | while IFS='|' read -r aid anchor within; do
170
- [ -z "$anchor" ] && continue
171
- ref_line=$(grep -m1 -F "$anchor" "$TEMPLATES_DIR/$TEMPLATE_NAME" 2>/dev/null || true)
172
- if [ -z "$ref_line" ]; then
173
- echo "[doc-format-guard] anchor_miss: schema ${aid} 声明的 anchor '${anchor}' 在 ${TEMPLATE_NAME} 中未定位,跳过该规则(冒烟巡检兑底)" >&2
174
- continue
175
- fi
176
- tokens=$(echo "$ref_line" | tr '#*`|(){' ' ' | tr -s ' ' '\n' | grep -v '^$' | grep -vF '{' | sort -u)
177
- [ -z "$tokens" ] && continue
178
- scope="$CONTENT"
179
- if [ -n "$within" ]; then
180
- if grep -q "^${within}" <<< "$CONTENT"; then
181
- scope=$(sed -n "/^${within}/,/^## /p" <<< "$CONTENT")
182
- else
183
- echo "[doc-format-guard] within_miss: schema ${aid} 的 within '${within}' 在文档中未定位,跳过该规则" >&2
184
- continue
185
- fi
186
- fi
187
- # 声明级缺失统计:一个锚点声明的必选 token 有任一缺失即记 1 条规则缺失(struct_score 精确语义)
188
- miss_tokens=""
189
- for tok in $tokens; do
190
- if ! grep -qF "$tok" <<< "$scope"; then
191
- miss_tokens="${miss_tokens}${tok} "
192
- fi
193
- done
194
- # if 语句(非 && 链):miss_tokens 为空时返回 0,避免 while 管道退出码非零触发 set -e
195
- if [ -n "$miss_tokens" ]; then
196
- echo " 缺锚点(${aid}): ${miss_tokens}"
197
- fi
198
- done >> "$TMPFILE"
199
- fi
200
-
201
- # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
202
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
203
- jq -r '.sections[] | select(.required == true) | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
204
- if ! grep -qF "$heading" <<< "$CONTENT"; then
205
- echo " 缺章节: $heading"
206
- fi
207
- done >> "$TMPFILE"
208
-
209
- # 子章节
210
- jq -r '.sections[].subsections[]? | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
211
- if ! grep -qF "$sub" <<< "$CONTENT"; then
212
- echo " 缺子章节: $sub"
213
- fi
214
- done >> "$TMPFILE"
215
- fi
216
-
217
- # ── 占位符校验 ──
218
- jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
219
- if grep -qF "$ph" <<< "$CONTENT"; then
220
- echo " 未替换占位符: $ph"
221
- fi
222
- done >> "$TMPFILE"
223
-
224
- # ── 结构位禁词校验(仅标题行 + schema 声明的 groupColumn;正文叙述免疫;Task 1.2)──
225
- struct_text=$(grep -E '^#{2,}[[:space:]]' <<< "$CONTENT")
226
- col=$(jq -r '.groupColumn // empty' "$SCHEMA_FILE" 2>/dev/null)
227
- if [ -n "$col" ]; then
228
- struct_text="${struct_text}
229
- $(awk -F'|' -v c="$col" 'NF>c {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $(c+1)); print $(c+1)}' <<< "$CONTENT")"
230
- fi
231
- jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
232
- # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
233
- if grep -qF "$term" <<< "$struct_text"; then
234
- echo " 结构位禁词: $term"
235
- fi
236
- done >> "$TMPFILE"
237
-
238
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
239
-
240
- # ── 判定回流 extra(Task 1.5 数据契约:anchor_hit/struct_score 精确语义;override 由上层 hook 在人类推翻时追加)──
241
- # struct_score = (适用规则数 - 缺失规则数) / 适用规则数 × 100,适用规则从 schema 实况计数
242
- anchor_total=$(jq -r '[.sections[] | select(.anchor != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
243
- heading_total=$(jq -r '[.sections[] | select(.required == true and .heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
244
- sub_total=$(jq -r '[.sections[].subsections[]? | select(.heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
245
- term_total=$(jq -r '.forbidden_terms | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
246
- if [ "$IS_SEARCH_REPLACE" = "true" ]; then
247
- applied=$term_total # patch 路径仅禁词规则适用
248
- else
249
- applied=$(( anchor_total + heading_total + sub_total + term_total ))
250
- fi
251
- missed=$(wc -l < "$TMPFILE" 2>/dev/null | tr -d ' ')
252
- missed=${missed:-0}
253
- anchor_hit=true
254
- grep -q "缺锚点" "$TMPFILE" 2>/dev/null && anchor_hit=false
255
- struct_score=100
256
- if [ "$applied" -gt 0 ] 2>/dev/null && [ "$missed" -gt 0 ] 2>/dev/null; then
257
- struct_score=$(( (applied - missed) * 100 / applied ))
258
- [ "$struct_score" -lt 0 ] && struct_score=0
259
- fi
260
- BACKFLOW_EXTRA="\"anchor_hit\":${anchor_hit},\"struct_score\":${struct_score}"
261
-
262
- # ── 阻断或放行 ──
263
- if [ -n "$ISSUES" ]; then
264
- echo "⛔ $TEMPLATE_NAME 校验不通过:
265
- $ISSUES" >&2
266
- # 明细注入 stdout reason(去引号防 JSON 破坏,截断 180 字),让被拦方第一时间知道改什么
267
- ISSUES_BRIEF=$(printf '%s' "$ISSUES" | tr '\n' ' ' | tr -d '"' | tr -s ' ' | cut -c1-180)
268
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过: ${ISSUES_BRIEF}\"}}"
269
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
270
- exit 2
271
- fi
272
-
273
- # ── 自动更新 index.md ──
274
- if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
275
- if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
276
- "$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
277
- fi
278
- fi
279
-
280
- # 放行回流(Task 1.5:anchor_hit/struct_score 正样本底座)
281
- write_hook_event "doc-format-guard" "allow" "$file_path" "校验通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
282
-
283
- exit 0