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
@@ -1,117 +1,10 @@
1
1
  #!/bin/bash
2
- # state-detect.sh — ADD 活跃流程检测 + dev action 追踪
3
- # 共享库
4
-
5
- PROJECT_DIR="$PWD"
6
-
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
2
+ # state-detect.sh — 兼容入口;状态裁决统一委托 common.sh,禁止维护第二套 magicDir 探测。
3
+ # 协议层契约(Review P1 #5): 仅当前 magicDir scope;禁止跨端扫描与 .add fallback;
4
+ # 禁止 adapter 名称默认值(入口注入 MAGIC_DIR 或物理位置推导)。
5
+
6
+ if ! type detect_active_add >/dev/null 2>&1; then
7
+ HOOK_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8
+ # shellcheck source=common.sh
9
+ source "$HOOK_LIB_DIR/common.sh"
17
10
  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() {
@@ -62,8 +62,9 @@ EOF
62
62
  exit 2
63
63
  fi
64
64
 
65
- # 检测 2: sed -i 原地编辑
66
- if echo "$command" | grep -qE '\bsed\b.*-i'; then
65
+ # 检测 2: sed -i 原地编辑(精确判定:`-i` 必须是 sed 独立 option;不把路径子串如 session-init 误判)
66
+ # 协议层契约(Review P0 #3/命令分类): codex 已实现版本同契约语义
67
+ if echo "$command" | grep -qE '(^|[;&|][[:space:]]*)sed[[:space:]]+([^;&|]*[[:space:]])?(-[[:alpha:]]*i[^[:space:];&|]*|--in-place(=[^[:space:];&|]*)?)([[:space:];&|]|$)'; then
67
68
  _reason="禁止通过 sed -i 直接编辑文件。请使用 SearchReplace 工具。"
68
69
  cat >&2 <<'EOF'
69
70
  ⛔ [ADD PreToolUse §A] 阻断: 禁止通过 sed -i 原地编辑文件。
@@ -136,7 +137,8 @@ if [ "$tool_name" = "Write" ] || [ "$tool_name" = "Edit" ] || [ "$tool_name" = "
136
137
  file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
137
138
  [ -z "$file_path" ] && exit 0
138
139
 
139
- if echo "$file_path" | grep -qE '\.(qoder|claude|add)/(plans|specs|reviews)/'; then
140
+ # §B: 写入守卫按当前 magicDir 限域(Review P1 #5: 禁止多端路径正则)
141
+ if echo "$file_path" | grep -qE "${MAGIC_DIR}/(plans|specs|reviews)/"; then
140
142
  if type detect_active_add >/dev/null 2>&1; then
141
143
  state=$(detect_active_add 2>/dev/null || true)
142
144
  if [ -z "$state" ]; then
@@ -154,8 +156,8 @@ if [ "$tool_name" = "Write" ] || [ "$tool_name" = "Edit" ] || [ "$tool_name" = "
154
156
  exit 2
155
157
  fi
156
158
 
157
- # §C: 模板类型前置注入 — plans/ 写入前提示应选模板
158
- if echo "$file_path" | grep -qE '\.(qoder|claude|add|vscode|trae)/(plans)/'; then
159
+ # §C: 模板类型前置注入 — 仅当前 magicDir plans/ 写入前提示应选模板
160
+ if echo "$file_path" | grep -qE "${MAGIC_DIR}/(plans)/"; then
159
161
  fname=$(basename "$file_path")
160
162
  if echo "$fname" | grep -qE 'plan-v[0-9]'; then
161
163
  echo "💡 [ADD PreToolUse] 写入 Plan → 模板: standard-plan-template.md(标准)或 simple-plan-template.md(≤3文件)" >&2
@@ -177,13 +179,13 @@ if [ "$tool_name" = "Write" ] || [ "$tool_name" = "Edit" ] || [ "$tool_name" = "
177
179
  # implementation/runtime review 不需要 HITL,走 §B 活跃 Plan 检查
178
180
  # handoff 是 Step 8 收敛产物(多轮交接手册),不经 HITL 审批,同样豁免
179
181
  # TODO 应该caijuehub管理
180
- if echo "$file_path" | grep -qE '\.(qoder|claude|add|vscode|trae)/(plans)/'; then
182
+ if echo "$file_path" | grep -qE "${MAGIC_DIR}/(plans)/"; then
181
183
  if echo "$file_path" | grep -qE '\-handoff'; then
182
184
  _do_hitl=false # handoff 不被 HITL 拦截(Step 8 产物,无独立审批)
183
185
  else
184
186
  _do_hitl=true
185
187
  fi
186
- elif echo "$file_path" | grep -qE '\.(qoder|claude|add|vscode|trae)/(reviews)/'; then
188
+ elif echo "$file_path" | grep -qE "${MAGIC_DIR}/(reviews)/"; then
187
189
  if echo "$file_path" | grep -qE '-(implementation|runtime)'; then
188
190
  _do_hitl=false # implementation/runtime review 不被 HITL 拦截
189
191
  else
@@ -194,7 +196,11 @@ if [ "$tool_name" = "Write" ] || [ "$tool_name" = "Edit" ] || [ "$tool_name" = "
194
196
  fi
195
197
 
196
198
  if [ "$_do_hitl" = true ]; then
197
- _relative=$(echo "$file_path" | sed 's|.*/\.\(qoder\|claude\|add\|vscode\|trae\)/\(plans\|reviews\)/||')
199
+ _relative=$(echo "$file_path" | sed "s|.*/\.\(qoder\|claude\|add\|vscode\|trae\)/\(plans\|reviews\)/||")
200
+ # 限域: 仅当前 magicDir 下的 plans/reviews 触发 HITL 门禁
201
+ if ! echo "$file_path" | grep -qE "${MAGIC_DIR}/(plans|reviews)/"; then
202
+ _do_hitl=false
203
+ fi
198
204
  _planName=$(basename "$_relative" .md | sed 's/\.hitl$//;s/-plan-v[0-9]*$//;s/-add-route-v[0-9]*$//;s/-review-v[0-9]*$//;s/-review-implementation$//;s/-review-runtime$//')
199
205
  if [ -n "$_planName" ]; then
200
206
  _tongyi_marker="${PROJECT_DIR}/${MAGIC_DIR}/hitl/.tongyi-${_planName}"
@@ -1,10 +1,12 @@
1
1
  #!/bin/bash
2
2
  # stop-check.sh — Claude Code Stop:四象限分流 + 验收检查(few-shot 上下文注入)
3
3
  # 治理卡位 #7: 验收检查 + devlog + 阻断
4
+ # DB lifecycle 真相源(协议层):DB 不可用时 fail-closed,禁止回退 Handoff/add-route 猜测
4
5
  set -euo pipefail
5
6
 
6
7
  HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
7
8
  export CURRENT_MAGIC=$(basename "$(dirname "$HOOK_DIR")")
9
+ export MAGIC_DIR=$(basename "$(dirname "$HOOK_DIR")")
8
10
  COMMON_LIB="$HOOK_DIR/lib/common.sh"
9
11
  [ -f "$COMMON_LIB" ] && source "$COMMON_LIB"
10
12
 
@@ -13,6 +15,13 @@ export PROJECT_DIR="$PWD"
13
15
  state=$(detect_active_add 2>/dev/null || true)
14
16
  has_dev=$(has_dev_action 2>/dev/null && echo "true" || echo "false")
15
17
 
18
+ # ═══════ Q0: DB 不可用 → fail closed(禁止当“无 Plan”) ═══════
19
+ if [ "${state%%::*}" = "__STATUS_UNAVAILABLE__" ]; then
20
+ reason=$(echo "$state" | awk -F'::' '{print $2}')
21
+ echo "[ADD Stop] ⛔ Plan status 暂不可用(${reason})。未回退 Handoff/add-route 猜测,请恢复数据库或 MCP resolver 后重试。" >&2
22
+ exit "${EXIT_BLOCK:-2}"
23
+ fi
24
+
16
25
  # ═══════ Q1: 无 ADD + 无 dev → 正常停 ═══════
17
26
  if [ -z "$state" ] && [ "$has_dev" != "true" ]; then
18
27
  exit 0
@@ -62,43 +62,52 @@ fi
62
62
  PROJECT_DIR="$PWD"
63
63
  TEMPLATES_DIR="$PROJECT_DIR/$MAGIC_DIR/templates"
64
64
 
65
- # 通过 filename 匹配模板名
65
+ # 文件名类型 token 匹配(特异性排序,取代子串 grep;R5/Task 1.4)
66
+ # review/handoff 不在此层绑定(需内容特征细分单多轮/运行时),留给下方探测链
66
67
  TEMPLATE_NAME=""
67
- for tmpl in "$TEMPLATES_DIR"/*.md; do
68
- base=$(basename "$tmpl")
69
- if echo "$file_path" | grep -q "$base"; then
70
- TEMPLATE_NAME="$base"
71
- break
72
- fi
73
- done
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
74
83
 
75
84
  # 退一步:根据文件内容特征猜测模板类型
76
85
  if [ -z "$TEMPLATE_NAME" ]; then
77
- if echo "$CONTENT" | grep -q "## 四、Handoff"; then
86
+ if grep -q "## 四、Handoff" <<< "$CONTENT"; then
78
87
  TEMPLATE_NAME="simple-standard-plan-template.md"
79
- elif echo "$CONTENT" | grep -q "## PLAN 元信息"; then
88
+ elif grep -q "## PLAN 元信息" <<< "$CONTENT"; then
80
89
  TEMPLATE_NAME="standard-plan-template.md"
81
- elif echo "$CONTENT" | grep -q "## Review 元信息"; then
82
- if echo "$CONTENT" | grep -q "运行时验证"; then
90
+ elif grep -q "## Review 元信息" <<< "$CONTENT"; then
91
+ if grep -q "运行时验证" <<< "$CONTENT"; then
83
92
  TEMPLATE_NAME="review-runtime-template.md"
84
- elif echo "$CONTENT" | grep -q "跨仓库格式契约"; then
93
+ elif grep -q "跨仓库格式契约" <<< "$CONTENT"; then
85
94
  TEMPLATE_NAME="review-implementation-template.md"
86
95
  else
87
96
  TEMPLATE_NAME="review-template.md"
88
97
  fi
89
- elif echo "$CONTENT" | grep -q "## Why"; then
98
+ elif grep -q "## Why" <<< "$CONTENT"; then
90
99
  TEMPLATE_NAME="spec-template.md"
91
- elif echo "$CONTENT" | grep -q "## Preconditions"; then
100
+ elif grep -q "## Preconditions" <<< "$CONTENT"; then
92
101
  TEMPLATE_NAME="tasks-template.md"
93
- elif echo "$CONTENT" | grep -q "审计链(证据→devlog→checklist)"; then
102
+ elif grep -q "审计链(证据→devlog→checklist)" <<< "$CONTENT"; then
94
103
  TEMPLATE_NAME="checklist-template.md"
95
104
  else
96
105
  case "$file_path" in
97
106
  *handoff*)
98
107
  # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
99
- if echo "$CONTENT" | grep -qF "## 全局元信息"; then
108
+ if grep -qF "## 全局元信息" <<< "$CONTENT"; then
100
109
  TEMPLATE_NAME="handoff-multi-round-template.md"
101
- elif echo "$CONTENT" | grep -qF "## 1. 交接前状态"; then
110
+ elif grep -qF "## 1. 交接前状态" <<< "$CONTENT"; then
102
111
  TEMPLATE_NAME="handoff-single-round-template.md"
103
112
  else
104
113
  echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
@@ -107,18 +116,19 @@ if [ -z "$TEMPLATE_NAME" ]; then
107
116
  exit 2
108
117
  fi
109
118
  ;;
110
- *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
111
119
  *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
112
120
  *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
121
+ *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
113
122
  *tasks*) TEMPLATE_NAME="tasks-template.md" ;;
114
123
  *spec*) TEMPLATE_NAME="spec-template.md" ;;
115
124
  *checklist*) TEMPLATE_NAME="checklist-template.md" ;;
116
125
  *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
117
126
  *report*) TEMPLATE_NAME="report-template.md" ;;
118
127
  *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
128
+ *hitl*) TEMPLATE_NAME="hitl-template.md" ;;
119
129
  *)
120
130
  # 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
121
- if echo "$CONTENT" | grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订'; then
131
+ if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
122
132
  echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
123
133
  exit 0
124
134
  fi
@@ -150,17 +160,54 @@ ISSUES=""
150
160
  TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
151
161
  : > "$TMPFILE"
152
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
+
153
200
  # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
154
201
  if [ "$IS_SEARCH_REPLACE" != "true" ]; then
155
- jq -r '.sections[] | select(.required == true) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
156
- if ! echo "$CONTENT" | grep -qF "$heading"; 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
157
204
  echo " 缺章节: $heading"
158
205
  fi
159
- done > "$TMPFILE"
206
+ done >> "$TMPFILE"
160
207
 
161
208
  # 子章节
162
- jq -r '.sections[].subsections[]?.heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
163
- if ! echo "$CONTENT" | grep -qF "$sub"; then
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
164
211
  echo " 缺子章节: $sub"
165
212
  fi
166
213
  done >> "$TMPFILE"
@@ -168,26 +215,57 @@ fi
168
215
 
169
216
  # ── 占位符校验 ──
170
217
  jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
171
- if echo "$CONTENT" | grep -qF "$ph"; then
218
+ if grep -qF "$ph" <<< "$CONTENT"; then
172
219
  echo " 未替换占位符: $ph"
173
220
  fi
174
221
  done >> "$TMPFILE"
175
222
 
176
- # ── 禁止词校验 ──
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
177
230
  jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
178
- if echo "$CONTENT" | grep -qw "$term"; then
179
- echo " 禁止词: $term"
231
+ # 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
232
+ if grep -qF "$term" <<< "$struct_text"; then
233
+ echo " 结构位禁词: $term"
180
234
  fi
181
235
  done >> "$TMPFILE"
182
236
 
183
237
  ISSUES=$(cat "$TMPFILE" 2>/dev/null)
184
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
+
185
261
  # ── 阻断或放行 ──
186
262
  if [ -n "$ISSUES" ]; then
187
263
  echo "⛔ $TEMPLATE_NAME 校验不通过:
188
264
  $ISSUES" >&2
189
- echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过,请对照模板修正\"}}"
190
- write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" 2>/dev/null || true
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
191
269
  exit 2
192
270
  fi
193
271
 
@@ -198,4 +276,7 @@ if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
198
276
  fi
199
277
  fi
200
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
+
201
282
  exit 0
@@ -34,99 +34,58 @@ json_get() {
34
34
 
35
35
  # ── ADD 活跃 Plan 检测(裁决定逻辑,SKILL §0.7.1) ──
36
36
  #
37
- # 活跃判定标准:
38
- # 1. magicDir 优先级: Hook 脚本自身的路径推断(.claude/hooks/xxx.sh → .claude)→ .add → stderr 提示全局搜索
39
- # 2. 索引优先: 先读 plans/index.md 匹配 无匹配才 glob *-handoff*.md
40
- # 3. 活跃定义: add-route 中存在 [ ] 未勾选项 = 活跃;全部 [x] = 已收敛
41
- # 4. 多 Plan 冲突: 取 add-route 最近 mtime
42
- # 5. add-route: Plan 初始态,视为活跃
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 名称默认值。
43
45
  #
44
46
  # 前置条件: 调用方需设置 PROJECT_DIR 环境变量(项目根目录)
45
- # 返回: "plan_keyword::step_n/total::round_n/total::handoff_path::add_route_path" 或 ""
46
-
47
- detect_active_add() {
48
- local plans_dir=""
49
-
50
- # 1. magicDir 回退: 当前 IDE(CURRENT_MAGIC)→ 目录检测 → .add → 提示全局搜索
51
- local magic_dirs=()
52
- if [ -n "${CURRENT_MAGIC:-}" ]; then
53
- magic_dirs+=("$CURRENT_MAGIC")
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"
54
57
  else
55
- # 兜底: 检测 PROJECT_DIR 下哪个 magicDir 存在
56
- for m in ".claude" ".qoder" ".vscode" ".trae"; do
57
- [ -d "${PROJECT_DIR:-$PWD}/$m" ] && { magic_dirs+=("$m"); break; }
58
- done
59
- fi
60
- magic_dirs+=(".add")
61
-
62
- for magic in "${magic_dirs[@]}"; do
63
- if [ -d "${PROJECT_DIR:-$PWD}/$magic/plans" ]; then
64
- plans_dir="${PROJECT_DIR:-$PWD}/$magic/plans"
65
- break
66
- fi
67
- done
68
-
69
- if [ -z "$plans_dir" ]; then
70
- echo "[ADD detect] 未在当前 IDE magicDir 和 .add 中找到 plans 目录。是否需要全局搜索所有 magicDir?" >&2
71
- return 1
72
- fi
73
-
74
- local handoff=""
75
- local add_route=""
76
-
77
- # 2. 索引优先: 先读 index.md
78
- if [ -f "$plans_dir/index.md" ]; then
79
- # 从 index.md 表格提取最近一个 handoff 相对路径
80
- # 表格格式: | YYYY-MM/DD/path-to-handoff.md | ...
81
- handoff=$(grep -oE '[0-9]{4}-[0-9]{2}/[0-9]{2}/[^|]*handoff[^|]*\.md' "$plans_dir/index.md" 2>/dev/null | head -1 | xargs)
82
- if [ -n "$handoff" ] && [ -f "$plans_dir/$handoff" ]; then
83
- handoff="$plans_dir/$handoff"
84
- else
85
- handoff=""
86
- fi
58
+ echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"magicDir 未注入且无法从物理位置推导"}'
59
+ return 3
87
60
  fi
88
-
89
- # 3. 退路: glob handoff 文件(mtime 7 天内)
90
- if [ -z "$handoff" ]; then
91
- local today_dir="$plans_dir/$(date +%Y-%m)"
92
- [ -d "$today_dir" ] && handoff=$(find "$today_dir" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
93
- [ -z "$handoff" ] && handoff=$(find "$plans_dir" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
61
+ if [ ! -f "$bridge" ]; then
62
+ echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"plan-status bridge missing"}'
63
+ return 3
94
64
  fi
95
- [ -z "$handoff" ] && return 1
96
-
97
- # 提取 plan keyword
98
- local plan_kw=$(basename "$handoff" | sed 's/-handoff.*//')
99
-
100
- # 查找 add-route
101
- local handoff_dir=$(dirname "$handoff")
102
- add_route=$(find "$handoff_dir" -name "*add-route*.md" -type f 2>/dev/null | head -1)
103
- [ -z "$add_route" ] && add_route=$(find "$plans_dir" -name "*add-route*.md" -type f 2>/dev/null | head -1)
65
+ node --import tsx "$bridge"
66
+ }
104
67
 
105
- # 4. 活跃判定: 读 add-route,存在 [ ] 未勾选项 = 活跃
106
- local step_info="?"
107
- if [ -f "$add_route" ]; then
108
- local ck=$(grep -c '\[x\]' "$add_route" 2>/dev/null || true)
109
- local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || true)
110
- ck=${ck:-0}; uc=${uc:-0}
111
- local total=$((ck + uc))
112
- [ "$total" -gt 0 ] && step_info="${ck}/${total}"
113
- # 全部 [x] 已收敛(但 handoff 还存在 可能是遗留,仍返回但标记)
114
- else
115
- # 无 add-route → Plan 初始态,视为活跃
116
- step_info="init"
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
117
79
  fi
118
-
119
- # 5. 轮次统计(从 handoff)
120
- local round_info="?"
121
- if grep -q '<第[0-9]轮>' "$handoff" 2>/dev/null; then
122
- local ck=$(grep -c '\[x\]' "$handoff" 2>/dev/null || true); ck=${ck:-0}
123
- local total=$(grep -c '<第[0-9]轮>' "$handoff" 2>/dev/null || true); total=${total:-0}
124
- [ "$total" -gt 0 ] && round_info="${ck}/${total}"
125
- else
126
- round_info="1/1"
80
+ if ! printf '%s' "$snapshot" | jq -e '.availability == "READY" and .isActive == true' >/dev/null 2>&1; then
81
+ return 1
127
82
  fi
128
-
129
- echo "${plan_kw}::${step_info}::${round_info}::${handoff}::${add_route:-none}"
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"
130
89
  }
131
90
 
132
91
  # ── Dev Action 追踪 ──
@@ -223,8 +182,8 @@ Plan 不是"文档开销"——它是代码治理的基础设施。跳过 Plan
223
182
  方案 B — 补不上则回滚:
224
183
  如果改动太复杂无法追溯生成 Plan,则:
225
184
  ① git diff 确认改动范围
226
- git checkout -- <改动的文件> 撤回
227
- 告知用户 "以下改动已回滚,请先走 ADD 流程再重新实施: <文件列表>"
185
+ 仅对已确认属于本轮的文件生成反向 apply_patch
186
+ 无法安全确认改动所有权时停止,并请求用户决定
228
187
 
229
188
  无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
230
189
  EOF
@@ -45,8 +45,8 @@ Plan 不是"文档开销"——它是代码治理的基础设斻。跳过 Plan
45
45
  方案 B — 补不上则回滚:
46
46
  如果改动太复杂无法追溯生成 Plan,则:
47
47
  ① git diff 确认改动范围
48
- git checkout -- <改动的文件> 撤回
49
- 告知用户 "以下改动已回滚,请先走 ADD 流程再重新实施: <文件列表>"
48
+ 仅对已确认属于本轮的文件生成反向 apply_patch
49
+ 无法安全确认改动所有权时停止,并请求用户决定
50
50
 
51
51
  无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
52
52
  EOF