remem-ai 0.6.71

Local-first coding agent memory for Claude Code and OpenAI Codex
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
#[cfg(test)]
use std::cell::Cell;
use std::cell::RefCell;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use rusqlite::{Connection, OpenFlags};
use sha2::{Digest, Sha256};

use super::pragma::{ConnectionMode, ConnectionPragmas};

thread_local! {
    static DATA_DIR_OVERRIDE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}

#[cfg(test)]
thread_local! {
    static CONFIGURED_CONNECTION_OPENS: Cell<usize> = const { Cell::new(0) };
}

#[cfg(test)]
pub(crate) fn reset_configured_connection_open_count() {
    CONFIGURED_CONNECTION_OPENS.with(|count| count.set(0));
}

#[cfg(test)]
pub(crate) fn configured_connection_open_count() -> usize {
    CONFIGURED_CONNECTION_OPENS.with(Cell::get)
}

#[cfg(test)]
fn record_configured_connection_open() {
    CONFIGURED_CONNECTION_OPENS.with(|count| count.set(count.get() + 1));
}

pub(crate) fn with_data_dir<T>(dir: &Path, f: impl FnOnce() -> T) -> T {
    let _guard = DataDirOverrideGuard::set(dir.to_path_buf());
    f()
}

pub fn deterministic_hash(data: &[u8]) -> u64 {
    const FNV_OFFSET: u64 = 0xcbf29ce484222325;
    const FNV_PRIME: u64 = 0x00000100000001B3;
    let mut hash = FNV_OFFSET;
    for &byte in data {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

pub fn content_identity_hash(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    format!("sha256:content-v1:{:x}", hasher.finalize())
}

pub fn legacy_content_identity_hash(data: &[u8]) -> String {
    format!("{:016x}", deterministic_hash(data))
}

pub fn to_sql_refs(params: &[Box<dyn rusqlite::types::ToSql>]) -> Vec<&dyn rusqlite::types::ToSql> {
    params.iter().map(|b| b.as_ref()).collect()
}

pub fn truncate_str(s: &str, max_bytes: usize) -> &str {
    if s.len() <= max_bytes {
        return s;
    }
    let mut end = max_bytes;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    &s[..end]
}

pub fn canonical_project_path(cwd: &str) -> PathBuf {
    crate::project_id::canonical_project_path(cwd)
}

pub fn project_from_cwd(cwd: &str) -> String {
    crate::project_id::project_from_cwd(cwd)
}

pub fn data_dir() -> PathBuf {
    if let Some(path) = DATA_DIR_OVERRIDE.with(|slot| slot.borrow().clone()) {
        return path;
    }
    std::env::var("REMEM_DATA_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|_| {
            dirs::home_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join(".remem")
        })
}

pub fn absolute_data_dir() -> Result<PathBuf> {
    let path = data_dir();
    if path.is_absolute() {
        return Ok(path);
    }
    Ok(std::env::current_dir()
        .context("read current directory for relative REMEM_DATA_DIR")?
        .join(path))
}

pub fn db_path() -> PathBuf {
    data_dir().join("remem.db")
}

pub fn open_db() -> Result<Connection> {
    let path = db_path();
    let key = super::crypto::require_cipher_key_or_plaintext_override()?;
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(0o700);
            if let Err(e) = std::fs::set_permissions(parent, perms) {
                crate::log::warn("db", &format!("cannot set data dir permissions: {}", e));
            }
        }
    }

    let conn = open_configured_connection(&path, key.as_ref())?;
    crate::retrieval::vector::load_vec_extension(&conn)?;
    crate::migrate::run_migrations(&conn)?;
    crate::retrieval::vector::ensure_vec_table(&conn)?;
    advance_vec_index(&conn);
    crate::memory::retrieval_enrichment::enforce_binary_policy_floor(&conn)?;
    Ok(conn)
}

/// Advance the sqlite-vec KNN index by one backfill batch. Index maintenance
/// failure keeps the connection usable: retrieval degrades to the portable
/// brute-force scan, so log at error level instead of failing the open.
fn advance_vec_index(conn: &Connection) {
    if let Err(error) = crate::retrieval::vector::ensure_vec_index(conn) {
        crate::log::error(
            "db",
            &format!("vec index maintenance failed; brute-force vector scan remains: {error:#}"),
        );
    }
}

