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

feat(metaharness): audit-trend β€” diff two oia-audit snapshots (iter 15)

ruv70b859dae17b
diff --git a/plugins/ruflo-metaharness/scripts/audit-trend.mjs b/plugins/ruflo-metaharness/scripts/audit-trend.mjs
new file mode 100755
index 0000000000..a5ac22a780
--- /dev/null
+++ b/plugins/ruflo-metaharness/scripts/audit-trend.mjs
@@ -0,0 +1,207 @@
+#!/usr/bin/env node
+// audit-trend.mjs β€” diff two oia-audit records to detect drift over time.
+//
+// The iter-7 oia-audit composite worker stores timestamped records under
+// the `metaharness-audit` memory namespace. This script reads two such
+// records and surfaces the delta:
+// - composite worst severity change (clean β†’ low, low β†’ medium, etc.)
+// - per-component (oia / threat-model / mcp-scan) status change
+// - new HIGH-severity findings introduced
+// - findings cleared
+//
+// Pairs with iter-7's oia-audit + iter-8's weekly cron β€” accumulated
+// records enable drift detection without ad-hoc tooling.
+//
+// USAGE
+// node scripts/audit-trend.mjs --baseline audit-<ts1>.json --current audit-<ts2>.json
+// node scripts/audit-trend.mjs --baseline-key audit-2026-06-01... --current-key audit-2026-06-15...
+// # pulls both from memory namespace `metaharness-audit`
+// node scripts/audit-trend.mjs ... --alert-on-worsening
+// node scripts/audit-trend.mjs ... --format json
+//
+// EXIT CODES
+// 0 ok (no worsening, or --alert-on-worsening not set)
+// 1 --alert-on-worsening AND composite severity worsened
+// 2 config error or input not found
+
+import { readFileSync, existsSync } from 'node:fs';
+import { spawnSync } from 'node:child_process';
+
+const SEVERITY_RANK = { clean: 0, low: 1, medium: 2, high: 3 };
+const NS = process.env.AUDIT_TREND_NAMESPACE || 'metaharness-audit';
+const CLI_PKG = process.env.CLI_CORE === '1'
+ ? '@claude-flow/cli-core@alpha'
+ : '@claude-flow/cli@latest';
+
+const ARGS = (() => {
+ const a = {
+ baseline: null, current: null,
+ baselineKey: null, currentKey: null,
+ alertOnWorsening: false, format: 'table',
+ };
+ for (let i = 2; i < process.argv.length; i++) {
+ const v = process.argv[i];
+ if (v === '--baseline') a.baseline = process.argv[++i];
+ else if (v === '--current') a.current = process.argv[++i];
+ else if (v === '--baseline-key') a.baselineKey = process.argv[++i];
+ else if (v === '--current-key') a.currentKey = process.argv[++i];
+ else if (v === '--alert-on-worsening') a.alertOnWorsening = true;
+ else if (v === '--format') a.format = process.argv[++i];
+ }
+ return a;
+})();
+
+function memRetrieve(key) {
+ const r = spawnSync('npx', [
+ CLI_PKG, 'memory', 'retrieve',
+ '--namespace', NS, '--key', key,
+ ], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
+ if (r.status !== 0) return null;
+ const m = /\{[\s\S]*\}/.exec(r.stdout || '');
+ if (!m) return null;
+ try { return JSON.parse(m[0]); } catch { return null; }
+}
+
+function loadRecord(label, filePath, memKey) {
+ if (filePath) {
+ if (!existsSync(filePath)) {
+ console.error(`audit-trend: ${label} file not found: ${filePath}`);
+ process.exit(2);
+ }
+ try { return JSON.parse(readFileSync(filePath, 'utf-8')); }
+ catch (e) {
+ console.error(`audit-trend: ${label} file invalid JSON: ${e.message}`);
+ process.exit(2);
+ }
+ }
+ if (memKey) {
+ const rec = memRetrieve(memKey);
+ if (!rec) {
+ console.error(`audit-trend: ${label} key not found in namespace ${NS}: ${memKey}`);
+ process.exit(2);
+ }
+ return rec;
+ }
+ console.error(`audit-trend: --${label} or --${label}-key is required`);
+ process.exit(2);
+}
+
+function main() {
+ const baseline = loadRecord('baseline', ARGS.baseline, ARGS.baselineKey);
+ const current = loadRecord('current', ARGS.current, ARGS.currentKey);
+
+ // Sanity: both must look like oia-audit records (composite + components).
+ if (!baseline.composite || !current.composite) {
+ console.error('audit-trend: snapshots must contain `composite` (from oia-audit.mjs)');
+ process.exit(2);
+ }
+
+ const baseWorst = String(baseline.composite.worst || 'clean').toLowerCase();
+ const currWorst = String(current.composite.worst || 'clean').toLowerCase();
+ const deltaRank = SEVERITY_RANK[currWorst] - SEVERITY_RANK[baseWorst];
+ const worsened = deltaRank > 0;
+ const improved = deltaRank < 0;
+
+ // Per-component status change. Each `components.{oiaManifest, threatModel,
+ // mcpScan}` has degraded:bool + exitCode + (for threat-model) json.worst.
+ const componentDelta = (label, b, c) => {
+ if (!b || !c) return { label, delta: 'missing' };
+ if (b.degraded !== c.degraded) {
+ return { label, delta: c.degraded ? 'became degraded' : 'recovered from degraded' };
+ }
+ if (b.exitCode !== c.exitCode) {
+ return { label, delta: `exit code ${b.exitCode} β†’ ${c.exitCode}` };
+ }
+ return { label, delta: 'unchanged' };
+ };
+ const components = {
+ oiaManifest: componentDelta('oia-manifest', baseline.components?.oiaManifest, current.components?.oiaManifest),
+ threatModel: componentDelta('threat-model', baseline.components?.threatModel, current.components?.threatModel),
+ mcpScan: componentDelta('mcp-scan', baseline.components?.mcpScan, current.components?.mcpScan),
+ };
+
+ // mcp-scan findings β€” what was introduced vs cleared.
+ const baseFindings = Array.isArray(baseline.components?.mcpScan?.json?.findings)
+ ? baseline.components.mcpScan.json.findings : [];
+ const currFindings = Array.isArray(current.components?.mcpScan?.json?.findings)
+ ? current.components.mcpScan.json.findings : [];
+ const fingerprint = (f) => `${f.severity}:${f.id ?? '-'}:${f.server ?? '-'}:${f.tool ?? '-'}:${(f.message ?? '').slice(0, 80)}`;
+ const baseSet = new Set(baseFindings.map(fingerprint));
+ const currSet = new Set(currFindings.map(fingerprint));
+ const introduced = currFindings.filter((f) => !baseSet.has(fingerprint(f)));
+ const cleared = baseFindings.filter((f) => !currSet.has(fingerprint(f)));
+
+ const payload = {
+ baseline: {
+ startedAt: baseline.startedAt,
+ composite: baseline.composite,
+ },
+ current: {
+ startedAt: current.startedAt,
+ composite: current.composite,
+ },
+ delta: {
+ worst: { baseline: baseWorst, current: currWorst, rankDelta: deltaRank,
+ verdict: worsened ? 'worsened' : (improved ? 'improved' : 'unchanged') },
+ components,
+ findings: {
+ introducedCount: introduced.length,
+ clearedCount: cleared.length,
+ introduced: introduced.slice(0, 20), // truncate for output sanity
+ cleared: cleared.slice(0, 20),
+ },
+ },
+ alert: ARGS.alertOnWorsening ? {
+ triggered: worsened,
+ reason: worsened
+ ? `composite worst ${baseWorst} β†’ ${currWorst}`
+ : `composite worst stable or improved (${baseWorst} β†’ ${currWorst})`,
+ } : null,
+ generatedAt: new Date().toISOString(),
+ };
+
+ if (ARGS.format === 'json') {
+ console.log(JSON.stringify(payload, null, 2));
+ } else {
+ console.log(`# audit-trend`);
+ console.log('');
+ console.log(`Baseline: ${baseline.startedAt}`);
+ console.log(`Current: ${current.startedAt}`);
+ console.log('');
+ console.log(`| Metric | Baseline | Current | Verdict |`);
+ console.log(`|---|---|---|---|`);
+ const verdictIcon = worsened ? '⚠ worsened' : (improved ? 'βœ“ improved' : 'β€” unchanged');
+ console.log(`| composite worst | ${baseWorst} | ${currWorst} | ${verdictIcon} |`);
+ for (const [label, c] of Object.entries(components)) {
+ console.log(`| ${label} | β€” | β€” | ${c.delta} |`);
+ }
+ console.log('');
+ console.log(`Findings: **+${introduced.length} introduced**, **βˆ’${cleared.length} cleared**`);
+ if (introduced.length > 0) {
+ console.log('');
+ console.log('## Introduced findings');
+ console.log('');
+ console.log('| Severity | Server | Tool | Message |');
+ console.log('|---|---|---|---|');
+ for (const f of introduced.slice(0, 20)) {
+ console.log(`| ${f.severity ?? '-'} | ${f.server ?? '-'} | ${f.tool ?? '-'} | ${(f.message ?? '').slice(0, 80)} |`);
+ }
+ }
+ if (cleared.length > 0) {
+ console.log('');
+ console.log('## Cleared findings');
+ console.log('');
+ for (const f of cleared.slice(0, 5)) {
+ console.log(`- ${f.severity ?? '-'} ${f.id ?? '-'} (${f.server ?? '-'}/${f.tool ?? '-'})`);
+ }
+ }
+ console.log('');
+ if (payload.alert) {
+ console.log(payload.alert.triggered ? `⚠ **ALERT**: ${payload.alert.reason}` : `βœ“ ${payload.alert.reason}`);
+ }
+ }
+
+ if (payload.alert?.triggered) process.exit(1);
+}
+
+main();
diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh
index f7b9111171..aafd0b58d1 100755
--- a/plugins/ruflo-metaharness/scripts/smoke.sh
+++ b/plugins/ruflo-metaharness/scripts/smoke.sh
@@ -170,6 +170,23 @@ if (!od.metaharness) { console.error('missing metaharness in optionalDependencie
if (j.dependencies && j.dependencies.metaharness) { console.error('metaharness leaked into dependencies'); process.exit(1); }
" 2>/dev/null && ok || bad "ruflo wrapper missing metaharness optionalDep"
+step "17i. audit-trend β€” diff two oia-audit snapshots (iter 15)"
+F="$ROOT/scripts/audit-trend.mjs"
+miss=""
+[[ -x "$F" ]] || miss="$miss not-executable"
+node --check "$F" 2>/dev/null || miss="$miss syntax-error"
+# Severity rank present + correct ordering
+grep -q "SEVERITY_RANK = { clean: 0, low: 1, medium: 2, high: 3 }" "$F" || miss="$miss no-severity-rank"
+# Both file-input AND memory-key-input paths
+grep -q "baseline-key\|baselineKey" "$F" || miss="$miss no-mem-key-input"
+grep -q "current-key\|currentKey" "$F" || miss="$miss no-current-key"
+# Findings set-diff (fingerprint-based)
+grep -q "fingerprint\|new Set" "$F" || miss="$miss no-findings-diff"
+# Alert flag + exit semantics
+grep -q "alert-on-worsening" "$F" || miss="$miss no-alert-flag"
+grep -q "process.exit(1)" "$F" || miss="$miss no-fail-closed"
+[[ -z "$miss" ]] && ok || bad "$miss"
+
step "17h. doctor integration β€” checkMetaharness in standard health checks (iter 14)"
F="$ROOT/../../v3/@claude-flow/cli/src/commands/doctor.ts"
miss=""