|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Phase-level eval harness for GEPA. |
| 4 | + * |
| 5 | + * Evaluates conductor prompt phase files against gold sets. |
| 6 | + * Scores each phase independently. Used by GEPA auto-optimization |
| 7 | + * to validate mutations before applying. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * node eval-phases.js # eval all phases |
| 11 | + * node eval-phases.js --phase validate # eval single phase |
| 12 | + * node eval-phases.js --json # JSON output for CI |
| 13 | + */ |
| 14 | + |
| 15 | +import fs from 'fs'; |
| 16 | +import path from 'path'; |
| 17 | +import { fileURLToPath } from 'url'; |
| 18 | +import { homedir } from 'os'; |
| 19 | + |
| 20 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 21 | +const GOLD_DIR = path.join(__dirname, 'gold'); |
| 22 | +const PROMPTS_DIR = path.join( |
| 23 | + homedir(), |
| 24 | + '.stackmemory', |
| 25 | + 'conductor', |
| 26 | + 'prompts' |
| 27 | +); |
| 28 | + |
| 29 | +const PHASES = ['understand', 'implement', 'validate', 'deliver']; |
| 30 | + |
| 31 | +// Parse args |
| 32 | +const phaseIdx = process.argv.indexOf('--phase'); |
| 33 | +const targetPhase = phaseIdx !== -1 ? process.argv[phaseIdx + 1] : null; |
| 34 | +const jsonOutput = process.argv.includes('--json'); |
| 35 | + |
| 36 | +/** |
| 37 | + * Load gold set for a phase |
| 38 | + */ |
| 39 | +function loadGoldSet(phase) { |
| 40 | + const goldPath = path.join(GOLD_DIR, `${phase}.jsonl`); |
| 41 | + if (!fs.existsSync(goldPath)) return []; |
| 42 | + return fs |
| 43 | + .readFileSync(goldPath, 'utf-8') |
| 44 | + .split('\n') |
| 45 | + .filter(Boolean) |
| 46 | + .map((l) => JSON.parse(l)); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Score a phase prompt against its gold set using heuristic evaluation. |
| 51 | + * This is a fast, offline eval (no LLM calls) based on outcome patterns. |
| 52 | + * |
| 53 | + * For LLM-judge evaluation, use the full GEPA optimize.js eval pipeline. |
| 54 | + */ |
| 55 | +function evalPhase(phase) { |
| 56 | + const goldSet = loadGoldSet(phase); |
| 57 | + if (goldSet.length === 0) { |
| 58 | + return { phase, score: 0, total: 0, passed: 0, skipped: true }; |
| 59 | + } |
| 60 | + |
| 61 | + const promptPath = path.join(PROMPTS_DIR, `${phase}.md`); |
| 62 | + if (!fs.existsSync(promptPath)) { |
| 63 | + return { phase, score: 0, total: goldSet.length, passed: 0, missing: true }; |
| 64 | + } |
| 65 | + |
| 66 | + const prompt = fs.readFileSync(promptPath, 'utf-8'); |
| 67 | + let passed = 0; |
| 68 | + const failures = []; |
| 69 | + |
| 70 | + for (const entry of goldSet) { |
| 71 | + const expected = entry.expected; |
| 72 | + if (!expected) continue; |
| 73 | + |
| 74 | + // Heuristic: check if the prompt addresses the failure patterns |
| 75 | + let entryPassed = true; |
| 76 | + |
| 77 | + switch (phase) { |
| 78 | + case 'understand': { |
| 79 | + // Check if prompt guides complexity assessment |
| 80 | + if (expected.complexity === 'careful' && !prompt.includes('plan')) { |
| 81 | + entryPassed = false; |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + case 'implement': { |
| 87 | + // Check if prompt constrains scope |
| 88 | + if (!expected.scopeKept && !prompt.includes('scope')) { |
| 89 | + entryPassed = false; |
| 90 | + } |
| 91 | + // Check ESM import guidance |
| 92 | + if ( |
| 93 | + entry.errorTail && |
| 94 | + /import|ESM/i.test(entry.errorTail) && |
| 95 | + !prompt.includes('.js') |
| 96 | + ) { |
| 97 | + entryPassed = false; |
| 98 | + } |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + case 'validate': { |
| 103 | + // Check if prompt covers the specific failure type |
| 104 | + if (expected.retryStrategy === 'fix_lint' && !prompt.includes('lint')) { |
| 105 | + entryPassed = false; |
| 106 | + } |
| 107 | + if (expected.retryStrategy === 'fix_test' && !prompt.includes('test')) { |
| 108 | + entryPassed = false; |
| 109 | + } |
| 110 | + if ( |
| 111 | + expected.retryStrategy === 'fix_build' && |
| 112 | + !prompt.includes('build') |
| 113 | + ) { |
| 114 | + entryPassed = false; |
| 115 | + } |
| 116 | + // Check --no-verify prevention |
| 117 | + if (!prompt.includes('no-verify') && !prompt.includes('--no-verify')) { |
| 118 | + entryPassed = false; |
| 119 | + } |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + case 'deliver': { |
| 124 | + // Check commit format guidance |
| 125 | + if (!prompt.includes('type(scope)') && !prompt.includes('commit')) { |
| 126 | + entryPassed = false; |
| 127 | + } |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (entryPassed) { |
| 133 | + passed++; |
| 134 | + } else { |
| 135 | + failures.push({ |
| 136 | + issue: entry.issue, |
| 137 | + outcome: entry.outcome, |
| 138 | + reason: `Prompt missing guidance for: ${JSON.stringify(expected)}`, |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return { |
| 144 | + phase, |
| 145 | + score: goldSet.length > 0 ? passed / goldSet.length : 0, |
| 146 | + total: goldSet.length, |
| 147 | + passed, |
| 148 | + failures: failures.slice(0, 5), // top 5 failures |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +// Main |
| 153 | +const phases = targetPhase ? [targetPhase] : PHASES; |
| 154 | +const results = phases.map(evalPhase); |
| 155 | + |
| 156 | +if (jsonOutput) { |
| 157 | + console.log(JSON.stringify(results, null, 2)); |
| 158 | +} else { |
| 159 | + console.log('GEPA Phase Evaluation'); |
| 160 | + console.log('═'.repeat(50)); |
| 161 | + |
| 162 | + let totalScore = 0; |
| 163 | + let totalPhases = 0; |
| 164 | + |
| 165 | + for (const r of results) { |
| 166 | + if (r.skipped) { |
| 167 | + console.log(` ${r.phase.padEnd(12)} — no gold set`); |
| 168 | + continue; |
| 169 | + } |
| 170 | + if (r.missing) { |
| 171 | + console.log(` ${r.phase.padEnd(12)} — prompt file missing`); |
| 172 | + continue; |
| 173 | + } |
| 174 | + |
| 175 | + const pct = (r.score * 100).toFixed(1); |
| 176 | + const bar = '█'.repeat(Math.round(r.score * 20)).padEnd(20, '░'); |
| 177 | + const status = r.score >= 0.7 ? '✓' : r.score >= 0.4 ? '~' : '✗'; |
| 178 | + console.log( |
| 179 | + ` ${status} ${r.phase.padEnd(12)} ${bar} ${pct}% (${r.passed}/${r.total})` |
| 180 | + ); |
| 181 | + |
| 182 | + if (r.failures && r.failures.length > 0) { |
| 183 | + for (const f of r.failures.slice(0, 3)) { |
| 184 | + console.log(` └ ${f.issue}: ${f.reason.slice(0, 80)}`); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + totalScore += r.score; |
| 189 | + totalPhases++; |
| 190 | + } |
| 191 | + |
| 192 | + if (totalPhases > 0) { |
| 193 | + const avg = ((totalScore / totalPhases) * 100).toFixed(1); |
| 194 | + console.log('─'.repeat(50)); |
| 195 | + console.log(` Average: ${avg}%`); |
| 196 | + } |
| 197 | +} |
0 commit comments