Skip to main content

second_brain_api/
lib.rs

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