evolve.js
3.71 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
#!/usr/bin/env node
/**
* evolving - 双循环 RL 进化脚本
* Agent: evolver-youtube-crawler
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const WORKSPACE = path.resolve(__dirname, '../../../workspace/evolver-youtube-crawler');
const LOG_FILE = path.join(WORKSPACE, 'evolution_log.md');
const SCORE_FILE = path.join(WORKSPACE, 'score_history.json');
function ensureWorkspace() {
if (!fs.existsSync(WORKSPACE)) {
fs.mkdirSync(WORKSPACE, { recursive: true });
}
}
function initLog() {
ensureWorkspace();
if (!fs.existsSync(LOG_FILE)) {
fs.writeFileSync(LOG_FILE, `# Evolution Log - evolver-youtube-crawler\n\n`, 'utf-8');
}
if (!fs.existsSync(SCORE_FILE)) {
fs.writeFileSync(SCORE_FILE, JSON.stringify({ history: [] }, null, 2), 'utf-8');
}
}
function log(msg) {
const line = `[${new Date().toISOString()}] ${msg}\n`;
fs.appendFileSync(LOG_FILE, line, 'utf-8');
console.log(line.trim());
}
function recordScore(round, score, notes) {
const data = JSON.parse(fs.readFileSync(SCORE_FILE, 'utf-8'));
data.history.push({
round,
timestamp: new Date().toISOString(),
total_score: score.total_score ?? score,
dimensions: score.dimensions || {},
notes: notes || ''
});
fs.writeFileSync(SCORE_FILE, JSON.stringify(data, null, 2), 'utf-8');
}
function parseArgs() {
const args = process.argv.slice(2);
const opts = {
skillDir: '/mnt/lemonboss/system/agents/youtube-crawler/.claude/skills/youtube-crawler/',
benchmark: '/mnt/lemonboss/system/agents/youtube-crawler/workspace/evolve/benchmark_test.js',
maxSmallLoops: 20,
targetScore: 90
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--skill-dir') opts.skillDir = args[i + 1];
if (args[i] === '--benchmark') opts.benchmark = args[i + 1];
if (args[i] === '--max-small-loops') opts.maxSmallLoops = parseInt(args[i + 1], 10);
if (args[i] === '--target-score') opts.targetScore = parseInt(args[i + 1], 10);
}
return opts;
}
function runBenchmark(benchmarkPath) {
const cwd = path.dirname(path.resolve(__dirname, benchmarkPath));
const cmd = `node ${path.basename(benchmarkPath)}`;
try {
execSync(cmd, { cwd, stdio: 'pipe', timeout: 120000 });
const resultPath = path.join(cwd, 'benchmark_result.json');
if (fs.existsSync(resultPath)) {
return JSON.parse(fs.readFileSync(resultPath, 'utf-8'));
}
} catch (e) {
log(`Benchmark run failed: ${e.message}`);
}
return { total_score: 0, passed: false };
}
function main() {
initLog();
const opts = parseArgs();
log('========================================');
log('Evolving started');
log(`Target score: ${opts.targetScore}`);
log(`Max Small Loops: ${opts.maxSmallLoops}`);
log('========================================');
let round = 0;
let lastLargeLoopRound = 0;
let bestScore = 0;
while (round < opts.maxSmallLoops) {
round++;
log(`--- Small Loop #${round} ---`);
log('Action: Read benchmark logs, identify highest-priority failure, fix skill code.');
if (round - lastLargeLoopRound >= 5 || round === 1) {
log('--- Large Loop triggered ---');
const score = runBenchmark(opts.benchmark);
bestScore = Math.max(bestScore, score.total_score || 0);
recordScore(round, score, `Large Loop benchmark`);
log(`Score: ${score.total_score || 0}/100 | Best: ${bestScore}`);
if (score.total_score >= opts.targetScore) {
log(`Target reached! Evolving complete at round ${round}.`);
break;
}
lastLargeLoopRound = round;
}
}
log('========================================');
log(`Evolving finished. Best score: ${bestScore}`);
log('========================================');
}
main();