1use std::collections::HashSet;
2
3use anyhow::Result;
4use serde::{Deserialize, Serialize};
5use tracing::debug;
6
7use reposcry_cache::db::CacheDb;
8use reposcry_git::GitIntegration;
9use reposcry_graph::edge::EdgeKind;
10use reposcry_graph::graph::CodeGraph;
11use reposcry_graph::node::{GraphNode, NodeKind};
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct ContextPack {
15 pub user_task: String,
16 pub relevant_files: Vec<ContextFile>,
17 pub dependency_paths: Vec<String>,
18 pub reverse_dependencies: Vec<ReverseDep>,
19 pub risk_warnings: Vec<String>,
20 pub suggested_read_order: Vec<String>,
21 pub suggested_tests: Vec<String>,
22 pub architecture_rules: Vec<String>,
23 pub implementation_plan: Vec<String>,
24 pub confidence: Confidence,
25 pub strict_warnings: Vec<String>,
26 pub omitted_symbols: Vec<OmittedSymbols>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct OmittedSymbols {
31 pub path: String,
32 pub omitted_count: u32,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct ContextFile {
37 pub path: String,
38 pub reason: String,
39 pub important_symbols: Vec<String>,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ReverseDep {
44 pub path: String,
45 pub used_by: Vec<String>,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
49pub enum Confidence {
50 High,
51 Medium,
52 Low,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct ContextConfig {
57 pub token_budget: u32,
58 pub strict_mode: bool,
59 pub max_files: u32,
60 pub max_reverse_depth: u32,
61 pub include_full_files: bool,
62 pub format: OutputFormat,
63 pub max_symbols_per_file: u32,
64 pub show_omitted: bool,
65}
66
67impl Default for ContextConfig {
68 fn default() -> Self {
69 Self {
70 token_budget: 4_000,
71 strict_mode: false,
72 max_files: 30,
73 max_reverse_depth: 2,
74 include_full_files: false,
75 format: OutputFormat::Human,
76 max_symbols_per_file: 8,
77 show_omitted: false,
78 }
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub enum OutputFormat {
84 Human,
85 Markdown,
86 Json,
87}
88
89impl Default for OutputFormat {
90 fn default() -> Self {
91 Self::Markdown
92 }
93}
94
95pub struct ContextBuilder {
96 graph: CodeGraph,
97 cache: Option<CacheDb>,
98 git: Option<GitIntegration>,
99 config: ContextConfig,
100}
101
102impl ContextBuilder {
103 pub fn new(graph: CodeGraph, config: ContextConfig) -> Self {
104 Self {
105 graph,
106 cache: None,
107 git: None,
108 config,
109 }
110 }
111
112 pub fn with_cache(mut self, cache: CacheDb) -> Self {
113 self.cache = Some(cache);
114 self
115 }
116
117 pub fn with_git(mut self, git: GitIntegration) -> Self {
118 self.git = Some(git);
119 self
120 }
121
122 pub fn build(&self, task: &str) -> Result<ContextPack> {
123 debug!("Building context pack for task: {}", task);
124 let keywords = extract_keywords(task);
125 let backend_keywords = [
126 "backend", "storage", "database", "cache", "provider", "driver", "adapter",
127 "plugin", "feature",
128 ];
129 let task_lower = task.to_lowercase();
130 let is_backend_task = backend_keywords.iter().any(|kw| task_lower.contains(kw));
131
132 let mut matched_files: Vec<ContextFile> = Vec::new();
135 for node in self.graph.nodes.values() {
136 let path = match &node.file_path {
137 Some(p) => p,
138 None => continue,
139 };
140 let relevance = score_relevance(&node.name, path, &keywords);
141 if relevance > 0.0 {
142 matched_files.push(ContextFile {
143 path: path.to_string(),
144 reason: format!("Keyword match (score: {:.2})", relevance),
145 important_symbols: self.symbols_in_file(path, &keywords),
146 });
147 }
148 }
149
150 if let Some(ref cache) = self.cache {
153 for entry in cache.get_all_files()? {
154 if matched_files.iter().any(|f| f.path == entry.path) {
155 continue;
156 }
157 let relevance = score_file_relevance(&entry.path, &keywords, is_backend_task);
158 if relevance > 0.0 {
159 matched_files.push(ContextFile {
160 path: entry.path.clone(),
161 reason: format!("File name match (score: {:.2})", relevance),
162 important_symbols: self.symbols_in_file(&entry.path, &keywords),
163 });
164 }
165 }
166 }
167
168 if is_backend_task {
170 if let Some(ref cache) = self.cache {
171 for entry in cache.get_all_files()? {
172 let path = normalize_path(&entry.path);
173 let is_manifest = path.ends_with("cargo.toml")
174 || path.ends_with("package.json")
175 || path.ends_with("/lib.rs")
176 || path.ends_with("/mod.rs");
177 if is_manifest && !matched_files.iter().any(|f| f.path == entry.path) {
178 let relevance = score_file_relevance(&entry.path, &keywords, true);
179 if relevance > 0.0 {
180 matched_files.push(ContextFile {
181 path: entry.path.clone(),
182 reason: "Manifest/public API for backend/storage task".into(),
183 important_symbols: self.symbols_in_file(&entry.path, &keywords),
184 });
185 }
186 }
187 }
188 }
189 }
190
191 matched_files.sort_by(|a, b| {
193 let a_score = score_file_relevance(&a.path, &keywords, is_backend_task);
194 let b_score = score_file_relevance(&b.path, &keywords, is_backend_task);
195 b_score
196 .partial_cmp(&a_score)
197 .unwrap_or(std::cmp::Ordering::Equal)
198 .then_with(|| a.path.cmp(&b.path))
199 });
200
201 let mut seen_file_paths = HashSet::new();
204 matched_files.retain(|f| seen_file_paths.insert(f.path.clone()));
205
206 let max_syms = self.config.max_symbols_per_file as usize;
208 let mut omitted_list = Vec::new();
209 for file in &mut matched_files {
210 if file.important_symbols.len() > max_syms {
211 let omitted_count = file.important_symbols.len() as u32 - max_syms as u32;
212 file.important_symbols.truncate(max_syms);
213 omitted_list.push(OmittedSymbols {
214 path: file.path.clone(),
215 omitted_count,
216 });
217 }
218 }
219
220 let max_by_budget = (self.config.token_budget / 750).clamp(4, self.config.max_files);
221 matched_files.truncate(max_by_budget as usize);
222
223 let dependency_paths = self.find_dependency_paths(&matched_files);
225 let reverse_dependencies = self.find_reverse_dependencies(&matched_files);
226
227 let suggested_tests = self.find_tests(&matched_files);
229
230 let risk_warnings = self.compute_risk_warnings(&matched_files);
232
233 let matched_paths: HashSet<&str> = matched_files.iter().map(|f| f.path.as_str()).collect();
235 let architecture_rules = self.compute_architecture_rules(&matched_paths);
236 let implementation_plan = self.compute_implementation_plan(task, &matched_files);
237
238 let confidence = if matched_files.is_empty() {
240 Confidence::Low
241 } else if matched_files.len() < 3 {
242 Confidence::Medium
243 } else {
244 Confidence::High
245 };
246
247 let mut strict_warnings = Vec::new();
249 if self.config.strict_mode {
250 if matched_files.is_empty() {
251 strict_warnings.push("No relevant source files found.".into());
252 }
253 if dependency_paths.is_empty() {
254 strict_warnings.push("No dependency paths found.".into());
255 }
256 if reverse_dependencies.is_empty() {
257 strict_warnings.push("No reverse dependency check performed.".into());
258 }
259 if suggested_tests.is_empty() {
260 strict_warnings.push("No test candidates found. Consider adding tests.".into());
261 }
262 if confidence == Confidence::Low {
263 strict_warnings.push(format!(
264 "Confidence is LOW. Consider searching for: {}",
265 keywords.join(", ")
266 ));
267 }
268 }
269
270 let mut suggested_read_order: Vec<String> =
272 matched_files.iter().map(|f| f.path.clone()).collect();
273 suggested_read_order.truncate(10);
274
275 let mut pack = ContextPack {
276 user_task: task.to_string(),
277 relevant_files: matched_files,
278 dependency_paths,
279 reverse_dependencies,
280 risk_warnings,
281 suggested_read_order,
282 suggested_tests,
283 architecture_rules,
284 implementation_plan,
285 confidence,
286 strict_warnings,
287 omitted_symbols: if self.config.show_omitted { omitted_list } else { vec![] },
288 };
289 pack.dedupe();
290 Ok(pack)
291 }
292
293 fn symbols_in_file(&self, path: &str, keywords: &[String]) -> Vec<String> {
294 let mut nodes: Vec<&GraphNode> = self
295 .graph
296 .nodes
297 .values()
298 .filter(|n| n.file_path.as_deref() == Some(path) && n.kind != NodeKind::File)
299 .collect();
300
301 nodes.sort_by(|a, b| {
302 let a_score = symbol_relevance_score(a, keywords);
303 let b_score = symbol_relevance_score(b, keywords);
304 b_score
305 .partial_cmp(&a_score)
306 .unwrap_or(std::cmp::Ordering::Equal)
307 .then_with(|| a.name.cmp(&b.name))
308 });
309
310 nodes.iter().map(|n| Self::format_symbol(n)).collect()
311 }
312
313 fn format_symbol(node: &GraphNode) -> String {
314 let line_range = match (node.start_line, node.end_line) {
315 (Some(start), Some(end)) if end > start => format!(" L{}-{}", start, end),
316 (Some(start), _) => format!(" L{}", start),
317 _ => String::new(),
318 };
319
320 if let Some(signature) = &node.signature {
321 format!(
322 "{}{} - {}",
323 node.name,
324 line_range,
325 Self::ascii_arrow(signature)
326 )
327 } else {
328 format!("{}{} ({})", node.name, line_range, node.kind.as_str())
329 }
330 }
331
332 fn find_dependency_paths(&self, files: &[ContextFile]) -> Vec<String> {
333 let mut paths = Vec::new();
334 for file in files {
335 let mut deps: Vec<String> = self
336 .graph
337 .edges
338 .iter()
339 .filter(|e| e.kind == EdgeKind::Imports)
340 .filter(|e| {
341 self.graph
342 .nodes
343 .get(&e.source_id)
344 .and_then(|n| n.file_path.as_deref())
345 == Some(&file.path)
346 })
347 .filter_map(|e| {
348 self.graph
349 .nodes
350 .get(&e.target_id)
351 .and_then(|n| n.file_path.clone())
352 })
353 .collect();
354 deps.sort();
355 deps.dedup();
356 if !deps.is_empty() {
357 let chain = format!("{} -> {}", file.path, deps.join(" -> "));
358 paths.push(chain);
359 }
360 }
361 paths
362 }
363
364 fn find_reverse_dependencies(&self, files: &[ContextFile]) -> Vec<ReverseDep> {
365 let mut results = Vec::new();
366 for file in files {
367 let mut users: Vec<String> = self
368 .graph
369 .edges
370 .iter()
371 .filter(|e| e.kind == EdgeKind::Imports)
372 .filter(|e| {
373 self.graph
374 .nodes
375 .get(&e.target_id)
376 .and_then(|n| n.file_path.as_deref())
377 == Some(&file.path)
378 })
379 .filter_map(|e| {
380 self.graph
381 .nodes
382 .get(&e.source_id)
383 .and_then(|n| n.file_path.clone())
384 })
385 .collect();
386 users.sort();
387 users.dedup();
388 if !users.is_empty() {
389 results.push(ReverseDep {
390 path: file.path.clone(),
391 used_by: users,
392 });
393 }
394 }
395 results
396 }
397
398 fn find_tests(&self, ctx_files: &[ContextFile]) -> Vec<String> {
399 let matched_crates: HashSet<String> = ctx_files
400 .iter()
401 .filter_map(|f| {
402 normalize_path(&f.path)
403 .split('/')
404 .find(|segment| segment.starts_with("reposcry-"))
405 .map(|c| c.to_string())
406 })
407 .collect();
408
409 let mut tests = Vec::new();
410 let mut crate_names: Vec<String> = matched_crates.iter().cloned().collect();
411 crate_names.sort();
412 for crate_name in &crate_names {
413 tests.push(format!("cargo test -p {}", crate_name));
414 }
415
416 let mut graph_tests: Vec<(String, u32)> = self
417 .graph
418 .nodes
419 .values()
420 .filter(|n| {
421 n.kind == NodeKind::Test
422 && n.file_path.as_deref().map_or(false, |p| p.contains("test"))
423 })
424 .filter_map(|n| {
425 let path = n.file_path.clone()?;
426 let normalized = normalize_path(&path);
427 let crate_match = matched_crates.iter().find(|c| normalized.contains(c.as_str()));
428 let priority: u32 = if crate_match.is_some() {
429 0 } else if normalized.contains("fixtures") || normalized.contains("benchmarks") {
431 2 } else {
433 1 };
435 Some((path, priority))
436 })
437 .collect();
438 graph_tests.sort_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
439
440 for (path, priority) in graph_tests {
441 if priority > 0 && !tests.is_empty() {
442 continue;
443 }
444 tests.push(path);
445 if tests.len() >= 8 {
446 break;
447 }
448 }
449
450 let mut seen_tests = HashSet::new();
451 tests.retain(|t| seen_tests.insert(t.clone()));
452 tests
453 }
454
455 fn compute_risk_warnings(&self, files: &[ContextFile]) -> Vec<String> {
456 let mut seen_paths = HashSet::new();
457 let mut warnings = Vec::new();
458 for file in files {
459 if !seen_paths.insert(file.path.clone()) {
460 continue;
461 }
462 let rdeps = self
463 .graph
464 .edges
465 .iter()
466 .filter(|e| e.kind == EdgeKind::Imports)
467 .filter(|e| {
468 self.graph
469 .nodes
470 .get(&e.target_id)
471 .and_then(|n| n.file_path.as_deref())
472 == Some(&file.path)
473 })
474 .count();
475 if rdeps > 5 {
476 warnings.push(format!(
477 "`{}` has high fan-in ({} dependents). Changes may have wide impact.",
478 file.path, rdeps
479 ));
480 }
481 let deps = self
482 .graph
483 .edges
484 .iter()
485 .filter(|e| e.kind == EdgeKind::Imports)
486 .filter(|e| {
487 self.graph
488 .nodes
489 .get(&e.source_id)
490 .and_then(|n| n.file_path.as_deref())
491 == Some(&file.path)
492 })
493 .count();
494 if deps > 10 {
495 warnings.push(format!(
496 "`{}` has high fan-out ({} dependencies). May be doing too much.",
497 file.path, deps
498 ));
499 }
500 }
501 warnings
502 }
503
504 fn compute_architecture_rules(&self, matched_paths: &HashSet<&str>) -> Vec<String> {
505 let mut rules = Vec::new();
506
507 let has_cache_files = matched_paths.iter().any(|p| p.contains("reposcry-cache"));
508 if has_cache_files {
509 rules.push("Cache backend changes must preserve existing SQLite CacheDb behavior.".into());
510 rules.push("New backends must maintain search index, import, symbol, and call-site semantics.".into());
511 }
512
513 let has_cli_files = matched_paths.iter().any(|p| p.contains("reposcry-cli"));
514 if has_cli_files {
515 rules.push("CLI commands should prefer the crg_cli module for complex output formatting.".into());
516 }
517
518 let has_context_files = matched_paths
519 .iter()
520 .any(|p| p.contains("reposcry-context") || p.contains("reposcry-graph"));
521 if has_context_files {
522 rules.push("Context pack changes must preserve backward compatibility of the ContextPack JSON schema.".into());
523 }
524
525 rules
526 }
527
528 fn compute_implementation_plan(&self, task: &str, files: &[ContextFile]) -> Vec<String> {
529 let task_lower = task.to_lowercase();
530 let paths: Vec<&str> = files.iter().map(|f| f.path.as_str()).collect();
531 let cache_backend_task = task_lower.contains("cache")
532 && (task_lower.contains("backend")
533 || task_lower.contains("storage")
534 || task_lower.contains("database")
535 || task_lower.contains("adapter"));
536
537 if cache_backend_task && paths.iter().any(|p| p.contains("reposcry-cache")) {
538 return vec![
539 "Inspect crates/reposcry-cache/src/lib.rs and crates/reposcry-cache/Cargo.toml before changing the storage API.".into(),
540 "Keep CacheDb as the current SQLite-backed implementation unless introducing a trait preserves all existing public methods.".into(),
541 "Touch constructor/config paths first: CacheDb::open, CacheDb::open_in_memory, CacheDb::initialize, set_config, and get_config.".into(),
542 "Preserve search document/vector, import, symbol, call-site, and edge persistence semantics for the new backend.".into(),
543 "Run cargo test -p reposcry-cache before dependent CLI/context tests.".into(),
544 ];
545 }
546
547 Vec::new()
548 }
549
550 fn ascii_arrow(s: &str) -> String {
551 s.replace('\u{2014}', "-")
552 .replace('\u{2013}', "-")
553 .replace('\u{2192}', "->")
554 .replace('\u{2190}', "<-")
555 .replace('\u{2713}', "[ok]")
556 .replace('\u{2717}', "[no]")
557 .replace('\u{26a0}', "[!]")
558 .replace('\u{2018}', "'")
559 .replace('\u{2019}', "'")
560 .replace('\u{201c}', "\"")
561 .replace('\u{201d}', "\"")
562 }
563
564 pub fn render_markdown(&self, pack: &ContextPack) -> String {
565 let mut md = String::new();
566 md.push_str("# AI Context Pack\n\n");
567 md.push_str("## User task\n\n");
568 md.push_str(&Self::ascii_arrow(&pack.user_task));
569 md.push_str("\n\n");
570 md.push_str("## Relevant files\n\n");
571 for file in &pack.relevant_files {
572 md.push_str(&format!("### {}\n", file.path));
573 md.push_str(&format!("Reason: {}\n", Self::ascii_arrow(&file.reason)));
574 if !file.important_symbols.is_empty() {
575 md.push_str("Important symbols:\n");
576 for sym in &file.important_symbols {
577 md.push_str(&format!("- {}\n", Self::ascii_arrow(sym)));
578 }
579 }
580 md.push('\n');
581 }
582 if !pack.omitted_symbols.is_empty() {
583 md.push_str("## Omitted lower-priority symbols\n\n");
584 for omitted in &pack.omitted_symbols {
585 md.push_str(&format!(
586 "- {}: {} omitted\n",
587 omitted.path, omitted.omitted_count
588 ));
589 }
590 md.push('\n');
591 }
592 if !pack.implementation_plan.is_empty() {
593 md.push_str("## Likely implementation plan\n\n");
594 for (i, step) in pack.implementation_plan.iter().enumerate() {
595 md.push_str(&format!("{}. {}\n", i + 1, Self::ascii_arrow(step)));
596 }
597 md.push('\n');
598 }
599 if !pack.dependency_paths.is_empty() {
600 md.push_str("## Dependency paths\n\n");
601 for path in &pack.dependency_paths {
602 md.push_str(&Self::ascii_arrow(path));
603 md.push('\n');
604 }
605 md.push('\n');
606 }
607 if !pack.reverse_dependencies.is_empty() {
608 md.push_str("## Reverse dependencies\n\n");
609 for rd in &pack.reverse_dependencies {
610 md.push_str(&format!("{} is used by:\n", rd.path));
611 for user in &rd.used_by {
612 md.push_str(&format!("- {}\n", user));
613 }
614 }
615 md.push('\n');
616 }
617 if !pack.risk_warnings.is_empty() {
618 md.push_str("## Risk warnings\n\n");
619 for warning in &pack.risk_warnings {
620 md.push_str(&format!("- {}\n", warning));
621 }
622 md.push('\n');
623 }
624 md.push_str("## Suggested files to read before editing\n\n");
625 for (i, file) in pack.suggested_read_order.iter().enumerate() {
626 md.push_str(&format!("{}. {}\n", i + 1, file));
627 }
628 md.push('\n');
629 if !pack.suggested_tests.is_empty() {
630 md.push_str("## Suggested tests\n\n");
631 for test in &pack.suggested_tests {
632 md.push_str(&format!("- {}\n", test));
633 }
634 md.push('\n');
635 }
636 md.push_str(&format!(
637 "## Confidence\n\n{}\n\n",
638 match pack.confidence {
639 Confidence::High => "HIGH",
640 Confidence::Medium => "MEDIUM",
641 Confidence::Low => "LOW",
642 }
643 ));
644 if !pack.strict_warnings.is_empty() {
645 md.push_str("## Strict mode warnings\n\n");
646 for warning in &pack.strict_warnings {
647 md.push_str(&format!("- {}\n", warning));
648 }
649 md.push('\n');
650 }
651 if !pack.architecture_rules.is_empty() {
652 md.push_str("## Architecture rules\n\n");
653 for rule in &pack.architecture_rules {
654 md.push_str(&format!("- {}\n", Self::ascii_arrow(rule)));
655 }
656 md.push('\n');
657 }
658 md
659 }
660}
661
662impl ContextPack {
663 pub fn dedupe(&mut self) {
665 let mut seen_paths = HashSet::new();
666 self.relevant_files.retain(|f| seen_paths.insert(f.path.clone()));
667
668 for file in &mut self.relevant_files {
669 let mut seen_syms = HashSet::new();
670 file.important_symbols.retain(|s| seen_syms.insert(s.clone()));
671 }
672
673 let mut seen_deps = HashSet::new();
674 self.dependency_paths.retain(|p| seen_deps.insert(p.clone()));
675
676 let mut seen_rd = HashSet::new();
677 self.reverse_dependencies.retain(|rd| seen_rd.insert(rd.path.clone()));
678 for rd in &mut self.reverse_dependencies {
679 let mut seen_users = HashSet::new();
680 rd.used_by.retain(|u| seen_users.insert(u.clone()));
681 }
682
683 let mut seen_warnings = HashSet::new();
684 self.risk_warnings.retain(|w| seen_warnings.insert(w.clone()));
685
686 let mut seen_read = HashSet::new();
687 self.suggested_read_order.retain(|r| seen_read.insert(r.clone()));
688
689 let mut seen_tests = HashSet::new();
690 self.suggested_tests.retain(|t| seen_tests.insert(t.clone()));
691
692 let mut seen_rules = HashSet::new();
693 self.architecture_rules.retain(|r| seen_rules.insert(r.clone()));
694
695 let mut seen_steps = HashSet::new();
696 self.implementation_plan.retain(|s| seen_steps.insert(s.clone()));
697 }
698}
699
700fn extract_keywords(task: &str) -> Vec<String> {
701 let stop_words = [
702 "the", "a", "an", "is", "are", "was", "were", "be", "been", "being", "have",
703 "has", "had", "do", "does", "did", "will", "would", "could", "should", "may",
704 "might", "shall", "can", "fix", "add", "change", "update", "remove", "implement",
705 "make", "get", "set", "new", "this", "that", "these", "those", "to", "of", "in",
706 "for", "on", "with", "at", "by", "from", "as", "into", "about", "after", "before",
707 "between", "under", "and", "but", "or", "nor", "not", "so", "yet", "no", "bug",
708 "feature", "issue", "task", "when",
709 ];
710 let mut words: Vec<String> = task
711 .split(|c: char| !c.is_alphanumeric())
712 .filter(|w| w.len() > 2)
713 .filter(|w| !stop_words.contains(w))
714 .map(|w| w.to_lowercase())
715 .collect();
716 words.sort();
717 words.dedup();
718 words
719}
720
721fn normalize_path(path: &str) -> String {
722 path.replace('\\', "/").to_lowercase()
723}
724
725fn score_file_relevance(path: &str, keywords: &[String], is_backend_task: bool) -> f64 {
726 let lower_path = normalize_path(path);
727 let mut score = score_relevance(path, path, keywords);
728
729 if lower_path.contains("/src/") {
730 score += 1.0;
731 }
732 if lower_path.contains("benchmarks/fixtures") || lower_path.contains("/fixtures/") {
733 score -= 5.0;
734 }
735 if lower_path.contains("target/") || lower_path.contains("node_modules/") {
736 score -= 10.0;
737 }
738
739 if is_backend_task {
740 if lower_path.ends_with("cargo.toml") || lower_path.ends_with("package.json") {
741 score += 3.0;
742 }
743 if lower_path.ends_with("/lib.rs") || lower_path.ends_with("/mod.rs") {
744 score += 2.5;
745 }
746 if lower_path.contains("/db.rs")
747 || lower_path.contains("database")
748 || lower_path.contains("storage")
749 || lower_path.contains("cache")
750 {
751 score += 2.0;
752 }
753 }
754
755 score
756}
757
758fn score_relevance(name: &str, path: &str, keywords: &[String]) -> f64 {
759 let mut score = 0.0;
760 let lower_name = name.to_lowercase();
761 let lower_path = path.to_lowercase();
762 for kw in keywords {
763 let kw_lower = kw.to_lowercase();
764 if lower_name.contains(&kw_lower) {
765 score += 1.0;
766 }
767 if lower_path.contains(&kw_lower) {
768 score += 0.5;
769 }
770 }
771 score
772}
773
774fn symbol_relevance_score(node: &GraphNode, keywords: &[String]) -> f64 {
775 let lower_name = node.name.to_lowercase();
776 let lower_sig = node.signature.as_deref().unwrap_or_default().to_lowercase();
777 let mut score = 0.0;
778
779 for kw in keywords {
780 if lower_name.contains(kw) {
781 score += 2.0;
782 }
783 if lower_sig.contains(kw) {
784 score += 1.0;
785 }
786 }
787
788 if matches!(
789 node.kind,
790 NodeKind::Struct | NodeKind::Class | NodeKind::Enum | NodeKind::Trait | NodeKind::Interface
791 ) {
792 score += 2.5;
793 }
794
795 if lower_name == "cachedb" {
796 score += 10.0;
797 }
798 if lower_name.contains("open") || lower_name.contains("initialize") {
799 score += 6.0;
800 }
801 if lower_name.contains("config") {
802 score += 5.0;
803 }
804 if lower_name.contains("search_vector") || lower_name.contains("search_document") {
805 score += 4.5;
806 }
807 if lower_name.contains("insert")
808 || lower_name.contains("upsert")
809 || lower_name.contains("delete")
810 || lower_name.contains("clear")
811 {
812 score += 3.0;
813 }
814 if lower_name.contains("count") || lower_name.contains("stats") || lower_name.contains("language_stats") {
815 score -= 2.0;
816 }
817
818 score
819}