evolution-memory.js
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* EvolutionMemory — Skill 进化历史记忆模块
*
* 记录每个 Skill 的进化迭代历史,包括策略、分数、教训。
* 为 RL Loop 的 Reason & Plan 阶段提供经验参考。
*
* 存储位置: {skillDir}/evolution_memory.json
*/
const fs = require('fs');
const path = require('path');
const MEMORY_FILE = 'evolution_memory.json';
const MAX_RECORDS = 50; // 保留最近 50 条迭代记录
class EvolutionMemory {
/**
* @param {string} skillDir - Skill 目录绝对路径
*/
constructor(skillDir) {
this.skillDir = skillDir;
this.filePath = path.join(skillDir, MEMORY_FILE);
this._data = null;
}
/**
* 读取完整历史
* @returns {{ iterations: Array, bestScore: number|null, totalAttempts: number }}
*/
getHistory() {
const data = this._load();
return {
iterations: data.iterations || [],
bestScore: data.bestScore || null,
totalAttempts: data.totalAttempts || 0,
};
}
/**
* 从历史失败中提取教训
* @returns {string[]} 教训列表
*/
getLessons() {
const data = this._load();
const lessons = [];
for (const iter of (data.iterations || [])) {
if (iter.lessonsLearned && Array.isArray(iter.lessonsLearned)) {
lessons.push(...iter.lessonsLearned);
}
}
// 去重
return [...new Set(lessons)];
}
/**
* 获取历史最高分时的策略
* @returns {object|null}
*/
getBestStrategy() {
const data = this._load();
if (!data.iterations || data.iterations.length === 0) return null;
let best = null;
for (const iter of data.iterations) {
if (!best || (iter.score && iter.score > (best.score || 0))) {
best = iter;
}
}
return best ? best.strategy : null;
}
/**
* 获取最近 N 轮的弱项维度 (用于聚焦)
* @param {number} n
* @returns {string[]}
*/
getRecentWeakDimensions(n = 3) {
const data = this._load();
const iterations = data.iterations || [];
const recent = iterations.slice(-n);
const dims = [];
for (const iter of recent) {
if (iter.weakDimensions && Array.isArray(iter.weakDimensions)) {
dims.push(...iter.weakDimensions);
}
}
// 按频次排序
const freq = {};
for (const d of dims) { freq[d] = (freq[d] || 0) + 1; }
return Object.entries(freq)
.sort((a, b) => b[1] - a[1])
.map(e => e[0]);
}
/**
* 记录一轮进化结果
* @param {object} record
* @param {number} record.iteration - 迭代轮次
* @param {object} record.strategy - 使用的策略
* @param {number} record.score - 本轮分数
* @param {string[]} [record.weakDimensions] - 弱项维度
* @param {string[]} [record.strongDimensions] - 强项维度
* @param {object[]} [record.adjustments] - 调整记录
* @param {string[]} [record.lessonsLearned] - 教训
*/
record(record) {
const data = this._load();
const entry = {
...record,
timestamp: new Date().toISOString(),
};
data.iterations = data.iterations || [];
data.iterations.push(entry);
// 更新聚合指标
data.totalAttempts = (data.totalAttempts || 0) + 1;
if (!data.bestScore || (record.score && record.score > data.bestScore)) {
data.bestScore = record.score;
}
// 裁剪历史
if (data.iterations.length > MAX_RECORDS) {
data.iterations = data.iterations.slice(-MAX_RECORDS);
}
this._save(data);
}
/**
* 检测是否处于停滞 (连续 N 轮分数变化 < threshold)
* @param {number} rounds - 检查的轮次数
* @param {number} threshold - 分数变化阈值
* @returns {boolean}
*/
isStagnant(rounds = 3, threshold = 2) {
const data = this._load();
const iterations = data.iterations || [];
if (iterations.length < rounds) return false;
const recent = iterations.slice(-rounds);
const scores = recent.map(r => r.score).filter(s => typeof s === 'number');
if (scores.length < rounds) return false;
for (let i = 1; i < scores.length; i++) {
if (Math.abs(scores[i] - scores[i - 1]) >= threshold) {
return false;
}
}
return true;
}
// ── 内部方法 ──
_load() {
if (this._data) return this._data;
try {
if (fs.existsSync(this.filePath)) {
this._data = JSON.parse(fs.readFileSync(this.filePath, 'utf-8'));
return this._data;
}
} catch (err) {
console.warn(`[EvolutionMemory] 读取失败: ${err.message}`);
}
this._data = { iterations: [], bestScore: null, totalAttempts: 0 };
return this._data;
}
_save(data) {
try {
const dir = path.dirname(this.filePath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(this.filePath, JSON.stringify(data, null, 2), 'utf-8');
this._data = data;
} catch (err) {
console.error(`[EvolutionMemory] 保存失败: ${err.message}`);
}
}
}
module.exports = { EvolutionMemory, MEMORY_FILE, MAX_RECORDS };