ruvector-sona 0.1.8

Self-Optimizing Neural Architecture - Runtime-adaptive learning for LLM routers with two-tier LoRA, EWC++, and ReasoningBank
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Training Templates for SONA
//!
//! Pre-configured training setups optimized for different use cases.

use crate::types::SonaConfig;
use serde::{Deserialize, Serialize};

/// Agent specialization types
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AgentType {
    /// Code generation and assistance
    CodeAgent,
    /// General chat and conversation
    ChatAgent,
    /// Document retrieval and Q&A
    RagAgent,
    /// Task decomposition and planning
    TaskPlanner,
    /// Domain-specific expert
    DomainExpert,
    /// Codebase-aware assistant
    CodebaseHelper,
    /// Data analysis and insights
    DataAnalyst,
    /// Creative writing and content
    CreativeWriter,
    /// Reasoning and logic
    ReasoningAgent,
    /// Multi-modal understanding
    MultiModal,
    /// Custom agent type
    Custom(String),
}

impl std::fmt::Display for AgentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AgentType::CodeAgent => write!(f, "code-agent"),
            AgentType::ChatAgent => write!(f, "chat-agent"),
            AgentType::RagAgent => write!(f, "rag-agent"),
            AgentType::TaskPlanner => write!(f, "task-planner"),
            AgentType::DomainExpert => write!(f, "domain-expert"),
            AgentType::CodebaseHelper => write!(f, "codebase-helper"),
            AgentType::DataAnalyst => write!(f, "data-analyst"),
            AgentType::CreativeWriter => write!(f, "creative-writer"),
            AgentType::ReasoningAgent => write!(f, "reasoning-agent"),
            AgentType::MultiModal => write!(f, "multi-modal"),
            AgentType::Custom(name) => write!(f, "custom-{}", name),
        }
    }
}

/// Task domain for training focus
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskDomain {
    /// Software development
    SoftwareDevelopment,
    /// Customer support
    CustomerSupport,
    /// Healthcare
    Healthcare,
    /// Finance
    Finance,
    /// Legal
    Legal,
    /// Education
    Education,
    /// Research
    Research,
    /// Marketing
    Marketing,
    /// General purpose
    General,
    /// Custom domain
    Custom(String),
}

/// Training method configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TrainingMethod {
    /// Standard supervised learning
    Supervised {
        /// Batch size for training
        batch_size: usize,
        /// Number of epochs
        epochs: usize,
    },
    /// Reinforcement learning from feedback
    RLHF {
        /// Reward model weight
        reward_weight: f32,
        /// KL divergence penalty
        kl_penalty: f32,
    },
    /// Direct preference optimization
    DPO {
        /// Beta parameter for DPO
        beta: f32,
        /// Reference model weight
        ref_weight: f32,
    },
    /// Continuous online learning
    Online {
        /// Learning rate decay
        lr_decay: f32,
        /// Window size for recent examples
        window_size: usize,
    },
    /// Few-shot adaptation
    FewShot {
        /// Number of examples per class
        k_shot: usize,
        /// Meta-learning rate
        meta_lr: f32,
    },
}

impl Default for TrainingMethod {
    fn default() -> Self {
        TrainingMethod::Online {
            lr_decay: 0.999,
            window_size: 1000,
        }
    }
}

/// Vertical-specific configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VerticalConfig {
    /// Domain focus
    pub domain: TaskDomain,
    /// Specialized vocabulary size
    pub vocab_boost: usize,
    /// Domain-specific quality metrics
    pub quality_metrics: Vec<String>,
    /// Compliance requirements
    pub compliance_level: ComplianceLevel,
}

/// Compliance level for regulated industries
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub enum ComplianceLevel {
    #[default]
    None,
    /// Basic audit logging
    Basic,
    /// HIPAA compliance
    Hipaa,
    /// SOC2 compliance
    Soc2,
    /// GDPR compliance
    Gdpr,
    /// Custom compliance
    Custom(String),
}

/// Template preset for quick configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TemplatePreset {
    /// Minimal configuration for testing
    Minimal,
    /// Balanced for general use
    Balanced,
    /// High performance for production
    Production,
    /// Maximum quality regardless of speed
    MaxQuality,
    /// Edge deployment (<5MB)
    Edge,
    /// Research and experimentation
    Research,
}

