second-brain-core 0.13.1

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
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
use std::collections::BTreeMap;

use anyhow::Result;
use rusqlite::types::{ToSqlOutput, Value, ValueRef};
use rusqlite::{Connection, OptionalExtension, Row, ToSql, Transaction, params};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::payload_redaction::scan_payload;
use super::{
    MEMORY_COLS, RewriteOutcome, SqliteStore, parse_relation_type, parse_uuid, row_to_entity,
    row_to_memory,
};
use crate::maintenance::EmbedBatchFn;
use crate::redact::{Finding, redact};
use crate::schema::{Entity, Relation, SyncOp};

const STATE_KEY: &str = "stored_redaction_v1";
const SKIPPED_KEY: &str = "stored_redaction_v1_skipped";
const SKIPPED_CAP: usize = 1000;
const BATCH: i64 = 256;
const REWRITE_ATTEMPTS: usize = 3;

/// Where the one-time pass over stored content stands. A cursor is the last
/// rowid its phase finished, so a restart resumes after it. Relations have
/// no rowid, so that phase restarts from the top; redacted rows no longer
/// match, which makes the rescan cheap and the outcome the same.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(tag = "phase", content = "after", rename_all = "snake_case")]
pub enum RedactionPhase {
    Memories(i64),
    Chunks(i64),
    Entities(i64),
    Relations,
    SyncLog(i64),
    Done,
}

impl RedactionPhase {
    fn encode(self) -> String {
        match self {
            Self::Memories(n) => format!("memories:{n}"),
            Self::Chunks(n) => format!("chunks:{n}"),
            Self::Entities(n) => format!("entities:{n}"),
            Self::Relations => "relations".to_string(),
            Self::SyncLog(n) => format!("sync_log:{n}"),
            Self::Done => "done".to_string(),
        }
    }

