second-brain-core 0.13.2

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
use std::collections::BTreeSet;

use anyhow::Result;
use rusqlite::params;

use super::identity::{Envelope, Epoch, IdentityTag};
use crate::sqlite_store::SqliteStore;

impl SqliteStore {
    /// Commits the epochs and envelopes with the WAL synced to disk, so the
    /// caller may delete the only other copy of a key once this returns.
    /// Rows already stored are kept as they are.
    pub fn put_keys_durably(
        &self,
        tag: &IdentityTag,
        epochs: &[Epoch],
        envelopes: &[Envelope],
        created_at: i64,
    ) -> Result<()> {
        self.with_durable_txn(|tx| {
            for epoch in epochs {
                tx.execute(
                    "INSERT OR IGNORE INTO key_epochs (identity_tag, epoch, key_id, cause, created_at) \
                     VALUES (?1, ?2, ?3, NULL, ?4)",
                    params![tag.as_str(), epoch.number, epoch.key_id.to_string(), created_at],
                )?;
            }
            for envelope in envelopes {
                tx.execute(
                    "INSERT OR IGNORE INTO key_envelopes \
                     (identity_tag, epoch, key_id, recipient, machine_id, envelope) \
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                    params![
                        envelope.identity_tag.as_str(),
                        envelope.epoch.number,
                        envelope.epoch.key_id.to_string(),
                        envelope.recipient,
                        envelope.machine_id,
                        envelope.ciphertext,
                    ],
                )?;
            }
            Ok(())
        })
    }

    /// This device's identity, named by its own device record, and the
    /// envelopes of that identity wrapped to `recipient`. None until the
    /// store holds the record.
    pub fn own_identity_envelopes(
        &self,
        recipient: &str,
    ) -> Result<Option<(IdentityTag, Vec<Envelope>)>> {
        let own = self.machine_id();
        let Some(row) = self.device_record(own)? else {
            return Ok(None);
        };
        let tag = IdentityTag::from(row.current.record.identity_tag);
        let envelopes = self
            .envelopes_for_machine(own)?
            .into_iter()
            .filter(|e| e.identity_tag == tag && e.recipient == recipient)
            .collect();
        Ok(Some((tag, envelopes)))
    }

    /// Returns false when no envelope was stored for that epoch and
    /// recipient.
    pub fn delete_envelope(
        &self,
        tag: &IdentityTag,
        epoch: &Epoch,
        recipient: &str,
    ) -> Result<bool> {
        let deleted = self.conn().execute(
            "DELETE FROM key_envelopes \
             WHERE identity_tag = ?1 AND epoch = ?2 AND key_id = ?3 AND recipient = ?4",
            params![
                tag.as_str(),
                epoch.number,
                epoch.key_id.to_string(),
                recipient
            ],
        )?;
        Ok(deleted > 0)
    }

    /// Every other machine this store has synced with or holds records
    /// from: sync cursor keys (with any `push:` prefix removed) and the
    /// origins in the sync log.
    pub fn known_peer_ids(&self) -> Result<BTreeSet<String>> {
        let conn = self.reader();
        let mut ids = BTreeSet::new();
        let mut cursors = conn.prepare("SELECT peer_id FROM sync_state")?;
        for peer in cursors.query_map([], |r| r.get::<_, String>(0))? {
            let peer = peer?;
            ids.insert(peer.strip_prefix("push:").unwrap_or(&peer).to_string());
        }
        let mut origins = conn.prepare("SELECT DISTINCT origin_machine_id FROM sync_log")?;
        for origin in origins.query_map([], |r| r.get::<_, String>(0))? {
            ids.insert(origin?);
        }
        ids.remove(self.machine_id());
        Ok(ids)
    }
}

#[cfg(test)]
mod tests {
    use chrono::Utc;

    use crate::keys::{DeviceKeys, IdentityKey, wrap};
    use crate::schema::SyncState;
    use crate::sqlite_store::SqliteStore;
    use crate::store::StoreAdmin;

    use super::*;

    #[test]
    fn durable_keys_are_stored_once_and_the_writer_returns_to_normal_sync() {
        let store = SqliteStore::in_memory("local".to_string()).unwrap();
        let device = DeviceKeys::generate().unwrap();
        let key = IdentityKey::generate().unwrap();
        let tag = IdentityTag::migrated(&key.key_id());
        let envelope = wrap(&key, 0, &tag, &device.age_recipient(), Some("local".into())).unwrap();
        for _ in 0..2 {
            store
                .put_keys_durably(&tag, &[envelope.epoch], std::slice::from_ref(&envelope), 7)
                .unwrap();
        }
        assert_eq!(store.key_epochs(&tag).unwrap().len(), 1);
        assert_eq!(
            store.envelopes_for_machine("local").unwrap(),
            vec![envelope]
        );
        let mode: i64 = store
            .conn()
            .query_row("PRAGMA synchronous", [], |r| r.get(0))
            .unwrap();
        assert_eq!(mode, 1, "synchronous must be back to NORMAL");
    }

    #[test]
    fn an_envelope_is_deleted_for_its_recipient_alone() {
        let store = SqliteStore::in_memory("local".to_string()).unwrap();
        let key = IdentityKey::generate().unwrap();
        let tag = IdentityTag::migrated(&key.key_id());
        let mine = DeviceKeys::generate().unwrap().age_recipient();
        let peer = DeviceKeys::generate().unwrap().age_recipient();
        let own = wrap(&key, 0, &tag, &mine, Some("local".into())).unwrap();
        let theirs = wrap(&key, 0, &tag, &peer, Some("peer".into())).unwrap();
        store.put_envelope(&own).unwrap();
        store.put_envelope(&theirs).unwrap();
        assert!(
            store
                .delete_envelope(&tag, &theirs.epoch, &peer.to_string())
                .unwrap()
        );
        assert!(
            !store
                .delete_envelope(&tag, &theirs.epoch, &peer.to_string())
                .unwrap()
        );
        assert_eq!(store.envelopes_for_machine("local").unwrap(), vec![own]);
        assert!(store.envelopes_for_machine("peer").unwrap().is_empty());
    }

    #[test]
    fn known_peers_come_from_cursors_and_log_origins_but_never_this_machine() {
        let store = SqliteStore::in_memory("local".to_string()).unwrap();
        for peer in ["pi", "push:mac", "local"] {
            store
                .set_sync_state(&SyncState {
                    peer_id: peer.to_string(),
                    last_seq: 1,
                    last_sync_at: Utc::now(),
                })
                .unwrap();
        }
        store
            .conn()
            .execute(
                "INSERT INTO sync_log (id, local_seq, origin_machine_id, origin_seq, op, node_type, \
                 node_id, timestamp) VALUES ('l1', 1, 'laptop', 1, 'create', 'memory', 'n', \
                 '2026-01-01T00:00:00Z')",
                [],
            )
            .unwrap();
        let ids: Vec<String> = store.known_peer_ids().unwrap().into_iter().collect();
        assert_eq!(ids, vec!["laptop", "mac", "pi"]);
    }
}