cc-devflow 2.4.6 → 4.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 (191) hide show
  1. package/.claude/CLAUDE.md +1065 -48
  2. package/.claude/agents/dev-implementer.md +195 -0
  3. package/.claude/commands/{flow-archive.md → flow/archive.md} +46 -11
  4. package/.claude/commands/flow/context.md +150 -0
  5. package/.claude/commands/flow/delta.md +245 -0
  6. package/.claude/commands/{flow-dev.md → flow/dev.md} +112 -11
  7. package/.claude/commands/flow/init.md +45 -0
  8. package/.claude/commands/flow/quality.md +159 -0
  9. package/.claude/commands/flow/spec.md +186 -0
  10. package/.claude/commands/flow/workspace.md +146 -0
  11. package/.claude/commands/{cancel-ralph.md → util/cancel-ralph.md} +1 -0
  12. package/.claude/config/quality-gates.yml +305 -0
  13. package/.claude/docs/guides/TEAM_MODE_GUIDE.md +313 -0
  14. package/.claude/docs/templates/DELTA_SPEC_TEMPLATE.md +91 -0
  15. package/.claude/docs/templates/DESIGN_DECISIONS_TEMPLATE.md +151 -0
  16. package/.claude/docs/templates/JOURNAL_TEMPLATE.md +75 -0
  17. package/.claude/docs/templates/_shared/CLAUDE.md +36 -0
  18. package/.claude/docs/templates/_shared/CONSTITUTION_CHECK.md +125 -0
  19. package/.claude/docs/templates/_shared/VALIDATION_CHECKLIST.md +187 -0
  20. package/.claude/docs/templates/_shared/YAML_FRONTMATTER.md +164 -0
  21. package/.claude/docs/templates/context/dev.jsonl.template +6 -0
  22. package/.claude/docs/templates/context/epic.jsonl.template +5 -0
  23. package/.claude/docs/templates/context/prd.jsonl.template +4 -0
  24. package/.claude/docs/templates/context/research.jsonl.template +4 -0
  25. package/.claude/docs/templates/context/review.jsonl.template +5 -0
  26. package/.claude/docs/templates/context/tech.jsonl.template +5 -0
  27. package/.claude/hooks/CLAUDE.md +342 -0
  28. package/.claude/hooks/inject-agent-context.ts +480 -0
  29. package/.claude/hooks/inject-skill-context.ts +359 -0
  30. package/.claude/hooks/ralph-loop.ts +931 -0
  31. package/.claude/hooks/task-completed-hook.ts +593 -0
  32. package/.claude/hooks/teammate-idle-hook.ts +690 -0
  33. package/.claude/hooks/types/team-types.d.ts +238 -0
  34. package/.claude/rules/devflow-conventions.md +82 -9
  35. package/.claude/scripts/archive-requirement.sh +44 -1
  36. package/.claude/scripts/common.sh +670 -3
  37. package/.claude/scripts/delta-parser.ts +527 -0
  38. package/.claude/scripts/detect-file-conflicts.sh +151 -0
  39. package/.claude/scripts/flow-context-add.sh +134 -0
  40. package/.claude/scripts/flow-context-init.sh +133 -0
  41. package/.claude/scripts/flow-context-validate.sh +144 -0
  42. package/.claude/scripts/flow-delta-apply.sh +297 -0
  43. package/.claude/scripts/flow-delta-archive.sh +71 -0
  44. package/.claude/scripts/flow-delta-create.sh +202 -0
  45. package/.claude/scripts/flow-delta-list.sh +142 -0
  46. package/.claude/scripts/flow-delta-status.sh +235 -0
  47. package/.claude/scripts/flow-quality-full.sh +184 -0
  48. package/.claude/scripts/flow-quality-quick.sh +64 -0
  49. package/.claude/scripts/flow-workspace-init.sh +117 -0
  50. package/.claude/scripts/flow-workspace-record.sh +164 -0
  51. package/.claude/scripts/flow-workspace-start.sh +88 -0
  52. package/.claude/scripts/get-workflow-status.sh +415 -0
  53. package/.claude/scripts/parse-task-dependencies.js +334 -0
  54. package/.claude/scripts/record-quality-error.sh +165 -0
  55. package/.claude/scripts/run-quality-gates.sh +242 -0
  56. package/.claude/scripts/team-dev-init.sh +319 -0
  57. package/.claude/scripts/team-state-recovery.sh +229 -0
  58. package/.claude/scripts/workflow-status.ts +433 -0
  59. package/.claude/settings.json +19 -0
  60. package/.claude/skills/cc-devflow-orchestrator/SKILL.md +85 -200
  61. package/.claude/skills/domain/using-git-worktrees/SKILL.md +252 -0
  62. package/.claude/skills/domain/using-git-worktrees/assets/SHELL_ALIASES.md +133 -0
  63. package/.claude/skills/domain/using-git-worktrees/context.jsonl +4 -0
  64. package/.claude/skills/domain/using-git-worktrees/scripts/worktree-cleanup.sh +218 -0
  65. package/.claude/skills/domain/using-git-worktrees/scripts/worktree-create.sh +232 -0
  66. package/.claude/skills/domain/using-git-worktrees/scripts/worktree-list.sh +130 -0
  67. package/.claude/skills/domain/using-git-worktrees/scripts/worktree-status.sh +140 -0
  68. package/.claude/skills/domain/using-git-worktrees/scripts/worktree-switch.sh +70 -0
  69. package/.claude/skills/skill-rules.json +72 -1
  70. package/.claude/skills/utility/journey-checker/SKILL.md +199 -0
  71. package/.claude/skills/utility/journey-checker/pressure-scenarios.md +164 -0
  72. package/.claude/skills/utility/skill-creator/LICENSE.txt +202 -0
  73. package/.claude/skills/utility/skill-creator/SKILL.md +356 -0
  74. package/.claude/skills/utility/skill-creator/references/output-patterns.md +82 -0
  75. package/.claude/skills/utility/skill-creator/references/workflows.md +28 -0
  76. package/.claude/skills/utility/skill-creator/scripts/init_skill.py +303 -0
  77. package/.claude/skills/utility/skill-creator/scripts/package_skill.py +110 -0
  78. package/.claude/skills/utility/skill-creator/scripts/quick_validate.py +95 -0
  79. package/.claude/skills/workflow/flow-dev/CLAUDE.md +78 -0
  80. package/.claude/skills/workflow/flow-dev/SKILL.md +96 -0
  81. package/.claude/skills/workflow/flow-dev/assets/IMPLEMENTATION_PLAN_TEMPLATE.md +71 -0
  82. package/.claude/skills/workflow/flow-dev/context.jsonl +8 -0
  83. package/.claude/skills/workflow/flow-dev/dev-implementer.jsonl +8 -0
  84. package/.claude/skills/workflow/flow-dev/scripts/entry-gate.sh +116 -0
  85. package/.claude/skills/workflow/flow-dev/scripts/exit-gate.sh +101 -0
  86. package/.claude/skills/workflow/flow-dev/scripts/task-orchestrator.sh +106 -0
  87. package/.claude/skills/workflow/flow-fix/SKILL.md +105 -0
  88. package/.claude/skills/workflow/flow-fix/context.jsonl +6 -0
  89. package/.claude/skills/workflow/flow-fix/references/bug-analyzer.md +381 -0
  90. package/.claude/skills/workflow/flow-init/SKILL.md +211 -0
  91. package/.claude/skills/workflow/flow-init/assets/BRAINSTORM_TEMPLATE.md +148 -0
  92. package/.claude/skills/workflow/flow-init/assets/INIT_FLOW_TEMPLATE.md +198 -0
  93. package/.claude/skills/workflow/flow-init/assets/RESEARCH_TEMPLATE.md +276 -0
  94. package/.claude/skills/workflow/flow-init/context.jsonl +5 -0
  95. package/.claude/skills/workflow/flow-init/references/flow-researcher.md +132 -0
  96. package/.claude/skills/workflow/flow-init/scripts/check-prerequisites.sh +232 -0
  97. package/.claude/skills/workflow/flow-init/scripts/consolidate-research.sh +182 -0
  98. package/.claude/skills/workflow/flow-init/scripts/create-requirement.sh +515 -0
  99. package/.claude/skills/workflow/flow-init/scripts/generate-research-tasks.sh +157 -0
  100. package/.claude/skills/workflow/flow-init/scripts/populate-research-tasks.sh +284 -0
  101. package/.claude/skills/workflow/flow-init/scripts/validate-research.sh +332 -0
  102. package/.claude/skills/workflow/flow-quality/SKILL.md +94 -0
  103. package/.claude/skills/workflow/flow-quality/context.jsonl +6 -0
  104. package/.claude/skills/workflow/flow-quality/references/code-quality-reviewer.md +205 -0
  105. package/.claude/skills/workflow/flow-quality/references/qa-tester.md +313 -0
  106. package/.claude/skills/workflow/flow-quality/references/security-reviewer.md +314 -0
  107. package/.claude/skills/workflow/flow-quality/references/spec-reviewer.md +221 -0
  108. package/.claude/skills/workflow/flow-release/SKILL.md +126 -0
  109. package/.claude/skills/workflow/flow-release/context.jsonl +7 -0
  110. package/.claude/skills/workflow/flow-release/references/release-manager.md +295 -0
  111. package/.claude/skills/workflow/flow-spec/CLAUDE.md +103 -0
  112. package/.claude/skills/workflow/flow-spec/SKILL.md +545 -0
  113. package/.claude/skills/workflow/flow-spec/context.jsonl +7 -0
  114. package/.claude/skills/workflow/flow-spec/scripts/entry-gate.sh +194 -0
  115. package/.claude/skills/workflow/flow-spec/scripts/exit-gate.sh +244 -0
  116. package/.claude/skills/workflow/flow-spec/scripts/parallel-orchestrator.sh +205 -0
  117. package/.claude/skills/workflow/flow-spec/scripts/team-communication.sh +353 -0
  118. package/.claude/skills/workflow/flow-spec/scripts/team-init.sh +195 -0
  119. package/.claude/skills/workflow/flow-spec/scripts/test-team-mode.sh +496 -0
  120. package/.claude/skills/workflow/flow-spec/team-config.json +165 -0
  121. package/.claude/skills/workflow.yaml +417 -0
  122. package/CHANGELOG.md +254 -0
  123. package/README.md +193 -33
  124. package/README.zh-CN.md +206 -46
  125. package/lib/compiler/CLAUDE.md +77 -46
  126. package/lib/compiler/__tests__/multi-module-emitters.test.js +508 -0
  127. package/lib/compiler/context-expander.js +179 -0
  128. package/lib/compiler/emitters/antigravity-emitter.js +195 -5
  129. package/lib/compiler/emitters/base-emitter.js +217 -2
  130. package/lib/compiler/emitters/codex-emitter.js +200 -4
  131. package/lib/compiler/emitters/cursor-emitter.js +307 -3
  132. package/lib/compiler/emitters/qwen-emitter.js +196 -4
  133. package/lib/compiler/index.js +197 -2
  134. package/lib/compiler/platforms.js +270 -21
  135. package/package.json +1 -1
  136. package/.claude/commands/flow-epic.md +0 -183
  137. package/.claude/commands/flow-init.md +0 -370
  138. package/.claude/commands/flow-prd.md +0 -144
  139. package/.claude/commands/flow-qa.md +0 -93
  140. package/.claude/commands/flow-review.md +0 -257
  141. package/.claude/commands/flow-tech.md +0 -142
  142. package/.claude/commands/flow-ui.md +0 -189
  143. package/.claude/skills/file-header-guardian/SKILL.md +0 -56
  144. package/.claude/skills/skill-developer/ADVANCED.md +0 -197
  145. package/.claude/skills/skill-developer/HOOK_MECHANISMS.md +0 -306
  146. package/.claude/skills/skill-developer/PATTERNS_LIBRARY.md +0 -152
  147. package/.claude/skills/skill-developer/SKILL.md +0 -426
  148. package/.claude/skills/skill-developer/SKILL_RULES_REFERENCE.md +0 -315
  149. package/.claude/skills/skill-developer/TRIGGER_TYPES.md +0 -305
  150. package/.claude/skills/skill-developer/TROUBLESHOOTING.md +0 -514
  151. package/.claude/skills/writing-skills/SKILL.md +0 -655
  152. package/.claude/skills/writing-skills/anthropic-best-practices.md +0 -1150
  153. package/.claude/skills/writing-skills/examples/CLAUDE_MD_TESTING.md +0 -189
  154. package/.claude/skills/writing-skills/graphviz-conventions.dot +0 -172
  155. package/.claude/skills/writing-skills/persuasion-principles.md +0 -187
  156. package/.claude/skills/writing-skills/render-graphs.js +0 -168
  157. package/.claude/skills/writing-skills/testing-skills-with-subagents.md +0 -384
  158. package/.claude/tsc-cache/795ba6e3-b98a-423b-bab2-51aa62812569/affected-repos.txt +0 -1
  159. package/.claude/tsc-cache/ae335694-be5a-4ba4-a1a0-b676c09a7906/affected-repos.txt +0 -1
  160. /package/.claude/commands/{core-architecture.md → core/architecture.md} +0 -0
  161. /package/.claude/commands/{core-guidelines.md → core/guidelines.md} +0 -0
  162. /package/.claude/commands/{core-roadmap.md → core/roadmap.md} +0 -0
  163. /package/.claude/commands/{core-style.md → core/style.md} +0 -0
  164. /package/.claude/commands/{flow-checklist.md → flow/checklist.md} +0 -0
  165. /package/.claude/commands/{flow-clarify.md → flow/clarify.md} +0 -0
  166. /package/.claude/commands/{flow-constitution.md → flow/constitution.md} +0 -0
  167. /package/.claude/commands/{flow-fix.md → flow/fix.md} +0 -0
  168. /package/.claude/commands/{flow-ideate.md → flow/ideate.md} +0 -0
  169. /package/.claude/commands/{flow-new.md → flow/new.md} +0 -0
  170. /package/.claude/commands/{flow-release.md → flow/release.md} +0 -0
  171. /package/.claude/commands/{flow-restart.md → flow/restart.md} +0 -0
  172. /package/.claude/commands/{flow-status.md → flow/status.md} +0 -0
  173. /package/.claude/commands/{flow-update.md → flow/update.md} +0 -0
  174. /package/.claude/commands/{flow-upgrade.md → flow/upgrade.md} +0 -0
  175. /package/.claude/commands/{flow-verify.md → flow/verify.md} +0 -0
  176. /package/.claude/commands/{code-review-high.md → util/code-review.md} +0 -0
  177. /package/.claude/commands/{git-commit.md → util/git-commit.md} +0 -0
  178. /package/.claude/commands/{problem-analyzer.md → util/problem-analyzer.md} +0 -0
  179. /package/.claude/skills/{flow-attention-refresh → domain/attention-refresh}/SKILL.md +0 -0
  180. /package/.claude/skills/{flow-brainstorming → domain/brainstorming}/SKILL.md +0 -0
  181. /package/.claude/skills/{flow-debugging → domain/debugging}/SKILL.md +0 -0
  182. /package/.claude/skills/{flow-finishing-branch → domain/finishing-branch}/SKILL.md +0 -0
  183. /package/.claude/skills/{flow-receiving-review → domain/receiving-review}/SKILL.md +0 -0
  184. /package/.claude/skills/{flow-tdd → domain/tdd}/SKILL.md +0 -0
  185. /package/.claude/skills/{verification-before-completion → domain/verification}/SKILL.md +0 -0
  186. /package/.claude/skills/{constitution-guardian → guardrail/constitution-guardian}/SKILL.md +0 -0
  187. /package/.claude/skills/{devflow-tdd-enforcer → guardrail/tdd-enforcer}/SKILL.md +0 -0
  188. /package/.claude/skills/{devflow-constitution-quick-ref → utility/constitution-quick-ref}/SKILL.md +0 -0
  189. /package/.claude/skills/{devflow-file-standards → utility/file-standards}/SKILL.md +0 -0
  190. /package/.claude/skills/{fractal-docs-generator → utility/fractal-docs}/SKILL.md +0 -0
  191. /package/.claude/skills/{npm-release → utility/npm-release}/SKILL.md +0 -0
