second-brain-core 0.13.1

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
use anyhow::{Result, bail};
use rusqlite::{OptionalExtension, params};

use super::SqliteStore;

const KEY: &str = "bootstrap";
const PENDING: &str = "pending";
const INGEST_DUE: &str = "ingest";

/// Where a device paired into an identity stands before its first ingest.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BootstrapState {
    /// Waiting for the snapshot and follow-up pull from the pairing device;
    /// nothing is ingested meanwhile.
    Pending,
    /// The bootstrap finished but the one full ingest pass that follows it
    /// has not completed yet.
    IngestDue,
}

impl BootstrapState {
    fn as_meta(self) -> &'static str {
        match self {
            BootstrapState::Pending => PENDING,
            BootstrapState::IngestDue => INGEST_DUE,
        }
    }

    fn from_meta(value: &str) -> Result<Self> {
        match value {
            PENDING => Ok(BootstrapState::Pending),
            INGEST_DUE => Ok(BootstrapState::IngestDue),
            other => bail!("index_meta {KEY} = {other:?}: unknown bootstrap state"),
        }
    }
}

impl SqliteStore {
    pub fn bootstrap_state(&self) -> Result<Option<BootstrapState>> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![KEY],
                |r| r.get(0),
            )
            .optional()?;
        value.as_deref().map(BootstrapState::from_meta).transpose()
    }

    /// `None` clears the state.
    pub fn set_bootstrap_state(&self, state: Option<BootstrapState>) -> Result<()> {
        let conn = self.conn();
        match state {
            Some(state) => conn.execute(
                "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
                params![KEY, state.as_meta()],
            )?,
            None => conn.execute("DELETE FROM index_meta WHERE key = ?1", params![KEY])?,
        };
        Ok(())
    }

    /// True when any memory, entity, conversation or relation is stored.
    pub fn holds_replicated_rows(&self) -> Result<bool> {
        Ok(self.reader().query_row(
            "SELECT EXISTS (SELECT 1 FROM memories) OR EXISTS (SELECT 1 FROM entities) \
             OR EXISTS (SELECT 1 FROM conversations) OR EXISTS (SELECT 1 FROM relations)",
            [],
            |r| r.get(0),
        )?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn store() -> SqliteStore {
        SqliteStore::in_memory("m".to_string()).unwrap()
    }

    #[test]
    fn a_new_store_has_no_bootstrap_state() {
        assert_eq!(store().bootstrap_state().unwrap(), None);
    }

    #[test]
    fn each_bootstrap_state_reads_back_and_none_clears_it() {
        let store = store();
        for state in [BootstrapState::Pending, BootstrapState::IngestDue] {
            store.set_bootstrap_state(Some(state)).unwrap();
            assert_eq!(store.bootstrap_state().unwrap(), Some(state));
        }
        store.set_bootstrap_state(None).unwrap();
        assert_eq!(store.bootstrap_state().unwrap(), None);
    }

    #[test]
    fn a_pending_bootstrap_survives_a_reopen() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("brain.db");
        SqliteStore::open(&path, "m".to_string())
            .unwrap()
            .set_bootstrap_state(Some(BootstrapState::Pending))
            .unwrap();
        let reopened = SqliteStore::open(&path, "m".to_string()).unwrap();
        assert_eq!(
            reopened.bootstrap_state().unwrap(),
            Some(BootstrapState::Pending)
        );
    }

    #[test]
    fn an_unknown_bootstrap_value_is_an_error() {
        let store = store();
        store
            .conn()
            .execute(
                "INSERT INTO index_meta (key, value) VALUES ('bootstrap', 'later')",
                [],
            )
            .unwrap();
        assert!(store.bootstrap_state().is_err());
    }

    #[test]
    fn an_empty_store_holds_no_replicated_rows() {
        assert!(!store().holds_replicated_rows().unwrap());
    }

    #[test]
    fn a_row_in_any_replicated_table_counts() {
        let inserts = [
            "INSERT INTO memories (id, content, memory_type, confidence, created_at, \
             updated_at, last_accessed, source, source_id, machine_id) \
             VALUES ('m1', 'c', 'fact', 1.0, 't', 't', 't', 'remember', 's', 'm')",
            "INSERT INTO entities (id, name, entity_type) VALUES ('e1', 'n', 'tool')",
            "INSERT INTO conversations (id, source, machine_id, started_at) \
             VALUES ('c1', 'claude', 'm', 't')",
            "INSERT INTO relations (from_id, to_id, relation_type) VALUES ('a', 'b', 'uses')",
        ];
        for insert in inserts {
            let store = store();
            store.conn().execute(insert, []).unwrap();
            assert!(store.holds_replicated_rows().unwrap(), "{insert}");
        }
    }

    #[test]
    fn non_replicated_tables_do_not_count() {
        let store = store();
        store
            .set_bootstrap_state(Some(BootstrapState::Pending))
            .unwrap();
        store
            .conn()
            .execute(
                "INSERT INTO ingest_log (file_path, file_hash, ingested_at, memory_count, source) \
                 VALUES ('p', 'h', 't', 0, 'claude')",
                [],
            )
            .unwrap();
        assert!(!store.holds_replicated_rows().unwrap());
    }
}