Skip to main content

second_brain_core/sqlite_store/
delete.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use anyhow::{Result, bail};
4use chrono::Utc;
5use rusqlite::{Connection, OptionalExtension, Transaction, params};
6use serde::Serialize;
7use uuid::Uuid;
8
9use super::authored_order::{self, Tombstone};
10use super::sessions::session_of;
11use super::{SqliteStore, blocklist, delete_status, settled, sync_dedup};
12use crate::schema::{AGENT_NOTE_SOURCE, Conversation, Memory, SyncNodeType, SyncOp};
13
14const PURGE_REQUESTED: &str = "delete_purge_requested";
15
16/// What one delete removes: a memory by id, everything a Claude Code
17/// session produced (its transcript's raws, its subagents', notes filed
18/// under it), or everything filed under a stored project path.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum DeleteScope {
21    Memory(Uuid),
22    Session(String),
23    Project(String),
24}
25
26#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
27pub struct DeleteCounts {
28    pub memories: u64,
29    /// Memories some distillation marker names as its distillate.
30    pub distillates: u64,
31    pub chunks: u64,
32    pub relations: u64,
33    pub markers: u64,
34    pub conversations: u64,
35    pub vector_keys: u64,
36    pub chunk_vector_keys: u64,
37    pub bm25: u64,
38    pub cold_records: u64,
39    /// Memories a removed memory had superseded, live again.
40    pub restored: u64,
41    /// Earlier create and update log records of the removed rows, reclaimed.
42    pub log_records: u64,
43}
44
45#[derive(Debug, Clone, Default, PartialEq, Eq)]
46pub struct DeleteReport {
47    pub counts: DeleteCounts,
48    pub memory_ids: Vec<Uuid>,
49    /// Claude Code sessions whose conversation is now tombstoned, so their
50    /// transcripts leave the archive and are never captured again.
51    pub sessions: Vec<String>,
52    pub conversations: Vec<Uuid>,
53    /// Set once the delete ran, never by a preview: its id for
54    /// `delete_status` and the log seq a peer must acknowledge.
55    pub delete_id: Option<Uuid>,
56    pub seq: Option<u64>,
57}
58
59impl DeleteReport {
60    pub fn is_empty(&self) -> bool {
61        self.memory_ids.is_empty() && self.sessions.is_empty() && self.conversations.is_empty()
62    }
63}
64
65#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
66pub struct ColdPurge {
67    pub retired: u64,
68    pub rewritten: u64,
69    pub removed_files: u64,
70}
71
72struct Target {
73    id: String,
74    distillate: bool,
75}
76
77struct Plan {
78    targets: Vec<Target>,
79    markers: BTreeSet<String>,
80    /// Out-of-scope raws whose marker named a removed distillate, moved to
81    /// the restored member of their own session.
82    repoint: BTreeMap<String, Uuid>,
83    restored: Vec<Uuid>,
84    conversations: BTreeSet<Uuid>,
85    sessions: BTreeSet<String>,
86}
87
88struct Matched {
89    source: String,
90    source_id: String,
91    conversation: Option<String>,
92}
93
94const MEMORY_COLUMNS: &str = "SELECT id, source, source_id, conversation_id FROM memories";
95
96impl SqliteStore {
97    /// Removes the scope's memories, the distillates any of them was
98    /// distilled into, their chunks, relations and markers, and the scope's
99    /// conversations, all in one transaction. Every removed memory and
100    /// conversation is tombstoned and the delete logged, so it wins on
101    /// every peer, and the earlier log records of each removed row are
102    /// reclaimed so the log keeps none of its content.
103    /// `archived_sessions` are sessions the caller found in the transcript
104    /// archive under this scope; their conversations are tombstoned even
105    /// when the store holds nothing of them.
106    pub fn delete_scope(
107        &self,
108        scope: &DeleteScope,
109        archived_sessions: &[String],
110    ) -> Result<DeleteReport> {
111        check_scope(scope)?;
112        self.with_txn(|tx| {
113            let plan = plan(tx, scope, archived_sessions)?;
114            let mut report = self.execute(tx, &plan)?;
115            if !report.is_empty() {
116                let (id, seq) = delete_status::record(tx, scope)?;
117                report.delete_id = Some(id);
118                report.seq = Some(seq);
119            }
120            Ok(report)
121        })
122    }
123
124    fn execute(&self, tx: &Transaction<'_>, plan: &Plan) -> Result<DeleteReport> {
125        let mut report = counted(tx, plan)?;
126        if report.is_empty() {
127            return Ok(report);
128        }
129        for raw in &plan.markers {
130            let Ok(raw_id) = Uuid::parse_str(raw) else {
131                continue;
132            };
133            if let Some(member) = plan.repoint.get(raw) {
134                self.repoint_marker_on_tx(tx, raw_id, *member)?;
135            } else if self.drop_marker_on_tx(tx, raw_id)? {
136                report.counts.markers += 1;
137            }
138        }
139        for id in &plan.restored {
140            self.restore_superseded_on_tx(tx, *id)?;
141        }
142        for target in &plan.targets {
143            report.counts.log_records += erase_memory_traces(tx, &target.id)?;
144            blocklist::block_row(tx, &target.id, None)?;
145            tx.execute("DELETE FROM memories WHERE id = ?1", params![target.id])?;
146            let authored = self.authored_now(tx)?;
147            Self::upsert_tombstone(tx, &target.id, "memory", &self.machine_id, authored)?;
148            if let Ok(id) = Uuid::parse_str(&target.id) {
149                Self::append_outbox(tx, id, "delete")?;
150            }
151            self.append_local_log_at(
152                tx,
153                SyncOp::Delete,
154                SyncNodeType::Memory,
155                &target.id,
156                None,
157                authored,
158            )?;
159        }
160        for id in &plan.conversations {
161            let id = id.to_string();
162            report.counts.log_records += erase_conversation_traces(tx, &id)?;
163            let removed = tx.execute("DELETE FROM conversations WHERE id = ?1", params![id])?;
164            if removed > 0 {
165                tx.execute("DELETE FROM relations WHERE to_id = ?1", params![id])?;
166            }
167            let authored = self.authored_now(tx)?;
168            let tombstone = Tombstone {
169                deleted_at: authored.timestamp_micros(),
170                origin: self.machine_id.clone(),
171            };
172            authored_order::record_tombstone(tx, "conversation", &id, &tombstone)?;
173            let data = authored_order::data_with_updated_by(
174                &serde_json::json!({ "id": id }),
175                &self.machine_id,
176            )?;
177            self.append_local_log_at(
178                tx,
179                SyncOp::Delete,
180                SyncNodeType::Conversation,
181                &id,
182                Some(&data),
183                authored,
184            )?;
185        }
186        Ok(report)
187    }
188
189    /// Deletes, tombstoned and logged as this store's own, every row of an
190    /// applied conversation delete's session that the deleting peer never
191    /// named because it never held them, so they reach no store again.
192    pub(super) fn cascade_conversation_delete(
193        &self,
194        tx: &Transaction<'_>,
195        conversation_id: &str,
196    ) -> Result<()> {
197        let Ok(id) = Uuid::parse_str(conversation_id) else {
198            return Ok(());
199        };
200        let mut matched: BTreeMap<String, Matched> = matching(
201            tx,
202            &format!("{MEMORY_COLUMNS} WHERE conversation_id = ?1"),
203            conversation_id,
204        )?
205        .into_iter()
206        .collect();
207        let mut sessions = BTreeSet::new();
208        for m in matched.values() {
209            if let Some(session) = session_source(&m.source, &m.source_id)
210                && Conversation::claude_code_id(&session) == id
211            {
212                sessions.insert(session);
213            }
214        }
215        for key in ingested_session_keys(tx)? {
216            let base = key.split('/').next().unwrap_or(&key);
217            if Conversation::claude_code_id(&key) == id || Conversation::claude_code_id(base) == id
218            {
219                sessions.insert(base.to_string());
220            }
221        }
222        for session in &sessions {
223            matched.extend(session_rows(tx, session)?);
224        }
225        if matched.is_empty() {
226            return Ok(());
227        }
228        let conversations = scope_conversations(&matched, &sessions);
229        let plan = plan_of(tx, matched, conversations, sessions)?;
230        self.execute(tx, &plan)?;
231        Ok(())
232    }
233
234    fn repoint_marker_on_tx(&self, tx: &Transaction<'_>, raw: Uuid, member: Uuid) -> Result<()> {
235        let Some((marker, _)) = authored_order::marker_stamp(tx, raw)? else {
236            return Ok(());
237        };
238        let model = marker.model.clone().unwrap_or_default();
239        self.mark_distillation_on_tx(tx, raw, member, &model, &marker.status)
240    }
241
242    fn restore_superseded_on_tx(&self, tx: &Transaction<'_>, id: Uuid) -> Result<()> {
243        let changed = tx.execute(
244            "UPDATE memories SET superseded_by = NULL, updated_at = ?2 \
245             WHERE id = ?1 AND superseded_by IS NOT NULL",
246            params![id.to_string(), Utc::now().to_rfc3339()],
247        )?;
248        if changed == 0 {
249            return Ok(());
250        }
251        Self::append_outbox(tx, id, "upsert")?;
252        if let Some(memory) = Self::memory_on_tx(tx, id)? {
253            self.log_memory_update(tx, &memory)?;
254        }
255        Ok(())
256    }
257
258    /// A token while a delete applied from a peer still waits for the index
259    /// compaction and cold purge the deleting daemon ran itself.
260    pub fn delete_purge_requested(&self) -> Result<Option<String>> {
261        Ok(self
262            .reader()
263            .query_row(
264                "SELECT value FROM index_meta WHERE key = ?1",
265                params![PURGE_REQUESTED],
266                |r| r.get(0),
267            )
268            .optional()?)
269    }
270
271    /// Clears the request `token` named, unless a delete applied since.
272    pub fn delete_purge_done(&self, token: &str) -> Result<()> {
273        self.conn().execute(
274            "DELETE FROM index_meta WHERE key = ?1 AND value = ?2",
275            params![PURGE_REQUESTED, token],
276        )?;
277        Ok(())
278    }
279
280    /// What `delete_scope` would remove, counted without writing anything.
281    pub fn preview_delete(
282        &self,
283        scope: &DeleteScope,
284        archived_sessions: &[String],
285    ) -> Result<DeleteReport> {
286        check_scope(scope)?;
287        let conn = self.reader();
288        let plan = plan(&conn, scope, archived_sessions)?;
289        let mut report = counted(&conn, &plan)?;
290        report.counts.markers = (plan.markers.len() - plan.repoint.len()) as u64;
291        Ok(report)
292    }
293
294    pub fn conversation_deleted(&self, id: Uuid) -> Result<bool> {
295        conversation_tombstoned(&self.reader(), id)
296    }
297
298    /// Whether the Claude Code session a transcript at `path` belongs to was
299    /// deleted, in which case nothing of it may be archived or ingested.
300    pub fn session_deleted(&self, path: &str) -> Result<bool> {
301        let Some(session) = session_of(path) else {
302            return Ok(false);
303        };
304        self.conversation_deleted(Conversation::claude_code_id(&session))
305    }
306
307    /// Retires or rewrites every cold segment holding records no row points
308    /// at any more, then deletes the files left without a manifest, so a
309    /// deleted cold row's text leaves the disk now rather than at the next
310    /// cold pass. A store without a cold tier has nothing to purge.
311    pub fn purge_cold_segments(&self) -> Result<ColdPurge> {
312        let Some(tier) = self.cold_tier() else {
313            return Ok(ColdPurge::default());
314        };
315        let _pass = tier.pass_guard();
316        let (retired, rewritten) = self.retire_dead_records(tier)?;
317        let (removed_files, _) = self.sweep_cold_files(tier)?;
318        Ok(ColdPurge {
319            retired,
320            rewritten,
321            removed_files,
322        })
323    }
324}
325
326fn matching(conn: &Connection, sql: &str, value: &str) -> Result<Vec<(String, Matched)>> {
327    let mut stmt = conn.prepare(sql)?;
328    let rows = stmt.query_map(params![value], |r| {
329        Ok((
330            r.get::<_, String>(0)?,
331            Matched {
332                source: r.get(1)?,
333                source_id: r.get(2)?,
334                conversation: r.get(3)?,
335            },
336        ))
337    })?;
338    Ok(rows.collect::<rusqlite::Result<_>>()?)
339}
340
341fn check_scope(scope: &DeleteScope) -> Result<()> {
342    match scope {
343        DeleteScope::Session(key) if key.trim().is_empty() => {
344            bail!("a session delete needs a session id")
345        }
346        DeleteScope::Project(path) if path.trim().is_empty() => {
347            bail!("a project delete needs a project")
348        }
349        _ => Ok(()),
350    }
351}
352
353/// The Claude Code session a row came from: transcript raws and the
354/// distillates filed under a transcript by its path, agent notes by their
355/// bare session id. Other sources (memory files, documents) have file
356/// stems too, but no session.
357fn session_source(source: &str, source_id: &str) -> Option<String> {
358    let transcript =
359        matches!(source, "claude-code" | "consolidation") && source_id.ends_with(".jsonl");
360    if transcript || source == AGENT_NOTE_SOURCE {
361        session_of(source_id)
362    } else {
363        None
364    }
365}
366
367/// Whether `memory` belongs to a deleted conversation or session, so no
368/// local or applied write may store it again. Checked inside the writing
369/// transaction, which a concurrent delete commits before or after whole.
370pub(super) fn in_deleted_session(conn: &Connection, memory: &Memory) -> Result<bool> {
371    if let Some(c) = memory.conversation_id
372        && conversation_tombstoned(conn, c)?
373    {
374        return Ok(true);
375    }
376    match session_source(&memory.source, &memory.source_id) {
377        Some(session) => conversation_tombstoned(conn, Conversation::claude_code_id(&session)),
378        None => Ok(false),
379    }
380}
381
382/// Asks the daemon to compact its indexes and purge cold segments, as the
383/// deleting daemon did, once a peer's delete has applied here.
384pub(super) fn request_purge(tx: &Transaction<'_>) -> Result<()> {
385    tx.execute(
386        "INSERT INTO index_meta (key, value) VALUES (?1, '1') \
387         ON CONFLICT(key) DO UPDATE SET value = CAST(value AS INTEGER) + 1",
388        params![PURGE_REQUESTED],
389    )?;
390    Ok(())
391}
392
393fn ingested_session_keys(conn: &Connection) -> Result<Vec<String>> {
394    let mut stmt =
395        conn.prepare("SELECT DISTINCT session_key FROM ingest_log WHERE session_key IS NOT NULL")?;
396    let keys = stmt
397        .query_map([], |r| r.get(0))?
398        .collect::<rusqlite::Result<_>>()?;
399    Ok(keys)
400}
401
402/// A session's rows by the session their source names, or by the
403/// conversation its main transcript was ingested under; neither needs a
404/// project path, which older rows may lack.
405fn session_rows(conn: &Connection, key: &str) -> Result<Vec<(String, Matched)>> {
406    let mut rows = matching(
407        conn,
408        &format!("{MEMORY_COLUMNS} WHERE instr(source_id, ?1) > 0"),
409        key,
410    )?;
411    rows.retain(|(_, m)| session_source(&m.source, &m.source_id).as_deref() == Some(key));
412    rows.extend(matching(
413        conn,
414        &format!("{MEMORY_COLUMNS} WHERE conversation_id = ?1"),
415        &Conversation::claude_code_id(key).to_string(),
416    )?);
417    Ok(rows)
418}
419
420/// The named memory and every earlier version it superseded: deleting a
421/// fact leaves none of its paraphrases to come back.
422fn versions_of(conn: &Connection, id: Uuid) -> Result<BTreeMap<String, Matched>> {
423    let mut found = BTreeMap::new();
424    let mut frontier = vec![id.to_string()];
425    let mut older = conn.prepare("SELECT id FROM memories WHERE superseded_by = ?1")?;
426    while let Some(id) = frontier.pop() {
427        if found.contains_key(&id) {
428            continue;
429        }
430        let Some((_, m)) = matching(conn, &format!("{MEMORY_COLUMNS} WHERE id = ?1"), &id)?
431            .into_iter()
432            .next()
433        else {
434            continue;
435        };
436        for prior in older.query_map(params![id], |r| r.get::<_, String>(0))? {
437            frontier.push(prior?);
438        }
439        found.insert(id, m);
440    }
441    Ok(found)
442}
443
444fn plan(conn: &Connection, scope: &DeleteScope, archived_sessions: &[String]) -> Result<Plan> {
445    let matched: BTreeMap<String, Matched> = match scope {
446        DeleteScope::Memory(id) => versions_of(conn, *id)?,
447        DeleteScope::Session(key) => session_rows(conn, key)?.into_iter().collect(),
448        DeleteScope::Project(path) => {
449            let mut rows = matching(
450                conn,
451                &format!("{MEMORY_COLUMNS} WHERE project_path = ?1"),
452                path,
453            )?;
454            for session in archived_sessions {
455                rows.extend(session_rows(conn, session)?);
456            }
457            rows.into_iter().collect()
458        }
459    };
460    let sessions = scope_sessions(scope, &matched, archived_sessions);
461    let mut conversations = BTreeSet::new();
462    match scope {
463        DeleteScope::Memory(_) => {}
464        DeleteScope::Session(_) => conversations = scope_conversations(&matched, &sessions),
465        DeleteScope::Project(path) => {
466            conversations = scope_conversations(&matched, &sessions);
467            let mut stmt = conn.prepare("SELECT id FROM conversations WHERE project_path = ?1")?;
468            for id in stmt.query_map(params![path], |r| r.get::<_, String>(0))? {
469                if let Ok(id) = Uuid::parse_str(&id?) {
470                    conversations.insert(id);
471                }
472            }
473        }
474    }
475    plan_of(conn, matched, conversations, sessions)
476}
477
478fn scope_conversations(
479    matched: &BTreeMap<String, Matched>,
480    sessions: &BTreeSet<String>,
481) -> BTreeSet<Uuid> {
482    let mut conversations: BTreeSet<Uuid> = matched
483        .values()
484        .filter_map(|m| m.conversation.as_deref())
485        .filter_map(|c| Uuid::parse_str(c).ok())
486        .collect();
487    conversations.extend(sessions.iter().map(|s| Conversation::claude_code_id(s)));
488    conversations
489}
490
491fn plan_of(
492    conn: &Connection,
493    matched: BTreeMap<String, Matched>,
494    conversations: BTreeSet<Uuid>,
495    sessions: BTreeSet<String>,
496) -> Result<Plan> {
497    let mut distilled_into = BTreeSet::new();
498    let mut markers = BTreeSet::new();
499    {
500        let mut by_raw =
501            conn.prepare("SELECT distilled_id FROM distillation_log WHERE memory_id = ?1")?;
502        let mut onto =
503            conn.prepare("SELECT memory_id FROM distillation_log WHERE distilled_id = ?1")?;
504        for id in matched.keys() {
505            for distilled in by_raw.query_map(params![id], |r| r.get::<_, Option<String>>(0))? {
506                if let Some(distilled) = distilled? {
507                    distilled_into.insert(distilled);
508                }
509                markers.insert(id.clone());
510            }
511        }
512        for id in matched.keys().chain(distilled_into.iter()) {
513            for raw in onto.query_map(params![id], |r| r.get::<_, String>(0))? {
514                markers.insert(raw?);
515            }
516        }
517    }
518
519    let mut targets = Vec::new();
520    let mut exists = conn.prepare("SELECT EXISTS (SELECT 1 FROM memories WHERE id = ?1)")?;
521    let mut is_distillate =
522        conn.prepare("SELECT EXISTS (SELECT 1 FROM distillation_log WHERE distilled_id = ?1)")?;
523    let extra = distilled_into
524        .iter()
525        .filter(|d| !matched.contains_key(d.as_str()));
526    for id in matched.keys().chain(extra) {
527        if !exists.query_row(params![id], |r| r.get::<_, bool>(0))? {
528            continue;
529        }
530        targets.push(Target {
531            id: id.clone(),
532            distillate: is_distillate.query_row(params![id], |r| r.get::<_, bool>(0))?,
533        });
534    }
535
536    let doomed: BTreeSet<String> = targets.iter().map(|t| t.id.clone()).collect();
537    let revived = restore_plan(conn, &doomed, &markers)?;
538    for id in revived.derived {
539        targets.push(Target {
540            id: id.to_string(),
541            distillate: true,
542        });
543    }
544    let (restored, repoint) = (revived.restored, revived.repoint);
545
546    let mut pending = BTreeSet::new();
547    for id in conversations {
548        let held: bool = conn.query_row(
549            "SELECT EXISTS (SELECT 1 FROM conversations WHERE id = ?1)",
550            params![id.to_string()],
551            |r| r.get(0),
552        )?;
553        if held || !conversation_tombstoned(conn, id)? {
554            pending.insert(id);
555        }
556    }
557    Ok(Plan {
558        targets,
559        markers,
560        repoint,
561        restored,
562        conversations: pending,
563        sessions,
564    })
565}
566
567struct Revived {
568    restored: Vec<Uuid>,
569    derived: Vec<Uuid>,
570    repoint: BTreeMap<String, Uuid>,
571}
572
573#[derive(Clone)]
574struct Superseded {
575    id: String,
576    source: String,
577    source_id: String,
578}
579
580/// The memories each removed row superseded, and below them the ones those
581/// superseded, as merges of merges leave them.
582struct SupersedeTree<'c> {
583    older: rusqlite::Statement<'c>,
584    doomed: &'c BTreeSet<String>,
585    fed_sessions: &'c BTreeSet<String>,
586    tainted: BTreeMap<String, bool>,
587}
588
589impl<'c> SupersedeTree<'c> {
590    fn new(
591        conn: &'c Connection,
592        doomed: &'c BTreeSet<String>,
593        fed_sessions: &'c BTreeSet<String>,
594    ) -> Result<Self> {
595        Ok(Self {
596            older: conn
597                .prepare("SELECT id, source, source_id FROM memories WHERE superseded_by = ?1")?,
598            doomed,
599            fed_sessions,
600            tainted: BTreeMap::new(),
601        })
602    }
603
604    fn children(&mut self, id: &str) -> Result<Vec<Superseded>> {
605        let rows = self.older.query_map(params![id], |r| {
606            Ok(Superseded {
607                id: r.get(0)?,
608                source: r.get(1)?,
609                source_id: r.get(2)?,
610            })
611        })?;
612        Ok(rows.collect::<rusqlite::Result<_>>()?)
613    }
614
615    /// A distillate summarising a removed row: one of a session whose raw
616    /// fed a removed distillate, or a merge of anything removed or tainted.
617    fn tainted(&mut self, node: &Superseded) -> Result<bool> {
618        if let Some(known) = self.tainted.get(&node.id) {
619            return Ok(*known);
620        }
621        self.tainted.insert(node.id.clone(), false);
622        let tainted = if node.source != "consolidation" {
623            false
624        } else if session_source(&node.source, &node.source_id)
625            .is_some_and(|s| self.fed_sessions.contains(&s))
626        {
627            true
628        } else {
629            let mut any = false;
630            for child in self.children(&node.id)? {
631                if self.doomed.contains(&child.id) || self.tainted(&child)? {
632                    any = true;
633                    break;
634                }
635            }
636            any
637        };
638        self.tainted.insert(node.id.clone(), tainted);
639        Ok(tainted)
640    }
641}
642
643/// A near-duplicate merge or a correction supersedes memories of other
644/// sessions; removing the superseding row must make them live again, or
645/// recall loses them for good. A superseded member of the session of a
646/// removed raw that fed the removed distillate summarised that raw, so it
647/// goes too. A raw outside the scope whose marker named a removed
648/// distillate moves to the restored member of its own session.
649fn restore_plan(
650    conn: &Connection,
651    doomed: &BTreeSet<String>,
652    markers: &BTreeSet<String>,
653) -> Result<Revived> {
654    let mut marker = conn.prepare(
655        "SELECT d.distilled_id, m.source, m.source_id FROM distillation_log d \
656         JOIN memories m ON m.id = d.memory_id WHERE d.memory_id = ?1",
657    )?;
658    let mut marker_of = |raw: &str| -> Result<Option<(Option<String>, String, String)>> {
659        Ok(marker
660            .query_row(params![raw], |r| {
661                Ok((
662                    r.get::<_, Option<String>>(0)?,
663                    r.get::<_, String>(1)?,
664                    r.get::<_, String>(2)?,
665                ))
666            })
667            .optional()?)
668    };
669    let mut fed_sessions = BTreeSet::new();
670    for id in doomed {
671        if let Some((Some(distilled), source, source_id)) = marker_of(id)?
672            && distilled != *id
673            && doomed.contains(&distilled)
674            && let Some(session) = session_source(&source, &source_id)
675        {
676            fed_sessions.insert(session);
677        }
678    }
679    let mut tree = SupersedeTree::new(conn, doomed, &fed_sessions)?;
680    let mut restored: BTreeMap<Uuid, Option<String>> = BTreeMap::new();
681    let mut derived = Vec::new();
682    let mut frontier: Vec<String> = doomed.iter().cloned().collect();
683    let mut visited = BTreeSet::new();
684    while let Some(parent) = frontier.pop() {
685        for child in tree.children(&parent)? {
686            if doomed.contains(&child.id) || !visited.insert(child.id.clone()) {
687                continue;
688            }
689            let Ok(id) = Uuid::parse_str(&child.id) else {
690                continue;
691            };
692            if tree.tainted(&child)? {
693                derived.push(id);
694                frontier.push(child.id);
695            } else {
696                restored.insert(id, session_source(&child.source, &child.source_id));
697            }
698        }
699    }
700    let mut repoint = BTreeMap::new();
701    if !restored.is_empty() {
702        for raw in markers {
703            if doomed.contains(raw) {
704                continue;
705            }
706            let Some((distilled, source, source_id)) = marker_of(raw)? else {
707                continue;
708            };
709            if !distilled.is_some_and(|d| doomed.contains(&d)) {
710                continue;
711            }
712            let Some(session) = session_source(&source, &source_id) else {
713                continue;
714            };
715            if let Some((member, _)) = restored
716                .iter()
717                .find(|(_, s)| s.as_deref() == Some(session.as_str()))
718            {
719                repoint.insert(raw.clone(), *member);
720            }
721        }
722    }
723    derived.sort();
724    derived.dedup();
725    Ok(Revived {
726        restored: restored.into_keys().collect(),
727        derived,
728        repoint,
729    })
730}
731
732/// A session counts as deleted once anything of it matched: a stored row
733/// or a transcript the caller found in the archive.
734fn scope_sessions(
735    scope: &DeleteScope,
736    matched: &BTreeMap<String, Matched>,
737    archived_sessions: &[String],
738) -> BTreeSet<String> {
739    match scope {
740        DeleteScope::Memory(_) => BTreeSet::new(),
741        DeleteScope::Session(key) => {
742            if matched.is_empty() && archived_sessions.is_empty() {
743                BTreeSet::new()
744            } else {
745                BTreeSet::from([key.clone()])
746            }
747        }
748        DeleteScope::Project(_) => {
749            let mut sessions: BTreeSet<String> = archived_sessions.iter().cloned().collect();
750            for m in matched.values() {
751                if let Some(session) = session_source(&m.source, &m.source_id) {
752                    sessions.insert(session);
753                }
754            }
755            sessions
756        }
757    }
758}
759
760fn conversation_tombstoned(conn: &Connection, id: Uuid) -> Result<bool> {
761    Ok(conn.query_row(
762        "SELECT EXISTS (SELECT 1 FROM type_tombstones \
763         WHERE node_type = 'conversation' AND node_id = ?1)",
764        params![id.to_string()],
765        |r| r.get(0),
766    )?)
767}
768
769fn count(conn: &Connection, sql: &str, id: &str) -> Result<u64> {
770    Ok(conn.query_row(sql, params![id], |r| r.get::<_, i64>(0))? as u64)
771}
772
773fn counted(conn: &Connection, plan: &Plan) -> Result<DeleteReport> {
774    let mut counts = DeleteCounts::default();
775    let mut memory_ids = Vec::new();
776    let mut relations = BTreeSet::new();
777    let mut related = conn.prepare(
778        "SELECT from_id || ':' || to_id || ':' || relation_type FROM relations \
779         WHERE from_id = ?1 OR to_id = ?1",
780    )?;
781    for target in &plan.targets {
782        let id = target.id.as_str();
783        if target.distillate {
784            counts.distillates += 1;
785        } else {
786            counts.memories += 1;
787        }
788        counts.chunks += count(
789            conn,
790            "SELECT COUNT(*) FROM memory_chunks WHERE memory_id = ?1",
791            id,
792        )?;
793        counts.vector_keys += count(
794            conn,
795            "SELECT COUNT(*) FROM vec_keys WHERE memory_id = ?1",
796            id,
797        )?;
798        counts.chunk_vector_keys += count(
799            conn,
800            "SELECT COUNT(*) FROM chunk_vec_keys WHERE memory_id = ?1",
801            id,
802        )?;
803        counts.bm25 += count(
804            conn,
805            "SELECT COUNT(*) FROM memories WHERE id = ?1 AND tier = 'hot' AND superseded_by IS NULL",
806            id,
807        )?;
808        counts.cold_records += count(
809            conn,
810            "SELECT COUNT(*) FROM memories WHERE id = ?1 AND archived_segment IS NOT NULL",
811            id,
812        )?;
813        for key in related.query_map(params![id], |r| r.get::<_, String>(0))? {
814            relations.insert(key?);
815        }
816        if let Ok(uuid) = Uuid::parse_str(id) {
817            memory_ids.push(uuid);
818        }
819    }
820    for id in &plan.conversations {
821        counts.conversations += count(
822            conn,
823            "SELECT COUNT(*) FROM conversations WHERE id = ?1",
824            &id.to_string(),
825        )?;
826    }
827    counts.relations = relations.len() as u64;
828    counts.restored = plan.restored.len() as u64;
829    Ok(DeleteReport {
830        counts,
831        memory_ids,
832        sessions: plan.sessions.iter().cloned().collect(),
833        conversations: plan.conversations.iter().copied().collect(),
834        delete_id: None,
835        seq: None,
836    })
837}
838
839/// Drops what a removed memory leaves outside its row: its chunks, its
840/// relations, and its earlier create and update log records with their
841/// relations', which carry its content. Those records go as repair's log
842/// GC reclaims them, keys kept in `sync_dedup`, so a relay of one is still
843/// skipped and a puller that never read them gets only the delete. Runs in
844/// the transaction that logs the memory's delete; returns the records
845/// reclaimed.
846pub(super) fn erase_memory_traces(tx: &Transaction<'_>, memory_id: &str) -> Result<u64> {
847    tx.execute(
848        "DELETE FROM memory_chunks WHERE memory_id = ?1",
849        params![memory_id],
850    )?;
851    let relation_keys: Vec<String> = {
852        let mut stmt = tx.prepare(
853            "SELECT from_id || ':' || to_id || ':' || relation_type FROM relations \
854             WHERE from_id = ?1 OR to_id = ?1",
855        )?;
856        stmt.query_map(params![memory_id], |r| r.get(0))?
857            .collect::<rusqlite::Result<_>>()?
858    };
859    tx.execute(
860        "DELETE FROM relations WHERE from_id = ?1 OR to_id = ?1",
861        params![memory_id],
862    )?;
863    let mut reclaimed = reclaim_writes(tx, "memory", memory_id)?;
864    settled::drop_failed_of(tx, memory_id)?;
865    for key in &relation_keys {
866        reclaimed += reclaim_writes(tx, "relation", key)?;
867        settled::drop_failed_of(tx, key)?;
868    }
869    Ok(reclaimed)
870}
871
872/// The conversation counterpart of `erase_memory_traces`: its log records
873/// carry its project path, and its distillate state goes with it.
874pub(super) fn erase_conversation_traces(
875    tx: &Transaction<'_>,
876    conversation_id: &str,
877) -> Result<u64> {
878    tx.execute(
879        "DELETE FROM distillates WHERE conversation_id = ?1",
880        params![conversation_id],
881    )?;
882    settled::drop_failed_of(tx, conversation_id)?;
883    reclaim_writes(tx, "conversation", conversation_id)
884}
885
886fn reclaim_writes(tx: &Transaction<'_>, node_type: &str, node_id: &str) -> Result<u64> {
887    let seqs: Vec<i64> = {
888        let mut stmt = tx.prepare(
889            "SELECT local_seq FROM sync_log \
890             WHERE node_type = ?1 AND node_id = ?2 AND op IN ('create', 'update')",
891        )?;
892        stmt.query_map(params![node_type, node_id], |r| r.get(0))?
893            .collect::<rusqlite::Result<_>>()?
894    };
895    if seqs.is_empty() {
896        return Ok(0);
897    }
898    sync_dedup::keep_keys_of(tx, &seqs)?;
899    let mut removed = 0;
900    for seq in &seqs {
901        removed += tx.execute("DELETE FROM sync_log WHERE local_seq = ?1", params![seq])? as u64;
902    }
903    Ok(removed)
904}