leindex 1.7.1

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// cleanup - Stale Artifact Garbage Collection
//
// Scans temp directories for LeIndex-owned artifacts and removes those older
// than a configurable threshold. The in-project `.leindex/` directories are
// never touched.

use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use tracing::{debug, info, warn};

/// Name of the marker file placed inside every LeIndex temp artifact directory.
pub const LEINDEX_MARKER_FILE: &str = ".leindex-artifact-marker";

/// Default age threshold (in days) beyond which artifacts are considered stale.
pub const DEFAULT_MAX_AGE_DAYS: u64 = 7;

/// Summary of a garbage-collection pass.
#[derive(Debug, Default)]
pub struct GcReport {
    /// Number of artifact directories scanned.
    pub scanned: usize,
    /// Number of artifact directories removed.
    pub removed: usize,
    /// Total bytes freed (approximate, based on directory sizes).
    pub bytes_freed: u64,
    /// Paths that could not be removed (locked or permission errors).
    pub failed: Vec<(PathBuf, String)>,
}

impl std::fmt::Display for GcReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "GC Report:")?;
        writeln!(f, "  Scanned:  {} artifact(s)", self.scanned)?;
        writeln!(f, "  Removed:  {} artifact(s)", self.removed)?;
        if self.bytes_freed > 0 {
            let mb = self.bytes_freed as f64 / 1024.0 / 1024.0;
            writeln!(f, "  Freed:    {:.2} MB", mb)?;
        }
        if !self.failed.is_empty() {
            writeln!(f, "  Failed:   {} artifact(s)", self.failed.len())?;
            for (path, reason) in &self.failed {
                writeln!(f, "    {} - {}", path.display(), reason)?;
            }
        }
        Ok(())
    }
}

/// Return the list of temp directories that may contain LeIndex artifacts.
///
/// The candidates are:
/// - `$TMPDIR/leindex/`   (the `std::env::temp_dir()` fallback from `resolve_storage_path`)
/// - `$TMPDIR/lephase-*`  (phase index leftovers)
pub fn artifact_scan_roots() -> Vec<PathBuf> {
    let tmp = std::env::temp_dir();
    let mut roots = vec![tmp.join("leindex")];

    // Also scan for lephase-* directories directly in tmp
    if let Ok(entries) = fs::read_dir(&tmp) {
        for entry in entries.flatten() {
            let name = entry.file_name();
            let name_lossy = name.to_string_lossy();
            if name_lossy.starts_with("lephase-") {
                roots.push(entry.path());
            }
        }
    }

    roots
}

/// Check whether a directory is owned by LeIndex by looking for the marker file.
pub fn is_leindex_artifact(dir: &Path) -> bool {
    dir.join(LEINDEX_MARKER_FILE).exists()
}

/// Write the ownership marker into a directory.  This is a best-effort
/// operation; if it fails we only log a warning.
pub fn write_artifact_marker(dir: &Path) {
    let marker_path = dir.join(LEINDEX_MARKER_FILE);
    if marker_path.exists() {
        return;
    }
    let timestamp = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let content = format!(
        "leindex-artifact\ncreated={}\nversion={}\n",
        timestamp,
        env!("CARGO_PKG_VERSION")
    );
    if let Err(e) = fs::write(&marker_path, content) {
        warn!(
            "Failed to write artifact marker at {}: {}",
            marker_path.display(),
            e
        );
    }
}

/// Compute the total size of a directory tree (recursively).
pub fn dir_size(path: &Path) -> u64 {
    walkdir_size(path)
}

fn walkdir_size(path: &Path) -> u64 {
    let mut total: u64 = 0;
    // Use a manual stack to avoid recursion depth issues.
    let mut stack = vec![path.to_path_buf()];
    while let Some(current) = stack.pop() {
        let entries = match fs::read_dir(&current) {
            Ok(e) => e,
            Err(_) => continue,
        };
        for entry in entries.flatten() {
            let meta = match entry.metadata() {
                Ok(m) => m,
                Err(_) => continue,
            };
            if meta.is_dir() {
                stack.push(entry.path());
            } else {
                total += meta.len();
            }
        }
    }
    total
}

