mneme-brain 0.3.0

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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
use std::collections::HashMap;
use std::str::FromStr;

use chrono::{DateTime, Utc};
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use uuid::Uuid;

use crate::store::memory::{
    Importance, MatchType, Memory, MemoryType, Scope, SearchQuery, SearchResult,
};

/// Pesos para cada componente de la búsqueda híbrida.
#[derive(Debug, Clone, Copy)]
pub struct SearchWeights {
    /// Peso para FTS5 (default 0.5).
    pub fts: f64,
    /// Peso para fuzzy matching (default 0.2).
    pub fuzzy: f64,
    /// Peso para búsqueda semántica (default 0.3).
    pub semantic: f64,
}

impl Default for SearchWeights {
    fn default() -> Self {
        Self {
            fts: 0.5,
            fuzzy: 0.2,
            semantic: 0.3,
        }
    }
}

impl SearchWeights {
    /// Renormaliza pesos cuando embeddings están deshabilitados.
    /// fts=0.7, fuzzy=0.3, semantic=0.0.
    pub fn renormalize_without_semantic(&self) -> Self {
        Self {
            fts: 0.7,
            fuzzy: 0.3,
            semantic: 0.0,
        }
    }
}

/// Motor de búsqueda multi-señal con RRF (Reciprocal Rank Fusion).
/// Combina FTS5 + fuzzy + semántica + entidades + recencia usando RRF.
pub struct SearchEngine;

impl Default for SearchEngine {
    fn default() -> Self {
        Self::new()
    }
}

/// Resultado individual de un matcher.
struct RankedSignal {
    memory_id: Uuid,
    rank: usize,
    /// Pre-computed score for debugging/logging purposes.
    #[allow(dead_code)]
    score: f64,
}

/// Constante RRF (k=60 es el valor estándar).
const RRF_K: f64 = 60.0;

impl SearchEngine {
    /// Crea un nuevo SearchEngine.
    pub fn new() -> Self {
        Self
    }

