Skip to content

armgit / xerj

Public Imported from GitHub
0

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.

fix(autoindex,common): bound every container's impl, and let end_line only tighten passage spans

xerj-org44121260c45a
diff --git a/engine/crates/xerj-autoindex/src/extract/code.rs b/engine/crates/xerj-autoindex/src/extract/code.rs
index 15e338617..d92024e44 100644
--- a/engine/crates/xerj-autoindex/src/extract/code.rs
+++ b/engine/crates/xerj-autoindex/src/extract/code.rs
@@ -1424,13 +1424,16 @@ fn emit_code_doc(
// #1237: symbol records carry a bounded `impl` slice (`body[line..=end_line]`,
// capped at 16 KB on a UTF-8 char boundary) so `xerj code --fuse-symbols`
// can score a second BM25 leg over definition bodies without length-norm
- // dilution from a 4,000-line file. For container kinds (`class`), bound
- // `impl` to the lines preceding the first nested definition so method
- // bodies are not duplicated inside the enclosing class record.
+ // dilution from a 4,000-line file. For every kind except
+ // `function`/`method` (i.e. containers: `class`, `module`, `trait`,
+ // `interface`, `impl`, `struct`, `extension`, ...), bound `impl` to the
+ // lines preceding the first nested definition so member bodies are not
+ // duplicated inside the enclosing record. Functions keep their whole
+ // body: a nested closure/inner def is part of what the function does.
// `reconcile_plan::ensure_compatible` exempts `impl` and `end_line` on
// pre-#1237 frozen datasets so incremental runs over older state dirs
// never abort.
- let impl_end_line = if s.kind == "class" {
+ let impl_end_line = if !matches!(s.kind.as_str(), "function" | "method") {
symbols
.iter()
.filter(|child| child.line > line && child.line <= s.end_line)
@@ -4483,6 +4486,44 @@ mod tests {
);
}
+ /// The container bound is not specific to `class`: a Rust `mod` holding a
+ /// function must not copy that function's body into its own `impl`.
+ #[test]
+ fn emit_code_doc_bounds_every_container_kind_before_nested_defs() {
+ let mut records = Vec::new();
+ let mut stats = ExtractStats::default();
+ let src = "mod engine {\n use std::fmt;\n fn spin_up() {\n let unique_marker = 1;\n }\n}\n";
+ let s = syms("rust", src);
+ assert!(has(&s, "engine", "module"), "got {s:?}");
+ assert!(has(&s, "spin_up", "function"), "got {s:?}");
+ assert!(emit_code_doc(
+ Path::new("lib.rs"),
+ "rust",
+ src,
+ &s,
+ &mut stats,
+ &mut |rec| {
+ records.push(rec);
+ true
+ },
+ ));
+ let module_impl = records
+ .iter()
+ .find(|r| r.locator == "code:1:engine")
+ .and_then(|r| r.fields.get("impl"))
+ .and_then(Value::as_str)
+ .expect("module impl");
+ assert_eq!(module_impl, "mod engine {\n use std::fmt;");
+ assert!(!module_impl.contains("unique_marker"));
+ let fn_impl = records
+ .iter()
+ .find(|r| r.locator == "code:3:spin_up")
+ .and_then(|r| r.fields.get("impl"))
+ .and_then(Value::as_str)
+ .expect("function impl");
+ assert!(fn_impl.contains("unique_marker"), "{fn_impl}");
+ }
+
#[test]
fn emit_code_doc_records_symbol_impl_and_bounds_class_before_methods() {
let mut records = Vec::new();
diff --git a/engine/crates/xerj-common/src/xccode/passage.rs b/engine/crates/xerj-common/src/xccode/passage.rs
index 312067a4f..8a99228ce 100644
--- a/engine/crates/xerj-common/src/xccode/passage.rs
+++ b/engine/crates/xerj-common/src/xccode/passage.rs
@@ -38,8 +38,19 @@ pub fn query_terms(query: &str) -> Vec<String> {
}
/// `[(name, kind, start_line, end_line)]` from the record's own `symbols`.
-/// Uses recorded `end_line` when present (#1237); otherwise falls back to
-/// running until the next definition starts.
+/// A definition runs until the next definition starts — a coarse but honest
+/// bound: it never claims a range the index did not report. When the record
+/// carries the symbol's own `end_line` (#1237), the span also stops there, so
+/// trailing non-definition lines (comments, top-level statements) after a
+/// function are not scored as part of it.
+///
+/// The recorded `end_line` only ever TIGHTENS the next-definition bound; it
+/// never extends a span past a nested definition. A container (`class`,
+/// `impl`, `module`, ...) records an `end_line` covering every method inside
+/// it, so its full extent is a superset of each method's text: it would
+/// out-score (or tie and, being earlier, beat) the method that actually
+/// matches, and the 800-char truncation would then show the container's head
+/// instead of the matching method.
fn symbol_spans(src: &Value) -> Vec<(String, String, i64, Option<i64>)> {
let Some(syms) = src.get("symbols").and_then(Value::as_array) else {
return Vec::new();
@@ -72,7 +83,11 @@ fn symbol_spans(src: &Value) -> Vec<(String, String, i64, Option<i64>)> {
got.windows(2)
.map(|w| {
let (name, kind, line, end_line) = w[0].clone();
- let end = end_line.or_else(|| w.get(1).map(|(_, _, l, _)| *l - 1));
+ let next_def = w.get(1).map(|(_, _, l, _)| *l - 1);
+ let end = match (end_line, next_def) {
+ (Some(e), Some(n)) => Some(e.min(n)),
+ (e, n) => e.or(n),
+ };
(name, kind, line, end)
})
.collect::<Vec<_>>()
@@ -382,6 +397,27 @@ mod tests {
assert_eq!(commaus(1_234_567), "1,234,567");
}
+ /// A container's recorded `end_line` covers its methods; it must not let
+ /// the container swallow (and out-rank) the method that actually matches.
+ #[test]
+ fn recorded_end_line_does_not_let_a_container_swallow_its_methods() {
+ let mut body = String::from("class Widget:\n");
+ for i in 0..60 {
+ body.push_str(&format!(" pad_{i} = {i}\n"));
+ }
+ body.push_str(" def render(self):\n return needle\n");
+ let render_line = 62;
+ let src = json!({
+ "symbols": [
+ { "name": "Widget", "kind": "class", "line": 1, "end_line": 63 },
+ { "name": "render", "kind": "method", "line": render_line, "end_line": 63 }
+ ]
+ });
+ let (text, label) = symbol_passage(&body, &src, "needle", 800).unwrap();
+ assert_eq!(label, format!("method render @ line {render_line}"));
+ assert!(text.contains("return needle"), "{text}");
+ }
+
#[test]
fn symbol_passage_bounds_span_by_recorded_end_line() {
let body = "fn alpha() {\n let needle = 1;\n}\n// trailing comment mentioning needle needle needle\nfn beta() {}\n";