second-brain-core 0.13.4

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
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
use std::fs;
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{Context, Result, anyhow, bail, ensure};
use rusqlite::{Connection, params};

use super::SqliteStore;
use super::compaction::{SPACE_MARGIN, VacuumSpace};
use super::pool::{self, open_connection};
use crate::keys::StoreKey;

const PLAIN_HEADER: &[u8; 16] = b"SQLite format 3\0";
const REWRITE_SUFFIX: &str = "enc";
const READERS_WAIT: Duration = Duration::from_secs(30);
const PROBE_BUSY: Duration = Duration::from_secs(1);
pub const PLAINTEXT_COPY_PREFIX: &str = "brain.db.plaintext-";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FileState {
    Absent,
    Plaintext,
    Encrypted,
}

/// An empty file counts as absent, since SQLite writes the header with the
/// first page.
pub fn db_file_state(path: &Path) -> Result<FileState> {
    let mut file = match fs::File::open(path) {
        Ok(file) => file,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(FileState::Absent),
        Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
    };
    let mut header = [0u8; 16];
    let mut filled = 0;
    while filled < header.len() {
        let read = file
            .read(&mut header[filled..])
            .with_context(|| format!("reading {}", path.display()))?;
        if read == 0 {
            break;
        }
        filled += read;
    }
    Ok(match filled {
        0 => FileState::Absent,
        16 if &header == PLAIN_HEADER => FileState::Plaintext,
        _ => FileState::Encrypted,
    })
}

pub fn rewrite_path(db: &Path) -> PathBuf {
    let mut name = db.file_name().unwrap_or_default().to_os_string();
    name.push(".");
    name.push(REWRITE_SUFFIX);
    db.with_file_name(name)
}

/// Deletes the copy a rewrite left when it stopped before its rename; the
/// store at `db` is then as it was before that rewrite. Returns true when
/// there was one.
pub fn remove_interrupted_rewrite(db: &Path) -> Result<bool> {
    let copy = rewrite_path(db);
    let mut removed = false;
    let sidecars = ["-journal", "-wal", "-shm"].map(|suffix| sidecar(&copy, suffix));
    for path in std::iter::once(copy.clone()).chain(sidecars) {
        match fs::remove_file(&path) {
            Ok(()) => removed = true,
            Err(e) if e.kind() == ErrorKind::NotFound => {}
            Err(e) => return Err(e).with_context(|| format!("removing {}", path.display())),
        }
    }
    Ok(removed)
}

#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum RewriteTo<'a> {
    Key(&'a StoreKey),
    /// Whatever key the store is open under when the rewrite takes the
    /// writer, so a rewrite racing a rotation never restores the old key.
    CurrentKey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rewrite {
    pub bytes_before: u64,
    pub bytes_after: u64,
    /// How long writes waited: from the start of the export to the reopen.
    pub pause: Duration,
}

/// The rewrite renamed its copy into place but the store could not reopen
/// it; every read and write fails until the process opens the store again.
#[derive(Debug)]
pub struct NotReopened(pub anyhow::Error);

impl std::fmt::Display for NotReopened {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "the store was rewritten but did not reopen; restart to open it: {:#}",
            self.0
        )
    }
}

impl std::error::Error for NotReopened {}

/// A read held a store connection past the wait, so the rewrite stopped
/// before the swap and left the store as it was.
#[derive(Debug)]
pub struct ReadsHeld(pub Duration);

impl std::fmt::Display for ReadsHeld {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "a read held a store connection for over {}s; the rewrite runs again later",
            self.0.as_secs()
        )
    }
}

impl std::error::Error for ReadsHeld {}

impl SqliteStore {
    pub fn is_encrypted(&self) -> bool {
        self.store_key().is_some()
    }

    pub(crate) fn store_key(&self) -> Option<Arc<StoreKey>> {
        match &self.readers {
            super::ReadBackend::Pooled(pool) => pool.key(),
            super::ReadBackend::SameAsWriter => None,
        }
    }

