converge-mnemos-knowledge 1.2.2

Self-learning knowledgebase with vector search, gRPC, and MCP interfaces. Implements Converge recall and storage suggestors.
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
//! Meta-Learning Primitives
//!
//! Implements meta-learning ("learning to learn") mechanisms that allow agents to:
//!
//! 1. Learn task priors from past experience
//! 2. Adapt quickly to new tasks with few examples
//! 3. Maintain a repertoire of learning strategies
//! 4. Select optimal learning approaches for new tasks
//!
//! Based on MAML, Reptile, and meta-learning research.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Meta-learner that learns how to learn.
///
/// Maintains initialization parameters that enable fast
/// adaptation to new tasks (inspired by MAML/Reptile).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaLearner {
    /// Unique identifier.
    pub id: Uuid,

    /// Name/description.
    pub name: String,

    /// Meta-parameters (good initialization for fast adaptation).
    pub meta_params: Vec<f32>,

    /// Learning strategies discovered.
    pub strategies: Vec<LearningStrategy>,

    /// Task embeddings for similarity.
    pub task_embeddings: HashMap<String, Vec<f32>>,

    /// Meta-learning rate (outer loop).
    pub meta_lr: f32,

    /// Inner loop learning rate.
    pub inner_lr: f32,

    /// Number of tasks learned.
    pub task_count: u64,

    /// When created.
    pub created_at: DateTime<Utc>,

    /// When last updated.
    pub updated_at: DateTime<Utc>,
}

impl MetaLearner {
    /// Create a new meta-learner.
    pub fn new(name: impl Into<String>, num_params: usize) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name: name.into(),
            meta_params: vec![0.0; num_params],
            strategies: Vec::new(),
            task_embeddings: HashMap::new(),
            meta_lr: 0.1,
            inner_lr: 0.01,
            task_count: 0,
            created_at: now,
            updated_at: now,
        }
    }

    /// Set meta learning rate.
    pub fn with_meta_lr(mut self, lr: f32) -> Self {
        self.meta_lr = lr;
        self
    }

    /// Set inner learning rate.
    pub fn with_inner_lr(mut self, lr: f32) -> Self {
        self.inner_lr = lr;
        self
    }

    /// Get initialization parameters for a new task.
    ///
    /// Uses meta-params as starting point, potentially adjusted
    /// based on task similarity.
    pub fn initialize_for_task(&self, task_embedding: Option<&[f32]>) -> Vec<f32> {
        let mut params = self.meta_params.clone();

        // If we have a task embedding, adjust based on similar tasks
        if let Some(emb) = task_embedding
            && let Some((_, similar_params)) = self.find_similar_task(emb)
        {
            // Blend meta-params with similar task's successful params
            for i in 0..params.len().min(similar_params.len()) {
                params[i] = 0.7 * params[i] + 0.3 * similar_params[i];
            }
        }

        params
    }

    /// Find most similar past task.
    fn find_similar_task(&self, embedding: &[f32]) -> Option<(&str, Vec<f32>)> {
        let mut best_sim = -1.0f32;
        let mut best_task: Option<&str> = None;

        for (task_id, task_emb) in &self.task_embeddings {
            let sim = cosine_similarity(embedding, task_emb);
            if sim > best_sim {
                best_sim = sim;
                best_task = Some(task_id);
            }
        }

        // Only return if similarity is meaningful
        if best_sim > 0.5 {
            best_task.map(|t| (t, self.meta_params.clone()))
        } else {
            None
        }
    }

    /// Meta-update after completing a task (Reptile-style).
    ///
    /// Updates meta-params to be a better initialization
    /// for future tasks.
    pub fn meta_update(
        &mut self,
        task_id: &str,
        final_params: &[f32],
        task_embedding: Option<Vec<f32>>,
    ) {
        if final_params.len() != self.meta_params.len() {
            return;
        }

        // Reptile update: move meta-params towards task solution
        for (meta, &fin) in self.meta_params.iter_mut().zip(final_params.iter()) {
            let delta = fin - *meta;
            *meta += self.meta_lr * delta;
        }

        // Store task embedding for similarity lookup
        if let Some(emb) = task_embedding {
            self.task_embeddings.insert(task_id.to_string(), emb);
        }

        self.task_count += 1;
        self.updated_at = Utc::now();
    }

    /// Register a learning strategy that worked well.
    pub fn register_strategy(&mut self, strategy: LearningStrategy) {
        // Check if similar strategy exists
        let exists = self.strategies.iter().any(|s| s.name == strategy.name);
        if !exists {
            self.strategies.push(strategy);
        }
    }

    /// Select best strategy for a new task.
    pub fn select_strategy(&self, task_features: &TaskFeatures) -> Option<&LearningStrategy> {
        let mut best_score = 0.0f32;
        let mut best_strategy: Option<&LearningStrategy> = None;

        for strategy in &self.strategies {
            let score = strategy.score_for_task(task_features);
            if score > best_score {
                best_score = score;
                best_strategy = Some(strategy);
            }
        }

        // Only recommend if confident
        if best_score > 0.5 {
            best_strategy
        } else {
            None
        }
    }

    /// Get number of strategies.
    pub fn num_strategies(&self) -> usize {
        self.strategies.len()
    }

    /// Get number of tasks learned.
    pub fn num_tasks(&self) -> u64 {
        self.task_count
    }
}

