Skip to main content

second_brain_api/
lib.rs

1pub mod alert;
2pub mod associate;
3pub mod auth;
4pub mod backfill;
5pub mod eval;
6pub mod file_modes;
7pub mod footprint;
8pub mod health;
9pub mod heap;
10pub mod ingest;
11pub mod logging;
12pub mod maintenance;
13pub mod mentions;
14pub mod models;
15pub mod near_duplicates;
16pub mod note_cover;
17pub mod progress;
18pub mod raw_index;
19pub mod repairs;
20pub mod routes;
21pub mod self_update;
22pub mod sources;
23pub mod startup;
24pub mod store_compaction;
25pub mod stored_redaction;
26pub mod sync_loop;
27pub mod value_metrics;
28pub mod vaults;
29pub mod watcher;
30pub mod write_gate;
31pub mod write_queue;
32
33use std::sync::{Arc, RwLock};
34
35use axum::extract::DefaultBodyLimit;
36use axum::{Router, middleware};
37use tower_http::trace::TraceLayer;
38
39use second_brain_core::embedding::Embedder;
40use second_brain_core::index::Indexes;
41use second_brain_core::index::raw::RawIndex;
42use second_brain_core::machine::MachineIdentity;
43use second_brain_core::maintenance::MaintenanceSummary;
44use second_brain_core::retrieval::{Reranker, RetrievalTunables};
45use second_brain_core::sqlite_store::SqliteStore;
46use second_brain_core::store::StoreAdmin;
47
48pub struct AppState {
49    pub store: Arc<dyn StoreAdmin>,
50    /// Same store as `store`: Retriever::candidates and the outbox drain need
51    /// the concrete SqliteStore, which the trait object cannot provide.
52    pub sqlite: Arc<SqliteStore>,
53    /// A read-only `Indexes` handle shared by every recall call; searches take
54    /// the `RwLock` read side, which does not block other readers. The drain
55    /// loop owns a separate read-write `Indexes` and only reaches into this
56    /// lock's write side briefly, to call `reload_view` after persisting.
57    /// None when index open/rebuild failed; recall then uses the legacy engine.
58    pub indexes: Option<Arc<RwLock<Indexes>>>,
59    /// Archived raws and transcript passages, searched only when nothing in
60    /// `indexes` clears the abstention bar. The raw index thread writes it.
61    pub raw_index: Option<Arc<RwLock<RawIndex>>>,
62    /// The raw index's age bound and what it left out, for status.
63    pub raw_coverage: Option<Arc<crate::raw_index::RawCoverage>>,
64    pub reranker: Option<Arc<Reranker>>,
65    pub tunables: RetrievalTunables,
66    pub embedder: Option<Arc<Embedder>>,
67    pub machine: MachineIdentity,
68    /// The most recent repair, scheduled or requested; `/v1/status` reports
69    /// it without recomputing anything.
70    pub maintenance: Arc<RwLock<Option<MaintenanceSummary>>>,
71    /// Hands a repair request to the drain thread, the one owner of the
72    /// writable indexes, so an on-demand repair never races the scheduled
73    /// one. None when the indexes failed to open and no drain thread exists;
74    /// the repair route then runs against the store alone.
75    pub repair_tx: Option<std::sync::mpsc::Sender<maintenance::DrainRequest>>,
76    /// `[distill] near_duplicate_threshold`: the default for the
77    /// near-duplicates route and the threshold status counts at.
78    pub near_duplicate_threshold: f32,
79    /// The last near-duplicate count at `near_duplicate_threshold`, from a
80    /// listing or a repair. Counting is a grouping pass over every live
81    /// distillate (seconds on the Pi), so status reports this instead.
82    pub near_duplicates: Arc<RwLock<Option<near_duplicates::NearDupCount>>>,
83    pub health: Arc<health::Health>,
84    pub gate: Arc<write_gate::WriteGate>,
85}
86
87pub fn app(state: Arc<AppState>, api_token: String) -> Router {
88    Router::new()
89        .merge(routes::router())
90        .layer(middleware::from_fn_with_state(
91            state.clone(),
92            write_gate::hold_writes,
93        ))
94        .layer(DefaultBodyLimit::max(64 * 1024 * 1024))
95        .layer(middleware::from_fn_with_state(
96            Arc::<str>::from(api_token),
97            auth::require_token,
98        ))
99        .layer(TraceLayer::new_for_http())
100        .with_state(state)
101}
102
103/// The retrieval settings the daemon serves recall with: `base` from config,
104/// with abstention and source order on.
105pub fn daemon_tunables(base: RetrievalTunables) -> RetrievalTunables {
106    RetrievalTunables {
107        abstain: true,
108        source_order: true,
109        ..base
110    }
111}
112
113/// Extract the entity names a query refers to, using the same gazetteer
114/// extraction the ingest path applies to transcripts. Matching extractors on
115/// both sides is what lets query entities resolve to stored entity ids, so the
116/// graph term in recall is conditioned on the query rather than raw
117/// connectedness. Shared by the live recall route and the eval harness so both
118/// measure the same ranking.
119pub fn link_query_entities(text: &str) -> Vec<String> {
120    second_brain_adapters::claude_code::extract_entities(text)
121        .into_iter()
122        .map(|e| e.name)
123        .collect()
124}
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129
130    #[test]
131    fn the_daemon_abstains_and_orders_by_source_while_the_core_default_does_neither() {
132        let base = RetrievalTunables {
133            fuse_top: 7,
134            ..RetrievalTunables::default()
135        };
136        assert!(!base.abstain && !base.source_order);
137        let served = daemon_tunables(base);
138        assert!(served.abstain && served.source_order);
139        assert_eq!(served.fuse_top, 7, "config-driven settings pass through");
140    }
141}