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.

feat(api): #310 emission site 3 — re-narrow the scroll snapshot after a pierce

xerj-org1f6d9204d1ae
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 21692c16e..a0a5a9b0e 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -10866,9 +10866,11 @@ async fn search_impl(
Vec::new()
};
// One schema read-lock per participating index, and only for a request that
- // could actually consume the answer.
+ // could actually consume the answer. (The collapse `inner_hits` companion
+ // leak under a default projection is a separate, pre-existing gap tracked as
+ // a follow-up; it is not driven by the pierce, so it is not populated here.)
let mut companions_by_index: HashMap<String, HashSet<String>> = HashMap::new();
- if default_source_projection && (!value_bearing_names.is_empty() || body.collapse.is_some()) {
+ if default_source_projection && !value_bearing_names.is_empty() {
for idx_name in &index_names {
let Ok(idx) = state.engine.get_index(idx_name) else {
continue;
@@ -11755,7 +11757,23 @@ async fn search_impl(
let scroll_snapshot: Option<Vec<(String, xerj_query::executor::Hit)>> = if is_scroll_request {
// Keep the index name with each hit (#414): dropping it here is what
// made continuation pages fall back to a single context-level name.
- Some(merged_hits.clone())
+ let mut snap = merged_hits.clone();
+ // #310 emission site 3: the snapshot outlives this response, and every
+ // later page is rendered by `scroll_page_response`, which carries no
+ // `fields` clause of its own and so has no reason to hold companions.
+ // Undo the pierce before the context is stored — otherwise page 1
+ // honours #309 and pages 2..n ship the companion in `_source`.
+ if pierce_default_source {
+ for (idx_name, h) in snap.iter_mut() {
+ if let (Some(companions), Some(obj)) = (
+ companions_by_index.get(idx_name.as_str()),
+ h.source.as_object_mut(),
+ ) {
+ obj.retain(|name, _| !companions.contains(name));
+ }
+ }
+ }
+ Some(snap)
} else {
None
};
diff --git a/engine/crates/xerj-api/tests/fields_pierce_default_source.rs b/engine/crates/xerj-api/tests/fields_pierce_default_source.rs
index 21900f718..d15c3daa2 100644
--- a/engine/crates/xerj-api/tests/fields_pierce_default_source.rs
+++ b/engine/crates/xerj-api/tests/fields_pierce_default_source.rs
@@ -294,3 +294,74 @@ async fn a_dotted_target_field_keeps_unrelated_empty_objects() {
"the dotted companion itself must still be omitted: {response}"
);
}
+
+/// #310 emission site 3: a `_search?scroll=` that pierced the default `_source`
+/// for a `fields` companion must NOT leak that companion on the CONTINUATION
+/// pages. The scroll snapshot is taken in `search_impl`; the pierce widened it
+/// to the intact source, so page 1 (rendered inline) is re-narrowed by site 1
+/// but pages 2..n (rendered by `scroll_page_response`, which has no `fields`
+/// clause) would ship the vector unless the snapshot itself is re-narrowed.
+#[tokio::test]
+async fn scroll_continuation_does_not_leak_a_companion_after_a_pierce() {
+ let (app, _dir) = seeded_app().await;
+ // A second doc so a size:1 scroll has a continuation page.
+ let (status, _) = post(
+ &app,
+ "/docs/_doc/2",
+ json!({ "body": "a second document so the scroll has a page two. ".repeat(8), "title": "second" }),
+ )
+ .await;
+ assert_eq!(status, StatusCode::CREATED, "index second doc");
+ let _ = post(&app, "/docs/_refresh", Value::Null).await;
+
+ // Page 1: pierce for the named companion, size 1, default `_source`.
+ let (status, page1) = post(
+ &app,
+ "/docs/_search?scroll=5m",
+ json!({ "query": { "match_all": {} }, "size": 1, "fields": ["body_vector"] }),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{page1}");
+ let h1 = first_hit(&page1);
+ assert_eq!(
+ h1["fields"]["body_vector"]
+ .as_array()
+ .map(|v| v.len())
+ .unwrap_or(0),
+ DIMS,
+ "page 1 must resolve the named companion via the pierce: {page1}"
+ );
+ assert!(
+ !h1["_source"]
+ .as_object()
+ .expect("_source")
+ .contains_key("body_vector"),
+ "page 1 _source must not leak the companion (#309 via site 1): {page1}"
+ );
+ let sid = page1["_scroll_id"].as_str().expect("scroll id").to_string();
+
+ // Page 2 (continuation): rendered by scroll_page_response, no `fields`
+ // clause. The companion must be absent from `_source` (site 3).
+ let (status, page2) = post(
+ &app,
+ "/_search/scroll",
+ json!({ "scroll": "5m", "scroll_id": sid }),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{page2}");
+ let h2 = first_hit(&page2);
+ assert!(
+ !h2["_source"]
+ .as_object()
+ .expect("_source")
+ .contains_key("body_vector"),
+ "#310 site 3: the scroll continuation must not leak the companion: {page2}"
+ );
+ assert!(
+ !h2["_source"]
+ .as_object()
+ .expect("_source")
+ .contains_key("body_vector_chunks"),
+ "#310 site 3: the chunks companion must not leak either: {page2}"
+ );
+}