Skip to main content

second_brain_core/sqlite_store/
rewrite.rs

1use anyhow::{Result, bail};
2use chrono::Utc;
3use rusqlite::{OptionalExtension, Transaction, params};
4use uuid::Uuid;
5
6use super::{SqliteStore, body_of, embedding_to_blob};
7use crate::chunk;
8use crate::index::EMBEDDING_DIM;
9use crate::maintenance::EmbedBatchFn;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum RewriteOutcome {
13    Rewritten,
14    Unchanged,
15    NotFound,
16    /// The row's content or context changed between the read and the write,
17    /// so nothing was written; a later pass sees the newer text.
18    Conflict,
19}
20
21#[derive(PartialEq)]
22struct Texts {
23    content: String,
24    context: Option<String>,
25    retired_reason: Option<String>,
26}
27
28struct Snapshot {
29    texts: Texts,
30    live: bool,
31    embedded: bool,
32    cold: bool,
33}
34
35impl SqliteStore {
36    /// Rewrites a memory's content, context and retirement reason through
37    /// `rewrite`, whatever its tier. Vectors are computed before the write
38    /// transaction, so the writer lock is never held across model inference;
39    /// the transaction then stores the text, the memory vector, a fresh chunk
40    /// set (live rows only), an index upsert and a sync Update carrying the
41    /// new row. Without `embed`, or when embedding fails, the memory vector
42    /// and chunks are dropped and the repair loop's backfills restore them. A
43    /// row outside the live set that had no vector gets none. A change to the
44    /// reason alone keeps the vectors and chunks, which are not derived from
45    /// it. A cold row is an error, since its row holds only a stub.
46    pub fn rewrite_memory_content(
47        &self,
48        memory_id: Uuid,
49        rewrite: &dyn Fn(&str) -> String,
50        embed: Option<EmbedBatchFn<'_>>,
51    ) -> Result<RewriteOutcome> {
52        let id = memory_id.to_string();
53        let Some(before) = self.rewrite_snapshot(&id)? else {
54            return Ok(RewriteOutcome::NotFound);
55        };
56        if before.cold {
57            bail!(
58                "memory {memory_id} is in the cold tier; its text is sealed in a segment, not the row"
59            );
60        }
61        let old = &before.texts;
62        let new = Texts {
63            content: rewrite(&old.content),
64            context: old.context.as_deref().map(rewrite),
65            retired_reason: old.retired_reason.as_deref().map(rewrite),
66        };
67        if new == *old {
68            return Ok(RewriteOutcome::Unchanged);
69        }
70        let body_changed = new.content != old.content || new.context != old.context;
71        let vectors = body_changed.then(|| {
72            let body = body_of(new.context.clone(), new.content.clone());
73            let embedding = match embed {
74                Some(embed) if before.embedded || before.live => embed_one(embed, memory_id, &body),
75                _ => None,
76            };
77            let chunks = match embed {
78                Some(embed) if before.live => embed_chunks(embed, memory_id, &new.content),
79                _ => Vec::new(),
80            };
81            (embedding, chunks)
82        });
83
84        self.with_txn(|tx| {
85            if Self::texts_on_tx(tx, &id)?.as_ref() != Some(old) {
86                return Ok(RewriteOutcome::Conflict);
87            }
88            tx.execute(
89                "UPDATE memories SET content = ?2, context = ?3, retired_reason = ?4, \
90                 updated_at = ?5 WHERE id = ?1",
91                params![
92                    id,
93                    new.content,
94                    new.context,
95                    new.retired_reason,
96                    Utc::now().to_rfc3339(),
97                ],
98            )?;
99            if let Some((embedding, chunks)) = &vectors {
100                tx.execute(
101                    "UPDATE memories SET embedding = ?2 WHERE id = ?1",
102                    params![id, embedding.as_deref().map(embedding_to_blob)],
103                )?;
104                Self::replace_chunk_rows(
105                    tx,
106                    &id,
107                    &chunk::content_hash(&new.content),
108                    chunks
109                        .iter()
110                        .enumerate()
111                        .map(|(ord, (text, vector))| (ord, text.as_str(), vector.as_slice())),
112                )?;
113                Self::append_outbox(tx, memory_id, "upsert")?;
114            }
115            if let Some(memory) = Self::memory_on_tx(tx, memory_id)? {
116                self.log_memory_update(tx, &memory)?;
117            }
118            Ok(RewriteOutcome::Rewritten)
119        })
120    }
121
122    fn texts_on_tx(tx: &Transaction<'_>, id: &str) -> Result<Option<Texts>> {
123        tx.query_row(
124            "SELECT content, context, retired_reason FROM memories WHERE id = ?1",
125            params![id],
126            |r| {
127                Ok(Texts {
128                    content: r.get(0)?,
129                    context: r.get(1)?,
130                    retired_reason: r.get(2)?,
131                })
132            },
133        )
134        .optional()
135        .map_err(Into::into)
136    }
137
138    fn rewrite_snapshot(&self, id: &str) -> Result<Option<Snapshot>> {
139        let conn = self.reader();
140        conn.query_row(
141            "SELECT content, context, retired_reason, tier = 'hot' AND superseded_by IS NULL, \
142             embedding IS NOT NULL, archived_segment IS NOT NULL FROM memories WHERE id = ?1",
143            params![id],
144            |r| {
145                Ok(Snapshot {
146                    texts: Texts {
147                        content: r.get(0)?,
148                        context: r.get(1)?,
149                        retired_reason: r.get(2)?,
150                    },
151                    live: r.get(3)?,
152                    embedded: r.get(4)?,
153                    cold: r.get(5)?,
154                })
155            },
156        )
157        .optional()
158        .map_err(Into::into)
159    }
160}
161
162fn embed_one(embed: EmbedBatchFn<'_>, memory_id: Uuid, body: &str) -> Option<Vec<f32>> {
163    match embed(&[body]) {
164        Ok(mut v) if v.len() == 1 && v[0].len() == EMBEDDING_DIM => v.pop(),
165        Ok(_) => {
166            tracing::warn!(%memory_id, "rewrite embedding had the wrong shape");
167            None
168        }
169        Err(e) => {
170            tracing::warn!(%memory_id, "rewrite embedding failed: {e:#}");
171            None
172        }
173    }
174}
175
176/// All of the content's chunks with their vectors, or none: a partial set
177/// would read as complete to `scan_chunk_work`, which only compares hashes.
178fn embed_chunks(
179    embed: EmbedBatchFn<'_>,
180    memory_id: Uuid,
181    content: &str,
182) -> Vec<(String, Vec<f32>)> {
183    let texts = chunk::split(content);
184    if texts.is_empty() {
185        return Vec::new();
186    }
187    let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
188    match embed(&refs) {
189        Ok(v) if v.len() == texts.len() && v.iter().all(|e| e.len() == EMBEDDING_DIM) => {
190            texts.into_iter().zip(v).collect()
191        }
192        Ok(_) => {
193            tracing::warn!(%memory_id, "rewrite chunk embedding had the wrong shape");
194            Vec::new()
195        }
196        Err(e) => {
197            tracing::warn!(%memory_id, "rewrite chunk embedding failed: {e:#}");
198            Vec::new()
199        }
200    }
201}