add-coder 0.3.26 → 0.3.27

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 (132) hide show
  1. package/README.md +36 -10
  2. package/dist/index.js +481 -209
  3. package/package.json +14 -4
  4. package/templates/.add-coder-src-hash.json +129 -106
  5. package/templates/adapters/claude/hooks/doc-format-guard.sh +112 -31
  6. package/templates/adapters/claude/hooks/lib/common.sh +46 -87
  7. package/templates/adapters/claude/hooks/lib/context-inject.sh +2 -2
  8. package/templates/adapters/claude/hooks/lib/notify.sh +6 -4
  9. package/templates/adapters/claude/hooks/lib/preload-templates.sh +27 -2
  10. package/templates/adapters/claude/hooks/lib/state-detect.sh +8 -112
  11. package/templates/adapters/claude/hooks/lib/vocabulary.sh +13 -7
  12. package/templates/adapters/claude/hooks/prompt-submit.sh +1 -1
  13. package/templates/adapters/claude/hooks/stop-check.sh +9 -0
  14. package/templates/adapters/codex/config.toml.example +12 -0
  15. package/templates/adapters/codex/hooks/doc-format-guard.sh +121 -39
  16. package/templates/adapters/codex/hooks/lib/common.sh +47 -99
  17. package/templates/adapters/codex/hooks/lib/context-inject.sh +4 -4
  18. package/templates/adapters/codex/hooks/lib/notify.sh +6 -4
  19. package/templates/adapters/codex/hooks/lib/preload-templates.sh +27 -6
  20. package/templates/adapters/codex/hooks/lib/state-detect.sh +5 -114
  21. package/templates/adapters/codex/hooks/lib/vocabulary.sh +3 -8
  22. package/templates/adapters/codex/hooks/notification.sh +3 -18
  23. package/templates/adapters/codex/hooks/post-tool-use.sh +24 -119
  24. package/templates/adapters/codex/hooks/pre-tool-use.sh +97 -184
  25. package/templates/adapters/codex/hooks/prompt-submit.sh +8 -3
  26. package/templates/adapters/codex/hooks/session-start.sh +19 -9
  27. package/templates/adapters/codex/hooks/stop-check.sh +44 -18
  28. package/templates/adapters/codex/hooks.json +67 -1
  29. package/templates/adapters/qoder/hooks/doc-format-guard.sh +119 -38
  30. package/templates/adapters/qoder/hooks/lib/common.sh +49 -87
  31. package/templates/adapters/qoder/hooks/lib/context-inject.sh +2 -2
  32. package/templates/adapters/qoder/hooks/lib/notify.sh +6 -4
  33. package/templates/adapters/qoder/hooks/lib/preload-templates.sh +27 -2
  34. package/templates/adapters/qoder/hooks/lib/state-detect.sh +8 -112
  35. package/templates/adapters/qoder/hooks/lib/vocabulary.sh +13 -7
  36. package/templates/adapters/qoder/hooks/prompt-submit.sh +1 -1
  37. package/templates/adapters/qoder/hooks/session-start.sh +1 -1
  38. package/templates/adapters/qoder/hooks/stop-check.sh +11 -0
  39. package/templates/adapters/trae/hooks/doc-format-guard.sh +120 -38
  40. package/templates/adapters/trae/hooks/lib/common.sh +46 -87
  41. package/templates/adapters/trae/hooks/lib/context-inject.sh +2 -2
  42. package/templates/adapters/trae/hooks/lib/notify.sh +5 -3
  43. package/templates/adapters/trae/hooks/lib/preload-templates.sh +27 -2
  44. package/templates/adapters/trae/hooks/lib/state-detect.sh +8 -115
  45. package/templates/adapters/trae/hooks/lib/vocabulary.sh +13 -7
  46. package/templates/adapters/trae/hooks/pre-tool-use.sh +14 -8
  47. package/templates/adapters/trae/hooks/stop-check.sh +9 -0
  48. package/templates/adapters/vscode/hooks/doc-format-guard.sh +112 -31
  49. package/templates/adapters/vscode/hooks/lib/common.sh +46 -87
  50. package/templates/adapters/vscode/hooks/lib/context-inject.sh +2 -2
  51. package/templates/adapters/vscode/hooks/lib/notify.sh +6 -4
  52. package/templates/adapters/vscode/hooks/lib/preload-templates.sh +27 -2
  53. package/templates/adapters/vscode/hooks/lib/state-detect.sh +8 -112
  54. package/templates/adapters/vscode/hooks/lib/vocabulary.sh +13 -7
  55. package/templates/adapters/vscode/hooks/notification.sh +3 -5
  56. package/templates/adapters/vscode/hooks/prompt-submit.sh +1 -1
  57. package/templates/adapters/vscode/hooks/stop-check.sh +9 -0
  58. package/templates/core/docs/ADD-governance-claude-code.md +1 -1
  59. package/templates/core/docs/ADD-governance-codex.md +1 -1
  60. package/templates/core/docs/ADD-governance-qoder-cn.md +1 -1
  61. package/templates/core/docs/ADD-governance-trae.md +1 -1
  62. package/templates/core/docs/ADD-governance-vscode-copilot.md +1 -1
  63. package/templates/core/hooks/doc-format-guard.sh +120 -38
  64. package/templates/core/hooks/lib/common.sh +46 -87
  65. package/templates/core/hooks/lib/context-inject.sh +2 -2
  66. package/templates/core/hooks/lib/notify.sh +5 -3
  67. package/templates/core/hooks/lib/preload-templates.sh +27 -2
  68. package/templates/core/hooks/lib/state-detect.sh +8 -115
  69. package/templates/core/hooks/lib/vocabulary.sh +13 -7
  70. package/templates/core/hooks/pre-tool-use.sh +14 -8
  71. package/templates/core/hooks/stop-check.sh +9 -0
  72. package/templates/core/prisma/add.prisma +140 -82
  73. package/templates/core/rules/project_rules.md +27 -24
  74. package/templates/core/scripts/db-ensure.sh +8 -0
  75. package/templates/core/scripts/gen-plan-index.sh +4 -15
  76. package/templates/core/scripts/mcp-server/notifications/hitl.ts +9 -1
  77. package/templates/core/scripts/mcp-server/notifications/hook.ts +25 -9
  78. package/templates/core/scripts/mcp-server/resources/hitl-approval-widget.ts +34 -0
  79. package/templates/core/scripts/mcp-server/resources/hook-events-report.ts +14 -2
  80. package/templates/core/scripts/mcp-server/resources/index.ts +2 -0
  81. package/templates/core/scripts/mcp-server/sampling/review.ts +25 -10
  82. package/templates/core/scripts/mcp-server/shared/db-types.ts +33 -8
  83. package/templates/core/scripts/mcp-server/shared/dps-scoring.strategy.ts +1 -1
  84. package/templates/core/scripts/mcp-server/shared/env.ts +28 -8
  85. package/templates/core/scripts/mcp-server/shared/fs.ts +3 -0
  86. package/templates/core/scripts/mcp-server/shared/hitl-interaction.strategy.ts +2 -3
  87. package/templates/core/scripts/mcp-server/shared/hitl-lifecycle-mutation.ts +129 -0
  88. package/templates/core/scripts/mcp-server/shared/hitl-proposal-mutation.ts +98 -0
  89. package/templates/core/scripts/mcp-server/shared/hitl-ui.ts +3 -0
  90. package/templates/core/scripts/mcp-server/shared/plan-lifecycle-events.ts +62 -0
  91. package/templates/core/scripts/mcp-server/shared/plan-lifecycle-mutation.ts +78 -0
  92. package/templates/core/scripts/mcp-server/shared/plan-lifecycle-subscriber.ts +145 -0
  93. package/templates/core/scripts/mcp-server/shared/plan-lifecycle.ts +153 -0
  94. package/templates/core/scripts/mcp-server/shared/plan-round-events.ts +56 -0
  95. package/templates/core/scripts/mcp-server/shared/plan-round-mutation.ts +143 -0
  96. package/templates/core/scripts/mcp-server/shared/plan-round-store.ts +47 -0
  97. package/templates/core/scripts/mcp-server/shared/plan-round-subscriber.ts +99 -0
  98. package/templates/core/scripts/mcp-server/shared/plan-status-store.ts +37 -0
  99. package/templates/core/scripts/mcp-server/shared/plan-tracking-mutation.ts +71 -0
  100. package/templates/core/scripts/mcp-server/shared/prisma.ts +12 -6
  101. package/templates/core/scripts/mcp-server/shared/project-root-strategy.ts +1 -2
  102. package/templates/core/scripts/mcp-server/shared/quant-recheck.strategy.ts +16 -0
  103. package/templates/core/scripts/mcp-server/shared/redact.ts +28 -0
  104. package/templates/core/scripts/mcp-server/shared/response.ts +3 -2
  105. package/templates/core/scripts/mcp-server/shared/runtime-context.ts +77 -0
  106. package/templates/core/scripts/mcp-server/shared/schema-topology.ts +159 -0
  107. package/templates/core/scripts/mcp-server/tools/audit.ts +49 -7
  108. package/templates/core/scripts/mcp-server/tools/context.ts +117 -121
  109. package/templates/core/scripts/mcp-server/tools/contract.ts +34 -9
  110. package/templates/core/scripts/mcp-server/tools/gateway/check_add_route_status.ts +50 -3
  111. package/templates/core/scripts/mcp-server/tools/gateway/check_doc_similarity.ts +218 -0
  112. package/templates/core/scripts/mcp-server/tools/gateway/check_dps.ts +22 -7
  113. package/templates/core/scripts/mcp-server/tools/gateway/check_rahs.ts +75 -4
  114. package/templates/core/scripts/mcp-server/tools/gateway/check_spec_sync.ts +19 -6
  115. package/templates/core/scripts/mcp-server/tools/gateway/index.ts +2 -0
  116. package/templates/core/scripts/mcp-server/tools/hitl.ts +160 -75
  117. package/templates/core/scripts/mcp-server/tools/hook-event-report.ts +9 -2
  118. package/templates/core/scripts/mcp-server/tools/index.ts +69 -3
  119. package/templates/core/scripts/mcp-server/tools/plan.ts +140 -15
  120. package/templates/core/scripts/mcp-server/tools/review.ts +44 -15
  121. package/templates/core/scripts/mcp-server.ts +60 -1
  122. package/templates/core/scripts/plan-status-bridge.ts +26 -0
  123. package/templates/core/skills/add-paradigm/SKILL.md +2 -2
  124. package/templates/core/templates/add-route-template.schema.json +24 -56
  125. package/templates/core/templates/collab-contract-template.md +1 -1
  126. package/templates/core/templates/handoff-multi-round-template.schema.json +48 -0
  127. package/templates/core/templates/handoff-single-round-template.schema.json +92 -0
  128. package/templates/core/templates/handoff-single-round.schema.json +1 -1
  129. package/templates/core/templates/hitl-approval-widget.html +222 -37
  130. package/templates/core/templates/spec-template.schema.json +9 -46
  131. package/templates/core/templates/standard-plan-template.schema.json +1 -1
  132. package/templates/core/templates/tasks-template.schema.json +13 -54
