context-forge 0.5.0-beta.4

Local-first persistent memory for LLM applications - SQLite + FTS5 BM25 retrieval, recency decay, token-budget context assembly, secret scrubbing, and optional local-LLM distillation.
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use std::path::Path;
use std::sync::Arc;

use r2d2::{CustomizeConnection, Pool};
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::TransactionBehavior;

use crate::entry::ContextEntry;
use crate::storage::schema::{migrate, row_to_entry};
use crate::traits::ContextStorage;

/// Forward-only schema migrations and row-to-entry conversion.
pub mod schema;
/// FTS5-backed `Searcher` implementation.
pub mod searcher;

pub use searcher::SqliteSearcher;

/// Create a paired storage + searcher sharing the same connection pool.
///
/// # Errors
///
/// Returns an error if the database cannot be opened, the connection pool
/// cannot be built, or migrations fail.
pub fn open_storage(
    db_path: &Path,
    max_entries: usize,
) -> crate::Result<(SqliteStorage, SqliteSearcher)> {
    let storage = SqliteStorage::open(db_path, max_entries)?;
    let searcher = SqliteSearcher::new(storage.pool());
    Ok((storage, searcher))
}

#[derive(Debug)]
struct PragmaCustomizer;

impl CustomizeConnection<rusqlite::Connection, rusqlite::Error> for PragmaCustomizer {
    fn on_acquire(
        &self,
        conn: &mut rusqlite::Connection,
    ) -> std::result::Result<(), rusqlite::Error> {
        // busy_timeout must be set before journal_mode=WAL: switching to WAL
        // briefly takes an exclusive lock, and on a fresh database with
        // multiple connections racing the switch, an un-armed busy_timeout
        // means that lock contention fails immediately with SQLITE_BUSY
        // instead of waiting.
        conn.execute_batch(
            "PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;",
        )?;
        Ok(())
    }
}

/// SQLite-backed implementation of [`ContextStorage`].
pub struct SqliteStorage {
    pool: Arc<Pool<SqliteConnectionManager>>,
    max_entries: usize,
}

impl SqliteStorage {
    /// Open (or create) a `SQLite` database at `db_path` and run migrations.
    ///
    /// For `":memory:"`, a single-connection pool is used so that all operations
    /// share the same in-memory database instance.
    ///
    /// # Errors
    ///
    /// Returns an error if the database cannot be opened, the connection
    /// pool cannot be built, or migrations fail.
    pub fn open(db_path: &Path, max_entries: usize) -> crate::Result<Self> {
        let manager = SqliteConnectionManager::file(db_path);
        let mut builder = Pool::builder().connection_customizer(Box::new(PragmaCustomizer));

        // Each `:memory:` connection is a distinct in-memory database.
        // Restrict to a single connection so all callers see the same DB.
        if db_path == Path::new(":memory:") {
            builder = builder.max_size(1);
        } else {
            builder = builder.max_size(4);
        }

        let pool = builder.build(manager)?;

        let conn = pool.get()?;
        migrate(&conn)?;

        Ok(Self {
            pool: Arc::new(pool),
            max_entries,
        })
    }

    /// Return a reference-counted handle to the connection pool so that
    /// [`SqliteSearcher`] can share it.
    #[must_use]
    pub fn pool(&self) -> Arc<Pool<SqliteConnectionManager>> {
        Arc::clone(&self.pool)
    }

    /// Return the current schema version from the database.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection pool or query fails.
    pub fn schema_version(&self) -> crate::Result<i64> {
        let conn = self.pool.get()?;
        let version = conn.query_row(
            "SELECT COALESCE(MAX(version), 0) FROM schema_version",
            [],
            |row| row.get(0),
        )?;
        Ok(version)
    }

    /// Run a WAL checkpoint (TRUNCATE mode) to flush the WAL file.
    ///
    /// Safe to call at any time; no-op if no WAL pages are pending.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection pool or checkpoint pragma fails.
    pub fn checkpoint(&self) -> crate::Result<()> {
        let conn = self.pool.get()?;
        conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        Ok(())
    }
}