pub fn open_db_no_migrate() -> Result<Connection> {
    let path = db_path();
    let key = super::crypto::require_cipher_key_or_plaintext_override()?;
    if !path.exists() {
        anyhow::bail!("database not found: {}", path.display());
    }

    let conn = open_configured_existing_read_write_connection(&path, key.as_ref())?;
    crate::retrieval::vector::load_vec_extension(&conn)?;
    crate::migrate::ensure_schema_current(&conn)?;
    advance_vec_index(&conn);
    crate::memory::retrieval_enrichment::enforce_binary_policy_floor(&conn)?;
    Ok(conn)
}

pub fn open_db_for_hook() -> Result<Connection> {
    let conn = open_db_no_migrate().context(
        "hook database open requires an existing current schema without drift; run `remem install` outside the hook path",
    )?;
    Ok(conn)
}

pub fn open_db_read_only() -> Result<Connection> {
    let path = db_path();
    let key = super::crypto::require_cipher_key_or_plaintext_override()?;
    if !path.exists() {
        anyhow::bail!("database not found: {}", path.display());
    }

    open_configured_read_only_connection(&path, key.as_ref())
}

/// Open an existing database read-only and fail closed unless its schema is
/// current for this binary. This never creates, migrates, or repairs the store.
pub fn open_db_read_only_current() -> Result<Connection> {
    let conn = open_db_read_only()?;
    crate::migrate::ensure_schema_current(&conn)?;
    Ok(conn)
}

pub(crate) fn open_configured_connection(
    path: &Path,
    key: Option<&super::crypto::CipherKey>,
) -> Result<Connection> {
    let pragmas = ConnectionPragmas::from_env()?;
    let conn = Connection::open(path)
        .with_context(|| format!("Failed to open database: {}", path.display()))?;
    #[cfg(test)]
    record_configured_connection_open();

    super::crypto::configure_cipher(&conn, key)?;

    pragmas.apply(&conn, ConnectionMode::ReadWrite)?;
    Ok(conn)
}

pub(crate) fn open_configured_existing_read_write_connection(
    path: &Path,
    key: Option<&super::crypto::CipherKey>,
) -> Result<Connection> {
    let pragmas = ConnectionPragmas::from_env()?;
    let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_WRITE).with_context(
        || {
            format!(
                "Failed to open existing database read-write: {}",
                path.display()
            )
        },
    )?;
    #[cfg(test)]
    record_configured_connection_open();

    super::crypto::configure_cipher(&conn, key)?;

    pragmas.apply(&conn, ConnectionMode::ReadWrite)?;
    Ok(conn)
}

pub(crate) fn open_configured_read_only_connection(
    path: &Path,
    key: Option<&super::crypto::CipherKey>,
) -> Result<Connection> {
    let pragmas = ConnectionPragmas::from_env()?;
    let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)
        .with_context(|| format!("Failed to open database read-only: {}", path.display()))?;
    #[cfg(test)]
    record_configured_connection_open();

    super::crypto::configure_cipher(&conn, key)?;
    pragmas.apply(&conn, ConnectionMode::ReadOnly)?;
    Ok(conn)
}

pub fn detect_git_branch(cwd: &str) -> Option<String> {
    let output =
        crate::git_util::git_output_soft(Path::new(cwd), &["rev-parse", "--abbrev-ref", "HEAD"])?;
    if !output.status.success() {
        return None;
    }
    let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if branch.is_empty() || branch == "HEAD" {
        None
    } else {
        Some(branch)
    }
}

struct DataDirOverrideGuard {
    previous: Option<PathBuf>,
}

impl DataDirOverrideGuard {
    fn set(path: PathBuf) -> Self {
        let previous = DATA_DIR_OVERRIDE.with(|slot| slot.replace(Some(path)));
        Self { previous }
    }
}

impl Drop for DataDirOverrideGuard {
    fn drop(&mut self) {
        let previous = self.previous.take();
        DATA_DIR_OVERRIDE.with(|slot| {
            slot.replace(previous);
        });
    }
}

