add-coder 0.1.13 → 0.1.15

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.en.md ADDED
@@ -0,0 +1,218 @@
1
+ # add-coder
2
+
3
+ > 🌐 [🀄中文](./README.md) | English
4
+
5
+ **AI Governance, Implemented** — The complete scaffolding from [codein2027](https://github.com/xiaomingming92/codein2027) for rapidly building the ADD programming paradigm. Built on the core principle of **Audit as Infrastructure**, it shatters the black-box programming process and cross-session amnesia, evolving the programming paradigm into an auditable, traceable, and convergent new era. [NPM](https://www.npmjs.com/package/add-coder) · [GitHub](https://github.com/xiaomingming92/add-coder)
6
+
7
+
8
+ > 🧭 **Getting hands-on?** See [GUIDE.md](https://github.com/xiaomingming92/add-coder/blob/main/GUIDE.md) — trigger word quick reference, requirements-to-Plan, and full workflow walkthrough.
9
+
10
+ ```bash
11
+ npx add-coder init
12
+ ```
13
+
14
+ ---
15
+
16
+ ## Not a Template Tool — An Architectural Difference
17
+
18
+ There are already plenty of AI coding templates, hook adapters, and MCP scaffolds. What fundamentally sets add-coder apart is not "what files it generates," but a **paradigm shift at the architectural level**:
19
+
20
+ ### ① Audit Is Infrastructure, Not After-the-Fact Logging
21
+
22
+ Traditional AI development: Chat → Generate code → Dig through chat history afterward to find "who changed what"
23
+
24
+ | Traditional Model | add-coder |
25
+ |-------------------|-----------|
26
+ | Logs are append-only text files | Audit is a **structured data table** (DevOperation + AuditLog), supporting multi-dimensional queries by plan/step/agent/tool |
27
+ | Auditing relies on developer discipline | The **MCP audit toolchain** automatically records every operation; system gateways enforce checks |
28
+ | No traceability | Audit events are naturally linked: Plan → Spec → Task → Step → Tool Call, forming a complete evidence chain |
29
+
30
+ ### ② Gateway-Driven, Not Free-Form Conversation
31
+
32
+ Traditional AI coding is "you say, I do" — quality depends entirely on the LLM's state that day. add-coder embeds **dual quality gateways** into the architecture:
33
+
34
+ ```
35
+ DPS (Design-Process Symmetry) — Design / Implementation / Docs / Audit, each weighted 25%, < 85% BLOCKED
36
+ RAHS (Runtime Architecture Health Score) — Runtime architecture health, < 90% BLOCKED
37
+ ```
38
+
39
+ These are not "suggestions" — they are **architectural blocks**. A Step cannot advance without passing its gateway.
40
+
41
+ ### ③ Cross-Session Memory, Not Per-Session Amnesia
42
+
43
+ The fatal flaw of AI conversations: architectural decisions from last session, bugs fixed, agreements reached — all forgotten in the next conversation. add-coder solves this at the architecture level:
44
+
45
+ - **Handoff Documents** — Automatically generated structured handoff at the end of each session, auto-loaded by the next session
46
+ - **Plan Index** — All Plans are centrally indexed via `index.md`, supporting fuzzy-match quick lookup
47
+ - **DevLog Timeline** — Every operation is written to the `{YYYY-MM}/{DD}/` timeline, enabling full historical state traceability
48
+
49
+ ### ④ Policy-Update-Loop: Self-Evolving Governance (the scaffold itself does not include this architectural capability; a DEMO repo will be provided next to better illustrate the Policy-Update-Loop and Report system)
50
+
51
+ Not a static template, but a **closed-loop adaptive system**:
52
+
53
+ ```
54
+ Execute → Audit → Boundary Report → Rule Adjustment → Next Execution
55
+ ```
56
+
57
+ Runtime-generated Reports feed back into governance rules, enabling continuous evolution of governance strategies.
58
+
59
+ ### ⑤ Multi-IDE Hooks as the Governance Layer
60
+
61
+ Hooks are not "notification push" — they are the **IDE runtime interception layer**:
62
+
63
+ | Hook Type | Function |
64
+ |-----------|----------|
65
+ | PreToolUse | Validates whitelist before tool invocation, injects context, DPS condition checks |
66
+ | PostToolUse | Automatic audit logging, Plan sync detection, format guarding |
67
+ | PreCompact | Forces retention of critical document paths during cross-session context compression |
68
+ | PromptSubmit | Injects ADD vocabulary triggers, ensuring zero-latency LLM response to commands like "acceptance" and "gateway" |
69
+
70
+ Each IDE (Claude/Qoder/VS Code) has its own hook implementation, but the **governance logic is unified** — the architecture is consistent, only the adapter layer differs.
71
+
72
+ ---
73
+
74
+ ## Quick Start
75
+
76
+ ```bash
77
+ npx add-coder init
78
+ ```
79
+
80
+ The first `init` auto-detects your IDE and interactively guides you through database selection (PostgreSQL / SQLite / self-managed), container runtime (podman / docker / self-managed), Prisma initialization, and ADD template deployment.
81
+
82
+ ```bash
83
+ npx add-coder init
84
+ # → Choose IDE (Qoder / Claude / VS Code)
85
+ # → Choose database (PostgreSQL / SQLite / self-managed)
86
+ # → Choose container (podman / docker / self-managed)
87
+ # → prisma init + add.prisma copied
88
+ # → prisma db push (adds new tables only, no data deletion)
89
+ # → prisma generate
90
+ # → ADD governance model ready ✓
91
+ ```
92
+
93
+ > **Env file priority**: `.env.development.local` > `.env.development` > `.env.local` > `.env`
94
+
95
+ ## Commands
96
+
97
+ | Command | Description |
98
+ |---------|-------------|
99
+ | `init` | Initialize ADD templates, supports `--adapter claude\|qoder\|vscode\|auto` |
100
+ | `sync` | Incrementally sync missing files |
101
+ | `status` | Check template integrity |
102
+
103
+ ### init Internal Flow
104
+
105
+ | Step | Action | Description |
106
+ |------|--------|-------------|
107
+ | ① | Detect IDE | Scan for `.qoder/` `.claude/` `.vscode/` existence, or specify via `--adapter` |
108
+ | ② | Load config | Interactive Q&A > `add-coder.config.ts` > auto-detect > defaults |
109
+ | ③ | DB deployment | `db-ensure.sh` starts container/PG connection + `injectPrisma()` Caijue layer (Prisma init → AddUser model copy → db push → generate) |
110
+ | ④ | Render templates | 55 core template files (skills/agents/templates/plans/specs/scripts…) |
111
+ | ⑤ | Deploy adapters | Copy core content to `.add/` `.qoder/` `.claude/` directories, supplement IDE-specific hooks/mcp |
112
+ | ⑥ | Write files | Four modes: interactive / yes / force / dry-run; `.sh` scripts auto `chmod` |
113
+ | ⑦ | Output summary | Created / skipped / overwritten stats + next-step hints |
114
+
115
+ ### init Options
116
+
117
+ | Option | Description |
118
+ |--------|-------------|
119
+ | `--adapter <type>` | Target IDE: claude / qoder / vscode / auto (default) |
120
+ | `--config <path>` | Specify config file |
121
+ | `--yes` | Skip interactions, create new files only |
122
+ | `--force` | Overwrite existing files |
123
+ | `--dry-run` | Preview mode, no writes |
124
+
125
+ ## Generated Content
126
+
127
+ | Directory | Content |
128
+ |-----------|---------|
129
+ | `.add/` | ADD shared core (skills, agents, docs, scripts, rules, etc.) |
130
+ | `.claude/` | Claude Code adapter (hooks, settings.json) |
131
+ | `.qoder/` | Qoder adapter (hooks, settings.json, mcp.json) |
132
+ | `.vscode/` | VS Code adapter (settings.json, tasks.json) |
133
+
134
+ ## MCP Audit Toolchain
135
+
136
+ `init` automatically deploys the MCP server (`mcp-server.ts`) into the project, loaded by the IDE via `mcp.json`. The following audit and governance tools are provided:
137
+
138
+ | Tool | Purpose | Trigger Scenario |
139
+ |------|---------|-----------------|
140
+ | `record_dev_operation` | Record development operation audits (before/after/reason) | Every file change, config modification |
141
+ | `query_audit_logs` | Query audit records by planKeyword / targetId | Cross-session context recovery, iteration evidence verification |
142
+ | `get_project_context` | Get ADD workflow status snapshot | Fresh conversation start |
143
+ | `get_db_schema` | Get Prisma schema info | Database-related operations |
144
+ | `check_dps` | DPS gateway (Design/Implementation/Docs/Audit, each 25%) | End of Step 0 |
145
+ | `check_rahs` | RAHS gateway (runtime architecture health) | Step 4/8 |
146
+ | `check_add_route_status` | add-route file existence check | Before Step 3 |
147
+ | `check_spec_sync` | Spec doc checkbox status vs. code consistency | After Spec execution |
148
+ | `find_related_docs` | Search related architecture/spec documents | Context understanding |
149
+
150
+ > Full tool list: [MCP Toolchain Specification](https://github.com/xiaomingming92/codein2027/blob/main/docs/大田精准耕播智能决策系统/knowledge/02-规范/%E3%80%8A%E5%BC%80%E5%8F%91%E6%93%8D%E4%BD%9C%E5%AE%A1%E8%AE%A1%E5%AD%98%E6%A1%A3%E8%A7%84%E8%8C%83%E3%80%8B.md).
151
+
152
+ ## Architecture Overview
153
+
154
+ ```
155
+ ┌─────────────┐
156
+ │ ADD Paradigm│
157
+ │ Step 0-9 │
158
+ └──────┬──────┘
159
+ │ Gateway-driven
160
+ ┌────────────┼────────────┐
161
+ ▼ ▼ ▼
162
+ ┌──────────┐ ┌──────────┐ ┌──────────┐
163
+ │DPS Gateway│ │RAHS Gate │ │Compliance│
164
+ └────┬─────┘ └────┬─────┘ └────┬─────┘
165
+ │ │ │
166
+ ▼ ▼ ▼
167
+ ┌─────────────────────────────────────┐
168
+ │ Audit Infrastructure Layer │
169
+ │ DevOperation / AuditLog Tables │
170
+ │ MCP Audit Toolchain │
171
+ │ Handoff / DevLog Timeline Docs │
172
+ └─────────────────────────────────────┘
173
+
174
+ ┌────────────┼────────────┐
175
+ ▼ ▼ ▼
176
+ ┌──────────┐ ┌──────────┐ ┌──────────┐
177
+ │ Claude │ │ Qoder │ │ VS Code │
178
+ │ Hooks │ │ Hooks │ │ Config │
179
+ └──────────┘ └──────────┘ └──────────┘
180
+
181
+ ┌────────────┘
182
+
183
+ ┌─────────────────────────────────────┐
184
+ │ Caijuehub Rule Engine │
185
+ │ TOML-Driven Policy System │
186
+ │ Detect / Adapt / Prisma / Write — │
187
+ │ Fully Configurable │
188
+ └─────────────────────────────────────┘
189
+ ```
190
+
191
+ ## Prerequisites
192
+
193
+ - Node.js >= 20
194
+ - Prisma ^7.0 (auto-detected during `init`, guided installation if missing)
195
+ - PostgreSQL / SQLite (MCP toolchain depends on DevOperation + AuditLog tables)
196
+
197
+ > **Recommended**: Run PostgreSQL via Podman/Docker, reference config:
198
+ > ```yaml
199
+ > postgres:
200
+ > image: docker.io/postgres:16-alpine
201
+ > ports: ["127.0.0.1:5433:5432"]
202
+ > environment:
203
+ > POSTGRES_DB: mydb
204
+ > POSTGRES_USER: admin
205
+ > POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
206
+ > ```
207
+ > Mount data volume to `~/data/your_project/postgres/` to avoid data loss on container removal.
208
+
209
+ > 📦 [Changelog](./CHANGELOG.md)
210
+
211
+ ---
212
+
213
+ ## 🎬 Coming Soon
214
+
215
+ | Plan | Description |
216
+ |------|-------------|
217
+ | Demo Repo | A full example repository showcasing end-to-end closed-loop practice of Policy-Update-Loop and the Report system |
218
+ | MCP Restructure | MCP toolchain architecture upgrade, improving audit and gateway tool extensibility and standalone deployment capability |
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # add-coder
2
2
 
3
+ > 🌐 🀄中文 | [English](./README.en.md)
4
+
3
5
  **AI 代码治理的落地方案** — [codein2027](https://github.com/xiaomingming92/codein2027) 快速构建 ADD 编程范式的完整脚手架。以「审计即基础设施」为核心,彻底打破编程过程黑盒与跨轮失忆,让编程范式进化为可审计、可追溯、可收敛的新时代。 [NPM](https://www.npmjs.com/package/add-coder) · [GitHub](https://github.com/xiaomingming92/add-coder)
4
6
 
7
+
5
8
  > 🧭 **从零上手实操?** 请参见 [GUIDE.md](https://github.com/xiaomingming92/add-coder/blob/main/GUIDE.md) — 包含触发词速查、需求转 Plan、完整链路演练。
6
9
 
7
10
  ```bash
@@ -43,7 +46,7 @@ AI 对话的致命缺陷:上次讨论的架构决策、已修复的 Bug、达
43
46
  - **Plan 索引** — 所有 Plan 通过 `index.md` 集中索引,支持模糊匹配快速定位
44
47
  - **DevLog 时序记录** — 每一步操作写入 `{YYYY-MM}/{DD}/` 时间轴,可回溯任意历史状态
45
48
 
46
- ### ④ Policy-Update-Loop:治理自我进化
49
+ ### ④ Policy-Update-Loop:治理自我进化(脚手架不包含此架构能力,接下来会给到DEMO仓库让大家更好理解Policy-Update-Loop和Report体系)
47
50
 
48
51
  不是静态模板,而是**闭环自适应系统**:
49
52
 
@@ -201,3 +204,14 @@ npx add-coder init
201
204
  > POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
202
205
  > ```
203
206
  > 数据卷建议挂载到 `~/data/your_project/postgres/`,避免容器销毁丢失数据。
207
+
208
+ > 📦 [更新日志 (CHANGELOG)](./CHANGELOG.md)
209
+
210
+ ---
211
+
212
+ ## 🎬 预告
213
+
214
+ | 计划 | 说明 |
215
+ |------|------|
216
+ | Demo 仓库演示 | 提供完整示例仓库,展示 Policy-Update-Loop 与 Report 体系的端到端闭环实践 |
217
+ | MCP 能力重构 | MCP 工具链架构升级,提升审计与门禁工具的可扩展性和独立部署能力 |
package/dist/index.js CHANGED
@@ -759,7 +759,7 @@ PROJECT_NAME=${config.projectName || "add-project"}
759
759
  }
760
760
  const coreFiles = renderCore(config, !!options.dryRun);
761
761
  console.log(`Core \u6A21\u677F: ${coreFiles.size} \u6587\u4EF6`);
762
- const CORE_TARGETS = [".add", ".qoder", ".claude", ".vscode"];
762
+ const CORE_TARGETS = [".add", magicDir];
763
763
  const allFiles = /* @__PURE__ */ new Map();
764
764
  for (const [relPath, content] of coreFiles) {
765
765
  for (const t of CORE_TARGETS) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "add-coder",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "private": false,
5
5
  "description": "[codein2027](https://github.com/xiaomingming92/codein2027) 快速构建 ADD 编程范式的完整脚手架——AI 代码治理的落地方案。以「审计即基础设施」为核心,彻底打破编程过程黑盒与跨轮失忆,让编程范式进化为可审计、可追溯、可收敛的新时代。npx 即用,人人可体验。",
6
6
  "type": "module",
@@ -0,0 +1,17 @@
1
+ #!/bin/bash
2
+ # doc-format-guard.sh — 文档格式守卫(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ input=$(parse_input)
10
+ file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
11
+ [ -z "$file_path" ] && exit 0
12
+
13
+ # 检查 ADD 文档格式
14
+ if echo "$file_path" | grep -qE '\.qoder/(specs|plans|reviews)/.*\.md$'; then
15
+ echo "[ADD DocFormatGuard] 检测到 ADD 文档变更,请确保格式符合模板规范。"
16
+ fi
17
+ exit 0
@@ -0,0 +1,96 @@
1
+ #!/bin/bash
2
+ # context-inject.sh — 上下文注入模板
3
+ # 共享库
4
+
5
+ # 结构化 SessionStart JSON 注入
6
+ build_session_start_json() {
7
+ local plan="$1" step="$2" round="$3" handoff="$4"
8
+ cat <<EOF
9
+ {
10
+ "continue": true,
11
+ "hookSpecificOutput": {
12
+ "hookEventName": "SessionStart",
13
+ "additionalContext": "上次 ADD 流程未完成:\n Plan: ${plan}\n 轮次: ${round}\n 当前 Step: ${step} (add-route)\n 恢复命令: query_audit_logs({ planKeyword: '${plan}' })\n handoff: ${handoff}"
14
+ }
15
+ }
16
+ EOF
17
+ }
18
+
19
+ # Stop 四象限分流 few-shot 上下文
20
+ build_stop_context() {
21
+ local quadrant="$1" info="$2"
22
+ case "$quadrant" in
23
+ "no_add_no_dev")
24
+ echo "[ADD Stop] 无活跃 Plan,无代码改动。正常结束。"
25
+ ;;
26
+ "no_add_has_dev")
27
+ cat <<'EOF'
28
+ [ADD Stop] ⚠️ 检测到代码修改但无活跃 ADD Plan。
29
+
30
+ Plan 不是"文档开销"——它是代码治理的基础设斻。跳过 Plan 的后果:
31
+ · 没有 add-route → 每次改动无法追溯到具体 Task
32
+ · 没有 tasks.md → 后续 AI Session 不知道改了哪些文件
33
+ · 没有 handoff → 交接时上下文全丢,只能靠 git log 猜
34
+
35
+ 你必须立即补救,二选一:
36
+
37
+ 方案 A — 补 ADD 流程(招安):
38
+ Step 0: 读 .qoder/templates/plan-template.md → 生成 Plan → .qoder/plans/{today}/{keyword}-plan-v1.md
39
+ 生成 add-route → check_dps ≥ 85
40
+ Step 1: 扩展 AgentAuditPhase(如需要)
41
+ Step 2: 确认 agentAudit() 通道
42
+ Step 3: 将已写代码关联到 tasks.md
43
+ 完成后可正常停止。
44
+
45
+ 方案 B — 补不上则回滚:
46
+ 如果改动太复杂无法追溯生成 Plan,则:
47
+ ① git diff 确认改动范围
48
+ ② git checkout -- <改动的文件> 撤回
49
+ ③ 告知用户 "以下改动已回滚,请先走 ADD 流程再重新实施: <文件列表>"
50
+
51
+ 无论选 A 还是 B,完成后告诉用户下次执行 session-init 恢复上下文。
52
+ EOF
53
+ ;;
54
+ "has_add_no_dev")
55
+ echo "[ADD Stop] ADD 流程进行中 (${info}),本次无代码改动。下次继续时执行 session-init 恢复上下文。"
56
+ ;;
57
+ "has_add_dev_step02")
58
+ echo "[ADD Stop] ADD Step 0-2: 文档先行/审计准备阶段。无需验收闭环。下一步: 进入 Step 3 代码实现。"
59
+ ;;
60
+ "has_add_dev_step3")
61
+ echo "[ADD Stop] ADD Step 3: 代码实现进行中 (${info})。完成后进入 Step 3.5 实现审查。"
62
+ ;;
63
+ "has_add_dev_unclosed")
64
+ cat <<'EOF'
65
+ [ADD Stop] ⚠️ 代码已完成但验收未闭环:
66
+ ${info}
67
+
68
+ 请依次执行(不要等下次会话):
69
+ ① Write devlog → handoff 同目录 devlog-{plan}-v{n}.md
70
+ 格式: # Devlog: {plan}\n 日期 / Plan / 轮次 / 本轮改了什么 / 验收结果 / 遗留项 / 架构回看
71
+ ② Edit handoff → 更新 §验证标准 全部 [x] + 补充审计 ID
72
+ ★ 同步: checklist 有新 cuid → handoff ADD-7 表必须对应新增行
73
+ ★ Step 0 准入: handoff + add-route + Specs 三元组缺一不可,缺则回退 Step 0.5
74
+ ③ Read docs/ → 回看架构文档确认一致性
75
+ ④ Edit add-route → 勾选对应 Step [x]
76
+
77
+ 以上全部完成后 Agent 才能停止。
78
+
79
+ 下次恢复: 读 handoff → 查同目录 devlog-*.md → query_audit_logs
80
+ EOF
81
+ ;;
82
+ "has_add_dev_closed")
83
+ echo "[ADD Stop] ✅ 验收闭环: add-route全部[x], devlog已记录, handoff已更新。验收幂等——重复触发不覆盖已有结论。"
84
+ ;;
85
+ esac
86
+ }
87
+
88
+ # 写操作前置守卫上下文
89
+ build_pretool_context() {
90
+ local plan="$1" round="$2"
91
+ cat <<EOF
92
+ [ADD PreToolUse] 当前 Plan: ${plan},轮次: ${round}。
93
+ 本次写入应属于 ADD Step 3 代码实现阶段。
94
+ 完成后执行 record_dev_operation 记录审计。
95
+ EOF
96
+ }
@@ -0,0 +1,104 @@
1
+ #!/bin/bash
2
+ # state-detect.sh — ADD 活跃流程检测 + dev action 追踪
3
+ # 共享库
4
+
5
+ PROJECT_DIR="${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}"
6
+ QODER_DIR="$PROJECT_DIR/.qoder"
7
+ PLANS_DIR="$QODER_DIR/plans"
8
+
9
+ # dev action 标记文件(项目级,PreToolUse 写入,Stop 读取)
10
+ DEV_FLAG="/tmp/qoder_dev_$(echo "$PROJECT_DIR" | md5sum 2>/dev/null | cut -c1-8 || echo "default")"
11
+
12
+ # 检测活跃 ADD 流程
13
+ # 返回: "plan_keyword::step_x/total::round_n/total_r::handoff_path::add_route_path" 或 ""
14
+ detect_active_add() {
15
+ local handoff=""
16
+ local today_dir="$PLANS_DIR/$(date +%Y-%m)"
17
+ [ -d "$today_dir" ] && handoff=$(find "$today_dir" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
18
+ [ -z "$handoff" ] && handoff=$(find "$PLANS_DIR" -name "*handoff*.md" -mtime -7 -type f 2>/dev/null | head -1)
19
+ [ -z "$handoff" ] && return 1
20
+
21
+ local plan_kw=$(basename "$handoff" | sed 's/-handoff.*//')
22
+
23
+ # add-route
24
+ local add_route=$(find "$PLANS_DIR" -name "*add-route*.md" -type f 2>/dev/null | head -1)
25
+
26
+ # Step 统计(从 add-route)
27
+ local step_info="?"
28
+ if [ -f "$add_route" ]; then
29
+ local ck=$(grep -c '\[x\]' "$add_route" 2>/dev/null || true)
30
+ local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || true)
31
+ ck=${ck:-0}; uc=${uc:-0}
32
+ local total=$((ck + uc))
33
+ [ "$total" -gt 0 ] && step_info="${ck}/${total}"
34
+ fi
35
+
36
+ # 轮次统计(从 handoff)
37
+ local round_info="?"
38
+ if grep -q '<第[0-9]轮>' "$handoff" 2>/dev/null; then
39
+ local ck=$(grep -c '\[x\]' "$handoff" 2>/dev/null || true); ck=${ck:-0}
40
+ local total=$(grep -c '<第[0-9]轮>' "$handoff" 2>/dev/null || true); total=${total:-0}
41
+ [ "$total" -gt 0 ] && round_info="${ck}/${total}"
42
+ else
43
+ round_info="1/1"
44
+ fi
45
+
46
+ echo "${plan_kw}::${step_info}::${round_info}::${handoff}::${add_route:-none}"
47
+ }
48
+
49
+ # 标记 dev action
50
+ mark_dev_action() {
51
+ touch "$DEV_FLAG" 2>/dev/null || true
52
+ }
53
+
54
+ # 检测 dev action
55
+ has_dev_action() {
56
+ [ -f "$DEV_FLAG" ]
57
+ }
58
+
59
+ # 清理 dev action 标记(验收闭环后调用)
60
+ clear_dev_action() {
61
+ rm -f "$DEV_FLAG" 2>/dev/null || true
62
+ }
63
+
64
+ # 检查验收完整度,返回 issues 文本
65
+ check_add_completeness() {
66
+ local handoff="$1" add_route="$2"
67
+ local issues=""
68
+
69
+ # devlog(内容已回流至 handoff,检查 handoff 是否含验收结果)
70
+ if [ -f "$handoff" ] && ! grep -qE '验收|收敛|闭环|本轮改了什么|devlog' "$handoff" 2>/dev/null; then
71
+ issues="${issues} [ ] devlog 缺失(handoff 无验收记录)\n"
72
+ fi
73
+
74
+ # handoff 验证标准
75
+ if [ -f "$handoff" ]; then
76
+ local uc=$(grep -c '\[ \]' "$handoff" 2>/dev/null || echo "0")
77
+ [ "$uc" -gt 0 ] && issues="${issues} [ ] handoff ${uc} 项未勾选\n"
78
+ fi
79
+
80
+ # add-route Step
81
+ if [ -f "$add_route" ]; then
82
+ local uc=$(grep -c '\[ \]' "$add_route" 2>/dev/null || echo "0")
83
+ [ "$uc" -gt 0 ] && issues="${issues} [ ] add-route ${uc} Step 未闭环\n"
84
+ fi
85
+
86
+ echo -e "$issues"
87
+ }
88
+
89
+ # 检查是否已验收(幂等保护)
90
+ # 返回 0 = 未验收,1 = 已验收
91
+ is_already_accepted() {
92
+ local add_route="$1" handoff="$2"
93
+ # add-route Step 8 是否已 [x]
94
+ if [ -f "$add_route" ]; then
95
+ # 检查 Step 8 产出项是否勾选
96
+ if grep -A 10 'Step 8' "$add_route" 2>/dev/null | grep -q '\[x\].*验证并更新项目状态'; then
97
+ # handoff 是否有验收记录
98
+ if [ -f "$handoff" ] && grep -qE '✅.*验收|收敛|全部闭环|全部.*完成' "$handoff" 2>/dev/null; then
99
+ return 0
100
+ fi
101
+ fi
102
+ fi
103
+ return 1
104
+ }
@@ -0,0 +1,49 @@
1
+ #!/bin/bash
2
+ # vocabulary.sh — 从 vocabulary markdown 表格加载触发词
3
+ # 单一数据源: .qoder/vocabulary/add-governance-vocabulary.md §类别 A-F 表格
4
+
5
+ VOCABULARY_FILE="$PWD/.qoder/vocabulary/add-governance-vocabulary.md"
6
+
7
+ # 输出格式: 优先级::触发词正则::响应文本(:: 避免与触发词内的 | 冲突)
8
+ load_triggers() {
9
+ [ ! -f "$VOCABULARY_FILE" ] && return 1
10
+ sed -n '/^## 类别 A: 文档类型/,/^## 类别 [G-Z]/p' "$VOCABULARY_FILE" \
11
+ | grep -E '^\| (P0|P1|P2) ' \
12
+ | while IFS='|' read -r _ prio raw_trigger action; do
13
+ prio=$(echo "$prio" | xargs)
14
+ trigger=$(echo "$raw_trigger" | sed 's/`//g;s/ *\/ */|/g;s/^ *//;s/ *$//')
15
+ action=$(echo "$action" | xargs)
16
+ [ -z "$trigger" ] && continue
17
+ echo "${prio}::${trigger}::${action}"
18
+ done || true
19
+ }
20
+
21
+ match_trigger() {
22
+ local prompt="$1"
23
+ while IFS= read -r line; do
24
+ [ -z "$line" ] && continue
25
+ local prio="${line%%::*}"
26
+ local rest="${line#*::}"
27
+ local regex="${rest%%::*}"
28
+ local action="${rest#*::}"
29
+ # 跳过开发关键词检测行(由 Layer 2/3 分流处理,特征:超长regex含"修bug")
30
+ if echo "$regex" | grep -qE '修\.\?bug|fix\.\?bug' 2>/dev/null; then
31
+ continue
32
+ fi
33
+ if echo "$prompt" | grep -qiE "$regex" 2>/dev/null; then
34
+ echo "[ADD 触发] ${regex} → ${action}"
35
+ fi
36
+ done < <(load_triggers 2>/dev/null)
37
+ }
38
+
39
+ load_dev_keywords() {
40
+ load_triggers 2>/dev/null | while IFS= read -r line; do
41
+ [ -z "$line" ] && continue
42
+ # 只取包含"开发|改功能"的开发关键词检测行
43
+ if echo "$line" | grep -q '开发|改功能' 2>/dev/null; then
44
+ local rest="${line#*::}"
45
+ local regex="${rest%%::*}"
46
+ echo "$regex"
47
+ fi
48
+ done || true
49
+ }
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+ # Notification — 通知事件处理(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ exit 0
@@ -0,0 +1,18 @@
1
+ #!/bin/bash
2
+ # PermissionRequest — 权限请求门禁(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ input=$(parse_input)
10
+ tool_name=$(echo "$input" | jq -r '.tool_name // empty')
11
+
12
+ # 高风险工具需要二次确认
13
+ case "$tool_name" in
14
+ Bash|Write|Edit)
15
+ echo "[ADD PermissionGate] 高风险工具: ${tool_name},请确认操作。"
16
+ ;;
17
+ esac
18
+ exit 0
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ # PostToolUseFailure — 工具失败后处理(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ echo "[ADD PostToolFailure] 工具调用失败,请检查错误信息并修复。"
10
+ exit 0
@@ -0,0 +1,18 @@
1
+ #!/bin/bash
2
+ # post-tool-use.sh — Claude Code PostToolUse 审计提醒
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ input=$(parse_input)
10
+ file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
11
+ [ -z "$file_path" ] && exit 0
12
+
13
+ if ! echo "$file_path" | grep -qE 'src/agents/|src/lib/agent-audit-logger\.ts|src/types/|prisma/schema\.prisma'; then
14
+ exit 0
15
+ fi
16
+
17
+ echo "[ADD PostToolUse] 文件已写入: ${file_path}。如果尚未记录,请执行 record_dev_operation 落库审计。"
18
+ exit 0
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+ # PreCompact — 上下文压缩前检查(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ # 检查是否有未完成的 ADD 审计记录
10
+ PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$PWD}"
11
+ [ -d "$PROJECT_DIR/.qoder/plans" ] || exit 0
12
+
13
+ echo "[ADD PreCompact] 上下文压缩前,请确认 ADD-7 审计记录已落库。"
14
+ exit 0
@@ -0,0 +1,28 @@
1
+ #!/bin/bash
2
+ # pre-tool-use.sh — Claude Code PreToolUse:源码 Plan 关联检查
3
+ # 与 Qoder 版本共享核心逻辑,差异仅在工具名和环境变量
4
+ set -euo pipefail
5
+
6
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
7
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
8
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
9
+
10
+ input=$(parse_input)
11
+ file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
12
+ [ -z "$file_path" ] && exit 0
13
+
14
+ if ! echo "$file_path" | grep -qE '(src/|/src/).*\.(ts|tsx)$'; then
15
+ exit 0
16
+ fi
17
+
18
+ PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$PWD}"
19
+ MOD=$(basename "$file_path" | sed 's/\.[jt]sx\?$//')
20
+
21
+ MATCHES=$(grep -rl "$MOD" "$PROJECT_DIR/.qoder/plans" "$PROJECT_DIR/.qoder/specs" "$PROJECT_DIR/.qoder/reports" 2>/dev/null | wc -l)
22
+
23
+ if [ "$MATCHES" -gt 0 ]; then
24
+ exit 0
25
+ fi
26
+
27
+ echo "⛔ 阻断: ${file_path} 无相关 ADD Plan。请先创建 Plan。" >&2
28
+ exit $EXIT_BLOCK
@@ -0,0 +1,16 @@
1
+ #!/bin/bash
2
+ # UserPromptSubmit — 用户输入提交前检查(ADD 关键词兜底)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ input=$(parse_input)
10
+ prompt=$(echo "$input" | jq -r '.prompt // empty')
11
+
12
+ # 检测 ADD 关键词触发
13
+ if echo "$prompt" | grep -qiE '开发|改功能|修.?bug|加需求|新增|重构|实施|验收|继续'; then
14
+ echo "[ADD PromptSubmit] 检测到开发关键词,请确认已加载 ADD 工作流上下文。"
15
+ fi
16
+ exit 0
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ # review-checklist.sh — Review 检查清单校验(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ echo "[ADD ReviewChecklist] 请在提交前确认 checklist.md 全部项已勾选。"
10
+ exit 0
@@ -0,0 +1,23 @@
1
+ #!/bin/bash
2
+ # SessionStart — ADD 上下文恢复(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ # 尝试加载 Qoder 的 state-detect 和 context-inject 库
10
+ QODER_LIB="$HOOK_DIR/../../qoder/hooks/lib"
11
+ [ -f "$QODER_LIB/state-detect.sh" ] && source "$QODER_LIB/state-detect.sh"
12
+ [ -f "$QODER_LIB/context-inject.sh" ] && source "$QODER_LIB/context-inject.sh"
13
+
14
+ export QODER_PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$PWD}"
15
+
16
+ state=$(detect_active_add 2>/dev/null || true)
17
+ if [ -z "$state" ]; then
18
+ exit 0
19
+ fi
20
+
21
+ IFS='::' read -r plan step rounds handoff <<< "$state"
22
+ build_session_start_json "$plan" "$step" "$rounds" "$handoff"
23
+ exit 0
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ # Stop — 停止前合规检查(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ echo "[ADD Stop] 会话结束前,请确认所有 checklist 项已勾选、ADD-7 审计已落库。"
10
+ exit 0
@@ -0,0 +1,15 @@
1
+ #!/bin/bash
2
+ # SubagentStart/SubagentStop — 子代理门禁(Claude Code 适配)
3
+ set -euo pipefail
4
+
5
+ HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
6
+ SHARED_LIB="$HOOK_DIR/../../shared/hooks-lib/common.sh"
7
+ [ -f "$SHARED_LIB" ] && source "$SHARED_LIB"
8
+
9
+ input=$(parse_input)
10
+ event=$(echo "$input" | jq -r '.hook_event_name // empty')
11
+
12
+ if [ "$event" = "SubagentStart" ]; then
13
+ echo "[ADD SubagentGuard] 子代理启动,请确认其遵循 ADD 规范。"
14
+ fi
15
+ exit 0