Skip to main content

mur_common/config/
codebase_reindex.rs

1//! Daemon-side codebase reindex tuning (`codebase_reindex:`).
2//!
3//! A sandboxed agent cannot write `~/.mur/indexes/codebase/`, so its
4//! post-commit hook drops a wake-up into the inbox and the daemon runs the
5//! index worker instead. Every key is optional; an absent block means the
6//! defaults below.
7
8use super::*;
9
10/// Default: the daemon consumes wake-ups and reconciles stale indexes.
11pub const CODEBASE_REINDEX_DEFAULT_ENABLED: bool = true;
12/// Default inbox sweep interval.
13pub const CODEBASE_REINDEX_DEFAULT_POLL_SECS: u64 = 30;
14/// Default interval for a reconcile pass that runs without any wake-up
15/// (missed hooks, daemon was down).
16pub const CODEBASE_REINDEX_DEFAULT_RECONCILE_SECS: u64 = 3600;
17/// Default minimum gap between two attempts for one project.
18pub const CODEBASE_REINDEX_DEFAULT_MIN_INTERVAL_SECS: u64 = 120;
19/// Default ceiling for the failure backoff.
20pub const CODEBASE_REINDEX_DEFAULT_MAX_BACKOFF_SECS: u64 = 3600;
21/// Default number of inbox entries handled per sweep.
22pub const CODEBASE_REINDEX_DEFAULT_MAX_INTAKE: usize = 256;
23/// Default number of index children running at once.
24pub const CODEBASE_REINDEX_DEFAULT_MAX_CONCURRENT: usize = 1;
25/// Default wall-clock limit before a child is killed.
26pub const CODEBASE_REINDEX_DEFAULT_JOB_TIMEOUT_SECS: u64 = 1800;
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29pub struct CodebaseReindexConfig {
30    /// Daemon consumes wake-ups and reconciles. Off = no daemon task at all.
31    #[serde(default = "cr_default_enabled")]
32    pub enabled: bool,
33    /// Inbox sweep interval, seconds.
34    #[serde(default = "cr_default_poll_secs")]
35    pub poll_secs: u64,
36    /// Reconcile without a wake-up every this many seconds.
37    #[serde(default = "cr_default_reconcile_secs")]
38    pub reconcile_secs: u64,
39    /// Minimum gap between attempts for one project, seconds. Also the
40    /// starting value of the failure backoff.
41    #[serde(default = "cr_default_min_interval_secs")]
42    pub min_interval_secs: u64,
43    /// Failure backoff ceiling, seconds.
44    #[serde(default = "cr_default_max_backoff_secs")]
45    pub max_backoff_secs: u64,
46    /// Inbox entries handled per sweep; the rest wait for the next one.
47    #[serde(default = "cr_default_max_intake")]
48    pub max_intake: usize,
49    /// Concurrent index children.
50    #[serde(default = "cr_default_max_concurrent")]
51    pub max_concurrent: usize,
52    /// Kill a child that runs longer than this, seconds.
53    #[serde(default = "cr_default_job_timeout_secs")]
54    pub job_timeout_secs: u64,
55}
56
57fn cr_default_enabled() -> bool {
58    CODEBASE_REINDEX_DEFAULT_ENABLED
59}
60fn cr_default_poll_secs() -> u64 {
61    CODEBASE_REINDEX_DEFAULT_POLL_SECS
62}
63fn cr_default_reconcile_secs() -> u64 {
64    CODEBASE_REINDEX_DEFAULT_RECONCILE_SECS
65}
66fn cr_default_min_interval_secs() -> u64 {
67    CODEBASE_REINDEX_DEFAULT_MIN_INTERVAL_SECS
68}
69fn cr_default_max_backoff_secs() -> u64 {
70    CODEBASE_REINDEX_DEFAULT_MAX_BACKOFF_SECS
71}
72fn cr_default_max_intake() -> usize {
73    CODEBASE_REINDEX_DEFAULT_MAX_INTAKE
74}
75fn cr_default_max_concurrent() -> usize {
76    CODEBASE_REINDEX_DEFAULT_MAX_CONCURRENT
77}
78fn cr_default_job_timeout_secs() -> u64 {
79    CODEBASE_REINDEX_DEFAULT_JOB_TIMEOUT_SECS
80}
81
82impl Default for CodebaseReindexConfig {
83    fn default() -> Self {
84        Self {
85            enabled: CODEBASE_REINDEX_DEFAULT_ENABLED,
86            poll_secs: CODEBASE_REINDEX_DEFAULT_POLL_SECS,
87            reconcile_secs: CODEBASE_REINDEX_DEFAULT_RECONCILE_SECS,
88            min_interval_secs: CODEBASE_REINDEX_DEFAULT_MIN_INTERVAL_SECS,
89            max_backoff_secs: CODEBASE_REINDEX_DEFAULT_MAX_BACKOFF_SECS,
90            max_intake: CODEBASE_REINDEX_DEFAULT_MAX_INTAKE,
91            max_concurrent: CODEBASE_REINDEX_DEFAULT_MAX_CONCURRENT,
92            job_timeout_secs: CODEBASE_REINDEX_DEFAULT_JOB_TIMEOUT_SECS,
93        }
94    }
95}
96
97impl CodebaseReindexConfig {
98    /// Clamp values that are legal YAML but illegal at runtime, called from
99    /// `Config::sanitize` at load. A zero period panics
100    /// `tokio::time::interval`; a zero intake or concurrency leaves the task
101    /// looking enabled while it never works; a zero backoff start never
102    /// grows; a ceiling below the start contradicts itself.
103    pub(crate) fn sanitize(&mut self) {
104        fn nonzero_u64(v: &mut u64, d: u64) {
105            if *v == 0 {
106                *v = d;
107            }
108        }
109        fn nonzero_usize(v: &mut usize, d: usize) {
110            if *v == 0 {
111                *v = d;
112            }
113        }
114        nonzero_u64(&mut self.poll_secs, CODEBASE_REINDEX_DEFAULT_POLL_SECS);
115        nonzero_u64(
116            &mut self.reconcile_secs,
117            CODEBASE_REINDEX_DEFAULT_RECONCILE_SECS,
118        );
119        nonzero_u64(
120            &mut self.min_interval_secs,
121            CODEBASE_REINDEX_DEFAULT_MIN_INTERVAL_SECS,
122        );
123        nonzero_u64(
124            &mut self.max_backoff_secs,
125            CODEBASE_REINDEX_DEFAULT_MAX_BACKOFF_SECS,
126        );
127        nonzero_u64(
128            &mut self.job_timeout_secs,
129            CODEBASE_REINDEX_DEFAULT_JOB_TIMEOUT_SECS,
130        );
131        nonzero_usize(&mut self.max_intake, CODEBASE_REINDEX_DEFAULT_MAX_INTAKE);
132        nonzero_usize(
133            &mut self.max_concurrent,
134            CODEBASE_REINDEX_DEFAULT_MAX_CONCURRENT,
135        );
136        if self.max_backoff_secs < self.min_interval_secs {
137            self.max_backoff_secs = self.min_interval_secs;
138        }
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    #[test]
147    fn config_without_block_uses_spec_defaults() {
148        let c: Config = serde_yaml::from_str("llm: {}\n").unwrap();
149        let r = &c.codebase_reindex;
150        assert!(r.enabled);
151        assert_eq!((r.poll_secs, r.reconcile_secs), (30, 3600));
152        assert_eq!((r.min_interval_secs, r.max_backoff_secs), (120, 3600));
153        assert_eq!((r.max_intake, r.max_concurrent), (256, 1));
154        assert_eq!(r.job_timeout_secs, 1800);
155        assert_eq!(*r, CodebaseReindexConfig::default());
156    }
157
158    #[test]
159    fn partial_block_keeps_other_defaults() {
160        let c: Config =
161            serde_yaml::from_str("codebase_reindex:\n  enabled: false\n  max_concurrent: 2\n")
162                .unwrap();
163        let r = &c.codebase_reindex;
164        assert!(!r.enabled);
165        assert_eq!(r.max_concurrent, 2);
166        assert_eq!(r.poll_secs, CODEBASE_REINDEX_DEFAULT_POLL_SECS);
167        assert_eq!(
168            r.job_timeout_secs,
169            CODEBASE_REINDEX_DEFAULT_JOB_TIMEOUT_SECS
170        );
171    }
172
173    #[test]
174    fn round_trips_through_yaml() {
175        let mut c = Config::default();
176        c.codebase_reindex.max_intake = 9;
177        let back: Config = serde_yaml::from_str(&serde_yaml::to_string(&c).unwrap()).unwrap();
178        assert_eq!(back.codebase_reindex, c.codebase_reindex);
179    }
180
181    fn load(yaml: &str) -> CodebaseReindexConfig {
182        let tmp = tempfile::tempdir().unwrap();
183        let path = tmp.path().join("config.yaml");
184        std::fs::write(&path, yaml).unwrap();
185        Config::load_or_default(&path).codebase_reindex
186    }
187
188    /// Zeroes are legal YAML but illegal at runtime; the loader clamps each
189    /// one to its default.
190    #[test]
191    fn zero_values_clamp_to_defaults_at_load() {
192        let r = load(
193            "codebase_reindex:\n  poll_secs: 0\n  reconcile_secs: 0\n  min_interval_secs: 0\n  max_backoff_secs: 0\n  max_intake: 0\n  max_concurrent: 0\n  job_timeout_secs: 0\n",
194        );
195        assert_eq!(r, CodebaseReindexConfig::default());
196    }
197
198    #[test]
199    fn backoff_ceiling_below_start_is_raised_to_start() {
200        let r = load("codebase_reindex:\n  min_interval_secs: 600\n  max_backoff_secs: 60\n");
201        assert_eq!((r.min_interval_secs, r.max_backoff_secs), (600, 600));
202    }
203
204    /// The clamp must not rewrite legitimate tuning.
205    #[test]
206    fn positive_values_survive_the_load() {
207        let r = load(
208            "codebase_reindex:\n  enabled: false\n  poll_secs: 5\n  min_interval_secs: 10\n  max_backoff_secs: 20\n  max_concurrent: 3\n",
209        );
210        assert!(!r.enabled);
211        assert_eq!((r.poll_secs, r.max_concurrent), (5, 3));
212        assert_eq!((r.min_interval_secs, r.max_backoff_secs), (10, 20));
213    }
214}