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

refactor(metaharness): SEVERITY_RANK consolidated to _harness.mjs (iter 63)

ruv5be1876a800a
diff --git a/plugins/ruflo-metaharness/scripts/_harness.mjs b/plugins/ruflo-metaharness/scripts/_harness.mjs
index 7a9eec1a5b..27105db1c6 100755
--- a/plugins/ruflo-metaharness/scripts/_harness.mjs
+++ b/plugins/ruflo-metaharness/scripts/_harness.mjs
@@ -168,6 +168,39 @@ export function runHarness(args, opts) {
return { stdout, stderr, exitCode: r.status ?? 0, json, durationMs, degraded: false };
}
+/**
+ * iter 63 β€” single source of truth for severity ranks across the
+ * metaharness plugin family. Pre-iter-63, three scripts (oia-audit,
+ * audit-trend, mcp-scan) maintained their own SEVERITY_RANK literal,
+ * each missing different keys, each producing different NaN-compare
+ * behavior on unknown severities. Iter 62 fixed oia-audit; iter 63
+ * propagates the fix and consolidates.
+ *
+ * Mapping rationale:
+ * clean / info β†’ 0 (no harm)
+ * low β†’ 1
+ * medium / warn β†’ 2
+ * high / error β†’ 3
+ * critical β†’ 4 (explicit elevation above high)
+ *
+ * `rankSeverity(s)` is the safe accessor β€” returns 0 for any unknown
+ * string instead of `undefined`, eliminating the NaN-compare hazard
+ * (`undefined > 3` evaluates to false β†’ unknown severities silently
+ * ignored in reduce expressions).
+ */
+export const SEVERITY_RANK = Object.freeze({
+ clean: 0, info: 0,
+ low: 1,
+ medium: 2, warn: 2,
+ high: 3, error: 3,
+ critical: 4,
+});
+
+export function rankSeverity(s) {
+ if (s == null) return 0;
+ return SEVERITY_RANK[String(s).toLowerCase()] ?? 0;
+}
+
/**
* iter 50 β€” parse `harness mcp-scan` text output into structured findings.
*
diff --git a/plugins/ruflo-metaharness/scripts/audit-trend.mjs b/plugins/ruflo-metaharness/scripts/audit-trend.mjs
index b99173f813..e635167651 100755
--- a/plugins/ruflo-metaharness/scripts/audit-trend.mjs
+++ b/plugins/ruflo-metaharness/scripts/audit-trend.mjs
@@ -30,8 +30,11 @@ import { spawnSync } from 'node:child_process';
// Falls back to null if either record predates iter-38 oia-audit (no
// fingerprint field) β€” graceful degradation, never throws.
import { similarity } from './_similarity.mjs';
+// iter 63 β€” shared SEVERITY_RANK from _harness.mjs (was a local literal
+// missing info/warn/error/critical, which caused NaN-compare hazards).
+import { SEVERITY_RANK, rankSeverity } from './_harness.mjs';
-const SEVERITY_RANK = { clean: 0, low: 1, medium: 2, high: 3 };
+// iter 63 β€” SEVERITY_RANK moved to _harness.mjs (imported above)
const NS = process.env.AUDIT_TREND_NAMESPACE || 'metaharness-audit';
const CLI_PKG = process.env.CLI_CORE === '1'
? '@claude-flow/cli-core@alpha'
@@ -105,7 +108,9 @@ function main() {
const baseWorst = String(baseline.composite.worst || 'clean').toLowerCase();
const currWorst = String(current.composite.worst || 'clean').toLowerCase();
- const deltaRank = SEVERITY_RANK[currWorst] - SEVERITY_RANK[baseWorst];
+ // iter 63 β€” safe rankSeverity() eliminates NaN-compare when either
+ // severity is unknown (was a latent bug in audit-trend's drift verdict).
+ const deltaRank = rankSeverity(currWorst) - rankSeverity(baseWorst);
const worsened = deltaRank > 0;
const improved = deltaRank < 0;
diff --git a/plugins/ruflo-metaharness/scripts/mcp-scan.mjs b/plugins/ruflo-metaharness/scripts/mcp-scan.mjs
index 9e2b3619c6..ef579cdef8 100755
--- a/plugins/ruflo-metaharness/scripts/mcp-scan.mjs
+++ b/plugins/ruflo-metaharness/scripts/mcp-scan.mjs
@@ -18,9 +18,9 @@
// iter 50 β€” parseMcpScanText extracted to _harness.mjs so oia-audit
// can use the same parser without duplicating logic.
-import { runHarness, emitDegradedJsonAndExit, parseMcpScanText } from './_harness.mjs';
-
-const SEVERITY_RANK = { info: 0, low: 1, medium: 2, high: 3, critical: 3 };
+// iter 63 β€” SEVERITY_RANK also moves to _harness.mjs (single source of
+// truth; previously this file had a literal that diverged from oia-audit's).
+import { runHarness, emitDegradedJsonAndExit, parseMcpScanText, SEVERITY_RANK, rankSeverity } from './_harness.mjs';
const ARGS = (() => {
const a = { path: '.', format: 'json', failOn: 'high' };
@@ -61,7 +61,10 @@ function main() {
};
const findings = payload.findings;
const threshold = SEVERITY_RANK[ARGS.failOn];
- const offending = findings.filter((f) => SEVERITY_RANK[String(f.severity || 'low').toLowerCase()] >= threshold);
+ // iter 63 β€” rankSeverity() safe lookup. Pre-iter-63 `undefined >= 3`
+ // was false β†’ unknown-severity findings (e.g., warn / error) were
+ // silently excluded from the offending set, weakening --fail-on alerts.
+ const offending = findings.filter((f) => rankSeverity(f.severity) >= threshold);
const alert = {
threshold: ARGS.failOn,
diff --git a/plugins/ruflo-metaharness/scripts/oia-audit.mjs b/plugins/ruflo-metaharness/scripts/oia-audit.mjs
index a77927609d..c0ee474a9d 100755
--- a/plugins/ruflo-metaharness/scripts/oia-audit.mjs
+++ b/plugins/ruflo-metaharness/scripts/oia-audit.mjs
@@ -25,43 +25,13 @@
// 2 config error or audit failure
import { spawnSync } from 'node:child_process';
-import { runHarness, runMetaharness, runHarnessAsync, runMetaharnessAsync, emitDegradedJsonAndExit, parseMcpScanText } from './_harness.mjs';
+// iter 63 β€” SEVERITY_RANK + rankSeverity consolidated to _harness.mjs
+// (was local in iter 62; now shared with audit-trend + mcp-scan).
+import { runHarness, runMetaharness, runHarnessAsync, runMetaharnessAsync, emitDegradedJsonAndExit, parseMcpScanText, SEVERITY_RANK, rankSeverity } from './_harness.mjs';
-// iter 62 β€” extended SEVERITY_RANK to include severity values the
-// iter-50 text parser actually emits. Upstream `harness mcp-scan`
-// outputs [INFO], [WARN], [HIGH], [CRITICAL] (and possibly [ERROR]).
-// Pre-iter-62, only `clean/low/medium/high` were ranked; any other
-// severity returned `undefined` from the table, and the reduce
-// expression `SEVERITY_RANK[s] > SEVERITY_RANK[acc]` evaluated to
-// false (NaN > 0 === false), silently ignoring them in the composite
-// severity rollup. A [CRITICAL] finding would NOT bump composite worst.
-//
-// Mapping rationale:
-// info β†’ 0 (informational, no harm)
-// low β†’ 1
-// medium β†’ 2
-// warn β†’ 2 (warn β‰ˆ medium)
-// high β†’ 3
-// critical β†’ 4 (critical > high β€” explicit elevation)
-// error β†’ 3 (error β‰ˆ high; some tools use ERROR for HIGH-equivalent)
-const SEVERITY_RANK = {
- clean: 0, info: 0,
- low: 1,
- medium: 2, warn: 2,
- high: 3, error: 3,
- critical: 4,
-};
-
-// Composite severity-to-string mapping respects the new rank space:
-// anything β‰₯ 3 reports as 'high' so existing alert thresholds stay
-// honest (a CRITICAL finding triggers --alert-on-worst high).
-function rankToSeverity(rank) {
- if (rank >= 4) return 'critical';
- if (rank >= 3) return 'high';
- if (rank >= 2) return 'medium';
- if (rank >= 1) return 'low';
- return 'clean';
-}
+// iter 63 β€” SEVERITY_RANK + rankSeverity moved to _harness.mjs (single
+// source of truth). The local copy from iter 62 is gone; the imports
+// at the top of this file provide the same names.
const NS = process.env.OIA_AUDIT_NAMESPACE || 'metaharness-audit';
const CLI_PKG = process.env.CLI_CORE === '1'
? '@claude-flow/cli-core@alpha'
@@ -171,21 +141,19 @@ async function main() {
// correctly elevates composite worst.
const tmWorst = String(tm.json?.worst || 'clean').toLowerCase();
const mcpFindings = Array.isArray(mcp.json?.findings) ? mcp.json.findings : [];
+ // iter 63 β€” rankSeverity() from _harness.mjs handles unknown-severity
+ // safely (returns 0 instead of undefined) so the reduce never sees NaN.
const mcpWorst = mcpFindings.reduce((acc, f) => {
const s = String(f.severity || 'low').toLowerCase();
- const sRank = SEVERITY_RANK[s] ?? 0;
- const accRank = SEVERITY_RANK[acc] ?? 0;
- return sRank > accRank ? s : acc;
+ return rankSeverity(s) > rankSeverity(acc) ? s : acc;
}, 'clean');
- const tmRank = SEVERITY_RANK[tmWorst] ?? 0;
- const mcpRank = SEVERITY_RANK[mcpWorst] ?? 0;
- const compositeWorst = tmRank > mcpRank ? tmWorst : mcpWorst;
+ const compositeWorst = rankSeverity(tmWorst) > rankSeverity(mcpWorst) ? tmWorst : mcpWorst;
let alertTriggered = false;
let alertReason = null;
if (ARGS.alertWorst !== null) {
- const threshold = SEVERITY_RANK[ARGS.alertWorst] ?? 0;
- const compositeRank = SEVERITY_RANK[compositeWorst] ?? 0;
+ const threshold = rankSeverity(ARGS.alertWorst);
+ const compositeRank = rankSeverity(compositeWorst);
if (compositeRank >= threshold && threshold > 0) {
alertTriggered = true;
alertReason = `composite worst=${compositeWorst} β‰₯ ${ARGS.alertWorst}`;
diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh
index 76598cfadf..b4f4032846 100755
--- a/plugins/ruflo-metaharness/scripts/smoke.sh
+++ b/plugins/ruflo-metaharness/scripts/smoke.sh
@@ -191,17 +191,34 @@ 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 "17z26. SEVERITY_RANK consolidated to _harness.mjs (iter 63)"
+miss=""
+HARNESS="$ROOT/scripts/_harness.mjs"
+grep -q "export const SEVERITY_RANK" "$HARNESS" 2>/dev/null || miss="$miss no-shared-rank"
+grep -q "export function rankSeverity" "$HARNESS" 2>/dev/null || miss="$miss no-shared-rank-fn"
+grep -q "Object.freeze" "$HARNESS" 2>/dev/null || miss="$miss no-freeze"
+# 3 consumers now import (not define local)
+for f in oia-audit audit-trend mcp-scan; do
+ S="$ROOT/scripts/$f.mjs"
+ grep -q "SEVERITY_RANK.*from './_harness.mjs'\|SEVERITY_RANK, rankSeverity.*from './_harness.mjs'\|rankSeverity.*from './_harness.mjs'" "$S" 2>/dev/null || miss="$miss ${f}-not-importing"
+ # Local SEVERITY_RANK literal must NOT be present (it was the bug source)
+ ! grep -qE "^const SEVERITY_RANK = \{" "$S" 2>/dev/null || miss="$miss ${f}-local-literal-remains"
+done
+[[ -z "$miss" ]] && ok || bad "$miss"
+
step "17z25. SEVERITY_RANK covers iter-50 parser output + safe ?? 0 lookup (iter 62)"
miss=""
+# iter 63 β€” SEVERITY_RANK moved to _harness.mjs (consolidated)
+HARNESS="$ROOT/scripts/_harness.mjs"
OIA="$ROOT/scripts/oia-audit.mjs"
-# Extended SEVERITY_RANK has all 7 keys
+# Extended SEVERITY_RANK has all 8 keys in _harness.mjs
for sev in clean info low medium warn high error critical; do
- grep -qE "\\b${sev}: [0-9]" "$OIA" 2>/dev/null || miss="$miss missing-rank-${sev}"
+ grep -qE "\\b${sev}: [0-9]" "$HARNESS" 2>/dev/null || miss="$miss missing-rank-${sev}"
done
-# Safe ?? 0 lookup eliminates NaN-compare hazard
-grep -q "SEVERITY_RANK\[s\] ?? 0\|sRank = SEVERITY_RANK" "$OIA" 2>/dev/null || miss="$miss no-safe-rank-lookup"
-# Comment block documents the rationale
-grep -q "would NOT bump composite worst" "$OIA" 2>/dev/null || miss="$miss no-rationale-comment"
+# Safe rankSeverity() accessor exported
+grep -q "export function rankSeverity\|return SEVERITY_RANK\[.*\] ?? 0" "$HARNESS" 2>/dev/null || miss="$miss no-safe-rank-lookup"
+# Rationale documented at the new location
+grep -q "NaN-compare hazard" "$HARNESS" 2>/dev/null || miss="$miss no-rationale-comment"
# Runtime: live oia-audit still produces clean (only INFO finding)
OUT=$(node "$OIA" --dry-run --format json 2>/dev/null)
echo "$OUT" | grep -q '"worst": "clean"' || miss="$miss live-not-clean"