mneme-brain 0.2.5

Persistent memory system for AI coding agents — Rust + SQLite + FTS5 + MCP. Hybrid search, age encryption, CRDT P2P sync, WASM plugins, TUI with knowledge graph.
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
//! Memory benchmarks para evaluar la calidad del retrieval.
//!
//! Inspirado por Mem0's LoCoMo / LongMemEval / BEAM. Permite definir
//! escenarios de prueba (memorias a cargar + preguntas con expected answers)
//! y medir métricas como precision@k, recall@k, MRR, y faithfulness.

use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::store::db::Database;
use crate::store::memory::{CreateMemoryInput, Scope, SearchQuery};

/// Un escenario de benchmark: memorias semilla + preguntas con respuestas esperadas.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkScenario {
    /// Nombre del escenario (ej: "rust-decisions", "auth-patterns").
    pub name: String,
    /// Descripción opcional.
    pub description: Option<String>,
    /// Proyecto al que se cargan las memorias de prueba.
    pub project: String,
    /// Memorias semilla a cargar antes de evaluar.
    #[serde(default)]
    pub seed_memories: Vec<BenchmarkSeedMemory>,
    /// Preguntas de evaluación.
    pub queries: Vec<BenchmarkQuery>,
}

/// Una memoria semilla para el benchmark.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkSeedMemory {
    pub title: String,
    pub content: String,
    pub memory_type: String,
    pub importance: String,
    pub tags: Vec<String>,
}

/// Una pregunta con respuestas esperadas.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkQuery {
    /// El query de búsqueda.
    pub query: String,
    /// IDs de memorias que se esperan encontrar (1-indexed por orden en seed).
    /// Permite expected_memory_id o expected_keywords.
    #[serde(default)]
    pub expected_titles: Vec<String>,
    /// Keywords que deberían aparecer en al menos un resultado top-k.
    #[serde(default)]
    pub expected_keywords: Vec<String>,
    /// Posición esperada (1-indexed) en los resultados. 0 = cualquier posición.
    #[serde(default)]
    pub expected_rank: u32,
    /// Profundidad k para métricas (default 5).
    #[serde(default = "default_k")]
    pub k: u32,
}

fn default_k() -> u32 {
    5
}

/// Resultados de un escenario ejecutado.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    pub scenario_name: String,
    pub total_queries: u32,
    pub metrics: BenchmarkMetrics,
    pub per_query: Vec<QueryResult>,
}

/// Métricas agregadas.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BenchmarkMetrics {
    /// Mean Reciprocal Rank @ k
    pub mrr: f64,
    /// Precision @ k: promedio de (relevantos en top-k) / k
    pub precision_at_k: f64,
    /// Recall @ k: promedio de (relevantos en top-k) / total relevante
    pub recall_at_k: f64,
    /// Hit rate @ k: promedio de queries con al menos 1 relevante en top-k
    pub hit_rate: f64,
    /// F1 @ k: promedio de F1 scores
    pub f1_at_k: f64,
    /// Latencia promedio de búsqueda (ms)
    pub avg_latency_ms: f64,
}

/// Resultado individual de un query.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
    pub query: String,
    pub k: u32,
    pub relevant_found: u32,
    pub total_relevant: u32,
    pub reciprocal_rank: f64,
    pub precision: f64,
    pub recall: f64,
    pub hit: bool,
    pub latency_ms: u64,
    pub top_titles: Vec<String>,
}

/// Runner de benchmarks.
pub struct BenchmarkRunner {
    db: std::sync::Arc<Database>,
}

impl BenchmarkRunner {
    pub fn new(db: std::sync::Arc<Database>) -> Self {
        Self { db }
    }

    /// Carga un escenario desde un archivo TOML o JSON.
    pub fn load_scenario(&self, path: &Path) -> crate::error::Result<BenchmarkScenario> {
        let content = std::fs::read_to_string(path)?;
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        let scenario = if ext == "json" {
            serde_json::from_str(&content)?
        } else {
            // Default to TOML
            toml::from_str(&content)
                .map_err(|e| crate::error::MnemeError::Config(format!("TOML parse error: {}", e)))?
        };
        Ok(scenario)
    }

