Skip to main content

lean_ctx/tools/ctx_read/
mod.rs

1use std::path::Path;
2
3use crate::core::cache::SessionCache;
4use crate::core::compressor;
5use crate::core::deps;
6use crate::core::entropy;
7use crate::core::protocol;
8use crate::core::signatures;
9use crate::core::symbol_map::{self, SymbolMap};
10use crate::core::tokens::count_tokens;
11use crate::tools::CrpMode;
12mod render;
13pub(crate) use render::*;
14#[cfg(test)]
15mod tests;
16
17/// Pre-counted read output carrying the output string, resolved mode,
18/// and token count computed during mode processing.
19pub struct ReadOutput {
20    pub content: String,
21    pub resolved_mode: String,
22    /// Approximate output token count from mode processing.
23    /// The dispatch layer recounts the final assembled string for accurate savings.
24    pub output_tokens: usize,
25}
26
27const COMPRESSED_HINT: &str = "[compressed — use mode=\"full\" for complete source]";
28
29const CACHEABLE_MODES: &[&str] = &["map", "signatures"];
30
31fn is_cacheable_mode(mode: &str) -> bool {
32    CACHEABLE_MODES.contains(&mode)
33}
34
35fn compressed_cache_key(mode: &str, crp_mode: CrpMode, task: Option<&str>) -> String {
36    // Bump when the rendered map/signatures body changes shape so stale
37    // pre-line-range entries are not served from an older session cache.
38    let versioned_mode = match mode {
39        "map" => "map:v2",
40        "signatures" => "signatures:v2",
41        _ => mode,
42    };
43    let base = if crp_mode.is_tdd() {
44        format!("{versioned_mode}:tdd")
45    } else {
46        versioned_mode.to_string()
47    };
48    // map/signatures output now embeds a task-relevant body, so task-aware and
49    // task-free variants must cache under distinct keys.
50    match task.map(str::trim).filter(|t| !t.is_empty()) {
51        Some(t) => {
52            use std::hash::{Hash, Hasher};
53            let mut h = std::collections::hash_map::DefaultHasher::new();
54            t.hash(&mut h);
55            format!("{base}:t{:x}", h.finish())
56        }
57        None => base,
58    }
59}
60
61/// Extracts a short proof-line from file content to include in cache-hit stubs.
62/// Returns the first non-empty line (truncated to 60 chars) as evidence the cache is valid.
63/// Only shown after 2+ reads to avoid noise on early interactions.
64fn cache_hit_proof_line(content: &str, read_count: u32) -> Option<String> {
65    if read_count < 2 {
66        return None;
67    }
68    let first_line = content.lines().find(|l| !l.trim().is_empty())?;
69    let trimmed = first_line.trim();
70    if trimmed.len() > 60 {
71        let mut end = 57;
72        while end > 0 && !trimmed.is_char_boundary(end) {
73            end -= 1;
74        }
75        Some(format!("{}...", &trimmed[..end]))
76    } else {
77        Some(trimmed.to_string())
78    }
79}
80
81fn append_compressed_hint(output: &str, file_path: &str) -> String {
82    if !crate::core::profiles::active_profile()
83        .output_hints
84        .compressed_hint()
85    {
86        return output.to_string();
87    }
88    format!(
89        "{output}\n{COMPRESSED_HINT}\n  ctx_read(\"{file_path}\", mode=\"full\") | ctx_retrieve(\"{file_path}\")"
90    )
91}
92
93/// Reads a file as UTF-8 with lossy fallback, enforcing binary detection and max read size limit.
94/// Defense-in-depth: verifies that the canonical path stays within the process's project root
95/// (if determinable) even though callers SHOULD have already jail-checked the path.
96pub fn read_file_lossy(path: &str) -> Result<String, std::io::Error> {
97    if crate::core::binary_detect::is_binary_file(path) {
98        let msg = crate::core::binary_detect::binary_file_message(path);
99        return Err(std::io::Error::other(msg));
100    }
101
102    {
103        let canonical =
104            crate::core::pathutil::safe_canonicalize_bounded(std::path::Path::new(path), 2000);
105        if let Ok(cwd) = std::env::current_dir() {
106            let root = crate::core::pathutil::safe_canonicalize_bounded(&cwd, 2000);
107            if !canonical.starts_with(&root) {
108                let allow = crate::core::pathjail::allow_paths_from_env_and_config();
109                let data_dir_ok = crate::core::data_dir::lean_ctx_data_dir()
110                    .ok()
111                    .is_some_and(|d| canonical.starts_with(d));
112                let tmp_ok = canonical.starts_with(std::env::temp_dir());
113                if !allow.iter().any(|a| canonical.starts_with(a)) && !data_dir_ok && !tmp_ok {
114                    tracing::warn!(
115                        "defense-in-depth: path may escape project root: {}",
116                        canonical.display()
117                    );
118                }
119            }
120        }
121    }
122
123    let cap = crate::core::limits::max_read_bytes();
124
125    let file = open_with_retry(path)?;
126    let meta = file
127        .metadata()
128        .map_err(|e| std::io::Error::other(format!("cannot stat open file descriptor: {e}")))?;
129    if meta.len() > cap as u64 {
130        return Err(std::io::Error::other(format!(
131            "file too large ({} bytes, limit {} bytes via LCTX_MAX_READ_BYTES). \
132             Increase the limit or use a line-range read: mode=\"lines:1-100\"",
133            meta.len(),
134            cap
135        )));
136    }
137
138    use std::io::Read;
139    let mut bytes = Vec::with_capacity(meta.len() as usize);
140    std::io::BufReader::new(file).read_to_end(&mut bytes)?;
141    match String::from_utf8(bytes) {
142        Ok(s) => Ok(s),
143        Err(e) => Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()),
144    }
145}
146
147/// Opens a file, retrying once after a brief pause on NotFound.
148/// Works around overlay/FUSE stat-cache races in container runtimes (Docker, Codex).
149/// Uses O_NOFOLLOW on Unix for TOCTOU symlink protection.
150fn open_with_retry(path: &str) -> Result<std::fs::File, std::io::Error> {
151    match open_nofollow(path) {
152        Ok(f) => Ok(f),
153        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
154            std::thread::sleep(std::time::Duration::from_millis(50));
155            open_nofollow(path).map_err(|e| {
156                if e.kind() == std::io::ErrorKind::NotFound {
157                    std::io::Error::other(format!(
158                        "file not found: {path} — verify the path with ctx_tree or ctx_search"
159                    ))
160                } else {
161                    e
162                }
163            })
164        }
165        Err(e) => Err(e),
166    }
167}
168
169#[cfg(unix)]
170fn open_nofollow(path: &str) -> Result<std::fs::File, std::io::Error> {
171    use std::os::unix::fs::OpenOptionsExt;
172    use std::path::Path;
173
174    let p = Path::new(path);
175    // Canonicalize the parent directory (resolving symlinks in the directory path)
176    // but apply O_NOFOLLOW only to the final file component. This prevents
177    // symlink-following attacks on the target file while allowing legitimate
178    // directory symlinks (e.g., /tmp → /private/tmp on macOS).
179    if let (Some(parent), Some(filename)) = (p.parent(), p.file_name()) {
180        if parent.exists() {
181            let canonical_parent = crate::core::pathutil::safe_canonicalize_bounded(parent, 2000);
182            let canonical_path = canonical_parent.join(filename);
183            return std::fs::OpenOptions::new()
184                .read(true)
185                .custom_flags(libc::O_NOFOLLOW)
186                .open(&canonical_path);
187        }
188    }
189
190    // Fallback: direct open with O_NOFOLLOW
191    std::fs::OpenOptions::new()
192        .read(true)
193        .custom_flags(libc::O_NOFOLLOW)
194        .open(path)
195}
196
197#[cfg(not(unix))]
198fn open_nofollow(path: &str) -> Result<std::fs::File, std::io::Error> {
199    std::fs::File::open(path)
200}
201
202/// Reads a file through the cache and applies the requested compression mode.
203pub fn handle(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
204    handle_with_options(cache, path, mode, false, crp_mode, None)
205}
206
207/// Like `handle`, but invalidates the cache first to force a fresh disk read.
208pub fn handle_fresh(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
209    handle_with_options(cache, path, mode, true, crp_mode, None)
210}
211
212/// Reads a file with task-aware filtering to prioritize task-relevant content.
213pub fn handle_with_task(
214    cache: &mut SessionCache,
215    path: &str,
216    mode: &str,
217    crp_mode: CrpMode,
218    task: Option<&str>,
219) -> String {
220    handle_with_options(cache, path, mode, false, crp_mode, task)
221}
222
223/// Like `handle_with_task`, also returns the resolved mode name and pre-counted tokens.
224pub fn handle_with_task_resolved(
225    cache: &mut SessionCache,
226    path: &str,
227    mode: &str,
228    crp_mode: CrpMode,
229    task: Option<&str>,
230) -> ReadOutput {
231    handle_with_options_resolved(cache, path, mode, false, crp_mode, task)
232}
233
234/// Fresh read with task-aware filtering (invalidates cache first).
235pub fn handle_fresh_with_task(
236    cache: &mut SessionCache,
237    path: &str,
238    mode: &str,
239    crp_mode: CrpMode,
240    task: Option<&str>,
241) -> String {
242    handle_with_options(cache, path, mode, true, crp_mode, task)
243}
244
245/// Fresh read with task-aware filtering, also returns the resolved mode name and pre-counted tokens.
246pub fn handle_fresh_with_task_resolved(
247    cache: &mut SessionCache,
248    path: &str,
249    mode: &str,
250    crp_mode: CrpMode,
251    task: Option<&str>,
252) -> ReadOutput {
253    handle_with_options_resolved(cache, path, mode, true, crp_mode, task)
254}
255
256fn handle_with_options(
257    cache: &mut SessionCache,
258    path: &str,
259    mode: &str,
260    fresh: bool,
261    crp_mode: CrpMode,
262    task: Option<&str>,
263) -> String {
264    handle_with_options_resolved(cache, path, mode, fresh, crp_mode, task).content
265}
266
267/// Detects if the current execution context is a subagent (forked agent).
268/// Subagents inherit stale parent caches, so force-fresh prevents VERIFY FAIL.
269fn is_subagent_context() -> bool {
270    static IS_SUBAGENT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
271    *IS_SUBAGENT.get_or_init(|| {
272        if std::env::var("LEAN_CTX_FORCE_FRESH").is_ok_and(|v| v == "1" || v == "true") {
273            return true;
274        }
275        std::env::var("CURSOR_TASK_ID").is_ok_and(|v| !v.is_empty())
276    })
277}
278
279fn handle_with_options_resolved(
280    cache: &mut SessionCache,
281    path: &str,
282    mode: &str,
283    fresh: bool,
284    crp_mode: CrpMode,
285    task: Option<&str>,
286) -> ReadOutput {
287    let effective_fresh = fresh || is_subagent_context();
288
289    if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
290        bt.next_seq();
291    }
292    let mut result = handle_with_options_inner(cache, path, mode, effective_fresh, crp_mode, task);
293
294    if let Some(entry) = cache.get_mut(path) {
295        entry.last_mode.clone_from(&result.resolved_mode);
296    }
297
298    let dedup_allowed = matches!(
299        result.resolved_mode.as_str(),
300        "map" | "signatures" | "aggressive" | "entropy" | "task"
301    );
302    if dedup_allowed {
303        if let Some(deduped) = cache.apply_dedup(path, &result.content) {
304            let new_tokens = count_tokens(&deduped);
305            if new_tokens < result.output_tokens {
306                result.content = deduped;
307                result.output_tokens = new_tokens;
308            }
309        }
310    }
311
312    if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
313        let original_tokens = cache.get(path).map_or(0, |e| e.original_tokens);
314        bt.record_read(
315            path,
316            &result.resolved_mode,
317            result.output_tokens,
318            original_tokens,
319        );
320    }
321
322    result
323}
324
325fn handle_with_options_inner(
326    cache: &mut SessionCache,
327    path: &str,
328    mode: &str,
329    fresh: bool,
330    crp_mode: CrpMode,
331    task: Option<&str>,
332) -> ReadOutput {
333    let file_ref = cache.get_file_ref(path);
334    let short = protocol::shorten_path(path);
335    let ext = Path::new(path)
336        .extension()
337        .and_then(|e| e.to_str())
338        .unwrap_or("");
339
340    if fresh {
341        if mode == "diff" {
342            let warning = "[warning] fresh+diff is redundant — fresh invalidates cache, no diff possible. Use mode=full with fresh=true instead.";
343            return ReadOutput {
344                content: warning.to_string(),
345                resolved_mode: "diff".into(),
346                output_tokens: count_tokens(warning),
347            };
348        }
349        cache.invalidate(path);
350    }
351
352    if mode == "diff" {
353        let (out, _) = handle_diff(cache, path, &file_ref);
354        let out = crate::core::redaction::redact_text_if_enabled(&out);
355        let sent = count_tokens(&out);
356        return ReadOutput {
357            content: out,
358            resolved_mode: "diff".into(),
359            output_tokens: sent,
360        };
361    }
362
363    if mode != "full" {
364        if let Some(existing) = cache.get(path) {
365            let stale = crate::core::cache::is_cache_entry_stale(path, existing.stored_mtime);
366            if stale {
367                cache.invalidate(path);
368            }
369        }
370    }
371
372    // Extract immutable data from cache entry, then drop the borrow before
373    // any mutable operations (record_cache_hit, set_compressed, invalidate).
374    let cache_snapshot = cache.get(path).map(|existing| {
375        (
376            existing.stored_mtime,
377            existing.read_count,
378            existing.line_count,
379            existing.original_tokens,
380            existing.content(),
381        )
382    });
383
384    if let Some((cached_mtime, read_count, line_count, original_tokens, content_opt)) =
385        cache_snapshot
386    {
387        if mode == "full" {
388            let no_deg = crate::core::config::Config::load().no_degrade_effective();
389            let prof = crate::core::profiles::active_profile();
390            let force_full = no_deg
391                || (prof.read.default_mode_effective() == "full"
392                    && prof.compression.crp_mode_effective() == "off");
393            let policy_allows_stub =
394                crate::server::compaction_sync::effective_cache_policy() != "safe" && !force_full;
395            if policy_allows_stub
396                && !crate::core::cache::is_cache_entry_stale(path, cached_mtime)
397                && cache.is_full_delivered(path)
398            {
399                cache.record_cache_hit(path);
400                let out = if crate::core::protocol::meta_visible() {
401                    format!(
402                        "{file_ref}={short} [unchanged {line_count}L]\nUnchanged on disk. Use fresh=true to force re-read.",
403                        )
404                } else {
405                    let proof = content_opt
406                        .as_deref()
407                        .and_then(|c| cache_hit_proof_line(c, read_count));
408                    let reads_note = if read_count > 3 {
409                        format!(" (read {}x)", read_count + 1)
410                    } else {
411                        String::new()
412                    };
413                    match proof {
414                        Some(p) => format!(
415                            "{file_ref}={short} [unchanged {line_count}L{reads_note} | \"{p}\"]"
416                        ),
417                        None => format!("{file_ref}={short} [unchanged {line_count}L{reads_note}]"),
418                    }
419                };
420                let out = crate::core::redaction::redact_text_if_enabled(&out);
421                let sent = count_tokens(&out);
422                return ReadOutput {
423                    content: out,
424                    resolved_mode: "full".into(),
425                    output_tokens: sent,
426                };
427            }
428            let (out, _) = handle_full_with_auto_delta(cache, path, &file_ref, &short, ext, task);
429            let out = crate::core::redaction::redact_text_if_enabled(&out);
430            let sent = count_tokens(&out);
431            return ReadOutput {
432                content: out,
433                resolved_mode: "full".into(),
434                output_tokens: sent,
435            };
436        }
437
438        // Resolve mode first so we can check compressed output cache BEFORE
439        // decompressing the full content (avoids ~2-5ms zstd overhead on hits).
440        let resolved_mode = if mode == "auto" {
441            resolve_auto_mode(path, original_tokens, task)
442        } else {
443            mode.to_string()
444        };
445
446        if is_cacheable_mode(&resolved_mode) {
447            let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
448            let compressed_hit = cache.get_compressed(path, &cache_key).cloned();
449            if let Some(cached_output) = compressed_hit {
450                cache.record_cache_hit(path);
451                let out = crate::core::redaction::redact_text_if_enabled(&cached_output);
452                let sent = count_tokens(&out);
453                return ReadOutput {
454                    content: out,
455                    resolved_mode,
456                    output_tokens: sent,
457                };
458            }
459        }
460
461        if let Some(content) = content_opt {
462            let (out, _) = process_mode(
463                &content,
464                &resolved_mode,
465                &file_ref,
466                &short,
467                ext,
468                original_tokens,
469                crp_mode,
470                path,
471                task,
472            );
473            if is_cacheable_mode(&resolved_mode) {
474                let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
475                cache.set_compressed(path, &cache_key, out.clone());
476            }
477            let out = crate::core::redaction::redact_text_if_enabled(&out);
478            let sent = count_tokens(&out);
479            return ReadOutput {
480                content: out,
481                resolved_mode,
482                output_tokens: sent,
483            };
484        }
485        cache.invalidate(path);
486    }
487
488    let content = match read_file_lossy(path) {
489        Ok(c) => c,
490        Err(e) => {
491            let msg = format!("ERROR: {e}");
492            let tokens = count_tokens(&msg);
493            return ReadOutput {
494                content: msg,
495                resolved_mode: "error".into(),
496                output_tokens: tokens,
497            };
498        }
499    };
500
501    let store_result = cache.store(path, &content);
502
503    // Skip expensive hint computation for line-range reads and first reads.
504    // Hints are only useful from the 2nd read onwards when the file is contextually relevant.
505    let is_line_range = mode.starts_with("lines:");
506    let hints = crate::core::profiles::active_profile().output_hints;
507    let is_repeat_read = store_result.read_count > 1;
508    let similar_hint = if !is_line_range && is_repeat_read && hints.semantic_hint() {
509        find_similar_and_update_semantic_index(path, &content)
510    } else {
511        None
512    };
513    let graph_hint = if !is_line_range && is_repeat_read && hints.related_hint() {
514        build_graph_related_hint(path)
515    } else {
516        None
517    };
518
519    if mode == "full" {
520        cache.mark_full_delivered(path);
521        let (mut output, _) = format_full_output(
522            &file_ref,
523            &short,
524            ext,
525            &content,
526            store_result.original_tokens,
527            store_result.line_count,
528            task,
529        );
530        if let Some(hint) = &graph_hint {
531            output.push_str(&format!("\n{hint}"));
532        }
533        if let Some(hint) = similar_hint {
534            output.push_str(&format!("\n{hint}"));
535        }
536        let output = crate::core::redaction::redact_text_if_enabled(&output);
537        let sent = count_tokens(&output);
538        return ReadOutput {
539            content: output,
540            resolved_mode: "full".into(),
541            output_tokens: sent,
542        };
543    }
544
545    let resolved_mode = if mode == "auto" {
546        resolve_auto_mode(path, store_result.original_tokens, task)
547    } else {
548        mode.to_string()
549    };
550
551    let (mut output, _sent) = process_mode(
552        &content,
553        &resolved_mode,
554        &file_ref,
555        &short,
556        ext,
557        store_result.original_tokens,
558        crp_mode,
559        path,
560        task,
561    );
562    if let Some(hint) = &graph_hint {
563        output.push_str(&format!("\n{hint}"));
564    }
565    if let Some(hint) = similar_hint {
566        output.push_str(&format!("\n{hint}"));
567    }
568    if is_cacheable_mode(&resolved_mode) {
569        let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
570        cache.set_compressed(path, &cache_key, output.clone());
571    }
572    let output = crate::core::redaction::redact_text_if_enabled(&output);
573    let final_tokens = count_tokens(&output);
574    ReadOutput {
575        content: output,
576        resolved_mode,
577        output_tokens: final_tokens,
578    }
579}
580
581pub fn is_instruction_file(path: &str) -> bool {
582    let lower = path.to_lowercase();
583    let filename = std::path::Path::new(&lower)
584        .file_name()
585        .and_then(|f| f.to_str())
586        .unwrap_or("");
587
588    matches!(
589        filename,
590        "skill.md"
591            | "agents.md"
592            | "rules.md"
593            | ".cursorrules"
594            | ".clinerules"
595            | "lean-ctx.md"
596            | "lean-ctx.mdc"
597    ) || lower.contains("/skills/")
598        || lower.contains("/.cursor/rules/")
599        || lower.contains("/.claude/rules/")
600        || lower.contains("/agents.md")
601}
602
603/// Delegates to the unified `auto_mode_resolver::resolve()`.
604fn resolve_auto_mode(file_path: &str, original_tokens: usize, task: Option<&str>) -> String {
605    let ctx = crate::core::auto_mode_resolver::AutoModeContext {
606        path: file_path,
607        token_count: original_tokens,
608        task,
609        cache: None,
610    };
611    crate::core::auto_mode_resolver::resolve(&ctx).mode
612}
613
614fn find_similar_and_update_semantic_index(path: &str, content: &str) -> Option<String> {
615    const MAX_CONTENT_BYTES_FOR_SEMANTIC: usize = 32_768;
616
617    if content.len() > MAX_CONTENT_BYTES_FOR_SEMANTIC {
618        return None;
619    }
620
621    let cfg = crate::core::config::Config::load();
622    let profile = crate::core::config::MemoryProfile::effective(&cfg);
623    if !profile.semantic_cache_enabled() {
624        return None;
625    }
626
627    let project_root = detect_project_root(path);
628    let session_id = format!("{}", std::process::id());
629    let mut index = crate::core::semantic_cache::SemanticCacheIndex::load_or_create(&project_root);
630
631    let similar = index.find_similar(content, 0.7);
632    let relevant: Vec<_> = similar
633        .into_iter()
634        .filter(|(p, _)| p != path)
635        .take(3)
636        .collect();
637
638    index.add_file(path, content, &session_id);
639    if let Err(e) = index.save(&project_root) {
640        tracing::warn!("lean-ctx: failed to persist semantic index: {e}");
641    }
642
643    if relevant.is_empty() {
644        return None;
645    }
646
647    let hints: Vec<String> = relevant
648        .iter()
649        .map(|(p, score)| format!("  {p} ({:.0}% similar)", score * 100.0))
650        .collect();
651
652    Some(format!(
653        "[semantic: {} similar file(s) in cache]\n{}",
654        relevant.len(),
655        hints.join("\n")
656    ))
657}
658
659fn detect_project_root(path: &str) -> String {
660    crate::core::protocol::detect_project_root_or_cwd(path)
661}
662
663fn build_graph_related_hint(path: &str) -> Option<String> {
664    let project_root = detect_project_root(path);
665    crate::core::graph_context::build_related_hint(path, &project_root, 5)
666}
667
668const AUTO_DELTA_THRESHOLD: f64 = 0.6;
669
670/// Re-reads from disk; if content changed and delta is compact, sends auto-delta.
671fn handle_full_with_auto_delta(
672    cache: &mut SessionCache,
673    path: &str,
674    file_ref: &str,
675    short: &str,
676    ext: &str,
677    task: Option<&str>,
678) -> (String, usize) {
679    let _mode_guard = crate::core::savings_footer::ModeGuard::new("full");
680    let Ok(disk_content) = read_file_lossy(path) else {
681        cache.record_cache_hit(path);
682        if let Some(existing) = cache.get(path) {
683            if !crate::core::protocol::meta_visible() {
684                if let Some(cached) = existing.content() {
685                    return format_full_output(
686                        file_ref,
687                        short,
688                        ext,
689                        &cached,
690                        existing.original_tokens,
691                        existing.line_count,
692                        task,
693                    );
694                }
695            }
696            let out = format!(
697                "[using cached version — file read failed]\n{file_ref}={short} cached {}t {}L",
698                existing.read_count, existing.line_count
699            );
700            let sent = count_tokens(&out);
701            return (out, sent);
702        }
703        let out = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
704            format!("[file read failed and no cached version available] {file_ref}={short}")
705        } else {
706            format!("[file read failed and no cached version available] {short}")
707        };
708        let sent = count_tokens(&out);
709        return (out, sent);
710    };
711
712    let no_deg = crate::core::config::Config::load().no_degrade_effective();
713    let prof = crate::core::profiles::active_profile();
714    let force_full = no_deg
715        || (prof.read.default_mode_effective() == "full"
716            && prof.compression.crp_mode_effective() == "off");
717
718    let old_content = cache
719        .get(path)
720        .and_then(crate::core::cache::CacheEntry::content)
721        .unwrap_or_default();
722    let store_result = cache.store(path, &disk_content);
723
724    if store_result.was_hit {
725        let policy_allows_stub =
726            crate::server::compaction_sync::effective_cache_policy() != "safe" && !force_full;
727        if policy_allows_stub && store_result.full_content_delivered {
728            let out = if crate::core::protocol::meta_visible() {
729                format!(
730                    "{file_ref}={short} [unchanged {}L]\nUnchanged on disk. Use fresh=true to force re-read.",
731                    store_result.line_count
732                )
733            } else {
734                let proof = cache_hit_proof_line(&disk_content, store_result.read_count);
735                let reads_note = if store_result.read_count > 3 {
736                    format!(" (read {}x)", store_result.read_count)
737                } else {
738                    String::new()
739                };
740                match proof {
741                    Some(p) => format!(
742                        "{file_ref}={short} [unchanged {}L{reads_note} | \"{p}\"]",
743                        store_result.line_count
744                    ),
745                    None => format!(
746                        "{file_ref}={short} [unchanged {}L{reads_note}]",
747                        store_result.line_count
748                    ),
749                }
750            };
751            let sent = count_tokens(&out);
752            return (out, sent);
753        }
754        cache.mark_full_delivered(path);
755        return format_full_output(
756            file_ref,
757            short,
758            ext,
759            &disk_content,
760            store_result.original_tokens,
761            store_result.line_count,
762            task,
763        );
764    }
765
766    let diff = compressor::diff_content(&old_content, &disk_content);
767    let diff_tokens = count_tokens(&diff);
768    let full_tokens = store_result.original_tokens;
769
770    if !force_full
771        && full_tokens > 0
772        && (diff_tokens as f64) < (full_tokens as f64 * AUTO_DELTA_THRESHOLD)
773    {
774        let savings = protocol::format_savings(full_tokens, diff_tokens);
775        let head = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
776            format!("{file_ref}={short}")
777        } else {
778            short.to_string()
779        };
780        let out = format!(
781            "{head} [auto-delta] ∆{}L\n{diff}\n{savings}",
782            disk_content.lines().count()
783        );
784        return (out, diff_tokens);
785    }
786
787    format_full_output(
788        file_ref,
789        short,
790        ext,
791        &disk_content,
792        store_result.original_tokens,
793        store_result.line_count,
794        task,
795    )
796}
797
798fn handle_diff(cache: &mut SessionCache, path: &str, file_ref: &str) -> (String, usize) {
799    let _mode_guard = crate::core::savings_footer::ModeGuard::new("diff");
800    let short = protocol::shorten_path(path);
801    let old_content = cache
802        .get(path)
803        .and_then(crate::core::cache::CacheEntry::content);
804
805    let new_content = match read_file_lossy(path) {
806        Ok(c) => c,
807        Err(e) => {
808            let msg = format!("ERROR: {e}");
809            let tokens = count_tokens(&msg);
810            return (msg, tokens);
811        }
812    };
813
814    let original_tokens = count_tokens(&new_content);
815
816    let diff_output = if let Some(old) = &old_content {
817        compressor::diff_content(old, &new_content)
818    } else {
819        // No previous version cached — store content for future diffs but
820        // return a short guidance message instead of dumping the full file.
821        cache.store(path, &new_content);
822        let msg = format!(
823            "{file_ref}={short} [no cached version for diff — use mode=full first, then diff on re-read]"
824        );
825        let sent = count_tokens(&msg);
826        return (msg, sent);
827    };
828
829    cache.store(path, &new_content);
830
831    let sent = count_tokens(&diff_output);
832    let savings = protocol::format_savings(original_tokens, sent);
833    let head = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
834        format!("{file_ref}={short}")
835    } else {
836        short.clone()
837    };
838    (format!("{head} [diff]\n{diff_output}\n{savings}"), sent)
839}