Skip to main content

lean_ctx/tools/
ctx_read.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;
12
13/// Pre-counted read output carrying the output string, resolved mode,
14/// and token count computed during mode processing.
15pub struct ReadOutput {
16    pub content: String,
17    pub resolved_mode: String,
18    /// Approximate output token count from mode processing.
19    /// The dispatch layer recounts the final assembled string for accurate savings.
20    pub output_tokens: usize,
21}
22
23const COMPRESSED_HINT: &str = "[compressed — use mode=\"full\" for complete source]";
24
25const CACHEABLE_MODES: &[&str] = &["map", "signatures"];
26
27fn is_cacheable_mode(mode: &str) -> bool {
28    CACHEABLE_MODES.contains(&mode)
29}
30
31fn compressed_cache_key(mode: &str, crp_mode: CrpMode, task: Option<&str>) -> String {
32    let base = if crp_mode.is_tdd() {
33        format!("{mode}:tdd")
34    } else {
35        mode.to_string()
36    };
37    // map/signatures output now embeds a task-relevant body, so task-aware and
38    // task-free variants must cache under distinct keys.
39    match task.map(str::trim).filter(|t| !t.is_empty()) {
40        Some(t) => {
41            use std::hash::{Hash, Hasher};
42            let mut h = std::collections::hash_map::DefaultHasher::new();
43            t.hash(&mut h);
44            format!("{base}:t{:x}", h.finish())
45        }
46        None => base,
47    }
48}
49
50/// Extracts a short proof-line from file content to include in cache-hit stubs.
51/// Returns the first non-empty line (truncated to 60 chars) as evidence the cache is valid.
52/// Only shown after 2+ reads to avoid noise on early interactions.
53fn cache_hit_proof_line(content: &str, read_count: u32) -> Option<String> {
54    if read_count < 2 {
55        return None;
56    }
57    let first_line = content.lines().find(|l| !l.trim().is_empty())?;
58    let trimmed = first_line.trim();
59    if trimmed.len() > 60 {
60        let mut end = 57;
61        while end > 0 && !trimmed.is_char_boundary(end) {
62            end -= 1;
63        }
64        Some(format!("{}...", &trimmed[..end]))
65    } else {
66        Some(trimmed.to_string())
67    }
68}
69
70fn append_compressed_hint(output: &str, file_path: &str) -> String {
71    if !crate::core::profiles::active_profile()
72        .output_hints
73        .compressed_hint()
74    {
75        return output.to_string();
76    }
77    format!(
78        "{output}\n{COMPRESSED_HINT}\n  ctx_read(\"{file_path}\", mode=\"full\") | ctx_retrieve(\"{file_path}\")"
79    )
80}
81
82/// Reads a file as UTF-8 with lossy fallback, enforcing binary detection and max read size limit.
83/// Defense-in-depth: verifies that the canonical path stays within the process's project root
84/// (if determinable) even though callers SHOULD have already jail-checked the path.
85pub fn read_file_lossy(path: &str) -> Result<String, std::io::Error> {
86    if crate::core::binary_detect::is_binary_file(path) {
87        let msg = crate::core::binary_detect::binary_file_message(path);
88        return Err(std::io::Error::other(msg));
89    }
90
91    {
92        let canonical =
93            crate::core::pathutil::safe_canonicalize_bounded(std::path::Path::new(path), 2000);
94        if let Ok(cwd) = std::env::current_dir() {
95            let root = crate::core::pathutil::safe_canonicalize_bounded(&cwd, 2000);
96            if !canonical.starts_with(&root) {
97                let allow = crate::core::pathjail::allow_paths_from_env_and_config();
98                let data_dir_ok = crate::core::data_dir::lean_ctx_data_dir()
99                    .ok()
100                    .is_some_and(|d| canonical.starts_with(d));
101                let tmp_ok = canonical.starts_with(std::env::temp_dir());
102                if !allow.iter().any(|a| canonical.starts_with(a)) && !data_dir_ok && !tmp_ok {
103                    tracing::warn!(
104                        "defense-in-depth: path may escape project root: {}",
105                        canonical.display()
106                    );
107                }
108            }
109        }
110    }
111
112    let cap = crate::core::limits::max_read_bytes();
113
114    let file = open_with_retry(path)?;
115    let meta = file
116        .metadata()
117        .map_err(|e| std::io::Error::other(format!("cannot stat open file descriptor: {e}")))?;
118    if meta.len() > cap as u64 {
119        return Err(std::io::Error::other(format!(
120            "file too large ({} bytes, limit {} bytes via LCTX_MAX_READ_BYTES). \
121             Increase the limit or use a line-range read: mode=\"lines:1-100\"",
122            meta.len(),
123            cap
124        )));
125    }
126
127    use std::io::Read;
128    let mut bytes = Vec::with_capacity(meta.len() as usize);
129    std::io::BufReader::new(file).read_to_end(&mut bytes)?;
130    match String::from_utf8(bytes) {
131        Ok(s) => Ok(s),
132        Err(e) => Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()),
133    }
134}
135
136/// Opens a file, retrying once after a brief pause on NotFound.
137/// Works around overlay/FUSE stat-cache races in container runtimes (Docker, Codex).
138/// Uses O_NOFOLLOW on Unix for TOCTOU symlink protection.
139fn open_with_retry(path: &str) -> Result<std::fs::File, std::io::Error> {
140    match open_nofollow(path) {
141        Ok(f) => Ok(f),
142        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
143            std::thread::sleep(std::time::Duration::from_millis(50));
144            open_nofollow(path).map_err(|e| {
145                if e.kind() == std::io::ErrorKind::NotFound {
146                    std::io::Error::other(format!(
147                        "file not found: {path} — verify the path with ctx_tree or ctx_search"
148                    ))
149                } else {
150                    e
151                }
152            })
153        }
154        Err(e) => Err(e),
155    }
156}
157
158#[cfg(unix)]
159fn open_nofollow(path: &str) -> Result<std::fs::File, std::io::Error> {
160    use std::os::unix::fs::OpenOptionsExt;
161    use std::path::Path;
162
163    let p = Path::new(path);
164    // Canonicalize the parent directory (resolving symlinks in the directory path)
165    // but apply O_NOFOLLOW only to the final file component. This prevents
166    // symlink-following attacks on the target file while allowing legitimate
167    // directory symlinks (e.g., /tmp → /private/tmp on macOS).
168    if let (Some(parent), Some(filename)) = (p.parent(), p.file_name()) {
169        if parent.exists() {
170            let canonical_parent = crate::core::pathutil::safe_canonicalize_bounded(parent, 2000);
171            let canonical_path = canonical_parent.join(filename);
172            return std::fs::OpenOptions::new()
173                .read(true)
174                .custom_flags(libc::O_NOFOLLOW)
175                .open(&canonical_path);
176        }
177    }
178
179    // Fallback: direct open with O_NOFOLLOW
180    std::fs::OpenOptions::new()
181        .read(true)
182        .custom_flags(libc::O_NOFOLLOW)
183        .open(path)
184}
185
186#[cfg(not(unix))]
187fn open_nofollow(path: &str) -> Result<std::fs::File, std::io::Error> {
188    std::fs::File::open(path)
189}
190
191/// Reads a file through the cache and applies the requested compression mode.
192pub fn handle(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
193    handle_with_options(cache, path, mode, false, crp_mode, None)
194}
195
196/// Like `handle`, but invalidates the cache first to force a fresh disk read.
197pub fn handle_fresh(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
198    handle_with_options(cache, path, mode, true, crp_mode, None)
199}
200
201/// Reads a file with task-aware filtering to prioritize task-relevant content.
202pub fn handle_with_task(
203    cache: &mut SessionCache,
204    path: &str,
205    mode: &str,
206    crp_mode: CrpMode,
207    task: Option<&str>,
208) -> String {
209    handle_with_options(cache, path, mode, false, crp_mode, task)
210}
211
212/// Like `handle_with_task`, also returns the resolved mode name and pre-counted tokens.
213pub fn handle_with_task_resolved(
214    cache: &mut SessionCache,
215    path: &str,
216    mode: &str,
217    crp_mode: CrpMode,
218    task: Option<&str>,
219) -> ReadOutput {
220    handle_with_options_resolved(cache, path, mode, false, crp_mode, task)
221}
222
223/// Fresh read with task-aware filtering (invalidates cache first).
224pub fn handle_fresh_with_task(
225    cache: &mut SessionCache,
226    path: &str,
227    mode: &str,
228    crp_mode: CrpMode,
229    task: Option<&str>,
230) -> String {
231    handle_with_options(cache, path, mode, true, crp_mode, task)
232}
233
234/// Fresh read with task-aware filtering, also returns the resolved mode name and pre-counted tokens.
235pub fn handle_fresh_with_task_resolved(
236    cache: &mut SessionCache,
237    path: &str,
238    mode: &str,
239    crp_mode: CrpMode,
240    task: Option<&str>,
241) -> ReadOutput {
242    handle_with_options_resolved(cache, path, mode, true, crp_mode, task)
243}
244
245fn handle_with_options(
246    cache: &mut SessionCache,
247    path: &str,
248    mode: &str,
249    fresh: bool,
250    crp_mode: CrpMode,
251    task: Option<&str>,
252) -> String {
253    handle_with_options_resolved(cache, path, mode, fresh, crp_mode, task).content
254}
255
256/// Detects if the current execution context is a subagent (forked agent).
257/// Subagents inherit stale parent caches, so force-fresh prevents VERIFY FAIL.
258fn is_subagent_context() -> bool {
259    static IS_SUBAGENT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
260    *IS_SUBAGENT.get_or_init(|| {
261        if std::env::var("LEAN_CTX_FORCE_FRESH").is_ok_and(|v| v == "1" || v == "true") {
262            return true;
263        }
264        std::env::var("CURSOR_TASK_ID").is_ok_and(|v| !v.is_empty())
265    })
266}
267
268fn handle_with_options_resolved(
269    cache: &mut SessionCache,
270    path: &str,
271    mode: &str,
272    fresh: bool,
273    crp_mode: CrpMode,
274    task: Option<&str>,
275) -> ReadOutput {
276    let effective_fresh = fresh || is_subagent_context();
277
278    if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
279        bt.next_seq();
280    }
281    let mut result = handle_with_options_inner(cache, path, mode, effective_fresh, crp_mode, task);
282
283    if let Some(entry) = cache.get_mut(path) {
284        entry.last_mode.clone_from(&result.resolved_mode);
285    }
286
287    let dedup_allowed = matches!(
288        result.resolved_mode.as_str(),
289        "map" | "signatures" | "aggressive" | "entropy" | "task"
290    );
291    if dedup_allowed {
292        if let Some(deduped) = cache.apply_dedup(path, &result.content) {
293            let new_tokens = count_tokens(&deduped);
294            if new_tokens < result.output_tokens {
295                result.content = deduped;
296                result.output_tokens = new_tokens;
297            }
298        }
299    }
300
301    if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
302        let original_tokens = cache.get(path).map_or(0, |e| e.original_tokens);
303        bt.record_read(
304            path,
305            &result.resolved_mode,
306            result.output_tokens,
307            original_tokens,
308        );
309    }
310
311    result
312}
313
314fn handle_with_options_inner(
315    cache: &mut SessionCache,
316    path: &str,
317    mode: &str,
318    fresh: bool,
319    crp_mode: CrpMode,
320    task: Option<&str>,
321) -> ReadOutput {
322    let file_ref = cache.get_file_ref(path);
323    let short = protocol::shorten_path(path);
324    let ext = Path::new(path)
325        .extension()
326        .and_then(|e| e.to_str())
327        .unwrap_or("");
328
329    if fresh {
330        if mode == "diff" {
331            let warning = "[warning] fresh+diff is redundant — fresh invalidates cache, no diff possible. Use mode=full with fresh=true instead.";
332            return ReadOutput {
333                content: warning.to_string(),
334                resolved_mode: "diff".into(),
335                output_tokens: count_tokens(warning),
336            };
337        }
338        cache.invalidate(path);
339    }
340
341    if mode == "diff" {
342        let (out, _) = handle_diff(cache, path, &file_ref);
343        let out = crate::core::redaction::redact_text_if_enabled(&out);
344        let sent = count_tokens(&out);
345        return ReadOutput {
346            content: out,
347            resolved_mode: "diff".into(),
348            output_tokens: sent,
349        };
350    }
351
352    if mode != "full" {
353        if let Some(existing) = cache.get(path) {
354            let stale = crate::core::cache::is_cache_entry_stale(path, existing.stored_mtime);
355            if stale {
356                cache.invalidate(path);
357            }
358        }
359    }
360
361    // Extract immutable data from cache entry, then drop the borrow before
362    // any mutable operations (record_cache_hit, set_compressed, invalidate).
363    let cache_snapshot = cache.get(path).map(|existing| {
364        (
365            existing.stored_mtime,
366            existing.read_count,
367            existing.line_count,
368            existing.original_tokens,
369            existing.content(),
370        )
371    });
372
373    if let Some((cached_mtime, read_count, line_count, original_tokens, content_opt)) =
374        cache_snapshot
375    {
376        if mode == "full" {
377            let no_deg = crate::core::config::Config::load().no_degrade_effective();
378            let prof = crate::core::profiles::active_profile();
379            let force_full = no_deg
380                || (prof.read.default_mode_effective() == "full"
381                    && prof.compression.crp_mode_effective() == "off");
382            let policy_allows_stub =
383                crate::server::compaction_sync::effective_cache_policy() != "safe" && !force_full;
384            if policy_allows_stub
385                && !crate::core::cache::is_cache_entry_stale(path, cached_mtime)
386                && cache.is_full_delivered(path)
387            {
388                cache.record_cache_hit(path);
389                let out = if crate::core::protocol::meta_visible() {
390                    format!(
391                        "{file_ref}={short} [unchanged {line_count}L]\nUnchanged on disk. Use fresh=true to force re-read.",
392                        )
393                } else {
394                    let proof = content_opt
395                        .as_deref()
396                        .and_then(|c| cache_hit_proof_line(c, read_count));
397                    let reads_note = if read_count > 3 {
398                        format!(" (read {}x)", read_count + 1)
399                    } else {
400                        String::new()
401                    };
402                    match proof {
403                        Some(p) => format!(
404                            "{file_ref}={short} [unchanged {line_count}L{reads_note} | \"{p}\"]"
405                        ),
406                        None => format!("{file_ref}={short} [unchanged {line_count}L{reads_note}]"),
407                    }
408                };
409                let out = crate::core::redaction::redact_text_if_enabled(&out);
410                let sent = count_tokens(&out);
411                return ReadOutput {
412                    content: out,
413                    resolved_mode: "full".into(),
414                    output_tokens: sent,
415                };
416            }
417            let (out, _) = handle_full_with_auto_delta(cache, path, &file_ref, &short, ext, task);
418            let out = crate::core::redaction::redact_text_if_enabled(&out);
419            let sent = count_tokens(&out);
420            return ReadOutput {
421                content: out,
422                resolved_mode: "full".into(),
423                output_tokens: sent,
424            };
425        }
426
427        // Resolve mode first so we can check compressed output cache BEFORE
428        // decompressing the full content (avoids ~2-5ms zstd overhead on hits).
429        let resolved_mode = if mode == "auto" {
430            resolve_auto_mode(path, original_tokens, task)
431        } else {
432            mode.to_string()
433        };
434
435        if is_cacheable_mode(&resolved_mode) {
436            let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
437            let compressed_hit = cache.get_compressed(path, &cache_key).cloned();
438            if let Some(cached_output) = compressed_hit {
439                cache.record_cache_hit(path);
440                let out = crate::core::redaction::redact_text_if_enabled(&cached_output);
441                let sent = count_tokens(&out);
442                return ReadOutput {
443                    content: out,
444                    resolved_mode,
445                    output_tokens: sent,
446                };
447            }
448        }
449
450        if let Some(content) = content_opt {
451            let (out, _) = process_mode(
452                &content,
453                &resolved_mode,
454                &file_ref,
455                &short,
456                ext,
457                original_tokens,
458                crp_mode,
459                path,
460                task,
461            );
462            if is_cacheable_mode(&resolved_mode) {
463                let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
464                cache.set_compressed(path, &cache_key, out.clone());
465            }
466            let out = crate::core::redaction::redact_text_if_enabled(&out);
467            let sent = count_tokens(&out);
468            return ReadOutput {
469                content: out,
470                resolved_mode,
471                output_tokens: sent,
472            };
473        }
474        cache.invalidate(path);
475    }
476
477    let content = match read_file_lossy(path) {
478        Ok(c) => c,
479        Err(e) => {
480            let msg = format!("ERROR: {e}");
481            let tokens = count_tokens(&msg);
482            return ReadOutput {
483                content: msg,
484                resolved_mode: "error".into(),
485                output_tokens: tokens,
486            };
487        }
488    };
489
490    let store_result = cache.store(path, &content);
491
492    // Skip expensive hint computation for line-range reads and first reads.
493    // Hints are only useful from the 2nd read onwards when the file is contextually relevant.
494    let is_line_range = mode.starts_with("lines:");
495    let hints = crate::core::profiles::active_profile().output_hints;
496    let is_repeat_read = store_result.read_count > 1;
497    let similar_hint = if !is_line_range && is_repeat_read && hints.semantic_hint() {
498        find_similar_and_update_semantic_index(path, &content)
499    } else {
500        None
501    };
502    let graph_hint = if !is_line_range && is_repeat_read && hints.related_hint() {
503        build_graph_related_hint(path)
504    } else {
505        None
506    };
507
508    if mode == "full" {
509        cache.mark_full_delivered(path);
510        let (mut output, _) = format_full_output(
511            &file_ref,
512            &short,
513            ext,
514            &content,
515            store_result.original_tokens,
516            store_result.line_count,
517            task,
518        );
519        if let Some(hint) = &graph_hint {
520            output.push_str(&format!("\n{hint}"));
521        }
522        if let Some(hint) = similar_hint {
523            output.push_str(&format!("\n{hint}"));
524        }
525        let output = crate::core::redaction::redact_text_if_enabled(&output);
526        let sent = count_tokens(&output);
527        return ReadOutput {
528            content: output,
529            resolved_mode: "full".into(),
530            output_tokens: sent,
531        };
532    }
533
534    let resolved_mode = if mode == "auto" {
535        resolve_auto_mode(path, store_result.original_tokens, task)
536    } else {
537        mode.to_string()
538    };
539
540    let (mut output, _sent) = process_mode(
541        &content,
542        &resolved_mode,
543        &file_ref,
544        &short,
545        ext,
546        store_result.original_tokens,
547        crp_mode,
548        path,
549        task,
550    );
551    if let Some(hint) = &graph_hint {
552        output.push_str(&format!("\n{hint}"));
553    }
554    if let Some(hint) = similar_hint {
555        output.push_str(&format!("\n{hint}"));
556    }
557    if is_cacheable_mode(&resolved_mode) {
558        let cache_key = compressed_cache_key(&resolved_mode, crp_mode, task);
559        cache.set_compressed(path, &cache_key, output.clone());
560    }
561    let output = crate::core::redaction::redact_text_if_enabled(&output);
562    let final_tokens = count_tokens(&output);
563    ReadOutput {
564        content: output,
565        resolved_mode,
566        output_tokens: final_tokens,
567    }
568}
569
570pub fn is_instruction_file(path: &str) -> bool {
571    let lower = path.to_lowercase();
572    let filename = std::path::Path::new(&lower)
573        .file_name()
574        .and_then(|f| f.to_str())
575        .unwrap_or("");
576
577    matches!(
578        filename,
579        "skill.md"
580            | "agents.md"
581            | "rules.md"
582            | ".cursorrules"
583            | ".clinerules"
584            | "lean-ctx.md"
585            | "lean-ctx.mdc"
586    ) || lower.contains("/skills/")
587        || lower.contains("/.cursor/rules/")
588        || lower.contains("/.claude/rules/")
589        || lower.contains("/agents.md")
590}
591
592/// Delegates to the unified `auto_mode_resolver::resolve()`.
593fn resolve_auto_mode(file_path: &str, original_tokens: usize, task: Option<&str>) -> String {
594    let ctx = crate::core::auto_mode_resolver::AutoModeContext {
595        path: file_path,
596        token_count: original_tokens,
597        task,
598        cache: None,
599    };
600    crate::core::auto_mode_resolver::resolve(&ctx).mode
601}
602
603fn find_similar_and_update_semantic_index(path: &str, content: &str) -> Option<String> {
604    const MAX_CONTENT_BYTES_FOR_SEMANTIC: usize = 32_768;
605
606    if content.len() > MAX_CONTENT_BYTES_FOR_SEMANTIC {
607        return None;
608    }
609
610    let cfg = crate::core::config::Config::load();
611    let profile = crate::core::config::MemoryProfile::effective(&cfg);
612    if !profile.semantic_cache_enabled() {
613        return None;
614    }
615
616    let project_root = detect_project_root(path);
617    let session_id = format!("{}", std::process::id());
618    let mut index = crate::core::semantic_cache::SemanticCacheIndex::load_or_create(&project_root);
619
620    let similar = index.find_similar(content, 0.7);
621    let relevant: Vec<_> = similar
622        .into_iter()
623        .filter(|(p, _)| p != path)
624        .take(3)
625        .collect();
626
627    index.add_file(path, content, &session_id);
628    if let Err(e) = index.save(&project_root) {
629        tracing::warn!("lean-ctx: failed to persist semantic index: {e}");
630    }
631
632    if relevant.is_empty() {
633        return None;
634    }
635
636    let hints: Vec<String> = relevant
637        .iter()
638        .map(|(p, score)| format!("  {p} ({:.0}% similar)", score * 100.0))
639        .collect();
640
641    Some(format!(
642        "[semantic: {} similar file(s) in cache]\n{}",
643        relevant.len(),
644        hints.join("\n")
645    ))
646}
647
648fn detect_project_root(path: &str) -> String {
649    crate::core::protocol::detect_project_root_or_cwd(path)
650}
651
652fn build_graph_related_hint(path: &str) -> Option<String> {
653    let project_root = detect_project_root(path);
654    crate::core::graph_context::build_related_hint(path, &project_root, 5)
655}
656
657const AUTO_DELTA_THRESHOLD: f64 = 0.6;
658
659/// Re-reads from disk; if content changed and delta is compact, sends auto-delta.
660fn handle_full_with_auto_delta(
661    cache: &mut SessionCache,
662    path: &str,
663    file_ref: &str,
664    short: &str,
665    ext: &str,
666    task: Option<&str>,
667) -> (String, usize) {
668    let Ok(disk_content) = read_file_lossy(path) else {
669        cache.record_cache_hit(path);
670        if let Some(existing) = cache.get(path) {
671            if !crate::core::protocol::meta_visible() {
672                if let Some(cached) = existing.content() {
673                    return format_full_output(
674                        file_ref,
675                        short,
676                        ext,
677                        &cached,
678                        existing.original_tokens,
679                        existing.line_count,
680                        task,
681                    );
682                }
683            }
684            let out = format!(
685                "[using cached version — file read failed]\n{file_ref}={short} cached {}t {}L",
686                existing.read_count, existing.line_count
687            );
688            let sent = count_tokens(&out);
689            return (out, sent);
690        }
691        let out = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
692            format!("[file read failed and no cached version available] {file_ref}={short}")
693        } else {
694            format!("[file read failed and no cached version available] {short}")
695        };
696        let sent = count_tokens(&out);
697        return (out, sent);
698    };
699
700    let no_deg = crate::core::config::Config::load().no_degrade_effective();
701    let prof = crate::core::profiles::active_profile();
702    let force_full = no_deg
703        || (prof.read.default_mode_effective() == "full"
704            && prof.compression.crp_mode_effective() == "off");
705
706    let old_content = cache
707        .get(path)
708        .and_then(crate::core::cache::CacheEntry::content)
709        .unwrap_or_default();
710    let store_result = cache.store(path, &disk_content);
711
712    if store_result.was_hit {
713        let policy_allows_stub =
714            crate::server::compaction_sync::effective_cache_policy() != "safe" && !force_full;
715        if policy_allows_stub && store_result.full_content_delivered {
716            let out = if crate::core::protocol::meta_visible() {
717                format!(
718                    "{file_ref}={short} [unchanged {}L]\nUnchanged on disk. Use fresh=true to force re-read.",
719                    store_result.line_count
720                )
721            } else {
722                let proof = cache_hit_proof_line(&disk_content, store_result.read_count);
723                let reads_note = if store_result.read_count > 3 {
724                    format!(" (read {}x)", store_result.read_count)
725                } else {
726                    String::new()
727                };
728                match proof {
729                    Some(p) => format!(
730                        "{file_ref}={short} [unchanged {}L{reads_note} | \"{p}\"]",
731                        store_result.line_count
732                    ),
733                    None => format!(
734                        "{file_ref}={short} [unchanged {}L{reads_note}]",
735                        store_result.line_count
736                    ),
737                }
738            };
739            let sent = count_tokens(&out);
740            return (out, sent);
741        }
742        cache.mark_full_delivered(path);
743        return format_full_output(
744            file_ref,
745            short,
746            ext,
747            &disk_content,
748            store_result.original_tokens,
749            store_result.line_count,
750            task,
751        );
752    }
753
754    let diff = compressor::diff_content(&old_content, &disk_content);
755    let diff_tokens = count_tokens(&diff);
756    let full_tokens = store_result.original_tokens;
757
758    if !force_full
759        && full_tokens > 0
760        && (diff_tokens as f64) < (full_tokens as f64 * AUTO_DELTA_THRESHOLD)
761    {
762        let savings = protocol::format_savings(full_tokens, diff_tokens);
763        let head = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
764            format!("{file_ref}={short}")
765        } else {
766            short.to_string()
767        };
768        let out = format!(
769            "{head} [auto-delta] ∆{}L\n{diff}\n{savings}",
770            disk_content.lines().count()
771        );
772        return (out, diff_tokens);
773    }
774
775    format_full_output(
776        file_ref,
777        short,
778        ext,
779        &disk_content,
780        store_result.original_tokens,
781        store_result.line_count,
782        task,
783    )
784}
785
786fn format_full_output(
787    file_ref: &str,
788    short: &str,
789    ext: &str,
790    content: &str,
791    original_tokens: usize,
792    line_count: usize,
793    _task: Option<&str>,
794) -> (String, usize) {
795    let tokens = original_tokens;
796    let metadata = build_header(file_ref, short, ext, content, line_count, true);
797
798    let output = format!("{metadata}\n{content}");
799    let sent = count_tokens(&output);
800    (protocol::append_savings(&output, tokens, sent), sent)
801}
802
803fn build_header(
804    file_ref: &str,
805    short: &str,
806    ext: &str,
807    content: &str,
808    line_count: usize,
809    include_deps: bool,
810) -> String {
811    let mut header = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
812        format!("{file_ref}={short} {line_count}L")
813    } else {
814        format!("{short} {line_count}L")
815    };
816
817    if include_deps {
818        let dep_info = deps::extract_deps(content, ext);
819        if !dep_info.imports.is_empty() {
820            let imports_str: Vec<&str> = dep_info
821                .imports
822                .iter()
823                .take(8)
824                .map(std::string::String::as_str)
825                .collect();
826            header.push_str(&format!("\n deps {}", imports_str.join(",")));
827        }
828        if !dep_info.exports.is_empty() {
829            let exports_str: Vec<&str> = dep_info
830                .exports
831                .iter()
832                .take(8)
833                .map(std::string::String::as_str)
834                .collect();
835            header.push_str(&format!("\n exports {}", exports_str.join(",")));
836        }
837    }
838
839    header
840}
841
842#[allow(clippy::too_many_arguments)]
843fn process_mode(
844    content: &str,
845    mode: &str,
846    file_ref: &str,
847    short: &str,
848    ext: &str,
849    original_tokens: usize,
850    crp_mode: CrpMode,
851    file_path: &str,
852    task: Option<&str>,
853) -> (String, usize) {
854    let line_count = content.lines().count();
855
856    match mode {
857        "auto" => {
858            let chosen = resolve_auto_mode(file_path, original_tokens, task);
859            process_mode(
860                content,
861                &chosen,
862                file_ref,
863                short,
864                ext,
865                original_tokens,
866                crp_mode,
867                file_path,
868                task,
869            )
870        }
871        "full" => format_full_output(
872            file_ref,
873            short,
874            ext,
875            content,
876            original_tokens,
877            line_count,
878            task,
879        ),
880        "signatures" => {
881            let sigs = signatures::extract_signatures(content, ext);
882            let dep_info = deps::extract_deps(content, ext);
883
884            let mut output = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
885                format!("{file_ref}={short} {line_count}L")
886            } else {
887                format!("{short} {line_count}L")
888            };
889            if !dep_info.imports.is_empty() {
890                let imports_str: Vec<&str> = dep_info
891                    .imports
892                    .iter()
893                    .take(8)
894                    .map(std::string::String::as_str)
895                    .collect();
896                output.push_str(&format!("\n deps {}", imports_str.join(",")));
897            }
898            for sig in &sigs {
899                output.push('\n');
900                if crp_mode.is_tdd() {
901                    output.push_str(&sig.to_tdd());
902                } else {
903                    output.push_str(&sig.to_compact());
904                }
905            }
906            if let Some(body) = task_relevant_body(content, file_path, ext, task) {
907                output.push('\n');
908                output.push_str(&body);
909            }
910            let sent = count_tokens(&output);
911            (
912                append_compressed_hint(
913                    &protocol::append_savings(&output, original_tokens, sent),
914                    file_path,
915                ),
916                sent,
917            )
918        }
919        "map" => {
920            if ext == "php" {
921                if let Some(php_map) = crate::core::patterns::php::compress_php_map(content, short)
922                {
923                    let output = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
924                        format!("{file_ref}={short} {line_count}L\n{php_map}")
925                    } else {
926                        format!("{short} {line_count}L\n{php_map}")
927                    };
928                    let sent = count_tokens(&output);
929                    let output = protocol::append_savings(&output, original_tokens, sent);
930                    return (append_compressed_hint(&output, file_path), sent);
931                }
932            }
933
934            let structured = match ext {
935                "md" | "mdx" | "rst" => {
936                    crate::core::structured_read::extract_markdown_outline(content)
937                }
938                "json" => crate::core::structured_read::extract_json_structure(content),
939                "yaml" | "yml" => crate::core::structured_read::extract_yaml_structure(content),
940                "toml" => crate::core::structured_read::extract_toml_structure(content),
941                _ if file_path.to_lowercase().ends_with(".lock")
942                    || file_path.to_lowercase().ends_with("go.sum") =>
943                {
944                    crate::core::structured_read::extract_lock_summary(content, file_path)
945                }
946                _ => String::new(),
947            };
948
949            if !structured.is_empty() {
950                let mut output = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
951                    format!("{file_ref}={short} {line_count}L\n{structured}")
952                } else {
953                    format!("{short} {line_count}L\n{structured}")
954                };
955                let sent = count_tokens(&output);
956                output = protocol::append_savings(&output, original_tokens, sent);
957                return (append_compressed_hint(&output, file_path), sent);
958            }
959
960            let sigs = signatures::extract_signatures(content, ext);
961            let dep_info = deps::extract_deps(content, ext);
962
963            let mut output = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
964                format!("{file_ref}={short} {line_count}L")
965            } else {
966                format!("{short} {line_count}L")
967            };
968
969            if !dep_info.imports.is_empty() {
970                output.push_str("\n  deps: ");
971                output.push_str(&dep_info.imports.join(", "));
972            }
973
974            if !dep_info.exports.is_empty() {
975                output.push_str("\n  exports: ");
976                output.push_str(&dep_info.exports.join(", "));
977            }
978
979            let key_sigs: Vec<&signatures::Signature> = sigs
980                .iter()
981                .filter(|s| s.is_exported || s.indent == 0)
982                .collect();
983
984            if !key_sigs.is_empty() {
985                output.push_str("\n  API:");
986                for sig in &key_sigs {
987                    output.push_str("\n    ");
988                    if crp_mode.is_tdd() {
989                        output.push_str(&sig.to_tdd());
990                    } else {
991                        output.push_str(&sig.to_compact());
992                    }
993                }
994            }
995
996            if let Some(body) = task_relevant_body(content, file_path, ext, task) {
997                output.push('\n');
998                output.push_str(&body);
999            }
1000
1001            let sent = count_tokens(&output);
1002            (
1003                append_compressed_hint(
1004                    &protocol::append_savings(&output, original_tokens, sent),
1005                    file_path,
1006                ),
1007                sent,
1008            )
1009        }
1010        "aggressive" => {
1011            #[cfg(feature = "tree-sitter")]
1012            let ast_pruned = crate::core::signatures_ts::ast_prune(content, ext);
1013            #[cfg(not(feature = "tree-sitter"))]
1014            let ast_pruned: Option<String> = None;
1015
1016            let base = ast_pruned.as_deref().unwrap_or(content);
1017
1018            let session_intent = crate::core::session::SessionState::load_latest()
1019                .and_then(|s| s.active_structured_intent);
1020            let raw = if let Some(ref intent) = session_intent {
1021                compressor::task_aware_compress(base, Some(ext), intent)
1022            } else {
1023                compressor::aggressive_compress(base, Some(ext))
1024            };
1025            let compressed = compressor::safeguard_ratio(content, &raw);
1026            let header = build_header(file_ref, short, ext, content, line_count, true);
1027
1028            let mut sym = SymbolMap::new();
1029            let idents = symbol_map::extract_identifiers(&compressed, ext);
1030            for ident in &idents {
1031                sym.register(ident);
1032            }
1033
1034            if symbol_map::substitution_enabled() && sym.len() >= 3 {
1035                let sym_table = sym.format_table();
1036                let sym_applied = sym.apply(&compressed);
1037                let orig_tok = count_tokens(&compressed);
1038                let comp_tok = count_tokens(&sym_applied) + count_tokens(&sym_table);
1039                let net = orig_tok.saturating_sub(comp_tok);
1040                if orig_tok > 0 && net * 100 / orig_tok >= 5 {
1041                    let savings = protocol::format_savings(original_tokens, comp_tok);
1042                    return (
1043                        append_compressed_hint(
1044                            &format!("{header}\n{sym_applied}{sym_table}\n{savings}"),
1045                            file_path,
1046                        ),
1047                        comp_tok,
1048                    );
1049                }
1050                let savings = protocol::format_savings(original_tokens, orig_tok);
1051                return (
1052                    append_compressed_hint(
1053                        &format!("{header}\n{compressed}\n{savings}"),
1054                        file_path,
1055                    ),
1056                    orig_tok,
1057                );
1058            }
1059
1060            let sent = count_tokens(&compressed);
1061            let savings = protocol::format_savings(original_tokens, sent);
1062            (
1063                append_compressed_hint(&format!("{header}\n{compressed}\n{savings}"), file_path),
1064                sent,
1065            )
1066        }
1067        "entropy" => {
1068            let result = entropy::entropy_compress_adaptive(content, file_path);
1069            let avg_h = entropy::analyze_entropy(content).avg_entropy;
1070            let header = build_header(file_ref, short, ext, content, line_count, false);
1071            let techs = result.techniques.join(", ");
1072            let output = format!("{header} H̄={avg_h:.1} [{techs}]\n{}", result.output);
1073            let sent = count_tokens(&output);
1074            let savings = protocol::format_savings(original_tokens, sent);
1075            let compression_ratio = if original_tokens > 0 {
1076                1.0 - (sent as f64 / original_tokens as f64)
1077            } else {
1078                0.0
1079            };
1080            crate::core::adaptive_thresholds::report_bandit_outcome(compression_ratio > 0.15);
1081            (
1082                append_compressed_hint(&format!("{output}\n{savings}"), file_path),
1083                sent,
1084            )
1085        }
1086        "task" => {
1087            let task_str = task.unwrap_or("");
1088            if task_str.is_empty() {
1089                let header = build_header(file_ref, short, ext, content, line_count, true);
1090                let out = format!("{header}\n{content}\n[task mode: no task set — returned full]");
1091                let sent = count_tokens(&out);
1092                return (out, sent);
1093            }
1094            let (_files, keywords) = crate::core::task_relevance::parse_task_hints(task_str);
1095            if keywords.is_empty() {
1096                let header = build_header(file_ref, short, ext, content, line_count, true);
1097                let out = format!(
1098                    "{header}\n{content}\n[task mode: no keywords extracted — returned full]"
1099                );
1100                let sent = count_tokens(&out);
1101                return (out, sent);
1102            }
1103            let filtered =
1104                crate::core::task_relevance::information_bottleneck_filter(content, &keywords, 0.3);
1105            let filtered_lines = filtered.lines().count();
1106            let header = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
1107                format!("{file_ref}={short} {line_count}L [task-filtered: {line_count}→{filtered_lines}]")
1108            } else {
1109                format!("{short} {line_count}L [task-filtered: {line_count}→{filtered_lines}]")
1110            };
1111            let graph_ctx = if crate::core::profiles::active_profile()
1112                .output_hints
1113                .graph_context_block()
1114            {
1115                let project_root = detect_project_root(file_path);
1116                crate::core::graph_context::build_graph_context(
1117                    file_path,
1118                    &project_root,
1119                    Some(crate::core::graph_context::GraphContextOptions::default()),
1120                )
1121                .map(|c| crate::core::graph_context::format_graph_context(&c))
1122                .unwrap_or_default()
1123            } else {
1124                String::new()
1125            };
1126
1127            let sent = count_tokens(&filtered) + count_tokens(&header) + count_tokens(&graph_ctx);
1128            let savings = protocol::format_savings(original_tokens, sent);
1129            (
1130                append_compressed_hint(
1131                    &format!("{header}\n{filtered}{graph_ctx}\n{savings}"),
1132                    file_path,
1133                ),
1134                sent,
1135            )
1136        }
1137        "reference" => {
1138            let tok = count_tokens(content);
1139            let output = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
1140                format!("{file_ref}={short}: {line_count} lines, {tok} tok ({ext})")
1141            } else {
1142                format!("{short}: {line_count} lines, {tok} tok ({ext})")
1143            };
1144            let sent = count_tokens(&output);
1145            let savings = protocol::format_savings(original_tokens, sent);
1146            (format!("{output}\n{savings}"), sent)
1147        }
1148        mode if mode.starts_with("lines:") => {
1149            let range_str = &mode[6..];
1150            let extracted = extract_line_range(content, range_str);
1151            let header = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
1152                format!("{file_ref}={short} {line_count}L lines:{range_str}")
1153            } else {
1154                format!("{short} {line_count}L lines:{range_str}")
1155            };
1156            let sent = count_tokens(&extracted);
1157            let savings = protocol::format_savings(original_tokens, sent);
1158            (format!("{header}\n{extracted}\n{savings}"), sent)
1159        }
1160        unknown => {
1161            let header = build_header(file_ref, short, ext, content, line_count, true);
1162            let out = format!(
1163                "[WARNING: unknown mode '{unknown}', falling back to full]\n{header}\n{content}"
1164            );
1165            let sent = count_tokens(&out);
1166            (out, sent)
1167        }
1168    }
1169}
1170
1171/// When a task is active, find the symbol whose name best matches a task
1172/// keyword and return its body as numbered source lines (capped).
1173///
1174/// `map`/`signatures` stay compact but include the one symbol body the agent is
1175/// most likely about to read, avoiding a follow-up full read. Uses the
1176/// tree-sitter chunk extractor (which carries spans + body across languages); a
1177/// no-op when tree-sitter is unavailable.
1178fn task_relevant_body(
1179    content: &str,
1180    file_path: &str,
1181    ext: &str,
1182    task: Option<&str>,
1183) -> Option<String> {
1184    const MAX_BODY_LINES: usize = 80;
1185
1186    let task = task.map(str::trim).filter(|t| !t.is_empty())?;
1187    let (_files, keywords) = crate::core::task_relevance::parse_task_hints(task);
1188    if keywords.is_empty() {
1189        return None;
1190    }
1191    let kw_lower: Vec<String> = keywords.iter().map(|k| k.to_lowercase()).collect();
1192
1193    let chunks = crate::core::chunks_ts::extract_chunks_ts(file_path, content, ext)?;
1194
1195    // Score: exact name match (2) beats substring overlap (1).
1196    let mut best_idx: Option<usize> = None;
1197    let mut best_score = 0u8;
1198    for (i, ch) in chunks.iter().enumerate() {
1199        if ch.symbol_name.is_empty() {
1200            continue;
1201        }
1202        let name_l = ch.symbol_name.to_lowercase();
1203        let substr = kw_lower
1204            .iter()
1205            .any(|k| k.len() >= 3 && (name_l.contains(k.as_str()) || k.contains(name_l.as_str())));
1206        let score = if kw_lower.contains(&name_l) {
1207            2
1208        } else {
1209            u8::from(substr)
1210        };
1211        if score > best_score {
1212            best_score = score;
1213            best_idx = Some(i);
1214        }
1215    }
1216
1217    let ch = &chunks[best_idx?];
1218    let body_lines: Vec<&str> = ch.content.lines().collect();
1219    let total = body_lines.len();
1220    let shown = total.min(MAX_BODY_LINES);
1221    let body: String = body_lines[..shown]
1222        .iter()
1223        .enumerate()
1224        .map(|(i, l)| format!("{:>4}|{l}", ch.start_line + i))
1225        .collect::<Vec<_>>()
1226        .join("\n");
1227    let truncated = if shown < total {
1228        format!(
1229            "\n  … +{} lines — ctx_read(mode=\"lines:{}-{}\")",
1230            total - shown,
1231            ch.start_line + shown,
1232            ch.end_line
1233        )
1234    } else {
1235        String::new()
1236    };
1237    Some(format!(
1238        "  ▸ body {} L{}-{}:\n{body}{truncated}",
1239        ch.symbol_name, ch.start_line, ch.end_line
1240    ))
1241}
1242
1243fn extract_line_range(content: &str, range_str: &str) -> String {
1244    let lines: Vec<&str> = content.lines().collect();
1245    let total = lines.len();
1246    let mut selected = Vec::new();
1247
1248    for part in range_str.split(',') {
1249        let part = part.trim();
1250        if let Some((start_s, end_s)) = part.split_once('-') {
1251            let start = start_s.trim().parse::<usize>().unwrap_or(1).max(1);
1252            let end = end_s.trim().parse::<usize>().unwrap_or(total).min(total);
1253            for i in start..=end {
1254                if i >= 1 && i <= total {
1255                    selected.push(format!("{i:>4}| {}", lines[i - 1]));
1256                }
1257            }
1258        } else if let Ok(n) = part.parse::<usize>() {
1259            if n >= 1 && n <= total {
1260                selected.push(format!("{n:>4}| {}", lines[n - 1]));
1261            }
1262        }
1263    }
1264
1265    if selected.is_empty() {
1266        "No lines matched the range.".to_string()
1267    } else {
1268        selected.join("\n")
1269    }
1270}
1271
1272fn handle_diff(cache: &mut SessionCache, path: &str, file_ref: &str) -> (String, usize) {
1273    let short = protocol::shorten_path(path);
1274    let old_content = cache
1275        .get(path)
1276        .and_then(crate::core::cache::CacheEntry::content);
1277
1278    let new_content = match read_file_lossy(path) {
1279        Ok(c) => c,
1280        Err(e) => {
1281            let msg = format!("ERROR: {e}");
1282            let tokens = count_tokens(&msg);
1283            return (msg, tokens);
1284        }
1285    };
1286
1287    let original_tokens = count_tokens(&new_content);
1288
1289    let diff_output = if let Some(old) = &old_content {
1290        compressor::diff_content(old, &new_content)
1291    } else {
1292        // No previous version cached — store content for future diffs but
1293        // return a short guidance message instead of dumping the full file.
1294        cache.store(path, &new_content);
1295        let msg = format!(
1296            "{file_ref}={short} [no cached version for diff — use mode=full first, then diff on re-read]"
1297        );
1298        let sent = count_tokens(&msg);
1299        return (msg, sent);
1300    };
1301
1302    cache.store(path, &new_content);
1303
1304    let sent = count_tokens(&diff_output);
1305    let savings = protocol::format_savings(original_tokens, sent);
1306    let head = if crate::core::protocol::meta_visible() && !file_ref.is_empty() {
1307        format!("{file_ref}={short}")
1308    } else {
1309        short.clone()
1310    };
1311    (format!("{head} [diff]\n{diff_output}\n{savings}"), sent)
1312}
1313
1314#[cfg(test)]
1315mod tests {
1316    use super::*;
1317    use std::time::Duration;
1318
1319    #[test]
1320    fn test_header_toon_format_no_brackets() {
1321        let _lock = crate::core::data_dir::test_env_lock();
1322        std::env::set_var("LEAN_CTX_META", "1");
1323        let content = "use std::io;\nfn main() {}\n";
1324        let header = build_header("F1", "main.rs", "rs", content, 2, false);
1325        assert!(!header.contains('['));
1326        assert!(!header.contains(']'));
1327        assert!(header.contains("F1=main.rs 2L"));
1328        std::env::remove_var("LEAN_CTX_META");
1329    }
1330
1331    #[test]
1332    fn test_header_toon_deps_indented() {
1333        let _lock = crate::core::data_dir::test_env_lock();
1334        std::env::set_var("LEAN_CTX_META", "1");
1335        let content = "use crate::core::cache;\nuse crate::tools;\npub fn main() {}\n";
1336        let header = build_header("F1", "main.rs", "rs", content, 3, true);
1337        if header.contains("deps") {
1338            assert!(
1339                header.contains("\n deps "),
1340                "deps should use indented TOON format"
1341            );
1342            assert!(
1343                !header.contains("deps:["),
1344                "deps should not use bracket format"
1345            );
1346        }
1347        std::env::remove_var("LEAN_CTX_META");
1348    }
1349
1350    #[test]
1351    fn test_header_toon_saves_tokens() {
1352        let _lock = crate::core::data_dir::test_env_lock();
1353        std::env::set_var("LEAN_CTX_META", "1");
1354        let content = "use crate::foo;\nuse crate::bar;\npub fn baz() {}\npub fn qux() {}\n";
1355        let old_header = "F1=main.rs [4L +] deps:[foo,bar] exports:[baz,qux]".to_string();
1356        let new_header = build_header("F1", "main.rs", "rs", content, 4, true);
1357        let old_tokens = count_tokens(&old_header);
1358        let new_tokens = count_tokens(&new_header);
1359        assert!(
1360            new_tokens <= old_tokens,
1361            "TOON header ({new_tokens} tok) should be <= old format ({old_tokens} tok)"
1362        );
1363        std::env::remove_var("LEAN_CTX_META");
1364    }
1365
1366    #[test]
1367    fn test_tdd_symbols_are_compact() {
1368        let symbols = [
1369            "⊕", "⊖", "∆", "→", "⇒", "✓", "✗", "⚠", "λ", "§", "∂", "τ", "ε",
1370        ];
1371        for sym in &symbols {
1372            let tok = count_tokens(sym);
1373            assert!(tok <= 2, "Symbol {sym} should be 1-2 tokens, got {tok}");
1374        }
1375    }
1376
1377    #[test]
1378    fn test_task_mode_filters_content() {
1379        let content = (0..200)
1380            .map(|i| {
1381                if i % 20 == 0 {
1382                    format!("fn validate_token(token: &str) -> bool {{ /* line {i} */ }}")
1383                } else {
1384                    format!("fn unrelated_helper_{i}(x: i32) -> i32 {{ x + {i} }}")
1385                }
1386            })
1387            .collect::<Vec<_>>()
1388            .join("\n");
1389        let full_tokens = count_tokens(&content);
1390        let task = Some("fix bug in validate_token");
1391        let (result, result_tokens) = process_mode(
1392            &content,
1393            "task",
1394            "F1",
1395            "test.rs",
1396            "rs",
1397            full_tokens,
1398            CrpMode::Off,
1399            "test.rs",
1400            task,
1401        );
1402        assert!(
1403            result_tokens < full_tokens,
1404            "task mode ({result_tokens} tok) should be less than full ({full_tokens} tok)"
1405        );
1406        assert!(
1407            result.contains("task-filtered"),
1408            "output should contain task-filtered marker"
1409        );
1410    }
1411
1412    #[test]
1413    fn test_task_mode_without_task_returns_full() {
1414        let content = "fn main() {}\nfn helper() {}\n";
1415        let tokens = count_tokens(content);
1416        let (result, _sent) = process_mode(
1417            content,
1418            "task",
1419            "F1",
1420            "test.rs",
1421            "rs",
1422            tokens,
1423            CrpMode::Off,
1424            "test.rs",
1425            None,
1426        );
1427        assert!(
1428            result.contains("no task set"),
1429            "should indicate no task: {result}"
1430        );
1431    }
1432
1433    #[test]
1434    fn test_reference_mode_one_line() {
1435        let content = "fn main() {}\nfn helper() {}\nfn other() {}\n";
1436        let tokens = count_tokens(content);
1437        let (result, _sent) = process_mode(
1438            content,
1439            "reference",
1440            "F1",
1441            "test.rs",
1442            "rs",
1443            tokens,
1444            CrpMode::Off,
1445            "test.rs",
1446            None,
1447        );
1448        let lines: Vec<&str> = result.lines().collect();
1449        assert!(
1450            lines.len() <= 3,
1451            "reference mode should be very compact, got {} lines",
1452            lines.len()
1453        );
1454        assert!(result.contains("lines"), "should contain line count");
1455        assert!(result.contains("tok"), "should contain token count");
1456    }
1457
1458    #[test]
1459    fn cached_lines_mode_invalidates_on_mtime_change() {
1460        let dir = tempfile::tempdir().unwrap();
1461        let path = dir.path().join("file.txt");
1462        let p = path.to_string_lossy().to_string();
1463
1464        std::fs::write(&path, "one\nsecond\n").unwrap();
1465        let mut cache = SessionCache::new();
1466
1467        let r1 = handle_with_task_resolved(&mut cache, &p, "lines:1-1", CrpMode::Off, None);
1468        let l1: Vec<&str> = r1.content.lines().collect();
1469        let got1 = l1.get(1).copied().unwrap_or_default().trim();
1470        let got1 = got1.split_once('|').map_or(got1, |(_, s)| s.trim());
1471        assert_eq!(got1, "one");
1472
1473        std::thread::sleep(Duration::from_secs(1));
1474        std::fs::write(&path, "two\nsecond\n").unwrap();
1475
1476        let r2 = handle_with_task_resolved(&mut cache, &p, "lines:1-1", CrpMode::Off, None);
1477        let l2: Vec<&str> = r2.content.lines().collect();
1478        let got2 = l2.get(1).copied().unwrap_or_default().trim();
1479        let got2 = got2.split_once('|').map_or(got2, |(_, s)| s.trim());
1480        assert_eq!(got2, "two");
1481    }
1482
1483    #[test]
1484    #[cfg_attr(tarpaulin, ignore)]
1485    fn benchmark_task_conditioned_compression() {
1486        // Keep this reasonably small so CI coverage instrumentation stays fast.
1487        let content = generate_benchmark_code(200);
1488        let full_tokens = count_tokens(&content);
1489        let task = Some("fix authentication in validate_token");
1490
1491        let (_full_output, full_tok) = process_mode(
1492            &content,
1493            "full",
1494            "F1",
1495            "server.rs",
1496            "rs",
1497            full_tokens,
1498            CrpMode::Off,
1499            "server.rs",
1500            task,
1501        );
1502        let (_task_output, task_tok) = process_mode(
1503            &content,
1504            "task",
1505            "F1",
1506            "server.rs",
1507            "rs",
1508            full_tokens,
1509            CrpMode::Off,
1510            "server.rs",
1511            task,
1512        );
1513        let (_sig_output, sig_tok) = process_mode(
1514            &content,
1515            "signatures",
1516            "F1",
1517            "server.rs",
1518            "rs",
1519            full_tokens,
1520            CrpMode::Off,
1521            "server.rs",
1522            task,
1523        );
1524        let (_ref_output, ref_tok) = process_mode(
1525            &content,
1526            "reference",
1527            "F1",
1528            "server.rs",
1529            "rs",
1530            full_tokens,
1531            CrpMode::Off,
1532            "server.rs",
1533            task,
1534        );
1535
1536        eprintln!("\n=== Task-Conditioned Compression Benchmark ===");
1537        eprintln!("Source: 200-line Rust file, task='fix authentication in validate_token'");
1538        eprintln!("  full:       {full_tok:>6} tokens (baseline)");
1539        eprintln!(
1540            "  task:       {task_tok:>6} tokens ({:.0}% savings)",
1541            (1.0 - task_tok as f64 / full_tok as f64) * 100.0
1542        );
1543        eprintln!(
1544            "  signatures: {sig_tok:>6} tokens ({:.0}% savings)",
1545            (1.0 - sig_tok as f64 / full_tok as f64) * 100.0
1546        );
1547        eprintln!(
1548            "  reference:  {ref_tok:>6} tokens ({:.0}% savings)",
1549            (1.0 - ref_tok as f64 / full_tok as f64) * 100.0
1550        );
1551        eprintln!("================================================\n");
1552
1553        assert!(task_tok < full_tok, "task mode should save tokens");
1554        assert!(sig_tok < full_tok, "signatures should save tokens");
1555        assert!(ref_tok < sig_tok, "reference should be most compact");
1556    }
1557
1558    fn generate_benchmark_code(lines: usize) -> String {
1559        let mut code = Vec::with_capacity(lines);
1560        code.push("use std::collections::HashMap;".to_string());
1561        code.push("use crate::core::auth;".to_string());
1562        code.push(String::new());
1563        code.push("pub struct Server {".to_string());
1564        code.push("    config: Config,".to_string());
1565        code.push("    cache: HashMap<String, String>,".to_string());
1566        code.push("}".to_string());
1567        code.push(String::new());
1568        code.push("impl Server {".to_string());
1569        code.push(
1570            "    pub fn validate_token(&self, token: &str) -> Result<Claims, AuthError> {"
1571                .to_string(),
1572        );
1573        code.push("        let decoded = auth::decode_jwt(token)?;".to_string());
1574        code.push("        if decoded.exp < chrono::Utc::now().timestamp() {".to_string());
1575        code.push("            return Err(AuthError::Expired);".to_string());
1576        code.push("        }".to_string());
1577        code.push("        Ok(decoded.claims)".to_string());
1578        code.push("    }".to_string());
1579        code.push(String::new());
1580
1581        let remaining = lines.saturating_sub(code.len());
1582        for i in 0..remaining {
1583            if i % 30 == 0 {
1584                code.push(format!(
1585                    "    pub fn handler_{i}(&self, req: Request) -> Response {{"
1586                ));
1587            } else if i % 30 == 29 {
1588                code.push("    }".to_string());
1589            } else {
1590                code.push(format!("        let val_{i} = self.cache.get(\"key_{i}\").unwrap_or(&\"default\".to_string());"));
1591            }
1592        }
1593        code.push("}".to_string());
1594        code.join("\n")
1595    }
1596
1597    #[test]
1598    fn map_mode_inlines_task_relevant_body() {
1599        let content = "pub fn alpha() {\n    let a = 1;\n}\n\npub fn validate_token(t: &str) -> bool {\n    let ok = check(t);\n    ok\n}\n";
1600        let tokens = count_tokens(content);
1601        let (with_task, _) = process_mode(
1602            content,
1603            "map",
1604            "F1",
1605            "test.rs",
1606            "rs",
1607            tokens,
1608            CrpMode::Off,
1609            "test.rs",
1610            Some("fix bug in validate_token"),
1611        );
1612        assert!(
1613            with_task.contains("▸ body") && with_task.contains("validate_token"),
1614            "map with task should inline the matching body: {with_task}"
1615        );
1616        let (no_task, _) = process_mode(
1617            content,
1618            "map",
1619            "F1",
1620            "test.rs",
1621            "rs",
1622            tokens,
1623            CrpMode::Off,
1624            "test.rs",
1625            None,
1626        );
1627        assert!(
1628            !no_task.contains("▸ body"),
1629            "map without a task must not inline a body: {no_task}"
1630        );
1631    }
1632
1633    #[test]
1634    fn compressed_cache_key_distinguishes_task() {
1635        let no_task = compressed_cache_key("map", CrpMode::Off, None);
1636        let with_task = compressed_cache_key("map", CrpMode::Off, Some("fix login"));
1637        let other_task = compressed_cache_key("map", CrpMode::Off, Some("refactor db"));
1638        assert_eq!(no_task, "map");
1639        assert_ne!(with_task, no_task);
1640        assert_ne!(with_task, other_task);
1641    }
1642
1643    #[test]
1644    fn instruction_file_detection() {
1645        assert!(is_instruction_file(
1646            "/home/user/.pi/agent/skills/committing-changes/SKILL.md"
1647        ));
1648        assert!(is_instruction_file("/workspace/.cursor/rules/lean-ctx.mdc"));
1649        assert!(is_instruction_file("/project/AGENTS.md"));
1650        assert!(is_instruction_file("/project/.cursorrules"));
1651        assert!(is_instruction_file("/home/user/.claude/rules/my-rule.md"));
1652        assert!(is_instruction_file("/skills/some-skill/README.md"));
1653
1654        assert!(!is_instruction_file("/project/src/main.rs"));
1655        assert!(!is_instruction_file("/project/config.json"));
1656        assert!(!is_instruction_file("/project/data/report.csv"));
1657    }
1658
1659    #[test]
1660    fn resolve_auto_mode_returns_full_for_instruction_files() {
1661        let mode = resolve_auto_mode(
1662            "/home/user/.pi/agent/skills/committing-changes/SKILL.md",
1663            5000,
1664            Some("read"),
1665        );
1666        assert_eq!(mode, "full", "SKILL.md must always be read in full");
1667
1668        let mode = resolve_auto_mode("/workspace/AGENTS.md", 3000, Some("read"));
1669        assert_eq!(mode, "full", "AGENTS.md must always be read in full");
1670
1671        let mode = resolve_auto_mode("/workspace/.cursorrules", 2000, None);
1672        assert_eq!(mode, "full", ".cursorrules must always be read in full");
1673    }
1674}