From 5be1876a800a624cb4ee67348eb271bf19bea04b Mon Sep 17 00:00:00 2001 From: ruv Date: Tue, 16 Jun 2026 21:12:16 -0400 Subject: [PATCH] refactor(metaharness): SEVERITY_RANK consolidated to _harness.mjs (iter 63) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Iter 62 fixed oia-audit's SEVERITY_RANK gap. Iter 63 finishes the job: the SAME latent bug existed in audit-trend.mjs and mcp-scan.mjs, with slightly DIFFERENT incomplete rank tables in each file. Three sources of truth, three different blind spots, none of them aligned with the iter-50 parser's output. PRE-ITER-63 INCONSISTENCY oia-audit.mjs: { clean, info, low, medium, warn, high, error, critical } (iter 62 fixed) audit-trend.mjs: { clean, low, medium, high } (still buggy) mcp-scan.mjs: { info, low, medium, high, critical } (partial) Effects: - audit-trend deltaRank computed against UNKNOWN-severity baseline → `undefined - 0` = NaN; verdict could be 'unchanged' even on warn→critical drift. - mcp-scan --fail-on high silently ignored 'warn' and 'error' findings (returned undefined from local table, filter excluded them). WHAT SHIPPED 1. plugins/ruflo-metaharness/scripts/_harness.mjs — new exports: 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; } Object.freeze prevents accidental mutation; rankSeverity() is the safe accessor that always returns a number (never undefined → no NaN-compare hazard). 2. plugins/ruflo-metaharness/scripts/oia-audit.mjs — local SEVERITY_RANK literal + rankToSeverity helper removed; imports from _harness.mjs. Two rollup expressions tightened to use rankSeverity() consistently. 3. plugins/ruflo-metaharness/scripts/audit-trend.mjs — local SEVERITY_RANK removed; deltaRank computed via rankSeverity() (was NaN-prone). 4. plugins/ruflo-metaharness/scripts/mcp-scan.mjs — local SEVERITY_RANK removed; offending-findings filter uses rankSeverity() — `warn` and `error` findings now correctly count against `--fail-on high`. SMOKE STEP 17z26 anchors the consolidation: - _harness.mjs exports both SEVERITY_RANK and rankSeverity - Object.freeze present - All 3 consumers import from _harness.mjs (positive grep) - All 3 consumers have NO local literal (negative grep — anti-fork) PLUS iter-62's step 17z25 grep updated for the new location. GATES THIS ITER smoke: 65 → 66 (step 17z26 green; 17z25 grep relocated) live audit: composite=clean (regression-free — INFO maps to 0) roundtrip: 31/31 (no semantic change) tsc: no TS source changes WHY CONSOLIDATION MATTERS 3-file fork was a constant-drift hazard: any iter that adds a new severity string to one file forgets to update the other two. The post-iter-63 single-source-of-truth means a future addition (e.g., upstream introduces [URGENT]) is one edit in one place. The smoke step's anti-fork negative grep enforces that no future iter accidentally re-introduces a local literal. ITER 50-62-63 LATENT-BUG ARC 50 — added structured findings parser (mcp-scan text → JSON) 62 — found oia-audit's SEVERITY_RANK missed warn/critical 63 — found audit-trend + mcp-scan had the same gap; consolidated Pattern: the iter-50 parser's expanded severity vocabulary unlocked two more downstream consumers' latent bugs. Each new data shape that downstream code touches surfaces old assumptions one at a time. Co-Authored-By: RuFlo --- .../ruflo-metaharness/scripts/_harness.mjs | 33 +++++++++++ .../ruflo-metaharness/scripts/audit-trend.mjs | 9 ++- .../ruflo-metaharness/scripts/mcp-scan.mjs | 11 ++-- .../ruflo-metaharness/scripts/oia-audit.mjs | 56 ++++--------------- plugins/ruflo-metaharness/scripts/smoke.sh | 29 ++++++++-- 5 files changed, 82 insertions(+), 56 deletions(-) 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"