@chongyan/autospec 1.0.1

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 (243) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +472 -0
  3. package/README.md +476 -0
  4. package/bin/autospec.js +3 -0
  5. package/knowledge/README.md +144 -0
  6. package/knowledge/checklists/code.md +182 -0
  7. package/knowledge/checklists/design.md +196 -0
  8. package/knowledge/checklists/release.md +70 -0
  9. package/knowledge/checklists/requirement.md +169 -0
  10. package/knowledge/checklists/test.md +46 -0
  11. package/knowledge/config/README.en.md +44 -0
  12. package/knowledge/config/README.md +44 -0
  13. package/knowledge/config/role-composition.yaml +98 -0
  14. package/knowledge/config/role-extensions.yaml +140 -0
  15. package/knowledge/config/skill-compositions.yaml +142 -0
  16. package/knowledge/config/team-stage.yaml +95 -0
  17. package/knowledge/config/team-tasks.yaml +139 -0
  18. package/knowledge/config/team-triggers.yaml +198 -0
  19. package/knowledge/config/validation-patterns.yaml +137 -0
  20. package/knowledge/domain/README.md +115 -0
  21. package/knowledge/domain/flows/README.md +194 -0
  22. package/knowledge/domain/glossary.md +143 -0
  23. package/knowledge/domain/rules.md +138 -0
  24. package/knowledge/environment/README.en.md +36 -0
  25. package/knowledge/environment/README.md +87 -0
  26. package/knowledge/environment/component-knowledge.md +316 -0
  27. package/knowledge/environment/detection-patterns.yaml +502 -0
  28. package/knowledge/environment/middleware-knowledge.md +237 -0
  29. package/knowledge/environment/template-registry.md +321 -0
  30. package/knowledge/guides/domain-driven-design.md +345 -0
  31. package/knowledge/guides/knowledge-management.md +369 -0
  32. package/knowledge/guides/requirement-engineering.md +329 -0
  33. package/knowledge/guides/stages/ai-effect-evaluator.md +93 -0
  34. package/knowledge/guides/stages/code-implementer.md +205 -0
  35. package/knowledge/guides/stages/code-reviewer.md +111 -0
  36. package/knowledge/guides/stages/consistency-checker.md +177 -0
  37. package/knowledge/guides/stages/design-planner.md +401 -0
  38. package/knowledge/guides/stages/design-reviewer.md +83 -0
  39. package/knowledge/guides/stages/integration-test-runner.md +105 -0
  40. package/knowledge/guides/stages/release-checker.md +205 -0
  41. package/knowledge/guides/stages/requirement-analyzer.md +195 -0
  42. package/knowledge/guides/stages/requirement-reviewer.md +83 -0
  43. package/knowledge/guides/stages/security-reviewer.md +89 -0
  44. package/knowledge/guides/stages/test-context-analyzer.md +250 -0
  45. package/knowledge/guides/stages/test-generator.md +241 -0
  46. package/knowledge/guides/stages/test-planner.md +183 -0
  47. package/knowledge/guides/stages/test-reviewer.md +76 -0
  48. package/knowledge/guides/stages/unit-test-runner.md +83 -0
  49. package/knowledge/guides/support/ai-agent-analyzer.md +362 -0
  50. package/knowledge/guides/support/ai-anomaly-analyzer.md +213 -0
  51. package/knowledge/guides/support/ai-artifact-evaluator.md +192 -0
  52. package/knowledge/guides/support/ai-capability-analyzer.md +193 -0
  53. package/knowledge/guides/support/ai-component-analyzer.md +169 -0
  54. package/knowledge/guides/support/ai-data-validator.md +276 -0
  55. package/knowledge/guides/support/ai-evaluation-planner.md +374 -0
  56. package/knowledge/guides/support/ai-path-evaluator.md +274 -0
  57. package/knowledge/guides/support/ai-pipeline-evaluator.md +219 -0
  58. package/knowledge/guides/support/ai-rag-analyzer.md +339 -0
  59. package/knowledge/guides/support/ai-task-assessor.md +418 -0
  60. package/knowledge/guides/support/ai-test-diagnostics.md +133 -0
  61. package/knowledge/guides/support/complexity-assessor.md +268 -0
  62. package/knowledge/guides/support/component-discovery.md +183 -0
  63. package/knowledge/guides/support/environment-scanner.md +207 -0
  64. package/knowledge/guides/support/environment-validator.md +207 -0
  65. package/knowledge/guides/support/knowledge-generator.md +234 -0
  66. package/knowledge/guides/support/methodology-extractor.md +55 -0
  67. package/knowledge/guides/support/pipeline-protocol.md +438 -0
  68. package/knowledge/guides/support/practice-logger.md +359 -0
  69. package/knowledge/guides/support/scope-inference.md +174 -0
  70. package/knowledge/guides/support/skill-distiller.md +91 -0
  71. package/knowledge/guides/support/skill-updater.md +45 -0
  72. package/knowledge/guides/support/skill-validator.md +72 -0
  73. package/knowledge/guides/support/team-orchestrator.md +323 -0
  74. package/knowledge/guides/support/tech-stack-analyzer.md +139 -0
  75. package/knowledge/guides/support/test-runner.md +254 -0
  76. package/knowledge/guides/system-design.md +352 -0
  77. package/knowledge/organization/ai-native-team.md +318 -0
  78. package/knowledge/organization/team-metrics.md +228 -0
  79. package/knowledge/principles/constitution.md +134 -0
  80. package/knowledge/principles/core-principles.md +368 -0
  81. package/knowledge/principles/design-philosophy.md +877 -0
  82. package/knowledge/principles/evolution.md +553 -0
  83. package/knowledge/process/01-requirement.md +113 -0
  84. package/knowledge/process/02-design.md +123 -0
  85. package/knowledge/process/03-implementation.md +90 -0
  86. package/knowledge/process/04-review.md +80 -0
  87. package/knowledge/process/05-testing.md +90 -0
  88. package/knowledge/process/06-delivery.md +88 -0
  89. package/knowledge/process/README.en.md +38 -0
  90. package/knowledge/process/README.md +48 -0
  91. package/knowledge/process/ai-sdlc.md +475 -0
  92. package/knowledge/process/overview.md +319 -0
  93. package/knowledge/standards/code-review.md +876 -0
  94. package/knowledge/standards/coding-style.md +940 -0
  95. package/knowledge/standards/data-consistency.md +1085 -0
  96. package/knowledge/standards/document-versioning.md +210 -0
  97. package/knowledge/standards/risk-detection.md +186 -0
  98. package/knowledge/templates/ai-evaluation.md +150 -0
  99. package/knowledge/templates/api-design.md +117 -0
  100. package/knowledge/templates/database-design.md +132 -0
  101. package/knowledge/templates/domain-driven-design.md +321 -0
  102. package/knowledge/templates/product-proposal.md +201 -0
  103. package/knowledge/templates/system-design.md +227 -0
  104. package/knowledge/templates/task-breakdown.md +107 -0
  105. package/knowledge/templates/test-case.md +170 -0
  106. package/package.json +53 -0
  107. package/plugins/.claude-plugin/plugin.json +134 -0
  108. package/plugins/agents/roles/ai-engineer.md +129 -0
  109. package/plugins/agents/roles/backend-engineer.md +165 -0
  110. package/plugins/agents/roles/ceo.md +94 -0
  111. package/plugins/agents/roles/data-engineer.md +135 -0
  112. package/plugins/agents/roles/devops-engineer.md +181 -0
  113. package/plugins/agents/roles/frontend-engineer.md +129 -0
  114. package/plugins/agents/roles/product-owner.md +98 -0
  115. package/plugins/agents/roles/quality-engineer.md +129 -0
  116. package/plugins/agents/roles/security-engineer.md +180 -0
  117. package/plugins/agents/roles/tech-lead.md +97 -0
  118. package/plugins/agents/support/blind-comparator.md +88 -0
  119. package/plugins/agents/support/consistency-checker.md +103 -0
  120. package/plugins/agents/support/failure-diagnostician.md +141 -0
  121. package/plugins/agents/support/independent-reviewer.md +80 -0
  122. package/plugins/agents/support/safety-auditor.md +121 -0
  123. package/plugins/agents/support/skill-benchmarker.md +86 -0
  124. package/plugins/agents/support/skill-forger.md +105 -0
  125. package/plugins/agents/support/stage-gate-evaluator.md +121 -0
  126. package/plugins/agents/support/test-coverage-reviewer.md +73 -0
  127. package/plugins/benchmarks/templates/README.md +44 -0
  128. package/plugins/benchmarks/templates/commands/explore-template.yaml +48 -0
  129. package/plugins/benchmarks/templates/pipeline/agile-template.yaml +84 -0
  130. package/plugins/benchmarks/templates/pipeline/waterfall-template.yaml +106 -0
  131. package/plugins/benchmarks/templates/skills/requirement-analyzer-template.yaml +48 -0
  132. package/plugins/commands/README.en.md +96 -0
  133. package/plugins/commands/README.md +96 -0
  134. package/plugins/commands/apply.md +191 -0
  135. package/plugins/commands/archive.md +76 -0
  136. package/plugins/commands/env-export.md +79 -0
  137. package/plugins/commands/env-sync.md +640 -0
  138. package/plugins/commands/env-template.md +223 -0
  139. package/plugins/commands/env-update.md +264 -0
  140. package/plugins/commands/env-validate.md +176 -0
  141. package/plugins/commands/env.md +79 -0
  142. package/plugins/commands/explore.md +76 -0
  143. package/plugins/commands/field-evolve.md +536 -0
  144. package/plugins/commands/memory.md +249 -0
  145. package/plugins/commands/project-evolve.md +821 -0
  146. package/plugins/commands/propose.md +93 -0
  147. package/plugins/commands/review.md +140 -0
  148. package/plugins/commands/run.md +224 -0
  149. package/plugins/commands/status.md +62 -0
  150. package/plugins/commands/validate.md +108 -0
  151. package/plugins/hooks/README.en.md +56 -0
  152. package/plugins/hooks/README.md +56 -0
  153. package/plugins/hooks/ai-project-guard.js +329 -0
  154. package/plugins/hooks/artifact-evaluation-hook.js +237 -0
  155. package/plugins/hooks/constitution-guard.js +211 -0
  156. package/plugins/hooks/environment-autocommit.js +264 -0
  157. package/plugins/hooks/environment-manager.js +778 -0
  158. package/plugins/hooks/execution-tracker.js +354 -0
  159. package/plugins/hooks/frozen-zone-guard.js +140 -0
  160. package/plugins/hooks/layer1-validator.js +423 -0
  161. package/plugins/hooks/lib/artifact-evaluator.js +414 -0
  162. package/plugins/hooks/lib/benchmarks/change-detector.js +390 -0
  163. package/plugins/hooks/lib/benchmarks/evaluator.js +605 -0
  164. package/plugins/hooks/lib/benchmarks/integration-example.js +169 -0
  165. package/plugins/hooks/lib/data-and-ai-detector.js +275 -0
  166. package/plugins/hooks/lib/detection-pattern-loader.js +865 -0
  167. package/plugins/hooks/lib/directory-discovery.js +395 -0
  168. package/plugins/hooks/lib/environment-config-loader.js +341 -0
  169. package/plugins/hooks/lib/environment-detector.js +553 -0
  170. package/plugins/hooks/lib/environment-evolver.js +564 -0
  171. package/plugins/hooks/lib/environment-registry.js +813 -0
  172. package/plugins/hooks/lib/execution-path.js +427 -0
  173. package/plugins/hooks/lib/hook-error-recorder.js +245 -0
  174. package/plugins/hooks/lib/hook-logger.js +538 -0
  175. package/plugins/hooks/lib/hook-runner.js +97 -0
  176. package/plugins/hooks/lib/hook-runner.sh +44 -0
  177. package/plugins/hooks/lib/hook-state-manager.js +480 -0
  178. package/plugins/hooks/lib/memory-extractor.js +377 -0
  179. package/plugins/hooks/lib/memory-manager.js +673 -0
  180. package/plugins/hooks/lib/metrics-analyzer.js +489 -0
  181. package/plugins/hooks/lib/project-evolution/auto-fixer.js +511 -0
  182. package/plugins/hooks/lib/project-evolution/memory-manager.js +346 -0
  183. package/plugins/hooks/lib/project-evolution/pattern-detector.js +476 -0
  184. package/plugins/hooks/lib/project-evolution/semantic-indexer.js +480 -0
  185. package/plugins/hooks/lib/project-structure-detector.js +326 -0
  186. package/plugins/hooks/lib/rollback-tracker.js +346 -0
  187. package/plugins/hooks/lib/source-code-scanner.js +596 -0
  188. package/plugins/hooks/lib/technology-stack-detector.js +374 -0
  189. package/plugins/hooks/lib/test-failure-analyzer.js +375 -0
  190. package/plugins/hooks/lib/test-failure-fixer.js +268 -0
  191. package/plugins/hooks/lib/trace-context.js +277 -0
  192. package/plugins/hooks/lib/validation-patterns.js +415 -0
  193. package/plugins/hooks/memory-sync.js +171 -0
  194. package/plugins/hooks/pipeline-observer.js +413 -0
  195. package/plugins/hooks/scope-sentinel.js +204 -0
  196. package/plugins/hooks/trace-initialization.js +169 -0
  197. package/plugins/memory/templates/code-quality.yaml +149 -0
  198. package/plugins/memory/templates/multi-system.yaml +155 -0
  199. package/plugins/memory/templates/team-habits.yaml +119 -0
  200. package/plugins/memory/templates/testing.yaml +121 -0
  201. package/plugins/skills/README.en.md +47 -0
  202. package/plugins/skills/README.md +104 -0
  203. package/plugins/skills/benchmark-executor/README.md +93 -0
  204. package/plugins/skills/benchmark-executor/SKILL.md +647 -0
  205. package/plugins/skills/benchmark-generator/SKILL.md +349 -0
  206. package/plugins/skills/delivery-stage/SKILL.md +203 -0
  207. package/plugins/skills/design-stage/SKILL.md +216 -0
  208. package/plugins/skills/evolution-process/SKILL.md +291 -0
  209. package/plugins/skills/exploration-phase/SKILL.md +133 -0
  210. package/plugins/skills/implementation-stage/SKILL.md +179 -0
  211. package/plugins/skills/layer1-validation/SKILL.md +79 -0
  212. package/plugins/skills/pending-dashboard/SKILL.md +109 -0
  213. package/plugins/skills/project-evolution/SKILL.md +847 -0
  214. package/plugins/skills/requirement-stage/SKILL.md +183 -0
  215. package/plugins/skills/skill-forge/SKILL.md +223 -0
  216. package/plugins/skills/skill-forge/references/description-guide.md +92 -0
  217. package/plugins/skills/skill-forge/references/quality-rubric.md +104 -0
  218. package/plugins/skills/skill-forge/references/skill-template.md +106 -0
  219. package/plugins/skills/startup-guard/SKILL.md +38 -0
  220. package/plugins/skills/testing-stage/SKILL.md +195 -0
  221. package/scripts/cli/global-init.js +288 -0
  222. package/scripts/cli/global.js +324 -0
  223. package/scripts/cli/index.js +55 -0
  224. package/scripts/cli/init.js +382 -0
  225. package/scripts/cli/list.js +69 -0
  226. package/scripts/cli/org.js +340 -0
  227. package/scripts/cli/update.js +44 -0
  228. package/scripts/config/commands.config.js +145 -0
  229. package/scripts/config/hooks.config.js +197 -0
  230. package/scripts/evolution/evolution-router.js +273 -0
  231. package/scripts/evolution/evolution-signal-collector.js +307 -0
  232. package/scripts/evolution/knowledge-loader.js +346 -0
  233. package/scripts/evolution/marketplace.js +317 -0
  234. package/scripts/evolution/version-manager.js +371 -0
  235. package/scripts/install/agents.js +106 -0
  236. package/scripts/install/commands.js +133 -0
  237. package/scripts/install/constants.js +424 -0
  238. package/scripts/install/hook-logger.js +536 -0
  239. package/scripts/install/hooks.js +110 -0
  240. package/scripts/install/index.js +39 -0
  241. package/scripts/install/skills.js +95 -0
  242. package/scripts/postinstall.js +25 -0
  243. package/scripts/state.js +376 -0
