second-brain-core 0.13.2

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
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail, ensure};
use chrono::{DateTime, Utc};
use rusqlite::{Connection, OptionalExtension, Transaction, params};
use uuid::Uuid;

use super::raw_sources::ARCHIVED_RAW;
use super::stored_redaction::unparseable;
use super::{MEMORY_COLS, RedactionPhase, SqliteStore, blob_to_embedding, row_to_memory};
use crate::cold::{
    COLD_AFTER_DAYS, ColdRecord, ColdTier, MAX_RECORD_BYTES, SEGMENT_BYTES, UnreadableSegment,
    stub_digest, stub_for, stub_matches,
};
use crate::schema::{Memory, Tier};

/// Rows read per candidate query; a segment takes them until it holds
/// `SEGMENT_BYTES`.
const CANDIDATE_BATCH: usize = 4096;

/// What one call to `seal_cold_segment` did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColdSeal {
    Sealed(SealedSegment),
    /// No archived raw is old enough.
    Nothing,
    /// No archive key is available, so every row stays hot.
    NoKey,
    /// The one-time stored-redaction pass has not finished, and content must
    /// not reach a segment before it was redacted.
    AwaitingRedaction,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SealedSegment {
    pub segment: String,
    pub rows: u64,
    pub bytes: u64,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
pub struct ColdStats {
    pub segments: u64,
    pub rows: u64,
    pub segment_bytes: u64,
    pub unreadable: Vec<UnreadableSegment>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ColdPassReport {
    pub sealed_segments: u64,
    pub sealed_rows: u64,
    pub sealed_bytes: u64,
    /// Segment files no manifest named any more.
    pub removed_files: u64,
    pub removed_partial_files: u64,
    /// Segments no row pointed at; their files go on the next pass.
    pub retired_segments: u64,
    /// Segments copied without the records no row pointed at.
    pub rewritten_segments: u64,
    pub unreadable: Vec<UnreadableSegment>,
    /// Why sealing stopped: `Nothing`, `NoKey` or `AwaitingRedaction`.
    pub stopped: Option<ColdSeal>,
}

struct CandidateKey {
    at: Vec<u8>,
    id: Vec<u8>,
}

struct Candidate {
    record: ColdRecord,
    embedding_blob: Option<Vec<u8>>,
    updated_at: String,
}

fn unreadable_known(tier: &ColdTier, known: &HashSet<String>) -> Vec<UnreadableSegment> {
    tier.unreadable()
        .into_iter()
        .filter(|u| known.contains(&u.segment))
        .collect()
}

fn manifest_on(conn: &Connection, segment: &str) -> Result<Option<String>> {
    Ok(conn
        .query_row(
            "SELECT manifest FROM archive_manifest WHERE segment_id = ?1",
            params![segment],
            |r| r.get(0),
        )
        .optional()?)
}

fn insert_manifest(tx: &Transaction<'_>, segment: &str, manifest: &str) -> Result<()> {
    tx.execute(
        "INSERT INTO archive_manifest (segment_id, manifest, created_at) VALUES (?1, ?2, ?3)",
        params![segment, manifest, Utc::now().to_rfc3339()],
    )?;
    Ok(())
}

fn retire_manifest_on(tx: &Transaction<'_>, segment: &str) -> Result<bool> {
    Ok(tx.execute(
        "DELETE FROM archive_manifest WHERE segment_id = ?1 \
         AND NOT EXISTS (SELECT 1 FROM memories WHERE archived_segment = ?1)",
        params![segment],
    )? > 0)
}

fn cold_row(conn: &Connection, id: Uuid) -> Result<Option<(String, Option<String>)>> {
    Ok(conn
        .query_row(
            "SELECT content, archived_segment FROM memories WHERE id = ?1",
            params![id.to_string()],
            |r| Ok((r.get(0)?, r.get(1)?)),
        )
        .optional()?)
}

impl SqliteStore {
    /// Gives the store the cold tier its cold rows live in. Only the first
    /// call takes effect; returns whether this one did.
    pub fn attach_cold_tier(&self, tier: Arc<ColdTier>) -> bool {
        self.cold.set(tier).is_ok()
    }

    pub fn cold_tier(&self) -> Option<&Arc<ColdTier>> {
        self.cold.get()
    }

    fn require_cold(&self) -> Result<&Arc<ColdTier>> {
        self.cold
            .get()
            .ok_or_else(|| anyhow!("this store holds cold rows but has no cold tier attached"))
    }

    /// Reads a cold record and checks it against the row's stub. No store
    /// connection may be held across this: opening a segment can look up
    /// keys in the store, so `manifest` loads through a connection of its
    /// own, or through the caller's write transaction.
    fn cold_record_with(
        &self,
        id: Uuid,
        segment: &str,
        stub: &str,
        manifest: impl FnOnce() -> Result<Option<String>>,
    ) -> Result<ColdRecord> {
        let record = self.require_cold()?.read(segment, id, manifest)?;
        ensure!(
            stub_digest(stub).is_some(),
            "cold memory {id} has no stub where its content was"
        );
        ensure!(
            stub_matches(stub, &record.content),
            "cold segment {segment} holds text for memory {id} that does not match its stub"
        );
        Ok(record)
    }

    fn cold_record_read(&self, id: Uuid, segment: &str, stub: &str) -> Result<ColdRecord> {
        self.cold_record_with(id, segment, stub, || manifest_on(&self.reader(), segment))
    }

    /// The full text and embedding of memory `id` from its cold segment, or
    /// None when the row is not cold. A segment that fails to open is an
    /// error, never an absent record.
    pub fn cold_record(&self, id: Uuid) -> Result<Option<ColdRecord>> {
        let row = cold_row(&self.reader(), id)?;
        match row {
            Some((stub, Some(segment))) => self.cold_record_read(id, &segment, &stub).map(Some),
            _ => Ok(None),
        }
    }

    /// `get_memory_with_embedding` with a cold row's content and embedding
    /// read back from its segment. Store readers otherwise see the stub.
    pub fn get_memory_full(&self, id: Uuid) -> Result<Option<Memory>> {
        let memory = self
            .reader()
            .query_row(
                &format!("SELECT {MEMORY_COLS}, embedding FROM memories WHERE id = ?1"),
                params![id.to_string()],
                |r| row_to_memory(r, true),
            )
            .optional()?;
        let Some(mut memory) = memory else {
            return Ok(None);
        };
        if let Some(segment) = memory.archived_segment.clone() {
            let record = self.cold_record_read(id, &segment, &memory.content)?;
            memory.content = record.content;
            memory.embedding = record.embedding;
        }
        Ok(Some(memory))
    }

    /// The form of `memory` a sync record carries: never its segment, which
    /// is local, for a cold row its full text and embedding read back from
    /// the segment, since a peer that took the stub would lose the text, and
    /// always its row's author and scope.
    pub(crate) fn sync_form<'m>(
        &self,
        tx: &Connection,
        memory: &'m Memory,
    ) -> Result<Cow<'m, Memory>> {
        let form = self.full_text_form(tx, memory)?;
        Self::with_row_attribution(tx, form)
    }

    pub(crate) fn full_text_form<'m>(
        &self,
        tx: &Connection,
        memory: &'m Memory,
    ) -> Result<Cow<'m, Memory>> {
        let Some(segment) = &memory.archived_segment else {
            return Ok(Cow::Borrowed(memory));
        };
        let mut full = memory.clone();
        full.archived_segment = None;
        if stub_digest(&memory.content).is_some() {
            let record = self
                .cold_record_with(memory.id, segment, &memory.content, || {
                    manifest_on(tx, segment)
                })
                .with_context(|| format!("logging cold memory {} for sync", memory.id))?;
            full.content = record.content;
            full.embedding = record.embedding;
        }
        Ok(Cow::Owned(full))
    }

    /// `memory` with the content and segment its row holds now, when either
    /// side is cold: a caller's copy can predate a seal or a segment rewrite.
    pub(crate) fn as_stored_now<'m>(
        tx: &Connection,
        memory: &'m Memory,
    ) -> Result<Cow<'m, Memory>> {
        let Some((content, segment)) = cold_row(tx, memory.id)? else {
            return Ok(Cow::Borrowed(memory));
        };
        if segment.is_none() && memory.archived_segment.is_none() {
            return Ok(Cow::Borrowed(memory));
        }
        let mut now = memory.clone();
        now.content = content;
        now.archived_segment = segment;
        Ok(Cow::Owned(now))
    }

    /// Fits an incoming synced row, its peer segment id already dropped, to
    /// this store's cold tier: an archived row whose text matches the local
    /// cold row's stub keeps the stub, so the update applies without
    /// bringing the text back into the hot store.
    pub(crate) fn keep_cold_stub(tx: &Transaction<'_>, incoming: &mut Memory) -> Result<()> {
        if incoming.tier != Tier::Archived {
            return Ok(());
        }
        if let Some((stub, Some(segment))) = cold_row(tx, incoming.id)?
            && stub_matches(&stub, &incoming.content)
        {
            incoming.content = stub;
            incoming.embedding.clear();
            incoming.archived_segment = Some(segment);
        }
        Ok(())
    }

    /// Archived raws old enough to seal, oldest first after `cursor`, up to
    /// one segment's worth; `cursor` moves past every row looked at, so a
    /// pass reads each row once. A row too large for a block, or one whose
    /// values do not convert, stays hot.
    fn cold_candidates(
        &self,
        older_than: DateTime<Utc>,
        cursor: &mut Option<CandidateKey>,
    ) -> Result<Vec<Candidate>> {
        let mut out = Vec::new();
        let mut bytes = 0;
        loop {
            let keys = self.candidate_keys(older_than, cursor.as_ref())?;
            if keys.is_empty() {
                return Ok(out);
            }
            for key in keys {
                let id = std::str::from_utf8(&key.id)
                    .ok()
                    .and_then(|id| Uuid::parse_str(id).ok());
                *cursor = Some(key);
                let Some(id) = id else {
                    continue;
                };
                let Some(candidate) = self.candidate(id)? else {
                    continue;
                };
                bytes += candidate.record.content.len()
                    + candidate.embedding_blob.as_ref().map_or(0, Vec::len);
                out.push(candidate);
                if bytes >= SEGMENT_BYTES {
                    return Ok(out);
                }
            }
        }
    }

    fn candidate_keys(
        &self,
        older_than: DateTime<Utc>,
        after: Option<&CandidateKey>,
    ) -> Result<Vec<CandidateKey>> {
        let conn = self.reader();
        let mut stmt = conn.prepare(&format!(
            "SELECT CAST(COALESCE(session_at, created_at) AS BLOB) AS at, CAST(id AS BLOB) AS bid \
             FROM memories \
             WHERE {ARCHIVED_RAW} AND archived_segment IS NULL \
             AND COALESCE(session_at, created_at) < ?1 \
             AND (?2 IS NULL OR (at, bid) > (?2, ?3)) \
             ORDER BY at, bid LIMIT ?4"
        ))?;
        let keys = stmt
            .query_map(
                params![
                    older_than.to_rfc3339(),
                    after.map(|k| &k.at),
                    after.map(|k| &k.id),
                    CANDIDATE_BATCH as i64
                ],
                |r| {
                    Ok(CandidateKey {
                        at: r.get(0)?,
                        id: r.get(1)?,
                    })
                },
            )?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(keys)
    }

    fn candidate(&self, id: Uuid) -> Result<Option<Candidate>> {
        let conn = self.reader();
        let row = conn
            .query_row(
                &format!(
                    "SELECT content, embedding, updated_at FROM memories \
                     WHERE id = ?1 AND {ARCHIVED_RAW} AND archived_segment IS NULL \
                     AND length(CAST(content AS BLOB)) + COALESCE(length(embedding), 0) <= ?2"
                ),
                params![id.to_string(), MAX_RECORD_BYTES as i64],
                |r| {
                    Ok((
                        r.get::<_, String>(0)?,
                        r.get::<_, Option<Vec<u8>>>(1)?,
                        r.get::<_, String>(2)?,
                    ))
                },
            )
            .optional();
        let (content, embedding_blob, updated_at) = match row {
            Ok(Some(row)) => row,
            Ok(None) => return Ok(None),
            Err(e) if unparseable(&e) => {
                tracing::debug!("cold tier: memory {id} stays hot: {e}");
                return Ok(None);
            }
            Err(e) => return Err(e.into()),
        };
        Ok(Some(Candidate {
            record: ColdRecord {
                id,
                content,
                embedding: embedding_blob
                    .as_deref()
                    .map(blob_to_embedding)
                    .unwrap_or_default(),
            },
            embedding_blob,
            updated_at,
        }))
    }

    /// Moves up to one segment's worth of archived raws older than
    /// `older_than` into a new cold segment. Each row keeps a stub of its
    /// content and loses its embedding; `updated_at`, the sync log and the
    /// index outbox are left alone, because archiving is local. A row that
    /// changed after it was read, its embedding included, stays as it is.
    pub fn seal_cold_segment(&self, older_than: DateTime<Utc>) -> Result<ColdSeal> {
        let tier = self.require_cold()?;
        let _pass = tier.pass_guard();
        self.seal_locked(tier, older_than, &mut None)
    }

    fn seal_locked(
        &self,
        tier: &ColdTier,
        older_than: DateTime<Utc>,
        cursor: &mut Option<CandidateKey>,
    ) -> Result<ColdSeal> {
        if self.redaction_phase()? != RedactionPhase::Done {
            return Ok(ColdSeal::AwaitingRedaction);
        }
        let candidates = self.cold_candidates(older_than, cursor)?;
        if candidates.is_empty() {
            return Ok(ColdSeal::Nothing);
        }
        let records: Vec<ColdRecord> = candidates.iter().map(|c| c.record.clone()).collect();
        let Some((segment, manifest)) = tier.write_segment(&records)? else {
            return Ok(ColdSeal::NoKey);
        };
        drop(records);
        let committed = self.with_txn(|tx| {
            insert_manifest(tx, &segment, &manifest)?;
            let mut stmt = tx.prepare(&format!(
                "UPDATE memories SET content = ?2, embedding = NULL, archived_segment = ?3 \
                 WHERE id = ?1 AND {ARCHIVED_RAW} AND archived_segment IS NULL \
                 AND updated_at = ?4 AND content = ?5 AND embedding IS ?6"
            ))?;
            let mut rows = 0u64;
            for c in &candidates {
                rows += stmt.execute(params![
                    c.record.id.to_string(),
                    stub_for(&c.record.content),
                    segment,
                    c.updated_at,
                    c.record.content,
                    c.embedding_blob,
                ])? as u64;
            }
            drop(stmt);
            if rows == 0 {
                retire_manifest_on(tx, &segment)?;
            }
            Ok(rows)
        });
        match committed {
            Ok(0) => {
                tier.remove_segment(&segment)?;
                Ok(ColdSeal::Nothing)
            }
            Ok(rows) => {
                let bytes = tier.segment_len(&segment).unwrap_or(0);
                Ok(ColdSeal::Sealed(SealedSegment {
                    segment,
                    rows,
                    bytes,
                }))
            }
            // A commit that reports an error may still have landed, so the
            // file is left for the sweep, which keeps any a manifest names.
            Err(e) => Err(e),
        }
    }

    pub fn cold_manifest(&self, segment: &str) -> Result<Option<String>> {
        manifest_on(&self.reader(), segment)
    }

    fn manifest_ids(&self) -> Result<HashSet<String>> {
        let conn = self.reader();
        let mut stmt = conn.prepare("SELECT segment_id FROM archive_manifest")?;
        let ids = stmt
            .query_map([], |r| r.get(0))?
            .collect::<rusqlite::Result<HashSet<String>>>()?;
        Ok(ids)
    }

    /// Opens every segment's header so one that would fail a recall shows
    /// up in status first. Returns the segments that failed.
    pub fn check_cold_segments(&self) -> Result<Vec<UnreadableSegment>> {
        let tier = self.require_cold()?;
        let known = self.manifest_ids()?;
        for segment in &known {
            if let Err(e) = tier.check(segment, || manifest_on(&self.reader(), segment)) {
                tracing::warn!("{e:#}");
            }
        }
        Ok(unreadable_known(tier, &known))
    }

    fn cold_rows_per_segment(&self) -> Result<HashMap<String, usize>> {
        let conn = self.reader();
        let mut stmt = conn.prepare(
            "SELECT archived_segment, COUNT(*) FROM memories \
             WHERE archived_segment IS NOT NULL GROUP BY archived_segment",
        )?;
        let counts = stmt
            .query_map([], |r| {
                Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)? as usize))
            })?
            .collect::<rusqlite::Result<HashMap<_, _>>>()?;
        Ok(counts)
    }

    /// Deletes segment files no manifest names: a write whose rows never
    /// committed, or a segment an earlier pass retired.
    pub(super) fn sweep_cold_files(&self, tier: &ColdTier) -> Result<(u64, u64)> {
        let known = self.manifest_ids()?;
        let (files, partial) = tier.files()?;
        let mut removed = 0;
        for segment in files.iter().filter(|s| !known.contains(*s)) {
            tier.remove_segment(segment)?;
            removed += 1;
        }
        let mut partials = 0;
        for path in partial {
            std::fs::remove_file(&path).with_context(|| format!("removing {}", path.display()))?;
            partials += 1;
        }
        Ok((removed, partials))
    }

    /// Retires the manifest of every segment no row points at any more, and
    /// rewrites a segment some of whose records no row points at, so text a
    /// synced edit or a hard forget replaced does not stay on disk.
    pub(super) fn retire_dead_records(&self, tier: &ColdTier) -> Result<(u64, u64)> {
        let live = self.cold_rows_per_segment()?;
        let (mut retired, mut rewritten) = (0, 0);
        for segment in self.manifest_ids()? {
            let listed = match tier.record_count(&segment, || manifest_on(&self.reader(), &segment))
            {
                Ok(n) => n,
                Err(e) => {
                    tracing::warn!("cold segment {segment}: {e:#}");
                    continue;
                }
            };
            match live.get(&segment).copied().unwrap_or(0) {
                0 => {
                    retired +=
                        u64::from(self.with_durable_txn(|tx| retire_manifest_on(tx, &segment))?)
                }
                n if n < listed => match self.rewrite_segment(tier, &segment) {
                    Ok(true) => rewritten += 1,
                    Ok(false) => {}
                    Err(e) => tracing::warn!("rewriting cold segment {segment}: {e:#}"),
                },
                _ => {}
            }
        }
        Ok((retired, rewritten))
    }

    /// Copies the records rows still point at into a new segment and moves
    /// those rows to it. False when no key is available to seal the copy.
    fn rewrite_segment(&self, tier: &ColdTier, segment: &str) -> Result<bool> {
        let rows: Vec<(String, String)> = {
            let conn = self.reader();
            let mut stmt =
                conn.prepare("SELECT id, content FROM memories WHERE archived_segment = ?1")?;
            stmt.query_map(params![segment], |r| Ok((r.get(0)?, r.get(1)?)))?
                .collect::<rusqlite::Result<_>>()?
        };
        let mut records = Vec::with_capacity(rows.len());
        for (id, stub) in &rows {
            records.push(self.cold_record_read(Uuid::parse_str(id)?, segment, stub)?);
        }
        let Some((copy, manifest)) = tier.write_segment(&records)? else {
            return Ok(false);
        };
        drop(records);
        self.with_durable_txn(|tx| {
            insert_manifest(tx, &copy, &manifest)?;
            let mut stmt = tx.prepare(
                "UPDATE memories SET archived_segment = ?3 \
                 WHERE id = ?1 AND archived_segment = ?2 AND content = ?4",
            )?;
            for (id, stub) in &rows {
                stmt.execute(params![id, segment, copy, stub])?;
            }
            drop(stmt);
            retire_manifest_on(tx, segment)?;
            Ok(())
        })?;
        Ok(true)
    }

    /// One full cold pass: deletes files earlier passes retired, retires or
    /// rewrites segments holding records no row points at, seals until
    /// nothing is old enough (pausing between segments so other writers get
    /// the writer lock), then checks every segment.
    pub fn run_cold_pass(&self, now: DateTime<Utc>, pause: Duration) -> Result<ColdPassReport> {
        let tier = self.require_cold()?;
        let _pass = tier.pass_guard();
        let mut report = ColdPassReport::default();
        (report.removed_files, report.removed_partial_files) = self.sweep_cold_files(tier)?;
        (report.retired_segments, report.rewritten_segments) = self.retire_dead_records(tier)?;
        let older_than = now - chrono::Duration::days(COLD_AFTER_DAYS);
        let mut cursor = None;
        let sealing = loop {
            match self.seal_locked(tier, older_than, &mut cursor) {
                Ok(ColdSeal::Sealed(sealed)) => {
                    report.sealed_segments += 1;
                    report.sealed_rows += sealed.rows;
                    report.sealed_bytes += sealed.bytes;
                    std::thread::sleep(pause);
                }
                Ok(stopped) => {
                    report.stopped = Some(stopped);
                    break Ok(());
                }
                Err(e) => break Err(e),
            }
        };
        report.unreadable = self.check_cold_segments()?;
        sealing?;
        Ok(report)
    }

    /// Fails when the store holds cold rows but no cold tier is attached,
    /// since none of them can then be read.
    pub fn cold_stats(&self) -> Result<ColdStats> {
        let (segments, rows) = {
            let conn = self.reader();
            let segments: i64 =
                conn.query_row("SELECT COUNT(*) FROM archive_manifest", [], |r| r.get(0))?;
            let rows: i64 = conn.query_row(
                "SELECT COUNT(*) FROM memories WHERE archived_segment IS NOT NULL",
                [],
                |r| r.get(0),
            )?;
            (segments as u64, rows as u64)
        };
        let Some(tier) = self.cold.get() else {
            if rows > 0 {
                bail!("{rows} cold rows but no cold tier attached to read them");
            }
            return Ok(ColdStats {
                segments,
                rows,
                ..ColdStats::default()
            });
        };
        let segment_bytes = tier
            .files()?
            .0
            .iter()
            .filter_map(|s| tier.segment_len(s))
            .sum();
        Ok(ColdStats {
            segments,
            rows,
            segment_bytes,
            unreadable: unreadable_known(tier, &self.manifest_ids()?),
        })
    }
}