lean_ctx/cli/config_cmd/
cache.rs1use super::{load_mcp_live_stats, mcp_cache_stats_lines, prune_bm25_caches, prune_graph_caches};
4
5pub fn cmd_cache(args: &[String]) {
6 if let Err(error) = try_cmd_cache(args) {
7 eprintln!("CLI cache operation failed: {error}");
8 std::process::exit(1);
9 }
10}
11
12fn try_cmd_cache(args: &[String]) -> std::io::Result<()> {
13 use crate::core::cli_cache;
14 match args.first().map(std::string::String::as_str) {
15 Some("clear") => {
16 let count = cli_cache::clear()?;
17 println!("Cleared {count} cached entries.");
18 }
19 Some("reset") => {
20 let project_flag = args.get(1).map(std::string::String::as_str) == Some("--project");
21 if project_flag {
22 let root =
23 crate::core::session::SessionState::load_latest().and_then(|s| s.project_root);
24 if let Some(root) = root {
25 let count = cli_cache::clear_project(&root)?;
26 println!("Reset {count} cache entries for project: {root}");
27 } else {
28 eprintln!("No active project root found. Start a session first.");
29 std::process::exit(1);
30 }
31 } else {
32 let count = cli_cache::clear()?;
33 println!("Reset all {count} cache entries.");
34 }
35 }
36 Some("stats") => {
37 let (hits, reads, entries) = cli_cache::stats()?;
38 let rate = if reads > 0 {
39 (hits as f64 / reads as f64 * 100.0).round() as u32
40 } else {
41 0
42 };
43 println!("CLI Cache Stats (lean-ctx read / lean-ctx grep):");
44 println!(" Entries: {entries}");
45 println!(" Reads: {reads}");
46 println!(" Hits: {hits}");
47 println!(" Hit Rate: {rate}%");
48
49 if let Some(value) = load_mcp_live_stats() {
50 println!();
51 for line in mcp_cache_stats_lines(&value) {
52 println!("{line}");
53 }
54 } else {
55 println!();
56 println!("MCP Session Cache: no data yet (start a session with your AI editor)");
57 }
58 }
59 Some("invalidate") => {
60 if args.len() < 2 {
61 eprintln!("Usage: lean-ctx cache invalidate <path>");
62 std::process::exit(1);
63 }
64 cli_cache::invalidate(&args[1])?;
65 println!("Invalidated cache for {}", args[1]);
66 }
67 Some("prune") => {
68 let bm25 = prune_bm25_caches();
69 let graph = prune_graph_caches();
70 let archive_before = crate::core::archive::disk_usage_bytes()
73 + crate::core::archive_fts::db_size_bytes();
74 let archive_removed = crate::core::archive::cleanup();
75 let _ = crate::core::archive_fts::enforce_cap();
76 let archive_after = crate::core::archive::disk_usage_bytes()
77 + crate::core::archive_fts::db_size_bytes();
78 let archive_freed = archive_before.saturating_sub(archive_after);
79
80 let orphans = crate::core::knowledge::maintenance::prune_orphaned_stores();
84
85 let removed = bm25.removed + graph.removed + archive_removed + orphans.removed as u32;
86 let failed = bm25.failed + graph.failed;
87 let freed =
88 bm25.bytes_freed + graph.bytes_freed + archive_freed + orphans.reclaimed_bytes;
89 println!(
90 "Pruned {} entries, freed {:.1} MB (BM25: {}, graphs: {}, archive: {}, orphaned stores: {}, failed: {})",
91 removed,
92 freed as f64 / 1_048_576.0,
93 bm25.removed,
94 graph.removed,
95 archive_removed,
96 orphans.removed,
97 failed,
98 );
99 }
100 _ => {
101 let (hits, reads, entries) = cli_cache::stats()?;
102 let rate = if reads > 0 {
103 (hits as f64 / reads as f64 * 100.0).round() as u32
104 } else {
105 0
106 };
107 println!("CLI File Cache: {entries} entries, {hits}/{reads} hits ({rate}%)");
108 println!();
109 println!("Subcommands:");
110 println!(" cache stats Show detailed stats");
111 println!(" cache clear Clear all cached entries");
112 println!(" cache reset Reset all cache (or --project for current project only)");
113 println!(" cache invalidate Remove specific file from cache");
114 println!(
115 " cache prune Reclaim BM25 + graph indexes, archive, and orphaned knowledge stores"
116 );
117 }
118 }
119 Ok(())
120}