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,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,282 +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
- exit 2
60
- fi
61
-
62
- PROJECT_DIR="$PWD"
63
- TEMPLATES_DIR="$PROJECT_DIR/$MAGIC_DIR/templates"
64
-
65
- # 文件名类型 token 匹配(特异性排序,取代子串 grep;R5/Task 1.4)
66
- # review/handoff 不在此层绑定(需内容特征细分单多轮/运行时),留给下方探测链
67
- TEMPLATE_NAME=""
68
- base=$(basename "$file_path")
69
- base=${base%-v[0-9]*}
70
- case "$base" in
71
- *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
72
- *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
73
- *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
74
- *handoff*) : ;; # R15: 跳过 token 层,交内容探测链;必须在 *report* 前,防含 report 的 handoff 被误吞
75
- *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
76
- *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
77
- *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
78
- *report*) TEMPLATE_NAME="report-template.md" ;;
79
- *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
80
- *spec*) TEMPLATE_NAME="spec-template.md" ;;
81
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
82
- esac
83
-
84
- # 退一步:根据文件内容特征猜测模板类型
85
- if [ -z "$TEMPLATE_NAME" ]; then
86
- if grep -q "## 四、Handoff" <<< "$CONTENT"; then
87
- TEMPLATE_NAME="simple-standard-plan-template.md"
88
- elif grep -q "## PLAN 元信息" <<< "$CONTENT"; then
89
- TEMPLATE_NAME="standard-plan-template.md"
90
- elif grep -q "## Review 元信息" <<< "$CONTENT"; then
91
- if grep -q "运行时验证" <<< "$CONTENT"; then
92
- TEMPLATE_NAME="review-runtime-template.md"
93
- elif grep -q "跨仓库格式契约" <<< "$CONTENT"; then
94
- TEMPLATE_NAME="review-implementation-template.md"
95
- else
96
- TEMPLATE_NAME="review-template.md"
97
- fi
98
- elif grep -q "## Why" <<< "$CONTENT"; then
99
- TEMPLATE_NAME="spec-template.md"
100
- elif grep -q "## Preconditions" <<< "$CONTENT"; then
101
- TEMPLATE_NAME="tasks-template.md"
102
- elif grep -q "审计链(证据→devlog→checklist)" <<< "$CONTENT"; then
103
- TEMPLATE_NAME="checklist-template.md"
104
- else
105
- case "$file_path" in
106
- *handoff*)
107
- # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
108
- if grep -qF "## 全局元信息" <<< "$CONTENT"; then
109
- TEMPLATE_NAME="handoff-multi-round-template.md"
110
- elif grep -qF "## 1. 交接前状态" <<< "$CONTENT"; then
111
- TEMPLATE_NAME="handoff-single-round-template.md"
112
- else
113
- echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
114
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"handoff 内容不符合 single/multi 模板规范"}}'
115
- write_hook_event "doc-format-guard" "deny" "$file_path" "handoff 模板类型无法识别" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
116
- exit 2
117
- fi
118
- ;;
119
- *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
120
- *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
121
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
122
- *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
123
- *spec*) TEMPLATE_NAME="spec-template.md" ;;
124
- *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
125
- *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
126
- *report*) TEMPLATE_NAME="report-template.md" ;;
127
- *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
128
- *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
129
- *)
130
- # 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
131
- if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
132
- echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
133
- exit 0
134
- fi
135
- echo "⛔ 拒绝:无法识别文档类型 (file_path: $file_path),缺少模板匹配规则" >&2
136
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"无法识别 ADD 文档类型,请联系管理员更新 doc-format-guard.sh"}}'
137
- write_hook_event "doc-format-guard" "deny" "$file_path" "无法识别文档类型" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
138
- exit 2
139
- ;;
140
- esac
141
- fi
142
- fi
143
-
144
- SCHEMA_FILE="$TEMPLATES_DIR/${TEMPLATE_NAME%.md}.schema.json"
145
- # L84: schema 文件不存在 → 无校验规则,阻断(不允许无规则放行)
146
- if [ ! -f "$SCHEMA_FILE" ]; then
147
- echo "⛔ 阻断:模板 ${TEMPLATE_NAME} 缺少对应的 .schema.json 校验规则" >&2
148
- echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"缺少 .schema.json 校验规则文件,禁止无规则放行"}}'
149
- write_hook_event "doc-format-guard" "deny" "$file_path" "缺少 .schema.json 校验规则" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
150
- exit 2
151
- fi
152
-
153
- # SearchReplace 只传 patch(replacements[].new_text),不传全文件 → 跳过章节校验
154
- IS_SEARCH_REPLACE=$(echo "$input" | jq -r '(.tool_input.replacements | length > 0) or (.tool_input.new_string != null) // false')
155
-
156
- ISSUES=""
157
-
158
- # ── 章节校验 ──
159
- # 使用项目目录下的临时文件,避免 /tmp 在沙箱中不可写导致静默跳过
160
- TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
161
- : > "$TMPFILE"
162
- trap "rm -f $TMPFILE" EXIT
163
-
164
- # ── 锚定校验(锚定制:schema 声明 anchor,参照内容从模板现场提取;Task 1.2)──
165
- # 锚点不在模板 → 跳过该规则 + stderr 告警(不阻塞写入,冒烟巡检兑底)
166
- # within 前缀匹配;heading 缺失 → 跳过规则 + 告警(不误拦)
167
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
168
- jq -r '.sections[] | select(.anchor != null) | .id + "|" + .anchor + "|" + (.within // "")' "$SCHEMA_FILE" 2>/dev/null | while IFS='|' read -r aid anchor within; do
169
- [ -z "$anchor" ] && continue
170
- ref_line=$(grep -m1 -F "$anchor" "$TEMPLATES_DIR/$TEMPLATE_NAME" 2>/dev/null || true)
171
- if [ -z "$ref_line" ]; then
172
- echo "[doc-format-guard] anchor_miss: schema ${aid} 声明的 anchor '${anchor}' 在 ${TEMPLATE_NAME} 中未定位,跳过该规则(冒烟巡检兑底)" >&2
173
- continue
174
- fi
175
- tokens=$(echo "$ref_line" | tr '#*`|(){' ' ' | tr -s ' ' '\n' | grep -v '^$' | grep -vF '{' | sort -u)
176
- [ -z "$tokens" ] && continue
177
- scope="$CONTENT"
178
- if [ -n "$within" ]; then
179
- if grep -q "^${within}" <<< "$CONTENT"; then
180
- scope=$(sed -n "/^${within}/,/^## /p" <<< "$CONTENT")
181
- else
182
- echo "[doc-format-guard] within_miss: schema ${aid} 的 within '${within}' 在文档中未定位,跳过该规则" >&2
183
- continue
184
- fi
185
- fi
186
- # 声明级缺失统计:一个锚点声明的必选 token 有任一缺失即记 1 条规则缺失(struct_score 精确语义)
187
- miss_tokens=""
188
- for tok in $tokens; do
189
- if ! grep -qF "$tok" <<< "$scope"; then
190
- miss_tokens="${miss_tokens}${tok} "
191
- fi
192
- done
193
- # if 语句(非 && 链):miss_tokens 为空时返回 0,避免 while 管道退出码非零触发 set -e
194
- if [ -n "$miss_tokens" ]; then
195
- echo " 缺锚点(${aid}): ${miss_tokens}"
196
- fi
197
- done >> "$TMPFILE"
198
- fi
199
-
200
- # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
201
- if [ "$IS_SEARCH_REPLACE" != "true" ]; then
202
- jq -r '.sections[] | select(.required == true) | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
203
- if ! grep -qF "$heading" <<< "$CONTENT"; then
204
- echo " 缺章节: $heading"
205
- fi
206
- done >> "$TMPFILE"
207
-
208
- # 子章节
209
- jq -r '.sections[].subsections[]? | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
210
- if ! grep -qF "$sub" <<< "$CONTENT"; then
211
- echo " 缺子章节: $sub"
212
- fi
213
- done >> "$TMPFILE"
214
- fi
215
-
216
- # ── 占位符校验 ──
217
- jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
218
- if grep -qF "$ph" <<< "$CONTENT"; then
219
- echo " 未替换占位符: $ph"
220
- fi
221
- done >> "$TMPFILE"
222
-
223
- # ── 结构位禁词校验(仅标题行 + schema 声明的 groupColumn;正文叙述免疫;Task 1.2)──
224
- struct_text=$(grep -E '^#{2,}[[:space:]]' <<< "$CONTENT")
225
- col=$(jq -r '.groupColumn // empty' "$SCHEMA_FILE" 2>/dev/null)
226
- if [ -n "$col" ]; then
227
- struct_text="${struct_text}
228
- $(awk -F'|' -v c="$col" 'NF>c {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $(c+1)); print $(c+1)}' <<< "$CONTENT")"
229
- fi
230
- jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
231
- # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
232
- if grep -qF "$term" <<< "$struct_text"; then
233
- echo " 结构位禁词: $term"
234
- fi
235
- done >> "$TMPFILE"
236
-
237
- ISSUES=$(cat "$TMPFILE" 2>/dev/null)
238
-
239
- # ── 判定回流 extra(Task 1.5 数据契约:anchor_hit/struct_score 精确语义;override 由上层 hook 在人类推翻时追加)──
240
- # struct_score = (适用规则数 - 缺失规则数) / 适用规则数 × 100,适用规则从 schema 实况计数
241
- anchor_total=$(jq -r '[.sections[] | select(.anchor != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
242
- heading_total=$(jq -r '[.sections[] | select(.required == true and .heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
243
- sub_total=$(jq -r '[.sections[].subsections[]? | select(.heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
244
- term_total=$(jq -r '.forbidden_terms | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
245
- if [ "$IS_SEARCH_REPLACE" = "true" ]; then
246
- applied=$term_total # patch 路径仅禁词规则适用
247
- else
248
- applied=$(( anchor_total + heading_total + sub_total + term_total ))
249
- fi
250
- missed=$(wc -l < "$TMPFILE" 2>/dev/null | tr -d ' ')
251
- missed=${missed:-0}
252
- anchor_hit=true
253
- grep -q "缺锚点" "$TMPFILE" 2>/dev/null && anchor_hit=false
254
- struct_score=100
255
- if [ "$applied" -gt 0 ] 2>/dev/null && [ "$missed" -gt 0 ] 2>/dev/null; then
256
- struct_score=$(( (applied - missed) * 100 / applied ))
257
- [ "$struct_score" -lt 0 ] && struct_score=0
258
- fi
259
- BACKFLOW_EXTRA="\"anchor_hit\":${anchor_hit},\"struct_score\":${struct_score}"
260
-
261
- # ── 阻断或放行 ──
262
- if [ -n "$ISSUES" ]; then
263
- echo "⛔ $TEMPLATE_NAME 校验不通过:
264
- $ISSUES" >&2
265
- # 明细注入 stdout reason(去引号防 JSON 破坏,截断 180 字),让被拦方第一时间知道改什么
266
- ISSUES_BRIEF=$(printf '%s' "$ISSUES" | tr '\n' ' ' | tr -d '"' | tr -s ' ' | cut -c1-180)
267
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过: ${ISSUES_BRIEF}\"}}"
268
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
269
- exit 2
270
- fi
271
-
272
- # ── 自动更新 index.md ──
273
- if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
274
- if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
275
- "$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
276
- fi
277
- fi
278
-
279
- # 放行回流(Task 1.5:anchor_hit/struct_score 正样本底座)
280
- write_hook_event "doc-format-guard" "allow" "$file_path" "校验通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
281
-
282
- 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
- }