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(ruflo-goals): research-list runs the installed ruflo CLI before npx @latest (#3558)

Mr. Maka04319a7322c
diff --git a/plugins/ruflo-goals/scripts/lib/ruflo-cli.mjs b/plugins/ruflo-goals/scripts/lib/ruflo-cli.mjs
new file mode 100644
index 0000000000..57f677342e
--- /dev/null
+++ b/plugins/ruflo-goals/scripts/lib/ruflo-cli.mjs
@@ -0,0 +1,123 @@
+// ruflo-cli.mjs — which ruflo CLI research-list.mjs runs (#3558).
+//
+// research-list.mjs ran `npx -y @claude-flow/cli@latest` for the namespace
+// listing and again for EVERY research record. A plugin installed from the
+// Claude Code marketplace is copied to
+// ~/.claude/plugins/cache/<marketplace>/<plugin>/<version>/ with no CLI next
+// to it, so each call resolved `latest` through the npm registry and could run
+// a different @claude-flow/cli (and memory store) than the one the user
+// installed and that deep-research wrote with.
+//
+// Same resolution as ruflo-adr/scripts/lib/ruflo-cli.mjs, first hit wins:
+// 1. a built @claude-flow/cli that ships this plugin, or a repo checkout
+// (<repo>/v3/@claude-flow/cli), guarded by package.json name AND
+// bin/cli.js AND dist/src/index.js;
+// 2. a `ruflo`, then `claude-flow`, executable on PATH (fs-only probe);
+// 3. otherwise the unchanged `npx -y @claude-flow/cli@latest`.
+//
+// TEST SEAMS (shared with ruflo-adr): RUFLO_PLUGIN_SKIP_LOCAL_CLI=1 disables
+// step 1, RUFLO_PLUGIN_SKIP_PATH_CLI=1 disables step 2.
+import { spawnSync } from 'node:child_process';
+import { accessSync, constants, existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
+import { basename, delimiter, dirname, isAbsolute, join, relative, sep } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+export const CLI_PKG = '@claude-flow/cli@latest';
+
+const PLUGIN_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
+
+/** Case-insensitive env lookup — Windows env keys are not case-stable (`Path`). */
+function envValue(env, name) {
+ const key = Object.keys(env).find((k) => k.toLowerCase() === name.toLowerCase());
+ return key ? env[key] : undefined;
+}
+
+/** bin/cli.js of a built @claude-flow/cli at `dir`, else null (#3366 guards). */
+function builtCliBin(dir) {
+ try {
+ const bin = join(dir, 'bin', 'cli.js');
+ const pj = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf-8'));
+ if (pj.name === '@claude-flow/cli' && existsSync(bin) && existsSync(join(dir, 'dist', 'src', 'index.js'))) return bin;
+ } catch { /* not this layout */ }
+ return null;
+}
+
+/**
+ * npm's Windows shim (`<prefix>/ruflo.cmd`, or `node_modules/.bin/ruflo.cmd`)
+ * cannot be spawned without a shell (CVE-2024-27980), so map it to the entry
+ * it wraps: the package's own `bin` field, which must resolve inside the
+ * package — ruflo-core's resolveNpmShim(). null when it is not such a shim.
+ */
+function npmShimEntry(shim, name) {
+ try {
+ const shimDir = dirname(shim);
+ const pkgDir = basename(shimDir).toLowerCase() === '.bin'
+ ? join(shimDir, '..', name)
+ : join(shimDir, 'node_modules', name);
+ const pj = JSON.parse(readFileSync(join(pkgDir, 'package.json'), 'utf-8'));
+ const declared = typeof pj.bin === 'string' ? pj.bin : pj.bin?.[name];
+ if (typeof declared !== 'string') return null;
+ const entry = realpathSync(join(pkgDir, declared));
+ const rel = relative(realpathSync(pkgDir), entry);
+ if (rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel) || !statSync(entry).isFile()) return null;
+ return entry;
+ } catch { return null; }
+}
+
+/**
+ * `ruflo`, then `claude-flow`, on PATH → `{ command, args, shell, source }`,
+ * else null. fs-only like ruflo-core's resolveCommandPath(): no `which` /
+ * `where` shell per call. Relative PATH entries (including the empty one) are
+ * skipped so a `ruflo` file inside the project being worked on is never run.
+ * On win32 only an npm shim that maps to its JS entry counts; anything else
+ * is skipped rather than run through cmd.exe. `env` / `platform` are
+ * parameters so the win32 branch is testable on POSIX.
+ */
+export function findRufloOnPath(env = process.env, platform = process.platform) {
+ const dirs = (envValue(env, 'PATH') || '')
+ .split(platform === 'win32' ? ';' : delimiter)
+ .filter((d) => isAbsolute(d));
+ for (const name of ['ruflo', 'claude-flow']) {
+ for (const dir of dirs) {
+ if (platform === 'win32') {
+ const shim = join(dir, `${name}.cmd`);
+ const entry = existsSync(shim) ? npmShimEntry(shim, name) : null;
+ if (entry) return { command: process.execPath, args: [entry], shell: false, source: 'path' };
+ continue;
+ }
+ const file = join(dir, name);
+ try {
+ accessSync(file, constants.X_OK);
+ if (statSync(file).isFile()) return { command: file, args: [], shell: false, source: 'path' };
+ } catch { /* keep searching */ }
+ }
+ }
+ return null;
+}
+
+let RUFLO_CLI;
+/** `{ command, args, shell, source }` for an installed ruflo CLI, or null (→ npx). Memoized. */
+export function resolveRufloCli() {
+ if (RUFLO_CLI !== undefined) return RUFLO_CLI;
+ const candidates = process.env.RUFLO_PLUGIN_SKIP_LOCAL_CLI === '1' ? [] : [
+ join(PLUGIN_ROOT, '..', '..'), // published: <cli>/plugins/<plugin>
+ join(PLUGIN_ROOT, '..', '..', 'v3', '@claude-flow', 'cli'), // repo checkout / marketplace clone
+ ];
+ for (const dir of candidates) {
+ const bin = builtCliBin(dir);
+ if (bin) return (RUFLO_CLI = { command: process.execPath, args: [bin], shell: false, source: 'local' });
+ }
+ RUFLO_CLI = process.env.RUFLO_PLUGIN_SKIP_PATH_CLI === '1' ? null : findRufloOnPath();
+ return RUFLO_CLI;
+}
+
+/**
+ * Run the ruflo CLI with `cliArgs` (e.g. ['memory', 'list', ...]): the
+ * installed one when it resolves, else `npx -y @claude-flow/cli@latest`,
+ * exactly as before. Never a shell.
+ */
+export function runCli(cliArgs, options = {}) {
+ const cli = resolveRufloCli();
+ if (cli) return spawnSync(cli.command, [...cli.args, ...cliArgs], { ...options, shell: false });
+ return spawnSync('npx', ['-y', CLI_PKG, ...cliArgs], { ...options, shell: false });
+}
diff --git a/plugins/ruflo-goals/scripts/research-list.mjs b/plugins/ruflo-goals/scripts/research-list.mjs
index a5ae3377a3..fa3a14d970 100644
--- a/plugins/ruflo-goals/scripts/research-list.mjs
+++ b/plugins/ruflo-goals/scripts/research-list.mjs
@@ -8,21 +8,21 @@
// Never throws: a missing CLI or empty namespace prints {version:1,records:[]}.
// Records that are not valid version-1 research records are skipped.
-import { spawnSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
+import { runCli } from './lib/ruflo-cli.mjs';
+
const NAMESPACE = 'research';
-const CLI_PKG = '@claude-flow/cli@latest';
-function npx(args) {
- return spawnSync('npx', ['-y', CLI_PKG, ...args], {
- stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: false,
- });
+// #3558: the installed ruflo CLI first, npx @latest only as the fallback
+// (lib/ruflo-cli.mjs), so this reads the store deep-research wrote.
+function cli(args) {
+ return runCli(args, { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8' });
}
function cliLoad() {
- const r = npx(['memory', 'list', '--namespace', NAMESPACE, '--format', 'json']);
+ const r = cli(['memory', 'list', '--namespace', NAMESPACE, '--format', 'json']);
if (r.status !== 0) return [];
const m = /\[[\s\S]*\]/.exec(r.stdout || '');
if (!m) return [];
@@ -31,7 +31,7 @@ function cliLoad() {
const out = [];
for (const e of entries) {
if (typeof e?.key !== 'string' || !e.key.startsWith('research-')) continue;
- const g = npx(['memory', 'retrieve', '--namespace', NAMESPACE, '--key', e.key, '--value-only']);
+ const g = cli(['memory', 'retrieve', '--namespace', NAMESPACE, '--key', e.key, '--value-only']);
if (g.status === 0) out.push({ key: e.key, value: (g.stdout || '').trim() });
}
return out;
diff --git a/plugins/ruflo-goals/scripts/smoke.sh b/plugins/ruflo-goals/scripts/smoke.sh
index bb2f0fbf30..f777fddc84 100755
--- a/plugins/ruflo-goals/scripts/smoke.sh
+++ b/plugins/ruflo-goals/scripts/smoke.sh
@@ -98,6 +98,9 @@ grep -E '^ - .*aidefence_scan' "$ROOT/agents/deep-researcher.md" >/dev/null ||
step "12. research-list.mjs runtime test passes"
out=$(node "$ROOT/scripts/test-research-list.mjs" 2>&1) && ok || bad "$out"
+step "12b. research-list.mjs prefers the installed ruflo CLI over npx @latest (#3558)"
+out=$(node "$ROOT/scripts/test-cli-resolution-3558.mjs" 2>&1) && ok || bad "$out"
+
# M1. The mod (ADR-445 pattern): hooks module registered, files within the 500-line rule
step "M1. mod: hooks.json names register.ts, every hook file is present and under 500 lines"
diff --git a/plugins/ruflo-goals/scripts/test-cli-resolution-3558.mjs b/plugins/ruflo-goals/scripts/test-cli-resolution-3558.mjs
new file mode 100644
index 0000000000..ab0aaeecc7
--- /dev/null
+++ b/plugins/ruflo-goals/scripts/test-cli-resolution-3558.mjs
@@ -0,0 +1,68 @@
+#!/usr/bin/env node
+// #3558: research-list.mjs must run the ruflo CLI that is already installed,
+// not `npx -y @claude-flow/cli@latest` once per call (1 list + 1 per record).
+//
+// Hermetic: the plugin is copied into a temp marketplace-cache layout (no CLI
+// beside it), and PATH holds only temp dirs with fake `ruflo` / `npx` scripts
+// that log their argv and answer like the memory CLI.
+import { strict as assert } from 'node:assert';
+import { spawnSync } from 'node:child_process';
+import { chmodSync, cpSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { delimiter, dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const PLUGIN_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
+const RECORD = JSON.stringify({ version: 1, question: 'q', status: 'done', findings: [], at: '2026-10-05T00:00:00Z' });
+
+function fixture(fn) {
+ const tmp = realpathSync(mkdtempSync(join(tmpdir(), 'ruflo-goals-cli-3558-')));
+ try {
+ const cache = join(tmp, 'home', '.claude', 'plugins', 'cache', 'ruflo', 'ruflo-goals', '0.0.0-test');
+ cpSync(PLUGIN_ROOT, cache, { recursive: true });
+ const log = join(tmp, 'calls.log');
+ const fake = (dir, name) => {
+ mkdirSync(dir, { recursive: true });
+ const f = join(dir, name);
+ writeFileSync(f, `#!/bin/sh\nprintf '%s %s\\n' "${name}" "$*" >> "${log}"\n` +
+ `case "$*" in *"memory list"*) echo '[{"key":"research-1"}]' ;; *"memory retrieve"*) echo '${RECORD}' ;; esac\n`);
+ chmodSync(f, 0o755);
+ };
+ const withRuflo = join(tmp, 'bin-ruflo'); fake(withRuflo, 'ruflo');
+ const withNpx = join(tmp, 'bin-npx'); fake(withNpx, 'npx');
+ const run = (pathDirs, env = {}) => {
+ if (existsSync(log)) rmSync(log);
+ const r = spawnSync(process.execPath, [join(cache, 'scripts', 'research-list.mjs')], {
+ encoding: 'utf-8',
+ env: { PATH: pathDirs.join(delimiter), HOME: join(tmp, 'home'), RUFLO_PLUGIN_SKIP_LOCAL_CLI: '1', ...env },
+ });
+ assert.equal(r.status, 0, r.stderr);
+ const calls = existsSync(log) ? readFileSync(log, 'utf-8').trim().split('\n') : [];
+ return { out: JSON.parse(r.stdout), calls };
+ };
+ fn({ run, withRuflo, withNpx });
+ } finally { rmSync(tmp, { recursive: true, force: true }); }
+}
+
+let passed = 0;
+fixture(({ run, withRuflo, withNpx }) => {
+ // 1. An installed ruflo on PATH serves the list and every retrieve; npx is never run.
+ const a = run([withRuflo, withNpx]);
+ assert.deepEqual(a.calls.map((c) => c.split(' ')[0]), ['ruflo', 'ruflo'], a.calls.join('\n'));
+ assert.match(a.calls[0], /^ruflo memory list --namespace research/);
+ assert.match(a.calls[1], /^ruflo memory retrieve --namespace research --key research-1/);
+ assert.equal(a.out.records.length, 1, 'the record read through the installed CLI is listed');
+ passed++;
+
+ // 2. No ruflo installed: the unchanged npx -y @latest fallback.
+ const b = run([withNpx]);
+ assert.ok(b.calls.length === 2 && b.calls.every((c) => c.startsWith('npx -y @claude-flow/cli@latest memory ')), b.calls.join('\n'));
+ assert.equal(b.out.records.length, 1);
+ passed++;
+
+ // 3. The PATH step can be switched off (test seam shared with ruflo-adr).
+ const c = run([withRuflo, withNpx], { RUFLO_PLUGIN_SKIP_PATH_CLI: '1' });
+ assert.ok(c.calls.every((x) => x.startsWith('npx ')), c.calls.join('\n'));
+ passed++;
+});
+console.log(`research-list cli resolution (#3558): ${passed} passed`);