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): don't emit a section that the next one repeats whole

Tom Villanidc6353059423
diff --git a/engine/crates/xerj-autoindex/src/extract/mod.rs b/engine/crates/xerj-autoindex/src/extract/mod.rs
index 314b44abb..5bc60756a 100644
--- a/engine/crates/xerj-autoindex/src/extract/mod.rs
+++ b/engine/crates/xerj-autoindex/src/extract/mod.rs
@@ -467,7 +467,12 @@ pub fn for_each_section(text: &str, emit: &mut dyn FnMut(String) -> bool) -> boo
let mut cur = String::new();
for para in text.split("\n\n") {
- if !cur.is_empty() && cur.len() + para.len() > SECTION_CHARS {
+ // `cur.len() > SECTION_OVERLAP`: a shorter section would be carried
+ // WHOLE into the next as its overlap, so emitting it adds a record
+ // with no text of its own (a heading before a long first paragraph).
+ // It stays in `cur` and leads the next section instead.
+ if !cur.is_empty() && cur.len() + para.len() > SECTION_CHARS && cur.len() > SECTION_OVERLAP
+ {
let done = std::mem::take(&mut cur);
let carry = tail(&done, SECTION_OVERLAP);
if !emit(done) {
@@ -724,6 +729,43 @@ mod section_tests {
}
}
+ /// A short paragraph (a heading) followed by one too big to join it was
+ /// emitted as a section of its own, and the next section then repeated it
+ /// whole as its overlap: a record with no text of its own. Seen as
+ /// "CHAPTER 26. Knights and Squires." alone on a Gutenberg EPUB.
+ #[test]
+ fn a_section_the_next_would_repeat_whole_is_not_emitted() {
+ // Each ~2100 bytes: too big to join the heading in one section.
+ let long = |i: usize| format!("w{i} ").repeat(700);
+ let t = format!(
+ "CHAPTER 26. Knights and Squires.\n\n{}\n\n{}\n\n{}",
+ long(1),
+ long(2),
+ long(3)
+ );
+ let secs = split_sections(&t);
+ assert!(
+ secs[0].starts_with("CHAPTER 26.") && secs[0].contains("w1 w1"),
+ "the heading leads a real section: {:?}",
+ &secs[0][..secs[0].len().min(60)]
+ );
+ for w in secs.windows(2) {
+ assert!(
+ !w[1].starts_with(w[0].as_str()),
+ "a section repeated whole by its successor: {:?}",
+ w[0]
+ );
+ }
+ let short = format!("{}\n\nCHAPTER 26.", "y".repeat(SECTION_CHARS));
+ assert!(
+ split_sections(&short)
+ .last()
+ .unwrap()
+ .contains("CHAPTER 26."),
+ "a short LAST paragraph is still emitted"
+ );
+ }
+
/// A single paragraph larger than two sections must still be bounded.
#[test]
fn pathological_single_paragraph_is_hard_split() {
@@ -735,7 +777,8 @@ mod section_tests {
}
}
- /// The pre-#239 implementation, kept verbatim as an oracle. It is quadratic
+ /// The pre-#239 implementation, kept as an oracle (verbatim but for the
+ /// redundant-section rule, changed in both in step). It is quadratic
/// — only ever call it on inputs of a few hundred KB.
fn legacy_split_sections(text: &str) -> Vec<String> {
if text.len() <= SECTION_CHARS {
@@ -744,7 +787,13 @@ mod section_tests {
let mut out: Vec<String> = Vec::new();
let mut cur = String::new();
for para in text.split("\n\n") {
- if !cur.is_empty() && cur.len() + para.len() > SECTION_CHARS {
+ // The one deliberate change since #239, mirrored from
+ // `for_each_section`: a section its successor would repeat whole
+ // is not emitted.
+ if !cur.is_empty()
+ && cur.len() + para.len() > SECTION_CHARS
+ && cur.len() > SECTION_OVERLAP
+ {
let done = std::mem::take(&mut cur);
let carry = tail(&done, SECTION_OVERLAP);
out.push(done);
@@ -791,6 +840,12 @@ mod section_tests {
doc(200, 400),
doc(60, 300),
doc(3, 9000),
+ // A short paragraph before ones too big to join it, at the
+ // overlap boundary and one byte either side.
+ format!("{}\n\n{}", "h".repeat(32), doc(4, 2100)),
+ format!("{}\n\n{}", "h".repeat(SECTION_OVERLAP - 1), doc(4, 2100)),
+ format!("{}\n\n{}", "h".repeat(SECTION_OVERLAP), doc(4, 2100)),
+ format!("{}\n\n{}", "h".repeat(SECTION_OVERLAP + 1), doc(4, 2100)),
// Multi-byte, with the cut landing inside a char: 3-byte and 4-byte
// sequences do not divide SECTION_CHARS evenly.
"é".repeat(4 * SECTION_CHARS),