    /// Multi-signal search using Reciprocal Rank Fusion.
    ///
    /// Señales:
    /// - FTS5 full-text search
    /// - Fuzzy title matching
    /// - Semantic (cosine) similarity
    /// - Entity name matching
    /// - Temporal recency
    ///
    /// Cada señal produce un ranking. RRF fusiona los rankings en un score final.
    pub fn search(
        &self,
        conn: &rusqlite::Connection,
        query: &SearchQuery,
        _weights: &SearchWeights,
        semantic_scores: Option<&HashMap<Uuid, f32>>,
    ) -> crate::error::Result<Vec<SearchResult>> {
        let mut all_signals: Vec<RankedSignal> = Vec::new();
        let mut seen_ids: std::collections::HashSet<Uuid> = std::collections::HashSet::new();

        // 1. FTS5 signal
        if query.text.len() >= 3 {
            let fts_results = self.search_fts(conn, query)?;
            for (rank, result) in fts_results.iter().enumerate() {
                all_signals.push(RankedSignal {
                    memory_id: result.memory.id,
                    rank,
                    score: result.score,
                });
                seen_ids.insert(result.memory.id);
            }
        }

        // 2. Fuzzy signal
        let fuzzy_results = self.search_fuzzy(conn, query)?;
        for (rank, result) in fuzzy_results.iter().enumerate() {
            all_signals.push(RankedSignal {
                memory_id: result.memory.id,
                rank,
                score: result.score,
            });
            seen_ids.insert(result.memory.id);
        }

        // 3. Semantic signal
        if let Some(scores) = semantic_scores {
            let mut semantic_ranked: Vec<(Uuid, f32)> =
                scores.iter().map(|(id, s)| (*id, *s)).collect();
            semantic_ranked
                .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
            for (rank, (id, _)) in semantic_ranked.iter().enumerate() {
                all_signals.push(RankedSignal {
                    memory_id: *id,
                    rank,
                    score: f64::from(*scores.get(id).unwrap_or(&0.0)),
                });
                seen_ids.insert(*id);
            }
        }

        // 4. Entity signal: search entity names matching the query
        let entity_matches = Self::search_entities(conn, &query.text, query.project.as_deref());
        for (rank, memory_id) in entity_matches.iter().enumerate() {
            all_signals.push(RankedSignal {
                memory_id: *memory_id,
                rank,
                score: 1.0,
            });
            seen_ids.insert(*memory_id);
        }

        // 5. Temporal recency signal: prefer recently accessed
        let recency_results = self.search_recency(conn, query);
        for (rank, memory_id) in recency_results.iter().enumerate() {
            if seen_ids.contains(memory_id) {
                all_signals.push(RankedSignal {
                    memory_id: *memory_id,
                    rank,
                    score: 1.0,
                });
            }
        }

        // Apply RRF: for each unique memory, sum 1/(k + rank) across all signals
        let mut rrf_scores: HashMap<Uuid, f64> = HashMap::new();
        for signal in &all_signals {
            let entry = rrf_scores.entry(signal.memory_id).or_insert(0.0);
            *entry += 1.0 / (RRF_K + signal.rank as f64);
        }

        // Build full Memory objects for scored IDs
        let mut filtered: Vec<SearchResult> = Vec::new();
        for (id, rrf_score) in &rrf_scores {
            if let Ok(Some(memory)) = Self::get_memory(conn, *id) {
                // Apply filters
                if let Some(memory_type) = &query.memory_type {
                    if &memory.memory_type != memory_type {
                        continue;
                    }
                }
                if let Some(importance) = &query.importance {
                    if &memory.importance != importance {
                        continue;
                    }
                }
                if !query.tags.is_empty() && !query.tags.iter().all(|tag| memory.tags.contains(tag))
                {
                    continue;
                }

                let cosine = semantic_scores.and_then(|s| s.get(id).copied());
                let match_type = Self::best_match_type(&all_signals, *id);

                // Apply importance boost
                let final_score = rrf_score * memory.importance.boost_factor();

                filtered.push(SearchResult {
                    memory,
                    score: final_score,
                    snippet: None,
                    match_type,
                    cosine_score: cosine,
                });
            }
        }

        // Sort by score DESC
        filtered.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        filtered.truncate(query.limit as usize);

        // Extract snippets if requested
        if query.include_snippet {
            for result in &mut filtered {
                result.snippet = self.extract_snippet(&result.memory.content, &query.text);
            }
        }

        Ok(filtered)
    }

    /// Search for memories whose entities match the query text.
    fn search_entities(
        conn: &rusqlite::Connection,
        query: &str,
        project: Option<&str>,
    ) -> Vec<Uuid> {
        let like_pattern = format!("%{}%", query);
        let mut results = Vec::new();

        let sql = if let Some(_proj) = project {
            "SELECT DISTINCT e.memory_id FROM memory_entities e
             JOIN memories m ON m.id = e.memory_id
             WHERE e.entity_name LIKE ?1 AND m.project = ?2 AND m.deleted_at IS NULL
             LIMIT 20"
        } else {
            "SELECT DISTINCT e.memory_id FROM memory_entities e
             JOIN memories m ON m.id = e.memory_id
             WHERE e.entity_name LIKE ?1 AND m.deleted_at IS NULL
             LIMIT 20"
        };

        if let Some(proj) = project {
            if let Ok(mut stmt) = conn.prepare(sql) {
                if let Ok(rows) = stmt.query_map(rusqlite::params![like_pattern, proj], |row| {
                    row.get::<_, String>(0)
                }) {
                    for id_str in rows.flatten() {
                        if let Ok(id) = Uuid::parse_str(&id_str) {
                            results.push(id);
                        }
                    }
                }
            }
        } else {
            if let Ok(mut stmt) = conn.prepare(sql) {
                if let Ok(rows) = stmt.query_map(rusqlite::params![like_pattern], |row| {
                    row.get::<_, String>(0)
                }) {
                    for id_str in rows.flatten() {
                        if let Ok(id) = Uuid::parse_str(&id_str) {
                            results.push(id);
                        }
                    }
                }
            }
        }

        results
    }