@@ -0,0 +1,375 @@
1
+ /**
2
+ * Failure Analyzer - 分析失败原因、分类错误
3
+ *
4
+ * 从 src/core/test/test-failure-analyzer.js 迁移
5
+ * 此文件是 hooks 模块的一部分,可独立运行于目标项目中。
6
+ */
7
+
8
+ import { execSync } from 'child_process';
9
+
10
+ /**
11
+ * 错误分类
12
+ */
13
+ export const ERROR_TYPES = {
14
+ SYNTAX: 'syntax', // 语法错误
15
+ IMPORT: 'import', // 导入错误
16
+ REFERENCE: 'reference', // 引用错误
17
+ TYPE: 'type', // 类型错误
18
+ LOGIC: 'logic', // 逻辑错误
19
+ TEST: 'test', // 测试失败
20
+ RUNTIME: 'runtime', // 运行时错误
21
+ UNKNOWN: 'unknown' // 未知错误
22
+ };
23
+
24
+ /**
25
+ * 错误严重级别
26
+ */
27
+ export const SEVERITY = {
28
+ LOW: 'low', // 低:格式、拼写
29
+ MEDIUM: 'medium', // 中:逻辑、边界
30
+ HIGH: 'high', // 高:安全、性能
31
+ CRITICAL: 'critical' // 严重:数据丢失、安全漏洞
32
+ };
33
+
34
+ /**
35
+ * 错误分类规则
36
+ */
37
+ const ERROR_PATTERNS = [
38
+ {
39
+ type: ERROR_TYPES.SYNTAX,
40
+ patterns: [
41
+ /SyntaxError/,
42
+ /unexpected token/,
43
+ /unexpected character/,
44
+ /unterminated string/,
45
+ /missing (?:semicolon|parentheses|brackets|brace)/
46
+ ],
47
+ severity: SEVERITY.HIGH,
48
+ autoFixable: true
49
+ },
50
+ {
51
+ type: ERROR_TYPES.IMPORT,
52
+ patterns: [
53
+ /Cannot find module/,
54
+ /Cannot resolve/,
55
+ /module not found/,
56
+ /import.*failed/
57
+ ],
58
+ severity: SEVERITY.HIGH,
59
+ autoFixable: true
60
+ },
61
+ {
62
+ type: ERROR_TYPES.REFERENCE,
63
+ patterns: [
64
+ /is not defined/,
65
+ /is not a function/,
66
+ /is not an (?:object|function)/,
67
+ /undefined (?:is not|has no)/
68
+ ],
69
+ severity: SEVERITY.HIGH,
70
+ autoFixable: false
71
+ },
72
+ {
73
+ type: ERROR_TYPES.TYPE,
74
+ patterns: [
75
+ /TypeError/,
76
+ /is not a type/,
77
+ /type mismatch/
78
+ ],
79
+ severity: SEVERITY.HIGH,
80
+ autoFixable: false
81
+ },
82
+ {
83
+ type: ERROR_TYPES.RUNTIME,
84
+ patterns: [
85
+ /RuntimeError/,
86
+ /Error:/,
87
+ /Exception:/
88
+ ],
89
+ severity: SEVERITY.MEDIUM,
90
+ autoFixable: false
91
+ },
92
+ {
93
+ type: ERROR_TYPES.TEST,
94
+ patterns: [
95
+ /expect.*toBe/,
96
+ /expect.*toEqual/,
97
+ /AssertionError/,
98
+ /FAIL.*test/,
99
+ /×/
100
+ ],
101
+ severity: SEVERITY.MEDIUM,
102
+ autoFixable: false
103
+ }
104
+ ];
105
+
106
+ /**
107
+ * 分析测试失败
108
+ * @param {Object} testResult - 测试结果
109
+ * @returns {Object} 分析结果
110
+ */
111
+ export function analyzeFailures(testResult) {
112
+ const { errors = [], files = [] } = testResult;
113
+
114
+ if (errors.length === 0) {
115
+ return {
116
+ hasErrors: false,
117
+ errorCount: 0,
118
+ classifiedErrors: [],
119
+ severity: null,
120
+ summary: 'No errors found',
121
+ suggestions: []
122
+ };
123
+ }
124
+
125
+ // 分类错误
126
+ const classifiedErrors = errors.map(error => classifyError(error));
127
+
128
+ // 计算总体严重级别
129
+ const severity = calculateOverallSeverity(classifiedErrors);
130
+
131
+ // 按类型分组
132
+ const byType = groupByType(classifiedErrors);
133
+
134
+ // 按文件分组
135
+ const byFile = groupByFile(classifiedErrors);
136
+
137
+ // 生成修复建议
138
+ const suggestions = generateSuggestions(classifiedErrors);
139
+
140
+ return {
141
+ hasErrors: true,
142
+ errorCount: errors.length,
143
+ classifiedErrors,
144
+ severity,
145
+ byType,
146
+ byFile,
147
+ suggestions,
148
+ summary: `${errors.length} errors found (${severity} severity)`
149
+ };
150
+ }
151
+
152
+ /**
153
+ * 分类单个错误
154
+ */
155
+ function classifyError(error) {
156
+ const message = error.message || error.toString();
157
+
158
+ for (const rule of ERROR_PATTERNS) {
159
+ for (const pattern of rule.patterns) {
160
+ if (pattern.test(message)) {
161
+ return {
162
+ ...error,
163
+ type: rule.type,
164
+ severity: rule.severity,
165
+ autoFixable: rule.autoFixable,
166
+ originalMessage: message
167
+ };
168
+ }
169
+ }
170
+ }
171
+
172
+ // 未匹配到任何规则
173
+ return {
174
+ ...error,
175
+ type: ERROR_TYPES.UNKNOWN,
176
+ severity: SEVERITY.MEDIUM,
177
+ autoFixable: false,
178
+ originalMessage: message
179
+ };
180
+ }
181
+
182
+ /**
183
+ * 计算总体严重级别
184
+ */
185
+ function calculateOverallSeverity(errors) {
186
+ if (errors.some(e => e.severity === SEVERITY.CRITICAL)) {
187
+ return SEVERITY.CRITICAL;
188
+ }
189
+ if (errors.some(e => e.severity === SEVERITY.HIGH)) {
190
+ return SEVERITY.HIGH;
191
+ }
192
+ if (errors.some(e => e.severity === SEVERITY.MEDIUM)) {
193
+ return SEVERITY.MEDIUM;
194
+ }
195
+ return SEVERITY.LOW;
196
+ }
197
+
198
+ /**
199
+ * 按类型分组
200
+ */
201
+ function groupByType(errors) {
202
+ const groups = {};
203
+
204
+ for (const error of errors) {
205
+ if (!groups[error.type]) {
206
+ groups[error.type] = [];
207
+ }
208
+ groups[error.type].push(error);
209
+ }
210
+
211
+ return groups;
212
+ }
213
+
214
+ /**
215
+ * 按文件分组
216
+ */
217
+ function groupByFile(errors) {
218
+ const groups = {};
219
+
220
+ for (const error of errors) {
221
+ const file = error.file || 'unknown';
222
+ if (!groups[file]) {
223
+ groups[file] = [];
224
+ }
225
+ groups[file].push(error);
226
+ }
227
+
228
+ return groups;
229
+ }
230
+
231
+ /**
232
+ * 生成修复建议
233
+ */
234
+ function generateSuggestions(errors) {
235
+ const suggestions = [];
236
+
237
+ for (const error of errors) {
238
+ const suggestion = generateSingleSuggestion(error);
239
+ if (suggestion) {
240
+ suggestions.push(suggestion);
241
+ }
242
+ }
243
+
244
+ return suggestions;
245
+ }
246
+
247
+ /**
248
+ * 生成单个错误的修复建议
249
+ */
250
+ function generateSingleSuggestion(error) {
251
+ const { type, autoFixable, file, originalMessage } = error;
252
+
253
+ const base = {
254
+ file,
255
+ errorType: type,
256
+ autoFixable,
257
+ message: originalMessage
258
+ };
259
+
260
+ switch (type) {
261
+ case ERROR_TYPES.SYNTAX:
262
+ return {
263
+ ...base,
264
+ action: 'fix_syntax',
265
+ description: '语法错误,需要修复代码语法',
266
+ fixable: autoFixable,
267
+ fixCommand: file ? `node --check ${file}` : null
268
+ };
269
+
270
+ case ERROR_TYPES.IMPORT:
271
+ return {
272
+ ...base,
273
+ action: 'fix_import',
274
+ description: '导入错误,检查模块路径和依赖安装',
275
+ fixable: autoFixable,
276
+ fixCommand: 'npm install'
277
+ };
278
+
279
+ case ERROR_TYPES.REFERENCE:
280
+ return {
281
+ ...base,
282
+ action: 'review_code',
283
+ description: '引用错误,需要检查变量/函数定义',
284
+ fixable: false,
285
+ fixCommand: null
286
+ };
287
+
288
+ case ERROR_TYPES.TYPE:
289
+ return {
290
+ ...base,
291
+ action: 'review_type',
292
+ description: '类型错误,需要检查类型定义',
293
+ fixable: false,
294
+ fixCommand: null
295
+ };
296
+
297
+ case ERROR_TYPES.TEST:
298
+ return {
299
+ ...base,
300
+ action: 'fix_test',
301
+ description: '测试失败,需要修复测试用例或代码逻辑',
302
+ fixable: false,
303
+ fixCommand: null
304
+ };
305
+
306
+ case ERROR_TYPES.RUNTIME:
307
+ return {
308
+ ...base,
309
+ action: 'debug_runtime',
310
+ description: '运行时错误,需要调试',
311
+ fixable: false,
312
+ fixCommand: null
313
+ };
314
+
315
+ default:
316
+ return {
317
+ ...base,
318
+ action: 'manual_review',
319
+ description: '需要人工审查',
320
+ fixable: false,
321
+ fixCommand: null
322
+ };
323
+ }
324
+ }
325
+
326
+ /**
327
+ * 查找根本原因
328
+ */
329
+ export function findRootCause(testFailures) {
330
+ // 简化实现:返回最可能的根本原因
331
+ const errors = testFailures.classifiedErrors || [];
332
+
333
+ // 统计错误类型
334
+ const typeCount = {};
335
+ for (const error of errors) {
336
+ typeCount[error.type] = (typeCount[error.type] || 0) + 1;
337
+ }
338
+
339
+ // 找到最常见的错误类型
340
+ let mostCommonType = null;
341
+ let maxCount = 0;
342
+ for (const [type, count] of Object.entries(typeCount)) {
343
+ if (count > maxCount) {
344
+ maxCount = count;
345
+ mostCommonType = type;
346
+ }
347
+ }
348
+
349
+ return {
350
+ likelyRootCause: mostCommonType,
351
+ confidence: maxCount / errors.length,
352
+ typeCount
353
+ };
354
+ }
355
+
356
+ /**
357
+ * 判断是否需要人工确认
358
+ */
359
+ export function requiresHumanConfirmation(analysis) {
360
+ // 关键错误需要人工确认
361
+ const criticalTypes = [ERROR_TYPES.REFERENCE, ERROR_TYPES.TYPE, ERROR_TYPES.RUNTIME];
362
+
363
+ for (const error of analysis.classifiedErrors) {
364
+ if (criticalTypes.includes(error.type) && !error.autoFixable) {
365
+ return true;
366
+ }
367
+ }
368
+
369
+ // 高严重级别也需要确认
370
+ if (analysis.severity === SEVERITY.CRITICAL || analysis.severity === SEVERITY.HIGH) {
371
+ return true;
372
+ }
373
+
374
+ return false;
375
+ }
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Auto Fixer - 自动修复、确认机制
3
+ *
4
+ * 从 src/core/test/test-failure-fixer.js 迁移
5
+ * 此文件是 hooks 模块的一部分,可独立运行于目标项目中。
6
+ */
7
+
8
+ import fs from 'fs';
9
+ import { execSync } from 'child_process';
10
+ import { requiresHumanConfirmation } from './test-failure-analyzer.js';
11
+
12
+ /**
13
+ * 修复动作类型
14
+ */
15
+ export const FIX_ACTIONS = {
16
+ FIX_SYNTAX: 'fix_syntax',
17
+ FIX_IMPORT: 'fix_import',
18
+ REVIEW_CODE: 'review_code',
19
+ REVIEW_TYPE: 'review_type',
20
+ FIX_TEST: 'fix_test',
21
+ DEBUG_RUNTIME: 'debug_runtime',
22
+ MANUAL_REVIEW: 'manual_review'
23
+ };
24
+
25
+ /**
26
+ * 尝试自动修复
27
+ * @param {Object} analysis - 分析结果
28
+ * @param {Object} options - 选项
29
+ * @returns {Promise<Object>} 修复结果
30
+ */
31
+ export async function attemptFix(analysis, options = {}) {
32
+ const { dryRun = false, interactive = false } = options;
33
+
34
+ const results = {
35
+ fixed: [],
36
+ failed: [],
37
+ skipped: [],
38
+ requiresConfirmation: []
39
+ };
40
+
41
+ // 检查是否需要人工确认
42
+ if (requiresHumanConfirmation(analysis)) {
43
+ return {
44
+ ...results,
45
+ requiresConfirmation: analysis.classifiedErrors.filter(e => !e.autoFixable),
46
+ needsHumanConfirmation: true
47
+ };
48
+ }
49
+
50
+ // 逐个处理可修复的错误
51
+ for (const suggestion of analysis.suggestions) {
52
+ if (!suggestion.fixable) {
53
+ results.skipped.push(suggestion);
54
+ continue;
55
+ }
56
+
57
+ if (dryRun) {
58
+ results.fixed.push({
59
+ ...suggestion,
60
+ dryRun: true
61
+ });
62
+ continue;
63
+ }
64
+
65
+ try {
66
+ const fixResult = await applyFix(suggestion);
67
+ if (fixResult.success) {
68
+ results.fixed.push(fixResult);
69
+ } else {
70
+ results.failed.push(fixResult);
71
+ }
72
+ } catch (e) {
73
+ results.failed.push({
74
+ ...suggestion,
75
+ error: e.message
76
+ });
77
+ }
78
+ }
79
+
80
+ return results;
81
+ }
82
+
83
+ /**
84
+ * 应用单个修复
85
+ */
86
+ async function applyFix(suggestion) {
87
+ const { action, file, message } = suggestion;
88
+
89
+ switch (action) {
90
+ case FIX_ACTIONS.FIX_SYNTAX:
91
+ return fixSyntaxError(file, message);
92
+
93
+ case FIX_ACTIONS.FIX_IMPORT:
94
+ return fixImportError(file, message);
95
+
96
+ default:
97
+ return {
98
+ success: false,
99
+ error: `Unknown action: ${action}`,
100
+ suggestion
101
+ };
102
+ }
103
+ }
104
+
105
+ /**
106
+ * 修复语法错误
107
+ */
108
+ function fixSyntaxError(file, message) {
109
+ if (!file) {
110
+ return { success: false, error: 'No file specified' };
111
+ }
112
+
113
+ // 常见语法错误修复
114
+ let content = fs.readFileSync(file, 'utf-8');
115
+ let fixed = false;
116
+
117
+ // 修复:缺少分号
118
+ if (message.includes('missing semicolon')) {
119
+ const lines = content.split('\n');
120
+ for (let i = 0; i < lines.length; i++) {
121
+ const line = lines[i];
122
+ // 检查是否是可能缺少分号的行
123
+ if (line.trim() && !line.trim().endsWith(';') && !line.trim().endsWith('{') && !line.trim().endsWith('}')) {
124
+ // 简单修复:添加分号
125
+ lines[i] = line + ';';
126
+ fixed = true;
127
+ break;
128
+ }
129
+ }
130
+ if (fixed) {
131
+ content = lines.join('\n');
132
+ }
133
+ }
134
+
135
+ // 修复:缺少引号
136
+ if (message.includes('unterminated string')) {
137
+ // 尝试修复未闭合的字符串
138
+ const lines = content.split('\n');
139
+ for (let i = 0; i < lines.length; i++) {
140
+ const line = lines[i];
141
+ const singleQuotes = (line.match(/'/g) || []).length;
142
+ const doubleQuotes = (line.match(/"/g) || []).length;
143
+
144
+ if (singleQuotes % 2 !== 0 || doubleQuotes % 2 !== 0) {
145
+ // 简单处理:移除有问题的行
146
+ lines[i] = '// ' + line;
147
+ fixed = true;
148
+ break;
149
+ }
150
+ }
151
+ if (fixed) {
152
+ content = lines.join('\n');
153
+ }
154
+ }
155
+
156
+ if (fixed) {
157
+ fs.writeFileSync(file, content, 'utf-8');
158
+ return {
159
+ success: true,
160
+ file,
161
+ action: 'fix_syntax',
162
+ description: 'Applied syntax fix'
163
+ };
164
+ }
165
+
166
+ return {
167
+ success: false,
168
+ file,
169
+ action: 'fix_syntax',
170
+ error: 'Could not auto-fix syntax error'
171
+ };
172
+ }
173
+
174
+ /**
175
+ * 修复导入错误
176
+ */
177
+ function fixImportError(file, message) {
178
+ // 提取缺失的模块名
179
+ const match = message.match(/Cannot find module ['"]([^'"]+)['"]/);
180
+ if (!match) {
181
+ return { success: false, error: 'Could not parse module name' };
182
+ }
183
+
184
+ const moduleName = match[1];
185
+
186
+ // 尝试安装缺失的模块
187
+ try {
188
+ execSync(`npm install ${moduleName}`, {
189
+ stdio: 'pipe',
190
+ cwd: process.cwd()
191
+ });
192
+
193
+ return {
194
+ success: true,
195
+ moduleName,
196
+ action: 'fix_import',
197
+ description: `Installed missing module: ${moduleName}`
198
+ };
199
+ } catch (e) {
200
+ return {
201
+ success: false,
202
+ moduleName,
203
+ action: 'fix_import',
204
+ error: `Failed to install module: ${e.message}`
205
+ };
206
+ }
207
+ }
208
+
209
+ /**
210
+ * 检查是否需要人工确认
211
+ */
212
+ export function checkConfirmationNeeded(analysis) {
213
+ return requiresHumanConfirmation(analysis);
214
+ }
215
+
216
+ /**
217
+ * 生成修复报告
218
+ */
219
+ export function generateFixReport(fixResult) {
220
+ const {
221
+ fixed = [],
222
+ failed = [],
223
+ skipped = [],
224
+ requiresConfirmation = [],
225
+ needsHumanConfirmation
226
+ } = fixResult;
227
+
228
+ return {
229
+ summary: {
230
+ total: fixed.length + failed.length + skipped.length,
231
+ fixed: fixed.length,
232
+ failed: failed.length,
233
+ skipped: skipped.length,
234
+ needsConfirmation: requiresConfirmation.length
235
+ },
236
+ needsHumanConfirmation,
237
+ fixed,
238
+ failed,
239
+ skipped,
240
+ requiresConfirmation
241
+ };
242
+ }
243
+
244
+ /**
245
+ * 验证修复结果
246
+ * @param {Object} fixResult - 修复结果
247
+ * @param {Function} testRunner - 测试运行函数
248
+ * @returns {Promise<Object>} 验证结果
249
+ */
250
+ export async function verifyFix(fixResult, testRunner) {
251
+ const { fixed } = fixResult;
252
+
253
+ if (fixed.length === 0) {
254
+ return {
255
+ verified: false,
256
+ reason: 'No fixes to verify'
257
+ };
258
+ }
259
+
260
+ // 运行测试验证修复
261
+ const testResult = await testRunner();
262
+
263
+ return {
264
+ verified: testResult.passed,
265
+ testResult,
266
+ fixedCount: fixed.length
267
+ };
268
+ }