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(xerj-autoindex): a 404 from the resume cleanup is an answer, and an uncountable node is UNKNOWN — not "wrote no new records"

xerj-orgc3d109767bc0
diff --git a/engine/crates/xerj-autoindex/src/esclient.rs b/engine/crates/xerj-autoindex/src/esclient.rs
index 2e8f09764..08301f8eb 100644
--- a/engine/crates/xerj-autoindex/src/esclient.rs
+++ b/engine/crates/xerj-autoindex/src/esclient.rs
@@ -1669,6 +1669,16 @@ impl Es {
}
/// One server-side delete pass; returns the reported `deleted` count.
+ ///
+ /// A 404 is the answer, not a failure — the same rule as
+ /// [`Self::count_endpoint`]: this cleanup runs before a file-level
+ /// replacement against the index the journal names, and when a namespace
+ /// swap already retired that index the node answers 404. The stale
+ /// documents are then already absent (a missing index cannot hold any),
+ /// so the pass deleted nothing and the goal state holds. Treating it as
+ /// an abort (#1173) turned a converged state into a dead run whose final
+ /// message then claimed "wrote no new records" on a run that had written
+ /// plenty. Every other status keeps the refusal error.
fn delete_by_query_pass(&self, index: &str, query: &Value) -> Result<u64> {
self.with_retry(
"delete_by_query",
@@ -1682,6 +1692,9 @@ impl Es {
.map_err(|e| anyhow!("delete_by_query: {e}"))
},
|resp| {
+ if resp.status() == reqwest::StatusCode::NOT_FOUND {
+ return Ok(0);
+ }
let status = resp.status();
let body: Value = resp.json().unwrap_or(Value::Null);
if status.is_success()
@@ -2352,6 +2365,57 @@ mod tests {
}
}
+ /// #1173: the cleanup before a file-level replacement targets the index
+ /// the journal names. When a namespace swap already retired it the node
+ /// answers 404, and the client turned that converged state ("the stale
+ /// documents are already absent — a missing index cannot hold any") into
+ /// a fatal abort. 404 must be the answer "deleted nothing", the same rule
+ /// as `count_endpoint`'s, and it must be answered on the FIRST request —
+ /// convergent is not a thing to retry.
+ #[test]
+ fn delete_by_query_on_a_missing_index_is_already_clean_not_an_abort() {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let address = listener.local_addr().unwrap();
+ let requests = Arc::new(Mutex::new(Vec::new()));
+ let requests_server = requests.clone();
+ let server = std::thread::spawn(move || {
+ let (mut stream, _) = listener.accept().unwrap();
+ requests_server
+ .lock()
+ .unwrap()
+ .push(read_request(&mut stream));
+ respond_status(
+ &mut stream,
+ "404 Not Found",
+ br#"{"error":{"type":"index_not_found_exception","status":404},"status":404}"#,
+ );
+ });
+ let es = Es::with_bulk_policy(
+ &format!("http://{address}"),
+ None,
+ Duration::from_millis(100),
+ Duration::from_millis(10),
+ Duration::from_millis(20),
+ )
+ .unwrap();
+ es.delete_by_query(
+ "retired-generation",
+ &serde_json::json!({"term": {"ax_file": "key"}}),
+ )
+ .expect("a missing index is already clean, not a failed cleanup");
+ server.join().unwrap();
+ let requests = requests.lock().unwrap();
+ assert_eq!(
+ requests.len(),
+ 1,
+ "a 404 is answered, not retried: {:#?}",
+ requests
+ .iter()
+ .map(|r| String::from_utf8_lossy(r).to_string())
+ .collect::<Vec<_>>()
+ );
+ }
+
/// #345: the reporter's whole issue was `delete_by_query: HTTP 500
/// Internal Server Error` and nothing else, so it was filed "not
/// investigated". The server had already said why — every by-query refusal
diff --git a/engine/crates/xerj-autoindex/src/xc.rs b/engine/crates/xerj-autoindex/src/xc.rs
index 7da47e7c0..51970dcf8 100644
--- a/engine/crates/xerj-autoindex/src/xc.rs
+++ b/engine/crates/xerj-autoindex/src/xc.rs
@@ -1341,6 +1341,33 @@ fn pack_record_gap(name: &str, root: &Path, docs: Option<u64>) -> Option<(u64, u
(files > 0 && docs < lines).then_some((lines, files))
}
+/// What a failed legacy-mode run can honestly claim about the records it
+/// wrote (#1173). The three cases are exclusive, and the whole point of the
+/// type is which one is forbidden when: "wrote no new records" asserted from
+/// a count the node never answered is an absence claim with no evidence —
+/// exactly the honest-claims rule the public docs are held to, applied to
+/// our own tooling. The node under a failed long build is precisely the node
+/// that answers counts with UNKNOWN.
+enum LegacyFailureVerdict {
+ /// The count grew: records from THIS run are queryable despite the exit.
+ Salvaged { before: u64, after: u64 },
+ /// The node could not be counted (before, after, or both). The honest
+ /// claim is "unknown" — never "absent".
+ CountUnknown,
+ /// Both counts are known and did not grow.
+ WroteNothing { before: u64, after: u64 },
+}
+
+fn legacy_failure_verdict(before: Option<u64>, after: Option<u64>) -> LegacyFailureVerdict {
+ match (before, after) {
+ (Some(before), Some(after)) if after > before => {
+ LegacyFailureVerdict::Salvaged { before, after }
+ }
+ (Some(before), Some(after)) => LegacyFailureVerdict::WroteNothing { before, after },
+ _ => LegacyFailureVerdict::CountUnknown,
+ }
+}
+
/// The build/verify/swap flow over an injected node, autoindex runner and
/// clock (#1004) — the destructive-operation contracts are pinned by the
/// tests below against a [`FakeNode`](tests::FakeNode), which is why none of
@@ -1619,23 +1646,39 @@ fn corpus_index_flow(
// lying around" (salvaging the latter would date stale data to
// now, which is worse than no index).
let before_known = count_under(node, &format!("xc-{name}"));
- let docs_before = before_known.unwrap_or(0);
let rc = run_autoindex(&format!("xc-{name}"), None);
let mut salvaged = false;
let mut docs: Option<u64> = None;
if rc != 0 && rc != 3 {
docs = count_under(node, &format!("xc-{name}"));
- if before_known.is_some() && docs.is_some_and(|d| d > docs_before) {
- salvaged = true;
- eprintln!("xerj corpus index: WARNING — autoindex exited {rc}, but this run wrote records");
- eprintln!("xerj corpus index: ({docs_before} -> {}). The corpus is queryable and is being", docs.unwrap_or(0));
- eprintln!("xerj corpus index: recorded as indexed, with autoindex_exit={rc} in its state file.");
- eprintln!("xerj corpus index: Coverage is not guaranteed — please report the error above.");
- } else {
- eprintln!("xerj corpus index: autoindex failed with exit {rc} and wrote no new records.");
- eprintln!("xerj corpus index: If the error above says the state directory cannot become generation");
- eprintln!("xerj corpus index: authority, or that --fresh is refused, run: xerj corpus index {name} --fresh");
- return rc;
+ match legacy_failure_verdict(before_known, docs) {
+ LegacyFailureVerdict::Salvaged { before, after } => {
+ salvaged = true;
+ eprintln!("xerj corpus index: WARNING — autoindex exited {rc}, but this run wrote records");
+ eprintln!("xerj corpus index: ({before} -> {after}). The corpus is queryable and is being");
+ eprintln!("xerj corpus index: recorded as indexed, with autoindex_exit={rc} in its state file.");
+ eprintln!("xerj corpus index: Coverage is not guaranteed — please report the error above.");
+ }
+ // #1173: a node that cannot be counted cannot be reported
+ // on. "Wrote no new records" was asserted here while the
+ // count was UNKNOWN — the honest-claims rule applied to
+ // our own tooling: say what is unknown, never claim an
+ // absence the node never confirmed.
+ LegacyFailureVerdict::CountUnknown => {
+ eprintln!("xerj corpus index: autoindex exited {rc} and the node could not be counted");
+ eprintln!("xerj corpus index: afterwards, so whether this run wrote records is UNKNOWN (not absent).");
+ eprintln!("xerj corpus index: Count again once the node answers, or rebuild beside the old");
+ eprintln!(
+ "xerj corpus index: index with: xerj corpus index {name} --fresh"
+ );
+ return rc;
+ }
+ LegacyFailureVerdict::WroteNothing { before, after } => {
+ eprintln!("xerj corpus index: autoindex failed with exit {rc} and wrote no new records ({before} -> {after}).");
+ eprintln!("xerj corpus index: If the error above says the state directory cannot become generation");
+ eprintln!("xerj corpus index: authority, or that --fresh is refused, run: xerj corpus index {name} --fresh");
+ return rc;
+ }
}
}
docs = docs.or_else(|| count_under(node, &format!("xc-{name}")));
@@ -2327,6 +2370,49 @@ mod tests {
assert_eq!(st.salvaged, Some(false));
}
+ /// #1173: the three honest claims a failed legacy run can make. The one
+ /// this exists to pin is the middle: a count the node never answered is
+ /// UNKNOWN, and "wrote no new records" asserted from it is a fabricated
+ /// absence — exactly what the overnight resume failure printed while the
+ /// run had written records the node was too unhealthy to count.
+ #[test]
+ fn a_failed_run_the_node_cannot_count_is_unknown_not_wrote_nothing() {
+ use super::LegacyFailureVerdict::*;
+ // grew → salvaged, with the pair the message quotes
+ assert!(matches!(
+ legacy_failure_verdict(Some(100), Some(250)),
+ Salvaged {
+ before: 100,
+ after: 250
+ }
+ ));
+ // known and did not grow → the absence claim is EARNED
+ assert!(matches!(
+ legacy_failure_verdict(Some(100), Some(100)),
+ WroteNothing {
+ before: 100,
+ after: 100
+ }
+ ));
+ assert!(matches!(
+ legacy_failure_verdict(Some(100), Some(40)),
+ WroteNothing {
+ before: 100,
+ after: 40
+ }
+ ));
+ // any UNKNOWN side forbids the absence claim — before, after, or both
+ assert!(matches!(
+ legacy_failure_verdict(None, Some(250)),
+ CountUnknown
+ ));
+ assert!(matches!(
+ legacy_failure_verdict(Some(100), None),
+ CountUnknown
+ ));
+ assert!(matches!(legacy_failure_verdict(None, None), CountUnknown));
+ }
+
#[test]
fn sibling_corpus_indices_are_never_touched_by_a_rebuild() {
let root = corpus_root("battle");