diff --git a/plugins/ruflo-metaharness/scripts/_darwin.mjs b/plugins/ruflo-metaharness/scripts/_darwin.mjs new file mode 100644 index 0000000000..2fd5bfac87 --- /dev/null +++ b/plugins/ruflo-metaharness/scripts/_darwin.mjs @@ -0,0 +1,200 @@ +// _darwin.mjs — shared invocation helper for the `@metaharness/darwin` CLI. +// +// Mirrors `_harness.mjs` for the umbrella `metaharness` / `harness` binaries, +// but targets the separate darwin binary (`metaharness-darwin`) which is +// published as its own npm package (`@metaharness/darwin@~0.3.1`). +// +// Three subcommands surfaced (matches darwin 0.3.x): +// - `metaharness-darwin evolve [...]` — harness self-improvement +// - `metaharness-darwin bench ...` — bench-suite lifecycle +// - `metaharness-darwin security bench [...]` — Darwin Shield (upstream ADR-155) +// +// CONTRACT (identical to runMetaharness/runHarness): +// - returns `{ stdout, stderr, exitCode, json|null, durationMs, degraded, reason? }` +// - `--json` is appended automatically when `opts.json !== false` +// - subprocess hard timeout (default 60s, override with opts.timeoutMs) +// - on MODULE_NOT_FOUND / network failure / "not installed", returns +// `degraded: true, reason: 'metaharness-darwin-not-available'` — never throws +// (ADR-150 graceful-degradation rule #3; ADR-153 §"Architecture" constraint 3) +// +// IMPORTANT — evolve has long timeouts: +// `evolve` is the only long-running verb in the metaharness family. A real +// evolution with --generations 10 --children 5 --concurrency 2 takes +// minutes-to-hours, NOT seconds. Callers MUST pass an explicit `timeoutMs` +// matched to their --generations × --children × sandbox cost; the 60s +// default is for `bench verify` and `security bench --population 2 --cycles 1` +// smoke shapes only. + +import { spawnSync, spawn } from 'node:child_process'; + +const DEFAULT_TIMEOUT_MS = 60_000; + +// Pinned semver range. Bump in lock-step with optionalDependencies in +// @claude-flow/cli/package.json + ruflo/package.json. The `~` allows +// patch upgrades without re-pinning. +const DARWIN_PIN = '@metaharness/darwin@~0.3.1'; + +const DEGRADED_RX = /could not determine executable|404|not installed|MODULE_NOT_FOUND|ENOTFOUND|getaddrinfo|ECONNREFUSED|ETIMEDOUT/i; + +function buildArgv(args, wantJson) { + // `metaharness-darwin` historically does NOT take --json on every + // subcommand (some emit plain text reports — security bench in + // particular is markdown). We still append --json for callers that + // explicitly request it, but never silently inject it on subcommands + // that don't accept it. The shape that does support --json is the + // single-verb `evolve` and `bench verify`. + if (!wantJson || args.includes('--json')) return args; + return [...args, '--json']; +} + +function classifyDegraded(stderr, exitCode) { + if (exitCode === null) return { degraded: true, reason: 'metaharness-darwin-timeout' }; + if (DEGRADED_RX.test(stderr)) return { degraded: true, reason: 'metaharness-darwin-not-available' }; + return { degraded: false }; +} + +function maybeParseJson(stdout) { + // darwin's evolve emits a final JSON object at end of stdout when --json + // is passed; everything before is human-readable progress. Grab the LAST + // {...} block, not the first — the first may be a per-generation log line. + const matches = [...stdout.matchAll(/\{[\s\S]*?\}/g)]; + if (matches.length === 0) return null; + // Try last match first; fall back to greedy single-block match. + for (let i = matches.length - 1; i >= 0; i--) { + try { return JSON.parse(matches[i][0]); } catch { /* try previous */ } + } + const greedy = /\{[\s\S]*\}/.exec(stdout); + if (greedy) { try { return JSON.parse(greedy[0]); } catch { return null; } } + return null; +} + +/** + * Sync invocation. Use for short subcommands (`bench verify`, smoke shapes). + * Async variant below for long-running `evolve` calls. + */ +export function runDarwin(args, opts = {}) { + const start = Date.now(); + const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const wantJson = opts.json !== false; + const argv = buildArgv(args, wantJson); + const r = spawnSync('npx', ['-y', '-p', DARWIN_PIN, 'metaharness-darwin', ...argv], { + stdio: ['ignore', 'pipe', 'pipe'], + encoding: 'utf-8', + timeout: timeoutMs, + cwd: opts.cwd, + env: { ...process.env, ...(opts.env || {}) }, + shell: process.platform === 'win32', + }); + const durationMs = Date.now() - start; + const stdout = r.stdout || ''; + const stderr = r.stderr || ''; + const classified = classifyDegraded(stderr, r.status); + if (classified.degraded) { + return { + stdout, stderr, + exitCode: r.status ?? 127, + json: null, + durationMs, + degraded: true, + reason: classified.reason, + }; + } + return { + stdout, stderr, + exitCode: r.status ?? 0, + json: wantJson ? maybeParseJson(stdout) : null, + durationMs, + degraded: false, + }; +} + +/** + * Async invocation with streaming. Required for `evolve` because the run + * can take 10+ minutes and the caller wants progress visibility. + * + * If `opts.onProgress(line)` is provided, called once per stderr line. + * darwin writes per-generation progress to stderr, structured result to + * stdout. Cancellable via opts.signal (AbortSignal). + */ +export function runDarwinAsync(args, opts = {}) { + return new Promise((resolve) => { + const start = Date.now(); + const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const wantJson = opts.json !== false; + const argv = buildArgv(args, wantJson); + const p = spawn('npx', ['-y', '-p', DARWIN_PIN, 'metaharness-darwin', ...argv], { + stdio: ['ignore', 'pipe', 'pipe'], + cwd: opts.cwd, + env: { ...process.env, ...(opts.env || {}) }, + shell: process.platform === 'win32', + }); + let stdout = '', stderr = '', stderrBuf = ''; + p.stdout?.on('data', (d) => { stdout += d.toString(); }); + p.stderr?.on('data', (d) => { + const s = d.toString(); + stderr += s; + if (opts.onProgress) { + stderrBuf += s; + const lines = stderrBuf.split('\n'); + stderrBuf = lines.pop() ?? ''; + for (const line of lines) opts.onProgress(line); + } + }); + const timer = setTimeout(() => { + try { p.kill('SIGTERM'); } catch { /* ignore */ } + }, timeoutMs); + if (opts.signal) { + opts.signal.addEventListener('abort', () => { + try { p.kill('SIGTERM'); } catch { /* ignore */ } + }, { once: true }); + } + p.on('error', (e) => { + clearTimeout(timer); + resolve({ + stdout, + stderr: stderr + String(e?.message ?? e), + exitCode: 127, json: null, durationMs: Date.now() - start, + degraded: true, reason: 'metaharness-darwin-not-available', + }); + }); + p.on('close', (code) => { + clearTimeout(timer); + const durationMs = Date.now() - start; + const classified = classifyDegraded(stderr, code); + if (classified.degraded) { + resolve({ + stdout, stderr, + exitCode: code ?? 127, json: null, durationMs, + degraded: true, reason: classified.reason, + }); + return; + } + resolve({ + stdout, stderr, + exitCode: code ?? 0, + json: wantJson ? maybeParseJson(stdout) : null, + durationMs, + degraded: false, + }); + }); + }); +} + +/** + * Match the existing `emitDegradedJsonAndExit` shape used by the + * metaharness-umbrella scripts so MCP tool consumers see one contract. + */ +export function emitDarwinDegradedJsonAndExit(reason) { + const payload = { + degraded: true, + reason, + hint: 'Install with `npm i -D ' + DARWIN_PIN + '` or run `npx -y ' + DARWIN_PIN + ' --help` to verify network access.', + generatedAt: new Date().toISOString(), + }; + console.log(JSON.stringify(payload, null, 2)); + // Exit 0 — ADR-150 architectural constraint: ruflo continues to function + // when MetaHarness is absent. Same posture as the umbrella scripts. + process.exit(0); +} + +export const DARWIN_VERSION_PIN = DARWIN_PIN; diff --git a/plugins/ruflo-metaharness/scripts/bench.mjs b/plugins/ruflo-metaharness/scripts/bench.mjs new file mode 100644 index 0000000000..7213a5718d --- /dev/null +++ b/plugins/ruflo-metaharness/scripts/bench.mjs @@ -0,0 +1,95 @@ +#!/usr/bin/env node +// bench.mjs — wrapper around `metaharness-darwin bench `. +// +// Bench suites are JSON files describing a corpus of (input, expected-output) +// tasks. `evolve --bench ` 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 { runDarwin, emitDarwinDegradedJsonAndExit } from './_darwin.mjs'; +import { existsSync } from 'node:fs'; +import { resolve } from 'node:path'; + +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); + } +} + +function main() { + safetyChecks(); + + 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(); diff --git a/plugins/ruflo-metaharness/scripts/evolve.mjs b/plugins/ruflo-metaharness/scripts/evolve.mjs new file mode 100644 index 0000000000..635c659e91 --- /dev/null +++ b/plugins/ruflo-metaharness/scripts/evolve.mjs @@ -0,0 +1,244 @@ +#!/usr/bin/env node +// evolve.mjs — wrapper around `metaharness-darwin evolve `. +// +// ADR-153: Darwin Mode is the WRITE layer that closes the loop ADR-150's +// READ layer (score / genome / mcp-scan / threat-model / oia-audit) opens. +// score+genome tell you where the harness IS; darwin evolve tells you which +// mutation makes it provably better, without retraining the foundation model. +// +// SAFETY (ADR-153 §"Safety model"): +// - Variant generation + sandbox happen under /.metaharness/variants/, +// never in the repo root. Upstream `inspectVariant()` rejects nested dirs, +// symlinks, secret-shaped strings, shell-out / network / dynamic-eval +// before any variant runs. Exit code 99 is reserved for "safety-disqualified". +// - --confirm is REQUIRED. Without it the script prints a plan and exits 0 +// (mirrors mint.mjs convention). This is in addition to upstream's safety +// layer — defense in depth at the ruflo boundary. +// - Default --generations 3 --children 3 (small) — anything larger is opt-in. +// Real evolutions are minutes-to-hours; ruflo's default sandbox config +// errs toward "show me the mechanism works" over "find a winner today". +// +// USAGE +// node scripts/evolve.mjs --repo . # dry-run plan +// node scripts/evolve.mjs --repo . --confirm # actually evolve +// node scripts/evolve.mjs --repo . --confirm --generations 5 --children 3 +// node scripts/evolve.mjs --repo . --confirm --sandbox mock # no real tests +// node scripts/evolve.mjs --repo . --confirm --selection pareto +// +// EXIT CODES +// 0 evolved OK (or dry-run, or degraded — MetaHarness Darwin not available) +// 1 --alert-on-no-improvement and champion did not beat parent +// 2 config error or evolution failure +// 99 reserved — upstream "safety-disqualified" (propagated) + +import { runDarwinAsync, emitDarwinDegradedJsonAndExit } from './_darwin.mjs'; +import { existsSync } from 'node:fs'; +import { resolve } from 'node:path'; + +const ARGS = (() => { + const a = { + repo: '.', + generations: 3, + children: 3, + concurrency: 2, + seed: null, + sandbox: 'real', + selection: null, + crossover: false, + epistasis: false, + curriculum: false, + riskBudget: null, + fdr: null, + tie: null, + bench: null, + mutator: 'deterministic', + ruvllmUrl: null, + ruvllmModel: null, + confirm: false, + alertOnNoImprovement: false, + format: 'json', + timeoutMs: null, // computed below if unset + }; + for (let i = 2; i < process.argv.length; i++) { + const v = process.argv[i]; + if (v === '--repo') a.repo = process.argv[++i]; + else if (v === '--generations') a.generations = parseInt(process.argv[++i], 10); + else if (v === '--children') a.children = parseInt(process.argv[++i], 10); + else if (v === '--concurrency') a.concurrency = parseInt(process.argv[++i], 10); + else if (v === '--seed') a.seed = parseInt(process.argv[++i], 10); + else if (v === '--sandbox') a.sandbox = process.argv[++i]; + else if (v === '--selection') a.selection = process.argv[++i]; + else if (v === '--crossover') a.crossover = true; + else if (v === '--epistasis') a.epistasis = true; + else if (v === '--curriculum') a.curriculum = true; + else if (v === '--risk-budget') a.riskBudget = parseInt(process.argv[++i], 10); + else if (v === '--fdr') a.fdr = parseFloat(process.argv[++i]); + else if (v === '--tie') a.tie = process.argv[++i]; + else if (v === '--bench') a.bench = process.argv[++i]; + else if (v === '--mutator') a.mutator = process.argv[++i]; + else if (v === '--ruvllm-url') a.ruvllmUrl = process.argv[++i]; + else if (v === '--ruvllm-model') a.ruvllmModel = process.argv[++i]; + else if (v === '--confirm') a.confirm = true; + else if (v === '--alert-on-no-improvement') a.alertOnNoImprovement = true; + else if (v === '--format') a.format = process.argv[++i]; + else if (v === '--timeout-ms') a.timeoutMs = parseInt(process.argv[++i], 10); + } + return a; +})(); + +function safetyChecks() { + const repoPath = resolve(ARGS.repo); + if (!existsSync(repoPath)) { + console.error(`evolve: repo path does not exist: ${repoPath}`); + process.exit(2); + } + if (ARGS.generations < 1 || ARGS.generations > 50) { + console.error('evolve: --generations must be 1..50 (ruflo cap; upstream supports more)'); + process.exit(2); + } + if (ARGS.children < 1 || ARGS.children > 20) { + console.error('evolve: --children must be 1..20 (ruflo cap)'); + process.exit(2); + } + if (ARGS.concurrency < 1 || ARGS.concurrency > 8) { + console.error('evolve: --concurrency must be 1..8 (ruflo cap)'); + process.exit(2); + } + if (!['real', 'mock', 'agent'].includes(ARGS.sandbox)) { + console.error(`evolve: --sandbox must be real|mock|agent (got: ${ARGS.sandbox})`); + process.exit(2); + } + if (ARGS.selection && !['quality-diversity', 'behavioral-diversity', 'niche-steering', 'clade', 'pareto'].includes(ARGS.selection)) { + console.error(`evolve: --selection must be one of quality-diversity|behavioral-diversity|niche-steering|clade|pareto`); + process.exit(2); + } + if (!['deterministic', 'ruvllm'].includes(ARGS.mutator)) { + console.error(`evolve: --mutator must be deterministic|ruvllm`); + process.exit(2); + } + return repoPath; +} + +// Compute a sensible timeout from the search shape if caller didn't specify. +// Rough budget: each variant ~= 30s (sandbox test command + safety inspect), +// total ~= generations × children × per-variant / concurrency. +function defaultTimeoutMs() { + const perVariantMs = ARGS.sandbox === 'mock' ? 2_000 : 60_000; + const variants = ARGS.generations * ARGS.children; + const parallelism = Math.min(ARGS.concurrency, variants); + const wall = Math.ceil(variants / parallelism) * perVariantMs; + // Add 30s overhead for npm install + initial profile + final report. + return Math.max(60_000, wall + 30_000); +} + +async function main() { + const repoPath = safetyChecks(); + + const plan = { + binary: 'metaharness-darwin evolve', + repo: repoPath, + generations: ARGS.generations, + children: ARGS.children, + concurrency: ARGS.concurrency, + sandbox: ARGS.sandbox, + selection: ARGS.selection, + crossover: ARGS.crossover, + epistasis: ARGS.epistasis, + curriculum: ARGS.curriculum, + mutator: ARGS.mutator, + estVariants: ARGS.generations * ARGS.children, + timeoutMs: ARGS.timeoutMs ?? defaultTimeoutMs(), + output: `${repoPath}/.metaharness/{archive.json, lineage.json, variants/, runs/, reports/winner.json}`, + }; + + if (!ARGS.confirm) { + const payload = { + success: true, + data: { plan, dryRun: true, message: 'Pass --confirm to run the evolution.' }, + generatedAt: new Date().toISOString(), + }; + console.log(JSON.stringify(payload, null, 2)); + process.exit(0); + } + + const cliArgs = ['evolve', repoPath, + '--generations', String(ARGS.generations), + '--children', String(ARGS.children), + '--concurrency', String(ARGS.concurrency), + '--sandbox', ARGS.sandbox, + '--mutator', ARGS.mutator, + ]; + if (ARGS.seed != null) cliArgs.push('--seed', String(ARGS.seed)); + if (ARGS.selection) cliArgs.push('--selection', ARGS.selection); + if (ARGS.crossover) cliArgs.push('--crossover'); + if (ARGS.epistasis) cliArgs.push('--epistasis'); + if (ARGS.curriculum) cliArgs.push('--curriculum'); + if (ARGS.riskBudget != null) cliArgs.push('--risk-budget', String(ARGS.riskBudget)); + if (ARGS.fdr != null) cliArgs.push('--fdr', String(ARGS.fdr)); + if (ARGS.tie) cliArgs.push('--tie', ARGS.tie); + if (ARGS.bench) cliArgs.push('--bench', ARGS.bench); + if (ARGS.ruvllmUrl) cliArgs.push('--ruvllm-url', ARGS.ruvllmUrl); + if (ARGS.ruvllmModel) cliArgs.push('--ruvllm-model', ARGS.ruvllmModel); + + // Forward progress lines to stderr so the user sees per-generation activity + // (subprocess-of-an-MCP-tool case: this still surfaces in the agent log). + const r = await runDarwinAsync(cliArgs, { + timeoutMs: plan.timeoutMs, + onProgress: (line) => { if (line.trim()) process.stderr.write(`[evolve] ${line}\n`); }, + }); + + if (r.degraded) { + emitDarwinDegradedJsonAndExit(r.reason); + return; + } + + // Upstream exit code 99 = safety-disqualified — propagate verbatim so + // CI gates can distinguish "evolution failed" from "evolution surfaced + // a safety-tripping mutation". This is a designed-in tripwire, not an + // error the ruflo layer should remap. + if (r.exitCode === 99) { + const payload = { + success: false, + data: { safetyDisqualified: true, hint: 'A variant tripped the safety inspection layer. See /.metaharness/runs/ for which surface and pattern.' }, + stderrTail: r.stderr.slice(-400), + durationMs: r.durationMs, + generatedAt: new Date().toISOString(), + }; + console.log(JSON.stringify(payload, null, 2)); + process.exit(99); + } + + if (r.exitCode !== 0) { + 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 champion = r.json || {}; + const noImprovement = champion.parentScore != null && champion.championScore != null && + champion.championScore <= champion.parentScore; + + const payload = { + success: true, + data: { + ...champion, + plan, + durationMs: r.durationMs, + improved: !noImprovement, + }, + generatedAt: new Date().toISOString(), + }; + + console.log(JSON.stringify(payload, null, 2)); + if (ARGS.alertOnNoImprovement && noImprovement) process.exit(1); + process.exit(0); +} + +main().catch((e) => { + console.error(`evolve: unexpected failure: ${e?.message ?? e}`); + process.exit(2); +}); diff --git a/plugins/ruflo-metaharness/scripts/security-bench.mjs b/plugins/ruflo-metaharness/scripts/security-bench.mjs new file mode 100644 index 0000000000..436ac11c6d --- /dev/null +++ b/plugins/ruflo-metaharness/scripts/security-bench.mjs @@ -0,0 +1,174 @@ +#!/usr/bin/env node +// security-bench.mjs — wrapper around `metaharness-darwin security bench`. +// +// "Darwin Shield" — upstream's own ADR-155 — evolves a champion harness +// on a 10-vuln/9-decoy corpus and measures it against four baselines +// (B0 static-only, B1 LLM single-pass, B2 fixed agent, B3 Darwin champion). +// The CHAMPION reaches TPR=1, FPR=0; the eight acceptance gates verify +// reproducibility, statistical significance, and unsafe-output=0. +// +// CONNECTION TO RUFLO ADR-155 +// =========================== +// ruflo's ADR-155 (#2417) proposes a nightly self-learning security harness +// with three learning loops (per-dimension confidence weighting, severity +// calibration, auto-fix bid). The upstream Darwin Shield is the closest +// reference implementation — same shape, different scope (evolves a +// security-detection harness vs evaluates findings; both grade by realized +// TPR/FPR vs ground-truth corpus). Running `security bench` periodically +// gives us the empirical baseline that ruflo's Phase 2 loop A needs before +// training: if Darwin Shield converges on a known-good corpus, the loop A +// gradient signal is sound; if it doesn't, the corpus / sandbox is the +// gap, not the learning algorithm. +// +// USAGE +// node scripts/security-bench.mjs # default population=2 cycles=1 +// node scripts/security-bench.mjs --population 4 --cycles 3 # deeper run +// node scripts/security-bench.mjs --population 4 --cycles 3 --alert-on-fail +// +// EXIT CODES +// 0 bench passed all gates (or degraded — Darwin not available) +// 1 --alert-on-fail and any acceptance gate failed +// 2 config error or bench infrastructure failure + +import { runDarwinAsync, emitDarwinDegradedJsonAndExit } from './_darwin.mjs'; + +const ARGS = (() => { + const a = { + population: 2, + cycles: 1, + seed: null, + alertOnFail: false, + format: 'json', + timeoutMs: null, + }; + for (let i = 2; i < process.argv.length; i++) { + const v = process.argv[i]; + if (v === '--population') a.population = parseInt(process.argv[++i], 10); + else if (v === '--cycles') a.cycles = parseInt(process.argv[++i], 10); + else if (v === '--seed') a.seed = parseInt(process.argv[++i], 10); + else if (v === '--alert-on-fail') a.alertOnFail = true; + else if (v === '--format') a.format = process.argv[++i]; + else if (v === '--timeout-ms') a.timeoutMs = parseInt(process.argv[++i], 10); + } + return a; +})(); + +function safetyChecks() { + if (ARGS.population < 1 || ARGS.population > 20) { + console.error('security-bench: --population must be 1..20 (ruflo cap)'); + process.exit(2); + } + if (ARGS.cycles < 1 || ARGS.cycles > 100) { + console.error('security-bench: --cycles must be 1..100 (ruflo cap)'); + process.exit(2); + } +} + +function defaultTimeoutMs() { + // Bench corpus has 10 vulns + 9 decoys = 19 evaluations per cycle. + // Each evaluation runs the candidate detector + scores patches. + // Rough budget: ~3s per evaluation × population × cycles + 30s overhead. + return Math.max(60_000, 3_000 * 19 * ARGS.population * ARGS.cycles + 30_000); +} + +// Parse the markdown report that `security bench` emits. The header +// "Overall: ✅ PASS" or "❌ FAIL" is the rolled-up gate. We also extract +// each acceptance gate's pass/fail line. +function parseSecurityBenchMarkdown(stdout) { + const overallMatch = /\*\*Overall:\s*([✅❌])\s*(PASS|FAIL)\*\*/i.exec(stdout); + const overall = overallMatch ? { ok: overallMatch[2].toUpperCase() === 'PASS', icon: overallMatch[1] } : null; + + const gates = []; + // Match lines like: "- ✅ **TPR improvement ≥ 25% vs fixed harness** — +150%" + const gateRx = /^-\s+([✅❌])\s+\*\*(.+?)\*\*\s*[—-]\s*(.+)$/gm; + let m; + while ((m = gateRx.exec(stdout)) !== null) { + gates.push({ + ok: m[1] === '✅', + criterion: m[2].trim(), + measured: m[3].trim(), + }); + } + + // Extract the baselines-vs-champion table — useful for diff over time. + const baselines = []; + const tableRx = /^\|\s*B[0-3]\s+([^|]+?)\s*\|\s*([\d.]+)\s*\|\s*([\d.]+)\s*\|\s*([\d.]+)\s*\|\s*([\d.]+)\s*\|\s*([\d.]+)\s*\|\s*(\d+)\s*\|\s*(.+?)\s*\|/gm; + while ((m = tableRx.exec(stdout)) !== null) { + baselines.push({ + harness: m[1].trim(), + fitness: parseFloat(m[2]), + tpr: parseFloat(m[3]), + fpr: parseFloat(m[4]), + patchPass: parseFloat(m[5]), + repro: parseFloat(m[6]), + unsafe: parseInt(m[7], 10), + cost: m[8].trim(), + }); + } + + return { overall, gates, baselines }; +} + +async function main() { + safetyChecks(); + + const cliArgs = ['security', 'bench', + '--population', String(ARGS.population), + '--cycles', String(ARGS.cycles), + ]; + if (ARGS.seed != null) cliArgs.push('--seed', String(ARGS.seed)); + + const r = await runDarwinAsync(cliArgs, { + timeoutMs: ARGS.timeoutMs ?? defaultTimeoutMs(), + json: false, // bench output is markdown, not JSON + onProgress: (line) => { if (line.trim()) process.stderr.write(`[security-bench] ${line}\n`); }, + }); + + if (r.degraded) { + emitDarwinDegradedJsonAndExit(r.reason); + return; + } + + if (r.exitCode !== 0 && r.exitCode !== 1) { + // Exit 1 may be the bench's own "gates failed" signal; treat as data. + // Anything else is infrastructure failure. + 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 parsed = parseSecurityBenchMarkdown(r.stdout); + const gatesPassed = parsed.gates.filter((g) => g.ok).length; + const gatesFailed = parsed.gates.length - gatesPassed; + + const payload = { + success: true, + data: { + overall: parsed.overall, + gates: { + total: parsed.gates.length, + passed: gatesPassed, + failed: gatesFailed, + details: parsed.gates, + }, + baselines: parsed.baselines, + rawMarkdown: r.stdout, + shape: { population: ARGS.population, cycles: ARGS.cycles, seed: ARGS.seed }, + durationMs: r.durationMs, + }, + generatedAt: new Date().toISOString(), + }; + + console.log(JSON.stringify(payload, null, 2)); + if (ARGS.alertOnFail && parsed.overall && !parsed.overall.ok) process.exit(1); + process.exit(0); +} + +main().catch((e) => { + console.error(`security-bench: unexpected failure: ${e?.message ?? e}`); + process.exit(2); +}); diff --git a/plugins/ruflo-metaharness/scripts/test-graceful-degradation.mjs b/plugins/ruflo-metaharness/scripts/test-graceful-degradation.mjs index fa182be325..dc8877f119 100755 --- a/plugins/ruflo-metaharness/scripts/test-graceful-degradation.mjs +++ b/plugins/ruflo-metaharness/scripts/test-graceful-degradation.mjs @@ -111,6 +111,14 @@ function main() { { name: 'mcp-scan', args: ['--format', 'json'] }, { name: 'threat-model', args: ['--format', 'json'] }, { name: 'oia-audit', args: ['--dry-run', '--format', 'json'] }, + // ADR-153 — darwin scripts (separate optional dep @metaharness/darwin). + // evolve.mjs without --confirm short-circuits to a dry-run plan BEFORE + // any subprocess call, so it's not exercising the degraded path. Pass + // --confirm with --sandbox mock + minimal shape so we hit the subprocess + // path quickly and verify the {degraded: true} emit on registry-unreachable. + { name: 'evolve', args: ['--repo', '.', '--confirm', '--sandbox', 'mock', '--generations', '1', '--children', '1', '--concurrency', '1', '--timeout-ms', '60000'] }, + { name: 'security-bench', args: ['--population', '1', '--cycles', '1', '--timeout-ms', '60000'] }, + { name: 'bench', args: ['--op', 'verify', '--suite', '/dev/null'] }, ]; for (const s of skills) { diff --git a/plugins/ruflo-metaharness/skills/harness-bench/SKILL.md b/plugins/ruflo-metaharness/skills/harness-bench/SKILL.md new file mode 100644 index 0000000000..0981031957 --- /dev/null +++ b/plugins/ruflo-metaharness/skills/harness-bench/SKILL.md @@ -0,0 +1,64 @@ +--- +name: harness-bench +description: Manage `@metaharness/darwin` bench suites — `bench create ` scaffolds a JSON suite from a repo's test corpus; `bench verify ` checks suite well-formedness. Bench suites are the fixed evaluation corpora that `harness-evolve --bench ` scores variants against, decoupling evolution from the repo's natural tests. Degrades gracefully when @metaharness/darwin is absent. +argument-hint: "--op create --repo [--out ] | --op verify --suite " +allowed-tools: Bash +--- + +Surfaces `metaharness-darwin bench ` — the supporting verb +for `harness-evolve --bench`. Use when you want evolution scored against a +fixed corpus (independent of `npm test`) so champion fitness is comparable +across commits or across forks of the same harness. + +## When to use + +- Setting up a new evolution pipeline for a repo whose `npm test` is + flaky, slow, or undersized — scaffold a deterministic bench suite once, + then evolve against it repeatedly. +- CI: `bench verify` the checked-in suite on every PR that touches it + (cheap; ~5s). +- Forking a harness to a new domain: copy and edit the suite to retarget + the evaluation without losing comparability to the parent. + +## Algorithm + +Implementation: [`scripts/bench.mjs`](../../scripts/bench.mjs). + +### `--op create` +1. Resolve `--repo` path; reject if missing. +2. Shell to `metaharness-darwin bench create [--out ]`. +3. Default output path: `/.metaharness/bench/suite.json` (chosen by upstream). +4. Suite shape (per upstream): array of `{ input, expectedOutput, weight }` tasks + derived from existing test cases. + +### `--op verify` +1. Resolve `--suite` path; reject if missing. +2. Shell to `metaharness-darwin bench verify `. +3. Exit 1 if any task malformed (upstream's signal). + +## Output shape + +```json +{ + "success": true, + "data": { + "op": "verify", + "taskCount": 42, + "wellFormed": true, + "durationMs": 870 + } +} +``` + +## Exit codes + +| Code | Meaning | +|---|---| +| 0 | OK (or degraded — Darwin absent) | +| 1 | `--op verify` and suite malformed | +| 2 | Config error or upstream invocation failure | + +## Graceful degradation + +When `@metaharness/darwin` is absent, emits the standard `{degraded: true, +reason: 'metaharness-darwin-not-available'}` payload and exits 0. diff --git a/plugins/ruflo-metaharness/skills/harness-evolve/SKILL.md b/plugins/ruflo-metaharness/skills/harness-evolve/SKILL.md new file mode 100644 index 0000000000..37ede12de6 --- /dev/null +++ b/plugins/ruflo-metaharness/skills/harness-evolve/SKILL.md @@ -0,0 +1,92 @@ +--- +name: harness-evolve +description: Run `@metaharness/darwin evolve ` to mutate a harness's seven policy surfaces (planner/contextBuilder/reviewer/retryPolicy/toolPolicy/memoryPolicy/scorePolicy), sandbox-score each variant, and promote only measured wins. The model is frozen; the harness evolves. Closes the loop ADR-150 opens (score+genome describe; evolve changes). Degrades gracefully when @metaharness/darwin is absent (ADR-150 + ADR-153 architectural constraints). +argument-hint: "--repo [--generations 3] [--children 3] [--concurrency 2] [--sandbox real|mock|agent] [--selection pareto|quality-diversity|...] [--mutator deterministic|ruvllm] [--confirm]" +allowed-tools: Bash +--- + +Surfaces the upstream `metaharness-darwin evolve` CLI as a ruflo skill. The +**write** layer that pairs with ADR-150's read layer (score / genome / +mcp-scan / threat-model / oia-audit). Use when you have a harness whose +readiness scores are flat and you want to discover *which* surface mutation +moves them — without retraining the foundation model. + +## When to use + +- A `harness-score` result is below target and you don't know which policy + surface is responsible. +- You're seeding a harness for a new vertical and want to find a good + starting configuration empirically rather than hand-tuning. +- You're comparing your hand-tuned harness against an evolved baseline + (treat darwin's champion as the strawman). + +## When NOT to use + +- For continuous background optimization. Darwin Mode is human-initiated. + Wire it into CI for one-shot exploration, not for autonomous self-modification. +- For ruflo itself in CI. ADR-153 §5 explicitly rejects auto-evolving ruflo + — the CI gate verifies graceful degradation, not convergence. + +## Algorithm + +Implementation: [`scripts/evolve.mjs`](../../scripts/evolve.mjs). + +1. Validate args (`--repo` exists, caps on `--generations` ≤ 50, `--children` + ≤ 20, `--concurrency` ≤ 8, sandbox/selection/mutator are known values). +2. Without `--confirm`: print plan + exit 0 (mirrors `harness-mint` safety + convention; defense in depth over the upstream `safety.ts` checks). +3. With `--confirm`: shell to `npx -y @metaharness/darwin@~0.3.1 metaharness-darwin evolve ...` + via the shared `_darwin.mjs` async helper. Per-generation progress is + forwarded to stderr; final champion JSON is captured from stdout. +4. Compute timeout from `generations × children × per-variant` (per-variant + ≈ 60s real, ≈ 2s mock). Caller may override with `--timeout-ms`. +5. Honor upstream exit code 99 — propagate as "safety-disqualified", do not + remap. This is a designed-in tripwire (a variant tripped `inspectVariant` + for secrets / shell-out / network / dynamic-eval). See ADR-153 §"Safety model". +6. Optional `--alert-on-no-improvement`: exit 1 when champion ≤ parent. + +## The seven mutation surfaces + +| Surface | What it owns | +|---|---| +| `planner` | task decomposition / step ordering | +| `contextBuilder` | what gets fed into the prompt | +| `reviewer` | self-critique / output verification | +| `retryPolicy` | when + how to retry on failure | +| `toolPolicy` | which tools the agent may use, under which conditions | +| `memoryPolicy` | what to persist, recall, forget | +| `scorePolicy` | how the agent grades its own output | + +One mutation per variant. Multi-surface mutations are not allowed (causal +attribution stays clean). + +## Output + +Reports land under `/.metaharness/`: + +``` +.metaharness/ + archive.json # full lineage tree (sampling next gen draws from this) + lineage.json # parent→child edges only + variants// # per-variant code (kept for audit) + runs// # per-variant sandbox test output + reports/winner.json # final champion + score delta vs parent +``` + +Skill stdout = JSON `{success, data: {champion, plan, durationMs, improved}}`. + +## Exit codes + +| Code | Meaning | +|---|---| +| 0 | Evolved OK, or dry-run, or degraded (Darwin absent) | +| 1 | `--alert-on-no-improvement` and champion did not beat parent | +| 2 | Config error or evolution infrastructure failure | +| 99 | Upstream "safety-disqualified" (PROPAGATED, not remapped) | + +## Graceful degradation (ADR-150 constraint 3 + ADR-153) + +When `@metaharness/darwin` is not installed, the script emits +`{degraded: true, reason: 'metaharness-darwin-not-available', hint: ...}` +and exits 0. ruflo continues to function. CI's +`no-metaharness-smoke.yml`-style job asserts this path. diff --git a/plugins/ruflo-metaharness/skills/harness-security-bench/SKILL.md b/plugins/ruflo-metaharness/skills/harness-security-bench/SKILL.md new file mode 100644 index 0000000000..c6f34dde87 --- /dev/null +++ b/plugins/ruflo-metaharness/skills/harness-security-bench/SKILL.md @@ -0,0 +1,101 @@ +--- +name: harness-security-bench +description: Run `@metaharness/darwin security bench` (upstream "Darwin Shield" / ADR-155) — evolves a champion security-detection harness against a 10-vuln / 9-decoy corpus and grades it on TPR/FPR/patch-pass/repro/unsafe vs four baselines (B0 static, B1 LLM-single-pass, B2 fixed-agent, B3 Darwin-champion). Closest reference implementation for ruflo's own ADR-155 nightly self-learning security harness (PR #2417). Degrades gracefully when @metaharness/darwin is absent. +argument-hint: "[--population 2] [--cycles 1] [--seed N] [--alert-on-fail]" +allowed-tools: Bash +--- + +Surfaces the upstream `metaharness-darwin security bench` command. **This is +the upstream's own ADR-155 — Darwin Shield — and is the closest reference +implementation for ruflo's nightly self-learning security harness ([#2417](https://github.com/ruvnet/ruflo/pull/2417)).** + +## Why this matters for ruflo's ADR-155 + +ruflo's ADR-155 proposes three learning loops (per-dimension confidence, +severity calibration, auto-fix bid). Loop A trains on accumulated +`(finding, dimension, human_outcome)` tuples — but the gradient signal is +only sound if the underlying detection mechanism converges on a known-good +corpus. Darwin Shield evolves exactly that mechanism on a 10-vuln/9-decoy +ground-truth set. Running this nightly gives us: + +- **Empirical floor:** if Darwin Shield's champion can't reach + TPR=1/FPR=0 on the bench corpus, our Loop A's reward signal is noise. +- **Drift detection:** week-over-week champion fitness deltas surface + when the security landscape (or our mutator policy) shifts. +- **Baseline diversity:** the 4 baselines (B0–B3) give us 4 anchor + points to weight per-dimension confidence against. + +## Algorithm + +Implementation: [`scripts/security-bench.mjs`](../../scripts/security-bench.mjs). + +1. Shell to `npx -y @metaharness/darwin@~0.3.1 metaharness-darwin security bench --population N --cycles N [--seed S]`. +2. Default timeout = `3s × 19 evaluations × population × cycles + 30s overhead`. + At default `--population 2 --cycles 1` ≈ 144s; at `--population 4 --cycles 3` ≈ 12 min. +3. Parse the markdown report — overall PASS/FAIL plus per-gate + pass/fail rows (gate examples: "TPR improvement ≥ 25% vs fixed", + "FPR reduction ≥ 40%", "Patch-test pass rate ≥ 80%", "Reproduction + success ≥ 90%", "Unsafe outputs = 0", "Cost increase ≤ 2× fixed", + "Beyond SOTA: champion statistically beats previous champion", + "Compounding: false-positive repeat-rate drop ≥ 35%"). +4. Parse the baselines-vs-champion table (4 rows: fitness/TPR/FPR/patchPass/ + repro/unsafe/cost per harness). +5. Emit structured JSON. With `--alert-on-fail`, exit 1 when overall = FAIL. + +## Output shape + +```json +{ + "success": true, + "data": { + "overall": { "ok": true, "icon": "✅" }, + "gates": { + "total": 11, + "passed": 11, + "failed": 0, + "details": [{ "ok": true, "criterion": "TPR improvement ≥ 25% vs fixed harness", "measured": "+150% (B2 0.4 → B3 1)" }, ...] + }, + "baselines": [ + { "harness": "static-only", "fitness": 0.5665, "tpr": 0.3, "fpr": 1, "unsafe": 0, ... }, + { "harness": "LLM single-pass", "fitness": 0.1365, ... }, + { "harness": "fixed agent", "fitness": 0.598, ... }, + { "harness": "Darwin champion", "fitness": 0.93275, "tpr": 1, "fpr": 0, ... } + ], + "rawMarkdown": "...", + "shape": { "population": 2, "cycles": 1, "seed": null }, + "durationMs": 142000 + } +} +``` + +## Wiring into ADR-155 nightly harness + +The ADR-155 nightly workflow (per #2418 task `W1.5`) will spawn this as +one of the active-pentest dimension's calls — its results become a +trajectory record: + +```jsonc +{ + "dimension": "mcp-pentest", + "subdimension": "darwin-shield-bench", + "champion_fitness": 0.93275, + "champion_tpr": 1, "champion_fpr": 0, + "gates_passed": 11, "gates_failed": 0, + "shape": { "population": 4, "cycles": 3 } +} +``` + +Loop A learns: if `darwin-shield-bench` consistently passes on the seeded +corpus, weight findings caught only by `mcp-pentest` higher. + +## Exit codes + +| Code | Meaning | +|---|---| +| 0 | Bench ran (overall PASS or FAIL — distinguish via JSON `overall.ok`), or degraded | +| 1 | `--alert-on-fail` and `overall.ok === false` | +| 2 | Config error or upstream infrastructure failure | + +## Graceful degradation + +When `@metaharness/darwin` is absent, emits `{degraded: true, reason: 'metaharness-darwin-not-available'}` and exits 0. diff --git a/ruflo/package.json b/ruflo/package.json index 80a597cb1a..77e9c4c092 100644 --- a/ruflo/package.json +++ b/ruflo/package.json @@ -43,9 +43,10 @@ "@claude-flow/cli": "^3.10.3" }, "optionalDependencies": { - "metaharness": "~0.1.11", + "metaharness": "~0.2.6", "@metaharness/router": "~0.3.2", - "@metaharness/kernel": "~0.1.0" + "@metaharness/kernel": "~0.1.0", + "@metaharness/darwin": "~0.3.1" }, "overrides": { "ruvector": "^0.2.27", diff --git a/v3/@claude-flow/cli/package.json b/v3/@claude-flow/cli/package.json index dbcbd2c525..1a8c4279f7 100644 --- a/v3/@claude-flow/cli/package.json +++ b/v3/@claude-flow/cli/package.json @@ -113,9 +113,10 @@ "@claude-flow/memory": "^3.0.0-alpha.20", "@claude-flow/plugin-gastown-bridge": "^0.1.3", "@claude-flow/security": "^3.0.0-alpha.10", + "@metaharness/darwin": "~0.3.1", "@metaharness/kernel": "~0.1.0", "@metaharness/router": "~0.3.2", - "metaharness": "~0.1.11", + "metaharness": "~0.2.6", "@ruvector/attention": "^0.1.32", "@ruvector/attention-darwin-arm64": "0.1.32", "@ruvector/diskann": "^0.1.0", diff --git a/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts index 5b392029e0..ae950633e9 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts @@ -11,6 +11,13 @@ * - metaharness_threat_model enterprise-grade threat model * - metaharness_oia_audit composite audit (score + threat + mcp) → memory * + * ADR-153 Darwin Mode integration adds three additional tools that target + * the separate `@metaharness/darwin` npm package (not the umbrella): + * + * - metaharness_evolve mutate harness policy surfaces, sandbox-score, promote + * - metaharness_security_bench upstream's "Darwin Shield" (their own ADR-155) + * - metaharness_bench create/verify bench suites used by evolve --bench + * * Every tool resolves the corresponding plugin script * (`plugins/ruflo-metaharness/scripts/.mjs`) via the same locator * the commands/metaharness.ts dispatcher uses, then spawns it with @@ -348,4 +355,113 @@ export const metaharnessTools: MCPTool[] = [ return { success: r.success, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; }, }, + // ─────────────────────────────────────────────────────────────────────── + // ADR-153 — @metaharness/darwin integration (3 tools). + // Backed by the separate `@metaharness/darwin@~0.3.1` npm package, NOT + // the umbrella `metaharness`. Plugin scripts shell out via _darwin.mjs. + // Same {success, data, degraded, exitCode} contract. + // ─────────────────────────────────────────────────────────────────────── + { + name: 'metaharness_evolve', + description: 'ADR-153 — Darwin Mode: mutate one of seven harness policy surfaces (planner/contextBuilder/reviewer/retryPolicy/toolPolicy/memoryPolicy/scorePolicy), sandbox-score each variant, promote only measured wins. The WRITE layer that closes the loop ADR-150 opens (score+genome describe; evolve changes). Use when readiness scores are flat and you want to discover WHICH surface mutation moves them, without retraining the foundation model. Bypassing this tool and hand-tuning is wrong because (a) single-degree-of-freedom mutations keep causal attribution clean, (b) the upstream safety layer catches secret/shell-out/network/dynamic-eval patterns before any variant runs (exit 99 = safety-disqualified, propagated verbatim). REQUIRES --confirm; defaults to dry-run plan output. Long-running: timeout scales with generations×children×sandbox-cost. ' + MCP_SUCCESS_SEMANTIC, + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + repo: { type: 'string', description: 'Repo path to evolve (default: cwd)', default: '.' }, + generations: { type: 'number', description: '1..50 (ruflo cap)', default: 3 }, + children: { type: 'number', description: '1..20 (ruflo cap) — variants per generation', default: 3 }, + concurrency: { type: 'number', description: '1..8 (ruflo cap)', default: 2 }, + seed: { type: 'number', description: 'PRNG seed for reproducibility' }, + sandbox: { type: 'string', enum: ['real', 'mock', 'agent'], description: 'real = run npm test; mock = scoring stub; agent = LLM judge', default: 'real' }, + selection: { type: 'string', enum: ['quality-diversity', 'behavioral-diversity', 'niche-steering', 'clade', 'pareto'], description: 'Next-generation sampling strategy from the archive tree' }, + crossover: { type: 'boolean', description: 'Enable crossover (2-parent) mutations alongside the default 1-parent path', default: false }, + epistasis: { type: 'boolean', description: 'Detect epistatic surface interactions before mutating', default: false }, + curriculum: { type: 'boolean', description: 'Schedule increasing-difficulty bench tasks across generations', default: false }, + riskBudget: { type: 'number', description: 'Max number of safety-near-miss variants allowed before halting' }, + fdr: { type: 'number', description: 'Benjamini-Hochberg FDR threshold for accepting variant fitness as significant' }, + tie: { type: 'string', enum: ['faster'], description: 'Tiebreaker when champions are within noise — "faster" prefers lower sandbox cost' }, + bench: { type: 'string', description: 'Path to a bench suite JSON (use metaharness_bench --op create to scaffold)' }, + mutator: { type: 'string', enum: ['deterministic', 'ruvllm'], description: 'deterministic = template-based; ruvllm = local LLM-driven', default: 'deterministic' }, + ruvllmUrl: { type: 'string', description: 'RuVLLM endpoint URL (only used when mutator=ruvllm)' }, + ruvllmModel: { type: 'string', description: 'RuVLLM model id (only used when mutator=ruvllm)' }, + confirm: { type: 'boolean', description: 'REQUIRED to actually evolve; without it, returns a dry-run plan', default: false }, + alertOnNoImprovement: { type: 'boolean', description: 'Exit 1 when champion ≤ parent', default: false }, + timeoutMs: { type: 'number', description: 'Override the computed timeout (default = generations×children×per-variant)' }, + }, + }, + handler: async (input) => { + const args: string[] = []; + if (input.repo) args.push('--repo', String(input.repo)); + if (input.generations !== undefined) args.push('--generations', String(input.generations)); + if (input.children !== undefined) args.push('--children', String(input.children)); + if (input.concurrency !== undefined) args.push('--concurrency', String(input.concurrency)); + if (input.seed !== undefined) args.push('--seed', String(input.seed)); + if (input.sandbox) args.push('--sandbox', String(input.sandbox)); + if (input.selection) args.push('--selection', String(input.selection)); + if (input.crossover === true) args.push('--crossover'); + if (input.epistasis === true) args.push('--epistasis'); + if (input.curriculum === true) args.push('--curriculum'); + if (input.riskBudget !== undefined) args.push('--risk-budget', String(input.riskBudget)); + if (input.fdr !== undefined) args.push('--fdr', String(input.fdr)); + if (input.tie) args.push('--tie', String(input.tie)); + if (input.bench) args.push('--bench', String(input.bench)); + if (input.mutator) args.push('--mutator', String(input.mutator)); + if (input.ruvllmUrl) args.push('--ruvllm-url', String(input.ruvllmUrl)); + if (input.ruvllmModel) args.push('--ruvllm-model', String(input.ruvllmModel)); + if (input.confirm === true) args.push('--confirm'); + if (input.alertOnNoImprovement === true) args.push('--alert-on-no-improvement'); + if (input.timeoutMs !== undefined) args.push('--timeout-ms', String(input.timeoutMs)); + const r = await runScript('evolve.mjs', args); + return { success: r.success, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_security_bench', + description: 'ADR-153 — upstream Darwin Shield (their own ADR-155): evolves a champion security-detection harness against a 10-vuln/9-decoy ground-truth corpus and grades on TPR/FPR/patch-pass/repro/unsafe vs four baselines (B0 static, B1 LLM-single-pass, B2 fixed-agent, B3 Darwin-champion). Closest reference implementation for ruflo ADR-155 nightly self-learning security harness (#2417). Use when you need an empirical floor for Loop A reward-signal soundness; running this periodically gives baseline diversity and week-over-week champion-fitness drift. Bypassing this and just running the static MCP scan is wrong because static-only baseline (B0) reaches TPR=0.3/FPR=1 — proving static-alone has a measured detection ceiling. Parses overall PASS/FAIL + per-gate verdicts + baselines table from markdown. ' + MCP_SUCCESS_SEMANTIC, + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + population: { type: 'number', description: '1..20 (ruflo cap) — candidate detectors per cycle', default: 2 }, + cycles: { type: 'number', description: '1..100 (ruflo cap) — evolution cycles', default: 1 }, + seed: { type: 'number', description: 'PRNG seed for reproducibility' }, + alertOnFail: { type: 'boolean', description: 'Exit 1 when overall verdict is FAIL', default: false }, + timeoutMs: { type: 'number', description: 'Override the computed timeout (default = 3s × 19 evals × population × cycles + 30s)' }, + }, + }, + handler: async (input) => { + const args: string[] = []; + if (input.population !== undefined) args.push('--population', String(input.population)); + if (input.cycles !== undefined) args.push('--cycles', String(input.cycles)); + if (input.seed !== undefined) args.push('--seed', String(input.seed)); + if (input.alertOnFail === true) args.push('--alert-on-fail'); + if (input.timeoutMs !== undefined) args.push('--timeout-ms', String(input.timeoutMs)); + const r = await runScript('security-bench.mjs', args); + return { success: r.success, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_bench', + description: 'ADR-153 supporting verb — create or verify bench suites used by metaharness_evolve --bench. Bench suites are JSON files of {input, expectedOutput, weight} tasks; scoring against a fixed corpus decouples evolution from flaky/slow/undersized `npm test`. Use --op create to scaffold from a repo, --op verify (cheap, ~5s) to gate suite changes in CI. Skipping bench suites is wrong when iterating on the same harness across commits because per-run noise drowns out champion-fitness deltas; bench gives you a stable baseline. ' + MCP_SUCCESS_SEMANTIC, + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + op: { type: 'string', enum: ['create', 'verify'], description: 'create scaffolds suite.json from a repo; verify validates an existing suite' }, + repo: { type: 'string', description: 'Repo path (required for --op create)' }, + suite: { type: 'string', description: 'Suite JSON path (required for --op verify)' }, + out: { type: 'string', description: 'Override default output path for --op create (default: /.metaharness/bench/suite.json)' }, + }, + required: ['op'], + }, + handler: async (input) => { + const args: string[] = ['--op', String(input.op)]; + if (input.repo) args.push('--repo', String(input.repo)); + if (input.suite) args.push('--suite', String(input.suite)); + if (input.out) args.push('--out', String(input.out)); + const r = await runScript('bench.mjs', args); + return { success: r.success, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, ];