Skip to main content

second_brain_api/
note_cover.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use anyhow::Result;
5use chrono::{DateTime, TimeDelta, Utc};
6use second_brain_core::sqlite_store::SqliteStore;
7
8use crate::health::DistillMode;
9
10pub const INTERVAL: Duration = Duration::from_secs(600);
11const FIRST_PASS_DELAY: Duration = Duration::from_secs(60);
12
13/// How far back LLM distillation, here or on a peer, keeps a daemon without
14/// an `[llm]` section out of no-GPU mode.
15pub const LLM_RECENT: TimeDelta = TimeDelta::days(7);
16
17/// `NoGpu` only when no LLM endpoint is configured and no LLM distilled a
18/// raw within `LLM_RECENT` of `now`, on this machine or a peer.
19pub fn distill_mode(
20    store: &SqliteStore,
21    llm_configured: bool,
22    now: DateTime<Utc>,
23) -> Result<DistillMode> {
24    Ok(
25        if llm_configured || store.llm_distilled_since(now - LLM_RECENT)? {
26            DistillMode::Llm
27        } else {
28            DistillMode::NoGpu
29        },
30    )
31}
32
33/// Retires raws an agent note covers, for a daemon with no LLM endpoint to
34/// distill them.
35pub struct NoteCover {
36    pub store: Arc<SqliteStore>,
37}
38
39impl NoteCover {
40    pub fn spawn(self) {
41        let spawned = std::thread::Builder::new()
42            .name("note-cover".into())
43            .spawn(move || {
44                std::thread::sleep(FIRST_PASS_DELAY);
45                loop {
46                    self.run_once();
47                    std::thread::sleep(INTERVAL);
48                }
49            });
50        if let Err(e) = spawned {
51            tracing::error!("cannot start the note-cover thread: {e}");
52        }
53    }
54
55    /// Marks and retires note-covered raws unless recent LLM distillation
56    /// shows a GPU is distilling them; `None` when the pass was skipped.
57    pub fn run_pass(&self, now: DateTime<Utc>) -> Result<Option<usize>> {
58        if distill_mode(&self.store, false, now)? == DistillMode::Llm {
59            return Ok(None);
60        }
61        self.store.retire_note_covered_raws().map(Some)
62    }
63
64    fn run_once(&self) {
65        match self.run_pass(Utc::now()) {
66            Ok(None) => tracing::debug!(
67                "note-cover: skipped, an LLM distilled raws within the last {} days",
68                LLM_RECENT.num_days()
69            ),
70            Ok(Some(0)) => {}
71            Ok(Some(n)) => tracing::info!("note-cover: retired {n} raws covered by an agent note"),
72            Err(e) => tracing::warn!("note-cover: retiring raws covered by an agent note: {e:#}"),
73        }
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use second_brain_core::schema::{AGENT_NOTE_SOURCE, Memory, MemoryType, Tier};
80    use second_brain_core::store::{Store, StoreAdmin};
81
82    use super::*;
83
84    struct Fixture {
85        _dir: tempfile::TempDir,
86        store: Arc<SqliteStore>,
87    }
88
89    fn fixture() -> Fixture {
90        let dir = tempfile::tempdir().unwrap();
91        let store =
92            SqliteStore::open(&dir.path().join("brain.db"), "machine-a".to_string()).unwrap();
93        Fixture {
94            _dir: dir,
95            store: Arc::new(store),
96        }
97    }
98
99    fn memory(store: &SqliteStore, source: &str, source_id: &str, hours_ago: i64) -> Memory {
100        let mut m = Memory::new(
101            format!("a memory from {source_id}"),
102            MemoryType::Episodic,
103            source.to_string(),
104            source_id.to_string(),
105        );
106        m.created_at = Utc::now() - TimeDelta::hours(hours_ago);
107        store.store_memory(&m).unwrap();
108        m
109    }
110
111    fn covered_raw(store: &SqliteStore) -> Memory {
112        let raw = memory(
113            store,
114            "claude-code",
115            "/home/someone/.claude/projects/-work-demo/sess-1.jsonl",
116            3,
117        );
118        memory(store, AGENT_NOTE_SOURCE, "sess-1", 2);
119        raw
120    }
121
122    fn llm_distilled(store: &SqliteStore) {
123        let raw = memory(
124            store,
125            "claude-code",
126            "/home/someone/.claude/projects/-work-demo/sess-2.jsonl",
127            5,
128        );
129        let summary = memory(store, "consolidation", "batch-1", 1);
130        store
131            .mark_consolidated(raw.id, summary.id, "some-model")
132            .unwrap();
133    }
134
135    fn tier(store: &SqliteStore, raw: &Memory) -> Tier {
136        store.get_memory(raw.id).unwrap().unwrap().tier
137    }
138
139    #[test]
140    fn a_pass_retires_note_covered_raws_when_no_llm_distilled_lately() {
141        let f = fixture();
142        let raw = covered_raw(&f.store);
143        let cover = NoteCover {
144            store: f.store.clone(),
145        };
146        assert_eq!(cover.run_pass(Utc::now()).unwrap(), Some(1));
147        assert_eq!(tier(&f.store, &raw), Tier::Archived);
148    }
149
150    #[test]
151    fn recent_llm_distillation_skips_the_pass_and_leaves_raws_hot() {
152        let f = fixture();
153        llm_distilled(&f.store);
154        let raw = covered_raw(&f.store);
155        let cover = NoteCover {
156            store: f.store.clone(),
157        };
158        assert_eq!(cover.run_pass(Utc::now()).unwrap(), None);
159        assert_eq!(tier(&f.store, &raw), Tier::Hot);
160
161        let later = Utc::now() + LLM_RECENT + TimeDelta::hours(1);
162        assert_eq!(cover.run_pass(later).unwrap(), Some(1));
163        assert_eq!(tier(&f.store, &raw), Tier::Archived);
164    }
165
166    #[test]
167    fn the_health_note_names_the_recent_llm_window() {
168        let window = format!("in {} days", LLM_RECENT.num_days());
169        assert!(crate::health::NO_GPU_NOTE.contains(&window));
170    }
171
172    #[test]
173    fn the_mode_is_llm_when_configured_or_distilled_lately() {
174        let f = fixture();
175        let now = Utc::now();
176        assert_eq!(distill_mode(&f.store, true, now).unwrap(), DistillMode::Llm);
177        assert_eq!(
178            distill_mode(&f.store, false, now).unwrap(),
179            DistillMode::NoGpu
180        );
181        llm_distilled(&f.store);
182        assert_eq!(
183            distill_mode(&f.store, false, now).unwrap(),
184            DistillMode::Llm
185        );
186        let later = now + LLM_RECENT + TimeDelta::hours(1);
187        assert_eq!(
188            distill_mode(&f.store, false, later).unwrap(),
189            DistillMode::NoGpu
190        );
191    }
192}