Skip to main content

lean_ctx/core/
cache.rs

1use md5::{Digest, Md5};
2use std::collections::HashMap;
3use std::sync::OnceLock;
4use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
5use std::time::{Duration, Instant, SystemTime};
6
7use super::tokens::count_tokens;
8
9/// Process-global monotonic base for encoding `Instant`s into an `AtomicU64`.
10/// Stored as milliseconds since this base, which is sufficient resolution for
11/// LRU/RRF eviction recency while allowing lock-free access on cache hits.
12fn instant_base() -> Instant {
13    static BASE: OnceLock<Instant> = OnceLock::new();
14    *BASE.get_or_init(Instant::now)
15}
16
17fn encode_instant(i: Instant) -> u64 {
18    i.saturating_duration_since(instant_base()).as_millis() as u64
19}
20
21fn decode_instant(ms: u64) -> Instant {
22    instant_base() + Duration::from_millis(ms)
23}
24
25fn normalize_key(path: &str) -> String {
26    crate::core::pathutil::normalize_tool_path(path)
27}
28
29/// Built-in default token budget for the in-memory read cache.
30/// 2M covers ~100 typical source files, reducing premature eviction
31/// before re-reads occur. RAM-pressure eviction via EvictionOrchestrator
32/// provides an independent safety net regardless of this budget.
33pub(crate) const DEFAULT_CACHE_MAX_TOKENS: usize = 2_000_000;
34
35/// Pure resolver for the read-cache token budget. `env` (the raw
36/// `LEAN_CTX_CACHE_MAX_TOKENS` value) wins when it parses to a positive integer,
37/// then the `configured` `[core] cache_max_tokens`, else
38/// [`DEFAULT_CACHE_MAX_TOKENS`]. A `0` (or unparseable env) in either source
39/// means "use the default". Split out so the precedence is unit-testable without
40/// touching the global env or config.
41fn resolve_cache_max_tokens(env: Option<&str>, configured: usize) -> usize {
42    if let Some(raw) = env
43        && let Ok(n) = raw.trim().parse::<usize>()
44        && n > 0
45    {
46        return n;
47    }
48    if configured > 0 {
49        configured
50    } else {
51        DEFAULT_CACHE_MAX_TOKENS
52    }
53}
54
55/// Resolved token budget for the read cache. `LEAN_CTX_CACHE_MAX_TOKENS` wins
56/// (env-first keeps the hot eviction path cheap for power users), then
57/// `[core] cache_max_tokens` in config.toml, else [`DEFAULT_CACHE_MAX_TOKENS`].
58/// Shared with `eviction_orchestrator` so both eviction rails read one budget.
59pub(crate) fn max_cache_tokens() -> usize {
60    resolve_cache_max_tokens(
61        std::env::var("LEAN_CTX_CACHE_MAX_TOKENS").ok().as_deref(),
62        crate::core::config::Config::load().cache_max_tokens,
63    )
64}
65
66/// A cached file read: zstd-compressed content, hash, token count, and access metadata.
67///
68/// `read_count` and `last_access` use interior mutability (atomics) so cache
69/// hits can be recorded under a shared (read) lock — parallel reads of distinct
70/// files no longer serialize on a global write lock.
71#[derive(Debug)]
72pub struct CacheEntry {
73    compressed_content: Vec<u8>,
74    pub hash: String,
75    pub line_count: usize,
76    pub original_tokens: usize,
77    read_count: AtomicU32,
78    pub path: String,
79    last_access: AtomicU64,
80    pub stored_mtime: Option<SystemTime>,
81    /// Mode-specific compressed outputs (e.g. "map", "signatures") cached to avoid re-parsing.
82    pub compressed_outputs: HashMap<String, String>,
83    /// Whether full (uncompressed) content was already delivered for this hash.
84    /// Prevents cache-stub loops when upgrading from compressed to full mode.
85    pub full_content_delivered: bool,
86    /// Conversation id that received the full content (see
87    /// [`crate::core::conversation`]). The `[unchanged]` stub is only valid for
88    /// a re-read from this same conversation; `None` means delivered without a
89    /// known conversation context (legacy / hooks absent).
90    pub delivered_conversation: Option<String>,
91    /// Last read mode used for this file (for auto-escalation on edit failure).
92    pub last_mode: String,
93}
94
95const ZSTD_LEVEL: i32 = 3;
96
97fn zstd_compress(data: &str) -> Vec<u8> {
98    zstd::encode_all(data.as_bytes(), ZSTD_LEVEL).unwrap_or_else(|_| data.as_bytes().to_vec())
99}
100
101fn zstd_decompress(data: &[u8]) -> Option<String> {
102    zstd::decode_all(data)
103        .ok()
104        .and_then(|v| String::from_utf8(v).ok())
105}
106
107impl CacheEntry {
108    /// Creates a new entry with zstd-compressed content.
109    pub fn new(
110        content: &str,
111        hash: String,
112        line_count: usize,
113        original_tokens: usize,
114        path: String,
115        stored_mtime: Option<SystemTime>,
116    ) -> Self {
117        let compressed_content = zstd_compress(content);
118        Self {
119            compressed_content,
120            hash,
121            line_count,
122            original_tokens,
123            read_count: AtomicU32::new(1),
124            path,
125            last_access: AtomicU64::new(encode_instant(Instant::now())),
126            stored_mtime,
127            compressed_outputs: HashMap::new(),
128            full_content_delivered: false,
129            delivered_conversation: None,
130            last_mode: String::new(),
131        }
132    }
133
134    /// Current read count (lock-free).
135    pub fn read_count(&self) -> u32 {
136        self.read_count.load(Ordering::Relaxed)
137    }
138
139    /// Atomically increments the read count and returns the new value (lock-free).
140    pub fn bump_read_count(&self) -> u32 {
141        self.read_count.fetch_add(1, Ordering::Relaxed) + 1
142    }
143
144    /// Overwrites the read count (used by `store` and tests).
145    pub fn set_read_count(&self, n: u32) {
146        self.read_count.store(n, Ordering::Relaxed);
147    }
148
149    /// Last access time, decoded from the atomic millisecond offset.
150    pub fn last_access(&self) -> Instant {
151        decode_instant(self.last_access.load(Ordering::Relaxed))
152    }
153
154    /// Marks the entry as accessed now (lock-free).
155    pub fn touch(&self) {
156        self.last_access
157            .store(encode_instant(Instant::now()), Ordering::Relaxed);
158    }
159
160    /// Overwrites the last-access time (used by tests and eviction setup).
161    pub fn set_last_access(&self, when: Instant) {
162        self.last_access
163            .store(encode_instant(when), Ordering::Relaxed);
164    }
165
166    /// Decompresses and returns the full file content.
167    pub fn content(&self) -> Option<String> {
168        zstd_decompress(&self.compressed_content)
169    }
170
171    /// Replaces the stored content with new zstd-compressed data.
172    pub fn set_content(&mut self, content: &str) {
173        self.compressed_content = zstd_compress(content);
174    }
175
176    /// Approximate RAM usage of the compressed content in bytes.
177    pub fn compressed_size(&self) -> usize {
178        self.compressed_content.len()
179    }
180}
181
182/// Result of a cache store operation, indicating whether it was a hit or new entry.
183#[derive(Debug, Clone)]
184pub struct StoreResult {
185    pub line_count: usize,
186    pub original_tokens: usize,
187    pub read_count: u32,
188    pub was_hit: bool,
189    /// Whether full content was previously delivered for this cache entry.
190    pub full_content_delivered: bool,
191}
192
193impl CacheEntry {
194    /// Computes a legacy eviction score blending recency, frequency, and size.
195    pub fn eviction_score_legacy(&self, now: Instant) -> f64 {
196        let elapsed = now
197            .checked_duration_since(self.last_access())
198            .unwrap_or_default()
199            .as_secs_f64();
200        let recency = 1.0 / (1.0 + elapsed.sqrt());
201        let frequency = (self.read_count() as f64 + 1.0).ln();
202        let size_value = (self.original_tokens as f64 + 1.0).ln();
203        recency * 0.4 + frequency * 0.3 + size_value * 0.3
204    }
205
206    pub fn get_compressed(&self, mode_key: &str) -> Option<&String> {
207        self.compressed_outputs.get(mode_key)
208    }
209
210    pub fn set_compressed(&mut self, mode_key: &str, output: String) {
211        const MAX_COMPRESSED_VARIANTS: usize = 3;
212        if self.compressed_outputs.len() >= MAX_COMPRESSED_VARIANTS
213            && !self.compressed_outputs.contains_key(mode_key)
214            && let Some(oldest_key) = self.compressed_outputs.keys().next().cloned()
215        {
216            self.compressed_outputs.remove(&oldest_key);
217        }
218        self.compressed_outputs.insert(mode_key.to_string(), output);
219    }
220
221    pub fn mark_full_delivered(&mut self, conversation: Option<String>) {
222        self.full_content_delivered = true;
223        self.delivered_conversation = conversation;
224    }
225}
226
227const RRF_K: f64 = 60.0;
228
229/// Hebbian protection added to an entry's RRF eviction score per unit of
230/// association strength with the currently-active working set (#3). Files that
231/// are read together resist eviction together ("fire together, wire together").
232/// Deterministic: a fixed multiplier, no sampling.
233const HEBBIAN_PROTECT_WEIGHT: f64 = 0.05;
234/// Size of the "active working set" (most-recently-accessed entries) against
235/// which Hebbian association is measured during eviction.
236const HEBBIAN_ACTIVE_SET: usize = 8;
237
238/// Compute Reciprocal Rank Fusion eviction scores for a batch of cache entries.
239/// Each signal (recency, frequency, size) produces an independent ranking.
240/// The final score is the sum of `1/(k + rank)` across all signals.
241/// Higher score = more valuable = keep longer.
242pub fn eviction_scores_rrf(entries: &[(&String, &CacheEntry)], now: Instant) -> Vec<(String, f64)> {
243    if entries.is_empty() {
244        return Vec::new();
245    }
246
247    let n = entries.len();
248
249    let mut recency_order: Vec<usize> = (0..n).collect();
250    recency_order.sort_by(|&a, &b| {
251        let elapsed_a = now
252            .checked_duration_since(entries[a].1.last_access())
253            .unwrap_or_default()
254            .as_secs_f64();
255        let elapsed_b = now
256            .checked_duration_since(entries[b].1.last_access())
257            .unwrap_or_default()
258            .as_secs_f64();
259        elapsed_a
260            .partial_cmp(&elapsed_b)
261            .unwrap_or(std::cmp::Ordering::Equal)
262    });
263
264    let mut frequency_order: Vec<usize> = (0..n).collect();
265    frequency_order.sort_by(|&a, &b| entries[b].1.read_count().cmp(&entries[a].1.read_count()));
266
267    let mut size_order: Vec<usize> = (0..n).collect();
268    size_order.sort_by(|&a, &b| {
269        entries[b]
270            .1
271            .original_tokens
272            .cmp(&entries[a].1.original_tokens)
273    });
274
275    let mut recency_ranks = vec![0usize; n];
276    let mut frequency_ranks = vec![0usize; n];
277    let mut size_ranks = vec![0usize; n];
278
279    for (rank, &idx) in recency_order.iter().enumerate() {
280        recency_ranks[idx] = rank;
281    }
282    for (rank, &idx) in frequency_order.iter().enumerate() {
283        frequency_ranks[idx] = rank;
284    }
285    for (rank, &idx) in size_order.iter().enumerate() {
286        size_ranks[idx] = rank;
287    }
288
289    entries
290        .iter()
291        .enumerate()
292        .map(|(i, (path, _))| {
293            let score = 1.0 / (RRF_K + recency_ranks[i] as f64)
294                + 1.0 / (RRF_K + frequency_ranks[i] as f64)
295                + 1.0 / (RRF_K + size_ranks[i] as f64);
296            ((*path).clone(), score)
297        })
298        .collect()
299}
300
301/// Add the Hebbian co-access bonus (#3) to RRF eviction scores in place. A
302/// higher score means "keep longer", so co-accessed entries are protected.
303fn apply_hebbian_bonus(scores: &mut [(String, f64)], bonus: &HashMap<String, f64>) {
304    if bonus.is_empty() {
305        return;
306    }
307    for s in scores.iter_mut() {
308        if let Some(b) = bonus.get(&s.0) {
309            s.1 += *b;
310        }
311    }
312}
313
314/// Aggregated cache statistics: hits, reads, and token savings.
315///
316/// Counters are atomic so they can be updated on the read-locked cache-hit
317/// fast path without taking a write lock.
318#[derive(Debug, Default)]
319pub struct CacheStats {
320    total_reads: AtomicU64,
321    cache_hits: AtomicU64,
322    total_original_tokens: AtomicU64,
323    total_sent_tokens: AtomicU64,
324    files_tracked: AtomicU64,
325}
326
327impl CacheStats {
328    /// Total number of read operations recorded.
329    pub fn total_reads(&self) -> u64 {
330        self.total_reads.load(Ordering::Relaxed)
331    }
332
333    /// Total number of cache hits recorded.
334    pub fn cache_hits(&self) -> u64 {
335        self.cache_hits.load(Ordering::Relaxed)
336    }
337
338    /// Sum of original (uncompressed) token counts across all reads.
339    pub fn total_original_tokens(&self) -> u64 {
340        self.total_original_tokens.load(Ordering::Relaxed)
341    }
342
343    /// Sum of tokens actually sent to the model.
344    pub fn total_sent_tokens(&self) -> u64 {
345        self.total_sent_tokens.load(Ordering::Relaxed)
346    }
347
348    /// Number of distinct files currently tracked.
349    pub fn files_tracked(&self) -> u64 {
350        self.files_tracked.load(Ordering::Relaxed)
351    }
352
353    /// Returns the cache hit rate as a percentage (0–100).
354    pub fn hit_rate(&self) -> f64 {
355        let total = self.total_reads();
356        if total == 0 {
357            return 0.0;
358        }
359        (self.cache_hits() as f64 / total as f64) * 100.0
360    }
361
362    /// Returns the total number of tokens saved by cache hits.
363    pub fn tokens_saved(&self) -> u64 {
364        self.total_original_tokens()
365            .saturating_sub(self.total_sent_tokens())
366    }
367
368    /// Returns the savings as a percentage of total original tokens.
369    pub fn savings_percent(&self) -> f64 {
370        let original = self.total_original_tokens();
371        if original == 0 {
372            return 0.0;
373        }
374        (self.tokens_saved() as f64 / original as f64) * 100.0
375    }
376}
377
378/// A block shared across multiple files, identified by its canonical source.
379#[derive(Clone, Debug)]
380pub struct SharedBlock {
381    pub canonical_path: String,
382    pub canonical_ref: String,
383    pub start_line: usize,
384    pub end_line: usize,
385    pub content: String,
386}
387
388/// In-memory file cache with segmented LRU eviction (probationary vs protected),
389/// file references, and cross-file dedup.
390pub struct SessionCache {
391    entries: HashMap<String, CacheEntry>,
392    file_refs: HashMap<String, String>,
393    next_ref: usize,
394    stats: CacheStats,
395    shared_blocks: Vec<SharedBlock>,
396    /// Hebbian co-access matrix (#3): tracks which files are read together so
397    /// eviction can protect co-accessed clusters. Updated on `store`, consulted
398    /// during eviction.
399    co_access: crate::core::hebbian_cache::CoAccessMatrix,
400}
401
402impl Default for SessionCache {
403    fn default() -> Self {
404        Self::new()
405    }
406}
407
408impl SessionCache {
409    /// Creates an empty session cache with default stats.
410    pub fn new() -> Self {
411        Self {
412            entries: HashMap::new(),
413            file_refs: HashMap::new(),
414            next_ref: 1,
415            shared_blocks: Vec::new(),
416            stats: CacheStats::default(),
417            co_access: crate::core::hebbian_cache::CoAccessMatrix::new(),
418        }
419    }
420
421    /// Record that `path` was accessed, strengthening its Hebbian association
422    /// with other files read in the same burst window (#3). Called on every
423    /// `store`; co-access boundaries are flushed via `flush_co_access`.
424    pub fn record_co_access(&mut self, path: &str) {
425        let key = normalize_key(path);
426        self.co_access
427            .record_access(crate::core::hebbian_cache::path_hash(&key));
428    }
429
430    /// Close the current co-access burst so its associations are committed.
431    /// Call at the end of a logical tool call (post-dispatch).
432    pub fn flush_co_access(&mut self) {
433        self.co_access.end_burst();
434    }
435
436    /// Per-entry Hebbian eviction bonus (#3): each cached entry that is
437    /// co-accessed with the recently-active working set earns a positive bonus
438    /// that is added to its RRF score, so clustered files survive eviction
439    /// together. Deterministic (no sampling); ticks the activation registry when
440    /// any association actually influences the decision.
441    pub(crate) fn hebbian_eviction_bonus(&self) -> HashMap<String, f64> {
442        use crate::core::hebbian_cache::path_hash;
443        if self.entries.is_empty() {
444            return HashMap::new();
445        }
446        let mut by_recency: Vec<(&String, Instant)> = self
447            .entries
448            .iter()
449            .map(|(k, e)| (k, e.last_access()))
450            .collect();
451        by_recency.sort_by_key(|(_, t)| std::cmp::Reverse(*t));
452        let active: Vec<u64> = by_recency
453            .iter()
454            .take(HEBBIAN_ACTIVE_SET)
455            .map(|(k, _)| path_hash(k))
456            .collect();
457
458        let mut out = HashMap::new();
459        for k in self.entries.keys() {
460            let h = path_hash(k);
461            // Exclude self so an entry never "protects itself".
462            let peers: Vec<u64> = active.iter().copied().filter(|&a| a != h).collect();
463            let strength = self.co_access.association_strength(h, &peers);
464            if strength > 0.0 {
465                out.insert(k.clone(), f64::from(strength) * HEBBIAN_PROTECT_WEIGHT);
466            }
467        }
468        if !out.is_empty() {
469            crate::core::introspect::tick("hebbian_cache");
470        }
471        out
472    }
473
474    /// Returns or assigns a short file reference label (F1, F2, ...) for the given path.
475    pub fn get_file_ref(&mut self, path: &str) -> String {
476        let key = normalize_key(path);
477        if let Some(r) = self.file_refs.get(&key) {
478            return r.clone();
479        }
480        let r = format!("F{}", self.next_ref);
481        self.next_ref += 1;
482        self.file_refs.insert(key, r.clone());
483        r
484    }
485
486    /// Returns the file reference label for a path without assigning a new one.
487    pub fn get_file_ref_readonly(&self, path: &str) -> Option<String> {
488        self.file_refs.get(&normalize_key(path)).cloned()
489    }
490
491    /// Looks up a cached entry by file path.
492    pub fn get(&self, path: &str) -> Option<&CacheEntry> {
493        self.entries.get(&normalize_key(path))
494    }
495
496    /// Mutable lookup of a cached entry by file path.
497    pub fn get_mut(&mut self, path: &str) -> Option<&mut CacheEntry> {
498        self.entries.get_mut(&normalize_key(path))
499    }
500
501    /// Retrieves the full (uncompressed) content for a file path, if cached.
502    /// Used by the CCR (Compress-Cache-Retrieve) mechanism.
503    pub fn get_full_content(&self, path: &str) -> Option<String> {
504        self.entries
505            .get(&normalize_key(path))
506            .and_then(CacheEntry::content)
507    }
508
509    /// Staleness-safe accessor for the *current* full content and its token
510    /// count: returns the cached copy when it is still fresh, or a fresh disk
511    /// re-read when the cached copy is stale (mtime/hash changed since it was
512    /// cached). Returns `None` when there is no cache entry, or the entry is
513    /// stale and the file can no longer be read.
514    ///
515    /// Cross-agent / retrieve paths (`ctx_retrieve`, `ctx_share`) MUST use this
516    /// instead of [`get_full_content`](Self::get_full_content): serving the raw
517    /// cached copy hands an agent a version that may no longer match disk — e.g.
518    /// a handover file edited between two agents — silently feeding it stale
519    /// context. Validation uses the entry's stored absolute `path`, because a
520    /// caller's `path` may be relative and resolve against a different CWD.
521    pub fn current_full_content(&self, path: &str) -> Option<(String, usize)> {
522        let entry = self.entries.get(&normalize_key(path))?;
523        if is_cache_entry_stale_verified(&entry.path, entry.stored_mtime, &entry.hash)
524            && let Ok(fresh) = crate::core::io_boundary::read_file_lossy(&entry.path)
525        {
526            // Cache is behind disk → serve the current bytes. If the file is now
527            // unreadable (deleted/permission), fall through to the cached copy:
528            // last-known content beats nothing, and that fall-through is not the
529            // staleness bug (it only fires when there is no current content).
530            let tokens = count_tokens(&fresh);
531            return Some((fresh, tokens));
532        }
533        Some((entry.content()?, entry.original_tokens))
534    }
535
536    /// Records a cache hit, updates access stats, and emits a cache-hit event.
537    ///
538    /// Takes `&self`: the hit counters use interior-mutable atomics, so this
539    /// runs under a shared (read) lock and lets parallel reads of different
540    /// files proceed concurrently instead of serializing on a write lock.
541    pub fn record_cache_hit(&self, path: &str) -> Option<&CacheEntry> {
542        let key = normalize_key(path);
543        let ref_label = self
544            .file_refs
545            .get(&key)
546            .cloned()
547            .unwrap_or_else(|| "F?".to_string());
548        let entry = self.entries.get(&key)?;
549        let new_count = entry.bump_read_count();
550        entry.touch();
551        self.stats.total_reads.fetch_add(1, Ordering::Relaxed);
552        self.stats.cache_hits.fetch_add(1, Ordering::Relaxed);
553        self.stats
554            .total_original_tokens
555            .fetch_add(entry.original_tokens as u64, Ordering::Relaxed);
556        let hit_msg = format!("{ref_label} cached {new_count}t {}L", entry.line_count);
557        self.stats
558            .total_sent_tokens
559            .fetch_add(count_tokens(&hit_msg) as u64, Ordering::Relaxed);
560        crate::core::events::emit_cache_hit(path, entry.original_tokens as u64);
561        Some(entry)
562    }
563
564    /// Stores file content in the cache; returns a hit if content hash matches.
565    pub fn store(&mut self, path: &str, content: &str) -> StoreResult {
566        let key = normalize_key(path);
567        // #3: feed the Hebbian co-access matrix on every read so eviction can
568        // later protect files that are habitually read together.
569        self.co_access
570            .record_access(crate::core::hebbian_cache::path_hash(&key));
571        let hash = compute_md5(content);
572        let line_count = content.lines().count();
573        let original_tokens = count_tokens(content);
574        let stored_mtime = std::fs::metadata(path).and_then(|m| m.modified()).ok();
575        let now = Instant::now();
576
577        self.stats.total_reads.fetch_add(1, Ordering::Relaxed);
578        self.stats
579            .total_original_tokens
580            .fetch_add(original_tokens as u64, Ordering::Relaxed);
581
582        if let Some(existing) = self.entries.get_mut(&key) {
583            existing.set_last_access(now);
584            if stored_mtime.is_some() {
585                existing.stored_mtime = stored_mtime;
586            }
587            if existing.hash == hash {
588                let new_count = existing.bump_read_count();
589                self.stats.cache_hits.fetch_add(1, Ordering::Relaxed);
590                let hit_msg = format!(
591                    "{} cached {new_count}t {}L",
592                    self.file_refs.get(&key).unwrap_or(&"F?".to_string()),
593                    existing.line_count,
594                );
595                self.stats
596                    .total_sent_tokens
597                    .fetch_add(count_tokens(&hit_msg) as u64, Ordering::Relaxed);
598                return StoreResult {
599                    line_count: existing.line_count,
600                    original_tokens: existing.original_tokens,
601                    read_count: new_count,
602                    was_hit: true,
603                    full_content_delivered: existing.full_content_delivered,
604                };
605            }
606            existing.compressed_outputs.clear();
607            existing.set_content(content);
608            existing.hash = hash;
609            existing.line_count = line_count;
610            existing.original_tokens = original_tokens;
611            let new_count = existing.bump_read_count();
612            existing.full_content_delivered = false;
613            existing.delivered_conversation = None;
614            if stored_mtime.is_some() {
615                existing.stored_mtime = stored_mtime;
616            }
617            self.stats
618                .total_sent_tokens
619                .fetch_add(original_tokens as u64, Ordering::Relaxed);
620            return StoreResult {
621                line_count,
622                original_tokens,
623                read_count: new_count,
624                was_hit: false,
625                full_content_delivered: false,
626            };
627        }
628
629        self.evict_if_needed(original_tokens);
630        self.get_file_ref(&key);
631
632        let entry = CacheEntry::new(
633            content,
634            hash,
635            line_count,
636            original_tokens,
637            key.clone(),
638            stored_mtime,
639        );
640
641        self.entries.insert(key, entry);
642        self.stats.files_tracked.fetch_add(1, Ordering::Relaxed);
643        self.stats
644            .total_sent_tokens
645            .fetch_add(original_tokens as u64, Ordering::Relaxed);
646        StoreResult {
647            line_count,
648            original_tokens,
649            read_count: 1,
650            was_hit: false,
651            full_content_delivered: false,
652        }
653    }
654
655    /// Returns the sum of original token counts across all cached entries.
656    pub fn total_cached_tokens(&self) -> usize {
657        self.entries.values().map(|e| e.original_tokens).sum()
658    }
659
660    /// Evict until cache fits within token budget using RRF (Reciprocal Rank Fusion).
661    /// Combines recency, frequency, and size signals to evict least-valuable entries first.
662    pub fn evict_if_needed(&mut self, incoming_tokens: usize) {
663        let max_tokens = max_cache_tokens();
664        let current = self.total_cached_tokens();
665        if current + incoming_tokens <= max_tokens {
666            return;
667        }
668
669        let now = Instant::now();
670        let all: Vec<(&String, &CacheEntry)> = self.entries.iter().collect();
671        let mut scores = eviction_scores_rrf(&all, now);
672        apply_hebbian_bonus(&mut scores, &self.hebbian_eviction_bonus());
673        // Sort ascending: lowest RRF score = least valuable = evict first
674        scores.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
675
676        let mut freed = 0usize;
677        let mut redelivered = 0u64;
678        let target = (current + incoming_tokens).saturating_sub(max_tokens);
679
680        for (path, _score) in &scores {
681            if freed >= target {
682                break;
683            }
684            if let Some(entry) = self.entries.remove(path) {
685                freed += entry.original_tokens;
686                if entry.full_content_delivered {
687                    redelivered += 1;
688                }
689                self.file_refs.remove(path);
690            }
691        }
692        crate::core::cache_telemetry::record_eviction(redelivered);
693    }
694
695    /// Returns all cached entries as (path, entry) pairs.
696    pub fn get_all_entries(&self) -> Vec<(&String, &CacheEntry)> {
697        self.entries.iter().collect()
698    }
699
700    /// Returns a reference to the aggregated cache statistics.
701    pub fn get_stats(&self) -> &CacheStats {
702        &self.stats
703    }
704
705    /// Returns the path-to-file-ref mapping (e.g. "/src/main.rs" → "F1").
706    pub fn file_ref_map(&self) -> &HashMap<String, String> {
707        &self.file_refs
708    }
709
710    /// Replaces the cross-file shared blocks used for deduplication.
711    pub fn set_shared_blocks(&mut self, blocks: Vec<SharedBlock>) {
712        self.shared_blocks = blocks;
713    }
714
715    /// Returns the current set of cross-file shared blocks.
716    pub fn get_shared_blocks(&self) -> &[SharedBlock] {
717        &self.shared_blocks
718    }
719
720    /// Replace shared blocks in content with cross-file references.
721    pub fn apply_dedup(&self, path: &str, content: &str) -> Option<String> {
722        if self.shared_blocks.is_empty() {
723            return None;
724        }
725        let refs: Vec<&SharedBlock> = self
726            .shared_blocks
727            .iter()
728            .filter(|b| b.canonical_path != path && content.contains(&b.content))
729            .collect();
730        if refs.is_empty() {
731            return None;
732        }
733        let mut result = content.to_string();
734        for block in refs {
735            result = result.replacen(
736                &block.content,
737                &format!(
738                    "[= {}:{}-{}]",
739                    block.canonical_ref, block.start_line, block.end_line
740                ),
741                1,
742            );
743        }
744        Some(result)
745    }
746
747    /// Removes a file from the cache, forcing a fresh read on next access.
748    pub fn invalidate(&mut self, path: &str) -> bool {
749        self.entries.remove(&normalize_key(path)).is_some()
750    }
751
752    /// Returns a cached compressed output for a given file and mode key.
753    /// Counts as a cache hit — the caller avoids a full disk read + recompression.
754    pub fn get_compressed(&self, path: &str, mode_key: &str) -> Option<&String> {
755        let key = normalize_key(path);
756        let entry = self.entries.get(&key)?;
757        let result = entry.get_compressed(mode_key)?;
758        entry.bump_read_count();
759        entry.touch();
760        self.stats.total_reads.fetch_add(1, Ordering::Relaxed);
761        self.stats.cache_hits.fetch_add(1, Ordering::Relaxed);
762        self.stats
763            .total_original_tokens
764            .fetch_add(entry.original_tokens as u64, Ordering::Relaxed);
765        let sent = count_tokens(result) as u64;
766        self.stats
767            .total_sent_tokens
768            .fetch_add(sent, Ordering::Relaxed);
769        crate::core::events::emit_cache_hit(path, entry.original_tokens as u64);
770        Some(result)
771    }
772
773    /// Marks that full (uncompressed) content was delivered for this file,
774    /// tagging it with the current conversation so a later re-read only serves
775    /// the `[unchanged]` stub to the same conversation (see
776    /// [`crate::core::conversation`]).
777    pub fn mark_full_delivered(&mut self, path: &str) {
778        let conversation = crate::core::conversation::current_conversation_id();
779        let key = normalize_key(path);
780        let file_ref = self.file_refs.get(&key).cloned();
781        if let Some(entry) = self.entries.get_mut(&key) {
782            entry.mark_full_delivered(conversation.clone());
783            // Write-through to the persistent stub index so an unchanged re-read
784            // in the same conversation survives a daemon restart / idle clear
785            // (#955). `record` ignores None-conversation deliveries.
786            crate::core::read_stub_index::record(crate::core::read_stub_index::StubRecord::new(
787                key.clone(),
788                entry.hash.clone(),
789                entry.stored_mtime,
790                entry.line_count,
791                file_ref.unwrap_or_default(),
792                conversation,
793            ));
794        }
795    }
796
797    /// Stores a compressed output for a given file and mode key.
798    pub fn set_compressed(&mut self, path: &str, mode_key: &str, output: String) {
799        if let Some(entry) = self.entries.get_mut(&normalize_key(path)) {
800            entry.set_compressed(mode_key, output);
801        }
802    }
803
804    /// Resets `full_content_delivered` for all entries without removing them.
805    /// Used after host context compaction — forces re-delivery on next read
806    /// while preserving compressed content and file refs.
807    pub fn reset_delivery_flags(&mut self) -> usize {
808        let mut count = 0;
809        for entry in self.entries.values_mut() {
810            if entry.full_content_delivered {
811                entry.full_content_delivered = false;
812                count += 1;
813            }
814        }
815        count
816    }
817
818    /// Returns whether full content was previously delivered for this path.
819    pub fn is_full_delivered(&self, path: &str) -> bool {
820        self.entries
821            .get(&normalize_key(path))
822            .is_some_and(|e| e.full_content_delivered)
823    }
824
825    /// Counts entries that have full content delivered — i.e. those that would
826    /// serve a cheap `[unchanged]` stub and therefore force a full re-delivery
827    /// if dropped. Used by re-delivery telemetry at clear/eviction sites.
828    pub fn count_full_delivered(&self) -> usize {
829        self.entries
830            .values()
831            .filter(|e| e.full_content_delivered)
832            .count()
833    }
834
835    /// Removes all compressed output variants (map, signatures, etc.) from every entry,
836    /// keeping the full zstd-compressed content intact. Returns the number of entries trimmed.
837    pub fn trim_compressed_outputs(&mut self) -> usize {
838        let mut trimmed = 0;
839        for entry in self.entries.values_mut() {
840            if !entry.compressed_outputs.is_empty() {
841                entry.compressed_outputs.clear();
842                trimmed += 1;
843            }
844        }
845        trimmed
846    }
847
848    /// Evicts all entries that have been read at most once (probationary).
849    /// Returns the number of entries removed.
850    pub fn evict_probationary(&mut self) -> usize {
851        let to_remove: Vec<String> = self
852            .entries
853            .iter()
854            .filter(|(_, e)| e.read_count() <= 1)
855            .map(|(k, _)| k.clone())
856            .collect();
857        let count = to_remove.len();
858        let mut redelivered = 0u64;
859        for key in &to_remove {
860            if self
861                .entries
862                .remove(key)
863                .is_some_and(|e| e.full_content_delivered)
864            {
865                redelivered += 1;
866            }
867            self.file_refs.remove(key);
868        }
869        crate::core::cache_telemetry::record_eviction(redelivered);
870        count
871    }
872
873    /// Evicts entries via RRF scoring until total tokens are at or below `target_tokens`.
874    pub fn evict_to_budget(&mut self, target_tokens: usize) {
875        let current = self.total_cached_tokens();
876        if current <= target_tokens {
877            return;
878        }
879        let now = Instant::now();
880        let all: Vec<(&String, &CacheEntry)> = self.entries.iter().collect();
881        let mut scores = eviction_scores_rrf(&all, now);
882        apply_hebbian_bonus(&mut scores, &self.hebbian_eviction_bonus());
883        scores.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
884
885        let mut freed = 0usize;
886        let mut redelivered = 0u64;
887        let target_free = current.saturating_sub(target_tokens);
888        for (path, _score) in &scores {
889            if freed >= target_free {
890                break;
891            }
892            if let Some(entry) = self.entries.remove(path) {
893                freed += entry.original_tokens;
894                if entry.full_content_delivered {
895                    redelivered += 1;
896                }
897                self.file_refs.remove(path);
898            }
899        }
900        crate::core::cache_telemetry::record_eviction(redelivered);
901    }
902
903    /// Estimates the approximate heap memory usage in bytes.
904    pub fn approximate_bytes(&self) -> usize {
905        let entries_bytes: usize = self
906            .entries
907            .values()
908            .map(|e| {
909                e.compressed_content.len()
910                    + e.hash.len()
911                    + e.path.len()
912                    + e.compressed_outputs
913                        .iter()
914                        .map(|(k, v)| k.len() + v.len())
915                        .sum::<usize>()
916                    + 128 // fixed overhead per entry
917            })
918            .sum();
919        let refs_bytes: usize = self.file_refs.iter().map(|(k, v)| k.len() + v.len()).sum();
920        let blocks_bytes: usize = self
921            .shared_blocks
922            .iter()
923            .map(|b| b.canonical_path.len() + b.canonical_ref.len() + b.content.len() + 32)
924            .sum();
925        entries_bytes + refs_bytes + blocks_bytes
926    }
927
928    const MAX_SHARED_BLOCKS: usize = 100;
929
930    /// Trims shared blocks to a maximum count, keeping the most recent.
931    pub fn trim_shared_blocks(&mut self) {
932        if self.shared_blocks.len() > Self::MAX_SHARED_BLOCKS {
933            let excess = self.shared_blocks.len() - Self::MAX_SHARED_BLOCKS;
934            self.shared_blocks.drain(..excess);
935        }
936    }
937
938    /// Clears all cached entries, file refs, and resets stats. Returns the number of entries removed.
939    pub fn clear(&mut self) -> usize {
940        let count = self.entries.len();
941        self.entries.clear();
942        self.file_refs.clear();
943        self.shared_blocks.clear();
944        self.next_ref = 1;
945        self.stats = CacheStats::default();
946        count
947    }
948}
949
950pub fn file_mtime(path: &str) -> Option<SystemTime> {
951    std::fs::metadata(path).and_then(|m| m.modified()).ok()
952}
953
954pub fn is_cache_entry_stale(path: &str, cached_mtime: Option<SystemTime>) -> bool {
955    let current = file_mtime(path);
956    match (cached_mtime, current) {
957        // Both unavailable (e.g. WSL DrvFS): can't tell → assume fresh (conservative).
958        (None, None) => false,
959        // One side missing: metadata changed or appeared/disappeared → stale.
960        (Some(_), None) | (None, Some(_)) => true,
961        // `!=`, not `>`: a *backward* mtime (git checkout, touch -t, snapshot
962        // restore) is just as much a content change as a forward one.
963        (Some(cached), Some(current)) => current != cached,
964    }
965}
966
967/// Files larger than this are not content-hashed for stub verification; the
968/// mtime check alone decides. Keeps the stub fast-path O(small-file-read).
969const VERIFY_HASH_CAP_BYTES: u64 = 8 * 1024 * 1024;
970
971fn cache_verify_enabled() -> bool {
972    std::env::var("LEAN_CTX_CACHE_VERIFY").map_or(true, |v| v != "0")
973}
974
975/// Staleness with content verification: like [`is_cache_entry_stale`], but when
976/// the mtime claims "unchanged", additionally compares the md5 of the on-disk
977/// content against the cached hash.
978///
979/// mtime alone cannot be trusted for *correctness*: same-second writes are
980/// invisible on coarse-granularity filesystems (HFS+ 1s, FAT 2s) and mtimes can
981/// be restored by tools. Serving an `[unchanged]` stub for changed content
982/// would silently mislead the agent — the worst failure mode a context layer
983/// can have. The extra disk read costs microseconds for typical source files;
984/// the stub's token savings are unaffected. Opt out: `LEAN_CTX_CACHE_VERIFY=0`.
985///
986/// Note: entries whose stored content differs from disk by design (e.g. secret
987/// redaction) hash differently and therefore never serve stubs — conservative
988/// and correct.
989pub fn is_cache_entry_stale_verified(
990    path: &str,
991    cached_mtime: Option<SystemTime>,
992    cached_hash: &str,
993) -> bool {
994    if is_cache_entry_stale(path, cached_mtime) {
995        return true;
996    }
997    if cached_hash.is_empty() || !cache_verify_enabled() {
998        return false;
999    }
1000    let Ok(meta) = std::fs::metadata(path) else {
1001        // Can't stat → never serve a stub on top of it.
1002        return true;
1003    };
1004    if meta.len() > VERIFY_HASH_CAP_BYTES {
1005        return false;
1006    }
1007    match std::fs::read(path) {
1008        // Hash the same view of the bytes that `store()` hashed (lossy UTF-8).
1009        Ok(bytes) => compute_md5(&String::from_utf8_lossy(&bytes)) != cached_hash,
1010        Err(_) => true,
1011    }
1012}
1013
1014fn compute_md5(content: &str) -> String {
1015    let mut hasher = Md5::new();
1016    hasher.update(content.as_bytes());
1017    crate::core::agent_identity::hex_encode(&hasher.finalize())
1018}
1019
1020#[cfg(test)]
1021mod tests {
1022    use super::*;
1023    use std::time::Duration;
1024
1025    #[test]
1026    fn cache_stores_and_retrieves() {
1027        let mut cache = SessionCache::new();
1028        let result = cache.store("/test/file.rs", "fn main() {}");
1029        assert!(!result.was_hit);
1030        assert_eq!(result.line_count, 1);
1031        assert!(cache.get("/test/file.rs").is_some());
1032    }
1033
1034    #[test]
1035    fn cache_hit_on_same_content() {
1036        let mut cache = SessionCache::new();
1037        cache.store("/test/file.rs", "content");
1038        let result = cache.store("/test/file.rs", "content");
1039        assert!(result.was_hit, "same content should be a cache hit");
1040    }
1041
1042    #[test]
1043    fn cache_miss_on_changed_content() {
1044        let mut cache = SessionCache::new();
1045        cache.store("/test/file.rs", "old content");
1046        let result = cache.store("/test/file.rs", "new content");
1047        assert!(!result.was_hit, "changed content should not be a cache hit");
1048    }
1049
1050    #[test]
1051    fn file_refs_are_sequential() {
1052        let mut cache = SessionCache::new();
1053        assert_eq!(cache.get_file_ref("/a.rs"), "F1");
1054        assert_eq!(cache.get_file_ref("/b.rs"), "F2");
1055        assert_eq!(cache.get_file_ref("/a.rs"), "F1"); // stable
1056    }
1057
1058    #[test]
1059    fn cache_clear_resets_everything() {
1060        let mut cache = SessionCache::new();
1061        cache.store("/a.rs", "a");
1062        cache.store("/b.rs", "b");
1063        let count = cache.clear();
1064        assert_eq!(count, 2);
1065        assert!(cache.get("/a.rs").is_none());
1066        assert_eq!(cache.get_file_ref("/c.rs"), "F1"); // refs reset
1067    }
1068
1069    #[test]
1070    fn cache_invalidate_removes_entry() {
1071        let mut cache = SessionCache::new();
1072        cache.store("/test.rs", "test");
1073        assert!(cache.invalidate("/test.rs"));
1074        assert!(!cache.invalidate("/nonexistent.rs"));
1075    }
1076
1077    #[test]
1078    fn cache_stats_track_correctly() {
1079        let mut cache = SessionCache::new();
1080        cache.store("/a.rs", "hello");
1081        cache.store("/a.rs", "hello"); // hit
1082        let stats = cache.get_stats();
1083        assert_eq!(stats.total_reads(), 2);
1084        assert_eq!(stats.cache_hits(), 1);
1085        assert!(stats.hit_rate() > 0.0);
1086    }
1087
1088    #[test]
1089    fn current_full_content_serves_cached_when_fresh() {
1090        let dir = tempfile::tempdir().unwrap();
1091        let file = dir.path().join("handover.md");
1092        std::fs::write(&file, "HANDOVER V1\n").unwrap();
1093        let path = file.to_str().unwrap();
1094
1095        let mut cache = SessionCache::new();
1096        cache.store(path, "HANDOVER V1\n");
1097
1098        let (content, tokens) = cache.current_full_content(path).unwrap();
1099        assert_eq!(content, "HANDOVER V1\n");
1100        assert!(tokens > 0);
1101    }
1102
1103    #[test]
1104    fn current_full_content_rereads_when_file_changed() {
1105        // Handover staleness: a file cached by agent A and then edited must not
1106        // be served from the stale cache to agent B (ctx_retrieve / ctx_share).
1107        let dir = tempfile::tempdir().unwrap();
1108        let file = dir.path().join("handover.md");
1109        std::fs::write(&file, "HANDOVER V1\n").unwrap();
1110        let path = file.to_str().unwrap();
1111
1112        let mut cache = SessionCache::new();
1113        cache.store(path, "HANDOVER V1\n");
1114
1115        // Simulate an edit between agents (new mtime + new content).
1116        std::thread::sleep(std::time::Duration::from_millis(10));
1117        std::fs::write(&file, "HANDOVER V2 CHANGED\n").unwrap();
1118
1119        let (content, _) = cache.current_full_content(path).unwrap();
1120        assert_eq!(
1121            content, "HANDOVER V2 CHANGED\n",
1122            "stale cached copy must be re-read from disk, not served as-is"
1123        );
1124    }
1125
1126    #[test]
1127    fn current_full_content_none_without_entry() {
1128        let cache = SessionCache::new();
1129        assert!(cache.current_full_content("/no/such/file.rs").is_none());
1130    }
1131
1132    #[test]
1133    fn current_full_content_falls_back_to_cache_when_file_unreadable() {
1134        // Stale + now-unreadable (deleted/moved): there is no current content to
1135        // serve, so the last-known cached copy is returned rather than nothing.
1136        // Canonicalize the temp dir up front so the cache key is stable after the
1137        // file is removed (macOS /var -> /private/var symlink).
1138        let dir = tempfile::tempdir().unwrap();
1139        let canon = dir.path().canonicalize().unwrap();
1140        let file = canon.join("gone.md");
1141        std::fs::write(&file, "ORIGINAL\n").unwrap();
1142        let path = file.to_str().unwrap().to_string();
1143
1144        let mut cache = SessionCache::new();
1145        cache.store(&path, "ORIGINAL\n");
1146        std::fs::remove_file(&file).unwrap();
1147
1148        let (content, _) = cache.current_full_content(&path).unwrap();
1149        assert_eq!(
1150            content, "ORIGINAL\n",
1151            "unreadable file must fall back to last-known cached content"
1152        );
1153    }
1154
1155    #[test]
1156    fn record_cache_hit_works_through_shared_ref() {
1157        let mut cache = SessionCache::new();
1158        cache.store("/x.rs", "hello world");
1159        // &self path: a cache hit can be recorded without a write lock.
1160        let shared: &SessionCache = &cache;
1161        assert!(shared.record_cache_hit("/x.rs").is_some());
1162        assert!(shared.record_cache_hit("/x.rs").is_some());
1163        // store=1 + two hits => read_count 3, cache_hits 2.
1164        assert_eq!(cache.get("/x.rs").unwrap().read_count(), 3);
1165        assert_eq!(cache.get_stats().cache_hits(), 2);
1166    }
1167
1168    #[test]
1169    fn concurrent_cache_hits_are_lossless() {
1170        use std::sync::Arc;
1171        let mut cache = SessionCache::new();
1172        cache.store("/a.rs", "a");
1173        cache.store("/b.rs", "b");
1174        // Shared (no RwLock): proves SessionCache is Sync and hit recording is
1175        // lock-free and atomic — the whole point of the read-mostly refactor.
1176        let cache = Arc::new(cache);
1177        let threads = 8;
1178        let iters = 1_000;
1179        let handles: Vec<_> = (0..threads)
1180            .map(|_| {
1181                let c = Arc::clone(&cache);
1182                std::thread::spawn(move || {
1183                    for _ in 0..iters {
1184                        c.record_cache_hit("/a.rs");
1185                        c.record_cache_hit("/b.rs");
1186                    }
1187                })
1188            })
1189            .collect();
1190        for h in handles {
1191            h.join().unwrap();
1192        }
1193        let total = (threads * iters) as u64;
1194        assert_eq!(cache.get_stats().cache_hits(), total * 2);
1195        assert_eq!(cache.get("/a.rs").unwrap().read_count(), 1 + total as u32);
1196        assert_eq!(cache.get("/b.rs").unwrap().read_count(), 1 + total as u32);
1197    }
1198
1199    #[test]
1200    fn hebbian_eviction_bonus_is_wired() {
1201        // #3: files read together build a Hebbian association via store()'s
1202        // recording, and that association must feed the eviction bonus.
1203        let mut cache = SessionCache::new();
1204        cache.store("/a.rs", "fn a() {}");
1205        cache.store("/b.rs", "fn b() {}");
1206        cache.flush_co_access(); // commit the burst → association (a,b) forms
1207        let bonus = cache.hebbian_eviction_bonus();
1208        assert!(
1209            !bonus.is_empty(),
1210            "co-accessed reads must yield a Hebbian eviction bonus (#3 wired)"
1211        );
1212    }
1213
1214    #[test]
1215    fn md5_is_deterministic() {
1216        let h1 = compute_md5("test content");
1217        let h2 = compute_md5("test content");
1218        assert_eq!(h1, h2);
1219        assert_ne!(h1, compute_md5("different"));
1220    }
1221
1222    #[test]
1223    fn rrf_eviction_prefers_recent() {
1224        let key_a = "a.rs".to_string();
1225        let key_b = "b.rs".to_string();
1226        // Construct entries first so the global instant base is initialized,
1227        // then assign access times relative to a post-init reference.
1228        let recent = CacheEntry::new("a", "h1".to_string(), 1, 10, "/a.rs".to_string(), None);
1229        let old = CacheEntry::new("b", "h2".to_string(), 1, 10, "/b.rs".to_string(), None);
1230        let t_old = Instant::now();
1231        std::thread::sleep(std::time::Duration::from_millis(10));
1232        let t_recent = Instant::now();
1233        old.set_last_access(t_old);
1234        recent.set_last_access(t_recent);
1235        let now = Instant::now();
1236        let entries: Vec<(&String, &CacheEntry)> = vec![(&key_a, &recent), (&key_b, &old)];
1237        let scores = eviction_scores_rrf(&entries, now);
1238        let score_a = scores.iter().find(|(p, _)| p == "a.rs").unwrap().1;
1239        let score_b = scores.iter().find(|(p, _)| p == "b.rs").unwrap().1;
1240        assert!(
1241            score_a > score_b,
1242            "recently accessed entries should score higher via RRF"
1243        );
1244    }
1245
1246    #[test]
1247    fn rrf_eviction_prefers_frequent() {
1248        let now = Instant::now();
1249        let key_a = "a.rs".to_string();
1250        let key_b = "b.rs".to_string();
1251        let frequent = {
1252            let e = CacheEntry::new("a", "h1".to_string(), 1, 10, "/a.rs".to_string(), None);
1253            e.set_read_count(20);
1254            e
1255        };
1256        let rare = CacheEntry::new("b", "h2".to_string(), 1, 10, "/b.rs".to_string(), None);
1257        let entries: Vec<(&String, &CacheEntry)> = vec![(&key_a, &frequent), (&key_b, &rare)];
1258        let scores = eviction_scores_rrf(&entries, now);
1259        let score_a = scores.iter().find(|(p, _)| p == "a.rs").unwrap().1;
1260        let score_b = scores.iter().find(|(p, _)| p == "b.rs").unwrap().1;
1261        assert!(
1262            score_a > score_b,
1263            "frequently accessed entries should score higher via RRF"
1264        );
1265    }
1266
1267    #[test]
1268    fn cache_budget_resolver_precedence() {
1269        // env wins when positive
1270        assert_eq!(resolve_cache_max_tokens(Some("250000"), 999), 250_000);
1271        assert_eq!(resolve_cache_max_tokens(Some(" 80000 "), 0), 80_000);
1272        // env 0 / blank / garbage falls through to config
1273        assert_eq!(resolve_cache_max_tokens(Some("0"), 123_456), 123_456);
1274        assert_eq!(resolve_cache_max_tokens(Some(""), 123_456), 123_456);
1275        assert_eq!(resolve_cache_max_tokens(Some("lots"), 123_456), 123_456);
1276        // no env → config field
1277        assert_eq!(resolve_cache_max_tokens(None, 42_000), 42_000);
1278        // nothing set anywhere → built-in default
1279        assert_eq!(resolve_cache_max_tokens(None, 0), DEFAULT_CACHE_MAX_TOKENS);
1280        assert_eq!(
1281            resolve_cache_max_tokens(Some("0"), 0),
1282            DEFAULT_CACHE_MAX_TOKENS
1283        );
1284    }
1285
1286    #[test]
1287    fn evict_if_needed_removes_lowest_score() {
1288        crate::test_env::set_var("LEAN_CTX_CACHE_MAX_TOKENS", "50");
1289        let mut cache = SessionCache::new();
1290        let big_content = "a]".repeat(30); // ~30 tokens
1291        cache.store("/old.rs", &big_content);
1292        // /old.rs now in cache with ~30 tokens
1293
1294        let new_content = "b ".repeat(30); // ~30 tokens incoming
1295        cache.store("/new.rs", &new_content);
1296        // should have evicted /old.rs to make room
1297        // (total would be ~60 which exceeds 50)
1298
1299        // At least one should remain, total should be <= 50
1300        assert!(
1301            cache.total_cached_tokens() <= 60,
1302            "eviction should have kicked in"
1303        );
1304        crate::test_env::remove_var("LEAN_CTX_CACHE_MAX_TOKENS");
1305    }
1306
1307    #[test]
1308    fn stale_detection_flags_newer_file() {
1309        let dir = tempfile::tempdir().unwrap();
1310        let path = dir.path().join("stale.txt");
1311        let p = path.to_string_lossy().to_string();
1312
1313        std::fs::write(&path, "one").unwrap();
1314        let mut cache = SessionCache::new();
1315        cache.store(&p, "one");
1316
1317        let entry = cache.get(&p).unwrap();
1318        assert!(!is_cache_entry_stale(&p, entry.stored_mtime));
1319
1320        // Ensure mtime granularity differences don't make this flaky.
1321        std::thread::sleep(Duration::from_secs(1));
1322        std::fs::write(&path, "two").unwrap();
1323
1324        let entry = cache.get(&p).unwrap();
1325        assert!(is_cache_entry_stale(&p, entry.stored_mtime));
1326    }
1327
1328    // P0-7 (#419): a *backward* mtime (git checkout, touch -t) is a change.
1329    #[test]
1330    fn stale_detection_flags_backward_mtime() {
1331        let dir = tempfile::tempdir().unwrap();
1332        let path = dir.path().join("backward.txt");
1333        let p = path.to_string_lossy().to_string();
1334
1335        std::fs::write(&path, "one").unwrap();
1336        let mut cache = SessionCache::new();
1337        cache.store(&p, "one");
1338        let entry_mtime = cache.get(&p).unwrap().stored_mtime;
1339        assert!(!is_cache_entry_stale(&p, entry_mtime));
1340
1341        // Simulate `git checkout` of an older version: content + older mtime.
1342        std::fs::write(&path, "zero").unwrap();
1343        let f = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
1344        f.set_modified(SystemTime::now() - Duration::from_hours(1))
1345            .unwrap();
1346        drop(f);
1347
1348        assert!(
1349            is_cache_entry_stale(&p, entry_mtime),
1350            "older mtime must read as stale"
1351        );
1352    }
1353
1354    // P0-7 (#419): identical mtime with different content (same-second write,
1355    // restored timestamps) is caught by the content-hash verification.
1356    #[test]
1357    fn verified_staleness_catches_same_mtime_content_change() {
1358        let dir = tempfile::tempdir().unwrap();
1359        let path = dir.path().join("sneaky.txt");
1360        let p = path.to_string_lossy().to_string();
1361
1362        std::fs::write(&path, "one").unwrap();
1363        let original_mtime = std::fs::metadata(&path).unwrap().modified().unwrap();
1364        let mut cache = SessionCache::new();
1365        cache.store(&p, "one");
1366        let (mtime, hash) = {
1367            let e = cache.get(&p).unwrap();
1368            (e.stored_mtime, e.hash.clone())
1369        };
1370
1371        // Unchanged file: both checks agree it is fresh.
1372        assert!(!is_cache_entry_stale_verified(&p, mtime, &hash));
1373
1374        // Change the content but restore the exact original mtime.
1375        std::fs::write(&path, "two").unwrap();
1376        let f = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
1377        f.set_modified(original_mtime).unwrap();
1378        drop(f);
1379
1380        assert!(
1381            !is_cache_entry_stale(&p, mtime),
1382            "test premise: the mtime check alone is fooled"
1383        );
1384        assert!(
1385            is_cache_entry_stale_verified(&p, mtime, &hash),
1386            "hash verification must catch the change"
1387        );
1388    }
1389
1390    #[test]
1391    fn verified_staleness_flags_unreadable_file() {
1392        let mut cache = SessionCache::new();
1393        cache.store("/nonexistent/file.rs", "content");
1394        let (mtime, hash) = {
1395            let e = cache.get("/nonexistent/file.rs").unwrap();
1396            (e.stored_mtime, e.hash.clone())
1397        };
1398        assert!(is_cache_entry_stale_verified(
1399            "/nonexistent/file.rs",
1400            mtime,
1401            &hash
1402        ));
1403    }
1404
1405    #[test]
1406    fn compressed_outputs_cached_and_retrieved() {
1407        let mut cache = SessionCache::new();
1408        cache.store("/test.rs", "fn main() {}");
1409        cache.set_compressed("/test.rs", "map", "compressed map output".to_string());
1410        assert_eq!(
1411            cache.get_compressed("/test.rs", "map"),
1412            Some(&"compressed map output".to_string())
1413        );
1414        assert_eq!(cache.get_compressed("/test.rs", "signatures"), None);
1415    }
1416
1417    #[test]
1418    fn compressed_outputs_cleared_on_content_change() {
1419        let mut cache = SessionCache::new();
1420        cache.store("/test.rs", "old content");
1421        cache.set_compressed("/test.rs", "map", "old map".to_string());
1422        assert!(cache.get_compressed("/test.rs", "map").is_some());
1423
1424        cache.store("/test.rs", "new content");
1425        assert_eq!(cache.get_compressed("/test.rs", "map"), None);
1426    }
1427
1428    #[test]
1429    fn compressed_outputs_survive_same_content_store() {
1430        let mut cache = SessionCache::new();
1431        cache.store("/test.rs", "content");
1432        cache.set_compressed("/test.rs", "map", "cached map".to_string());
1433
1434        let result = cache.store("/test.rs", "content");
1435        assert!(result.was_hit);
1436        assert_eq!(
1437            cache.get_compressed("/test.rs", "map"),
1438            Some(&"cached map".to_string())
1439        );
1440    }
1441
1442    #[test]
1443    fn compressed_outputs_cleared_on_invalidate() {
1444        let mut cache = SessionCache::new();
1445        cache.store("/test.rs", "content");
1446        cache.set_compressed("/test.rs", "signatures", "cached sigs".to_string());
1447        cache.invalidate("/test.rs");
1448        assert_eq!(cache.get_compressed("/test.rs", "signatures"), None);
1449    }
1450
1451    #[test]
1452    fn compressed_outputs_cleared_on_clear() {
1453        let mut cache = SessionCache::new();
1454        cache.store("/a.rs", "a");
1455        cache.set_compressed("/a.rs", "map", "map_a".to_string());
1456        cache.clear();
1457        assert_eq!(cache.get_compressed("/a.rs", "map"), None);
1458    }
1459}