1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
pub mod alert;
pub mod associate;
pub mod auth;
pub mod eval;
pub mod file_modes;
pub mod health;
pub mod heap;
pub mod ingest;
pub mod logging;
pub mod maintenance;
pub mod mentions;
pub mod near_duplicates;
pub mod progress;
pub mod repairs;
pub mod routes;
pub mod store_compaction;
pub mod stored_redaction;
pub mod watcher;
pub mod write_gate;
pub mod write_queue;
use std::sync::{Arc, RwLock};
use axum::extract::DefaultBodyLimit;
use axum::{Router, middleware};
use tower_http::trace::TraceLayer;
use second_brain_core::embedding::Embedder;
use second_brain_core::index::Indexes;
use second_brain_core::machine::MachineIdentity;
use second_brain_core::maintenance::MaintenanceSummary;
use second_brain_core::retrieval::{Reranker, RetrievalTunables};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::StoreAdmin;
pub struct AppState {
pub store: Arc<dyn StoreAdmin>,
/// Same store as `store`: Retriever::candidates and the outbox drain need
/// the concrete SqliteStore, which the trait object cannot provide.
pub sqlite: Arc<SqliteStore>,
/// A read-only `Indexes` handle shared by every recall call; searches take
/// the `RwLock` read side, which does not block other readers. The drain
/// loop owns a separate read-write `Indexes` and only reaches into this
/// lock's write side briefly, to call `reload_view` after persisting.
/// None when index open/rebuild failed; recall then uses the legacy engine.
pub indexes: Option<Arc<RwLock<Indexes>>>,
pub reranker: Option<Arc<Reranker>>,
pub tunables: RetrievalTunables,
pub embedder: Option<Arc<Embedder>>,
pub machine: MachineIdentity,
/// The most recent repair, scheduled or requested; `/v1/status` reports
/// it without recomputing anything.
pub maintenance: Arc<RwLock<Option<MaintenanceSummary>>>,
/// Hands a repair request to the drain thread, the one owner of the
/// writable indexes, so an on-demand repair never races the scheduled
/// one. None when the indexes failed to open and no drain thread exists;
/// the repair route then runs against the store alone.
pub repair_tx: Option<std::sync::mpsc::Sender<maintenance::DrainRequest>>,
/// `[distill] near_duplicate_threshold`: the default for the
/// near-duplicates route and the threshold status counts at.
pub near_duplicate_threshold: f32,
/// The last near-duplicate count at `near_duplicate_threshold`, from a
/// listing or a repair. Counting is a grouping pass over every live
/// distillate (seconds on the Pi), so status reports this instead.
pub near_duplicates: Arc<RwLock<Option<near_duplicates::NearDupCount>>>,
pub health: Arc<health::Health>,
pub gate: Arc<write_gate::WriteGate>,
}
pub fn app(state: Arc<AppState>, api_token: String) -> Router {
Router::new()
.merge(routes::router())
.layer(middleware::from_fn_with_state(
state.clone(),
write_gate::hold_writes,
))
.layer(DefaultBodyLimit::max(64 * 1024 * 1024))
.layer(middleware::from_fn_with_state(
Arc::<str>::from(api_token),
auth::require_token,
))
.layer(TraceLayer::new_for_http())
.with_state(state)
}
/// Extract the entity names a query refers to, using the same gazetteer
/// extraction the ingest path applies to transcripts. Matching extractors on
/// both sides is what lets query entities resolve to stored entity ids, so the
/// graph term in recall is conditioned on the query rather than raw
/// connectedness. Shared by the live recall route and the eval harness so both
/// measure the same ranking.
pub fn link_query_entities(text: &str) -> Vec<String> {
second_brain_adapters::claude_code::extract_entities(text)
.into_iter()
.map(|e| e.name)
.collect()
}