@@ -0,0 +1,593 @@
1
+ #!/usr/bin/env npx ts-node
2
+ /**
3
+ * [INPUT]: 依赖 quality-gates.yml 的 task_completed.verify 配置,依赖 common.sh 的 mark_teammate_task_complete
4
+ * [OUTPUT]: 对外提供 TaskCompleted 钩子,验证任务完成质量并更新状态
5
+ * [POS]: hooks/ 的任务完成验证器,被 Claude Team 系统消费
6
+ * [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
7
+ *
8
+ * =============================================================================
9
+ * CC-DevFlow TaskCompleted Hook (v4.7)
10
+ * =============================================================================
11
+ *
12
+ * 核心设计哲学:
13
+ * - 任务标记完成时执行程序化验证
14
+ * - 验证失败且 block_on_failure=true 时阻止完成
15
+ * - 记录失败到 ERROR_LOG.md
16
+ * - 更新 Team 状态并检查阶段转换
17
+ *
18
+ * 触发条件:TaskCompleted 事件
19
+ */
20
+
21
+ import * as fs from 'fs';
22
+ import * as path from 'path';
23
+ import { execSync } from 'child_process';
24
+
25
+ // =============================================================================
26
+ // 类型定义 (复用 team-types.d.ts 中的定义)
27
+ // =============================================================================
28
+
29
+ interface TaskCompletedInput {
30
+ hook_event_name: 'TaskCompleted';
31
+ task_id: string;
32
+ task_subject: string;
33
+ completed_by: string;
34
+ completion_time: string;
35
+ cwd: string;
36
+ session_id: string;
37
+ }
38
+
39
+ interface TaskCompletedOutput {
40
+ decision: 'accept' | 'reject';
41
+ reason: string;
42
+ next_actions?: string[];
43
+ }
44
+
45
+ interface VerifyCommand {
46
+ name?: string;
47
+ command: string;
48
+ required?: boolean;
49
+ }
50
+
51
+ interface TaskCompletedConfig {
52
+ verify?: (string | VerifyCommand)[];
53
+ block_on_failure?: boolean;
54
+ max_retries?: number;
55
+ }
56
+
57
+ interface QualityGatesConfig {
58
+ task_completed?: TaskCompletedConfig;
59
+ verify?: (string | VerifyCommand)[];
60
+ }
61
+
62
+ interface FailureRecord {
63
+ command: string;
64
+ output: string;
65
+ timestamp: string;
66
+ }
67
+
68
+ // =============================================================================
69
+ // 配置常量
70
+ // =============================================================================
71
+
72
+ const CONFIG_FILE = '.claude/config/quality-gates.yml';
73
+ const COMMAND_TIMEOUT_MS = 120000; // 2 minutes per command
74
+ const SCRIPTS_DIR = '.claude/scripts';
75
+
76
+ // =============================================================================
77
+ // 工具函数
78
+ // =============================================================================
79
+
80
+ /**
81
+ * 查找 Git 仓库根目录
82
+ */
83
+ function findRepoRoot(startPath: string): string | null {
84
+ let current = path.resolve(startPath);
85
+ while (current !== path.dirname(current)) {
86
+ if (fs.existsSync(path.join(current, '.git'))) {
87
+ return current;
88
+ }
89
+ current = path.dirname(current);
90
+ }
91
+ return null;
92
+ }
93
+
94
+ /**
95
+ * 简单 YAML 解析器 (无外部依赖)
96
+ * 解析 task_completed 和顶级 verify 配置
97
+ */
98
+ function parseSimpleYaml(content: string): QualityGatesConfig {
99
+ const config: QualityGatesConfig = {};
100
+ const lines = content.split('\n');
101
+
102
+ let currentSection = '';
103
+ let inVerifySection = false;
104
+ let inTaskCompletedSection = false;
105
+ let inTaskCompletedVerify = false;
106
+ const topLevelVerify: (string | VerifyCommand)[] = [];
107
+ const taskCompletedVerify: (string | VerifyCommand)[] = [];
108
+ let blockOnFailure = true; // default
109
+
110
+ for (const line of lines) {
111
+ const trimmed = line.trim();
112
+ const indent = line.length - line.trimStart().length;
113
+
114
+ // 跳过注释和空行
115
+ if (trimmed.startsWith('#') || trimmed === '') continue;
116
+
117
+ // 检测顶级 section
118
+ if (indent === 0 && trimmed.endsWith(':')) {
119
+ currentSection = trimmed.slice(0, -1);
120
+ inVerifySection = false;
121
+ inTaskCompletedSection = currentSection === 'task_completed';
122
+ inTaskCompletedVerify = false;
123
+ continue;
124
+ }
125
+
126
+ // 检测顶级 verify section
127
+ if (indent === 0 && trimmed === 'verify:') {
128
+ inVerifySection = true;
129
+ inTaskCompletedSection = false;
130
+ continue;
131
+ }
132
+
133
+ // 解析顶级 verify 列表项
134
+ if (inVerifySection && trimmed.startsWith('- ')) {
135
+ const value = trimmed.slice(2).trim();
136
+ if (!value.startsWith('name:') && !value.includes(':')) {
137
+ topLevelVerify.push(value);
138
+ }
139
+ continue;
140
+ }
141
+
142
+ // 解析 task_completed section
143
+ if (inTaskCompletedSection) {
144
+ if (trimmed === 'verify:') {
145
+ inTaskCompletedVerify = true;
146
+ continue;
147
+ }
148
+
149
+ if (trimmed.startsWith('block_on_failure:')) {
150
+ const val = trimmed.split(':')[1].trim().toLowerCase();
151
+ blockOnFailure = val === 'true';
152
+ continue;
153
+ }
154
+
155
+ if (inTaskCompletedVerify && trimmed.startsWith('- ')) {
156
+ const value = trimmed.slice(2).trim();
157
+ if (!value.startsWith('name:') && !value.includes(':')) {
158
+ taskCompletedVerify.push(value);
159
+ }
160
+ continue;
161
+ }
162
+ }
163
+ }
164
+
165
+ // 设置顶级 verify
166
+ if (topLevelVerify.length > 0) {
167
+ config.verify = topLevelVerify;
168
+ }
169
+
170
+ // 设置 task_completed 配置
171
+ if (taskCompletedVerify.length > 0 || inTaskCompletedSection) {
172
+ config.task_completed = {
173
+ verify: taskCompletedVerify.length > 0 ? taskCompletedVerify : undefined,
174
+ block_on_failure: blockOnFailure
175
+ };
176
+ }
177
+
178
+ return config;
179
+ }
180
+
181
+ /**
182
+ * 读取 quality-gates.yml 配置
183
+ */
184
+ function loadConfig(repoRoot: string): QualityGatesConfig {
185
+ const configPath = path.join(repoRoot, CONFIG_FILE);
186
+ if (!fs.existsSync(configPath)) {
187
+ return {};
188
+ }
189
+
190
+ try {
191
+ const content = fs.readFileSync(configPath, 'utf-8');
192
+ return parseSimpleYaml(content);
193
+ } catch {
194
+ return {};
195
+ }
196
+ }
197
+
198
+ /**
199
+ * 获取 verify 命令列表
200
+ * 优先使用 task_completed.verify,否则使用顶级 verify
201
+ */
202
+ function getVerifyCommands(config: QualityGatesConfig): string[] {
203
+ const taskCompletedVerify = config.task_completed?.verify;
204
+ if (taskCompletedVerify && taskCompletedVerify.length > 0) {
205
+ return taskCompletedVerify.map(cmd => {
206
+ if (typeof cmd === 'string') return cmd;
207
+ return cmd.command;
208
+ });
209
+ }
210
+
211
+ if (config.verify && config.verify.length > 0) {
212
+ return config.verify.map(cmd => {
213
+ if (typeof cmd === 'string') return cmd;
214
+ return cmd.command;
215
+ });
216
+ }
217
+
218
+ // 默认验证命令
219
+ return [
220
+ 'npm run lint --if-present',
221
+ 'npm run typecheck --if-present',
222
+ 'npm test -- --passWithNoTests'
223
+ ];
224
+ }
225
+
226
+ /**
227
+ * 获取 block_on_failure 配置
228
+ */
229
+ function getBlockOnFailure(config: QualityGatesConfig): boolean {
230
+ return config.task_completed?.block_on_failure ?? true;
231
+ }
232
+
233
+ /**
234
+ * 执行验证命令 (复用 ralph-loop.ts 逻辑)
235
+ */
236
+ function runVerifyCommands(
237
+ repoRoot: string,
238
+ commands: string[]
239
+ ): { passed: boolean; failures: FailureRecord[] } {
240
+ const failures: FailureRecord[] = [];
241
+
242
+ for (const cmd of commands) {
243
+ try {
244
+ execSync(cmd, {
245
+ cwd: repoRoot,
246
+ timeout: COMMAND_TIMEOUT_MS,
247
+ stdio: ['pipe', 'pipe', 'pipe'],
248
+ encoding: 'utf-8'
249
+ });
250
+ } catch (error: unknown) {
251
+ let output = '';
252
+ if (error && typeof error === 'object') {
253
+ const execError = error as { stderr?: string; stdout?: string; message?: string };
254
+ output = execError.stderr || execError.stdout || execError.message || 'Unknown error';
255
+ }
256
+
257
+ // 截断过长输出
258
+ if (output.length > 500) {
259
+ output = output.slice(0, 500) + '...';
260
+ }
261
+
262
+ failures.push({
263
+ command: cmd,
264
+ output,
265
+ timestamp: new Date().toISOString()
266
+ });
267
+ }
268
+ }
269
+
270
+ return {
271
+ passed: failures.length === 0,
272
+ failures
273
+ };
274
+ }
275
+
276
+ /**
277
+ * 格式化失败信息
278
+ */
279
+ function formatFailures(failures: FailureRecord[]): string {
280
+ return failures.map(f => `Command: ${f.command}\nOutput: ${f.output}`).join('\n\n');
281
+ }
282
+
283
+ /**
284
+ * 记录错误到 ERROR_LOG.md
285
+ */
286
+ function recordErrorToLog(
287
+ repoRoot: string,
288
+ taskId: string,
289
+ taskSubject: string,
290
+ failures: FailureRecord[]
291
+ ): void {
292
+ // 尝试找到当前需求目录
293
+ const reqId = detectReqId(repoRoot);
294
+ if (!reqId) return;
295
+
296
+ const reqDir = path.join(repoRoot, 'devflow', 'requirements', reqId);
297
+ const errorLogPath = path.join(reqDir, 'ERROR_LOG.md');
298
+
299
+ const timestamp = new Date().toISOString();
300
+ const errorNumber = getNextErrorNumber(errorLogPath);
301
+
302
+ const entry = `
303
+ ## [${timestamp}] E${errorNumber.toString().padStart(3, '0')}: Task ${taskId} Verification Failed
304
+
305
+ **Phase**: flow-dev / ${taskId}
306
+ **Task**: ${taskSubject}
307
+ **Error Type**: Verification Failure
308
+ **Error Message**:
309
+ \`\`\`
310
+ ${formatFailures(failures)}
311
+ \`\`\`
312
+ **Root Cause**: [Pending analysis]
313
+ **Resolution**: [Pending fix]
314
+ **Prevention**: [Optional]
315
+
316
+ ---
317
+ `;
318
+
319
+ try {
320
+ if (fs.existsSync(errorLogPath)) {
321
+ fs.appendFileSync(errorLogPath, entry, 'utf-8');
322
+ } else {
323
+ const header = `# ERROR_LOG.md
324
+
325
+ > 执行错误日志 - 记录 flow-dev 阶段的所有错误
326
+
327
+ ---
328
+ `;
329
+ fs.writeFileSync(errorLogPath, header + entry, 'utf-8');
330
+ }
331
+ } catch {
332
+ // 忽略写入错误
333
+ }
334
+ }
335
+
336
+ /**
337
+ * 获取下一个错误编号
338
+ */
339
+ function getNextErrorNumber(errorLogPath: string): number {
340
+ if (!fs.existsSync(errorLogPath)) return 1;
341
+
342
+ try {
343
+ const content = fs.readFileSync(errorLogPath, 'utf-8');
344
+ const matches = content.match(/E(\d{3}):/g);
345
+ if (!matches || matches.length === 0) return 1;
346
+
347
+ const numbers = matches.map(m => parseInt(m.slice(1, 4), 10));
348
+ return Math.max(...numbers) + 1;
349
+ } catch {
350
+ return 1;
351
+ }
352
+ }
353
+
354
+ /**
355
+ * 检测当前需求 ID
356
+ */
357
+ function detectReqId(repoRoot: string): string | null {
358
+ // 1. 从环境变量获取
359
+ const envReqId = process.env.CURRENT_REQ_ID;
360
+ if (envReqId) return envReqId;
361
+
362
+ // 2. 从 .current-task 文件获取
363
+ const currentTaskPath = path.join(repoRoot, '.current-task');
364
+ if (fs.existsSync(currentTaskPath)) {
365
+ try {
366
+ const content = fs.readFileSync(currentTaskPath, 'utf-8').trim();
367
+ const match = content.match(/REQ-\d+/);
368
+ if (match) return match[0];
369
+ } catch {
370
+ // 忽略
371
+ }
372
+ }
373
+
374
+ // 3. 从 Git 分支名获取
375
+ try {
376
+ const branch = execSync('git rev-parse --abbrev-ref HEAD', {
377
+ cwd: repoRoot,
378
+ encoding: 'utf-8',
379
+ timeout: 5000
380
+ }).trim();
381
+ const match = branch.match(/REQ-\d+/);
382
+ if (match) return match[0];
383
+ } catch {
384
+ // 忽略
385
+ }
386
+
387
+ return null;
388
+ }
389
+
390
+ /**
391
+ * 调用 common.sh 中的 mark_teammate_task_complete 函数
392
+ */
393
+ function markTeammateTaskComplete(
394
+ repoRoot: string,
395
+ reqId: string,
396
+ teammateId: string,
397
+ taskId: string
398
+ ): boolean {
399
+ const commonShPath = path.join(repoRoot, SCRIPTS_DIR, 'common.sh');
400
+ if (!fs.existsSync(commonShPath)) {
401
+ return false;
402
+ }
403
+
404
+ try {
405
+ const script = `
406
+ source "${commonShPath}"
407
+ mark_teammate_task_complete "${repoRoot}" "${reqId}" "${teammateId}" "${taskId}"
408
+ `;
409
+ execSync(`bash -c '${script}'`, {
410
+ cwd: repoRoot,
411
+ timeout: 10000,
412
+ stdio: ['pipe', 'pipe', 'pipe']
413
+ });
414
+ return true;
415
+ } catch {
416
+ return false;
417
+ }
418
+ }
419
+
420
+ /**
421
+ * 检查是否触发阶段转换
422
+ */
423
+ function checkPhaseTransition(
424
+ repoRoot: string,
425
+ reqId: string
426
+ ): string[] {
427
+ const nextActions: string[] = [];
428
+
429
+ // 读取 orchestration_status.json
430
+ const statusPath = path.join(repoRoot, 'devflow', 'requirements', reqId, 'orchestration_status.json');
431
+ if (!fs.existsSync(statusPath)) {
432
+ return nextActions;
433
+ }
434
+
435
+ try {
436
+ const content = fs.readFileSync(statusPath, 'utf-8');
437
+ const status = JSON.parse(content);
438
+
439
+ // 检查是否所有任务都已完成
440
+ const team = status.team;
441
+ if (!team) return nextActions;
442
+
443
+ const allTasksCompleted = team.teammates.every(
444
+ (t: { completedTasks?: string[]; currentTask?: string | null }) =>
445
+ !t.currentTask && (t.completedTasks?.length ?? 0) > 0
446
+ );
447
+
448
+ if (allTasksCompleted) {
449
+ nextActions.push('All tasks completed. Consider running /flow:quality');
450
+ }
451
+
452
+ // 检查是否有空闲的 teammate 可以分配新任务
453
+ const idleTeammates = team.teammates.filter(
454
+ (t: { status: string }) => t.status === 'idle'
455
+ );
456
+ if (idleTeammates.length > 0) {
457
+ nextActions.push(`${idleTeammates.length} teammate(s) idle. Check for unassigned tasks.`);
458
+ }
459
+ } catch {
460
+ // 忽略解析错误
461
+ }
462
+
463
+ return nextActions;
464
+ }
465
+
466
+ // =============================================================================
467
+ // 主函数
468
+ // =============================================================================
469
+
470
+ function main(): void {
471
+ let inputData: TaskCompletedInput;
472
+
473
+ try {
474
+ const stdin = fs.readFileSync(0, 'utf-8');
475
+ inputData = JSON.parse(stdin);
476
+ } catch {
477
+ // 无法解析输入,接受完成
478
+ const output: TaskCompletedOutput = {
479
+ decision: 'accept',
480
+ reason: 'Unable to parse input. Accepting task completion.'
481
+ };
482
+ console.log(JSON.stringify(output, null, 0));
483
+ process.exit(0);
484
+ }
485
+
486
+ const hookEvent = inputData.hook_event_name || '';
487
+
488
+ // 只处理 TaskCompleted 事件
489
+ if (hookEvent !== 'TaskCompleted') {
490
+ const output: TaskCompletedOutput = {
491
+ decision: 'accept',
492
+ reason: `Ignoring non-TaskCompleted event: ${hookEvent}`
493
+ };
494
+ console.log(JSON.stringify(output, null, 0));
495
+ process.exit(0);
496
+ }
497
+
498
+ const cwd = inputData.cwd || process.cwd();
499
+ const taskId = inputData.task_id;
500
+ const taskSubject = inputData.task_subject;
501
+ const completedBy = inputData.completed_by;
502
+
503
+ // 查找仓库根目录
504
+ const repoRoot = findRepoRoot(cwd);
505
+ if (!repoRoot) {
506
+ const output: TaskCompletedOutput = {
507
+ decision: 'accept',
508
+ reason: 'Not in a git repository. Accepting task completion.'
509
+ };
510
+ console.log(JSON.stringify(output, null, 0));
511
+ process.exit(0);
512
+ }
513
+
514
+ // 加载配置
515
+ const config = loadConfig(repoRoot);
516
+ const verifyCommands = getVerifyCommands(config);
517
+ const blockOnFailure = getBlockOnFailure(config);
518
+
519
+ // 执行验证命令
520
+ const { passed, failures } = runVerifyCommands(repoRoot, verifyCommands);
521
+
522
+ // 检测需求 ID
523
+ const reqId = detectReqId(repoRoot);
524
+
525
+ if (passed) {
526
+ // 所有验证通过
527
+ const nextActions: string[] = [];
528
+
529
+ // 更新 Team 状态
530
+ if (reqId && completedBy) {
531
+ const updated = markTeammateTaskComplete(repoRoot, reqId, completedBy, taskId);
532
+ if (updated) {
533
+ nextActions.push(`Updated team state for ${completedBy}`);
534
+ }
535
+
536
+ // 检查阶段转换
537
+ const phaseActions = checkPhaseTransition(repoRoot, reqId);
538
+ nextActions.push(...phaseActions);
539
+ }
540
+
541
+ const output: TaskCompletedOutput = {
542
+ decision: 'accept',
543
+ reason: `Task ${taskId} completed successfully. All verification passed.`,
544
+ next_actions: nextActions.length > 0 ? nextActions : undefined
545
+ };
546
+ console.log(JSON.stringify(output, null, 0));
547
+ process.exit(0);
548
+ }
549
+
550
+ // 验证失败
551
+ const failureDetails = formatFailures(failures);
552
+
553
+ // 记录错误到 ERROR_LOG.md
554
+ if (reqId) {
555
+ recordErrorToLog(repoRoot, taskId, taskSubject, failures);
556
+ }
557
+
558
+ if (blockOnFailure) {
559
+ // 阻止任务完成
560
+ const output: TaskCompletedOutput = {
561
+ decision: 'reject',
562
+ reason: `Task ${taskId} verification failed:\n\n${failureDetails}\n\nPlease fix the issues before marking the task as complete.`,
563
+ next_actions: ['Fix verification errors', 'Re-run task completion']
564
+ };
565
+ console.log(JSON.stringify(output, null, 0));
566
+ process.exit(0);
567
+ }
568
+
569
+ // 不阻止,但记录警告
570
+ const nextActions: string[] = ['Warning: Verification failed but block_on_failure is false'];
571
+
572
+ // 更新 Team 状态
573
+ if (reqId && completedBy) {
574
+ const updated = markTeammateTaskComplete(repoRoot, reqId, completedBy, taskId);
575
+ if (updated) {
576
+ nextActions.push(`Updated team state for ${completedBy}`);
577
+ }
578
+
579
+ // 检查阶段转换
580
+ const phaseActions = checkPhaseTransition(repoRoot, reqId);
581
+ nextActions.push(...phaseActions);
582
+ }
583
+
584
+ const output: TaskCompletedOutput = {
585
+ decision: 'accept',
586
+ reason: `Task ${taskId} accepted with warnings. Verification failed:\n\n${failureDetails}`,
587
+ next_actions: nextActions
588
+ };
589
+ console.log(JSON.stringify(output, null, 0));
590
+ process.exit(0);
591
+ }
592
+
593
+ main();