🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
feat: AgentDB integration, real analyze commands, honesty tests v3.5.58
Reuvene030ee0397b9
diff --git a/package.json b/package.jsonindex 418df59dee..cd730d57c5 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "claude-flow",- "version": "3.5.57",+ "version": "3.5.58","description": "Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration","main": "dist/index.js","type": "module",diff --git a/ruflo/package.json b/ruflo/package.jsonindex c4fe437cca..7f49350e48 100644--- a/ruflo/package.json+++ b/ruflo/package.json@@ -1,6 +1,6 @@{"name": "ruflo",- "version": "3.5.57",+ "version": "3.5.58","description": "Ruflo - Enterprise AI agent orchestration platform. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration","main": "bin/ruflo.js","type": "module",diff --git a/v3/@claude-flow/cli/package.json b/v3/@claude-flow/cli/package.jsonindex 95bcdb2b9a..9c0c0ef0bd 100644--- a/v3/@claude-flow/cli/package.json+++ b/v3/@claude-flow/cli/package.json@@ -1,6 +1,6 @@{"name": "@claude-flow/cli",- "version": "3.5.57",+ "version": "3.5.58","type": "module","description": "Ruflo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code","main": "dist/src/index.js",diff --git a/v3/@claude-flow/cli/src/commands/analyze.ts b/v3/@claude-flow/cli/src/commands/analyze.tsindex 493221e528..5010d767c7 100644--- a/v3/@claude-flow/cli/src/commands/analyze.ts+++ b/v3/@claude-flow/cli/src/commands/analyze.ts@@ -21,6 +21,7 @@ import * as path from 'path';import * as fs from 'fs/promises';import { writeFile } from 'fs/promises';import { resolve } from 'path';+import { execSync } from 'child_process';// Dynamic import for AST analyzerasync function getASTAnalyzer() {@@ -314,30 +315,155 @@ const codeCommand: Command = {{ command: 'claude-flow analyze code --type complexity', description: 'Run complexity analysis' },],action: async (ctx: CommandContext): Promise<CommandResult> => {- const path = ctx.flags.path as string || '.';+ const targetPath = resolve(ctx.flags.path as string || '.');const analysisType = ctx.flags.type as string || 'quality';+ const formatJson = (ctx.flags.format as string) === 'json';output.writeln();output.writeln(output.bold('Code Analysis'));output.writeln(output.dim('-'.repeat(50)));- output.printInfo(`Analyzing ${path} for ${analysisType}...`);- output.writeln();+ const spinner = output.createSpinner({ text: `Analyzing ${targetPath}...`, spinner: 'dots' });+ spinner.start();- // Placeholder - would integrate with actual code analysis tools- output.printBox(- [- `Path: ${path}`,- `Type: ${analysisType}`,- `Status: Feature in development`,- ``,- `Code analysis capabilities coming soon.`,- `Use 'analyze diff' for change analysis.`,- ].join('\n'),- 'Code Analysis'- );+ try {+ const files = await scanSourceFiles(targetPath);+ if (files.length === 0) {+ spinner.stop();+ output.printWarning('No source files found');+ return { success: true };+ }- return { success: true };+ const fileStats: Array<{ file: string; loc: number; todos: number; functions: number; imports: number; maxNesting: number; securityIssues: string[] }> = [];++ for (const filePath of files) {+ const content = await fs.readFile(filePath, 'utf-8');+ const lines = content.split('\n');+ const nonEmpty = lines.filter(l => l.trim().length > 0 && !/^\s*(\/\/|\/\*|\*\s|#)/.test(l)).length;+ const todos = (content.match(/\b(TODO|FIXME|HACK|XXX)\b/gi) || []).length;+ const fns = (content.match(/(?:export\s+)?(?:async\s+)?function\s+\w+|(?:const|let|var)\s+\w+\s*=\s*(?:async\s+)?\([^)]*\)\s*=>/g) || []).length;+ const imps = (content.match(/^import\s+/gm) || []).length + (content.match(/require\s*\(/g) || []).length;++ let maxNesting = 0;+ let nesting = 0;+ for (const line of lines) {+ nesting += (line.match(/\{/g) || []).length;+ nesting -= (line.match(/\}/g) || []).length;+ if (nesting > maxNesting) maxNesting = nesting;+ }++ const securityIssues: string[] = [];+ if (/\beval\s*\(/.test(content)) securityIssues.push('eval()');+ if (/\bexec\s*\(/.test(content)) securityIssues.push('exec()');+ if (/\.innerHTML\s*=/.test(content)) securityIssues.push('innerHTML');+ if (/dangerouslySetInnerHTML/.test(content)) securityIssues.push('dangerouslySetInnerHTML');+ if (/['"](?:password|secret|api[_-]?key|token)\s*[:=]\s*['"][^'"]{3,}['"]/i.test(content)) securityIssues.push('hardcoded secret');+ if (/new\s+Function\s*\(/.test(content)) securityIssues.push('new Function()');++ fileStats.push({+ file: filePath,+ loc: nonEmpty,+ todos,+ functions: fns,+ imports: imps,+ maxNesting,+ securityIssues,+ });+ }++ spinner.stop();++ const totalLoc = fileStats.reduce((s, f) => s + f.loc, 0);+ const totalTodos = fileStats.reduce((s, f) => s + f.todos, 0);+ const totalFunctions = fileStats.reduce((s, f) => s + f.functions, 0);+ const totalImports = fileStats.reduce((s, f) => s + f.imports, 0);+ const avgFileSize = Math.round(totalLoc / files.length);+ const longestFile = fileStats.reduce((a, b) => a.loc > b.loc ? a : b);+ const avgFnPerFile = (totalFunctions / files.length).toFixed(1);+ const deepestNesting = fileStats.reduce((a, b) => a.maxNesting > b.maxNesting ? a : b);+ const allSecurityIssues = fileStats.filter(f => f.securityIssues.length > 0);++ if (formatJson) {+ const jsonData = { type: analysisType, path: targetPath, files: files.length, totalLoc, totalTodos, totalFunctions, totalImports, avgFileSize, fileStats: fileStats.map(f => ({ relativePath: path.relative(targetPath, f.file), loc: f.loc, todos: f.todos, functions: f.functions, imports: f.imports, maxNesting: f.maxNesting, securityIssues: f.securityIssues })) };+ output.printJson(jsonData);+ return { success: true, data: jsonData };+ }++ if (analysisType === 'quality') {+ output.printBox(+ [`Files: ${files.length}`, `Lines of Code: ${totalLoc.toLocaleString()}`, `Avg File Size: ${avgFileSize} LOC`, `TODO/FIXME: ${totalTodos}`, `Functions: ${totalFunctions}`, `Imports: ${totalImports}`].join('\n'),+ 'Quality Summary'+ );+ output.writeln();+ output.writeln(output.bold('Largest Files'));+ output.writeln(output.dim('-'.repeat(60)));+ const top10 = [...fileStats].sort((a, b) => b.loc - a.loc).slice(0, 10);+ output.printTable({+ columns: [+ { key: 'file', header: 'File', width: 45 },+ { key: 'loc', header: 'LOC', width: 8, align: 'right' as const },+ { key: 'fns', header: 'Fns', width: 6, align: 'right' as const },+ { key: 'todos', header: 'TODOs', width: 7, align: 'right' as const },+ ],+ data: top10.map(f => ({ file: path.relative(targetPath, f.file), loc: f.loc, fns: f.functions, todos: f.todos })),+ });+ if (totalTodos > 0) {+ output.writeln();+ output.printWarning(`${totalTodos} TODO/FIXME comments found across ${fileStats.filter(f => f.todos > 0).length} files`);+ }+ } else if (analysisType === 'complexity') {+ output.printBox(+ [`Files: ${files.length}`, `Total Functions: ${totalFunctions}`, `Avg Functions/File: ${avgFnPerFile}`, `Deepest Nesting: ${deepestNesting.maxNesting} levels (${path.relative(targetPath, deepestNesting.file)})`, `Longest File: ${longestFile.loc} LOC (${path.relative(targetPath, longestFile.file)})`].join('\n'),+ 'Complexity Summary'+ );+ output.writeln();+ output.writeln(output.bold('High Complexity Files (nesting > 5)'));+ output.writeln(output.dim('-'.repeat(60)));+ const complex = fileStats.filter(f => f.maxNesting > 5).sort((a, b) => b.maxNesting - a.maxNesting);+ if (complex.length === 0) {+ output.printSuccess('No files with excessive nesting detected');+ } else {+ output.printTable({+ columns: [+ { key: 'file', header: 'File', width: 45 },+ { key: 'nesting', header: 'Max Nest', width: 10, align: 'right' as const },+ { key: 'fns', header: 'Fns', width: 6, align: 'right' as const },+ { key: 'loc', header: 'LOC', width: 8, align: 'right' as const },+ ],+ data: complex.slice(0, 15).map(f => ({ file: path.relative(targetPath, f.file), nesting: f.maxNesting, fns: f.functions, loc: f.loc })),+ });+ }+ } else if (analysisType === 'security') {+ output.printBox(+ [`Files Scanned: ${files.length}`, `Files with Issues: ${allSecurityIssues.length}`, `Total Issues: ${allSecurityIssues.reduce((s, f) => s + f.securityIssues.length, 0)}`].join('\n'),+ 'Security Summary'+ );+ if (allSecurityIssues.length === 0) {+ output.writeln();+ output.printSuccess('No common security patterns detected');+ } else {+ output.writeln();+ output.writeln(output.bold('Security Concerns'));+ output.writeln(output.dim('-'.repeat(60)));+ output.printTable({+ columns: [+ { key: 'file', header: 'File', width: 40 },+ { key: 'issues', header: 'Issues', width: 35 },+ ],+ data: allSecurityIssues.map(f => ({ file: path.relative(targetPath, f.file), issues: f.securityIssues.join(', ') })),+ });+ }+ } else {+ output.printWarning(`Unknown analysis type: ${analysisType}. Use quality, complexity, or security.`);+ }++ return { success: true };+ } catch (error) {+ spinner.stop();+ const message = error instanceof Error ? error.message : String(error);+ output.printError(`Code analysis failed: ${message}`);+ return { success: false, exitCode: 1 };+ }},};@@ -1294,28 +1420,132 @@ const depsCommand: Command = {action: async (ctx: CommandContext): Promise<CommandResult> => {const showOutdated = ctx.flags.outdated as boolean;const checkSecurity = ctx.flags.security as boolean;+ const formatJson = (ctx.flags.format as string) === 'json';output.writeln();output.writeln(output.bold('Dependency Analysis'));output.writeln(output.dim('-'.repeat(50)));- output.printInfo('Analyzing dependencies...');- output.writeln();+ try {+ const pkgPath = resolve('package.json');+ let pkgContent: string;+ try {+ pkgContent = await fs.readFile(pkgPath, 'utf-8');+ } catch {+ output.printError('No package.json found in current directory');+ return { success: false, exitCode: 1 };+ }- // Placeholder - would integrate with npm/yarn audit- output.printBox(- [- `Outdated Check: ${showOutdated ? 'Enabled' : 'Disabled'}`,- `Security Check: ${checkSecurity ? 'Enabled' : 'Disabled'}`,- `Status: Feature in development`,- ``,- `Dependency analysis capabilities coming soon.`,- `Use 'security scan --type deps' for security scanning.`,- ].join('\n'),- 'Dependency Analysis'- );+ const pkg = JSON.parse(pkgContent);+ const deps = Object.entries(pkg.dependencies || {}) as [string, string][];+ const devDeps = Object.entries(pkg.devDependencies || {}) as [string, string][];+ const optDeps = Object.entries(pkg.optionalDependencies || {}) as [string, string][];+ const peerDeps = Object.entries(pkg.peerDependencies || {}) as [string, string][];+ const total = deps.length + devDeps.length + optDeps.length + peerDeps.length;++ if (formatJson && !showOutdated && !checkSecurity) {+ const jsonData = { name: pkg.name, version: pkg.version, dependencies: deps.length, devDependencies: devDeps.length, optionalDependencies: optDeps.length, peerDependencies: peerDeps.length, total };+ output.printJson(jsonData);+ return { success: true, data: jsonData };+ }- return { success: true };+ output.printBox(+ [`Package: ${pkg.name || 'unknown'} @ ${pkg.version || '0.0.0'}`, `Dependencies: ${deps.length}`, `Dev Dependencies: ${devDeps.length}`, `Optional: ${optDeps.length}`, `Peer: ${peerDeps.length}`, `Total: ${total}`].join('\n'),+ 'Dependency Summary'+ );++ if (showOutdated) {+ output.writeln();+ output.writeln(output.bold('Outdated Check'));+ output.writeln(output.dim('-'.repeat(60)));+ const outdated: Array<{ name: string; declared: string; installed: string; category: string }> = [];++ const checkDeps = async (entries: [string, string][], category: string) => {+ for (const [name, declared] of entries) {+ try {+ const installedPkg = resolve('node_modules', name, 'package.json');+ const raw = await fs.readFile(installedPkg, 'utf-8');+ const installedContent = JSON.parse(raw) as { version?: string };+ const installed = installedContent.version || 'unknown';+ const cleanDeclared = (declared as string).replace(/^[\^~>=<]+/, '');+ if (installed !== cleanDeclared) {+ outdated.push({ name, declared: declared as string, installed, category });+ }+ } catch {+ outdated.push({ name, declared: declared as string, installed: 'not installed', category });+ }+ }+ };++ await checkDeps(deps, 'prod');+ await checkDeps(devDeps, 'dev');++ if (outdated.length === 0) {+ output.printSuccess('All dependencies match declared versions');+ } else {+ output.printTable({+ columns: [+ { key: 'name', header: 'Package', width: 30 },+ { key: 'declared', header: 'Declared', width: 14 },+ { key: 'installed', header: 'Installed', width: 14 },+ { key: 'category', header: 'Type', width: 6 },+ ],+ data: outdated.slice(0, 30),+ });+ if (outdated.length > 30) {+ output.writeln(output.dim(` ... and ${outdated.length - 30} more`));+ }+ }+ }++ if (checkSecurity) {+ output.writeln();+ output.writeln(output.bold('Security Audit'));+ output.writeln(output.dim('-'.repeat(60)));++ try {+ const auditRaw = execSync('npm audit --json 2>/dev/null', { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });+ const audit = JSON.parse(auditRaw);+ const vulns = audit.metadata?.vulnerabilities || audit.vulnerabilities || {};+ const info = vulns.info || 0;+ const low = vulns.low || 0;+ const moderate = vulns.moderate || 0;+ const high = vulns.high || 0;+ const critical = vulns.critical || 0;+ const totalVulns = info + low + moderate + high + critical;++ if (totalVulns === 0) {+ output.printSuccess('No known vulnerabilities found');+ } else {+ output.printTable({+ columns: [+ { key: 'severity', header: 'Severity', width: 12 },+ { key: 'count', header: 'Count', width: 8, align: 'right' as const },+ ],+ data: [+ ...(critical > 0 ? [{ severity: 'Critical', count: critical }] : []),+ ...(high > 0 ? [{ severity: 'High', count: high }] : []),+ ...(moderate > 0 ? [{ severity: 'Moderate', count: moderate }] : []),+ ...(low > 0 ? [{ severity: 'Low', count: low }] : []),+ ...(info > 0 ? [{ severity: 'Info', count: info }] : []),+ { severity: 'Total', count: totalVulns },+ ],+ });+ if (critical > 0 || high > 0) {+ output.printWarning(`${critical + high} high/critical vulnerabilities found. Run 'npm audit' for details.`);+ }+ }+ } catch {+ output.printWarning('npm audit failed. Ensure npm is available and node_modules is installed.');+ }+ }++ return { success: true };+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ output.printError(`Dependency analysis failed: ${message}`);+ return { success: false, exitCode: 1 };+ }},};diff --git a/v3/@claude-flow/cli/src/mcp-tools/daa-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/daa-tools.tsindex e07e94896a..332c513344 100644--- a/v3/@claude-flow/cli/src/mcp-tools/daa-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/daa-tools.ts@@ -173,6 +173,19 @@ export const daaTools: MCPTool[] = [agent.status = 'active';saveDAAStore(store);+ // Store adaptation feedback in AgentDB for pattern learning (backward compat: JSON store above)+ let _storedIn: 'agentdb' | 'json-store' = 'json-store';+ try {+ const bridge = await import('../memory/memory-bridge.js');+ await bridge.bridgeRecordFeedback({+ taskId: `adapt-${agentId}-${agent.metrics.adaptations}`,+ success: performanceScore >= 0.5,+ quality: performanceScore,+ agent: agentId,+ });+ _storedIn = 'agentdb';+ } catch { /* AgentDB not available */ }+return {success: true,agentId,@@ -183,6 +196,7 @@ export const daaTools: MCPTool[] = [newSuccessRate: agent.metrics.successRate,},status: agent.status,+ _storedIn,};},},@@ -286,13 +300,29 @@ export const daaTools: MCPTool[] = [const domain = (input.knowledgeDomain as string) || 'general';const knowledgeId = `knowledge-${Date.now()}`;- store.knowledge[knowledgeId] = {+ const knowledgeEntry = {domain,content: input.knowledgeContent || {},sharedBy: sourceId,+ targetAgents: targetIds,timestamp: new Date().toISOString(),};+ // Primary: store in AgentDB for vector-searchable knowledge+ let _storedIn: 'agentdb' | 'json-store' = 'json-store';+ try {+ const bridge = await import('../memory/memory-bridge.js');+ await bridge.bridgeStoreEntry({+ key: knowledgeId,+ value: JSON.stringify(knowledgeEntry),+ namespace: 'daa-knowledge',+ tags: [domain, sourceId, ...targetIds],+ });+ _storedIn = 'agentdb';+ } catch { /* AgentDB not available */ }++ // Backward compat: always persist in JSON store+ store.knowledge[knowledgeId] = knowledgeEntry;saveDAAStore(store);return {@@ -301,8 +331,11 @@ export const daaTools: MCPTool[] = [sourceAgent: sourceId,targetAgents: targetIds,domain,- sharedAt: new Date().toISOString(),- _note: 'Knowledge stored in shared registry. Target agents can retrieve via daa_learning_status. No cross-agent memory transfer occurs.',+ sharedAt: knowledgeEntry.timestamp,+ _storedIn,+ _note: _storedIn === 'agentdb'+ ? 'Knowledge stored in AgentDB (vector-searchable) and JSON store. Target agents can retrieve via daa_learning_status or memory search.'+ : 'Knowledge stored in shared JSON registry. Target agents can retrieve via daa_learning_status. No cross-agent memory transfer occurs.',};},},diff --git a/v3/@claude-flow/cli/src/mcp-tools/system-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/system-tools.tsindex ffbc37ff3b..75bbb5b619 100644--- a/v3/@claude-flow/cli/src/mcp-tools/system-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/system-tools.ts@@ -157,6 +157,73 @@ export const systemTools: MCPTool[] = [const totalMem = os.totalmem();const freeMem = os.freemem();+ // Read real agent/task counts — try AgentDB first, fallback to JSON stores+ let agentCounts = { active: 0, total: 0 };+ let taskCounts = { pending: 0, completed: 0, failed: 0 };+ let _metricsSource: 'agentdb' | 'json-store' | 'none' = 'none';++ // Primary: AgentDB (sql.js + HNSW)+ try {+ const bridge = await import('../memory/memory-bridge.js');+ const agentResults = await bridge.bridgeListEntries({ namespace: 'agents', limit: 10000 }) as { entries?: Array<{ metadata?: string; value?: string }> } | null;+ const agentEntries = agentResults?.entries;+ if (agentEntries && agentEntries.length > 0) {+ let active = 0;+ for (const a of agentEntries) {+ try {+ const meta = a.metadata ? JSON.parse(a.metadata) : (a.value ? JSON.parse(a.value) : {});+ if (meta.status === 'active' || meta.status === 'running') active++;+ } catch { /* skip unparseable */ }+ }+ agentCounts = { total: agentEntries.length, active };+ _metricsSource = 'agentdb';+ }+ const taskResults = await bridge.bridgeListEntries({ namespace: 'tasks', limit: 10000 }) as { entries?: Array<{ metadata?: string; value?: string }> } | null;+ const taskEntries = taskResults?.entries;+ if (taskEntries && taskEntries.length > 0) {+ let pending = 0, completed = 0, failed = 0;+ for (const t of taskEntries) {+ try {+ const meta = t.metadata ? JSON.parse(t.metadata) : (t.value ? JSON.parse(t.value) : {});+ if (meta.status === 'pending' || meta.status === 'assigned') pending++;+ else if (meta.status === 'completed') completed++;+ else if (meta.status === 'failed') failed++;+ } catch { /* skip */ }+ }+ taskCounts = { pending, completed, failed };+ _metricsSource = 'agentdb';+ }+ } catch { /* AgentDB not available, try JSON fallback */ }++ // Fallback: JSON store files (backward compatibility)+ if (_metricsSource === 'none') {+ try {+ const agentStorePath = join(getProjectCwd(), STORAGE_DIR, 'agents', 'store.json');+ if (existsSync(agentStorePath)) {+ const agentStore = JSON.parse(readFileSync(agentStorePath, 'utf-8'));+ const agents = Object.values(agentStore.agents || {}) as Array<{ status: string }>;+ agentCounts = {+ total: agents.length,+ active: agents.filter(a => a.status === 'active' || a.status === 'running').length,+ };+ _metricsSource = 'json-store';+ }+ } catch { /* agent store not available */ }+ try {+ const taskStorePath = join(getProjectCwd(), STORAGE_DIR, 'tasks', 'store.json');+ if (existsSync(taskStorePath)) {+ const taskStore = JSON.parse(readFileSync(taskStorePath, 'utf-8'));+ const tasks = Object.values(taskStore.tasks || {}) as Array<{ status: string }>;+ taskCounts = {+ pending: tasks.filter(t => t.status === 'pending' || t.status === 'assigned').length,+ completed: tasks.filter(t => t.status === 'completed').length,+ failed: tasks.filter(t => t.status === 'failed').length,+ };+ _metricsSource = 'json-store';+ }+ } catch { /* task store not available */ }+ }+const currentMetrics: SystemMetrics = {...store,cpu: loadAvg[0] * 100 / cpus.length, // Real CPU load percentage@@ -164,6 +231,9 @@ export const systemTools: MCPTool[] = [used: Math.round((totalMem - freeMem) / 1024 / 1024), // Real MB usedtotal: Math.round(totalMem / 1024 / 1024), // Real total MB},+ agents: agentCounts,+ tasks: taskCounts,+ // requests: no MCP request tracking infrastructure yet — stays at stored valueuptime: Date.now() - new Date(store.startTime).getTime(),lastCheck: new Date().toISOString(),};@@ -173,7 +243,8 @@ export const systemTools: MCPTool[] = [if (category === 'all') {return {...currentMetrics,- _real: true, // Flag indicating real metrics+ _real: true,+ _metricsSource,heap: {used: Math.round(memUsage.heapUsed / 1024 / 1024),total: Math.round(memUsage.heapTotal / 1024 / 1024),diff --git a/v3/__tests__/honesty/tool-honesty.test.ts b/v3/__tests__/honesty/tool-honesty.test.tsnew file mode 100644index 0000000000..e3929bbfa2--- /dev/null+++ b/v3/__tests__/honesty/tool-honesty.test.ts@@ -0,0 +1,400 @@+/**+ * Tool Honesty Integration Tests (v3.5.56-57)+ *+ * These tests verify that MCP tool handlers do not fabricate data.+ * They primarily do source-level checks (reading .ts files and asserting+ * dishonest patterns are absent) plus lightweight runtime checks where feasible.+ */+import { describe, it, expect } from 'vitest';+import { readFileSync, existsSync } from 'node:fs';+import { join } from 'node:path';++// Paths relative to this test file+const CLI_SRC = join(__dirname, '../../@claude-flow/cli/src');+const MCP_TOOLS = join(CLI_SRC, 'mcp-tools');++// Helper: read a source file and return its contents+function readSource(relativePath: string): string {+ const fullPath = join(MCP_TOOLS, relativePath);+ if (!existsSync(fullPath)) {+ throw new Error(`Source file not found: ${fullPath}`);+ }+ return readFileSync(fullPath, 'utf-8');+}++// Helper: find all Math.random() usages and classify them+function findMathRandomUsages(source: string): { line: number; text: string; isIdGeneration: boolean }[] {+ const lines = source.split('\n');+ const usages: { line: number; text: string; isIdGeneration: boolean }[] = [];++ for (let i = 0; i < lines.length; i++) {+ const line = lines[i];+ if (line.includes('Math.random()')) {+ // ID generation patterns use .toString(36) to create random IDs+ const isIdGeneration = line.includes('.toString(36)');+ usages.push({ line: i + 1, text: line.trim(), isIdGeneration });+ }+ }++ return usages;+}++describe('Tool Honesty (v3.5.56-57)', () => {+ // =========================================================================+ // 1. No Math.random() in metrics/confidence/accuracy contexts+ // =========================================================================+ describe('No Math.random() in metrics', () => {+ const filesToCheck = [+ 'performance-tools.ts',+ 'system-tools.ts',+ 'hooks-tools.ts',+ 'daa-tools.ts',+ 'neural-tools.ts',+ 'embeddings-tools.ts',+ ];++ it.each(filesToCheck)('should not use Math.random() for metrics in %s', (fileName) => {+ const source = readSource(fileName);+ const usages = findMathRandomUsages(source);+ const metricUsages = usages.filter(u => !u.isIdGeneration);++ // Filter out legitimate uses: performance benchmark array generation+ // (used to actually benchmark sorting/matrix ops, not to fabricate metrics)+ const fabricatedUsages = metricUsages.filter(u => {+ // Real benchmark computation: filling arrays for sorting or matrix multiplication+ if (u.text.includes('new Array') && u.text.includes('.fill(0).map')) return false;+ if (u.text.includes('Array.from') && u.text.includes('Math.random()')) return false;+ return true;+ });++ expect(fabricatedUsages).toEqual([]);+ });++ it('should not use Math.random() for confidence scores anywhere in mcp-tools', () => {+ const allFiles = [+ 'performance-tools.ts', 'system-tools.ts', 'hooks-tools.ts',+ 'daa-tools.ts', 'neural-tools.ts', 'embeddings-tools.ts',+ 'agent-tools.ts', 'coordination-tools.ts', 'memory-tools.ts',+ ];++ for (const fileName of allFiles) {+ const source = readSource(fileName);+ // Check that Math.random() never appears near confidence/accuracy/score assignment+ const lines = source.split('\n');+ for (let i = 0; i < lines.length; i++) {+ const line = lines[i];+ if (line.includes('Math.random()') && !line.includes('.toString(36)')) {+ // This line uses Math.random() for something other than ID generation.+ // Verify it is NOT assigning to a confidence/accuracy/score field.+ const contextWindow = lines.slice(Math.max(0, i - 2), i + 3).join(' ');+ const isFabricatingMetric =+ /(?:confidence|accuracy|score|successRate|similarity)\s*[:=].*Math\.random/.test(contextWindow) ||+ /Math\.random.*(?:confidence|accuracy|score|successRate|similarity)/.test(contextWindow);++ expect(isFabricatingMetric).toBe(false);+ }+ }+ }+ });+ });++ // =========================================================================+ // 2. daa_agent_adapt has no setTimeout (no fake delays)+ // =========================================================================+ describe('No fake delays in daa_agent_adapt', () => {+ it('should not contain setTimeout in daa-tools.ts', () => {+ const source = readSource('daa-tools.ts');+ expect(source).not.toContain('setTimeout');+ });++ it('should handle daa_agent_adapt synchronously (< 50ms conceptual)', () => {+ const source = readSource('daa-tools.ts');+ // Verify the handler does NOT contain any sleep/delay pattern+ expect(source).not.toMatch(/new Promise.*setTimeout/);+ expect(source).not.toMatch(/await.*delay/);+ expect(source).not.toMatch(/await.*sleep/);+ });+ });++ // =========================================================================+ // 3. daa_workflow_execute does not auto-complete steps+ // =========================================================================+ describe('daa_workflow_execute does not auto-complete', () => {+ it('should not set step status to completed in the execute handler', () => {+ const source = readSource('daa-tools.ts');++ // Find the daa_workflow_execute handler section+ const executeStart = source.indexOf("name: 'daa_workflow_execute'");+ expect(executeStart).toBeGreaterThan(-1);++ // Find the next tool definition to bound the handler+ const nextToolStart = source.indexOf("name: 'daa_knowledge_share'");+ expect(nextToolStart).toBeGreaterThan(executeStart);++ const handlerSource = source.slice(executeStart, nextToolStart);++ // The handler should NOT mark steps as 'completed'+ expect(handlerSource).not.toContain("status: 'completed'");+ expect(handlerSource).not.toContain('status: "completed"');++ // The handler should set the workflow status to 'running' (not 'completed')+ expect(handlerSource).toContain("'running'");++ // The handler should include the _note about steps not being auto-executed+ expect(handlerSource).toContain('Steps are tracked but not auto-executed');+ });++ it('should preserve step status as pending after execute', () => {+ const source = readSource('daa-tools.ts');++ // Verify the workflow_create handler sets steps to 'pending'+ const createStart = source.indexOf("name: 'daa_workflow_create'");+ const executeStart = source.indexOf("name: 'daa_workflow_execute'");+ const createSource = source.slice(createStart, executeStart);++ expect(createSource).toContain("status: 'pending'");+ });+ });++ // =========================================================================+ // 4. system_reset uses real OS values+ // =========================================================================+ describe('system_reset uses real OS values', () => {+ it('should use os.loadavg() for cpu in the reset handler', () => {+ const source = readSource('system-tools.ts');++ // Find the system_reset handler section+ const resetStart = source.indexOf("name: 'system_reset'");+ expect(resetStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(resetStart, resetStart + 1000);++ // Should use real os functions, not hardcoded values+ expect(handlerSection).toContain('os.loadavg()');+ expect(handlerSection).toContain('os.totalmem()');+ expect(handlerSection).toContain('os.freemem()');++ // Should NOT contain hardcoded fake values+ expect(handlerSection).not.toMatch(/cpu:\s*25\b/);+ expect(handlerSection).not.toMatch(/total:\s*1024\b/);+ });++ it('should use os.totalmem() and os.freemem() for memory values', () => {+ const source = readSource('system-tools.ts');+ const resetStart = source.indexOf("name: 'system_reset'");+ const handlerSection = source.slice(resetStart, resetStart + 1000);++ // Verify memory is computed from os module+ expect(handlerSection).toContain('os.totalmem()');+ expect(handlerSection).toContain('os.freemem()');+ expect(handlerSection).toContain('Math.round');+ });++ it('should import the os module', () => {+ const source = readSource('system-tools.ts');+ expect(source).toMatch(/import.*os.*from.*['"]node:os['"]/);+ });+ });++ // =========================================================================+ // 5. hooks_intelligence_attention fallback returns empty results+ // =========================================================================+ describe('hooks_intelligence_attention fallback', () => {+ it('should return empty results array when no backend is available', () => {+ const source = readSource('hooks-tools.ts');++ // Find the hooks_intelligence_attention handler+ const attentionStart = source.indexOf("name: 'hooks_intelligence_attention'");+ expect(attentionStart).toBeGreaterThan(-1);++ // Get the handler section (up to the next major section marker)+ const handlerSection = source.slice(attentionStart, attentionStart + 4000);++ // When no real implementation worked, results should be empty+ expect(handlerSection).toContain("implementation = 'none'");++ // Should NOT fabricate fake weights with Math.exp+ expect(handlerSection).not.toContain('Math.exp');++ // Should NOT generate fake results when implementation is none+ // Verify the fallback logic: "If no real implementation worked, return empty"+ expect(handlerSection).toContain('results.length === 0');+ });++ it('should mark _stub as true when no backend is available', () => {+ const source = readSource('hooks-tools.ts');+ const attentionStart = source.indexOf("name: 'hooks_intelligence_attention'");+ const handlerSection = source.slice(attentionStart, attentionStart + 4000);++ // The stats object should indicate stub status when implementation is 'none'+ expect(handlerSection).toContain("_stub: implementation === 'none'");+ });++ it('should not generate fake speedup claims when no backend is available', () => {+ const source = readSource('hooks-tools.ts');+ const attentionStart = source.indexOf("name: 'hooks_intelligence_attention'");+ const handlerSection = source.slice(attentionStart, attentionStart + 4000);++ // Speedup should be null when implementation is not real+ expect(handlerSection).toMatch(/speedup:.*implementation\.startsWith\('real-'\)/);+ });+ });++ // =========================================================================+ // 6. No "Simulated" text in embeddings+ // =========================================================================+ describe('No "Simulated" text in embeddings', () => {+ it('should not contain "Simulated" in embeddings-tools.ts', () => {+ const source = readSource('embeddings-tools.ts');+ expect(source).not.toContain('Simulated');+ });++ it('should not contain "simulated" (case-insensitive) in embeddings-tools.ts', () => {+ const source = readSource('embeddings-tools.ts');+ // Allow "Simulated" only in comments explaining what was removed+ const lines = source.split('\n');+ for (let i = 0; i < lines.length; i++) {+ const line = lines[i];+ if (/simulated/i.test(line)) {+ // It is acceptable in a comment explaining the fix+ const isComment = line.trim().startsWith('//') || line.trim().startsWith('*');+ if (!isComment) {+ expect(line).not.toMatch(/simulated/i);+ }+ }+ }+ });++ it('should return honest empty results when no embeddings are indexed', () => {+ const source = readSource('embeddings-tools.ts');+ // Check that the fallback message is honest+ expect(source).toContain('No embeddings indexed yet');+ });+ });++ // =========================================================================+ // 7. Benchmark fallback returns 0 not 0.5 for searchTime+ // =========================================================================+ describe('Benchmark fallback returns 0 for searchTimeMs', () => {+ it('should return searchTimeMs: 0 in pattern-search fallback', () => {+ const source = readSource('hooks-tools.ts');++ // Find the hooks_intelligence_pattern-search handler+ const patternSearchStart = source.indexOf("name: 'hooks_intelligence_pattern-search'");+ expect(patternSearchStart).toBeGreaterThan(-1);++ // Get the handler up to the next tool definition+ const remaining = source.slice(patternSearchStart);+ const nextToolMatch = remaining.indexOf("\nname: '", 10);+ const endIdx = nextToolMatch > 0 ? nextToolMatch : remaining.indexOf('\n};', 100);+ const handlerSection = remaining.slice(0, endIdx > 0 ? endIdx : 2000);++ // The fallback searchTimeMs should be 0, never 0.5+ expect(handlerSection).not.toContain('searchTimeMs: 0.5');++ // All fallback paths should use 0+ const searchTimeMatches = handlerSection.match(/searchTimeMs:\s*[\d.]+/g) || [];+ for (const match of searchTimeMatches) {+ const value = parseFloat(match.replace('searchTimeMs:', '').trim());+ // Values of 0 are honest fallbacks; real values from searchResult.searchTime are fine too+ // But fabricated values like 0.5 are not acceptable+ if (!match.includes('searchResult.searchTime')) {+ expect(value).toBe(0);+ }+ }+ });++ it('should return searchTimeMs: 0 in error and unavailable paths', () => {+ const source = readSource('hooks-tools.ts');++ // Check that error/unavailable fallback paths use searchTimeMs: 0+ const errorFallbacks = source.match(/backend:\s*'(?:error|unavailable)'[\s\S]*?searchTimeMs:\s*([\d.]+)/g) || [];+ // Also check the reverse: searchTimeMs before backend+ const fallbackBlocks = source.match(/searchTimeMs:\s*([\d.]+)[\s\S]*?backend:\s*'(?:error|unavailable)'/g) || [];++ // Combine and verify all use 0+ const all = [...errorFallbacks, ...fallbackBlocks];+ for (const block of all) {+ const timeMatch = block.match(/searchTimeMs:\s*([\d.]+)/);+ if (timeMatch) {+ expect(parseFloat(timeMatch[1])).toBe(0);+ }+ }+ });+ });++ // =========================================================================+ // Bonus: Verify performance_report uses real OS values (not fabricated)+ // =========================================================================+ describe('performance_report uses real OS values', () => {+ it('should use process.memoryUsage() and os module for real metrics', () => {+ const source = readSource('performance-tools.ts');++ const reportStart = source.indexOf("name: 'performance_report'");+ expect(reportStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(reportStart, reportStart + 2000);++ expect(handlerSection).toContain('process.memoryUsage()');+ expect(handlerSection).toContain('process.cpuUsage()');+ expect(handlerSection).toContain('os.loadavg()');+ expect(handlerSection).toContain('os.cpus()');+ expect(handlerSection).toContain('os.totalmem()');+ expect(handlerSection).toContain('os.freemem()');+ });++ it('should mark report output with _real: true', () => {+ const source = readSource('performance-tools.ts');+ expect(source).toContain('_real: true');+ });++ it('should use real timing in performance_benchmark', () => {+ const source = readSource('performance-tools.ts');++ const benchmarkStart = source.indexOf("name: 'performance_benchmark'");+ expect(benchmarkStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(benchmarkStart, benchmarkStart + 3000);++ // Should use performance.now() for real timing+ expect(handlerSection).toContain('performance.now()');+ // Should have a warmup phase+ expect(handlerSection).toMatch(/[Ww]armup/);+ });+ });++ // =========================================================================+ // Bonus: Stubs are marked honestly with _stub: true+ // =========================================================================+ describe('Stubs are honestly marked', () => {+ it('should mark performance_profile as _stub: true', () => {+ const source = readSource('performance-tools.ts');+ const profileStart = source.indexOf("name: 'performance_profile'");+ expect(profileStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(profileStart, profileStart + 1000);+ expect(handlerSection).toContain('_stub: true');+ expect(handlerSection).toContain('not yet implemented');+ });++ it('should mark performance_optimize as _stub: true', () => {+ const source = readSource('performance-tools.ts');+ const optimizeStart = source.indexOf("name: 'performance_optimize'");+ expect(optimizeStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(optimizeStart, optimizeStart + 1000);+ expect(handlerSection).toContain('_stub: true');+ expect(handlerSection).toContain('not yet implemented');+ });++ it('should mark performance_bottleneck as _stub: true', () => {+ const source = readSource('performance-tools.ts');+ const bottleneckStart = source.indexOf("name: 'performance_bottleneck'");+ expect(bottleneckStart).toBeGreaterThan(-1);++ const handlerSection = source.slice(bottleneckStart, bottleneckStart + 1000);+ expect(handlerSection).toContain('_stub: true');+ expect(handlerSection).toContain('not yet implemented');+ });+ });+});diff --git a/v3/implementation/adrs/ADR-073-stub-tool-honesty-real-predictions.md b/v3/implementation/adrs/ADR-073-stub-tool-honesty-real-predictions.mdindex aab5e245a9..eeefa8c0ec 100644--- a/v3/implementation/adrs/ADR-073-stub-tool-honesty-real-predictions.md+++ b/v3/implementation/adrs/ADR-073-stub-tool-honesty-real-predictions.md@@ -2,8 +2,8 @@# ADR-073: Stub Tool Honesty & Real Predictions**Status**: Accepted-**Date**: 2026-04-06 (updated v3.5.55)-**Context**: Issue #1514 (independent audit), Issues #1516, #1518, #1521, #1526+**Date**: 2026-04-06 (updated v3.5.57)+**Context**: Issue #1514 (independent audit), Issues #1516, #1518, #1521, #1526, #1530, #1531, #1538## Decision@@ -57,14 +57,55 @@ All embedding model defaults now use `Xenova/` prefix (e.g., `Xenova/all-MiniLM-- Scope `bootstrapFromMemoryFiles()` to current project only (was scanning all 51+ project dirs)- Fix `tool_input` snake_case mismatch in hook-handler+### 7. Deep audit Math.random() removal (v3.5.56)++- `agent_health` aggregate: replaced Math.random() CPU/memory/latency with `null` + `_note`+- `system_health`: replaced hardcoded "healthy" + random latency with real `fs.existsSync()` checks timed with `performance.now()`+- `system_status`: replaced hardcoded component health (0.95, 0.90, 1.0) with `status: 'unknown'`+- `coordination_metrics`: replaced Math.random() with null + real sync counts+- `github-tools`: all 5 tools return `_stub: true`, Math.random() removed+- `neural_compress` and `neural_optimize`: return `_stub: true`+- `performance bottleneck/profile/optimize`: return `_stub: true`, profile no longer sleeps 100ms+- `hooks_metrics`: reads real counts from memory store+- `hooks_pretrain`: returns `_stub: true`+- `hooks_intelligence-reset`: actually deletes data files now+- `hooks_session-end`: reads real task/file/agent counts+- `hooks_explain`: reads real success rate from routing-outcomes.json+- `hooks_transfer`: returns failure instead of substituting demo data+- `workflow_execute/resume`: steps stay pending, don't auto-complete+- `task-tools`: fixed path mismatch `agents.json` → `agents/store.json`+- `session_restore`: syncs to sql.js database after writing legacy JSON+- `claims_rebalance`: executes moves when `dryRun=false`+- `config get`: calls `configManager.get()` instead of hardcoded map+- `process monitor`: uses real `os.loadavg()`, `process.memoryUsage()`+- `process logs`: reads actual log files+- `status`: flashAttention/searchSpeed → `'not measured'`+- `token-optimizer`: removed double-increment of `editsOptimized`++### 8. Remaining stub cleanup (v3.5.57)++- `daa_agent_adapt`: removed fake 50ms setTimeout delay+- `daa_workflow_execute`: steps stay pending instead of auto-completing+- `daa_cognitive_pattern`: replaced hardcoded analysis with real agent metrics+- `daa_knowledge_share`: added `_note` clarifying no cross-agent transfer+- `hooks_intelligence_attention`: removed `Math.exp(-i*0.5)` fake sigmoid weights, returns empty with `_stub: true` when no backend+- `system_reset` + `loadMetrics()`: use real `os.loadavg()/os.totalmem()` instead of hardcoded cpu:25, memory:256/1024+- `benchmark` CLI: honest zero fallback instead of hardcoded `searchTime: 0.5`+- `embeddings` CLI: "Skipped" instead of "Simulated"+- `providers` CLI: clarifying comment on static catalog+- `system_metrics`: wired up real agent/task counters from store files+## Consequences- Token optimizer reports honest numbers (will show 0 savings when agentic-flow is not installed)- `neural_predict` returns real cosine similarity results when patterns stored, empty array when not- `neural_train` stores real embeddings, no simulated accuracy- Zero instances of `Math.random()` for confidence/accuracy/metrics in shipped code+- Zero instances of `setTimeout()` for fake delays in shipped code+- All `_stub: true` markers on tools that don't perform real work- Users can distinguish real ML vs hash-based embedding via `_realEmbedding` flag- `hooks explain` matchScore uses real keyword ratio instead of random+- system_metrics returns real agent/task counts from persistent stores## Tools Status (Post-Fix)@@ -76,6 +117,8 @@ All embedding model defaults now use `Xenova/` prefix (e.g., `Xenova/all-MiniLM-| Neural predict | Real (with patterns) | Cosine similarity search; empty array when no patterns || Neural train | Real | Embeds training data, stores as searchable patterns || Token optimizer | Honest metrics | No fabricated numbers |-| Agent spawn/task | Coordination stubs | State tracking, no LLM execution |+| Agent spawn/task | Real state tracking | Store persistence via agents/store.json |+| DAA tools | Honest | Local state, no fake delays or auto-completion |+| System metrics | Real | CPU/memory from os module, agents/tasks from stores || WASM agents | Stub | Echo-based, no WASM runtime || Hive-mind | Partial | Vote counting works, BFT not differentiated |