add-coder 0.1.0

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 (97) hide show
  1. package/README.md +50 -0
  2. package/bin/add-coder.js +2 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +576 -0
  5. package/package.json +64 -0
  6. package/templates/adapters/claude/hooks/doc-format-guard.sh +17 -0
  7. package/templates/adapters/claude/hooks/notification.sh +9 -0
  8. package/templates/adapters/claude/hooks/permission-gate.sh +18 -0
  9. package/templates/adapters/claude/hooks/post-tool-failure.sh +10 -0
  10. package/templates/adapters/claude/hooks/post-tool-use.sh +18 -0
  11. package/templates/adapters/claude/hooks/pre-compact.sh +14 -0
  12. package/templates/adapters/claude/hooks/pre-tool-use.sh +28 -0
  13. package/templates/adapters/claude/hooks/prompt-submit.sh +16 -0
  14. package/templates/adapters/claude/hooks/review-checklist.sh +10 -0
  15. package/templates/adapters/claude/hooks/session-start.sh +23 -0
  16. package/templates/adapters/claude/hooks/stop-check.sh +10 -0
  17. package/templates/adapters/claude/hooks/subagent-guard.sh +15 -0
  18. package/templates/adapters/claude/mcp.json +13 -0
  19. package/templates/adapters/claude/settings.json +125 -0
  20. package/templates/adapters/qoder/hooks/doc-format-guard.sh +164 -0
  21. package/templates/adapters/qoder/hooks/lib/context-inject.sh +96 -0
  22. package/templates/adapters/qoder/hooks/lib/state-detect.sh +104 -0
  23. package/templates/adapters/qoder/hooks/lib/vocabulary.sh +49 -0
  24. package/templates/adapters/qoder/hooks/notification.sh +22 -0
  25. package/templates/adapters/qoder/hooks/permission-gate.sh +8 -0
  26. package/templates/adapters/qoder/hooks/post-tool-failure.sh +8 -0
  27. package/templates/adapters/qoder/hooks/post-tool-use.sh +20 -0
  28. package/templates/adapters/qoder/hooks/pre-compact.sh +12 -0
  29. package/templates/adapters/qoder/hooks/pre-tool-use.sh +77 -0
  30. package/templates/adapters/qoder/hooks/prompt-submit.sh +72 -0
  31. package/templates/adapters/qoder/hooks/review-checklist.sh +157 -0
  32. package/templates/adapters/qoder/hooks/session-start.sh +16 -0
  33. package/templates/adapters/qoder/hooks/stop-check.sh +71 -0
  34. package/templates/adapters/qoder/hooks/subagent-guard.sh +11 -0
  35. package/templates/adapters/qoder/mcp.json +13 -0
  36. package/templates/adapters/qoder/settings.json +125 -0
  37. package/templates/adapters/qoder/sync-policy.json +18 -0
  38. package/templates/adapters/vscode/extensions.json +5 -0
  39. package/templates/adapters/vscode/launch.json +16 -0
  40. package/templates/adapters/vscode/settings.json +16 -0
  41. package/templates/adapters/vscode/tasks.json +38 -0
  42. package/templates/core/agents/add-flow-guardian.md +276 -0
  43. package/templates/core/agents/add-orchestrator.md +217 -0
  44. package/templates/core/plans/2026-07/08/farm-agent-add-coder-npm-package-add-route-v1.md +323 -0
  45. package/templates/core/plans/2026-07/08/farm-agent-add-coder-npm-package-handoff-v1.md +678 -0
  46. package/templates/core/plans/2026-07/08/farm-agent-add-coder-npm-package-plan-v1.md +785 -0
  47. package/templates/core/prisma/add.prisma +34 -0
  48. package/templates/core/reports/REPORT-WORKFLOW.md +250 -0
  49. package/templates/core/reports/boundary-runtime-report.md +134 -0
  50. package/templates/core/reports/code-review-combined-report.md +227 -0
  51. package/templates/core/reports/code-review-fix-verification-report.md +505 -0
  52. package/templates/core/reports/code-review-suggestions.md +66 -0
  53. package/templates/core/reports/index.md +57 -0
  54. package/templates/core/reports/runtime-report/gateway.md +741 -0
  55. package/templates/core/rules/project_rules.md +905 -0
  56. package/templates/core/rules/theory-practice-map.toml +105 -0
  57. package/templates/core/scripts/mcp-server.ts +3492 -0
  58. package/templates/core/skills/add-paradigm/SKILL.md +1086 -0
  59. package/templates/core/skills/session-init/SKILL.md +215 -0
  60. package/templates/core/specs/farm-agent-add-coder-npm-package/checklist.md +125 -0
  61. package/templates/core/specs/farm-agent-add-coder-npm-package/spec.md +343 -0
  62. package/templates/core/specs/farm-agent-add-coder-npm-package/tasks.md +203 -0
  63. package/templates/core/templates/01-/346/236/266/346/236/204//343/200/212ADD/345/274/200/345/217/221/345/267/245/344/275/234/350/267/257/345/276/204/344/270/216/346/226/207/346/241/243/345/215/217/345/220/214/350/247/204/350/214/203/343/200/213.md +386 -0
  64. package/templates/core/templates/TERMINOLOGY.md +81 -0
  65. package/templates/core/templates/add-route-template-heavyweight.md +288 -0
  66. package/templates/core/templates/add-route-template.md +242 -0
  67. package/templates/core/templates/add-route-template.schema.json +35 -0
  68. package/templates/core/templates/checklist-template.md +72 -0
  69. package/templates/core/templates/checklist-template.schema.json +20 -0
  70. package/templates/core/templates/fix-verification-template.md +132 -0
  71. package/templates/core/templates/fix-verification-template.schema.json +54 -0
  72. package/templates/core/templates/handoff-multi-round-template.md +295 -0
  73. package/templates/core/templates/handoff-multi-round.schema.json +48 -0
  74. package/templates/core/templates/handoff-single-round-template.md +145 -0
  75. package/templates/core/templates/handoff-single-round.schema.json +92 -0
  76. package/templates/core/templates/index.md +60 -0
  77. package/templates/core/templates/report-template.md +126 -0
  78. package/templates/core/templates/report-template.schema.json +69 -0
  79. package/templates/core/templates/review-implementation-template.md +66 -0
  80. package/templates/core/templates/review-implementation-template.schema.json +58 -0
  81. package/templates/core/templates/review-runtime-template.md +73 -0
  82. package/templates/core/templates/review-runtime-template.schema.json +47 -0
  83. package/templates/core/templates/review-template.md +37 -0
  84. package/templates/core/templates/review-template.schema.json +42 -0
  85. package/templates/core/templates/runtime-report-template.md +73 -0
  86. package/templates/core/templates/runtime-report-template.schema.json +48 -0
  87. package/templates/core/templates/simple-plan-template.md +166 -0
  88. package/templates/core/templates/simple-plan-template.schema.json +109 -0
  89. package/templates/core/templates/spec-template.md +22 -0
  90. package/templates/core/templates/spec-template.schema.json +41 -0
  91. package/templates/core/templates/standard-plan-template.md +96 -0
  92. package/templates/core/templates/standard-plan-template.schema.json +73 -0
  93. package/templates/core/templates/tasks-template.md +54 -0
  94. package/templates/core/templates/tasks-template.schema.json +43 -0
  95. package/templates/core/tools/README.md +361 -0
  96. package/templates/core/vocabulary/add-governance-vocabulary.md +370 -0
  97. package/templates/shared/hooks-lib/common.sh +22 -0