@@ -11,8 +11,8 @@
11
11
  # - MCP Server 宕机不丢事件,重启后从文件恢复消费
12
12
 
13
13
  write_hook_event() {
14
- local hook="$1" decision="$2" cmd="$3" reason="$4" plan="${5:-unknown}" status="${6:-none}"
15
- local dir="${MAGIC_DIR:-.qoder}/reports"
14
+ local hook="$1" decision="$2" cmd="$3" reason="$4" plan="${5:-unknown}" status="${6:-none}" extra="${7:-}"
15
+ local dir="${MAGIC_DIR}/reports"
16
16
  mkdir -p "$dir" 2>/dev/null || true
17
17
  local file="$dir/hook-events.jsonl"
18
18
 
@@ -23,12 +23,14 @@ write_hook_event() {
23
23
  [ "$sz" -gt 262144 ] && mv "$file" "${file}.old" 2>/dev/null || true
24
24
  fi
25
25
 
26
- printf '{"ts":"%s","hook":"%s","decision":"%s","cmd":"%s","reason":"%s","planKeyword":"%s","planStatus":"%s"}\n' \
26
+ # extra 为可选 JSON 片段(Task 1.5 数据契约:anchor_hit/struct_score/override 等),非空时以逗号拼接
27
+ printf '{"ts":"%s","hook":"%s","decision":"%s","cmd":"%s","reason":"%s","planKeyword":"%s","planStatus":"%s"%s}\n' \
27
28
  "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
28
29
  "$hook" \
29
30
  "$decision" \
30
31
  "$cmd" \
31
32
  "$reason" \
32
33
  "$plan" \
33
- "$status" >> "$file"
34
+ "$status" \
35
+ "${extra:+,$extra}" >> "$file"
34
36
  }
@@ -21,9 +21,10 @@
21
21
 
22
22
  set -euo pipefail
23
23
 
24
- # 模板目录(相对于本脚本)
24
+ # 模板目录(分发期物化到各 adapter 本地:<magicDir>/templates;禁止回指源仓)
25
+ # 协议层契约(模板预载): 读 ${magicDir}/templates 本地物化副本;目录/标准模板缺失 fail-fast
25
26
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
26
- TEMPLATES_DIR="${SCRIPT_DIR}/../../../core/templates"
27
+ TEMPLATES_DIR="${SCRIPT_DIR}/../../templates"
27
28
 
28
29
  # tpl-injected 标记文件路径(项目级,/tmp 下按项目 hash 区分)
29
30
  PROJECT_HASH=$(echo "${PROJECT_DIR:-$PWD}" | md5sum 2>/dev/null | cut -c1-8 || echo "default")
@@ -88,6 +89,27 @@ read_template_content() {
88
89
  fi
89
90
  }
90
91
 
92
+ # ── 模板目录存在性校验(fail-fast)──
93
+
94
+ validate_templates_dir() {
95
+ if [ ! -d "$TEMPLATES_DIR" ]; then
96
+ echo "[ADD preload] 模板目录不存在: $TEMPLATES_DIR(生成态物化缺失,请执行 add-coder sync 后重试)" >&2
97
+ exit 1
98
+ fi
99
+
100
+ local available=0
101
+ local tmpl
102
+ for tmpl in "${PRIORITY_ORDER[@]}"; do
103
+ if [ -f "$TEMPLATES_DIR/$tmpl" ]; then
104
+ available=$((available + 1))
105
+ fi
106
+ done
107
+ if [ "$available" -eq 0 ]; then
108
+ echo "[ADD preload] 模板目录中未找到 ADD 标准模板: $TEMPLATES_DIR(缺失清单: ${PRIORITY_ORDER[*]})" >&2
109
+ exit 1
110
+ fi
111
+ }
112
+
91
113
  # ── --index 模式:输出模板清单 ──
92
114
 
93
115
  output_index() {
@@ -165,6 +187,9 @@ main() {
165
187
  esac
166
188
  done
167
189
 
190
+ # 入口校验:目录缺失/标准模板缺失 → fail-fast(不再静默空输出)
191
+ validate_templates_dir
192
+
168
193
  case "$mode" in
169
194
  index)
170
195
  output_index
@@ -1,117 +1,13 @@
1
1
  #!/bin/bash
2
- # state-detect.sh — ADD 活跃流程检测 + dev action 追踪
3
- # 共享库
2
+ # state-detect.sh — 兼容入口;状态裁决统一委托 common.sh,禁止维护第二套 magicDir 探测。
3
+ # 协议层契约(Review P1 #5): 仅当前 magicDir scope;禁止跨端扫描与 .add fallback;
4
+ # 禁止 adapter 名称默认值(入口注入 MAGIC_DIR 或物理位置推导)。
4
5
 
6
+ # 宿主注入链(Claude/Qoder 私有协议:项目根由 IDE 注入;VS Code 经 Claude Agent Host 运行)
5
7
  PROJECT_DIR="${CLAUDE_PROJECT_DIR:-${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}}"
6
8
 
7
- # 动态探测 MAGIC_DIR(兼容多 adapter,source hook 脚本时继承调用者的 HOOK_DIR)
8
- if [ -z "${MAGIC_DIR:-}" ]; then
9
- if [ -n "${HOOK_DIR:-}" ]; then
10
- MAGIC_DIR="$(basename "$(dirname "$HOOK_DIR")")"
11
- else
12
- for m in ".claude" ".qoder" ".vscode" ".add"; do
13
- [ -d "$PROJECT_DIR/$m" ] && { MAGIC_DIR="$m"; break; }
14
- done
15
- MAGIC_DIR="${MAGIC_DIR:-.add}"
16
- fi
9
+ if ! type detect_active_add >/dev/null 2>&1; then
10
+ HOOK_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11
+ # shellcheck source=common.sh
12
+ source "$HOOK_LIB_DIR/common.sh"
17
13
  fi
18
-
19
- MAGIC_PATH="$PROJECT_DIR/$MAGIC_DIR"
20
- PLANS_DIR="$MAGIC_PATH/plans"
21
-
22
- # dev action 标记文件(项目级,PreToolUse 写入,Stop 读取)
23
- DEV_FLAG="/tmp/add_dev_$(echo "$PROJECT_DIR" | md5sum 2>/dev/null | cut -c1-8 || echo "default")"
24
-
25
- # 检测活跃 ADD 流程
26
- # 返回: "plan_keyword::step_x/total::round_n/total_r::handoff_path::add_route_path" 或 ""
27
- detect_active_add() {
28
- local handoff=""
29
- local today_dir="$PLANS_DIR/$(date +%Y-%m)"
30
- [ -d "$today_dir" ] && handoff=$(find "$today_dir" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
31
- [ -z "$handoff" ] && handoff=$(find "$PLANS_DIR" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
32
- [ -z "$handoff" ] && return 1
33
-
34
- local plan_kw=$(basename "$handoff" | sed 's/-handoff.*//')
35
-
36
- # add-route
37
- local add_route=$(find "$PLANS_DIR" -name "*add-route*.md" -type f 2>/dev/null | head -1)
38
-
39
- # Step 统计(从 add-route)
40
- local step_info="?"
41
- if [ -f "$add_route" ]; then
42
- local ck=$(grep -c '\[x\]' "$add_route" 2>/dev/null || true)
43
- local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || true)
44
- ck=${ck:-0}; uc=${uc:-0}
45
- local total=$((ck + uc))
46
- [ "$total" -gt 0 ] && step_info="${ck}/${total}"
47
- fi
48
-
49
- # 轮次统计(从 handoff)
50
- local round_info="?"
51
- if grep -q '<第[0-9]轮>' "$handoff" 2>/dev/null; then
52
- local ck=$(grep -c '\[x\]' "$handoff" 2>/dev/null || true); ck=${ck:-0}
53
- local total=$(grep -c '<第[0-9]轮>' "$handoff" 2>/dev/null || true); total=${total:-0}
54
- [ "$total" -gt 0 ] && round_info="${ck}/${total}"
55
- else
56
- round_info="1/1"
57
- fi
58
-
59
- echo "${plan_kw}::${step_info}::${round_info}::${handoff}::${add_route:-none}"
60
- }
61
-
62
- # 标记 dev action
63
- mark_dev_action() {
64
- touch "$DEV_FLAG" 2>/dev/null || true
65
- }
66
-
67
- # 检测 dev action
68
- has_dev_action() {
69
- [ -f "$DEV_FLAG" ]
70
- }
71
-
72
- # 清理 dev action 标记(验收闭环后调用)
73
- clear_dev_action() {
74
- rm -f "$DEV_FLAG" 2>/dev/null || true
75
- }
76
-
77
- # 检查验收完整度,返回 issues 文本
78
- check_add_completeness() {
79
- local handoff="$1" add_route="$2"
80
- local issues=""
81
-
82
- # devlog(内容已回流至 handoff,检查 handoff 是否含验收结果)
83
- if [ -f "$handoff" ] && ! grep -qE '验收|收敛|闭环|本轮改了什么|devlog' "$handoff" 2>/dev/null; then
84
- issues="${issues} [ ] devlog 缺失(handoff 无验收记录)\n"
85
- fi
86
-
87
- # handoff 验证标准
88
- if [ -f "$handoff" ]; then
89
- local uc=$(grep -c '\[ \]' "$handoff" 2>/dev/null || echo "0")
90
- [ "$uc" -gt 0 ] && issues="${issues} [ ] handoff ${uc} 项未勾选\n"
91
- fi
92
-
93
- # add-route Step
94
- if [ -f "$add_route" ]; then
95
- local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || echo "0")
96
- [ "$uc" -gt 0 ] && issues="${issues} [ ] add-route ${uc} Step 未闭环\n"
97
- fi
98
-
99
- echo -e "$issues"
100
- }
101
-
102
- # 检查是否已验收(幂等保护)
103
- # 返回 0 = 未验收,1 = 已验收
104
- is_already_accepted() {
105
- local add_route="$1" handoff="$2"
106
- # add-route Step 8 是否已 [x]
107
- if [ -f "$add_route" ]; then
108
- # 检查 Step 8 产出项是否勾选
109
- if grep -A 10 'Step 8' "$add_route" 2>/dev/null | grep -q '\[x\].*验证并更新项目状态'; then
110
- # handoff 是否有验收记录
111
- if [ -f "$handoff" ] && grep -qE '✅.*验收|收敛|全部闭环|全部.*完成' "$handoff" 2>/dev/null; then
112
- return 0
113
- fi
114
- fi
115
- fi
116
- return 1
117
- }
@@ -1,15 +1,21 @@
1
1
  #!/bin/bash
2
2
  # vocabulary.sh — 从 vocabulary markdown 表格加载触发词
3
- # 单一数据源: .qoder/vocabulary/add-governance-vocabulary.md §类别 A-F 表格
3
+ # 单一数据源: {MAGIC_DIR}/vocabulary/add-governance-vocabulary.md §类别 A-F 表格
4
+ # 协议层契约(Review P1 #5): 仅当前 magicDir scope;入口注入 MAGIC_DIR 或物理位置推导;
5
+ # 禁止跨端扫描与 .add fallback,禁止 adapter 名称默认值。
4
6
 
5
- # 动态探测 MAGIC_DIR
7
+ # MAGIC_DIR 解析: 入口注入优先;未注入时从脚本物理位置(<magicDir>/hooks/lib/)反推
6
8
  if [ -z "${MAGIC_DIR:-}" ]; then
7
- for m in ".claude" ".qoder" ".vscode" ".add"; do
8
- [ -d "${PROJECT_DIR:-$PWD}/$m" ] && { MAGIC_DIR="$m"; break; }
9
- done
10
- MAGIC_DIR="${MAGIC_DIR:-.add}"
9
+ if [ -n "${HOOK_DIR:-}" ]; then
10
+ MAGIC_DIR="$(basename "$(dirname "$HOOK_DIR")")"
11
+ else
12
+ # 无注入且无物理位置 → 无法判定 → 返回空(调用方 fail-closed,禁止猜测 adapter)
13
+ VOCABULARY_FILE=""
14
+ fi
15
+ fi
16
+ if [ -n "${MAGIC_DIR:-}" ]; then
17
+ VOCABULARY_FILE="${PROJECT_DIR:-$PWD}/$MAGIC_DIR/vocabulary/add-governance-vocabulary.md"
11
18
  fi
12
- VOCABULARY_FILE="${PROJECT_DIR:-$PWD}/$MAGIC_DIR/vocabulary/add-governance-vocabulary.md"
13
19
 
14
20
  # 输出格式: 优先级::触发词正则::响应文本(:: 避免与触发词内的 | 冲突)
15
21
  load_triggers() {
@@ -22,12 +22,10 @@ state=$(detect_active_add 2>/dev/null || true)
22
22
 
23
23
  IFS='::' read -r plan _ _ _ _ <<< "$state"
24
24
 
25
- # 动态探测
25
+ # 当前 magicDir 解析(入口注入优先;未注入时物理位置反推)
26
26
  if [ -z "${MAGIC_DIR:-}" ]; then
27
- for m in ".claude" ".qoder" ".vscode" ".add"; do
28
- [ -d "${PROJECT_DIR:-$PWD}/$m" ] && { MAGIC_DIR="$m"; break; }
29
- done
30
- MAGIC_DIR="${MAGIC_DIR:-.add}"
27
+ HOOK_DIR_ABS="$(cd "$(dirname "$0")" && pwd)"
28
+ MAGIC_DIR="$(basename "$(dirname "$HOOK_DIR_ABS")")"
31
29
  fi
32
30
  reviews_dir="${PROJECT_DIR}/$MAGIC_DIR/reviews"
33
31
  if ls "$reviews_dir"/*.md >/dev/null 2>&1; then
@@ -77,7 +77,7 @@ cat <<EOF
77
77
  EOF
78
78
 
79
79
  # ─── Hook 治理日报(注入 AI 上下文)───
80
- HOOK_JSONL="${MAGIC_DIR:-.qoder}/reports/hook-events.jsonl"
80
+ HOOK_JSONL="${MAGIC_DIR}/reports/hook-events.jsonl"
81
81
  if [ -f "$HOOK_JSONL" ]; then
82
82
  TODAY="$(date +%Y-%m-%d)"
83
83
  TOTAL=$(grep -c "\"ts\":\"${TODAY}" "$HOOK_JSONL" 2>/dev/null || echo 0)
@@ -9,10 +9,12 @@
9
9
  ###
10
10
  # stop-check.sh — VS Code Copilot Stop:四象限分流 + 验收检查
11
11
  # 治理卡位 #7: 验收检查 + devlog + 阻断
12
+ # DB lifecycle 真相源(协议层):DB 不可用时 fail-closed,禁止回退 Handoff/add-route 猜测
12
13
  set -euo pipefail
13
14
 
14
15
  HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
15
16
  export CURRENT_MAGIC=$(basename "$(dirname "$HOOK_DIR")")
17
+ export MAGIC_DIR=$(basename "$(dirname "$HOOK_DIR")")
16
18
  COMMON_LIB="$HOOK_DIR/lib/common.sh"
17
19
  [ -f "$COMMON_LIB" ] && source "$COMMON_LIB"
18
20
 
@@ -21,6 +23,13 @@ export PROJECT_DIR="$PWD"
21
23
  state=$(detect_active_add 2>/dev/null || true)
22
24
  has_dev=$(has_dev_action 2>/dev/null && echo "true" || echo "false")
23
25
 
26
+ # ═══════ Q0: DB 不可用 → fail closed(禁止当“无 Plan”) ═══════
27
+ if [ "${state%%::*}" = "__STATUS_UNAVAILABLE__" ]; then
28
+ reason=$(echo "$state" | awk -F'::' '{print $2}')
29
+ echo "[ADD Stop] ⛔ Plan status 暂不可用(${reason})。未回退 Handoff/add-route 猜测,请恢复数据库或 MCP resolver 后重试。" >&2
30
+ exit "${EXIT_BLOCK:-2}"
31
+ fi
32
+
24
33
  # ═══════ Q1: 无 ADD + 无 dev → 正常停 ═══════
25
34
  if [ -z "$state" ] && [ "$has_dev" != "true" ]; then
26
35
  exit 0
@@ -1,7 +1,7 @@
1
1
  # ADD 范式在 Claude Code 上的确定性运行
2
2
 
3
3
  > **定位**:描述 ADD 范式如何通过 Claude Code 的 Hook 机制在 agent 生命周期中确定性运行。面向 add-coder 用户和贡献者,说明每个 hook 事件的治理职能和注入通道。
4
- > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../.qoder/plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md) | [issue-6-report](../.qoder/reports/issue-6-tool-call-throttling-report.md)
4
+ > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md) | [issue-6-report](../reports/issue-6-tool-call-throttling-report.md)
5
5
  > **Hook 参考**: https://code.claude.com/docs/zh-CN/hooks
6
6
 
7
7
  ---
@@ -1,7 +1,7 @@
1
1
  # ADD 范式在 Codex 上的确定性运行
2
2
 
3
3
  > **定位**:描述 ADD 范式如何通过 Codex 的 Hook 机制在 agent 生命周期中确定性运行。
4
- > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../.qoder/plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
4
+ > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
5
5
  > **Hook 参考**: https://www.runoob.com/codex/codex-hooks.html | Codex 设置 → 导入其他 agent 配置
6
6
 
7
7
  ---
@@ -1,7 +1,7 @@
1
1
  # ADD 范式在 Qoder CN 上的确定性运行
2
2
 
3
3
  > **定位**:描述 ADD 范式如何通过 Qoder CN 的 Hook 机制在 agent 生命周期中确定性运行。
4
- > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../.qoder/plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
4
+ > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
5
5
  > **Hook 参考**: https://help.aliyun.com/zh/lingma/qoder-cn-update-log
6
6
 
7
7
  ---
@@ -1,7 +1,7 @@
1
1
  # ADD 范式在 Trae 上的确定性运行
2
2
 
3
3
  > **定位**:描述 ADD 范式如何通过 Trae 的 Hook 机制在 agent 生命周期中确定性运行。
4
- > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../.qoder/plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
4
+ > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md)
5
5
  > **Hook 参考**: https://docs.trae.cn/ide_automate-actions-with-hooks | https://docs.trae.cn/ide_hook-configuration-reference
6
6
 
7
7
  ---
@@ -1,7 +1,7 @@
1
1
  # ADD 范式在 VS Code Copilot 上的确定性运行
2
2
 
3
3
  > **定位**:描述 ADD 范式如何通过 VS Code Copilot 的 Agent Hook 机制在 agent 生命周期中确定性运行。
4
- > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../.qoder/plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md) | [issue-6-report](../.qoder/reports/issue-6-tool-call-throttling-report.md)
4
+ > **关联文档**:[add-coder-hook-full-alignment-plan-v1](../plans/2026-07/17/add-coder-hook-full-alignment-plan-v1.md) | [issue-6-report](../reports/issue-6-tool-call-throttling-report.md)
5
5
  > **Hook 参考**: https://docs.github.com/zh/copilot/concepts/agents/hooks | https://vscode.js.cn/docs/agent-customization/hooks
6
6
  > **VS Code 版本要求**: 1.127+(Agent Hook 预览),1.129+(Agent Host 架构,支持 `.claude/settings.json`)
7
7
 
@@ -63,45 +63,54 @@ fi
63
63
  PROJECT_DIR="$PWD"
64
64
  TEMPLATES_DIR="$PROJECT_DIR/$MAGIC_DIR/templates"
65
65
 
66
- # 通过 filename 匹配模板名
66
+ # 文件名类型 token 匹配(特异性排序,取代子串 grep;R5/Task 1.4)
67
+ # review/handoff 不在此层绑定(需内容特征细分单多轮/运行时),留给下方探测链
67
68
  TEMPLATE_NAME=""
68
- for tmpl in "$TEMPLATES_DIR"/*.md; do
69
- base=$(basename "$tmpl")
70
- if echo "$file_path" | grep -q "$base"; then
71
- TEMPLATE_NAME="$base"
72
- break
73
- fi
74
- done
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
75
84
 
76
85
  # 退一步:根据文件内容特征猜测模板类型
77
86
  if [ -z "$TEMPLATE_NAME" ]; then
78
- if echo "$CONTENT" | grep -q "## PLAN 元信息"; then
87
+ if grep -q "## PLAN 元信息" <<< "$CONTENT"; then
79
88
  TEMPLATE_NAME="standard-plan-template.md"
80
- elif echo "$CONTENT" | grep -q "## 一、Plan 概述"; then
89
+ elif grep -q "## 一、Plan 概述" <<< "$CONTENT"; then
81
90
  TEMPLATE_NAME="simple-plan-template.md"
82
- elif echo "$CONTENT" | grep -q "## 四、Handoff"; then
91
+ elif grep -q "## 四、Handoff" <<< "$CONTENT"; then
83
92
  TEMPLATE_NAME="simple-plan-template.md"
84
- elif echo "$CONTENT" | grep -q "## Review 元信息"; then
85
- if echo "$CONTENT" | grep -q "运行时验证"; then
93
+ elif grep -q "## Review 元信息" <<< "$CONTENT"; then
94
+ if grep -q "运行时验证" <<< "$CONTENT"; then
86
95
  TEMPLATE_NAME="review-runtime-template.md"
87
- elif echo "$CONTENT" | grep -q "跨仓库格式契约"; then
96
+ elif grep -q "跨仓库格式契约" <<< "$CONTENT"; then
88
97
  TEMPLATE_NAME="review-implementation-template.md"
89
98
  else
90
99
  TEMPLATE_NAME="review-template.md"
91
100
  fi
92
- elif echo "$CONTENT" | grep -q "## Why"; then
101
+ elif grep -q "## Why" <<< "$CONTENT"; then
93
102
  TEMPLATE_NAME="spec-template.md"
94
- elif echo "$CONTENT" | grep -q "## Preconditions"; then
103
+ elif grep -q "## Preconditions" <<< "$CONTENT"; then
95
104
  TEMPLATE_NAME="tasks-template.md"
96
- elif echo "$CONTENT" | grep -q "审计链(证据→devlog→checklist)"; then
105
+ elif grep -q "审计链(证据→devlog→checklist)" <<< "$CONTENT"; then
97
106
  TEMPLATE_NAME="checklist-template.md"
98
107
  else
99
108
  case "$file_path" in
100
109
  *handoff*)
101
110
  # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
102
- if echo "$CONTENT" | grep -qF "## 全局元信息"; then
111
+ if grep -qF "## 全局元信息" <<< "$CONTENT"; then
103
112
  TEMPLATE_NAME="handoff-multi-round-template.md"
104
- elif echo "$CONTENT" | grep -qF "## 1. 交接前状态"; then
113
+ elif grep -qF "## 1. 交接前状态" <<< "$CONTENT"; then
105
114
  TEMPLATE_NAME="handoff-single-round-template.md"
106
115
  else
107
116
  echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
@@ -110,9 +119,9 @@ if [ -z "$TEMPLATE_NAME" ]; then
110
119
  exit 2
111
120
  fi
112
121
  ;;
113
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
114
122
  *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
115
123
  *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
124
+ *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
116
125
  *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
117
126
  *spec*) TEMPLATE_NAME="spec-template.md" ;;
118
127
  *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
@@ -122,7 +131,7 @@ if [ -z "$TEMPLATE_NAME" ]; then
122
131
  *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
123
132
  *)
124
133
  # 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
125
- if echo "$CONTENT" | grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订'; then
134
+ if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
126
135
  echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
127
136
  exit 0
128
137
  fi
@@ -154,17 +163,56 @@ ISSUES=""
154
163
  TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
155
164
  : > "$TMPFILE"
156
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
+
157
205
  # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
158
206
  if [ "$IS_SEARCH_REPLACE" != "true" ]; then
159
- jq -r '.sections[] | select(.required == true) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
160
- if ! echo "$CONTENT" | grep -qF "$heading"; 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
161
209
  echo " 缺章节: $heading"
162
210
  fi
163
- done > "$TMPFILE"
211
+ done >> "$TMPFILE"
164
212
 
165
213
  # 子章节
166
- jq -r '.sections[].subsections[]?.heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
167
- if ! echo "$CONTENT" | grep -qF "$sub"; then
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
168
216
  echo " 缺子章节: $sub"
169
217
  fi
170
218
  done >> "$TMPFILE"
@@ -172,15 +220,22 @@ fi
172
220
 
173
221
  # ── 占位符校验 ──
174
222
  jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
175
- if echo "$CONTENT" | grep -qF "$ph"; then
223
+ if grep -qF "$ph" <<< "$CONTENT"; then
176
224
  echo " 未替换占位符: $ph"
177
225
  fi
178
226
  done >> "$TMPFILE"
179
227
 
180
- # ── 禁止词校验 ──
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
181
235
  jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
182
- if echo "$CONTENT" | grep -qw "$term"; then
183
- echo " 禁止词: $term"
236
+ # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
237
+ if grep -qF "$term" <<< "$struct_text"; then
238
+ echo " 结构位禁词: $term"
184
239
  fi
185
240
  done >> "$TMPFILE"
186
241
 
@@ -192,29 +247,29 @@ ALGO_ISSUES=""
192
247
  # 规则1: 精简版 Plan 反作弊
193
248
  if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
194
249
  # 1a. 文件数 ≤ 3
195
- file_count=$(echo "$CONTENT" | grep -cP '^\|\s*`[^`]+`' 2>/dev/null || echo "0")
250
+ file_count=$(grep -cP '^\|\s*`[^`]+`' <<< "$CONTENT" 2>/dev/null || echo "0")
196
251
  if [ "$file_count" -gt 3 ] 2>/dev/null; then
197
252
  ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 涉及 ${file_count} 个文件(超过 3 个限制),应改用 standard-plan-template.md\n"
198
253
  fi
199
254
  # 1b. HITL 表不能写 "等 N 个文件"
200
- if echo "$CONTENT" | grep -qP '等\s*\d*\s*个文件|等\s*若干'; then
255
+ if grep -qP '等\s*\d*\s*个文件|等\s*若干' <<< "$CONTENT"; then
201
256
  ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表文件清单使用模糊描述('等 N 个文件'),必须列出实际完整路径\n"
202
257
  fi
203
258
  # 1c. HITL 方案/设计决策不能写 "等若干决策"
204
- if echo "$CONTENT" | grep -qP '等\s*若干\s*(决策|方案|设计)'; then
259
+ if grep -qP '等\s*若干\s*(决策|方案|设计)' <<< "$CONTENT"; then
205
260
  ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表方案/设计决策使用模糊描述('等若干决策'),必须逐条列出\n"
206
261
  fi
207
262
  # 1d. 不能包含架构设计章节(精简版无架构设计)
208
- if echo "$CONTENT" | grep -q '## 三、架构设计'; then
263
+ if grep -q '## 三、架构设计' <<< "$CONTENT"; then
209
264
  ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 包含 '## 三、架构设计' 章节,精简版不应有架构设计——应改用 standard-plan-template.md\n"
210
265
  fi
211
266
  fi
212
267
 
213
268
  # 规则2: HITL 表非空校验(所有 Plan + Review 模板)
214
269
  if echo "$TEMPLATE_NAME" | grep -qE 'plan|review'; then
215
- if echo "$CONTENT" | grep -q '## HITL'; then
270
+ if grep -q '## HITL' <<< "$CONTENT"; then
216
271
  # HITL 表至少要有 1 行非占位符内容(不包含 { } 占位符)
217
- hitl_rows=$(echo "$CONTENT" | sed -n '/## HITL/,/^## /p' | grep -cP '^\|\s*[^|{]*\s*\|' 2>/dev/null || echo "0")
272
+ hitl_rows=$(sed -n '/## HITL/,/^## /p' <<< "$CONTENT" | grep -cP '^\|\s*[^|{]*\s*\|' 2>/dev/null || echo "0")
218
273
  # rows 包含表头行和分隔行,实际数据行 = rows - 2
219
274
  hitl_data=$((hitl_rows - 2))
220
275
  if [ "$hitl_data" -lt 1 ] 2>/dev/null; then
@@ -242,12 +297,36 @@ fi
242
297
 
243
298
  ISSUES=$(cat "$TMPFILE" 2>/dev/null)
244
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
+
245
322
  # ── 阻断或放行 ──
246
323
  if [ -n "$ISSUES" ]; then
247
324
  echo "⛔ $TEMPLATE_NAME 校验不通过:
248
325
  $ISSUES" >&2
249
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过,请对照模板修正\"}}"
250
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
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
251
330
  exit 2
252
331
  fi
253
332
 
@@ -258,4 +337,7 @@ if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
258
337
  fi
259
338
  fi
260
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
+
261
343
  exit 0