    /// Rewrites the whole file under `to` through `sqlcipher_export` into a
    /// copy beside it, checks the copy, and renames it over the store.
    /// Writes wait on the writer for the whole rewrite; reads continue
    /// until the swap. A failure before the rename leaves the store as it
    /// was; one after it is a `NotReopened`.
    pub fn rewrite_under_key(&self, to: RewriteTo<'_>) -> Result<Rewrite> {
        self.rewrite_waiting(to, READERS_WAIT)
    }

    pub(crate) fn rewrite_waiting(
        &self,
        to: RewriteTo<'_>,
        readers_wait: Duration,
    ) -> Result<Rewrite> {
        let super::ReadBackend::Pooled(readers) = &self.readers else {
            bail!("an in-memory store has no file to rewrite");
        };
        let db = self
            .file_path()?
            .ok_or_else(|| anyhow!("the store has no file to rewrite"))?;
        let copy = rewrite_path(&db);
        let mut writer = self.conn();
        remove_interrupted_rewrite(&db)?;
        let old_key = self.store_key();
        let key = match to {
            RewriteTo::Key(key) => Some(Arc::new(key.duplicate())),
            RewriteTo::CurrentKey => old_key.clone(),
        };
        let started = Instant::now();
        let bytes_before = file_len(&db)?;
        let exported = export(&writer, &copy, key.as_deref(), old_key.is_none())
            .and_then(|()| check_copy(&writer, &copy, key.as_deref()));
        if let Err(e) = exported {
            remove_interrupted_rewrite(&db)?;
            return Err(e);
        }

        let Some(mut exclusive) = readers.exclusive(readers_wait) else {
            remove_interrupted_rewrite(&db)?;
            return Err(ReadsHeld(readers_wait).into());
        };
        let placeholder = pool::unusable_connection()?;
        exclusive.close_all();
        drop(std::mem::replace(&mut *writer, placeholder));
        let swapped =
            prove_alone(&db, old_key.as_deref()).and_then(|()| swap_into_place(&db, &copy));
        if let Err(e) = swapped {
            let restored = reopen_store(&mut writer, exclusive, &db, old_key);
            if let Err(cleanup) = remove_interrupted_rewrite(&db) {
                tracing::warn!("cannot remove the rewrite copy: {cleanup:#}");
            }
            return match restored {
                Ok(()) => Err(e),
                Err(reopen) => Err(NotReopened(reopen.context(format!("{e:#}"))).into()),
            };
        }
        if let Err(e) = reopen_store(&mut writer, exclusive, &db, key) {
            return Err(NotReopened(e).into());
        }
        let pause = started.elapsed();
        Ok(Rewrite {
            bytes_before,
            bytes_after: file_len(&db)?,
            pause,
        })
    }

    /// A rewrite needs room beside the store for a copy of its live pages,
    /// with a quarter more for the copy's own page overhead.
    pub fn rewrite_space(&self) -> Result<VacuumSpace> {
        let db = self
            .file_path()?
            .ok_or_else(|| anyhow!("the store has no file to rewrite"))?;
        let dir = db
            .parent()
            .map_or_else(|| PathBuf::from("."), Path::to_path_buf);
        let live = self.live_bytes()?;
        Ok(VacuumSpace {
            free_bytes: fs4::available_space(&dir)
                .with_context(|| format!("reading free space at {}", dir.display()))?,
            path: dir,
            needed_bytes: live + live / 4 + SPACE_MARGIN,
        })
    }