pub fn detect_git_commit(cwd: &str) -> Option<String> {
    let output =
        crate::git_util::git_output_soft(Path::new(cwd), &["rev-parse", "--short", "HEAD"])?;
    if !output.status.success() {
        return None;
    }
    let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if sha.is_empty() {
        None
    } else {
        Some(sha)
    }
}

#[cfg(test)]
mod tests {
    use rusqlite::{params, Connection};

    use super::*;
    use crate::db::crypto::ALLOW_PLAINTEXT_ENV;
    use crate::db::test_support::ScopedTestDataDir;

    #[test]
    fn content_identity_hash_is_versioned_sha256() {
        let hash = content_identity_hash(b"hello world");

        assert_eq!(
            hash,
            "sha256:content-v1:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
        );
        let legacy_hash = legacy_content_identity_hash(b"hello world");
        assert_eq!(legacy_hash.len(), 16);
        assert!(!legacy_hash.starts_with("sha256:"));
    }

    #[test]
    fn open_db_for_hook_does_not_create_missing_database() {
        let test_dir = ScopedTestDataDir::new("hook-open-missing");
        test_dir.remove_db_files();

        let err = open_db_for_hook().expect_err("missing database should fail");

        let message = err.to_string();
        assert!(
            message.contains("hook database open requires"),
            "unexpected error: {message}"
        );
        assert!(
            !test_dir.path.exists(),
            "hook open must not create data dir"
        );
        assert!(
            !test_dir.db_path().exists(),
            "hook open must not create database file"
        );
    }

    #[test]
    fn open_db_for_hook_opens_current_schema_read_write() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("hook-open-current-rw");
        let setup = crate::db::open_db()?;
        drop(setup);

        let conn = crate::db::open_db_for_hook()?;
        conn.execute("CREATE TABLE hook_rw_probe(id INTEGER PRIMARY KEY)", [])?;
        conn.execute("INSERT INTO hook_rw_probe(id) VALUES (1)", [])?;
        let count: i64 =
            conn.query_row("SELECT COUNT(*) FROM hook_rw_probe", [], |row| row.get(0))?;

