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,343 +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=".trae"
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 "## 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
- # 参照行剖 token:去 markdown 符号与括号 → 空白切分 → 去空 → 去占位符 token → 去重
179
- tokens=$(echo "$ref_line" | tr '#*`|(){' ' ' | tr -s ' ' '\n' | grep -v '^$' | grep -vF '{' | sort -u)
180
- [ -z "$tokens" ] && continue
181
- # within 作用域(前缀匹配,heading 缺失 → 跳过该规则 + 告警)
182
- scope="$CONTENT"
183
- if [ -n "$within" ]; then
184
- if grep -q "^${within}" <<< "$CONTENT"; then
185
- scope=$(sed -n "/^${within}/,/^## /p" <<< "$CONTENT")
186
- else
187
- echo "[doc-format-guard] within_miss: schema ${aid} 的 within '${within}' 在文档中未定位,跳过该规则" >&2
188
- continue
189
- fi
190
- fi
191
- # 声明级缺失统计:一个锚点声明的必选 token 有任一缺失即记 1 条规则缺失(struct_score 精确语义)
192
- miss_tokens=""
193
- for tok in $tokens; do
194
- if ! grep -qF "$tok" <<< "$scope"; then
195
- miss_tokens="${miss_tokens}${tok} "
196
- fi
197
- done
198
- # if 语句(非 && 链):miss_tokens 为空时返回 0,避免 while 管道退出码非零触发 set -e
199
- if [ -n "$miss_tokens" ]; then
200
- echo " 缺锚点(${aid}): ${miss_tokens}"
201
- fi
202
- done >> "$TMPFILE"
203
- fi
204
-
205
- # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
206
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
207
- jq -r '.sections[] | select(.required == true) | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
208
- if ! grep -qF "$heading" <<< "$CONTENT"; then
209
- echo " 缺章节: $heading"
210
- fi
211
- done >> "$TMPFILE"
212
-
213
- # 子章节
214
- jq -r '.sections[].subsections[]? | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
215
- if ! grep -qF "$sub" <<< "$CONTENT"; then
216
- echo " 缺子章节: $sub"
217
- fi
218
- done >> "$TMPFILE"
219
- fi
220
-
221
- # ── 占位符校验 ──
222
- jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
223
- if grep -qF "$ph" <<< "$CONTENT"; then
224
- echo " 未替换占位符: $ph"
225
- fi
226
- done >> "$TMPFILE"
227
-
228
- # ── 结构位禁词校验(仅标题行 + schema 声明的 groupColumn;正文叙述免疫;Task 1.2)──
229
- struct_text=$(grep -E '^#{2,}[[:space:]]' <<< "$CONTENT")
230
- col=$(jq -r '.groupColumn // empty' "$SCHEMA_FILE" 2>/dev/null)
231
- if [ -n "$col" ]; then
232
- struct_text="${struct_text}
233
- $(awk -F'|' -v c="$col" 'NF>c {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $(c+1)); print $(c+1)}' <<< "$CONTENT")"
234
- fi
235
- jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
236
- # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
237
- if grep -qF "$term" <<< "$struct_text"; then
238
- echo " 结构位禁词: $term"
239
- fi
240
- done >> "$TMPFILE"
241
-
242
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
243
-
244
- # ── 算法化规则校验(ADD 范式约束下沉为代码)──
245
- ALGO_ISSUES=""
246
-
247
- # 规则1: 精简版 Plan 反作弊
248
- if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
249
- # 1a. 文件数 ≤ 3
250
- file_count=$(grep -cP '^\|\s*`[^`]+`' <<< "$CONTENT" 2>/dev/null || echo "0")
251
- if [ "$file_count" -gt 3 ] 2>/dev/null; then
252
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 涉及 ${file_count} 个文件(超过 3 个限制),应改用 standard-plan-template.md\n"
253
- fi
254
- # 1b. HITL 表不能写 "等 N 个文件"
255
- if grep -qP '等\s*\d*\s*个文件|等\s*若干' <<< "$CONTENT"; then
256
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表文件清单使用模糊描述('等 N 个文件'),必须列出实际完整路径\n"
257
- fi
258
- # 1c. HITL 方案/设计决策不能写 "等若干决策"
259
- if grep -qP '等\s*若干\s*(决策|方案|设计)' <<< "$CONTENT"; then
260
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表方案/设计决策使用模糊描述('等若干决策'),必须逐条列出\n"
261
- fi
262
- # 1d. 不能包含架构设计章节(精简版无架构设计)
263
- if grep -q '## 三、架构设计' <<< "$CONTENT"; then
264
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 包含 '## 三、架构设计' 章节,精简版不应有架构设计——应改用 standard-plan-template.md\n"
265
- fi
266
- fi
267
-
268
- # 规则2: HITL 表非空校验(所有 Plan + Review 模板)
269
- if echo "$TEMPLATE_NAME" | grep -qE 'plan|review'; then
270
- if grep -q '## HITL' <<< "$CONTENT"; then
271
- # HITL 表至少要有 1 行非占位符内容(不包含 { } 占位符)
272
- hitl_rows=$(sed -n '/## HITL/,/^## /p' <<< "$CONTENT" | grep -cP '^\|\s*[^|{]*\s*\|' 2>/dev/null || echo "0")
273
- # rows 包含表头行和分隔行,实际数据行 = rows - 2
274
- hitl_data=$((hitl_rows - 2))
275
- if [ "$hitl_data" -lt 1 ] 2>/dev/null; then
276
- ALGO_ISSUES="${ALGO_ISSUES} ⚠️ HITL 表为空——必须填写至少 1 行实际内容后再提交审核\n"
277
- fi
278
- fi
279
- fi
280
-
281
- # 规则3: 精简版 Plan 禁止同时存在独立 handoff 文件
282
- if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
283
- plan_base=$(basename "$file_path" | sed 's/-plan-v.*//')
284
- plan_dir=$(dirname "$file_path")
285
- handoff_pattern="${plan_dir}/${plan_base}-handoff"
286
- if find "$plan_dir" -name "${plan_base}-handoff*.md" 2>/dev/null | grep -q .; then
287
- ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版 Handoff 冲突: 检测到独立 handoff 文件(${plan_base}-handoff*.md)。精简版 Plan 的 Handoff 已融合在 §四,不应生成独立文件。请删除独立 handoff 文件或改用 standard-plan-template.md\n"
288
- fi
289
- fi
290
-
291
- if [ -n "$ALGO_ISSUES" ]; then
292
- echo "⛔ 算法化规则校验不通过:" >&2
293
- echo -e "$ALGO_ISSUES" >&2
294
- # 合并到 ISSUES 中一起阻断
295
- echo -e "$ALGO_ISSUES" >> "$TMPFILE"
296
- fi
297
-
298
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
299
-
300
- # ── 判定回流 extra(Task 1.5 数据契约:anchor_hit/struct_score 精确语义;override 由上层 hook 在人类推翻时追加)──
301
- # struct_score = (适用规则数 - 缺失规则数) / 适用规则数 × 100,适用规则从 schema 实况计数
302
- anchor_total=$(jq -r '[.sections[] | select(.anchor != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
303
- heading_total=$(jq -r '[.sections[] | select(.required == true and .heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
304
- sub_total=$(jq -r '[.sections[].subsections[]? | select(.heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
305
- term_total=$(jq -r '.forbidden_terms | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
306
- if [ "$IS_SEARCH_REPLACE" = "true" ]; then
307
- applied=$term_total # patch 路径仅禁词规则适用
308
- else
309
- applied=$(( anchor_total + heading_total + sub_total + term_total ))
310
- fi
311
- missed=$(wc -l < "$TMPFILE" 2>/dev/null | tr -d ' ')
312
- missed=${missed:-0}
313
- anchor_hit=true
314
- grep -q "缺锚点" "$TMPFILE" 2>/dev/null && anchor_hit=false
315
- struct_score=100
316
- if [ "$applied" -gt 0 ] 2>/dev/null && [ "$missed" -gt 0 ] 2>/dev/null; then
317
- struct_score=$(( (applied - missed) * 100 / applied ))
318
- [ "$struct_score" -lt 0 ] && struct_score=0
319
- fi
320
- BACKFLOW_EXTRA="\"anchor_hit\":${anchor_hit},\"struct_score\":${struct_score}"
321
-
322
- # ── 阻断或放行 ──
323
- if [ -n "$ISSUES" ]; then
324
- echo "⛔ $TEMPLATE_NAME 校验不通过:
325
- $ISSUES" >&2
326
- # 明细注入 stdout reason(去引号防 JSON 破坏,截断 180 字),让被拦方第一时间知道改什么
327
- ISSUES_BRIEF=$(printf '%s' "$ISSUES" | tr '\n' ' ' | tr -d '"' | tr -s ' ' | cut -c1-180)
328
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过: ${ISSUES_BRIEF}\"}}"
329
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
330
- exit 2
331
- fi
332
-
333
- # ── 自动更新 index.md ──
334
- if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
335
- if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
336
- "$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
337
- fi
338
- fi
339
-
340
- # 放行回流(Task 1.5:anchor_hit/struct_score 正样本底座)
341
- write_hook_event "doc-format-guard" "allow" "$file_path" "校验通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
342
-
343
- exit 0
@@ -1,220 +0,0 @@
1
- #!/bin/bash
2
- ###
3
- # @Author : xiaomingming wujixmm@gmail.com
4
- # @Date : 2026-07-17 13:14:46
5
- # @LastEditors : xiaomingming wujixmm@gmail.com
6
- # @LastEditTime : 2026-07-17 13:21:36
7
- # @FilePath : /add-coder/templates/core/hooks/lib/common.sh
8
- # @Description : ADD Hook 共享库 — 四端通用函数
9
- ###
10
- # ADD Hook 共享库 — 四端通用函数
11
- # 被 Claude/Qoder/VS Code/Trae adapter 的 hook 脚本 source 引用
12
- # 路径: templates/core/hooks/lib/common.sh
13
-
14
- # 退出码常量,目前适配的ide都一样
15
- export EXIT_PASS=0 # 放行
16
- export EXIT_BLOCK=2 # 阻断
17
-
18
- # ── 输入解析 ──
19
-
20
- # 从 stdin 解析 JSON 输入(hook 事件通过 stdin 传入 JSON)
21
- parse_input() {
22
- if [ -t 0 ]; then
23
- echo "{}"
24
- else
25
- cat
26
- fi
27
- }
28
-
29
- # 从 JSON 中提取字段值(简单实现,不依赖 jq)
30
- # 用法: json_get "$json" "field_name"
31
- json_get() {
32
- echo "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | head -1 | sed 's/.*: *"\([^"]*\)".*/\1/'
33
- }
34
-
35
- # ── ADD 活跃 Plan 检测(裁决定逻辑,SKILL §0.7.1) ──
36
- #
37
- # lifecycle 的唯一真相源是 scoped DB。短生命周期 Hook 不常驻 LISTEN;每次触发
38
- # 都通过当前 adapter 生成态的机器桥(plan-status-bridge)调用 shared resolver。
39
- # Handoff/add-route 不再参与 active 裁决,禁止跨 adapter 或文件系统 fallback
40
- # (含 .add 回退与 mtime/勾选数推断)。
41
- #
42
- # magicDir 解析(Review P1 #5): 入口必须注入自身 magicDir;未注入时从生成态
43
- # 脚本物理位置(<magicDir>/hooks/...)反推;推导失败 → STATUS_UNAVAILABLE,
44
- # 禁止任何 adapter 名称默认值。
45
- #
46
- # 前置条件: 调用方需设置 PROJECT_DIR 环境变量(项目根目录)
47
- # 返回: "plan_keyword::step_n/total::round_n/total::handoff_path::add_route_path"
48
- # 或 "__STATUS_UNAVAILABLE__::reason::database::none::none"(调用方必须 fail-closed)
49
-
50
- query_plan_status() {
51
- local bridge=""
52
- if [ -n "${MAGIC_DIR:-}" ]; then
53
- bridge="${PROJECT_DIR:-$PWD}/${MAGIC_DIR}/scripts/plan-status-bridge.ts"
54
- elif [ -n "${HOOK_DIR:-}" ]; then
55
- # 物理位置反推: <magicDir>/hooks/lib/common.sh → <magicDir>
56
- bridge="${PROJECT_DIR:-$PWD}/$(basename "$(dirname "$(dirname "$HOOK_DIR")")")/scripts/plan-status-bridge.ts"
57
- else
58
- echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"magicDir 未注入且无法从物理位置推导"}'
59
- return 3
60
- fi
61
- if [ ! -f "$bridge" ]; then
62
- echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"plan-status bridge missing"}'
63
- return 3
64
- fi
65
- node --import tsx "$bridge"
66
- }
67
-
68
- # 兼容既有 Hook 字段协议:
69
- # "planName::done/total::approvalStatus::none::none";后两个路径不参与 active 判定。
70
- # DB 不可用/不可判定时返回特殊首字段,调用方必须 fail closed,不能当作“无 Plan”。
71
- detect_active_add() {
72
- local snapshot="" rc=0
73
- snapshot="$(query_plan_status 2>/dev/null)" || rc=$?
74
- if [ "$rc" -ne 0 ]; then
75
- local reason
76
- reason=$(printf '%s' "$snapshot" | jq -r '.reason // "database status unavailable"' 2>/dev/null || echo "database status unavailable")
77
- echo "__STATUS_UNAVAILABLE__::${reason}::database::none::none"
78
- return 0
79
- fi
80
- if ! printf '%s' "$snapshot" | jq -e '.availability == "READY" and .isActive == true' >/dev/null 2>&1; then
81
- return 1
82
- fi
83
- local plan done total approval
84
- plan=$(printf '%s' "$snapshot" | jq -r '.planName')
85
- done=$(printf '%s' "$snapshot" | jq -r '.progress.doneTasks // 0')
86
- total=$(printf '%s' "$snapshot" | jq -r '.progress.totalTasks // 0')
87
- approval=$(printf '%s' "$snapshot" | jq -r '.approvalStatus // "none"')
88
- echo "${plan}::${done}/${total}::${approval}::none::none"
89
- }
90
-
91
- # ── Dev Action 追踪 ──
92
-
93
- # dev action 标记文件(项目级,PreToolUse 写入,Stop 读取)
94
- DEV_FLAG="/tmp/add_dev_$(echo "${PROJECT_DIR:-$PWD}" | md5sum 2>/dev/null | cut -c1-8 || echo "default")"
95
-
96
- mark_dev_action() {
97
- touch "$DEV_FLAG" 2>/dev/null || true
98
- }
99
-
100
- has_dev_action() {
101
- [ -f "$DEV_FLAG" ]
102
- }
103
-
104
- clear_dev_action() {
105
- rm -f "$DEV_FLAG" 2>/dev/null || true
106
- }
107
-
108
- # ── 验收完整度检查 ──
109
-
110
- # 检查 handoff + add-route 的验收完整度
111
- # 用法: check_add_completeness "$handoff_path" "$add_route_path"
112
- check_add_completeness() {
113
- local handoff="$1" add_route="$2"
114
- local issues=""
115
-
116
- # devlog(内容已回流至 handoff,检查 handoff 是否含验收结果)
117
- if [ -f "$handoff" ] && ! grep -qE '验收|收敛|闭环|本轮改了什么|devlog' "$handoff" 2>/dev/null; then
118
- issues="${issues} [ ] devlog 缺失(handoff 无验收记录)\n"
119
- fi
120
-
121
- # handoff 验证标准
122
- if [ -f "$handoff" ]; then
123
- local uc=$(grep -c '\[ \]' "$handoff" 2>/dev/null || echo "0")
124
- [ "$uc" -gt 0 ] && issues="${issues} [ ] handoff ${uc} 项未勾选\n"
125
- fi
126
-
127
- # add-route Step
128
- if [ -f "$add_route" ]; then
129
- local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || echo "0")
130
- [ "$uc" -gt 0 ] && issues="${issues} [ ] add-route ${uc} Step 未闭环\n"
131
- fi
132
-
133
- echo -e "$issues"
134
- }
135
-
136
- # ── 验收幂等保护 ──
137
-
138
- # 检查是否已验收(幂等保护)
139
- # 用法: is_already_accepted "$add_route_path" "$handoff_path"
140
- # 返回 0 = 已验收,1 = 未验收
141
- is_already_accepted() {
142
- local add_route="$1" handoff="$2"
143
- # add-route Step 8 是否已 [x]
144
- if [ -f "$add_route" ]; then
145
- if grep -A 10 'Step 8' "$add_route" 2>/dev/null | grep -q '\[x\].*验证并更新项目状态'; then
146
- # handoff 是否有验收记录
147
- if [ -f "$handoff" ] && grep -qE '✅.*验收|收敛|全部闭环|全部.*完成' "$handoff" 2>/dev/null; then
148
- return 0
149
- fi
150
- fi
151
- fi
152
- return 1
153
- }
154
-
155
- # ── Stop 四象限分流 few-shot 上下文(卡位 #7) ──
156
- # 用法: build_stop_context "$quadrant" "$info"
157
- build_stop_context() {
158
- local quadrant="$1" info="$2"
159
- case "$quadrant" in
160
- "no_add_no_dev")
161
- echo "[ADD Stop] 无活跃 Plan,无代码改动。正常结束。"
162
- ;;
163
- "no_add_has_dev")
164
- cat <<'EOF'
165
- [ADD Stop] ⚠️ 检测到代码修改但无活跃 ADD Plan。
166
-
167
- Plan 不是"文档开销"——它是代码治理的基础设施。跳过 Plan 的后果:
168
- · 没有 add-route → 每次改动无法追溯到具体 Task
169
- · 没有 tasks.md → 后续 AI Session 不知道改了哪些文件
170
- · 没有 handoff → 交接时上下文全丢,只能靠 git log 猜
171
-
172
- 你必须立即补救,二选一:
173
-
174
- 方案 A — 补 ADD 流程(招安):
175
- Step 0: 读 .qoder/templates/plan-template.md → 生成 Plan
176
- 生成 add-route → check_dps ≥ {{dpsPass}}
177
- Step 1: 扩展 AgentAuditPhase(如需要)
178
- Step 2: 确认 agentAudit() 通道
179
- Step 3: 将已写代码关联到 tasks.md
180
- 完成后可正常停止。
181
-
182
- 方案 B — 补不上则回滚:
183
- 如果改动太复杂无法追溯生成 Plan,则:
184
- ① git diff 确认改动范围
185
- ② 仅对已确认属于本轮的文件生成反向 apply_patch
186
- ③ 无法安全确认改动所有权时停止,并请求用户决定
187
-
188
- 无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
189
- EOF
190
- ;;
191
- "has_add_no_dev")
192
- echo "[ADD Stop] ADD 流程进行中 (${info}),本次无代码改动。下次继续时执行 session-init 恢复上下文。"
193
- ;;
194
- "has_add_dev_unclosed")
195
- cat <<EOF
196
- [ADD Stop] ⚠️ 代码已完成但验收未闭环:
197
- ${info}
198
-
199
- 请依次执行(不要等下次会话):
200
- ① Write devlog → handoff 同目录 devlog-{plan}-v{n}.md
201
- 格式: # Devlog: {plan}\n 日期 / Plan / 轮次 / 本轮改了什么 / 验收结果 / 遗留项 / 架构回看
202
- ② Edit handoff → 更新 §验证标准 全部 [x] + 补充审计 ID
203
- ③ Read docs/ → 回看架构文档确认一致性
204
- ④ Edit add-route → 勾选对应 Step [x]
205
-
206
- 以上全部完成后 Agent 才能停止。
207
- EOF
208
- ;;
209
- esac
210
- }
211
-
212
- # ── PreToolUse 写入前置守卫上下文(卡位 #4) ──
213
- build_pretool_context() {
214
- local plan="$1" round="$2"
215
- cat <<EOF
216
- [ADD PreToolUse] 当前 Plan: ${plan},轮次: ${round}。
217
- 本次写入应属于 ADD Step 3 代码实现阶段。
218
- 完成后执行 record_dev_operation 记录审计。
219
- EOF
220
- }
@@ -1,96 +0,0 @@
1
- #!/bin/bash
2
- # context-inject.sh — 上下文注入模板
3
- # 共享库
4
-
5
- # 结构化 SessionStart JSON 注入
6
- build_session_start_json() {
7
- local plan="$1" step="$2" round="$3" handoff="$4"
8
- cat <<EOF
9
- {
10
- "continue": true,
11
- "hookSpecificOutput": {
12
- "hookEventName": "SessionStart",
13
- "additionalContext": "上次 ADD 流程未完成:\n Plan: ${plan}\n 轮次: ${round}\n 当前 Step: ${step} (add-route)\n 恢复命令: query_audit_logs({ planKeyword: '${plan}' })\n handoff: ${handoff}"
14
- }
15
- }
16
- EOF
17
- }
18
-
19
- # Stop 四象限分流 few-shot 上下文
20
- build_stop_context() {
21
- local quadrant="$1" info="$2"
22
- case "$quadrant" in
23
- "no_add_no_dev")
24
- echo "[ADD Stop] 无活跃 Plan,无代码改动。正常结束。"
25
- ;;
26
- "no_add_has_dev")
27
- cat <<'EOF'
28
- [ADD Stop] ⚠️ 检测到代码修改但无活跃 ADD Plan。
29
-
30
- Plan 不是"文档开销"——它是代码治理的基础设斻。跳过 Plan 的后果:
31
- · 没有 add-route → 每次改动无法追溯到具体 Task
32
- · 没有 tasks.md → 后续 AI Session 不知道改了哪些文件
33
- · 没有 handoff → 交接时上下文全丢,只能靠 git log 猜
34
-
35
- 你必须立即补救,二选一:
36
-
37
- 方案 A — 补 ADD 流程(招安):
38
- Step 0: 读 .qoder/templates/plan-template.md → 生成 Plan → .qoder/plans/{today}/{keyword}-plan-v1.md
39
- 生成 add-route → check_dps ≥ {{dpsPass}}
40
- Step 1: 扩展 AgentAuditPhase(如需要)
41
- Step 2: 确认 agentAudit() 通道
42
- Step 3: 将已写代码关联到 tasks.md
43
- 完成后可正常停止。
44
-
45
- 方案 B — 补不上则回滚:
46
- 如果改动太复杂无法追溯生成 Plan,则:
47
- ① git diff 确认改动范围
48
- ② 仅对已确认属于本轮的文件生成反向 apply_patch
49
- ③ 无法安全确认改动所有权时停止,并请求用户决定
50
-
51
- 无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
52
- EOF
53
- ;;
54
- "has_add_no_dev")
55
- echo "[ADD Stop] ADD 流程进行中 (${info}),本次无代码改动。下次继续时执行 session-init 恢复上下文。"
56
- ;;
57
- "has_add_dev_step02")
58
- echo "[ADD Stop] ADD Step 0-2: 文档先行/审计准备阶段。无需验收闭环。下一步: 进入 Step 3 代码实现。"
59
- ;;
60
- "has_add_dev_step3")
61
- echo "[ADD Stop] ADD Step 3: 代码实现进行中 (${info})。完成后进入 Step 3.5 实现审查。"
62
- ;;
63
- "has_add_dev_unclosed")
64
- cat <<'EOF'
65
- [ADD Stop] ⚠️ 代码已完成但验收未闭环:
66
- ${info}
67
-
68
- 请依次执行(不要等下次会话):
69
- ① Write devlog → handoff 同目录 devlog-{plan}-v{n}.md
70
- 格式: # Devlog: {plan}\n 日期 / Plan / 轮次 / 本轮改了什么 / 验收结果 / 遗留项 / 架构回看
71
- ② Edit handoff → 更新 §验证标准 全部 [x] + 补充审计 ID
72
- ★ 同步: checklist 有新 cuid → handoff ADD-7 表必须对应新增行
73
- ★ Step 0 准入: handoff + add-route + Specs 三元组缺一不可,缺则回退 Step 0.5
74
- ③ Read docs/ → 回看架构文档确认一致性
75
- ④ Edit add-route → 勾选对应 Step [x]
76
-
77
- 以上全部完成后 Agent 才能停止。
78
-
79
- 下次恢复: 读 handoff → 查同目录 devlog-*.md → query_audit_logs
80
- EOF
81
- ;;
82
- "has_add_dev_closed")
83
- echo "[ADD Stop] ✅ 验收闭环: add-route全部[x], devlog已记录, handoff已更新。验收幂等——重复触发不覆盖已有结论。"
84
- ;;
85
- esac
86
- }
87
-
88
- # 写操作前置守卫上下文
89
- build_pretool_context() {
90
- local plan="$1" round="$2"
91
- cat <<EOF
92
- [ADD PreToolUse] 当前 Plan: ${plan},轮次: ${round}。
93
- 本次写入应属于 ADD Step 3 代码实现阶段。
94
- 完成后执行 record_dev_operation 记录审计。
95
- EOF
96
- }