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): one junk definition per map, and mock-fixture hygiene

xerj-orgb947a8ac7efd
diff --git a/engine/crates/xerj-autoindex/src/catalog.rs b/engine/crates/xerj-autoindex/src/catalog.rs
index 90349e208..9c5ac3e88 100644
--- a/engine/crates/xerj-autoindex/src/catalog.rs
+++ b/engine/crates/xerj-autoindex/src/catalog.rs
@@ -8,7 +8,28 @@ use serde_json::{json, Value};
pub const CATALOG_INDEX: &str = "autoindex-catalog";
+/// Explicit mapping for a **freshly created** catalog index.
+///
+/// Tripwire — `started` is bimodal across installs, on purpose. This function
+/// declares it `date`, but the additive upgrade in `run_index_report`
+/// deliberately omits it: catalogs created before this mapping existed have a
+/// dynamically inferred `text` `started`, and asking the engine to change an
+/// existing `text` field to `date` is refused with a 400
+/// `illegal_argument_exception` (`xerj-api/src/es_compat.rs:1792`) and aborts
+/// the whole invocation. So a catalog created from here sorts `started`
+/// server-side, and an upgraded legacy catalog does not.
+///
+/// Nothing may rely on `started` being a `date`: `run_map` sorts runs
+/// client-side, which is what keeps the split benign. Any future server-side
+/// range query, `sort`, or date aggregation on `started` must first migrate
+/// legacy catalogs by reindexing them — not by adding `started` to the
+/// additive upgrade, which is exactly the abort described above.
pub fn catalog_mapping() -> Value {
+ // The three run-metadata fields are inserted after the literal rather than
+ // written inside it: `serde_json::json!` recurses once per key, and at 40
+ // properties the macro exceeds the default `recursion_limit = 128` and
+ // fails to compile. Adding another field here must use this same tail, not
+ // the literal.
let mut mapping = json!({
"mappings": {"properties": {
"doc_kind": {"type": "keyword"},
@@ -54,6 +75,7 @@ pub fn catalog_mapping() -> Value {
.pointer_mut("/mappings/properties")
.and_then(Value::as_object_mut)
.expect("catalog mapping properties");
+ // See the tripwire on this function before touching `started`.
properties.insert(
"started".into(),
json!({"type": "date", "format": "strict_date_optional_time||epoch_millis"}),
diff --git a/engine/crates/xerj-autoindex/src/failure_resume_http_tests.rs b/engine/crates/xerj-autoindex/src/failure_resume_http_tests.rs
index 2edc173fd..f252ed189 100644
--- a/engine/crates/xerj-autoindex/src/failure_resume_http_tests.rs
+++ b/engine/crates/xerj-autoindex/src/failure_resume_http_tests.rs
@@ -31,6 +31,12 @@ struct MockState {
catalog_preexists: bool,
catalog_mapping_upgraded: bool,
catalog_bulk_before_upgrade: bool,
+ /// Catalog bulk actions the fixture does not model. Recorded rather than
+ /// panicked on: this runs inside the server thread while the state guard
+ /// is held, so a panic here would poison the `Mutex` and turn
+ /// `MockEndpoint::drop`'s `join().unwrap()` into a second, misleading
+ /// panic. Tests assert on it from the test thread instead.
+ unexpected_catalog_actions: Vec<String>,
stop: bool,
embedding_identity_sha256: String,
/// Reject every data-bulk item with a 403 explicit write-block error
@@ -165,12 +171,21 @@ fn handle(mut stream: TcpStream, state: &Arc<Mutex<MockState>>) {
});
let mut locked = state.lock().unwrap();
if locked.catalog_preexists && started_requested {
+ // The literal shape `PUT /_mapping` returns for an incompatible
+ // type change (`xerj-api/src/es_compat.rs`): 400
+ // `illegal_argument_exception`, not `mapper_parsing_exception`,
+ // which is reserved for a malformed mapping body.
+ let reason = "mapper [started] cannot be changed from type [text] to [date]";
(
400,
- json!({"error": {
- "type": "mapper_parsing_exception",
- "reason": "field [started] already exists as [text], cannot add [date]"
- }}),
+ json!({
+ "error": {
+ "root_cause": [{"type": "illegal_argument_exception", "reason": reason}],
+ "type": "illegal_argument_exception",
+ "reason": reason,
+ },
+ "status": 400,
+ }),
)
} else {
locked.catalog_mapping_upgraded = upgraded;
@@ -259,6 +274,14 @@ fn bulk_response(body: &[u8], state: &Arc<Mutex<MockState>>) -> Value {
locked.catalog_docs.insert(id.to_owned(), doc);
line += 2;
} else {
+ // Fixture drift, not a product signal. Recorded rather than
+ // panicked on: this runs on the server thread with the state
+ // guard held, so a panic here poisons the `Mutex` and turns
+ // `MockEndpoint::drop`'s `join().unwrap()` into a second,
+ // misleading panic. Tests assert on it from the test thread.
+ locked
+ .unexpected_catalog_actions
+ .push(format!("unexpected catalog bulk action: {action}"));
line += 1;
}
}
@@ -664,6 +687,11 @@ fn existing_catalog_is_upgraded_before_new_run_metadata_is_written() {
!state.catalog_bulk_before_upgrade,
"new run metadata must not reach a legacy catalog before its additive mapping upgrade"
);
+ assert!(
+ state.unexpected_catalog_actions.is_empty(),
+ "fixture does not model these catalog bulk actions: {:?}",
+ state.unexpected_catalog_actions
+ );
}
#[test]
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index f58218f68..1f782cbdf 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -1349,6 +1349,9 @@ pub fn run_index_report(cfg: IndexCfg) -> Result<(i32, Option<Value>)> {
// `started` intentionally stays out of this additive upgrade.
// Older catalogs dynamically mapped it as text, and asking the
// engine to change that existing field to date aborts every run.
+ // `catalog::catalog_mapping` declares it `date` for a fresh
+ // catalog, so the field is permanently bimodal across installs;
+ // its doc comment carries the full tripwire.
"summary_generated_at": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"invocation_telemetry_scope": {"type": "keyword"}
}}),
@@ -2360,9 +2363,19 @@ pub fn run_index_report(cfg: IndexCfg) -> Result<(i32, Option<Value>)> {
// dataset docs
let mut junk_records_by_run: u64 = junk_records.load(Ordering::Relaxed);
- let dataset_stats = {
+ let (dataset_stats, durable_junk_records) = {
let journal = journal_mx.lock().unwrap();
- durable_dataset_stats(&plan, &journal.done)
+ let stats = durable_dataset_stats(&plan, &journal.done);
+ // One definition of "junk records" per map. The dataset docs below
+ // report the durable per-file commits, so the run doc must too:
+ // reporting the invocation-local counter there made a no-op resume
+ // publish `junk_records_total: 0` next to dataset docs that showed
+ // the real number, in the same `xerj autoindex map` output. The
+ // per-dataset values are this same total attributed to each file's
+ // first assigned dataset, so they sum back to it for every file the
+ // frozen plan still describes.
+ let durable_junk: u64 = journal.done.values().map(|done| done.junk).sum();
+ (stats, durable_junk)
};
for d in &plan.datasets {
let sample_queries = catalog::build_sample_queries(d, &key_corrs);
@@ -2558,7 +2571,11 @@ pub fn run_index_report(cfg: IndexCfg) -> Result<(i32, Option<Value>)> {
"duplicate_files": plan.duplicate_files.len(),
"files_junk": junk_file_count,
"records_total": total_records,
- "junk_records_total": junk_records_by_run,
+ // Durable corpus descriptor, same definition as every dataset doc's
+ // `junk_records`; `junk_records_this_run` below is the invocation
+ // counter, which also includes records the backend itself rejected.
+ "junk_records_total": durable_junk_records,
+ "junk_records_this_run": junk_records_by_run,
// This-run submission accounting (#195): what THIS invocation sent
// and had accepted, vs `records_total` above which is the live
// server-side count. A healthy run keeps these consistent; a
@@ -3251,6 +3268,7 @@ mod map_metadata_tests {
.enumerate()
.map(|(group, slug)| (Some(format!("group-{group}")), (*slug).to_string()))
.collect(),
+ as_document: false,
}
}