closed-loop-cli 1.0.2 → 1.0.4
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.
Potentially problematic release.
This version of closed-loop-cli might be problematic. Click here for more details.
- package/CLAUDE.md +17 -0
- package/Learnings.md +73 -0
- package/dist/index.js +56 -126
- package/package.json +1 -7
- package/src/index.ts +356 -425
- package/tsconfig.json +16 -0
- package/dist/orchestrator/autogenesis.js +0 -973
- package/dist/orchestrator/dgm-archive.js +0 -223
- package/dist/orchestrator/fitness-evaluator.js +0 -99
- package/dist/orchestrator/mutation-strategies.js +0 -174
- package/dist/orchestrator/prompt-benchmark.js +0 -102
- package/dist/orchestrator/prompt-optimizer.js +0 -169
- package/dist/orchestrator/refactor-scanner.js +0 -222
- package/src/orchestrator/autogenesis.ts +0 -1078
- package/src/orchestrator/dgm-archive.ts +0 -257
- package/src/orchestrator/fitness-evaluator.ts +0 -154
- package/src/orchestrator/mutation-strategies.ts +0 -214
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Darwin Gödel Machine (DGM) — Archive Entry
|
|
6
|
-
* แต่ละ entry คือ snapshot ของ agent ณ จุดหนึ่งใน evolutionary lineage
|
|
7
|
-
*/
|
|
8
|
-
export interface ArchiveEntry {
|
|
9
|
-
/** Unique identifier สำหรับ snapshot นี้ */
|
|
10
|
-
id: string;
|
|
11
|
-
/** Git commit hash ที่ snapshot นี้ถูก commit ไว้ */
|
|
12
|
-
commitHash: string;
|
|
13
|
-
/** ID ของ parent entry ที่สร้าง snapshot นี้ขึ้นมา (null = origin) */
|
|
14
|
-
parentId: string | null;
|
|
15
|
-
/** Empirical fitness score (0–1) วัดจาก test pass rate */
|
|
16
|
-
fitness: number;
|
|
17
|
-
/** Mutation strategy ที่ถูกใช้เพื่อสร้าง snapshot นี้ */
|
|
18
|
-
mutationStrategy: string;
|
|
19
|
-
/** Task description ที่ mutation นี้ถูก apply สำหรับ */
|
|
20
|
-
task: string;
|
|
21
|
-
/** Timestamp ของการสร้าง snapshot */
|
|
22
|
-
timestamp: string;
|
|
23
|
-
/** Metadata เพิ่มเติม (test counts, metrics, etc.) */
|
|
24
|
-
metadata: {
|
|
25
|
-
passCount?: number;
|
|
26
|
-
failCount?: number;
|
|
27
|
-
totalTests?: number;
|
|
28
|
-
passRate?: number;
|
|
29
|
-
goalDriftIndex?: number;
|
|
30
|
-
constraintPreservationScore?: number;
|
|
31
|
-
[key: string]: unknown;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Summary statistics ของ Archive ปัจจุบัน
|
|
37
|
-
*/
|
|
38
|
-
export interface ArchiveStats {
|
|
39
|
-
totalEntries: number;
|
|
40
|
-
bestFitness: number;
|
|
41
|
-
averageFitness: number;
|
|
42
|
-
bestEntryId: string | null;
|
|
43
|
-
latestEntryId: string | null;
|
|
44
|
-
generationDepth: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const ARCHIVE_FILENAME = 'dgm-archive.json';
|
|
48
|
-
const DEFAULT_MAX_SIZE = 20;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* DGM Archive Manager
|
|
52
|
-
*
|
|
53
|
-
* เก็บ population ของ agent snapshots และจัดการ selection สำหรับ
|
|
54
|
-
* Darwin Gödel Machine evolution loop ตาม paper:
|
|
55
|
-
* "Darwin Gödel Machine: Open-Ended Evolution of Self-Improving Agents"
|
|
56
|
-
*/
|
|
57
|
-
export class DGMArchive {
|
|
58
|
-
private archivePath: string;
|
|
59
|
-
private entries: ArchiveEntry[];
|
|
60
|
-
|
|
61
|
-
constructor(workspaceRoot?: string) {
|
|
62
|
-
const root = workspaceRoot || process.cwd();
|
|
63
|
-
this.archivePath = path.join(root, ARCHIVE_FILENAME);
|
|
64
|
-
this.entries = this.load();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ─────────────────────────────────────────
|
|
68
|
-
// Persistence
|
|
69
|
-
// ─────────────────────────────────────────
|
|
70
|
-
|
|
71
|
-
private load(): ArchiveEntry[] {
|
|
72
|
-
if (!fs.existsSync(this.archivePath)) {
|
|
73
|
-
return [];
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
const raw = fs.readFileSync(this.archivePath, 'utf-8');
|
|
77
|
-
const parsed = JSON.parse(raw);
|
|
78
|
-
return Array.isArray(parsed.entries) ? parsed.entries : [];
|
|
79
|
-
} catch {
|
|
80
|
-
return [];
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private save(): void {
|
|
85
|
-
const data = {
|
|
86
|
-
version: '1.0.0',
|
|
87
|
-
lastUpdated: new Date().toISOString(),
|
|
88
|
-
entries: this.entries
|
|
89
|
-
};
|
|
90
|
-
try {
|
|
91
|
-
fs.writeFileSync(this.archivePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
92
|
-
} catch (err) {
|
|
93
|
-
console.error('[DGMArchive] Failed to save archive:', err);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ─────────────────────────────────────────
|
|
98
|
-
// Core Operations
|
|
99
|
-
// ─────────────────────────────────────────
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* เพิ่ม snapshot ใหม่เข้า archive
|
|
103
|
-
* @returns entry ที่เพิ่งเพิ่มเข้าไป
|
|
104
|
-
*/
|
|
105
|
-
addEntry(entry: Omit<ArchiveEntry, 'id' | 'timestamp'>): ArchiveEntry {
|
|
106
|
-
const newEntry: ArchiveEntry = {
|
|
107
|
-
...entry,
|
|
108
|
-
id: this.generateId(),
|
|
109
|
-
timestamp: new Date().toISOString()
|
|
110
|
-
};
|
|
111
|
-
this.entries.push(newEntry);
|
|
112
|
-
this.save();
|
|
113
|
-
return newEntry;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* เลือก parent entry สำหรับ mutation รอบต่อไป
|
|
118
|
-
* ใช้ fitness-weighted sampling (DGM parent selection strategy)
|
|
119
|
-
*
|
|
120
|
-
* @param strategy 'fitness_weighted' | 'best' | 'random'
|
|
121
|
-
*/
|
|
122
|
-
getBestParent(strategy: 'fitness_weighted' | 'best' | 'random' = 'fitness_weighted'): ArchiveEntry | null {
|
|
123
|
-
if (this.entries.length === 0) return null;
|
|
124
|
-
|
|
125
|
-
if (strategy === 'best') {
|
|
126
|
-
return [...this.entries].sort((a, b) => b.fitness - a.fitness)[0];
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (strategy === 'random') {
|
|
130
|
-
return this.entries[Math.floor(Math.random() * this.entries.length)];
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// fitness_weighted: สุ่มโดยน้ำหนักตาม fitness score (DGM default)
|
|
134
|
-
return this.fitnessWeightedSample();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Trace lineage จาก entry ไปถึง origin
|
|
139
|
-
* @returns ordered array ตั้งแต่ origin จนถึง entry นี้
|
|
140
|
-
*/
|
|
141
|
-
getLineage(entryId: string): ArchiveEntry[] {
|
|
142
|
-
const lineage: ArchiveEntry[] = [];
|
|
143
|
-
let current = this.findById(entryId);
|
|
144
|
-
|
|
145
|
-
while (current) {
|
|
146
|
-
lineage.unshift(current);
|
|
147
|
-
if (!current.parentId) break;
|
|
148
|
-
current = this.findById(current.parentId);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return lineage;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* ตัด entries เก่าเพื่อป้องกัน archive ใหญ่เกินไป
|
|
156
|
-
* จะเก็บ entries ที่มี fitness สูงสุดไว้ก่อน
|
|
157
|
-
*/
|
|
158
|
-
pruneOldEntries(maxSize: number = DEFAULT_MAX_SIZE): number {
|
|
159
|
-
if (this.entries.length <= maxSize) return 0;
|
|
160
|
-
|
|
161
|
-
// เรียง fitness สูงสุดก่อน แล้วตัดส่วนที่เกิน
|
|
162
|
-
const sorted = [...this.entries].sort((a, b) => b.fitness - a.fitness);
|
|
163
|
-
const pruned = this.entries.length - maxSize;
|
|
164
|
-
this.entries = sorted.slice(0, maxSize);
|
|
165
|
-
this.save();
|
|
166
|
-
return pruned;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// ─────────────────────────────────────────
|
|
170
|
-
// Query / Stats
|
|
171
|
-
// ─────────────────────────────────────────
|
|
172
|
-
|
|
173
|
-
getAll(): ArchiveEntry[] {
|
|
174
|
-
return [...this.entries];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
findById(id: string): ArchiveEntry | undefined {
|
|
178
|
-
return this.entries.find(e => e.id === id);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
getBestEntry(): ArchiveEntry | null {
|
|
182
|
-
if (this.entries.length === 0) return null;
|
|
183
|
-
return [...this.entries].sort((a, b) => b.fitness - a.fitness)[0];
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
getStats(): ArchiveStats {
|
|
187
|
-
if (this.entries.length === 0) {
|
|
188
|
-
return {
|
|
189
|
-
totalEntries: 0,
|
|
190
|
-
bestFitness: 0,
|
|
191
|
-
averageFitness: 0,
|
|
192
|
-
bestEntryId: null,
|
|
193
|
-
latestEntryId: null,
|
|
194
|
-
generationDepth: 0
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const sorted = [...this.entries].sort((a, b) => b.fitness - a.fitness);
|
|
199
|
-
const best = sorted[0];
|
|
200
|
-
const latest = [...this.entries].sort(
|
|
201
|
-
(a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
|
|
202
|
-
)[0];
|
|
203
|
-
|
|
204
|
-
const avgFitness =
|
|
205
|
-
this.entries.reduce((sum, e) => sum + e.fitness, 0) / this.entries.length;
|
|
206
|
-
|
|
207
|
-
const depth = latest ? this.getLineage(latest.id).length : 0;
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
totalEntries: this.entries.length,
|
|
211
|
-
bestFitness: best.fitness,
|
|
212
|
-
averageFitness: Math.round(avgFitness * 1000) / 1000,
|
|
213
|
-
bestEntryId: best.id,
|
|
214
|
-
latestEntryId: latest?.id ?? null,
|
|
215
|
-
generationDepth: depth
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* ดึง history ล่าสุด N entries (เรียงตามเวลา)
|
|
221
|
-
*/
|
|
222
|
-
getRecentHistory(n: number = 10): ArchiveEntry[] {
|
|
223
|
-
return [...this.entries]
|
|
224
|
-
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())
|
|
225
|
-
.slice(0, n);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// ─────────────────────────────────────────
|
|
229
|
-
// Private Helpers
|
|
230
|
-
// ─────────────────────────────────────────
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Fitness-weighted random sampling (Roulette Wheel Selection)
|
|
234
|
-
* สูตรจาก DGM paper: เลือก parent ตาม probability ∝ fitness
|
|
235
|
-
*/
|
|
236
|
-
private fitnessWeightedSample(): ArchiveEntry {
|
|
237
|
-
const totalFitness = this.entries.reduce((sum, e) => sum + Math.max(e.fitness, 0.01), 0);
|
|
238
|
-
let r = Math.random() * totalFitness;
|
|
239
|
-
|
|
240
|
-
for (const entry of this.entries) {
|
|
241
|
-
r -= Math.max(entry.fitness, 0.01);
|
|
242
|
-
if (r <= 0) return entry;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// fallback: คืน entry สุดท้าย
|
|
246
|
-
return this.entries[this.entries.length - 1];
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* สร้าง unique ID แบบ timestamp + random
|
|
251
|
-
*/
|
|
252
|
-
private generateId(): string {
|
|
253
|
-
const ts = Date.now().toString(36);
|
|
254
|
-
const rand = Math.random().toString(36).substring(2, 6);
|
|
255
|
-
return `dgm_${ts}_${rand}`;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { runCommand } from '../tools/shell-tools';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Fitness Report จากการ evaluate โดย unit tests
|
|
5
|
-
* Fitness = empirical test pass rate (0–1) ตาม DGM paper concept
|
|
6
|
-
*/
|
|
7
|
-
export interface FitnessReport {
|
|
8
|
-
/** Pass rate (0–1) = passCount / totalTests */
|
|
9
|
-
passRate: number;
|
|
10
|
-
/** จำนวน tests ที่ผ่าน */
|
|
11
|
-
passCount: number;
|
|
12
|
-
/** จำนวน tests ที่ fail */
|
|
13
|
-
failCount: number;
|
|
14
|
-
/** จำนวน tests ทั้งหมด */
|
|
15
|
-
totalTests: number;
|
|
16
|
-
/** Composite fitness score (0–1) รวม compile status */
|
|
17
|
-
score: number;
|
|
18
|
-
/** Compilation ผ่านหรือไม่ */
|
|
19
|
-
compileSuccess: boolean;
|
|
20
|
-
/** สรุปผล */
|
|
21
|
-
summary: string;
|
|
22
|
-
/** Raw output จาก test runner */
|
|
23
|
-
rawOutput?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* ผลเปรียบเทียบ fitness ระหว่าง baseline กับ candidate
|
|
28
|
-
*/
|
|
29
|
-
export interface FitnessComparison {
|
|
30
|
-
improved: boolean;
|
|
31
|
-
delta: number;
|
|
32
|
-
baseline: FitnessReport;
|
|
33
|
-
candidate: FitnessReport;
|
|
34
|
-
message: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* วัด empirical fitness จาก test suite ปัจจุบัน
|
|
39
|
-
*
|
|
40
|
-
* DGM approach: fitness วัดจากผล unit test จริง ไม่ใช่ heuristic เพียงอย่างเดียว
|
|
41
|
-
* "Each new agent version is evaluated empirically using coding benchmarks"
|
|
42
|
-
*/
|
|
43
|
-
export async function evaluateFitness(): Promise<FitnessReport> {
|
|
44
|
-
// Step 1: ตรวจ compile
|
|
45
|
-
const compileRes = await runCommand('npm run build');
|
|
46
|
-
const compileSuccess = compileRes.exitCode === 0;
|
|
47
|
-
|
|
48
|
-
if (!compileSuccess) {
|
|
49
|
-
return {
|
|
50
|
-
passRate: 0,
|
|
51
|
-
passCount: 0,
|
|
52
|
-
failCount: 0,
|
|
53
|
-
totalTests: 0,
|
|
54
|
-
score: 0,
|
|
55
|
-
compileSuccess: false,
|
|
56
|
-
summary: `Compilation failed — fitness = 0`,
|
|
57
|
-
rawOutput: compileRes.stdout + compileRes.stderr
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Step 2: run tests
|
|
62
|
-
const testRes = await runCommand('npm test');
|
|
63
|
-
const rawOutput = (testRes.stdout || '') + (testRes.stderr || '');
|
|
64
|
-
|
|
65
|
-
// Step 3: parse test output
|
|
66
|
-
const parsed = parseTestOutput(rawOutput);
|
|
67
|
-
|
|
68
|
-
// Step 4: คำนวณ composite score
|
|
69
|
-
// score = pass_rate ถ้า compile ผ่าน, 0 ถ้า compile fail
|
|
70
|
-
const score = compileSuccess ? parsed.passRate : 0;
|
|
71
|
-
|
|
72
|
-
const summary =
|
|
73
|
-
`Compile: ${compileSuccess ? 'PASS' : 'FAIL'} | ` +
|
|
74
|
-
`Tests: ${parsed.passCount}/${parsed.totalTests} passed ` +
|
|
75
|
-
`(${(parsed.passRate * 100).toFixed(1)}%) | Score: ${score.toFixed(3)}`;
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
...parsed,
|
|
79
|
-
score,
|
|
80
|
-
compileSuccess,
|
|
81
|
-
summary,
|
|
82
|
-
rawOutput
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* เปรียบเทียบ fitness ระหว่าง baseline snapshot กับ candidate snapshot
|
|
88
|
-
*/
|
|
89
|
-
export function compareToBaseline(
|
|
90
|
-
candidate: FitnessReport,
|
|
91
|
-
baseline: FitnessReport
|
|
92
|
-
): FitnessComparison {
|
|
93
|
-
const delta = candidate.score - baseline.score;
|
|
94
|
-
const improved = delta > 0;
|
|
95
|
-
|
|
96
|
-
const message = improved
|
|
97
|
-
? `✔ Fitness improved: ${baseline.score.toFixed(3)} → ${candidate.score.toFixed(3)} (+${delta.toFixed(3)})`
|
|
98
|
-
: delta === 0
|
|
99
|
-
? `= Fitness unchanged: ${candidate.score.toFixed(3)}`
|
|
100
|
-
: `✘ Fitness regressed: ${baseline.score.toFixed(3)} → ${candidate.score.toFixed(3)} (${delta.toFixed(3)})`;
|
|
101
|
-
|
|
102
|
-
return { improved, delta, baseline, candidate, message };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Parse test runner output เพื่อนับ pass/fail counts
|
|
107
|
-
* รองรับ format ของ test runner ปัจจุบันในโปรเจกต์
|
|
108
|
-
*/
|
|
109
|
-
export function parseTestOutput(output: string): Pick<
|
|
110
|
-
FitnessReport,
|
|
111
|
-
'passRate' | 'passCount' | 'failCount' | 'totalTests'
|
|
112
|
-
> {
|
|
113
|
-
let passCount = 0;
|
|
114
|
-
let failCount = 0;
|
|
115
|
-
|
|
116
|
-
// Pattern 1: "[Pass] <test name>" ซึ่งเป็น format ของ run-tests.ts ปัจจุบัน
|
|
117
|
-
const passMatches = output.match(/\[Pass\]/g);
|
|
118
|
-
const failMatches = output.match(/\[Fail\]/g);
|
|
119
|
-
|
|
120
|
-
if (passMatches || failMatches) {
|
|
121
|
-
passCount = passMatches ? passMatches.length : 0;
|
|
122
|
-
failCount = failMatches ? failMatches.length : 0;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Pattern 2: Mocha format — "X passing" / "X failing"
|
|
126
|
-
const mochaPassing = output.match(/(\d+)\s+passing/i);
|
|
127
|
-
const mochaFailing = output.match(/(\d+)\s+failing/i);
|
|
128
|
-
|
|
129
|
-
if (mochaPassing || mochaFailing) {
|
|
130
|
-
passCount = mochaPassing ? parseInt(mochaPassing[1], 10) : passCount;
|
|
131
|
-
failCount = mochaFailing ? parseInt(mochaFailing[1], 10) : failCount;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Pattern 3: Jest format — "Tests: X passed, Y failed"
|
|
135
|
-
const jestLine = output.match(/Tests:\s+(\d+)\s+passed(?:,\s+(\d+)\s+failed)?/i);
|
|
136
|
-
if (jestLine) {
|
|
137
|
-
passCount = parseInt(jestLine[1], 10) || passCount;
|
|
138
|
-
failCount = jestLine[2] ? parseInt(jestLine[2], 10) : failCount;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Pattern 4: "ALL BINARY ASSERTION TESTS PASSED" — หมายถึง 100% ผ่าน
|
|
142
|
-
if (output.includes('ALL BINARY ASSERTION TESTS PASSED')) {
|
|
143
|
-
// ถ้าตรวจจับ pass ไม่ได้ชัดเจนให้ fallback = ผ่านทั้งหมด
|
|
144
|
-
if (passCount === 0 && failCount === 0) {
|
|
145
|
-
passCount = 11; // จำนวน tests ใน run-tests.ts (Test 1–11)
|
|
146
|
-
failCount = 0;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const totalTests = passCount + failCount;
|
|
151
|
-
const passRate = totalTests > 0 ? passCount / totalTests : (failCount === 0 ? 1 : 0);
|
|
152
|
-
|
|
153
|
-
return { passRate, passCount, failCount, totalTests };
|
|
154
|
-
}
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { DGMArchive, ArchiveEntry } from './dgm-archive';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* DGM Mutation Strategies
|
|
5
|
-
*
|
|
6
|
-
* จาก DGM paper: open-ended evolution ต้องการ diversity ของ mutation types
|
|
7
|
-
* ไม่ใช่แค่ refactor อย่างเดียว — เพื่อสำรวจ search space ที่กว้างขึ้น
|
|
8
|
-
*/
|
|
9
|
-
export enum MutationStrategy {
|
|
10
|
-
/** เพิ่ม feature ใหม่เข้าไปในระบบ */
|
|
11
|
-
ADD_FEATURE = 'add_feature',
|
|
12
|
-
/** Refactor code เดิมให้อ่านง่ายขึ้น / ลด duplication */
|
|
13
|
-
REFACTOR = 'refactor',
|
|
14
|
-
/** Optimize performance หรือ token efficiency */
|
|
15
|
-
OPTIMIZE = 'optimize',
|
|
16
|
-
/** เพิ่ม unit tests เพื่อเพิ่ม coverage */
|
|
17
|
-
ADD_TESTS = 'add_tests',
|
|
18
|
-
/** แก้ bug ที่ตรวจพบจาก test failures */
|
|
19
|
-
FIX_BUG = 'fix_bug',
|
|
20
|
-
/** ปรับปรุง system prompt / agent prompt */
|
|
21
|
-
IMPROVE_PROMPT = 'improve_prompt',
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* ผลการเลือก mutation strategy
|
|
26
|
-
*/
|
|
27
|
-
export interface MutationSelection {
|
|
28
|
-
strategy: MutationStrategy;
|
|
29
|
-
rationale: string;
|
|
30
|
-
targetHint?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* เลือก mutation strategy ที่เหมาะสมจาก archive history + task context
|
|
35
|
-
*
|
|
36
|
-
* DGM approach: strategy selection ควร adaptive ตาม population history
|
|
37
|
-
* — ถ้า fitness ต่ำ → ลอง ADD_TESTS หรือ FIX_BUG ก่อน
|
|
38
|
-
* — ถ้า fitness สูงแล้ว → ลอง ADD_FEATURE หรือ OPTIMIZE
|
|
39
|
-
* — ถ้า task พูดถึง bug → เลือก FIX_BUG ก่อน
|
|
40
|
-
*/
|
|
41
|
-
export function selectMutationStrategy(
|
|
42
|
-
archive: DGMArchive,
|
|
43
|
-
task: string,
|
|
44
|
-
currentFitness: number = 0
|
|
45
|
-
): MutationSelection {
|
|
46
|
-
const taskLower = task.toLowerCase();
|
|
47
|
-
|
|
48
|
-
// 1. Task-based override: ถ้า task บ่งชี้ strategy ชัดเจน
|
|
49
|
-
if (taskLower.includes('fix') || taskLower.includes('bug') || taskLower.includes('error')) {
|
|
50
|
-
return {
|
|
51
|
-
strategy: MutationStrategy.FIX_BUG,
|
|
52
|
-
rationale: 'Task description indicates a bug fix is needed'
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (taskLower.includes('test') || taskLower.includes('coverage')) {
|
|
57
|
-
return {
|
|
58
|
-
strategy: MutationStrategy.ADD_TESTS,
|
|
59
|
-
rationale: 'Task description requests test improvements'
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (taskLower.includes('prompt') || taskLower.includes('instruction')) {
|
|
64
|
-
return {
|
|
65
|
-
strategy: MutationStrategy.IMPROVE_PROMPT,
|
|
66
|
-
rationale: 'Task description targets prompt engineering'
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (taskLower.includes('refactor') || taskLower.includes('clean') || taskLower.includes('reformat')) {
|
|
71
|
-
return {
|
|
72
|
-
strategy: MutationStrategy.REFACTOR,
|
|
73
|
-
rationale: 'Task description requests code refactoring'
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (taskLower.includes('optim') || taskLower.includes('speed') || taskLower.includes('performance') || taskLower.includes('token')) {
|
|
78
|
-
return {
|
|
79
|
-
strategy: MutationStrategy.OPTIMIZE,
|
|
80
|
-
rationale: 'Task description requests optimization'
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (taskLower.includes('add') || taskLower.includes('implement') || taskLower.includes('create') || taskLower.includes('new feature')) {
|
|
85
|
-
return {
|
|
86
|
-
strategy: MutationStrategy.ADD_FEATURE,
|
|
87
|
-
rationale: 'Task description requests adding a new feature'
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 2. Fitness-based heuristic: ถ้า fitness ต่ำ → focus on fixing
|
|
92
|
-
if (currentFitness < 0.7) {
|
|
93
|
-
return {
|
|
94
|
-
strategy: MutationStrategy.FIX_BUG,
|
|
95
|
-
rationale: `Low fitness (${(currentFitness * 100).toFixed(1)}%) — prioritizing stability fixes`
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// 3. Archive diversity: ตรวจสอบว่า strategy ไหนถูกใช้ไปน้อยสุดใน archive
|
|
100
|
-
const recentHistory = archive.getRecentHistory(8);
|
|
101
|
-
if (recentHistory.length > 0) {
|
|
102
|
-
const strategyCounts: Record<string, number> = {};
|
|
103
|
-
for (const s of Object.values(MutationStrategy)) {
|
|
104
|
-
strategyCounts[s] = 0;
|
|
105
|
-
}
|
|
106
|
-
for (const entry of recentHistory) {
|
|
107
|
-
if (entry.mutationStrategy && strategyCounts[entry.mutationStrategy] !== undefined) {
|
|
108
|
-
strategyCounts[entry.mutationStrategy]++;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// เลือก strategy ที่ถูกใช้น้อยสุด (diversity promotion)
|
|
113
|
-
const leastUsed = Object.entries(strategyCounts)
|
|
114
|
-
.sort((a, b) => a[1] - b[1])[0];
|
|
115
|
-
|
|
116
|
-
if (leastUsed && leastUsed[1] < 2) {
|
|
117
|
-
return {
|
|
118
|
-
strategy: leastUsed[0] as MutationStrategy,
|
|
119
|
-
rationale: `Diversity promotion: "${leastUsed[0]}" has been used least (${leastUsed[1]}x in recent history)`
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// 4. Default: ADD_FEATURE (DGM paper prefers expansive mutations)
|
|
125
|
-
return {
|
|
126
|
-
strategy: MutationStrategy.ADD_FEATURE,
|
|
127
|
-
rationale: 'Default open-ended evolution strategy: expanding capabilities'
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* สร้าง task prompt เฉพาะ mutation strategy
|
|
133
|
-
* ให้ Agent ทราบว่ากำลังทำ mutation ประเภทใด
|
|
134
|
-
*/
|
|
135
|
-
export function generateMutationPrompt(
|
|
136
|
-
strategy: MutationStrategy,
|
|
137
|
-
originalTask: string,
|
|
138
|
-
context?: {
|
|
139
|
-
targetFile?: string;
|
|
140
|
-
parentEntry?: ArchiveEntry | null;
|
|
141
|
-
currentFitness?: number;
|
|
142
|
-
}
|
|
143
|
-
): string {
|
|
144
|
-
const parentInfo = context?.parentEntry
|
|
145
|
-
? `\n[DGM Context] Building upon parent snapshot: ${context.parentEntry.id} (fitness: ${(context.parentEntry.fitness * 100).toFixed(1)}%)`
|
|
146
|
-
: '';
|
|
147
|
-
|
|
148
|
-
const fitnessInfo = context?.currentFitness !== undefined
|
|
149
|
-
? `\n[DGM Context] Current system fitness: ${(context.currentFitness * 100).toFixed(1)}% (test pass rate)`
|
|
150
|
-
: '';
|
|
151
|
-
|
|
152
|
-
const fileInfo = context?.targetFile
|
|
153
|
-
? `\n[DGM Context] Primary target file: ${context.targetFile}`
|
|
154
|
-
: '';
|
|
155
|
-
|
|
156
|
-
const dgmHeader = `[DGM Mutation: ${strategy.toUpperCase()}]${parentInfo}${fitnessInfo}${fileInfo}\n\n`;
|
|
157
|
-
|
|
158
|
-
switch (strategy) {
|
|
159
|
-
case MutationStrategy.ADD_FEATURE:
|
|
160
|
-
return dgmHeader +
|
|
161
|
-
`Your mutation goal is to ADD A NEW FEATURE. Implement the following capability:\n${originalTask}\n\n` +
|
|
162
|
-
`Guidelines:\n` +
|
|
163
|
-
`- Create new files or add new exported functions/classes as needed\n` +
|
|
164
|
-
`- Do not break existing functionality\n` +
|
|
165
|
-
`- Ensure all new code compiles and existing tests still pass\n` +
|
|
166
|
-
`- The feature should integrate cleanly with the existing codebase`;
|
|
167
|
-
|
|
168
|
-
case MutationStrategy.REFACTOR:
|
|
169
|
-
return dgmHeader +
|
|
170
|
-
`Your mutation goal is to REFACTOR existing code for better quality:\n${originalTask}\n\n` +
|
|
171
|
-
`Guidelines:\n` +
|
|
172
|
-
`- Improve readability, reduce duplication, or strengthen type safety\n` +
|
|
173
|
-
`- Preserve all existing behavior exactly (zero functional change)\n` +
|
|
174
|
-
`- All existing tests must still pass after refactoring`;
|
|
175
|
-
|
|
176
|
-
case MutationStrategy.OPTIMIZE:
|
|
177
|
-
return dgmHeader +
|
|
178
|
-
`Your mutation goal is to OPTIMIZE for performance or efficiency:\n${originalTask}\n\n` +
|
|
179
|
-
`Guidelines:\n` +
|
|
180
|
-
`- Focus on reducing token usage, execution time, or memory\n` +
|
|
181
|
-
`- Preserve correctness — all tests must still pass\n` +
|
|
182
|
-
`- Measure and report the optimization impact if possible`;
|
|
183
|
-
|
|
184
|
-
case MutationStrategy.ADD_TESTS:
|
|
185
|
-
return dgmHeader +
|
|
186
|
-
`Your mutation goal is to ADD UNIT TESTS to improve coverage:\n${originalTask}\n\n` +
|
|
187
|
-
`Guidelines:\n` +
|
|
188
|
-
`- Add tests to src/tests/dynamic/ directory as .ts files\n` +
|
|
189
|
-
`- Tests must export a default function or a run() function\n` +
|
|
190
|
-
`- Prioritize testing edge cases and untested public functions\n` +
|
|
191
|
-
`- Do not modify existing test files (protected by Campbell Regime)`;
|
|
192
|
-
|
|
193
|
-
case MutationStrategy.FIX_BUG:
|
|
194
|
-
return dgmHeader +
|
|
195
|
-
`Your mutation goal is to FIX A BUG or stability issue:\n${originalTask}\n\n` +
|
|
196
|
-
`Guidelines:\n` +
|
|
197
|
-
`- Diagnose the root cause carefully before making changes\n` +
|
|
198
|
-
`- Make the minimal change required to fix the issue\n` +
|
|
199
|
-
`- Add a regression test if appropriate\n` +
|
|
200
|
-
`- All tests must pass after the fix`;
|
|
201
|
-
|
|
202
|
-
case MutationStrategy.IMPROVE_PROMPT:
|
|
203
|
-
return dgmHeader +
|
|
204
|
-
`Your mutation goal is to IMPROVE AGENT PROMPTS for better performance:\n${originalTask}\n\n` +
|
|
205
|
-
`Guidelines:\n` +
|
|
206
|
-
`- Edit src/orchestrator/system-prompt.txt or src/orchestrator/agent-prompts.ts\n` +
|
|
207
|
-
`- Make prompts more concise, clear, and effective\n` +
|
|
208
|
-
`- Preserve all existing agent capabilities\n` +
|
|
209
|
-
`- Build compiles and tests pass after changes`;
|
|
210
|
-
|
|
211
|
-
default:
|
|
212
|
-
return dgmHeader + originalTask;
|
|
213
|
-
}
|
|
214
|
-
}
|