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` vs XDG). Split out so the
69/// priority rules stay unit-testable despite the test sandbox above.
70fn resolve_home_data_dir() -> Result<PathBuf, String> {
71    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
72
73    // 1. Legacy `~/.lean-ctx` holding real data → keep it (back-compat).
74    let legacy = home.join(".lean-ctx");
75    if legacy.exists() && has_data_files(&legacy) {
76        ensure_dir_permissions(&legacy);
77        return Ok(legacy);
78    }
79
80    // 2. Pre-split install that mixed data into `$XDG_CONFIG_HOME/lean-ctx` →
81    //    keep it there. Existing users never get their data silently relocated;
82    //    `lean-ctx doctor --fix` performs the per-category split on demand.
83    let xdg_config = std::env::var("XDG_CONFIG_HOME")
84        .ok()
85        .filter(|s| !s.trim().is_empty())
86        .map_or_else(|| home.join(".config"), PathBuf::from);
87    let mixed_config = xdg_config.join("lean-ctx");
88    if mixed_config.exists() && has_data_files(&mixed_config) {
89        ensure_dir_permissions(&mixed_config);
90        return Ok(mixed_config);
91    }
92
93    // 3. A non-empty legacy dir without data markers (e.g. user-created) keeps
94    //    winning so we don't surprise such setups.
95    if legacy.exists() {
96        ensure_dir_permissions(&legacy);
97        return Ok(legacy);
98    }
99
100    // 4. Fresh install: default DATA to `$XDG_DATA_HOME/lean-ctx` (GH #408) so
101    //    the config dir (`$XDG_CONFIG_HOME`) holds only config and stays
102    //    read-only-sandbox-safe.
103    let xdg_data = std::env::var("XDG_DATA_HOME")
104        .ok()
105        .filter(|s| !s.trim().is_empty())
106        .map_or_else(|| home.join(".local").join("share"), PathBuf::from);
107    let data_dir = xdg_data.join("lean-ctx");
108    ensure_dir_permissions(&data_dir);
109    Ok(data_dir)
110}
111
112pub(crate) fn has_data_files(dir: &std::path::Path) -> bool {
113    DATA_MARKERS.iter().any(|f| dir.join(f).exists())
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            std::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        std::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        std::env::set_var("LEAN_CTX_DATA_DIR", "");
266        std::env::set_var("XDG_CONFIG_HOME", xdg_config.path());
267        std::env::set_var("XDG_DATA_HOME", xdg_data.path());
268
269        let result = resolve_home_data_dir().unwrap();
270
271        std::env::remove_var("LEAN_CTX_DATA_DIR");
272        std::env::remove_var("XDG_CONFIG_HOME");
273        std::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 has_data_files_with_sessions() {
285        let dir = std::env::temp_dir().join("test_data_dir_sessions");
286        let _ = std::fs::remove_dir_all(&dir);
287        let _ = std::fs::create_dir_all(&dir);
288        let _ = std::fs::create_dir_all(dir.join("sessions"));
289        assert!(has_data_files(&dir));
290        let _ = std::fs::remove_dir_all(&dir);
291    }
292
293    #[test]
294    fn lean_ctx_data_dir_env_override() {
295        let _lock = test_env_lock();
296        let dir = std::env::temp_dir().join("test_data_dir_env");
297        let _ = std::fs::remove_dir_all(&dir);
298        let _ = std::fs::create_dir_all(&dir);
299        std::env::set_var("LEAN_CTX_DATA_DIR", dir.to_str().unwrap());
300        let result = lean_ctx_data_dir().unwrap();
301        assert_eq!(result, dir);
302        std::env::remove_var("LEAN_CTX_DATA_DIR");
303        let _ = std::fs::remove_dir_all(&dir);
304    }
305
306    #[test]
307    fn has_data_files_is_false_for_empty_dir() {
308        let dir = std::env::temp_dir().join("test_data_dir_no_data");
309        let _ = std::fs::remove_dir_all(&dir);
310        let _ = std::fs::create_dir_all(&dir);
311        std::fs::write(dir.join("random.txt"), "not a marker").unwrap();
312        assert!(!has_data_files(&dir));
313        let _ = std::fs::remove_dir_all(&dir);
314    }
315
316    #[test]
317    fn xdg_override_with_data_wins() {
318        let _lock = test_env_lock();
319
320        let xdg_base = std::env::temp_dir().join("test_xdg_override_wins");
321        let _ = std::fs::remove_dir_all(&xdg_base);
322        let xdg_dir = xdg_base.join("lean-ctx");
323        let _ = std::fs::create_dir_all(&xdg_dir);
324        std::fs::write(xdg_dir.join("stats.json"), r#"{"total_commands":1}"#).unwrap();
325
326        std::env::set_var("LEAN_CTX_DATA_DIR", "");
327        std::env::set_var("XDG_CONFIG_HOME", xdg_base.to_str().unwrap());
328
329        // Calls the home resolver directly: lean_ctx_data_dir() is sandboxed
330        // under cfg(test) (GL #512) and would short-circuit before XDG logic.
331        let result = resolve_home_data_dir().unwrap();
332
333        std::env::remove_var("LEAN_CTX_DATA_DIR");
334        std::env::remove_var("XDG_CONFIG_HOME");
335
336        let home = dirs::home_dir().unwrap();
337        let legacy = home.join(".lean-ctx");
338        if !has_data_files(&legacy) {
339            assert_eq!(
340                result, xdg_dir,
341                "XDG with data should win when legacy has no data"
342            );
343        }
344
345        let _ = std::fs::remove_dir_all(&xdg_base);
346    }
347}