Skip to main content

second_brain_api/
models.rs

1//! Startup of the search indexes and the embedding and rerank models, and the
2//! timers that unload idle models.
3
4use std::path::Path;
5use std::sync::{Arc, RwLock};
6use std::time::Duration;
7
8use second_brain_core::embedding::{Activations, Embedder};
9use second_brain_core::index::{IndexClaim, Indexes};
10use second_brain_core::model_cache::DEFAULT_RERANK_MODEL;
11use second_brain_core::residency;
12use second_brain_core::retrieval::Reranker;
13use second_brain_core::sealed::{SealError, SealKeys, seal_error};
14use second_brain_core::sqlite_store::SqliteStore;
15
16/// The reader is what `AppState` and every recall call see; the writer is
17/// private to the drain loop. Splitting them means recall never contends
18/// with the drain loop for anything heavier than the brief `reload_view`
19/// call the loop makes on the reader after each successful drain.
20pub struct IndexHandles {
21    pub reader: Arc<RwLock<Indexes>>,
22    pub writer: Indexes,
23    pub claim: IndexClaim,
24}
25
26/// `on_rebuild` hears how many memories a rebuild covers before it starts.
27pub fn open_index_handles(
28    dir: &Path,
29    store: &SqliteStore,
30    keys: Option<&SealKeys>,
31    on_rebuild: impl FnOnce(usize),
32) -> anyhow::Result<IndexHandles> {
33    let claim = IndexClaim::acquire(dir)?;
34    let mut writer = match keys {
35        Some(keys) => open_keyed_or_discard(dir, keys)?,
36        None => Indexes::open(dir)?,
37    };
38    if writer.consistent_with(store)? {
39        tracing::info!("search indexes consistent at {}", dir.display());
40        // `rebuild` persists as its last step, but the "already consistent"
41        // branch never mutates, so a brand-new index (nothing on disk yet,
42        // trivially consistent with an empty store) would otherwise leave
43        // `open_readonly` below with no files to open.
44        writer.persist()?;
45    } else {
46        tracing::info!("rebuilding search indexes at {}", dir.display());
47        match store.indexable_count() {
48            Ok(n) => on_rebuild(n),
49            Err(e) => tracing::warn!("cannot count the memories to rebuild: {e:#}"),
50        }
51        let n = writer.rebuild(store)?;
52        tracing::info!("search indexes rebuilt: {n} memories");
53        residency::release_free_heap();
54    }
55    let reader = match keys {
56        Some(_) => writer.shared_reader()?,
57        None => Indexes::open_readonly(dir)?,
58    };
59    Ok(IndexHandles {
60        reader: Arc::new(RwLock::new(reader)),
61        writer,
62        claim,
63    })
64}
65
66/// Opens sealed indexes, or starts them empty when their files open under
67/// none of `keys` (a store restored without the files' key): they are
68/// rebuilt from the store, which holds every embedding.
69fn open_keyed_or_discard(dir: &Path, keys: &SealKeys) -> anyhow::Result<Indexes> {
70    match Indexes::open_keyed(dir, keys) {
71        Err(e) if seal_error(&e).is_some_and(|cause| *cause != SealError::KeyRequired) => {
72            tracing::warn!(
73                "search indexes at {} do not open under the store's keys, rebuilding them: {e:#}",
74                dir.display()
75            );
76            for name in Indexes::FILE_NAMES {
77                let path = dir.join(name);
78                if path.is_dir() {
79                    std::fs::remove_dir_all(&path)?;
80                } else if path.exists() {
81                    std::fs::remove_file(&path)?;
82                }
83            }
84            Indexes::open_keyed(dir, keys)
85        }
86        other => other,
87    }
88}
89
90/// A rerank pool of 1 (the old default) serializes every concurrent recall
91/// behind one session; `evals/rerank-cost-curve.md` reproduced the result
92/// of that under ordinary eval load: a queued request timed out at 30s.
93/// `Reranker`'s intra-op thread count is already sized per-session as
94/// `cores / pool_size` (see `crates/core/src/retrieval/rerank.rs`), so a
95/// larger pool does not add CPU throughput the machine doesn't have, and
96/// wave 1's measurement found oversubscribed sessions just contend for the
97/// same cores. What a larger pool buys is concurrency headroom: enough
98/// sessions that an ordinary burst of overlapping recalls queues briefly
99/// instead of erroring. Capping at 4 keeps that headroom without loading
100/// more model copies into memory than a handful of concurrent clients would
101/// ever need.
102fn rerank_pool_size() -> usize {
103    match std::env::var("SECOND_BRAIN_RERANK_POOL") {
104        Ok(v) => v
105            .parse::<usize>()
106            .unwrap_or_else(|_| {
107                panic!("invalid SECOND_BRAIN_RERANK_POOL '{v}': expected a positive integer")
108            })
109            .max(1),
110        Err(_) => {
111            let cores = std::thread::available_parallelism()
112                .map(|n| n.get())
113                .unwrap_or(1);
114            cores.clamp(1, 4)
115        }
116    }
117}
118
119pub fn load_embedder() -> Option<Arc<Embedder>> {
120    match Embedder::with_sessions_and_activations(1, Activations::Released) {
121        Ok(e) => {
122            tracing::info!("embedding model loaded");
123            Some(Arc::new(e))
124        }
125        Err(e) => {
126            tracing::warn!("embedding model unavailable: {e}");
127            None
128        }
129    }
130}
131
132pub fn load_reranker(indexes_open: bool) -> Option<Arc<Reranker>> {
133    let rerank_model = std::env::var("SECOND_BRAIN_RERANK_MODEL")
134        .unwrap_or_else(|_| DEFAULT_RERANK_MODEL.to_string());
135    if !indexes_open || rerank_model == "off" {
136        None
137    } else {
138        let pool_size = rerank_pool_size();
139        match Reranker::with_sessions(&rerank_model, pool_size) {
140            Ok(r) => {
141                tracing::info!("rerank model {rerank_model} loaded ({pool_size} sessions)");
142                Some(Arc::new(r))
143            }
144            Err(e) => {
145                tracing::warn!("rerank model {rerank_model} unavailable: {e:#}");
146                None
147            }
148        }
149    }
150}
151
152/// Must run inside the tokio runtime.
153pub fn spawn_idle_unload(embedder: Option<Arc<Embedder>>, reranker: Option<Arc<Reranker>>) {
154    if let Some(emb) = embedder {
155        tokio::spawn(async move {
156            let mut interval = tokio::time::interval(Duration::from_secs(60));
157            loop {
158                interval.tick().await;
159                emb.drop_if_idle(300);
160            }
161        });
162    }
163
164    if let Some(r) = reranker {
165        tokio::spawn(async move {
166            let mut interval = tokio::time::interval(Duration::from_secs(60));
167            loop {
168                interval.tick().await;
169                r.drop_if_idle(300);
170            }
171        });
172    }
173}
174
175#[cfg(test)]
176mod tests {
177    use super::*;
178    use second_brain_core::schema::{Memory, MemoryType};
179    use second_brain_core::store::Store;
180
181    #[test]
182    fn a_held_claim_refuses_a_second_daemon_until_dropped() {
183        let dir = tempfile::tempdir().unwrap();
184        let store = SqliteStore::in_memory("m".to_string()).unwrap();
185        let first = open_index_handles(dir.path(), &store, None, |_| {}).unwrap();
186        let err = open_index_handles(dir.path(), &store, None, |_| {})
187            .err()
188            .unwrap();
189        assert!(
190            format!("{err:#}").contains("held by another daemon"),
191            "{err:#}"
192        );
193        drop(first);
194        open_index_handles(dir.path(), &store, None, |_| {}).unwrap();
195    }
196
197    #[test]
198    fn a_rebuild_is_announced_with_its_size_and_a_consistent_open_is_not() {
199        let dir = tempfile::tempdir().unwrap();
200        let store = SqliteStore::in_memory("m".to_string()).unwrap();
201        for content in ["first note", "second note"] {
202            let m = Memory::new(
203                content.to_string(),
204                MemoryType::Semantic,
205                "test".to_string(),
206                String::new(),
207            );
208            store.store_memory(&m).unwrap();
209        }
210
211        let mut announced = None;
212        drop(open_index_handles(dir.path(), &store, None, |n| announced = Some(n)).unwrap());
213        assert_eq!(announced, Some(2));
214
215        let mut again = None;
216        drop(open_index_handles(dir.path(), &store, None, |n| again = Some(n)).unwrap());
217        assert_eq!(again, None);
218    }
219}