second-brain-api 0.13.3

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use chrono::{DateTime, TimeDelta, Utc};
use second_brain_core::sqlite_store::SqliteStore;

use crate::health::DistillMode;

pub const INTERVAL: Duration = Duration::from_secs(600);
const FIRST_PASS_DELAY: Duration = Duration::from_secs(60);

/// How far back LLM distillation, here or on a peer, keeps a daemon without
/// an `[llm]` section out of no-GPU mode.
pub const LLM_RECENT: TimeDelta = TimeDelta::days(7);

/// `NoGpu` only when no LLM endpoint is configured and no LLM distilled a
/// raw within `LLM_RECENT` of `now`, on this machine or a peer.
pub fn distill_mode(
    store: &SqliteStore,
    llm_configured: bool,
    now: DateTime<Utc>,
) -> Result<DistillMode> {
    Ok(
        if llm_configured || store.llm_distilled_since(now - LLM_RECENT)? {
            DistillMode::Llm
        } else {
            DistillMode::NoGpu
        },
    )
}

/// Retires raws an agent note covers, for a daemon with no LLM endpoint to
/// distill them.
pub struct NoteCover {
    pub store: Arc<SqliteStore>,
}

impl NoteCover {
    pub fn spawn(self) {
        let spawned = std::thread::Builder::new()
            .name("note-cover".into())
            .spawn(move || {
                std::thread::sleep(FIRST_PASS_DELAY);
                loop {
                    self.run_once();
                    std::thread::sleep(INTERVAL);
                }
            });
        if let Err(e) = spawned {
            tracing::error!("cannot start the note-cover thread: {e}");
        }
    }

    /// Marks and retires note-covered raws unless recent LLM distillation
    /// shows a GPU is distilling them; `None` when the pass was skipped.
    pub fn run_pass(&self, now: DateTime<Utc>) -> Result<Option<usize>> {
        if distill_mode(&self.store, false, now)? == DistillMode::Llm {
            return Ok(None);
        }
        self.store.retire_note_covered_raws().map(Some)
    }

    fn run_once(&self) {
        match self.run_pass(Utc::now()) {
            Ok(None) => tracing::debug!(
                "note-cover: skipped, an LLM distilled raws within the last {} days",
                LLM_RECENT.num_days()
            ),
            Ok(Some(0)) => {}
            Ok(Some(n)) => tracing::info!("note-cover: retired {n} raws covered by an agent note"),
            Err(e) => tracing::warn!("note-cover: retiring raws covered by an agent note: {e:#}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use second_brain_core::schema::{AGENT_NOTE_SOURCE, Memory, MemoryType, Tier};
    use second_brain_core::store::{Store, StoreAdmin};

    use super::*;

    struct Fixture {
        _dir: tempfile::TempDir,
        store: Arc<SqliteStore>,
    }

    fn fixture() -> Fixture {
        let dir = tempfile::tempdir().unwrap();
        let store =
            SqliteStore::open(&dir.path().join("brain.db"), "machine-a".to_string()).unwrap();
        Fixture {
            _dir: dir,
            store: Arc::new(store),
        }
    }

    fn memory(store: &SqliteStore, source: &str, source_id: &str, hours_ago: i64) -> Memory {
        let mut m = Memory::new(
            format!("a memory from {source_id}"),
            MemoryType::Episodic,
            source.to_string(),
            source_id.to_string(),
        );
        m.created_at = Utc::now() - TimeDelta::hours(hours_ago);
        store.store_memory(&m).unwrap();
        m
    }

    fn covered_raw(store: &SqliteStore) -> Memory {
        let raw = memory(
            store,
            "claude-code",
            "/home/someone/.claude/projects/-work-demo/sess-1.jsonl",
            3,
        );
        memory(store, AGENT_NOTE_SOURCE, "sess-1", 2);
        raw
    }

    fn llm_distilled(store: &SqliteStore) {
        let raw = memory(
            store,
            "claude-code",
            "/home/someone/.claude/projects/-work-demo/sess-2.jsonl",
            5,
        );
        let summary = memory(store, "consolidation", "batch-1", 1);
        store
            .mark_consolidated(raw.id, summary.id, "some-model")
            .unwrap();
    }

    fn tier(store: &SqliteStore, raw: &Memory) -> Tier {
        store.get_memory(raw.id).unwrap().unwrap().tier
    }

    #[test]
    fn a_pass_retires_note_covered_raws_when_no_llm_distilled_lately() {
        let f = fixture();
        let raw = covered_raw(&f.store);
        let cover = NoteCover {
            store: f.store.clone(),
        };
        assert_eq!(cover.run_pass(Utc::now()).unwrap(), Some(1));
        assert_eq!(tier(&f.store, &raw), Tier::Archived);
    }

    #[test]
    fn recent_llm_distillation_skips_the_pass_and_leaves_raws_hot() {
        let f = fixture();
        llm_distilled(&f.store);
        let raw = covered_raw(&f.store);
        let cover = NoteCover {
            store: f.store.clone(),
        };
        assert_eq!(cover.run_pass(Utc::now()).unwrap(), None);
        assert_eq!(tier(&f.store, &raw), Tier::Hot);

        let later = Utc::now() + LLM_RECENT + TimeDelta::hours(1);
        assert_eq!(cover.run_pass(later).unwrap(), Some(1));
        assert_eq!(tier(&f.store, &raw), Tier::Archived);
    }

    #[test]
    fn the_health_note_names_the_recent_llm_window() {
        let window = format!("in {} days", LLM_RECENT.num_days());
        assert!(crate::health::NO_GPU_NOTE.contains(&window));
    }

    #[test]
    fn the_mode_is_llm_when_configured_or_distilled_lately() {
        let f = fixture();
        let now = Utc::now();
        assert_eq!(distill_mode(&f.store, true, now).unwrap(), DistillMode::Llm);
        assert_eq!(
            distill_mode(&f.store, false, now).unwrap(),
            DistillMode::NoGpu
        );
        llm_distilled(&f.store);
        assert_eq!(
            distill_mode(&f.store, false, now).unwrap(),
            DistillMode::Llm
        );
        let later = now + LLM_RECENT + TimeDelta::hours(1);
        assert_eq!(
            distill_mode(&f.store, false, later).unwrap(),
            DistillMode::NoGpu
        );
    }
}