Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

test(metaharness): _similarity.mjs unit tests β€” library-grade contract (iter 39)

ruvcddab17a77c8
diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh
index dffb41fbe9..1346c3b9bc 100755
--- a/plugins/ruflo-metaharness/scripts/smoke.sh
+++ b/plugins/ruflo-metaharness/scripts/smoke.sh
@@ -191,6 +191,22 @@ 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 "17z2. _similarity.mjs unit tests (iter 39 β€” library-grade testability)"
+F="$ROOT/scripts/test-similarity.mjs"
+miss=""
+[[ -x "$F" ]] || miss="$miss not-executable"
+node --check "$F" 2>/dev/null || miss="$miss syntax-error"
+# 8 phases enumerated (anti-shrink guard)
+for phase in 'Phase 1' 'Phase 2' 'Phase 3' 'Phase 4' 'Phase 5' 'Phase 6' 'Phase 7' 'Phase 8'; do
+ grep -q "$phase" "$F" || miss="$miss missing-${phase// /-}"
+done
+# Phase 8 regression anchor β€” exact spike numbers must be hard-coded
+grep -q "0.8296" "$F" || miss="$miss no-spike-overall-anchor"
+grep -q "0.9987" "$F" || miss="$miss no-spike-cosine-anchor"
+# Runtime: full unit-test pass (this is the actual gate)
+node "$F" >/dev/null 2>&1 || miss="$miss unit-tests-fail"
+[[ -z "$miss" ]] && ok || bad "$miss"
+
step "17z. ADR-152 Β§3.1 deep integration β€” oia-audit fingerprint + audit-trend structuralDistance (iter 38)"
miss=""
# oia-audit captures score + genome AND surfaces a fingerprint{score,genome}
diff --git a/plugins/ruflo-metaharness/scripts/test-similarity.mjs b/plugins/ruflo-metaharness/scripts/test-similarity.mjs
new file mode 100755
index 0000000000..d3ade40429
--- /dev/null
+++ b/plugins/ruflo-metaharness/scripts/test-similarity.mjs
@@ -0,0 +1,213 @@
+#!/usr/bin/env node
+// test-similarity.mjs β€” unit tests for the iter-36 production module
+// `_similarity.mjs` (ADR-152 Β§3.1).
+//
+// The spike (_spike-similarity.mjs) verifies the two architectural
+// invariants on synthetic fixtures. This test exercises each public
+// function in isolation: projectToVec, cosine, categoricalAgreement,
+// jaccard, similarity, opts.weights override, opts.perDimension shape.
+//
+// CONTRACT EACH FUNCTION MUST SATISFY
+// - input contract documented in _similarity.mjs holds
+// - missing optional fields default safely (graceful, never throws)
+// - numerical results are deterministic (no floats from Math.random)
+// - return shapes match ADR-152 Β§"return shape"
+//
+// USAGE
+// node scripts/test-similarity.mjs
+// node scripts/test-similarity.mjs --format json
+//
+// EXIT
+// 0 all unit tests pass
+// 1 at least one test failed
+
+import {
+ projectToVec, cosine, categoricalAgreement, jaccard, similarity,
+} from './_similarity.mjs';
+
+const ARGS = (() => {
+ const a = { format: 'table' };
+ for (let i = 2; i < process.argv.length; i++) {
+ if (process.argv[i] === '--format') a.format = process.argv[++i];
+ }
+ return a;
+})();
+
+let passed = 0, failed = 0;
+const failures = [];
+
+function assert(cond, label) {
+ if (cond) { console.log(` βœ“ ${label}`); passed++; }
+ else { console.log(` βœ— ${label}`); failures.push(label); failed++; }
+}
+
+function approx(a, b, eps = 0.0001) {
+ return Math.abs(a - b) < eps;
+}
+
+// ──────────────────────────────────────────────────────────────────
+// FIXTURES β€” share two harness shapes across all tests
+// ──────────────────────────────────────────────────────────────────
+
+const A = {
+ score: {
+ harnessFit: 80, compileConfidence: 100, taskCoverage: 70, toolSafety: 90,
+ memoryUsefulness: 50, estCostPerRunUsd: 0.05, recommendedMode: 'CLI + MCP',
+ archetype: 'typescript-sdk-harness', template: 'vertical:coding',
+ },
+ genome: {
+ repo_type: 'node_mcp_ci',
+ agent_topology: ['maintainer', 'tester', 'security'],
+ risk_score: 0.3, test_confidence: 0.85, publish_readiness: 0.9,
+ },
+};
+
+const B = {
+ score: {
+ harnessFit: 60, compileConfidence: 80, taskCoverage: 50, toolSafety: 70,
+ memoryUsefulness: 30, estCostPerRunUsd: 0.02, recommendedMode: 'CLI',
+ archetype: 'python-agent-harness', template: 'vertical:devops',
+ },
+ genome: {
+ repo_type: 'python_ops',
+ agent_topology: ['deployer', 'rollback'],
+ risk_score: 0.1, test_confidence: 0.6, publish_readiness: 0.5,
+ },
+};
+
+// ──────────────────────────────────────────────────────────────────
+console.log(`# test-similarity β€” _similarity.mjs unit tests (iter 39)\n`);
+
+console.log('Phase 1 β€” projectToVec');
+const vA = projectToVec(A);
+assert(Array.isArray(vA), 'returns an array');
+assert(vA.length === 9, 'returns 9-dim vector (ADR-152 Β§Decision Table 1)');
+assert(approx(vA[0], 0.80), 'index 0 = harnessFit/100 (80 β†’ 0.80)');
+assert(approx(vA[1], 1.00), 'index 1 = compileConfidence/100 (100 β†’ 1.00)');
+assert(approx(vA[2], 0.70), 'index 2 = taskCoverage/100');
+assert(approx(vA[3], 0.90), 'index 3 = toolSafety/100');
+assert(approx(vA[4], 0.50), 'index 4 = memoryUsefulness/100');
+assert(approx(vA[5], 0.30), 'index 5 = risk_score (already 0..1)');
+assert(approx(vA[6], 0.85), 'index 6 = test_confidence');
+assert(approx(vA[7], 0.90), 'index 7 = publish_readiness');
+// Missing-field defaults safely (graceful degradation)
+const vEmpty = projectToVec({});
+assert(vEmpty.length === 9, 'empty input still returns 9-dim');
+assert(vEmpty.every((x) => typeof x === 'number'), 'all elements numeric (no NaN propagation)');
+const vNoGenome = projectToVec({ score: A.score });
+assert(approx(vNoGenome[5], 0), 'missing genome β†’ risk_score=0 default');
+assert(approx(vNoGenome[0], 0.80), 'missing genome does not corrupt score-derived indices');
+
+console.log('\nPhase 2 β€” cosine');
+assert(approx(cosine(vA, vA), 1), 'cosine(x, x) === 1');
+assert(approx(cosine([1, 0], [0, 1]), 0), 'orthogonal vectors β†’ 0');
+assert(approx(cosine([0, 0, 0], [1, 1, 1]), 0), 'zero vector β†’ 0 (graceful divide-by-zero)');
+assert(cosine([1, 2], [1, 2, 3]) === 0, 'length mismatch β†’ 0 (graceful)');
+assert(cosine(null, vA) === 0, 'null input β†’ 0 (graceful)');
+const cosAB = cosine(vA, projectToVec(B));
+assert(cosAB >= 0 && cosAB <= 1, 'output bounded to [0, 1] for nonneg vectors');
+
+console.log('\nPhase 3 β€” categoricalAgreement');
+assert(approx(categoricalAgreement(A, A), 1), 'identical input β†’ 1 (all 4 enums match)');
+assert(approx(categoricalAgreement(A, B), 0), 'A vs B β†’ 0 (all 4 enums differ)');
+assert(approx(categoricalAgreement({}, {}), 0), 'both empty β†’ 0 (no matches counted)');
+// Partial match β€” 2 of 4 fields agree
+const partial = {
+ score: { archetype: A.score.archetype, template: 'different', recommendedMode: A.score.recommendedMode },
+ genome: { repo_type: 'different' },
+};
+assert(approx(categoricalAgreement(A, partial), 0.5),
+ '2-of-4 partial match β†’ 0.5 (archetype + recommendedMode)');
+
+console.log('\nPhase 4 β€” jaccard');
+assert(approx(jaccard(A, A), 1), 'identical topology β†’ 1');
+assert(approx(jaccard(A, B), 0), 'disjoint topology β†’ 0');
+assert(approx(jaccard({ genome: {} }, { genome: {} }), 1),
+ 'both empty topology β†’ 1 (set-equality convention)');
+const overlap = {
+ genome: { agent_topology: ['maintainer', 'tester', 'extra'] },
+};
+// A has {maintainer, tester, security}; overlap has {maintainer, tester, extra}
+// |A ∩ B|=2, |A βˆͺ B|=4 β†’ 2/4 = 0.5
+assert(approx(jaccard(A, overlap), 0.5), 'partial topology overlap β†’ |∩|/|βˆͺ|');
+
+console.log('\nPhase 5 β€” similarity composite + return shape');
+const sAA = similarity(A, A);
+assert(typeof sAA === 'object' && sAA !== null, 'returns object');
+assert(typeof sAA.overall === 'number', 'overall is numeric');
+assert(approx(sAA.overall, 1), 'similarity(X, X).overall === 1');
+assert(typeof sAA.components === 'object', 'components present');
+assert(approx(sAA.components.cosine, 1), 'components.cosine === 1 on self-match');
+assert(approx(sAA.components.categorical, 1), 'components.categorical === 1');
+assert(approx(sAA.components.jaccard, 1), 'components.jaccard === 1');
+assert(typeof sAA.weights === 'object', 'weights echoed in return');
+assert(approx(sAA.weights.cosine, 0.6), 'default cosine weight = 0.6 (ADR-152)');
+assert(approx(sAA.weights.categorical, 0.25), 'default categorical weight = 0.25');
+assert(approx(sAA.weights.jaccard, 0.15), 'default jaccard weight = 0.15');
+assert(approx(sAA.weights.cosine + sAA.weights.categorical + sAA.weights.jaccard, 1),
+ 'default weights sum to 1');
+
+console.log('\nPhase 6 β€” similarity opts.weights override');
+const reweighted = similarity(A, B, { weights: { cosine: 0, categorical: 0, jaccard: 1 } });
+const jacOnly = jaccard(A, B);
+assert(approx(reweighted.overall, jacOnly),
+ 'opts.weights cosine=0/cat=0/jac=1 β†’ overall === jaccard');
+const reweighted2 = similarity(A, A, { weights: { cosine: 0.5, categorical: 0.3, jaccard: 0.2 } });
+assert(approx(reweighted2.overall, 1),
+ 'self-match holds under any weight scheme');
+
+console.log('\nPhase 7 β€” similarity opts.perDimension shape');
+const sPD = similarity(A, B, { perDimension: true });
+assert(typeof sPD.perDimension === 'object', 'perDimension included when requested');
+assert('numeric.harnessFit' in sPD.perDimension,
+ 'perDimension exposes each numeric feature by name');
+assert('categorical.archetype' in sPD.perDimension,
+ 'perDimension exposes each categorical field by name');
+assert('set.agent_topology' in sPD.perDimension,
+ 'perDimension exposes the set-typed feature');
+const hf = sPD.perDimension['numeric.harnessFit'];
+assert(hf.a === 80 && hf.b === 60, 'perDimension preserves raw a/b values');
+assert(typeof hf.contribution === 'number', 'perDimension has numeric contribution');
+const sNoPD = similarity(A, B);
+assert(!('perDimension' in sNoPD), 'perDimension omitted by default');
+
+console.log('\nPhase 8 β€” round-trip with the iter-35 spike fixtures (regression anchor)');
+// Hard-coded spike numbers from `_spike-similarity.mjs` β€” ANY change
+// here means the production module drifted from the spike's frozen
+// invariants (catches future weight or normalization changes).
+const LEGAL = {
+ 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 SUPPORT = {
+ score: { harnessFit: 75, compileConfidence: 90, taskCoverage: 70, toolSafety: 90, memoryUsefulness: 72, estCostPerRunUsd: 0.05, recommendedMode: 'CLI + MCP', archetype: 'compliance-harness', template: 'vertical:support' },
+ genome: { repo_type: 'node_mcp_ci', agent_topology: ['triager', 'kb-searcher', 'responder', 'risk-rater', 'compliance-officer'], risk_score: 0.40, test_confidence: 0.75, publish_readiness: 0.65 },
+};
+const sLS = similarity(LEGAL, SUPPORT);
+assert(sLS.overall === 0.8296,
+ `LEGAL Γ— SUPPORT overall must be exactly 0.8296 (got ${sLS.overall})`);
+assert(sLS.components.cosine === 0.9987,
+ `LEGAL Γ— SUPPORT cosine must be 0.9987 (got ${sLS.components.cosine})`);
+assert(sLS.components.categorical === 0.75,
+ `LEGAL Γ— SUPPORT categorical must be 0.75 (got ${sLS.components.categorical})`);
+assert(sLS.components.jaccard === 0.2857,
+ `LEGAL Γ— SUPPORT jaccard must be 0.2857 (got ${sLS.components.jaccard})`);
+
+// ──────────────────────────────────────────────────────────────────
+const summary = {
+ passed, failed,
+ failures,
+ total: passed + failed,
+ graduates: failed === 0,
+};
+
+console.log(`\n${passed} passed, ${failed} failed`);
+if (ARGS.format === 'json') {
+ console.log(JSON.stringify(summary, null, 2));
+}
+if (failed > 0) {
+ console.log('\nFailures:');
+ for (const f of failures) console.log(` - ${f}`);
+ process.exit(1);
+}
+console.log('\nβœ“ All _similarity.mjs unit tests pass (ADR-152 Β§3.1 production contract).');