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