Skip to main content

lean_ctx/core/
cli_cache.rs

1use md5::{Digest, Md5};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::path::PathBuf;
5use std::time::{SystemTime, UNIX_EPOCH};
6
7const CACHE_TTL_SECS: u64 = 300;
8const MAX_ENTRIES: usize = 200;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct CliCacheEntry {
12    pub path: String,
13    pub hash: String,
14    pub line_count: usize,
15    pub original_tokens: usize,
16    pub timestamp: u64,
17    pub read_count: u32,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, Default)]
21pub struct CliCacheStore {
22    pub entries: HashMap<String, CliCacheEntry>,
23    pub total_hits: u64,
24    pub total_reads: u64,
25}
26
27pub enum CacheResult {
28    Hit {
29        entry: CliCacheEntry,
30        file_ref: String,
31    },
32    Miss {
33        content: String,
34    },
35}
36
37fn cache_dir() -> Option<PathBuf> {
38    crate::core::data_dir::lean_ctx_data_dir()
39        .ok()
40        .map(|d| d.join("cli-cache"))
41}
42
43fn cache_file() -> Option<PathBuf> {
44    cache_dir().map(|d| d.join("cache.json"))
45}
46
47fn now_secs() -> u64 {
48    SystemTime::now()
49        .duration_since(UNIX_EPOCH)
50        .unwrap_or_default()
51        .as_secs()
52}
53
54fn compute_md5(content: &str) -> String {
55    let mut hasher = Md5::new();
56    hasher.update(content.as_bytes());
57    format!("{:x}", hasher.finalize())
58}
59
60fn normalize_key(path: &str) -> String {
61    crate::core::pathutil::normalize_tool_path(path)
62}
63
64fn load_store() -> CliCacheStore {
65    let Some(path) = cache_file() else {
66        return CliCacheStore::default();
67    };
68    match std::fs::read_to_string(&path) {
69        Ok(data) => serde_json::from_str(&data).unwrap_or_default(),
70        Err(_) => CliCacheStore::default(),
71    }
72}
73
74fn save_store(store: &CliCacheStore) {
75    let Some(dir) = cache_dir() else { return };
76    let _ = std::fs::create_dir_all(&dir);
77    let path = dir.join("cache.json");
78    if let Ok(data) = serde_json::to_string(store) {
79        let _ = std::fs::write(path, data);
80    }
81}
82
83fn file_ref(key: &str, store: &CliCacheStore) -> String {
84    let keys: Vec<&String> = store.entries.keys().collect();
85    let idx = keys
86        .iter()
87        .position(|k| k.as_str() == key)
88        .unwrap_or(store.entries.len());
89    format!("F{}", idx + 1)
90}
91
92pub fn check_and_read(path: &str) -> CacheResult {
93    let Ok(content) = crate::core::io_boundary::read_file_lossy(path) else {
94        return CacheResult::Miss {
95            content: String::new(),
96        };
97    };
98
99    let key = normalize_key(path);
100    let hash = compute_md5(&content);
101    let now = now_secs();
102    let mut store = load_store();
103
104    store.total_reads += 1;
105
106    if let Some(entry) = store.entries.get_mut(&key) {
107        if entry.hash == hash && (now - entry.timestamp) < CACHE_TTL_SECS {
108            entry.read_count += 1;
109            entry.timestamp = now;
110            store.total_hits += 1;
111            let result = CacheResult::Hit {
112                entry: entry.clone(),
113                file_ref: file_ref(&key, &store),
114            };
115            save_store(&store);
116            return result;
117        }
118    }
119
120    let line_count = content.lines().count();
121    let original_tokens = crate::core::tokens::count_tokens(&content);
122
123    let entry = CliCacheEntry {
124        path: key.clone(),
125        hash,
126        line_count,
127        original_tokens,
128        timestamp: now,
129        read_count: 1,
130    };
131    store.entries.insert(key, entry);
132
133    evict_stale(&mut store, now);
134
135    save_store(&store);
136    CacheResult::Miss { content }
137}
138
139pub fn invalidate(path: &str) {
140    let key = normalize_key(path);
141    let mut store = load_store();
142    store.entries.remove(&key);
143    save_store(&store);
144}
145
146pub fn clear() -> usize {
147    let mut store = load_store();
148    let count = store.entries.len();
149    store.entries.clear();
150    save_store(&store);
151    count
152}
153
154pub fn clear_project(project_root: &str) -> usize {
155    let mut store = load_store();
156    let prefix = normalize_key(project_root);
157    let before = store.entries.len();
158    store
159        .entries
160        .retain(|key, entry| !key.starts_with(&prefix) && !entry.path.starts_with(&prefix));
161    let removed = before - store.entries.len();
162    save_store(&store);
163    removed
164}
165
166pub fn stats() -> (u64, u64, usize) {
167    let store = load_store();
168    (store.total_hits, store.total_reads, store.entries.len())
169}
170
171fn evict_stale(store: &mut CliCacheStore, now: u64) {
172    store
173        .entries
174        .retain(|_, e| (now - e.timestamp) < CACHE_TTL_SECS);
175
176    if store.entries.len() > MAX_ENTRIES {
177        let mut entries: Vec<(String, u64)> = store
178            .entries
179            .iter()
180            .map(|(k, e)| (k.clone(), e.timestamp))
181            .collect();
182        entries.sort_by_key(|(_, ts)| *ts);
183        let to_remove = store.entries.len() - MAX_ENTRIES;
184        for (key, _) in entries.into_iter().take(to_remove) {
185            store.entries.remove(&key);
186        }
187    }
188}
189
190pub fn format_hit(entry: &CliCacheEntry, file_ref: &str, short_path: &str) -> String {
191    if crate::core::protocol::savings_footer_visible() {
192        format!(
193            "{file_ref} cached {short_path} [{}L {}t] (read #{})",
194            entry.line_count, entry.original_tokens, entry.read_count
195        )
196    } else {
197        format!("cached {short_path} [{}L]", entry.line_count)
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204
205    #[test]
206    fn compute_md5_deterministic() {
207        let h1 = compute_md5("test content");
208        let h2 = compute_md5("test content");
209        assert_eq!(h1, h2);
210        assert_ne!(h1, compute_md5("different"));
211    }
212
213    #[test]
214    fn evict_stale_removes_old_entries() {
215        let mut store = CliCacheStore::default();
216        store.entries.insert(
217            "/old.rs".to_string(),
218            CliCacheEntry {
219                path: "/old.rs".to_string(),
220                hash: "h1".into(),
221                line_count: 10,
222                original_tokens: 50,
223                timestamp: 1000,
224                read_count: 1,
225            },
226        );
227        store.entries.insert(
228            "/new.rs".to_string(),
229            CliCacheEntry {
230                path: "/new.rs".to_string(),
231                hash: "h2".into(),
232                line_count: 20,
233                original_tokens: 100,
234                timestamp: now_secs(),
235                read_count: 1,
236            },
237        );
238
239        evict_stale(&mut store, now_secs());
240        assert!(!store.entries.contains_key("/old.rs"));
241        assert!(store.entries.contains_key("/new.rs"));
242    }
243
244    #[test]
245    fn evict_respects_max_entries() {
246        let mut store = CliCacheStore::default();
247        let now = now_secs();
248        for i in 0..MAX_ENTRIES + 10 {
249            store.entries.insert(
250                format!("/file_{i}.rs"),
251                CliCacheEntry {
252                    path: format!("/file_{i}.rs"),
253                    hash: format!("h{i}"),
254                    line_count: 1,
255                    original_tokens: 10,
256                    timestamp: now - i as u64,
257                    read_count: 1,
258                },
259            );
260        }
261        evict_stale(&mut store, now);
262        assert!(store.entries.len() <= MAX_ENTRIES);
263    }
264
265    #[test]
266    fn format_hit_output() {
267        let _lock = crate::core::data_dir::test_env_lock();
268        let entry = CliCacheEntry {
269            path: "/test.rs".into(),
270            hash: "abc".into(),
271            line_count: 42,
272            original_tokens: 500,
273            timestamp: now_secs(),
274            read_count: 3,
275        };
276        std::env::set_var("LEAN_CTX_SAVINGS_FOOTER", "never");
277        let output = format_hit(&entry, "F1", "test.rs");
278        assert!(output.contains("cached test.rs"));
279        assert!(output.contains("42L"));
280        assert!(!output.contains("F1"));
281        assert!(!output.contains("500t"));
282        assert!(!output.contains("read #3"));
283        std::env::remove_var("LEAN_CTX_SAVINGS_FOOTER");
284    }
285
286    #[test]
287    fn stats_returns_defaults_on_empty() {
288        let s = CliCacheStore::default();
289        assert_eq!(s.total_hits, 0);
290        assert_eq!(s.total_reads, 0);
291        assert!(s.entries.is_empty());
292    }
293
294    #[test]
295    fn cache_result_integration() {
296        let _lock = crate::core::data_dir::test_env_lock();
297
298        let nanos = std::time::SystemTime::now()
299            .duration_since(std::time::UNIX_EPOCH)
300            .unwrap()
301            .as_nanos();
302        let test_data_dir = std::env::temp_dir().join(format!("lean_ctx_cache_iso_{nanos}"));
303        std::fs::create_dir_all(&test_data_dir).unwrap();
304        std::env::set_var("LEAN_CTX_DATA_DIR", &test_data_dir);
305
306        let tmp = test_data_dir.join("test_file.txt");
307        std::fs::write(&tmp, "fn main() {}\n").unwrap();
308        let path_str = tmp.to_str().unwrap();
309
310        invalidate(path_str);
311
312        let result = check_and_read(path_str);
313        assert!(matches!(result, CacheResult::Miss { .. }));
314
315        let result2 = check_and_read(path_str);
316        assert!(matches!(result2, CacheResult::Hit { .. }));
317        if let CacheResult::Hit { entry, .. } = result2 {
318            assert_eq!(entry.line_count, 1);
319            assert!(entry.read_count >= 2);
320        }
321
322        invalidate(path_str);
323        let result3 = check_and_read(path_str);
324        assert!(matches!(result3, CacheResult::Miss { .. }));
325
326        std::env::remove_var("LEAN_CTX_DATA_DIR");
327        let _ = std::fs::remove_dir_all(&test_data_dir);
328    }
329}