/// Training template with full configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TrainingTemplate {
    /// Template name
    pub name: String,
    /// Agent type
    pub agent_type: AgentType,
    /// SONA configuration
    pub sona_config: SonaConfig,
    /// Training method
    pub training_method: TrainingMethod,
    /// Vertical configuration
    pub vertical: Option<VerticalConfig>,
    /// Expected training data size
    pub expected_data_size: DataSizeHint,
    /// Memory budget in MB
    pub memory_budget_mb: usize,
    /// Target latency in microseconds
    pub target_latency_us: u64,
    /// Enable continuous learning
    pub continuous_learning: bool,
    /// Auto-export trained adapters
    pub auto_export: bool,
    /// Tags for organization
    pub tags: Vec<String>,
}

/// Hint about training data size
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub enum DataSizeHint {
    /// <100 examples (few-shot)
    Tiny,
    /// 100-1000 examples
    Small,
    /// 1000-10000 examples
    #[default]
    Medium,
    /// 10000-100000 examples
    Large,
    /// >100000 examples
    Massive,
}

impl TrainingTemplate {
    /// Create a new training template
    pub fn new(name: impl Into<String>, agent_type: AgentType) -> Self {
        Self {
            name: name.into(),
            agent_type,
            sona_config: SonaConfig::default(),
            training_method: TrainingMethod::default(),
            vertical: None,
            expected_data_size: DataSizeHint::default(),
            memory_budget_mb: 100,
            target_latency_us: 1000,
            continuous_learning: true,
            auto_export: false,
            tags: Vec::new(),
        }
    }

    /// Create from preset
    pub fn from_preset(preset: TemplatePreset, agent_type: AgentType) -> Self {
        let mut template = Self::new(format!("{:?}-{}", preset, agent_type), agent_type.clone());

        match preset {
            TemplatePreset::Minimal => {
                template.sona_config = SonaConfig::edge_deployment();
                template.memory_budget_mb = 10;
                template.expected_data_size = DataSizeHint::Tiny;
            }
            TemplatePreset::Balanced => {
                template.sona_config = SonaConfig::default();
                template.memory_budget_mb = 100;
            }
            TemplatePreset::Production => {
                template.sona_config = SonaConfig::max_throughput();
                template.memory_budget_mb = 200;
                template.auto_export = true;
            }
            TemplatePreset::MaxQuality => {
                template.sona_config = SonaConfig::max_quality();
                template.memory_budget_mb = 500;
                template.expected_data_size = DataSizeHint::Large;
            }
            TemplatePreset::Edge => {
                template.sona_config = SonaConfig::edge_deployment();
                template.memory_budget_mb = 5;
                template.target_latency_us = 500;
            }
            TemplatePreset::Research => {
                template.sona_config = SonaConfig::max_quality();
                template.sona_config.trajectory_capacity = 50000;
                template.memory_budget_mb = 1000;
                template.expected_data_size = DataSizeHint::Massive;
            }
        }

        // Apply agent-specific optimizations
        template.apply_agent_optimizations();
        template
    }

    //------------------------------------------------------------------
    // Pre-built Templates for Common Use Cases
    //------------------------------------------------------------------

    /// Code agent template - optimized for code generation
    ///
    /// **Best for**: Code completion, bug fixes, refactoring
    /// **Config**: baseLoraRank=16, clusters=200, capacity=10000
    /// **Training data**: Code completions, fixes, reviews
    pub fn code_agent() -> Self {
        let mut template = Self::new("code-agent", AgentType::CodeAgent);
        template.sona_config.base_lora_rank = 16; // Deeper for code patterns
        template.sona_config.pattern_clusters = 200; // Many code patterns
        template.sona_config.trajectory_capacity = 10000;
        template.sona_config.quality_threshold = 0.2; // Learn from most examples
        template.training_method = TrainingMethod::Online {
            lr_decay: 0.9995,
            window_size: 5000,
        };
        template.tags = vec!["code".into(), "development".into(), "completion".into()];
        template
    }