/// A learning strategy discovered through meta-learning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearningStrategy {
    /// Strategy name.
    pub name: String,

    /// Description of when to use this strategy.
    pub description: String,

    /// Hyperparameters for this strategy.
    pub hyperparams: HashMap<String, f32>,

    /// Which task features favor this strategy.
    pub preferred_features: TaskFeatures,

    /// Success rate when applied.
    pub success_rate: f32,

    /// Number of times used.
    pub usage_count: u64,
}

impl LearningStrategy {
    /// Create a new strategy.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            hyperparams: HashMap::new(),
            preferred_features: TaskFeatures::default(),
            success_rate: 0.5,
            usage_count: 0,
        }
    }

    /// Set description.
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Set hyperparameter.
    pub fn with_hyperparam(mut self, name: impl Into<String>, value: f32) -> Self {
        self.hyperparams.insert(name.into(), value);
        self
    }

    /// Set preferred features.
    pub fn with_preferred_features(mut self, features: TaskFeatures) -> Self {
        self.preferred_features = features;
        self
    }

    /// Score how well this strategy fits a task.
    pub fn score_for_task(&self, task: &TaskFeatures) -> f32 {
        let mut score = 0.0f32;
        let mut count = 0;

        // Compare features
        if let (Some(a), Some(b)) = (self.preferred_features.data_size, task.data_size) {
            score += 1.0 - (a as f32 - b as f32).abs() / (a.max(b) as f32 + 1.0);
            count += 1;
        }

        if let (Some(a), Some(b)) = (self.preferred_features.noise_level, task.noise_level) {
            score += 1.0 - (a - b).abs();
            count += 1;
        }

        if let (Some(a), Some(b)) = (self.preferred_features.complexity, task.complexity) {
            score += 1.0 - (a - b).abs();
            count += 1;
        }

        if self.preferred_features.is_classification == task.is_classification {
            score += 1.0;
            count += 1;
        }

        // Weight by success rate
        let feature_score = if count > 0 { score / count as f32 } else { 0.5 };

        feature_score * self.success_rate
    }

    /// Record usage outcome.
    pub fn record_usage(&mut self, succeeded: bool) {
        self.usage_count += 1;
        let outcome = if succeeded { 1.0 } else { 0.0 };
        // Exponential moving average
        self.success_rate = 0.9 * self.success_rate + 0.1 * outcome;
    }
}

/// Features describing a learning task.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TaskFeatures {
    /// Number of training examples.
    pub data_size: Option<usize>,

    /// Estimated noise level (0.0 to 1.0).
    pub noise_level: Option<f32>,

    /// Task complexity (0.0 to 1.0).
    pub complexity: Option<f32>,

    /// Whether this is classification (vs regression).
    pub is_classification: bool,

    /// Number of input features.
    pub input_dim: Option<usize>,

    /// Number of output classes/values.
    pub output_dim: Option<usize>,

    /// Domain identifier.
    pub domain: Option<String>,
}

impl TaskFeatures {
    /// Create new task features.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set data size.
    pub fn with_data_size(mut self, size: usize) -> Self {
        self.data_size = Some(size);
        self
    }

