Files
ruflo/plugins/ruflo-metaharness/scripts/bench.mjs
T
randRuFlo 9b46f57c58 feat(metaharness): @metaharness/darwin as a regular dependency, used in-process (ADR-526); release 3.74.0
- darwin moves to dependencies (ADR-526 amends ADR-150 rule 2 for darwin
  only); the smoke gate, doctor check and clean-install test accept it.
- src/metaharness/darwin-lib.ts: cached import; metaharness_bench verify and
  every metaharness_gepa op run in-process with the scripts' exact payloads
  and exit codes, falling back to the scripts when darwin does not import.
- ruflo-metaharness 0.5.0: bench.mjs verify imports darwin directly (bare
  import, no tool-time install) and falls back to the installed darwin CLI.
- Parity test vs the plugin scripts; benchmark script + measured runs.
- Version 3.74.0.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-11-08 22:01:25 -05:00

138 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
// bench.mjs — wrapper around `metaharness-darwin bench <create|verify>`.
//
// Bench suites are JSON files describing a corpus of (input, expected-output)
// tasks. `evolve --bench <suite.json>` scores each variant against the
// corpus instead of (or in addition to) the project's test command, which
// lets you evolve a harness against a fixed evaluation set independent of
// the repo's natural tests. `bench create` scaffolds a suite from a repo;
// `bench verify` checks a suite parses and every task is well-formed.
//
// USAGE
// node scripts/bench.mjs --op create --repo . # scaffold suite.json from current dir
// node scripts/bench.mjs --op create --repo . --out /tmp/foo.json
// node scripts/bench.mjs --op verify --suite /tmp/foo.json
//
// EXIT CODES
// 0 ok (or degraded — Darwin not available)
// 1 --op verify and any task malformed
// 2 config error or upstream invocation failure
import { guardCliArgs } from './_cli-args.mjs';
import { runDarwin, emitDarwinDegradedJsonAndExit, importDarwinLib } from './_darwin.mjs';
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
guardCliArgs(import.meta.url, {
'--op': 'value',
'--repo': 'value',
'--suite': 'value',
'--out': 'value',
'--format': 'value',
});
const ARGS = (() => {
const a = { op: null, repo: '.', suite: null, out: null, format: 'json' };
for (let i = 2; i < process.argv.length; i++) {
const v = process.argv[i];
if (v === '--op') a.op = process.argv[++i];
else if (v === '--repo') a.repo = process.argv[++i];
else if (v === '--suite') a.suite = process.argv[++i];
else if (v === '--out') a.out = process.argv[++i];
else if (v === '--format') a.format = process.argv[++i];
}
return a;
})();
function safetyChecks() {
if (!['create', 'verify'].includes(ARGS.op)) {
console.error('bench: --op must be create|verify');
process.exit(2);
}
if (ARGS.op === 'create' && !existsSync(resolve(ARGS.repo))) {
console.error(`bench: --repo path does not exist: ${ARGS.repo}`);
process.exit(2);
}
if (ARGS.op === 'verify' && !ARGS.suite) {
console.error('bench: --suite is required for verify');
process.exit(2);
}
if (ARGS.op === 'verify' && !existsSync(resolve(ARGS.suite))) {
console.error(`bench: --suite path does not exist: ${ARGS.suite}`);
process.exit(2);
}
}
// ADR-526 — verify through darwin's library (loadSuite + verifySuite), the
// same two calls the darwin CLI makes, without spawning it. Returns false when
// the library is unavailable so main() falls back to the CLI.
async function verifyInProcess() {
const darwin = await importDarwinLib();
if (!darwin?.bench?.loadSuite || !darwin?.bench?.verifySuite) return false;
const start = Date.now();
let payload;
let exitCode;
try {
const suite = await darwin.bench.loadSuite(resolve(ARGS.suite));
const check = darwin.bench.verifySuite(suite);
const tasks = Array.isArray(suite.tasks) ? suite.tasks.length : 0;
exitCode = check.ok ? 0 : 1;
payload = {
success: check.ok,
data: {
op: 'verify', suiteId: suite.id, version: suite.version, tasks, hashOk: check.ok, hash: check.actual,
stdoutTail: `Suite ${suite.id}@${suite.version}: ${tasks} tasks, hash ${check.ok ? 'OK' : 'MISMATCH'} (${String(check.actual).slice(0, 12)}…)\n`,
durationMs: Date.now() - start,
},
generatedAt: new Date().toISOString(),
};
} catch (e) {
exitCode = 1;
payload = { success: false, data: { op: 'verify', error: e?.message ?? String(e), durationMs: Date.now() - start }, generatedAt: new Date().toISOString() };
}
console.log(JSON.stringify(payload, null, 2));
process.exit(exitCode);
}
async function main() {
safetyChecks();
if (ARGS.op === 'verify' && (await verifyInProcess())) return;
const cliArgs = ARGS.op === 'create'
? ['bench', 'create', resolve(ARGS.repo), ...(ARGS.out ? ['--out', resolve(ARGS.out)] : [])]
: ['bench', 'verify', resolve(ARGS.suite)];
const r = runDarwin(cliArgs, { timeoutMs: 60_000 });
if (r.degraded) {
emitDarwinDegradedJsonAndExit(r.reason);
return;
}
if (r.exitCode !== 0 && r.exitCode !== 1) {
const payload = {
success: false,
data: { exitCode: r.exitCode, stderrTail: r.stderr.slice(-400) },
generatedAt: new Date().toISOString(),
};
console.log(JSON.stringify(payload, null, 2));
process.exit(2);
}
const payload = {
success: r.exitCode === 0,
data: {
op: ARGS.op,
...(r.json ?? { stdoutTail: r.stdout.slice(-2000) }),
durationMs: r.durationMs,
},
generatedAt: new Date().toISOString(),
};
console.log(JSON.stringify(payload, null, 2));
process.exit(r.exitCode);
}
main();