diff --git a/v3/@claude-flow/cli/__tests__/sona-neighbour-reinforcement-4134.test.ts b/v3/@claude-flow/cli/__tests__/sona-neighbour-reinforcement-4134.test.ts new file mode 100644 index 0000000000..71b6348d25 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/sona-neighbour-reinforcement-4134.test.ts @@ -0,0 +1,75 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { SONAOptimizer, SONA_ROUTING_GATE, routingGateStatus } from '../src/memory/sona-optimizer.js'; + +const TASK_A = 'Verify a ruflo plugin end-to-end: read docs+tool source, probe live MCP behaviour, root-cause anomalies in node_modules, file upstream issues'; +const TASK_B = 'Verify the ruflo-intelligence plugin end-to-end: read docs+tool source, probe live MCP behaviour, root-cause anomalies in node_modules, file upstream issues'; +const TASK_C = 'Verify the ruflo-ruvector plugin end-to-end: read docs and tool source, probe live MCP behaviour, root-cause anomalies, file upstream issues'; + +let dir: string; +let cwd: string; +beforeEach(() => { cwd = process.cwd(); dir = mkdtempSync(join(tmpdir(), 'sona-4134-')); process.chdir(dir); }); +afterEach(() => { process.chdir(cwd); rmSync(dir, { recursive: true, force: true }); }); + +const outcome = (task: string, success = true, agent = 'researcher') => ({ trajectoryId: 't', task, agent, success, steps: [], duration: 1 }); + +describe('SONA near-identical tasks reinforce one pattern (#4134)', () => { + it('the two near-identical tasks from the report share a pattern instead of creating two', () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + const a = sona.processTrajectoryOutcome(outcome(TASK_A)); + const b = sona.processTrajectoryOutcome(outcome(TASK_B)); + expect(b.patternKey).toBe(a.patternKey); + expect(b.reinforcedNeighbour).toBe(true); + expect(b.confidence).toBeGreaterThan(a.confidence); // 0.55 -> 0.595, not a new pattern at 0.55 + expect(sona.getStats().totalPatterns).toBe(1); + }); + + it('routing eventually fires for a near-identical task: before the fix it never did (#4134)', async () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + let fired = 0; + for (let i = 1; i <= 14 && !fired; i++) { + sona.processTrajectoryOutcome(outcome(i % 2 ? TASK_A : TASK_B)); + const s = await sona.getRoutingSuggestion(TASK_C); + if (s.agent === 'researcher' && s.confidence >= SONA_ROUTING_GATE) fired = i; + } + expect(fired, 'learned routing never fired within 14 trajectories').toBeGreaterThan(0); + expect(sona.getStats().totalPatterns).toBe(1); + }); + + it('routing fires for the identical task once confidence reaches the gate (3 successes)', async () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + for (let i = 0; i < 3; i++) sona.processTrajectoryOutcome(outcome(TASK_A)); + const s = await sona.getRoutingSuggestion(TASK_A); + expect(s.agent).toBe('researcher'); + expect(s.confidence).toBeGreaterThanOrEqual(SONA_ROUTING_GATE); + }); + + it('does not merge different agents or genuinely different tasks', () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + const a = sona.processTrajectoryOutcome(outcome(TASK_A, true, 'researcher')); + const other = sona.processTrajectoryOutcome(outcome(TASK_A, true, 'coder')); + expect(other.patternKey).not.toBe(a.patternKey); + const unrelated = sona.processTrajectoryOutcome(outcome('Refactor the billing invoice rounding logic and add currency tests', true, 'researcher')); + expect(unrelated.patternKey).not.toBe(a.patternKey); + expect(unrelated.reinforcedNeighbour).toBe(false); + }); + + it('a failure reduces the shared pattern instead of spawning a fresh 0.5 one', () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + sona.processTrajectoryOutcome(outcome(TASK_A)); + const first = sona.processTrajectoryOutcome(outcome(TASK_B)); + const failed = sona.processTrajectoryOutcome(outcome(TASK_B, false)); + expect(failed.patternKey).toBe(first.patternKey); + expect(failed.confidence).toBeLessThan(first.confidence); + }); + + it('reports how far the pattern is from the routing gate', () => { + const sona = new SONAOptimizer({ persistencePath: '.swarm/sona-test.json' }); + const r = sona.processTrajectoryOutcome(outcome(TASK_A)); + expect(r.routingGate).toEqual({ threshold: SONA_ROUTING_GATE, confidence: r.confidence, reached: false, successesNeeded: 2 }); + expect(routingGateStatus(0.7)).toMatchObject({ reached: true, successesNeeded: 0 }); + expect(routingGateStatus(0.5).successesNeeded).toBe(3); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts index c5e03b27f6..07b7c63b47 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts @@ -3422,7 +3422,7 @@ export const hooksTrajectoryEnd: MCPTool = { } catch { /* prototype path — never blocks trajectory finalization */ } // SONA Learning - process trajectory outcome for routing optimization - let sonaResult: { learned: boolean; patternKey: string; confidence: number } = { + let sonaResult: { learned: boolean; patternKey: string; confidence: number; reinforcedNeighbour?: boolean; routingGate?: { threshold: number; confidence: number; reached: boolean; successesNeeded: number } } = { learned: false, patternKey: '', confidence: 0 }; let ewcResult: { consolidated: boolean; penalty: number } = { @@ -3450,6 +3450,8 @@ export const hooksTrajectoryEnd: MCPTool = { learned: result.learned, patternKey: result.patternKey, confidence: result.confidence, + reinforcedNeighbour: result.reinforcedNeighbour, + routingGate: result.routingGate, }; } catch { // SONA learning failed, continue without it @@ -3618,6 +3620,8 @@ export const hooksTrajectoryEnd: MCPTool = { sonaUpdate: sonaResult.learned, sonaPatternKey: sonaResult.patternKey || undefined, sonaConfidence: sonaResult.confidence || undefined, + sonaReinforcedNeighbour: sonaResult.reinforcedNeighbour || undefined, + sonaRoutingGate: sonaResult.routingGate, ewcConsolidation: ewcResult.consolidated, ewcPenalty: ewcResult.penalty || undefined, patternsExtracted, diff --git a/v3/@claude-flow/cli/src/memory/sona-optimizer.ts b/v3/@claude-flow/cli/src/memory/sona-optimizer.ts index 255556a6bb..82a1af14fb 100644 --- a/v3/@claude-flow/cli/src/memory/sona-optimizer.ts +++ b/v3/@claude-flow/cli/src/memory/sona-optimizer.ts @@ -129,6 +129,10 @@ const CONFIDENCE_INCREMENT = 0.1; const CONFIDENCE_DECREMENT = 0.15; const DECAY_RATE = 0.01; // Per day const MAX_PATTERNS = 1000; +/** #4134: a trajectory reinforces an existing pattern of the same agent whose keyword set overlaps at least this much (Jaccard). */ +const NEIGHBOUR_MIN_JACCARD = 0.6; +/** The routing gate in getRoutingSuggestion: confidence x matchRatio must reach this for a learned pattern to be used. */ +export const SONA_ROUTING_GATE = 0.6; // ============================================================================ // Contrastive Trainer (lazy-loaded from @ruvector/ruvllm) @@ -228,6 +232,19 @@ const KEYWORD_CATEGORIES: Record = { * Learns from past task outcomes to improve future routing decisions. * Integrates with Q-learning router for hybrid routing strategy. */ +/** + * How close a pattern's confidence is to the routing gate. `successesNeeded` + * assumes a perfect keyword match (matchRatio 1) and the 0.1 success increment; + * a partial match needs more. + */ +export function routingGateStatus(confidence: number): { threshold: number; confidence: number; reached: boolean; successesNeeded: number } { + const reached = confidence >= SONA_ROUTING_GATE; + const successesNeeded = reached || confidence >= MAX_CONFIDENCE + ? 0 + : Math.ceil(Math.log((1 - SONA_ROUTING_GATE) / (1 - confidence)) / Math.log(1 - CONFIDENCE_INCREMENT)); + return { threshold: SONA_ROUTING_GATE, confidence, reached, successesNeeded }; +} + export class SONAOptimizer { private patterns: Map = new Map(); private trajectoriesProcessed = 0; @@ -313,6 +330,10 @@ export class SONAOptimizer { patternKey: string; confidence: number; keywordsExtracted: string[]; + /** True when this trajectory reinforced an existing near-identical pattern instead of creating a new one (#4134). */ + reinforcedNeighbour?: boolean; + /** How far the pattern is from influencing routing, so a user can see learning has not kicked in yet (#4134). */ + routingGate?: { threshold: number; confidence: number; reached: boolean; successesNeeded: number }; } { const { task, agent, success } = outcome; @@ -328,7 +349,19 @@ export class SONAOptimizer { } // Create pattern key from sorted keywords - const patternKey = this.createPatternKey(keywords, agent); + let patternKey = this.createPatternKey(keywords, agent); + + // #4134: near-identical tasks ("Verify the X plugin ..." / "Verify the Y plugin ...") + // used to create a separate pattern each at confidence 0.5, so none ever reached + // the routing gate. Reinforce the closest existing pattern of the same agent instead. + let reinforcedNeighbour = false; + if (!this.patterns.has(patternKey)) { + const neighbour = this.findNeighbourPattern(keywords, agent); + if (neighbour) { + patternKey = neighbour; + reinforcedNeighbour = true; + } + } // Get or create pattern let pattern = this.patterns.get(patternKey); @@ -395,6 +428,8 @@ export class SONAOptimizer { patternKey, confidence: pattern.confidence, keywordsExtracted: keywords, + reinforcedNeighbour, + routingGate: routingGateStatus(pattern.confidence), }; } @@ -438,7 +473,7 @@ export class SONAOptimizer { // Priority 2: Try SONA learned pattern matching const sonaResult = this.findBestPatternMatch(keywords); - if (sonaResult && sonaResult.confidence >= 0.6) { + if (sonaResult && sonaResult.confidence >= SONA_ROUTING_GATE) { return { agent: sonaResult.agent, confidence: sonaResult.confidence, @@ -688,6 +723,30 @@ export class SONAOptimizer { return `${agent}:${sortedKeywords.join('+')}`; } + /** + * The key of the existing pattern for `agent` whose keyword set overlaps + * `keywords` the most, if that overlap (Jaccard) reaches NEIGHBOUR_MIN_JACCARD. + * Ties go to the more confident pattern. Linear in the pattern count (<= 1000). + */ + private findNeighbourPattern(keywords: string[], agent: string): string | null { + const wanted = new Set(keywords); + let bestKey: string | null = null; + let bestJaccard = 0; + let bestConfidence = -1; + for (const [key, pattern] of this.patterns) { + if (pattern.agent !== agent) continue; + let shared = 0; + for (const k of pattern.keywords) if (wanted.has(k)) shared++; + const union = wanted.size + pattern.keywords.length - shared; + const jaccard = union === 0 ? 0 : shared / union; + if (jaccard >= NEIGHBOUR_MIN_JACCARD && + (jaccard > bestJaccard || (jaccard === bestJaccard && pattern.confidence > bestConfidence))) { + bestKey = key; bestJaccard = jaccard; bestConfidence = pattern.confidence; + } + } + return bestKey; + } + /** * Find the best matching pattern for given keywords */