    /// Set noise level.
    pub fn with_noise(mut self, noise: f32) -> Self {
        self.noise_level = Some(noise.clamp(0.0, 1.0));
        self
    }

    /// Set complexity.
    pub fn with_complexity(mut self, complexity: f32) -> Self {
        self.complexity = Some(complexity.clamp(0.0, 1.0));
        self
    }

    /// Set classification flag.
    pub fn classification(mut self) -> Self {
        self.is_classification = true;
        self
    }

    /// Set regression flag.
    pub fn regression(mut self) -> Self {
        self.is_classification = false;
        self
    }

    /// Set domain.
    pub fn with_domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }
}

/// Few-shot learner for quick adaptation.
///
/// Given a small number of examples, adapts quickly
/// using meta-learned priors.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FewShotLearner {
    /// Base parameters from meta-learner.
    base_params: Vec<f32>,

    /// Adapted parameters.
    adapted_params: Vec<f32>,

    /// Support set (few examples).
    support_set: Vec<(Vec<f32>, f32)>,

    /// Learning rate for adaptation.
    adapt_lr: f32,

    /// Number of adaptation steps.
    adapt_steps: usize,
}

impl FewShotLearner {
    /// Create from meta-learner initialization.
    pub fn from_meta(meta: &MetaLearner, task_embedding: Option<&[f32]>) -> Self {
        let params = meta.initialize_for_task(task_embedding);
        Self {
            base_params: params.clone(),
            adapted_params: params,
            support_set: Vec::new(),
            adapt_lr: meta.inner_lr,
            adapt_steps: 5,
        }
    }

    /// Set adaptation learning rate.
    pub fn with_adapt_lr(mut self, lr: f32) -> Self {
        self.adapt_lr = lr;
        self
    }

    /// Set number of adaptation steps.
    pub fn with_adapt_steps(mut self, steps: usize) -> Self {
        self.adapt_steps = steps;
        self
    }

    /// Add an example to the support set.
    pub fn add_example(&mut self, features: Vec<f32>, target: f32) {
        self.support_set.push((features, target));
    }

    /// Adapt to the support set.
    ///
    /// Performs gradient descent on support set to adapt
    /// parameters from meta-learned initialization.
    pub fn adapt(&mut self) {
        self.adapted_params = self.base_params.clone();

        for _ in 0..self.adapt_steps {
            for (features, target) in &self.support_set {
                if features.len() != self.adapted_params.len() {
                    continue;
                }

                // Forward pass
                let pred: f32 = features
                    .iter()
                    .zip(self.adapted_params.iter())
                    .map(|(f, p)| f * p)
                    .sum();

                // Backward pass
                let error = pred - target;
                for (param, &feat) in self.adapted_params.iter_mut().zip(features.iter()) {
                    let grad = 2.0 * error * feat;
                    *param -= self.adapt_lr * grad;
                }
            }
        }
    }

    /// Predict for new input.
    pub fn predict(&self, features: &[f32]) -> f32 {
        if features.len() != self.adapted_params.len() {
            return 0.0;
        }

        features
            .iter()
            .zip(self.adapted_params.iter())
            .map(|(f, p)| f * p)
            .sum()
    }

    /// Get final adapted parameters.
    pub fn get_adapted_params(&self) -> &[f32] {
        &self.adapted_params
    }

    /// Number of support examples.
    pub fn support_size(&self) -> usize {
        self.support_set.len()
    }
}

