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): #739 - sweep a legacy file-alias: catalog doc by id (alias half)

Vincenzo Lombardo8e27a4a7d4d6
diff --git a/engine/crates/xerj-autoindex/src/incremental_reconcile_http_tests.rs b/engine/crates/xerj-autoindex/src/incremental_reconcile_http_tests.rs
index 5042c5585..49116155a 100644
--- a/engine/crates/xerj-autoindex/src/incremental_reconcile_http_tests.rs
+++ b/engine/crates/xerj-autoindex/src/incremental_reconcile_http_tests.rs
@@ -2431,6 +2431,77 @@ fn sweep_excluded_groups_deletes_a_legacy_main_doc_by_id() {
);
}
+/// #739 (alias half): a `file-alias:` catalog doc written by a pre-#737
+/// binary has NO `prefix` field either, so it survives both the #737
+/// prefix-scoped alias sweep AND the #693 term-scoped one on the first
+/// upgraded run — the residual the main-doc fix (#739 above) left open. The
+/// frozen `plan.duplicate_files` still carries the alias's `rel`/`path_id`,
+/// so the sweep can reconstruct its exact `_id` and delete it directly.
+#[test]
+fn sweep_excluded_groups_deletes_a_legacy_alias_doc_by_id() {
+ let ep = HttpEndpoint::start();
+ let legacy_alias_id = catalog::duplicate_file_id("ax", "legacy-key", "alias.csv", "pid1");
+ let sibling_alias_id = catalog::duplicate_file_id("bx", "legacy-key", "alias.csv", "pid1");
+
+ {
+ let mut st = ep.state.lock().unwrap();
+ // Legacy alias docs: no `prefix` field (pre-#737). Corpus "ax"
+ // excludes the canonical file; corpus "bx" holds a same-key alias
+ // under its own (differently prefix-encoded) id.
+ st.docs.insert(
+ (catalog::CATALOG_INDEX.to_string(), legacy_alias_id.clone()),
+ json!({"doc_kind": "file", "path": "alias.csv"}),
+ );
+ st.docs.insert(
+ (catalog::CATALOG_INDEX.to_string(), sibling_alias_id.clone()),
+ json!({"doc_kind": "file", "path": "alias.csv"}),
+ );
+ }
+
+ let es = Es::with_bulk_timeout(&ep.url, None, 30).expect("es client");
+ let mut plan = Plan::default();
+ plan.datasets.push(crate::state::PlanDataset {
+ slug: "ds".into(),
+ index: "incremental-http-ds".into(),
+ family: "csv".into(),
+ group: None,
+ specs: Vec::new(),
+ time_field: None,
+ semantic_field: None,
+ sampled_records: 1,
+ file_count: 1,
+ });
+ plan.duplicate_files.push(crate::state::DuplicateFile {
+ file_key: "legacy-key".into(),
+ rel: "alias.csv".into(),
+ path_id: "pid1".into(),
+ is_symlink: None,
+ duplicate_of: "legacy.csv".into(),
+ bytes: 0,
+ });
+ let excluded = vec![InventoryDeltaEntry {
+ file_key: "legacy-key".into(),
+ path: "legacy.csv".into(),
+ }];
+
+ sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
+
+ let st = ep.state.lock().unwrap();
+ // ax's legacy alias doc is swept by its reconstructed id, even without a
+ // `prefix` field.
+ assert!(
+ !st.docs
+ .contains_key(&(catalog::CATALOG_INDEX.to_string(), legacy_alias_id)),
+ "#739: the excluding corpus's legacy file-alias: doc must be swept by id"
+ );
+ // The sibling corpus's same-key legacy alias doc survives (different id).
+ assert!(
+ st.docs
+ .contains_key(&(catalog::CATALOG_INDEX.to_string(), sibling_alias_id)),
+ "#739: a sibling corpus's legacy file-alias: doc must NOT be swept"
+ );
+}
+
/// #736: the sweep also soft-invalidates INBOUND edges — ones a surviving file
/// taught that point AT the excluded file's anchor node (`dst`). Without it they
/// stay live, pointing at a node that is gone. An edge to a different node stays
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index ff4aee5f9..9563bdf9c 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -3231,18 +3231,33 @@ fn sweep_excluded_groups(
// the first upgraded run. Delete the main `file:` doc by its exact,
// prefix-encoded `_id` too: it catches legacy and current docs alike and
// is inherently corpus-scoped (a sibling corpus's id differs), so it
- // cannot strand or over-delete. (Legacy `file-alias:` ids can't be
- // reconstructed here without the per-alias path_ids — a smaller residual.)
- es.delete_by_query(
- catalog::CATALOG_INDEX,
- &json!({"ids": {"values": [catalog::file_id(prefix, &entry.file_key)]}}),
- )
- .with_context(|| {
- format!(
- "sweep catalog file doc by id for newly-excluded {}",
- entry.path
- )
- })?;
+ // cannot strand or over-delete.
+ //
+ // Legacy `file-alias:` docs get the same treatment: `plan` is the
+ // FROZEN plan (the state before this run), so `duplicate_files` still
+ // carries every alias's `rel`/`path_id` for a content group being
+ // excluded now — the same lookup `catalog::duplicate_file_id` needs,
+ // and the same shape `stale_alias_ids` above already reconstructs ids
+ // from elsewhere in this file. Building each alias's exact id closes
+ // the residual the main-doc fix (above) left open: a pre-#737
+ // `file-alias:` doc has no `prefix` either, so it also survives the
+ // #693 term-scoped alias sweep on the first upgraded run.
+ let mut ids = vec![catalog::file_id(prefix, &entry.file_key)];
+ ids.extend(
+ plan.duplicate_files
+ .iter()
+ .filter(|alias| alias.file_key == entry.file_key)
+ .map(|alias| {
+ catalog::duplicate_file_id(prefix, &alias.file_key, &alias.rel, &alias.path_id)
+ }),
+ );
+ es.delete_by_query(catalog::CATALOG_INDEX, &json!({"ids": {"values": ids}}))
+ .with_context(|| {
+ format!(
+ "sweep catalog file/alias docs by id for newly-excluded {}",
+ entry.path
+ )
+ })?;
// #694: soft-invalidate the edges this file taught. `invalidate_prior_edges`
// tolerates a not-yet-created edges index (returns 0), so a graph run whose
// edges index has not been ensured at this point is safe.