    /// Ejecuta un escenario: carga memorias, corre queries, mide métricas.
    pub fn run(&self, scenario: &BenchmarkScenario) -> crate::error::Result<BenchmarkResult> {
        // 1. Cargar memorias semilla
        let memories = self.db.memories();
        let project = scenario.project.clone();

        // Limpiar memorias existentes del proyecto (para reproducibilidad)
        let _ = memories.forget_project(&project);

        for seed in &scenario.seed_memories {
            let memory_type = seed
                .memory_type
                .parse()
                .unwrap_or(crate::store::memory::MemoryType::Note);
            let importance = seed
                .importance
                .parse()
                .unwrap_or(crate::store::memory::Importance::Medium);
            let input = CreateMemoryInput {
                project: project.clone(),
                scope: Some(Scope::Project),
                title: seed.title.clone(),
                content: seed.content.clone(),
                what: None,
                why: None,
                context: None,
                learned: None,
                memory_type,
                importance,
                tags: seed.tags.clone(),
                topic_key: None,
                capture_prompt: None,
                encrypt: false,
                valid_from: None,
                valid_until: None,
                provenance: Some("benchmark/seed".to_string()),
            };
            memories.save(input, None, None)?;
        }

        // 2. Ejecutar cada query
        let mut per_query = Vec::new();
        let mut sum_mrr = 0.0;
        let mut sum_precision = 0.0;
        let mut sum_recall = 0.0;
        let mut sum_hit = 0u32;
        let mut sum_f1 = 0.0;
        let mut sum_latency = 0u64;

        for q in &scenario.queries {
            let k = if q.k == 0 { 5 } else { q.k };
            let start = std::time::Instant::now();
            let search_query = SearchQuery {
                text: q.query.clone(),
                project: Some(project.clone()),
                scope: Some(Scope::Project),
                memory_type: None,
                importance: None,
                tags: Vec::new(),
                limit: k,
                include_snippet: false,
                all_projects: false,
            };
            let weights = crate::store::search::SearchWeights::default();
            let results = memories.search(&search_query, &weights, None)?;
            let latency_ms = start.elapsed().as_millis() as u64;

            let top_titles: Vec<String> = results
                .iter()
                .take(k as usize)
                .map(|r| r.memory.title.clone())
                .collect();

            // Calcular relevance
            let mut relevant_found = 0u32;
            let mut reciprocal_rank = 0.0;
            for (idx, r) in results.iter().take(k as usize).enumerate() {
                let title = &r.memory.title;
                let is_relevant = q.expected_titles.iter().any(|t| t == title)
                    || q.expected_keywords.iter().any(|kw| {
                        r.memory.content.to_lowercase().contains(&kw.to_lowercase())
                            || r.memory.title.to_lowercase().contains(&kw.to_lowercase())
                    });
                if is_relevant {
                    relevant_found += 1;
                    if reciprocal_rank == 0.0 {
                        reciprocal_rank = 1.0 / (idx as f64 + 1.0);
                    }
                }
            }

            let total_relevant = if q.expected_titles.is_empty() && q.expected_keywords.is_empty() {
                0
            } else {
                q.expected_titles.len().max(q.expected_keywords.len()) as u32
            };

            let precision = if k > 0 {
                relevant_found as f64 / k as f64
            } else {
                0.0
            };
            let recall = if total_relevant > 0 {
                relevant_found as f64 / total_relevant as f64
            } else if relevant_found > 0 {
                1.0
            } else {
                0.0
            };
            let f1 = if precision + recall > 0.0 {
                2.0 * precision * recall / (precision + recall)
            } else {
                0.0
            };
            let hit = relevant_found > 0;

            sum_mrr += reciprocal_rank;
            sum_precision += precision;
            sum_recall += recall;
            if hit {
                sum_hit += 1;
            }
            sum_f1 += f1;
            sum_latency += latency_ms;

            per_query.push(QueryResult {
                query: q.query.clone(),
                k,
                relevant_found,
                total_relevant,
                reciprocal_rank,
                precision,
                recall,
                hit,
                latency_ms,
                top_titles,
            });
        }

        let total = scenario.queries.len() as f64;
        let metrics = if total > 0.0 {
            BenchmarkMetrics {
                mrr: sum_mrr / total,
                precision_at_k: sum_precision / total,
                recall_at_k: sum_recall / total,
                hit_rate: sum_hit as f64 / total,
                f1_at_k: sum_f1 / total,
                avg_latency_ms: if total > 0.0 {
                    sum_latency as f64 / total
                } else {
                    0.0
                },
            }
        } else {
            BenchmarkMetrics::default()
        };

        Ok(BenchmarkResult {
            scenario_name: scenario.name.clone(),
            total_queries: scenario.queries.len() as u32,
            metrics,
            per_query,
        })
    }
}