@@ -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,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
@@ -0,0 +1,13 @@
1
+ {
2
+ "mcpServers": {
3
+ "add-dev-tools": {
4
+ "command": "{{mcpServerCommand}}",
5
+ "args": [
6
+ "{{projectRoot}}/.claude/scripts/mcp-server.ts"
7
+ ],
8
+ "env": {
9
+ "NODE_ENV": "development"
10
+ }
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,125 @@
1
+ {
2
+ "hooks": {
3
+ "SessionStart": [
4
+ {
5
+ "hooks": [
6
+ {
7
+ "type": "command",
8
+ "command": ".claude/hooks/session-start.sh"
9
+ }
10
+ ]
11
+ }
12
+ ],
13
+ "UserPromptSubmit": [
14
+ {
15
+ "hooks": [
16
+ {
17
+ "type": "command",
18
+ "command": ".claude/hooks/prompt-submit.sh"
19
+ }
20
+ ]
21
+ }
22
+ ],
23
+ "PreToolUse": [
24
+ {
25
+ "matcher": "Write|Edit|Bash",
26
+ "hooks": [
27
+ {
28
+ "type": "command",
29
+ "command": ".claude/hooks/pre-tool-use.sh"
30
+ },
31
+ {
32
+ "type": "command",
33
+ "command": ".claude/hooks/doc-format-guard.sh"
34
+ }
35
+ ]
36
+ }
37
+ ],
38
+ "PostToolUse": [
39
+ {
40
+ "matcher": "Write|Edit|Bash",
41
+ "hooks": [
42
+ {
43
+ "type": "command",
44
+ "command": ".claude/hooks/post-tool-use.sh"
45
+ },
46
+ {
47
+ "type": "command",
48
+ "command": ".claude/hooks/doc-format-guard.sh"
49
+ }
50
+ ]
51
+ }
52
+ ],
53
+ "PostToolUseFailure": [
54
+ {
55
+ "hooks": [
56
+ {
57
+ "type": "command",
58
+ "command": ".claude/hooks/post-tool-failure.sh"
59
+ }
60
+ ]
61
+ }
62
+ ],
63
+ "Stop": [
64
+ {
65
+ "hooks": [
66
+ {
67
+ "type": "command",
68
+ "command": ".claude/hooks/stop-check.sh"
69
+ }
70
+ ]
71
+ }
72
+ ],
73
+ "SubagentStart": [
74
+ {
75
+ "hooks": [
76
+ {
77
+ "type": "command",
78
+ "command": ".claude/hooks/subagent-guard.sh"
79
+ }
80
+ ]
81
+ }
82
+ ],
83
+ "SubagentStop": [
84
+ {
85
+ "hooks": [
86
+ {
87
+ "type": "command",
88
+ "command": ".claude/hooks/subagent-guard.sh"
89
+ }
90
+ ]
91
+ }
92
+ ],
93
+ "PreCompact": [
94
+ {
95
+ "hooks": [
96
+ {
97
+ "type": "command",
98
+ "command": ".claude/hooks/pre-compact.sh"
99
+ }
100
+ ]
101
+ }
102
+ ],
103
+ "Notification": [
104
+ {
105
+ "matcher": "result",
106
+ "hooks": [
107
+ {
108
+ "type": "command",
109
+ "command": ".claude/hooks/notification.sh"
110
+ }
111
+ ]
112
+ }
113
+ ],
114
+ "PermissionRequest": [
115
+ {
116
+ "hooks": [
117
+ {
118
+ "type": "command",
119
+ "command": ".claude/hooks/permission-gate.sh"
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ }
125
+ }
@@ -0,0 +1,164 @@
1
+ #!/bin/bash
2
+ # doc-format-guard.sh — schema.json 驱动的 ADD 文档格式守卫
3
+ set -euo pipefail
4
+
5
+ input=$(cat)
6
+
7
+ # DEBUG: dump stdin for investigation
8
+ mkdir -p .qoder/debug-dump
9
+ echo "=== $(date) ===" >> .qoder/debug-dump/stdin.log
10
+ echo "file_path: $(echo "$input" | jq -r '.tool_input.file_path // "EMPTY"')" >> .qoder/debug-dump/stdin.log
11
+ echo "has_file_content: $(echo "$input" | jq 'has("tool_input") and (.tool_input | has("file_content"))')" >> .qoder/debug-dump/stdin.log
12
+ echo "has_replacements: $(echo "$input" | jq 'has("tool_input") and (.tool_input | has("replacements"))')" >> .qoder/debug-dump/stdin.log
13
+ echo "top_keys: $(echo "$input" | jq -r 'keys | join(", ")')" >> .qoder/debug-dump/stdin.log
14
+ echo "tool_input_keys: $(echo "$input" | jq -r '.tool_input | keys | join(", ") // "NO_TOOL_INPUT"')" >> .qoder/debug-dump/stdin.log
15
+ echo "=== DONE ===" >> .qoder/debug-dump/stdin.log
16
+ file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
17
+ # L17: 非文件工具事件(空 stdin)→ 不拦截(由 matcher 层过滤)
18
+ [ -z "$file_path" ] && exit 0
19
+
20
+ if ! echo "$file_path" | grep -qE '\.qoder/(plans|specs)/'; then
21
+ exit 0
22
+ fi
23
+
24
+ CONTENT=$(echo "$input" | jq -r '
25
+ if .tool_input.file_content then .tool_input.file_content
26
+ elif .tool_input.content then .tool_input.content
27
+ elif .tool_input.replacements then .tool_input.replacements[0].new_text
28
+ else "" end')
29
+ # L24: PostToolUse 不可阻断(仅反馈),PreToolUse exit 2 + ask 可拦截
30
+ # L24: 文件在 .qoder/(plans|specs)/ 下但 Write 工具未传 content → 无法校验,阻断
31
+ if [ -z "$CONTENT" ]; then
32
+ echo "⛔ 拒绝:Write 工具未传 file_content,无法校验手写文档格式" >&2
33
+ echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"Write 工具未传 file_content,无法校验手写文档。请用 SearchReplace 改写已有文件,或用 Write 工具重试。"}}'
34
+ exit 2
35
+ fi
36
+
37
+ PROJECT_DIR="${QODER_PROJECT_DIR:-${QODERCN_PROJECT_DIR:-$PWD}}"
38
+ TEMPLATES_DIR="$PROJECT_DIR/.qoder/templates"
39
+
40
+ # 通过 filename 匹配模板名
41
+ TEMPLATE_NAME=""
42
+ for tmpl in "$TEMPLATES_DIR"/*.md; do
43
+ base=$(basename "$tmpl")
44
+ if echo "$file_path" | grep -q "$base"; then
45
+ TEMPLATE_NAME="$base"
46
+ break
47
+ fi
48
+ done
49
+
50
+ # 退一步:根据文件内容特征猜测模板类型
51
+ if [ -z "$TEMPLATE_NAME" ]; then
52
+ if echo "$CONTENT" | grep -q "## 四、Handoff"; then
53
+ TEMPLATE_NAME="simple-standard-plan-template.md"
54
+ elif echo "$CONTENT" | grep -q "## PLAN 元信息"; then
55
+ TEMPLATE_NAME="standard-plan-template.md"
56
+ elif echo "$CONTENT" | grep -q "## Review 元信息"; then
57
+ if echo "$CONTENT" | grep -q "运行时验证"; then
58
+ TEMPLATE_NAME="review-runtime-template.md"
59
+ elif echo "$CONTENT" | grep -q "跨仓库格式契约"; then
60
+ TEMPLATE_NAME="review-implementation-template.md"
61
+ else
62
+ TEMPLATE_NAME="review-template.md"
63
+ fi
64
+ elif echo "$CONTENT" | grep -q "## Why"; then
65
+ TEMPLATE_NAME="spec-template.md"
66
+ elif echo "$CONTENT" | grep -q "## Preconditions"; then
67
+ TEMPLATE_NAME="tasks-template.md"
68
+ elif echo "$CONTENT" | grep -q "审计链(证据→devlog→checklist)"; then
69
+ TEMPLATE_NAME="checklist-template.md"
70
+ else
71
+ case "$file_path" in
72
+ *handoff*)
73
+ # ★ 按内容特征区分单/多轮 handoff,不依赖文件名
74
+ if echo "$CONTENT" | grep -qF "## 全局元信息"; then
75
+ TEMPLATE_NAME="handoff-multi-round-template.md"
76
+ elif echo "$CONTENT" | grep -qF "## 1. 交接前状态"; then
77
+ TEMPLATE_NAME="handoff-single-round-template.md"
78
+ else
79
+ echo "⛔ handoff 文件内容无法识别模板类型(缺 '## 全局元信息' 或 '## 1. 交接前状态'),拒绝写入" >&2
80
+ echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"handoff 内容不符合 single/multi 模板规范"}}'
81
+ exit 2
82
+ fi
83
+ ;;
84
+ *plan*) TEMPLATE_NAME="standard-plan-template.md" ;;
85
+ *add-route*heavy*) TEMPLATE_NAME="add-route-template-heavyweight.md" ;;
86
+ *add-route*) TEMPLATE_NAME="add-route-template.md" ;;
87
+ *report*runtime*) TEMPLATE_NAME="runtime-report-template.md" ;;
88
+ *report*) TEMPLATE_NAME="report-template.md" ;;
89
+ *fix-verif*) TEMPLATE_NAME="fix-verification-template.md" ;;
90
+ *)
91
+ echo "⛔ 拒绝:无法识别文档类型 (file_path: $file_path),缺少模板匹配规则" >&2
92
+ echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"无法识别 ADD 文档类型,请联系管理员更新 doc-format-guard.sh"}}'
93
+ exit 2
94
+ ;;
95
+ esac
96
+ fi
97
+ fi
98
+
99
+ SCHEMA_FILE="$TEMPLATES_DIR/${TEMPLATE_NAME%.md}.schema.json"
100
+ # L84: schema 文件不存在 → 无校验规则,阻断(不允许无规则放行)
101
+ if [ ! -f "$SCHEMA_FILE" ]; then
102
+ echo "⛔ 阻断:模板 ${TEMPLATE_NAME} 缺少对应的 .schema.json 校验规则" >&2
103
+ echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"缺少 .schema.json 校验规则文件,禁止无规则放行"}}'
104
+ exit 2
105
+ fi
106
+
107
+ # SearchReplace 只传 patch(replacements[].new_text),不传全文件 → 跳过章节校验
108
+ IS_SEARCH_REPLACE=$(echo "$input" | jq -r '.tool_input.replacements | length > 0 // false')
109
+
110
+ ISSUES=""
111
+
112
+ # ── 章节校验 ──
113
+ # 使用项目目录下的临时文件,避免 /tmp 在沙箱中不可写导致静默跳过
114
+ TMPFILE="$PROJECT_DIR/.qoder/.doc-guard-issues.tmp"
115
+ : > "$TMPFILE"
116
+ trap "rm -f $TMPFILE" EXIT
117
+ # SearchReplace 只传 patch → 跳过章节/子章节校验(无法从 patch 推断完整文档结构)
118
+ if [ "$IS_SEARCH_REPLACE" != "true" ]; then
119
+ jq -r '.sections[] | select(.required == true) | .heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r heading; do
120
+ if ! echo "$CONTENT" | grep -qF "$heading"; then
121
+ echo " 缺章节: $heading"
122
+ fi
123
+ done > "$TMPFILE"
124
+
125
+ # 子章节
126
+ jq -r '.sections[].subsections[]?.heading' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r sub; do
127
+ if ! echo "$CONTENT" | grep -qF "$sub"; then
128
+ echo " 缺子章节: $sub"
129
+ fi
130
+ done >> "$TMPFILE"
131
+ fi
132
+
133
+ # ── 占位符校验 ──
134
+ jq -r '.placeholders[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r ph; do
135
+ if echo "$CONTENT" | grep -qF "$ph"; then
136
+ echo " 未替换占位符: $ph"
137
+ fi
138
+ done >> "$TMPFILE"
139
+
140
+ # ── 禁止词校验 ──
141
+ jq -r '.forbidden_terms[]?' "$SCHEMA_FILE" 2>/dev/null | while IFS= read -r term; do
142
+ if echo "$CONTENT" | grep -qw "$term"; then
143
+ echo " 禁止词: $term"
144
+ fi
145
+ done >> "$TMPFILE"
146
+
147
+ ISSUES=$(cat "$TMPFILE" 2>/dev/null)
148
+
149
+ # ── 阻断或放行 ──
150
+ if [ -n "$ISSUES" ]; then
151
+ echo "⛔ $TEMPLATE_NAME 校验不通过:
152
+ $ISSUES" >&2
153
+ echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"ask\",\"permissionDecisionReason\":\"文档格式校验不通过,请对照模板修正\"}}"
154
+ exit 2
155
+ fi
156
+
157
+ # ── 自动更新 index.md ──
158
+ if echo "$file_path" | grep -q '\.qoder/plans/'; then
159
+ if [ -x "$PROJECT_DIR/scripts/gen-plan-index.sh" ]; then
160
+ "$PROJECT_DIR/scripts/gen-plan-index.sh" 2>/dev/null || true
161
+ fi
162
+ fi
163
+
164
+ 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
+ }