femind 0.2.0

Pluggable, feature-gated memory engine for AI agent applications
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
use rusqlite::params;

use crate::error::Result;
use crate::storage::Database;

/// Result from an FTS5 keyword search.
#[derive(Debug, Clone)]
pub struct FtsResult {
    /// Memory row ID.
    pub memory_id: i64,
    /// BM25 relevance score (lower = more relevant in SQLite FTS5).
    /// Negated so higher = better (consistent with other scoring).
    pub score: f32,
}

/// FTS5 full-text search with Porter stemming and BM25 ranking.
pub struct FtsSearch;

impl FtsSearch {
    /// Search memories by keyword query.
    ///
    /// Uses FTS5 `MATCH` with Porter stemming (configured at table creation).
    /// Returns results ranked by BM25 score, limited to `limit` results.
    ///
    /// The query is passed directly to FTS5 — consumers can use FTS5 syntax:
    /// - Simple terms: `"authentication error"`
    /// - Phrase: `"\"exact phrase\""`
    /// - Boolean: `"auth AND error"`, `"auth OR login"`
    /// - Prefix: `"auth*"`
    /// - Column filter: `"category:error"`
    pub fn search(
        db: &Database,
        query: &str,
        limit: usize,
        category_filter: Option<&str>,
        memory_type_filter: Option<&str>,
    ) -> Result<Vec<FtsResult>> {
        Self::search_with_tiers(db, query, limit, category_filter, memory_type_filter, None)
    }

    /// Search with optional tier filtering (AND mode — all terms must match).
    pub fn search_with_tiers(
        db: &Database,
        query: &str,
        limit: usize,
        category_filter: Option<&str>,
        memory_type_filter: Option<&str>,
        min_tier: Option<i32>,
    ) -> Result<Vec<FtsResult>> {
        let sanitized = sanitize_fts5_query(query);
        Self::execute_fts(
            db,
            &sanitized,
            limit,
            category_filter,
            memory_type_filter,
            min_tier,
        )
    }

    /// Search using OR mode with stop-word removal — returns partial matches ranked by BM25.
    ///
    /// 1. Sanitizes the query (strip special chars, hyphens)
    /// 2. Removes stop words (the, is, how, many, did, etc.)
    /// 3. Joins remaining terms with OR for partial matching
    ///
    /// Much better for long natural language queries where AND yields zero results.
    pub fn search_or_mode(
        db: &Database,
        query: &str,
        limit: usize,
        category_filter: Option<&str>,
        memory_type_filter: Option<&str>,
        min_tier: Option<i32>,
    ) -> Result<Vec<FtsResult>> {
        let sanitized = sanitize_fts5_query(query);
        let stripped = strip_stop_words(&sanitized);
        let or_query = to_or_query(&stripped);
        Self::execute_fts(
            db,
            &or_query,
            limit,
            category_filter,
            memory_type_filter,
            min_tier,
        )
    }

    /// Execute the FTS5 query against the database.
    fn execute_fts(
        db: &Database,
        fts_query: &str,
        limit: usize,
        category_filter: Option<&str>,
        memory_type_filter: Option<&str>,
        min_tier: Option<i32>,
    ) -> Result<Vec<FtsResult>> {
        if fts_query.trim().is_empty() {
            return Ok(Vec::new());
        }

        db.with_reader(|conn| {
            let mut results = Vec::new();

            let sql = "SELECT m.id, -rank AS score
                 FROM memories_fts fts
                 JOIN memories m ON m.id = fts.rowid
                 WHERE memories_fts MATCH ?1
                   AND (?2 IS NULL OR m.category = ?2)
                   AND (?3 IS NULL OR m.memory_type = ?3)
                   AND (?4 IS NULL OR m.tier >= ?4)
                 ORDER BY rank
                 LIMIT ?5";

            let mut stmt = conn.prepare(sql)?;
            let rows = stmt.query_map(
                params![
                    fts_query,
                    category_filter,
                    memory_type_filter,
                    min_tier,
                    limit as i64
                ],
                |row| {
                    Ok(FtsResult {
                        memory_id: row.get(0)?,
                        score: row.get(1)?,
                    })
                },
            )?;

            for row in rows {
                results.push(row?);
            }

            Ok(results)
        })
    }

    /// Search with an over-fetch multiplier (for RRF merge).
    ///
    /// Returns `limit * multiplier` results to give RRF more candidates to work with.
    pub fn search_overfetch(
        db: &Database,
        query: &str,
        limit: usize,
        multiplier: usize,
    ) -> Result<Vec<FtsResult>> {
        Self::search(db, query, limit * multiplier, None, None)
    }
}