/// Genera un escenario de benchmark de ejemplo para Rust.
pub fn example_rust_scenario() -> BenchmarkScenario {
    BenchmarkScenario {
        name: "rust-decisions".to_string(),
        description: Some("Evalúa retrieval de decisiones arquitectónicas sobre Rust".to_string()),
        project: "bench-rust".to_string(),
        seed_memories: vec![
            BenchmarkSeedMemory {
                title: "Async runtime: tokio vs async-std".to_string(),
                content: "Decidimos usar tokio como runtime async por mejor ecosystem, tracing integrado, y amplia adoption en la comunidad Rust.".to_string(),
                memory_type: "decision".to_string(),
                importance: "high".to_string(),
                tags: vec!["rust".to_string(), "async".to_string(), "architecture".to_string()],
            },
            BenchmarkSeedMemory {
                title: "Error handling: anyhow vs thiserror".to_string(),
                content: "Para errores de library usamos thiserror (typed errors), para binarios usamos anyhow (con context).".to_string(),
                memory_type: "decision".to_string(),
                importance: "medium".to_string(),
                tags: vec!["rust".to_string(), "errors".to_string()],
            },
            BenchmarkSeedMemory {
                title: "Web framework: axum vs actix-web".to_string(),
                content: "Elegimos axum por mejor integración con tower ecosystem y menor curva de aprendizaje.".to_string(),
                memory_type: "decision".to_string(),
                importance: "high".to_string(),
                tags: vec!["rust".to_string(), "web".to_string(), "architecture".to_string()],
            },
            BenchmarkSeedMemory {
                title: "Database: rusqlite con FTS5".to_string(),
                content: "Usamos rusqlite con FTS5 para full-text search nativo sin dependencias externas. Embeddings via fastembed ONNX.".to_string(),
                memory_type: "decision".to_string(),
                importance: "high".to_string(),
                tags: vec!["rust".to_string(), "database".to_string(), "sqlite".to_string()],
            },
            BenchmarkSeedMemory {
                title: "CLI: clap con derive".to_string(),
                content: "Usamos clap v4 con derive macros para type-safe argument parsing.".to_string(),
                memory_type: "convention".to_string(),
                importance: "medium".to_string(),
                tags: vec!["rust".to_string(), "cli".to_string()],
            },
        ],
        queries: vec![
            BenchmarkQuery {
                query: "qué runtime async usamos".to_string(),
                expected_titles: vec!["Async runtime: tokio vs async-std".to_string()],
                expected_keywords: vec!["tokio".to_string()],
                expected_rank: 1,
                k: 3,
            },
            BenchmarkQuery {
                query: "cómo manejamos errores".to_string(),
                expected_titles: vec!["Error handling: anyhow vs thiserror".to_string()],
                expected_keywords: vec!["anyhow".to_string(), "thiserror".to_string()],
                expected_rank: 1,
                k: 3,
            },
            BenchmarkQuery {
                query: "framework web".to_string(),
                expected_titles: vec!["Web framework: axum vs actix-web".to_string()],
                expected_keywords: vec!["axum".to_string()],
                expected_rank: 1,
                k: 3,
            },
            BenchmarkQuery {
                query: "base de datos sqlite fulltext".to_string(),
                expected_titles: vec!["Database: rusqlite con FTS5".to_string()],
                expected_keywords: vec!["rusqlite".to_string(), "fts5".to_string()],
                expected_rank: 1,
                k: 3,
            },
            BenchmarkQuery {
                query: "cli argument parsing".to_string(),
                expected_titles: vec!["CLI: clap con derive".to_string()],
                expected_keywords: vec!["clap".to_string()],
                expected_rank: 1,
                k: 3,
            },
        ],
    }
}