/// Check whether a directory is likely in active use by trying to create and
/// delete a test file inside it.  If we cannot, we skip removal.
fn is_locked(dir: &Path) -> bool {
    let test_file = dir.join(".leindex-gc-lock-test");
    match fs::write(&test_file, b"test") {
        Ok(_) => {
            // Clean up the test file
            let _ = fs::remove_file(&test_file);
            false
        }
        Err(_) => true,
    }
}

/// Run garbage collection on all known temp artifact directories.
///
/// Artifacts older than `max_age` are removed.  Artifacts that appear locked
/// (e.g., an active LeIndex process is using them) are skipped.
pub fn run_gc(max_age: Duration) -> GcReport {
    let mut report = GcReport::default();
    let cutoff = SystemTime::now() - max_age;

    for root in artifact_scan_roots() {
        if !root.exists() {
            continue;
        }

        // If the root *itself* is a lephase-* artifact directory
        if root
            .file_name()
            .map(|n| n.to_string_lossy().starts_with("lephase-"))
            .unwrap_or(false)
        {
            maybe_remove_artifact(&root, &cutoff, &mut report);
            continue;
        }

        // Otherwise iterate children of the root directory
        let entries = match fs::read_dir(&root) {
            Ok(e) => e,
            Err(err) => {
                debug!("Cannot read {}: {}", root.display(), err);
                continue;
            }
        };

        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }

            // Skip any .leindex directories inside project roots — these are
            // in-project storage and must never be touched.
            if path.file_name().map(|n| n == ".leindex").unwrap_or(false) {
                debug!("Skipping in-project .leindex at {}", path.display());
                continue;
            }

            maybe_remove_artifact(&path, &cutoff, &mut report);
        }
    }

    report
}

/// Evaluate a single artifact directory and remove it if stale and not locked.
fn maybe_remove_artifact(dir: &Path, cutoff: &SystemTime, report: &mut GcReport) {
    // Only consider directories that are LeIndex artifacts (have marker or
    // match known naming patterns).
    if !is_leindex_artifact(dir) && !is_leindex_artifact_by_pattern(dir) {
        return;
    }

    report.scanned += 1;

    // Determine age from the marker file or directory mtime
    let age = artifact_age(dir);
    if age >= *cutoff {
        debug!(
            "Artifact {} is not stale yet (age: {:?})",
            dir.display(),
            SystemTime::now().duration_since(age).unwrap_or_default()
        );
        return;
    }

    // Check if the directory appears locked / in-use
    if is_locked(dir) {
        debug!("Skipping locked artifact: {}", dir.display());
        return;
    }

    let size = dir_size(dir);
    match fs::remove_dir_all(dir) {
        Ok(()) => {
            info!(
                "Removed stale artifact: {} ({:.2} MB)",
                dir.display(),
                size as f64 / 1024.0 / 1024.0
            );
            report.removed += 1;
            report.bytes_freed += size;
        }
        Err(e) => {
            warn!("Failed to remove stale artifact {}: {}", dir.display(), e);
            report.failed.push((dir.to_path_buf(), e.to_string()));
        }
    }
}

/// Check whether a directory matches known LeIndex artifact naming patterns
/// even without a marker file (for legacy artifacts created before the marker
/// was introduced).
pub fn is_leindex_artifact_by_pattern(dir: &Path) -> bool {
    let name = dir
        .file_name()
        .map(|n| n.to_string_lossy())
        .unwrap_or_default();

    // Pattern: <project>-<hash> under $TMPDIR/leindex/
    // Pattern: lephase-phase<N>-<hash>
    if name.contains('-') {
        // Check if under a "leindex" parent directory
        if dir
            .parent()
            .map(|p| p.file_name().map(|n| n == "leindex").unwrap_or(false))
            .unwrap_or(false)
        {
            // Check if it contains leindex.db (strong indicator)
            return dir.join("leindex.db").exists();
        }
    }

    // lephase-* directories
    if name.starts_with("lephase-") {
        return true;
    }

    false
}