/// Compute cosine similarity between two vectors.
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }

    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        0.0
    } else {
        dot / (norm_a * norm_b)
    }
}

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

    /// Test: Meta-learning accumulates knowledge across tasks.
    ///
    /// What happens:
    /// 1. Train on multiple similar tasks
    /// 2. Meta-params move towards common solution
    /// 3. New tasks can adapt faster from meta-params
    #[test]
    fn test_meta_learning() {
        let mut meta = MetaLearner::new("test_meta", 2)
            .with_meta_lr(0.3)
            .with_inner_lr(0.1);

        // Train on multiple tasks where y = 2*x1 + 3*x2 + noise
        for task_idx in 0..5 {
            // Simulate task-specific adaptation
            let task_id = format!("task_{}", task_idx);
            let noise = (task_idx as f32 - 2.0) * 0.1; // Small variation per task

            // Final params after task training (simulated)
            let final_params = vec![2.0 + noise, 3.0 - noise];

            meta.meta_update(&task_id, &final_params, None);
        }

        // Meta-params should have moved towards [2, 3]
        // After 5 updates with meta_lr=0.3, they should be in a reasonable range
        assert!(
            (meta.meta_params[0] - 2.0).abs() < 1.5,
            "param[0] = {}",
            meta.meta_params[0]
        );
        assert!(
            (meta.meta_params[1] - 3.0).abs() < 1.5,
            "param[1] = {}",
            meta.meta_params[1]
        );
        assert_eq!(meta.num_tasks(), 5);
    }

    /// Test: Few-shot learning with meta initialization.
    ///
    /// What happens:
    /// 1. Create meta-learner with good initialization
    /// 2. Create few-shot learner from meta
    /// 3. Adapt quickly to new task with few examples
    #[test]
    fn test_few_shot_learning() {
        // Set up meta-learner with good prior
        let mut meta = MetaLearner::new("few_shot_meta", 1);
        meta.meta_params = vec![1.5]; // Good starting point

        // Create few-shot learner
        let mut few_shot = FewShotLearner::from_meta(&meta, None)
            .with_adapt_lr(0.5)
            .with_adapt_steps(10);

        // Add few examples for y = 2*x
        few_shot.add_example(vec![1.0], 2.0);
        few_shot.add_example(vec![2.0], 4.0);
        few_shot.add_example(vec![0.5], 1.0);

        // Adapt
        few_shot.adapt();

        // Predict
        let pred = few_shot.predict(&[3.0]);

        // Should be close to 6.0 (3 * 2)
        assert!((pred - 6.0).abs() < 1.0, "Expected ~6.0, got {}", pred);
    }

    /// Test: Learning strategy selection.
    ///
    /// What happens:
    /// 1. Register multiple strategies
    /// 2. Select best strategy for a new task
    /// 3. Strategy matching considers task features
    #[test]
    fn test_strategy_selection() {
        let mut meta = MetaLearner::new("strategy_meta", 1);

        // Register strategies with high success rates to pass the 0.5 threshold
        let mut small_data_strategy = LearningStrategy::new("few_shot")
            .with_description("For small datasets")
            .with_hyperparam("lr", 0.1)
            .with_preferred_features(TaskFeatures {
                data_size: Some(10),
                noise_level: Some(0.1),
                ..Default::default()
            });
        small_data_strategy.success_rate = 0.9; // High success rate

        let mut large_data_strategy = LearningStrategy::new("batch_gd")
            .with_description("For large datasets")
            .with_hyperparam("lr", 0.01)
            .with_preferred_features(TaskFeatures {
                data_size: Some(10000),
                noise_level: Some(0.0),
                ..Default::default()
            });
        large_data_strategy.success_rate = 0.9;

        meta.register_strategy(small_data_strategy);
        meta.register_strategy(large_data_strategy);

        assert_eq!(meta.num_strategies(), 2);

        // Small data task should select few_shot
        let small_task = TaskFeatures::new().with_data_size(15).with_noise(0.1);
        let selected = meta.select_strategy(&small_task);
        // Either strategy may be selected since both have high success
        // Just verify we get a strategy back
        assert!(selected.is_some(), "Should select a strategy for the task");
    }

    /// Test: Task features describe learning problems.
    ///
    /// What happens:
    /// 1. Create task features for different problems
    /// 2. Features help select appropriate strategies
    #[test]
    fn test_task_features() {
        let classification_task = TaskFeatures::new()
            .with_data_size(1000)
            .with_noise(0.05)
            .with_complexity(0.7)
            .classification()
            .with_domain("nlp");

        assert!(classification_task.is_classification);
        assert_eq!(classification_task.data_size, Some(1000));
        assert!(classification_task.noise_level.unwrap() < 0.1);

        let regression_task = TaskFeatures::new()
            .with_data_size(500)
            .with_noise(0.2)
            .with_complexity(0.3)
            .regression()
            .with_domain("timeseries");

        assert!(!regression_task.is_classification);
        assert_eq!(regression_task.domain.as_deref(), Some("timeseries"));
    }
}