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

fix(cost): preserve memory JSON across Windows npx

ruvnet49b36d7acd7c
diff --git a/plugins/ruflo-cost-tracker/scripts/_npx.mjs b/plugins/ruflo-cost-tracker/scripts/_npx.mjs
new file mode 100644
index 0000000000..0dd1644f30
--- /dev/null
+++ b/plugins/ruflo-cost-tracker/scripts/_npx.mjs
@@ -0,0 +1,15 @@
+import { spawnSync } from 'node:child_process';
+import { dirname, join } from 'node:path';
+
+// Windows npm exposes npx through a .cmd shim, which cannot be spawned
+// directly by Node without a shell. Invoking npm's JS entry point preserves
+// argv exactly (especially JSON values) and avoids shell injection/quoting.
+export function spawnNpxSync(args, options = {}) {
+ const npxArgs = args[0] === '-y' ? args : ['-y', ...args];
+ const { shell: _ignoredShell, ...safeOptions } = options;
+ if (process.platform === 'win32') {
+ const npxCli = join(dirname(process.execPath), 'node_modules', 'npm', 'bin', 'npx-cli.js');
+ return spawnSync(process.execPath, [npxCli, ...npxArgs], { ...safeOptions, shell: false });
+ }
+ return spawnSync('npx', npxArgs, { ...safeOptions, shell: false });
+}
diff --git a/plugins/ruflo-cost-tracker/scripts/_sessions.mjs b/plugins/ruflo-cost-tracker/scripts/_sessions.mjs
index d1d8a30bbb..9a0cdd5f6d 100644
--- a/plugins/ruflo-cost-tracker/scripts/_sessions.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/_sessions.mjs
@@ -14,14 +14,14 @@
// - `sessionTs(rec)` β†’ ms-since-epoch from capturedAt|endedAt|startedAt
// - `parseDurationMs(spec)` β†’ ms for "Nh"/"Nd"/"Nw"/"Nm"; null on parse fail
-import { spawnSync } from 'node:child_process';
+import { spawnNpxSync } from './_npx.mjs';
const CLI_PKG = process.env.CLI_CORE === '1'
? '@claude-flow/cli-core@alpha'
: '@claude-flow/cli@latest';
export function memoryListAllKeys(namespace) {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'list',
'--namespace', namespace, '--format', 'json',
], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
@@ -40,14 +40,12 @@ export function memoryListSessionKeys(namespace) {
}
export function memoryRetrieve(namespace, key) {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'retrieve',
- '--namespace', namespace, '--key', key,
+ '--namespace', namespace, '--key', key, '--value-only',
], { 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; }
+ try { return JSON.parse((r.stdout || '').trim()); } catch { return null; }
}
export function loadSessions(namespace) {
diff --git a/plugins/ruflo-cost-tracker/scripts/budget.mjs b/plugins/ruflo-cost-tracker/scripts/budget.mjs
index de2170818a..f93e8a3dfc 100755
--- a/plugins/ruflo-cost-tracker/scripts/budget.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/budget.mjs
@@ -12,7 +12,7 @@
// BUDGET_PERIOD=today|week|month|all filter session totals by capturedAt
// BUDGET_QUIET=1 machine-readable JSON only
-import { spawnSync } from 'node:child_process';
+import { spawnNpxSync } from './_npx.mjs';
// iter 73 β€” shared session-loader (was duplicated across 6 scripts).
// Only the session-list path consolidates; budget-config reads stay local
// because they have their own "latest stamp" upsert resolution logic.
@@ -37,7 +37,7 @@ function memoryStore(key, value) {
// This avoids the conflict entirely. Retrieve resolves via the index.
if (key === KEY) {
const stamped = `${KEY}-${Date.now()}`;
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'store',
'--namespace', NS, '--key', stamped, '--value', JSON.stringify(value),
], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
@@ -47,7 +47,7 @@ function memoryStore(key, value) {
// (timestamp suffixes sort correctly because they are equal-width).
return;
}
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'store',
'--namespace', NS, '--key', key, '--value', JSON.stringify(value),
], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
@@ -55,20 +55,18 @@ function memoryStore(key, value) {
}
function memoryRetrieveOne(key) {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'retrieve',
- '--namespace', NS, '--key', key,
+ '--namespace', NS, '--key', key, '--value-only',
], { 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; }
+ try { return JSON.parse((r.stdout || '').trim()); } catch { return null; }
}
function memoryRetrieve(key) {
// For budget-config: pick the latest budget-config-<timestamp> entry.
if (key === KEY) {
- const list = spawnSync('npx', [
+ const list = spawnNpxSync([
CLI_PKG, 'memory', 'list',
'--namespace', NS, '--format', 'json',
], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
diff --git a/plugins/ruflo-cost-tracker/scripts/federation.mjs b/plugins/ruflo-cost-tracker/scripts/federation.mjs
index 58e6b9ed88..81d766d5f2 100755
--- a/plugins/ruflo-cost-tracker/scripts/federation.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/federation.mjs
@@ -17,7 +17,7 @@
// FED_FORMAT=json node scripts/federation.mjs # JSON
// FED_NAMESPACE=federation-spend (default)
-import { spawnSync } from 'node:child_process';
+import { spawnNpxSync } from './_npx.mjs';
// ADR-100 / #1748 Issue 3 β€” CLI_CORE=1 routes to lite cli-core (~2s cold-cache).
// Federation aggregation is list+retrieve only on the federation-spend namespace.
@@ -28,7 +28,7 @@ const CLI_PKG = process.env.CLI_CORE === '1'
const NS = process.env.FED_NAMESPACE || 'federation-spend';
function memoryList() {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'list',
'--namespace', NS, '--format', 'json',
], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
@@ -38,14 +38,12 @@ function memoryList() {
try { return JSON.parse(m[0]).map((e) => e.key).filter(Boolean); } catch { return []; }
}
function memoryRetrieve(key) {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
CLI_PKG, 'memory', 'retrieve',
- '--namespace', NS, '--key', key,
+ '--namespace', NS, '--key', key, '--value-only',
], { 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; }
+ try { return JSON.parse((r.stdout || '').trim()); } catch { return null; }
}
function gather() {
diff --git a/plugins/ruflo-cost-tracker/scripts/outcome.mjs b/plugins/ruflo-cost-tracker/scripts/outcome.mjs
index 27213cef27..7f14cd69d7 100755
--- a/plugins/ruflo-cost-tracker/scripts/outcome.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/outcome.mjs
@@ -15,7 +15,7 @@
// shell pipelines is error-prone. This script uses spawnSync with explicit
// argv so a task description with quotes/newlines is preserved.
-import { spawnSync } from 'node:child_process';
+import { spawnNpxSync } from './_npx.mjs';
const ALLOWED = new Set(['success', 'escalated', 'failure']);
@@ -30,7 +30,7 @@ function main() {
console.error(`invalid outcome '${outcome}' β€” must be one of: success, escalated, failure`);
process.exit(2);
}
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
'@claude-flow/cli@latest', 'hooks', 'model-outcome',
'-t', task, '-m', model, '-o', outcome,
], { stdio: 'inherit', shell: process.platform === 'win32' });
diff --git a/plugins/ruflo-cost-tracker/scripts/smoke.sh b/plugins/ruflo-cost-tracker/scripts/smoke.sh
index 1a49b4a4ec..177ff50967 100755
--- a/plugins/ruflo-cost-tracker/scripts/smoke.sh
+++ b/plugins/ruflo-cost-tracker/scripts/smoke.sh
@@ -249,12 +249,12 @@ grep -q "|| true" "$F" || miss="$miss not-resilient"
grep -q "CLAUDE_PLUGIN_ROOT" "$F" || miss="$miss no-plugin-root-var"
[[ -z "$miss" ]] && ok || bad "$miss"
-step "29. track.mjs harness present + parses + uses spawnSync (no shell-escape risks)"
+step "29. track.mjs harness present + parses + uses safe argv spawning"
F="$ROOT/scripts/track.mjs"
miss=""
[[ -x "$F" ]] || miss="$miss not-executable"
node --check "$F" 2>/dev/null || miss="$miss syntax-error"
-grep -q "spawnSync" "$F" || miss="$miss no-spawnSync"
+grep -qE "spawnNpxSync|spawnSync" "$F" || miss="$miss no-safe-spawn"
grep -q "PRICING" "$F" || miss="$miss no-pricing-table"
[[ -z "$miss" ]] && ok || bad "$miss"
@@ -285,12 +285,12 @@ grep -qE "budget\.mjs|cost-tracking:budget-config" "$F" || miss="$miss budget-sc
grep -q '^allowed-tools:[[:space:]]*\*' "$F" && miss="$miss wildcard"
[[ -z "$miss" ]] && ok || bad "$miss"
-step "32. budget.mjs harness present + parses + uses spawnSync"
+step "32. budget.mjs harness present + parses + uses safe argv spawning"
F="$ROOT/scripts/budget.mjs"
miss=""
[[ -x "$F" ]] || miss="$miss not-executable"
node --check "$F" 2>/dev/null || miss="$miss syntax-error"
-grep -qE "spawnSync|_sessions\.mjs" "$F" || miss="$miss no-safe-exec"
+grep -qE "spawnNpxSync|spawnSync|_sessions\.mjs" "$F" || miss="$miss no-safe-exec"
grep -qE "HARD_STOP|alertLevel" "$F" || miss="$miss no-alert-impl"
grep -q "process.exit(1)" "$F" || miss="$miss no-fail-closed"
# iter 75 regression guard: in the WITH-BUDGET branch (after `const alert =
@@ -325,7 +325,7 @@ F="$ROOT/scripts/outcome.mjs"
miss=""
[[ -x "$F" ]] || miss="$miss not-executable"
node --check "$F" 2>/dev/null || miss="$miss syntax-error"
-grep -q "spawnSync" "$F" || miss="$miss no-spawnSync"
+grep -qE "spawnNpxSync|spawnSync" "$F" || miss="$miss no-safe-spawn"
grep -q "hooks.*model-outcome" "$F" || miss="$miss no-hooks-call"
grep -qE "success.*escalated.*failure|ALLOWED" "$F" || miss="$miss no-validation"
[[ -z "$miss" ]] && ok || bad "$miss"
@@ -621,7 +621,7 @@ F="$ROOT/scripts/_sessions.mjs"
miss=""
[[ -f "$F" ]] || miss="$miss missing"
node --check "$F" 2>/dev/null || miss="$miss syntax-error"
-grep -q "spawnSync" "$F" || miss="$miss no-spawnSync"
+grep -qE "spawnNpxSync|spawnSync" "$F" || miss="$miss no-safe-spawn"
grep -q "memoryListAllKeys" "$F" || miss="$miss no-list-all-export"
grep -q "memoryListSessionKeys" "$F" || miss="$miss no-list-session-export"
grep -q "memoryRetrieve" "$F" || miss="$miss no-retrieve-export"
diff --git a/plugins/ruflo-cost-tracker/scripts/test-health-integration.mjs b/plugins/ruflo-cost-tracker/scripts/test-health-integration.mjs
index a8b159f87a..b85f80c611 100755
--- a/plugins/ruflo-cost-tracker/scripts/test-health-integration.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/test-health-integration.mjs
@@ -27,6 +27,7 @@ import { mkdtempSync, rmSync, mkdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { tmpdir } from 'node:os';
+import { spawnNpxSync } from './_npx.mjs';
const SCRIPTS_DIR = dirname(fileURLToPath(import.meta.url));
const HEALTH = join(SCRIPTS_DIR, 'health.mjs');
@@ -48,7 +49,7 @@ function assert(cond, label) {
}
function memStore(cwd, key, valueObj) {
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
'-y', '@claude-flow/cli@latest', 'memory', 'store',
'--namespace', NS, '--key', key,
'--value', JSON.stringify(valueObj),
@@ -65,6 +66,22 @@ function memStore(cwd, key, valueObj) {
return r.status === 0;
}
+function memInit(cwd) {
+ const r = spawnNpxSync([
+ '-y', '@claude-flow/cli@latest', 'memory', 'init',
+ ], {
+ cwd,
+ stdio: ['ignore', 'pipe', 'pipe'],
+ encoding: 'utf-8',
+ shell: process.platform === 'win32',
+ });
+ if (r.status !== 0) {
+ const detail = r.error?.message || r.stderr?.trim() || `exit status ${r.status}`;
+ console.error(` β†’ npx memory init failed: ${detail}`);
+ }
+ return r.status === 0;
+}
+
function runHealth(cwd) {
return new Promise((resolve) => {
const p = spawn('node', [HEALTH, '--format', 'json'], { cwd, stdio: ['ignore', 'pipe', 'pipe'] });
@@ -120,6 +137,7 @@ async function main() {
{
const cwd = join(fixture, 'case-2-hardstop');
mkdirSync(join(cwd, '.swarm'), { recursive: true });
+ if (!memInit(cwd)) { console.error(' β†’ fixture initialization failed'); process.exit(2); }
// 5 sessions Γ— $0.10 = $0.50 spend
for (let i = 0; i < 5; i++) {
const ts = new Date(Date.now() - (i + 2) * 86_400_000).toISOString();
@@ -166,7 +184,14 @@ async function main() {
// Cleanup
if (process.env.TEST_HEALTH_KEEP_FIXTURE !== '1') {
- rmSync(fixture, { recursive: true, force: true });
+ try {
+ rmSync(fixture, { recursive: true, force: true });
+ } catch (error) {
+ // Windows can retain a transient AgentDB handle after the npx child
+ // exits. Fixture cleanup must not hide a completed contract test.
+ if (process.platform !== 'win32' || error?.code !== 'EPERM') throw error;
+ console.warn(` β†’ fixture cleanup deferred: ${error.message}`);
+ }
} else {
console.log(`\n(fixture kept at ${fixture})`);
}
diff --git a/plugins/ruflo-cost-tracker/scripts/track.mjs b/plugins/ruflo-cost-tracker/scripts/track.mjs
index d773b393b9..f564324b75 100755
--- a/plugins/ruflo-cost-tracker/scripts/track.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/track.mjs
@@ -16,7 +16,7 @@
import { readFileSync, writeFileSync, readdirSync, statSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
-import { spawnSync } from 'node:child_process';
+import { spawnNpxSync } from './_npx.mjs';
// iter 68 β€” shared PRICING table (was duplicated in counterfactual.mjs too).
import { modelTier, costForUsage } from './_prices.mjs';
@@ -104,7 +104,7 @@ function persistToMemory(summary) {
? '@claude-flow/cli-core@alpha'
: '@claude-flow/cli@latest';
// spawnSync with explicit args avoids shell-escape pitfalls for the JSON value.
- const r = spawnSync('npx', [
+ const r = spawnNpxSync([
cliPkg, 'memory', 'store',
'--namespace', ns,
'--key', key,