    /// Chat agent template - optimized for conversational AI
    ///
    /// **Best for**: Customer support, general chat, assistants
    /// **Config**: baseLoraRank=8, clusters=50, fast response
    /// **Training data**: Conversation histories, feedback
    pub fn chat_agent() -> Self {
        let mut template = Self::new("chat-agent", AgentType::ChatAgent);
        template.sona_config.base_lora_rank = 8;
        template.sona_config.pattern_clusters = 50;
        template.sona_config.quality_threshold = 0.4;
        template.target_latency_us = 500; // Fast responses
        template.training_method = TrainingMethod::RLHF {
            reward_weight: 0.5,
            kl_penalty: 0.1,
        };
        template.tags = vec!["chat".into(), "conversation".into(), "support".into()];
        template
    }

    /// RAG agent template - optimized for retrieval-augmented generation
    ///
    /// **Best for**: Document Q&A, knowledge bases, search
    /// **Config**: clusters=200, capacity=10000, high pattern storage
    /// **Training data**: Document chunks, Q&A pairs
    pub fn rag_agent() -> Self {
        let mut template = Self::new("rag-agent", AgentType::RagAgent);
        template.sona_config.pattern_clusters = 200; // Many document patterns
        template.sona_config.trajectory_capacity = 10000;
        template.sona_config.embedding_dim = 512; // Larger embeddings for retrieval
        template.sona_config.hidden_dim = 512;
        template.training_method = TrainingMethod::Supervised {
            batch_size: 32,
            epochs: 10,
        };
        template.tags = vec!["rag".into(), "retrieval".into(), "documents".into()];
        template
    }

    /// Task planner template - optimized for task decomposition
    ///
    /// **Best for**: Project planning, task breakdown, scheduling
    /// **Config**: baseLoraRank=16, ewcLambda=2000, multi-task
    /// **Training data**: Task decompositions, planning examples
    pub fn task_planner() -> Self {
        let mut template = Self::new("task-planner", AgentType::TaskPlanner);
        template.sona_config.base_lora_rank = 16;
        template.sona_config.ewc_lambda = 2000.0; // Important for multi-task
        template.sona_config.pattern_clusters = 100;
        template.training_method = TrainingMethod::DPO {
            beta: 0.1,
            ref_weight: 0.5,
        };
        template.tags = vec!["planning".into(), "tasks".into(), "decomposition".into()];
        template
    }

    /// Domain expert template - optimized for specialized knowledge
    ///
    /// **Best for**: Legal, medical, financial expertise
    /// **Config**: qualityThreshold=0.1, high capacity, compliance
    /// **Training data**: Domain-specific Q&A, expert responses
    pub fn domain_expert(domain: TaskDomain) -> Self {
        let domain_name = format!("{:?}", domain).to_lowercase();
        let mut template = Self::new(
            format!("domain-expert-{}", domain_name),
            AgentType::DomainExpert,
        );
        template.sona_config.quality_threshold = 0.1; // Learn from all domain examples
        template.sona_config.trajectory_capacity = 20000;
        template.sona_config.base_lora_rank = 16;
        template.vertical = Some(VerticalConfig {
            domain: domain.clone(),
            vocab_boost: 10000,
            quality_metrics: vec!["accuracy".into(), "relevance".into(), "compliance".into()],
            compliance_level: match domain {
                TaskDomain::Healthcare => ComplianceLevel::Hipaa,
                TaskDomain::Finance => ComplianceLevel::Soc2,
                TaskDomain::Legal => ComplianceLevel::Basic,
                _ => ComplianceLevel::None,
            },
        });
        template.tags = vec!["domain".into(), "expert".into(), domain_name];
        template
    }

    /// Codebase helper template - learns your specific codebase
    ///
    /// **Best for**: Repository-specific assistance, code navigation
    /// **Config**: clusters=200, capacity=10000, high pattern storage
    /// **Training data**: Your repo's code, documentation
    pub fn codebase_helper() -> Self {
        let mut template = Self::new("codebase-helper", AgentType::CodebaseHelper);
        template.sona_config.pattern_clusters = 200;
        template.sona_config.trajectory_capacity = 10000;
        template.sona_config.quality_threshold = 0.2;
        template.sona_config.base_lora_rank = 16;
        template.expected_data_size = DataSizeHint::Large;
        template.training_method = TrainingMethod::Online {
            lr_decay: 0.999,
            window_size: 10000,
        };
        template.tags = vec!["codebase".into(), "repository".into(), "navigation".into()];
        template
    }

