Skip to main content

second_brain_core/
config.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::{Context, Result, bail};
4use serde::Deserialize;
5
6use crate::retrieval::{RetrievalTunables, blend};
7
8pub const CONFIG_FILE: &str = "config.toml";
9
10#[derive(Debug, Clone, Default, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct Config {
13    pub llm: Option<LlmConfig>,
14    #[serde(default)]
15    pub cloud: CloudConfig,
16    #[serde(default)]
17    pub distill: DistillConfig,
18    #[serde(default)]
19    pub retrieval: RetrievalConfig,
20    #[serde(default)]
21    pub sources: SourcesConfig,
22    #[serde(default)]
23    pub maintenance: MaintenanceConfig,
24}
25
26/// Scheduled store repair (see `maintenance::repair`). The daemon runs it
27/// once at startup and again whenever `interval_secs` have passed since the
28/// last run, from the thread that owns the writable indexes.
29#[derive(Debug, Clone, Deserialize)]
30#[serde(deny_unknown_fields)]
31pub struct MaintenanceConfig {
32    #[serde(default = "default_true")]
33    pub enabled: bool,
34    #[serde(default = "default_maintenance_interval")]
35    pub interval_secs: u64,
36}
37
38#[derive(Debug, Clone, Deserialize)]
39#[serde(deny_unknown_fields)]
40pub struct LlmConfig {
41    // No default endpoint on purpose: the v0.5 hardcoded localhost:8081 default
42    // silently pointed every LLM stage at a dead machine for weeks.
43    pub endpoint: String,
44    pub model: String,
45    pub api_key_env: Option<String>,
46    #[serde(default = "default_context_budget")]
47    pub context_budget_tokens: usize,
48}
49
50#[derive(Debug, Clone, Deserialize)]
51#[serde(deny_unknown_fields)]
52pub struct CloudConfig {
53    pub vault_id: Option<String>,
54    #[serde(default)]
55    pub providers: Vec<ProviderConfig>,
56    #[serde(default = "default_data_shards")]
57    pub data_shards: usize,
58    #[serde(default = "default_total_shards")]
59    pub total_shards: usize,
60}
61
62#[derive(Debug, Clone, Deserialize)]
63#[serde(deny_unknown_fields)]
64pub struct ProviderConfig {
65    pub name: String,
66    pub endpoint: String,
67    pub bucket: String,
68    #[serde(default)]
69    pub region: Option<String>,
70    pub access_key_id: String,
71    pub secret_key_env: String,
72}
73
74#[derive(Debug, Clone, Deserialize)]
75#[serde(deny_unknown_fields)]
76pub struct DistillConfig {
77    #[serde(default = "default_min_idle_minutes")]
78    pub min_idle_minutes: u64,
79    #[serde(default = "default_archive_after_days")]
80    pub archive_after_days: u64,
81}
82
83#[derive(Debug, Clone, Deserialize)]
84#[serde(deny_unknown_fields)]
85pub struct RetrievalConfig {
86    #[serde(default = "default_rerank_gate")]
87    pub rerank_gate: f32,
88    #[serde(default = "default_cosine_gate")]
89    pub cosine_gate: f32,
90    #[serde(default = "default_rerank_model")]
91    pub rerank_model: String,
92    #[serde(default = "default_true")]
93    pub rerank_enabled: bool,
94    /// How many fused candidates reach the reranker. Measured on the
95    /// distilled corpus (evals/fuse-top-distilled.md): 20 beats 10 on
96    /// recall@3 and MRR with the confidence interval clear of zero.
97    #[serde(default = "default_fuse_top")]
98    pub fuse_top: usize,
99    /// Cosine floor for a recall whose scope was scanned whole; see
100    /// `RetrievalTunables::exhausted_cosine_gate`.
101    #[serde(default = "default_exhausted_cosine_gate")]
102    pub exhausted_cosine_gate: f32,
103}
104
105impl RetrievalConfig {
106    /// The gates and fan-out the daemon ranks with. Candidate depths are
107    /// not configurable and keep their built-in values.
108    pub fn tunables(&self) -> RetrievalTunables {
109        RetrievalTunables {
110            fuse_top: self.fuse_top,
111            rerank_gate: self.rerank_gate,
112            cosine_gate: self.cosine_gate,
113            exhausted_cosine_gate: self.exhausted_cosine_gate,
114            ..RetrievalTunables::default()
115        }
116    }
117}
118
119#[derive(Debug, Clone, Default, Deserialize)]
120#[serde(deny_unknown_fields)]
121pub struct SourcesConfig {
122    #[serde(default)]
123    pub obsidian_vaults: Vec<PathBuf>,
124    #[serde(default)]
125    pub codex_sessions: Option<PathBuf>,
126}
127
128fn default_context_budget() -> usize {
129    16000
130}
131fn default_data_shards() -> usize {
132    2
133}
134fn default_total_shards() -> usize {
135    3
136}
137fn default_min_idle_minutes() -> u64 {
138    60
139}
140fn default_archive_after_days() -> u64 {
141    7
142}
143fn default_rerank_gate() -> f32 {
144    0.30
145}
146fn default_cosine_gate() -> f32 {
147    blend::MIN_SIMILARITY
148}
149fn default_exhausted_cosine_gate() -> f32 {
150    blend::EXHAUSTED_MIN_SIMILARITY
151}
152fn default_fuse_top() -> usize {
153    20
154}
155fn default_rerank_model() -> String {
156    "bge-reranker-v2-m3".to_string()
157}
158fn default_true() -> bool {
159    true
160}
161fn default_maintenance_interval() -> u64 {
162    21600
163}
164
165impl Default for CloudConfig {
166    fn default() -> Self {
167        Self {
168            vault_id: None,
169            providers: Vec::new(),
170            data_shards: default_data_shards(),
171            total_shards: default_total_shards(),
172        }
173    }
174}
175
176impl Default for DistillConfig {
177    fn default() -> Self {
178        Self {
179            min_idle_minutes: default_min_idle_minutes(),
180            archive_after_days: default_archive_after_days(),
181        }
182    }
183}
184
185impl Default for MaintenanceConfig {
186    fn default() -> Self {
187        Self {
188            enabled: true,
189            interval_secs: default_maintenance_interval(),
190        }
191    }
192}
193
194impl Default for RetrievalConfig {
195    fn default() -> Self {
196        Self {
197            rerank_gate: default_rerank_gate(),
198            cosine_gate: default_cosine_gate(),
199            rerank_model: default_rerank_model(),
200            rerank_enabled: true,
201            fuse_top: default_fuse_top(),
202            exhausted_cosine_gate: default_exhausted_cosine_gate(),
203        }
204    }
205}
206
207impl Config {
208    pub fn parse(text: &str) -> Result<Self> {
209        let config: Config = toml::from_str(text).context("invalid config.toml")?;
210        config.validate()?;
211        Ok(config)
212    }
213
214    pub fn load(path: &Path) -> Result<Self> {
215        let text = std::fs::read_to_string(path)
216            .with_context(|| format!("cannot read config at {}", path.display()))?;
217        Self::parse(&text)
218    }
219
220    /// Loads ~/.second-brain/config.toml, falling back to all-default config
221    /// (no llm, no cloud) when the file does not exist so read-only commands
222    /// work on an unconfigured machine.
223    pub fn load_default() -> Result<Self> {
224        let path = crate::machine::data_dir()?.join(CONFIG_FILE);
225        if path.exists() {
226            Self::load(&path)
227        } else {
228            Ok(Self::default())
229        }
230    }
231
232    fn validate(&self) -> Result<()> {
233        if let Some(llm) = &self.llm {
234            if llm.endpoint.trim().is_empty() {
235                bail!("llm.endpoint must not be empty");
236            }
237            if llm.model.trim().is_empty() {
238                bail!("llm.model must not be empty");
239            }
240        }
241        if self.retrieval.fuse_top == 0 {
242            bail!("retrieval.fuse_top must be at least 1");
243        }
244        for (name, gate) in [
245            ("cosine_gate", self.retrieval.cosine_gate),
246            (
247                "exhausted_cosine_gate",
248                self.retrieval.exhausted_cosine_gate,
249            ),
250        ] {
251            if !(0.0..=1.0).contains(&gate) {
252                bail!("retrieval.{name} must be between 0 and 1 (got {gate})");
253            }
254        }
255        let c = &self.cloud;
256        if c.data_shards == 0 || c.total_shards <= c.data_shards {
257            bail!(
258                "cloud erasure config requires 0 < data_shards < total_shards (got {}/{})",
259                c.data_shards,
260                c.total_shards
261            );
262        }
263        if !c.providers.is_empty() && c.providers.len() < c.total_shards {
264            bail!(
265                "{} cloud providers configured but total_shards = {}; one shard per provider is required",
266                c.providers.len(),
267                c.total_shards
268            );
269        }
270        Ok(())
271    }
272
273    pub fn llm(&self) -> Result<&LlmConfig> {
274        self.llm.as_ref().context(
275            "no LLM endpoint configured; add an [llm] section with endpoint and model \
276             to ~/.second-brain/config.toml (there is deliberately no default)",
277        )
278    }
279
280    pub fn cloud(&self) -> Result<&CloudConfig> {
281        if self.cloud.providers.is_empty() {
282            bail!(
283                "no cloud providers configured; add [[cloud.providers]] entries \
284                 to ~/.second-brain/config.toml"
285            );
286        }
287        Ok(&self.cloud)
288    }
289}