impl ContextStorage for SqliteStorage {
    fn save(&self, entry: &ContextEntry) -> crate::Result<()> {
        let mut conn = self.pool.get()?;

        let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;

        // LRU eviction: only evict when inserting a new entry (not replacing
        // an existing ID) and currently at capacity.
        let exists: bool = tx.query_row(
            "SELECT EXISTS(SELECT 1 FROM entries WHERE id = ?1)",
            [&entry.id],
            |r| r.get(0),
        )?;

        if !exists {
            let current_count: i64 =
                tx.query_row("SELECT COUNT(*) FROM entries", [], |r| r.get(0))?;

            let current_count = usize::try_from(current_count).unwrap_or(usize::MAX);
            if current_count >= self.max_entries {
                tx.execute(
                    "DELETE FROM entries WHERE id = (\
                     SELECT id FROM entries ORDER BY timestamp ASC LIMIT 1)",
                    [],
                )?;
            }
        }

        let metadata_json = entry
            .metadata
            .as_ref()
            .map(serde_json::to_string)
            .transpose()
            .map_err(|e| crate::Error::InvalidEntry(format!("metadata is not valid JSON: {e}")))?;

        tx.execute(
            "INSERT OR REPLACE INTO entries (
                id,
                content,
                timestamp,
                kind,
                scope,
                session_id,
                token_count,
                metadata
            ) VALUES (
                ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8
            )",
            rusqlite::params![
                entry.id,
                entry.content,
                entry.timestamp,
                entry.kind,
                entry.scope,
                entry.session_id,
                entry
                    .token_count
                    .map(|v| i64::try_from(v).unwrap_or(i64::MAX)),
                metadata_json,
            ],
        )?;

        tx.commit()?;

