@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,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evolution Signal Collector - 进化信号收集器
|
|
3
|
+
*
|
|
4
|
+
* 复用 memory-manager.js 的 sessions-index 统计进化信号
|
|
5
|
+
* 实现同类问题3次检测逻辑和5条 practice-log 触发逻辑
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
MEMORY_SCOPE,
|
|
10
|
+
MEMORY_FILES
|
|
11
|
+
} from '../install/constants.js';
|
|
12
|
+
|
|
13
|
+
// 尝试导入记忆模块
|
|
14
|
+
let memoryManager = null;
|
|
15
|
+
function getMemoryManager() {
|
|
16
|
+
if (memoryManager === null) {
|
|
17
|
+
try {
|
|
18
|
+
memoryManager = require('../../plugins/hooks/lib/memory-manager.js');
|
|
19
|
+
} catch (e) {
|
|
20
|
+
memoryManager = false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return memoryManager || null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 进化信号类型
|
|
28
|
+
*/
|
|
29
|
+
export const EVOLUTION_SIGNAL_TYPES = {
|
|
30
|
+
FLOW_COMPLETE: 'flow-complete', // 全流程完成
|
|
31
|
+
SAME_PROBLEM_3TIMES: 'same-problem-3x', // 同类问题出现3次
|
|
32
|
+
PRACTICE_LOG_5: 'practice-log-5', // 5条 practice-log
|
|
33
|
+
MANUAL_TRIGGER: 'manual-trigger', // 手动触发
|
|
34
|
+
HUMAN_FEEDBACK: 'human-feedback' // 人工反馈
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 收集进化信号
|
|
39
|
+
*
|
|
40
|
+
* @param {string} projectRoot - 项目根目录
|
|
41
|
+
* @returns {Object} 进化信号 { signals: [], shouldEvolve: boolean }
|
|
42
|
+
*/
|
|
43
|
+
export function collectEvolutionSignals(projectRoot) {
|
|
44
|
+
const signals = [];
|
|
45
|
+
const mm = getMemoryManager();
|
|
46
|
+
|
|
47
|
+
// 1. 检查全流程完成信号(通过 state.json)
|
|
48
|
+
try {
|
|
49
|
+
const statePath = `${projectRoot}/.autospec/runtime/state.json`;
|
|
50
|
+
const state = mm ? mm.getMemory(MEMORY_SCOPE.PROJECT, 'runtime/state.json', projectRoot) : null;
|
|
51
|
+
if (state && state.currentStage === '06' && state.status === 'completed') {
|
|
52
|
+
signals.push({
|
|
53
|
+
type: EVOLUTION_SIGNAL_TYPES.FLOW_COMPLETE,
|
|
54
|
+
timestamp: new Date().toISOString(),
|
|
55
|
+
detail: '全流程已完成'
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// 忽略错误
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 2. 检查同类问题出现3次(通过 sessions-index)
|
|
63
|
+
if (mm) {
|
|
64
|
+
try {
|
|
65
|
+
const sessionsIndex = mm.getSessionsIndex(projectRoot);
|
|
66
|
+
const problemCounts = countProblems(sessionsIndex);
|
|
67
|
+
|
|
68
|
+
for (const [problem, count] of Object.entries(problemCounts)) {
|
|
69
|
+
if (count >= 3) {
|
|
70
|
+
signals.push({
|
|
71
|
+
type: EVOLUTION_SIGNAL_TYPES.SAME_PROBLEM_3TIMES,
|
|
72
|
+
timestamp: new Date().toISOString(),
|
|
73
|
+
detail: `问题 "${problem}" 出现 ${count} 次`,
|
|
74
|
+
problem,
|
|
75
|
+
count
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} catch (e) {
|
|
80
|
+
// 忽略错误
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 3. 检查 practice-log 数量(通过 evolution 目录)
|
|
85
|
+
try {
|
|
86
|
+
const practiceLogCount = countPracticeLogs(projectRoot);
|
|
87
|
+
if (practiceLogCount >= 5) {
|
|
88
|
+
signals.push({
|
|
89
|
+
type: EVOLUTION_SIGNAL_TYPES.PRACTICE_LOG_5,
|
|
90
|
+
timestamp: new Date().toISOString(),
|
|
91
|
+
detail: `已有 ${practiceLogCount} 条 practice-log`,
|
|
92
|
+
count: practiceLogCount
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
} catch (e) {
|
|
96
|
+
// 忽略错误
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 判断是否应该触发进化
|
|
100
|
+
const shouldEvolve = signals.length > 0;
|
|
101
|
+
|
|
102
|
+
return { signals, shouldEvolve };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 统计同类问题出现次数
|
|
107
|
+
* @param {Object} sessionsIndex - 会话索引
|
|
108
|
+
* @returns {Object} 问题计数
|
|
109
|
+
*/
|
|
110
|
+
function countProblems(sessionsIndex) {
|
|
111
|
+
const problemCounts = {};
|
|
112
|
+
|
|
113
|
+
if (!sessionsIndex || !sessionsIndex.sessions) {
|
|
114
|
+
return problemCounts;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (const session of sessionsIndex.sessions) {
|
|
118
|
+
if (session.issues) {
|
|
119
|
+
for (const issue of session.issues) {
|
|
120
|
+
const key = issue.type || issue.category || 'unknown';
|
|
121
|
+
problemCounts[key] = (problemCounts[key] || 0) + 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return problemCounts;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 统计 practice-log 数量
|
|
131
|
+
* @param {string} projectRoot - 项目根目录
|
|
132
|
+
* @returns {number} practice-log 数量
|
|
133
|
+
*/
|
|
134
|
+
function countPracticeLogs(projectRoot) {
|
|
135
|
+
const mm = getMemoryManager();
|
|
136
|
+
if (!mm) {
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const evolutionDir = `${projectRoot}/.autospec/evolution`;
|
|
142
|
+
const practiceLogDir = `${evolutionDir}/practice-log`;
|
|
143
|
+
|
|
144
|
+
// 尝试从记忆模块读取
|
|
145
|
+
const practiceLogs = mm.getMemory(
|
|
146
|
+
MEMORY_SCOPE.PROJECT,
|
|
147
|
+
'evolution/practice-log/index.json',
|
|
148
|
+
projectRoot
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
if (practiceLogs && Array.isArray(practiceLogs.entries)) {
|
|
152
|
+
return practiceLogs.entries.length;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return 0;
|
|
156
|
+
} catch (e) {
|
|
157
|
+
return 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 记录进化信号
|
|
163
|
+
*
|
|
164
|
+
* @param {string} projectRoot - 项目根目录
|
|
165
|
+
* @param {string} signalType - 信号类型
|
|
166
|
+
* @param {Object} detail - 详细信息
|
|
167
|
+
* @returns {boolean} 是否成功
|
|
168
|
+
*/
|
|
169
|
+
export function recordEvolutionSignal(projectRoot, signalType, detail = {}) {
|
|
170
|
+
const mm = getMemoryManager();
|
|
171
|
+
if (!mm) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const signal = {
|
|
177
|
+
type: signalType,
|
|
178
|
+
timestamp: new Date().toISOString(),
|
|
179
|
+
...detail
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// 读取现有信号
|
|
183
|
+
const existingSignals = mm.getMemory(
|
|
184
|
+
MEMORY_SCOPE.PROJECT,
|
|
185
|
+
'evolution/signals.json',
|
|
186
|
+
projectRoot
|
|
187
|
+
) || { signals: [] };
|
|
188
|
+
|
|
189
|
+
existingSignals.signals = existingSignals.signals || [];
|
|
190
|
+
existingSignals.signals.push(signal);
|
|
191
|
+
|
|
192
|
+
// 保留最近 100 条
|
|
193
|
+
if (existingSignals.signals.length > 100) {
|
|
194
|
+
existingSignals.signals = existingSignals.signals.slice(-100);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return mm.updateMemory(
|
|
198
|
+
MEMORY_SCOPE.PROJECT,
|
|
199
|
+
'evolution/signals.json',
|
|
200
|
+
existingSignals,
|
|
201
|
+
projectRoot
|
|
202
|
+
);
|
|
203
|
+
} catch (e) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 获取进化信号历史
|
|
210
|
+
*
|
|
211
|
+
* @param {string} projectRoot - 项目根目录
|
|
212
|
+
* @param {number} limit - 返回数量限制
|
|
213
|
+
* @returns {Array} 信号历史
|
|
214
|
+
*/
|
|
215
|
+
export function getEvolutionSignalHistory(projectRoot, limit = 10) {
|
|
216
|
+
const mm = getMemoryManager();
|
|
217
|
+
if (!mm) {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
const signals = mm.getMemory(
|
|
223
|
+
MEMORY_SCOPE.PROJECT,
|
|
224
|
+
'evolution/signals.json',
|
|
225
|
+
projectRoot
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
if (signals && signals.signals) {
|
|
229
|
+
return signals.signals.slice(-limit);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return [];
|
|
233
|
+
} catch (e) {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 清除进化信号
|
|
240
|
+
*
|
|
241
|
+
* @param {string} projectRoot - 项目根目录
|
|
242
|
+
* @returns {boolean} 是否成功
|
|
243
|
+
*/
|
|
244
|
+
export function clearEvolutionSignals(projectRoot) {
|
|
245
|
+
const mm = getMemoryManager();
|
|
246
|
+
if (!mm) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
return mm.updateMemory(
|
|
252
|
+
MEMORY_SCOPE.PROJECT,
|
|
253
|
+
'evolution/signals.json',
|
|
254
|
+
{ signals: [] },
|
|
255
|
+
projectRoot
|
|
256
|
+
);
|
|
257
|
+
} catch (e) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 检查是否应该触发进化
|
|
264
|
+
*
|
|
265
|
+
* @param {string} projectRoot - 项目根目录
|
|
266
|
+
* @returns {Object} 检查结果 { shouldEvolve, reason, signals }
|
|
267
|
+
*/
|
|
268
|
+
export function shouldTriggerEvolution(projectRoot) {
|
|
269
|
+
const { signals, shouldEvolve } = collectEvolutionSignals(projectRoot);
|
|
270
|
+
|
|
271
|
+
if (!shouldEvolve) {
|
|
272
|
+
return {
|
|
273
|
+
shouldEvolve: false,
|
|
274
|
+
reason: '未达到进化触发条件',
|
|
275
|
+
signals: []
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// 确定触发原因
|
|
280
|
+
const signalTypes = signals.map(s => s.type);
|
|
281
|
+
let reason = '';
|
|
282
|
+
|
|
283
|
+
if (signalTypes.includes(EVOLUTION_SIGNAL_TYPES.FLOW_COMPLETE)) {
|
|
284
|
+
reason = '全流程已完成';
|
|
285
|
+
} else if (signalTypes.includes(EVOLUTION_SIGNAL_TYPES.SAME_PROBLEM_3TIMES)) {
|
|
286
|
+
reason = '同类问题出现3次';
|
|
287
|
+
} else if (signalTypes.includes(EVOLUTION_SIGNAL_TYPES.PRACTICE_LOG_5)) {
|
|
288
|
+
reason = '已有5条以上实践日志';
|
|
289
|
+
} else {
|
|
290
|
+
reason = '满足进化条件';
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
shouldEvolve: true,
|
|
295
|
+
reason,
|
|
296
|
+
signals
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export default {
|
|
301
|
+
EVOLUTION_SIGNAL_TYPES,
|
|
302
|
+
collectEvolutionSignals,
|
|
303
|
+
recordEvolutionSignal,
|
|
304
|
+
getEvolutionSignalHistory,
|
|
305
|
+
clearEvolutionSignals,
|
|
306
|
+
shouldTriggerEvolution
|
|
307
|
+
};
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knowledge Loader - 知识加载器
|
|
3
|
+
*
|
|
4
|
+
* 实现三级优先级加载:项目 > 用户全局 > 组织
|
|
5
|
+
* 集成记忆模块的全局路径和双模式逻辑
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import os from 'os';
|
|
11
|
+
import {
|
|
12
|
+
GLOBAL_AUTOSPEC_DIR,
|
|
13
|
+
GLOBAL_AUTOSPEC_SUBDIRS,
|
|
14
|
+
ORG_AUTOSPEC_DIR,
|
|
15
|
+
ORG_AUTOSPEC_SUBDIRS,
|
|
16
|
+
KNOWLEDGE_SCOPE,
|
|
17
|
+
KNOWLEDGE_ZONES
|
|
18
|
+
} from '../install/constants.js';
|
|
19
|
+
|
|
20
|
+
// 缓存
|
|
21
|
+
const knowledgeCache = new Map();
|
|
22
|
+
const CACHE_TTL = 5000; // 5秒缓存
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 获取全局知识库根目录
|
|
26
|
+
* @returns {string} 全局知识库根目录
|
|
27
|
+
*/
|
|
28
|
+
export function getGlobalKnowledgeRoot() {
|
|
29
|
+
return path.join(os.homedir(), GLOBAL_AUTOSPEC_DIR);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 获取组织知识库根目录
|
|
34
|
+
* @param {string} orgName - 组织名称
|
|
35
|
+
* @returns {string} 组织知识库根目录
|
|
36
|
+
*/
|
|
37
|
+
export function getOrgKnowledgeRoot(orgName) {
|
|
38
|
+
return path.join(os.homedir(), ORG_AUTOSPEC_DIR, orgName);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 获取项目知识库根目录
|
|
43
|
+
* @param {string} projectRoot - 项目根目录
|
|
44
|
+
* @returns {string} 项目知识库根目录
|
|
45
|
+
*/
|
|
46
|
+
export function getProjectKnowledgeRoot(projectRoot) {
|
|
47
|
+
return path.join(projectRoot, '.autospec');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 解析知识文件路径
|
|
52
|
+
* @param {string} scope - 作用域 (global|org|project)
|
|
53
|
+
* @param {string} type - 知识类型 (knowledge|skills|roles|environment)
|
|
54
|
+
* @param {string} name - 知识名称
|
|
55
|
+
* @param {Object} options - 选项
|
|
56
|
+
* @returns {string} 完整文件路径
|
|
57
|
+
*/
|
|
58
|
+
export function resolveKnowledgePath(scope, type, name, options = {}) {
|
|
59
|
+
const { projectRoot = '.', orgName = null } = options;
|
|
60
|
+
|
|
61
|
+
switch (scope) {
|
|
62
|
+
case KNOWLEDGE_SCOPE.GLOBAL:
|
|
63
|
+
return path.join(getGlobalKnowledgeRoot(), GLOBAL_AUTOSPEC_SUBDIRS[type.toUpperCase()] || type, name);
|
|
64
|
+
|
|
65
|
+
case KNOWLEDGE_SCOPE.ORG:
|
|
66
|
+
if (!orgName) {
|
|
67
|
+
throw new Error('orgName is required for org scope');
|
|
68
|
+
}
|
|
69
|
+
return path.join(getOrgKnowledgeRoot(orgName), ORG_AUTOSPEC_SUBDIRS[type.toUpperCase()] || type, name);
|
|
70
|
+
|
|
71
|
+
case KNOWLEDGE_SCOPE.PROJECT:
|
|
72
|
+
default:
|
|
73
|
+
return path.join(getProjectKnowledgeRoot(projectRoot), type, name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 安全地读取文件
|
|
79
|
+
* @param {string} filePath - 文件路径
|
|
80
|
+
* @returns {string|null} 文件内容
|
|
81
|
+
*/
|
|
82
|
+
function safeReadFile(filePath) {
|
|
83
|
+
try {
|
|
84
|
+
if (!fs.existsSync(filePath)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return fs.readFileSync(filePath, 'utf-8');
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 安全地读取 JSON 文件
|
|
95
|
+
* @param {string} filePath - 文件路径
|
|
96
|
+
* @param {any} defaultValue - 默认值
|
|
97
|
+
* @returns {any} 解析后的数据
|
|
98
|
+
*/
|
|
99
|
+
function safeReadJson(filePath, defaultValue = null) {
|
|
100
|
+
try {
|
|
101
|
+
const content = safeReadFile(filePath);
|
|
102
|
+
if (!content) {
|
|
103
|
+
return defaultValue;
|
|
104
|
+
}
|
|
105
|
+
return JSON.parse(content);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return defaultValue;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 获取知识缓存键
|
|
113
|
+
* @param {string} scope - 作用域
|
|
114
|
+
* @param {string} type - 类型
|
|
115
|
+
* @param {string} name - 名称
|
|
116
|
+
* @returns {string} 缓存键
|
|
117
|
+
*/
|
|
118
|
+
function getCacheKey(scope, type, name) {
|
|
119
|
+
return `${scope}:${type}:${name}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 加载知识(带缓存)
|
|
124
|
+
* @param {string} scope - 作用域
|
|
125
|
+
* @param {string} type - 类型
|
|
126
|
+
* @param {string} name - 名称
|
|
127
|
+
* @param {Object} options - 选项
|
|
128
|
+
* @returns {Object|null} 知识内容
|
|
129
|
+
*/
|
|
130
|
+
export function loadKnowledge(scope, type, name, options = {}) {
|
|
131
|
+
const { useCache = true, projectRoot = '.', orgName = null } = options;
|
|
132
|
+
const cacheKey = getCacheKey(scope, type, name);
|
|
133
|
+
|
|
134
|
+
if (useCache) {
|
|
135
|
+
const cached = knowledgeCache.get(cacheKey);
|
|
136
|
+
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
|
|
137
|
+
return cached.data;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const filePath = resolveKnowledgePath(scope, type, name, { projectRoot, orgName });
|
|
142
|
+
const content = safeReadFile(filePath);
|
|
143
|
+
|
|
144
|
+
if (content && useCache) {
|
|
145
|
+
knowledgeCache.set(cacheKey, { data: content, timestamp: Date.now() });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return content;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 加载知识 JSON(带缓存)
|
|
153
|
+
* @param {string} scope - 作用域
|
|
154
|
+
* @param {string} type - 类型
|
|
155
|
+
* @param {string} name - 名称
|
|
156
|
+
* @param {Object} options - 选项
|
|
157
|
+
* @returns {any} 知识内容
|
|
158
|
+
*/
|
|
159
|
+
export function loadKnowledgeJson(scope, type, name, options = {}) {
|
|
160
|
+
const { useCache = true, projectRoot = '.', orgName = null } = options;
|
|
161
|
+
const cacheKey = `json:${getCacheKey(scope, type, name)}`;
|
|
162
|
+
|
|
163
|
+
if (useCache) {
|
|
164
|
+
const cached = knowledgeCache.get(cacheKey);
|
|
165
|
+
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
|
|
166
|
+
return cached.data;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const filePath = resolveKnowledgePath(scope, type, name, { projectRoot, orgName });
|
|
171
|
+
const data = safeReadJson(filePath, null);
|
|
172
|
+
|
|
173
|
+
if (data && useCache) {
|
|
174
|
+
knowledgeCache.set(cacheKey, { data, timestamp: Date.now() });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return data;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 三级优先级加载知识
|
|
182
|
+
* 优先级:项目 > 用户全局 > 组织
|
|
183
|
+
*
|
|
184
|
+
* @param {string} type - 知识类型
|
|
185
|
+
* @param {string} name - 知识名称
|
|
186
|
+
* @param {Object} options - 选项
|
|
187
|
+
* @returns {Object} 加载结果 { content, scope, path }
|
|
188
|
+
*/
|
|
189
|
+
export function loadKnowledgeWithFallback(type, name, options = {}) {
|
|
190
|
+
const { projectRoot = '.', orgName = null, preferScope = null } = options;
|
|
191
|
+
|
|
192
|
+
// 如果指定了偏好作用域,优先尝试
|
|
193
|
+
if (preferScope) {
|
|
194
|
+
const content = loadKnowledge(preferScope, type, name, { projectRoot, orgName });
|
|
195
|
+
if (content) {
|
|
196
|
+
return {
|
|
197
|
+
content,
|
|
198
|
+
scope: preferScope,
|
|
199
|
+
path: resolveKnowledgePath(preferScope, type, name, { projectRoot, orgName })
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// 1. 优先加载项目知识
|
|
205
|
+
const projectContent = loadKnowledge(KNOWLEDGE_SCOPE.PROJECT, type, name, { projectRoot });
|
|
206
|
+
if (projectContent) {
|
|
207
|
+
return {
|
|
208
|
+
content: projectContent,
|
|
209
|
+
scope: KNOWLEDGE_SCOPE.PROJECT,
|
|
210
|
+
path: resolveKnowledgePath(KNOWLEDGE_SCOPE.PROJECT, type, name, { projectRoot })
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 2. 回退到全局知识
|
|
215
|
+
const globalContent = loadKnowledge(KNOWLEDGE_SCOPE.GLOBAL, type, name, { projectRoot });
|
|
216
|
+
if (globalContent) {
|
|
217
|
+
return {
|
|
218
|
+
content: globalContent,
|
|
219
|
+
scope: KNOWLEDGE_SCOPE.GLOBAL,
|
|
220
|
+
path: resolveKnowledgePath(KNOWLEDGE_SCOPE.GLOBAL, type, name, { projectRoot })
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 3. 最后回退到组织知识
|
|
225
|
+
if (orgName) {
|
|
226
|
+
const orgContent = loadKnowledge(KNOWLEDGE_SCOPE.ORG, type, name, { projectRoot, orgName });
|
|
227
|
+
if (orgContent) {
|
|
228
|
+
return {
|
|
229
|
+
content: orgContent,
|
|
230
|
+
scope: KNOWLEDGE_SCOPE.ORG,
|
|
231
|
+
path: resolveKnowledgePath(KNOWLEDGE_SCOPE.ORG, type, name, { projectRoot, orgName })
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return { content: null, scope: null, path: null };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 加载技能(带版本支持)
|
|
241
|
+
* @param {string} skillName - 技能名称
|
|
242
|
+
* @param {Object} options - 选项
|
|
243
|
+
* @returns {Object} 技能内容
|
|
244
|
+
*/
|
|
245
|
+
export function loadSkill(skillName, options = {}) {
|
|
246
|
+
const { version = 'latest', projectRoot = '.', orgName = null } = options;
|
|
247
|
+
|
|
248
|
+
// 尝试加载指定版本
|
|
249
|
+
if (version !== 'latest') {
|
|
250
|
+
const versionedResult = loadKnowledgeWithFallback('skills', `${skillName}/v${version}/SKILL.md`, options);
|
|
251
|
+
if (versionedResult.content) {
|
|
252
|
+
return versionedResult;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 尝试加载最新版本
|
|
257
|
+
const latestResult = loadKnowledgeWithFallback('skills', `${skillName}/SKILL.md`, options);
|
|
258
|
+
if (latestResult.content) {
|
|
259
|
+
return latestResult;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// 尝试从 plugins 源码加载
|
|
263
|
+
const sourcePath = path.join(process.cwd(), 'plugins', 'skills', skillName, 'SKILL.md');
|
|
264
|
+
const sourceContent = safeReadFile(sourcePath);
|
|
265
|
+
if (sourceContent) {
|
|
266
|
+
return {
|
|
267
|
+
content: sourceContent,
|
|
268
|
+
scope: 'source',
|
|
269
|
+
path: sourcePath
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return { content: null, scope: null, path: null };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 获取知识分区
|
|
278
|
+
* @param {string} knowledgePath - 知识路径
|
|
279
|
+
* @returns {string} 知识分区
|
|
280
|
+
*/
|
|
281
|
+
export function getKnowledgeZone(knowledgePath) {
|
|
282
|
+
// 冻结区检测
|
|
283
|
+
if (/\/knowledge\/principles\/(constitution|evolution|design-philosophy)\.md$/.test(knowledgePath)) {
|
|
284
|
+
return KNOWLEDGE_ZONES.FROZEN;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 受控区检测
|
|
288
|
+
if (/\/knowledge\/(process|guides\/support|config|checklists)\//.test(knowledgePath)) {
|
|
289
|
+
return KNOWLEDGE_ZONES.CONTROLLED;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 自由区检测
|
|
293
|
+
if (/\/knowledge\/guides\/stages\/|\/knowledge\/domain\/|\/plugins\/skills\//.test(knowledgePath)) {
|
|
294
|
+
return KNOWLEDGE_ZONES.FREE;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// 默认返回受控区
|
|
298
|
+
return KNOWLEDGE_ZONES.CONTROLLED;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* 检查知识是否存在
|
|
303
|
+
* @param {string} scope - 作用域
|
|
304
|
+
* @param {string} type - 类型
|
|
305
|
+
* @param {string} name - 名称
|
|
306
|
+
* @param {Object} options - 选项
|
|
307
|
+
* @returns {boolean} 是否存在
|
|
308
|
+
*/
|
|
309
|
+
export function knowledgeExists(scope, type, name, options = {}) {
|
|
310
|
+
const { projectRoot = '.', orgName = null } = options;
|
|
311
|
+
const filePath = resolveKnowledgePath(scope, type, name, { projectRoot, orgName });
|
|
312
|
+
return fs.existsSync(filePath);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 清除知识缓存
|
|
317
|
+
* @param {string} scope - 作用域(可选)
|
|
318
|
+
*/
|
|
319
|
+
export function clearKnowledgeCache(scope = null) {
|
|
320
|
+
if (scope) {
|
|
321
|
+
for (const key of knowledgeCache.keys()) {
|
|
322
|
+
if (key.startsWith(`${scope}:`)) {
|
|
323
|
+
knowledgeCache.delete(key);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
knowledgeCache.clear();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export default {
|
|
332
|
+
// 路径解析
|
|
333
|
+
getGlobalKnowledgeRoot,
|
|
334
|
+
getOrgKnowledgeRoot,
|
|
335
|
+
getProjectKnowledgeRoot,
|
|
336
|
+
resolveKnowledgePath,
|
|
337
|
+
// 加载
|
|
338
|
+
loadKnowledge,
|
|
339
|
+
loadKnowledgeJson,
|
|
340
|
+
loadKnowledgeWithFallback,
|
|
341
|
+
loadSkill,
|
|
342
|
+
// 工具
|
|
343
|
+
getKnowledgeZone,
|
|
344
|
+
knowledgeExists,
|
|
345
|
+
clearKnowledgeCache
|
|
346
|
+
};
|