@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.
- package/LICENSE +21 -0
- package/README.en.md +472 -0
- package/README.md +476 -0
- package/bin/autospec.js +3 -0
- package/knowledge/README.md +144 -0
- package/knowledge/checklists/code.md +182 -0
- package/knowledge/checklists/design.md +196 -0
- package/knowledge/checklists/release.md +70 -0
- package/knowledge/checklists/requirement.md +169 -0
- package/knowledge/checklists/test.md +46 -0
- package/knowledge/config/README.en.md +44 -0
- package/knowledge/config/README.md +44 -0
- package/knowledge/config/role-composition.yaml +98 -0
- package/knowledge/config/role-extensions.yaml +140 -0
- package/knowledge/config/skill-compositions.yaml +142 -0
- package/knowledge/config/team-stage.yaml +95 -0
- package/knowledge/config/team-tasks.yaml +139 -0
- package/knowledge/config/team-triggers.yaml +198 -0
- package/knowledge/config/validation-patterns.yaml +137 -0
- package/knowledge/domain/README.md +115 -0
- package/knowledge/domain/flows/README.md +194 -0
- package/knowledge/domain/glossary.md +143 -0
- package/knowledge/domain/rules.md +138 -0
- package/knowledge/environment/README.en.md +36 -0
- package/knowledge/environment/README.md +87 -0
- package/knowledge/environment/component-knowledge.md +316 -0
- package/knowledge/environment/detection-patterns.yaml +502 -0
- package/knowledge/environment/middleware-knowledge.md +237 -0
- package/knowledge/environment/template-registry.md +321 -0
- package/knowledge/guides/domain-driven-design.md +345 -0
- package/knowledge/guides/knowledge-management.md +369 -0
- package/knowledge/guides/requirement-engineering.md +329 -0
- package/knowledge/guides/stages/ai-effect-evaluator.md +93 -0
- package/knowledge/guides/stages/code-implementer.md +205 -0
- package/knowledge/guides/stages/code-reviewer.md +111 -0
- package/knowledge/guides/stages/consistency-checker.md +177 -0
- package/knowledge/guides/stages/design-planner.md +401 -0
- package/knowledge/guides/stages/design-reviewer.md +83 -0
- package/knowledge/guides/stages/integration-test-runner.md +105 -0
- package/knowledge/guides/stages/release-checker.md +205 -0
- package/knowledge/guides/stages/requirement-analyzer.md +195 -0
- package/knowledge/guides/stages/requirement-reviewer.md +83 -0
- package/knowledge/guides/stages/security-reviewer.md +89 -0
- package/knowledge/guides/stages/test-context-analyzer.md +250 -0
- package/knowledge/guides/stages/test-generator.md +241 -0
- package/knowledge/guides/stages/test-planner.md +183 -0
- package/knowledge/guides/stages/test-reviewer.md +76 -0
- package/knowledge/guides/stages/unit-test-runner.md +83 -0
- package/knowledge/guides/support/ai-agent-analyzer.md +362 -0
- package/knowledge/guides/support/ai-anomaly-analyzer.md +213 -0
- package/knowledge/guides/support/ai-artifact-evaluator.md +192 -0
- package/knowledge/guides/support/ai-capability-analyzer.md +193 -0
- package/knowledge/guides/support/ai-component-analyzer.md +169 -0
- package/knowledge/guides/support/ai-data-validator.md +276 -0
- package/knowledge/guides/support/ai-evaluation-planner.md +374 -0
- package/knowledge/guides/support/ai-path-evaluator.md +274 -0
- package/knowledge/guides/support/ai-pipeline-evaluator.md +219 -0
- package/knowledge/guides/support/ai-rag-analyzer.md +339 -0
- package/knowledge/guides/support/ai-task-assessor.md +418 -0
- package/knowledge/guides/support/ai-test-diagnostics.md +133 -0
- package/knowledge/guides/support/complexity-assessor.md +268 -0
- package/knowledge/guides/support/component-discovery.md +183 -0
- package/knowledge/guides/support/environment-scanner.md +207 -0
- package/knowledge/guides/support/environment-validator.md +207 -0
- package/knowledge/guides/support/knowledge-generator.md +234 -0
- package/knowledge/guides/support/methodology-extractor.md +55 -0
- package/knowledge/guides/support/pipeline-protocol.md +438 -0
- package/knowledge/guides/support/practice-logger.md +359 -0
- package/knowledge/guides/support/scope-inference.md +174 -0
- package/knowledge/guides/support/skill-distiller.md +91 -0
- package/knowledge/guides/support/skill-updater.md +45 -0
- package/knowledge/guides/support/skill-validator.md +72 -0
- package/knowledge/guides/support/team-orchestrator.md +323 -0
- package/knowledge/guides/support/tech-stack-analyzer.md +139 -0
- package/knowledge/guides/support/test-runner.md +254 -0
- package/knowledge/guides/system-design.md +352 -0
- package/knowledge/organization/ai-native-team.md +318 -0
- package/knowledge/organization/team-metrics.md +228 -0
- package/knowledge/principles/constitution.md +134 -0
- package/knowledge/principles/core-principles.md +368 -0
- package/knowledge/principles/design-philosophy.md +877 -0
- package/knowledge/principles/evolution.md +553 -0
- package/knowledge/process/01-requirement.md +113 -0
- package/knowledge/process/02-design.md +123 -0
- package/knowledge/process/03-implementation.md +90 -0
- package/knowledge/process/04-review.md +80 -0
- package/knowledge/process/05-testing.md +90 -0
- package/knowledge/process/06-delivery.md +88 -0
- package/knowledge/process/README.en.md +38 -0
- package/knowledge/process/README.md +48 -0
- package/knowledge/process/ai-sdlc.md +475 -0
- package/knowledge/process/overview.md +319 -0
- package/knowledge/standards/code-review.md +876 -0
- package/knowledge/standards/coding-style.md +940 -0
- package/knowledge/standards/data-consistency.md +1085 -0
- package/knowledge/standards/document-versioning.md +210 -0
- package/knowledge/standards/risk-detection.md +186 -0
- package/knowledge/templates/ai-evaluation.md +150 -0
- package/knowledge/templates/api-design.md +117 -0
- package/knowledge/templates/database-design.md +132 -0
- package/knowledge/templates/domain-driven-design.md +321 -0
- package/knowledge/templates/product-proposal.md +201 -0
- package/knowledge/templates/system-design.md +227 -0
- package/knowledge/templates/task-breakdown.md +107 -0
- package/knowledge/templates/test-case.md +170 -0
- package/package.json +53 -0
- package/plugins/.claude-plugin/plugin.json +134 -0
- package/plugins/agents/roles/ai-engineer.md +129 -0
- package/plugins/agents/roles/backend-engineer.md +165 -0
- package/plugins/agents/roles/ceo.md +94 -0
- package/plugins/agents/roles/data-engineer.md +135 -0
- package/plugins/agents/roles/devops-engineer.md +181 -0
- package/plugins/agents/roles/frontend-engineer.md +129 -0
- package/plugins/agents/roles/product-owner.md +98 -0
- package/plugins/agents/roles/quality-engineer.md +129 -0
- package/plugins/agents/roles/security-engineer.md +180 -0
- package/plugins/agents/roles/tech-lead.md +97 -0
- package/plugins/agents/support/blind-comparator.md +88 -0
- package/plugins/agents/support/consistency-checker.md +103 -0
- package/plugins/agents/support/failure-diagnostician.md +141 -0
- package/plugins/agents/support/independent-reviewer.md +80 -0
- package/plugins/agents/support/safety-auditor.md +121 -0
- package/plugins/agents/support/skill-benchmarker.md +86 -0
- package/plugins/agents/support/skill-forger.md +105 -0
- package/plugins/agents/support/stage-gate-evaluator.md +121 -0
- package/plugins/agents/support/test-coverage-reviewer.md +73 -0
- package/plugins/benchmarks/templates/README.md +44 -0
- package/plugins/benchmarks/templates/commands/explore-template.yaml +48 -0
- package/plugins/benchmarks/templates/pipeline/agile-template.yaml +84 -0
- package/plugins/benchmarks/templates/pipeline/waterfall-template.yaml +106 -0
- package/plugins/benchmarks/templates/skills/requirement-analyzer-template.yaml +48 -0
- package/plugins/commands/README.en.md +96 -0
- package/plugins/commands/README.md +96 -0
- package/plugins/commands/apply.md +191 -0
- package/plugins/commands/archive.md +76 -0
- package/plugins/commands/env-export.md +79 -0
- package/plugins/commands/env-sync.md +640 -0
- package/plugins/commands/env-template.md +223 -0
- package/plugins/commands/env-update.md +264 -0
- package/plugins/commands/env-validate.md +176 -0
- package/plugins/commands/env.md +79 -0
- package/plugins/commands/explore.md +76 -0
- package/plugins/commands/field-evolve.md +536 -0
- package/plugins/commands/memory.md +249 -0
- package/plugins/commands/project-evolve.md +821 -0
- package/plugins/commands/propose.md +93 -0
- package/plugins/commands/review.md +140 -0
- package/plugins/commands/run.md +224 -0
- package/plugins/commands/status.md +62 -0
- package/plugins/commands/validate.md +108 -0
- package/plugins/hooks/README.en.md +56 -0
- package/plugins/hooks/README.md +56 -0
- package/plugins/hooks/ai-project-guard.js +329 -0
- package/plugins/hooks/artifact-evaluation-hook.js +237 -0
- package/plugins/hooks/constitution-guard.js +211 -0
- package/plugins/hooks/environment-autocommit.js +264 -0
- package/plugins/hooks/environment-manager.js +778 -0
- package/plugins/hooks/execution-tracker.js +354 -0
- package/plugins/hooks/frozen-zone-guard.js +140 -0
- package/plugins/hooks/layer1-validator.js +423 -0
- package/plugins/hooks/lib/artifact-evaluator.js +414 -0
- package/plugins/hooks/lib/benchmarks/change-detector.js +390 -0
- package/plugins/hooks/lib/benchmarks/evaluator.js +605 -0
- package/plugins/hooks/lib/benchmarks/integration-example.js +169 -0
- package/plugins/hooks/lib/data-and-ai-detector.js +275 -0
- package/plugins/hooks/lib/detection-pattern-loader.js +865 -0
- package/plugins/hooks/lib/directory-discovery.js +395 -0
- package/plugins/hooks/lib/environment-config-loader.js +341 -0
- package/plugins/hooks/lib/environment-detector.js +553 -0
- package/plugins/hooks/lib/environment-evolver.js +564 -0
- package/plugins/hooks/lib/environment-registry.js +813 -0
- package/plugins/hooks/lib/execution-path.js +427 -0
- package/plugins/hooks/lib/hook-error-recorder.js +245 -0
- package/plugins/hooks/lib/hook-logger.js +538 -0
- package/plugins/hooks/lib/hook-runner.js +97 -0
- package/plugins/hooks/lib/hook-runner.sh +44 -0
- package/plugins/hooks/lib/hook-state-manager.js +480 -0
- package/plugins/hooks/lib/memory-extractor.js +377 -0
- package/plugins/hooks/lib/memory-manager.js +673 -0
- package/plugins/hooks/lib/metrics-analyzer.js +489 -0
- package/plugins/hooks/lib/project-evolution/auto-fixer.js +511 -0
- package/plugins/hooks/lib/project-evolution/memory-manager.js +346 -0
- package/plugins/hooks/lib/project-evolution/pattern-detector.js +476 -0
- package/plugins/hooks/lib/project-evolution/semantic-indexer.js +480 -0
- package/plugins/hooks/lib/project-structure-detector.js +326 -0
- package/plugins/hooks/lib/rollback-tracker.js +346 -0
- package/plugins/hooks/lib/source-code-scanner.js +596 -0
- package/plugins/hooks/lib/technology-stack-detector.js +374 -0
- package/plugins/hooks/lib/test-failure-analyzer.js +375 -0
- package/plugins/hooks/lib/test-failure-fixer.js +268 -0
- package/plugins/hooks/lib/trace-context.js +277 -0
- package/plugins/hooks/lib/validation-patterns.js +415 -0
- package/plugins/hooks/memory-sync.js +171 -0
- package/plugins/hooks/pipeline-observer.js +413 -0
- package/plugins/hooks/scope-sentinel.js +204 -0
- package/plugins/hooks/trace-initialization.js +169 -0
- package/plugins/memory/templates/code-quality.yaml +149 -0
- package/plugins/memory/templates/multi-system.yaml +155 -0
- package/plugins/memory/templates/team-habits.yaml +119 -0
- package/plugins/memory/templates/testing.yaml +121 -0
- package/plugins/skills/README.en.md +47 -0
- package/plugins/skills/README.md +104 -0
- package/plugins/skills/benchmark-executor/README.md +93 -0
- package/plugins/skills/benchmark-executor/SKILL.md +647 -0
- package/plugins/skills/benchmark-generator/SKILL.md +349 -0
- package/plugins/skills/delivery-stage/SKILL.md +203 -0
- package/plugins/skills/design-stage/SKILL.md +216 -0
- package/plugins/skills/evolution-process/SKILL.md +291 -0
- package/plugins/skills/exploration-phase/SKILL.md +133 -0
- package/plugins/skills/implementation-stage/SKILL.md +179 -0
- package/plugins/skills/layer1-validation/SKILL.md +79 -0
- package/plugins/skills/pending-dashboard/SKILL.md +109 -0
- package/plugins/skills/project-evolution/SKILL.md +847 -0
- package/plugins/skills/requirement-stage/SKILL.md +183 -0
- package/plugins/skills/skill-forge/SKILL.md +223 -0
- package/plugins/skills/skill-forge/references/description-guide.md +92 -0
- package/plugins/skills/skill-forge/references/quality-rubric.md +104 -0
- package/plugins/skills/skill-forge/references/skill-template.md +106 -0
- package/plugins/skills/startup-guard/SKILL.md +38 -0
- package/plugins/skills/testing-stage/SKILL.md +195 -0
- package/scripts/cli/global-init.js +288 -0
- package/scripts/cli/global.js +324 -0
- package/scripts/cli/index.js +55 -0
- package/scripts/cli/init.js +382 -0
- package/scripts/cli/list.js +69 -0
- package/scripts/cli/org.js +340 -0
- package/scripts/cli/update.js +44 -0
- package/scripts/config/commands.config.js +145 -0
- package/scripts/config/hooks.config.js +197 -0
- package/scripts/evolution/evolution-router.js +273 -0
- package/scripts/evolution/evolution-signal-collector.js +307 -0
- package/scripts/evolution/knowledge-loader.js +346 -0
- package/scripts/evolution/marketplace.js +317 -0
- package/scripts/evolution/version-manager.js +371 -0
- package/scripts/install/agents.js +106 -0
- package/scripts/install/commands.js +133 -0
- package/scripts/install/constants.js +424 -0
- package/scripts/install/hook-logger.js +536 -0
- package/scripts/install/hooks.js +110 -0
- package/scripts/install/index.js +39 -0
- package/scripts/install/skills.js +95 -0
- package/scripts/postinstall.js +25 -0
- package/scripts/state.js +376 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AutoSpec Hooks Utilities
|
|
3
|
+
* Shared utilities for all hook scripts
|
|
4
|
+
*
|
|
5
|
+
* This file is self-contained for hooks to work independently after being copied to target projects.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ============================================================
|
|
9
|
+
// Global error handlers (防止未捕获的错误导致 hook 静默失败)
|
|
10
|
+
// ============================================================
|
|
11
|
+
|
|
12
|
+
// 只注册一次
|
|
13
|
+
let globalHandlersRegistered = false;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 注册全局错误处理器
|
|
17
|
+
* 防止 uncaughtException 和 unhandledRejection 导致 hook 静默失败
|
|
18
|
+
*/
|
|
19
|
+
function registerGlobalErrorHandlers() {
|
|
20
|
+
if (globalHandlersRegistered) return;
|
|
21
|
+
globalHandlersRegistered = true;
|
|
22
|
+
|
|
23
|
+
process.on('uncaughtException', (err) => {
|
|
24
|
+
console.error('[AutoSpec Hook] Uncaught exception:', err.message);
|
|
25
|
+
if (process.env.VERBOSE || process.env.DEBUG) {
|
|
26
|
+
console.error(err.stack);
|
|
27
|
+
}
|
|
28
|
+
process.exit(0); // 允许操作继续,但至少记录了错误
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
32
|
+
const message = reason instanceof Error ? reason.message : String(reason);
|
|
33
|
+
console.error('[AutoSpec Hook] Unhandled rejection:', message);
|
|
34
|
+
if (process.env.VERBOSE || process.env.DEBUG && reason instanceof Error) {
|
|
35
|
+
console.error(reason.stack);
|
|
36
|
+
}
|
|
37
|
+
process.exit(0);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 自动注册全局错误处理器
|
|
42
|
+
registerGlobalErrorHandlers();
|
|
43
|
+
|
|
44
|
+
// ============================================================
|
|
45
|
+
// Logger utilities
|
|
46
|
+
// ============================================================
|
|
47
|
+
|
|
48
|
+
export const LOG_LEVELS = {
|
|
49
|
+
ERROR: 0,
|
|
50
|
+
WARN: 1,
|
|
51
|
+
INFO: 2,
|
|
52
|
+
DEBUG: 3
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
let currentLevel = LOG_LEVELS.INFO;
|
|
56
|
+
let isVerbose = false;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set log level
|
|
60
|
+
*/
|
|
61
|
+
export function setLogLevel(level) {
|
|
62
|
+
if (typeof level === 'string') {
|
|
63
|
+
currentLevel = LOG_LEVELS[level.toUpperCase()] ?? LOG_LEVELS.INFO;
|
|
64
|
+
} else {
|
|
65
|
+
currentLevel = level;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Enable verbose mode
|
|
71
|
+
*/
|
|
72
|
+
export function setVerbose(verbose) {
|
|
73
|
+
isVerbose = verbose;
|
|
74
|
+
if (verbose) {
|
|
75
|
+
currentLevel = LOG_LEVELS.DEBUG;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Check if verbose mode is enabled
|
|
81
|
+
*/
|
|
82
|
+
export function isVerboseMode() {
|
|
83
|
+
return isVerbose || currentLevel >= LOG_LEVELS.DEBUG;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Format log message with timestamp and level
|
|
88
|
+
*/
|
|
89
|
+
function formatMessage(level, message, meta = {}) {
|
|
90
|
+
const timestamp = new Date().toISOString();
|
|
91
|
+
const levelStr = Object.keys(LOG_LEVELS).find(k => LOG_LEVELS[k] === level) || 'UNKNOWN';
|
|
92
|
+
|
|
93
|
+
let output = `[${timestamp}] [${levelStr}] ${message}`;
|
|
94
|
+
|
|
95
|
+
if (Object.keys(meta).length > 0) {
|
|
96
|
+
output += '\n' + Object.entries(meta)
|
|
97
|
+
.map(([k, v]) => ` ${k}: ${typeof v === 'object' ? JSON.stringify(v) : v}`)
|
|
98
|
+
.join('\n');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return output;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Log error message
|
|
106
|
+
*/
|
|
107
|
+
export function error(message, meta = {}) {
|
|
108
|
+
if (currentLevel >= LOG_LEVELS.ERROR) {
|
|
109
|
+
console.error(formatMessage(LOG_LEVELS.ERROR, message, meta));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Log warning message
|
|
115
|
+
*/
|
|
116
|
+
export function warn(message, meta = {}) {
|
|
117
|
+
if (currentLevel >= LOG_LEVELS.WARN) {
|
|
118
|
+
console.warn(formatMessage(LOG_LEVELS.WARN, message, meta));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Log info message
|
|
124
|
+
* Note: Use console.error to avoid mixing with hook JSON output on stdout
|
|
125
|
+
*/
|
|
126
|
+
export function info(message, meta = {}) {
|
|
127
|
+
if (currentLevel >= LOG_LEVELS.INFO) {
|
|
128
|
+
console.error(formatMessage(LOG_LEVELS.INFO, message, meta));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Log debug message (only in verbose mode)
|
|
134
|
+
* Note: Use console.error to avoid mixing with hook JSON output on stdout
|
|
135
|
+
*/
|
|
136
|
+
export function debug(message, meta = {}) {
|
|
137
|
+
if (currentLevel >= LOG_LEVELS.DEBUG) {
|
|
138
|
+
console.error(formatMessage(LOG_LEVELS.DEBUG, message, meta));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Create a hook-specific logger
|
|
144
|
+
*/
|
|
145
|
+
export function createHookLogger(hookName) {
|
|
146
|
+
return {
|
|
147
|
+
error: (msg, meta) => error(`[${hookName}] ${msg}`, meta),
|
|
148
|
+
warn: (msg, meta) => warn(`[${hookName}] ${msg}`, meta),
|
|
149
|
+
info: (msg, meta) => info(`[${hookName}] ${msg}`, meta),
|
|
150
|
+
debug: (msg, meta) => debug(`[${hookName}] ${msg}`, meta)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ============================================================
|
|
155
|
+
// Hook error classification and security levels
|
|
156
|
+
// ============================================================
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Hook 错误类型枚举
|
|
160
|
+
*/
|
|
161
|
+
export const HookErrorType = {
|
|
162
|
+
// 可恢复错误 - hook 逻辑自身无法处理,但不影响操作
|
|
163
|
+
RECOVERABLE: {
|
|
164
|
+
code: 'RECOVERABLE',
|
|
165
|
+
severity: 'low',
|
|
166
|
+
failStrategy: 'open', // 允许操作继续
|
|
167
|
+
shouldAlert: false,
|
|
168
|
+
shouldLog: true
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
// 输入解析错误 - 输入数据格式问题
|
|
172
|
+
PARSE_ERROR: {
|
|
173
|
+
code: 'PARSE_ERROR',
|
|
174
|
+
severity: 'medium',
|
|
175
|
+
failStrategy: 'open', // 允许操作继续(无法解析则跳过检查)
|
|
176
|
+
shouldAlert: true,
|
|
177
|
+
shouldLog: true
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
// 权限错误 - 文件系统权限问题
|
|
181
|
+
PERMISSION_ERROR: {
|
|
182
|
+
code: 'PERMISSION_ERROR',
|
|
183
|
+
severity: 'high',
|
|
184
|
+
failStrategy: 'open', // 允许操作继续,但必须告警
|
|
185
|
+
shouldAlert: true,
|
|
186
|
+
shouldLog: true
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
// 运行时错误 - hook 内部逻辑错误
|
|
190
|
+
RUNTIME_ERROR: {
|
|
191
|
+
code: 'RUNTIME_ERROR',
|
|
192
|
+
severity: 'high',
|
|
193
|
+
failStrategy: 'context', // 根据 hook 类型决定
|
|
194
|
+
shouldAlert: true,
|
|
195
|
+
shouldLog: true
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
// 关键错误 - 必须阻止操作
|
|
199
|
+
CRITICAL_ERROR: {
|
|
200
|
+
code: 'CRITICAL_ERROR',
|
|
201
|
+
severity: 'critical',
|
|
202
|
+
failStrategy: 'closed', // 阻止操作
|
|
203
|
+
shouldAlert: true,
|
|
204
|
+
shouldLog: true
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 根据 hook 名称获取其安全级别
|
|
210
|
+
*/
|
|
211
|
+
export const HookSecurityLevel = {
|
|
212
|
+
'frozen-zone-guard': 'critical', // 关键安全 hook
|
|
213
|
+
'layer1-validator': 'critical', // 流程门禁 hook
|
|
214
|
+
'constitution-guard': 'high',
|
|
215
|
+
'scope-sentinel': 'medium',
|
|
216
|
+
'execution-tracker': 'low',
|
|
217
|
+
'pipeline-observer': 'low',
|
|
218
|
+
'trace-initialization': 'low',
|
|
219
|
+
'artifact-evaluation-hook': 'medium',
|
|
220
|
+
'environment-manager': 'medium',
|
|
221
|
+
'ai-project-guard': 'medium'
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 分类错误
|
|
226
|
+
* @param {Error} err - 错误对象
|
|
227
|
+
* @param {string} hookName - Hook 名称
|
|
228
|
+
* @returns {Object} 错误类型
|
|
229
|
+
*/
|
|
230
|
+
export function classifyError(err, hookName) {
|
|
231
|
+
// 权限错误
|
|
232
|
+
if (err.code === 'EACCES' ||
|
|
233
|
+
err.message?.includes('permission denied') ||
|
|
234
|
+
err.message?.includes('ENOENT')) {
|
|
235
|
+
return HookErrorType.PERMISSION_ERROR;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// 解析错误
|
|
239
|
+
if (err.message?.includes('JSON') ||
|
|
240
|
+
err.message?.includes('parse') ||
|
|
241
|
+
err.message?.includes('Unexpected token')) {
|
|
242
|
+
return HookErrorType.PARSE_ERROR;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// 根据 hook 安全级别和错误上下文判断
|
|
246
|
+
const securityLevel = HookSecurityLevel[hookName] || 'low';
|
|
247
|
+
|
|
248
|
+
// 关键 hook 的任何错误都视为严重
|
|
249
|
+
if (securityLevel === 'critical') {
|
|
250
|
+
return {
|
|
251
|
+
...HookErrorType.RUNTIME_ERROR,
|
|
252
|
+
failStrategy: 'closed' // 覆盖为 fail-closed
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return HookErrorType.RUNTIME_ERROR;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Handle hook errors gracefully
|
|
261
|
+
* 增强版错误处理:分类错误、记录日志、决定是否阻止操作
|
|
262
|
+
* @param {string} hookName - Hook 名称
|
|
263
|
+
* @param {Error} err - 错误对象
|
|
264
|
+
* @param {Object} context - 上下文信息(包含 eventName, projectRoot, input 等)
|
|
265
|
+
* @returns {Object} 输出对象,包含是否应该阻止操作
|
|
266
|
+
*/
|
|
267
|
+
export function handleHookError(hookName, err, context = {}) {
|
|
268
|
+
const errorType = classifyError(err, hookName);
|
|
269
|
+
const securityLevel = HookSecurityLevel[hookName] || 'low';
|
|
270
|
+
|
|
271
|
+
// 构建详细的错误信息
|
|
272
|
+
const errorInfo = {
|
|
273
|
+
hookName,
|
|
274
|
+
errorType: errorType.code,
|
|
275
|
+
errorMessage: err.message,
|
|
276
|
+
errorCode: err.code,
|
|
277
|
+
severity: errorType.severity,
|
|
278
|
+
timestamp: new Date().toISOString(),
|
|
279
|
+
context
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// 记录到控制台
|
|
283
|
+
const errorMeta = {
|
|
284
|
+
hook: hookName,
|
|
285
|
+
error: err.message,
|
|
286
|
+
code: err.code,
|
|
287
|
+
type: errorType.code,
|
|
288
|
+
severity: errorType.severity,
|
|
289
|
+
stack: isVerbose ? err.stack : undefined
|
|
290
|
+
};
|
|
291
|
+
error(`Hook execution failed`, errorMeta);
|
|
292
|
+
|
|
293
|
+
// 记录到 hook 错误日志(如果有 projectRoot)
|
|
294
|
+
if (context.projectRoot) {
|
|
295
|
+
try {
|
|
296
|
+
// 动态导入避免循环依赖
|
|
297
|
+
import('./hook-error-recorder.js').then(({ recordHookError }) => {
|
|
298
|
+
recordHookError(context.projectRoot, errorInfo);
|
|
299
|
+
}).catch(() => {
|
|
300
|
+
// 忽略导入错误
|
|
301
|
+
});
|
|
302
|
+
} catch {
|
|
303
|
+
// 忽略错误
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 决定是否阻止操作
|
|
308
|
+
let permissionDecision = 'allow';
|
|
309
|
+
let permissionDecisionReason = null;
|
|
310
|
+
|
|
311
|
+
if (errorType.failStrategy === 'closed' ||
|
|
312
|
+
(errorType.failStrategy === 'context' && securityLevel === 'critical')) {
|
|
313
|
+
permissionDecision = 'deny';
|
|
314
|
+
permissionDecisionReason =
|
|
315
|
+
`[AutoSpec] 🚨 HOOK ERROR - Operation Blocked\n` +
|
|
316
|
+
`Hook: ${hookName}\n` +
|
|
317
|
+
`Error Type: ${errorType.code}\n` +
|
|
318
|
+
`Reason: ${err.message}\n\n` +
|
|
319
|
+
`This is a critical hook that protects the project. ` +
|
|
320
|
+
`When it fails, the operation must be blocked for safety.\n` +
|
|
321
|
+
`Please check the error and try again.`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// 构建用户提示
|
|
325
|
+
let additionalContext = buildUserMessage(hookName, errorType, err);
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
hookSpecificOutput: {
|
|
329
|
+
hookEventName: context.eventName || 'Unknown',
|
|
330
|
+
permissionDecision,
|
|
331
|
+
permissionDecisionReason,
|
|
332
|
+
additionalContext
|
|
333
|
+
},
|
|
334
|
+
// 返回错误分类信息供调用者使用
|
|
335
|
+
errorType,
|
|
336
|
+
shouldBlock: permissionDecision === 'deny'
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* 构建用户提示信息
|
|
342
|
+
*/
|
|
343
|
+
function buildUserMessage(hookName, errorType, err) {
|
|
344
|
+
// 权限错误的特殊处理
|
|
345
|
+
if (errorType.code === 'PERMISSION_ERROR') {
|
|
346
|
+
return `[AutoSpec] ⚠️ 权限错误: ${hookName} 无法写入文件
|
|
347
|
+
|
|
348
|
+
可能原因:.autospec 或 .claude 目录由 root 创建
|
|
349
|
+
|
|
350
|
+
修复方法:运行以下命令修复权限
|
|
351
|
+
sudo chown -R $(whoami) .autospec .claude
|
|
352
|
+
|
|
353
|
+
或者重新初始化(不使用 sudo):
|
|
354
|
+
rm -rf .autospec .claude CLAUDE.md
|
|
355
|
+
autospec init`;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// 其他错误的通用提示
|
|
359
|
+
const severity = errorType.severity;
|
|
360
|
+
const icon = severity === 'critical' ? '🚨' :
|
|
361
|
+
severity === 'high' ? '⚠️' : 'ℹ️';
|
|
362
|
+
|
|
363
|
+
return `[AutoSpec] ${icon} Hook '${hookName}' encountered an error (${errorType.code})
|
|
364
|
+
|
|
365
|
+
Error: ${err.message}
|
|
366
|
+
|
|
367
|
+
${errorType.failStrategy === 'closed' ?
|
|
368
|
+
'This operation has been blocked for safety.' :
|
|
369
|
+
'The operation was allowed to continue. Use --verbose for details.'}`;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Safe JSON parse with logging
|
|
374
|
+
*/
|
|
375
|
+
export function safeJsonParse(str, defaultValue = null, context = '') {
|
|
376
|
+
try {
|
|
377
|
+
return JSON.parse(str);
|
|
378
|
+
} catch (err) {
|
|
379
|
+
debug(`JSON parse failed${context ? ` (${context})` : ''}`, {
|
|
380
|
+
error: err.message,
|
|
381
|
+
input: str?.slice(0, 200) // Log truncated input
|
|
382
|
+
});
|
|
383
|
+
return defaultValue;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ============================================================
|
|
388
|
+
// Common utilities
|
|
389
|
+
// ============================================================
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Read stdin safely with timeout
|
|
393
|
+
*/
|
|
394
|
+
export function readStdin(timeout = 1000) {
|
|
395
|
+
return new Promise((resolve) => {
|
|
396
|
+
let data = '';
|
|
397
|
+
process.stdin.setEncoding('utf8');
|
|
398
|
+
|
|
399
|
+
const timeoutId = setTimeout(() => {
|
|
400
|
+
cleanup();
|
|
401
|
+
resolve(data);
|
|
402
|
+
}, timeout);
|
|
403
|
+
|
|
404
|
+
function cleanup() {
|
|
405
|
+
clearTimeout(timeoutId);
|
|
406
|
+
process.stdin.removeListener('data', onData);
|
|
407
|
+
process.stdin.removeListener('end', onEnd);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function onData(chunk) {
|
|
411
|
+
data += chunk;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function onEnd() {
|
|
415
|
+
cleanup();
|
|
416
|
+
resolve(data);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
process.stdin.on('data', onData);
|
|
420
|
+
process.stdin.on('end', onEnd);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Find project root by searching upward for .autospec directory
|
|
426
|
+
*/
|
|
427
|
+
export function findProjectRoot(startDir) {
|
|
428
|
+
const fs = require('fs');
|
|
429
|
+
const path = require('path');
|
|
430
|
+
|
|
431
|
+
let dir = path.resolve(startDir);
|
|
432
|
+
const root = path.parse(dir).root;
|
|
433
|
+
|
|
434
|
+
while (dir !== root) {
|
|
435
|
+
if (fs.existsSync(path.join(dir, '.autospec'))) {
|
|
436
|
+
return dir;
|
|
437
|
+
}
|
|
438
|
+
dir = path.dirname(dir);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Async version of findProjectRoot
|
|
446
|
+
*/
|
|
447
|
+
export async function findProjectRootAsync(startDir) {
|
|
448
|
+
const { default: fs } = await import('fs');
|
|
449
|
+
const { default: path } = await import('path');
|
|
450
|
+
|
|
451
|
+
let dir = path.resolve(startDir);
|
|
452
|
+
const root = path.parse(dir).root;
|
|
453
|
+
|
|
454
|
+
while (dir !== root) {
|
|
455
|
+
try {
|
|
456
|
+
await fs.promises.access(path.join(dir, '.autospec'));
|
|
457
|
+
return dir;
|
|
458
|
+
} catch {
|
|
459
|
+
dir = path.dirname(dir);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Simple in-memory cache for hooks
|
|
468
|
+
*/
|
|
469
|
+
export class HookCache {
|
|
470
|
+
constructor(maxSize = 100) {
|
|
471
|
+
this.cache = new Map();
|
|
472
|
+
this.maxSize = maxSize;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
get(key) {
|
|
476
|
+
const item = this.cache.get(key);
|
|
477
|
+
if (item && Date.now() - item.timestamp < item.ttl) {
|
|
478
|
+
return item.value;
|
|
479
|
+
}
|
|
480
|
+
this.cache.delete(key);
|
|
481
|
+
return undefined;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
set(key, value, ttlMs = 60000) {
|
|
485
|
+
if (this.cache.size >= this.maxSize) {
|
|
486
|
+
const firstKey = this.cache.keys().next().value;
|
|
487
|
+
this.cache.delete(firstKey);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
this.cache.set(key, {
|
|
491
|
+
value,
|
|
492
|
+
timestamp: Date.now(),
|
|
493
|
+
ttl: ttlMs
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
clear() {
|
|
498
|
+
this.cache.clear();
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* 全局单例缓存实例
|
|
504
|
+
* 供所有 hook 统一使用,避免各自实例化
|
|
505
|
+
*/
|
|
506
|
+
export const globalHookCache = new HookCache(200);
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Check if path matches any pattern
|
|
510
|
+
*/
|
|
511
|
+
export function matchesAnyPattern(path, patterns) {
|
|
512
|
+
return patterns.some(pattern => pattern.test(path));
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Safe file existence check
|
|
517
|
+
*/
|
|
518
|
+
export async function fileExists(path) {
|
|
519
|
+
const { default: fs } = await import('fs');
|
|
520
|
+
try {
|
|
521
|
+
await fs.promises.access(path);
|
|
522
|
+
return true;
|
|
523
|
+
} catch {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Safe directory sync check
|
|
530
|
+
*/
|
|
531
|
+
export function directoryExistsSync(path) {
|
|
532
|
+
const fs = require('fs');
|
|
533
|
+
try {
|
|
534
|
+
return fs.existsSync(path) && fs.statSync(path).isDirectory();
|
|
535
|
+
} catch {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AutoSpec Hook Runner
|
|
5
|
+
*
|
|
6
|
+
* 通用 hook 启动器,解决子目录工作时相对路径无法正确解析的问题。
|
|
7
|
+
*
|
|
8
|
+
* 工作原理:
|
|
9
|
+
* 1. 从当前工作目录向上查找 .autospec 目录
|
|
10
|
+
* 2. 找到后,动态加载指定的 hook 脚本
|
|
11
|
+
* 3. 如果找不到,静默退出(可能是非 AutoSpec 项目)
|
|
12
|
+
*
|
|
13
|
+
* 使用方式:
|
|
14
|
+
* node .autospec/plugins/hooks/lib/hook-runner.js <hook-name>
|
|
15
|
+
*
|
|
16
|
+
* 例如:
|
|
17
|
+
* node .autospec/plugins/hooks/lib/hook-runner.js pipeline-observer
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import path from 'path';
|
|
21
|
+
import fs from 'fs';
|
|
22
|
+
import { pathToFileURL } from 'url';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 向上查找 .autospec 目录
|
|
26
|
+
* @param {string} startDir - 开始查找的目录
|
|
27
|
+
* @returns {string|null} - 找到的项目根目录,或 null
|
|
28
|
+
*/
|
|
29
|
+
function findProjectRoot(startDir) {
|
|
30
|
+
let dir = path.resolve(startDir);
|
|
31
|
+
const root = path.parse(dir).root;
|
|
32
|
+
|
|
33
|
+
while (dir !== root) {
|
|
34
|
+
if (fs.existsSync(path.join(dir, '.autospec'))) {
|
|
35
|
+
return dir;
|
|
36
|
+
}
|
|
37
|
+
dir = path.dirname(dir);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 主函数
|
|
45
|
+
*/
|
|
46
|
+
async function main() {
|
|
47
|
+
// 获取要运行的 hook 名称
|
|
48
|
+
const hookName = process.argv[2];
|
|
49
|
+
|
|
50
|
+
if (!hookName) {
|
|
51
|
+
console.error('[Hook Runner] Error: No hook name provided');
|
|
52
|
+
console.error('Usage: node hook-runner.js <hook-name>');
|
|
53
|
+
process.exit(0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 从当前工作目录向上查找 .autospec 目录
|
|
57
|
+
const cwd = process.cwd();
|
|
58
|
+
const projectRoot = findProjectRoot(cwd);
|
|
59
|
+
|
|
60
|
+
if (!projectRoot) {
|
|
61
|
+
// 没有找到 .autospec 目录,静默退出
|
|
62
|
+
if (process.env.DEBUG || process.env.VERBOSE) {
|
|
63
|
+
console.error(`[Hook Runner] No .autospec directory found from ${cwd}`);
|
|
64
|
+
}
|
|
65
|
+
process.exit(0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 构建 hook 脚本路径
|
|
69
|
+
const hookScriptPath = path.join(projectRoot, '.autospec', 'plugins', 'hooks', `${hookName}.js`);
|
|
70
|
+
|
|
71
|
+
if (!fs.existsSync(hookScriptPath)) {
|
|
72
|
+
console.error(`[Hook Runner] Error: Hook script not found: ${hookScriptPath}`);
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (process.env.DEBUG || process.env.VERBOSE) {
|
|
77
|
+
console.error(`[Hook Runner] Loading hook: ${hookName} from ${hookScriptPath}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
// 动态导入并执行 hook 脚本(ESM)
|
|
82
|
+
const hookUrl = pathToFileURL(hookScriptPath).href;
|
|
83
|
+
await import(hookUrl);
|
|
84
|
+
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error(`[Hook Runner] Error loading hook ${hookName}:`, err.message);
|
|
87
|
+
if (process.env.DEBUG || process.env.VERBOSE) {
|
|
88
|
+
console.error(err.stack);
|
|
89
|
+
}
|
|
90
|
+
process.exit(0);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
main().catch((err) => {
|
|
95
|
+
console.error('[Hook Runner] Unexpected error:', err.message);
|
|
96
|
+
process.exit(0);
|
|
97
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
#
|
|
3
|
+
# AutoSpec Hook Runner Shell Wrapper
|
|
4
|
+
#
|
|
5
|
+
# 此脚本解决子目录工作时相对路径无法正确解析的问题。
|
|
6
|
+
# 它会从当前目录向上查找 .autospec 目录,然后执行对应的 hook。
|
|
7
|
+
#
|
|
8
|
+
# 使用方式:
|
|
9
|
+
# .autospec/plugins/hooks/lib/hook-runner.sh <hook-name>
|
|
10
|
+
#
|
|
11
|
+
|
|
12
|
+
set -e
|
|
13
|
+
|
|
14
|
+
# 获取 hook 名称
|
|
15
|
+
HOOK_NAME="${1:-}"
|
|
16
|
+
|
|
17
|
+
if [ -z "$HOOK_NAME" ]; then
|
|
18
|
+
echo "[Hook Runner] Error: No hook name provided" >&2
|
|
19
|
+
echo "Usage: hook-runner.sh <hook-name>" >&2
|
|
20
|
+
exit 0
|
|
21
|
+
fi
|
|
22
|
+
|
|
23
|
+
# 向上查找 .autospec 目录
|
|
24
|
+
DIR="$(pwd)"
|
|
25
|
+
while [ "$DIR" != "/" ] && [ ! -d "$DIR/.autospec" ]; do
|
|
26
|
+
DIR="$(dirname "$DIR")"
|
|
27
|
+
done
|
|
28
|
+
|
|
29
|
+
# 检查是否找到
|
|
30
|
+
if [ ! -d "$DIR/.autospec" ]; then
|
|
31
|
+
# 没有找到 .autospec 目录,静默退出
|
|
32
|
+
exit 0
|
|
33
|
+
fi
|
|
34
|
+
|
|
35
|
+
# 构建 hook 脚本路径
|
|
36
|
+
HOOK_SCRIPT="$DIR/.autospec/plugins/hooks/lib/hook-runner.js"
|
|
37
|
+
|
|
38
|
+
if [ ! -f "$HOOK_SCRIPT" ]; then
|
|
39
|
+
echo "[Hook Runner] Error: Hook runner not found: $HOOK_SCRIPT" >&2
|
|
40
|
+
exit 0
|
|
41
|
+
fi
|
|
42
|
+
|
|
43
|
+
# 执行 hook
|
|
44
|
+
exec node "$HOOK_SCRIPT" "$HOOK_NAME"
|