Skip to main content

lean_ctx/core/
cache.rs

1use md5::{Digest, Md5};
2use std::collections::HashMap;
3use std::time::{Instant, SystemTime};
4
5use super::tokens::count_tokens;
6
7fn normalize_key(path: &str) -> String {
8    crate::core::pathutil::normalize_tool_path(path)
9}
10
11fn max_cache_tokens() -> usize {
12    std::env::var("LEAN_CTX_CACHE_MAX_TOKENS")
13        .ok()
14        .and_then(|v| v.parse().ok())
15        .unwrap_or(500_000)
16}
17
18/// A cached file read: zstd-compressed content, hash, token count, and access metadata.
19#[derive(Clone, Debug)]
20pub struct CacheEntry {
21    compressed_content: Vec<u8>,
22    pub hash: String,
23    pub line_count: usize,
24    pub original_tokens: usize,
25    pub read_count: u32,
26    pub path: String,
27    pub last_access: Instant,
28    pub stored_mtime: Option<SystemTime>,
29    /// Mode-specific compressed outputs (e.g. "map", "signatures") cached to avoid re-parsing.
30    pub compressed_outputs: HashMap<String, String>,
31    /// Whether full (uncompressed) content was already delivered for this hash.
32    /// Prevents cache-stub loops when upgrading from compressed to full mode.
33    pub full_content_delivered: bool,
34    /// Last read mode used for this file (for auto-escalation on edit failure).
35    pub last_mode: String,
36}
37
38const ZSTD_LEVEL: i32 = 3;
39
40fn zstd_compress(data: &str) -> Vec<u8> {
41    zstd::encode_all(data.as_bytes(), ZSTD_LEVEL).unwrap_or_else(|_| data.as_bytes().to_vec())
42}
43
44fn zstd_decompress(data: &[u8]) -> Option<String> {
45    zstd::decode_all(data)
46        .ok()
47        .and_then(|v| String::from_utf8(v).ok())
48}
49
50impl CacheEntry {
51    /// Creates a new entry with zstd-compressed content.
52    pub fn new(
53        content: &str,
54        hash: String,
55        line_count: usize,
56        original_tokens: usize,
57        path: String,
58        stored_mtime: Option<SystemTime>,
59    ) -> Self {
60        let compressed_content = zstd_compress(content);
61        Self {
62            compressed_content,
63            hash,
64            line_count,
65            original_tokens,
66            read_count: 1,
67            path,
68            last_access: Instant::now(),
69            stored_mtime,
70            compressed_outputs: HashMap::new(),
71            full_content_delivered: false,
72            last_mode: String::new(),
73        }
74    }
75
76    /// Decompresses and returns the full file content.
77    pub fn content(&self) -> Option<String> {
78        zstd_decompress(&self.compressed_content)
79    }
80
81    /// Replaces the stored content with new zstd-compressed data.
82    pub fn set_content(&mut self, content: &str) {
83        self.compressed_content = zstd_compress(content);
84    }
85
86    /// Approximate RAM usage of the compressed content in bytes.
87    pub fn compressed_size(&self) -> usize {
88        self.compressed_content.len()
89    }
90}
91
92/// Result of a cache store operation, indicating whether it was a hit or new entry.
93#[derive(Debug, Clone)]
94pub struct StoreResult {
95    pub line_count: usize,
96    pub original_tokens: usize,
97    pub read_count: u32,
98    pub was_hit: bool,
99    /// Whether full content was previously delivered for this cache entry.
100    pub full_content_delivered: bool,
101}
102
103impl CacheEntry {
104    /// Computes a legacy eviction score blending recency, frequency, and size.
105    pub fn eviction_score_legacy(&self, now: Instant) -> f64 {
106        let elapsed = now
107            .checked_duration_since(self.last_access)
108            .unwrap_or_default()
109            .as_secs_f64();
110        let recency = 1.0 / (1.0 + elapsed.sqrt());
111        let frequency = (self.read_count as f64 + 1.0).ln();
112        let size_value = (self.original_tokens as f64 + 1.0).ln();
113        recency * 0.4 + frequency * 0.3 + size_value * 0.3
114    }
115
116    pub fn get_compressed(&self, mode_key: &str) -> Option<&String> {
117        self.compressed_outputs.get(mode_key)
118    }
119
120    pub fn set_compressed(&mut self, mode_key: &str, output: String) {
121        const MAX_COMPRESSED_VARIANTS: usize = 3;
122        if self.compressed_outputs.len() >= MAX_COMPRESSED_VARIANTS
123            && !self.compressed_outputs.contains_key(mode_key)
124        {
125            if let Some(oldest_key) = self.compressed_outputs.keys().next().cloned() {
126                self.compressed_outputs.remove(&oldest_key);
127            }
128        }
129        self.compressed_outputs.insert(mode_key.to_string(), output);
130    }
131
132    pub fn mark_full_delivered(&mut self) {
133        self.full_content_delivered = true;
134    }
135}
136
137const RRF_K: f64 = 60.0;
138
139/// Compute Reciprocal Rank Fusion eviction scores for a batch of cache entries.
140/// Each signal (recency, frequency, size) produces an independent ranking.
141/// The final score is the sum of `1/(k + rank)` across all signals.
142/// Higher score = more valuable = keep longer.
143pub fn eviction_scores_rrf(entries: &[(&String, &CacheEntry)], now: Instant) -> Vec<(String, f64)> {
144    if entries.is_empty() {
145        return Vec::new();
146    }
147
148    let n = entries.len();
149
150    let mut recency_order: Vec<usize> = (0..n).collect();
151    recency_order.sort_by(|&a, &b| {
152        let elapsed_a = now
153            .checked_duration_since(entries[a].1.last_access)
154            .unwrap_or_default()
155            .as_secs_f64();
156        let elapsed_b = now
157            .checked_duration_since(entries[b].1.last_access)
158            .unwrap_or_default()
159            .as_secs_f64();
160        elapsed_a
161            .partial_cmp(&elapsed_b)
162            .unwrap_or(std::cmp::Ordering::Equal)
163    });
164
165    let mut frequency_order: Vec<usize> = (0..n).collect();
166    frequency_order.sort_by(|&a, &b| entries[b].1.read_count.cmp(&entries[a].1.read_count));
167
168    let mut size_order: Vec<usize> = (0..n).collect();
169    size_order.sort_by(|&a, &b| {
170        entries[b]
171            .1
172            .original_tokens
173            .cmp(&entries[a].1.original_tokens)
174    });
175
176    let mut recency_ranks = vec![0usize; n];
177    let mut frequency_ranks = vec![0usize; n];
178    let mut size_ranks = vec![0usize; n];
179
180    for (rank, &idx) in recency_order.iter().enumerate() {
181        recency_ranks[idx] = rank;
182    }
183    for (rank, &idx) in frequency_order.iter().enumerate() {
184        frequency_ranks[idx] = rank;
185    }
186    for (rank, &idx) in size_order.iter().enumerate() {
187        size_ranks[idx] = rank;
188    }
189
190    entries
191        .iter()
192        .enumerate()
193        .map(|(i, (path, _))| {
194            let score = 1.0 / (RRF_K + recency_ranks[i] as f64)
195                + 1.0 / (RRF_K + frequency_ranks[i] as f64)
196                + 1.0 / (RRF_K + size_ranks[i] as f64);
197            ((*path).clone(), score)
198        })
199        .collect()
200}
201
202/// Aggregated cache statistics: hits, reads, and token savings.
203#[derive(Debug)]
204pub struct CacheStats {
205    pub total_reads: u64,
206    pub cache_hits: u64,
207    pub total_original_tokens: u64,
208    pub total_sent_tokens: u64,
209    pub files_tracked: usize,
210}
211
212impl CacheStats {
213    /// Returns the cache hit rate as a percentage (0–100).
214    pub fn hit_rate(&self) -> f64 {
215        if self.total_reads == 0 {
216            return 0.0;
217        }
218        (self.cache_hits as f64 / self.total_reads as f64) * 100.0
219    }
220
221    /// Returns the total number of tokens saved by cache hits.
222    pub fn tokens_saved(&self) -> u64 {
223        self.total_original_tokens
224            .saturating_sub(self.total_sent_tokens)
225    }
226
227    /// Returns the savings as a percentage of total original tokens.
228    pub fn savings_percent(&self) -> f64 {
229        if self.total_original_tokens == 0 {
230            return 0.0;
231        }
232        (self.tokens_saved() as f64 / self.total_original_tokens as f64) * 100.0
233    }
234}
235
236/// A block shared across multiple files, identified by its canonical source.
237#[derive(Clone, Debug)]
238pub struct SharedBlock {
239    pub canonical_path: String,
240    pub canonical_ref: String,
241    pub start_line: usize,
242    pub end_line: usize,
243    pub content: String,
244}
245
246/// In-memory file cache with segmented LRU eviction (probationary vs protected),
247/// file references, and cross-file dedup.
248pub struct SessionCache {
249    entries: HashMap<String, CacheEntry>,
250    file_refs: HashMap<String, String>,
251    next_ref: usize,
252    stats: CacheStats,
253    shared_blocks: Vec<SharedBlock>,
254}
255
256impl Default for SessionCache {
257    fn default() -> Self {
258        Self::new()
259    }
260}
261
262impl SessionCache {
263    /// Creates an empty session cache with default stats.
264    pub fn new() -> Self {
265        Self {
266            entries: HashMap::new(),
267            file_refs: HashMap::new(),
268            next_ref: 1,
269            shared_blocks: Vec::new(),
270            stats: CacheStats {
271                total_reads: 0,
272                cache_hits: 0,
273                total_original_tokens: 0,
274                total_sent_tokens: 0,
275                files_tracked: 0,
276            },
277        }
278    }
279
280    /// Returns or assigns a short file reference label (F1, F2, ...) for the given path.
281    pub fn get_file_ref(&mut self, path: &str) -> String {
282        let key = normalize_key(path);
283        if let Some(r) = self.file_refs.get(&key) {
284            return r.clone();
285        }
286        let r = format!("F{}", self.next_ref);
287        self.next_ref += 1;
288        self.file_refs.insert(key, r.clone());
289        r
290    }
291
292    /// Returns the file reference label for a path without assigning a new one.
293    pub fn get_file_ref_readonly(&self, path: &str) -> Option<String> {
294        self.file_refs.get(&normalize_key(path)).cloned()
295    }
296
297    /// Looks up a cached entry by file path.
298    pub fn get(&self, path: &str) -> Option<&CacheEntry> {
299        self.entries.get(&normalize_key(path))
300    }
301
302    /// Mutable lookup of a cached entry by file path.
303    pub fn get_mut(&mut self, path: &str) -> Option<&mut CacheEntry> {
304        self.entries.get_mut(&normalize_key(path))
305    }
306
307    /// Retrieves the full (uncompressed) content for a file path, if cached.
308    /// Used by the CCR (Compress-Cache-Retrieve) mechanism.
309    pub fn get_full_content(&self, path: &str) -> Option<String> {
310        self.entries
311            .get(&normalize_key(path))
312            .and_then(CacheEntry::content)
313    }
314
315    /// Records a cache hit, updates access stats, and emits a cache-hit event.
316    pub fn record_cache_hit(&mut self, path: &str) -> Option<&CacheEntry> {
317        let key = normalize_key(path);
318        let ref_label = self
319            .file_refs
320            .get(&key)
321            .cloned()
322            .unwrap_or_else(|| "F?".to_string());
323        if let Some(entry) = self.entries.get_mut(&key) {
324            entry.read_count += 1;
325            entry.last_access = Instant::now();
326            self.stats.total_reads += 1;
327            self.stats.cache_hits += 1;
328            self.stats.total_original_tokens += entry.original_tokens as u64;
329            let hit_msg = format!(
330                "{ref_label} cached {}t {}L",
331                entry.read_count, entry.line_count
332            );
333            self.stats.total_sent_tokens += count_tokens(&hit_msg) as u64;
334            crate::core::events::emit_cache_hit(path, entry.original_tokens as u64);
335            Some(entry)
336        } else {
337            None
338        }
339    }
340
341    /// Stores file content in the cache; returns a hit if content hash matches.
342    pub fn store(&mut self, path: &str, content: &str) -> StoreResult {
343        let key = normalize_key(path);
344        let hash = compute_md5(content);
345        let line_count = content.lines().count();
346        let original_tokens = count_tokens(content);
347        let stored_mtime = std::fs::metadata(path).and_then(|m| m.modified()).ok();
348        let now = Instant::now();
349
350        self.stats.total_reads += 1;
351        self.stats.total_original_tokens += original_tokens as u64;
352
353        if let Some(existing) = self.entries.get_mut(&key) {
354            existing.last_access = now;
355            if stored_mtime.is_some() {
356                existing.stored_mtime = stored_mtime;
357            }
358            if existing.hash == hash {
359                existing.read_count += 1;
360                self.stats.cache_hits += 1;
361                let hit_msg = format!(
362                    "{} cached {}t {}L",
363                    self.file_refs.get(&key).unwrap_or(&"F?".to_string()),
364                    existing.read_count,
365                    existing.line_count,
366                );
367                self.stats.total_sent_tokens += count_tokens(&hit_msg) as u64;
368                return StoreResult {
369                    line_count: existing.line_count,
370                    original_tokens: existing.original_tokens,
371                    read_count: existing.read_count,
372                    was_hit: true,
373                    full_content_delivered: existing.full_content_delivered,
374                };
375            }
376            existing.compressed_outputs.clear();
377            existing.set_content(content);
378            existing.hash = hash;
379            existing.line_count = line_count;
380            existing.original_tokens = original_tokens;
381            existing.read_count += 1;
382            existing.full_content_delivered = false;
383            if stored_mtime.is_some() {
384                existing.stored_mtime = stored_mtime;
385            }
386            self.stats.total_sent_tokens += original_tokens as u64;
387            return StoreResult {
388                line_count,
389                original_tokens,
390                read_count: existing.read_count,
391                was_hit: false,
392                full_content_delivered: false,
393            };
394        }
395
396        self.evict_if_needed(original_tokens);
397        self.get_file_ref(&key);
398
399        let entry = CacheEntry::new(
400            content,
401            hash,
402            line_count,
403            original_tokens,
404            key.clone(),
405            stored_mtime,
406        );
407
408        self.entries.insert(key, entry);
409        self.stats.files_tracked += 1;
410        self.stats.total_sent_tokens += original_tokens as u64;
411        StoreResult {
412            line_count,
413            original_tokens,
414            read_count: 1,
415            was_hit: false,
416            full_content_delivered: false,
417        }
418    }
419
420    /// Returns the sum of original token counts across all cached entries.
421    pub fn total_cached_tokens(&self) -> usize {
422        self.entries.values().map(|e| e.original_tokens).sum()
423    }
424
425    /// Evict until cache fits within token budget using segmented LRU:
426    /// probationary (`read_count` ≤ 1, newly inserted / single-touch) first by oldest `last_access`,
427    /// then protected entries (`read_count` > 1) by oldest access.
428    pub fn evict_if_needed(&mut self, incoming_tokens: usize) {
429        let max_tokens = max_cache_tokens();
430        let current = self.total_cached_tokens();
431        if current + incoming_tokens <= max_tokens {
432            return;
433        }
434
435        let mut freed = 0usize;
436        let target = (current + incoming_tokens).saturating_sub(max_tokens);
437
438        let mut probationary: Vec<(String, Instant)> = self
439            .entries
440            .iter()
441            .filter(|(_, e)| e.read_count <= 1)
442            .map(|(p, e)| (p.clone(), e.last_access))
443            .collect();
444        probationary.sort_by_key(|(_, t)| *t);
445
446        let mut protected: Vec<(String, Instant)> = self
447            .entries
448            .iter()
449            .filter(|(_, e)| e.read_count > 1)
450            .map(|(p, e)| (p.clone(), e.last_access))
451            .collect();
452        protected.sort_by_key(|(_, t)| *t);
453
454        for (path, _) in probationary.into_iter().chain(protected) {
455            if freed >= target {
456                break;
457            }
458            if let Some(entry) = self.entries.remove(&path) {
459                freed += entry.original_tokens;
460                self.file_refs.remove(&path);
461            }
462        }
463    }
464
465    /// Returns all cached entries as (path, entry) pairs.
466    pub fn get_all_entries(&self) -> Vec<(&String, &CacheEntry)> {
467        self.entries.iter().collect()
468    }
469
470    /// Returns a reference to the aggregated cache statistics.
471    pub fn get_stats(&self) -> &CacheStats {
472        &self.stats
473    }
474
475    /// Returns the path-to-file-ref mapping (e.g. "/src/main.rs" → "F1").
476    pub fn file_ref_map(&self) -> &HashMap<String, String> {
477        &self.file_refs
478    }
479
480    /// Replaces the cross-file shared blocks used for deduplication.
481    pub fn set_shared_blocks(&mut self, blocks: Vec<SharedBlock>) {
482        self.shared_blocks = blocks;
483    }
484
485    /// Returns the current set of cross-file shared blocks.
486    pub fn get_shared_blocks(&self) -> &[SharedBlock] {
487        &self.shared_blocks
488    }
489
490    /// Replace shared blocks in content with cross-file references.
491    pub fn apply_dedup(&self, path: &str, content: &str) -> Option<String> {
492        if self.shared_blocks.is_empty() {
493            return None;
494        }
495        let refs: Vec<&SharedBlock> = self
496            .shared_blocks
497            .iter()
498            .filter(|b| b.canonical_path != path && content.contains(&b.content))
499            .collect();
500        if refs.is_empty() {
501            return None;
502        }
503        let mut result = content.to_string();
504        for block in refs {
505            result = result.replacen(
506                &block.content,
507                &format!(
508                    "[= {}:{}-{}]",
509                    block.canonical_ref, block.start_line, block.end_line
510                ),
511                1,
512            );
513        }
514        Some(result)
515    }
516
517    /// Removes a file from the cache, forcing a fresh read on next access.
518    pub fn invalidate(&mut self, path: &str) -> bool {
519        self.entries.remove(&normalize_key(path)).is_some()
520    }
521
522    /// Returns a cached compressed output for a given file and mode key.
523    pub fn get_compressed(&self, path: &str, mode_key: &str) -> Option<&String> {
524        self.entries
525            .get(&normalize_key(path))?
526            .get_compressed(mode_key)
527    }
528
529    /// Marks that full (uncompressed) content was delivered for this file.
530    pub fn mark_full_delivered(&mut self, path: &str) {
531        if let Some(entry) = self.entries.get_mut(&normalize_key(path)) {
532            entry.mark_full_delivered();
533        }
534    }
535
536    /// Stores a compressed output for a given file and mode key.
537    pub fn set_compressed(&mut self, path: &str, mode_key: &str, output: String) {
538        if let Some(entry) = self.entries.get_mut(&normalize_key(path)) {
539            entry.set_compressed(mode_key, output);
540        }
541    }
542
543    /// Clears all cached entries, file refs, and resets stats. Returns the number of entries removed.
544    pub fn clear(&mut self) -> usize {
545        let count = self.entries.len();
546        self.entries.clear();
547        self.file_refs.clear();
548        self.shared_blocks.clear();
549        self.next_ref = 1;
550        self.stats = CacheStats {
551            total_reads: 0,
552            cache_hits: 0,
553            total_original_tokens: 0,
554            total_sent_tokens: 0,
555            files_tracked: 0,
556        };
557        count
558    }
559}
560
561pub fn file_mtime(path: &str) -> Option<SystemTime> {
562    std::fs::metadata(path).and_then(|m| m.modified()).ok()
563}
564
565pub fn is_cache_entry_stale(path: &str, cached_mtime: Option<SystemTime>) -> bool {
566    let current = file_mtime(path);
567    match (cached_mtime, current) {
568        (_, None) => false,
569        (None, Some(_)) => true,
570        (Some(cached), Some(current)) => current > cached,
571    }
572}
573
574fn compute_md5(content: &str) -> String {
575    let mut hasher = Md5::new();
576    hasher.update(content.as_bytes());
577    format!("{:x}", hasher.finalize())
578}
579
580#[cfg(test)]
581mod tests {
582    use super::*;
583    use std::time::Duration;
584
585    #[test]
586    fn cache_stores_and_retrieves() {
587        let mut cache = SessionCache::new();
588        let result = cache.store("/test/file.rs", "fn main() {}");
589        assert!(!result.was_hit);
590        assert_eq!(result.line_count, 1);
591        assert!(cache.get("/test/file.rs").is_some());
592    }
593
594    #[test]
595    fn cache_hit_on_same_content() {
596        let mut cache = SessionCache::new();
597        cache.store("/test/file.rs", "content");
598        let result = cache.store("/test/file.rs", "content");
599        assert!(result.was_hit, "same content should be a cache hit");
600    }
601
602    #[test]
603    fn cache_miss_on_changed_content() {
604        let mut cache = SessionCache::new();
605        cache.store("/test/file.rs", "old content");
606        let result = cache.store("/test/file.rs", "new content");
607        assert!(!result.was_hit, "changed content should not be a cache hit");
608    }
609
610    #[test]
611    fn file_refs_are_sequential() {
612        let mut cache = SessionCache::new();
613        assert_eq!(cache.get_file_ref("/a.rs"), "F1");
614        assert_eq!(cache.get_file_ref("/b.rs"), "F2");
615        assert_eq!(cache.get_file_ref("/a.rs"), "F1"); // stable
616    }
617
618    #[test]
619    fn cache_clear_resets_everything() {
620        let mut cache = SessionCache::new();
621        cache.store("/a.rs", "a");
622        cache.store("/b.rs", "b");
623        let count = cache.clear();
624        assert_eq!(count, 2);
625        assert!(cache.get("/a.rs").is_none());
626        assert_eq!(cache.get_file_ref("/c.rs"), "F1"); // refs reset
627    }
628
629    #[test]
630    fn cache_invalidate_removes_entry() {
631        let mut cache = SessionCache::new();
632        cache.store("/test.rs", "test");
633        assert!(cache.invalidate("/test.rs"));
634        assert!(!cache.invalidate("/nonexistent.rs"));
635    }
636
637    #[test]
638    fn cache_stats_track_correctly() {
639        let mut cache = SessionCache::new();
640        cache.store("/a.rs", "hello");
641        cache.store("/a.rs", "hello"); // hit
642        let stats = cache.get_stats();
643        assert_eq!(stats.total_reads, 2);
644        assert_eq!(stats.cache_hits, 1);
645        assert!(stats.hit_rate() > 0.0);
646    }
647
648    #[test]
649    fn md5_is_deterministic() {
650        let h1 = compute_md5("test content");
651        let h2 = compute_md5("test content");
652        assert_eq!(h1, h2);
653        assert_ne!(h1, compute_md5("different"));
654    }
655
656    #[test]
657    fn rrf_eviction_prefers_recent() {
658        let base = Instant::now();
659        std::thread::sleep(std::time::Duration::from_millis(5));
660        let now = Instant::now();
661        let key_a = "a.rs".to_string();
662        let key_b = "b.rs".to_string();
663        let recent = CacheEntry::new("a", "h1".to_string(), 1, 10, "/a.rs".to_string(), None);
664        let old = {
665            let mut e = CacheEntry::new("b", "h2".to_string(), 1, 10, "/b.rs".to_string(), None);
666            e.last_access = base;
667            e
668        };
669        let entries: Vec<(&String, &CacheEntry)> = vec![(&key_a, &recent), (&key_b, &old)];
670        let scores = eviction_scores_rrf(&entries, now);
671        let score_a = scores.iter().find(|(p, _)| p == "a.rs").unwrap().1;
672        let score_b = scores.iter().find(|(p, _)| p == "b.rs").unwrap().1;
673        assert!(
674            score_a > score_b,
675            "recently accessed entries should score higher via RRF"
676        );
677    }
678
679    #[test]
680    fn rrf_eviction_prefers_frequent() {
681        let now = Instant::now();
682        let key_a = "a.rs".to_string();
683        let key_b = "b.rs".to_string();
684        let frequent = {
685            let mut e = CacheEntry::new("a", "h1".to_string(), 1, 10, "/a.rs".to_string(), None);
686            e.read_count = 20;
687            e
688        };
689        let rare = CacheEntry::new("b", "h2".to_string(), 1, 10, "/b.rs".to_string(), None);
690        let entries: Vec<(&String, &CacheEntry)> = vec![(&key_a, &frequent), (&key_b, &rare)];
691        let scores = eviction_scores_rrf(&entries, now);
692        let score_a = scores.iter().find(|(p, _)| p == "a.rs").unwrap().1;
693        let score_b = scores.iter().find(|(p, _)| p == "b.rs").unwrap().1;
694        assert!(
695            score_a > score_b,
696            "frequently accessed entries should score higher via RRF"
697        );
698    }
699
700    #[test]
701    fn evict_if_needed_removes_lowest_score() {
702        std::env::set_var("LEAN_CTX_CACHE_MAX_TOKENS", "50");
703        let mut cache = SessionCache::new();
704        let big_content = "a]".repeat(30); // ~30 tokens
705        cache.store("/old.rs", &big_content);
706        // /old.rs now in cache with ~30 tokens
707
708        let new_content = "b ".repeat(30); // ~30 tokens incoming
709        cache.store("/new.rs", &new_content);
710        // should have evicted /old.rs to make room
711        // (total would be ~60 which exceeds 50)
712
713        // At least one should remain, total should be <= 50
714        assert!(
715            cache.total_cached_tokens() <= 60,
716            "eviction should have kicked in"
717        );
718        std::env::remove_var("LEAN_CTX_CACHE_MAX_TOKENS");
719    }
720
721    #[test]
722    fn stale_detection_flags_newer_file() {
723        let dir = tempfile::tempdir().unwrap();
724        let path = dir.path().join("stale.txt");
725        let p = path.to_string_lossy().to_string();
726
727        std::fs::write(&path, "one").unwrap();
728        let mut cache = SessionCache::new();
729        cache.store(&p, "one");
730
731        let entry = cache.get(&p).unwrap();
732        assert!(!is_cache_entry_stale(&p, entry.stored_mtime));
733
734        // Ensure mtime granularity differences don't make this flaky.
735        std::thread::sleep(Duration::from_secs(1));
736        std::fs::write(&path, "two").unwrap();
737
738        let entry = cache.get(&p).unwrap();
739        assert!(is_cache_entry_stale(&p, entry.stored_mtime));
740    }
741
742    #[test]
743    fn compressed_outputs_cached_and_retrieved() {
744        let mut cache = SessionCache::new();
745        cache.store("/test.rs", "fn main() {}");
746        cache.set_compressed("/test.rs", "map", "compressed map output".to_string());
747        assert_eq!(
748            cache.get_compressed("/test.rs", "map"),
749            Some(&"compressed map output".to_string())
750        );
751        assert_eq!(cache.get_compressed("/test.rs", "signatures"), None);
752    }
753
754    #[test]
755    fn compressed_outputs_cleared_on_content_change() {
756        let mut cache = SessionCache::new();
757        cache.store("/test.rs", "old content");
758        cache.set_compressed("/test.rs", "map", "old map".to_string());
759        assert!(cache.get_compressed("/test.rs", "map").is_some());
760
761        cache.store("/test.rs", "new content");
762        assert_eq!(cache.get_compressed("/test.rs", "map"), None);
763    }
764
765    #[test]
766    fn compressed_outputs_survive_same_content_store() {
767        let mut cache = SessionCache::new();
768        cache.store("/test.rs", "content");
769        cache.set_compressed("/test.rs", "map", "cached map".to_string());
770
771        let result = cache.store("/test.rs", "content");
772        assert!(result.was_hit);
773        assert_eq!(
774            cache.get_compressed("/test.rs", "map"),
775            Some(&"cached map".to_string())
776        );
777    }
778
779    #[test]
780    fn compressed_outputs_cleared_on_invalidate() {
781        let mut cache = SessionCache::new();
782        cache.store("/test.rs", "content");
783        cache.set_compressed("/test.rs", "signatures", "cached sigs".to_string());
784        cache.invalidate("/test.rs");
785        assert_eq!(cache.get_compressed("/test.rs", "signatures"), None);
786    }
787
788    #[test]
789    fn compressed_outputs_cleared_on_clear() {
790        let mut cache = SessionCache::new();
791        cache.store("/a.rs", "a");
792        cache.set_compressed("/a.rs", "map", "map_a".to_string());
793        cache.clear();
794        assert_eq!(cache.get_compressed("/a.rs", "map"), None);
795    }
796}