    /// Writes a plaintext copy of the store into `dir` for a tool that
    /// reads the file directly, under a fresh name created with O_EXCL and
    /// mode 0600. The copy is a deliberate local decryption; the caller
    /// removes it with `remove_plaintext_copies`.
    pub fn write_plaintext_copy(&self, dir: &Path) -> Result<PathBuf> {
        let path = dir.join(format!(
            "{PLAINTEXT_COPY_PREFIX}{}",
            uuid::Uuid::new_v4().simple()
        ));
        create_private_empty(&path)?;
        let copied = (|| -> Result<()> {
            let conn = self.reader();
            pool::require_sqlcipher(&conn)?;
            conn.execute(
                "ATTACH DATABASE ?1 AS plaintext KEY ''",
                params![path_text(&path)?],
            )?;
            let sort_on_disk = SortOnDisk::begin(&conn)?;
            let exported = (|| -> Result<()> {
                conn.execute_batch("BEGIN;")?;
                let inner = (|| -> Result<()> {
                    conn.query_row("SELECT sqlcipher_export('plaintext')", [], |_| Ok(()))?;
                    let version: i64 =
                        conn.query_row("PRAGMA main.user_version", [], |r| r.get(0))?;
                    conn.execute_batch(&format!("PRAGMA plaintext.user_version = {version};"))?;
                    conn.execute(
                        "DELETE FROM plaintext.index_meta WHERE substr(key, 1, length(?1)) = ?1",
                        params![super::at_rest_keys::ROW_PREFIX],
                    )?;
                    Ok(())
                })();
                let end = if inner.is_ok() {
                    "COMMIT;"
                } else {
                    "ROLLBACK;"
                };
                inner.and(conn.execute_batch(end).map_err(Into::into))
            })();
            drop(sort_on_disk);
            let detached = conn.execute_batch("DETACH DATABASE plaintext;");
            exported.and(detached.map_err(Into::into))
        })();
        match copied {
            Ok(()) => Ok(path),
            Err(e) => match fs::remove_file(&path) {
                Ok(()) => Err(e),
                Err(cleanup) => Err(e.context(format!(
                    "the partial copy {} is left: {cleanup}",
                    path.display()
                ))),
            },
        }
    }

    /// Read from the pool rather than a connection, so a rewrite waiting
    /// out held reads never queues behind them for its own path.
    pub fn file_path(&self) -> Result<Option<PathBuf>> {
        Ok(match &self.readers {
            super::ReadBackend::Pooled(readers) => Some(readers.path().to_path_buf()),
            super::ReadBackend::SameAsWriter => None,
        })
    }
}

/// True when `key` opens the file at `db` and reads its schema.
pub fn key_opens_file(db: &Path, key: &StoreKey) -> bool {
    Connection::open(db).is_ok_and(|conn| pool::apply_key(&conn, key, db).is_ok())
}

/// Removes plaintext copies in `dir` older than `max_age`, or every one
/// when None; returns how many went.
pub fn remove_plaintext_copies(dir: &Path, max_age: Option<Duration>) -> Result<usize> {
    let entries = match fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(0),
        Err(e) => return Err(e).with_context(|| format!("reading {}", dir.display())),
    };
    let mut removed = 0;
    for entry in entries {
        let entry = entry?;
        let name = entry.file_name();
        if !name
            .to_str()
            .is_some_and(|n| n.starts_with(PLAINTEXT_COPY_PREFIX))
        {
            continue;
        }
        let old_enough = match max_age {
            None => true,
            Some(age) => entry
                .metadata()?
                .modified()?
                .elapsed()
                .is_ok_and(|elapsed| elapsed >= age),
        };
        if old_enough {
            fs::remove_file(entry.path())
                .with_context(|| format!("removing {}", entry.path().display()))?;
            removed += 1;
        }
    }
    Ok(removed)
}

fn create_private_empty(path: &Path) -> Result<()> {
    let mut options = fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    std::os::unix::fs::OpenOptionsExt::mode(&mut options, crate::private_files::PRIVATE_FILE_MODE);
    options
        .open(path)
        .map(drop)
        .with_context(|| format!("creating {}", path.display()))
}