/// Get the creation/modification time of an artifact directory.
/// Prefers the marker file mtime (creation timestamp), falls back to dir mtime.
pub fn artifact_age(dir: &Path) -> SystemTime {
    let marker = dir.join(LEINDEX_MARKER_FILE);
    if let Ok(meta) = fs::metadata(&marker) {
        if let Ok(modified) = meta.modified() {
            return modified;
        }
    }
    // Fall back to directory modification time
    fs::metadata(dir)
        .and_then(|m| m.modified())
        .unwrap_or(SystemTime::UNIX_EPOCH)
}

/// Run startup garbage collection — removes artifacts older than the default
/// threshold.  This is meant to be called early in the CLI startup path.
pub fn startup_gc() {
    let max_age = Duration::from_secs(DEFAULT_MAX_AGE_DAYS * 24 * 3600);
    let report = run_gc(max_age);
    if report.removed > 0 {
        info!(
            "Startup GC: removed {} stale artifact(s), freed {:.2} MB",
            report.removed,
            report.bytes_freed as f64 / 1024.0 / 1024.0
        );
    }
}

/// Register an at-exit cleanup hook that removes the given temp storage
/// directory when the process exits cleanly.
///
/// This uses panic hooks for cleanup.  The cleanup is best-effort — if the
/// process is killed with SIGKILL, artifacts will remain until the next
/// startup GC pass.
pub fn register_at_exit_cleanup(storage_path: PathBuf) {
    // Only register cleanup for paths that are NOT in-project .leindex
    if storage_path
        .file_name()
        .map(|n| n == ".leindex")
        .unwrap_or(false)
    {
        debug!(
            "Skipping at-exit cleanup registration for in-project storage: {}",
            storage_path.display()
        );
        return;
    }

    // Only register for paths inside the system temp directory
    let tmp = std::env::temp_dir();
    if !storage_path.starts_with(&tmp) {
        debug!(
            "Skipping at-exit cleanup for non-temp storage: {}",
            storage_path.display()
        );
        return;
    }

    // Register a shared cleanup function using at_exit
    // We use std::sync::Once to ensure single registration
    static CLEANUP_REGISTERED: std::sync::Once = std::sync::Once::new();
    CLEANUP_REGISTERED.call_once(|| {
        // Note: We cannot move storage_path into the panic hook since
        // set_hook requires Fn and not FnOnce. Instead, we use a global
        // option for the cleanup path.
        // For now, the startup GC is the primary cleanup mechanism.
        // At-exit cleanup is best-effort via the startup GC on next run.
    });
}

