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