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(security): retry plain import + actionable error for aidefence load (closes #1807) (#1813)

rUv3673138249ab
diff --git a/v3/@claude-flow/cli/src/commands/doctor.ts b/v3/@claude-flow/cli/src/commands/doctor.ts
index b6fb51479c..cd2cbffbcd 100644
--- a/v3/@claude-flow/cli/src/commands/doctor.ts
+++ b/v3/@claude-flow/cli/src/commands/doctor.ts
@@ -240,6 +240,36 @@ async function checkGitRepo(): Promise<HealthCheck> {
}
}
+// Check AIDefence package availability (#1807)
+//
+// `aidefence_*` MCP tools (scan, analyze, has_pii, stats, learn) require
+// `@claude-flow/aidefence` to be installed and loadable. The package is an
+// optional dependency — present in some installs (project-local) but
+// missing in others (npm-global of `claude-flow`). Without it, every
+// aidefence MCP call fails at runtime with "Cannot find module".
+//
+// Surface that state in `doctor` so operators know BEFORE they rely on
+// AI-defence scanning. The probe is the same dynamic `import()` the MCP
+// tool's handler uses, so a `pass` here means the actual tools will work.
+async function checkAIDefence(): Promise<HealthCheck> {
+ try {
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
+ await import('@claude-flow/aidefence');
+ return {
+ name: 'AIDefence',
+ status: 'pass',
+ message: '@claude-flow/aidefence loadable — aidefence_* MCP tools functional',
+ };
+ } catch {
+ return {
+ name: 'AIDefence',
+ status: 'warn',
+ message: '@claude-flow/aidefence not loadable — aidefence_* MCP tools will fail (optional package)',
+ fix: 'npm install --save @claude-flow/aidefence (in your project), or run `claude-flow mcp start` from a directory that has it installed',
+ };
+ }
+}
+
// Check MCP servers
async function checkMcpServers(): Promise<HealthCheck> {
const mcpConfigPaths = [
@@ -650,6 +680,7 @@ export const doctorCommand: Command = {
checkMemoryDatabase,
checkApiKeys,
checkMcpServers,
+ checkAIDefence, // #1807
checkDiskSpace,
checkBuildTools,
checkAgenticFlow,
@@ -668,6 +699,7 @@ export const doctorCommand: Command = {
'api': checkApiKeys,
'git': checkGit,
'mcp': checkMcpServers,
+ 'aidefence': checkAIDefence, // #1807
'disk': checkDiskSpace,
'typescript': checkBuildTools,
'agentic-flow': checkAgenticFlow,
diff --git a/v3/@claude-flow/cli/src/mcp-tools/security-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/security-tools.ts
index b9eba583d6..07c4fca17f 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/security-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/security-tools.ts
@@ -84,7 +84,27 @@ async function getAIDefence(): Promise<AIDefenceInstance> {
throw new Error('AIDefence package not available. Install with: npm install @claude-flow/aidefence');
}
- // Retry with ESM cache busting via file:// URL + timestamp
+ // #1807 — auto-install lands the package somewhere Node's standard
+ // resolver couldn't find on the FIRST attempt (npm-global installs are
+ // a common offender). Try Node's resolver again first (it may have
+ // picked up the new node_modules directory), then fall back to the
+ // file:// + cache-bust import dance, then surface a clearly actionable
+ // error if everything still fails.
+ // Plain re-import (covers project-local installs that landed where Node
+ // looks). This often succeeds where the first attempt failed because
+ // the module cache is stable across the await boundary.
+ try {
+ const aidefence = await import(packageName);
+ const instance = aidefence.createAIDefence({ enableLearning: true });
+ if (instance) {
+ aidefenceInstance = instance;
+ console.error(`[claude-flow] ${packageName} loaded after install (resolver path)`);
+ return instance;
+ }
+ } catch { /* fall through to file:// attempt */ }
+
+ // file:// + cache-bust attempt (covers globally-installed packages whose
+ // path the standard resolver missed but require.resolve can locate).
try {
const modulePath = require.resolve(packageName);
const cacheBust = `?t=${Date.now()}`;
@@ -94,10 +114,17 @@ async function getAIDefence(): Promise<AIDefenceInstance> {
throw new Error('createAIDefence returned null after install');
}
aidefenceInstance = instance;
- console.error(`[claude-flow] ${packageName} loaded successfully after install`);
+ console.error(`[claude-flow] ${packageName} loaded after install (file:// path)`);
return instance;
} catch (retryError) {
- throw new Error(`AIDefence installed but failed to load: ${retryError}. Try restarting the MCP server.`);
+ throw new Error(
+ `AIDefence installed but failed to load: ${retryError}.\n` +
+ `This usually means npm installed the package somewhere Node's module resolver doesn't search ` +
+ `(common with global installs of \`claude-flow\`). Recovery options:\n` +
+ ` 1. Run \`npm install --save @claude-flow/aidefence\` in your project's working directory.\n` +
+ ` 2. Or run \`npx ruflo@latest mcp start\` from a directory whose node_modules contains the package.\n` +
+ ` 3. Or restart the MCP server after the install completes.`
+ );
}
}