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;
fn superseded_step_boost(direct: usize, transitive: usize) -> f32 {
(direct + transitive).clamp(0, ROOT_TRANSITIVE_CAP) as f32 / ROOT_TRANSITIVE_CAP as f32
* ROOT_TRANSITIVE_WEIGHT
}
const TOKEN_OVERLAP_WEIGHT: f32 = 0.08;
const SEMANTIC_WEIGHT: f32 = 0.40;
const SEM_EPSILON: f32 = 1e-6;
pub(crate) fn normalize_pool_cosines(cosines: &[Option<f32>]) -> Vec<f32> {
let usable = |c: &Option<f32>| c.filter(|v| v.is_finite());
let mut present = cosines.iter().filter_map(usable);
let Some(first) = present.next() else {
return vec![0.0; cosines.len()];
};
let (min, max) = present.fold((first, first), |(lo, hi), c| (lo.min(c), hi.max(c)));
let span = max - min;
if cosines.len() < 2 || span <= SEM_EPSILON {
return vec![0.0; cosines.len()];
}
cosines
.iter()
.map(|c| ((usable(c).unwrap_or(min) - min) / span).clamp(0.0, 1.0))
.collect()
}
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,
is_test_context: bool,
coverage: Coverage,
) -> f32 {
let Coverage {
direct,
transitive,
total_roots,
superseded_by_caller,
} = coverage;
if kind_boost(kind) < 0.0 || is_test_context || is_test_path(file_path) {
return 0.0;
}
if superseded_by_caller {
return superseded_step_boost(direct, transitive);
}
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;
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 is_test_context: bool,
}
pub fn is_test_entity(c: &Candidate<'_>) -> bool {
is_test_path(c.file_path) || c.is_test_context
}
pub fn is_test_row(kind: &str, file_path: &str, is_test_context: bool) -> bool {
kind_boost(kind) < 0.0 || is_test_context || is_test_path(file_path)
}
pub fn final_score(sem: f32, query: &str, candidate: Candidate<'_>) -> f32 {
let Candidate {
kind,
name,
context,
out_degree,
..
} = &candidate;
let mut score =
SEMANTIC_WEIGHT * sem + kind_boost(kind) + lexical_boost_in_context(query, name, *context);
if kind_boost(kind) == 0.0
&& out_degree >= &NEUTRAL_BEHAVIORAL_OUT_DEGREE
&& !is_test_entity(&candidate)
{
score += NEUTRAL_BEHAVIORAL_BOOST;
}
if is_test_entity(&candidate) {
score += TEST_PATH_PENALTY;
}
score
}
pub fn rerank(entities: Vec<serde_json::Value>, query: &str) -> Vec<serde_json::Value> {
let facts: Vec<RowFacts> = entities.iter().map(row_facts).collect();
let cosines: Vec<Option<f32>> = facts.iter().map(|f| f.cosine).collect();
let sems = normalize_pool_cosines(&cosines);
let mut scored: Vec<(f32, String, i64, String, serde_json::Value)> = entities
.into_iter()
.zip(facts)
.zip(sems)
.map(|((entity, facts), sem)| {
let score = score_row(&facts, sem, query);
trace_ranked(&facts, sem, score);
(score, facts.file_path, facts.start_line, facts.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()
}
struct RowFacts {
name: String,
kind: String,
file_path: String,
fqn: String,
start_line: i64,
uuid: String,
cosine: Option<f32>,
direct_roots: usize,
transitive_roots: usize,
total_roots: usize,
superseded_by_caller: bool,
out_degree: usize,
is_test_context: bool,
channel: String,
}
fn row_facts(entity: &serde_json::Value) -> RowFacts {
let string_field = |key: &str| {
entity
.get(key)
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string()
};
let count_field =
|key: &str| entity.get(key).and_then(|v| v.as_i64()).unwrap_or(0).max(0) as usize;
RowFacts {
name: string_field("name"),
kind: string_field("kind"),
file_path: string_field("file_path"),
fqn: string_field("fqn"),
start_line: entity
.get("start_line")
.and_then(|v| v.as_i64())
.unwrap_or(0),
uuid: string_field("uuid"),
cosine: entity
.get("score")
.and_then(|v| v.as_f64())
.map(|c| c as f32),
direct_roots: count_field("caller_roots"),
transitive_roots: count_field("caller_roots_transitive"),
total_roots: count_field("caller_root_total"),
superseded_by_caller: entity
.get("caller_superseded")
.and_then(|v| v.as_bool())
.unwrap_or(false),
out_degree: count_field("caller_out_degree"),
is_test_context: entity
.get("is_test_context")
.and_then(|v| v.as_bool())
.unwrap_or(false),
channel: entity
.get(super::CHANNEL_FIELD)
.and_then(|v| v.as_str())
.unwrap_or("cosine")
.to_string(),
}
}
fn score_row(facts: &RowFacts, sem: f32, query: &str) -> f32 {
final_score(
sem,
query,
Candidate {
kind: &facts.kind,
file_path: &facts.file_path,
name: &facts.name,
context: Some(&facts.fqn),
out_degree: facts.out_degree,
is_test_context: facts.is_test_context,
},
) + root_coverage_boost(
&facts.kind,
&facts.file_path,
facts.is_test_context,
Coverage {
direct: facts.direct_roots,
transitive: facts.transitive_roots,
total_roots: facts.total_roots,
superseded_by_caller: facts.superseded_by_caller,
},
)
}
fn trace_ranked(facts: &RowFacts, sem: f32, score: f32) {
tracing::debug!(
target: "search_hybrid_context::rank",
score = score,
cosine = facts.cosine.unwrap_or(0.0),
cosine_norm = sem,
kind = facts.kind, name = facts.name, fqn = facts.fqn,
channel = facts.channel,
out_degree = facts.out_degree,
direct = facts.direct_roots, transitive = facts.transitive_roots,
total_roots = facts.total_roots,
superseded = facts.superseded_by_caller,
"ranked"
);
}
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;
fn ranked_pool(rows: Vec<serde_json::Value>, query: &str) -> Vec<serde_json::Value> {
rerank(with_pool_tail(rows), query)
}
fn with_pool_tail(mut rows: Vec<serde_json::Value>) -> Vec<serde_json::Value> {
let cosine_of =
|row: &serde_json::Value| row.get("score").and_then(|v| v.as_f64()).map(|c| c as f32);
let cosines: Vec<f32> = rows.iter().filter_map(cosine_of).collect();
let Some(max) = cosines.iter().copied().reduce(f32::max) else {
return rows;
};
let min = cosines.iter().copied().fold(max, f32::min);
let floor = max - SEMANTIC_WEIGHT;
if floor < min {
rows.push(json!({
"uuid": "pool-tail", "name": "zzz_inert_tail", "kind": "rust_impl",
"fqn": "inert::zzz_inert_tail",
"file_path": "src/zzz_inert_tail.rs", "start_line": 1,
"score": floor
}));
}
rows
}
#[test]
fn root_coverage_ladder_rewards_multi_root_callers() {
assert_eq!(
root_coverage_boost(
"function",
"src/a.rs",
false,
Coverage {
direct: 0,
transitive: 0,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
let one = root_coverage_boost(
"function",
"src/a.rs",
false,
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",
false,
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",
false,
Coverage {
direct: 3,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let saturated = root_coverage_boost(
"function",
"src/a.rs",
false,
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,
is_test_context: false,
},
) + root_coverage_boost(
"function",
"src/a.rs",
false,
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,
is_test_context: false,
},
) + root_coverage_boost(
"function",
"src/b.rs",
false,
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",
false,
Coverage {
direct: 0,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let transitive3 = root_coverage_boost(
"function",
"src/a.rs",
false,
Coverage {
direct: 0,
transitive: 3,
total_roots: 8,
superseded_by_caller: false,
},
);
let direct2 = root_coverage_boost(
"function",
"src/a.rs",
false,
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",
false,
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",
false,
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: true,
},
);
let outer = root_coverage_boost(
"function",
"src/hooks.ts",
false,
Coverage {
direct: 3,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
let plain = root_coverage_boost(
"function",
"src/hooks.ts",
false,
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!((step - superseded_step_boost(2, 0)).abs() < 1e-5);
assert!(step < plain, "the downgrade must lower the boost");
assert!(step > 0.0, "downgraded, not zeroed");
assert!(outer > plain, "outer orchestrator keeps the full boost");
assert!(
(superseded_step_boost(2, 0) - superseded_step_boost(0, 2)).abs() < 1e-6,
"superseded provenance must ignore the direct/transitive split"
);
assert!(
superseded_step_boost(3, 3) <= ROOT_TRANSITIVE_WEIGHT,
"the downgraded grade saturates at the transitive weight"
);
}
#[test]
fn root_coverage_never_rescues_tests_prose_or_config() {
assert_eq!(
root_coverage_boost(
"rust_function",
"tests/login_test.rs",
false,
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"markdown_section",
"docs/a.md",
false,
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"config_property",
"config/app.yml",
false,
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false
}
),
0.0
);
assert_eq!(
root_coverage_boost(
"build_dependency",
"Cargo.toml",
false,
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 = ranked_pool(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 = ranked_pool(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.57}),
json!({"uuid": "5", "name": "login", "kind": "rust_function",
"file_path": "src/auth.rs", "start_line": 30, "score": 0.62}),
];
let ranked = ranked_pool(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 = ranked_pool(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 = ranked_pool(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 = ranked_pool(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 = ranked_pool(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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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 = ranked_pool(
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,
is_test_context: false,
},
)
};
let bare = final_score(
0.30,
query,
Candidate {
name: "x",
kind: "markdown_section",
file_path: "docs/a.md",
context: None,
out_degree: 0,
is_test_context: false,
},
);
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,
is_test_context: false,
},
);
assert_eq!(boosted("config_property", "config/a.yml"), config_bare);
assert_eq!(
boosted("rust_function", "tests/foo_test.rs"),
SEMANTIC_WEIGHT * 0.30 + CALLABLE_BOOST + TEST_PATH_PENALTY
);
}
#[test]
fn is_test_entity_truth_table() {
let path_only = Candidate {
kind: "function",
file_path: "tests/foo_test.rs",
name: "x",
context: None,
out_degree: 0,
is_test_context: false,
};
let flag_only = Candidate {
kind: "function",
file_path: "src/auth/credentials.rs",
name: "x",
context: None,
out_degree: 0,
is_test_context: true,
};
let both = Candidate {
kind: "function",
file_path: "tests/foo_test.rs",
name: "x",
context: None,
out_degree: 0,
is_test_context: true,
};
let neither = Candidate {
kind: "function",
file_path: "src/auth/credentials.rs",
name: "x",
context: None,
out_degree: 0,
is_test_context: false,
};
assert!(is_test_entity(&path_only));
assert!(is_test_entity(&flag_only));
assert!(is_test_entity(&both));
assert!(!is_test_entity(&neither));
}
#[test]
fn test_context_flag_receives_penalty_and_ranks_below_production() {
let production = final_score(
0.40,
"authenticate user",
Candidate {
kind: "rust_function",
file_path: "src/auth/user.rs",
name: "login",
context: None,
out_degree: 0,
is_test_context: false,
},
);
let cfg_test_helper = final_score(
0.40,
"authenticate user",
Candidate {
kind: "rust_function",
file_path: "src/auth/credentials.rs",
name: "normalize_email_helper",
context: None,
out_degree: 0,
is_test_context: true,
},
);
assert!(
(cfg_test_helper - (production + TEST_PATH_PENALTY)).abs() < 1e-6,
"a #[cfg(test)] helper in a production path must pay the penalty"
);
assert!(cfg_test_helper < production);
let both = final_score(
0.40,
"authenticate user",
Candidate {
kind: "rust_function",
file_path: "tests/foo_test.rs",
name: "normalize_email_helper",
context: None,
out_degree: 0,
is_test_context: true,
},
);
assert_eq!(both, cfg_test_helper);
}
#[test]
fn row_without_is_test_context_scores_identically() {
let row = json!({
"name": "login",
"kind": "rust_function",
"file_path": "src/auth/user.rs",
"fqn": "auth::user::login",
"start_line": 10,
"uuid": "u1",
"score": 0.9
});
let facts = row_facts(&row);
assert!(!facts.is_test_context);
let scored = score_row(&facts, 0.50, "login");
let expected = final_score(
0.50,
"login",
Candidate {
kind: "rust_function",
file_path: "src/auth/user.rs",
name: "login",
context: Some("auth::user::login"),
out_degree: 0,
is_test_context: false,
},
);
assert_eq!(scored, expected);
}
#[test]
fn cfg_test_helper_does_not_earn_root_coverage_provenance() {
let flag_only = root_coverage_boost(
"rust_function",
"src/auth/credentials.rs",
true,
Coverage {
direct: 3,
transitive: 2,
total_roots: 8,
superseded_by_caller: false,
},
);
assert_eq!(flag_only, 0.0);
let production = root_coverage_boost(
"rust_function",
"src/a.rs",
false,
Coverage {
direct: 2,
transitive: 0,
total_roots: 8,
superseded_by_caller: false,
},
);
assert!(production > 0.0);
}
#[test]
fn is_test_row_matches_kind_and_flag_rules() {
assert!(is_test_row("markdown_section", "docs/a.md", false));
assert!(is_test_row("function", "src/a.rs", true));
assert!(!is_test_row("function", "src/a.rs", false));
assert!(is_test_row("function", "tests/a.rs", false));
}
#[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,
is_test_context: false,
},
);
let zero = final_score(
0.40,
"anything at all",
Candidate {
kind: "constant",
file_path: "src/a.ts",
name: "k",
context: None,
out_degree: 0,
is_test_context: false,
},
);
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,
is_test_context: false,
},
);
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,
is_test_context: false,
},
);
assert_eq!(
callable,
SEMANTIC_WEIGHT * 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 normalizer_maps_pool_to_unit_range() {
let sems = normalize_pool_cosines(&[Some(0.1), Some(0.2), Some(0.3)]);
assert!((sems[0] - 0.0).abs() < 1e-6, "{sems:?}");
assert!((sems[1] - 0.5).abs() < 1e-6, "{sems:?}");
assert!((sems[2] - 1.0).abs() < 1e-6, "{sems:?}");
}
#[test]
fn normalizer_flat_pool_is_zero_without_nan() {
let sems = normalize_pool_cosines(&[Some(0.5), Some(0.5), Some(0.5)]);
assert_eq!(sems, vec![0.0, 0.0, 0.0]);
assert!(sems.iter().all(|s| s.is_finite()), "no NaN on a flat pool");
}
#[test]
fn normalizer_single_candidate_is_zero() {
assert_eq!(normalize_pool_cosines(&[Some(0.7)]), vec![0.0]);
}
#[test]
fn normalizer_empty_pool_is_empty() {
assert!(normalize_pool_cosines(&[]).is_empty());
}
#[test]
fn normalizer_missing_cosine_is_pool_minimum() {
let sems = normalize_pool_cosines(&[Some(0.4), None, Some(0.2)]);
assert!((sems[0] - 1.0).abs() < 1e-6, "{sems:?}");
assert!((sems[1] - 0.0).abs() < 1e-6, "{sems:?}");
assert!((sems[2] - 0.0).abs() < 1e-6, "{sems:?}");
let with_inf = normalize_pool_cosines(&[Some(0.4), Some(f32::INFINITY), Some(0.2)]);
assert!(with_inf.iter().all(|s| s.is_finite()), "{with_inf:?}");
assert!((with_inf[1] - 0.0).abs() < 1e-6, "{with_inf:?}");
}
#[test]
fn normalizer_is_affine_invariant() {
let base = [Some(0.24), Some(0.31), Some(0.40), Some(0.53)];
for (scale, offset) in [(0.25_f32, 0.55_f32), (3.0, -0.10), (0.1, 0.81)] {
let shifted: Vec<Option<f32>> = base
.iter()
.map(|c| c.map(|v| (v - 0.24) / 0.29 * scale + offset))
.collect();
let a = normalize_pool_cosines(&base);
let b = normalize_pool_cosines(&shifted);
for (x, y) in a.iter().zip(&b) {
assert!((x - y).abs() < 1e-5, "affine drift: {a:?} vs {b:?}");
}
}
}
#[test]
fn normalizer_clamps_outliers_into_unit_range() {
let sems =
normalize_pool_cosines(&[Some(0.99), Some(0.31), Some(0.30), Some(0.29), Some(-0.42)]);
assert!(
sems.iter().all(|s| (0.0..=1.0).contains(s)),
"out of range: {sems:?}"
);
assert!((sems[0] - 1.0).abs() < 1e-6, "{sems:?}");
assert!((sems[4] - 0.0).abs() < 1e-6, "{sems:?}");
}
#[test]
fn normalizer_is_deterministic() {
let pool = [Some(0.31), None, Some(0.52), Some(0.41), Some(0.52)];
assert_eq!(normalize_pool_cosines(&pool), normalize_pool_cosines(&pool));
}
#[test]
fn rerank_order_is_invariant_to_cosine_scale() {
let rows = |cosines: [f32; 6]| {
vec![
json!({"uuid": "1", "name": "setup", "kind": "markdown_section",
"file_path": "docs/AUTH.md", "start_line": 1, "score": cosines[0]}),
json!({"uuid": "2", "name": "test_login_success", "kind": "rust_function",
"file_path": "tests/login_test.rs", "start_line": 10, "score": cosines[1]}),
json!({"uuid": "3", "name": "handle_login", "kind": "rust_function",
"fqn": "app::routes::handle_login",
"file_path": "src/routes.rs", "start_line": 20, "score": cosines[2]}),
json!({"uuid": "4", "name": "normalize_email", "kind": "rust_function",
"fqn": "app::util::normalize_email",
"file_path": "src/util.rs", "start_line": 5, "score": cosines[3]}),
json!({"uuid": "5", "name": "login", "kind": "rust_function",
"fqn": "app::auth::login",
"file_path": "src/auth.rs", "start_line": 30, "score": cosines[4],
"caller_roots": 2, "caller_roots_transitive": 1,
"caller_root_total": 8}),
json!({"uuid": "6", "name": "AuthConfig", "kind": "rust_struct",
"fqn": "app::auth::AuthConfig",
"file_path": "src/auth.rs", "start_line": 12, "score": cosines[5]}),
]
};
let order = |ranked: Vec<serde_json::Value>| -> Vec<String> {
ranked
.iter()
.filter_map(|e| e.get("name").and_then(|v| v.as_str()))
.map(String::from)
.collect()
};
let query = "authenticate user with email and password";
let wide = order(rerank(rows([0.53, 0.47, 0.38, 0.41, 0.43, 0.24]), query));
let narrow = order(rerank(
rows([0.53, 0.47, 0.38, 0.41, 0.43, 0.24].map(|c| (c - 0.24) / 0.29 * 0.07 + 0.81)),
query,
));
assert_eq!(wide, narrow, "ranking must not depend on the cosine band");
}
#[test]
fn rerank_baselines_hold_across_every_supported_model_band() {
const BANDS: &[(&str, f32, f32)] = &[
("AllMiniLML6V2", 0.24, 0.53),
("BGESmallENV15", 0.54, 0.80),
("BGEBaseENV15", 0.44, 0.77),
("MultilingualE5Small", 0.81, 0.88),
("JinaEmbeddingsV2BaseCode", 0.15, 0.69),
("NomicEmbedTextV15", 0.55, 0.74),
];
for (model, low, high) in BANDS {
let at = |fraction: f32| low + (high - low) * fraction;
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": at(1.0)}),
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": at(0.93)}),
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": at(0.90)}),
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": at(0.76),
"caller_roots": 3, "caller_roots_transitive": 0,
"caller_root_total": 8}),
json!({"uuid": "5", "name": "connection pooling", "kind": "markdown_section",
"fqn": "README.md::connection pooling",
"file_path": "README.md", "start_line": 4, "score": at(0.83)}),
json!({"uuid": "6", "name": "testConnectionBorrow", "kind": "method",
"fqn": "com.zaxxer.hikari.pool.TestConnections.testConnectionBorrow",
"file_path": "src/test/java/com/zaxxer/hikari/pool/TestConnections.java",
"start_line": 61, "score": at(0.88)}),
],
"borrow a connection from the pool",
);
let names: Vec<&str> = ranked
.iter()
.filter_map(|e| e.get("name").and_then(|v| v.as_str()))
.collect();
assert_eq!(
names.first(),
Some(&"getConnection"),
"{model} band ({low}..{high}) broke the HikariCP baseline: {names:?}"
);
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "normalize_email", "kind": "rust_function",
"fqn": "jobwatch::auth::credentials::normalize_email",
"file_path": "src/auth/credentials.rs", "start_line": 71,
"score": at(1.0)}),
json!({"uuid": "2", "name": "verify_password", "kind": "rust_function",
"fqn": "jobwatch::auth::credentials::verify_password",
"file_path": "src/auth/credentials.rs", "start_line": 41,
"score": at(0.94)}),
json!({"uuid": "3", "name": "test_login_success", "kind": "rust_function",
"fqn": "tests::login::test_login_success",
"file_path": "tests/login_test.rs", "start_line": 10,
"score": at(0.90)}),
json!({"uuid": "4", "name": "login", "kind": "rust_function",
"fqn": "jobwatch::api::auth::login",
"file_path": "src/api/auth.rs", "start_line": 136,
"score": at(0.72),
"caller_roots": 3, "caller_roots_transitive": 1,
"caller_root_total": 8}),
json!({"uuid": "5", "name": "create_user", "kind": "rust_function",
"fqn": "jobwatch::api::admin::create_user",
"file_path": "src/api/admin.rs", "start_line": 44,
"score": at(0.86), "caller_roots": 1,
"caller_root_total": 8}),
],
"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();
assert_eq!(
names.first(),
Some(&"login"),
"{model} band ({low}..{high}) broke the job-watch baseline: {names:?}"
);
}
}
#[test]
fn rerank_compressed_band_does_not_let_boosts_outrank_semantics() {
const BANDS: &[(&str, f32, f32)] = &[
("AllMiniLML6V2", 0.24, 0.53),
("BGESmallENV15", 0.54, 0.80),
("BGEBaseENV15", 0.44, 0.77),
("MultilingualE5Small", 0.81, 0.88),
("JinaEmbeddingsV2BaseCode", 0.15, 0.69),
("NomicEmbedTextV15", 0.55, 0.74),
];
for (model, low, high) in BANDS {
let ranked = rerank(
vec![
json!({"uuid": "1", "name": "getScreenRecorder", "kind": "method",
"fqn": "McpContext.getScreenRecorder",
"file_path": "src/McpContext.ts", "start_line": 411,
"score": low}),
json!({"uuid": "2", "name": "screenshot", "kind": "constant",
"fqn": "screenshot",
"file_path": "src/tools/screenshot.ts", "start_line": 21,
"score": high}),
],
"capture the current view as an image",
);
assert_eq!(
ranked[0]["name"], "screenshot",
"{model} band ({low}..{high}): the kind boost swallowed the semantic span"
);
}
}
#[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);
}
}