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::tools::ctx_read::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 format!(
192 "{file_ref} cached {short_path} [{}L {}t] (read #{})",
193 entry.line_count, entry.original_tokens, entry.read_count
194 )
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[test]
202 fn compute_md5_deterministic() {
203 let h1 = compute_md5("test content");
204 let h2 = compute_md5("test content");
205 assert_eq!(h1, h2);
206 assert_ne!(h1, compute_md5("different"));
207 }
208
209 #[test]
210 fn evict_stale_removes_old_entries() {
211 let mut store = CliCacheStore::default();
212 store.entries.insert(
213 "/old.rs".to_string(),
214 CliCacheEntry {
215 path: "/old.rs".to_string(),
216 hash: "h1".into(),
217 line_count: 10,
218 original_tokens: 50,
219 timestamp: 1000,
220 read_count: 1,
221 },
222 );
223 store.entries.insert(
224 "/new.rs".to_string(),
225 CliCacheEntry {
226 path: "/new.rs".to_string(),
227 hash: "h2".into(),
228 line_count: 20,
229 original_tokens: 100,
230 timestamp: now_secs(),
231 read_count: 1,
232 },
233 );
234
235 evict_stale(&mut store, now_secs());
236 assert!(!store.entries.contains_key("/old.rs"));
237 assert!(store.entries.contains_key("/new.rs"));
238 }
239
240 #[test]
241 fn evict_respects_max_entries() {
242 let mut store = CliCacheStore::default();
243 let now = now_secs();
244 for i in 0..MAX_ENTRIES + 10 {
245 store.entries.insert(
246 format!("/file_{i}.rs"),
247 CliCacheEntry {
248 path: format!("/file_{i}.rs"),
249 hash: format!("h{i}"),
250 line_count: 1,
251 original_tokens: 10,
252 timestamp: now - i as u64,
253 read_count: 1,
254 },
255 );
256 }
257 evict_stale(&mut store, now);
258 assert!(store.entries.len() <= MAX_ENTRIES);
259 }
260
261 #[test]
262 fn format_hit_output() {
263 let entry = CliCacheEntry {
264 path: "/test.rs".into(),
265 hash: "abc".into(),
266 line_count: 42,
267 original_tokens: 500,
268 timestamp: now_secs(),
269 read_count: 3,
270 };
271 let output = format_hit(&entry, "F1", "test.rs");
272 assert!(output.contains("F1 cached"));
273 assert!(output.contains("42L"));
274 assert!(output.contains("500t"));
275 assert!(output.contains("read #3"));
276 }
277
278 #[test]
279 fn stats_returns_defaults_on_empty() {
280 let s = CliCacheStore::default();
281 assert_eq!(s.total_hits, 0);
282 assert_eq!(s.total_reads, 0);
283 assert!(s.entries.is_empty());
284 }
285
286 #[test]
287 fn cache_result_integration() {
288 let _lock = crate::core::data_dir::test_env_lock();
289
290 let nanos = std::time::SystemTime::now()
291 .duration_since(std::time::UNIX_EPOCH)
292 .unwrap()
293 .as_nanos();
294 let test_data_dir = std::env::temp_dir().join(format!("lean_ctx_cache_iso_{nanos}"));
295 std::fs::create_dir_all(&test_data_dir).unwrap();
296 std::env::set_var("LEAN_CTX_DATA_DIR", &test_data_dir);
297
298 let tmp = test_data_dir.join("test_file.txt");
299 std::fs::write(&tmp, "fn main() {}\n").unwrap();
300 let path_str = tmp.to_str().unwrap();
301
302 invalidate(path_str);
303
304 let result = check_and_read(path_str);
305 assert!(matches!(result, CacheResult::Miss { .. }));
306
307 let result2 = check_and_read(path_str);
308 assert!(matches!(result2, CacheResult::Hit { .. }));
309 if let CacheResult::Hit { entry, .. } = result2 {
310 assert_eq!(entry.line_count, 1);
311 assert!(entry.read_count >= 2);
312 }
313
314 invalidate(path_str);
315 let result3 = check_and_read(path_str);
316 assert!(matches!(result3, CacheResult::Miss { .. }));
317
318 std::env::remove_var("LEAN_CTX_DATA_DIR");
319 let _ = std::fs::remove_dir_all(&test_data_dir);
320 }
321}