    /// Data analyst template - optimized for data insights
    ///
    /// **Best for**: Data analysis, visualization, statistics
    /// **Config**: baseLoraRank=8, clusters=100, reasoning focus
    pub fn data_analyst() -> Self {
        let mut template = Self::new("data-analyst", AgentType::DataAnalyst);
        template.sona_config.base_lora_rank = 8;
        template.sona_config.pattern_clusters = 100;
        template.vertical = Some(VerticalConfig {
            domain: TaskDomain::Research,
            vocab_boost: 5000,
            quality_metrics: vec!["accuracy".into(), "insight_quality".into()],
            compliance_level: ComplianceLevel::None,
        });
        template.tags = vec!["data".into(), "analysis".into(), "insights".into()];
        template
    }

    /// Creative writer template - optimized for content generation
    ///
    /// **Best for**: Marketing copy, blog posts, creative writing
    /// **Config**: High diversity, quality focus
    pub fn creative_writer() -> Self {
        let mut template = Self::new("creative-writer", AgentType::CreativeWriter);
        template.sona_config.base_lora_rank = 8;
        template.sona_config.pattern_clusters = 50; // Fewer clusters for diversity
        template.sona_config.quality_threshold = 0.5; // Only learn from high quality
        template.training_method = TrainingMethod::RLHF {
            reward_weight: 0.7,
            kl_penalty: 0.05, // Less constraint for creativity
        };
        template.vertical = Some(VerticalConfig {
            domain: TaskDomain::Marketing,
            vocab_boost: 0,
            quality_metrics: vec!["creativity".into(), "engagement".into(), "clarity".into()],
            compliance_level: ComplianceLevel::None,
        });
        template.tags = vec!["creative".into(), "writing".into(), "content".into()];
        template
    }

    /// Reasoning agent template - optimized for logical reasoning
    ///
    /// **Best for**: Math, logic, chain-of-thought reasoning
    /// **Config**: High rank, strong EWC, accuracy focus
    pub fn reasoning_agent() -> Self {
        let mut template = Self::new("reasoning-agent", AgentType::ReasoningAgent);
        template.sona_config.base_lora_rank = 16;
        template.sona_config.ewc_lambda = 3000.0; // Strong protection
        template.sona_config.pattern_clusters = 150;
        template.sona_config.quality_threshold = 0.3;
        template.training_method = TrainingMethod::DPO {
            beta: 0.15,
            ref_weight: 0.4,
        };
        template.tags = vec!["reasoning".into(), "logic".into(), "math".into()];
        template
    }

    //------------------------------------------------------------------
    // Builder Methods
    //------------------------------------------------------------------

    /// Set SONA configuration
    pub fn with_sona_config(mut self, config: SonaConfig) -> Self {
        self.sona_config = config;
        self
    }

    /// Set training method
    pub fn with_training_method(mut self, method: TrainingMethod) -> Self {
        self.training_method = method;
        self
    }

    /// Set vertical configuration
    pub fn with_vertical(mut self, vertical: VerticalConfig) -> Self {
        self.vertical = Some(vertical);
        self
    }

    /// Set memory budget
    pub fn with_memory_budget(mut self, mb: usize) -> Self {
        self.memory_budget_mb = mb;
        self
    }

    /// Set target latency
    pub fn with_target_latency(mut self, us: u64) -> Self {
        self.target_latency_us = us;
        self
    }

    /// Enable continuous learning
    pub fn with_continuous_learning(mut self, enabled: bool) -> Self {
        self.continuous_learning = enabled;
        self
    }

    /// Enable auto-export
    pub fn with_auto_export(mut self, enabled: bool) -> Self {
        self.auto_export = enabled;
        self
    }

    /// Add tags
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Set hidden dimension
    pub fn with_hidden_dim(mut self, dim: usize) -> Self {
        self.sona_config.hidden_dim = dim;
        self.sona_config.embedding_dim = dim;
        self
    }

    /// Set LoRA ranks
    pub fn with_lora_ranks(mut self, micro: usize, base: usize) -> Self {
        self.sona_config.micro_lora_rank = micro.min(2); // MicroLoRA max rank is 2
        self.sona_config.base_lora_rank = base;
        self
    }