    /// Temporal recency signal: most recently accessed/updated memories.
    fn search_recency(&self, conn: &rusqlite::Connection, query: &SearchQuery) -> Vec<Uuid> {
        let mut results = Vec::new();
        let limit_i64 = (query.limit * 2) as i64;

        // Use two separate blocks to avoid Rust type incompatibility
        if let Some(ref project) = query.project {
            let sql = "SELECT id FROM memories
                       WHERE project = ?1 AND deleted_at IS NULL
                       ORDER BY last_accessed_at IS NULL, last_accessed_at DESC, updated_at DESC
                       LIMIT ?2";
            if let Ok(mut stmt) = conn.prepare(sql) {
                if let Ok(rows) = stmt.query_map(rusqlite::params![project, limit_i64], |row| {
                    row.get::<_, String>(0)
                }) {
                    for id_str in rows.flatten() {
                        if let Ok(id) = Uuid::parse_str(&id_str) {
                            results.push(id);
                        }
                    }
                }
            }
        } else {
            let sql = "SELECT id FROM memories
                       WHERE deleted_at IS NULL
                       ORDER BY last_accessed_at IS NULL, last_accessed_at DESC, updated_at DESC
                       LIMIT ?1";
            if let Ok(mut stmt) = conn.prepare(sql) {
                if let Ok(rows) =
                    stmt.query_map(rusqlite::params![limit_i64], |row| row.get::<_, String>(0))
                {
                    for id_str in rows.flatten() {
                        if let Ok(id) = Uuid::parse_str(&id_str) {
                            results.push(id);
                        }
                    }
                }
            }
        }

        results
    }

    /// Determina el mejor match type para una memoria basado en sus señales.
    fn best_match_type(signals: &[RankedSignal], memory_id: Uuid) -> MatchType {
        // Find the signal with highest score
        let best = signals
            .iter()
            .filter(|s| s.memory_id == memory_id)
            .min_by_key(|s| s.rank);

        match best {
            Some(s) if s.rank < 10 => MatchType::Fts,
            Some(_) => MatchType::Fuzzy,
            None => MatchType::Fts,
        }
    }