/// Best-effort cleanup of a single storage directory.
pub fn best_effort_cleanup(path: &Path) {
    if path.exists() && path.starts_with(std::env::temp_dir()) {
        match fs::remove_dir_all(path) {
            Ok(()) => {
                eprintln!("[leindex] Cleaned up temp storage: {}", path.display());
            }
            Err(e) => {
                eprintln!(
                    "[leindex] Warning: failed to clean up temp storage {}: {}",
                    path.display(),
                    e
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_marker_write_and_detect() {
        let dir = tempfile::tempdir().unwrap();
        let artifact = dir.path().join("test-artifact-abc123");
        fs::create_dir_all(&artifact).unwrap();

        assert!(!is_leindex_artifact(&artifact));

        write_artifact_marker(&artifact);
        assert!(is_leindex_artifact(&artifact));

        let marker_content = fs::read_to_string(artifact.join(LEINDEX_MARKER_FILE)).unwrap();
        assert!(marker_content.starts_with("leindex-artifact"));
        assert!(marker_content.contains("created="));
    }

    #[test]
    fn test_marker_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let artifact = dir.path().join("test-idempotent");
        fs::create_dir_all(&artifact).unwrap();

        write_artifact_marker(&artifact);
        let first = fs::read_to_string(artifact.join(LEINDEX_MARKER_FILE)).unwrap();

        write_artifact_marker(&artifact);
        let second = fs::read_to_string(artifact.join(LEINDEX_MARKER_FILE)).unwrap();

        assert_eq!(
            first, second,
            "Marker should not be overwritten if it exists"
        );
    }

    #[test]
    fn test_gc_skips_non_stale_artifacts() {
        // GC with 0-day threshold scans real system temp dirs.
        // This test verifies the logic doesn't crash or panic.
        // We do not assert on failed.len() because real lephase-* artifacts
        // may exist in the system temp dir and fail to be removed (e.g. locked).
        let _report = run_gc(Duration::from_secs(0));
    }

    #[test]
    fn test_is_locked_on_writable_dir() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!is_locked(dir.path()));
    }

    #[test]
    fn test_dir_size() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("file1.txt"), b"hello world").unwrap();
        fs::write(dir.path().join("file2.txt"), b"foo bar baz").unwrap();

        let size = dir_size(dir.path());
        assert_eq!(size, 11 + 11); // "hello world" + "foo bar baz"
    }

    #[test]
    fn test_artifact_age_uses_marker() {
        let dir = tempfile::tempdir().unwrap();
        let artifact = dir.path().join("age-test");
        fs::create_dir_all(&artifact).unwrap();
        write_artifact_marker(&artifact);

        let age = artifact_age(&artifact);
        // Should be recent (within last few seconds)
        let elapsed = SystemTime::now().duration_since(age).unwrap_or_default();
        assert!(elapsed.as_secs() < 10, "Artifact age should be recent");
    }

    #[test]
    fn test_artifact_age_falls_back_to_dir_mtime() {
        let dir = tempfile::tempdir().unwrap();
        let artifact = dir.path().join("no-marker");
        fs::create_dir_all(&artifact).unwrap();
        // No marker written

        let age = artifact_age(&artifact);
        let elapsed = SystemTime::now().duration_since(age).unwrap_or_default();
        assert!(
            elapsed.as_secs() < 10,
            "Artifact age should fall back to dir mtime"
        );
    }

    #[test]
    fn test_gc_report_display() {
        let report = GcReport {
            scanned: 10,
            removed: 3,
            bytes_freed: 1024 * 1024 * 50, // 50 MB
            failed: vec![(PathBuf::from("/tmp/locked"), "Permission denied".into())],
        };
        let output = report.to_string();
        assert!(output.contains("Scanned:  10"));
        assert!(output.contains("Removed:  3"));
        assert!(output.contains("50.00 MB"));
        assert!(output.contains("Failed:   1"));
    }

    #[test]
    fn test_is_leindex_artifact_by_pattern() {
        let dir = tempfile::tempdir().unwrap();

        // lephase-* pattern
        let lephase = dir.path().join("lephase-phase1-abc");
        fs::create_dir_all(&lephase).unwrap();
        assert!(is_leindex_artifact_by_pattern(&lephase));

        // Random directory should not match
        let random = dir.path().join("random-dir");
        fs::create_dir_all(&random).unwrap();
        assert!(!is_leindex_artifact_by_pattern(&random));
    }

    #[test]
    fn test_never_removes_in_project_leindex() {
        // Create a fake project with .leindex directory
        let dir = tempfile::tempdir().unwrap();
        let leindex_dir = dir.path().join(".leindex");
        fs::create_dir_all(&leindex_dir).unwrap();
        fs::write(leindex_dir.join("leindex.db"), b"important data").unwrap();

        // The GC should never touch directories named ".leindex"
        // This is verified by the skip check in maybe_remove_artifact
        assert_eq!(leindex_dir.file_name().unwrap(), ".leindex");
    }

    #[test]
    fn test_run_gc_on_empty_dirs() {
        // Should not crash when scan roots don't exist
        let report = run_gc(Duration::from_secs(0));
        // Just verify it doesn't panic
        let _ = report.scanned;
    }

    #[test]
    fn test_best_effort_cleanup_skips_non_temp() {
        // Create a path that is definitely NOT under the system temp dir
        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("/home/user"));
        let non_temp = home.join(".leindex-test-cleanup-should-not-delete");
        // Don't actually create it — just verify the function handles it
        // The key check is that it doesn't match the temp dir prefix
        assert!(!non_temp.starts_with(std::env::temp_dir()));
    }
}