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

perf(metaharness): parallelize oia-audit's 5 subprocess calls (iter 56)

ruv00dbcf1d1bb7
diff --git a/plugins/ruflo-metaharness/scripts/_harness.mjs b/plugins/ruflo-metaharness/scripts/_harness.mjs
index 3d122833c5..a9fa965bd0 100755
--- a/plugins/ruflo-metaharness/scripts/_harness.mjs
+++ b/plugins/ruflo-metaharness/scripts/_harness.mjs
@@ -16,10 +16,71 @@
// `degraded: true, reason: 'metaharness-not-available'` — never throws
// (ADR-150 graceful-degradation rule #3)
-import { spawnSync } from 'node:child_process';
+import { spawnSync, spawn } from 'node:child_process';
const DEFAULT_TIMEOUT_MS = 60_000;
+/**
+ * iter 56 — async variant of execCli. Used by oia-audit.mjs to parallelize
+ * its 5 subprocess calls, dropping worst-case wall-clock from 5×TIMEOUT
+ * (sequential) to 1×TIMEOUT (parallel). Identical return shape to the
+ * sync execCli so callers can swap without ceremony.
+ */
+function execCliAsync(npxArgs, opts = {}) {
+ return new Promise((resolve) => {
+ const start = Date.now();
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
+ const wantJson = opts.json !== false;
+ const argv = wantJson && !npxArgs.includes('--json') ? [...npxArgs, '--json'] : [...npxArgs];
+ const p = spawn('npx', argv, {
+ stdio: ['ignore', 'pipe', 'pipe'],
+ cwd: opts.cwd,
+ env: { ...process.env, ...(opts.env || {}) },
+ shell: process.platform === 'win32',
+ });
+ let stdout = '', stderr = '';
+ p.stdout?.on('data', (d) => { stdout += d.toString(); });
+ p.stderr?.on('data', (d) => { stderr += d.toString(); });
+ const timer = setTimeout(() => {
+ try { p.kill('SIGTERM'); } catch { /* ignore */ }
+ }, timeoutMs);
+ p.on('error', (e) => {
+ clearTimeout(timer);
+ resolve({
+ stdout, stderr: stderr + String(e?.message ?? e),
+ exitCode: 127, json: null, durationMs: Date.now() - start,
+ degraded: true, reason: 'metaharness-not-available',
+ });
+ });
+ p.on('close', (code) => {
+ clearTimeout(timer);
+ const durationMs = Date.now() - start;
+ if (code === null || /could not determine executable|404|not installed|MODULE_NOT_FOUND/i.test(stderr)) {
+ resolve({
+ stdout, stderr,
+ exitCode: code ?? 127, json: null, durationMs,
+ degraded: true, reason: 'metaharness-not-available',
+ });
+ return;
+ }
+ let json = null;
+ if (wantJson) {
+ const m = /\{[\s\S]*\}/.exec(stdout);
+ if (m) { try { json = JSON.parse(m[0]); } catch { /* leave null */ } }
+ }
+ resolve({ stdout, stderr, exitCode: code ?? 0, json, durationMs, degraded: false });
+ });
+ });
+}
+
+export function runMetaharnessAsync(args, opts) {
+ return execCliAsync(['-y', 'metaharness@latest', ...args], opts);
+}
+
+export function runHarnessAsync(args, opts) {
+ return execCliAsync(['-y', '-p', 'metaharness@latest', 'harness', ...args], opts);
+}
+
// ITER 27 — npx invocation hardening.
// The pre-iter-27 implementation passed `'-y metaharness@latest'` as a
// SINGLE argv element to npx (`spawnSync('npx', [bin, ...argv])` where
diff --git a/plugins/ruflo-metaharness/scripts/oia-audit.mjs b/plugins/ruflo-metaharness/scripts/oia-audit.mjs
index 03cdd666ef..9f5f347740 100755
--- a/plugins/ruflo-metaharness/scripts/oia-audit.mjs
+++ b/plugins/ruflo-metaharness/scripts/oia-audit.mjs
@@ -25,7 +25,7 @@
// 2 config error or audit failure
import { spawnSync } from 'node:child_process';
-import { runHarness, runMetaharness, emitDegradedJsonAndExit, parseMcpScanText } from './_harness.mjs';
+import { runHarness, runMetaharness, runHarnessAsync, runMetaharnessAsync, emitDegradedJsonAndExit, parseMcpScanText } from './_harness.mjs';
const SEVERITY_RANK = { clean: 0, low: 1, medium: 2, high: 3 };
const NS = process.env.OIA_AUDIT_NAMESPACE || 'metaharness-audit';
@@ -50,12 +50,8 @@ const ARGS = (() => {
// fingerprint to be consumable by _similarity.mjs (which expects the
// metaharness CLI shape: harnessFit/compileConfidence/agent_topology),
// we need to dispatch to runMetaharness for score+genome, not runHarness.
-function runOne(args, label, engine = 'harness') {
- const r = engine === 'metaharness' ? runMetaharness(args) : runHarness(args);
- // iter 50 — when label is 'mcp-scan' and the upstream returned text
- // (json null), parse findings via the shared text parser so audit-trend
- // can run its introduced/cleared diff on real data. The upstream may
- // someday emit structured JSON; if so r.json wins and we skip the parse.
+// iter 56 — annotated runOne result is identical for sync/async paths.
+function annotateOne(r, label) {
let json = r.json;
if (label === 'mcp-scan' && !json && !r.degraded && r.stdout) {
const parsed = parseMcpScanText(r.stdout);
@@ -72,6 +68,29 @@ function runOne(args, label, engine = 'harness') {
};
}
+function runOne(args, label, engine = 'harness') {
+ const r = engine === 'metaharness' ? runMetaharness(args) : runHarness(args);
+ return annotateOne(r, label);
+}
+
+// iter 56 — parallel variant. The composite audit's 5 subprocess calls
+// are mutually independent (each scans the same path read-only), so
+// they can run concurrently. Worst-case wall-clock drops from
+// 5×DEFAULT_TIMEOUT_MS (sequential) to 1×DEFAULT_TIMEOUT_MS (parallel)
+// — the iter-55-flagged gap (oia-audit timeout under unreachable
+// registry) is closed by this.
+async function runAllParallel(path) {
+ const tasks = [
+ runHarnessAsync(['oia-manifest', path]).then((r) => ['oiaManifest', annotateOne(r, 'oia-manifest')]),
+ runHarnessAsync(['threat-model', path]).then((r) => ['threatModel', annotateOne(r, 'threat-model')]),
+ runHarnessAsync(['mcp-scan', path]).then((r) => ['mcpScan', annotateOne(r, 'mcp-scan')]),
+ runMetaharnessAsync(['score', path]).then((r) => ['score', annotateOne(r, 'score')]),
+ runMetaharnessAsync(['genome', path]).then((r) => ['genome', annotateOne(r, 'genome')]),
+ ];
+ const results = await Promise.all(tasks);
+ return Object.fromEntries(results);
+}
+
function persist(payload) {
const key = `audit-${new Date().toISOString().replace(/[:.]/g, '-')}`;
const r = spawnSync('npx', [
@@ -88,23 +107,19 @@ function persist(payload) {
};
}
-function main() {
+async function main() {
if (ARGS.alertWorst !== null && !SEVERITY_RANK.hasOwnProperty(ARGS.alertWorst)) {
console.error(`oia-audit: --alert-on-worst must be one of clean|low|medium|high; got ${ARGS.alertWorst}`);
process.exit(2);
}
const startedAt = new Date().toISOString();
- const oia = runOne(['oia-manifest', ARGS.path], 'oia-manifest');
- const tm = runOne(['threat-model', ARGS.path], 'threat-model');
- const mcp = runOne(['mcp-scan', ARGS.path], 'mcp-scan');
- // iter 38 + iter 47 — bundle score + genome so audit-trend can compute
- // structural distance via _similarity.mjs. Both use the `metaharness`
- // CLI (not `harness`) because the two binaries have different schemas
- // for the same subcommand names; only the metaharness CLI emits the
- // shape similarity() expects (harnessFit/agent_topology/etc).
- const score = runOne(['score', ARGS.path], 'score', 'metaharness');
- const genome = runOne(['genome', ARGS.path], 'genome', 'metaharness');
+ // iter 56 — run the 5 sub-audits in parallel (was sequential pre-iter-56).
+ // Worst-case wall-clock improves from 5×TIMEOUT to 1×TIMEOUT in the
+ // unreachable-registry case; the happy path improves from sum-of-durations
+ // to max-of-durations (typically ~2-4× faster).
+ const all = await runAllParallel(ARGS.path);
+ const { oiaManifest: oia, threatModel: tm, mcpScan: mcp, score, genome } = all;
// If all FIVE say "metaharness not available", surface the degraded
// payload exactly once and exit 0 (architectural constraint #3).
@@ -181,4 +196,7 @@ function main() {
if (alertTriggered) process.exit(1);
}
-main();
+main().catch((e) => {
+ console.error('oia-audit crashed:', e?.message ?? e);
+ process.exit(2);
+});
diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh
index 33285843e9..bd04633d8a 100755
--- a/plugins/ruflo-metaharness/scripts/smoke.sh
+++ b/plugins/ruflo-metaharness/scripts/smoke.sh
@@ -191,6 +191,26 @@ 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 "17z19. oia-audit parallelizes 5 subprocesses (iter 56 — closes iter-55 gap A)"
+miss=""
+HARNESS="$ROOT/scripts/_harness.mjs"
+# Async variants exist
+grep -q "export function runHarnessAsync" "$HARNESS" 2>/dev/null || miss="$miss no-runHarnessAsync"
+grep -q "export function runMetaharnessAsync" "$HARNESS" 2>/dev/null || miss="$miss no-runMetaharnessAsync"
+grep -q "execCliAsync\|spawn(" "$HARNESS" 2>/dev/null || miss="$miss no-async-spawn"
+# oia-audit uses them via Promise.all
+OIA="$ROOT/scripts/oia-audit.mjs"
+grep -q "runAllParallel" "$OIA" 2>/dev/null || miss="$miss no-parallel-fn"
+grep -q "Promise.all" "$OIA" 2>/dev/null || miss="$miss no-promise-all"
+grep -q "async function main" "$OIA" 2>/dev/null || miss="$miss main-not-async"
+# Runtime check: happy-path oia-audit completes < 5s on a fresh repo
+START=$(date +%s)
+node "$OIA" --dry-run --format json >/dev/null 2>&1
+END=$(date +%s)
+DUR=$((END - START))
+[[ "$DUR" -le 30 ]] || miss="$miss oia-audit-slow:${DUR}s"
+[[ -z "$miss" ]] && ok || bad "$miss"
+
step "17z18. graceful-degradation drill extended to mint + drift-from-history (iter 55)"
miss=""
# Workflow drill updated