        Ok(())
    }

    fn get_top_k(&self, k: usize) -> crate::Result<Vec<ContextEntry>> {
        let conn = self.pool.get()?;
        let mut stmt = conn.prepare(
            "SELECT
                id,
                content,
                timestamp,
                kind,
                scope,
                session_id,
                token_count,
                metadata
             FROM entries
             ORDER BY timestamp DESC
             LIMIT ?1",
        )?;

        let entries = stmt
            .query_map([i64::try_from(k).unwrap_or(i64::MAX)], row_to_entry)?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(entries)
    }

    fn get_all(&self) -> crate::Result<Vec<ContextEntry>> {
        let conn = self.pool.get()?;
        let mut stmt = conn.prepare(
            "SELECT
                id,
                content,
                timestamp,
                kind,
                scope,
                session_id,
                token_count,
                metadata
             FROM entries
             ORDER BY timestamp DESC",
        )?;

        let entries = stmt
            .query_map([], row_to_entry)?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(entries)
    }

    fn delete(&self, id: &str) -> crate::Result<bool> {
        let conn = self.pool.get()?;
        let changes = conn.execute("DELETE FROM entries WHERE id = ?1", [id])?;
        Ok(changes > 0)
    }

    fn clear(&self) -> crate::Result<usize> {
        let conn = self.pool.get()?;
        let changes = conn.execute("DELETE FROM entries", [])?;
        Ok(changes)
    }

    fn clear_scope(&self, scope: &str) -> crate::Result<usize> {
        let conn = self.pool.get()?;
        let changes = conn.execute("DELETE FROM entries WHERE scope = ?1", [scope])?;
        Ok(changes)
    }

    fn count(&self) -> crate::Result<usize> {
        let conn = self.pool.get()?;
        let count: i64 = conn.query_row("SELECT COUNT(*) FROM entries", [], |r| r.get(0))?;
        Ok(usize::try_from(count).unwrap_or(usize::MAX))
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::Path;
    use std::time::{SystemTime, UNIX_EPOCH};

    use rusqlite::Connection;

    use crate::engine::MATCH_ALL_QUERY;
    use crate::entry::{kind, ContextEntry};
    use crate::storage::{open_storage, SqliteStorage};
    use crate::traits::{ContextStorage, Searcher};

    fn temp_db_path(name: &str) -> std::path::PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock drift before unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("cf-storage-{name}-{nanos}.db"))
    }

    fn make_entry(id: &str, content: &str, timestamp: i64, kind: &str) -> ContextEntry {
        ContextEntry {
            id: id.into(),
            content: content.into(),
            timestamp,
            kind: kind.to_owned(),
            scope: None,
            session_id: None,
            token_count: None,
            metadata: None,
        }
    }

    fn make_scoped_entry(id: &str, content: &str, timestamp: i64, scope: &str) -> ContextEntry {
        ContextEntry {
            id: id.into(),
            content: content.into(),
            timestamp,
            kind: kind::MANUAL.to_owned(),
            scope: Some(scope.to_owned()),
            session_id: None,
            token_count: None,
            metadata: None,
        }
    }

    #[test]
    fn checkpoint_runs_without_error() {
        let dir = tempfile::tempdir().unwrap();
        let storage = SqliteStorage::open(dir.path().join("test.db").as_path(), 100).unwrap();
        assert!(storage.checkpoint().is_ok());
    }

    #[test]
    fn test_save_and_count() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        let entry = make_entry("e1", "hello world", 1000, kind::MANUAL);
        storage.save(&entry).unwrap();
        assert_eq!(storage.count().unwrap(), 1);
    }

    #[test]
    fn test_save_and_get_top_k() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "first", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "second", 200, kind::SNAPSHOT))
            .unwrap();
        storage
            .save(&make_entry("e3", "third", 300, kind::SUMMARY))
            .unwrap();

        let top2 = storage.get_top_k(2).unwrap();
        assert_eq!(top2.len(), 2);
        assert_eq!(top2[0].id, "e3"); // most recent
        assert_eq!(top2[1].id, "e2");
    }

    #[test]
    fn test_save_and_get_all() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "first", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "second", 200, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e3", "third", 300, kind::MANUAL))
            .unwrap();

        let all = storage.get_all().unwrap();
        assert_eq!(all.len(), 3);
        // ordered by timestamp desc
        assert_eq!(all[0].id, "e3");
        assert_eq!(all[1].id, "e2");
        assert_eq!(all[2].id, "e1");
    }

    #[test]
    fn test_delete() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "hello", 1000, kind::MANUAL))
            .unwrap();

        assert!(storage.delete("e1").unwrap());
        assert!(!storage.delete("nonexistent").unwrap());
        assert_eq!(storage.count().unwrap(), 0);
    }

    #[test]
    fn test_clear() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "a", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "b", 200, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e3", "c", 300, kind::MANUAL))
            .unwrap();

        let cleared = storage.clear().unwrap();
        assert_eq!(cleared, 3);
        assert_eq!(storage.count().unwrap(), 0);
    }

    #[test]
    fn test_clear_scope() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_scoped_entry("e1", "a", 100, "scope-a"))
            .unwrap();
        storage
            .save(&make_scoped_entry("e2", "b", 200, "scope-a"))
            .unwrap();
        storage
            .save(&make_scoped_entry("e3", "c", 300, "scope-b"))
            .unwrap();

        let cleared = storage.clear_scope("scope-a").unwrap();
        assert_eq!(cleared, 2);
        assert_eq!(storage.count().unwrap(), 1);

        let all = storage.get_all().unwrap();
        assert_eq!(all[0].id, "e3");
    }

    #[test]
    fn test_clear_scope_no_match() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_scoped_entry("e1", "a", 100, "scope-a"))
            .unwrap();

        let cleared = storage.clear_scope("scope-z").unwrap();
        assert_eq!(cleared, 0);
        assert_eq!(storage.count().unwrap(), 1);
    }

    #[test]
    fn test_lru_eviction() {
        let (storage, _) = open_storage(Path::new(":memory:"), 2).unwrap();
        storage
            .save(&make_entry("e1", "oldest", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "middle", 200, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e3", "newest", 300, kind::MANUAL))
            .unwrap();

        assert_eq!(storage.count().unwrap(), 2);

        let all = storage.get_all().unwrap();
        let ids: Vec<&str> = all.iter().map(|e| e.id.as_str()).collect();
        assert!(
            !ids.contains(&"e1"),
            "oldest entry should have been evicted"
        );
        assert!(ids.contains(&"e2"));
        assert!(ids.contains(&"e3"));
    }

    #[test]
    fn test_fts_search() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry(
                "e1",
                "rust programming language",
                100,
                kind::MANUAL,
            ))
            .unwrap();
        storage
            .save(&make_entry("e2", "python scripting", 200, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e3", "rust borrow checker", 300, kind::MANUAL))
            .unwrap();

        let results = searcher.search("rust", None, 5).unwrap();
        assert_eq!(results.len(), 2);
        // Assert ordering by relevance (highest score first), not absolute values.
        assert!(
            results[0].score >= results[1].score,
            "results should be ordered by descending score"
        );
    }

    #[test]
    fn test_fts_search_no_results() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "hello world", 100, kind::MANUAL))
            .unwrap();

        let results = searcher.search("nonexistent", None, 5).unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn test_fts_search_scoped() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_scoped_entry("e1", "rust programming", 100, "a"))
            .unwrap();
        storage
            .save(&make_scoped_entry("e2", "rust borrow checker", 200, "b"))
            .unwrap();

        let results_a = searcher.search("rust", Some("a"), 5).unwrap();
        assert_eq!(results_a.len(), 1);
        assert_eq!(results_a[0].entry.id, "e1");

        let results_b = searcher.search("rust", Some("b"), 5).unwrap();
        assert_eq!(results_b.len(), 1);
        assert_eq!(results_b[0].entry.id, "e2");

        let results_all = searcher.search("rust", None, 5).unwrap();
        assert_eq!(results_all.len(), 2);
    }

    #[test]
    fn test_v2_migration_idempotent() {
        let storage1 = SqliteStorage::open(Path::new(":memory:"), 100).unwrap();
        let conn = storage1.pool().get().unwrap();
        // Running migrate a second time on the same connection should succeed.
        crate::storage::schema::migrate(&conn).unwrap();
    }

    #[test]
    fn test_v1_to_v3_migration() {
        let db_path = temp_db_path("v1-to-v3");

        {
            let conn = Connection::open(&db_path).unwrap();
            conn.execute_batch(crate::storage::schema::SCHEMA_V1)
                .unwrap();
            conn.execute_batch(
                "CREATE TABLE IF NOT EXISTS schema_version (id INTEGER PRIMARY KEY CHECK(id = 1), version INTEGER NOT NULL)",
            )
            .unwrap();
            conn.execute(
                "INSERT OR REPLACE INTO schema_version (id, version) VALUES (1, 1)",
                [],
            )
            .unwrap();

            conn.execute(
                "INSERT INTO entries (id, content, timestamp, kind, token_count) VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params!["m1", "manual entry", 100_i64, "Manual", 2_i64],
            )
            .unwrap();
            conn.execute(
                "INSERT INTO entries (id, content, timestamp, kind, token_count) VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params!["p1", "precompact entry", 200_i64, "PreCompact", 3_i64],
            )
            .unwrap();
            conn.execute(
                "INSERT INTO entries (id, content, timestamp, kind, token_count) VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params!["a1", "auto entry", 300_i64, "Auto", 4_i64],
            )
            .unwrap();
        }

        let storage = SqliteStorage::open(&db_path, 100).unwrap();
        let conn = storage.pool().get().unwrap();

        let version: i64 = conn
            .query_row(
                "SELECT COALESCE(MAX(version), 0) FROM schema_version",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(version, 3);

        // Kinds are remapped to the new lowercase TEXT vocabulary.
        let mut kinds: Vec<String> = conn
            .prepare("SELECT kind FROM entries ORDER BY timestamp")
            .unwrap()
            .query_map([], |r| r.get(0))
            .unwrap()
            .collect::<rusqlite::Result<Vec<_>>>()
            .unwrap();
        kinds.sort();
        assert_eq!(kinds, vec!["manual", "snapshot", "summary"]);

        let tags: i64 = conn
            .query_row("SELECT COUNT(*) FROM tags", [], |r| r.get(0))
            .unwrap();
        assert_eq!(tags, 0, "tags table should exist but be empty");

        let entry_tags: i64 = conn
            .query_row("SELECT COUNT(*) FROM entry_tags", [], |r| r.get(0))
            .unwrap();
        assert_eq!(entry_tags, 0, "entry_tags table should exist but be empty");

        // v2-only runtime tables are gone after the v3 rebuild.
        let runtime_configs_exists: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='runtime_configs'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(runtime_configs_exists, 0);

        let _ = fs::remove_file(&db_path);
    }

    #[test]
    fn test_v2_to_v3_migration() {
        let db_path = temp_db_path("v2-to-v3");

        {
            let conn = Connection::open(&db_path).unwrap();

            // Build a v2 fixture by running SCHEMA_V1 then SCHEMA_V2 directly,
            // then inserting rows with runtime columns populated.
            conn.execute_batch(crate::storage::schema::SCHEMA_V1)
                .unwrap();
            conn.execute_batch(
                "CREATE TABLE IF NOT EXISTS schema_version (id INTEGER PRIMARY KEY CHECK(id = 1), version INTEGER NOT NULL)",
            )
            .unwrap();
            conn.execute(
                "INSERT OR REPLACE INTO schema_version (id, version) VALUES (1, 1)",
                [],
            )
            .unwrap();
            conn.execute_batch(crate::storage::schema::SCHEMA_V2)
                .unwrap();

            conn.execute(
                "INSERT INTO entries (
                    id, content, timestamp, kind, token_count, session_id,
                    compaction_count, compaction_trigger, runtime, model, cwd,
                    git_branch, git_sha, turn_id, agent_type, agent_id
                 ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)",
                rusqlite::params![
                    "a1",
                    "auto entry with runtime metadata",
                    300_i64,
                    "Auto",
                    4_i64,
                    "session-abc",
                    2_i64,
                    "matcher:threshold",
                    "codex",
                    "gpt-5.3-codex",
                    "/workspace/context-forge",
                    "feature/schema-v2",
                    "abc123def",
                    "turn-77",
                    "coder",
                    "agent-main",
                ],
            )
            .unwrap();
            conn.execute(
                "INSERT INTO entries (id, content, timestamp, kind, token_count) VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params!["m1", "manual entry", 100_i64, "Manual", 2_i64],
            )
            .unwrap();
        }

        let storage = SqliteStorage::open(&db_path, 100).unwrap();

        // entries preserved
        let all = storage.get_all().unwrap();
        assert_eq!(all.len(), 2);

        // kinds remapped: 'Auto' -> 'summary', 'Manual' -> 'manual'
        let auto_entry = all.iter().find(|e| e.id == "a1").unwrap();
        let manual_entry = all.iter().find(|e| e.id == "m1").unwrap();
        assert_eq!(auto_entry.kind, "summary");
        assert_eq!(manual_entry.kind, "manual");

        // runtime fields present inside metadata JSON
        let metadata = auto_entry
            .metadata
            .as_ref()
            .expect("metadata should be present for migrated v2 entry");
        assert_eq!(metadata["runtime"], "codex");
        assert_eq!(metadata["model"], "gpt-5.3-codex");
        assert_eq!(metadata["cwd"], "/workspace/context-forge");
        assert_eq!(metadata["git_branch"], "feature/schema-v2");
        assert_eq!(metadata["git_sha"], "abc123def");
        assert_eq!(metadata["compaction_trigger"], "matcher:threshold");
        assert_eq!(metadata["turn_id"], "turn-77");
        assert_eq!(metadata["agent_type"], "coder");
        assert_eq!(metadata["agent_id"], "agent-main");

        // session_id and token_count survive the rebuild
        assert_eq!(auto_entry.session_id.as_deref(), Some("session-abc"));
        assert_eq!(auto_entry.token_count, Some(4));

        // FTS query still matches content
        let (_, searcher) = open_storage(&db_path, 100).unwrap();
        let results = searcher.search("runtime metadata", None, 10).unwrap();
        assert!(
            results.iter().any(|r| r.entry.id == "a1"),
            "FTS search should still find the migrated entry's content"
        );

        // runtime_configs table is gone
        let conn = storage.pool().get().unwrap();
        let runtime_configs_exists: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='runtime_configs'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(runtime_configs_exists, 0);

        let version: i64 = conn
            .query_row(
                "SELECT COALESCE(MAX(version), 0) FROM schema_version",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(version, 3);

        let _ = fs::remove_file(&db_path);
    }

    #[test]
    fn test_new_entry_with_scope_and_metadata() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();

        let metadata = serde_json::json!({"runtime": "codex", "model": "gpt-5.3-codex"});
        let entry = ContextEntry {
            id: "v3-entry".into(),
            content: "entry with scope and metadata".into(),
            timestamp: 1_700_000_100,
            kind: kind::SUMMARY.to_owned(),
            scope: Some("project:homelab-rs".into()),
            session_id: Some("session-123".into()),
            token_count: Some(6),
            metadata: Some(metadata.clone()),
        };

        storage.save(&entry).unwrap();

        let all = storage.get_all().unwrap();
        assert_eq!(all.len(), 1);
        let got = &all[0];
        assert_eq!(got.id, entry.id);
        assert_eq!(got.content, entry.content);
        assert_eq!(got.timestamp, entry.timestamp);
        assert_eq!(got.kind, entry.kind);
        assert_eq!(got.scope, entry.scope);
        assert_eq!(got.session_id, entry.session_id);
        assert_eq!(got.token_count, entry.token_count);
        assert_eq!(got.metadata, Some(metadata));
    }

    #[test]
    fn test_insert_or_replace() {
        let (storage, _) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "original content", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e1", "updated content", 200, kind::SUMMARY))
            .unwrap();

        assert_eq!(storage.count().unwrap(), 1);

        let all = storage.get_all().unwrap();
        assert_eq!(all[0].content, "updated content");
    }

    #[test]
    fn test_search_match_all_query() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "first entry", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "second entry", 200, kind::SNAPSHOT))
            .unwrap();
        storage
            .save(&make_entry("e3", "third entry", 300, kind::SUMMARY))
            .unwrap();

        let results = searcher.search(MATCH_ALL_QUERY, None, 10).unwrap();
        assert_eq!(results.len(), 3);

        // Ordered by timestamp descending (newest first).
        assert_eq!(results[0].entry.id, "e3");
        assert_eq!(results[1].entry.id, "e2");
        assert_eq!(results[2].entry.id, "e1");

        // Match-all results all share the same fixed score.
        for r in &results {
            assert!((r.score - 1.0).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn test_search_match_all_query_scoped() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_scoped_entry("e1", "first entry", 100, "a"))
            .unwrap();
        storage
            .save(&make_scoped_entry("e2", "second entry", 200, "b"))
            .unwrap();

        let results_a = searcher.search(MATCH_ALL_QUERY, Some("a"), 10).unwrap();
        assert_eq!(results_a.len(), 1);
        assert_eq!(results_a[0].entry.id, "e1");

        let results_all = searcher.search(MATCH_ALL_QUERY, None, 10).unwrap();
        assert_eq!(results_all.len(), 2);
    }

    #[test]
    fn search_with_fts5_operator_characters_does_not_error() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "marco polo", 100, kind::MANUAL))
            .unwrap();

        // Production bug: a query containing `"` and `.` previously caused
        // `fts5: syntax error`. It must now succeed and find the entry.
        let results = searcher.search(r#"if I say "marco"."#, None, 10).unwrap();

        assert!(
            results.iter().any(|r| r.entry.id == "e1"),
            "expected 'marco polo' entry to be found despite FTS5 operator characters in the query"
        );
    }

    #[test]
    fn search_or_joins_terms_for_message_length_queries() {
        let (storage, searcher) = open_storage(Path::new(":memory:"), 100).unwrap();
        storage
            .save(&make_entry("e1", "alpha one", 100, kind::MANUAL))
            .unwrap();
        storage
            .save(&make_entry("e2", "beta two", 200, kind::MANUAL))
            .unwrap();

        // Under implicit AND, no entry contains all of "alpha", "beta", and
        // "gamma", so this would return zero results. OR-join must return both.
        let results = searcher.search("alpha beta gamma", None, 10).unwrap();

        let ids: Vec<&str> = results.iter().map(|r| r.entry.id.as_str()).collect();
        assert!(ids.contains(&"e1"), "expected 'alpha one' entry in results");
        assert!(ids.contains(&"e2"), "expected 'beta two' entry in results");
    }
}