/** * SONA (Self-Optimizing Neural Architecture) Optimizer * * Processes trajectory outcomes to learn optimal routing patterns. * Integrates with Q-learning router and persistence layer. * * Features: * - Processes trajectory outcomes from hooksTrajectoryEnd * - Extracts keywords from tasks for pattern matching * - Maintains learned routing patterns with confidence scoring * - Persists patterns to .swarm/sona-patterns.json * - Integrates with Q-learning router for combined routing * * @module v3/cli/memory/sona-optimizer */ /** * Trajectory outcome from hooks/intelligence/trajectory-end */ export interface TrajectoryOutcome { trajectoryId: string; task: string; agent: string; success: boolean; steps?: Array<{ action: string; result: string; quality: number; timestamp: string; }>; feedback?: string; duration?: number; /** * ADR-511: files the task's verified fix edited (project-relative). Never keywords: they only decide which pattern a * trajectory reinforces, so two wordings of one task that fixed the same file learn together. */ anchors?: string[]; } /** * Learned routing pattern */ export interface LearnedPattern { /** Keywords extracted from task descriptions */ keywords: string[]; /** Agent that handled the task */ agent: string; /** Confidence score (0-1) */ confidence: number; /** Number of successful uses */ successCount: number; /** Number of failed uses */ failureCount: number; /** Last time pattern was used */ lastUsed: number; /** Pattern creation time */ createdAt: number; /** ADR-511: files verified fixes for this pattern edited; absent in patterns saved before it. */ anchors?: string[]; } /** * Routing suggestion result */ export interface RoutingSuggestion { /** Recommended agent */ agent: string; /** Confidence in recommendation (0-1) */ confidence: number; /** Whether Q-learning was used */ usedQLearning: boolean; /** Source of recommendation */ source: 'sona-native' | 'sona-pattern' | 'q-learning' | 'sona-keyword' | 'default'; /** Alternative agents with scores */ alternatives: Array<{ agent: string; score: number; }>; /** Matched keywords */ matchedKeywords?: string[]; } /** * SONA optimizer statistics */ export interface SONAStats { /** Total patterns learned */ totalPatterns: number; /** Successful routing decisions */ successfulRoutings: number; /** Failed routing decisions */ failedRoutings: number; /** Total trajectories processed */ trajectoriesProcessed: number; /** Average confidence of patterns */ avgConfidence: number; /** Q-learning integration status */ qLearningEnabled: boolean; /** Time of last learning update */ lastUpdate: number | null; /** Contrastive trainer status (from @ruvector/ruvllm) */ _contrastiveTrainer?: { triplets: number; agents: number; } | 'unavailable'; } /** The routing gate in getRoutingSuggestion: confidence x matchRatio must reach this for a learned pattern to be used. */ export declare const SONA_ROUTING_GATE = 0.6; /** Agent roles supported by the SONA routing optimizer. */ export declare function isSupportedSonaAgent(agent: string): boolean; /** * SONA Optimizer for adaptive routing based on trajectory outcomes * * 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 declare function routingGateStatus(confidence: number): { threshold: number; confidence: number; reached: boolean; successesNeeded: number; }; export declare class SONAOptimizer { private patterns; private trajectoriesProcessed; private successfulRoutings; private failedRoutings; private lastUpdate; private persistencePath; private qLearningRouter; private qLearningEnabled; /** Real @ruvector/sona engine — null if native not available, undefined if not yet tried */ private sonaEngine; constructor(options?: { persistencePath?: string; }); /** * Attempt to load the native @ruvector/sona engine (once). * Sets `sonaEngine` to the engine instance or null if unavailable. */ private loadSonaEngine; /** * Infer an agent type string from a SONA pattern result object. */ private inferAgentFromPattern; /** * Initialize the optimizer and load persisted state */ initialize(): Promise<{ success: boolean; patternsLoaded: number; }>; /** * Process a trajectory outcome and learn from it * Called by hooksTrajectoryEnd */ processTrajectoryOutcome(outcome: TrajectoryOutcome): { learned: boolean; 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; }; }; /** * Get routing suggestion based on learned patterns. * * Priority order: * 1. Real @ruvector/sona native engine (if available and has matches) * 2. SONA learned pattern matching (keyword overlap + confidence) * 3. Q-learning router (if enabled) * 4. Keyword heuristic * 5. Default fallback */ getRoutingSuggestion(task: string): Promise; /** * Get optimizer statistics */ getStats(): SONAStats; /** * Trigger contrastive training on accumulated agent embeddings. * Returns training metrics or { trained: false } if insufficient data. * * @param _epochs - reserved for future use (epochs are set at ContrastiveTrainer construction) */ trainAgentEmbeddings(_epochs?: number): Promise<{ trained: boolean; loss?: number; triplets?: number; }>; /** * Apply temporal decay to pattern confidence * Reduces confidence of unused patterns */ applyTemporalDecay(): number; /** * Reset all learned patterns */ reset(): void; /** * Export patterns for analysis */ exportPatterns(): Record; /** * Import patterns (for migration or testing) */ importPatterns(patterns: Record): number; /** * Convert extracted keywords into a lightweight 384-dim embedding proxy. * Uses a deterministic hash-scatter so each keyword set maps to a * consistent unit-length vector compatible with ContrastiveTrainer. */ private keywordsToEmbedding; /** * Extract meaningful keywords from task description */ private extractKeywords; /** * Check if word is a stop word */ private isStopWord; /** * Create a unique pattern key from keywords and agent */ private createPatternKey; /** * 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; /** * Find the best matching pattern for given keywords */ private findBestPatternMatch; /** * Match keywords to agent using category heuristics */ private matchKeywordsToAgent; /** * Get alternative agent suggestions */ private getAlternatives; /** * Prune old/low-confidence patterns if over limit */ private prunePatterns; /** * Validate pattern structure */ private validatePattern; /** * Load patterns from disk */ private loadFromDisk; /** * Save patterns to disk */ private saveToDisk; } /** * Get the singleton SONAOptimizer instance * Uses lazy initialization to avoid circular imports */ export declare function getSONAOptimizer(): Promise; /** * Reset the singleton instance (for testing) */ export declare function resetSONAOptimizer(): void; /** * Process a trajectory outcome (convenience function) */ export declare function processTrajectory(outcome: TrajectoryOutcome): Promise<{ learned: boolean; patternKey: string; confidence: number; keywordsExtracted: string[]; }>; /** * Get routing suggestion (convenience function) */ export declare function getSuggestion(task: string): Promise; /** * Get SONA statistics (convenience function) */ export declare function getSONAStats(): Promise; declare const _default: { SONAOptimizer: typeof SONAOptimizer; getSONAOptimizer: typeof getSONAOptimizer; resetSONAOptimizer: typeof resetSONAOptimizer; processTrajectory: typeof processTrajectory; getSuggestion: typeof getSuggestion; getSONAStats: typeof getSONAStats; }; export default _default; //# sourceMappingURL=sona-optimizer.d.ts.map