|
| 1 | +/** |
| 2 | + * Bench Command for StackMemory CLI |
| 3 | + * |
| 4 | + * Runs harness benchmarks and compares against online baselines |
| 5 | + * (SWE-bench Verified, internal targets). |
| 6 | + */ |
| 7 | + |
| 8 | +import { Command } from 'commander'; |
| 9 | +import { existsSync, readFileSync, readdirSync } from 'fs'; |
| 10 | +import { join } from 'path'; |
| 11 | +import { |
| 12 | + SWE_BENCH_BASELINES, |
| 13 | + HARNESS_TARGETS, |
| 14 | + summarizeRuns, |
| 15 | +} from '../../orchestrators/multimodal/baselines.js'; |
| 16 | +import type { HarnessRunMetrics } from '../../orchestrators/multimodal/baselines.js'; |
| 17 | + |
| 18 | +function loadRunMetrics(projectRoot: string): HarnessRunMetrics[] { |
| 19 | + const metricsFile = join( |
| 20 | + projectRoot, |
| 21 | + '.stackmemory', |
| 22 | + 'build', |
| 23 | + 'harness-metrics.jsonl' |
| 24 | + ); |
| 25 | + if (!existsSync(metricsFile)) return []; |
| 26 | + |
| 27 | + const lines = readFileSync(metricsFile, 'utf-8') |
| 28 | + .split('\n') |
| 29 | + .filter((l) => l.trim()); |
| 30 | + const runs: HarnessRunMetrics[] = []; |
| 31 | + for (const line of lines) { |
| 32 | + try { |
| 33 | + runs.push(JSON.parse(line)); |
| 34 | + } catch { |
| 35 | + // skip malformed |
| 36 | + } |
| 37 | + } |
| 38 | + return runs; |
| 39 | +} |
| 40 | + |
| 41 | +function loadSpikeAudits( |
| 42 | + projectRoot: string |
| 43 | +): Array<{ file: string; data: any }> { |
| 44 | + const dir = join(projectRoot, '.stackmemory', 'build'); |
| 45 | + if (!existsSync(dir)) return []; |
| 46 | + |
| 47 | + return readdirSync(dir) |
| 48 | + .filter((f) => f.startsWith('spike-') && f.endsWith('.json')) |
| 49 | + .sort() |
| 50 | + .reverse() |
| 51 | + .slice(0, 20) |
| 52 | + .map((f) => { |
| 53 | + try { |
| 54 | + return { |
| 55 | + file: f, |
| 56 | + data: JSON.parse(readFileSync(join(dir, f), 'utf-8')), |
| 57 | + }; |
| 58 | + } catch { |
| 59 | + return null; |
| 60 | + } |
| 61 | + }) |
| 62 | + .filter(Boolean) as Array<{ file: string; data: any }>; |
| 63 | +} |
| 64 | + |
| 65 | +export function createBenchCommand(): Command { |
| 66 | + const bench = new Command('bench') |
| 67 | + .description( |
| 68 | + 'Harness benchmarks — compare local runs against SWE-bench baselines' |
| 69 | + ) |
| 70 | + .option('--json', 'Output as JSON', false) |
| 71 | + .option('-d, --days <n>', 'Only include runs from last N days', '30') |
| 72 | + .option('--baselines', 'Show online benchmark baselines only', false) |
| 73 | + .action(async (options) => { |
| 74 | + const projectRoot = process.cwd(); |
| 75 | + |
| 76 | + // Baselines-only mode |
| 77 | + if (options.baselines) { |
| 78 | + if (options.json) { |
| 79 | + console.log( |
| 80 | + JSON.stringify( |
| 81 | + { baselines: SWE_BENCH_BASELINES, targets: HARNESS_TARGETS }, |
| 82 | + null, |
| 83 | + 2 |
| 84 | + ) |
| 85 | + ); |
| 86 | + return; |
| 87 | + } |
| 88 | + console.log('\nOnline Benchmark Baselines (SWE-bench Verified)'); |
| 89 | + console.log('─'.repeat(60)); |
| 90 | + console.log( |
| 91 | + `${'Agent'.padEnd(20)} ${'Model'.padEnd(20)} ${'Resolve'.padStart(8)}` |
| 92 | + ); |
| 93 | + console.log('─'.repeat(60)); |
| 94 | + for (const b of SWE_BENCH_BASELINES) { |
| 95 | + console.log( |
| 96 | + `${b.agent.padEnd(20)} ${b.model.padEnd(20)} ${(b.resolveRate * 100).toFixed(1).padStart(7)}%` |
| 97 | + ); |
| 98 | + } |
| 99 | + console.log('─'.repeat(60)); |
| 100 | + |
| 101 | + console.log('\nInternal Harness Targets'); |
| 102 | + console.log('─'.repeat(60)); |
| 103 | + console.log( |
| 104 | + ` Plan latency P95: ${HARNESS_TARGETS.planLatencyP95Ms}ms` |
| 105 | + ); |
| 106 | + console.log( |
| 107 | + ` Total latency P95: ${HARNESS_TARGETS.totalLatencyP95Ms}ms` |
| 108 | + ); |
| 109 | + console.log( |
| 110 | + ` First-pass approval: ${(HARNESS_TARGETS.firstPassApprovalRate * 100).toFixed(0)}%` |
| 111 | + ); |
| 112 | + console.log( |
| 113 | + ` Edit success rate: ${(HARNESS_TARGETS.editSuccessRate * 100).toFixed(0)}%` |
| 114 | + ); |
| 115 | + console.log( |
| 116 | + ` Fuzzy fallback rate: <${(HARNESS_TARGETS.editFuzzyFallbackRate * 100).toFixed(0)}%` |
| 117 | + ); |
| 118 | + console.log( |
| 119 | + ` Context token budget: ${HARNESS_TARGETS.contextTokenBudget}` |
| 120 | + ); |
| 121 | + console.log(''); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // Load local run data |
| 126 | + const days = parseInt(options.days, 10) || 30; |
| 127 | + const cutoff = Date.now() - days * 86400_000; |
| 128 | + const allRuns = loadRunMetrics(projectRoot); |
| 129 | + const runs = allRuns.filter((r) => r.timestamp >= cutoff); |
| 130 | + const audits = loadSpikeAudits(projectRoot); |
| 131 | + |
| 132 | + if (options.json) { |
| 133 | + const summary = summarizeRuns(runs); |
| 134 | + console.log( |
| 135 | + JSON.stringify( |
| 136 | + { |
| 137 | + summary, |
| 138 | + baselines: SWE_BENCH_BASELINES, |
| 139 | + targets: HARNESS_TARGETS, |
| 140 | + runsInWindow: runs.length, |
| 141 | + totalRuns: allRuns.length, |
| 142 | + recentAudits: audits.length, |
| 143 | + }, |
| 144 | + null, |
| 145 | + 2 |
| 146 | + ) |
| 147 | + ); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + // Human output |
| 152 | + console.log(`\nHarness Benchmark Report (last ${days} days)`); |
| 153 | + console.log('═'.repeat(60)); |
| 154 | + |
| 155 | + if (runs.length === 0) { |
| 156 | + console.log('\nNo harness runs recorded yet.'); |
| 157 | + console.log('Run: stackmemory build "your task" --execute'); |
| 158 | + console.log('Or: stackmemory mm-spike -t "task" --execute\n'); |
| 159 | + |
| 160 | + // Still show baselines for context |
| 161 | + console.log('Online Baselines (SWE-bench Verified):'); |
| 162 | + for (const b of SWE_BENCH_BASELINES.slice(0, 3)) { |
| 163 | + console.log( |
| 164 | + ` ${b.agent.padEnd(16)} ${(b.resolveRate * 100).toFixed(1)}%` |
| 165 | + ); |
| 166 | + } |
| 167 | + console.log(''); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + const summary = summarizeRuns(runs); |
| 172 | + |
| 173 | + // Harness metrics |
| 174 | + console.log('\nHarness Metrics:'); |
| 175 | + console.log(` Total runs: ${summary.totalRuns}`); |
| 176 | + console.log( |
| 177 | + ` Approval rate: ${(summary.approvalRate * 100).toFixed(1)}%` |
| 178 | + ); |
| 179 | + console.log( |
| 180 | + ` First-pass rate: ${(summary.firstPassRate * 100).toFixed(1)}%` |
| 181 | + ); |
| 182 | + console.log( |
| 183 | + ` Avg iterations: ${summary.avgIterations.toFixed(1)}` |
| 184 | + ); |
| 185 | + console.log( |
| 186 | + ` Plan latency (avg): ${Math.round(summary.avgPlanLatencyMs)}ms` |
| 187 | + ); |
| 188 | + console.log( |
| 189 | + ` Plan latency (P95): ${Math.round(summary.p95PlanLatencyMs)}ms` |
| 190 | + ); |
| 191 | + console.log( |
| 192 | + ` Total latency (avg): ${Math.round(summary.avgTotalLatencyMs)}ms` |
| 193 | + ); |
| 194 | + console.log( |
| 195 | + ` Total latency (P95): ${Math.round(summary.p95TotalLatencyMs)}ms` |
| 196 | + ); |
| 197 | + console.log( |
| 198 | + ` Edit success rate: ${(summary.editSuccessRate * 100).toFixed(1)}%` |
| 199 | + ); |
| 200 | + console.log( |
| 201 | + ` Fuzzy fallback rate: ${(summary.editFuzzyRate * 100).toFixed(1)}%` |
| 202 | + ); |
| 203 | + console.log( |
| 204 | + ` Context tokens (avg): ${Math.round(summary.avgContextTokens)}` |
| 205 | + ); |
| 206 | + |
| 207 | + // Target comparison |
| 208 | + console.log('\nTarget Comparison:'); |
| 209 | + const checks = summary.passesTargets; |
| 210 | + for (const [key, passes] of Object.entries(checks)) { |
| 211 | + const icon = passes ? 'PASS' : 'FAIL'; |
| 212 | + console.log(` [${icon}] ${key}`); |
| 213 | + } |
| 214 | + |
| 215 | + // Online baseline comparison |
| 216 | + console.log('\nOnline Baselines (SWE-bench Verified):'); |
| 217 | + for (const b of SWE_BENCH_BASELINES.slice(0, 4)) { |
| 218 | + console.log( |
| 219 | + ` ${b.agent.padEnd(16)} ${(b.resolveRate * 100).toFixed(1)}%` |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | + // Recent audits |
| 224 | + if (audits.length > 0) { |
| 225 | + console.log(`\nRecent Spike Audits (${audits.length}):`); |
| 226 | + for (const a of audits.slice(0, 5)) { |
| 227 | + const task = a.data?.input?.task || '(unknown)'; |
| 228 | + const approved = a.data?.iterations?.some( |
| 229 | + (it: any) => it.critique?.approved |
| 230 | + ); |
| 231 | + const icon = approved ? 'OK' : '--'; |
| 232 | + console.log(` [${icon}] ${task.slice(0, 50)}`); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + console.log(''); |
| 237 | + }); |
| 238 | + |
| 239 | + return bench; |
| 240 | +} |
0 commit comments