Skip to main content

lean_ctx/core/
task_relevance.rs

1use std::collections::{HashMap, HashSet};
2
3use super::graph_provider::{EdgeInfo, GraphProvider};
4
5#[derive(Debug, Clone)]
6pub struct RelevanceScore {
7    pub path: String,
8    pub score: f64,
9    pub recommended_mode: &'static str,
10}
11
12pub fn compute_relevance(
13    gp: &GraphProvider,
14    task_files: &[String],
15    task_keywords: &[String],
16) -> Vec<RelevanceScore> {
17    let all_edges = gp.edges();
18    let file_set: HashSet<String> = gp.file_paths().into_iter().collect();
19    let adj = build_adjacency_resolved(&all_edges, &file_set);
20    let all_nodes: Vec<String> = file_set.into_iter().collect();
21    if all_nodes.is_empty() {
22        return Vec::new();
23    }
24
25    let node_idx: HashMap<&str, usize> = all_nodes
26        .iter()
27        .enumerate()
28        .map(|(i, n)| (n.as_str(), i))
29        .collect();
30    let n = all_nodes.len();
31
32    // Build degree-normalized adjacency for heat diffusion
33    let degrees: Vec<f64> = all_nodes
34        .iter()
35        .map(|node| {
36            adj.get(node)
37                .map_or(0.0, |neigh| neigh.len() as f64)
38                .max(1.0)
39        })
40        .collect();
41
42    // Seed vector: task files get 1.0
43    let mut heat: Vec<f64> = vec![0.0; n];
44    for f in task_files {
45        if let Some(&idx) = node_idx.get(f.as_str()) {
46            heat[idx] = 1.0;
47        }
48    }
49
50    // Heat diffusion: h(t+1) = (1-alpha)*h(t) + alpha * A_norm * h(t)
51    // Run for k iterations
52    let alpha = 0.5;
53    let iterations = 4;
54    for _ in 0..iterations {
55        let mut new_heat = vec![0.0; n];
56        for (i, node) in all_nodes.iter().enumerate() {
57            let self_term = (1.0 - alpha) * heat[i];
58            let mut neighbor_sum = 0.0;
59            if let Some(neighbors) = adj.get(node) {
60                for neighbor in neighbors {
61                    if let Some(&j) = node_idx.get(neighbor.as_str()) {
62                        neighbor_sum += heat[j] / degrees[j];
63                    }
64                }
65            }
66            new_heat[i] = self_term + alpha * neighbor_sum;
67        }
68        heat = new_heat;
69    }
70
71    // PageRank centrality for gateway detection
72    let mut pagerank = vec![1.0 / n as f64; n];
73    let damping = 0.85;
74    for _ in 0..8 {
75        let mut new_pr = vec![(1.0 - damping) / n as f64; n];
76        for (i, node) in all_nodes.iter().enumerate() {
77            if let Some(neighbors) = adj.get(node) {
78                let out_deg = neighbors.len().max(1) as f64;
79                for neighbor in neighbors {
80                    if let Some(&j) = node_idx.get(neighbor.as_str()) {
81                        new_pr[j] += damping * pagerank[i] / out_deg;
82                    }
83                }
84            }
85        }
86        pagerank = new_pr;
87    }
88
89    // Combine: heat (primary) + pagerank centrality (gateway bonus)
90    let mut scores: HashMap<String, f64> = HashMap::new();
91    let heat_max = heat.iter().copied().fold(0.0_f64, f64::max).max(1e-10);
92    let pr_max = pagerank.iter().copied().fold(0.0_f64, f64::max).max(1e-10);
93
94    for (i, node) in all_nodes.iter().enumerate() {
95        let h = heat[i] / heat_max;
96        let pr = pagerank[i] / pr_max;
97        let combined = h * 0.8 + pr * 0.2;
98        if combined > 0.01 {
99            scores.insert(node.clone(), combined);
100        }
101    }
102
103    if !task_keywords.is_empty() {
104        let kw_lower: Vec<String> = task_keywords.iter().map(|k| k.to_lowercase()).collect();
105        for file_path in &all_nodes {
106            let path_lower = file_path.to_lowercase();
107            let mut keyword_hits = 0;
108            for kw in &kw_lower {
109                if path_lower.contains(kw) {
110                    keyword_hits += 1;
111                }
112                if let Some(entry) = gp.get_file_entry(file_path) {
113                    for export in &entry.exports {
114                        if export.to_lowercase().contains(kw) {
115                            keyword_hits += 1;
116                        }
117                    }
118                }
119            }
120            if keyword_hits > 0 {
121                let boost = (keyword_hits as f64 * 0.15).min(0.6);
122                let entry = scores.entry(file_path.clone()).or_insert(0.0);
123                *entry = (*entry + boost).min(1.0);
124            }
125        }
126    }
127
128    let mut result: Vec<RelevanceScore> = scores
129        .into_iter()
130        .map(|(path, score)| {
131            let mode = recommend_mode(score);
132            RelevanceScore {
133                path,
134                score,
135                recommended_mode: mode,
136            }
137        })
138        .collect();
139
140    result.sort_by(|a, b| {
141        b.score
142            .partial_cmp(&a.score)
143            .unwrap_or(std::cmp::Ordering::Equal)
144    });
145    result
146}
147
148pub fn compute_relevance_from_intent(
149    gp: &GraphProvider,
150    intent: &super::intent_engine::StructuredIntent,
151) -> Vec<RelevanceScore> {
152    use super::intent_engine::IntentScope;
153
154    let mut file_seeds: Vec<String> = Vec::new();
155    let mut extra_keywords: Vec<String> = intent.keywords.clone();
156
157    let file_paths = gp.file_paths();
158    for target in &intent.targets {
159        if target.contains('.') || target.contains('/') {
160            let matched = resolve_target_to_files(&file_paths, target);
161            if matched.is_empty() {
162                extra_keywords.push(target.clone());
163            } else {
164                file_seeds.extend(matched);
165            }
166        } else {
167            let from_symbol = resolve_symbol_to_files(gp, target);
168            if from_symbol.is_empty() {
169                extra_keywords.push(target.clone());
170            } else {
171                file_seeds.extend(from_symbol);
172            }
173        }
174    }
175
176    if let Some(lang) = &intent.language_hint {
177        let lang_ext = match lang.as_str() {
178            "rust" => Some("rs"),
179            "typescript" => Some("ts"),
180            "javascript" => Some("js"),
181            "python" => Some("py"),
182            "go" => Some("go"),
183            "ruby" => Some("rb"),
184            "java" => Some("java"),
185            _ => None,
186        };
187        if let Some(ext) = lang_ext
188            && file_seeds.is_empty()
189        {
190            for path in &file_paths {
191                if path.ends_with(&format!(".{ext}")) {
192                    extra_keywords.push(
193                        std::path::Path::new(path)
194                            .file_stem()
195                            .and_then(|s| s.to_str())
196                            .unwrap_or("")
197                            .to_string(),
198                    );
199                    break;
200                }
201            }
202        }
203    }
204
205    let mut result = compute_relevance(gp, &file_seeds, &extra_keywords);
206
207    match intent.scope {
208        IntentScope::SingleFile => {
209            result.truncate(5);
210        }
211        IntentScope::MultiFile => {
212            result.truncate(15);
213        }
214        IntentScope::CrossModule | IntentScope::ProjectWide => {}
215    }
216
217    result
218}
219
220fn resolve_target_to_files(file_paths: &[String], target: &str) -> Vec<String> {
221    file_paths
222        .iter()
223        .filter(|path| path.ends_with(target) || path.contains(target))
224        .cloned()
225        .collect()
226}
227
228fn resolve_symbol_to_files(gp: &GraphProvider, symbol: &str) -> Vec<String> {
229    let found = gp.find_symbols(symbol, None, None);
230    let mut matches: Vec<String> = found
231        .into_iter()
232        .map(|s| s.file)
233        .collect::<HashSet<_>>()
234        .into_iter()
235        .collect();
236    if matches.is_empty() {
237        let sym_lower = symbol.to_lowercase();
238        for path in gp.file_paths() {
239            if let Some(entry) = gp.get_file_entry(&path)
240                && entry
241                    .exports
242                    .iter()
243                    .any(|e| e.to_lowercase().contains(&sym_lower))
244                && !matches.contains(&path)
245            {
246                matches.push(path);
247            }
248        }
249    }
250    matches
251}
252
253fn recommend_mode(score: f64) -> &'static str {
254    if score >= 0.8 {
255        "full"
256    } else if score >= 0.5 {
257        "signatures"
258    } else if score >= 0.2 {
259        "map"
260    } else {
261        "reference"
262    }
263}
264
265fn build_adjacency_resolved(
266    edges: &[EdgeInfo],
267    file_set: &HashSet<String>,
268) -> HashMap<String, Vec<String>> {
269    let file_paths_vec: Vec<&str> = file_set.iter().map(String::as_str).collect();
270    let module_to_file = build_module_map(edges, file_set, &file_paths_vec);
271    let mut adj: HashMap<String, Vec<String>> = HashMap::new();
272
273    for edge in edges {
274        let from = &edge.from;
275        let to_resolved = module_to_file
276            .get(&edge.to)
277            .cloned()
278            .unwrap_or_else(|| edge.to.clone());
279
280        if file_set.contains(from) && file_set.contains(&to_resolved) {
281            adj.entry(from.clone())
282                .or_default()
283                .push(to_resolved.clone());
284            adj.entry(to_resolved).or_default().push(from.clone());
285        }
286    }
287    adj
288}
289
290fn build_module_map(
291    edges: &[EdgeInfo],
292    file_set: &HashSet<String>,
293    file_paths: &[&str],
294) -> HashMap<String, String> {
295    let mut mapping: HashMap<String, String> = HashMap::new();
296
297    let edge_targets: HashSet<String> = edges.iter().map(|e| e.to.clone()).collect();
298
299    for target in &edge_targets {
300        if file_set.contains(target) {
301            mapping.insert(target.clone(), target.clone());
302            continue;
303        }
304
305        if let Some(resolved) = resolve_module_to_file(target, file_paths) {
306            mapping.insert(target.clone(), resolved);
307        }
308    }
309
310    mapping
311}
312
313fn resolve_module_to_file(module_path: &str, file_paths: &[&str]) -> Option<String> {
314    let cleaned = module_path
315        .trim_start_matches("crate::")
316        .trim_start_matches("super::");
317
318    // Strip trailing symbol (e.g. `core::tokens::count_tokens` → `core::tokens`)
319    let parts: Vec<&str> = cleaned.split("::").collect();
320
321    // Try progressively shorter prefixes to find a matching file
322    for end in (1..=parts.len()).rev() {
323        let candidate = parts[..end].join("/");
324
325        // Try as .rs file
326        for fp in file_paths {
327            let fp_normalized = fp
328                .trim_start_matches("rust/src/")
329                .trim_start_matches("src/");
330
331            if fp_normalized == format!("{candidate}.rs")
332                || fp_normalized == format!("{candidate}/mod.rs")
333                || fp.ends_with(&format!("/{candidate}.rs"))
334                || fp.ends_with(&format!("/{candidate}/mod.rs"))
335            {
336                return Some(fp.to_string());
337            }
338        }
339    }
340
341    // Fallback: match by last segment as filename stem
342    if let Some(last) = parts.last() {
343        let stem = format!("{last}.rs");
344        for fp in file_paths {
345            if fp.ends_with(&stem) {
346                return Some(fp.to_string());
347            }
348        }
349    }
350
351    None
352}
353
354/// Extract likely task-relevant file paths and keywords from a task description.
355pub fn parse_task_hints(task_description: &str) -> (Vec<String>, Vec<String>) {
356    let mut files = Vec::new();
357    let mut keywords = Vec::new();
358
359    for word in task_description.split_whitespace() {
360        let clean = word.trim_matches(|c: char| {
361            !c.is_alphanumeric() && c != '.' && c != '/' && c != '_' && c != '-'
362        });
363        if clean.contains('.') && {
364            let p = std::path::Path::new(clean);
365            clean.contains('/')
366                || p.extension().is_some_and(|e| {
367                    e.eq_ignore_ascii_case("rs")
368                        || e.eq_ignore_ascii_case("ts")
369                        || e.eq_ignore_ascii_case("py")
370                        || e.eq_ignore_ascii_case("go")
371                        || e.eq_ignore_ascii_case("js")
372                })
373        } {
374            files.push(clean.to_string());
375        } else if clean.len() >= 3 && !STOP_WORDS.contains(&clean.to_lowercase().as_str()) {
376            keywords.push(clean.to_string());
377        }
378    }
379
380    (files, keywords)
381}
382
383const STOP_WORDS: &[&str] = &[
384    "the", "and", "for", "that", "this", "with", "from", "have", "has", "was", "are", "been",
385    "not", "but", "all", "can", "had", "her", "one", "our", "out", "you", "its", "will", "each",
386    "make", "like", "fix", "add", "use", "get", "set", "run", "new", "old", "should", "would",
387    "could", "into", "also", "than", "them", "then", "when", "just", "only", "very", "some",
388    "more", "other", "nach", "und", "die", "der", "das", "ist", "ein", "eine", "nicht", "auf",
389    "mit",
390];
391
392struct StructuralWeights {
393    error_handling: f64,
394    definition: f64,
395    control_flow: f64,
396    closing_brace: f64,
397    other: f64,
398}
399
400impl StructuralWeights {
401    const DEFAULT: Self = Self {
402        error_handling: 1.5,
403        definition: 1.0,
404        control_flow: 0.5,
405        closing_brace: 0.15,
406        other: 0.3,
407    };
408
409    fn for_task_type(task_type: Option<super::intent_engine::TaskType>) -> Self {
410        use super::intent_engine::TaskType;
411        match task_type {
412            Some(TaskType::FixBug) => Self {
413                error_handling: 2.0,
414                definition: 0.8,
415                control_flow: 0.8,
416                closing_brace: 0.1,
417                other: 0.2,
418            },
419            Some(TaskType::Debug) => Self {
420                error_handling: 2.0,
421                definition: 0.6,
422                control_flow: 1.0,
423                closing_brace: 0.1,
424                other: 0.2,
425            },
426            Some(TaskType::Generate) => Self {
427                error_handling: 0.8,
428                definition: 1.5,
429                control_flow: 0.3,
430                closing_brace: 0.15,
431                other: 0.4,
432            },
433            Some(TaskType::Refactor) => Self {
434                error_handling: 1.0,
435                definition: 1.5,
436                control_flow: 0.6,
437                closing_brace: 0.2,
438                other: 0.3,
439            },
440            Some(TaskType::Test) => Self {
441                error_handling: 1.2,
442                definition: 1.3,
443                control_flow: 0.4,
444                closing_brace: 0.15,
445                other: 0.3,
446            },
447            Some(TaskType::Review) => Self {
448                error_handling: 1.3,
449                definition: 1.2,
450                control_flow: 0.6,
451                closing_brace: 0.15,
452                other: 0.3,
453            },
454            None | Some(TaskType::Explore | _) => Self::DEFAULT,
455        }
456    }
457}
458
459/// Information Bottleneck filter v3 — Mutual Information scoring, QUITO-X inspired.
460///
461/// IB principle: maximize I(T;Y) (task relevance) while minimizing I(T;X) (input redundancy).
462/// v3: MI(line, task) approximated via token overlap + IDF weighting + structural importance.
463///
464/// Key changes from v2:
465///   - Mutual Information scoring: MI(line, task) = H(line) - H(line|task)
466///   - Adaptive budget allocation based on task type via TaskClassifier
467///   - Token-level IDF computed over full document for better term weighting
468///   - Maintains L-curve attention, MMR dedup, error-handling priority from v2
469pub fn information_bottleneck_filter(
470    content: &str,
471    task_keywords: &[String],
472    budget_ratio: f64,
473    force_keep: &[String],
474) -> String {
475    information_bottleneck_filter_typed(content, task_keywords, budget_ratio, None, force_keep)
476}
477
478/// #840: IB filter with optional markdown section-header preservation.
479/// When `preserve_section_headers` is true, the nearest preceding `#`-header
480/// for each selected line is injected into the output so fragments remain
481/// attributable to their enclosing sections.
482pub fn information_bottleneck_filter_with_headers(
483    content: &str,
484    task_keywords: &[String],
485    budget_ratio: f64,
486    force_keep: &[String],
487    preserve_section_headers: bool,
488) -> String {
489    if !preserve_section_headers {
490        return information_bottleneck_filter(content, task_keywords, budget_ratio, force_keep);
491    }
492    let filtered = information_bottleneck_filter(content, task_keywords, budget_ratio, force_keep);
493    inject_section_headers(content, &filtered)
494}
495
496/// For each line in `filtered` that appears in a markdown-sectioned `original`,
497/// find and inject the nearest preceding `#`-header line if not already present.
498fn inject_section_headers(original: &str, filtered: &str) -> String {
499    let orig_lines: Vec<&str> = original.lines().collect();
500    let filt_lines: Vec<&str> = filtered.lines().collect();
501
502    let header_indices: Vec<usize> = orig_lines
503        .iter()
504        .enumerate()
505        .filter(|(_, l)| l.starts_with('#'))
506        .map(|(i, _)| i)
507        .collect();
508
509    if header_indices.is_empty() {
510        return filtered.to_string();
511    }
512
513    let mut result_lines: Vec<&str> = Vec::with_capacity(filt_lines.len() + header_indices.len());
514    let mut injected_headers: std::collections::HashSet<usize> = std::collections::HashSet::new();
515
516    for filt_line in &filt_lines {
517        let trimmed = filt_line.trim();
518        if trimmed.is_empty() || trimmed.starts_with("[task:") {
519            result_lines.push(filt_line);
520            continue;
521        }
522        if let Some(orig_idx) = orig_lines.iter().position(|l| *l == *filt_line)
523            && let Some(&h) = header_indices.iter().rev().find(|&&h| h <= orig_idx)
524            && !injected_headers.contains(&h)
525            && !filt_lines.contains(&orig_lines[h])
526        {
527            injected_headers.insert(h);
528            result_lines.push(orig_lines[h]);
529        }
530        result_lines.push(filt_line);
531    }
532
533    result_lines.join("\n")
534}
535
536/// Task-type-aware IB filter. Uses `TaskType` to adjust structural weights.
537/// `force_keep` lines (explicit `protect` tokens, #709) are kept verbatim on top
538/// of the budget; `&[]` reproduces the pre-protect output byte-for-byte (#498).
539pub fn information_bottleneck_filter_typed(
540    content: &str,
541    task_keywords: &[String],
542    budget_ratio: f64,
543    task_type: Option<super::intent_engine::TaskType>,
544    force_keep: &[String],
545) -> String {
546    let lines: Vec<&str> = content.lines().collect();
547    if lines.is_empty() {
548        return String::new();
549    }
550
551    let n = lines.len();
552    let kw_lower: Vec<String> = task_keywords.iter().map(|k| k.to_lowercase()).collect();
553
554    let mut global_token_freq: HashMap<&str, usize> = HashMap::new();
555    for line in &lines {
556        for token in line.split_whitespace() {
557            *global_token_freq.entry(token).or_insert(0) += 1;
558        }
559    }
560    let total_unique = global_token_freq.len().max(1) as f64;
561    let total_lines = n.max(1) as f64;
562
563    let task_token_set: HashSet<String> = kw_lower
564        .iter()
565        .flat_map(|kw| kw.split(|c: char| !c.is_alphanumeric()).map(String::from))
566        .filter(|t| t.len() >= 2)
567        .collect();
568
569    let effective_ratio = if task_token_set.is_empty() {
570        budget_ratio
571    } else {
572        adaptive_ib_budget(content, budget_ratio)
573    };
574
575    let weights = StructuralWeights::for_task_type(task_type);
576
577    let mut scored_lines: Vec<(usize, &str, f64)> = lines
578        .iter()
579        .enumerate()
580        .map(|(i, line)| {
581            let trimmed = line.trim();
582            if trimmed.is_empty() {
583                return (i, *line, 0.05);
584            }
585
586            let line_lower = trimmed.to_lowercase();
587            let line_tokens: Vec<&str> = trimmed.split_whitespace().collect();
588            let line_token_count = line_tokens.len().max(1) as f64;
589
590            let mi_score = if task_token_set.is_empty() {
591                0.0
592            } else {
593                let line_token_set: HashSet<String> =
594                    line_tokens.iter().map(|t| t.to_lowercase()).collect();
595                let overlap: f64 = line_token_set
596                    .iter()
597                    .filter(|t| task_token_set.iter().any(|kw| t.contains(kw.as_str())))
598                    .map(|t| {
599                        let freq = *global_token_freq.get(t.as_str()).unwrap_or(&1) as f64;
600                        (total_lines / freq).ln().max(0.1)
601                    })
602                    .sum();
603                overlap / line_token_count
604            };
605
606            let keyword_hits: f64 = kw_lower
607                .iter()
608                .filter(|kw| line_lower.contains(kw.as_str()))
609                .count() as f64;
610
611            let structural = if is_error_handling(trimmed) {
612                weights.error_handling
613            } else if is_definition_line(trimmed) {
614                weights.definition
615            } else if is_control_flow(trimmed) {
616                weights.control_flow
617            } else if is_closing_brace(trimmed) {
618                weights.closing_brace
619            } else {
620                weights.other
621            };
622            let relevance = mi_score * 0.4 + keyword_hits * 0.3 + structural;
623
624            let unique_in_line = line_tokens.iter().collect::<HashSet<_>>().len() as f64;
625            let token_diversity = unique_in_line / line_token_count;
626
627            let avg_idf: f64 = if line_tokens.is_empty() {
628                0.0
629            } else {
630                line_tokens
631                    .iter()
632                    .map(|t| {
633                        let freq = *global_token_freq.get(t).unwrap_or(&1) as f64;
634                        (total_unique / freq).ln().max(0.0)
635                    })
636                    .sum::<f64>()
637                    / line_token_count
638            };
639            let information = (token_diversity * 0.4 + (avg_idf.min(3.0) / 3.0) * 0.6).min(1.0);
640
641            let pos = i as f64 / n.max(1) as f64;
642            let attn_weight = 1.0 - (pos - 0.5).abs() * 0.5;
643
644            let score = (relevance * 0.6 + 0.05)
645                * (information * 0.25 + 0.05)
646                * (attn_weight * 0.15 + 0.05);
647
648            // Explicit protect tokens (#709) force the line to the top of the
649            // ranking; INF survives the MMR lambda penalty so it is always kept.
650            let score = if super::protect::line_is_protected(line, force_keep) {
651                f64::INFINITY
652            } else {
653                score
654            };
655
656            (i, *line, score)
657        })
658        .collect();
659
660    // Protected lines (#709) are kept on top of the ranked budget, so widen the
661    // budget to hold them without displacing other selected content. With an
662    // empty `force_keep` this adds zero and the budget is byte-identical (#498).
663    let protected_count = lines
664        .iter()
665        .filter(|l| super::protect::line_is_protected(l, force_keep))
666        .count();
667    let budget = (((n as f64) * effective_ratio).ceil() as usize) + protected_count;
668
669    scored_lines.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
670
671    let selected = mmr_select(&scored_lines, budget, 0.3);
672
673    let mut output_lines: Vec<&str> = Vec::with_capacity(budget + 1);
674
675    if !kw_lower.is_empty() {
676        output_lines.push("");
677    }
678
679    for (_, line, _) in &selected {
680        output_lines.push(line);
681    }
682
683    if !kw_lower.is_empty() {
684        let summary = format!("[task: {}]", task_keywords.join(", "));
685        let mut result = summary;
686        result.push('\n');
687        result.push_str(&output_lines[1..].to_vec().join("\n"));
688        return result;
689    }
690
691    output_lines.join("\n")
692}
693
694/// Maximum Marginal Relevance selection — greedy selection that penalizes
695/// redundancy with already-selected lines using token-set Jaccard similarity.
696///
697/// MMR(i) = relevance(i) - lambda * max_{j in S} jaccard(i, j)
698fn mmr_select<'a>(
699    candidates: &[(usize, &'a str, f64)],
700    budget: usize,
701    lambda: f64,
702) -> Vec<(usize, &'a str, f64)> {
703    if candidates.is_empty() || budget == 0 {
704        return Vec::new();
705    }
706
707    let mut selected: Vec<(usize, &'a str, f64)> = Vec::with_capacity(budget);
708    let mut remaining: Vec<(usize, &'a str, f64)> = candidates.to_vec();
709
710    // Always take the top-scored line first
711    selected.push(remaining.remove(0));
712
713    while selected.len() < budget && !remaining.is_empty() {
714        let mut best_idx = 0;
715        let mut best_mmr = f64::NEG_INFINITY;
716
717        for (i, &(_, cand_line, cand_score)) in remaining.iter().enumerate() {
718            let cand_tokens: HashSet<&str> = cand_line.split_whitespace().collect();
719            if cand_tokens.is_empty() {
720                if cand_score > best_mmr {
721                    best_mmr = cand_score;
722                    best_idx = i;
723                }
724                continue;
725            }
726
727            let max_sim = selected
728                .iter()
729                .map(|&(_, sel_line, _)| {
730                    let sel_tokens: HashSet<&str> = sel_line.split_whitespace().collect();
731                    if sel_tokens.is_empty() {
732                        return 0.0;
733                    }
734                    let inter = cand_tokens.intersection(&sel_tokens).count();
735                    let union = cand_tokens.union(&sel_tokens).count();
736                    if union == 0 {
737                        0.0
738                    } else {
739                        inter as f64 / union as f64
740                    }
741                })
742                .fold(0.0_f64, f64::max);
743
744            let mmr = cand_score - lambda * max_sim;
745            if mmr > best_mmr {
746                best_mmr = mmr;
747                best_idx = i;
748            }
749        }
750
751        selected.push(remaining.remove(best_idx));
752    }
753
754    selected
755}
756
757fn is_error_handling(line: &str) -> bool {
758    line.starts_with("return Err(")
759        || line.starts_with("Err(")
760        || line.starts_with("bail!(")
761        || line.starts_with("anyhow::bail!")
762        || line.contains(".map_err(")
763        || line.contains("unwrap()")
764        || line.contains("expect(\"")
765        || line.starts_with("raise ")
766        || line.starts_with("throw ")
767        || line.starts_with("catch ")
768        || line.starts_with("except ")
769        || line.starts_with("try ")
770        || (line.contains("?;") && !line.starts_with("//"))
771        || line.starts_with("panic!(")
772        || line.contains("Error::")
773        || line.contains("error!")
774}
775
776/// Compute an adaptive IB budget ratio based on content characteristics.
777/// Highly repetitive content → more aggressive filtering (lower ratio).
778/// High-entropy diverse content → more conservative (higher ratio).
779pub fn adaptive_ib_budget(content: &str, base_ratio: f64) -> f64 {
780    let lines: Vec<&str> = content.lines().collect();
781    if lines.len() < 10 {
782        return 1.0;
783    }
784
785    let mut token_freq: HashMap<&str, usize> = HashMap::new();
786    let mut total_tokens = 0usize;
787    for line in &lines {
788        for token in line.split_whitespace() {
789            *token_freq.entry(token).or_insert(0) += 1;
790            total_tokens += 1;
791        }
792    }
793
794    if total_tokens == 0 {
795        return base_ratio;
796    }
797
798    let unique_ratio = token_freq.len() as f64 / total_tokens as f64;
799    let repetition_factor = 1.0 - unique_ratio;
800
801    (base_ratio * (1.0 - repetition_factor * 0.3)).clamp(0.2, 1.0)
802}
803
804fn is_definition_line(line: &str) -> bool {
805    let prefixes = [
806        "fn ",
807        "pub fn ",
808        "async fn ",
809        "pub async fn ",
810        "struct ",
811        "pub struct ",
812        "enum ",
813        "pub enum ",
814        "trait ",
815        "pub trait ",
816        "impl ",
817        "type ",
818        "pub type ",
819        "const ",
820        "pub const ",
821        "static ",
822        "pub static ",
823        "class ",
824        "export class ",
825        "interface ",
826        "export interface ",
827        "function ",
828        "export function ",
829        "async function ",
830        "def ",
831        "async def ",
832        "func ",
833    ];
834    prefixes
835        .iter()
836        .any(|p| line.starts_with(p) || line.trim_start().starts_with(p))
837}
838
839fn is_control_flow(line: &str) -> bool {
840    let trimmed = line.trim();
841    trimmed.starts_with("if ")
842        || trimmed.starts_with("else ")
843        || trimmed.starts_with("match ")
844        || trimmed.starts_with("for ")
845        || trimmed.starts_with("while ")
846        || trimmed.starts_with("return ")
847        || trimmed.starts_with("break")
848        || trimmed.starts_with("continue")
849        || trimmed.starts_with("yield")
850        || trimmed.starts_with("await ")
851}
852
853fn is_closing_brace(line: &str) -> bool {
854    let trimmed = line.trim();
855    trimmed == "}" || trimmed == "};" || trimmed == "})" || trimmed == "});"
856}
857
858#[cfg(test)]
859mod tests {
860    use super::*;
861
862    #[test]
863    fn parse_task_finds_files_and_keywords() {
864        let (files, keywords) =
865            parse_task_hints("Fix the authentication bug in src/auth.rs and update tests");
866        assert!(files.iter().any(|f| f.contains("auth.rs")));
867        assert!(
868            keywords
869                .iter()
870                .any(|k| k.to_lowercase().contains("authentication"))
871        );
872    }
873
874    #[test]
875    fn recommend_mode_by_score() {
876        assert_eq!(recommend_mode(1.0), "full");
877        assert_eq!(recommend_mode(0.6), "signatures");
878        assert_eq!(recommend_mode(0.3), "map");
879        assert_eq!(recommend_mode(0.1), "reference");
880    }
881
882    #[test]
883    fn info_bottleneck_preserves_definitions() {
884        let content = "fn main() {\n    let x = 42;\n    // boring comment\n    println!(x);\n}\n";
885        let result = information_bottleneck_filter(content, &["main".to_string()], 0.6, &[]);
886        assert!(result.contains("fn main"), "definitions must be preserved");
887        assert!(result.contains("[task: main]"), "should have task summary");
888    }
889
890    #[test]
891    fn protect_force_keeps_line_in_ib() {
892        let content = "fn main() {\n    let x = 1;\n    let unimportant = 2;\n    let y = 3;\n    let z = 4;\n}\n";
893        // Tiny budget → the 'unimportant' line is normally filtered out.
894        let kept = information_bottleneck_filter(
895            content,
896            &["main".to_string()],
897            0.1,
898            &["unimportant".to_string()],
899        );
900        assert!(
901            kept.contains("let unimportant = 2;"),
902            "protected line must survive the IB budget: {kept}"
903        );
904    }
905
906    #[test]
907    fn ib_empty_force_keep_is_byte_identical() {
908        // Protect must not change the unprotected IB output (#498).
909        let content = "fn main() {\n    let x = 1;\n    return Err(\"e\");\n    let y = 2;\n}\n";
910        let a = information_bottleneck_filter(content, &["main".to_string()], 0.5, &[]);
911        let b = information_bottleneck_filter_typed(content, &["main".to_string()], 0.5, None, &[]);
912        assert_eq!(a, b);
913    }
914
915    #[test]
916    fn info_bottleneck_error_handling_priority() {
917        let content = "fn validate() {\n    let data = parse()?;\n    return Err(\"invalid\");\n    let x = 1;\n    let y = 2;\n}\n";
918        let result = information_bottleneck_filter(content, &["validate".to_string()], 0.5, &[]);
919        assert!(
920            result.contains("return Err"),
921            "error handling should survive filtering"
922        );
923    }
924
925    #[test]
926    fn info_bottleneck_score_sorted() {
927        let content = "fn important() {\n    let x = 1;\n    let y = 2;\n    let z = 3;\n}\n}\n";
928        let result = information_bottleneck_filter(content, &[], 0.6, &[]);
929        let lines: Vec<&str> = result.lines().collect();
930        let def_pos = lines.iter().position(|l| l.contains("fn important"));
931        let brace_pos = lines.iter().position(|l| l.trim() == "}");
932        if let (Some(d), Some(b)) = (def_pos, brace_pos) {
933            assert!(
934                d < b,
935                "definitions should appear before closing braces in score-sorted output"
936            );
937        }
938    }
939
940    #[test]
941    fn adaptive_budget_reduces_for_repetitive() {
942        let repetitive = "let x = 1;\n".repeat(50);
943        let diverse = (0..50)
944            .map(|i| format!("let var_{i} = func_{i}(arg_{i});"))
945            .collect::<Vec<_>>()
946            .join("\n");
947        let budget_rep = super::adaptive_ib_budget(&repetitive, 0.7);
948        let budget_div = super::adaptive_ib_budget(&diverse, 0.7);
949        assert!(
950            budget_rep < budget_div,
951            "repetitive content should get lower budget"
952        );
953    }
954}