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 anyhow::{Result, bail};
use rusqlite::{OptionalExtension, params};
use serde::{Deserialize, Serialize};

use crate::sqlite_store::SqliteStore;

const ROUTE_PREFIX: &str = "device_route:";
const LEGACY_PREFIX: &str = "legacy_route:";
const PEERS_MIGRATED: &str = "sync_peers_migrated";
const MAX_ADDRESSES: usize = 8;

/// How to reach a device over ssh. Unsigned: a wrong address only fails
/// the dial, since host keys are pinned per machine id.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceRoute {
    pub machine_id: String,
    #[serde(default)]
    pub addresses: Vec<String>,
    #[serde(default)]
    pub ssh_login: Option<String>,
    #[serde(default)]
    pub ssh_port: Option<u16>,
}

/// A `sync-peers` line whose host answered with this device's id.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LegacyRoute {
    pub host: String,
    pub sb: String,
}

/// Names ssh is given as a host or login can't start with `-`, so none is
/// ever read as an option.
pub fn valid_address(address: &str) -> bool {
    !address.is_empty()
        && address.len() <= 253
        && !address.starts_with('-')
        && address
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | ':' | '-' | '_' | '%'))
}

/// What a machine id must be before it goes into an ssh alias, an
/// authorized_keys command or a thread name: 1 to 128 of A-Z a-z 0-9 . _ -.
pub fn valid_machine_id(id: &str) -> bool {
    (1..=128).contains(&id.len())
        && id
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
}

pub fn valid_login(login: &str) -> bool {
    !login.is_empty()
        && login.len() <= 64
        && !login.starts_with('-')
        && login
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))
}

impl DeviceRoute {
    /// Drops what could not be dialled safely and keeps the first few
    /// addresses, in order, once each.
    pub fn sanitized(mut self) -> Self {
        let mut kept: Vec<String> = Vec::new();
        for address in self.addresses {
            if valid_address(&address) && !kept.contains(&address) {
                kept.push(address);
            }
        }
        kept.truncate(MAX_ADDRESSES);
        self.addresses = kept;
        self.ssh_login = self.ssh_login.filter(|login| valid_login(login));
        self.ssh_port = self.ssh_port.filter(|port| *port != 0);
        self
    }

    /// `other`'s addresses after this route's own, for a route a third
    /// device relayed.
    fn filled_from(mut self, other: DeviceRoute) -> Self {
        self.addresses.extend(other.addresses);
        self.ssh_login = self.ssh_login.or(other.ssh_login);
        self.ssh_port = self.ssh_port.or(other.ssh_port);
        self.sanitized()
    }
}

impl SqliteStore {
    pub fn device_route(&self, machine_id: &str) -> Result<Option<DeviceRoute>> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![format!("{ROUTE_PREFIX}{machine_id}")],
                |r| r.get(0),
            )
            .optional()?;
        Ok(value.and_then(|text| serde_json::from_str(&text).ok()))
    }

    pub fn device_routes(&self) -> Result<Vec<DeviceRoute>> {
        let conn = self.reader();
        let mut stmt =
            conn.prepare("SELECT value FROM index_meta WHERE key >= ?1 AND key < ?2 ORDER BY key")?;
        let rows = stmt.query_map(params![ROUTE_PREFIX, prefix_end(ROUTE_PREFIX)], |r| {
            r.get::<_, String>(0)
        })?;
        let mut routes = Vec::new();
        for row in rows {
            if let Ok(route) = serde_json::from_str::<DeviceRoute>(&row?) {
                routes.push(route);
            }
        }
        Ok(routes)
    }

    /// Replaces what this store knows about reaching `route.machine_id`.
    pub fn set_device_route(&self, route: DeviceRoute) -> Result<()> {
        let route = route.sanitized();
        if route.machine_id.is_empty() {
            bail!("a device route needs a machine id");
        }
        self.conn().execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![
                format!("{ROUTE_PREFIX}{}", route.machine_id),
                serde_json::to_string(&route)?
            ],
        )?;
        Ok(())
    }

    /// Takes the routes a peer sent in its handshake: its own addresses go
    /// first, ahead of those this store already knew for it, and one it
    /// relays for a third device only adds to what this store already knows.
    /// Routes to this device and to machines with no active row here are
    /// ignored. True when anything changed.
    pub fn merge_device_routes(&self, sender: &str, routes: &[DeviceRoute]) -> Result<bool> {
        let own = self.machine_id();
        let mut changed = false;
        for route in routes {
            let id = route.machine_id.as_str();
            if id.is_empty()
                || id == own
                || !self.device_record(id)?.is_some_and(|row| !row.revoked)
            {
                continue;
            }
            let held = self.device_route(id)?;
            let incoming = route.clone().sanitized();
            let merged = match held.clone() {
                Some(held) if id == sender => incoming.filled_from(held),
                None if id == sender => incoming,
                Some(held) => held.filled_from(incoming),
                None => incoming,
            };
            if held.as_ref() != Some(&merged) {
                self.set_device_route(merged)?;
                changed = true;
            }
        }
        Ok(changed)
    }

    pub fn legacy_routes(&self) -> Result<Vec<(String, LegacyRoute)>> {
        let conn = self.reader();
        let mut stmt = conn.prepare(
            "SELECT key, value FROM index_meta WHERE key >= ?1 AND key < ?2 ORDER BY key",
        )?;
        let rows = stmt.query_map(params![LEGACY_PREFIX, prefix_end(LEGACY_PREFIX)], |r| {
            Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
        })?;
        let mut routes = Vec::new();
        for row in rows {
            let (key, value) = row?;
            if let (Some(id), Ok(route)) = (
                key.strip_prefix(LEGACY_PREFIX),
                serde_json::from_str::<LegacyRoute>(&value),
            ) {
                routes.push((id.to_string(), route));
            }
        }
        Ok(routes)
    }

    pub fn set_legacy_route(&self, machine_id: &str, route: &LegacyRoute) -> Result<()> {
        self.conn().execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
            params![
                format!("{LEGACY_PREFIX}{machine_id}"),
                serde_json::to_string(route)?
            ],
        )?;
        Ok(())
    }

    /// Set once every `sync-peers` line has been matched to a device, after
    /// which the file is no longer read.
    pub fn sync_peers_migrated(&self) -> Result<bool> {
        let value: Option<String> = self
            .reader()
            .query_row(
                "SELECT value FROM index_meta WHERE key = ?1",
                params![PEERS_MIGRATED],
                |r| r.get(0),
            )
            .optional()?;
        Ok(value.is_some())
    }

    pub fn mark_sync_peers_migrated(&self) -> Result<()> {
        self.conn().execute(
            "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, 'done')",
            params![PEERS_MIGRATED],
        )?;
        Ok(())
    }
}