fn export(
    writer: &Connection,
    copy: &Path,
    key: Option<&StoreKey>,
    source_plaintext: bool,
) -> Result<()> {
    pool::require_sqlcipher(writer)?;
    let literal = key.map(StoreKey::sql_literal);
    let key_text = literal.as_ref().map_or("", |l| l.as_str());
    writer
        .execute(
            "ATTACH DATABASE ?1 AS rewrite KEY ?2",
            params![path_text(copy)?, key_text],
        )
        .context("attaching the rewrite copy")?;
    let exported = (|| -> Result<()> {
        let sort_on_disk = source_plaintext
            .then(|| SortOnDisk::begin(writer))
            .transpose()?;
        let copied = writer
            .query_row("SELECT sqlcipher_export('rewrite')", [], |_| Ok(()))
            .context("exporting the store into the copy");
        drop(sort_on_disk);
        copied?;
        let version: i64 = writer.query_row("PRAGMA main.user_version", [], |r| r.get(0))?;
        writer.execute_batch(&format!("PRAGMA rewrite.user_version = {version};"))?;
        Ok(())
    })();
    let detached = writer
        .execute_batch("DETACH DATABASE rewrite;")
        .context("detaching the rewrite copy");
    exported.and(detached)
}

/// SQLCipher keeps temp tables in memory by default, so an export would
/// build every index of a large store in RAM. Sorting through temp files
/// exposes nothing new only when the source or the output is already
/// plaintext on disk; a keyed rewrite must never spill.
struct SortOnDisk<'a>(&'a Connection);

impl<'a> SortOnDisk<'a> {
    fn begin(conn: &'a Connection) -> Result<Self> {
        conn.execute_batch("PRAGMA temp_store = FILE;")?;
        Ok(Self(conn))
    }
}

impl Drop for SortOnDisk<'_> {
    fn drop(&mut self) {
        if let Err(e) = self.0.execute_batch("PRAGMA temp_store = DEFAULT;") {
            tracing::warn!("cannot reset temp_store after a rewrite: {e}");
        }
    }
}

/// Opens the copy on its own connection, so a wrong key or a damaged page
/// shows here, and compares every table's row count with the store's.
fn check_copy(writer: &Connection, copy: &Path, key: Option<&StoreKey>) -> Result<()> {
    let conn = open_checked(copy, key, "the rewrite copy")?;
    let mut tables = writer.prepare(
        "SELECT name FROM main.sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' \
         ORDER BY name",
    )?;
    let names: Vec<String> = tables
        .query_map([], |r| r.get(0))?
        .collect::<rusqlite::Result<_>>()?;
    for name in names {
        let count = |c: &Connection| -> Result<i64> {
            let sql = format!("SELECT count(*) FROM \"{}\"", name.replace('"', "\"\""));
            c.query_row(&sql, [], |r| r.get(0))
                .with_context(|| format!("counting {name}"))
        };
        let (live, copied) = (count(writer)?, count(&conn)?);
        ensure!(
            live == copied,
            "the rewrite copy holds {copied} rows of {name}, the store {live}"
        );
    }
    let versions = |c: &Connection| -> Result<i64> {
        Ok(c.query_row("PRAGMA user_version", [], |r| r.get(0))?)
    };
    ensure!(
        versions(writer)? == versions(&conn)?,
        "the rewrite copy lost the schema version"
    );
    Ok(())
}

/// Opens `copy` on its own connection under `key`, so a wrong key or a
/// damaged page shows here rather than when the copy is next opened.
pub(super) fn open_checked(copy: &Path, key: Option<&StoreKey>, what: &str) -> Result<Connection> {
    let conn =
        Connection::open(copy).with_context(|| format!("opening {what} {}", copy.display()))?;
    if let Some(key) = key {
        pool::apply_key(&conn, key, copy)?;
        let mut check = conn.prepare("PRAGMA cipher_integrity_check")?;
        let failures: Vec<String> = check
            .query_map([], |r| r.get(0))?
            .collect::<rusqlite::Result<_>>()?;
        ensure!(
            failures.is_empty(),
            "{what} failed its integrity check: {}",
            failures.join("; ")
        );
    }
    let quick: String = conn.query_row("PRAGMA quick_check", [], |r| r.get(0))?;
    ensure!(quick == "ok", "{what} failed its quick check: {quick}");
    Ok(conn)
}

