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 raw = compressor::aggressive_compress(content, Some(ext));
408            let compressed = compressor::safeguard_ratio(content, &raw);
409            let header = build_header(file_ref, short, ext, content, line_count, true);
410
411            let mut sym = SymbolMap::new();
412            let idents = symbol_map::extract_identifiers(&compressed, ext);
413            for ident in &idents {
414                sym.register(ident);
415            }
416
417            let sym_beneficial = if sym.len() >= 3 {
418                let sym_table = sym.format_table();
419                let sym_applied = sym.apply(&compressed);
420                let orig_tok = count_tokens(&compressed);
421                let comp_tok = count_tokens(&sym_applied) + count_tokens(&sym_table);
422                let net = orig_tok.saturating_sub(comp_tok);
423                orig_tok > 0 && net * 100 / orig_tok >= 5
424            } else {
425                false
426            };
427
428            if sym_beneficial {
429                let sym_output = sym.apply(&compressed);
430                let sym_table = sym.format_table();
431                let sent = count_tokens(&sym_output) + count_tokens(&sym_table);
432                let savings = protocol::format_savings(original_tokens, sent);
433                return format!("{header}\n{sym_output}{sym_table}\n{savings}");
434            }
435
436            let sent = count_tokens(&compressed);
437            let savings = protocol::format_savings(original_tokens, sent);
438            format!("{header}\n{compressed}\n{savings}")
439        }
440        "entropy" => {
441            let result = entropy::entropy_compress_adaptive(content, file_path);
442            let avg_h = entropy::analyze_entropy(content).avg_entropy;
443            let header = build_header(file_ref, short, ext, content, line_count, false);
444            let techs = result.techniques.join(", ");
445            let output = format!("{header} H̄={avg_h:.1} [{techs}]\n{}", result.output);
446            let sent = count_tokens(&output);
447            let savings = protocol::format_savings(original_tokens, sent);
448            format!("{output}\n{savings}")
449        }
450        "task" => {
451            let task_str = task.unwrap_or("");
452            if task_str.is_empty() {
453                let header = build_header(file_ref, short, ext, content, line_count, true);
454                return format!("{header}\n{content}\n[task mode: no task set — returned full]");
455            }
456            let (_files, keywords) = crate::core::task_relevance::parse_task_hints(task_str);
457            if keywords.is_empty() {
458                let header = build_header(file_ref, short, ext, content, line_count, true);
459                return format!(
460                    "{header}\n{content}\n[task mode: no keywords extracted — returned full]"
461                );
462            }
463            let filtered =
464                crate::core::task_relevance::information_bottleneck_filter(content, &keywords, 0.3);
465            let filtered_lines = filtered.lines().count();
466            let header = format!(
467                "{file_ref}={short} {line_count}L [task-filtered: {line_count}→{filtered_lines}]"
468            );
469            let sent = count_tokens(&filtered) + count_tokens(&header);
470            let savings = protocol::format_savings(original_tokens, sent);
471            format!("{header}\n{filtered}\n{savings}")
472        }
473        "reference" => {
474            let tok = count_tokens(content);
475            let output = format!("{file_ref}={short}: {line_count} lines, {tok} tok ({ext})");
476            let sent = count_tokens(&output);
477            let savings = protocol::format_savings(original_tokens, sent);
478            format!("{output}\n{savings}")
479        }
480        mode if mode.starts_with("lines:") => {
481            let range_str = &mode[6..];
482            let extracted = extract_line_range(content, range_str);
483            let header = format!("{file_ref}={short} {line_count}L lines:{range_str}");
484            let sent = count_tokens(&extracted);
485            let savings = protocol::format_savings(original_tokens, sent);
486            format!("{header}\n{extracted}\n{savings}")
487        }
488        _ => {
489            let header = build_header(file_ref, short, ext, content, line_count, true);
490            format!("{header}\n{content}")
491        }
492    }
493}
494
495fn extract_line_range(content: &str, range_str: &str) -> String {
496    let lines: Vec<&str> = content.lines().collect();
497    let total = lines.len();
498    let mut selected = Vec::new();
499
500    for part in range_str.split(',') {
501        let part = part.trim();
502        if let Some((start_s, end_s)) = part.split_once('-') {
503            let start = start_s.trim().parse::<usize>().unwrap_or(1).max(1);
504            let end = end_s.trim().parse::<usize>().unwrap_or(total).min(total);
505            for i in start..=end {
506                if i >= 1 && i <= total {
507                    selected.push(format!("{i:>4}| {}", lines[i - 1]));
508                }
509            }
510        } else if let Ok(n) = part.parse::<usize>() {
511            if n >= 1 && n <= total {
512                selected.push(format!("{n:>4}| {}", lines[n - 1]));
513            }
514        }
515    }
516
517    if selected.is_empty() {
518        "No lines matched the range.".to_string()
519    } else {
520        selected.join("\n")
521    }
522}
523
524fn handle_diff(cache: &mut SessionCache, path: &str, file_ref: &str) -> String {
525    let short = protocol::shorten_path(path);
526    let old_content = cache.get(path).map(|e| e.content.clone());
527
528    let new_content = match read_file_lossy(path) {
529        Ok(c) => c,
530        Err(e) => return format!("ERROR: {e}"),
531    };
532
533    let original_tokens = count_tokens(&new_content);
534
535    let diff_output = if let Some(old) = &old_content {
536        compressor::diff_content(old, &new_content)
537    } else {
538        format!("[first read]\n{new_content}")
539    };
540
541    cache.store(path, new_content);
542
543    let sent = count_tokens(&diff_output);
544    let savings = protocol::format_savings(original_tokens, sent);
545    format!("{file_ref}={short} [diff]\n{diff_output}\n{savings}")
546}
547
548#[cfg(test)]
549mod tests {
550    use super::*;
551
552    #[test]
553    fn test_header_toon_format_no_brackets() {
554        let content = "use std::io;\nfn main() {}\n";
555        let header = build_header("F1", "main.rs", "rs", content, 2, false);
556        assert!(!header.contains('['));
557        assert!(!header.contains(']'));
558        assert!(header.contains("F1=main.rs 2L"));
559    }
560
561    #[test]
562    fn test_header_toon_deps_indented() {
563        let content = "use crate::core::cache;\nuse crate::tools;\npub fn main() {}\n";
564        let header = build_header("F1", "main.rs", "rs", content, 3, true);
565        if header.contains("deps") {
566            assert!(
567                header.contains("\n deps "),
568                "deps should use indented TOON format"
569            );
570            assert!(
571                !header.contains("deps:["),
572                "deps should not use bracket format"
573            );
574        }
575    }
576
577    #[test]
578    fn test_header_toon_saves_tokens() {
579        let content = "use crate::foo;\nuse crate::bar;\npub fn baz() {}\npub fn qux() {}\n";
580        let old_header = "F1=main.rs [4L +] deps:[foo,bar] exports:[baz,qux]".to_string();
581        let new_header = build_header("F1", "main.rs", "rs", content, 4, true);
582        let old_tokens = count_tokens(&old_header);
583        let new_tokens = count_tokens(&new_header);
584        assert!(
585            new_tokens <= old_tokens,
586            "TOON header ({new_tokens} tok) should be <= old format ({old_tokens} tok)"
587        );
588    }
589
590    #[test]
591    fn test_tdd_symbols_are_compact() {
592        let symbols = [
593            "⊕", "⊖", "∆", "→", "⇒", "✓", "✗", "⚠", "λ", "§", "∂", "τ", "ε",
594        ];
595        for sym in &symbols {
596            let tok = count_tokens(sym);
597            assert!(tok <= 2, "Symbol {sym} should be 1-2 tokens, got {tok}");
598        }
599    }
600
601    #[test]
602    fn test_task_mode_filters_content() {
603        let content = (0..200)
604            .map(|i| {
605                if i % 20 == 0 {
606                    format!("fn validate_token(token: &str) -> bool {{ /* line {i} */ }}")
607                } else {
608                    format!("fn unrelated_helper_{i}(x: i32) -> i32 {{ x + {i} }}")
609                }
610            })
611            .collect::<Vec<_>>()
612            .join("\n");
613        let full_tokens = count_tokens(&content);
614        let task = Some("fix bug in validate_token");
615        let result = process_mode(
616            &content,
617            "task",
618            "F1",
619            "test.rs",
620            "rs",
621            full_tokens,
622            CrpMode::Off,
623            "test.rs",
624            task,
625        );
626        let result_tokens = count_tokens(&result);
627        assert!(
628            result_tokens < full_tokens,
629            "task mode ({result_tokens} tok) should be less than full ({full_tokens} tok)"
630        );
631        assert!(
632            result.contains("task-filtered"),
633            "output should contain task-filtered marker"
634        );
635    }
636
637    #[test]
638    fn test_task_mode_without_task_returns_full() {
639        let content = "fn main() {}\nfn helper() {}\n";
640        let tokens = count_tokens(content);
641        let result = process_mode(
642            content,
643            "task",
644            "F1",
645            "test.rs",
646            "rs",
647            tokens,
648            CrpMode::Off,
649            "test.rs",
650            None,
651        );
652        assert!(
653            result.contains("no task set"),
654            "should indicate no task: {result}"
655        );
656    }
657
658    #[test]
659    fn test_reference_mode_one_line() {
660        let content = "fn main() {}\nfn helper() {}\nfn other() {}\n";
661        let tokens = count_tokens(content);
662        let result = process_mode(
663            content,
664            "reference",
665            "F1",
666            "test.rs",
667            "rs",
668            tokens,
669            CrpMode::Off,
670            "test.rs",
671            None,
672        );
673        let lines: Vec<&str> = result.lines().collect();
674        assert!(
675            lines.len() <= 3,
676            "reference mode should be very compact, got {} lines",
677            lines.len()
678        );
679        assert!(result.contains("lines"), "should contain line count");
680        assert!(result.contains("tok"), "should contain token count");
681    }
682
683    #[test]
684    fn benchmark_task_conditioned_compression() {
685        let content = generate_benchmark_code(500);
686        let full_tokens = count_tokens(&content);
687        let task = Some("fix authentication in validate_token");
688
689        let full_output = process_mode(
690            &content,
691            "full",
692            "F1",
693            "server.rs",
694            "rs",
695            full_tokens,
696            CrpMode::Off,
697            "server.rs",
698            task,
699        );
700        let task_output = process_mode(
701            &content,
702            "task",
703            "F1",
704            "server.rs",
705            "rs",
706            full_tokens,
707            CrpMode::Off,
708            "server.rs",
709            task,
710        );
711        let sig_output = process_mode(
712            &content,
713            "signatures",
714            "F1",
715            "server.rs",
716            "rs",
717            full_tokens,
718            CrpMode::Off,
719            "server.rs",
720            task,
721        );
722        let ref_output = process_mode(
723            &content,
724            "reference",
725            "F1",
726            "server.rs",
727            "rs",
728            full_tokens,
729            CrpMode::Off,
730            "server.rs",
731            task,
732        );
733
734        let full_tok = count_tokens(&full_output);
735        let task_tok = count_tokens(&task_output);
736        let sig_tok = count_tokens(&sig_output);
737        let ref_tok = count_tokens(&ref_output);
738
739        eprintln!("\n=== Task-Conditioned Compression Benchmark ===");
740        eprintln!("Source: 500-line Rust file, task='fix authentication in validate_token'");
741        eprintln!("  full:       {full_tok:>6} tokens (baseline)");
742        eprintln!(
743            "  task:       {task_tok:>6} tokens ({:.0}% savings)",
744            (1.0 - task_tok as f64 / full_tok as f64) * 100.0
745        );
746        eprintln!(
747            "  signatures: {sig_tok:>6} tokens ({:.0}% savings)",
748            (1.0 - sig_tok as f64 / full_tok as f64) * 100.0
749        );
750        eprintln!(
751            "  reference:  {ref_tok:>6} tokens ({:.0}% savings)",
752            (1.0 - ref_tok as f64 / full_tok as f64) * 100.0
753        );
754        eprintln!("================================================\n");
755
756        assert!(task_tok < full_tok, "task mode should save tokens");
757        assert!(sig_tok < full_tok, "signatures should save tokens");
758        assert!(ref_tok < sig_tok, "reference should be most compact");
759    }
760
761    fn generate_benchmark_code(lines: usize) -> String {
762        let mut code = Vec::with_capacity(lines);
763        code.push("use std::collections::HashMap;".to_string());
764        code.push("use crate::core::auth;".to_string());
765        code.push(String::new());
766        code.push("pub struct Server {".to_string());
767        code.push("    config: Config,".to_string());
768        code.push("    cache: HashMap<String, String>,".to_string());
769        code.push("}".to_string());
770        code.push(String::new());
771        code.push("impl Server {".to_string());
772        code.push(
773            "    pub fn validate_token(&self, token: &str) -> Result<Claims, AuthError> {"
774                .to_string(),
775        );
776        code.push("        let decoded = auth::decode_jwt(token)?;".to_string());
777        code.push("        if decoded.exp < chrono::Utc::now().timestamp() {".to_string());
778        code.push("            return Err(AuthError::Expired);".to_string());
779        code.push("        }".to_string());
780        code.push("        Ok(decoded.claims)".to_string());
781        code.push("    }".to_string());
782        code.push(String::new());
783
784        let remaining = lines.saturating_sub(code.len());
785        for i in 0..remaining {
786            if i % 30 == 0 {
787                code.push(format!(
788                    "    pub fn handler_{i}(&self, req: Request) -> Response {{"
789                ));
790            } else if i % 30 == 29 {
791                code.push("    }".to_string());
792            } else {
793                code.push(format!("        let val_{i} = self.cache.get(\"key_{i}\").unwrap_or(&\"default\".to_string());"));
794            }
795        }
796        code.push("}".to_string());
797        code.join("\n")
798    }
799}