    /// Retrieves a single memory by ID from the connection.
    fn get_memory(conn: &rusqlite::Connection, id: Uuid) -> rusqlite::Result<Option<Memory>> {
        let mut stmt = conn.prepare(
            "SELECT id, project, scope, title, content, what, why, context, learned,
             memory_type, importance, tags, topic_key, access_count, revision_count,
             duplicate_count, normalized_hash, created_at, updated_at, last_accessed_at, last_seen_at, deleted_at,
             deprecated_at, deprecated_reason, supersedes_id, context_inject_count, origin_peer,
             is_encrypted, encrypted_for, valid_from, valid_until, provenance
             FROM memories WHERE id = ?1 AND deleted_at IS NULL"
        )?;

        let result = stmt.query_row(rusqlite::params![id.to_string()], |row| {
            Ok(Memory {
                id: Uuid::parse_str(&row.get::<_, String>(0)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                project: row.get(1)?,
                scope: Scope::from_str(&row.get::<_, String>(2)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        2,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                title: row.get(3)?,
                content: row.get(4)?,
                what: row.get(5)?,
                why: row.get(6)?,
                context: row.get(7)?,
                learned: row.get(8)?,
                memory_type: MemoryType::from_str(&row.get::<_, String>(9)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        9,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                importance: Importance::from_str(&row.get::<_, String>(10)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        10,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                tags: serde_json::from_str(&row.get::<_, String>(11)?).unwrap_or_default(),
                topic_key: row.get(12)?,
                access_count: row.get(13)?,
                revision_count: row.get(14)?,
                duplicate_count: row.get(15)?,
                normalized_hash: row.get(16)?,
                created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(17)?)
                    .map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            17,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?
                    .with_timezone(&Utc),
                updated_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(18)?)
                    .map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            18,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?
                    .with_timezone(&Utc),
                last_accessed_at: row.get::<_, Option<String>>(19)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                last_seen_at: row.get::<_, Option<String>>(20)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                deleted_at: row.get::<_, Option<String>>(21)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                deprecated_at: row.get::<_, Option<String>>(22)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                deprecated_reason: row.get(23)?,
                supersedes_id: row.get(24)?,
                context_inject_count: row.get(25)?,
                origin_peer: row.get(26)?,
                is_encrypted: row
                    .get::<_, Option<bool>>(27)
                    .unwrap_or(Some(false))
                    .unwrap_or(false),
                encrypted_for: row.get::<_, Option<String>>(28).unwrap_or(None),
                valid_from: row.get::<_, Option<String>>(29)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                valid_until: row.get::<_, Option<String>>(30)?.and_then(|s| {
                    DateTime::parse_from_rfc3339(&s)
                        .ok()
                        .map(|d| d.with_timezone(&Utc))
                }),
                provenance: row.get(31)?,
            })
        });

        match result {
            Ok(memory) => Ok(Some(memory)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e),
        }
    }

    fn search_fts(
        &self,
        conn: &rusqlite::Connection,
        query: &SearchQuery,
    ) -> crate::error::Result<Vec<SearchResult>> {
        let mut sql = String::from(
            "SELECT m.id, m.project, m.scope, m.title, m.content, m.what, m.why, m.context, m.learned,
             m.memory_type, m.importance, m.tags, m.topic_key, m.access_count, m.revision_count,
             m.duplicate_count, m.normalized_hash, m.created_at, m.updated_at, m.last_accessed_at, m.last_seen_at, m.deleted_at,
             0.0 as rank_score
             FROM memories_fts fts
             JOIN memories m ON m.rowid = fts.rowid
             WHERE memories_fts MATCH ?1 AND m.deleted_at IS NULL",
        );

        let mut params: Vec<&dyn rusqlite::ToSql> = Vec::new();
        params.push(&query.text);

        if let Some(project) = &query.project {
            sql.push_str(" AND m.project = ?");
            params.push(project);
        }

        sql.push_str(" ORDER BY m.updated_at DESC LIMIT ?");
        let limit = query.limit as i64;
        params.push(&limit);

        let mut stmt = conn.prepare(&sql)?;
        let rows = stmt.query_map(params.as_slice(), |row| {
            let _rank: f64 = row.get(22)?;
            let score = 1.0; // FTS match gets base score 1.0

            Ok(SearchResult {
                memory: Memory {
                    id: Uuid::parse_str(&row.get::<_, String>(0)?).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            0,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?,
                    project: row.get(1)?,
                    scope: Scope::from_str(&row.get::<_, String>(2)?).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            2,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?,
                    title: row.get(3)?,
                    content: row.get(4)?,
                    what: row.get(5)?,
                    why: row.get(6)?,
                    context: row.get(7)?,
                    learned: row.get(8)?,
                    memory_type: MemoryType::from_str(&row.get::<_, String>(9)?).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            9,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?,
                    importance: Importance::from_str(&row.get::<_, String>(10)?).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            10,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?,
                    tags: serde_json::from_str(&row.get::<_, String>(11)?).unwrap_or_default(),
                    topic_key: row.get(12)?,
                    access_count: row.get(13)?,
                    revision_count: row.get(14)?,
                    duplicate_count: row.get(15)?,
                    normalized_hash: row.get(16)?,
                    created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(17)?)
                        .map_err(|e| {
                            rusqlite::Error::FromSqlConversionFailure(
                                17,
                                rusqlite::types::Type::Text,
                                Box::new(e),
                            )
                        })?
                        .with_timezone(&Utc),
                    updated_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(18)?)
                        .map_err(|e| {
                            rusqlite::Error::FromSqlConversionFailure(
                                18,
                                rusqlite::types::Type::Text,
                                Box::new(e),
                            )
                        })?
                        .with_timezone(&Utc),
                    last_accessed_at: row
                        .get::<_, Option<String>>(19)?
                        .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                        .map(|d| d.with_timezone(&Utc)),
                    last_seen_at: row
                        .get::<_, Option<String>>(20)?
                        .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                        .map(|d| d.with_timezone(&Utc)),
                    deleted_at: row
                        .get::<_, Option<String>>(21)?
                        .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                        .map(|d| d.with_timezone(&Utc)),
                    deprecated_at: None,
                    deprecated_reason: None,
                    supersedes_id: None,
                    context_inject_count: 0,
                    origin_peer: None,
                    is_encrypted: false,
                    encrypted_for: None,
                    valid_from: None,
                    valid_until: None,
                    provenance: None,
                },
                score,
                snippet: None,
                match_type: MatchType::Fts,
                cosine_score: None,
            })
        })?;

        let mut results = Vec::new();
        for row in rows {
            results.push(row?);
        }
        Ok(results)
    }