/// Reporta los resultados como tabla markdown.
pub fn format_report(result: &BenchmarkResult) -> String {
    let mut out = String::new();
    out.push_str(&format!("# Benchmark: {}\n\n", result.scenario_name));
    out.push_str(&format!("Queries ejecutados: {}\n\n", result.total_queries));
    out.push_str("## Métricas agregadas\n\n");
    out.push_str("| Métrica | Valor |\n|---------|-------|\n");
    out.push_str(&format!("| MRR @ k | {:.4} |\n", result.metrics.mrr));
    out.push_str(&format!(
        "| Precision @ k | {:.4} |\n",
        result.metrics.precision_at_k
    ));
    out.push_str(&format!(
        "| Recall @ k | {:.4} |\n",
        result.metrics.recall_at_k
    ));
    out.push_str(&format!(
        "| Hit Rate @ k | {:.4} |\n",
        result.metrics.hit_rate
    ));
    out.push_str(&format!("| F1 @ k | {:.4} |\n", result.metrics.f1_at_k));
    out.push_str(&format!(
        "| Avg latency (ms) | {:.2} |\n",
        result.metrics.avg_latency_ms
    ));
    out.push_str("\n## Per-query\n\n");
    out.push_str("| Query | k | Rel/Total | P | R | F1 | MRR | Hit | Latency |\n");
    out.push_str("|-------|---|-----------|---|---|---|-----|-----|---------|\n");
    for q in &result.per_query {
        let f1 = if q.precision + q.recall > 0.0 {
            2.0 * q.precision * q.recall / (q.precision + q.recall)
        } else {
            0.0
        };
        out.push_str(&format!(
            "| {} | {} | {}/{} | {:.2} | {:.2} | {:.2} | {:.2} | {} | {}ms |\n",
            truncate(&q.query, 30),
            q.k,
            q.relevant_found,
            q.total_relevant,
            q.precision,
            q.recall,
            f1,
            q.reciprocal_rank,
            if q.hit { "" } else { "" },
            q.latency_ms,
        ));
    }
    out
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max.saturating_sub(1)).collect();
        format!("{}", truncated)
    }
}

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

    #[test]
    fn test_example_scenario_loads() {
        let scenario = example_rust_scenario();
        assert_eq!(scenario.name, "rust-decisions");
        assert_eq!(scenario.seed_memories.len(), 5);
        assert_eq!(scenario.queries.len(), 5);
    }

    #[test]
    fn test_metrics_default() {
        let m = BenchmarkMetrics::default();
        assert_eq!(m.mrr, 0.0);
        assert_eq!(m.precision_at_k, 0.0);
    }

    #[test]
    fn test_truncate() {
        assert_eq!(truncate("short", 10), "short");
        assert_eq!(truncate("a long string here", 10), "a long st…");
    }

    #[test]
    fn test_format_report_includes_metrics() {
        let result = BenchmarkResult {
            scenario_name: "test".to_string(),
            total_queries: 2,
            metrics: BenchmarkMetrics {
                mrr: 0.5,
                precision_at_k: 0.6,
                recall_at_k: 0.7,
                hit_rate: 0.5,
                f1_at_k: 0.65,
                avg_latency_ms: 5.0,
            },
            per_query: vec![],
        };
        let report = format_report(&result);
        assert!(report.contains("# Benchmark: test"));
        assert!(report.contains("MRR @ k | 0.5000"));
        assert!(report.contains("Precision @ k | 0.6000"));
    }
}