From ca56902b27ee226973b33cac24ec2b97bbb27f2d Mon Sep 17 00:00:00 2001 From: ruv Date: Tue, 16 Jun 2026 22:37:57 -0400 Subject: [PATCH] test(metaharness): upstream mcp-scan text-format compat tripwire (iter 80) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Iter 50's parseMcpScanText reverse-engineered `harness mcp-scan` text output to extract structured findings. If upstream changes the format (drops `[]` markers, switches to JSON, changes the Result: regex), the parser silently returns empty findings and iter-49's introduced/cleared diff regresses to the original dead-code state. This iter adds a compat tripwire mirroring scripts/check-metaharness- compat.mjs (which protects the @metaharness/router API surface). WHAT SHIPPED 1. scripts/check-mcp-scan-format.mjs — runs `harness mcp-scan` against ruflo and asserts 4 invariants: ✓ At least one ` [SEV] message` line in expected regex shape ✓ Result line matches `Result: SEV (N finding, M high)` ✓ parseMcpScanText loadable (sanity) ✓ parseMcpScanText extracts ≥ 1 finding from real output Exit 0 — invariants intact OR metaharness not installed (graceful skip) Exit 1 — invariant violated, parser will silently break Exit 2 — unexpected error The "extract ≥1 finding from real output" assertion is the load-bearing check. It catches drift the regex above might miss (e.g., if upstream switched to identical-looking but slightly different whitespace). 2. .github/workflows/metaharness-ci.yml — new step in the `metaharness-real-data` job runs the tripwire BEFORE the roundtrip. If upstream format drifted, CI fails with a clear "format invariant violated" message before chasing downstream symptoms. 3. plugins/ruflo-metaharness/scripts/smoke.sh — step 17z43 anchors: - tripwire script exists + parses - All 3 invariant text markers (finding/result/parser-roundtrip) - CI workflow references the script - Runtime: tripwire passes against the locally-installed metaharness WHY TWO TRIPWIRES NOW EXIST scripts/check-metaharness-compat.mjs (iter 12) → Protects @metaharness/router public API (constructor, route(), fromExamples(), TrainedRouter.fromJSON()) scripts/check-mcp-scan-format.mjs (THIS ITER) → Protects `harness mcp-scan` text format (regex + parser-roundtrip) These cover different upstream surfaces. The router compat-check ran at the API level; the new check is at the CLI-output level. Both are necessary because ruflo depends on both surfaces in different ways. VERIFIED LOCALLY $ node scripts/check-mcp-scan-format.mjs ✓ All 4 invariants intact. GATES THIS ITER smoke: 82 → 83 (step 17z43 green) ci: new pre-roundtrip step in metaharness-real-data job runtime: 4 format invariants pass against metaharness@0.1.14 PROTECTION RATIONALE Iter 50 was a downstream workaround for issue ruvnet/agent-harness- generator#16 (`harness mcp-scan` emits text only with --json). When upstream eventually fixes that issue and emits structured JSON, the parser's regex-based extraction will STILL work (the parser falls back gracefully). But the tripwire will fail on the FIRST run after upstream adds the JSON output AND drops the text format — which is a real risk because upstream is in v0.1.x. When the tripwire eventually fires, the fix is upstream-aware: - If text format dropped → switch parseMcpScanText to read r.json directly - If JSON added alongside text → prefer r.json, keep parser as fallback The tripwire makes that decision visible at the PR that introduces it, not 4 weeks later when someone notices drift detection is silent. Co-Authored-By: RuFlo --- .github/workflows/metaharness-ci.yml | 9 ++ plugins/ruflo-metaharness/scripts/smoke.sh | 16 +++ scripts/check-mcp-scan-format.mjs | 147 +++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100755 scripts/check-mcp-scan-format.mjs diff --git a/.github/workflows/metaharness-ci.yml b/.github/workflows/metaharness-ci.yml index a853924d91..fc73cb9b06 100644 --- a/.github/workflows/metaharness-ci.yml +++ b/.github/workflows/metaharness-ci.yml @@ -345,6 +345,15 @@ jobs: npx -y metaharness@latest --version echo "✓ metaharness CLI warm" + - name: Upstream mcp-scan format invariants (iter 80 — protects iter-50 parser) + # If upstream `harness mcp-scan` ever changes its text format, + # iter-50's parseMcpScanText silently returns empty findings and + # iter-49's introduced/cleared diff regresses to dead code. This + # tripwire fails the workflow if the format invariant drifts. + run: | + node scripts/check-mcp-scan-format.mjs --format json + echo "✓ upstream text format compatible with parseMcpScanText" + - name: End-to-end pipeline roundtrip (iter 47) # Must exit 0. Exit 2 means metaharness wasn't reachable (we just # warmed it, so that would be infra failure). Exit 1 means the diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh index 7ae5588bfc..e57ac1affb 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 "17z43. upstream mcp-scan format compat tripwire (iter 80)" +miss="" +F="$ROOT/../../scripts/check-mcp-scan-format.mjs" +[[ -x "$F" ]] || miss="$miss not-executable" +node --check "$F" 2>/dev/null || miss="$miss syntax-error" +# Tripwire asserts both upstream format invariants +grep -q "finding line matches" "$F" 2>/dev/null || miss="$miss no-finding-line-check" +grep -q "Result line matches" "$F" 2>/dev/null || miss="$miss no-result-line-check" +grep -q "parseMcpScanText extracts at least 1 finding" "$F" 2>/dev/null || miss="$miss no-parser-roundtrip" +# CI workflow runs it +W="$ROOT/../../.github/workflows/metaharness-ci.yml" +grep -q "check-mcp-scan-format.mjs" "$W" 2>/dev/null || miss="$miss not-in-ci" +# Runtime: tripwire passes on ruflo's own audit +node "$F" >/dev/null 2>&1 || miss="$miss tripwire-fails-locally" +[[ -z "$miss" ]] && ok || bad "$miss" + step "17z42. weekly cron uses --alert-on-new-severity + Stage 12 verifies (iter 79)" miss="" # Weekly cron wires the gate diff --git a/scripts/check-mcp-scan-format.mjs b/scripts/check-mcp-scan-format.mjs new file mode 100755 index 0000000000..94bf5d9d75 --- /dev/null +++ b/scripts/check-mcp-scan-format.mjs @@ -0,0 +1,147 @@ +#!/usr/bin/env node +// check-mcp-scan-format — protect iter-50's parseMcpScanText from silent +// upstream changes. +// +// iter 50 reverse-engineered upstream `harness mcp-scan` to extract +// structured findings from its plain-text output. That parser depends +// on TWO format invariants: +// +// 1. Finding lines: ` [SEV] ` (2-space indent, uppercase +// severity inside brackets, message on same line) +// +// 2. Summary line: `Result: (N finding, M high)` +// +// If upstream changes either format (e.g., drops `[]` markers, switches +// to JSON, changes the Result regex), parseMcpScanText silently returns +// `[]` findings and audit-trend's introduced/cleared diff returns to +// dead code (the iter-49 latent bug). +// +// This script runs `harness mcp-scan` against ruflo itself and asserts +// BOTH format invariants hold. CI fails the workflow if either drifts. +// +// USAGE +// node scripts/check-mcp-scan-format.mjs # exits 0 if compatible +// node scripts/check-mcp-scan-format.mjs --format json # CI-consumable +// +// EXIT CODES +// 0 format invariants intact (or metaharness not installed — +// ADR-150 graceful degradation: skip with reason) +// 1 format invariant violated — parser will silently break +// 2 unexpected error + +import { spawnSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { dirname, join } from 'node:path'; + +const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)); +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; +})(); + +function emit(payload, code) { + if (ARGS.format === 'json') { + console.log(JSON.stringify(payload, null, 2)); + } else { + console.log(`# check-mcp-scan-format\n`); + if (payload.skipped) console.log(`⊘ ${payload.reason}`); + else if (payload.ok) console.log(`✓ All ${payload.results?.length ?? 0} invariants intact.`); + else { + console.log(`✗ ${payload.failedCount}/${payload.results?.length ?? 0} invariants violated:`); + for (const r of payload.results || []) { + if (!r.ok) console.log(` - ${r.check}`); + } + } + } + process.exit(code); +} + +async function main() { + // Resolve repo root (one level up from scripts/) + const repoRoot = join(SCRIPT_DIR, '..'); + + // 1. Run `harness mcp-scan` against ruflo. ~5s on warm cache. + const r = spawnSync('npx', [ + '-y', '-p', 'metaharness@latest', 'harness', 'mcp-scan', repoRoot, + ], { encoding: 'utf-8', timeout: 90_000 }); + + // 2. Graceful path: dep absent + if (r.status === null + || /could not determine executable|404|not installed|MODULE_NOT_FOUND|ENOTFOUND/i.test(r.stderr || '')) { + emit({ + skipped: true, + reason: 'metaharness-not-available — install with `npm i --include=optional` to verify upstream format', + generatedAt: new Date().toISOString(), + }, 0); + } + + const stdout = r.stdout || ''; + const results = []; + + // 3. INVARIANT 1: at least one finding line in the expected shape + // ` [SEV] ` where SEV is uppercase letters in brackets. + // Pre-iter-50 we never asserted this — upstream changing to + // `* SEV: message` would have been undetectable. + const findingLineRegex = /^\s*\[([A-Z]+)\]\s+\S/m; + const findingMatch = findingLineRegex.exec(stdout); + results.push({ + check: 'finding line matches `[SEV] message` regex', + ok: findingMatch !== null, + detail: findingMatch ? `first match: [${findingMatch[1]}]` : 'no match — parser will return empty findings', + }); + + // 4. INVARIANT 2: Result summary line matches the expected shape. + // `Result: SEV (N finding, M high)` — used to populate summary. + const resultRegex = /Result:\s+([A-Z]+)\s+\((\d+)\s+finding/; + const resultMatch = resultRegex.exec(stdout); + results.push({ + check: 'Result line matches `Result: SEV (N finding, M high)` regex', + ok: resultMatch !== null, + detail: resultMatch + ? `severity=${resultMatch[1]}, count=${resultMatch[2]}` + : 'no match — summary parsing will return null', + }); + + // 5. INVARIANT 3: run the actual parser against the output. Catches + // any drift the regex above might miss but the parser depends on. + let parser; + try { + parser = await import(join(repoRoot, 'plugins', 'ruflo-metaharness', 'scripts', '_harness.mjs')); + } catch (e) { + results.push({ + check: 'parseMcpScanText module loadable', + ok: false, + detail: e?.message?.slice(0, 100), + }); + emit({ ok: false, results, failedCount: 1, generatedAt: new Date().toISOString() }, 1); + } + const parsed = parser.parseMcpScanText(stdout); + results.push({ + check: 'parseMcpScanText returns findings array', + ok: Array.isArray(parsed.findings), + detail: `findings.length=${parsed.findings?.length}`, + }); + results.push({ + check: 'parseMcpScanText extracts at least 1 finding from real output', + ok: parsed.findings.length >= 1, + detail: parsed.findings.length === 0 + ? 'parser produced empty findings — format may have drifted' + : `first: severity=${parsed.findings[0].severity}, msg-prefix="${parsed.findings[0].message?.slice(0, 40)}..."`, + }); + + const failed = results.filter((x) => !x.ok); + emit({ + ok: failed.length === 0, + results, + failedCount: failed.length, + generatedAt: new Date().toISOString(), + }, failed.length === 0 ? 0 : 1); +} + +main().catch((e) => { + console.error('check-mcp-scan-format crashed:', e?.message ?? e); + process.exit(2); +});