use std::collections::HashSet;
const CALLABLE_BOOST: f32 = 0.15;
const TYPE_BOOST: f32 = 0.10;
const PROSE_PENALTY: f32 = -0.20;
const CONFIG_BUILD_PENALTY: f32 = -0.20;
const TEST_PATH_PENALTY: f32 = -0.20;
const EXACT_NAME_BOOST: f32 = 0.30;
const GENERIC_EXACT_NAME_BOOST: f32 = 0.08;
const NEUTRAL_BEHAVIORAL_BOOST: f32 = 0.10;
const NEUTRAL_BEHAVIORAL_OUT_DEGREE: usize = 2;
const GENERIC_NAME_TOKENS: &[&str] = &[
"acquire", "add", "apply", "borrow", "build", "call", "check", "clear", "close", "create",
"current", "data", "delete", "execute", "find", "get", "handle", "init", "insert", "list",
"load", "make", "name", "new", "next", "open", "parse", "pop", "process", "push", "read",
"release", "remove", "render", "reset", "result", "run", "save", "send", "set", "start",
"stop", "update", "value", "write",
];
const ROOT_COVERAGE_WEIGHT: f32 = 0.36;
const ROOT_COVERAGE_CAP: usize = 3;
const ROOT_SET_BONUS: f32 = 0.14;
const ROOT_TRANSITIVE_WEIGHT: f32 = 0.10;
const ROOT_TRANSITIVE_CAP: usize = 3;
const ROOT_FRACTION_WEIGHT: f32 = 0.05;
const ROOT_BOOST_MAX: f32 = 0.55;
const ORCHESTRATOR_STEP_ATTENUATION: f32 = 0.5;
const TOKEN_OVERLAP_WEIGHT: f32 = 0.08;
use crate::cli_tools::kinds::{CALLABLE_KINDS, CONFIG_BUILD_KINDS, TYPE_KINDS};
pub(crate) use crate::cli_tools::kinds::{PROSE_KINDS, kinds_allow, parse_kinds};
const STOPWORDS: &[&str] = &[
"a", "an", "the", "for", "with", "from", "to", "of", "in", "on", "and", "or", "is", "are",
"be", "by", "at", "as", "it", "its", "this", "that", "how", "do", "does", "what", "when",
"where", "which", "my", "me", "i", "you", "your", "we", "our", "their", "his", "her",
];
pub fn kind_boost(kind: &str) -> f32 {
if CALLABLE_KINDS.contains(&kind) {
CALLABLE_BOOST
} else if TYPE_KINDS.contains(&kind) {
TYPE_BOOST
} else if PROSE_KINDS.contains(&kind) {
PROSE_PENALTY
} else if CONFIG_BUILD_KINDS.contains(&kind) {
CONFIG_BUILD_PENALTY
} else {
0.0
}
}
pub fn is_test_path(file_path: &str) -> bool {
let normalized = file_path.replace('\\', "/");
let lower = normalized.to_lowercase();
let padded = format!("/{lower}");
for marker in ["/test/", "/tests/", "/__tests__/", "/spec/"] {
if padded.contains(marker) {
return true;
}
}
let file_name = normalized.rsplit('/').next().unwrap_or(&normalized);
let lower_name = file_name.to_lowercase();
if lower_name.contains("_test.")
|| lower_name.contains(".test.")
|| lower_name.contains(".spec.")
{
return true;
}
if lower_name.starts_with("test_") {
return true;
}
let stem = file_name.split('.').next().unwrap_or(file_name);
stem.ends_with("Test") || stem.ends_with("Tests")
}
fn identifier_tokens(name: &str) -> Vec<String> {
crate::utils::identifiers::identifier_tokens(name)
}
pub(crate) fn query_tokens(query: &str) -> Vec<String> {
query
.split(|c: char| !c.is_alphanumeric())
.map(str::to_lowercase)
.filter(|t| t.len() >= 2 && !STOPWORDS.contains(&t.as_str()))
.collect()
}
pub fn significant_query_tokens(query: &str) -> Vec<String> {
let mut seen: HashSet<String> = HashSet::new();
query_tokens(query)
.into_iter()
.filter(|t| t.len() >= 3)
.filter(|t| seen.insert(t.clone()))
.take(4)
.collect()
}
pub fn probe_name_variants(tokens: &[String]) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
for token in tokens {
let mut pascal = token.clone();
if let Some(first) = pascal.get_mut(0..1) {
first.make_ascii_uppercase();
}
for candidate in [token.clone(), pascal] {
if !names.contains(&candidate) {
names.push(candidate);
}
}
}
names
}
pub(crate) struct Coverage {
pub direct: usize,
pub transitive: usize,
pub total_roots: usize,
pub superseded_by_caller: bool,
}
fn root_coverage_boost(kind: &str, file_path: &str, coverage: Coverage) -> f32 {
let Coverage {
direct,
transitive,
total_roots,
superseded_by_caller,
} = coverage;
if kind_boost(kind) < 0.0 || is_test_path(file_path) {
return 0.0;
}
let covered = direct.clamp(0, ROOT_COVERAGE_CAP) as f32 / ROOT_COVERAGE_CAP as f32;
let mut boost = covered * ROOT_COVERAGE_WEIGHT;
if direct >= 2 {
boost += ROOT_SET_BONUS;
}
boost += transitive.clamp(0, ROOT_TRANSITIVE_CAP) as f32 / ROOT_TRANSITIVE_CAP as f32
* ROOT_TRANSITIVE_WEIGHT;
boost += direct.clamp(0, ROOT_COVERAGE_CAP) as f32 / total_roots.max(1) as f32
* ROOT_FRACTION_WEIGHT;
if superseded_by_caller {
boost *= ORCHESTRATOR_STEP_ATTENUATION;
}
boost.min(ROOT_BOOST_MAX)
}
pub fn lexical_boost(query: &str, name: &str) -> f32 {
lexical_boost_in_context(query, name, None)
}
pub fn lexical_boost_in_context(query: &str, name: &str, context: Option<&str>) -> f32 {
let q_tokens = query_tokens(query);
if q_tokens.is_empty() {
return 0.0;
}
if q_tokens.iter().any(|q| *q == name.to_lowercase()) {
let matched_generic = GENERIC_NAME_TOKENS.contains(&name.to_lowercase().as_str());
if !matched_generic {
return EXACT_NAME_BOOST;
}
if let Some(ctx) = context {
let name_lower = name.to_lowercase();
if identifier_tokens(ctx)
.iter()
.any(|part| *part != name_lower && q_tokens.contains(part))
{
return EXACT_NAME_BOOST;
}
}
return GENERIC_EXACT_NAME_BOOST;
}
let n_tokens = identifier_tokens(name);
let matched = n_tokens.iter().filter(|t| q_tokens.contains(t)).count();
if matched == 0 {
return 0.0;
}
TOKEN_OVERLAP_WEIGHT * (matched as f32 / q_tokens.len() as f32)
}
pub struct Candidate<'a> {
pub name: &'a str,
pub kind: &'a str,
pub file_path: &'a str,
pub context: Option<&'a str>,
pub out_degree: usize,
}
pub fn final_score(cosine: f32, query: &str, candidate: Candidate<'_>) -> f32 {
let Candidate {
kind,
file_path,
name,
context,
out_degree,
} = candidate;
let mut score = cosine + kind_boost(kind) + lexical_boost_in_context(query, name, context);
if kind_boost(kind) == 0.0
&& out_degree >= NEUTRAL_BEHAVIORAL_OUT_DEGREE
&& !is_test_path(file_path)
{
score += NEUTRAL_BEHAVIORAL_BOOST;
}
if is_test_path(file_path) {
score += TEST_PATH_PENALTY;
}
score
}
pub fn rerank(entities: Vec<serde_json::Value>, query: &str) -> Vec<serde_json::Value> {
let mut scored: Vec<(f32, String, i64, String, serde_json::Value)> = entities
.into_iter()
.map(|entity| {
let kind = entity.get("kind").and_then(|v| v.as_str()).unwrap_or("");
let file_path = entity
.get("file_path")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let name = entity.get("name").and_then(|v| v.as_str()).unwrap_or("");
let fqn = entity.get("fqn").and_then(|v| v.as_str()).unwrap_or("");
let start_line = entity
.get("start_line")
.and_then(|v| v.as_i64())
.unwrap_or(0);
let uuid = entity
.get("uuid")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let cosine = entity.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
let direct_roots = entity
.get("caller_roots")
.and_then(|v| v.as_i64())
.unwrap_or(0)
.max(0) as usize;
let transitive_roots = entity
.get("caller_roots_transitive")
.and_then(|v| v.as_i64())
.unwrap_or(0)
.max(0) as usize;
let total_roots = entity
.get("caller_root_total")
.and_then(|v| v.as_i64())
.unwrap_or(0)
.max(0) as usize;
let superseded_by_caller = entity
.get("caller_superseded")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let channel = entity
.get(super::CHANNEL_FIELD)
.and_then(|v| v.as_str())
.unwrap_or("cosine");
let out_degree = entity
.get("caller_out_degree")
.and_then(|v| v.as_i64())
.unwrap_or(0)
.max(0) as usize;
let score = final_score(
cosine,
query,
Candidate {
kind,
file_path: &file_path,
name,
context: Some(fqn),
out_degree,
},
) + root_coverage_boost(
kind,
&file_path,
Coverage {
direct: direct_roots,
transitive: transitive_roots,
total_roots,
superseded_by_caller,
},
);
tracing::debug!(
target: "search_hybrid_context::rank",
score = score,
cosine = cosine,
kind = kind, name = name, fqn = fqn,
channel = channel,
out_degree = out_degree,
direct = direct_roots, transitive = transitive_roots,
total_roots = total_roots,
superseded = superseded_by_caller,
"ranked"
);
(score, file_path, start_line, uuid, entity)
})
.collect();
scored.sort_by(|a, b| {
b.0.partial_cmp(&a.0)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| a.1.cmp(&b.1))
.then_with(|| a.2.cmp(&b.2))
.then_with(|| a.3.cmp(&b.3))
});
scored
.into_iter()
.map(|(_, _, _, _, entity)| entity)
.collect()
}
pub fn path_allows(pattern: Option<&str>, file_path: &str) -> bool {
match pattern {
None => true,
Some(p) => crate::cli_tools::list_files::path_matches(p, file_path),
}
}
pub fn candidate_limit(max_results: usize) -> usize {
(max_results * 4).clamp(24, super::MAX_RESULTS_CEILING * 4)
}
pub fn dedup_by_identity(entities: Vec<serde_json::Value>) -> Vec<serde_json::Value> {
let mut seen: HashSet<String> = HashSet::new();
entities
.into_iter()
.filter(|entity| {
let uuid = entity
.get("uuid")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let fqn = entity
.get("fqn")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let repo = entity
.get("repo_name")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let line = entity
.get("start_line")
.and_then(|v| v.as_i64())
.unwrap_or(0);
let key = if fqn.is_empty() {
uuid
} else {
format!("{repo}\u{0}{fqn}\u{0}{line}")
};
seen.insert(key)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn root_coverage_ladder_rewards_multi_root_callers() {
assert_eq!(
root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 0,
transitive: 0,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
let one = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 1,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!(
(one - 1.0 / 3.0 * ROOT_COVERAGE_WEIGHT - ROOT_FRACTION_WEIGHT * (1.0 / 8.0)).abs()
< 1e-5
);
let two = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!(two >= 2.0 * one, "set bonus missing: {two} vs 2×{one}");
let three = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 3,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let saturated = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 7,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!(
(three - saturated).abs() < 1e-6,
"beyond the cap coverage must not change the score"
);
assert!(three <= ROOT_BOOST_MAX);
}
#[test]
fn root_coverage_two_of_three_beats_helper_with_higher_cosine() {
let a_covered = final_score(
0.40,
"unshared query",
Candidate {
kind: "function",
file_path: "src/a.rs",
name: "entryPoint",
context: Some("app::A::entryPoint"),
out_degree: 0,
},
) + root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 2,
transitive: 0,
total_roots: 3,
superseded_by_caller: false,
},
);
let b_higher_cosine = final_score(
0.5,
"unshared query",
Candidate {
kind: "function",
file_path: "src/b.rs",
name: "helperOne",
context: Some("app::B::helperOne"),
out_degree: 0,
},
) + root_coverage_boost(
"function",
"src/b.rs",
Coverage {
direct: 1,
transitive: 0,
total_roots: 5,
superseded_by_caller: false,
},
);
assert!(
a_covered > b_higher_cosine,
"coverage 2/3 must beat cosine +0.10 at 1/5"
);
}
#[test]
fn root_coverage_transitive_lifts_entry_point_calling_one_helper() {
let none = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 0,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let transitive3 = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 0,
transitive: 3,
total_roots: 8,
superseded_by_caller: false,
},
);
let direct2 = root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!(
transitive3 > none,
"transitive coverage must lift above zero"
);
assert!(
transitive3 + none <= direct2,
"one-helper orchestration must not outrank a real 2-root caller"
);
assert_eq!(
root_coverage_boost(
"function",
"src/a.rs",
Coverage {
direct: 0,
transitive: 5,
total_roots: 8,
superseded_by_caller: false
}
),
transitive3
);
}
#[test]
fn root_coverage_orchestration_step_attenuated_when_caller_covers_superset() {
let step = root_coverage_boost(
"function",
"src/hooks.ts",
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: true,
},
);
let outer = root_coverage_boost(
"function",
"src/hooks.ts",
Coverage {
direct: 3,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let plain = root_coverage_boost(
"function",
"src/hooks.ts",
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!((step - plain * ORCHESTRATOR_STEP_ATTENUATION).abs() < 1e-5);
assert!(step < plain, "attenuation must lower the boost");
assert!(outer > plain, "outer orchestrator keeps the full boost");
}
#[test]
fn root_coverage_never_rescues_tests_prose_or_config() {
assert_eq!(
root_coverage_boost(
"rust_function",
"tests/login_test.rs",
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"markdown_section",
"docs/a.md",
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"config_property",
"config/app.yml",
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"build_dependency",
"Cargo.toml",
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
}
#[test]
fn rerank_test_calling_same_helpers_stays_below_entry_point() {
let candidates = vec![
json!({"uuid": "1", "name": "test_login_success", "kind": "rust_function",
"file_path": "tests/login_test.rs", "start_line": 10, "score": 0.62,
"caller_roots": 2, "caller_roots_transitive": 1, "caller_root_total": 8}),
json!({"uuid": "2", "name": "login", "kind": "rust_function",
"file_path": "src/api/auth.rs", "start_line": 30, "score": 0.62,
"caller_roots": 2, "caller_roots_transitive": 1, "caller_root_total": 8}),
];
let ranked = rerank(candidates, "authenticate user with email and password");
assert_eq!(ranked[0]["name"], "login", "test path must stay below");
assert_eq!(ranked[1]["name"], "test_login_success");
}
#[test]
fn rerank_promotes_shared_caller_of_top_root() {
let candidates = vec![
json!({"uuid": "1", "name": "normalize_email", "kind": "rust_function",
"file_path": "src/auth/credentials.rs", "start_line": 71, "score": 0.319}),
json!({"uuid": "2", "name": "verify_password", "kind": "rust_function",
"file_path": "src/auth/credentials.rs", "start_line": 41, "score": 0.302}),
json!({"uuid": "3", "name": "login", "kind": "rust_function",
"file_path": "src/api/auth.rs", "start_line": 136, "score": 0.241,
"caller_roots": 2}),
json!({"uuid": "4", "name": "test_login_success", "kind": "rust_function",
"file_path": "tests/login_test.rs", "start_line": 10, "score": 0.24,
"caller_roots": 2}),
json!({"uuid": "5", "name": "create_user", "kind": "rust_function",
"file_path": "src/api/admin.rs", "start_line": 12, "score": 0.26,
"caller_roots": 1}),
];
let ranked = rerank(candidates, "authenticate user with email and password");
let names: Vec<&str> = ranked
.iter()
.filter_map(|e| e.get("name").and_then(|v| v.as_str()))
.collect();
let login = names.iter().position(|n| *n == "login").unwrap();
assert!(
login <= 2,
"login must enter the leading slots, got order {names:?}"
);
let test = names
.iter()
.position(|n| *n == "test_login_success")
.unwrap();
assert!(test > login, "test files stay below production callables");
}
#[test]
fn kind_boost_callables_outrank_types() {
assert!(kind_boost("function") > kind_boost("class"));
assert!(kind_boost("rust_method") > kind_boost("rust_struct"));
assert_eq!(kind_boost("java_method_placeholder_unused"), 0.0);
}
#[test]
fn kind_boost_covers_one_kind_per_language() {
for callable in [
"method",
"kotlin_function",
"rust_function",
"python_method",
"c_function",
"cpp_method",
"csharp_method",
"groovy_method",
] {
assert_eq!(kind_boost(callable), CALLABLE_BOOST, "callable {callable}");
}
for ty in [
"class",
"kotlin_class",
"rust_struct",
"python_class",
"cpp_class",
"groovy_class",
"csharp_class",
] {
assert_eq!(kind_boost(ty), TYPE_BOOST, "type {ty}");
}
}
#[test]
fn kind_boost_penalizes_prose_and_config() {
assert_eq!(kind_boost("markdown_section"), PROSE_PENALTY);
assert_eq!(kind_boost("markdown_document"), PROSE_PENALTY);
assert_eq!(kind_boost("config_property"), CONFIG_BUILD_PENALTY);
assert_eq!(kind_boost("build_dependency"), CONFIG_BUILD_PENALTY);
assert_eq!(kind_boost("cargo_package"), CONFIG_BUILD_PENALTY);
assert_eq!(kind_boost("project_identity"), CONFIG_BUILD_PENALTY);
assert_eq!(kind_boost("constant"), 0.0);
assert_eq!(kind_boost("css_class"), 0.0);
assert_eq!(kind_boost("rust_impl"), 0.0);
assert_eq!(kind_boost("something_new"), 0.0);
}
#[test]
fn test_path_positives() {
for path in [
"tests/login_test.rs",
"src/tests/login.rs",
"src/test/foo.java",
"src/__tests__/login.ts",
"spec/login_spec.rb",
"tests/foo.rs",
"login_test.rs",
"login.test.ts",
"login.spec.ts",
"test_login.rs",
"LoginTest.java",
"UserServiceTests.java",
] {
assert!(is_test_path(path), "expected test path: {path}");
}
}
#[test]
fn test_path_negatives_do_not_substring_match() {
for path in [
"src/latest.rs",
"src/contest.rs",
"src/testing_support.rs",
"src/login.rs",
"attestations.rs",
] {
assert!(!is_test_path(path), "expected non-test path: {path}");
}
}
#[test]
fn lexical_exact_name_hit() {
assert_eq!(
lexical_boost_in_context(
"authenticate user with email and password",
"authenticate",
None
),
EXACT_NAME_BOOST
);
}
#[test]
fn lexical_partial_overlap_ratio() {
let boost = lexical_boost(
"vector store similarity search implementation",
"similaritySearch",
);
assert!((boost - TOKEN_OVERLAP_WEIGHT * (2.0 / 5.0)).abs() < 1e-6);
}
#[test]
fn lexical_snake_case_splits() {
let boost = lexical_boost(
"link cross repository build dependencies",
"link_cross_repo_dependencies",
);
assert!((boost - TOKEN_OVERLAP_WEIGHT * (3.0 / 5.0)).abs() < 1e-6);
}
#[test]
fn lexical_no_overlap_is_zero() {
assert_eq!(
lexical_boost("authenticate user with email and password", "parse_config"),
0.0
);
}
#[test]
fn lexical_stopwords_and_single_chars_ignored() {
assert_eq!(
lexical_boost("a build of the thing", "build"),
GENERIC_EXACT_NAME_BOOST
);
assert_eq!(lexical_boost("a to i", "a"), 0.0);
}
#[test]
fn rerank_promotes_definition_over_prose_test_and_helper() {
let candidates = vec![
json!({"uuid": "1", "name": "setup", "kind": "markdown_section",
"file_path": "docs/AUTH.md", "start_line": 1, "score": 0.82}),
json!({"uuid": "2", "name": "test_login_success", "kind": "rust_function",
"file_path": "tests/login_test.rs", "start_line": 10, "score": 0.75}),
json!({"uuid": "3", "name": "handle_login", "kind": "rust_function",
"file_path": "src/routes.rs", "start_line": 20, "score": 0.58}),
json!({"uuid": "4", "name": "normalize_email", "kind": "rust_function",
"file_path": "src/util.rs", "start_line": 5, "score": 0.60}),
json!({"uuid": "5", "name": "login", "kind": "rust_function",
"file_path": "src/auth.rs", "start_line": 30, "score": 0.62}),
];
let ranked = rerank(candidates, "authenticate user with email and password");
assert_eq!(ranked[0]["name"], "login", "definition must rank first");
let names: Vec<&str> = ranked
.iter()
.filter_map(|e| e.get("name").and_then(|v| v.as_str()))
.collect();
let login_pos = names.iter().position(|n| *n == "login").unwrap();
let setup_pos = names.iter().position(|n| *n == "setup").unwrap();
let test_pos = names
.iter()
.position(|n| *n == "test_login_success")
.unwrap();
assert!(login_pos < setup_pos && login_pos < test_pos);
}
#[test]
fn rerank_recovers_definition_buried_far_below_prose() {
let candidates = vec![
json!({"uuid": "1", "name": "setup", "kind": "markdown_section",
"file_path": "docs/AUTH.md", "start_line": 1, "score": 0.82}),
json!({"uuid": "2", "name": "test_login_success", "kind": "rust_function",
"file_path": "tests/login_test.rs", "start_line": 10, "score": 0.70}),
json!({"uuid": "3", "name": "login", "kind": "rust_function",
"file_path": "src/auth.rs", "start_line": 30, "score": 0.52}),
];
let ranked = rerank(candidates, "authenticate user with email and password");
assert_eq!(ranked[0]["name"], "login");
assert_eq!(ranked[1]["name"], "test_login_success");
assert_eq!(ranked[2]["name"], "setup");
}
#[test]
fn rerank_lets_method_beat_its_own_container() {
let candidates = vec![
json!({"uuid": "1", "name": "LookupMaps", "kind": "rust_struct",
"fqn": "knot::pipeline::ingest::resolve::LookupMaps",
"file_path": "src/resolve/mod.rs", "start_line": 131, "score": 0.80}),
json!({"uuid": "2", "name": "build", "kind": "rust_method",
"fqn": "knot::pipeline::ingest::resolve::LookupMaps::build",
"file_path": "src/resolve/mod.rs", "start_line": 144, "score": 0.58}),
];
let ranked = rerank(candidates, "build lookup maps for reference resolution");
assert_eq!(ranked[0]["name"], "build");
}
#[test]
fn rerank_preserves_cosine_order_within_same_bucket() {
let candidates = vec![
json!({"uuid": "1", "name": "evictConnection", "kind": "method",
"file_path": "src/Pool.java", "start_line": 10, "score": 0.91}),
json!({"uuid": "2", "name": "shutdown", "kind": "method",
"file_path": "src/Pool.java", "start_line": 40, "score": 0.87}),
];
let ranked = rerank(candidates, "evict a connection from the pool");
assert_eq!(ranked[0]["name"], "evictConnection");
assert_eq!(ranked[1]["name"], "shutdown");
}
#[test]
fn rerank_is_deterministic_on_full_ties() {
let a = json!({"uuid": "aaa", "name": "foo", "kind": "function",
"file_path": "src/a.rs", "start_line": 1, "score": 0.5});
let b = json!({"uuid": "bbb", "name": "foo", "kind": "function",
"file_path": "src/a.rs", "start_line": 1, "score": 0.5});
let ranked = rerank(vec![b.clone(), a.clone()], "foo");
assert_eq!(ranked[0]["uuid"], "aaa");
assert_eq!(ranked[1]["uuid"], "bbb");
}
#[test]
fn rerank_handles_missing_fields_without_panic() {
let ranked = rerank(
vec![json!({"name": "mystery"}), json!({"uuid": "u"})],
"query",
);
assert_eq!(ranked.len(), 2);
}
#[test]
fn candidate_limit_clamped_window() {
assert_eq!(candidate_limit(1), 24);
assert_eq!(candidate_limit(5), 24);
assert_eq!(candidate_limit(10), 40);
assert_eq!(candidate_limit(20), 80);
}
#[test]
fn candidate_limit_is_unchanged_below_legacy_ceiling() {
for n in 1..=20 {
assert_eq!(
candidate_limit(n),
(n * 4).clamp(24, 80),
"candidate_limit changed for max_results = {n}"
);
}
}
#[test]
fn candidate_limit_scales_to_result_ceiling() {
assert_eq!(candidate_limit(crate::cli_tools::MAX_RESULTS_CEILING), 400);
}
#[test]
fn significant_query_tokens_filters_and_caps() {
let tokens = significant_query_tokens("build lookup maps for reference resolution");
assert_eq!(tokens, vec!["build", "lookup", "maps", "reference"]);
assert_eq!(
significant_query_tokens("a to do the the build"),
vec!["build"]
);
assert!(significant_query_tokens("of to in").is_empty());
}
#[test]
fn probe_name_variants_cover_snake_and_pascal() {
let variants = probe_name_variants(&["build".to_string(), "user".to_string()]);
assert_eq!(
variants,
vec![
"build".to_string(),
"Build".to_string(),
"user".to_string(),
"User".to_string()
]
);
assert!(probe_name_variants(&[]).is_empty());
}
#[test]
fn generic_name_boost_attenuated_without_context() {
assert_eq!(
lexical_boost_in_context(
"find who invokes a given symbol",
"Find",
Some("CodeMap.Mcp.ToolRegistry.Find")
),
GENERIC_EXACT_NAME_BOOST
);
assert_eq!(
lexical_boost("acquire a client for talking to the database", "acquire"),
GENERIC_EXACT_NAME_BOOST
);
}
#[test]
fn generic_name_boost_full_when_context_corroborates() {
assert_eq!(
lexical_boost_in_context(
"create a client to chat with a language model",
"create",
Some("org.springframework.ai.chat.client.ChatClient.create")
),
EXACT_NAME_BOOST
);
assert_eq!(
lexical_boost_in_context(
"build lookup maps for reference resolution",
"build",
Some("knot::pipeline::ingest::resolve::LookupMaps::build")
),
EXACT_NAME_BOOST
);
}
#[test]
fn non_generic_exact_name_keeps_full_boost() {
for (query, name) in [
("authenticate user with email and password", "authenticate"),
("find screenshot capture in tool reference", "screenshot"),
("encode the payload as utf8", "encode"),
] {
assert_eq!(
lexical_boost_in_context(query, name, None),
EXACT_NAME_BOOST,
"non-generic token must keep the full boost: {name}"
);
}
}
#[test]
fn regression_rust_job_watch_login() {
let ranked = rerank(
vec![
json!({"uuid": "ensure_account_eligible", "name": "ensure_account_eligible", "kind": "rust_function",
"fqn": "app::ensure_account_eligible",
"file_path": "src/api/auth.rs", "start_line": 9, "score": 0.62,
"caller_roots": 1, "caller_roots_transitive": 0,
"caller_root_total": 8}),
json!({"uuid": "2", "name": "create_user", "kind": "rust_function",
"fqn": "jobwatch::api::admin::create_user",
"file_path": "src/api/admin.rs", "start_line": 44, "score": 0.60}),
json!({"uuid": "3", "name": "bootstrap_email", "kind": "rust_function",
"fqn": "jobwatch::bootstrap_email",
"file_path": "src/main.rs", "start_line": 210, "score": 0.58}),
json!({"uuid": "3", "name": "login", "kind": "rust_function",
"fqn": "jobwatch::api::auth::login",
"file_path": "src/api/auth.rs", "start_line": 136, "score": 0.57,
"caller_roots": 3, "caller_roots_transitive": 1,
"caller_root_total": 8}),
],
"authenticate user with email and password",
);
assert_regression_first(&ranked, "login");
}
#[test]
fn regression_rust_knot_run_search_hybrid_context() {
let ranked = rerank(
vec![
json!({"uuid": "find_repo_dependents", "name": "find_repo_dependents", "kind": "rust_function",
"fqn": "app::find_repo_dependents",
"file_path": "src/db/graph/query_repo.rs", "start_line": 9, "score": 0.64,
"caller_roots": 0, "caller_roots_transitive": 0,
"caller_root_total": 8}),
json!({"uuid": "find_repo_dependencies", "name": "find_repo_dependencies", "kind": "rust_function",
"fqn": "app::find_repo_dependencies",
"file_path": "src/db/graph/query_repo.rs", "start_line": 9, "score": 0.62,
"caller_roots": 0, "caller_roots_transitive": 0,
"caller_root_total": 8}),
json!({"uuid": "3", "name": "run_search_hybrid_context",
"kind": "rust_function",
"fqn": "knot::cli_tools::search_hybrid_context::run_search_hybrid_context",
"file_path": "src/cli_tools/search_hybrid_context/mod.rs",
"start_line": 112, "score": 0.40,
"caller_roots": 2, "caller_roots_transitive": 2,
"caller_root_total": 8}),
],
"find relevant code by meaning across the repository",
);
assert_regression_first(&ranked, "run_search_hybrid_context");
}
#[test]
fn regression_java_hikari_get_connection_borrow() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "borrow", "kind": "method",
"fqn": "com.zaxxer.hikari.util.ConcurrentBag.borrow",
"file_path": "src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java",
"start_line": 312, "score": 0.62}),
json!({"uuid": "2", "name": "isConnectionAlive", "kind": "method",
"fqn": "com.zaxxer.hikari.pool.PoolBase.isConnectionAlive",
"file_path": "src/main/java/com/zaxxer/hikari/pool/PoolBase.java",
"start_line": 288, "score": 0.60}),
json!({"uuid": "3", "name": "releaseConnection", "kind": "method",
"fqn": "com.zaxxer.hikari.pool.HikariPool.releaseConnection",
"file_path": "src/main/java/com/zaxxer/hikari/pool/HikariPool.java",
"start_line": 292, "score": 0.59}),
json!({"uuid": "4", "name": "getConnection", "kind": "method",
"fqn": "com.zaxxer.hikari.pool.HikariPool.getConnection",
"file_path": "src/main/java/com/zaxxer/hikari/pool/HikariPool.java",
"start_line": 239, "score": 0.55,
"caller_roots": 3, "caller_roots_transitive": 0,
"caller_root_total": 8}),
],
"borrow a connection from the pool",
);
assert_regression_first(&ranked, "getConnection");
}
#[test]
fn regression_java_hikari_get_connection_acquire() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "acquire", "kind": "method",
"fqn": "com.zaxxer.hikari.util.SuspendResumeLock.acquire",
"file_path": "src/main/java/com/zaxxer/hikari/util/SuspendResumeLock.java",
"start_line": 62, "score": 0.63}),
json!({"uuid": "2", "name": "getConnectionTimeout", "kind": "method",
"fqn": "com.zaxxer.hikari.HikariConfigMXBean.getConnectionTimeout",
"file_path": "src/main/java/com/zaxxer/hikari/HikariConfigMXBean.java",
"start_line": 34, "score": 0.60}),
json!({"uuid": "3", "name": "getConnection", "kind": "method",
"fqn": "com.zaxxer.hikari.pool.HikariPool.getConnection",
"file_path": "src/main/java/com/zaxxer/hikari/pool/HikariPool.java",
"start_line": 239, "score": 0.54,
"caller_roots": 2, "caller_roots_transitive": 2,
"caller_root_total": 8}),
],
"acquire a client for talking to the database",
);
assert_regression_first(&ranked, "getConnection");
}
#[test]
fn regression_java_spring_ai_chat_client_create() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "create", "kind": "method",
"fqn": "org.springframework.ai.chat.client.ChatClient.create",
"file_path": "spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/ChatClient.java",
"start_line": 98, "score": 0.56}),
json!({"uuid": "2", "name": "model", "kind": "method",
"fqn": "org.springframework.ai.chat.prompt.ChatOptions.model",
"file_path": "spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java",
"start_line": 87, "score": 0.50}),
],
"create a client to chat with a language model",
);
assert_regression_first(&ranked, "create");
}
#[test]
fn regression_csharp_get_callers_async_find() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "Find", "kind": "csharp_method",
"fqn": "CodeMap.Mcp.ToolRegistry.Find",
"file_path": "src/CodeMap.Mcp/ToolRegistry.cs",
"start_line": 45, "score": 0.62}),
json!({"uuid": "2", "name": "GetFactsForSymbolAsync", "kind": "csharp_method",
"fqn": "CodeMap.Core.Interfaces.ISymbolStore.GetFactsForSymbolAsync",
"file_path": "src/CodeMap.Core/Interfaces/ISymbolStore.cs",
"start_line": 120, "score": 0.59}),
json!({"uuid": "3", "name": "GetCallersAsync", "kind": "csharp_method",
"fqn": "CodeMap.Query.QueryEngine.GetCallersAsync",
"file_path": "src/CodeMap.Query/QueryEngine.cs",
"start_line": 380, "score": 0.53,
"caller_roots": 3, "caller_roots_transitive": 1,
"caller_root_total": 8}),
],
"find who invokes a given symbol",
);
assert_regression_first(&ranked, "GetCallersAsync");
}
#[test]
fn regression_csharp_get_callers_async_get() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "GetSymbolAsync", "kind": "csharp_method",
"fqn": "CodeMap.Core.Interfaces.ISymbolStore.GetSymbolAsync",
"file_path": "src/CodeMap.Core/Interfaces/ISymbolStore.cs",
"start_line": 96, "score": 0.60}),
json!({"uuid": "2", "name": "GetCallersAsync", "kind": "csharp_method",
"fqn": "CodeMap.Query.QueryEngine.GetCallersAsync",
"file_path": "src/CodeMap.Query/QueryEngine.cs",
"start_line": 380, "score": 0.54,
"caller_roots": 2, "caller_roots_transitive": 2,
"caller_root_total": 8}),
],
"get the callers of a symbol",
);
assert_regression_first(&ranked, "GetCallersAsync");
}
#[test]
fn regression_ts_screenshot_capture_view() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "current", "kind": "constant",
"file_path": "src/TextSnapshot.ts", "start_line": 32, "score": 0.61}),
json!({"uuid": "2", "name": "getScreenRecorder", "kind": "method",
"fqn": "McpContext.getScreenRecorder",
"file_path": "src/McpContext.ts", "start_line": 411, "score": 0.59}),
json!({"uuid": "3", "name": "attachImage", "kind": "method",
"fqn": "McpResponse.attachImage",
"file_path": "src/McpResponse.ts", "start_line": 168, "score": 0.58}),
json!({"uuid": "4", "name": "screenshot", "kind": "constant",
"file_path": "src/tools/screenshot.ts", "start_line": 21, "score": 0.52,
"caller_roots": 3, "caller_roots_transitive": 1,
"caller_root_total": 8}),
],
"capture the current view as an image",
);
assert_regression_first(&ranked, "screenshot");
}
#[test]
fn regression_ts_screenshot_take_screenshot() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "take screenshot",
"kind": "markdown_section",
"fqn": "docs/tool-reference.md::Chrome DevTools MCP Tool Reference > Debugging > take screenshot",
"file_path": "docs/tool-reference.md", "start_line": 12, "score": 0.66}),
json!({"uuid": "2", "name": "screenshot", "kind": "constant",
"file_path": "src/tools/screenshot.ts", "start_line": 21, "score": 0.55,
"caller_roots": 3, "caller_roots_transitive": 1,
"caller_root_total": 8}),
],
"take screenshot",
);
assert_regression_first(&ranked, "screenshot");
}
#[test]
fn regression_js_job_watch_ui_login() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "Session", "kind": "class",
"file_path": "src/lib/session.js", "start_line": 18, "score": 0.60}),
json!({"uuid": "2", "name": "readSession", "kind": "function",
"fqn": "readSession",
"file_path": "src/lib/token.js", "start_line": 45, "score": 0.58}),
json!({"uuid": "3", "name": "login", "kind": "function",
"fqn": "src::auth::login",
"file_path": "src/auth.js", "start_line": 88, "score": 0.53,
"caller_roots": 3, "caller_roots_transitive": 0,
"caller_root_total": 8}),
],
"log a user in and issue a session token",
);
assert_regression_first(&ranked, "login");
}
#[test]
fn rerank_entry_point_beats_generic_verb_leaf() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "find_matching_nodes", "kind": "rust_function",
"fqn": "app::find_matching_nodes",
"file_path": "src/match.rs", "start_line": 9, "score": 0.55}),
json!({"uuid": "2", "name": "lookup_nodes", "kind": "rust_function",
"fqn": "app::lookup_tables",
"file_path": "src/tables.rs", "start_line": 21, "score": 0.54}),
json!({"uuid": "3", "name": "resolve", "kind": "rust_function",
"fqn": "app::resolve",
"file_path": "src/resolve.rs", "start_line": 31, "score": 0.52,
"caller_roots": 1, "caller_roots_transitive": 2,
"caller_root_total": 5}),
],
"find matching nodes by lookup",
);
assert_regression_first(&ranked, "resolve");
}
#[test]
fn neutral_kind_with_call_edges_outranks_lower_cosine_callable() {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "getScreenRecorder", "kind": "method",
"fqn": "McpContext.getScreenRecorder",
"file_path": "src/McpContext.ts", "start_line": 411, "score": 0.295}),
json!({"uuid": "2", "name": "screenshot", "kind": "constant",
"fqn": "screenshot",
"file_path": "src/tools/screenshot.ts", "start_line": 21, "score": 0.376,
"caller_out_degree": 16}),
],
"capture the current view as an image",
);
assert_eq!(ranked[0]["name"], "screenshot", "order got {ranked:?}");
}
#[test]
fn neutral_boost_never_rescues_prose_config_or_tests() {
let query = "capture anything";
let boosted = |kind: &str, path: &str| {
final_score(
0.30,
query,
Candidate {
name: "x",
kind,
file_path: path,
context: None,
out_degree: 20,
},
)
};
let bare = final_score(
0.30,
query,
Candidate {
name: "x",
kind: "markdown_section",
file_path: "docs/a.md",
context: None,
out_degree: 0,
},
);
assert_eq!(boosted("markdown_section", "docs/a.md"), bare);
let config_bare = final_score(
0.30,
query,
Candidate {
name: "x",
kind: "config_property",
file_path: "config/a.yml",
context: None,
out_degree: 0,
},
);
assert_eq!(boosted("config_property", "config/a.yml"), config_bare);
assert_eq!(
boosted("rust_function", "tests/foo_test.rs"),
0.30 + CALLABLE_BOOST + TEST_PATH_PENALTY
);
}
#[test]
fn neutral_boost_requires_minimum_out_degree() {
let one = final_score(
0.40,
"anything at all",
Candidate {
kind: "constant",
file_path: "src/a.ts",
name: "k",
context: None,
out_degree: 1,
},
);
let zero = final_score(
0.40,
"anything at all",
Candidate {
kind: "constant",
file_path: "src/a.ts",
name: "k",
context: None,
out_degree: 0,
},
);
assert_eq!(one, zero);
let two = final_score(
0.40,
"anything at all",
Candidate {
kind: "constant",
file_path: "src/a.ts",
name: "k",
context: None,
out_degree: 2,
},
);
assert!((two - (one + NEUTRAL_BEHAVIORAL_BOOST)).abs() < 1e-6);
}
#[test]
fn neutral_boost_applies_only_to_neutral_kinds() {
let callable = final_score(
0.40,
"q",
Candidate {
kind: "csharp_method",
file_path: "src/A.cs",
name: "M",
context: None,
out_degree: 10,
},
);
assert_eq!(
callable,
0.40 + CALLABLE_BOOST,
"callable kinds keep their own boost and never overlay the neutral one"
);
}
fn assert_regression_first(ranked: &[serde_json::Value], expected: &str) {
let names: Vec<&str> = ranked
.iter()
.filter_map(|e| e.get("name").and_then(|v| v.as_str()))
.collect();
assert_eq!(
names.first(),
Some(&expected),
"expected {expected} at position 1, got order {names:?}"
);
}
#[test]
fn dedup_keeps_first_of_duplicate_identity() {
let entities = vec![
json!({"uuid": "1", "name": "user", "fqn": "app::User", "repo_name": "r", "start_line": 3}),
json!({"uuid": "2", "name": "user", "fqn": "app::User", "repo_name": "r", "start_line": 3}),
json!({"uuid": "3", "name": "user", "fqn": "other::User", "repo_name": "r", "start_line": 9}),
];
let deduped = dedup_by_identity(entities);
assert_eq!(deduped.len(), 2);
assert_eq!(deduped[0]["uuid"], "1");
assert_eq!(deduped[1]["uuid"], "3");
}
#[test]
fn dedup_rows_without_fqn_survive_via_uuid_key() {
let entities = vec![
json!({"uuid": "1", "name": "a", "repo_name": "r", "start_line": 1}),
json!({"uuid": "2", "name": "b", "repo_name": "r", "start_line": 1}),
];
assert_eq!(dedup_by_identity(entities).len(), 2);
}
}