add-coder 0.3.25 → 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.
- package/README.md +36 -10
- package/dist/index.js +481 -209
- package/package.json +14 -4
- package/templates/.add-coder-src-hash.json +129 -106
- package/templates/adapters/claude/hooks/doc-format-guard.sh +114 -33
- package/templates/adapters/claude/hooks/lib/common.sh +46 -87
- package/templates/adapters/claude/hooks/lib/context-inject.sh +2 -2
- package/templates/adapters/claude/hooks/lib/notify.sh +6 -4
- package/templates/adapters/claude/hooks/lib/preload-templates.sh +27 -2
- package/templates/adapters/claude/hooks/lib/state-detect.sh +8 -112
- package/templates/adapters/claude/hooks/lib/vocabulary.sh +13 -7
- package/templates/adapters/claude/hooks/prompt-submit.sh +1 -1
- package/templates/adapters/claude/hooks/stop-check.sh +9 -0
- package/templates/adapters/codex/config.toml.example +12 -0
- package/templates/adapters/codex/hooks/doc-format-guard.sh +123 -41
- package/templates/adapters/codex/hooks/lib/common.sh +47 -99
- package/templates/adapters/codex/hooks/lib/context-inject.sh +4 -4
- package/templates/adapters/codex/hooks/lib/notify.sh +6 -4
- package/templates/adapters/codex/hooks/lib/preload-templates.sh +27 -6
- package/templates/adapters/codex/hooks/lib/state-detect.sh +5 -114
- package/templates/adapters/codex/hooks/lib/vocabulary.sh +3 -8
- package/templates/adapters/codex/hooks/notification.sh +3 -18
- package/templates/adapters/codex/hooks/post-tool-use.sh +24 -119
- package/templates/adapters/codex/hooks/pre-tool-use.sh +97 -184
- package/templates/adapters/codex/hooks/prompt-submit.sh +8 -3
- package/templates/adapters/codex/hooks/session-start.sh +19 -9
- package/templates/adapters/codex/hooks/stop-check.sh +44 -18
- package/templates/adapters/codex/hooks.json +67 -1
- package/templates/adapters/qoder/hooks/doc-format-guard.sh +121 -40
- package/templates/adapters/qoder/hooks/lib/common.sh +49 -87
- package/templates/adapters/qoder/hooks/lib/context-inject.sh +2 -2
- package/templates/adapters/qoder/hooks/lib/notify.sh +6 -4
- package/templates/adapters/qoder/hooks/lib/preload-templates.sh +27 -2
- package/templates/adapters/qoder/hooks/lib/state-detect.sh +8 -112
- package/templates/adapters/qoder/hooks/lib/vocabulary.sh +13 -7
- package/templates/adapters/qoder/hooks/prompt-submit.sh +1 -1
- package/templates/adapters/qoder/hooks/session-start.sh +1 -1
- package/templates/adapters/qoder/hooks/stop-check.sh +11 -0
- package/templates/adapters/trae/hooks/doc-format-guard.sh +122 -40
- package/templates/adapters/trae/hooks/lib/common.sh +46 -87
- package/templates/adapters/trae/hooks/lib/context-inject.sh +2 -2
- package/templates/adapters/trae/hooks/lib/notify.sh +5 -3
- package/templates/adapters/trae/hooks/lib/preload-templates.sh +27 -2
- package/templates/adapters/trae/hooks/lib/state-detect.sh +8 -115
- package/templates/adapters/trae/hooks/lib/vocabulary.sh +13 -7
- package/templates/adapters/trae/hooks/pre-tool-use.sh +14 -8
- package/templates/adapters/trae/hooks/stop-check.sh +9 -0
- package/templates/adapters/vscode/hooks/doc-format-guard.sh +114 -33
- package/templates/adapters/vscode/hooks/lib/common.sh +46 -87
- package/templates/adapters/vscode/hooks/lib/context-inject.sh +2 -2
- package/templates/adapters/vscode/hooks/lib/notify.sh +6 -4
- package/templates/adapters/vscode/hooks/lib/preload-templates.sh +27 -2
- package/templates/adapters/vscode/hooks/lib/state-detect.sh +8 -112
- package/templates/adapters/vscode/hooks/lib/vocabulary.sh +13 -7
- package/templates/adapters/vscode/hooks/notification.sh +3 -5
- package/templates/adapters/vscode/hooks/prompt-submit.sh +1 -1
- package/templates/adapters/vscode/hooks/stop-check.sh +9 -0
- package/templates/core/docs/ADD-governance-claude-code.md +1 -1
- package/templates/core/docs/ADD-governance-codex.md +1 -1
- package/templates/core/docs/ADD-governance-qoder-cn.md +1 -1
- package/templates/core/docs/ADD-governance-trae.md +1 -1
- package/templates/core/docs/ADD-governance-vscode-copilot.md +1 -1
- package/templates/core/hooks/doc-format-guard.sh +122 -40
- package/templates/core/hooks/lib/common.sh +46 -87
- package/templates/core/hooks/lib/context-inject.sh +2 -2
- package/templates/core/hooks/lib/notify.sh +5 -3
- package/templates/core/hooks/lib/preload-templates.sh +27 -2
- package/templates/core/hooks/lib/state-detect.sh +8 -115
- package/templates/core/hooks/lib/vocabulary.sh +13 -7
- package/templates/core/hooks/pre-tool-use.sh +14 -8
- package/templates/core/hooks/stop-check.sh +9 -0
- package/templates/core/prisma/add.prisma +140 -82
- package/templates/core/rules/project_rules.md +27 -24
- package/templates/core/scripts/db-ensure.sh +8 -0
- package/templates/core/scripts/gen-plan-index.sh +4 -15
- package/templates/core/scripts/mcp-server/notifications/hitl.ts +9 -1
- package/templates/core/scripts/mcp-server/notifications/hook.ts +25 -9
- package/templates/core/scripts/mcp-server/resources/hitl-approval-widget.ts +34 -0
- package/templates/core/scripts/mcp-server/resources/hook-events-report.ts +14 -2
- package/templates/core/scripts/mcp-server/resources/index.ts +2 -0
- package/templates/core/scripts/mcp-server/sampling/review.ts +25 -10
- package/templates/core/scripts/mcp-server/shared/db-types.ts +33 -8
- package/templates/core/scripts/mcp-server/shared/dps-scoring.strategy.ts +1 -1
- package/templates/core/scripts/mcp-server/shared/env.ts +28 -8
- package/templates/core/scripts/mcp-server/shared/fs.ts +3 -0
- package/templates/core/scripts/mcp-server/shared/hitl-interaction.strategy.ts +2 -3
- package/templates/core/scripts/mcp-server/shared/hitl-lifecycle-mutation.ts +129 -0
- package/templates/core/scripts/mcp-server/shared/hitl-proposal-mutation.ts +98 -0
- package/templates/core/scripts/mcp-server/shared/hitl-ui.ts +3 -0
- package/templates/core/scripts/mcp-server/shared/plan-lifecycle-events.ts +62 -0
- package/templates/core/scripts/mcp-server/shared/plan-lifecycle-mutation.ts +78 -0
- package/templates/core/scripts/mcp-server/shared/plan-lifecycle-subscriber.ts +145 -0
- package/templates/core/scripts/mcp-server/shared/plan-lifecycle.ts +153 -0
- package/templates/core/scripts/mcp-server/shared/plan-round-events.ts +56 -0
- package/templates/core/scripts/mcp-server/shared/plan-round-mutation.ts +143 -0
- package/templates/core/scripts/mcp-server/shared/plan-round-store.ts +47 -0
- package/templates/core/scripts/mcp-server/shared/plan-round-subscriber.ts +99 -0
- package/templates/core/scripts/mcp-server/shared/plan-status-store.ts +37 -0
- package/templates/core/scripts/mcp-server/shared/plan-tracking-mutation.ts +71 -0
- package/templates/core/scripts/mcp-server/shared/prisma.ts +12 -6
- package/templates/core/scripts/mcp-server/shared/project-root-strategy.ts +1 -2
- package/templates/core/scripts/mcp-server/shared/quant-recheck.strategy.ts +16 -0
- package/templates/core/scripts/mcp-server/shared/redact.ts +28 -0
- package/templates/core/scripts/mcp-server/shared/response.ts +3 -2
- package/templates/core/scripts/mcp-server/shared/runtime-context.ts +77 -0
- package/templates/core/scripts/mcp-server/shared/schema-topology.ts +159 -0
- package/templates/core/scripts/mcp-server/tools/audit.ts +49 -7
- package/templates/core/scripts/mcp-server/tools/context.ts +117 -121
- package/templates/core/scripts/mcp-server/tools/contract.ts +34 -9
- package/templates/core/scripts/mcp-server/tools/gateway/check_add_route_status.ts +50 -3
- package/templates/core/scripts/mcp-server/tools/gateway/check_doc_similarity.ts +218 -0
- package/templates/core/scripts/mcp-server/tools/gateway/check_dps.ts +22 -7
- package/templates/core/scripts/mcp-server/tools/gateway/check_rahs.ts +75 -4
- package/templates/core/scripts/mcp-server/tools/gateway/check_spec_sync.ts +19 -6
- package/templates/core/scripts/mcp-server/tools/gateway/index.ts +2 -0
- package/templates/core/scripts/mcp-server/tools/hitl.ts +160 -75
- package/templates/core/scripts/mcp-server/tools/hook-event-report.ts +9 -2
- package/templates/core/scripts/mcp-server/tools/index.ts +69 -3
- package/templates/core/scripts/mcp-server/tools/plan.ts +140 -15
- package/templates/core/scripts/mcp-server/tools/review.ts +44 -15
- package/templates/core/scripts/mcp-server.ts +60 -1
- package/templates/core/scripts/plan-status-bridge.ts +26 -0
- package/templates/core/skills/add-paradigm/SKILL.md +2 -2
- package/templates/core/templates/add-route-template.schema.json +24 -56
- package/templates/core/templates/collab-contract-template.md +1 -1
- package/templates/core/templates/handoff-multi-round-template.schema.json +48 -0
- package/templates/core/templates/handoff-single-round-template.schema.json +92 -0
- package/templates/core/templates/handoff-single-round.schema.json +1 -1
- package/templates/core/templates/hitl-approval-widget.html +222 -37
- package/templates/core/templates/spec-template.schema.json +9 -46
- package/templates/core/templates/standard-plan-template.schema.json +1 -1
- package/templates/core/templates/tasks-template.schema.json +13 -54
|
@@ -40,7 +40,7 @@ file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
|
|
|
40
40
|
# L17: 非文件工具事件(空 stdin)→ 不拦截(由 matcher 层过滤)
|
|
41
41
|
[ -z "$file_path" ] && exit 0
|
|
42
42
|
|
|
43
|
-
if ! echo "$file_path" | grep -qE
|
|
43
|
+
if ! echo "$file_path" | grep -qE "$MAGIC_DIR/(plans|specs)/"; then
|
|
44
44
|
exit 0
|
|
45
45
|
fi
|
|
46
46
|
|
|
@@ -63,45 +63,54 @@ fi
|
|
|
63
63
|
PROJECT_DIR="${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}"
|
|
64
64
|
TEMPLATES_DIR="$PROJECT_DIR/$MAGIC_DIR/templates"
|
|
65
65
|
|
|
66
|
-
#
|
|
66
|
+
# 文件名类型 token 匹配(特异性排序,取代子串 grep;R5/Task 1.4)
|
|
67
|
+
# review/handoff 不在此层绑定(需内容特征细分单多轮/运行时),留给下方探测链
|
|
67
68
|
TEMPLATE_NAME=""
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
87
|
+
if grep -q "## PLAN 元信息" <<< "$CONTENT"; then
|
|
79
88
|
TEMPLATE_NAME="standard-plan-template.md"
|
|
80
|
-
elif
|
|
89
|
+
elif grep -q "## 一、Plan 概述" <<< "$CONTENT"; then
|
|
81
90
|
TEMPLATE_NAME="simple-plan-template.md"
|
|
82
|
-
elif
|
|
91
|
+
elif grep -q "## 四、Handoff" <<< "$CONTENT"; then
|
|
83
92
|
TEMPLATE_NAME="simple-plan-template.md"
|
|
84
|
-
elif
|
|
85
|
-
if
|
|
93
|
+
elif grep -q "## Review 元信息" <<< "$CONTENT"; then
|
|
94
|
+
if grep -q "运行时验证" <<< "$CONTENT"; then
|
|
86
95
|
TEMPLATE_NAME="review-runtime-template.md"
|
|
87
|
-
elif
|
|
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
|
|
101
|
+
elif grep -q "## Why" <<< "$CONTENT"; then
|
|
93
102
|
TEMPLATE_NAME="spec-template.md"
|
|
94
|
-
elif
|
|
103
|
+
elif grep -q "## Preconditions" <<< "$CONTENT"; then
|
|
95
104
|
TEMPLATE_NAME="tasks-template.md"
|
|
96
|
-
elif
|
|
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
|
|
111
|
+
if grep -qF "## 全局元信息" <<< "$CONTENT"; then
|
|
103
112
|
TEMPLATE_NAME="handoff-multi-round-template.md"
|
|
104
|
-
elif
|
|
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,18 +119,19 @@ 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" ;;
|
|
119
128
|
*report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
|
|
120
129
|
*report*) TEMPLATE_NAME="report-template.md" ;;
|
|
121
130
|
*fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
|
|
131
|
+
*hitl*) TEMPLATE_NAME="hitl-template.md" ;;
|
|
122
132
|
*)
|
|
123
133
|
# 增量修订识别:包含 ~~删除线~~ / → 新增标记 / [修订日期] 任意一个 → 视为增量更新,放行
|
|
124
|
-
if
|
|
134
|
+
if grep -qE '~~.+~~|→|\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s+修订' <<< "$CONTENT"; then
|
|
125
135
|
echo "[doc-format-guard] 检测到增量修订格式,跳过完整章节校验" >&2
|
|
126
136
|
exit 0
|
|
127
137
|
fi
|
|
@@ -153,17 +163,54 @@ ISSUES=""
|
|
|
153
163
|
TMPFILE="$PROJECT_DIR/$MAGIC_DIR/.doc-guard-issues.tmp"
|
|
154
164
|
: > "$TMPFILE"
|
|
155
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
|
+
tokens=$(echo "$ref_line" | tr '#*`|(){' ' ' | tr -s ' ' '\n' | grep -v '^$' | grep -vF '{' | sort -u)
|
|
179
|
+
[ -z "$tokens" ] && continue
|
|
180
|
+
scope="$CONTENT"
|
|
181
|
+
if [ -n "$within" ]; then
|
|
182
|
+
if grep -q "^${within}" <<< "$CONTENT"; then
|
|
183
|
+
scope=$(sed -n "/^${within}/,/^## /p" <<< "$CONTENT")
|
|
184
|
+
else
|
|
185
|
+
echo "[doc-format-guard] within_miss: schema ${aid} 的 within '${within}' 在文档中未定位,跳过该规则" >&2
|
|
186
|
+
continue
|
|
187
|
+
fi
|
|
188
|
+
fi
|
|
189
|
+
# 声明级缺失统计:一个锚点声明的必选 token 有任一缺失即记 1 条规则缺失(struct_score 精确语义)
|
|
190
|
+
miss_tokens=""
|
|
191
|
+
for tok in $tokens; do
|
|
192
|
+
if ! grep -qF "$tok" <<< "$scope"; then
|
|
193
|
+
miss_tokens="${miss_tokens}${tok} "
|
|
194
|
+
fi
|
|
195
|
+
done
|
|
196
|
+
# if 语句(非 && 链):miss_tokens 为空时返回 0,避免 while 管道退出码非零触发 set -e
|
|
197
|
+
if [ -n "$miss_tokens" ]; then
|
|
198
|
+
echo " 缺锚点(${aid}): ${miss_tokens}"
|
|
199
|
+
fi
|
|
200
|
+
done >> "$TMPFILE"
|
|
201
|
+
fi
|
|
202
|
+
|
|
156
203
|
# SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
|
|
157
204
|
if [ "$IS_SEARCH_REPLACE" != "true" ]; then
|
|
158
|
-
jq -r '.sections[] | select(.required == true) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
|
|
159
|
-
if !
|
|
205
|
+
jq -r '.sections[] | select(.required == true) | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
|
|
206
|
+
if ! grep -qF "$heading" <<< "$CONTENT"; then
|
|
160
207
|
echo " 缺章节: $heading"
|
|
161
208
|
fi
|
|
162
|
-
done
|
|
209
|
+
done >> "$TMPFILE"
|
|
163
210
|
|
|
164
211
|
# 子章节
|
|
165
|
-
jq -r '.sections[].subsections[]
|
|
166
|
-
if !
|
|
212
|
+
jq -r '.sections[].subsections[]? | select(.heading != null) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
|
|
213
|
+
if ! grep -qF "$sub" <<< "$CONTENT"; then
|
|
167
214
|
echo " 缺子章节: $sub"
|
|
168
215
|
fi
|
|
169
216
|
done >> "$TMPFILE"
|
|
@@ -171,15 +218,22 @@ fi
|
|
|
171
218
|
|
|
172
219
|
# ── 占位符校验 ──
|
|
173
220
|
jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
|
|
174
|
-
if
|
|
221
|
+
if grep -qF "$ph" <<< "$CONTENT"; then
|
|
175
222
|
echo " 未替换占位符: $ph"
|
|
176
223
|
fi
|
|
177
224
|
done >> "$TMPFILE"
|
|
178
225
|
|
|
179
|
-
# ──
|
|
226
|
+
# ── 结构位禁词校验(仅标题行 + schema 声明的 groupColumn;正文叙述免疫;Task 1.2)──
|
|
227
|
+
struct_text=$(grep -E '^#{2,}[[:space:]]' <<< "$CONTENT")
|
|
228
|
+
col=$(jq -r '.groupColumn // empty' "$SCHEMA_FILE" 2>/dev/null)
|
|
229
|
+
if [ -n "$col" ]; then
|
|
230
|
+
struct_text="${struct_text}
|
|
231
|
+
$(awk -F'|' -v c="$col" 'NF>c {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $(c+1)); print $(c+1)}' <<< "$CONTENT")"
|
|
232
|
+
fi
|
|
180
233
|
jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
|
|
181
|
-
|
|
182
|
-
|
|
234
|
+
# 结构位(标题行/分组列)是原子单元:定串匹配即可命中;正文不参与(豁免由提取范围保证)
|
|
235
|
+
if grep -qF "$term" <<< "$struct_text"; then
|
|
236
|
+
echo " 结构位禁词: $term"
|
|
183
237
|
fi
|
|
184
238
|
done >> "$TMPFILE"
|
|
185
239
|
|
|
@@ -191,29 +245,29 @@ ALGO_ISSUES=""
|
|
|
191
245
|
# 规则1: 精简版 Plan 反作弊
|
|
192
246
|
if echo "$TEMPLATE_NAME" | grep -q 'simple-plan'; then
|
|
193
247
|
# 1a. 文件数 ≤ 3
|
|
194
|
-
file_count=$(
|
|
248
|
+
file_count=$(grep -cP '^\|\s*`[^`]+`' <<< "$CONTENT" 2>/dev/null || echo "0")
|
|
195
249
|
if [ "$file_count" -gt 3 ] 2>/dev/null; then
|
|
196
250
|
ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 涉及 ${file_count} 个文件(超过 3 个限制),应改用 standard-plan-template.md\n"
|
|
197
251
|
fi
|
|
198
252
|
# 1b. HITL 表不能写 "等 N 个文件"
|
|
199
|
-
if
|
|
253
|
+
if grep -qP '等\s*\d*\s*个文件|等\s*若干' <<< "$CONTENT"; then
|
|
200
254
|
ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表文件清单使用模糊描述('等 N 个文件'),必须列出实际完整路径\n"
|
|
201
255
|
fi
|
|
202
256
|
# 1c. HITL 方案/设计决策不能写 "等若干决策"
|
|
203
|
-
if
|
|
257
|
+
if grep -qP '等\s*若干\s*(决策|方案|设计)' <<< "$CONTENT"; then
|
|
204
258
|
ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: HITL 表方案/设计决策使用模糊描述('等若干决策'),必须逐条列出\n"
|
|
205
259
|
fi
|
|
206
260
|
# 1d. 不能包含架构设计章节(精简版无架构设计)
|
|
207
|
-
if
|
|
261
|
+
if grep -q '## 三、架构设计' <<< "$CONTENT"; then
|
|
208
262
|
ALGO_ISSUES="${ALGO_ISSUES} ❌ 精简版反作弊: 包含 '## 三、架构设计' 章节,精简版不应有架构设计——应改用 standard-plan-template.md\n"
|
|
209
263
|
fi
|
|
210
264
|
fi
|
|
211
265
|
|
|
212
266
|
# 规则2: HITL 表非空校验(所有 Plan + Review 模板)
|
|
213
267
|
if echo "$TEMPLATE_NAME" | grep -qE 'plan|review'; then
|
|
214
|
-
if
|
|
268
|
+
if grep -q '## HITL' <<< "$CONTENT"; then
|
|
215
269
|
# HITL 表至少要有 1 行非占位符内容(不包含 { } 占位符)
|
|
216
|
-
hitl_rows=$(
|
|
270
|
+
hitl_rows=$(sed -n '/## HITL/,/^## /p' <<< "$CONTENT" | grep -cP '^\|\s*[^|{]*\s*\|' 2>/dev/null || echo "0")
|
|
217
271
|
# rows 包含表头行和分隔行,实际数据行 = rows - 2
|
|
218
272
|
hitl_data=$((hitl_rows - 2))
|
|
219
273
|
if [ "$hitl_data" -lt 1 ] 2>/dev/null; then
|
|
@@ -241,20 +295,47 @@ fi
|
|
|
241
295
|
|
|
242
296
|
ISSUES=$(cat "$TMPFILE" 2>/dev/null)
|
|
243
297
|
|
|
298
|
+
# ── 判定回流 extra(Task 1.5 数据契约:anchor_hit/struct_score 精确语义;override 由上层 hook 在人类推翻时追加)──
|
|
299
|
+
# struct_score = (适用规则数 - 缺失规则数) / 适用规则数 × 100,适用规则从 schema 实况计数
|
|
300
|
+
anchor_total=$(jq -r '[.sections[] | select(.anchor != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
|
|
301
|
+
heading_total=$(jq -r '[.sections[] | select(.required == true and .heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
|
|
302
|
+
sub_total=$(jq -r '[.sections[].subsections[]? | select(.heading != null)] | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
|
|
303
|
+
term_total=$(jq -r '.forbidden_terms | length' "$SCHEMA_FILE" 2>/dev/null || echo 0)
|
|
304
|
+
if [ "$IS_SEARCH_REPLACE" = "true" ]; then
|
|
305
|
+
applied=$term_total # patch 路径仅禁词规则适用
|
|
306
|
+
else
|
|
307
|
+
applied=$(( anchor_total + heading_total + sub_total + term_total ))
|
|
308
|
+
fi
|
|
309
|
+
missed=$(wc -l < "$TMPFILE" 2>/dev/null | tr -d ' ')
|
|
310
|
+
missed=${missed:-0}
|
|
311
|
+
anchor_hit=true
|
|
312
|
+
grep -q "缺锚点" "$TMPFILE" 2>/dev/null && anchor_hit=false
|
|
313
|
+
struct_score=100
|
|
314
|
+
if [ "$applied" -gt 0 ] 2>/dev/null && [ "$missed" -gt 0 ] 2>/dev/null; then
|
|
315
|
+
struct_score=$(( (applied - missed) * 100 / applied ))
|
|
316
|
+
[ "$struct_score" -lt 0 ] && struct_score=0
|
|
317
|
+
fi
|
|
318
|
+
BACKFLOW_EXTRA="\"anchor_hit\":${anchor_hit},\"struct_score\":${struct_score}"
|
|
319
|
+
|
|
244
320
|
# ── 阻断或放行 ──
|
|
245
321
|
if [ -n "$ISSUES" ]; then
|
|
246
322
|
echo "⛔ $TEMPLATE_NAME 校验不通过:
|
|
247
323
|
$ISSUES" >&2
|
|
248
|
-
|
|
249
|
-
|
|
324
|
+
# 明细注入 stdout reason(去引号防 JSON 破坏,截断 180 字),让被拦方第一时间知道改什么
|
|
325
|
+
ISSUES_BRIEF=$(printf '%s' "$ISSUES" | tr '\n' ' ' | tr -d '"' | tr -s ' ' | cut -c1-180)
|
|
326
|
+
echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过: ${ISSUES_BRIEF}\"}}"
|
|
327
|
+
write_hook_event "doc-format-guard" "deny" "$file_path" "文档格式校验不通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
|
|
250
328
|
exit 2
|
|
251
329
|
fi
|
|
252
330
|
|
|
253
331
|
# ── 自动更新 index.md ──
|
|
254
|
-
if echo "$file_path" | grep -q
|
|
332
|
+
if echo "$file_path" | grep -q "$MAGIC_DIR/plans/"; then
|
|
255
333
|
if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
|
|
256
334
|
"$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
|
|
257
335
|
fi
|
|
258
336
|
fi
|
|
259
337
|
|
|
338
|
+
# 放行回流(Task 1.5:anchor_hit/struct_score 正样本底座)
|
|
339
|
+
write_hook_event "doc-format-guard" "allow" "$file_path" "校验通过" "$PLAN_KEYWORD" "$PLAN_STATUS" "$BACKFLOW_EXTRA" 2>/dev/null || true
|
|
340
|
+
|
|
260
341
|
exit 0
|
|
@@ -34,99 +34,61 @@ json_get() {
|
|
|
34
34
|
|
|
35
35
|
# ── ADD 活跃 Plan 检测(裁决定逻辑,SKILL §0.7.1) ──
|
|
36
36
|
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
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
|
+
# Qoder stdout 规范: 本文件内所有 echo 均被调用方命令替换捕获($(...),不直接
|
|
47
|
+
# 暴露给 IDE);hook 入口脚本不得将非 JSON 输出到 stdout。
|
|
43
48
|
#
|
|
44
49
|
# 前置条件: 调用方需设置 PROJECT_DIR 环境变量(项目根目录)
|
|
45
|
-
# 返回: "plan_keyword::step_n/total::round_n/total::handoff_path::add_route_path"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
# 返回: "plan_keyword::step_n/total::round_n/total::handoff_path::add_route_path"
|
|
51
|
+
# 或 "__STATUS_UNAVAILABLE__::reason::database::none::none"(调用方必须 fail-closed)
|
|
52
|
+
|
|
53
|
+
query_plan_status() {
|
|
54
|
+
local bridge=""
|
|
55
|
+
if [ -n "${MAGIC_DIR:-}" ]; then
|
|
56
|
+
bridge="${PROJECT_DIR:-$PWD}/${MAGIC_DIR}/scripts/plan-status-bridge.ts"
|
|
57
|
+
elif [ -n "${HOOK_DIR:-}" ]; then
|
|
58
|
+
# 物理位置反推: <magicDir>/hooks/lib/common.sh → <magicDir>
|
|
59
|
+
bridge="${PROJECT_DIR:-$PWD}/$(basename "$(dirname "$(dirname "$HOOK_DIR")")")/scripts/plan-status-bridge.ts"
|
|
54
60
|
else
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
[ -d "${PROJECT_DIR:-$PWD}/$m" ] && { magic_dirs+=("$m"); break; }
|
|
58
|
-
done
|
|
61
|
+
echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"magicDir 未注入且无法从物理位置推导"}'
|
|
62
|
+
return 3
|
|
59
63
|
fi
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
|
64
|
+
if [ ! -f "$bridge" ]; then
|
|
65
|
+
echo '{"availability":"STATUS_UNAVAILABLE","source":"database","reason":"plan-status bridge missing"}'
|
|
66
|
+
return 3
|
|
87
67
|
fi
|
|
68
|
+
node --import tsx "$bridge"
|
|
69
|
+
}
|
|
88
70
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
[
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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)
|
|
104
|
-
|
|
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"
|
|
71
|
+
# 兼容既有 Hook 字段协议:
|
|
72
|
+
# "planName::done/total::approvalStatus::none::none";后两个路径不参与 active 判定。
|
|
73
|
+
# DB 不可用/不可判定时返回特殊首字段,调用方必须 fail closed,不能当作“无 Plan”。
|
|
74
|
+
detect_active_add() {
|
|
75
|
+
local snapshot="" rc=0
|
|
76
|
+
snapshot="$(query_plan_status 2>/dev/null)" || rc=$?
|
|
77
|
+
if [ "$rc" -ne 0 ]; then
|
|
78
|
+
local reason
|
|
79
|
+
reason=$(printf '%s' "$snapshot" | jq -r '.reason // "database status unavailable"' 2>/dev/null || echo "database status unavailable")
|
|
80
|
+
echo "__STATUS_UNAVAILABLE__::${reason}::database::none::none"
|
|
81
|
+
return 0
|
|
117
82
|
fi
|
|
118
|
-
|
|
119
|
-
|
|
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"
|
|
83
|
+
if ! printf '%s' "$snapshot" | jq -e '.availability == "READY" and .isActive == true' >/dev/null 2>&1; then
|
|
84
|
+
return 1
|
|
127
85
|
fi
|
|
128
|
-
|
|
129
|
-
|
|
86
|
+
local plan done total approval
|
|
87
|
+
plan=$(printf '%s' "$snapshot" | jq -r '.planName')
|
|
88
|
+
done=$(printf '%s' "$snapshot" | jq -r '.progress.doneTasks // 0')
|
|
89
|
+
total=$(printf '%s' "$snapshot" | jq -r '.progress.totalTasks // 0')
|
|
90
|
+
approval=$(printf '%s' "$snapshot" | jq -r '.approvalStatus // "none"')
|
|
91
|
+
echo "${plan}::${done}/${total}::${approval}::none::none"
|
|
130
92
|
}
|
|
131
93
|
|
|
132
94
|
# ── Dev Action 追踪 ──
|
|
@@ -223,8 +185,8 @@ Plan 不是"文档开销"——它是代码治理的基础设施。跳过 Plan
|
|
|
223
185
|
方案 B — 补不上则回滚:
|
|
224
186
|
如果改动太复杂无法追溯生成 Plan,则:
|
|
225
187
|
① git diff 确认改动范围
|
|
226
|
-
②
|
|
227
|
-
③
|
|
188
|
+
② 仅对已确认属于本轮的文件生成反向 apply_patch
|
|
189
|
+
③ 无法安全确认改动所有权时停止,并请求用户决定
|
|
228
190
|
|
|
229
191
|
无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
|
|
230
192
|
EOF
|
|
@@ -45,8 +45,8 @@ Plan 不是"文档开销"——它是代码治理的基础设斻。跳过 Plan
|
|
|
45
45
|
方案 B — 补不上则回滚:
|
|
46
46
|
如果改动太复杂无法追溯生成 Plan,则:
|
|
47
47
|
① git diff 确认改动范围
|
|
48
|
-
②
|
|
49
|
-
③
|
|
48
|
+
② 仅对已确认属于本轮的文件生成反向 apply_patch
|
|
49
|
+
③ 无法安全确认改动所有权时停止,并请求用户决定
|
|
50
50
|
|
|
51
51
|
无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
|
|
52
52
|
EOF
|
|
@@ -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
|
|
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
|
-
|
|
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"
|
|
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}
|
|
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
|