Skip to main content

lean_ctx/core/
data_dir.rs

1use std::path::PathBuf;
2
3/// Markers that identify a legacy / pre-split install whose categories must
4/// stay collapsed onto one directory (GH #408).
5///
6/// Deliberately excludes `config.toml`: after the XDG split it legitimately
7/// lives alone in the config dir, so treating it as a data marker would
8/// re-collapse a clean four-dir install back onto the config dir. These are all
9/// real data/state artifacts that only exist in a pre-split (mixed) install.
10const DATA_MARKERS: &[&str] = &["stats.json", "sessions", "vectors", "graphs", "knowledge"];
11
12/// Resolve the lean-ctx data directory.
13///
14/// Priority order (backward-compatible XDG split, GH #408):
15/// 1. `LEAN_CTX_DATA_DIR` env var (explicit override)
16/// 2. `~/.lean-ctx` if it has actual data (legacy installs)
17/// 3. `$XDG_CONFIG_HOME/lean-ctx` if it has actual data (pre-split installs that
18///    mixed data into the config dir — kept in place, never silently moved)
19/// 4. `$XDG_DATA_HOME/lean-ctx` (default `~/.local/share/lean-ctx`) for fresh
20///    installs, so the config dir holds only config and stays RO-sandbox-safe.
21///
22/// An empty `~/.lean-ctx/` directory does NOT trigger legacy mode — this prevents
23/// data directory splits when setup creates the dir before MCP writes stats.
24pub fn lean_ctx_data_dir() -> Result<PathBuf, String> {
25    if let Ok(dir) = std::env::var("LEAN_CTX_DATA_DIR") {
26        let trimmed = dir.trim();
27        if !trimmed.is_empty() {
28            let p = PathBuf::from(trimmed);
29            ensure_dir_permissions(&p);
30            return Ok(p);
31        }
32    }
33
34    // Test sandbox (GL #512): without this, any unit test that triggers a
35    // store write (stats, savings ledger, context ledger, heatmap, ...)
36    // silently pollutes the developer's real ~/.lean-ctx — bounce events from
37    // test fixtures showed up as "today 61%" on the user dashboard. Tests that
38    // set LEAN_CTX_DATA_DIR keep full control (handled above); everyone else
39    // lands in a per-process temp dir and physically cannot touch real data.
40    #[cfg(test)]
41    {
42        Ok(test_sandbox_dir())
43    }
44
45    #[cfg(not(test))]
46    {
47        resolve_home_data_dir()
48    }
49}
50
51/// Per-process temp sandbox used as the default data dir under `#[cfg(test)]`,
52/// so any store write from a test fixture lands in a throwaway dir instead of
53/// the developer's real `~/.lean-ctx` (GL #512). Tests that need an *empty*,
54/// private dir should use [`isolated_data_dir`] instead (the shared sandbox is
55/// not empty: parallel tests write into it).
56#[cfg(test)]
57pub(crate) fn test_sandbox_dir() -> PathBuf {
58    static TEST_SANDBOX: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
59    TEST_SANDBOX
60        .get_or_init(|| {
61            let d = std::env::temp_dir().join(format!("lean-ctx-testdata-{}", std::process::id()));
62            let _ = std::fs::create_dir_all(&d);
63            d
64        })
65        .clone()
66}
67
68/// Home-based resolution (legacy `~/.lean-ctx` / mixed vs XDG). Split out so the
69/// priority rules stay unit-testable despite the test sandbox above.
70///
71/// The legacy/mixed back-compat decision lives in exactly ONE place,
72/// [`crate::core::paths::single_dir_override`], so the data dir can never
73/// disagree with config/state/cache (which all resolve through it): a legacy
74/// `~/.lean-ctx` or mixed `$XDG_CONFIG_HOME/lean-ctx` install wins only while it
75/// still holds data markers. Once `doctor --fix` has split it out, every
76/// category — data included — flips to its typed XDG dir, and a leftover
77/// (marker-free) `~/.lean-ctx` is no longer silently re-adopted (GH #436).
78fn resolve_home_data_dir() -> Result<PathBuf, String> {
79    // 1./2. Legacy or mixed single-dir install that still holds data → keep it
80    //       in place (back-compat). `LEAN_CTX_DATA_DIR` is handled by the caller,
81    //       and `single_dir_override` honors it too.
82    if let Some(dir) = crate::core::paths::single_dir_override() {
83        ensure_dir_permissions(&dir);
84        return Ok(dir);
85    }
86
87    // 3. Fresh / fully-split install: default DATA to `$XDG_DATA_HOME/lean-ctx`
88    //    (GH #408) so the config dir (`$XDG_CONFIG_HOME`) holds only config and
89    //    stays read-only-sandbox-safe.
90    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
91    let xdg_data = std::env::var("XDG_DATA_HOME")
92        .ok()
93        .filter(|s| !s.trim().is_empty())
94        .map_or_else(|| home.join(".local").join("share"), PathBuf::from);
95    let data_dir = xdg_data.join("lean-ctx");
96    ensure_dir_permissions(&data_dir);
97    Ok(data_dir)
98}
99
100pub(crate) fn has_data_files(dir: &std::path::Path) -> bool {
101    DATA_MARKERS.iter().any(|f| marker_has_data(&dir.join(f)))
102}
103
104/// A marker counts only when it actually carries data: a non-empty file, or a
105/// directory with at least one entry. An empty `sessions/` (a stray `mkdir`, a
106/// half-removed residue, a backup-restore artifact) must NOT collapse the whole
107/// layout onto a directory that holds no real data (GL #623 / #625).
108fn marker_has_data(path: &std::path::Path) -> bool {
109    match std::fs::metadata(path) {
110        Ok(m) if m.is_dir() => std::fs::read_dir(path).is_ok_and(|mut it| it.next().is_some()),
111        Ok(m) => m.len() > 0,
112        Err(_) => false,
113    }
114}
115
116/// Returns all known data directories that contain stats data, in resolution
117/// priority order (legacy → mixed config → XDG data). Used by the dual-dir
118/// consolidation ([`crate::core::data_consolidate`]) and doctor diagnostics.
119///
120/// `$XDG_DATA_HOME/lean-ctx` is included (GH #414): after the #408 default flip
121/// a fresh install writes stats there, so a user who *also* has a legacy/mixed
122/// tree has the split that the consolidation must detect and merge.
123pub fn all_data_dirs_with_stats() -> Vec<PathBuf> {
124    let mut dirs = Vec::new();
125    if let Some(home) = dirs::home_dir() {
126        let legacy = home.join(".lean-ctx");
127        if legacy.join("stats.json").exists() {
128            dirs.push(legacy);
129        }
130        let xdg_config = std::env::var("XDG_CONFIG_HOME")
131            .ok()
132            .filter(|s| !s.trim().is_empty())
133            .map_or_else(|| home.join(".config"), PathBuf::from)
134            .join("lean-ctx");
135        if xdg_config.join("stats.json").exists() && !dirs.contains(&xdg_config) {
136            dirs.push(xdg_config);
137        }
138        let xdg_data = std::env::var("XDG_DATA_HOME")
139            .ok()
140            .filter(|s| !s.trim().is_empty())
141            .map_or_else(|| home.join(".local").join("share"), PathBuf::from)
142            .join("lean-ctx");
143        if xdg_data.join("stats.json").exists() && !dirs.contains(&xdg_data) {
144            dirs.push(xdg_data);
145        }
146    }
147    dirs
148}
149
150#[cfg(unix)]
151pub(crate) fn ensure_dir_permissions(path: &std::path::Path) {
152    use std::os::unix::fs::PermissionsExt;
153    if path.is_dir() {
154        let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700));
155    }
156}
157
158#[cfg(not(unix))]
159pub(crate) fn ensure_dir_permissions(_path: &std::path::Path) {}
160
161pub fn test_env_lock() -> std::sync::MutexGuard<'static, ()> {
162    use std::sync::{Mutex, OnceLock};
163    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
164    let mutex = LOCK.get_or_init(|| Mutex::new(()));
165    mutex
166        .lock()
167        .unwrap_or_else(std::sync::PoisonError::into_inner)
168}
169
170/// RAII data-dir isolation for tests (GL #556): holds `test_env_lock` for
171/// the guard's lifetime, points `LEAN_CTX_DATA_DIR` at a fresh temp dir and
172/// restores the env on drop — even on panic, so a failing test cannot leak
173/// the override into others. Use this instead of hand-rolled
174/// `set_var`/`remove_var` pairs whenever a test needs an empty, private
175/// data dir (the shared per-process sandbox is NOT empty: parallel tests
176/// write stores like feedback, bandit and sessions into it).
177#[cfg(test)]
178pub struct IsolatedDataDir {
179    tmp: tempfile::TempDir,
180    _guard: std::sync::MutexGuard<'static, ()>,
181}
182
183#[cfg(test)]
184impl IsolatedDataDir {
185    pub fn path(&self) -> &std::path::Path {
186        self.tmp.path()
187    }
188}
189
190/// Category env vars pointed at the isolated temp dir so all four XDG
191/// categories (config/data/state/cache) collapse onto it in tests (GH #408).
192#[cfg(test)]
193const ISOLATED_ENV_VARS: &[&str] = &[
194    "LEAN_CTX_DATA_DIR",
195    "LEAN_CTX_CONFIG_DIR",
196    "LEAN_CTX_STATE_DIR",
197    "LEAN_CTX_CACHE_DIR",
198];
199
200#[cfg(test)]
201impl Drop for IsolatedDataDir {
202    fn drop(&mut self) {
203        // Struct Drop runs before field drops, so the env is restored while
204        // the lock is still held.
205        for var in ISOLATED_ENV_VARS {
206            crate::test_env::remove_var(var);
207        }
208    }
209}
210
211#[cfg(test)]
212pub fn isolated_data_dir() -> IsolatedDataDir {
213    let guard = test_env_lock();
214    let tmp = tempfile::tempdir().expect("tempdir for isolated data dir");
215    for var in ISOLATED_ENV_VARS {
216        crate::test_env::set_var(var, tmp.path());
217    }
218    IsolatedDataDir { tmp, _guard: guard }
219}
220
221#[cfg(test)]
222mod tests {
223    use super::*;
224
225    #[test]
226    fn has_data_files_empty_dir() {
227        let dir = std::env::temp_dir().join("test_data_dir_empty");
228        let _ = std::fs::remove_dir_all(&dir);
229        let _ = std::fs::create_dir_all(&dir);
230        assert!(!has_data_files(&dir));
231        let _ = std::fs::remove_dir_all(&dir);
232    }
233
234    #[test]
235    fn has_data_files_with_stats() {
236        let dir = std::env::temp_dir().join("test_data_dir_stats");
237        let _ = std::fs::remove_dir_all(&dir);
238        let _ = std::fs::create_dir_all(&dir);
239        std::fs::write(dir.join("stats.json"), "{}").unwrap();
240        assert!(has_data_files(&dir));
241        let _ = std::fs::remove_dir_all(&dir);
242    }
243
244    #[test]
245    fn has_data_files_ignores_config_only() {
246        // GH #408: config.toml (+ hooks) alone must NOT mark a dir as "has data",
247        // otherwise a clean post-split config dir would re-collapse the four-dir
248        // layout back onto itself via single_dir_override.
249        let dir = std::env::temp_dir().join("test_data_dir_config_only");
250        let _ = std::fs::remove_dir_all(&dir);
251        let _ = std::fs::create_dir_all(&dir);
252        std::fs::write(dir.join("config.toml"), "").unwrap();
253        std::fs::write(dir.join("env.sh"), "").unwrap();
254        assert!(!has_data_files(&dir), "config-only dir is not a data dir");
255        let _ = std::fs::remove_dir_all(&dir);
256    }
257
258    #[test]
259    fn fresh_install_defaults_data_to_xdg_data_home() {
260        // GH #408 flip: with no legacy/mixed data, a fresh install resolves DATA
261        // to $XDG_DATA_HOME/lean-ctx (not the config dir).
262        let _lock = test_env_lock();
263        let xdg_config = tempfile::tempdir().unwrap();
264        let xdg_data = tempfile::tempdir().unwrap();
265        crate::test_env::set_var("LEAN_CTX_DATA_DIR", "");
266        crate::test_env::set_var("XDG_CONFIG_HOME", xdg_config.path());
267        crate::test_env::set_var("XDG_DATA_HOME", xdg_data.path());
268
269        let result = resolve_home_data_dir().unwrap();
270
271        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
272        crate::test_env::remove_var("XDG_CONFIG_HOME");
273        crate::test_env::remove_var("XDG_DATA_HOME");
274
275        // A real `~/.lean-ctx` (legacy) would correctly take precedence; only
276        // assert the fresh default when it is absent (always true on CI).
277        let legacy = dirs::home_dir().unwrap().join(".lean-ctx");
278        if !legacy.exists() {
279            assert_eq!(result, xdg_data.path().join("lean-ctx"));
280        }
281    }
282
283    #[test]
284    fn empty_marker_dir_does_not_count() {
285        // GL #623/#625: an empty `sessions/` (a stray mkdir, a half-removed
286        // residue) must not flip the whole layout — only a marker carrying real
287        // data counts.
288        let dir = std::env::temp_dir().join("test_data_dir_empty_marker");
289        let _ = std::fs::remove_dir_all(&dir);
290        let _ = std::fs::create_dir_all(dir.join("sessions"));
291        assert!(!has_data_files(&dir), "empty marker dir must not count");
292        let _ = std::fs::remove_dir_all(&dir);
293    }
294
295    #[test]
296    fn empty_marker_file_does_not_count() {
297        // A zero-byte `stats.json` is not real data either (GL #625).
298        let dir = std::env::temp_dir().join("test_data_dir_empty_marker_file");
299        let _ = std::fs::remove_dir_all(&dir);
300        let _ = std::fs::create_dir_all(&dir);
301        std::fs::write(dir.join("stats.json"), "").unwrap();
302        assert!(!has_data_files(&dir), "empty marker file must not count");
303        let _ = std::fs::remove_dir_all(&dir);
304    }
305
306    #[test]
307    fn has_data_files_with_sessions() {
308        let dir = std::env::temp_dir().join("test_data_dir_sessions");
309        let _ = std::fs::remove_dir_all(&dir);
310        let _ = std::fs::create_dir_all(dir.join("sessions"));
311        // A non-empty sessions/ is real data (GL #625: empty dirs no longer count).
312        std::fs::write(dir.join("sessions").join("s1.json"), "{}").unwrap();
313        assert!(has_data_files(&dir));
314        let _ = std::fs::remove_dir_all(&dir);
315    }
316
317    #[test]
318    fn lean_ctx_data_dir_env_override() {
319        let _lock = test_env_lock();
320        let dir = std::env::temp_dir().join("test_data_dir_env");
321        let _ = std::fs::remove_dir_all(&dir);
322        let _ = std::fs::create_dir_all(&dir);
323        crate::test_env::set_var("LEAN_CTX_DATA_DIR", dir.to_str().unwrap());
324        let result = lean_ctx_data_dir().unwrap();
325        assert_eq!(result, dir);
326        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
327        let _ = std::fs::remove_dir_all(&dir);
328    }
329
330    #[test]
331    fn has_data_files_is_false_for_empty_dir() {
332        let dir = std::env::temp_dir().join("test_data_dir_no_data");
333        let _ = std::fs::remove_dir_all(&dir);
334        let _ = std::fs::create_dir_all(&dir);
335        std::fs::write(dir.join("random.txt"), "not a marker").unwrap();
336        assert!(!has_data_files(&dir));
337        let _ = std::fs::remove_dir_all(&dir);
338    }
339
340    #[test]
341    fn xdg_override_with_data_wins() {
342        let _lock = test_env_lock();
343
344        let xdg_base = std::env::temp_dir().join("test_xdg_override_wins");
345        let _ = std::fs::remove_dir_all(&xdg_base);
346        let xdg_dir = xdg_base.join("lean-ctx");
347        let _ = std::fs::create_dir_all(&xdg_dir);
348        std::fs::write(xdg_dir.join("stats.json"), r#"{"total_commands":1}"#).unwrap();
349
350        crate::test_env::set_var("LEAN_CTX_DATA_DIR", "");
351        crate::test_env::set_var("XDG_CONFIG_HOME", xdg_base.to_str().unwrap());
352
353        // Calls the home resolver directly: lean_ctx_data_dir() is sandboxed
354        // under cfg(test) (GL #512) and would short-circuit before XDG logic.
355        let result = resolve_home_data_dir().unwrap();
356
357        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
358        crate::test_env::remove_var("XDG_CONFIG_HOME");
359
360        let home = dirs::home_dir().unwrap();
361        let legacy = home.join(".lean-ctx");
362        if !has_data_files(&legacy) {
363            assert_eq!(
364                result, xdg_dir,
365                "XDG with data should win when legacy has no data"
366            );
367        }
368
369        let _ = std::fs::remove_dir_all(&xdg_base);
370    }
371
372    #[cfg(unix)]
373    fn restore_env(key: &str, val: Option<std::ffi::OsString>) {
374        match val {
375            Some(v) => crate::test_env::set_var(key, v),
376            None => crate::test_env::remove_var(key),
377        }
378    }
379
380    #[cfg(unix)]
381    #[test]
382    fn markerless_legacy_dir_does_not_win() {
383        // GH #436: after `doctor --fix` moves data to XDG, `~/.lean-ctx` lingers
384        // (runtime leftovers) but holds no data markers. It must NOT keep being
385        // re-adopted as the data dir — data must flip to $XDG_DATA_HOME/lean-ctx,
386        // exactly like config/state/cache already do via single_dir_override.
387        let _lock = test_env_lock();
388        let tmp = tempfile::tempdir().unwrap();
389        let home = tmp.path().join("home");
390        let xdg_data = tmp.path().join("xdg-data");
391        let legacy = home.join(".lean-ctx");
392        std::fs::create_dir_all(&legacy).unwrap();
393        // A runtime leftover (daemon.pid) is not a data marker.
394        std::fs::write(legacy.join("daemon.pid"), "123").unwrap();
395
396        let saved_home = std::env::var_os("HOME");
397        let saved_config = std::env::var_os("XDG_CONFIG_HOME");
398        let saved_data = std::env::var_os("XDG_DATA_HOME");
399        crate::test_env::set_var("HOME", &home);
400        crate::test_env::remove_var("XDG_CONFIG_HOME");
401        crate::test_env::set_var("XDG_DATA_HOME", &xdg_data);
402        crate::test_env::set_var("LEAN_CTX_DATA_DIR", "");
403
404        let result = resolve_home_data_dir().unwrap();
405
406        // Restore before asserting so a failure can't leak env into other tests.
407        restore_env("HOME", saved_home);
408        restore_env("XDG_CONFIG_HOME", saved_config);
409        restore_env("XDG_DATA_HOME", saved_data);
410        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
411
412        assert_eq!(
413            result,
414            xdg_data.join("lean-ctx"),
415            "marker-free legacy dir must not be re-adopted as the data dir"
416        );
417    }
418}