AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
search: cite the live name of a deduplicated file, not its backup copy (#2665)
xerj-orga41848713db2
diff --git a/engine/crates/xerj-common/src/code_query.rs b/engine/crates/xerj-common/src/code_query.rsindex 518809fa8..78568abdf 100644--- a/engine/crates/xerj-common/src/code_query.rs+++ b/engine/crates/xerj-common/src/code_query.rs@@ -563,7 +563,9 @@ fn build_search_query("query": { "bool": { "should": should }},// No `body` here: whole-file bodies are the token flood (measured ~9x// the size of the matching passage). `_passage` carries the snippet.- "_source": ["ax_path", "ax_locator", "line", "start_line", "language", "kind", "code", "title", "heading", "page", "cell"],+ // `ax_paths` lists every name a deduplicated file has, so the hit can+ // cite the live name instead of a backup copy (#2402).+ "_source": ["ax_path", "ax_paths", "ax_locator", "line", "start_line", "language", "kind", "code", "title", "heading", "page", "cell"],"fields": ["_passage"]})}diff --git a/engine/crates/xerj-common/src/xccode/passage.rs b/engine/crates/xerj-common/src/xccode/passage.rsindex 0d6370918..32b3753e6 100644--- a/engine/crates/xerj-common/src/xccode/passage.rs+++ b/engine/crates/xerj-common/src/xccode/passage.rs@@ -587,14 +587,17 @@ fn push_term_text(out: &mut String, text: &str) {/// does not contain a term, or whose passage start line is not known, is/// left untouched — a missing field, not a guessed line.pub fn annotate_search_match_lines(resp: &mut Value, query: &str) -> bool {+ // #2402: every search surface (CLI, `xerj_search`, batch) runs this pass,+ // so it is also where a deduplicated hit stops citing its backup copy.+ let renamed = super::render::prefer_live_alias_paths(resp);let terms = match_query_terms(query);if terms.is_empty() {- return false;+ return renamed;}let Some(hits) = resp.pointer_mut("/hits/hits").and_then(Value::as_array_mut) else {- return false;+ return renamed;};- let mut changed = false;+ let mut changed = renamed;for hit in hits {if stamp_match_line(hit, &terms) {changed = true;diff --git a/engine/crates/xerj-common/src/xccode/render.rs b/engine/crates/xerj-common/src/xccode/render.rsindex cafb02e26..b2a13e059 100644--- a/engine/crates/xerj-common/src/xccode/render.rs+++ b/engine/crates/xerj-common/src/xccode/render.rs@@ -280,6 +280,97 @@ pub fn source_looks_archived(src: &Value) -> bool {path_is_archived(path)}+/// True when `path` names a backup or archived copy rather than the live file:+/// an archive directory ([`path_is_archived`]), a backup directory+/// (`backup/`, `backups/`, `bak/`, `old/`, `备份/`, `root-bak-20261001/`), or a+/// backup file name (`notes.md.bak`, `notes.md.orig`, `notes.md~`,+/// `notes.md.old`).+///+/// Only used to choose WHICH name of a byte-identical group is cited, so a+/// false positive costs nothing but the name shown (#2402).+pub fn path_looks_like_copy(path: &str) -> bool {+ if path_is_archived(path) {+ return true;+ }+ let segments: Vec<&str> = path.split(['/', '\\']).filter(|s| !s.is_empty()).collect();+ let Some((file, dirs)) = segments.split_last() else {+ return false;+ };+ let dir_is_copy = dirs.iter().any(|seg| {+ let s = seg.to_lowercase();+ matches!(+ s.as_str(),+ "backup" | "backups" | "bak" | "_backup" | "_backups" | "old" | "_old" | "备份"+ ) || s.starts_with("bak-")+ || s.starts_with("bak_")+ || s.starts_with("backup-")+ || s.starts_with("backup_")+ || s.ends_with("-bak")+ || s.ends_with("_bak")+ || s.ends_with(".bak")+ || s.contains("-bak-")+ || s.contains("_bak_")+ });+ let f = file.to_lowercase();+ dir_is_copy+ || f.ends_with('~')+ || [".bak", ".orig", ".old", ".backup"]+ .iter()+ .any(|ext| f.ends_with(ext))+}++/// Cite the live name of a deduplicated file, not its backup copy (#2402).+///+/// Byte-identical files are indexed once; the record keeps every name in+/// `ax_paths` and the canonical `ax_path` is the lowest native path, which is+/// stable for the sync manifest but is often `archive/...` or+/// `backups/...` — so a search answered with the backup's path even though+/// `docs/...` holds the same bytes. When the cited path looks like a copy+/// ([`path_looks_like_copy`]) and another name in `ax_paths` does not, the hit+/// cites that name instead (`ax_path`, and `path` when it mirrored it). The+/// bytes are identical, so lines and passages are unchanged; `ax_paths` and+/// `ax_file` are untouched. Returns whether any hit changed.+pub fn prefer_live_alias_paths(resp: &mut Value) -> bool {+ let Some(hits) = resp.pointer_mut("/hits/hits").and_then(Value::as_array_mut) else {+ return false;+ };+ let mut changed = false;+ for hit in hits {+ let Some(src) = hit.get_mut("_source").and_then(Value::as_object_mut) else {+ continue;+ };+ let Some(cited) = src+ .get("ax_path")+ .and_then(Value::as_str)+ .map(str::to_owned)+ else {+ continue;+ };+ if !path_looks_like_copy(&cited) {+ continue;+ }+ let live = src+ .get("ax_paths")+ .and_then(Value::as_array)+ .and_then(|paths| {+ paths+ .iter()+ .filter_map(Value::as_str)+ .find(|p| *p != cited && !path_looks_like_copy(p))+ .map(str::to_owned)+ });+ let Some(live) = live else {+ continue;+ };+ if src.get("path").and_then(Value::as_str) == Some(cited.as_str()) {+ src.insert("path".into(), Value::String(live.clone()));+ }+ src.insert("ax_path".into(), Value::String(live));+ changed = true;+ }+ changed+}+/// Move a one-term symbol definition behind hits that cover more of a/// multi-term question (#2327).///@@ -776,6 +867,78 @@ mod tests {use super::*;use serde_json::json;+ #[test]+ fn identical_copies_cite_the_live_name_not_the_backup() {+ // #2402: the four names of one byte-identical file. The canonical+ // (lowest path) is the archived backup; the hit must cite docs/.+ let mut resp = json!({"hits":{"hits":[{"_source":{+ "ax_path":"archive/backup-cn/root-bak-20261001/models.md",+ "path":"archive/backup-cn/root-bak-20261001/models.md",+ "ax_paths":[+ "archive/backup-cn/root-bak-20261001/models.md",+ "docs/models.md",+ "docs/models.md.orig",+ "evidence/backups/models.md.bak"+ ],+ "ax_file":"axf2-x"+ }}]}});+ assert!(prefer_live_alias_paths(&mut resp));+ let src = &resp["hits"]["hits"][0]["_source"];+ assert_eq!(src["ax_path"], "docs/models.md");+ assert_eq!(src["path"], "docs/models.md");+ assert_eq!(src["ax_paths"].as_array().unwrap().len(), 4);+ assert_eq!(src["ax_file"], "axf2-x");+ assert!(!source_looks_archived(src));+ }++ #[test]+ fn live_canonical_or_all_copies_are_left_alone() {+ let mut live = json!({"hits":{"hits":[{"_source":{+ "ax_path":"docs/a.md","ax_paths":["docs/a.md","backups/a.md"]}}]}});+ assert!(!prefer_live_alias_paths(&mut live));+ assert_eq!(live["hits"]["hits"][0]["_source"]["ax_path"], "docs/a.md");+ let mut copies = json!({"hits":{"hits":[{"_source":{+ "ax_path":"backups/a.md","ax_paths":["backups/a.md","old/a.md.bak"]}}]}});+ assert!(!prefer_live_alias_paths(&mut copies));+ let mut single = json!({"hits":{"hits":[{"_source":{"ax_path":"backups/a.md"}}]}});+ assert!(!prefer_live_alias_paths(&mut single));+ }++ #[test]+ fn search_bodies_fetch_every_alias_name() {+ // Without `ax_paths` in `_source` the hit never sees the live name+ // and keeps citing the backup copy (#2402).+ for body in [+ crate::code_query::search_query_body("GGUF quantized weights", 5),+ crate::code_query::search_query_body_for_mapping("GGUF", 5, None, None),+ ] {+ let source = body["_source"].as_array().expect("_source list");+ assert!(source.iter().any(|f| f == "ax_paths"), "{body}");+ assert!(source.iter().any(|f| f == "ax_path"), "{body}");+ }+ }++ #[test]+ fn copy_names_are_recognised_without_catching_ordinary_code() {+ for p in [+ "evidence/backups/x.md.bak",+ "a/备份/root-bak-20261001/x.md",+ "notes.md~",+ "src/lib.rs.orig",+ "archive/x.md",+ ] {+ assert!(path_looks_like_copy(p), "{p}");+ }+ for p in [+ "src/backup.rs",+ "docs/models.md",+ "crates/bakery/src/lib.rs",+ "src/old_api.rs",+ ] {+ assert!(!path_looks_like_copy(p), "{p}");+ }+ }+fn hit(loc_fields: Value, body: &str, symbols: Value, score: f64) -> Value {let mut src = loc_fields;src["body"] = json!(body);diff --git a/engine/crates/xerj-mcp/src/lib.rs b/engine/crates/xerj-mcp/src/lib.rsindex 60208c952..265a2d5ce 100644--- a/engine/crates/xerj-mcp/src/lib.rs+++ b/engine/crates/xerj-mcp/src/lib.rs@@ -2637,6 +2637,9 @@ fn build_search_with_mapping(body.entry("_source".to_string()).or_insert_with(|| {json!(["ax_path",+ // Every name of a deduplicated file, so the hit cites the+ // live one rather than a backup copy (#2402).+ "ax_paths","ax_file","ax_locator","path",