second_brain_core/sqlite_store/
rewrite.rs1use anyhow::Result;
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 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}
33
34impl SqliteStore {
35 pub fn rewrite_memory_content(
46 &self,
47 memory_id: Uuid,
48 rewrite: &dyn Fn(&str) -> String,
49 embed: Option<EmbedBatchFn<'_>>,
50 ) -> Result<RewriteOutcome> {
51 let id = memory_id.to_string();
52 let Some(before) = self.rewrite_snapshot(&id)? else {
53 return Ok(RewriteOutcome::NotFound);
54 };
55 let old = &before.texts;
56 let new = Texts {
57 content: rewrite(&old.content),
58 context: old.context.as_deref().map(rewrite),
59 retired_reason: old.retired_reason.as_deref().map(rewrite),
60 };
61 if new == *old {
62 return Ok(RewriteOutcome::Unchanged);
63 }
64 let body_changed = new.content != old.content || new.context != old.context;
65 let vectors = body_changed.then(|| {
66 let body = body_of(new.context.clone(), new.content.clone());
67 let embedding = match embed {
68 Some(embed) if before.embedded || before.live => embed_one(embed, memory_id, &body),
69 _ => None,
70 };
71 let chunks = match embed {
72 Some(embed) if before.live => embed_chunks(embed, memory_id, &new.content),
73 _ => Vec::new(),
74 };
75 (embedding, chunks)
76 });
77
78 self.with_txn(|tx| {
79 if Self::texts_on_tx(tx, &id)?.as_ref() != Some(old) {
80 return Ok(RewriteOutcome::Conflict);
81 }
82 tx.execute(
83 "UPDATE memories SET content = ?2, context = ?3, retired_reason = ?4, \
84 updated_at = ?5 WHERE id = ?1",
85 params![
86 id,
87 new.content,
88 new.context,
89 new.retired_reason,
90 Utc::now().to_rfc3339(),
91 ],
92 )?;
93 if let Some((embedding, chunks)) = &vectors {
94 tx.execute(
95 "UPDATE memories SET embedding = ?2 WHERE id = ?1",
96 params![id, embedding.as_deref().map(embedding_to_blob)],
97 )?;
98 Self::replace_chunk_rows(
99 tx,
100 &id,
101 &chunk::content_hash(&new.content),
102 chunks
103 .iter()
104 .enumerate()
105 .map(|(ord, (text, vector))| (ord, text.as_str(), vector.as_slice())),
106 )?;
107 Self::append_outbox(tx, memory_id, "upsert")?;
108 }
109 if let Some(memory) = Self::memory_on_tx(tx, memory_id)? {
110 self.log_memory_update(tx, &memory)?;
111 }
112 Ok(RewriteOutcome::Rewritten)
113 })
114 }
115
116 fn texts_on_tx(tx: &Transaction<'_>, id: &str) -> Result<Option<Texts>> {
117 tx.query_row(
118 "SELECT content, context, retired_reason FROM memories WHERE id = ?1",
119 params![id],
120 |r| {
121 Ok(Texts {
122 content: r.get(0)?,
123 context: r.get(1)?,
124 retired_reason: r.get(2)?,
125 })
126 },
127 )
128 .optional()
129 .map_err(Into::into)
130 }
131
132 fn rewrite_snapshot(&self, id: &str) -> Result<Option<Snapshot>> {
133 let conn = self.reader();
134 conn.query_row(
135 "SELECT content, context, retired_reason, tier = 'hot' AND superseded_by IS NULL, \
136 embedding IS NOT NULL FROM memories WHERE id = ?1",
137 params![id],
138 |r| {
139 Ok(Snapshot {
140 texts: Texts {
141 content: r.get(0)?,
142 context: r.get(1)?,
143 retired_reason: r.get(2)?,
144 },
145 live: r.get(3)?,
146 embedded: r.get(4)?,
147 })
148 },
149 )
150 .optional()
151 .map_err(Into::into)
152 }
153}
154
155fn embed_one(embed: EmbedBatchFn<'_>, memory_id: Uuid, body: &str) -> Option<Vec<f32>> {
156 match embed(&[body]) {
157 Ok(mut v) if v.len() == 1 && v[0].len() == EMBEDDING_DIM => v.pop(),
158 Ok(_) => {
159 tracing::warn!(%memory_id, "rewrite embedding had the wrong shape");
160 None
161 }
162 Err(e) => {
163 tracing::warn!(%memory_id, "rewrite embedding failed: {e:#}");
164 None
165 }
166 }
167}
168
169fn embed_chunks(
172 embed: EmbedBatchFn<'_>,
173 memory_id: Uuid,
174 content: &str,
175) -> Vec<(String, Vec<f32>)> {
176 let texts = chunk::split(content);
177 if texts.is_empty() {
178 return Vec::new();
179 }
180 let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
181 match embed(&refs) {
182 Ok(v) if v.len() == texts.len() && v.iter().all(|e| e.len() == EMBEDDING_DIM) => {
183 texts.into_iter().zip(v).collect()
184 }
185 Ok(_) => {
186 tracing::warn!(%memory_id, "rewrite chunk embedding had the wrong shape");
187 Vec::new()
188 }
189 Err(e) => {
190 tracing::warn!(%memory_id, "rewrite chunk embedding failed: {e:#}");
191 Vec::new()
192 }
193 }
194}