    //------------------------------------------------------------------
    // Internal Methods
    //------------------------------------------------------------------

    /// Apply agent-specific optimizations
    fn apply_agent_optimizations(&mut self) {
        match &self.agent_type {
            AgentType::CodeAgent | AgentType::CodebaseHelper => {
                self.sona_config.pattern_clusters = 200;
                self.sona_config.base_lora_rank = 16;
            }
            AgentType::ChatAgent => {
                self.sona_config.pattern_clusters = 50;
                self.target_latency_us = 500;
            }
            AgentType::RagAgent => {
                self.sona_config.pattern_clusters = 200;
                self.sona_config.trajectory_capacity = 10000;
            }
            AgentType::ReasoningAgent => {
                self.sona_config.ewc_lambda = 3000.0;
                self.sona_config.base_lora_rank = 16;
            }
            AgentType::DomainExpert => {
                self.sona_config.quality_threshold = 0.1;
            }
            _ => {}
        }
    }

    /// Validate template configuration
    pub fn validate(&self) -> Result<(), String> {
        if self.sona_config.micro_lora_rank > 2 {
            return Err("MicroLoRA rank must be 1 or 2".into());
        }
        if self.sona_config.hidden_dim == 0 {
            return Err("Hidden dimension must be > 0".into());
        }
        if self.memory_budget_mb < 1 {
            return Err("Memory budget must be >= 1 MB".into());
        }
        Ok(())
    }

    /// Get estimated memory usage in MB
    pub fn estimated_memory_mb(&self) -> usize {
        let config = &self.sona_config;

        // Base engine memory
        let engine_mb = 5;

        // LoRA weights: hidden_dim * rank * 2 (A and B matrices) * 4 bytes * 2 (micro + base)
        let lora_bytes =
            config.hidden_dim * (config.micro_lora_rank + config.base_lora_rank) * 2 * 4 * 2;
        let lora_mb = lora_bytes / (1024 * 1024);

        // Trajectory buffer: capacity * ~800 bytes per trajectory
        let traj_mb = (config.trajectory_capacity * 800) / (1024 * 1024);

        // Pattern storage: clusters * embedding_dim * 4 bytes
        let pattern_mb = (config.pattern_clusters * config.embedding_dim * 4) / (1024 * 1024);

        engine_mb + lora_mb + traj_mb + pattern_mb + 1
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_template_creation() {
        let template = TrainingTemplate::code_agent();
        assert_eq!(template.agent_type, AgentType::CodeAgent);
        assert_eq!(template.sona_config.base_lora_rank, 16);
        assert_eq!(template.sona_config.pattern_clusters, 200);
    }

    #[test]
    fn test_preset_templates() {
        let production =
            TrainingTemplate::from_preset(TemplatePreset::Production, AgentType::ChatAgent);
        assert!(production.auto_export);

        let edge = TrainingTemplate::from_preset(TemplatePreset::Edge, AgentType::ChatAgent);
        assert_eq!(edge.memory_budget_mb, 5);
    }

    #[test]
    fn test_domain_expert() {
        let medical = TrainingTemplate::domain_expert(TaskDomain::Healthcare);
        assert!(medical.vertical.is_some());
        if let Some(v) = &medical.vertical {
            assert!(matches!(v.compliance_level, ComplianceLevel::Hipaa));
        }
    }

    #[test]
    fn test_builder_pattern() {
        let template = TrainingTemplate::new("custom", AgentType::Custom("test".into()))
            .with_hidden_dim(512)
            .with_lora_ranks(2, 16)
            .with_memory_budget(200)
            .with_continuous_learning(true);

        assert_eq!(template.sona_config.hidden_dim, 512);
        assert_eq!(template.sona_config.micro_lora_rank, 2);
        assert_eq!(template.sona_config.base_lora_rank, 16);
    }

    #[test]
    fn test_validation() {
        let mut template = TrainingTemplate::code_agent();
        assert!(template.validate().is_ok());

        template.sona_config.micro_lora_rank = 5;
        assert!(template.validate().is_err());
    }

    #[test]
    fn test_memory_estimation() {
        let template = TrainingTemplate::code_agent();
        let mem = template.estimated_memory_mb();
        assert!(mem > 0);
        assert!(mem < template.memory_budget_mb * 2);
    }
}