        assert_eq!(count, 1);
        Ok(())
    }

    #[test]
    fn open_db_for_hook_rejects_older_schema_without_migrating() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("hook-open-older-schema");
        let setup = crate::db::open_db()?;
        let latest = crate::migrate::latest_schema_version();
        setup.execute(
            "DELETE FROM _schema_migrations WHERE version = ?1",
            [latest],
        )?;
        drop(setup);

        let err = crate::db::open_db_for_hook()
            .expect_err("older schema should require foreground migration");

        assert!(
            err.to_string().contains("hook database open requires"),
            "unexpected error: {err:#}"
        );
        let check = Connection::open(crate::db::db_path())?;
        let latest_rows: i64 = check.query_row(
            "SELECT COUNT(*) FROM _schema_migrations WHERE version = ?1",
            [latest],
            |row| row.get(0),
        )?;
        assert_eq!(latest_rows, 0);
        Ok(())
    }

    #[test]
    fn open_db_for_hook_rejects_schema_drift_without_repairing() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("hook-open-schema-drift");
        create_current_schema_missing_migration(&test_dir.db_path(), 22)?;

        let err = crate::db::open_db_for_hook().expect_err("schema drift should fail closed");

        assert!(
            format!("{err:#}").contains("schema drift requires foreground migration"),
            "unexpected error: {err:#}"
        );
        assert!(
            format!("{err:#}").contains("schema drift"),
            "unexpected error: {err:#}"
        );
        let check = Connection::open(test_dir.db_path())?;
        let state_keys_exists: i64 = check.query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'memory_state_keys'",
            [],
            |row| row.get(0),
        )?;
        assert_eq!(state_keys_exists, 0);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_rejects_post_v022_schema_drift_without_repairing() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("no-migrate-post-v022-schema-drift");
        create_current_schema_missing_migration(&test_dir.db_path(), 45)?;

        let err = crate::db::open_db_no_migrate().expect_err("schema drift should fail closed");

        let message = format!("{err:#}");
        assert!(
            message.contains("schema drift requires foreground migration"),
            "unexpected error: {message}"
        );
        assert!(
            message.contains("v045_memory_usage_columns"),
            "unexpected error: {message}"
        );
        let check = Connection::open(test_dir.db_path())?;
        let usage_table_exists: i64 = check.query_row(
            "SELECT COUNT(*) FROM sqlite_master
             WHERE type = 'table' AND name = 'memory_citation_events'",
            [],
            |row| row.get(0),
        )?;
        assert_eq!(usage_table_exists, 0);
        Ok(())
    }

    #[test]
    fn open_db_read_only_does_not_create_missing_database() {
        let test_dir = ScopedTestDataDir::new("readonly-missing");
        test_dir.remove_db_files();

        let err = open_db_read_only().expect_err("missing database should fail");

        let message = err.to_string();
        assert!(
            message.contains("database not found"),
            "unexpected error: {message}"
        );
        assert!(
            !test_dir.path.exists(),
            "read-only open must not create data dir"
        );
        assert!(
            !test_dir.db_path().exists(),
            "read-only open must not create database file"
        );
    }

    #[test]
    fn open_db_read_only_refuses_plaintext_without_explicit_override() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("readonly-cipher-fail-closed");
        let conn = crate::db::open_db()?;
        drop(conn);
        std::env::remove_var(ALLOW_PLAINTEXT_ENV);

        let err = crate::db::open_db_read_only()
            .expect_err("read-only open must enforce the plaintext guard");

        let message = err.to_string();
        assert!(message.contains("SQLCipher key"), "got: {message}");
        assert!(
            test_dir.db_path().exists(),
            "read-only guard must not remove the existing database"
        );
        Ok(())
    }

    #[test]
    fn open_db_does_not_backfill_missing_vector_embeddings() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("open-no-vector-backfill");
        let conn = crate::db::open_db()?;
        conn.execute(
            "INSERT INTO memories
             (id, project, title, content, memory_type, created_at_epoch, updated_at_epoch, status)
             VALUES (1, '/repo', 'Vector row', 'Open must not backfill this row.', 'decision', 1, 1, 'active')",
            [],
        )?;
        drop(conn);

        let reopened = crate::db::open_db()?;
        let count: i64 =
            reopened.query_row("SELECT COUNT(*) FROM memory_embeddings", [], |row| {
                row.get(0)
            })?;
        assert_eq!(count, 0);
        assert_eq!(
            crate::retrieval::vector::backfill_missing_memory_embeddings(&reopened, 10)?,
            1
        );
        Ok(())
    }

    #[test]
    fn open_db_read_only_does_not_run_migrations() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("readonly-no-migration");
        let path = crate::db::db_path();
        std::fs::create_dir_all(crate::db::data_dir())?;
        let conn = Connection::open(&path)?;
        conn.execute("CREATE TABLE marker (id INTEGER PRIMARY KEY)", [])?;
        drop(conn);

        let readonly = crate::db::open_db_read_only()?;
        let marker_exists: i64 = readonly.query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'marker'",
            [],
            |row| row.get(0),
        )?;
        let migrations_exists: i64 = readonly.query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'schema_migrations'",
            [],
            |row| row.get(0),
        )?;

        assert_eq!(marker_exists, 1);
        assert_eq!(migrations_exists, 0);
        Ok(())
    }

    #[test]
    fn open_db_read_only_current_works_while_writer_holds_immediate_lock() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("readonly-current-write-lock");
        let writer = crate::db::open_db()?;
        writer.execute_batch("BEGIN IMMEDIATE")?;

        let readonly = crate::db::open_db_read_only_current()?;
        let latest: i64 =
            readonly.query_row("SELECT MAX(version) FROM _schema_migrations", [], |row| {
                row.get(0)
            })?;

        assert_eq!(latest, crate::migrate::latest_schema_version());
        writer.execute_batch("ROLLBACK")?;
        Ok(())
    }

    #[test]
    fn open_db_read_only_current_rejects_stale_schema_without_writes() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("readonly-current-stale");
        let setup = crate::db::open_db()?;
        let latest = crate::migrate::latest_schema_version();
        setup.execute(
            "DELETE FROM _schema_migrations WHERE version = ?1",
            [latest],
        )?;
        drop(setup);

        let err =
            crate::db::open_db_read_only_current().expect_err("stale schema must fail closed");
        assert!(
            err.to_string().contains("run a foreground remem command"),
            "unexpected error: {err:#}"
        );

        let check = Connection::open(crate::db::db_path())?;
        let rows: i64 = check.query_row(
            "SELECT COUNT(*) FROM _schema_migrations WHERE version = ?1",
            [latest],
            |row| row.get(0),
        )?;
        assert_eq!(rows, 0);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_does_not_create_missing_database() {
        let test_dir = ScopedTestDataDir::new("no-migrate-missing");
        test_dir.remove_db_files();

        let err = open_db_no_migrate().expect_err("missing database should fail");

        let message = err.to_string();
        assert!(
            message.contains("database not found"),
            "unexpected error: {message}"
        );
        assert!(
            !test_dir.path.exists(),
            "no-migrate open must not create data dir"
        );
        assert!(
            !test_dir.db_path().exists(),
            "no-migrate open must not create database file"
        );
    }

    #[test]
    fn open_db_no_migrate_refuses_plaintext_without_explicit_override() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("no-migrate-cipher-fail-closed");
        let conn = crate::db::open_db()?;
        drop(conn);
        std::env::remove_var(ALLOW_PLAINTEXT_ENV);

        let err = crate::db::open_db_no_migrate()
            .expect_err("no-migrate open must enforce the plaintext guard");

        let message = err.to_string();
        assert!(message.contains("SQLCipher key"), "got: {message}");
        assert!(
            test_dir.db_path().exists(),
            "no-migrate guard must not remove the existing database"
        );
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_opens_current_schema_read_write() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("no-migrate-current-rw");
        let setup = crate::db::open_db()?;
        drop(setup);

        let conn = crate::db::open_db_no_migrate()?;
        conn.execute(
            "CREATE TABLE no_migrate_rw_probe(id INTEGER PRIMARY KEY)",
            [],
        )?;
        conn.execute("INSERT INTO no_migrate_rw_probe(id) VALUES (1)", [])?;
        let count: i64 = conn.query_row("SELECT COUNT(*) FROM no_migrate_rw_probe", [], |row| {
            row.get(0)
        })?;

        assert_eq!(count, 1);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_does_not_run_migrations() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("no-migrate-no-migration");
        std::fs::create_dir_all(&test_dir.path)?;
        let setup = Connection::open(test_dir.db_path())?;
        setup.execute("CREATE TABLE marker (id INTEGER PRIMARY KEY)", [])?;
        drop(setup);

        let err = crate::db::open_db_no_migrate().expect_err("stale schema should fail");

        assert!(
            err.to_string().contains("schema is not initialized"),
            "unexpected error: {err:#}"
        );
        let check = Connection::open(test_dir.db_path())?;
        let migrations_exists: i64 = check.query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '_schema_migrations'",
            [],
            |row| row.get(0),
        )?;
        assert_eq!(migrations_exists, 0);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_rejects_older_schema_without_migrating() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("no-migrate-older-schema");
        let setup = crate::db::open_db()?;
        let latest = crate::migrate::latest_schema_version();
        setup.execute(
            "DELETE FROM _schema_migrations WHERE version = ?1",
            [latest],
        )?;
        drop(setup);

        let err = crate::db::open_db_no_migrate()
            .expect_err("older schema should require foreground migration");

        assert!(
            err.to_string().contains("requires schema"),
            "unexpected error: {err:#}"
        );
        let check = Connection::open(crate::db::db_path())?;
        let latest_rows: i64 = check.query_row(
            "SELECT COUNT(*) FROM _schema_migrations WHERE version = ?1",
            [latest],
            |row| row.get(0),
        )?;
        assert_eq!(latest_rows, 0);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_rejects_incomplete_schema_without_migrating() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("no-migrate-incomplete-schema");
        let setup = crate::db::open_db()?;
        let latest = crate::migrate::latest_schema_version();
        let missing = crate::migrate::MIGRATIONS
            .iter()
            .rev()
            .find(|migration| migration.version < latest)
            .expect("test requires at least two migrations")
            .version;
        setup.execute(
            "DELETE FROM _schema_migrations WHERE version = ?1",
            [missing],
        )?;
        drop(setup);

        let err = crate::db::open_db_no_migrate()
            .expect_err("incomplete schema should require foreground migration");

        assert!(
            err.to_string().contains("missing migration"),
            "unexpected error: {err:#}"
        );
        let check = Connection::open(crate::db::db_path())?;
        let (missing_rows, latest_rows): (i64, i64) = check.query_row(
            "SELECT
                SUM(CASE WHEN version = ?1 THEN 1 ELSE 0 END),
                SUM(CASE WHEN version = ?2 THEN 1 ELSE 0 END)
             FROM _schema_migrations",
            (missing, latest),
            |row| Ok((row.get(0)?, row.get(1)?)),
        )?;
        assert_eq!(missing_rows, 0);
        assert_eq!(latest_rows, 1);
        Ok(())
    }

    #[test]
    fn open_db_no_migrate_rejects_newer_schema_without_migrating() -> Result<()> {
        let _test_dir = ScopedTestDataDir::new("no-migrate-newer-schema");
        let setup = crate::db::open_db()?;
        let latest = crate::migrate::latest_schema_version();
        setup.execute(
            "INSERT INTO _schema_migrations (version, name, applied_at_epoch)
             VALUES (?1, 'future-test', 0)",
            [latest + 1],
        )?;
        drop(setup);

        let err = crate::db::open_db_no_migrate().expect_err("newer schema should fail closed");

        assert!(
            err.to_string().contains("only knows up to"),
            "unexpected error: {err:#}"
        );
        Ok(())
    }

    #[test]
    fn open_db_read_only_opens_existing_database_without_write_access() -> Result<()> {
        let test_dir = ScopedTestDataDir::new("readonly-existing");
        std::fs::create_dir_all(&test_dir.path)?;
        let setup = Connection::open(test_dir.db_path())?;
        setup.execute_batch(
            "CREATE TABLE readonly_probe(id INTEGER PRIMARY KEY);
             INSERT INTO readonly_probe(id) VALUES (1);",
        )?;
        drop(setup);

        let conn = open_db_read_only()?;
        let count: i64 =
            conn.query_row("SELECT COUNT(*) FROM readonly_probe", [], |row| row.get(0))?;
        assert_eq!(count, 1);

        let err = conn
            .execute("INSERT INTO readonly_probe(id) VALUES (2)", [])
            .expect_err("read-only connection must reject writes");
        assert_eq!(err.sqlite_error_code(), Some(rusqlite::ErrorCode::ReadOnly));
        Ok(())
    }

    fn create_current_schema_missing_migration(path: &Path, missing_version: i64) -> Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let conn = Connection::open(path)?;
        conn.execute_batch(
            "PRAGMA journal_mode=WAL;
             PRAGMA foreign_keys=OFF;
             PRAGMA writable_schema=ON;",
        )?;
        for migration in crate::migrate::MIGRATIONS
            .iter()
            .filter(|migration| migration.version != missing_version)
        {
            conn.execute_batch(migration.sql)?;
        }
        conn.execute_batch(
            "CREATE TABLE _schema_migrations (
                version INTEGER PRIMARY KEY,
                name TEXT NOT NULL,
                applied_at_epoch INTEGER NOT NULL
            );",
        )?;
        for migration in crate::migrate::MIGRATIONS {
            conn.execute(
                "INSERT INTO _schema_migrations (version, name, applied_at_epoch)
                 VALUES (?1, ?2, 1700000000)",
                params![migration.version, migration.name],
            )?;
        }
        conn.execute_batch(&format!(
            "PRAGMA writable_schema=OFF;
             PRAGMA user_version = {};
             PRAGMA foreign_keys=ON;",
            crate::migrate::latest_schema_version()
        ))?;
        Ok(())
    }
}