From a451bb6b8905477be5caa72bca0db0dcc44588fa Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 14 Jan 2026 03:13:49 +0000 Subject: [PATCH] Checkpoint: File edits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automatic checkpoint created by Claude Code - Branch: v3 - Timestamp: 2026-01-14T03:13:49Z - Changes: 1 file(s) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../cli/src/mcp-tools/hooks-tools.ts | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) 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 f3ea78a7c2..bbfdba0ab4 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts @@ -1436,6 +1436,60 @@ export const hooksTrajectoryEnd: MCPTool = { activeTrajectories.delete(trajectoryId); } + // SONA Learning - process trajectory outcome for routing optimization + let sonaResult: { learned: boolean; patternKey: string; confidence: number } = { + learned: false, patternKey: '', confidence: 0 + }; + let ewcResult: { consolidated: boolean; penalty: number } = { + consolidated: false, penalty: 0 + }; + + if (trajectory && persistResult.success) { + // Try SONA learning + const sona = await getSONAOptimizer(); + if (sona) { + try { + const outcome = { + trajectoryId, + task: trajectory.task, + agent: trajectory.agent, + success, + steps: trajectory.steps, + feedback, + duration: trajectory.startedAt + ? new Date(endedAt).getTime() - new Date(trajectory.startedAt).getTime() + : 0, + }; + const result = sona.processTrajectoryOutcome(outcome); + sonaResult = { + learned: result.learned, + patternKey: result.patternKey, + confidence: result.confidence, + }; + } catch { + // SONA learning failed, continue without it + } + } + + // Try EWC++ consolidation on successful trajectories + if (success) { + const ewc = await getEWCConsolidator(); + if (ewc) { + try { + // Record outcome for Fisher matrix update + ewc.recordOutcome(trajectory.agent, success, trajectory.steps.length); + const stats = ewc.getStats(); + ewcResult = { + consolidated: true, + penalty: stats.avgPenalty, + }; + } catch { + // EWC consolidation failed, continue without it + } + } + } + } + const learningTimeMs = Date.now() - startTime; return { @@ -1445,8 +1499,11 @@ export const hooksTrajectoryEnd: MCPTool = { persisted: persistResult.success, persistedId: persistResult.id, learning: { - sonaUpdate: persistResult.success, // Only true if actually persisted - ewcConsolidation: false, // Honest: EWC++ not yet implemented + sonaUpdate: sonaResult.learned, + sonaPatternKey: sonaResult.patternKey || undefined, + sonaConfidence: sonaResult.confidence || undefined, + ewcConsolidation: ewcResult.consolidated, + ewcPenalty: ewcResult.penalty || undefined, patternsExtracted: trajectory?.steps.length || 0, learningTimeMs, }, @@ -1456,8 +1513,10 @@ export const hooksTrajectoryEnd: MCPTool = { totalSteps: trajectory.steps.length, duration: trajectory.startedAt ? new Date(endedAt).getTime() - new Date(trajectory.startedAt).getTime() : 0, } : null, - implementation: persistResult.success ? 'real-persistence' : 'memory-only', - note: persistResult.success ? 'Trajectory persisted with embeddings for future learning' : (persistResult.error || 'Trajectory not found or store unavailable'), + implementation: sonaResult.learned ? 'real-sona-learning' : (persistResult.success ? 'real-persistence' : 'memory-only'), + note: sonaResult.learned + ? `SONA learned pattern "${sonaResult.patternKey}" with ${(sonaResult.confidence * 100).toFixed(1)}% confidence` + : (persistResult.success ? 'Trajectory persisted for future learning' : (persistResult.error || 'Trajectory not found')), }; }, };