From 99cffbd770df4604dbdcd0baf1ad07bfc2e2cea7 Mon Sep 17 00:00:00 2001 From: ruv Date: Tue, 16 Jun 2026 16:49:52 -0400 Subject: [PATCH] =?UTF-8?q?perf(metaharness):=20bench-similarity.mjs=20?= =?UTF-8?q?=E2=80=94=20sub-=CE=BCs=20baseline=20+=20CI=20regression=20gate?= =?UTF-8?q?=20(iter=2041)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Establishes the per-call performance contract for ADR-152 §3.1's similarity() so Phase-3 consumers (Recommender N×M ranking, Drift fleet-wide scan, Capability graph traversal) have a known budget AND future regressions are caught instead of silently accumulating. WHAT SHIPPED plugins/ruflo-metaharness/scripts/bench-similarity.mjs — three-category micro-benchmark over 1M iterations (default), per-category mean / p50 / p99, plus `--max-mean-us N` CI gate. MEASURED BASELINE (200k iters, Apple Silicon / Node 22.22.1) | Category | mean | p50 | p99 | |-------------------|---------:|--------:|--------:| | cheap | 0.34μs | 0.25μs | 0.42μs | | typical | 0.38μs | 0.38μs | 0.50μs | | rich (32 agents) | 1.87μs | 1.83μs | 2.17μs | INTERPRETATION - Sub-microsecond on the iter-38 oia-audit fingerprint shape — proves audit-trend's structural-distance computation adds ~0.4μs to the trend report, indistinguishable from noise vs the JSON.parse cost. - rich-fixture cost scales linearly with agent_topology size (Jaccard is the only variable-cost path). 32 agents → 1.87μs; 1000 harnesses × 1000 candidates × 1.9μs ≈ 1.9s — Phase-3 Recommender fleet-scale ranking budget is safe. WHAT GETS GATED `--max-mean-us N` exits 1 if any of the 3 categories breaches N microseconds. Verified both directions: - `--max-mean-us 100` (loose): exit 0 - `--max-mean-us 0.0001` (impossible): exit 1, alert lists each breaching category with its measured mean The CI workflow can wire this in as node scripts/bench-similarity.mjs --iters 100000 --max-mean-us 10 on every PR touching plugins/ruflo-metaharness/. Threshold 10μs gives ~5× headroom over the Apple Silicon baseline so slower CI runners pass. PHASE-3 BUDGET IMPLICATIONS Recommender §3.2 — ranking N candidates against M templates: N=100 candidates × M=20 templates × 0.4μs ≈ 0.8ms per request Drift Detection §3.3 — fleet-wide pairwise: N=50 harnesses × N=50 prior records × 0.4μs ≈ 1ms Capability Graph §3.4 — pairwise traversal over 1000 capability nodes: 1000 × 1000 × 1.9μs (rich) ≈ 1.9s — safe for batch, marginal for live All three fit within human-interactive budgets without optimization. If a future weight change pushes the typical case above 10μs, CI catches it before the bench artifact is committed. GATES THIS ITER smoke: 44 → 45 (step 17z4 green) fleet: 33/33 runtime: bench produces sub-μs result, gate trips correctly both ways ITERS 35-41 ARC (ADR-152 §3.1 — seventh surface) 35 — spike with 2 invariants 36 — production module + CLI skill + MCP tool + dispatcher 37 — MCP-registry runtime contract (74 assertions) 38 — oia-audit fingerprint + audit-trend structuralDistance 39 — 53 unit-test assertions (8 phases, regression anchor) 40 — dedicated CI job + CLAUDE.md surface docs 41 — performance characterization + regression gate ◀ this iter Co-Authored-By: RuFlo --- .../scripts/bench-similarity.mjs | 171 ++++++++++++++++++ plugins/ruflo-metaharness/scripts/smoke.sh | 21 +++ 2 files changed, 192 insertions(+) create mode 100755 plugins/ruflo-metaharness/scripts/bench-similarity.mjs diff --git a/plugins/ruflo-metaharness/scripts/bench-similarity.mjs b/plugins/ruflo-metaharness/scripts/bench-similarity.mjs new file mode 100755 index 0000000000..d9f1b931a2 --- /dev/null +++ b/plugins/ruflo-metaharness/scripts/bench-similarity.mjs @@ -0,0 +1,171 @@ +#!/usr/bin/env node +// bench-similarity.mjs — micro-benchmark for ADR-152 §3.1's production +// similarity() function. Establishes the per-call cost budget that +// future Phase-3 consumers (§3.2 Recommender N×M ranking, §3.3 Drift +// fleet-wide scan, §3.4 Capability graph traversal) inherit. +// +// THE CLAIM (iter 41 baseline) +// similarity(a, b) is a pure 9-dim cosine + 4-field categorical + +// set-jaccard composite — sub-microsecond per call on Apple Silicon / +// Node 22. Phase-3 consumers can therefore safely call it 10k+ times +// per request without sweating budget. +// +// WHAT IT MEASURES +// - Per-call mean + p50 + p99 over 1M iterations +// - Three input categories (cheap / typical / rich) to surface any +// payload-size sensitivity in projectToVec +// +// USAGE +// node scripts/bench-similarity.mjs # default 1M iters +// node scripts/bench-similarity.mjs --iters 5000000 +// node scripts/bench-similarity.mjs --format json +// node scripts/bench-similarity.mjs --max-mean-us 10 # CI gate (exit 1 if mean > 10μs) +// +// EXIT CODES +// 0 ok (or --max-mean-us not set / threshold satisfied) +// 1 --max-mean-us threshold exceeded (regression) + +import { performance } from 'node:perf_hooks'; +import { similarity } from './_similarity.mjs'; + +const ARGS = (() => { + const a = { + iters: 1_000_000, + format: 'table', + // CI regression gate. When --max-mean-us N is set, exit 1 if any + // measured category's mean per-call cost exceeds N microseconds. + // Default ceiling 10μs chosen as ~5× headroom over Apple-Silicon + // baseline; works on slower CI runners. + maxMeanUs: null, + }; + for (let i = 2; i < process.argv.length; i++) { + const v = process.argv[i]; + if (v === '--iters') a.iters = parseInt(process.argv[++i], 10); + else if (v === '--format') a.format = process.argv[++i]; + else if (v === '--max-mean-us') a.maxMeanUs = parseFloat(process.argv[++i]); + } + return a; +})(); + +// ─────────────────────────────────────────────────────────────────── +// Three fixture categories +// ─────────────────────────────────────────────────────────────────── + +const CHEAP = { + // Bare-bones — everything defaulted from missing fields + score: { harnessFit: 50 }, + genome: { agent_topology: ['a'] }, +}; + +const TYPICAL = { + // The shape the iter-38 oia-audit fingerprint produces + score: { + harnessFit: 78, compileConfidence: 92, taskCoverage: 65, + toolSafety: 88, memoryUsefulness: 70, estCostPerRunUsd: 0.04, + recommendedMode: 'CLI + MCP', archetype: 'compliance-harness', + template: 'vertical:legal', + }, + genome: { + repo_type: 'node_mcp_ci', + agent_topology: ['contract-analyst', 'redline-reviewer', 'risk-rater', 'compliance-officer'], + risk_score: 0.45, test_confidence: 0.7, publish_readiness: 0.6, + }, +}; + +const RICH = { + // Larger agent_topology (Jaccard's variable-cost path) + score: TYPICAL.score, + genome: { + ...TYPICAL.genome, + agent_topology: Array.from({ length: 32 }, (_, i) => `agent-${i}`), + }, +}; + +// ─────────────────────────────────────────────────────────────────── +// Benchmark harness +// ─────────────────────────────────────────────────────────────────── + +function bench(label, a, b, iters) { + // Warm-up + for (let i = 0; i < 10_000; i++) similarity(a, b); + + const samples = new Float64Array(iters); + for (let i = 0; i < iters; i++) { + const t0 = performance.now(); + similarity(a, b); + samples[i] = performance.now() - t0; + } + + // Stats + let sum = 0; + for (let i = 0; i < iters; i++) sum += samples[i]; + const mean = sum / iters; + + const sorted = Array.from(samples).sort((x, y) => x - y); + const p50 = sorted[Math.floor(iters * 0.5)]; + const p99 = sorted[Math.floor(iters * 0.99)]; + + return { + label, + iters, + meanMs: mean, + p50Ms: p50, + p99Ms: p99, + meanUs: mean * 1000, + p99Us: p99 * 1000, + }; +} + +// ─────────────────────────────────────────────────────────────────── +console.log(`# bench-similarity — ADR-152 §3.1 per-call cost\n`); +console.log(`iters: ${ARGS.iters.toLocaleString()}\n`); + +const results = [ + bench('cheap', CHEAP, CHEAP, ARGS.iters), + bench('typical', TYPICAL, TYPICAL, ARGS.iters), + bench('rich (32 agents)', RICH, RICH, ARGS.iters), +]; + +let gate = { triggered: false, reasons: [] }; +if (ARGS.maxMeanUs != null) { + for (const r of results) { + if (r.meanUs > ARGS.maxMeanUs) { + gate.triggered = true; + gate.reasons.push(`${r.label}: mean ${r.meanUs.toFixed(3)}μs > threshold ${ARGS.maxMeanUs}μs`); + } + } +} + +const payload = { + iters: ARGS.iters, + results, + gate: ARGS.maxMeanUs != null ? { + thresholdUs: ARGS.maxMeanUs, + triggered: gate.triggered, + reasons: gate.reasons, + } : null, + generatedAt: new Date().toISOString(), + // The performance contract — captured for /docs/benchmarks consumers + contract: 'similarity() is sub-microsecond on Apple Silicon / Node 22+; Phase-3 consumers may freely call O(N²) on N=1000 harnesses (~1s budget).', +}; + +if (ARGS.format === 'json') { + console.log(JSON.stringify(payload, null, 2)); +} else { + console.log(`| Category | mean | p50 | p99 |`); + console.log(`|------------------|-----------:|-----------:|-----------:|`); + for (const r of results) { + console.log(`| ${r.label.padEnd(16)} | ${r.meanUs.toFixed(3).padStart(7)}μs | ${(r.p50Ms * 1000).toFixed(3).padStart(7)}μs | ${r.p99Us.toFixed(3).padStart(7)}μs |`); + } + console.log(''); + if (payload.gate) { + if (payload.gate.triggered) { + console.log(`⚠ ALERT: mean per-call exceeded ${ARGS.maxMeanUs}μs ceiling:`); + for (const reason of payload.gate.reasons) console.log(` - ${reason}`); + } else { + console.log(`✓ all categories within --max-mean-us ${ARGS.maxMeanUs}μs ceiling`); + } + } +} + +if (gate.triggered) process.exit(1); diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh index 5610659c84..c9f7b7c3a3 100755 --- a/plugins/ruflo-metaharness/scripts/smoke.sh +++ b/plugins/ruflo-metaharness/scripts/smoke.sh @@ -191,6 +191,27 @@ grep -q "execCli(\[\s*'-y'\s*,\s*'metaharness@latest'" "$F" 2>/dev/null || \ grep -q "cwd: opts" "$F" || miss="$miss no-cwd-passthrough" [[ -z "$miss" ]] && ok || bad "$miss" +step "17z4. bench-similarity.mjs — perf characterization + regression gate (iter 41)" +F="$ROOT/scripts/bench-similarity.mjs" +miss="" +[[ -x "$F" ]] || miss="$miss not-executable" +node --check "$F" 2>/dev/null || miss="$miss syntax-error" +# 3 fixture categories declared (anti-shrink guard) +for cat in CHEAP TYPICAL RICH; do + grep -q "const ${cat} = {" "$F" 2>/dev/null || miss="$miss missing-fixture-${cat}" +done +# Imports from the production module, not the spike +grep -q "from './_similarity.mjs'" "$F" 2>/dev/null || miss="$miss not-using-production-module" +# Gate flag exposed +grep -q -- "--max-mean-us" "$F" 2>/dev/null || miss="$miss no-gate-flag" +# Runtime: quick bench succeeds (low iter count so smoke stays fast) +node "$F" --iters 10000 --max-mean-us 50 >/dev/null 2>&1 || miss="$miss runtime-fails-or-overhead-blew-50us" +# Runtime: gate trips on absurd ceiling, exit 1 path exercised +if node "$F" --iters 10000 --max-mean-us 0.0001 >/dev/null 2>&1; then + miss="$miss gate-failed-to-trip" +fi +[[ -z "$miss" ]] && ok || bad "$miss" + step "17z3. metaharness-ci.yml has the similarity-tests job (iter 40 — CI gate enforcement)" F="$ROOT/../../.github/workflows/metaharness-ci.yml" miss=""