    fn search_fuzzy(
        &self,
        conn: &rusqlite::Connection,
        query: &SearchQuery,
    ) -> crate::error::Result<Vec<SearchResult>> {
        let matcher = SkimMatcherV2::default();

        let mut sql = String::from(
            "SELECT id, project, scope, title, content, what, why, context, learned,
             memory_type, importance, tags, topic_key, access_count, revision_count,
             duplicate_count, normalized_hash, created_at, updated_at, last_accessed_at, last_seen_at, deleted_at
             FROM memories WHERE deleted_at IS NULL",
        );
        let mut params: Vec<&dyn rusqlite::ToSql> = Vec::new();

        if let Some(project) = &query.project {
            sql.push_str(" AND project = ?");
            params.push(project);
        }

        let mut stmt = conn.prepare(&sql)?;
        let rows = stmt.query_map(params.as_slice(), |row| {
            Ok(Memory {
                id: Uuid::parse_str(&row.get::<_, String>(0)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                project: row.get(1)?,
                scope: Scope::from_str(&row.get::<_, String>(2)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        2,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                title: row.get(3)?,
                content: row.get(4)?,
                what: row.get(5)?,
                why: row.get(6)?,
                context: row.get(7)?,
                learned: row.get(8)?,
                memory_type: MemoryType::from_str(&row.get::<_, String>(9)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        9,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                importance: Importance::from_str(&row.get::<_, String>(10)?).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        10,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?,
                tags: serde_json::from_str(&row.get::<_, String>(11)?).unwrap_or_default(),
                topic_key: row.get(12)?,
                access_count: row.get(13)?,
                revision_count: row.get(14)?,
                duplicate_count: row.get(15)?,
                normalized_hash: row.get(16)?,
                created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(17)?)
                    .map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            17,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?
                    .with_timezone(&Utc),
                updated_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(18)?)
                    .map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            18,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?
                    .with_timezone(&Utc),
                last_accessed_at: row
                    .get::<_, Option<String>>(19)?
                    .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                    .map(|d| d.with_timezone(&Utc)),
                last_seen_at: row
                    .get::<_, Option<String>>(20)?
                    .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                    .map(|d| d.with_timezone(&Utc)),
                deleted_at: row
                    .get::<_, Option<String>>(21)?
                    .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                    .map(|d| d.with_timezone(&Utc)),
                deprecated_at: None,
                deprecated_reason: None,
                supersedes_id: None,
                context_inject_count: 0,
                origin_peer: None,
                is_encrypted: false,
                encrypted_for: None,
                valid_from: None,
                valid_until: None,
                provenance: None,
            })
        })?;

        let mut results = Vec::new();
        for row in rows {
            let memory = row?;
            if let Some(score) = matcher.fuzzy_match(&memory.title, &query.text) {
                let normalized_score = (score as f64) / 100.0; // Normalize to ~0-1
                results.push(SearchResult {
                    memory,
                    score: normalized_score,
                    snippet: None,
                    match_type: MatchType::Fuzzy,
                    cosine_score: None,
                });
            }
        }

        // Sort and truncate
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(query.limit as usize);

        Ok(results)
    }

    fn extract_snippet(&self, content: &str, query: &str) -> Option<String> {
        let lower_content = content.to_lowercase();
        let lower_query = query.to_lowercase();

        if let Some(pos) = lower_content.find(&lower_query) {
            let start = pos.saturating_sub(50);
            let end = (pos + query.len() + 50).min(content.len());
            Some(content[start..end].to_string())
        } else {
            Some(content[..content.len().min(100)].to_string())
        }
    }
}