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
13pub fn read_file_lossy(path: &str) -> Result<String, std::io::Error> {
14    let bytes = std::fs::read(path)?;
15    match String::from_utf8(bytes) {
16        Ok(s) => Ok(s),
17        Err(e) => Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()),
18    }
19}
20
21pub fn handle(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
22    handle_with_options(cache, path, mode, false, crp_mode, None)
23}
24
25pub fn handle_fresh(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
26    handle_with_options(cache, path, mode, true, crp_mode, None)
27}
28
29pub fn handle_with_task(
30    cache: &mut SessionCache,
31    path: &str,
32    mode: &str,
33    crp_mode: CrpMode,
34    task: Option<&str>,
35) -> String {
36    handle_with_options(cache, path, mode, false, crp_mode, task)
37}
38
39pub fn handle_fresh_with_task(
40    cache: &mut SessionCache,
41    path: &str,
42    mode: &str,
43    crp_mode: CrpMode,
44    task: Option<&str>,
45) -> String {
46    handle_with_options(cache, path, mode, true, crp_mode, task)
47}
48
49fn handle_with_options(
50    cache: &mut SessionCache,
51    path: &str,
52    mode: &str,
53    fresh: bool,
54    crp_mode: CrpMode,
55    task: Option<&str>,
56) -> String {
57    let file_ref = cache.get_file_ref(path);
58    let short = protocol::shorten_path(path);
59    let ext = Path::new(path)
60        .extension()
61        .and_then(|e| e.to_str())
62        .unwrap_or("");
63
64    if fresh {
65        cache.invalidate(path);
66    }
67
68    if mode == "diff" {
69        return handle_diff(cache, path, &file_ref);
70    }
71
72    if cache.get(path).is_some() {
73        if mode == "full" {
74            let result = handle_full_with_auto_delta(cache, path, &file_ref, &short, ext, crp_mode);
75            return maybe_apply_task_filter(result, cache, path, task);
76        }
77        let existing = cache.get(path).unwrap();
78        let content = existing.content.clone();
79        let original_tokens = existing.original_tokens;
80        return process_mode(
81            &content,
82            mode,
83            &file_ref,
84            &short,
85            ext,
86            original_tokens,
87            crp_mode,
88            path,
89            task,
90        );
91    }
92
93    let content = match read_file_lossy(path) {
94        Ok(c) => c,
95        Err(e) => return format!("ERROR: {e}"),
96    };
97
98    let (entry, _is_hit) = cache.store(path, content.clone());
99
100    if mode == "full" {
101        let result = format_full_output(cache, &file_ref, &short, ext, &content, &entry, crp_mode);
102        return maybe_apply_task_filter(result, cache, path, task);
103    }
104
105    process_mode(
106        &content,
107        mode,
108        &file_ref,
109        &short,
110        ext,
111        entry.original_tokens,
112        crp_mode,
113        path,
114        task,
115    )
116}
117
118const AUTO_DELTA_THRESHOLD: f64 = 0.6;
119
120/// Re-reads from disk; if content changed and delta is compact, sends auto-delta.
121fn handle_full_with_auto_delta(
122    cache: &mut SessionCache,
123    path: &str,
124    file_ref: &str,
125    short: &str,
126    ext: &str,
127    crp_mode: CrpMode,
128) -> String {
129    let disk_content = match read_file_lossy(path) {
130        Ok(c) => c,
131        Err(_) => {
132            cache.record_cache_hit(path);
133            let existing = cache.get(path).unwrap();
134            return format!(
135                "{file_ref}={short} cached {}t {}L",
136                existing.read_count, existing.line_count
137            );
138        }
139    };
140
141    let old_content = cache.get(path).unwrap().content.clone();
142    let (entry, is_hit) = cache.store(path, disk_content.clone());
143
144    if is_hit {
145        return format!(
146            "{file_ref}={short} cached {}t {}L",
147            entry.read_count, entry.line_count
148        );
149    }
150
151    let diff = compressor::diff_content(&old_content, &disk_content);
152    let diff_tokens = count_tokens(&diff);
153    let full_tokens = entry.original_tokens;
154
155    if full_tokens > 0 && (diff_tokens as f64) < (full_tokens as f64 * AUTO_DELTA_THRESHOLD) {
156        let savings = protocol::format_savings(full_tokens, diff_tokens);
157        return format!(
158            "{file_ref}={short} [auto-delta] ∆{}L\n{diff}\n{savings}",
159            disk_content.lines().count()
160        );
161    }
162
163    format_full_output(cache, file_ref, short, ext, &disk_content, &entry, crp_mode)
164}
165
166fn format_full_output(
167    _cache: &mut SessionCache,
168    file_ref: &str,
169    short: &str,
170    ext: &str,
171    content: &str,
172    entry: &crate::core::cache::CacheEntry,
173    _crp_mode: CrpMode,
174) -> String {
175    let tokens = entry.original_tokens;
176    let header = build_header(file_ref, short, ext, content, entry.line_count, true);
177
178    let mut sym = SymbolMap::new();
179    let idents = symbol_map::extract_identifiers(content, ext);
180    for ident in &idents {
181        sym.register(ident);
182    }
183
184    let sym_beneficial = if sym.len() >= 3 {
185        let sym_table = sym.format_table();
186        let compressed = sym.apply(content);
187        let original_tok = count_tokens(content);
188        let compressed_tok = count_tokens(&compressed) + count_tokens(&sym_table);
189        let net_saving = original_tok.saturating_sub(compressed_tok);
190        original_tok > 0 && net_saving * 100 / original_tok >= 5
191    } else {
192        false
193    };
194
195    if sym_beneficial {
196        let compressed_content = sym.apply(content);
197        let sym_table = sym.format_table();
198        let output = format!("{header}\n{compressed_content}{sym_table}");
199        let sent = count_tokens(&output);
200        let savings = protocol::format_savings(tokens, sent);
201        return format!("{output}\n{savings}");
202    }
203
204    let output = format!("{header}\n{content}");
205    let sent = count_tokens(&output);
206    let savings = protocol::format_savings(tokens, sent);
207    format!("{output}\n{savings}")
208}
209
210const TASK_FILTER_TOKEN_THRESHOLD: usize = 1000;
211const TASK_FILTER_BUDGET_RATIO: f64 = 0.5;
212
213fn maybe_apply_task_filter(
214    full_output: String,
215    cache: &mut SessionCache,
216    path: &str,
217    task: Option<&str>,
218) -> String {
219    let task_str = match task {
220        Some(t) if !t.is_empty() => t,
221        _ => return full_output,
222    };
223
224    let ext = Path::new(path)
225        .extension()
226        .and_then(|e| e.to_str())
227        .unwrap_or("");
228
229    if !crate::tools::ctx_smart_read::is_code_ext(ext) {
230        return full_output;
231    }
232
233    let original_tokens = match cache.get(path) {
234        Some(entry) => entry.original_tokens,
235        None => return full_output,
236    };
237
238    if original_tokens < TASK_FILTER_TOKEN_THRESHOLD {
239        return full_output;
240    }
241
242    let content = match cache.get(path) {
243        Some(entry) => entry.content.clone(),
244        None => return full_output,
245    };
246
247    let (_files, keywords) = crate::core::task_relevance::parse_task_hints(task_str);
248    if keywords.is_empty() {
249        return full_output;
250    }
251
252    let original_lines = content.lines().count();
253    let filtered = crate::core::task_relevance::information_bottleneck_filter(
254        &content,
255        &keywords,
256        TASK_FILTER_BUDGET_RATIO,
257    );
258    let filtered_lines = filtered.lines().count();
259
260    if filtered_lines >= original_lines {
261        return full_output;
262    }
263
264    let file_ref = cache.get_file_ref(path);
265    let short = protocol::shorten_path(path);
266    let header = format!(
267        "{file_ref}={short} {original_lines}L [task-enhanced: {original_lines}→{filtered_lines}]"
268    );
269    let sent = count_tokens(&filtered) + count_tokens(&header);
270    let savings = protocol::format_savings(original_tokens, sent);
271    format!("{header}\n{filtered}\n{savings}")
272}
273
274fn build_header(
275    file_ref: &str,
276    short: &str,
277    ext: &str,
278    content: &str,
279    line_count: usize,
280    include_deps: bool,
281) -> String {
282    let mut header = format!("{file_ref}={short} {line_count}L");
283
284    if include_deps {
285        let dep_info = deps::extract_deps(content, ext);
286        if !dep_info.imports.is_empty() {
287            let imports_str: Vec<&str> = dep_info
288                .imports
289                .iter()
290                .take(8)
291                .map(|s| s.as_str())
292                .collect();
293            header.push_str(&format!("\n deps {}", imports_str.join(",")));
294        }
295        if !dep_info.exports.is_empty() {
296            let exports_str: Vec<&str> = dep_info
297                .exports
298                .iter()
299                .take(8)
300                .map(|s| s.as_str())
301                .collect();
302            header.push_str(&format!("\n exports {}", exports_str.join(",")));
303        }
304    }
305
306    header
307}
308
309#[allow(clippy::too_many_arguments)]
310fn process_mode(
311    content: &str,
312    mode: &str,
313    file_ref: &str,
314    short: &str,
315    ext: &str,
316    original_tokens: usize,
317    crp_mode: CrpMode,
318    file_path: &str,
319    task: Option<&str>,
320) -> String {
321    let line_count = content.lines().count();
322
323    match mode {
324        "auto" => {
325            let sig =
326                crate::core::mode_predictor::FileSignature::from_path(file_path, original_tokens);
327            let predictor = crate::core::mode_predictor::ModePredictor::new();
328            let resolved = predictor
329                .predict_best_mode(&sig)
330                .unwrap_or_else(|| "full".to_string());
331            process_mode(
332                content,
333                &resolved,
334                file_ref,
335                short,
336                ext,
337                original_tokens,
338                crp_mode,
339                file_path,
340                task,
341            )
342        }
343        "signatures" => {
344            let sigs = signatures::extract_signatures(content, ext);
345            let dep_info = deps::extract_deps(content, ext);
346
347            let mut output = format!("{file_ref}={short} {line_count}L");
348            if !dep_info.imports.is_empty() {
349                let imports_str: Vec<&str> = dep_info
350                    .imports
351                    .iter()
352                    .take(8)
353                    .map(|s| s.as_str())
354                    .collect();
355                output.push_str(&format!("\n deps {}", imports_str.join(",")));
356            }
357            for sig in &sigs {
358                output.push('\n');
359                if crp_mode.is_tdd() {
360                    output.push_str(&sig.to_tdd());
361                } else {
362                    output.push_str(&sig.to_compact());
363                }
364            }
365            let sent = count_tokens(&output);
366            let savings = protocol::format_savings(original_tokens, sent);
367            format!("{output}\n{savings}")
368        }
369        "map" => {
370            let sigs = signatures::extract_signatures(content, ext);
371            let dep_info = deps::extract_deps(content, ext);
372
373            let mut output = format!("{file_ref}={short} {line_count}L");
374
375            if !dep_info.imports.is_empty() {
376                output.push_str("\n  deps: ");
377                output.push_str(&dep_info.imports.join(", "));
378            }
379
380            if !dep_info.exports.is_empty() {
381                output.push_str("\n  exports: ");
382                output.push_str(&dep_info.exports.join(", "));
383            }
384
385            let key_sigs: Vec<&signatures::Signature> = sigs
386                .iter()
387                .filter(|s| s.is_exported || s.indent == 0)
388                .collect();
389
390            if !key_sigs.is_empty() {
391                output.push_str("\n  API:");
392                for sig in &key_sigs {
393                    output.push_str("\n    ");
394                    if crp_mode.is_tdd() {
395                        output.push_str(&sig.to_tdd());
396                    } else {
397                        output.push_str(&sig.to_compact());
398                    }
399                }
400            }
401
402            let sent = count_tokens(&output);
403            let savings = protocol::format_savings(original_tokens, sent);
404            format!("{output}\n{savings}")
405        }
406        "aggressive" => {
407            let compressed = compressor::aggressive_compress(content, Some(ext));
408            let header = build_header(file_ref, short, ext, content, line_count, true);
409
410            let mut sym = SymbolMap::new();
411            let idents = symbol_map::extract_identifiers(&compressed, ext);
412            for ident in &idents {
413                sym.register(ident);
414            }
415
416            let sym_beneficial = if sym.len() >= 3 {
417                let sym_table = sym.format_table();
418                let sym_applied = sym.apply(&compressed);
419                let orig_tok = count_tokens(&compressed);
420                let comp_tok = count_tokens(&sym_applied) + count_tokens(&sym_table);
421                let net = orig_tok.saturating_sub(comp_tok);
422                orig_tok > 0 && net * 100 / orig_tok >= 5
423            } else {
424                false
425            };
426
427            if sym_beneficial {
428                let sym_output = sym.apply(&compressed);
429                let sym_table = sym.format_table();
430                let sent = count_tokens(&sym_output) + count_tokens(&sym_table);
431                let savings = protocol::format_savings(original_tokens, sent);
432                return format!("{header}\n{sym_output}{sym_table}\n{savings}");
433            }
434
435            let sent = count_tokens(&compressed);
436            let savings = protocol::format_savings(original_tokens, sent);
437            format!("{header}\n{compressed}\n{savings}")
438        }
439        "entropy" => {
440            let result = entropy::entropy_compress_adaptive(content, file_path);
441            let avg_h = entropy::analyze_entropy(content).avg_entropy;
442            let header = build_header(file_ref, short, ext, content, line_count, false);
443            let mut output = format!("{header} (H̄={avg_h:.1})");
444            for tech in &result.techniques {
445                output.push('\n');
446                output.push_str(tech);
447            }
448            output.push('\n');
449            output.push_str(&result.output);
450            let sent = count_tokens(&output);
451            let savings = protocol::format_savings(original_tokens, sent);
452            format!("{output}\n{savings}")
453        }
454        "task" => {
455            let task_str = task.unwrap_or("");
456            if task_str.is_empty() {
457                let header = build_header(file_ref, short, ext, content, line_count, true);
458                return format!("{header}\n{content}\n[task mode: no task set — returned full]");
459            }
460            let (_files, keywords) = crate::core::task_relevance::parse_task_hints(task_str);
461            if keywords.is_empty() {
462                let header = build_header(file_ref, short, ext, content, line_count, true);
463                return format!(
464                    "{header}\n{content}\n[task mode: no keywords extracted — returned full]"
465                );
466            }
467            let filtered =
468                crate::core::task_relevance::information_bottleneck_filter(content, &keywords, 0.3);
469            let filtered_lines = filtered.lines().count();
470            let header = format!(
471                "{file_ref}={short} {line_count}L [task-filtered: {line_count}→{filtered_lines}]"
472            );
473            let sent = count_tokens(&filtered) + count_tokens(&header);
474            let savings = protocol::format_savings(original_tokens, sent);
475            format!("{header}\n{filtered}\n{savings}")
476        }
477        "reference" => {
478            let tok = count_tokens(content);
479            let output = format!("{file_ref}={short}: {line_count} lines, {tok} tok ({ext})");
480            let sent = count_tokens(&output);
481            let savings = protocol::format_savings(original_tokens, sent);
482            format!("{output}\n{savings}")
483        }
484        mode if mode.starts_with("lines:") => {
485            let range_str = &mode[6..];
486            let extracted = extract_line_range(content, range_str);
487            let header = format!("{file_ref}={short} {line_count}L lines:{range_str}");
488            let sent = count_tokens(&extracted);
489            let savings = protocol::format_savings(original_tokens, sent);
490            format!("{header}\n{extracted}\n{savings}")
491        }
492        _ => {
493            let header = build_header(file_ref, short, ext, content, line_count, true);
494            format!("{header}\n{content}")
495        }
496    }
497}
498
499fn extract_line_range(content: &str, range_str: &str) -> String {
500    let lines: Vec<&str> = content.lines().collect();
501    let total = lines.len();
502    let mut selected = Vec::new();
503
504    for part in range_str.split(',') {
505        let part = part.trim();
506        if let Some((start_s, end_s)) = part.split_once('-') {
507            let start = start_s.trim().parse::<usize>().unwrap_or(1).max(1);
508            let end = end_s.trim().parse::<usize>().unwrap_or(total).min(total);
509            for i in start..=end {
510                if i >= 1 && i <= total {
511                    selected.push(format!("{i:>4}| {}", lines[i - 1]));
512                }
513            }
514        } else if let Ok(n) = part.parse::<usize>() {
515            if n >= 1 && n <= total {
516                selected.push(format!("{n:>4}| {}", lines[n - 1]));
517            }
518        }
519    }
520
521    if selected.is_empty() {
522        "No lines matched the range.".to_string()
523    } else {
524        selected.join("\n")
525    }
526}
527
528#[cfg(test)]
529mod tests {
530    use super::*;
531
532    #[test]
533    fn test_header_toon_format_no_brackets() {
534        let content = "use std::io;\nfn main() {}\n";
535        let header = build_header("F1", "main.rs", "rs", content, 2, false);
536        assert!(!header.contains('['));
537        assert!(!header.contains(']'));
538        assert!(header.contains("F1=main.rs 2L"));
539    }
540
541    #[test]
542    fn test_header_toon_deps_indented() {
543        let content = "use crate::core::cache;\nuse crate::tools;\npub fn main() {}\n";
544        let header = build_header("F1", "main.rs", "rs", content, 3, true);
545        if header.contains("deps") {
546            assert!(
547                header.contains("\n deps "),
548                "deps should use indented TOON format"
549            );
550            assert!(
551                !header.contains("deps:["),
552                "deps should not use bracket format"
553            );
554        }
555    }
556
557    #[test]
558    fn test_header_toon_saves_tokens() {
559        let content = "use crate::foo;\nuse crate::bar;\npub fn baz() {}\npub fn qux() {}\n";
560        let old_header = format!("F1=main.rs [4L +] deps:[foo,bar] exports:[baz,qux]");
561        let new_header = build_header("F1", "main.rs", "rs", content, 4, true);
562        let old_tokens = count_tokens(&old_header);
563        let new_tokens = count_tokens(&new_header);
564        assert!(
565            new_tokens <= old_tokens,
566            "TOON header ({new_tokens} tok) should be <= old format ({old_tokens} tok)"
567        );
568    }
569
570    #[test]
571    fn test_tdd_symbols_are_compact() {
572        let symbols = [
573            "⊕", "⊖", "∆", "→", "⇒", "✓", "✗", "⚠", "λ", "§", "∂", "τ", "ε",
574        ];
575        for sym in &symbols {
576            let tok = count_tokens(sym);
577            assert!(tok <= 2, "Symbol {sym} should be 1-2 tokens, got {tok}");
578        }
579    }
580
581    #[test]
582    fn test_task_mode_filters_content() {
583        let content = (0..200)
584            .map(|i| {
585                if i % 20 == 0 {
586                    format!("fn validate_token(token: &str) -> bool {{ /* line {i} */ }}")
587                } else {
588                    format!("fn unrelated_helper_{i}(x: i32) -> i32 {{ x + {i} }}")
589                }
590            })
591            .collect::<Vec<_>>()
592            .join("\n");
593        let full_tokens = count_tokens(&content);
594        let task = Some("fix bug in validate_token");
595        let result = process_mode(
596            &content,
597            "task",
598            "F1",
599            "test.rs",
600            "rs",
601            full_tokens,
602            CrpMode::Off,
603            "test.rs",
604            task,
605        );
606        let result_tokens = count_tokens(&result);
607        assert!(
608            result_tokens < full_tokens,
609            "task mode ({result_tokens} tok) should be less than full ({full_tokens} tok)"
610        );
611        assert!(
612            result.contains("task-filtered"),
613            "output should contain task-filtered marker"
614        );
615    }
616
617    #[test]
618    fn test_task_mode_without_task_returns_full() {
619        let content = "fn main() {}\nfn helper() {}\n";
620        let tokens = count_tokens(content);
621        let result = process_mode(
622            content,
623            "task",
624            "F1",
625            "test.rs",
626            "rs",
627            tokens,
628            CrpMode::Off,
629            "test.rs",
630            None,
631        );
632        assert!(
633            result.contains("no task set"),
634            "should indicate no task: {result}"
635        );
636    }
637
638    #[test]
639    fn test_reference_mode_one_line() {
640        let content = "fn main() {}\nfn helper() {}\nfn other() {}\n";
641        let tokens = count_tokens(content);
642        let result = process_mode(
643            content,
644            "reference",
645            "F1",
646            "test.rs",
647            "rs",
648            tokens,
649            CrpMode::Off,
650            "test.rs",
651            None,
652        );
653        let lines: Vec<&str> = result.lines().collect();
654        assert!(
655            lines.len() <= 3,
656            "reference mode should be very compact, got {} lines",
657            lines.len()
658        );
659        assert!(result.contains("lines"), "should contain line count");
660        assert!(result.contains("tok"), "should contain token count");
661    }
662
663    #[test]
664    fn benchmark_task_conditioned_compression() {
665        let content = generate_benchmark_code(500);
666        let full_tokens = count_tokens(&content);
667        let task = Some("fix authentication in validate_token");
668
669        let full_output = process_mode(
670            &content,
671            "full",
672            "F1",
673            "server.rs",
674            "rs",
675            full_tokens,
676            CrpMode::Off,
677            "server.rs",
678            task,
679        );
680        let task_output = process_mode(
681            &content,
682            "task",
683            "F1",
684            "server.rs",
685            "rs",
686            full_tokens,
687            CrpMode::Off,
688            "server.rs",
689            task,
690        );
691        let sig_output = process_mode(
692            &content,
693            "signatures",
694            "F1",
695            "server.rs",
696            "rs",
697            full_tokens,
698            CrpMode::Off,
699            "server.rs",
700            task,
701        );
702        let ref_output = process_mode(
703            &content,
704            "reference",
705            "F1",
706            "server.rs",
707            "rs",
708            full_tokens,
709            CrpMode::Off,
710            "server.rs",
711            task,
712        );
713
714        let full_tok = count_tokens(&full_output);
715        let task_tok = count_tokens(&task_output);
716        let sig_tok = count_tokens(&sig_output);
717        let ref_tok = count_tokens(&ref_output);
718
719        eprintln!("\n=== Task-Conditioned Compression Benchmark ===");
720        eprintln!("Source: 500-line Rust file, task='fix authentication in validate_token'");
721        eprintln!("  full:       {full_tok:>6} tokens (baseline)");
722        eprintln!(
723            "  task:       {task_tok:>6} tokens ({:.0}% savings)",
724            (1.0 - task_tok as f64 / full_tok as f64) * 100.0
725        );
726        eprintln!(
727            "  signatures: {sig_tok:>6} tokens ({:.0}% savings)",
728            (1.0 - sig_tok as f64 / full_tok as f64) * 100.0
729        );
730        eprintln!(
731            "  reference:  {ref_tok:>6} tokens ({:.0}% savings)",
732            (1.0 - ref_tok as f64 / full_tok as f64) * 100.0
733        );
734        eprintln!("================================================\n");
735
736        assert!(task_tok < full_tok, "task mode should save tokens");
737        assert!(sig_tok < full_tok, "signatures should save tokens");
738        assert!(ref_tok < sig_tok, "reference should be most compact");
739    }
740
741    fn generate_benchmark_code(lines: usize) -> String {
742        let mut code = Vec::with_capacity(lines);
743        code.push("use std::collections::HashMap;".to_string());
744        code.push("use crate::core::auth;".to_string());
745        code.push(String::new());
746        code.push("pub struct Server {".to_string());
747        code.push("    config: Config,".to_string());
748        code.push("    cache: HashMap<String, String>,".to_string());
749        code.push("}".to_string());
750        code.push(String::new());
751        code.push("impl Server {".to_string());
752        code.push(
753            "    pub fn validate_token(&self, token: &str) -> Result<Claims, AuthError> {"
754                .to_string(),
755        );
756        code.push("        let decoded = auth::decode_jwt(token)?;".to_string());
757        code.push("        if decoded.exp < chrono::Utc::now().timestamp() {".to_string());
758        code.push("            return Err(AuthError::Expired);".to_string());
759        code.push("        }".to_string());
760        code.push("        Ok(decoded.claims)".to_string());
761        code.push("    }".to_string());
762        code.push(String::new());
763
764        let remaining = lines.saturating_sub(code.len());
765        for i in 0..remaining {
766            if i % 30 == 0 {
767                code.push(format!(
768                    "    pub fn handler_{i}(&self, req: Request) -> Response {{"
769                ));
770            } else if i % 30 == 29 {
771                code.push("    }".to_string());
772            } else {
773                code.push(format!("        let val_{i} = self.cache.get(\"key_{i}\").unwrap_or(&\"default\".to_string());"));
774            }
775        }
776        code.push("}".to_string());
777        code.join("\n")
778    }
779}
780
781fn handle_diff(cache: &mut SessionCache, path: &str, file_ref: &str) -> String {
782    let short = protocol::shorten_path(path);
783    let old_content = cache.get(path).map(|e| e.content.clone());
784
785    let new_content = match read_file_lossy(path) {
786        Ok(c) => c,
787        Err(e) => return format!("ERROR: {e}"),
788    };
789
790    let original_tokens = count_tokens(&new_content);
791
792    let diff_output = if let Some(old) = &old_content {
793        compressor::diff_content(old, &new_content)
794    } else {
795        format!("[first read]\n{new_content}")
796    };
797
798    cache.store(path, new_content);
799
800    let sent = count_tokens(&diff_output);
801    let savings = protocol::format_savings(original_tokens, sent);
802    format!("{file_ref}={short} [diff]\n{diff_output}\n{savings}")
803}