    /// An unreadable value restarts the pass, which is safe because every
    /// phase is idempotent.
    fn decode(value: &str) -> Self {
        let (phase, after) = value.split_once(':').unwrap_or((value, "0"));
        let Ok(after) = after.parse() else {
            return Self::Memories(0);
        };
        match phase {
            "memories" => Self::Memories(after),
            "chunks" => Self::Chunks(after),
            "entities" => Self::Entities(after),
            "relations" => Self::Relations,
            "sync_log" => Self::SyncLog(after),
            "done" => Self::Done,
            _ => Self::Memories(0),
        }
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct RedactionRun {
    pub memories_rewritten: u64,
    /// Rounds in which a row changed under every rewrite attempt; the pass
    /// stops its cursor before such a row and tries it again.
    pub memories_conflicted: u64,
    pub chunk_sets_dropped: u64,
    pub entities_rewritten: u64,
    pub relations_rewritten: u64,
    pub sync_rows_scrubbed: u64,
    /// Unparseable rows newly recorded as skipped in this run; a row already
    /// on the stored list is not counted again.
    pub rows_skipped: u64,
}

/// A row the pass could not parse. Its text was redacted in place (a chunk
/// row was deleted) without syncing or re-embedding it. `row` is `rowid N`,
/// `local_seq N` for `sync_log`, or `from_id/to_id/relation_type` for
/// `relations`; it and `reason` are redacted, since a corrupt value or an
/// error message can echo a secret.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SkippedRow {
    pub table: String,
    pub row: String,
    pub reason: String,
}

impl SkippedRow {
    fn new(table: &str, row: &str, reason: &str) -> Self {
        Self {
            table: table.to_string(),
            row: redact(row).text,
            reason: redact(reason).text,
        }
    }
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct SkippedRows {
    rows: Vec<SkippedRow>,
    /// Skipped rows past `SKIPPED_CAP`, counted but not listed.
    omitted: u64,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct KindCounts {
    /// Rows holding at least one finding.
    pub rows: u64,
    pub findings: BTreeMap<String, u64>,
}

impl KindCounts {
    fn add(&mut self, findings: &[Finding]) {
        if findings.is_empty() {
            return;
        }
        self.rows += 1;
        for finding in findings {
            *self
                .findings
                .entry(finding.kind.as_str().to_string())
                .or_default() += finding.count as u64;
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RedactionReport {
    pub state: RedactionPhase,
    pub memories: KindCounts,
    pub chunks: KindCounts,
    pub entities: KindCounts,
    pub relations: KindCounts,
    pub sync_log: KindCounts,
    pub skipped: Vec<SkippedRow>,
    pub skipped_omitted: u64,
}

impl SqliteStore {
    pub fn redaction_phase(&self) -> Result<RedactionPhase> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![STATE_KEY],
                |r| r.get(0),
            )
            .optional()?;
        Ok(value
            .as_deref()
            .map(RedactionPhase::decode)
            .unwrap_or(RedactionPhase::Memories(0)))
    }

    fn write_phase(tx: &Transaction<'_>, phase: RedactionPhase) -> Result<()> {
        tx.execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![STATE_KEY, phase.encode()],
        )?;
        Ok(())
    }

    /// Redacts each skipped row in place, lists the rows not already listed
    /// and returns how many were new.
    fn settle_skips(tx: &Transaction<'_>, skipped: &[Skip]) -> Result<u64> {
        if skipped.is_empty() {
            return Ok(0);
        }
        for skip in skipped {
            skip.raw.scrub(tx)?;
        }
        let mut stored = read_skipped(tx)?;
        let mut recorded = 0;
        for Skip { row, .. } in skipped {
            let listed = stored
                .rows
                .iter()
                .any(|r| r.table == row.table && r.row == row.row);
            if listed {
                continue;
            }
            recorded += 1;
            if stored.rows.len() < SKIPPED_CAP {
                stored.rows.push(row.clone());
            } else {
                stored.omitted += 1;
            }
        }
        if recorded > 0 {
            tx.execute(
                "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
                params![SKIPPED_KEY, serde_json::to_string(&stored)?],
            )?;
        }
        Ok(recorded)
    }

    /// The one-time pass that redacts content stored before redaction
    /// existed: every memory row whatever its tier (through
    /// `rewrite_memory_content`, so peers receive the redacted row), chunk
    /// sets the rewrite did not replace, entity names and aliases, relation
    /// context, and the payloads already in `sync_log`. Progress is kept in
    /// `index_meta`, not a schema migration, so peers on the same binary
    /// still agree on the schema while their passes run. A row that cannot be
    /// parsed has its raw text redacted in place and is recorded (see
    /// `RedactionReport::skipped`) before the cursor moves past it; any other
    /// error, including one from rewriting a row that parsed, fails the run.
    /// Returns immediately when the pass has already finished on this store.
    pub fn run_stored_redaction(&self, embed: Option<EmbedBatchFn<'_>>) -> Result<RedactionRun> {
        let mut run = RedactionRun::default();
        loop {
            let phase = self.redaction_phase()?;
            let mut skipped = Vec::new();
            let next = match phase {
                RedactionPhase::Memories(after) => {
                    self.redact_memory_batch(after, embed, &mut run, &mut skipped)?
                }
                RedactionPhase::Chunks(after) => {
                    self.redact_chunk_batch(after, &mut run, &mut skipped)?
                }
                RedactionPhase::Entities(after) => {
                    self.redact_entity_batch(after, &mut run, &mut skipped)?
                }
                RedactionPhase::Relations => self.redact_relations(&mut run)?,
                RedactionPhase::SyncLog(after) => self.scrub_sync_log_batch(after, &mut run)?,
                RedactionPhase::Done => return Ok(run),
            };
            if !matches!(phase, RedactionPhase::SyncLog(_)) {
                run.rows_skipped += self.with_txn(|tx| {
                    Self::write_phase(tx, next)?;
                    Self::settle_skips(tx, &skipped)
                })?;
            }
        }
    }

    fn rewrite_with_retries(
        &self,
        id: Uuid,
        embed: Option<EmbedBatchFn<'_>>,
    ) -> Result<RewriteOutcome> {
        let rewrite = |text: &str| redact(text).text;
        let mut outcome = RewriteOutcome::Conflict;
        for _ in 0..REWRITE_ATTEMPTS {
            outcome = self.rewrite_memory_content(id, &rewrite, embed)?;
            if outcome != RewriteOutcome::Conflict {
                break;
            }
        }
        Ok(outcome)
    }

    fn redact_memory_batch(
        &self,
        after: i64,
        embed: Option<EmbedBatchFn<'_>>,
        run: &mut RedactionRun,
        skipped: &mut Vec<Skip>,
    ) -> Result<RedactionPhase> {
        let rows: Vec<(i64, rusqlite::Result<Uuid>)> = {
            let conn = self.reader();
            let mut stmt = conn.prepare(&format!(
                "SELECT {MEMORY_COLS}, embedding, rowid AS row_key FROM memories WHERE rowid > ?1 \
                 ORDER BY rowid LIMIT ?2"
            ))?;
            let rows = stmt.query_map(params![after, BATCH], |r| {
                Ok((r.get("row_key")?, row_to_memory(r, true).map(|m| m.id)))
            })?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        let Some(&(last, _)) = rows.last() else {
            return Ok(RedactionPhase::Chunks(0));
        };
        let mut done = after;
        for (rowid, id) in rows {
            let Some(id) = parsed(id, || RawRow::Memory(rowid), skipped)? else {
                done = rowid;
                continue;
            };
            match self.rewrite_with_retries(id, embed)? {
                RewriteOutcome::Rewritten => run.memories_rewritten += 1,
                RewriteOutcome::Conflict => {
                    run.memories_conflicted += 1;
                    tracing::warn!(memory_id = %id, "redaction pass lost every rewrite race; retrying");
                    return Ok(RedactionPhase::Memories(done));
                }
                RewriteOutcome::Unchanged | RewriteOutcome::NotFound => {}
            }
            done = rowid;
        }
        Ok(RedactionPhase::Memories(last))
    }

    /// A chunk with a finding left after the memory pass belongs to a set the
    /// rewrite did not replace (stale, or of a row that left the live set
    /// before the index drained it). The set is dropped and an index upsert
    /// queued, so the drain removes its vectors and the chunk backfill splits
    /// the redacted content again.
    fn redact_chunk_batch(
        &self,
        after: i64,
        run: &mut RedactionRun,
        skipped: &mut Vec<Skip>,
    ) -> Result<RedactionPhase> {
        let rows: Vec<(i64, rusqlite::Result<(Uuid, bool)>)> = {
            let conn = self.reader();
            let mut stmt = conn.prepare(
                "SELECT rowid, memory_id, text FROM memory_chunks WHERE rowid > ?1 \
                 ORDER BY rowid LIMIT ?2",
            )?;
            let rows = stmt.query_map(params![after, BATCH], |r| {
                let chunk = || -> rusqlite::Result<(Uuid, bool)> {
                    let text: String = r.get(2)?;
                    Ok((
                        parse_uuid(&r.get::<_, String>(1)?)?,
                        redact(&text).changed(),
                    ))
                };
                Ok((r.get(0)?, chunk()))
            })?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        let Some(&(last, _)) = rows.last() else {
            return Ok(RedactionPhase::Entities(0));
        };
        let mut dirty = Vec::new();
        for (rowid, chunk) in rows {
            if let Some((id, true)) = parsed(chunk, || RawRow::Chunk(rowid), skipped)? {
                dirty.push(id);
            }
        }
        dirty.sort_unstable();
        dirty.dedup();
        self.with_txn(|tx| {
            for id in &dirty {
                tx.execute(
                    "DELETE FROM memory_chunks WHERE memory_id = ?1",
                    params![id.to_string()],
                )?;
                Self::append_outbox(tx, *id, "upsert")?;
            }
            Ok(())
        })?;
        run.chunk_sets_dropped += dirty.len() as u64;
        Ok(RedactionPhase::Chunks(last))
    }

    /// A rewritten entity is stored and synced as a whole (the store's only
    /// entity write). Its vector goes with a changed name, since it was
    /// computed from the secret.
    fn redact_entity_batch(
        &self,
        after: i64,
        run: &mut RedactionRun,
        skipped: &mut Vec<Skip>,
    ) -> Result<RedactionPhase> {
        let rows: Vec<(i64, rusqlite::Result<Entity>)> = {
            let conn = self.reader();
            let mut stmt = conn.prepare(
                "SELECT id, name, entity_type, embedding, aliases, rowid FROM entities \
                 WHERE rowid > ?1 ORDER BY rowid LIMIT ?2",
            )?;
            let rows =
                stmt.query_map(params![after, BATCH], |r| Ok((r.get(5)?, row_to_entity(r))))?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        let Some(&(last, _)) = rows.last() else {
            return Ok(RedactionPhase::Relations);
        };
        for (rowid, before) in rows {
            let Some(before) = parsed(before, || RawRow::Entity(rowid), skipped)? else {
                continue;
            };
            let Some(after) = redacted_entity(&before) else {
                continue;
            };
            let aliases = serde_json::to_string(&before.aliases)?;
            let written = self.with_txn(|tx| {
                let unchanged = tx
                    .query_row(
                        "SELECT 1 FROM entities WHERE id = ?1 AND name = ?2 AND aliases = ?3",
                        params![before.id.to_string(), before.name, aliases],
                        |_| Ok(()),
                    )
                    .optional()?
                    .is_some();
                if unchanged {
                    self.store_entity_on_tx(tx, &after, SyncOp::Update)?;
                }
                Ok(unchanged)
            })?;
            if written {
                run.entities_rewritten += 1;
            }
        }
        Ok(RedactionPhase::Entities(last))
    }

    /// Skips are recorded per batch, since this phase has no cursor to commit
    /// them with; a restart rescans and finds them already listed.
    fn redact_relations(&self, run: &mut RedactionRun) -> Result<RedactionPhase> {
        let mut cursor = [KeyPart::empty(), KeyPart::empty(), KeyPart::empty()];
        loop {
            let rows: Vec<([KeyPart; 3], rusqlite::Result<Relation>)> = {
                let conn = self.reader();
                let mut stmt = conn.prepare(
                    "SELECT from_id, to_id, relation_type, strength, context FROM relations \
                     WHERE (from_id, to_id, relation_type) > (?1, ?2, ?3) \
                     ORDER BY from_id, to_id, relation_type LIMIT ?4",
                )?;
                let rows =
                    stmt.query_map(params![cursor[0], cursor[1], cursor[2], BATCH], |r| {
                        let key = [
                            KeyPart::read(r, 0)?,
                            KeyPart::read(r, 1)?,
                            KeyPart::read(r, 2)?,
                        ];
                        Ok((key, row_to_relation(r)))
                    })?;
                rows.collect::<rusqlite::Result<Vec<_>>>()?
            };
            let Some((last, _)) = rows.last() else {
                return Ok(RedactionPhase::SyncLog(0));
            };
            cursor = last.clone();
            let mut skipped = Vec::new();
            let mut parsed_rows = Vec::new();
            for (key, relation) in rows {
                let raw = || RawRow::Relation(key.clone());
                if let Some(relation) = parsed(relation, raw, &mut skipped)? {
                    parsed_rows.push((key, relation));
                }
            }
            run.rows_skipped += self.with_txn(|tx| Self::settle_skips(tx, &skipped))?;
            for ([from, to, kind], before) in parsed_rows {
                let Some(context) = before.context.as_deref() else {
                    continue;
                };
                let redacted = redact(context);
                if !redacted.changed() {
                    continue;
                }
                let written = self.with_txn(|tx| {
                    let strength: Option<Option<f64>> = tx
                        .query_row(
                            "SELECT strength FROM relations WHERE from_id = ?1 AND to_id = ?2 \
                             AND relation_type = ?3 AND context = ?4",
                            params![from, to, kind, context],
                            |r| r.get(0),
                        )
                        .optional()?;
                    let Some(strength) = strength else {
                        return Ok(false);
                    };
                    let after = Relation {
                        strength: strength.unwrap_or(1.0) as f32,
                        context: Some(redacted.text.clone()),
                        ..before.clone()
                    };
                    self.store_relation_on_tx(tx, &after)?;
                    Ok(true)
                })?;
                if written {
                    run.relations_rewritten += 1;
                }
            }
        }
    }

    /// Rewrites payloads in place, keeping their `local_seq`, so a peer
    /// pulling from any cursor receives only redacted text. A payload loses
    /// its embedding when text the embedding was computed from changed; the
    /// rewrite's own entry, logged earlier in the pass, carries the new one.
    /// Each batch commits together with its cursor and its skipped rows.
    fn scrub_sync_log_batch(&self, after: i64, run: &mut RedactionRun) -> Result<RedactionPhase> {
        let rows: Vec<(i64, rusqlite::Result<(String, String)>)> = {
            let conn = self.reader();
            let mut stmt = conn.prepare(
                "SELECT local_seq, node_type, data FROM sync_log WHERE local_seq > ?1 \
                 AND data IS NOT NULL ORDER BY local_seq LIMIT ?2",
            )?;
            let rows = stmt.query_map(params![after, BATCH], |r| {
                Ok((r.get(0)?, r.get(1).and_then(|t| Ok((t, r.get(2)?)))))
            })?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        let next = match rows.last() {
            Some(&(last, _)) => RedactionPhase::SyncLog(last),
            None => RedactionPhase::Done,
        };
        let mut skipped = Vec::new();
        let mut scrubbed: Vec<(i64, String)> = Vec::new();
        for (seq, payload) in rows {
            let Some((node_type, data)) = parsed(payload, || RawRow::SyncLog(seq), &mut skipped)?
            else {
                continue;
            };
            match scan_payload(&node_type, &data) {
                Some((Some(data), _)) => scrubbed.push((seq, data)),
                Some((None, _)) => {}
                None => {
                    let reason = match serde_json::from_str::<serde_json::Value>(&data) {
                        Err(err) => format!("payload is not JSON: {err}"),
                        Ok(_) => "payload is not JSON".to_string(),
                    };
                    skipped.push(Skip::new(RawRow::SyncLog(seq), &reason));
                }
            }
        }
        let recorded = self.with_txn(|tx| {
            for (seq, data) in &scrubbed {
                tx.execute(
                    "UPDATE sync_log SET data = ?2 WHERE local_seq = ?1",
                    params![seq, data],
                )?;
            }
            Self::write_phase(tx, next)?;
            Self::settle_skips(tx, &skipped)
        })?;
        run.sync_rows_scrubbed += scrubbed.len() as u64;
        run.rows_skipped += recorded;
        Ok(next)
    }

    /// Counts what the pass would redact, by location and kind, without
    /// writing anything. Text is read leniently (invalid UTF-8 replaced, a
    /// blob read as text), as the pass reads rows it skips.
    pub fn stored_redaction_report(&self) -> Result<RedactionReport> {
        let state = self.redaction_phase()?;
        let conn = self.reader();
        let skipped = read_skipped(&conn)?;
        let mut report = RedactionReport {
            state,
            memories: KindCounts::default(),
            chunks: KindCounts::default(),
            entities: KindCounts::default(),
            relations: KindCounts::default(),
            sync_log: KindCounts::default(),
            skipped: skipped.rows,
            skipped_omitted: skipped.omitted,
        };
        let mut stmt = conn.prepare("SELECT content, context, retired_reason FROM memories")?;
        let mut rows = stmt.query([])?;
        while let Some(row) = rows.next()? {
            let mut findings = Vec::new();
            for col in 0..3 {
                findings.extend(text_findings(row, col)?);
            }
            report.memories.add(&findings);
        }
        let mut stmt = conn.prepare("SELECT text FROM memory_chunks")?;
        let mut rows = stmt.query([])?;
        while let Some(row) = rows.next()? {
            report.chunks.add(&text_findings(row, 0)?);
        }
        let mut stmt = conn.prepare("SELECT name, aliases FROM entities")?;
        let mut rows = stmt.query([])?;
        while let Some(row) = rows.next()? {
            let mut findings = text_findings(row, 0)?;
            if let Some(raw) = lossy_text(row, 1)? {
                let aliases =
                    serde_json::from_str::<Vec<String>>(&raw).unwrap_or_else(|_| vec![raw]);
                findings.extend(aliases.iter().flat_map(|alias| redact(alias).findings));
            }
            report.entities.add(&findings);
        }
        let mut stmt = conn.prepare("SELECT context FROM relations WHERE context IS NOT NULL")?;
        let mut rows = stmt.query([])?;
        while let Some(row) = rows.next()? {
            report.relations.add(&text_findings(row, 0)?);
        }
        let mut stmt =
            conn.prepare("SELECT node_type, data FROM sync_log WHERE data IS NOT NULL")?;
        let mut rows = stmt.query([])?;
        while let Some(row) = rows.next()? {
            let (Some(node_type), Some(data)) = (lossy_text(row, 0)?, lossy_text(row, 1)?) else {
                continue;
            };
            if let Some((_, findings)) = scan_payload(&node_type, &data) {
                report.sync_log.add(&findings);
            }
        }
        Ok(report)
    }
}

pub(super) fn unparseable(err: &rusqlite::Error) -> bool {
    matches!(
        err,
        rusqlite::Error::FromSqlConversionFailure(..)
            | rusqlite::Error::InvalidColumnType(..)
            | rusqlite::Error::Utf8Error(..)
            | rusqlite::Error::IntegralValueOutOfRange(..)
    )
}

/// The row as read, or None after queueing it as skipped when a value in it
/// could not be converted; any other error is returned.
fn parsed<T>(
    value: rusqlite::Result<T>,
    raw: impl FnOnce() -> RawRow,
    skipped: &mut Vec<Skip>,
) -> Result<Option<T>> {
    match value {
        Ok(value) => Ok(Some(value)),
        Err(err) if unparseable(&err) => {
            skipped.push(Skip::new(raw(), &err.to_string()));
            Ok(None)
        }
        Err(err) => Err(err.into()),
    }
}

struct Skip {
    row: SkippedRow,
    raw: RawRow,
}

impl Skip {
    fn new(raw: RawRow, reason: &str) -> Self {
        Self {
            row: SkippedRow::new(raw.table(), &raw.label(), reason),
            raw,
        }
    }
}

/// A skipped row's address, which reads without parsing anything in it.
enum RawRow {
    Memory(i64),
    Chunk(i64),
    Entity(i64),
    Relation([KeyPart; 3]),
    SyncLog(i64),
}

impl RawRow {
    fn table(&self) -> &'static str {
        match self {
            Self::Memory(_) => "memories",
            Self::Chunk(_) => "memory_chunks",
            Self::Entity(_) => "entities",
            Self::Relation(_) => "relations",
            Self::SyncLog(_) => "sync_log",
        }
    }

    fn label(&self) -> String {
        match self {
            Self::Memory(rowid) | Self::Chunk(rowid) | Self::Entity(rowid) => {
                format!("rowid {rowid}")
            }
            Self::Relation([from, to, kind]) => format!("{from}/{to}/{kind}"),
            Self::SyncLog(seq) => format!("local_seq {seq}"),
        }
    }

    fn scrub(&self, tx: &Transaction<'_>) -> Result<()> {
        match self {
            Self::Memory(rowid) => scrub_text(
                tx,
                "memories",
                &["content", "context", "retired_reason"],
                "rowid = ?",
                &[rowid],
            ),
            Self::Chunk(rowid) => {
                tx.execute("DELETE FROM memory_chunks WHERE rowid = ?1", [rowid])?;
                Ok(())
            }
            Self::Entity(rowid) => {
                scrub_text(tx, "entities", &["name", "aliases"], "rowid = ?", &[rowid])
            }
            Self::Relation([from, to, kind]) => scrub_text(
                tx,
                "relations",
                &["from_id", "to_id", "relation_type", "context"],
                "from_id IS ? AND to_id IS ? AND relation_type IS ?",
                &[from, to, kind],
            ),
            Self::SyncLog(seq) => scrub_text(tx, "sync_log", &["data"], "local_seq = ?", &[seq]),
        }
    }
}

/// Redacts `cols` of the row matching `key`, read leniently, and writes back
/// only the columns that changed. A relation key redacted into one already
/// stored replaces that row, which is safe because the marker makes every
/// such key unparseable, so the replaced row was a skipped row too.
fn scrub_text(
    tx: &Transaction<'_>,
    table: &str,
    cols: &[&str],
    key: &str,
    key_params: &[&dyn ToSql],
) -> Result<()> {
    let values: Option<Vec<Option<String>>> = tx
        .query_row(
            &format!("SELECT {} FROM {table} WHERE {key}", cols.join(", ")),
            key_params,
            |r| (0..cols.len()).map(|col| lossy_text(r, col)).collect(),
        )
        .optional()?;
    let changed: Vec<(&str, String)> = cols
        .iter()
        .zip(values.into_iter().flatten())
        .filter_map(|(col, value)| {
            let redacted = redact(&value?);
            redacted.changed().then_some((*col, redacted.text))
        })
        .collect();
    if changed.is_empty() {
        return Ok(());
    }
    let sets: Vec<String> = changed
        .iter()
        .map(|(col, _)| format!("{col} = ?"))
        .collect();
    let mut bound: Vec<&dyn ToSql> = changed.iter().map(|(_, text)| text as &dyn ToSql).collect();
    bound.extend_from_slice(key_params);
    tx.execute(
        &format!(
            "UPDATE OR REPLACE {table} SET {} WHERE {key}",
            sets.join(", ")
        ),
        bound.as_slice(),
    )?;
    Ok(())
}

/// An unreadable list is replaced rather than failing the pass on its own
/// bookkeeping.
fn read_skipped(conn: &Connection) -> Result<SkippedRows> {
    let value: Option<String> = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = ?1",
            params![SKIPPED_KEY],
            |r| r.get(0),
        )
        .optional()?;
    let Some(value) = value else {
        return Ok(SkippedRows::default());
    };
    Ok(serde_json::from_str(&value).unwrap_or_else(|err| {
        tracing::warn!("unreadable skipped-row list, starting a new one: {err}");
        SkippedRows::default()
    }))
}

fn lossy_text(row: &Row<'_>, col: usize) -> rusqlite::Result<Option<String>> {
    Ok(match row.get_ref(col)? {
        ValueRef::Text(bytes) | ValueRef::Blob(bytes) => {
            Some(String::from_utf8_lossy(bytes).into_owned())
        }
        _ => None,
    })
}

fn text_findings(row: &Row<'_>, col: usize) -> rusqlite::Result<Vec<Finding>> {
    Ok(lossy_text(row, col)?
        .map(|text| redact(&text).findings)
        .unwrap_or_default())
}

fn row_to_relation(r: &Row<'_>) -> rusqlite::Result<Relation> {
    Ok(Relation {
        from_id: parse_uuid(&r.get::<_, String>(0)?)?,
        to_id: parse_uuid(&r.get::<_, String>(1)?)?,
        relation_type: parse_relation_type(&r.get::<_, String>(2)?)?,
        strength: r.get::<_, Option<f64>>(3)?.unwrap_or(1.0) as f32,
        context: r.get(4)?,
    })
}

/// A relation key column as stored, so the cursor can move past a value
/// that is not valid UTF-8 text.
#[derive(Clone)]
enum KeyPart {
    Text(Vec<u8>),
    Other(Value),
}

impl KeyPart {
    fn empty() -> Self {
        Self::Text(Vec::new())
    }

    fn read(row: &Row<'_>, col: usize) -> rusqlite::Result<Self> {
        Ok(match row.get_ref(col)? {
            ValueRef::Text(bytes) => Self::Text(bytes.to_vec()),
            ValueRef::Blob(bytes) => Self::Other(Value::Blob(bytes.to_vec())),
            ValueRef::Integer(n) => Self::Other(Value::Integer(n)),
            ValueRef::Real(n) => Self::Other(Value::Real(n)),
            ValueRef::Null => Self::Other(Value::Null),
        })
    }
}

impl ToSql for KeyPart {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        match self {
            Self::Text(bytes) => Ok(ToSqlOutput::Borrowed(ValueRef::Text(bytes))),
            Self::Other(value) => value.to_sql(),
        }
    }
}

impl std::fmt::Display for KeyPart {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Text(bytes) => f.write_str(&String::from_utf8_lossy(bytes)),
            Self::Other(Value::Blob(bytes)) => write!(f, "<{} byte blob>", bytes.len()),
            Self::Other(value) => write!(f, "{value:?}"),
        }
    }
}

fn redacted_entity(entity: &Entity) -> Option<Entity> {
    let name = redact(&entity.name);
    let aliases: Vec<_> = entity.aliases.iter().map(|a| redact(a)).collect();
    if !name.changed() && !aliases.iter().any(|a| a.changed()) {
        return None;
    }
    Some(Entity {
        id: entity.id,
        embedding: if name.changed() {
            Vec::new()
        } else {
            entity.embedding.clone()
        },
        name: name.text,
        entity_type: entity.entity_type.clone(),
        aliases: aliases.into_iter().map(|a| a.text).collect(),
    })
}