/// Sanitize a query string for FTS5 — remove characters that FTS5 interprets as syntax.
///
/// FTS5 special characters: `*`, `"`, `(`, `)`, `:`, `^`, `{`, `}`, `+`, `-`, `~`, `?`
/// We strip them to prevent syntax errors when the query comes from user input.
fn sanitize_fts5_query(query: &str) -> String {
    let mut result = String::with_capacity(query.len());
    for ch in query.chars() {
        match ch {
            '*' | '"' | '(' | ')' | ':' | '^' | '{' | '}' | '+' | '~' | '?' | ',' | '.' | '!'
            | ';' | '\'' | '/' | '\\' | '[' | ']' | '<' | '>' | '&' | '#' | '@' | '=' | '$'
            | '%' | '`' | '|' => {
                result.push(' ');
            }
            '-' => {
                // Replace ALL hyphens with spaces — FTS5 interprets "word-other"
                // as a column filter (like "word:other"), causing "no such column" errors.
                result.push(' ');
            }
            _ => result.push(ch),
        }
    }
    // Collapse multiple spaces and trim
    let collapsed: String = result.split_whitespace().collect::<Vec<_>>().join(" ");
    if collapsed.is_empty() {
        return String::new();
    }
    collapsed
}

/// Convert a sanitized query into OR-mode: "word1 word2 word3" → "word1 OR word2 OR word3".
fn to_or_query(sanitized: &str) -> String {
    let terms: Vec<&str> = sanitized.split_whitespace().collect();
    if terms.len() <= 1 {
        return sanitized.to_string();
    }
    terms.join(" OR ")
}

/// English stop words — ONLY function words (no content-bearing verbs/nouns).
///
/// Categories: articles, copulas, auxiliaries, modals, pronouns, demonstratives,
/// conjunctions, question words, prepositions, adverbs of degree/frequency.
///
/// Deliberately excludes common verbs (go, get, make, find, etc.) because they
/// carry meaning in questions like "What did I find?" or "Where did I go?"
const STOP_WORDS: &[&str] = &[
    // Articles
    "a", "an", "the", // Copulas & auxiliaries
    "is", "are", "was", "were", "am", "be", "been", "being", "do", "did", "does", "have", "has",
    "had", // Modals
    "will", "would", "could", "should", "can", "may", "might", "shall", "must",
    // Pronouns
    "i", "me", "my", "mine", "we", "us", "our", "ours", "you", "your", "yours", "he", "him", "his",
    "she", "her", "hers", "it", "its", "they", "them", "their", "theirs",
    // Demonstratives
    "this", "that", "these", "those", // Conjunctions
    "and", "or", "but", "nor", "so", "if", "then", "than",
    // Question words (removed from FTS5 queries, but kept for embedding)
    "how", "what", "when", "where", "which", "who", "whom", "why", // Prepositions
    "in", "on", "at", "to", "for", "of", "with", "from", "by", "about", "into", "up", "out", "off",
    "over", "under", "between", "through", "during", "before", "after",
    // Adverbs / degree words
    "not", "no", "very", "just", "also", "too", "only", "there", "here", "as",
];