/// Runs once `prove_alone` has left the store in rollback mode with no
/// connection open, so a WAL or shared-memory file beside it is stale and
/// would otherwise be replayed onto the copy.
fn swap_into_place(db: &Path, copy: &Path) -> Result<()> {
    for suffix in ["-wal", "-shm"] {
        let path = sidecar(db, suffix);
        match fs::remove_file(&path) {
            Ok(()) => {}
            Err(e) if e.kind() == ErrorKind::NotFound => {}
            Err(e) => return Err(e).with_context(|| format!("removing {}", path.display())),
        }
    }
    fs::File::open(copy)
        .and_then(|f| f.sync_all())
        .with_context(|| format!("syncing {}", copy.display()))?;
    fs::rename(copy, db)
        .with_context(|| format!("renaming {} over {}", copy.display(), db.display()))?;
    let dir = db.parent().unwrap_or(Path::new("."));
    if let Err(e) = fs::File::open(dir).and_then(|d| d.sync_all()) {
        tracing::warn!(
            "the store was swapped, but syncing {} failed: {e}",
            dir.display()
        );
    }
    Ok(())
}

/// Leaving WAL mode needs every other connection to the file, in any
/// process, to be closed; one left open would delete the new store's WAL
/// by path when it closes.
fn prove_alone(db: &Path, key: Option<&StoreKey>) -> Result<()> {
    let conn = Connection::open(db).with_context(|| format!("opening {}", db.display()))?;
    conn.busy_timeout(PROBE_BUSY)?;
    if let Some(key) = key {
        pool::apply_key(&conn, key, db)?;
    }
    let mode: String = conn
        .query_row("PRAGMA journal_mode = DELETE", [], |r| r.get(0))
        .map_err(|e| anyhow!("another connection holds the store open: {e}"))?;
    ensure!(
        mode.eq_ignore_ascii_case("delete"),
        "another connection holds the store open, so its WAL cannot be cleared"
    );
    Ok(())
}

