second-brain-core 0.13.4

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

use anyhow::{Context, Result};
use rusqlite::{OptionalExtension, params};
use serde::{Deserialize, Serialize};

use super::SqliteStore;
use crate::keys::IdentityTag;

const COVERED_REVOCATIONS_KEY: &str = "store_key_covered_revocations";
const RETIREMENT_ID_PREFIX: &str = "retired:";
const ENCRYPTED_KEY: &str = "store_encrypted";
const ENCRYPTION_AWAITED_KEY: &str = "encryption_awaited_since";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct EncryptedRecord {
    pub at_micros: i64,
    pub pause_ms: u64,
}

impl SqliteStore {
    pub fn record_encrypted(&self, at_micros: i64, pause: Duration) -> Result<()> {
        let record = EncryptedRecord {
            at_micros,
            pause_ms: u64::try_from(pause.as_millis()).unwrap_or(u64::MAX),
        };
        self.conn().execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![ENCRYPTED_KEY, serde_json::to_string(&record)?],
        )?;
        Ok(())
    }

    /// When something first waited on this store's encryption migration:
    /// `now_micros` on the first call, the stored time on every later one.
    pub fn encryption_awaited_since(&self, now_micros: i64) -> Result<i64> {
        let conn = self.conn();
        conn.execute(
            "INSERT OR IGNORE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![ENCRYPTION_AWAITED_KEY, now_micros.to_string()],
        )?;
        let value: String = conn.query_row(
            "SELECT value FROM index_meta WHERE key = ?1",
            params![ENCRYPTION_AWAITED_KEY],
            |r| r.get(0),
        )?;
        value
            .parse()
            .with_context(|| format!("{ENCRYPTION_AWAITED_KEY} is corrupt"))
    }

    /// When the one-time migration rewrote this store under its key, and
    /// how long writes waited for it. None for a store created encrypted.
    pub fn encrypted_record(&self) -> Result<Option<EncryptedRecord>> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![ENCRYPTED_KEY],
                |r| r.get(0),
            )
            .optional()?;
        value
            .map(|text| serde_json::from_str(&text).context("store_encrypted is corrupt"))
            .transpose()
    }

    /// Revocations of other devices, under this device's identity, that
    /// arrived after the current store key was made. Each one means a
    /// device that left may hold `K_id`, which opens every older
    /// `store.key.wrapped`, so the store key must change.
    pub fn revocations_after_store_key(&self) -> Result<Vec<String>> {
        let covered = self.covered_revocations()?;
        Ok(self
            .revocation_ids()?
            .into_iter()
            .filter(|id| !covered.contains(id))
            .collect())
    }

    /// Fingerprints of recovery phrases retired, in a way that calls for
    /// rotation, after the current store key was made. A retired phrase
    /// yields `K_id`, which opens every older `store.key.wrapped`, whether
    /// or not this device trusted it.
    pub fn retirements_after_store_key(&self) -> Result<Vec<String>> {
        let covered = self.covered_revocations()?;
        Ok(self
            .retirement_ids()?
            .into_iter()
            .filter(|id| !covered.contains(id))
            .filter_map(|id| id.strip_prefix(RETIREMENT_ID_PREFIX).map(str::to_string))
            .collect())
    }

    /// What the current store key must follow: `revocation_ids` and
    /// `retirement_ids` together.
    pub(crate) fn rotation_causes(&self) -> Result<BTreeSet<String>> {
        let mut causes = self.revocation_ids()?;
        causes.extend(self.retirement_ids()?);
        Ok(causes)
    }

    fn retirement_ids(&self) -> Result<BTreeSet<String>> {
        let own = self.machine_id().to_string();
        let Some(row) = self.device_record(&own)? else {
            return Ok(BTreeSet::new());
        };
        let tag = IdentityTag::from(row.current.record.identity_tag);
        Ok(self
            .rotating_retirements(&tag)?
            .into_iter()
            .map(|fingerprint| format!("{RETIREMENT_ID_PREFIX}{fingerprint}"))
            .collect())
    }

    pub(crate) fn mark_revocations_covered(&self, ids: &BTreeSet<String>) -> Result<()> {
        self.conn().execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![COVERED_REVOCATIONS_KEY, serde_json::to_string(ids)?],
        )?;
        Ok(())
    }

    fn covered_revocations(&self) -> Result<BTreeSet<String>> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![COVERED_REVOCATIONS_KEY],
                |r| r.get(0),
            )
            .optional()?;
        match value {
            Some(text) => Ok(serde_json::from_str(&text)
                .with_context(|| format!("{COVERED_REVOCATIONS_KEY} is corrupt"))?),
            None => Ok(BTreeSet::new()),
        }
    }

    fn revocation_ids(&self) -> Result<BTreeSet<String>> {
        let own = self.machine_id().to_string();
        let Some(row) = self.device_record(&own)? else {
            return Ok(BTreeSet::new());
        };
        let tag = IdentityTag::from(row.current.record.identity_tag);
        Ok(self
            .revocations(&tag)?
            .into_iter()
            .filter(|r| r.revocation.target != own)
            .map(|r| r.id())
            .collect())
    }
}