fn prefix_end(prefix: &str) -> String {
    format!("{};", prefix.trim_end_matches(':'))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keys::{DeviceRecord, ensure_device_keys, found_identity};

    fn store_with(rows: &[&str]) -> (SqliteStore, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let store = SqliteStore::in_memory("self".to_string()).unwrap();
        ensure_device_keys(&store, dir.path()).unwrap();
        let device = crate::keys::load_device_keys(dir.path()).unwrap().unwrap();
        let founded = found_identity(&store, &device, "self", &[], 1).unwrap();
        for id in rows {
            let other = crate::keys::DeviceKeys::generate().unwrap();
            let record = DeviceRecord {
                machine_id: id.to_string(),
                identity_tag: founded.tag.as_str().to_string(),
                version: 1,
                name: id.to_string(),
                age_recipient: other.age_recipient().to_string(),
                ssh_public_key: other.ssh_public_key(),
                signing_public_key: other.verifying_key().to_bytes(),
                recovery_fingerprints: Vec::new(),
                ssh_login: None,
                ssh_port: None,
                ssh_host_key: None,
            };
            crate::keys::admit_device(&store, &device, record).unwrap();
        }
        (store, dir)
    }

    fn route(id: &str, addresses: &[&str]) -> DeviceRoute {
        DeviceRoute {
            machine_id: id.to_string(),
            addresses: addresses.iter().map(|a| a.to_string()).collect(),
            ssh_login: Some("me".to_string()),
            ssh_port: Some(22),
        }
    }

    #[test]
    fn a_senders_own_addresses_go_first_and_a_relayed_route_only_adds() {
        let (store, _dir) = store_with(&["a", "b"]);
        assert!(
            store
                .merge_device_routes(
                    "a",
                    &[route("a", &["192.0.2.1"]), route("b", &["192.0.2.2"])]
                )
                .unwrap()
        );
        store
            .merge_device_routes(
                "a",
                &[route("a", &["192.0.2.9"]), route("b", &["192.0.2.8"])],
            )
            .unwrap();
        assert_eq!(
            store.device_route("a").unwrap().unwrap().addresses,
            vec!["192.0.2.9", "192.0.2.1"]
        );
        assert_eq!(
            store.device_route("b").unwrap().unwrap().addresses,
            vec!["192.0.2.2", "192.0.2.8"]
        );
        assert!(
            !store
                .merge_device_routes("a", &[route("a", &["192.0.2.9", "192.0.2.1"])])
                .unwrap()
        );
    }

    #[test]
    fn routes_to_this_device_or_to_unknown_machines_are_ignored() {
        let (store, _dir) = store_with(&["a"]);
        store
            .merge_device_routes(
                "a",
                &[route("self", &["192.0.2.1"]), route("x", &["192.0.2.2"])],
            )
            .unwrap();
        assert!(store.device_route("self").unwrap().is_none());
        assert!(store.device_route("x").unwrap().is_none());
    }

    #[test]
    fn option_like_or_malformed_names_are_never_kept() {
        let (store, _dir) = store_with(&["a"]);
        let mut sent = route(
            "a",
            &["-oProxyCommand=touch", "ok.local", "a b", "ok.local"],
        );
        sent.ssh_login = Some("-l".to_string());
        store.merge_device_routes("a", &[sent]).unwrap();
        let held = store.device_route("a").unwrap().unwrap();
        assert_eq!(held.addresses, vec!["ok.local"]);
        assert_eq!(held.ssh_login, None);
    }

    #[test]
    fn legacy_routes_and_the_migration_mark_round_trip() {
        let (store, _dir) = store_with(&["a"]);
        assert!(!store.sync_peers_migrated().unwrap());
        let legacy = LegacyRoute {
            host: "me@pi".to_string(),
            sb: "sb".to_string(),
        };
        store.set_legacy_route("a", &legacy).unwrap();
        store.set_device_route(route("a", &["192.0.2.1"])).unwrap();
        assert_eq!(
            store.legacy_routes().unwrap(),
            vec![("a".to_string(), legacy)]
        );
        assert_eq!(store.device_routes().unwrap().len(), 1);
        store.mark_sync_peers_migrated().unwrap();
        assert!(store.sync_peers_migrated().unwrap());
    }
}