/// Remove stop words from a query, keeping only content-bearing terms.
///
/// Returns the filtered query. If ALL words are stop words, returns the
/// original query to avoid an empty search.
pub fn strip_stop_words(query: &str) -> String {
    let terms: Vec<&str> = query
        .split_whitespace()
        .filter(|w| !STOP_WORDS.contains(&w.to_lowercase().as_str()))
        .collect();

    if terms.is_empty() {
        // All words were stop words — return original to avoid empty query
        return query.to_string();
    }
    terms.join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::MemoryStore;
    use crate::storage::migrations;
    use crate::traits::{MemoryRecord, MemoryType};
    use chrono::Utc;

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    struct TestMem {
        id: Option<i64>,
        text: String,
        category: Option<String>,
        mem_type: String,
        created_at: chrono::DateTime<Utc>,
    }

    impl MemoryRecord for TestMem {
        fn id(&self) -> Option<i64> {
            self.id
        }
        fn searchable_text(&self) -> String {
            self.text.clone()
        }
        fn memory_type(&self) -> MemoryType {
            MemoryType::from_str(&self.mem_type).unwrap_or(MemoryType::Episodic)
        }
        fn created_at(&self) -> chrono::DateTime<Utc> {
            self.created_at
        }
        fn category(&self) -> Option<&str> {
            self.category.as_deref()
        }
    }

    fn setup() -> Database {
        let db = Database::open_in_memory().expect("open failed");
        db.with_writer(|conn| {
            migrations::migrate(conn)?;
            Ok(())
        })
        .expect("migrate failed");
        db
    }

    fn insert(db: &Database, text: &str, category: Option<&str>, mem_type: &str) {
        let store = MemoryStore::<TestMem>::new();
        let record = TestMem {
            id: None,
            text: text.to_string(),
            category: category.map(String::from),
            mem_type: mem_type.to_string(),
            created_at: Utc::now(),
        };
        store.store(db, &record).expect("store failed");
    }

    #[test]
    fn basic_keyword_search() {
        let db = setup();
        insert(
            &db,
            "authentication failed with JWT token",
            None,
            "procedural",
        );
        insert(&db, "database connection timeout error", None, "episodic");
        insert(
            &db,
            "build succeeded after fixing imports",
            None,
            "episodic",
        );

        let results =
            FtsSearch::search(&db, "authentication", 10, None, None).expect("search failed");
        assert_eq!(results.len(), 1);
        assert!(results[0].score > 0.0);
    }

    #[test]
    fn porter_stemming() {
        let db = setup();
        insert(&db, "authentication failed", None, "semantic");
        insert(&db, "the user authenticated successfully", None, "semantic");

        // "authenticate" should match both via Porter stemming
        let results =
            FtsSearch::search(&db, "authenticate", 10, None, None).expect("search failed");
        assert_eq!(results.len(), 2, "Porter stemming should match inflections");
    }

    #[test]
    fn empty_query() {
        let db = setup();
        insert(&db, "some memory", None, "semantic");

        let results = FtsSearch::search(&db, "", 10, None, None).expect("search failed");
        assert!(results.is_empty());

        let results = FtsSearch::search(&db, "   ", 10, None, None).expect("search failed");
        assert!(results.is_empty());
    }

    #[test]
    fn no_matches() {
        let db = setup();
        insert(&db, "authentication failed", None, "semantic");

        let results = FtsSearch::search(&db, "xyzzyplugh", 10, None, None).expect("search failed");
        assert!(results.is_empty());
    }

    #[test]
    fn category_filter() {
        let db = setup();
        insert(&db, "auth error in login", Some("error"), "procedural");
        insert(
            &db,
            "auth flow redesign decision",
            Some("decision"),
            "semantic",
        );

        let results =
            FtsSearch::search(&db, "auth", 10, Some("error"), None).expect("search failed");
        assert_eq!(results.len(), 1);

        let results =
            FtsSearch::search(&db, "auth", 10, Some("decision"), None).expect("search failed");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn memory_type_filter() {
        let db = setup();
        insert(&db, "build failed with error", None, "episodic");
        insert(&db, "build failures are caused by deps", None, "semantic");

        let results =
            FtsSearch::search(&db, "build", 10, None, Some("episodic")).expect("search failed");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn limit_respected() {
        let db = setup();
        for i in 0..20 {
            insert(
                &db,
                &format!("memory about testing item {i}"),
                None,
                "semantic",
            );
        }

        let results = FtsSearch::search(&db, "testing", 5, None, None).expect("search failed");
        assert_eq!(results.len(), 5);
    }

    #[test]
    fn results_ranked_by_bm25() {
        let db = setup();
        // More relevant: term appears multiple times
        insert(
            &db,
            "error error error in authentication",
            None,
            "procedural",
        );
        // Less relevant: term appears once
        insert(&db, "minor error in logging", None, "episodic");

        let results = FtsSearch::search(&db, "error", 10, None, None).expect("search failed");
        assert_eq!(results.len(), 2);
        // First result should have higher score (more relevant)
        assert!(results[0].score >= results[1].score);
    }

    #[test]
    fn overfetch() {
        let db = setup();
        for i in 0..20 {
            insert(&db, &format!("test memory number {i}"), None, "semantic");
        }

        let results = FtsSearch::search_overfetch(&db, "test", 5, 3).expect("search failed");
        assert_eq!(results.len(), 15); // 5 * 3
    }

    #[test]
    fn hyphenated_queries_dont_error() {
        let db = setup();
        insert(
            &db,
            "I participated in faith related activities",
            None,
            "episodic",
        );
        insert(
            &db,
            "The well known scientist published a paper",
            None,
            "semantic",
        );
        insert(
            &db,
            "The self driving car navigated the highway",
            None,
            "procedural",
        );

        // These hyphenated queries previously caused "no such column" errors
        // because FTS5 interprets "word-other" as a column filter.
        let queries = [
            "faith-related activities",
            "well-known scientist",
            "self-driving car",
            "How many faith-related events happened?",
            "state-of-the-art technology",
        ];

        for query in &queries {
            let result = FtsSearch::search(&db, query, 10, None, None);
            assert!(
                result.is_ok(),
                "Query '{query}' should not error: {:?}",
                result.err()
            );
        }

        // Verify "faith related" still matches after hyphen→space conversion
        let results =
            FtsSearch::search(&db, "faith-related", 10, None, None).expect("search failed");
        assert!(!results.is_empty(), "should find 'faith related' content");
    }

    #[test]
    fn sanitizer_handles_all_special_chars() {
        let db = setup();
        insert(&db, "test content for sanitizer", None, "semantic");

        // Queries with every kind of special character
        let queries = [
            "test*",
            "\"test\"",
            "(test)",
            "test:content",
            "test^2",
            "{test}",
            "test+content",
            "~test",
            "test?",
            "test,content",
            "test.content",
            "test!content",
            "test;content",
            "test'content",
            "test/content",
            "test\\content",
            "test[0]",
            "<test>",
            "test&content",
            "#test",
            "@test",
            "test=content",
        ];

        for query in &queries {
            let result = FtsSearch::search(&db, query, 10, None, None);
            assert!(
                result.is_ok(),
                "Query '{query}' should not error: {:?}",
                result.err()
            );
        }
    }

    #[test]
    fn sanitizer_output() {
        assert_eq!(sanitize_fts5_query("faith-related"), "faith related");
        assert_eq!(sanitize_fts5_query("self-driving car"), "self driving car");
        assert_eq!(sanitize_fts5_query("test:column"), "test column");
        assert_eq!(sanitize_fts5_query("  hello  world  "), "hello world");
        assert_eq!(sanitize_fts5_query("***"), "");
        assert_eq!(sanitize_fts5_query(""), "");
        assert_eq!(sanitize_fts5_query("normal query"), "normal query");
    }

    #[test]
    fn stop_word_removal() {
        // Content verbs/nouns are preserved — only function words stripped
        assert_eq!(
            strip_stop_words("How many days did I spend participating in activities"),
            "many days spend participating activities"
        );
        assert_eq!(
            strip_stop_words("What is my favorite color"),
            "favorite color"
        );
        assert_eq!(strip_stop_words("the cat sat on the mat"), "cat sat mat");
        assert_eq!(
            strip_stop_words("Where did I go last weekend"),
            "go last weekend"
        );
        assert_eq!(
            strip_stop_words("What food do I like to eat"),
            "food like eat"
        );
        // All stop words → return original
        assert_eq!(strip_stop_words("the is a"), "the is a");
        assert_eq!(strip_stop_words(""), "");
        assert_eq!(
            strip_stop_words("authentication JWT token error"),
            "authentication JWT token error"
        );
    }

    #[test]
    fn to_or_query_output() {
        assert_eq!(to_or_query("faith related"), "faith OR related");
        assert_eq!(to_or_query("one two three"), "one OR two OR three");
        assert_eq!(to_or_query("single"), "single");
        assert_eq!(to_or_query(""), "");
    }

    #[test]
    fn or_mode_returns_partial_matches() {
        let db = setup();
        insert(
            &db,
            "authentication failed with JWT token",
            None,
            "procedural",
        );
        insert(&db, "database connection timeout error", None, "episodic");
        insert(
            &db,
            "build succeeded after fixing imports",
            None,
            "episodic",
        );

        // AND mode: long query with many terms returns nothing
        let and_results = FtsSearch::search(
            &db,
            "How many days did I spend fixing authentication errors",
            10,
            None,
            None,
        )
        .expect("search");

        // OR mode: same query returns partial matches
        let or_results = FtsSearch::search_or_mode(
            &db,
            "How many days did I spend fixing authentication errors",
            10,
            None,
            None,
            None,
        )
        .expect("search");

        assert!(
            or_results.len() > and_results.len(),
            "OR mode should find more results than AND: OR={}, AND={}",
            or_results.len(),
            and_results.len()
        );
        assert!(
            !or_results.is_empty(),
            "OR mode should find partial matches"
        );
    }
}