#[cfg(test)]
thread_local! {
    pub(crate) static FAIL_REOPEN: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Reopens the writer and the readers on `db`; when the writer cannot open,
/// every read fails too rather than waiting on an empty pool.
fn reopen_store(
    writer: &mut Connection,
    exclusive: pool::Exclusive<'_>,
    db: &Path,
    key: Option<Arc<StoreKey>>,
) -> Result<()> {
    #[cfg(test)]
    if FAIL_REOPEN.get() {
        return exclusive.fail_reads(anyhow!("the test failed the reopen"));
    }
    match open_connection(db, key.as_deref()) {
        Ok(conn) => {
            *writer = conn;
            exclusive.refill(key)
        }
        Err(e) => exclusive.fail_reads(e),
    }
}

fn sidecar(path: &Path, suffix: &str) -> PathBuf {
    let mut name = path.as_os_str().to_os_string();
    name.push(suffix);
    PathBuf::from(name)
}

fn file_len(path: &Path) -> Result<u64> {
    Ok(fs::metadata(path)
        .with_context(|| format!("reading {}", path.display()))?
        .len())
}

pub(super) fn path_text(path: &Path) -> Result<&str> {
    path.to_str()
        .ok_or_else(|| anyhow!("{} is not valid UTF-8", path.display()))
}

#[cfg(all(test, feature = "encryption"))]
mod tests {
    use std::sync::mpsc;
    use std::thread;

    use super::*;
    use crate::keys::KeyGuard;
    use crate::schema::{Memory, MemoryType};
    use crate::sqlite_store::ReadDeadline;
    use crate::store::Store;

    fn key(byte: u8) -> StoreKey {
        StoreKey::from_bytes(KeyGuard::new([byte; 32]))
    }

    fn remember(store: &SqliteStore, n: usize) {
        for i in 0..n {
            store
                .store_memory(&Memory::new(
                    format!("row {i}"),
                    MemoryType::Semantic,
                    "test".to_string(),
                    String::new(),
                ))
                .unwrap();
        }
    }

    /// Holds a pooled read and a spare opened past a deadline on another
    /// thread until `release` fires.
    fn hold_a_spare<'scope>(
        store: &'scope SqliteStore,
        scope: &'scope thread::Scope<'scope, '_>,
        held: mpsc::Sender<()>,
        release: mpsc::Receiver<()>,
    ) {
        scope.spawn(move || {
            let pooled = store.reader();
            let _bound = ReadDeadline::set(Instant::now() + Duration::from_millis(10));
            let spare = store.reader();
            held.send(()).unwrap();
            release
                .recv_timeout(Duration::from_secs(20))
                .expect("the test releases the reads");
            drop(spare);
            drop(pooled);
        });
    }

    fn pool_of_one(dir: &Path) -> SqliteStore {
        SqliteStore::open_with_pool_size(&dir.join("brain.db"), "m".into(), 1).unwrap()
    }

    #[test]
    fn the_swap_waits_for_a_spare_read_and_the_new_wal_survives_it() {
        let dir = tempfile::tempdir().unwrap();
        let store = pool_of_one(dir.path());
        remember(&store, 3);
        thread::scope(|scope| {
            let (held, ready) = mpsc::channel();
            let (release, released) = mpsc::channel();
            hold_a_spare(&store, scope, held, released);
            ready.recv_timeout(Duration::from_secs(10)).unwrap();
            let releaser = scope.spawn(move || {
                thread::sleep(Duration::from_millis(300));
                release.send(()).unwrap();
            });
            store
                .rewrite_waiting(RewriteTo::Key(&key(1)), Duration::from_secs(10))
                .unwrap();
            releaser.join().unwrap();
        });
        remember(&store, 2);
        assert!(dir.path().join("brain.db-wal").exists());
        drop(store);
        let reopened =
            SqliteStore::open_with_key(&dir.path().join("brain.db"), "m".into(), &key(1)).unwrap();
        assert_eq!(reopened.memory_count().unwrap(), 5);
    }

    #[test]
    fn a_spare_read_held_past_the_wait_stops_the_rewrite_and_leaves_the_store_live() {
        let dir = tempfile::tempdir().unwrap();
        let store = pool_of_one(dir.path());
        remember(&store, 3);
        let result = thread::scope(|scope| {
            let (held, ready) = mpsc::channel();
            let (release, released) = mpsc::channel();
            hold_a_spare(&store, scope, held, released);
            ready.recv_timeout(Duration::from_secs(10)).unwrap();
            let result = store.rewrite_waiting(RewriteTo::Key(&key(2)), Duration::from_millis(200));
            release.send(()).unwrap();
            result
        });
        let err = format!("{:#}", result.unwrap_err());
        assert!(err.contains("held a store connection"), "{err}");
        assert!(!store.is_encrypted());
        assert!(!rewrite_path(&dir.path().join("brain.db")).exists());
        remember(&store, 1);
        assert_eq!(store.memory_count().unwrap(), 4);
    }

    #[test]
    fn reads_and_writes_fail_instead_of_waiting_when_the_store_cannot_reopen() {
        let dir = tempfile::tempdir().unwrap();
        let store = std::sync::Arc::new(pool_of_one(dir.path()));
        remember(&store, 2);
        FAIL_REOPEN.set(true);
        let err = store
            .rewrite_waiting(RewriteTo::Key(&key(3)), Duration::from_secs(5))
            .unwrap_err();
        FAIL_REOPEN.set(false);
        assert!(err.downcast_ref::<NotReopened>().is_some(), "{err:#}");

        let (done, read) = mpsc::channel();
        let reader = store.clone();
        thread::spawn(move || done.send(reader.memory_count().is_err()).unwrap());
        assert!(
            read.recv_timeout(Duration::from_secs(10))
                .expect("the read returns instead of waiting on an empty pool"),
            "a read on an unopened store fails"
        );
        assert!(
            store
                .store_memory(&Memory::new(
                    "after".to_string(),
                    MemoryType::Semantic,
                    "test".to_string(),
                    String::new(),
                ))
                .is_err()
        );
    }
}