π 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
fix: close semantic routing learning loop in hooks-tools (#1311)
Eric Swannadcfe6fad8f3
diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.tsindex 07f35c65b2..d50ea1051e 100644--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts@@ -4,7 +4,7 @@*/import { mkdirSync, writeFileSync, existsSync, readFileSync, statSync } from 'fs';-import { join, resolve } from 'path';+import { dirname, join, resolve } from 'path';import type { MCPTool } from './types.js';// Real vector search functions - lazy loaded to avoid circular imports@@ -158,7 +158,99 @@ function generateSimpleEmbedding(text: string, dimension: number = 384): Float32return embedding;}-// Task patterns used by both native and pure-JS routers+// ββ Runtime routing outcome persistence ββββββββββββββββββββββββββββββ+// Closes the learning loop: post-task records outcomes β route loads them.++const ROUTING_OUTCOMES_PATH = join(resolve('.'), '.claude-flow/routing-outcomes.json');++const ROUTING_STOPWORDS = new Set([+ 'the','a','an','is','are','was','were','be','been','being','have','has','had',+ 'do','does','did','will','would','could','should','may','might','shall','can',+ 'to','of','in','for','on','with','at','by','from','as','into','through','during',+ 'before','after','above','below','between','under','again','further','then','once',+ 'it','its','this','that','these','those','i','me','my','we','our','you','your',+ 'he','she','they','them','and','but','or','nor','not','no','so','if','when','than',+ 'very','just','also','only','both','each','all','any','few','more','most','other',+ 'some','such','same','new','now','here','there','where','how','what','which','who',+]);++interface RoutingOutcome {+ task: string;+ agent: string;+ success: boolean;+ quality: number;+ keywords: string[];+ timestamp: string;+}++function extractKeywords(text: string): string[] {+ if (!text) return [];+ return text.toLowerCase()+ .replace(/[^a-z0-9\s-]/g, ' ')+ .split(/\s+/)+ .filter(w => w.length > 2 && !ROUTING_STOPWORDS.has(w));+}++function loadRoutingOutcomes(): RoutingOutcome[] {+ try {+ if (existsSync(ROUTING_OUTCOMES_PATH)) {+ const data = JSON.parse(readFileSync(ROUTING_OUTCOMES_PATH, 'utf-8'));+ return data.outcomes || [];+ }+ } catch { /* corrupt file, start fresh */ }+ return [];+}++function saveRoutingOutcomes(outcomes: RoutingOutcome[]): void {+ try {+ const dir = dirname(ROUTING_OUTCOMES_PATH);+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });+ // Cap at 500 entries to bound file size+ const capped = outcomes.slice(-500);+ writeFileSync(ROUTING_OUTCOMES_PATH, JSON.stringify({ outcomes: capped }, null, 2));+ } catch { /* non-critical */ }+}++/**+ * Build learned routing patterns from successful task outcomes.+ * Returns patterns in the same shape as TASK_PATTERNS so they can be+ * merged into both the native HNSW and pure-JS semantic routers.+ */+function loadLearnedPatterns(): Record<string, { keywords: string[]; agents: string[] }> {+ const outcomes = loadRoutingOutcomes();+ const byAgent: Record<string, Set<string>> = {};+ for (const o of outcomes) {+ if (!o.success || !o.agent || !o.keywords?.length) continue;+ if (!byAgent[o.agent]) byAgent[o.agent] = new Set();+ for (const kw of o.keywords) byAgent[o.agent].add(kw);+ }+ const patterns: Record<string, { keywords: string[]; agents: string[] }> = {};+ for (const [agent, kwSet] of Object.entries(byAgent)) {+ patterns[`learned-${agent}`] = {+ keywords: [...kwSet].slice(0, 50),+ agents: [agent],+ };+ }+ return patterns;+}++/**+ * Merge static TASK_PATTERNS with runtime-learned patterns.+ * Static patterns take precedence (learned patterns won't overwrite them).+ */+function getMergedTaskPatterns(): Record<string, { keywords: string[]; agents: string[] }> {+ const merged = { ...TASK_PATTERNS };+ const learned = loadLearnedPatterns();+ for (const [key, pattern] of Object.entries(learned)) {+ if (!merged[key]) {+ merged[key] = pattern;+ }+ }+ return merged;+}++// ββ Static task patterns (used by both native and pure-JS routers) βββ+const TASK_PATTERNS: Record<string, { keywords: string[]; agents: string[] }> = {'security-task': {keywords: ['authentication', 'security', 'auth', 'password', 'encryption', 'vulnerability', 'cve', 'audit'],@@ -239,8 +331,8 @@ async function getSemanticRouter() {hnswEfSearch: 100,});- // Initialize with task patterns- for (const [patternName, { keywords }] of Object.entries(TASK_PATTERNS)) {+ // Initialize with static + runtime-learned task patterns+ for (const [patternName, { keywords }] of Object.entries(getMergedTaskPatterns())) {for (const keyword of keywords) {const embedding = generateSimpleEmbedding(keyword);db.insert(`${patternName}:${keyword}`, embedding);@@ -263,7 +355,7 @@ async function getSemanticRouter() {const { SemanticRouter } = await import('../ruvector/semantic-router.js');semanticRouter = new SemanticRouter({ dimension: 384 });- for (const [patternName, { keywords, agents }] of Object.entries(TASK_PATTERNS)) {+ for (const [patternName, { keywords, agents }] of Object.entries(getMergedTaskPatterns())) {const embeddings = keywords.map(kw => generateSimpleEmbedding(kw));semanticRouter.addIntentWithEmbeddings(patternName, embeddings, { agents, keywords });@@ -525,12 +617,35 @@ function suggestAgentsForFile(filePath: string): string[] {function suggestAgentsForTask(task: string): { agents: string[]; confidence: number } {const taskLower = task.toLowerCase();+ // Check static keyword patterns firstfor (const [pattern, result] of Object.entries(KEYWORD_PATTERNS)) {if (taskLower.includes(pattern)) {return result;}}+ // Check runtime-learned patterns from successful task outcomes+ const taskKeywords = extractKeywords(task);+ if (taskKeywords.length > 0) {+ const outcomes = loadRoutingOutcomes();+ let bestAgent = '';+ let bestOverlap = 0;++ for (const outcome of outcomes) {+ if (!outcome.success || !outcome.agent || !outcome.keywords?.length) continue;+ const overlap = taskKeywords.filter(kw => outcome.keywords.includes(kw)).length;+ if (overlap > bestOverlap) {+ bestOverlap = overlap;+ bestAgent = outcome.agent;+ }+ }++ // Require at least 2 keyword overlap to prevent false positives+ if (bestAgent && bestOverlap >= 2) {+ return { agents: [bestAgent], confidence: Math.min(0.6 + bestOverlap * 0.05, 0.85) };+ }+ }+// Default fallbackreturn { agents: ['coder', 'researcher', 'tester'], confidence: 0.7 };}@@ -807,13 +922,16 @@ export const hooksRoute: MCPTool = {backendInfo = 'native VectorDb (HNSW)';// Convert results to semantic format+ const mergedPatterns = getMergedTaskPatterns();semanticResult = results.map((r: { id: string; score: number }) => {const [patternName] = r.id.split(':');- const pattern = TASK_PATTERNS[patternName];+ const pattern = mergedPatterns[patternName];return {intent: patternName,score: 1 - r.score, // Native uses distance (lower is better), convert to similarity- metadata: { agents: pattern?.agents || ['coder'] },+ metadata: {+ agents: pattern?.agents || (patternName.startsWith('learned-') ? [patternName.slice(8)] : ['coder']),+ },};});} catch {@@ -1084,6 +1202,8 @@ export const hooksPostTask: MCPTool = {success: { type: 'boolean', description: 'Whether task was successful' },agent: { type: 'string', description: 'Agent that completed the task' },quality: { type: 'number', description: 'Quality score (0-1)' },+ task: { type: 'string', description: 'Task description text (used for learning keyword extraction)' },+ storeDecisions: { type: 'boolean', description: 'Also store routing decision in memory DB' },},required: ['taskId'],},@@ -1123,6 +1243,41 @@ export const hooksPostTask: MCPTool = {// Non-fatal}+ // Persist routing outcome for runtime learning (file-based, always reliable)+ const taskText = (params.task as string) || '';+ const outcomeKeywords = extractKeywords(taskText);+ let outcomePersisted = false;+ if (taskText && agent && agent.length <= 100 && /^[a-zA-Z0-9_-]+$/.test(agent)) {+ try {+ const outcomes = loadRoutingOutcomes();+ outcomes.push({+ task: taskText,+ agent,+ success,+ quality,+ keywords: outcomeKeywords,+ timestamp: new Date().toISOString(),+ });+ saveRoutingOutcomes(outcomes);+ outcomePersisted = true;+ } catch { /* non-critical */ }+ }++ // Optionally store in memory DB for cross-session vector retrieval+ if (params.storeDecisions && taskText && agent) {+ try {+ const storeFn = await getRealStoreFunction();+ if (storeFn) {+ await storeFn({+ key: `routing-decision:${taskId}`,+ namespace: 'patterns',+ value: JSON.stringify({ task: taskText, agent, success, quality, keywords: outcomeKeywords }),+ tags: ['routing-decision'],+ });+ }+ } catch { /* non-critical */ }+ }+const duration = Date.now() - startTime;return {@@ -1134,6 +1289,7 @@ export const hooksPostTask: MCPTool = {newPatterns: success ? 1 : 0,trajectoryId: `traj-${Date.now()}`,controller: feedbackResult?.controller || 'none',+ outcomePersisted,},quality,feedback: feedbackResult ? {