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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
//! Real-HTTP end-to-end coverage for incremental corpus generations.
//!
//! The endpoint below is deliberately stateful. It applies bulk index/update
//! actions, delete-by-query, and exact count searches so `run_index` traverses
//! the same HTTP client and validation barriers as a real server.
use super::*;
use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
static HTTP_E2E_LOCK: Mutex<()> = Mutex::new(());
#[derive(Default)]
struct HttpState {
docs: HashMap<(String, String), Value>,
requests: Vec<(String, String)>,
/// Every `POST …/_search` body, in order (#1183): the dataset read-back's
/// agg-free shape and its sorted time-bound queries are asserted against
/// these.
search_bodies: Vec<Value>,
data_bulk_requests: usize,
fail_next_data_bulk: bool,
/// #2807: refuse the first `index` action of the next data bulk as over
/// the per-document byte limit, echoing its `_id` as the server does.
refuse_next_data_item_as_too_large: bool,
/// #2807: refuse any record longer than this many bytes, as the server
/// refuses a record that the vectors it adds push over its limit.
refuse_documents_over: Option<usize>,
/// Apply the first half of the next data bulk's items and *then* report
/// `errors: true`. This is the dangerous backend shape the legacy
/// replacement transaction was hardened against (visibility changed, but
/// the durable record must not be): `no_graph` corpora take the generated
/// executor now, so the shape has to be proven here too.
partially_apply_next_data_bulk: bool,
fail_embedding_identity: bool,
/// #1971: answer semantic `exists` read-backs with no matches while
/// retaining the published records and catalog for refusal/resume checks.
hide_semantic_matches: bool,
stop: bool,
embedding_identity_sha256: String,
saw_dataset_mapping_update: bool,
saw_catalog_mapping_update: bool,
/// Opt-in (#755/#760): the fields this catalog already holds as `text`,
/// standing in for an older-build (v1.0.0-rc.15..rc.67) catalog. A catalog
/// `_mapping` PUT that still declares one of them fails with the exact
/// `field [X] already exists as [text]` conflict a real engine produces;
/// a PUT that no longer declares any of them is acknowledged, exactly like
/// the engine. Empty by default — every other test's faithful-accept
/// behaviour is unchanged.
///
/// #890: it also governs how a `term` on such a field MATCHES, in searches
/// and in delete-by-query alike. A legacy field is not merely undeclarable,
/// it is analyzed, and that is the whole defect — see [`term_matches`].
legacy_text_catalog_fields: Vec<String>,
/// Opt-in (#755): fail the catalog `_mapping` PUT with a 503 instead. Not a
/// type conflict, so it must still abort the run.
catalog_mapping_unavailable: bool,
/// The `properties` object of the last catalog `_mapping` PUT the endpoint
/// ACKNOWLEDGED — what a legacy catalog actually ends up declaring.
installed_catalog_properties: Option<Value>,
/// How many catalog `_mapping` PUTs were attempted (the drop-and-retry
/// loop's cost).
catalog_mapping_puts: usize,
/// Opt-in (#929): a DATASET `_mapping` PUT whose index name contains one of
/// these is refused with the 400 a real engine returns for a field it
/// cannot map — the literal response that aborted the 48,533-file run.
/// A refusal is a statement about that one dataset.
refused_dataset_mappings: Vec<String>,
/// Opt-in (#929): the same selection, answered 503 instead. That is the
/// endpoint failing, says nothing about the dataset, and must still abort.
unavailable_dataset_mappings: Vec<String>,
/// Every dataset index a `_mapping` PUT was attempted for, in order.
dataset_mapping_puts: Vec<String>,
/// Properties installed for a dataset index: the create body the first
/// time the index is seen, then additive `_mapping` merges. A later
/// `_mapping` whose `type` disagrees with what is already stored is the
/// engine's type-change refusal (`field [X] already exists as [old],
/// cannot add [new]`). A create of an index that already exists is
/// ignored, the way `Es::ensure_index` swallows "exists" without
/// re-sending the body.
installed_dataset_properties: HashMap<String, serde_json::Map<String, Value>>,
/// Opt-in (#931): fail the Nth data bulk (1-based) the way
/// `fail_next_data_bulk` fails the next one. Lets a replay be interrupted
/// AFTER some operations committed, which is the only state in which
/// "count what remains" differs from "count everything".
fail_data_bulk_number: Option<usize>,
/// Opt-in (#944): apply the even-position actions of the next data bulk
/// and answer the odd-position ones with the per-item 429 the engine's
/// memory circuit breaker produces while it is engaged. The client must
/// re-send exactly the odd ones.
throttle_next_data_bulk_odd_items: bool,
/// Opt-in (#944, second shape): answer the next N data bulks HTTP 429 as
/// a whole, applying nothing — how the engine answers when its memory
/// circuit breaker rejects the request, and the literal end of the resumed
/// full-corpus run (`error: _bulk: HTTP 429 Too Many Requests`).
throttle_whole_data_bulks: usize,
/// Action count of every data bulk answered, in order — how a test proves
/// a re-send carried only the rejected items.
data_bulk_item_counts: Vec<usize>,
/// Opt-in (#955): the engine's `limits.max_actions_per_bulk`. A `_bulk` of
/// more than twice this many lines — data or catalog — is refused whole,
/// before anything is applied, with the engine's literal answer: HTTP 200
/// carrying ONE item of status 413. That response ended the 48,533-file
/// run in `finalize-catalog` after every operation had been applied.
max_actions_per_bulk: Option<usize>,
/// Line count of every bulk refused by `max_actions_per_bulk`, in order.
oversize_bulks_refused: Vec<usize>,
/// Line count of every bulk that got past `max_actions_per_bulk`.
bulk_line_counts: Vec<usize>,
/// #933 opt-in: hold each DATA bulk for this many milliseconds before
/// applying it, outside the state lock. A serial client still sends one
/// request at a time, so for every existing test this changes nothing;
/// a multi-worker replay is forced into observable overlap.
data_bulk_delay_ms: u64,
/// #933: data bulks currently inside their delay window, and the most
/// that were ever inside it together. `1` forever is the fingerprint of
/// a serial index phase.
in_flight_data_bulks: usize,
max_concurrent_data_bulks: usize,
/// #2314: the next N catalog searches that filter on `file_key` omit the
/// last hit and report that shorter total. The visibility race four
/// identical files hit. The counter, not refresh, clears it, so the
/// recount after refresh sees the full set.
short_catalog_file_key_pages: usize,
}
struct HttpEndpoint {
url: String,
state: Arc<Mutex<HttpState>>,
join: Option<thread::JoinHandle<()>>,
}
impl HttpEndpoint {
fn start() -> Self {
let listener = crate::test_loopback::bind();
listener.set_nonblocking(true).unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let state = Arc::new(Mutex::new(HttpState {
embedding_identity_sha256: "a".repeat(64),
..HttpState::default()
}));
let server_state = Arc::clone(&state);
let join = thread::spawn(move || loop {
match listener.accept() {
Ok((stream, _)) => {
// BSD/macOS: accepted sockets inherit the listener's
// O_NONBLOCK; the handler does blocking reads.
stream.set_nonblocking(false).unwrap();
// #933: one thread per connection, so the parallel
// durable replay can be served the way a real node is —
// several requests inside the endpoint at once. Every
// handler touches `HttpState` only through its Mutex and
// never sleeps while holding it, and a serial client
// still has at most one connection open, so every other
// test sees the endpoint it always had.
let server_state = Arc::clone(&server_state);
thread::spawn(move || handle_http(stream, &server_state));
}
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
if server_state.lock().unwrap().stop {
break;
}
thread::yield_now();
}
Err(error) => panic!("HTTP test endpoint accept failed: {error}"),
}
});
Self {
url,
state,
join: Some(join),
}
}
fn data_docs(&self) -> Vec<Value> {
self.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), _)| index != catalog::CATALOG_INDEX)
.map(|(_, doc)| doc.clone())
.collect()
}
/// Every `POST …/_search` body the endpoint has seen, in order (#1183).
fn search_bodies(&self) -> Vec<Value> {
self.state.lock().unwrap().search_bodies.clone()
}
/// One catalog document by id, from the fake's stored docs.
fn catalog_doc(&self, id: &str) -> Option<Value> {
self.state
.lock()
.unwrap()
.docs
.get(&(catalog::CATALOG_INDEX.to_string(), id.to_string()))
.cloned()
}
fn data_bulk_requests(&self) -> usize {
self.state.lock().unwrap().data_bulk_requests
}
/// Properties the fake has installed for one dataset index (#2176).
fn dataset_properties(&self, index: &str) -> Option<Value> {
self.state
.lock()
.unwrap()
.installed_dataset_properties
.get(index)
.map(|properties| Value::Object(properties.clone()))
}
/// #755: the catalog mapping the endpoint finally acknowledged.
fn installed_catalog_properties(&self) -> Value {
self.state
.lock()
.unwrap()
.installed_catalog_properties
.clone()
.expect("no catalog _mapping PUT was acknowledged")
}
/// #755: every catalog document the run published.
fn catalog_docs(&self) -> Vec<Value> {
self.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), _)| index == catalog::CATALOG_INDEX)
.map(|(_, doc)| doc.clone())
.collect()
}
}
impl Drop for HttpEndpoint {
fn drop(&mut self) {
self.state.lock().unwrap().stop = true;
let _ = TcpStream::connect(self.url.trim_start_matches("http://"));
self.join.take().unwrap().join().unwrap();
}
}
fn handle_http(mut stream: TcpStream, state: &Arc<Mutex<HttpState>>) {
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut request_line = String::new();
if reader.read_line(&mut request_line).unwrap() == 0 {
return;
}
let mut content_length = 0usize;
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if line == "\r\n" || line.is_empty() {
break;
}
if let Some(value) = line
.to_ascii_lowercase()
.strip_prefix("content-length:")
.map(str::trim)
{
content_length = value.parse().unwrap();
}
}
let mut body = vec![0; content_length];
reader.read_exact(&mut body).unwrap();
let mut parts = request_line.split_whitespace();
let method = parts.next().unwrap_or("");
let path = parts.next().unwrap_or("");
state
.lock()
.unwrap()
.requests
.push((method.to_owned(), path.to_owned()));
let (status, response) = if method == "GET" && path == "/v1/embedding/identity" {
let locked = state.lock().unwrap();
if locked.fail_embedding_identity {
(
500,
json!({"error": {
"type": "injected_identity_failure",
"reason": "genesis recovery boundary"
}}),
)
} else {
(
200,
json!({"data": {
"version": 1,
"backend": "lexical",
"identity_sha256": locked.embedding_identity_sha256.clone(),
"dimensions": 384,
"semantic_contract": "semantic_text-derived-vector.v1",
"resumable": true
}, "took_ms": 0, "request_id": "incremental-http-test"}),
)
}
} else if method == "POST" && path == "/_bulk" {
let locked = state.lock().unwrap();
let writes_data = body.split(|byte| *byte == b'\n').any(|line| {
serde_json::from_slice::<Value>(line)
.ok()
.and_then(|action| {
action
.pointer("/index/_index")
.or_else(|| action.pointer("/update/_index"))
.and_then(Value::as_str)
.map(|index| index != catalog::CATALOG_INDEX)
})
.unwrap_or(false)
});
assert!(
locked.saw_catalog_mapping_update
&& (!writes_data || locked.saw_dataset_mapping_update),
"bulk publication preceded exact generation mapping updates"
);
drop(locked);
bulk_http(&body, state)
} else if method == "POST" && path.contains("/_delete_by_query") {
let deleted = delete_http(path, &body, state);
(200, json!({"deleted": deleted, "failures": []}))
} else if method == "POST" && path.ends_with("/_mget") {
(200, mget_http(path, &body, state))
} else if method == "POST" && path.ends_with("/_search") {
if let Ok(query) = serde_json::from_slice::<Value>(&body) {
state.lock().unwrap().search_bodies.push(query);
}
(200, search_http(path, &body, state))
} else if method == "GET" && path.ends_with("/_count") {
let index = path.trim_start_matches('/').trim_end_matches("/_count");
let count = state
.lock()
.unwrap()
.docs
.keys()
.filter(|(candidate, _)| candidate == index)
.count();
(200, json!({"count": count}))
} else if method == "PUT" {
let value: Value = serde_json::from_slice(&body).unwrap_or(Value::Null);
let mut locked = state.lock().unwrap();
// #755: the first still-declared field this catalog already holds as
// `text`, if any — the engine reports one conflict per request.
let mut conflict: Option<String> = None;
let mut unavailable = false;
let mut refused_dataset: Option<String> = None;
let mut type_conflict: Option<String> = None;
if path.ends_with("/_mapping") {
assert!(value.get("properties").is_some());
assert!(value.get("mappings").is_none());
if path.starts_with(&format!("/{}/", catalog::CATALOG_INDEX)) {
locked.saw_catalog_mapping_update = true;
locked.catalog_mapping_puts += 1;
unavailable = locked.catalog_mapping_unavailable;
let declared = value["properties"].as_object().cloned().unwrap_or_default();
conflict = locked
.legacy_text_catalog_fields
.iter()
.find(|field| declared.contains_key(field.as_str()))
.cloned();
if conflict.is_none() && !unavailable {
locked.installed_catalog_properties = Some(Value::Object(declared));
}
} else {
locked.saw_dataset_mapping_update = true;
let index = path
.trim_start_matches('/')
.trim_end_matches("/_mapping")
.to_owned();
let selected = |needles: &[String]| {
needles.iter().any(|needle| index.contains(needle.as_str()))
};
unavailable = selected(&locked.unavailable_dataset_mappings);
if selected(&locked.refused_dataset_mappings) {
refused_dataset = Some(index.clone());
}
if refused_dataset.is_none() && !unavailable {
let incoming = value["properties"].as_object().cloned().unwrap_or_default();
type_conflict = apply_dataset_mapping(&mut locked, &index, &incoming);
}
locked.dataset_mapping_puts.push(index);
}
} else {
assert!(value.pointer("/mappings/properties").is_some());
assert!(value.get("properties").is_none());
let index = path.trim_start_matches('/');
if !index.is_empty() && index != catalog::CATALOG_INDEX {
remember_dataset_create(&mut locked, index, &value);
}
}
if let Some(index) = refused_dataset {
// Byte for byte the shape of the refusal in #929.
let reason = format!(
"nested semantic_text field [{index}.unmappable] is not supported; map \
semantic_text as a top-level field"
);
(
400,
json!({
"error": {
"root_cause": [{"type": "mapper_parsing_exception", "reason": reason}],
"type": "mapper_parsing_exception",
"reason": reason
},
"status": 400
}),
)
} else if unavailable {
(
503,
json!({"error": {"type": "unavailable_shards_exception", "reason": "no node"}}),
)
} else if let Some(field) = conflict {
let reason = format!("field [{field}] already exists as [text], cannot add [keyword]");
(
400,
json!({
"error": {
"root_cause": [{"type": "mapper_parsing_exception", "reason": reason}],
"type": "mapper_parsing_exception",
"reason": reason
},
"status": 400
}),
)
} else if let Some(reason) = type_conflict {
// Same shape as the catalog conflict above, and the same words
// `xerj-api` uses when `PUT /_mapping` changes a field's type
// (`es_compat.rs`). `update_mapping` does not swallow a body that
// contains "exists"; `ensure_index` does, which is why a create
// of an existing index never reaches this branch.
(
400,
json!({
"error": {
"root_cause": [{"type": "mapper_parsing_exception", "reason": reason}],
"type": "mapper_parsing_exception",
"reason": reason
},
"status": 400
}),
)
} else {
(200, json!({"acknowledged": true}))
}
} else {
// Readiness, index creation/mapping, and refresh.
(200, json!({"acknowledged": true}))
};
let bytes = response.to_string();
write!(
stream,
"HTTP/1.1 {status} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
match status {
200 => "OK",
400 => "Bad Request",
429 => "Too Many Requests",
503 => "Service Unavailable",
_ => "Internal Server Error",
},
bytes.len(),
bytes
)
.unwrap();
}
/// Store the create-body properties the first time an index is seen.
/// A later create is the "index exists" case: the body is not applied.
fn remember_dataset_create(state: &mut HttpState, index: &str, body: &Value) {
if state.installed_dataset_properties.contains_key(index) {
return;
}
let Some(properties) = body
.pointer("/mappings/properties")
.and_then(Value::as_object)
.cloned()
else {
return;
};
state
.installed_dataset_properties
.insert(index.to_owned(), properties);
}
/// Merge a dataset `_mapping` into the stored properties, or refuse a type
/// change. Returns the engine's reason when a stored field's `type` would
/// change; the stored map is left untouched in that case.
fn apply_dataset_mapping(
state: &mut HttpState,
index: &str,
incoming: &serde_json::Map<String, Value>,
) -> Option<String> {
if let Some(existing) = state.installed_dataset_properties.get(index) {
for (name, spec) in incoming {
let (Some(old), Some(new)) = (
existing
.get(name)
.and_then(|value| value.get("type"))
.and_then(Value::as_str),
spec.get("type").and_then(Value::as_str),
) else {
continue;
};
if old != new {
return Some(format!(
"field [{name}] already exists as [{old}], cannot add [{new}]"
));
}
}
}
let slot = state
.installed_dataset_properties
.entry(index.to_owned())
.or_default();
for (name, spec) in incoming {
slot.entry(name.clone()).or_insert_with(|| spec.clone());
}
None
}
fn bulk_http(body: &[u8], state: &Arc<Mutex<HttpState>>) -> (u16, Value) {
let lines: Vec<&[u8]> = body
.split(|byte| *byte == b'\n')
.filter(|line| !line.is_empty())
.collect();
let is_data = lines.iter().step_by(2).any(|line| {
serde_json::from_slice::<Value>(line)
.ok()
.and_then(|action| {
action
.pointer("/index/_index")
.or_else(|| action.pointer("/update/_index"))
.and_then(Value::as_str)
.map(|index| index != catalog::CATALOG_INDEX)
})
.unwrap_or(false)
});
// #933: the artificial hold, OUTSIDE the state lock so concurrent data
// bulks hold at the same time — the overlap a parallel replay produces
// and a serial one never can.
let hold_ms = { state.lock().unwrap().data_bulk_delay_ms };
if is_data && hold_ms > 0 {
{
let mut locked = state.lock().unwrap();
locked.in_flight_data_bulks += 1;
locked.max_concurrent_data_bulks = locked
.max_concurrent_data_bulks
.max(locked.in_flight_data_bulks);
}
thread::sleep(std::time::Duration::from_millis(hold_ms));
state.lock().unwrap().in_flight_data_bulks -= 1;
}
let mut locked = state.lock().unwrap();
if let Some(max_actions) = locked.max_actions_per_bulk {
// Checked first and by LINES, as `xerj-engine/src/bulk.rs` does.
if lines.len() > max_actions * 2 {
locked.oversize_bulks_refused.push(lines.len());
return (
200,
json!({
"took": 0,
"errors": true,
"items": [{"index": {
"_index": "",
"_id": "",
"status": 413,
"error": {
"type": "engine_exception",
"reason": format!(
"bulk request contains {} lines (~{} actions); exceeds \
max_actions_per_bulk of {max_actions}",
lines.len(),
lines.len() / 2
),
"status": 413
}
}}]
}),
);
}
locked.bulk_line_counts.push(lines.len());
}
if is_data {
locked.data_bulk_requests += 1;
let numbered = locked.fail_data_bulk_number == Some(locked.data_bulk_requests);
if numbered {
locked.fail_data_bulk_number = None;
}
if std::mem::take(&mut locked.refuse_next_data_item_as_too_large) {
let action: Value = serde_json::from_slice(lines[0]).unwrap();
let id = action["index"]["_id"]
.as_str()
.unwrap_or_default()
.to_owned();
return (
200,
json!({
"errors": true,
"items": [{"index": {
"_id": id,
"status": 413,
"error": {
"type": "engine_exception",
"reason": "document is 16590136 bytes, over the 8388608 byte limit for one indexed JSON document; it was not indexed. Split it into smaller documents",
"status": 413
}
}}]
}),
);
}
if let Some(limit) = locked.refuse_documents_over {
// Pair each action with its source line; a `delete` has none.
let mut refused = None;
let mut rest = lines.iter();
while let Some(line) = rest.next() {
let action: Value = serde_json::from_slice(line).unwrap();
if action.get("delete").is_none()
&& rest.next().is_some_and(|source| source.len() > limit)
{
refused = Some(action);
break;
}
}
if let Some(action) = refused {
return (
200,
json!({
"errors": true,
"items": [{"index": {
"_id": action["index"]["_id"],
"status": 413,
"error": {
"type": "engine_exception",
"reason": "document is 16590136 bytes, over the 8388608 byte limit for one indexed JSON document; it was not indexed. Split it into smaller documents",
"status": 413
}
}}]
}),
);
}
}
if std::mem::take(&mut locked.fail_next_data_bulk) || numbered {
return (
200,
json!({
"errors": true,
"items": [{"index": {
"status": 500,
"error": {
"type": "injected_failure",
"reason": "leave durable generation pending"
}
}}]
}),
);
}
}
if is_data && locked.throttle_whole_data_bulks > 0 {
locked.throttle_whole_data_bulks -= 1;
let mut actions = 0usize;
let mut cursor = 0;
while cursor < lines.len() {
let action: Value = serde_json::from_slice(lines[cursor]).unwrap();
cursor += if action.get("delete").is_some() { 1 } else { 2 };
actions += 1;
}
locked.data_bulk_item_counts.push(actions);
return (
429,
json!({
"error": {
"type": "circuit_breaking_exception",
"reason": "[parent] real memory circuit breaker tripped: rss=15634MB >= watermark=15564MB (94% of limit=16384MB); writes rejected to prevent an out-of-memory kill"
},
"status": 429
}),
);
}
if is_data && std::mem::take(&mut locked.throttle_next_data_bulk_odd_items) {
let mut items = Vec::new();
let mut cursor = 0;
while cursor < lines.len() {
let action: Value = serde_json::from_slice(lines[cursor]).unwrap();
cursor += 1;
let position = items.len();
if let Some(meta) = action.get("delete") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
locked.docs.remove(&(index, id));
items.push(json!({"delete": {"status": 200}}));
continue;
}
let payload: Value = serde_json::from_slice(lines[cursor]).unwrap();
cursor += 1;
if position % 2 == 1 {
items.push(json!({"index": {
"status": 429,
"error": {
"type": "engine_exception",
"reason": "[parent] real memory circuit breaker tripped: rss=15679MB >= watermark=15564MB (94% of limit=16384MB); writes rejected to prevent an out-of-memory kill",
"status": 429
}
}}));
continue;
}
if let Some(meta) = action.get("index") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
locked.docs.insert((index, id), payload);
} else if let Some(meta) = action.get("update") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
let patch = payload["doc"].as_object().unwrap();
let target = locked.docs.get_mut(&(index, id)).unwrap();
let target = target.as_object_mut().unwrap();
target.extend(patch.clone());
}
items.push(json!({"index": {"status": 201}}));
}
locked.data_bulk_item_counts.push(items.len());
return (200, json!({"errors": true, "items": items}));
}
// Half-applied bulk: apply the leading actions, then report failure.
let applied_limit = if is_data && std::mem::take(&mut locked.partially_apply_next_data_bulk) {
Some(lines.len() / 2)
} else {
None
};
let mut cursor = 0;
let mut actions = 0usize;
while cursor < lines.len() {
if applied_limit.is_some_and(|limit| cursor >= limit) {
break;
}
let action: Value = serde_json::from_slice(lines[cursor]).unwrap();
cursor += 1;
actions += 1;
if let Some(meta) = action.get("delete") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
locked.docs.remove(&(index, id));
continue;
}
let payload: Value = serde_json::from_slice(lines[cursor]).unwrap();
cursor += 1;
if let Some(meta) = action.get("index") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
locked.docs.insert((index, id), payload);
} else if let Some(meta) = action.get("update") {
let index = meta["_index"].as_str().unwrap().to_owned();
let id = meta["_id"].as_str().unwrap().to_owned();
let patch = payload["doc"].as_object().unwrap();
let target = locked.docs.get_mut(&(index, id)).unwrap();
let target = target.as_object_mut().unwrap();
target.extend(patch.clone());
}
}
if is_data {
locked.data_bulk_item_counts.push(actions);
}
if applied_limit.is_some() {
return (
200,
json!({
"errors": true,
"items": [{"index": {
"status": 500,
"error": {
"type": "injected_failure",
"reason": "partial bulk applied before failing"
}
}}]
}),
);
}
(200, json!({"errors": false, "items": []}))
}
/// Does a `term` query for `expected` on `field` match `doc`, given the fields
/// this catalog holds as legacy `text` (#890)?
///
/// A `term` query is NOT analyzed, but the FIELD is. On a `keyword` field the
/// match is byte-exact, which is what every other test relies on. On a `text`
/// field the term is compared against the stored value's analyzed TOKENS, so
/// `{"term": {"prefix": "ax"}}` matches a document whose prefix is `ax-2`
/// (standard-analyzer tokens `[ax, 2]`) — the cross-corpus reach #890 is about.
/// Tokenisation here is the standard analyzer's shape for these values: split on
/// non-alphanumerics, lowercased.
fn term_matches(doc: &Value, field: &str, expected: &Value, legacy_text: &[String]) -> bool {
if doc.get(field) == Some(expected) {
return true;
}
if !legacy_text.iter().any(|legacy| legacy == field) {
return false;
}
let (Some(stored), Some(term)) = (doc.get(field).and_then(Value::as_str), expected.as_str())
else {
return false;
};
stored
.split(|ch: char| !ch.is_alphanumeric())
.filter(|token| !token.is_empty())
.any(|token| token.to_lowercase() == term)
}
fn delete_http(path: &str, body: &[u8], state: &Arc<Mutex<HttpState>>) -> usize {
let index = path
.trim_start_matches('/')
.split("/_delete_by_query")
.next()
.unwrap();
let query: Value = serde_json::from_slice(body).unwrap();
// Collect every term constraint: the single `/query/term` form AND each
// `/query/bool/filter[].term` (#737 scopes catalog deletes with a
// `prefix` + `path`/`file_key` bool filter). A doc is deleted iff it
// matches EVERY collected term.
let mut terms: Vec<(String, Value)> = Vec::new();
// #905: and every `/query/bool/filter[].terms` value ARRAY too — the
// duplicate-alias sweep's path list is one of those, and an endpoint that
// dropped it deleted at every path instead of at the named ones, which
// makes a delete look wider than the engine's.
let mut any_of: Vec<(String, Vec<Value>)> = Vec::new();
if let Some((field, value)) = query
.pointer("/query/term")
.and_then(Value::as_object)
.and_then(|term| term.iter().next())
{
terms.push((field.clone(), value.clone()));
}
if let Some(filters) = query
.pointer("/query/bool/filter")
.and_then(Value::as_array)
{
for filter in filters {
if let Some((field, value)) = filter
.pointer("/term")
.and_then(Value::as_object)
.and_then(|term| term.iter().next())
{
terms.push((field.clone(), value.clone()));
}
if let Some((field, values)) = filter
.pointer("/terms")
.and_then(Value::as_object)
.and_then(|terms| terms.iter().next())
{
any_of.push((
field.clone(),
values.as_array().cloned().unwrap_or_default(),
));
}
}
}
// #739: an `ids` query deletes by exact `_id` (the main `file:` doc sweep).
let ids: Option<Vec<String>> = query
.pointer("/query/ids/values")
.and_then(Value::as_array)
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(String::from))
.collect()
});
let mut locked = state.lock().unwrap();
let legacy_text = locked.legacy_text_catalog_fields.clone();
let before = locked.docs.len();
locked.docs.retain(|(candidate, id), doc| {
if candidate != index {
return true;
}
if let Some(ids) = &ids {
// ids query: delete iff this doc's _id is listed (nothing else).
return !ids.contains(id);
}
if terms.is_empty() && any_of.is_empty() {
return false; // no constraint: a match-all delete
}
let legacy: &[String] = if candidate == catalog::CATALOG_INDEX {
&legacy_text
} else {
&[]
};
// Retain (do NOT delete) unless the doc matches every term — #890: on
// the catalog's legacy text fields, "matches" means the analyzed tokens
// — and lands inside every `terms` value list (#905).
!(terms
.iter()
.all(|(field, expected)| term_matches(doc, field, expected, legacy))
&& any_of.iter().all(|(field, values)| {
values
.iter()
.any(|value| term_matches(doc, field, value, legacy))
}))
});
before - locked.docs.len()
}
/// `POST /{index}/_mget` `{"ids":[...]}` — the catalog read-back (#1267).
/// A missing id is `found: false`, not an error, matching the engine.
fn mget_http(path: &str, body: &[u8], state: &Arc<Mutex<HttpState>>) -> Value {
let index = path
.trim_start_matches('/')
.trim_end_matches("/_mget")
.to_owned();
let query: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
let ids = query
.get("ids")
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
let locked = state.lock().unwrap();
let docs: Vec<Value> = ids
.iter()
.map(|id| {
let Some(id) = id.as_str() else {
return json!({"_index": index, "found": false});
};
match locked.docs.get(&(index.clone(), id.to_owned())) {
Some(source) => json!({
"_index": index,
"_id": id,
"found": true,
"_source": source
}),
None => json!({"_index": index, "_id": id, "found": false}),
}
})
.collect();
json!({"docs": docs})
}
fn search_http(path: &str, body: &[u8], state: &Arc<Mutex<HttpState>>) -> Value {
let indices = path
.trim_start_matches('/')
.trim_end_matches("/_search")
.split(',')
.collect::<Vec<_>>();
let query: Value = serde_json::from_slice(body).unwrap();
let term = query
.pointer("/query/term")
.or_else(|| query.pointer("/query/bool/must/0/term"))
.and_then(Value::as_object)
.and_then(|term| term.iter().next());
let terms = query
.pointer("/query/terms")
.and_then(Value::as_object)
.and_then(|terms| terms.iter().next());
let filters = query
.pointer("/query/bool/filter")
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
let exists = query
.pointer("/query/bool/must/1/exists/field")
.and_then(Value::as_str);
// #694: `invalidate_prior_edges` filters live edges with
// `must_not:[{exists:{field:"invalid_at"}}]`; honour it so the pass
// converges (an already-invalidated edge must drop out of the result).
let must_not_exists = query
.pointer("/query/bool/must_not/0/exists/field")
.and_then(Value::as_str);
// #1977: the catalog gate reads rows back with `query.ids`. Dropping that
// clause made the search a match-all, and `size` (the published id count)
// then kept the `ds:` rows plus the `file:` rows that sort between `ds:`
// and `run:`. The run document was in the store and still looked missing.
// An id that was never written stays out of the hits, which is the case
// the gate exists to fail.
let ids: Option<Vec<String>> = query
.pointer("/query/ids/values")
.and_then(Value::as_array)
.map(|values| {
values
.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect()
});
let mut locked = state.lock().unwrap();
// #890: a catalog field an older build left as `text` is ANALYZED, so a
// `term` on it matches tokens, not the raw value. `legacy` is empty for
// every other index, which keeps every other search byte-exact.
let legacy_text = locked.legacy_text_catalog_fields.clone();
let legacy_for = |index: &str| -> &[String] {
if index == catalog::CATALOG_INDEX {
&legacy_text
} else {
&[]
}
};
let mut matching: Vec<_> = locked
.docs
.iter()
.filter(|((index, id), doc)| {
indices.contains(&index.as_str())
&& ids.as_ref().is_none_or(|wanted| wanted.contains(id))
&& term
.map(|(field, value)| term_matches(doc, field, value, legacy_for(index)))
.unwrap_or(true)
&& terms
.map(|(field, values)| {
values.as_array().is_some_and(|values| {
values.iter().any(|value| doc.get(field) == Some(value))
})
})
.unwrap_or(true)
&& filters.iter().all(|filter| {
filter
.get("term")
.and_then(Value::as_object)
.and_then(|term| term.iter().next())
.is_none_or(|(field, value)| {
term_matches(doc, field, value, legacy_for(index))
})
// A per-dataset read-back scopes `terms` on `ax_file`
// and `term` on `ax_dataset` in the same `bool.filter`,
// so an endpoint that honoured only the latter would
// count every file's records into every dataset.
&& filter
.get("terms")
.and_then(Value::as_object)
.and_then(|terms| terms.iter().next())
.is_none_or(|(field, values)| {
values.as_array().is_some_and(|values| {
values.iter().any(|value| doc.get(field) == Some(value))
})
})
// The batched finalize-verify (#1183's count lane)
// scopes its semantic windows with an `exists` on the
// semantic field inside this same `bool.filter`; the
// engine evaluates it there, so the fake must too or
// every window counts rows the real server excludes.
&& filter
.get("exists")
.and_then(|exists| exists.get("field"))
.and_then(Value::as_str)
.is_none_or(|field| {
!locked.hide_semantic_matches && doc.get(field).is_some()
})
})
&& exists.map(|field| doc.get(field).is_some()).unwrap_or(true)
&& must_not_exists
.map(|field| doc.get(field).is_none())
.unwrap_or(true)
})
.collect();
// #890: honour `from`/`size` when the request states them — the scoped
// catalog sweep pages, and a page has to be a page. A request that omits
// them still gets everything, which is what every older fixture assumes.
// Ordering is by `_id` so a page boundary is not `HashMap` iteration order.
//
// #1183: honour `sort` when the request states it — the dataset read-back's
// time bounds are size-1 searches sorted on the time field, and the first
// hit must BE the extreme, the same invariant a doc-values sort gives the
// real server. A request without `sort` keeps the `_id` order.
let sort_spec: Option<(String, String)> = query
.get("sort")
.and_then(Value::as_array)
.and_then(|clauses| clauses.first())
.and_then(|clause| {
if let Some(field) = clause.as_str() {
return Some((field.to_owned(), "asc".to_owned()));
}
clause
.as_object()
.and_then(|object| object.iter().next())
.map(|(field, spec)| {
(
field.clone(),
spec.get("order")
.and_then(Value::as_str)
.unwrap_or("asc")
.to_owned(),
)
})
});
match &sort_spec {
Some((field, order)) if order == "desc" => {
matching.sort_by(|(_, a), (_, b)| cmp_docs_by_field(b, a, field))
}
Some((field, _)) => matching.sort_by(|(_, a), (_, b)| cmp_docs_by_field(a, b, field)),
None => matching.sort_by_key(|(key, _)| *key),
}
// #2314: one short catalog page, internally consistent (total matches the
// hits actually returned). The per-group equality then falls short and
// the retry after refresh must see the document that was held back.
let catalog_file_key_page = indices.contains(&catalog::CATALOG_INDEX)
&& filters
.iter()
.any(|filter| filter.pointer("/terms/file_key").is_some());
let mut shorten = false;
if catalog_file_key_page && locked.short_catalog_file_key_pages > 0 && matching.len() > 1 {
matching.pop();
shorten = true;
}
let total = matching.len();
let from = query
.get("from")
.and_then(Value::as_u64)
.map_or(0usize, |from| from as usize);
let size = query
.get("size")
.and_then(Value::as_u64)
.map_or(total, |size| size as usize);
let hits = matching
.iter()
.skip(from)
.take(size)
.map(|((_, id), source)| json!({"_id": id, "_source": source}))
.collect::<Vec<_>>();
// #1183's batched finalize-verify reads per-digest counts as a `terms`
// aggregation over the matched set — the engine's terms `doc_count` is
// exact — so the fake counts the same way instead of answering every
// request with an empty aggregations body. Only the `terms` sub-agg is
// evaluated: it is the only shape the read-back asks for.
let aggregations = query
.get("aggs")
.or_else(|| query.get("aggregations"))
.and_then(Value::as_object)
.map(|aggs| {
let mut body = serde_json::Map::new();
for (name, spec) in aggs {
let Some(field) = spec
.get("terms")
.and_then(|terms| terms.get("field"))
.and_then(Value::as_str)
else {
continue;
};
let mut counts: std::collections::BTreeMap<&str, u64> =
std::collections::BTreeMap::new();
for (_, doc) in &matching {
if let Some(key) = doc.get(field).and_then(Value::as_str) {
*counts.entry(key).or_insert(0) += 1;
}
}
body.insert(
name.clone(),
json!({"buckets": counts
.into_iter()
.map(|(key, doc_count)| json!({"key": key, "doc_count": doc_count}))
.collect::<Vec<_>>()}),
);
}
Value::Object(body)
})
.unwrap_or_else(|| json!({}));
drop(matching);
if shorten {
locked.short_catalog_file_key_pages -= 1;
}
json!({
"hits": {
"total": {"value": total, "relation": "eq"},
"hits": hits
},
"aggregations": aggregations
})
}
/// Compare two docs on one field for the fake's `sort` support: numbers
/// numerically, strings lexically, a missing value sorting first (the null
/// ordering the real server gives an unmapped/absent field).
fn cmp_docs_by_field(a: &Value, b: &Value, field: &str) -> std::cmp::Ordering {
let rank = |doc: &Value| match doc.get(field) {
Some(Value::Number(n)) => (1u8, n.as_f64().unwrap_or(0.0), String::new()),
Some(Value::String(s)) => (2u8, 0.0, s.clone()),
_ => (0u8, 0.0, String::new()),
};
rank(a)
.partial_cmp(&rank(b))
.unwrap_or(std::cmp::Ordering::Equal)
}
fn cfg(root: &Path, state_dir: &Path, url: &str, semantic: bool) -> IndexCfg {
IndexCfg {
root: root.to_owned(),
endpoint_url: None,
stub_globs: Vec::new(),
url: url.to_owned(),
api_key: None,
api_key_file: None,
workers: 1,
scan_workers: 1,
pdf_workers: 1,
resource_notes: Vec::new(),
xerj_url_note: None,
pdf_timeout_secs: 30,
bulk_mb: 1,
bulk_timeout_secs: 30,
snapshot_max_bytes: 64 << 30,
prefix: "incremental-http".into(),
replace_prefix: false,
prefix_note: None,
state_dir: Some(state_dir.to_owned()),
fresh: false,
follow_symlinks: false,
follow_symlinks_outside_root: false,
ignore: crate::ignore_rules::IgnoreOptions::default(),
max_file_bytes: 1 << 30,
sample: 50,
no_semantic: !semantic,
code_analyzer: Default::default(),
brain: None,
no_graph: true,
// Gate off: these fixtures assert reconcile behaviour, not a
// timing-derived stop. See `gate_tests` and `cli::tests`.
max_minutes: 0,
approve: None,
max_index_bytes: None,
budget_plan: None,
drop_indexed: false,
dry_run: false,
walk_only: false,
json: false,
quiet: true,
progress: crate::progress::ProgressMode::None,
progress_interval: None,
watch: false,
label: None,
debounce: std::time::Duration::from_millis(0),
exclude_rels: Vec::new(),
exclude: Vec::new(),
skip_corpus_data: false,
}
}
fn journal_events(state_dir: &Path, kind: &str) -> usize {
fs::read_to_string(state_dir.join("journal.ndjson"))
.unwrap()
.lines()
.filter_map(|line| serde_json::from_str::<Value>(line).ok())
.filter(|event| event.get("kind").and_then(Value::as_str) == Some(kind))
.count()
}
fn paths(docs: &[Value]) -> Vec<String> {
let mut paths: Vec<_> = docs
.iter()
.filter_map(|doc| doc.get("ax_path").and_then(Value::as_str))
.map(str::to_owned)
.collect();
paths.sort();
paths.dedup();
paths
}
fn snapshot_names(state_dir: &Path) -> Vec<String> {
let mut names: Vec<String> = fs::read_dir(state_dir.join("sync-snapshots"))
.map(|entries| {
entries
.filter_map(Result::ok)
.filter(|entry| {
entry.file_type().is_ok_and(|kind| kind.is_dir())
&& !entry.file_name().to_string_lossy().starts_with('.')
})
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.collect()
})
.unwrap_or_default();
names.sort();
names
}
fn final_snapshot_count(state_dir: &Path) -> usize {
fs::read_dir(state_dir.join("sync-snapshots"))
.map(|entries| {
entries
.filter_map(Result::ok)
.filter(|entry| {
entry.file_type().is_ok_and(|kind| kind.is_dir())
&& !entry.file_name().to_string_lossy().starts_with('.')
})
.count()
})
.unwrap_or(0)
}
/// #755: an older-build global catalog holds `prefix` (and, on a fully dynamic
/// catalog, `doc_kind`) mapped as `text`, so the generation's keyword `_mapping`
/// update conflicts. Before this fix the whole run aborted — and because
/// `autoindex-catalog` is ONE index shared by every corpus on the node, that
/// bricked `xerj autoindex` outright for anyone upgrading from v1.0.0-rc.15..67.
///
/// An upgrade must never abort a run over a mapping an earlier release left
/// behind: the run has to complete, every field the engine will accept has to be
/// installed, and corpus scoping has to keep working — which it does by moving
/// onto `catalog::CORPUS_SCOPE_FIELD`, a keyword field no older release wrote.
///
/// End-to-end on purpose (#760): `install_catalog_mapping`'s own unit tests pass
/// even if the wiring at the catalog `update_mapping` is dropped, so this is what
/// actually guards it.
#[test]
fn a_legacy_text_mapped_catalog_completes_the_run_instead_of_aborting() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().legacy_text_catalog_fields =
vec!["prefix".into(), "doc_kind".into()];
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
// The run COMPLETES. This is the whole bug: it used to abort here.
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(endpoint.data_docs().len(), 1);
// Exactly the two legacy fields were given up, and nothing else.
let installed = endpoint.installed_catalog_properties();
let installed = installed.as_object().expect("properties object");
assert!(
!installed.contains_key("prefix") && !installed.contains_key("doc_kind"),
"a field the catalog already holds as text cannot be re-declared: {installed:?}"
);
assert_eq!(
installed["path"]["type"], "keyword",
"every non-conflicting field must still be installed: {installed:?}"
);
// …including the keyword field corpus scoping moved onto. No release ever
// wrote it, so a legacy catalog cannot be holding it as text.
assert_eq!(
installed[catalog::CORPUS_SCOPE_FIELD]["type"],
"keyword",
"the corpus-scope field must survive the legacy conflict: {installed:?}"
);
// And the published catalog documents carry the scope on that field, so the
// #737/#693 scoped sweeps stay exact on this node.
let scoped = endpoint
.catalog_docs()
.into_iter()
.filter(|doc| doc.get("doc_kind").and_then(Value::as_str) == Some("file"))
.collect::<Vec<_>>();
assert!(!scoped.is_empty(), "the run published no file catalog docs");
for doc in &scoped {
assert_eq!(
doc[catalog::CORPUS_SCOPE_FIELD],
"incremental-http",
"every file catalog doc must carry the keyword corpus scope: {doc}"
);
}
}
/// #755: the conflict tolerance is narrow on purpose. A catalog `_mapping` PUT
/// that fails for any reason OTHER than a legacy field type — an unreachable
/// node, a 503, an unrelated 400 — is a real failure and must still abort,
/// rather than being retried into a silently narrower catalog mapping.
#[test]
fn an_unavailable_catalog_mapping_still_aborts_the_run() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().catalog_mapping_unavailable = true;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let msg = format!("{:#}", run_index(config).unwrap_err());
assert!(
msg.contains("install generation catalog mapping") && msg.contains("503"),
"an unrelated mapping failure must abort with its own reason: {msg}"
);
assert_eq!(
endpoint.state.lock().unwrap().catalog_mapping_puts,
1,
"a non-conflict failure must not be retried: {msg}"
);
}
/// #755: tolerating the legacy mapping is not the same as hiding it. Documents
/// an older build already wrote keep the legacy type, so a scoped sweep can
/// still miss them — the operator has to be told, through the progress surface
/// that owns stderr (#241), with the reindex that retires the field.
#[test]
fn a_legacy_text_mapped_catalog_warns_through_the_progress_surface() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().legacy_text_catalog_fields = vec!["prefix".into()];
let es = Es::with_bulk_timeout(&endpoint.url, None, 30).expect("es client");
let (pr, sink) = progress::Progress::capture(
progress::Surface::Plain,
std::time::Duration::from_secs(3600),
);
ensure_generation_mappings(&es, &Plan::default(), &pr).expect("legacy catalog must not abort");
let text = String::from_utf8(sink.lock().unwrap().clone()).unwrap();
assert!(
text.contains("older build") && text.contains("`prefix`"),
"the operator must be told which field is legacy: {text}"
);
assert!(
text.contains("run CONTINUES") && text.contains("_reindex"),
"…and what happened plus how to retire it: {text}"
);
assert!(
text.contains(catalog::CORPUS_SCOPE_FIELD),
"…and where corpus scoping moved to: {text}"
);
}
/// #755: a healthy catalog is untouched — one `_mapping` PUT, nothing dropped.
#[test]
fn a_current_catalog_installs_its_mapping_in_a_single_put() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let endpoint = HttpEndpoint::start();
let es = Es::with_bulk_timeout(&endpoint.url, None, 30).expect("es client");
let (pr, sink) = progress::Progress::capture(
progress::Surface::Plain,
std::time::Duration::from_secs(3600),
);
ensure_generation_mappings(&es, &Plan::default(), &pr).expect("fresh catalog");
assert_eq!(endpoint.state.lock().unwrap().catalog_mapping_puts, 1);
assert_eq!(
endpoint.installed_catalog_properties()["prefix"]["type"],
"keyword",
"a catalog with no legacy field keeps the full declared mapping"
);
assert!(
String::from_utf8(sink.lock().unwrap().clone())
.unwrap()
.is_empty(),
"a healthy catalog must not warn"
);
}
/// Rewrite a journal so every generation in it carries the `index_identity` the
/// PREVIOUSLY SHIPPED release froze for the same plan — the exact on-disk state
/// of an install that is being upgraded.
///
/// The digests that bind the log are recomputed with it (`desired_manifest_digest`
/// on the begin/validate/commit records, and the next generation's
/// `base_manifest_digest`), so the journal stays internally valid and the only
/// thing that has moved is the frozen contract identity. Returns the identity
/// written in (previous release) and the one this build had written (this
/// build); while the contract holds they are equal and the rewrite is a
/// byte-for-byte no-op — that is the property under test.
fn freeze_journal_identities_as_previous_release(state_dir: &Path) -> (String, String) {
let path = state_dir.join("journal.ndjson");
let raw = fs::read_to_string(&path).unwrap();
let mut previous_release = None;
let mut this_build = None;
let mut committed_digest: Option<String> = None;
let mut pending_digest: Option<String> = None;
let mut out = String::new();
for line in raw.lines() {
let Ok(mut record) = serde_json::from_str::<Value>(line) else {
out.push_str(line);
out.push('\n');
continue;
};
match record.get("kind").and_then(Value::as_str) {
Some("sync_begin") => {
if let Some(digest) = &committed_digest {
record["base_manifest_digest"] = json!(digest);
}
let plan: Plan = serde_json::from_value(record["desired"]["plan"].clone()).unwrap();
let frozen = frozen_contract::index_identity(&plan);
this_build = record["desired"]["execution"]["index_identity"]
.as_str()
.map(str::to_owned);
record["desired"]["execution"]["index_identity"] = json!(frozen);
previous_release = Some(frozen);
let manifest: crate::sync::GenerationManifest =
serde_json::from_value(record["desired"].clone()).unwrap();
let digest = crate::sync::manifest_digest(&manifest).unwrap();
record["desired_manifest_digest"] = json!(digest);
pending_digest = Some(digest);
}
Some("sync_validated") | Some("sync_abort") => {
record["desired_manifest_digest"] = json!(pending_digest.clone().unwrap());
}
Some("sync_commit") => {
let digest = pending_digest.clone().unwrap();
record["desired_manifest_digest"] = json!(digest);
committed_digest = Some(digest);
}
_ => {}
}
out.push_str(&serde_json::to_string(&record).unwrap());
out.push('\n');
}
fs::write(&path, out).unwrap();
(
previous_release.expect("the journal has a sync_begin"),
this_build.expect("the sync_begin carries an execution identity"),
)
}
/// #755 upgrade path, end to end: generations frozen by the PREVIOUS RELEASE
/// must still be reconcilable by this build.
///
/// `index_identity` is hashed out of `catalog::catalog_mapping()` and frozen in
/// the journal's execution record, and two hard `ensure!`s then demand equality
/// against it: `provision_generation` when a pending (mid-commit) generation is
/// replayed ("desired generation index identity disagrees with its frozen
/// mappings"), and the incremental-reconcile no-change arm ("autoindex
/// execution configuration changed since the committed generation") — which
/// writes no new generation, so a mismatch there never heals. Declaring one
/// extra catalog property would therefore trade #755's abort for a different
/// abort against the very same upgrading population.
///
/// Both arms run here over a journal rewritten to the previous release's
/// identity: an unchanged corpus first (the no-change arm), then a generation
/// left pending mid-commit (the replay).
/// `catalog_mapping_is_the_frozen_on_disk_contract` pins the contract itself;
/// this test pins the wiring that consumes it.
#[test]
fn a_generation_frozen_by_the_previous_release_still_reconciles() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("a.csv");
fs::write(&source, "id,value\n1,committed\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
// One committed generation, aged to the previous release's contract.
assert_eq!(run_index(config.clone()).unwrap(), 0);
let (previous_release, this_build) =
freeze_journal_identities_as_previous_release(state_dir.path());
// Unchanged corpus → the no-change arm's ensure!. This is the permanent
// one: it commits no new generation, so a mismatch here never heals.
let unchanged = run_index(config.clone()).unwrap_or_else(|error| {
panic!(
"an unchanged corpus committed by the previous release must reconcile, not abort \
(previous release froze {previous_release}, this build computes {this_build}): \
{error:#}"
)
});
assert_eq!(unchanged, 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// Now a generation left pending mid-commit, aged the same way.
fs::write(&source, "id,value\n1,sealed-pending\n2,sealed-second\n").unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
assert!(run_index(config.clone()).is_err());
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let (previous_release, this_build) =
freeze_journal_identities_as_previous_release(state_dir.path());
// Replay of that pending generation → `provision_generation`'s ensure!.
let resumed = run_index(config).unwrap_or_else(|error| {
panic!(
"a pending generation frozen by the previous release must replay, not abort \
(previous release froze {previous_release}, this build computes {this_build}): \
{error:#}"
)
});
assert_eq!(resumed, 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
assert_eq!(endpoint.data_docs().len(), 2);
assert_eq!(
previous_release, this_build,
"this build must compute the identity the previous release froze; the catalog mapping \
is hashed into it, so a property added there aborts every upgraded state dir"
);
}
#[test]
fn genesis_recovers_after_snapshot_budget_failure_before_sync_begin() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.snapshot_max_bytes = 1;
let error = run_index(config.clone()).unwrap_err();
assert!(format!("{error:#}").contains("snapshot source footprint"));
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
assert_eq!(endpoint.data_bulk_requests(), 0);
config.snapshot_max_bytes = 64 << 30;
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(endpoint.data_docs().len(), 1);
}
#[test]
fn no_semantic_generation_does_not_require_embedding_identity_endpoint() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
endpoint.state.lock().unwrap().fail_embedding_identity = true;
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(journal_events(state_dir.path(), "finish"), 1);
assert_eq!(final_snapshot_count(state_dir.path()), 1);
assert_eq!(endpoint.data_docs().len(), 1);
}
/// #1183: the finalize read-back must not carry a corpus-materialising agg.
/// A `min`/`max` agg on the dataset's time field has no columnar fast path
/// under the sharded memtable, so a real server deep-clones every matching
/// document into owned Values ("aggregation corpus materialisation", ~2KB a
/// doc against `max_query_memory_mb`) — on the xerj-search rebuild one
/// ~570k-record dataset estimated 1.1GB against the 512MB default, the
/// server answered 429 circuit_breaking_exception, and the client retried
/// for its full 600s budget before aborting: the multi-hour
/// "finalize-catalog deadlock". The dataset read-back's count runs without
/// aggs and its time bounds come from size-1 searches sorted on the time
/// field, which sort from doc values and clone nothing. The batched
/// generation-wide verify (`sync_executor::validate`) is the one agg the
/// run issues — an exact `terms` count on a keyword digest field, and only
/// behind its outer `terms`-on-digest window filter, which prunes the walk
/// before any agg sees a document. What this guards against is the
/// materialising family coming back in any read-back.
#[test]
fn dataset_read_back_bounds_time_by_sort_and_no_materialising_agg() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("a.csv"),
"id,cloned_at\n1,2026-10-01T00:00:00.000Z\n2,2026-10-02T00:00:00.000Z\n3,2026-10-03T00:00:00.000Z\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, _) = run_index_report(config).unwrap();
assert_eq!(code, 0);
// 1. No search this run issued carries a corpus-materialising
// aggregation. The only agg allowed anywhere is the batched
// finalize-verify's exact `terms` count — and that one only paired
// with the outer `terms`-on-digest window filter that prunes the walk.
let bodies = endpoint.search_bodies();
assert!(!bodies.is_empty());
for body in &bodies {
let Some(aggs) = body
.get("aggs")
.or_else(|| body.get("aggregations"))
.and_then(Value::as_object)
else {
continue;
};
for (name, spec) in aggs {
assert!(
spec.get("terms").is_some(),
"#1183: a search carries the corpus-materialising agg {name}: {body}"
);
}
assert!(
body.pointer("/query/terms").is_some()
|| body.pointer("/query/bool/filter/0/terms").is_some(),
"a terms agg rides a search with no digest-window filter: {body}"
);
}
// 2. The time bounds are the sorted size-1 searches: one ascending, one
// descending, on the dataset's elected time field, under the dataset's
// own ax_dataset filter.
let sorted: Vec<&Value> = bodies
.iter()
.filter(|body| body.pointer("/sort/0/cloned_at").is_some())
.collect();
assert_eq!(
sorted.len(),
2,
"one ascending and one descending bound query: {bodies:?}"
);
assert!(
sorted
.iter()
.any(|body| body.pointer("/sort/0/cloned_at/order") == Some(&json!("asc"))),
"ascending bound missing: {bodies:?}"
);
assert!(
sorted
.iter()
.any(|body| body.pointer("/sort/0/cloned_at/order") == Some(&json!("desc"))),
"descending bound missing: {bodies:?}"
);
for body in &sorted {
assert_eq!(body.get("size"), Some(&json!(1)), "size-1 query: {body}");
assert!(
body.pointer("/query/bool/filter").is_some(),
"bound query keeps the dataset filter: {body}"
);
}
// 3. The catalog document carries the corpus's TRUE bounds — the fake
// honours `sort`, so the first hit is the extreme, exactly like the
// doc-values sort on a real server.
let dataset = endpoint
.catalog_doc("ds:incremental-http:csv")
.expect("dataset catalog document must exist");
assert_eq!(dataset["time_field"], json!("cloned_at"));
assert_eq!(dataset["time_min"], json!("2026-10-01T00:00:00.000Z"));
assert_eq!(dataset["time_max"], json!("2026-10-03T00:00:00.000Z"));
}
#[test]
fn initial_generation_and_noop_are_end_to_end_idempotent() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config.clone()).unwrap();
assert_eq!(code, 0);
let summary = summary.expect("generated run must return its committed run projection");
assert_eq!(summary["generation"], 1);
assert_eq!(summary["records_total"], 2);
let first_docs = endpoint.data_docs();
assert_eq!(first_docs.len(), 2);
assert_eq!(paths(&first_docs), ["a.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let root = corpus.path().canonicalize().unwrap();
let preflight = state::Journal::preflight(
state_dir.path(),
&root.to_string_lossy(),
&endpoint.url,
"incremental-http",
false,
)
.unwrap();
assert!(
preflight.committed_manifest.is_some(),
"sync_commit must be visible to the next preflight"
);
drop(preflight);
let bulks = endpoint.data_bulk_requests();
let (_, noop_summary) = run_index_report(config).unwrap();
assert_eq!(noop_summary.unwrap()["generation"], 1);
assert_eq!(endpoint.data_docs(), first_docs);
assert_eq!(endpoint.data_bulk_requests(), bulks);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(journal_events(state_dir.path(), "finish"), 2);
}
/// Incremental re-index must NOT re-run the Phase-A scan/parse for files whose
/// content is byte-identical to the committed generation — that is the ~100x
/// win for the edit-and-rerun / CI workflow, since the tree-sitter parse
/// dominates per-file cost. Proven directly with the run's own `ScanTally`
/// (0 on an unchanged re-run, exactly the changed count when one file changes),
/// and the committed index stays byte-identical.
///
/// #891: each measurement uses a tally created for THAT run. The counter used
/// to be a process-global `static`, which meant this test read whatever every
/// other test in the binary happened to parse in the same window — see
/// `another_runs_parses_never_land_in_this_runs_tally` below.
#[test]
fn unchanged_reindex_skips_the_parse_and_stays_byte_identical() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
// A code file (tree-sitter parse is the expensive Phase-A work) + a data file.
fs::write(
corpus.path().join("main.py"),
"def add(a, b):\n return a + b\n\nclass Calc:\n def run(self):\n return add(1, 2)\n",
)
.unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
// Genesis: everything is parsed.
let genesis = crate::ScanTally::default();
assert_eq!(run_index_tallied(config.clone(), &genesis).unwrap(), 0);
assert_eq!(
genesis.files_parsed(),
2,
"genesis has no committed generation to skip against: both files are parsed"
);
let first_docs = endpoint.data_docs();
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// Re-index with NOTHING changed → zero files parsed, index byte-identical.
let unchanged = crate::ScanTally::default();
assert_eq!(run_index_tallied(config.clone(), &unchanged).unwrap(), 0);
assert_eq!(
unchanged.files_parsed(),
0,
"unchanged re-index must parse zero files"
);
assert_eq!(
endpoint.data_docs(),
first_docs,
"unchanged re-index must leave the index byte-identical"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// Change ONE file → exactly that file is parsed (gate is digest-driven, not
// a blanket skip).
fs::write(
corpus.path().join("main.py"),
"def add(a, b):\n return a + b + 1\n",
)
.unwrap();
let one_changed = crate::ScanTally::default();
assert_eq!(run_index_tallied(config.clone(), &one_changed).unwrap(), 0);
assert_eq!(
one_changed.files_parsed(),
1,
"only the one changed file must be parsed; the unchanged file stays skipped"
);
}
/// #891: a parse measurement must describe the run that made it, and nothing
/// else the process is doing.
///
/// The counter above used to be one process-global `static`. `cargo test` runs
/// this lib test binary multi-threaded, the measuring test only holds
/// `HTTP_E2E_LOCK`, and every other test that parses files — those under
/// `FAILPOINT_TEST_LOCK`, `generation_catalog_http_tests`, plain unit tests —
/// added to the same number. Both assertions above could therefore read a total
/// that was not theirs: 2 of 6 and then 1 of 6 local suite runs on the branch
/// that found it (#886) went red here with no code change between them. It is a
/// scheduling race, so plain `main` can also sit clean for twenty consecutive
/// runs — which is exactly why the proof below does not rely on luck.
///
/// This reproduces that interference deterministically instead of waiting for
/// the scheduler to reproduce it: a SECOND, unrelated corpus is indexed inside
/// the first run's measurement window — after its run finishes, before its
/// number is read. That is the event the flake needed, sequenced rather than
/// raced; a genuinely concurrent second run would trade this flake for a new
/// one, since these fixtures are serialised by `HTTP_E2E_LOCK` for reasons of
/// their own. Against the old global counter the unchanged re-index reads the
/// interfering run's 2 parses instead of its own 0; against a per-run tally
/// each run reads only its own work.
#[test]
fn another_runs_parses_never_land_in_this_runs_tally() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
// The measured corpus, indexed once so that its re-index has something to
// skip.
let measured_corpus = tempfile::tempdir().unwrap();
let measured_state = tempfile::tempdir().unwrap();
fs::write(
measured_corpus.path().join("main.py"),
"def add(a, b):\n return a + b\n",
)
.unwrap();
fs::write(
measured_corpus.path().join("a.csv"),
"id,value\n1,alpha\n2,beta\n",
)
.unwrap();
let measured_endpoint = HttpEndpoint::start();
let measured_config = cfg(
measured_corpus.path(),
measured_state.path(),
&measured_endpoint.url,
false,
);
assert_eq!(
run_index_tallied(measured_config.clone(), &crate::ScanTally::default()).unwrap(),
0
);
// An unrelated corpus, standing in for whatever else the test binary is
// running. Its own genesis parses two files.
let other_corpus = tempfile::tempdir().unwrap();
let other_state = tempfile::tempdir().unwrap();
fs::write(
other_corpus.path().join("other.py"),
"def mul(a, b):\n return a * b\n",
)
.unwrap();
fs::write(
other_corpus.path().join("other.csv"),
"id,value\n7,gamma\n8,delta\n",
)
.unwrap();
let other_endpoint = HttpEndpoint::start();
let other_config = cfg(
other_corpus.path(),
other_state.path(),
&other_endpoint.url,
false,
);
// The measurement window: measure, let the other run happen, then read.
let measured = crate::ScanTally::default();
assert_eq!(run_index_tallied(measured_config, &measured).unwrap(), 0);
let other = crate::ScanTally::default();
assert_eq!(run_index_tallied(other_config, &other).unwrap(), 0);
assert_eq!(
other.files_parsed(),
2,
"the interfering run really did parse files — otherwise this proves nothing"
);
assert_eq!(
measured.files_parsed(),
0,
"an unchanged re-index parses nothing; another run's parses must not be \
added to this run's tally (#891)"
);
}
#[test]
fn add_change_delete_and_rename_converge_over_real_http() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
fs::write(corpus.path().join("b.csv"), "id,value\n2,bravo\n").unwrap();
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv", "b.csv"]);
fs::write(
corpus.path().join("a.csv"),
"id,value\n1,alpha-new\n3,charlie\n",
)
.unwrap();
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(endpoint.data_docs().len(), 3);
fs::remove_file(corpus.path().join("b.csv")).unwrap();
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv"]);
fs::rename(
corpus.path().join("a.csv"),
corpus.path().join("renamed.csv"),
)
.unwrap();
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(paths(&endpoint.data_docs()), ["renamed.csv"]);
assert_eq!(endpoint.data_docs().len(), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 5);
}
/// #580: re-indexing a **code file** must not abort. The AST file document
/// carries a `symbols` array (of objects); `FieldAcc` does not scalar-type it,
/// so it is pruned from the frozen dataset schema at genesis — and the second
/// generation observed `symbols` again and aborted with `field symbols is
/// absent from frozen dataset`. A field with no scalar values is now allowed to
/// be absent from the frozen schema.
#[test]
fn code_file_reindex_does_not_abort_on_absent_symbols_field() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("lib.rs"),
"pub struct AlphaConfig {\n pub retries: u32,\n}\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
// Genesis.
assert_eq!(run_index(config.clone()).unwrap(), 0);
// Edit the code file and re-index — before #580 this aborted the whole run
// on the frozen `symbols` field.
fs::write(
corpus.path().join("lib.rs"),
"pub struct AlphaConfig {\n pub retries: u64,\n}\n",
)
.unwrap();
assert_eq!(
run_index(config).unwrap(),
0,
"re-indexing a code file must reconcile, not abort on the frozen `symbols` field (#580)"
);
// The struct is still indexed (file doc + its symbol doc).
assert!(
endpoint
.data_docs()
.iter()
.any(|d| d.get("name").is_some_and(|n| n == "AlphaConfig")),
"the struct's symbol doc must survive the re-index"
);
}
/// #2139: re-indexing a changed code file must drop documents for symbols that
/// no longer exist, including when a declaration moves to a new line.
///
/// Per-symbol `_id`s are `code:<line>:<name>` (#500). A signature edit that
/// also shifts the declaration does not overwrite the old symbol document, so
/// the file-level delete on `ax_file` is what has to remove it. The sibling
/// code reindex above changes a field type without moving the declaration, so
/// the same locator is overwritten and a missed delete still looks clean.
///
/// `--no-graph` (this module). The graph-enabled twin lives in
/// `failure_resume_http_tests`.
#[test]
fn changed_code_file_reindex_drops_shifted_and_removed_symbols() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("jobs.rs");
fs::write(&source, CODE_V1).unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let first = endpoint.data_docs();
assert!(
symbol_rows(&first).iter().any(|(name, _, code)| {
name == "query_job" && code.contains("group_id") && !code.contains("&self")
}),
"genesis must index the original query_job symbol: {}",
serde_json::to_string(&symbol_rows(&first)).unwrap()
);
assert!(
symbol_rows(&first)
.iter()
.any(|(name, _, _)| name == "gone_symbol"),
"genesis must index gone_symbol: {}",
serde_json::to_string(&symbol_rows(&first)).unwrap()
);
fs::write(&source, CODE_V2).unwrap();
assert_eq!(
run_index(config).unwrap(),
0,
"an ordinary incremental reindex of an edited code file must succeed"
);
let live = endpoint.data_docs();
assert_code_matches_fresh(&live, "no-graph incremental reindex");
}
/// A from-scratch index of [`CODE_V2`], the oracle for #2139: the edited file's
/// live documents must match this, not a superset that still holds v1 symbols.
fn fresh_code_v2_docs() -> Vec<Value> {
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("jobs.rs"), CODE_V2).unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config).unwrap(), 0);
endpoint.data_docs()
}
fn assert_code_matches_fresh(live: &[Value], label: &str) {
let rendered = serde_json::to_string(live).unwrap();
assert!(
!rendered.contains("gone_symbol"),
"{label}: removed symbol gone_symbol is still indexed: {rendered}"
);
assert!(
!rendered.contains("pub fn query_job(group_id"),
"{label}: the pre-edit query_job signature is still indexed: {rendered}"
);
assert!(
rendered.contains("pub fn query_job(&self"),
"{label}: the current query_job signature is missing: {rendered}"
);
let fresh = fresh_code_v2_docs();
assert_eq!(
symbol_rows(live),
symbol_rows(&fresh),
"{label}: symbol documents (name, line, code) must match a fresh index of the edited file"
);
assert_eq!(
live.len(),
fresh.len(),
"{label}: document count must match a fresh index of the edited file\n\
live={rendered}\nfresh={}",
serde_json::to_string(&fresh).unwrap()
);
}
/// `(name, line, code)` of per-symbol documents, in a stable order.
fn symbol_rows(docs: &[Value]) -> Vec<(String, u64, String)> {
let mut rows: Vec<(String, u64, String)> = docs
.iter()
.filter(|doc| doc.get("defs").is_none())
.filter_map(|doc| {
Some((
doc.get("name")?.as_str()?.to_string(),
doc.get("line")?.as_u64()?,
doc.get("code")?.as_str()?.to_string(),
))
})
.collect();
rows.sort();
rows
}
/// v1: `gone_symbol` plus a two-argument `query_job`.
const CODE_V1: &str = "\
struct Jobs;\n\
\n\
impl Jobs {\n\
pub fn gone_symbol() -> i32 {\n\
1\n\
}\n\
\n\
pub fn query_job(group_id: &str, raw: &str) -> bool {\n\
!raw.is_empty()\n\
}\n\
}\n\
";
/// v2: `gone_symbol` removed, `query_job` gains `&self` and moves down.
const CODE_V2: &str = "\
struct Jobs;\n\
\n\
impl Jobs {\n\
// the signature gained &self and the declaration moved down\n\
\n\
\n\
pub fn query_job(&self, group_id: &str, raw: &str) -> bool {\n\
!raw.is_empty()\n\
}\n\
}\n\
";
#[test]
fn pending_generation_replays_sealed_source_after_live_source_mutates() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("a.csv");
fs::write(&source, "id,value\n1,committed\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
fs::write(&source, "id,value\n1,sealed-pending\n2,sealed-second\n").unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
assert!(run_index(config.clone()).is_err());
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
fs::write(&source, "id,value\n9,mutated-after-sync-begin\n").unwrap();
assert_eq!(run_index(config).unwrap(), 0);
let docs = endpoint.data_docs();
assert_eq!(docs.len(), 2);
let rendered = serde_json::to_string(&docs).unwrap();
assert!(rendered.contains("sealed-pending"), "{rendered}");
assert!(rendered.contains("sealed-second"), "{rendered}");
assert!(!rendered.contains("mutated-after-sync-begin"), "{rendered}");
}
/// Crash/resume with a *partially applied* data bulk, on `--no-graph`.
///
/// `failure_resume_http_tests` used to run its whole injected-failure matrix
/// with `no_graph: true`; since `--no-graph` now takes the generated executor,
/// that module keeps the legacy graph-enabled transaction and this test carries
/// the no-graph half of the coverage. The endpoint applies half of a bulk and
/// *then* reports failure — visibility has changed, so the generation must stay
/// uncommitted, and the retry must converge on exactly the new content with no
/// duplicate or stale document surviving the half-applied attempt.
#[test]
fn partially_applied_data_bulk_leaves_the_generation_pending_and_the_retry_converges() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("rows.csv");
fs::write(&source, "id,value\n1,first\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
fs::write(
&source,
"id,value\n1,replaced\n2,second\n3,third\n4,fourth\n",
)
.unwrap();
endpoint
.state
.lock()
.unwrap()
.partially_apply_next_data_bulk = true;
assert!(
run_index(config.clone()).is_err(),
"a partially applied bulk must fail the run, not be absorbed"
);
assert_eq!(
journal_events(state_dir.path(), "sync_begin"),
2,
"the generation was begun"
);
assert_eq!(
journal_events(state_dir.path(), "sync_commit"),
1,
"a partially applied bulk must never commit the generation"
);
// Retry: the sealed source replays, and the destination converges exactly.
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
let docs = endpoint.data_docs();
assert_eq!(paths(&docs), ["rows.csv"]);
assert_eq!(
docs.len(),
4,
"exactly the four new records survive — no half-applied duplicate, no stale first record"
);
let rendered = serde_json::to_string(&docs).unwrap();
assert!(rendered.contains("replaced"), "{rendered}");
assert!(rendered.contains("fourth"), "{rendered}");
assert!(
!rendered.contains("\"value\":\"first\""),
"the replaced record must not survive: {rendered}"
);
}
/// #490 (fix #3): a corpus committed by the `--no-graph` generated path must not
/// be silently re-runnable on the default *graph* path. A committed generation
/// manifest is graph-disabled by construction (`sync::validate_manifest`), so
/// the graph path recognises the mismatch and refuses it — the way the
/// graph→no-graph direction is already refused.
///
/// Scope of what this harness proves. Without the guard the re-run still fails,
/// but only later and opaquely: `write_plan`'s own `ensure!` rejects it with
/// `legacy plan write cannot follow a committed generated manifest`, *after*
/// `open_after_preflight` and `gc_snapshots` have run. On real binaries whose
/// graph path reaches Phase B before that point the destination is mutated first
/// (#490 matrix: 79 → 80 docs); this in-process harness is backstopped by that
/// `ensure!`, so it exercises the *message and ordering*, not the remote row.
/// The load-bearing contract pinned here is therefore: the run is refused with
/// the clear cross-path message and NOT the opaque `write_plan` one (i.e. the
/// guard preempts it, before the journal is opened for write). The destination
/// staying clean is asserted as a belt-and-suspenders invariant — true here
/// regardless of the guard, so a future change that lets the graph path reach
/// Phase B before refusing is caught.
#[test]
fn no_graph_committed_corpus_is_refused_on_the_graph_path_with_a_clear_message() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("rows.csv");
fs::write(&source, "id,value\n1,first\n").unwrap();
let endpoint = HttpEndpoint::start();
// Commit generation 1 with the `--no-graph` generated executor.
let no_graph = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(no_graph.clone()).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let committed = endpoint.data_docs().len();
assert!(committed > 0, "the no-graph run published documents");
// Grow the corpus so a graph re-run would have a new row to publish, then
// re-run the *same* state dir on the default graph path (`no_graph: false`).
fs::write(&source, "id,value\n1,first\n2,second\n").unwrap();
let mut graph = no_graph.clone();
graph.no_graph = false;
let error = run_index(graph).unwrap_err();
let rendered = format!("{error:#}");
// Load-bearing pair. Fail-before, `rendered` IS the opaque `write_plan`
// error (thrown after journal-open/gc_snapshots) and lacks this phrase; the
// guard replaces it with the clear, actionable one *and* preempts the opaque
// one, proving the refusal now lands earlier.
assert!(
rendered.contains("indexed with --no-graph"),
"the graph re-run must be refused with the clear cross-path message, got: {rendered}"
);
assert!(
!rendered.contains("legacy plan write cannot follow a committed generated manifest"),
"the guard must preempt write_plan's opaque late error, got: {rendered}"
);
// Belt-and-suspenders (true here regardless of the guard, because this
// harness is backstopped by write_plan's `ensure!`): the destination is left
// clean — the added row was not published and no second generation committed.
// Asserted so a future change that lets the graph path reach Phase B before
// refusing is caught as a mutation.
assert_eq!(
endpoint.data_docs().len(),
committed,
"the refused graph re-run must not publish the added row"
);
assert_eq!(
journal_events(state_dir.path(), "sync_commit"),
1,
"no second generation may be committed by the refused cross-path re-run"
);
}
#[test]
fn semantic_count_mismatch_reports_counts_and_datasets_and_resumes() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("report.rs"),
"pub fn quarterly_report() -> &'static str { \"Durable subscription revenue and operating income\" }\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().hide_semantic_matches = true;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, true);
let error = run_index(config.clone()).expect_err("semantic read-back must refuse the run");
let message = format!("{error:#}");
let journal = fs::read_to_string(state_dir.path().join("journal.ndjson")).unwrap();
let begin: Value = journal
.lines()
.map(|line| serde_json::from_str::<Value>(line).unwrap())
.find(|event| event["kind"] == "sync_begin")
.unwrap();
let desired: crate::sync::GenerationManifest =
serde_json::from_value(begin["desired"].clone()).unwrap();
assert_eq!(desired.groups.len(), 1);
let group = &desired.groups[0];
assert!(group.expected_passages > 0);
assert!(group.expected_vectors > 0);
assert!(!group.dataset_slugs.is_empty());
for expected in [
format!(
"live semantic count disagrees with desired group {}",
group.group_id
),
"live_semantic_records=0".into(),
format!("expected_passages={}", group.expected_passages),
format!("expected_vectors={}", group.expected_vectors),
format!("datasets={:?}", group.dataset_slugs),
] {
assert!(
message.contains(&expected),
"missing {expected:?}: {message}"
);
}
assert_eq!(journal_events(state_dir.path(), "sync_validated"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
let published = endpoint.data_docs();
assert!(
!published.is_empty(),
"the mismatch happens after publication"
);
// Restore truthful read-backs: the same durable generation must resume
// without duplicating or changing the documents it already published.
endpoint.state.lock().unwrap().hide_semantic_matches = false;
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(endpoint.data_docs(), published);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
#[test]
fn pending_semantic_identity_drift_rejects_before_any_further_bulk() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("report.txt");
fs::write(
&source,
"Initial quarterly report discusses durable subscription revenue and operating income.",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, true);
assert_eq!(run_index(config.clone()).unwrap(), 0);
fs::write(
&source,
"Updated quarterly report discusses materially stronger subscription revenue and margin.",
)
.unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
assert!(run_index(config.clone()).is_err());
let bulks_before = endpoint.data_bulk_requests();
endpoint.state.lock().unwrap().embedding_identity_sha256 = "b".repeat(64);
let error = run_index(config).unwrap_err();
let message = format!("{error:#}");
assert!(
message.contains("different embedding execution identity"),
"{message}"
);
assert!(
message.contains("no remote mutation was attempted"),
"{message}"
);
assert_eq!(
endpoint.data_bulk_requests(),
bulks_before,
"identity drift must be rejected before another data bulk"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
/// `--fresh` must be refused before it can destroy generation authority.
///
/// `Journal::open_after_preflight` deletes `journal.ndjson` whenever `fresh` is
/// set, and `gc_snapshots` then sees an empty protected set and removes every
/// sealed snapshot. Both run inside the generated branches, ahead of the legacy
/// plan gate, so without the preflight refusal a single `--fresh` erases the
/// committed manifest and every sealed source the next run needs.
#[test]
fn fresh_cannot_destroy_a_committed_generation() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
fs::write(corpus.path().join("b.csv"), "id,value\n2,bravo\n").unwrap();
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let committed_docs = endpoint.data_docs();
assert_eq!(paths(&committed_docs), ["a.csv", "b.csv"]);
let journal_path = state_dir.path().join("journal.ndjson");
let journal_before = fs::read(&journal_path).unwrap();
let snapshots_before = snapshot_names(state_dir.path());
assert_eq!(snapshots_before.len(), 1, "{snapshots_before:?}");
let requests_before = endpoint.state.lock().unwrap().requests.len();
// The folder also changed, which is exactly when a user reaches for
// --fresh.
fs::remove_file(corpus.path().join("b.csv")).unwrap();
config.fresh = true;
let error = run_index(config.clone()).unwrap_err();
let message = format!("{error:#}");
assert!(
message.contains("`--fresh` cannot discard committed corpus generation 1"),
"the refusal must name the durable state that blocks it: {message}"
);
assert!(
message.contains("without `--fresh`"),
"the refusal must point at the incremental path that does work: {message}"
);
assert_eq!(
fs::read(&journal_path).unwrap(),
journal_before,
"a refused --fresh must not rewrite the journal"
);
assert_eq!(
snapshot_names(state_dir.path()),
snapshots_before,
"a refused --fresh must not garbage-collect sealed snapshots"
);
let attempted = endpoint.state.lock().unwrap().requests[requests_before..].to_vec();
assert_eq!(
attempted,
[
("GET".to_owned(), "/".to_owned()),
("GET".to_owned(), "/_cat/allocation?format=json".to_owned(),),
],
"a refused --fresh may perform only the readiness GET and the read-only flood-stage probe"
);
// The surviving authority still reconciles the real folder change.
config.fresh = false;
let (code, summary) = run_index_report(config).unwrap();
assert_eq!(code, 0);
assert_eq!(summary.unwrap()["generation"], 2);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}
/// Every refusal on the generated path now sends the operator to an isolated
/// rebuild, so that recipe has to work. `--fresh` is no longer a rebuild-in-
/// place escape hatch, and a new `--prefix` alone is not enough when the state
/// directory was named explicitly: `preflight` refuses a journal recorded for a
/// different prefix before the run reaches anything else.
#[test]
fn the_isolated_rebuild_the_refusals_recommend_is_followable() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let committed_state = tempfile::tempdir().unwrap();
let rebuild_state = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), committed_state.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
// A new --prefix on its own cannot rebuild: the recorded journal owns it.
let mut prefix_only = config.clone();
prefix_only.prefix = "incremental-http-rebuild".into();
let message = format!("{:#}", run_index(prefix_only).unwrap_err());
assert!(message.contains("was created for root="), "{message}");
// A new --state-dir and a new --prefix together do rebuild, and leave the
// original generation and its sealed snapshot untouched.
let journal_before = fs::read(committed_state.path().join("journal.ndjson")).unwrap();
let snapshots_before = snapshot_names(committed_state.path());
let mut rebuild = cfg(corpus.path(), rebuild_state.path(), &endpoint.url, false);
rebuild.prefix = "incremental-http-rebuild".into();
let (code, summary) = run_index_report(rebuild).unwrap();
assert_eq!(code, 0);
assert_eq!(summary.unwrap()["generation"], 1);
assert_eq!(journal_events(rebuild_state.path(), "sync_commit"), 1);
assert_eq!(
fs::read(committed_state.path().join("journal.ndjson")).unwrap(),
journal_before,
"an isolated rebuild must not touch the original journal"
);
assert_eq!(
snapshot_names(committed_state.path()),
snapshots_before,
"an isolated rebuild must not touch the original sealed snapshots"
);
}
/// The same refusal on a crashed generation, where the journal additionally
/// holds the only record of the pending transaction and its sealed source.
#[test]
fn fresh_cannot_destroy_a_pending_generation() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("a.csv");
fs::write(&source, "id,value\n1,committed\n").unwrap();
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
fs::write(&source, "id,value\n1,sealed-pending\n2,sealed-second\n").unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
assert!(run_index(config.clone()).is_err());
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let journal_path = state_dir.path().join("journal.ndjson");
let journal_before = fs::read(&journal_path).unwrap();
let snapshots_before = snapshot_names(state_dir.path());
assert_eq!(snapshots_before.len(), 2, "{snapshots_before:?}");
let requests_before = endpoint.state.lock().unwrap().requests.len();
config.fresh = true;
let error = run_index(config.clone()).unwrap_err();
let message = format!("{error:#}");
assert!(
message.contains("`--fresh` cannot discard an uncommitted pending corpus generation"),
"the refusal must name the durable state that blocks it: {message}"
);
assert_eq!(
fs::read(&journal_path).unwrap(),
journal_before,
"a refused --fresh must not rewrite a pending journal"
);
assert_eq!(
snapshot_names(state_dir.path()),
snapshots_before,
"a refused --fresh must not delete the pending sealed source"
);
let attempted = endpoint.state.lock().unwrap().requests[requests_before..].to_vec();
assert_eq!(
attempted,
[
("GET".to_owned(), "/".to_owned()),
("GET".to_owned(), "/_cat/allocation?format=json".to_owned(),),
],
"a refused --fresh may perform only the readiness GET and the read-only flood-stage probe"
);
// The pending transaction is still resumable from its sealed source.
config.fresh = false;
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
let rendered = serde_json::to_string(&endpoint.data_docs()).unwrap();
assert!(rendered.contains("sealed-pending"), "{rendered}");
assert!(rendered.contains("sealed-second"), "{rendered}");
}
/// Two runs started in the same process and the same UTC second must not share
/// a `run_id`.
///
/// The generation catalog keys its managed documents on `run_id`, so a
/// collision makes the second run read the first run's documents back as its
/// own generation. `xerj brain` calls `run_index_report` more than once per
/// process, so this is reachable without any concurrency.
#[test]
fn same_second_runs_in_one_process_do_not_share_a_generation_identity() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let first_state = tempfile::tempdir().unwrap();
let second_state = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
assert_eq!(
run_index(cfg(corpus.path(), first_state.path(), &endpoint.url, false)).unwrap(),
0
);
let second = run_index_report(cfg(
corpus.path(),
second_state.path(),
&endpoint.url,
false,
));
let (code, summary) = second.expect("a second run in the same second must commit");
assert_eq!(code, 0);
assert_eq!(summary.unwrap()["generation"], 1);
let run_id = |state_dir: &Path| -> String {
fs::read_to_string(state_dir.join("journal.ndjson"))
.unwrap()
.lines()
.filter_map(|line| serde_json::from_str::<Value>(line).ok())
.find_map(|event| {
event
.get("run_id")
.and_then(Value::as_str)
.map(str::to_owned)
})
.expect("journal records a run_id")
};
assert_ne!(run_id(first_state.path()), run_id(second_state.path()));
}
/// An upsert must clear the desired content identity as well as the committed
/// one before it replays.
///
/// A byte-identical retry of the same prepared stream is idempotent on its own,
/// because `ids::doc_id` is derived from dataset slug, content id and locator.
/// The guard earns its keep when a prior aborted attempt left documents under
/// the desired `ax_file` whose ids the current stream does not overwrite — an
/// earlier generation of the same content under a different dataset
/// assignment, or a partially rolled-back attempt. This test models that
/// survivor directly, because the exact record validation at the commit
/// barrier is what turns such a leftover into a hard, unrecoverable failure.
#[test]
fn upsert_clears_a_survivor_under_the_desired_content_identity() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("a.csv");
let original = "id,value\n1,alpha\n";
fs::write(&source, original).unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let (index, content_id) = {
let locked = endpoint.state.lock().unwrap();
let ((index, _), doc) = locked
.docs
.iter()
.find(|((index, _), _)| index != catalog::CATALOG_INDEX)
.expect("the first generation published a data document");
(
index.clone(),
doc.get("ax_file")
.and_then(Value::as_str)
.expect("data documents carry ax_file")
.to_owned(),
)
};
// Replace the content, then restore it, so the third generation upserts the
// original content identity again.
fs::write(&source, "id,value\n2,bravo\n").unwrap();
assert_eq!(run_index(config.clone()).unwrap(), 0);
fs::write(&source, original).unwrap();
// A document from an aborted earlier attempt at exactly this content, under
// an id the prepared stream will not overwrite.
endpoint.state.lock().unwrap().docs.insert(
(index.clone(), "aborted-attempt-survivor".to_owned()),
json!({
"id": 1,
"value": "alpha",
"ax_file": content_id,
"ax_path": "a.csv",
"ax_locator": "aborted-attempt-survivor"
}),
);
assert_eq!(run_index(config).unwrap(), 0);
assert!(
!endpoint
.state
.lock()
.unwrap()
.docs
.contains_key(&(index, "aborted-attempt-survivor".to_owned())),
"the upsert must delete the desired content identity before replaying it"
);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 3);
}
/// `--dry-run` used to be consulted only *after* the generated branches had
/// already published and committed, so on any already-generated state
/// directory the flag was accepted, ignored, and the destination mutated.
#[test]
fn dry_run_on_a_generated_state_directory_publishes_nothing() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let committed_docs = endpoint.data_docs();
let bulks = endpoint.data_bulk_requests();
let journal_before = fs::read(state_dir.path().join("journal.ndjson")).unwrap();
// A real change, so the dry run has something it would publish.
fs::write(
corpus.path().join("a.csv"),
"id,value\n1,CHANGED\n2,added\n",
)
.unwrap();
let mut dry = config.clone();
dry.dry_run = true;
let (code, summary) = run_index_report(dry.clone()).unwrap();
assert_eq!(code, 0);
assert!(
summary.is_none(),
"--dry-run must not report a committed run summary"
);
assert_eq!(
endpoint.data_bulk_requests(),
bulks,
"--dry-run issued a data bulk"
);
assert_eq!(
endpoint.data_docs(),
committed_docs,
"--dry-run mutated the destination"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(
fs::read(state_dir.path().join("journal.ndjson")).unwrap(),
journal_before,
"--dry-run appended to the durable journal"
);
// An unchanged folder takes the no-op arm of the same branch: still inert.
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
assert_eq!(run_index(dry).unwrap(), 0);
assert_eq!(endpoint.data_bulk_requests(), bulks);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// And the real run that follows still converges on the change.
fs::write(
corpus.path().join("a.csv"),
"id,value\n1,CHANGED\n2,added\n",
)
.unwrap();
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(endpoint.data_docs().len(), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}
/// A junk/skipped file has no `plan.files` entry by construction, so demanding
/// a prepared artifact for every inventory entry failed the whole run on any
/// folder containing one unreadable, empty or unrecognised file.
#[test]
fn a_junk_file_is_recorded_and_never_fatal() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
fs::write(corpus.path().join("empty.csv"), "").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config.clone()).unwrap();
let summary = summary.expect("a corpus with one junk file still commits a generation");
assert_eq!(
code, 3,
"a completed run that skipped a file exits 3, not 0 (cli.rs EXIT CODES)"
);
assert_eq!(summary["generation"], 1);
assert_eq!(summary["files_junk"], 1);
assert_eq!(summary["records_total"], 2);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let junk_docs: Vec<Value> = endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("doc_kind").and_then(Value::as_str) == Some("file")
&& doc.get("path").and_then(Value::as_str) == Some("empty.csv")
})
.map(|(_, doc)| doc.clone())
.collect();
assert_eq!(
junk_docs.len(),
1,
"the skipped file must still get exactly one catalog entry"
);
// The generation reconciles normally afterwards, junk file and all.
fs::write(corpus.path().join("b.csv"), "id,value\n3,gamma\n").unwrap();
assert_eq!(run_index(config).unwrap(), 3);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv", "b.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}
#[test]
fn issue_283_inferred_float_manifest_digest_replays_on_unchanged_rerun() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
// 2,209 bytes / 9 records produces the exact inferred f64 which used to
// change from 245.44444444444446 to 245.44444444444449 on journal replay.
let mut csv = String::from("body\n");
for len in [245, 245, 245, 245, 245, 245, 245, 245, 249] {
csv.push_str(&"x".repeat(len));
csv.push('\n');
}
fs::write(corpus.path().join("data.csv"), csv).unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (first_code, first_summary) = run_index_report(config.clone()).unwrap();
assert_eq!(first_code, 0);
assert_eq!(first_summary.unwrap()["generation"], 1);
let (second_code, second_summary) = run_index_report(config).unwrap();
assert_eq!(second_code, 0);
assert_eq!(second_summary.unwrap()["generation"], 1);
}
#[test]
fn issue_283_junk_bearing_generation_reconciles_a_shrinking_file_set() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
// Preserve the inferred f64 from the original failure while exercising the
// user's actual second-run shape: generation one contains junk, then a
// tracked file disappears without any ignore-policy change.
let mut csv = String::from("body\n");
for len in [245, 245, 245, 245, 245, 245, 245, 245, 249] {
csv.push_str(&"x".repeat(len));
csv.push('\n');
}
fs::write(corpus.path().join("data.csv"), csv).unwrap();
fs::write(
corpus.path().join("remove.md"),
"# Temporary report\n\nThis file is removed before generation two.\n",
)
.unwrap();
fs::write(corpus.path().join("empty.csv"), "").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (first_code, first_summary) = run_index_report(config.clone()).unwrap();
assert_eq!(first_code, 3, "generation one completes with recorded junk");
let first_summary = first_summary.unwrap();
assert_eq!(first_summary["generation"], 1);
assert_eq!(first_summary["files_junk"], 1);
assert_eq!(paths(&endpoint.data_docs()), ["data.csv", "remove.md"]);
fs::remove_file(corpus.path().join("remove.md")).unwrap();
let (second_code, second_summary) = run_index_report(config).unwrap();
assert_eq!(second_code, 3, "the smaller tree still records empty.csv");
let second_summary = second_summary.unwrap();
assert_eq!(second_summary["generation"], 2);
assert_eq!(second_summary["files_junk"], 1);
assert_eq!(paths(&endpoint.data_docs()), ["data.csv"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}
/// Junk *records* were fatal on the generated path
/// (`durable preparation of X produced N junk records`) while the legacy path
/// merely accumulated them — directly contradicting the documented contract
/// "3 completed-with-junk (junk recorded, never fatal)".
#[test]
fn a_junk_record_is_counted_into_the_generation_and_never_fatal() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let mut log = String::from("!!! rotated by logrotate, not a log line\n");
for n in 1..=40 {
log.push_str(&format!(
"10.0.0.{n} - - [09/Aug/2026:10:00:{n:02} +0000] \"GET /p/{n} HTTP/1.1\" 200 {n}\n"
));
}
fs::write(corpus.path().join("access.log"), log).unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
let summary = summary.expect("a corpus with one junk record still commits a generation");
assert_eq!(code, 3, "completed-with-junk exits 3");
assert_eq!(summary["generation"], 1);
assert_eq!(summary["files_junk"], 0);
assert_eq!(
summary["junk_records_total"], 1,
"the unparseable line must be counted, not hardcoded to zero"
);
assert_eq!(summary["records_total"], 40);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// The same count reaches the per-file catalog document.
let file_doc = endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.find(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("doc_kind").and_then(Value::as_str) == Some("file")
&& doc.get("path").and_then(Value::as_str) == Some("access.log")
})
.map(|(_, doc)| doc.clone())
.expect("the indexed file has a catalog document");
assert_eq!(file_doc["junk"], 1);
assert_eq!(file_doc["records"], 40);
}
/// #360: one file can feed several datasets — a SQL dump is one file and N
/// tables — but the generated executor sealed a single flat record total per
/// *file* and reconciled that whole-file total against *each* dataset's
/// read-back. Every ordinary multi-table dump therefore aborted at catalog
/// publication with `exit=1` on a run whose data was already complete.
#[test]
fn a_multi_table_dump_reconciles_each_dataset_against_its_own_sealed_count() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("dump.sql"),
"CREATE TABLE `users` (`id` int, `name` varchar(64));\n\
INSERT INTO `users` VALUES (1,'ann'),(2,'bob');\n\
CREATE TABLE `orders` (`id` int, `total` int);\n\
INSERT INTO `orders` VALUES (10,100),(11,200);\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
let summary = summary.expect("a two-table dump must commit its generation");
assert_eq!(code, 0, "a valid multi-table dump is not a failed run");
assert_eq!(summary["generation"], 1);
assert_eq!(summary["records_total"], 4);
assert_eq!(summary["junk_records_total"], 0);
assert_eq!(endpoint.data_docs().len(), 4);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// Both tables became datasets, and each one's catalog document reports the
// records it actually holds — not the whole file's total.
let dataset_docs: BTreeMap<String, Value> = endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("doc_kind").and_then(Value::as_str) == Some("dataset")
})
.map(|((_, id), doc)| (id.clone(), doc.clone()))
.collect();
assert_eq!(
dataset_docs.len(),
2,
"one dataset per table: {dataset_docs:?}"
);
for (id, doc) in &dataset_docs {
assert_eq!(doc["record_count"], 2, "dataset {id} holds two rows");
}
// Each table's rows really are in their own index, under their own slug.
for doc in endpoint.data_docs() {
let slug = doc["ax_dataset"].as_str().unwrap().to_owned();
assert!(
dataset_docs.contains_key(&format!("ds:incremental-http:{slug}")),
"record published under an uncatalogued dataset {slug}"
);
}
}
/// The other everyday shape of #360: a real database file. One `.sqlite` is
/// one content group and one dataset per table, exactly like the dump above.
#[test]
fn a_two_table_sqlite_file_commits_its_generation() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let connection = rusqlite::Connection::open(corpus.path().join("shop.sqlite")).unwrap();
connection
.execute_batch(
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);\n\
INSERT INTO users VALUES (1,'ann'),(2,'bob');\n\
CREATE TABLE orders (id INTEGER PRIMARY KEY, total INTEGER);\n\
INSERT INTO orders VALUES (10,100),(11,200);",
)
.unwrap();
drop(connection);
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
let summary = summary.expect("a two-table database must commit its generation");
assert_eq!(code, 0);
assert_eq!(summary["records_total"], 4);
assert_eq!(endpoint.data_docs().len(), 4);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
/// The shape #360 was actually reported against, and the one that reaches the
/// fan-out through the *sniffer* rather than through an extension: ordinary
/// markdown prose with SQL blocks in it, as in `unum-cloud/usearch`'s
/// `sqlite/README.md`. The SQL blocks contain `INSERT INTO … VALUES`, which
/// is what `sniff` routes to the `sqldump` family (#1984: `CREATE TABLE`
/// alone is schema, not a dump), so a README documenting two tables is one
/// content group feeding two datasets — no dump and no database file involved.
/// Worth pinning separately: the reporter's corpus contained neither.
#[test]
fn a_readme_documenting_two_tables_is_not_a_fatal_condition() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("README.md"),
"# vectors\n\nStore them like this:\n\n```sql\n\
CREATE TABLE t1 (id INTEGER PRIMARY KEY, v JSON NOT NULL);\n\
INSERT INTO t1 (id, v) VALUES (10, '[1.0]'), (11, '[2.0]');\n\
CREATE TABLE t2 (id INTEGER PRIMARY KEY, v JSON NOT NULL);\n\
INSERT INTO t2 (id, v) VALUES (20, '[1.0]'), (21, '[2.0]');\n\
```\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
let summary = summary.expect("a README describing two tables still commits");
assert_eq!(code, 0, "a valid README is not a failed run");
assert_eq!(summary["records_total"], 4);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let mut counts: Vec<(String, u64)> = endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("doc_kind").and_then(Value::as_str) == Some("dataset")
})
.map(|(_, doc)| {
(
doc["slug"].as_str().unwrap_or_default().to_owned(),
doc["record_count"]
.as_u64()
.expect("a dataset catalog document reports its record count"),
)
})
.collect();
counts.sort();
assert_eq!(
counts.len(),
2,
"one dataset per declared table: {counts:?}"
);
assert!(
counts.iter().all(|(_, records)| *records == 2),
"each dataset carries only its own rows: {counts:?}"
);
}
/// The second abort #360 reported — the one that killed both of the corpora
/// the reporter pointed autoindex at, on one node, one after the other.
///
/// `content::full_digest` derives a file's identity from its CONTENT alone, and
/// `catalog::CATALOG_INDEX` is a single global index that no `--prefix`
/// namespaces. Two unrelated checkouts that happen to share one byte-identical
/// file — an Apache-2.0 `LICENSE`, a `.gitignore`, an empty `__init__.py` —
/// therefore share that file's catalog document IDs. A corpus holding the
/// content twice publishes a canonical document plus one alias document; a
/// corpus holding it once republishes only the canonical, so the *other*
/// corpus's alias document survives under its own `run_id`. The generation-wide
/// barrier counted every catalog document carrying that `file_key`, across every
/// run on the node, against `1 + aliases.len()` for the group in front of it —
/// so the second run aborted with `exit=1` while its data was complete.
#[test]
fn a_file_shared_with_another_corpus_on_the_same_node_is_not_a_fatal_condition() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let shared = "id,value\n1,apache-2.0\n2,copied-verbatim\n";
let first = tempfile::tempdir().unwrap();
let first_state = tempfile::tempdir().unwrap();
fs::create_dir(first.path().join("docs")).unwrap();
fs::write(first.path().join("LICENSE.csv"), shared).unwrap();
fs::write(first.path().join("docs").join("LICENSE.csv"), shared).unwrap();
let endpoint = HttpEndpoint::start();
let (code, summary) =
run_index_report(cfg(first.path(), first_state.path(), &endpoint.url, false))
.expect("the first corpus indexes");
let summary = summary.expect("the first corpus commits its generation");
assert_eq!(code, 0);
assert_eq!(
summary["duplicate_files"], 1,
"the first corpus must publish one alias document, or nothing is left \
behind for the second run to trip over: {summary}"
);
// A second, unrelated corpus on the same node, isolated exactly the way the
// CLI's refusals recommend: its own --state-dir and its own --prefix. Its
// one shared file is a group with no aliases at all.
let second = tempfile::tempdir().unwrap();
let second_state = tempfile::tempdir().unwrap();
fs::write(second.path().join("LICENSE.csv"), shared).unwrap();
fs::write(second.path().join("own.csv"), "id,value\n7,second\n").unwrap();
let mut config = cfg(second.path(), second_state.path(), &endpoint.url, false);
config.prefix = "incremental-http-second".into();
let (code, summary) = run_index_report(config)
.expect("a file shared with another corpus on the same node is not a fatal condition");
let summary = summary.expect("the second corpus commits its generation");
assert_eq!(code, 0, "the second corpus is a clean run: {summary}");
assert_eq!(summary["generation"], 1);
assert_eq!(summary["records_total"], 3);
assert_eq!(summary["duplicate_files"], 0);
assert_eq!(journal_events(second_state.path(), "sync_commit"), 1);
// And the first corpus's alias document is still there — the barrier stopped
// counting it, it was not swept away from under the other corpus's map.
let aliases = endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("status").and_then(Value::as_str) == Some("duplicate")
})
.count();
assert_eq!(
aliases, 1,
"the other corpus's alias document must survive this run untouched"
);
}
/// Where this branch and #241 meet.
///
/// The generated `--no-graph` route returns from inside `run_index_report`,
/// several hundred lines before the legacy `pr.finish` at the bottom of the
/// function. `Ticker::drop` closes an unfinished stream with `ok=false`, so a
/// generated run that merely returned would print an *aborted* terminal line
/// immediately after committing a generation successfully — a silent
/// contradiction between two changes that each looked right on its own.
///
/// This pins all three properties on the generated path: the phase-A scan is
/// narrated (it is a per-file, minutes-long phase here exactly as on the legacy
/// path), the stream closes with a truthful terminal line, and `--progress
/// none` on the same path stays completely silent.
/// #294, at the level where the loss actually happened: a mixed corpus of
/// source code and prose, over the generated (`--no-graph`) path, through the
/// real HTTP client.
///
/// Every code file was walked, sniffed as family `code`, counted — and then
/// prepared as ZERO documents, so the generation committed and the run
/// reported success while the entire code half of the corpus was missing.
/// `sync_executor` pins the extraction half of that regression; this pins what
/// a caller can actually observe: the documents that reach the endpoint carry
/// their AST fields, and the run document and terminal line carry coverage
/// counters that a wholly-junked corpus could not fake.
#[test]
fn code_files_index_with_ast_fields_and_the_run_reports_code_coverage() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let _sink_guard = crate::progress::SINK_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("alpha.rs"),
"pub struct AlphaConfig {\n pub retries: u32,\n}\n\n\
pub fn alpha_connect(cfg: &AlphaConfig) -> bool {\n cfg.retries > 0\n}\n",
)
.unwrap();
fs::write(
corpus.path().join("gamma.py"),
"def gamma_load(path):\n return open(path).read()\n\n\n\
class GammaStore:\n def __init__(self, root):\n self.root = root\n",
)
.unwrap();
fs::write(
corpus.path().join("notes.md"),
"# Fixture notes\n\nTwo source files and this prose file, so the corpus is mixed.\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.quiet = false;
config.progress = crate::progress::ProgressMode::Plain;
let buffer = Arc::new(Mutex::new(Vec::new()));
let (code, summary) = {
let _sink = crate::progress::install_test_sink(&buffer);
run_index_report(config).unwrap()
};
assert_eq!(code, 0, "nothing in this corpus is unparseable");
let summary = summary.expect("a generated run returns its committed run projection");
assert_eq!(summary["code_files"], 2, "{summary}");
assert_eq!(
summary["code_files_indexed"], 2,
"both source files must reach the index: {summary}"
);
assert_eq!(summary["code_files_junked"], 0, "{summary}");
let docs = endpoint.data_docs();
// The per-file AST document carries `defs`/`symbols`; the #500 per-symbol
// documents carry `code`/`name` but no `defs`, so filter on `defs` to keep
// the one-per-file assertions honest about the file-level document.
let mut code_docs: Vec<&Value> = docs
.iter()
.filter(|doc| doc.get("language").is_some() && doc.get("defs").is_some())
.collect();
code_docs.sort_by_key(|doc| doc["language"].as_str().unwrap_or("").to_owned());
assert_eq!(
code_docs.len(),
2,
"one AST (file-level) document per source file: {docs:?}"
);
// #500: each declaration is also promoted to its own retrievable document
// (has `code` + `name`, no `defs`). At least the Rust struct + Python class
// must each be their own symbol document, not only foldable inside a parent.
let symbol_docs: Vec<&Value> = docs
.iter()
.filter(|doc| doc.get("code").is_some() && doc.get("name").is_some())
.collect();
assert!(
symbol_docs
.iter()
.any(|d| d["name"] == "AlphaConfig" && d.get("defs").is_none()),
"the AlphaConfig struct must be its own symbol document (#500): {symbol_docs:?}"
);
assert!(
symbol_docs.iter().any(|d| d["name"] == "GammaStore"),
"the GammaStore class must be its own symbol document (#500): {symbol_docs:?}"
);
assert_eq!(code_docs[0]["language"], "python");
assert_eq!(code_docs[1]["language"], "rust");
for doc in &code_docs {
assert!(
doc["defs"].as_str().is_some_and(|defs| !defs.is_empty()),
"{doc}"
);
assert!(
doc["symbols"]
.as_array()
.is_some_and(|symbols| !symbols.is_empty()),
"{doc}"
);
}
// The title comes from the logical file name, never the content-addressed
// snapshot blob's ordinal — the other half of #294.
assert_eq!(code_docs[0]["title"], "gamma.py");
assert_eq!(code_docs[1]["title"], "alpha.rs");
let rendered = serde_json::to_string(&code_docs).unwrap();
assert!(rendered.contains("struct AlphaConfig"), "{rendered}");
assert!(rendered.contains("class GammaStore"), "{rendered}");
let stream = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
let done = stream
.lines()
.find(|line| line.starts_with("xerj-done "))
.unwrap_or_else(|| panic!("{stream}"));
assert!(
done.contains("code_files=2 code_files_indexed=2 code_files_junked=0"),
"the terminal line carries the coverage that made #294 invisible: {done}"
);
assert!(!stream.contains("warning:"), "{stream}");
}
#[test]
fn a_generated_run_narrates_its_scan_and_closes_its_own_stream() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let _sink_guard = crate::progress::SINK_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
for n in 0..4 {
fs::write(
corpus.path().join(format!("rows-{n}.csv")),
format!("id,value\n{n}0,alpha\n{n}1,beta\n"),
)
.unwrap();
}
let endpoint = HttpEndpoint::start();
// Generation 1: the initial commit, through the real progress surface.
let mut loud = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
loud.quiet = false;
loud.progress = crate::progress::ProgressMode::Plain;
loud.progress_interval = Some(std::time::Duration::from_secs(1));
let buffer = Arc::new(Mutex::new(Vec::new()));
let code = {
let _sink = crate::progress::install_test_sink(&buffer);
run_index(loud.clone()).unwrap()
};
assert_eq!(code, 0);
let stream = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
let done = stream
.lines()
.find(|line| line.starts_with("xerj-done "))
.unwrap_or_else(|| panic!("a generated run must close its own stream:\n{stream}"));
assert!(
done.contains("ok=true") && done.contains("exit=0"),
"a committed generation is not an aborted run: {done}"
);
assert!(done.contains("reason=completed"), "{done}");
// Generation 2: the reconcile branch, which runs its own phase-A scan.
fs::write(corpus.path().join("rows-0.csv"), "id,value\n00,CHANGED\n").unwrap();
let buffer = Arc::new(Mutex::new(Vec::new()));
let code = {
let _sink = crate::progress::install_test_sink(&buffer);
run_index(loud).unwrap()
};
assert_eq!(code, 0);
let stream = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
assert!(
stream
.lines()
.any(|line| line.starts_with("xerj-progress ") && line.contains("phase=scan")),
"the reconcile projection re-scans every file and must say so:\n{stream}"
);
let done = stream
.lines()
.find(|line| line.starts_with("xerj-done "))
.unwrap_or_else(|| panic!("a reconciled generation must close its own stream:\n{stream}"));
assert!(done.contains("ok=true"), "{done}");
// The other half of the contract on the same path: silence means silence.
fs::write(corpus.path().join("rows-1.csv"), "id,value\n10,CHANGED\n").unwrap();
let quiet = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(quiet.progress, crate::progress::ProgressMode::None);
let buffer = Arc::new(Mutex::new(Vec::new()));
{
let _sink = crate::progress::install_test_sink(&buffer);
assert_eq!(run_index(quiet).unwrap(), 0);
}
assert!(
buffer.lock().unwrap().is_empty(),
"--progress none asked for nothing: {}",
String::from_utf8_lossy(&buffer.lock().unwrap())
);
}
/// Two byte-identical junk files — two empty files are the everyday case —
/// made the fresh plan carry a duplicate alias for content that has no
/// `plan.files` entry, so the generation cutover failed its own
/// alias-projection invariant and the whole run aborted with exit 1 (#283's
/// reproducible sibling: junk plus duplicates diverging between the fresh plan
/// and the incremental projection). The fresh plan now projects aliases the
/// same way `reconcile_plan` does, so the folder indexes, a no-op re-run
/// confirms instead of committing a spurious generation, and a shrinking file
/// set on the junk-bearing generation reconciles.
#[test]
fn two_byte_identical_junk_files_commit_reconcile_and_survive_a_shrink() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
fs::write(corpus.path().join("e1.dat"), "").unwrap();
fs::write(corpus.path().join("e2.dat"), "").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config.clone())
.expect("a folder with two byte-identical junk files must index, not abort");
let summary = summary.expect("the junk-bearing generation still commits");
assert_eq!(code, 3, "junk is recorded, never fatal (cli.rs EXIT CODES)");
assert_eq!(summary["generation"], 1);
assert_eq!(summary["records_total"], 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// The fresh plan and the incremental projection must agree byte-for-byte
// on how junk-content aliases project, or this no-op re-run would see a
// "changed" plan and commit generation 2 over nothing.
assert_eq!(run_index(config.clone()).unwrap(), 3);
assert_eq!(
journal_events(state_dir.path(), "sync_commit"),
1,
"a no-op re-run over the junk-bearing generation confirms; it must not re-commit"
);
// The issue's headline shape: the file set shrinks on a generation that
// carries junk records.
fs::remove_file(corpus.path().join("a.csv")).unwrap();
assert_eq!(run_index(config.clone()).unwrap(), 3);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
assert!(
paths(&endpoint.data_docs()).is_empty(),
"the deleted dataset's records must not survive the reconcile"
);
// And the junk files themselves disappearing reconciles back to clean.
fs::remove_file(corpus.path().join("e1.dat")).unwrap();
fs::remove_file(corpus.path().join("e2.dat")).unwrap();
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 3);
}
/// The #283 abort itself: a durable generation record that fails its own
/// re-validation used to surface as a bare internal invariant ("desired
/// manifest digest does not match sync_begin payload", exit 1) — text the user
/// can do nothing with. However the journal got that way, the refusal must
/// name the recovery route the way the exec-config guard already does, and
/// keep the invariant attached as the cause.
#[test]
fn a_journal_that_fails_its_own_revalidation_names_the_rebuild_route() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("rows.csv");
fs::write(&source, "id,value\n1,first\n").unwrap();
fs::write(corpus.path().join("empty.csv"), "").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 3);
// Leave a durable pending generation behind: begin succeeds, the bulk is
// partially applied, the run fails before commit.
fs::write(&source, "id,value\n1,replaced\n2,second\n").unwrap();
endpoint
.state
.lock()
.unwrap()
.partially_apply_next_data_bulk = true;
assert!(run_index(config.clone()).is_err());
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// Corrupt the pending sync_begin payload in a spot only the manifest
// digest covers: the junk record's byte count. This makes the next run
// hit exactly the #283 invariant.
let journal_path = state_dir.path().join("journal.ndjson");
let tampered: String = fs::read_to_string(&journal_path)
.unwrap()
.lines()
.map(|line| {
let mut record: Value = serde_json::from_str(line).unwrap();
if record.get("kind").and_then(Value::as_str) == Some("sync_begin")
&& record
.pointer("/desired/generation")
.and_then(Value::as_u64)
== Some(2)
{
let bytes = record
.pointer_mut("/desired/plan/junk_files/0/bytes")
.expect("the junk-bearing desired manifest records its junk file");
*bytes = Value::from(bytes.as_u64().unwrap() + 1);
}
let mut line = serde_json::to_string(&record).unwrap();
line.push('\n');
line
})
.collect();
fs::write(&journal_path, tampered).unwrap();
let error = run_index(config).expect_err("a tampered generation journal must refuse to run");
let rendered = format!("{error:#}");
assert!(
rendered.contains("desired manifest digest does not match sync_begin payload"),
"the internal invariant must stay attached as the cause: {rendered}"
);
assert!(
rendered.contains("Rebuild with a new --state-dir and a new --prefix"),
"the refusal must name the recovery route, not just the invariant: {rendered}"
);
}
/// #589: `sweep_excluded_groups` actually deletes an excluded file's published
/// documents (by `ax_file`, across the plan's dataset indices) and its catalog
/// document (by `path`), while leaving every OTHER file's documents intact.
/// Exercised over the real mock HTTP backend so the delete-by-query truly runs.
#[test]
fn sweep_excluded_groups_deletes_only_the_excluded_files_documents() {
let ep = HttpEndpoint::start();
// Seed two files' records in one dataset index, plus their catalog docs.
{
let mut st = ep.state.lock().unwrap();
let ds = "incremental-http-ds".to_string();
st.docs.insert(
(ds.clone(), "d1".into()),
json!({"ax_file": "key-excluded", "body": "secret"}),
);
st.docs.insert(
(ds.clone(), "d2".into()),
json!({"ax_file": "key-kept", "body": "kept"}),
);
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file:excluded".into()),
json!({"doc_kind": "file", "prefix": "ax", "path": "secret.csv"}),
);
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file:kept".into()),
json!({"doc_kind": "file", "prefix": "ax", "path": "kept.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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
let excluded = vec![InventoryDeltaEntry {
file_key: "key-excluded".into(),
path: "secret.csv".into(),
}];
sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
let st = ep.state.lock().unwrap();
// The excluded file's dataset record is gone.
assert!(
!st.docs
.values()
.any(|d| d.get("ax_file").and_then(Value::as_str) == Some("key-excluded")),
"#589: the excluded file's indexed records must be swept"
);
// Its catalog document is gone.
assert!(
!st.docs
.iter()
.any(|((idx, _), d)| idx == catalog::CATALOG_INDEX
&& d.get("path").and_then(Value::as_str) == Some("secret.csv")),
"#589: the excluded file's catalog document must be swept"
);
// Every other file is untouched (no over-deletion).
assert!(
st.docs
.values()
.any(|d| d.get("ax_file").and_then(Value::as_str) == Some("key-kept")),
"#589: a different file's records must NOT be swept"
);
assert!(
st.docs
.iter()
.any(|((idx, _), d)| idx == catalog::CATALOG_INDEX
&& d.get("path").and_then(Value::as_str) == Some("kept.csv")),
"#589: a different file's catalog document must NOT be swept"
);
}
/// #694: `sweep_excluded_groups` also soft-invalidates the graph edges an
/// excluded file taught (reusing the replacement hook `invalidate_prior_edges`),
/// while leaving a different file's edges live. The bi-temporal record survives
/// (`as_of` time travel), so the edge is stamped `invalid_at`, not deleted.
/// Fail-before: with the `invalidate_prior_edges` call reverted, the excluded
/// file's edge carries no `invalid_at` and stays searchable.
#[test]
fn sweep_excluded_groups_invalidates_the_excluded_files_edges() {
let ep = HttpEndpoint::start();
let edges = ".xerj-memory-testbrain-edges";
// Two live edges (no `invalid_at`): one taught by the file about to be
// excluded, one by a file that stays.
{
let mut st = ep.state.lock().unwrap();
// The mock's `_bulk` guard asserts mappings were seen first (ingest
// ordering); the invalidation re-index is a bulk, so satisfy it — this
// test exercises edge invalidation, not the mapping-ordering invariant.
st.saw_dataset_mapping_update = true;
st.saw_catalog_mapping_update = true;
st.docs.insert(
(edges.to_string(), "edge-secret".to_string()),
json!({"src_file": "secret.csv", "kind": "samedir", "dst_file": "other.csv"}),
);
st.docs.insert(
(edges.to_string(), "edge-kept".to_string()),
json!({"src_file": "kept.csv", "kind": "samedir", "dst_file": "other.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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
let excluded = vec![InventoryDeltaEntry {
file_key: "key-excluded".into(),
path: "secret.csv".into(),
}];
let now_ms = 1_724_500_000_000i64;
sweep_excluded_groups(&es, "ax", &plan, &excluded, Some(edges), now_ms).expect("sweep");
let st = ep.state.lock().unwrap();
// The excluded file's edge is soft-invalidated (present, stamped invalid_at).
let secret_edge = st
.docs
.get(&(edges.to_string(), "edge-secret".to_string()))
.expect("excluded file's edge is soft-invalidated, not deleted");
assert_eq!(
secret_edge.get("invalid_at").and_then(Value::as_i64),
Some(now_ms),
"#694: the excluded file's edge must be stamped invalid_at by the sweep"
);
// A different file's edge stays live (no over-invalidation).
let kept_edge = st
.docs
.get(&(edges.to_string(), "edge-kept".to_string()))
.expect("kept file's edge present");
assert!(
kept_edge.get("invalid_at").is_none(),
"#694: a different file's edge must NOT be invalidated"
);
}
/// #693: `sweep_excluded_groups` also purges the excluded file's `file-alias:`
/// duplicate catalog docs. Those carry the DUPLICATE's own path (not the
/// canonical `entry.path`), so the `path` term alone misses them — leaving an
/// excluded file's alternate path/filename searchable. Deleting by `file_key`
/// catches them, and cannot strand a live duplicate (a group is only excluded
/// when no surviving file bears its key). Fail-before: without the `file_key`
/// delete, the alias doc (path "dup.csv") survives the canonical-path sweep.
#[test]
fn sweep_excluded_groups_purges_the_excluded_files_alias_catalog_docs() {
let ep = HttpEndpoint::start();
{
let mut st = ep.state.lock().unwrap();
// Excluded file: a canonical `file:` doc plus a byte-identical
// `file-alias:` doc under a DIFFERENT path — both carry the same
// `file_key`.
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file:excluded".into()),
json!({"doc_kind": "file", "prefix": "ax", "file_key": "key-excluded", "path": "canonical.csv"}),
);
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file-alias:ax:excluded".into()),
json!({"doc_kind": "file", "prefix": "ax", "file_key": "key-excluded", "path": "dup.csv", "status": "duplicate"}),
);
// A different file's alias (distinct file_key) must survive.
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file-alias:ax:kept".into()),
json!({"doc_kind": "file", "prefix": "ax", "file_key": "key-kept", "path": "kept-dup.csv", "status": "duplicate"}),
);
}
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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
// entry.path is the CANONICAL rel — deliberately NOT the alias's "dup.csv".
let excluded = vec![InventoryDeltaEntry {
file_key: "key-excluded".into(),
path: "canonical.csv".into(),
}];
// 6-arg signature (post-#694 edges, post-#737 prefix): this test does not
// exercise edges, so pass `None`/`0`; the corpus prefix is "ax".
sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
let st = ep.state.lock().unwrap();
// The excluded file's alias catalog doc is gone (the #693 fix).
assert!(
!st.docs
.iter()
.any(|((idx, _), d)| idx == catalog::CATALOG_INDEX
&& d.get("path").and_then(Value::as_str) == Some("dup.csv")),
"#693: the excluded file's file-alias: catalog doc must be swept"
);
// Its canonical file doc is gone too.
assert!(
!st.docs
.iter()
.any(|((idx, _), d)| idx == catalog::CATALOG_INDEX
&& d.get("file_key").and_then(Value::as_str) == Some("key-excluded")),
"#693: the excluded file's catalog docs (by file_key) must be swept"
);
// A different file's alias must NOT be swept (file_key is content-scoped).
assert!(
st.docs
.iter()
.any(|((idx, _), d)| idx == catalog::CATALOG_INDEX
&& d.get("path").and_then(Value::as_str) == Some("kept-dup.csv")),
"#693: a different file's alias doc must NOT be swept"
);
}
/// #737: the catalog sweep is scoped to THIS corpus's `prefix`. The
/// `autoindex-catalog` index is shared across corpora; a byte-identical common
/// file (LICENSE, a lockfile) indexed by a still-live SIBLING corpus shares the
/// same `file_key`/`path`, so an unscoped sweep would delete the sibling's live
/// catalog docs too. Fail-before: dropping the `prefix` filter (unscoped
/// `{term:{file_key}}`) deletes the sibling-corpus doc as well.
#[test]
fn sweep_excluded_groups_does_not_delete_a_sibling_corpus_catalog_doc() {
let ep = HttpEndpoint::start();
{
let mut st = ep.state.lock().unwrap();
// Corpus "ax" excludes a file; corpus "bx" is a live sibling holding a
// byte-identical copy (same file_key) under its own prefix-scoped id.
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file:ax:excluded".into()),
json!({"doc_kind": "file", "prefix": "ax", "file_key": "shared-key", "path": "LICENSE"}),
);
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), "file:bx:sibling".into()),
json!({"doc_kind": "file", "prefix": "bx", "file_key": "shared-key", "path": "LICENSE"}),
);
}
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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
let excluded = vec![InventoryDeltaEntry {
file_key: "shared-key".into(),
path: "LICENSE".into(),
}];
sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
let st = ep.state.lock().unwrap();
// Corpus ax's own doc is swept (same file_key/path, matching prefix).
assert!(
!st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file:ax:excluded".to_string()
)),
"#737: the excluding corpus's own catalog doc must still be swept"
);
// The live sibling corpus's byte-identical doc SURVIVES (prefix-scoped).
assert!(
st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file:bx:sibling".to_string()
)),
"#737: a sibling corpus's byte-identical catalog doc must NOT be swept"
);
}
/// #755: the scope the sweep actually relies on is `corpus_scope`, not
/// `prefix`. On a catalog upgraded from v1.0.0-rc.15..rc.67, `prefix` is
/// dynamically mapped `text`, so a `term` against it does not match the raw
/// scope value and the #737/#693 scoped deletes silently no-op — an alias doc
/// that the frozen plan does not name is then left searchable, which is exactly
/// the #439 exposure #589 exists to close. The docs seeded here carry ONLY the
/// keyword scope field, standing in for a catalog whose `prefix` cannot be
/// term-matched; the sweep must still find this corpus's docs and must still
/// leave the live sibling corpus's alone.
///
/// Fail-before: with only the `prefix`-scoped deletes (pre-#755), the `ax` alias
/// doc below survives the sweep.
#[test]
fn sweep_excluded_groups_scopes_on_the_keyword_corpus_scope_field() {
let ep = HttpEndpoint::start();
{
let mut st = ep.state.lock().unwrap();
// An alias doc the frozen plan does not name, so ONLY the term-scoped
// `file_key` delete can reach it — the `_id` sweep cannot.
st.docs.insert(
(
catalog::CATALOG_INDEX.to_string(),
"file-alias:ax:unlisted".into(),
),
json!({
"doc_kind": "file",
catalog::CORPUS_SCOPE_FIELD: "ax",
"file_key": "shared-key",
"path": "vendor/LICENSE",
"status": "duplicate",
}),
);
// A live sibling corpus's byte-identical doc under its own scope.
st.docs.insert(
(
catalog::CATALOG_INDEX.to_string(),
"file-alias:bx:sibling".into(),
),
json!({
"doc_kind": "file",
catalog::CORPUS_SCOPE_FIELD: "bx",
"file_key": "shared-key",
"path": "vendor/LICENSE",
"status": "duplicate",
}),
);
}
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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
let excluded = vec![InventoryDeltaEntry {
file_key: "shared-key".into(),
path: "vendor/LICENSE".into(),
}];
sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
let st = ep.state.lock().unwrap();
assert!(
!st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file-alias:ax:unlisted".to_string()
)),
"#755: an excluded file's catalog doc must be swept on the keyword scope \
field, not only on a `prefix` an upgraded catalog holds as text"
);
assert!(
st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file-alias:bx:sibling".to_string()
)),
"#755: the keyword scope must still bound the sweep to THIS corpus"
);
}
/// #890: the exclusion sweep must not reach ACROSS corpora on a catalog an
/// older build left with `prefix` mapped `text`.
///
/// `autoindex-catalog` is global, and on any install upgraded from
/// v1.0.0-rc.15..rc.67 `prefix` is dynamically inferred `text` (the #755 root
/// cause). A `term` query is not analyzed but the field is, so
/// `{"term": {"prefix": "ax"}}` matches the analyzed TOKENS of `ax-2`
/// (`[ax, 2]`) — and conjoined with a `path`/`file_key` a byte-identical file
/// shares, the #737 prefix-scoped `delete_by_query` deletes the SIBLING
/// corpus's live catalog documents. A delete is not recoverable.
///
/// The endpoint models exactly that: `legacy_text_catalog_fields` now governs
/// how a `term` matches on the catalog, not only which `_mapping` PUT is
/// refused. Fail-before, measured rather than reasoned: with `lib.rs` reverted
/// to the bare `delete_by_query` this replaces, the run reports
/// `Survivors: []` — sweeping corpus `ax` deleted all FOUR catalog documents,
/// both of corpus `ax-2`'s included.
///
/// The last two assertions are the other half, and they are why the fix is an
/// exact-scope delete rather than skipping the `prefix` pass on a legacy
/// catalog: corpus `ax`'s own documents — including an alias the frozen plan
/// does not name, which ONLY the term-scoped pass can reach — must still be
/// swept. Skipping the pass would leave that one searchable, which is the #439
/// exposure #589 exists to close.
#[test]
fn sweep_excluded_groups_cannot_reach_a_sibling_corpus_on_a_text_mapped_prefix() {
let ep = HttpEndpoint::start();
// The excluding corpus and a sibling whose prefix shares its first token.
let ax_main = catalog::file_id("ax", "shared-key");
let ax_alias = "file-alias:ax:unlisted".to_string();
let sibling_main = catalog::file_id("ax-2", "shared-key");
let sibling_alias = "file-alias:ax-2:sibling".to_string();
{
let mut st = ep.state.lock().unwrap();
// A catalog written by rc.15..rc.67: `prefix` is text, and no document
// carries the keyword `corpus_scope` field, which no such release wrote.
st.legacy_text_catalog_fields = vec!["prefix".into()];
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), ax_main.clone()),
json!({"doc_kind": "file", "prefix": "ax", "file_key": "shared-key", "path": "LICENSE"}),
);
// An `ax` alias the FROZEN plan does not name, so the by-`_id` sweep
// (#739) cannot reach it — only the term-scoped `file_key` pass can.
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), ax_alias.clone()),
json!({
"doc_kind": "file",
"prefix": "ax",
"file_key": "shared-key",
"path": "vendor/LICENSE",
"status": "duplicate",
}),
);
// The live sibling corpus's byte-identical copies.
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), sibling_main.clone()),
json!({"doc_kind": "file", "prefix": "ax-2", "file_key": "shared-key", "path": "LICENSE"}),
);
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), sibling_alias.clone()),
json!({
"doc_kind": "file",
"prefix": "ax-2",
"file_key": "shared-key",
"path": "sibling/LICENSE",
"status": "duplicate",
}),
);
}
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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
let excluded = vec![InventoryDeltaEntry {
file_key: "shared-key".into(),
path: "LICENSE".into(),
}];
sweep_excluded_groups(&es, "ax", &plan, &excluded, None, 0).expect("sweep");
// Read the four verdicts out and RELEASE the endpoint's lock before
// asserting: an assert that fires while holding it poisons the mutex the
// server thread also takes, and the resulting panic-in-a-destructor aborts
// the whole test binary instead of failing this one test. This test is
// written to fail on unfixed code, so its failure mode has to be clean.
let present = |id: &String| {
ep.state
.lock()
.unwrap()
.docs
.contains_key(&(catalog::CATALOG_INDEX.to_string(), id.clone()))
};
let (sibling_main_kept, sibling_alias_kept) = (present(&sibling_main), present(&sibling_alias));
let (ax_main_swept, ax_alias_swept) = (!present(&ax_main), !present(&ax_alias));
// Every message carries the full survivor set, so ONE failing run reports
// the whole before-picture rather than only the first verdict that broke.
let survivors = {
let mut ids: Vec<String> = ep
.state
.lock()
.unwrap()
.docs
.keys()
.filter(|(index, _)| index == catalog::CATALOG_INDEX)
.map(|(_, id)| id.clone())
.collect();
ids.sort();
format!("{ids:?}")
};
assert!(
sibling_main_kept,
"#890: a sibling corpus whose prefix shares an analyzed token must keep its \
catalog document — a text-mapped `prefix` is not a scope. Survivors: {survivors}"
);
assert!(
sibling_alias_kept,
"#890: the same holds for the sibling's alias doc, reached by `file_key`. \
Survivors: {survivors}"
);
assert!(
ax_main_swept,
"#890: the excluding corpus's own document must still be swept. \
Survivors: {survivors}"
);
assert!(
ax_alias_swept,
"#890: …including an alias the frozen plan does not name, which only the \
term-scoped pass reaches — the fix is an exact scope, not a skipped pass. \
Survivors: {survivors}"
);
}
/// #890 (review follow-up): the scoped sweep's page walk must end on the
/// candidate set it actually has, not on a short page it may never be sent.
///
/// `delete_catalog_docs_scoped` reads `SCOPED_SWEEP_PAGE` hits at a time for at
/// most `SCOPED_SWEEP_MAX_PAGES` pages. Its first version ended the walk ONLY
/// on a short page, so a scope conjunction matching exactly
/// `SCOPED_SWEEP_MAX_PAGES * SCOPED_SWEEP_PAGE` documents — every page full,
/// the last one included — fell out of the loop and returned the "refusing to
/// report a partial removal" error while `ids` already held the complete,
/// re-checked set: a run aborted on an answer the function was holding. That
/// off-by-one is unreachable from the four-document fixture above, which is why
/// this walks the boundary the way
/// `the_alias_sweep_partitions_its_paths_exactly_across_the_chunk_boundary`
/// walks `ALIAS_SWEEP_CHUNK`: 0 / 1 / PAGE-1 / PAGE / PAGE+1 / MAX*PAGE, plus
/// the one candidate over the cap, which must still refuse AND still delete
/// nothing.
///
/// Every case includes one sibling-corpus document, and it sorts LAST, so the
/// per-hit re-check is proven on the final page of a multi-page walk rather
/// than only inside the first page. `prefix` is legacy `text` here for the
/// reason it is in the test above: that is what makes the sibling a candidate
/// the server returns and the client has to drop.
#[test]
fn the_scoped_catalog_sweep_pages_to_the_exact_end_of_its_candidate_set() {
let ep = HttpEndpoint::start();
let es = Es::with_bulk_timeout(&ep.url, None, 30).expect("es client");
// Sorts after every `file-alias:` id below (`-` < `:`), so it is the last
// hit of the last page.
let sibling = "file:ax-2:shared-key".to_string();
let cap = SCOPED_SWEEP_MAX_PAGES * SCOPED_SWEEP_PAGE;
for candidates in [
0,
1,
SCOPED_SWEEP_PAGE - 1,
SCOPED_SWEEP_PAGE,
SCOPED_SWEEP_PAGE + 1,
cap,
cap + 1,
] {
// `candidates` counts what the QUERY returns. One of them is the
// sibling; the rest are this corpus's own documents, all of which the
// sweep must remove.
let own = candidates.saturating_sub(1);
{
let mut st = ep.state.lock().unwrap();
st.docs.clear();
st.legacy_text_catalog_fields = vec!["prefix".into()];
for n in 0..own {
st.docs.insert(
(
catalog::CATALOG_INDEX.to_string(),
format!("file-alias:ax:{n:06}"),
),
json!({"doc_kind": "file", "prefix": "ax", "path": "LICENSE"}),
);
}
if candidates > 0 {
st.docs.insert(
(catalog::CATALOG_INDEX.to_string(), sibling.clone()),
json!({"doc_kind": "file", "prefix": "ax-2", "path": "LICENSE"}),
);
}
}
let swept = delete_catalog_docs_scoped(&es, &[("prefix", "ax"), ("path", "LICENSE")]);
// Read every verdict out and RELEASE the endpoint's lock before
// asserting, for the reason the test above gives: an assert that fires
// while holding it poisons the mutex the server thread also takes.
let (survivors, sibling_kept) = {
let st = ep.state.lock().unwrap();
let survivors = st
.docs
.keys()
.filter(|(index, _)| index == catalog::CATALOG_INDEX)
.count();
let sibling_kept = st
.docs
.contains_key(&(catalog::CATALOG_INDEX.to_string(), sibling.clone()));
(survivors, sibling_kept)
};
if candidates > cap {
assert!(
swept.is_err(),
"#890: past {cap} candidates the sweep must refuse rather than report a \
partial removal ({candidates} candidates)"
);
assert_eq!(
survivors, candidates,
"#890: a refusal must delete NOTHING — a partial sweep reported as a failure \
is still a partial sweep ({candidates} candidates)"
);
continue;
}
swept.unwrap_or_else(|err| {
panic!("#890: {candidates} candidates is inside the page budget, not past it: {err}")
});
assert_eq!(
survivors,
usize::from(candidates > 0),
"#890: every one of this corpus's {own} documents must be swept, across page \
boundaries as much as inside one page ({candidates} candidates)"
);
assert!(
sibling_kept || candidates == 0,
"#890: the sibling corpus's document must survive the re-check on whichever page \
it lands ({candidates} candidates)"
);
}
}
/// #905 (data loss), end to end through the real finalize: a run over one
/// corpus must not delete a SIBLING corpus's duplicate-alias catalog document.
///
/// `autoindex-catalog` is global across every corpus on the node and `path` is
/// a logical path INSIDE a corpus, so the duplicate-alias sweep's
/// `{status: duplicate} + {path: [...]}` query reaches every corpus that has a
/// duplicate at the same relative path. Two checkouts that each carry
/// `LICENSE.csv` and `docs/LICENSE.csv` is the ordinary shape of that, not a
/// contrived one — and unlike #890 it needs no legacy mapping: this fixture's
/// catalog is the current `keyword` one.
///
/// The corpus next door is the one in
/// `a_file_shared_with_another_corpus_on_the_same_node_is_not_a_fatal_condition`
/// with ONE line added: its own duplicate at the same relative path. That is
/// what puts `docs/LICENSE.csv` into the second run's alias-sweep list and
/// makes the sweep reach next door.
///
/// Both runs take the DEFAULT graph path (`no_graph: false`), because that is
/// where this sweep lives: a `--no-graph` run returns from the generated path
/// several thousand lines earlier and retires stale catalog documents by their
/// prefix-scoped `_id` instead (`generation_catalog::managed_non_run_ids`),
/// which was never unscoped. That is the honest blast radius of #905.
///
/// FAIL-BEFORE, measured: with the body of `sweep_duplicate_aliases` replaced
/// by the `es.delete_by_query(CATALOG_INDEX, &alias_sweep_query(paths))` this
/// PR removes — i.e. the code as it stands on `main` — the run ends with ONE
/// alias document instead of two: the second corpus swept the first corpus's
/// away and then wrote its own.
#[test]
fn this_corpus_finalize_leaves_a_sibling_corpus_alias_document_alone() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let shared = "id,value\n1,apache-2.0\n2,copied-verbatim\n";
let endpoint = HttpEndpoint::start();
let first = tempfile::tempdir().unwrap();
let first_state = tempfile::tempdir().unwrap();
fs::create_dir(first.path().join("docs")).unwrap();
fs::write(first.path().join("LICENSE.csv"), shared).unwrap();
fs::write(first.path().join("docs").join("LICENSE.csv"), shared).unwrap();
let mut config = cfg(first.path(), first_state.path(), &endpoint.url, false);
config.no_graph = false;
config.brain = Some("testbrain".into());
let (code, summary) = run_index_report(config).expect("the first corpus indexes");
let summary = summary.expect("the first corpus reports");
assert_eq!(code, 0);
assert_eq!(
summary["duplicate_files"], 1,
"the first corpus must publish one alias document, or there is nothing \
next door for the second run to reach: {summary}"
);
// The second corpus: its own state dir and its own --prefix, exactly the
// isolation the CLI's refusals recommend — and its own duplicate at the
// SAME relative path, which is what the first corpus's alias sits at.
let second = tempfile::tempdir().unwrap();
let second_state = tempfile::tempdir().unwrap();
fs::create_dir(second.path().join("docs")).unwrap();
fs::write(second.path().join("LICENSE.csv"), shared).unwrap();
fs::write(second.path().join("docs").join("LICENSE.csv"), shared).unwrap();
let mut config = cfg(second.path(), second_state.path(), &endpoint.url, false);
config.prefix = "incremental-http-second".into();
config.no_graph = false;
config.brain = Some("testbrain-second".into());
let (code, summary) = run_index_report(config).expect("the second corpus indexes");
let summary = summary.expect("the second corpus reports");
assert_eq!(code, 0, "the second corpus is a clean run: {summary}");
assert_eq!(
summary["duplicate_files"], 1,
"the second corpus publishes its own alias at the same path: {summary}"
);
let aliases: Vec<(String, String)> = {
let st = endpoint.state.lock().unwrap();
let mut aliases: Vec<(String, String)> = st
.docs
.iter()
.filter(|((index, _), doc)| {
index == catalog::CATALOG_INDEX
&& doc.get("status").and_then(Value::as_str) == Some("duplicate")
})
.map(|((_, id), doc)| {
(
id.clone(),
doc.get(catalog::CORPUS_SCOPE_FIELD)
.and_then(Value::as_str)
.unwrap_or("<no corpus_scope>")
.to_string(),
)
})
.collect();
aliases.sort();
aliases
};
// Sorted by corpus, not by `_id`: the two ids share a body and differ only
// in their prefix, where `-` sorts before `:`.
let mut scopes: Vec<&str> = aliases.iter().map(|(_, scope)| scope.as_str()).collect();
scopes.sort_unstable();
assert_eq!(
scopes,
vec!["incremental-http", "incremental-http-second"],
"#905: both corpora must keep their own duplicate-alias document — the \
second corpus's sweep is scoped to itself, not to the logical path. \
Aliases: {aliases:?}"
);
}
/// #905, at the sweep itself: every identity scheme the catalog can still hold.
///
/// The end-to-end test above proves the wiring on documents this build wrote.
/// This one proves the part a naive scope filter would have broken. The sweep
/// exists BECAUSE alias ids changed as identity evolved, so it has to keep
/// reaching this corpus's older documents while stopping at the corpus
/// boundary — and the older a document is, the less corpus evidence it carries:
///
/// * current (`_source.corpus_scope`, #755/rc.72)
/// * `_source.prefix` only (#737/rc.68)
/// * neither, but a prefix-scoped `_id` (#416/rc.57)
/// * neither, and an `_id` with no prefix in it either (rc.10..rc.56) — the
/// case the issue calls out, decided by reconstructing the ids THIS corpus
/// would have written rather than by guessing.
///
/// The last document is the honest residual: anonymous, and NOT one of this
/// corpus's reconstructions (a different `file_key`). It is left in place —
/// deleting a document that names no owner is the reach this fix removes — and
/// it is reported, non-fatally, by `stranded_alias_notes`.
///
/// FAIL-BEFORE, measured: with `sweep_duplicate_aliases`'s body replaced by the
/// unscoped `delete_by_query` this PR removes, all four of the sibling's and
/// the anonymous stranger's documents are deleted too — `Survivors: []`.
#[test]
fn the_duplicate_alias_sweep_scopes_every_identity_scheme_to_this_corpus() {
let ep = HttpEndpoint::start();
let es = Es::with_bulk_timeout(&ep.url, None, 30).expect("es client");
// The logical path both corpora hold a duplicate at.
let path = "docs/LICENSE.csv";
let key = "shared-key";
let path_id = "pid-docs-license";
// This corpus ("ax"), one document per scheme. All of these must be swept.
let ax_current = catalog::duplicate_file_id("ax", key, path, path_id);
let ax_prefix_field = "file-alias:ax:00000000000000000000000000000737".to_string();
let ax_id_only = "file-alias:ax:00000000000000000000000000000416".to_string();
let ax_anonymous = catalog::unprefixed_duplicate_file_ids(key, path, path_id);
// The sibling ("bx"), the same three attributable schemes. All must survive.
let bx_current = catalog::duplicate_file_id("bx", key, path, path_id);
let bx_prefix_field = "file-alias:bx:00000000000000000000000000000737".to_string();
let bx_id_only = "file-alias:bx:00000000000000000000000000000416".to_string();
// Anonymous, and not reconstructible as ours: a different `file_key`, so a
// pre-#416 build of SOME corpus wrote it and nothing says which.
let anonymous_stranger =
catalog::unprefixed_duplicate_file_ids("someone-elses-key", path, "")[0].clone();
let alias_doc = |scope: Option<&str>, prefix: Option<&str>| {
let mut doc = json!({
"doc_kind": "file",
"file_key": key,
"path": path,
"format": "duplicate",
"status": "duplicate",
});
if let Some(scope) = scope {
doc[catalog::CORPUS_SCOPE_FIELD] = json!(scope);
}
if let Some(prefix) = prefix {
doc["prefix"] = json!(prefix);
}
doc
};
let mut seeded: Vec<String> = vec![
ax_current.clone(),
ax_prefix_field.clone(),
ax_id_only.clone(),
bx_current.clone(),
bx_prefix_field.clone(),
bx_id_only.clone(),
anonymous_stranger.clone(),
];
seeded.extend(ax_anonymous.iter().cloned());
{
let mut st = ep.state.lock().unwrap();
let mut put = |id: &String, doc: Value| {
st.docs
.insert((catalog::CATALOG_INDEX.to_string(), id.clone()), doc);
};
put(&ax_current, alias_doc(Some("ax"), Some("ax")));
put(&ax_prefix_field, alias_doc(None, Some("ax")));
put(&ax_id_only, alias_doc(None, None));
for id in &ax_anonymous {
put(id, alias_doc(None, None));
}
put(&bx_current, alias_doc(Some("bx"), Some("bx")));
put(&bx_prefix_field, alias_doc(None, Some("bx")));
put(&bx_id_only, alias_doc(None, None));
put(&anonymous_stranger, alias_doc(None, None));
}
// What the finalize passes: the ids THIS corpus would have written for its
// own aliases under the pre-#416 scheme, reconstructed from its plan.
let own: HashSet<String> = catalog::unprefixed_duplicate_file_ids(key, path, path_id)
.into_iter()
.collect();
let mut stranded: Vec<String> = Vec::new();
let swept = sweep_duplicate_aliases(&es, "ax", &[path], &own, &mut stranded);
// Read every verdict out and RELEASE the endpoint's lock before asserting:
// an assert that fires while holding it poisons the mutex the server thread
// also takes, and the panic-in-a-destructor that follows aborts the whole
// test binary instead of failing this one test. This test is written to
// fail on unfixed code, so its failure mode has to stay clean.
let (survivors, alive): (String, Vec<String>) = {
let st = ep.state.lock().unwrap();
let mut alive: Vec<String> = seeded
.iter()
.filter(|id| {
st.docs
.contains_key(&(catalog::CATALOG_INDEX.to_string(), (*id).clone()))
})
.cloned()
.collect();
alive.sort();
(format!("{alive:?}"), alive)
};
swept.expect("the sweep completes");
for (id, what) in [
(&ax_current, "current-scheme"),
(&ax_prefix_field, "#737 `prefix`-field"),
(&ax_id_only, "#416 prefix-in-id"),
] {
assert!(
!alive.contains(id),
"#905: this corpus's own {what} alias must still be swept — the scope is \
applied per hit, not by narrowing the query. Survivors: {survivors}"
);
}
for id in &ax_anonymous {
assert!(
!alive.contains(id),
"#905: this corpus's pre-#416 alias carries no corpus anywhere, so it is swept \
on a reconstructed id — not stranded. Survivors: {survivors}"
);
}
for (id, what) in [
(&bx_current, "current-scheme"),
(&bx_prefix_field, "#737 `prefix`-field"),
(&bx_id_only, "#416 prefix-in-id"),
] {
assert!(
alive.contains(id),
"#905: a sibling corpus's {what} alias at the same logical path must survive — \
deleting it is the data loss. Survivors: {survivors}"
);
}
assert!(
alive.contains(&anonymous_stranger),
"#905: an alias that names no corpus and is not one of this corpus's own \
reconstructions is left in place, not deleted on a guess. Survivors: {survivors}"
);
// …and it is reported. Silently stranding it would be the other half of the
// trade the issue warned about.
assert_eq!(stranded.len(), 1, "{stranded:?}");
assert!(stranded[0].contains(path), "{stranded:?}");
let notes = stranded_alias_notes(&stranded).join("\n");
assert!(notes.contains("LEFT IN PLACE"), "{notes}");
assert!(notes.contains(&anonymous_stranger), "{notes}");
assert!(
stranded_alias_notes(&[]).is_empty(),
"an ordinary corpus with nothing stranded says nothing"
);
}
/// #739: a `file:` catalog doc written by a pre-#737 binary has NO `prefix`
/// field, so the #737 prefix-scoped sweep misses it on the first upgraded run.
/// The by-`_id` delete catches it (the id encodes the prefix), while a sibling
/// corpus's same-key legacy doc — a different `_id` — survives.
#[test]
fn sweep_excluded_groups_deletes_a_legacy_main_doc_by_id() {
let ep = HttpEndpoint::start();
{
let mut st = ep.state.lock().unwrap();
// Legacy docs: no `prefix` field (pre-#737). Corpus "ax" excludes the
// file; corpus "bx" holds a same-key copy under its own id.
st.docs.insert(
(
catalog::CATALOG_INDEX.to_string(),
"file:ax:legacy-key".to_string(),
),
json!({"doc_kind": "file", "path": "legacy.csv"}),
);
st.docs.insert(
(
catalog::CATALOG_INDEX.to_string(),
"file:bx:legacy-key".to_string(),
),
json!({"doc_kind": "file", "path": "legacy.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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
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 main doc is swept by id, even without a `prefix` field.
assert!(
!st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file:ax:legacy-key".to_string()
)),
"#739: the excluding corpus's legacy file: doc must be swept by id"
);
// The sibling corpus's same-key legacy doc survives (different id).
assert!(
st.docs.contains_key(&(
catalog::CATALOG_INDEX.to_string(),
"file:bx:legacy-key".to_string()
)),
"#739: a sibling corpus's legacy doc must NOT be swept"
);
}
/// #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,
text_analyzer: None,
scope: 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
/// live. Fail-before: with the dst-side invalidation reverted, the inbound edge
/// keeps no `invalid_at`.
#[test]
fn sweep_excluded_groups_invalidates_inbound_edges() {
let ep = HttpEndpoint::start();
let edges = ".xerj-memory-testbrain-edges";
// The excluded file anchors in dataset slug "ds"; edges point at this id.
let anchor = crate::ids::doc_id("ds", "key-excluded", "file");
{
let mut st = ep.state.lock().unwrap();
st.saw_dataset_mapping_update = true;
st.saw_catalog_mapping_update = true;
// A surviving file's edge INTO the excluded file's anchor.
st.docs.insert(
(edges.to_string(), "edge-inbound".to_string()),
json!({"src_file": "survivor.csv", "dst": anchor, "kind": "samedir"}),
);
// A surviving file's edge to some OTHER node must stay live.
st.docs.insert(
(edges.to_string(), "edge-other".to_string()),
json!({"src_file": "survivor.csv", "dst": "other-node-id", "kind": "samedir"}),
);
}
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,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
});
// plan.files carries the excluded file's slug so the dst anchor resolves.
plan.files.insert(
"key-excluded".to_string(),
crate::state::FileAssignment {
rel: "secret.csv".into(),
path_id: String::new(),
is_symlink: None,
family: "csv".into(),
gzip: false,
content_digest: None,
assignments: vec![(None, "ds".into())],
as_document: false,
},
);
let excluded = vec![InventoryDeltaEntry {
file_key: "key-excluded".into(),
path: "secret.csv".into(),
}];
let now_ms = 1_724_500_000_000i64;
sweep_excluded_groups(&es, "ax", &plan, &excluded, Some(edges), now_ms).expect("sweep");
let st = ep.state.lock().unwrap();
let inbound = st
.docs
.get(&(edges.to_string(), "edge-inbound".to_string()))
.expect("inbound edge present (soft-invalidated, not deleted)");
assert_eq!(
inbound.get("invalid_at").and_then(Value::as_i64),
Some(now_ms),
"#736: an inbound edge to the excluded file's anchor must be invalidated"
);
let other = st
.docs
.get(&(edges.to_string(), "edge-other".to_string()))
.expect("other edge present");
assert!(
other.get("invalid_at").is_none(),
"#736: an edge to a different node must NOT be invalidated"
);
}
/// #736 (replacement half): a run that SUPERSEDES a file's anchor node must
/// soft-invalidate the edges pointing AT the old anchor, not only the edges the
/// old generation taught.
///
/// The reachable trigger is `--fresh`. An ordinary resume maps every file back
/// onto its planned key (`select_resume_plan_keys`), so an in-place edit keeps
/// its `file_key` and its anchor id — nothing is superseded and the inbound
/// edges keep pointing at a live node. `--fresh` discards the plan and rebuilds
/// every key from current content, so an edited file gets a NEW key and a NEW
/// anchor, while `cleanup_required` (the journal's record of what is live) has
/// just been wiped with the journal — so before this fix the run invalidated
/// nothing at all, and every edge a surviving file taught into the old anchor
/// stayed live and searchable, pointing at a superseded node.
///
/// Fail-before: with the superseded-anchor invalidation reverted, the two
/// `a.md → old b anchor` edges come back with no `invalid_at` and the first
/// assertion fires.
#[test]
fn fresh_run_invalidates_inbound_edges_to_a_superseded_anchor() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("a.md"),
"# A\n\nSee [[b]] for the details of the target document.\n",
)
.unwrap();
fs::write(
corpus.path().join("b.md"),
"# B\n\nThe target document says one thing.\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.no_graph = false;
config.brain = Some("testbrain".into());
assert_eq!(run_index(config.clone()).unwrap(), 0);
let edges_index = detect::edges_index_name("testbrain");
// Anchor ids are read off the published file cards (`ax_locator: "file"`),
// so the test never has to recompute `ids::doc_id` itself.
let anchors_of = |path: &str| -> Vec<String> {
let st = endpoint.state.lock().unwrap();
let mut ids: Vec<String> = st
.docs
.iter()
.filter(|((index, _), doc)| {
index != catalog::CATALOG_INDEX
&& doc.get("ax_locator").and_then(Value::as_str) == Some("file")
&& doc.get("ax_path").and_then(Value::as_str) == Some(path)
})
.map(|((_, id), _)| id.clone())
.collect();
ids.sort();
ids
};
let edges_of = |predicate: &dyn Fn(&Value) -> bool| -> Vec<(String, Value)> {
let st = endpoint.state.lock().unwrap();
let mut rows: Vec<(String, Value)> = st
.docs
.iter()
.filter(|((index, _), doc)| *index == edges_index && predicate(doc))
.map(|((_, id), doc)| (id.clone(), doc.clone()))
.collect();
rows.sort_by(|left, right| left.0.cmp(&right.0));
rows
};
let old_b_anchor = {
let mut found = anchors_of("b.md");
assert_eq!(found.len(), 1, "run 1 publishes exactly one b.md file card");
found.pop().unwrap()
};
// The inbound edges the fix has to reach: taught by the SURVIVING file a.md,
// pointing at b.md's anchor. Asserted before the change so a corpus that
// stopped producing them could never make this test vacuously pass.
let inbound_before = edges_of(&|doc| {
doc.get("src_file").and_then(Value::as_str) == Some("a.md")
&& doc.get("dst").and_then(Value::as_str) == Some(old_b_anchor.as_str())
});
assert!(
!inbound_before.is_empty(),
"#736: the fixture must actually teach a.md → b.md edges"
);
// a.md's other edges (its own card → section chain) must survive untouched:
// a.md is byte-identical across the two runs, and #868's skip means an
// unchanged file must neither re-teach nor lose its edges.
let a_internal_before: Vec<String> = edges_of(&|doc| {
doc.get("src_file").and_then(Value::as_str) == Some("a.md")
&& doc.get("dst").and_then(Value::as_str) != Some(old_b_anchor.as_str())
})
.into_iter()
.map(|(id, _)| id)
.collect();
assert!(
!a_internal_before.is_empty(),
"#736: the fixture must have edges that the sweep MUST NOT touch"
);
// Edit b.md, then rebuild in place with --fresh: b.md's content key, and so
// its anchor node id, is superseded.
fs::write(
corpus.path().join("b.md"),
"# B\n\nThe target document now says something completely different today.\n",
)
.unwrap();
let mut fresh = config.clone();
fresh.fresh = true;
assert_eq!(run_index(fresh).unwrap(), 0);
let new_b_anchor = {
let found = anchors_of("b.md");
let fresh_ids: Vec<&String> = found.iter().filter(|id| **id != old_b_anchor).collect();
assert_eq!(
fresh_ids.len(),
1,
"--fresh must publish a new b.md file card after the edit, got {found:?}"
);
fresh_ids[0].clone()
};
assert_ne!(
new_b_anchor, old_b_anchor,
"the fixture must actually supersede b.md's anchor"
);
// The bug: every live edge pointing at the superseded anchor.
let inbound_after =
edges_of(&|doc| doc.get("dst").and_then(Value::as_str) == Some(old_b_anchor.as_str()));
assert_eq!(
inbound_after.len(),
inbound_before.len(),
"#736: prior edges are soft-invalidated, never deleted — the bi-temporal \
record must still be there for `as_of`"
);
for (id, doc) in &inbound_after {
assert!(
doc.get("invalid_at").is_some(),
"#736: edge {id} still points at b.md's superseded anchor {old_b_anchor} \
and was left live: {doc}"
);
}
// The old generation's own outbound edges go too — same root cause, same
// pass: `src_file == b.md` was never invalidated under --fresh either.
for (id, doc) in
edges_of(&|doc| doc.get("src").and_then(Value::as_str) == Some(old_b_anchor.as_str()))
{
assert!(
doc.get("invalid_at").is_some(),
"#736: edge {id} is taught BY b.md's superseded anchor and was left live: {doc}"
);
}
// …and the graph is still a graph: a.md links to the NEW anchor, live.
let inbound_new = edges_of(&|doc| {
doc.get("src_file").and_then(Value::as_str) == Some("a.md")
&& doc.get("dst").and_then(Value::as_str) == Some(new_b_anchor.as_str())
&& doc.get("invalid_at").is_none()
});
assert!(
!inbound_new.is_empty(),
"#736: the re-run must re-teach a.md → b.md against the new anchor"
);
// #868: the unchanged file's unrelated edges are untouched.
for id in &a_internal_before {
let st = endpoint.state.lock().unwrap();
let doc = st
.docs
.get(&(edges_index.clone(), id.clone()))
.unwrap_or_else(|| panic!("#868: a.md's edge {id} must survive the re-run"));
assert!(
doc.get("invalid_at").is_none(),
"#868: byte-identical a.md's own edge {id} must stay live: {doc}"
);
}
}
/// #585 (case 1): a pending (uncommitted) `--no-graph` generation must NOT be
/// resumed and committed on the default graph path — doing so silently commits
/// a no-graph generation under a graph authority (the #584 hazard, but for a
/// not-yet-committed generation the committed-manifest guard never sees it
/// because the pending-sync branch returns first). The re-run must be refused
/// before any mutation, symmetric to #584.
#[test]
fn pending_no_graph_generation_is_refused_on_the_graph_path() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let source = corpus.path().join("rows.csv");
fs::write(&source, "id,value\n1,first\n").unwrap();
let endpoint = HttpEndpoint::start();
// Commit generation 1 with the `--no-graph` generated executor.
let no_graph = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(no_graph.clone()).unwrap(), 0);
// Interrupt a second `--no-graph` generation after sync_begin: the data bulk
// fails, leaving a pending sync (no-graph, uncommitted).
fs::write(&source, "id,value\n1,first\n2,second\n").unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
assert!(run_index(no_graph.clone()).is_err());
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 2);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// The destination as the refused re-run will find it (the interrupted run's
// delete-before-replace already mutated it — irrelevant to the guard).
let docs_before_rerun = endpoint.data_docs().len();
// Re-run the SAME state dir on the default graph path. The pending no-graph
// generation must be refused, not resumed-and-committed.
let mut graph = no_graph.clone();
graph.no_graph = false;
let error = run_index(graph).unwrap_err();
let rendered = format!("{error:#}");
assert!(
rendered.contains("pending sync") && rendered.contains("different graph authority"),
"#585: a pending no-graph generation must be refused on the graph path with the clear message, got: {rendered}"
);
// No second generation committed, and the guard mutated nothing further.
assert_eq!(
journal_events(state_dir.path(), "sync_commit"),
1,
"#585: the refused graph re-run must not commit the pending no-graph generation"
);
assert_eq!(
endpoint.data_docs().len(),
docs_before_rerun,
"#585: the refused re-run must mutate nothing"
);
}
/// #585 (case 2): a `--no-graph` generation's genesis bootstrap (`sync_bootstrap`
/// committed, interrupted before `sync_begin`) must NOT be continued on the
/// default graph path either. Genesis carries no execution identity of its own
/// (`validate_genesis` requires `execution: None`), so unlike case 1 the guard
/// cannot compare a recorded mode — it instead relies on `sync_bootstrap` having
/// actually replayed, which only `begin_non_graph_generation` ever writes, and
/// only under `--no-graph`.
#[test]
fn no_graph_genesis_bootstrap_is_refused_on_the_graph_path() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let mut no_graph = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
no_graph.snapshot_max_bytes = 1;
// Interrupt the very first --no-graph run between sync_bootstrap and
// sync_begin: generation 0 (genesis) is committed, nothing else is.
let error = run_index(no_graph.clone()).unwrap_err();
assert!(format!("{error:#}").contains("snapshot source footprint"));
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
assert_eq!(endpoint.data_docs().len(), 0);
// Re-run the SAME state dir on the default graph path. The pending
// --no-graph genesis bootstrap must be refused, not continued.
let mut graph = no_graph.clone();
graph.no_graph = false;
graph.snapshot_max_bytes = 64 << 30;
let error = run_index(graph.clone()).unwrap_err();
let rendered = format!("{error:#}");
assert!(
rendered.contains("genesis bootstrap") && rendered.contains("different graph authority"),
"#585 case 2: a --no-graph genesis bootstrap must be refused on the graph path with the \
clear message, got: {rendered}"
);
// #718: the message must name every valid recovery route, including
// `--fresh` — the guard exempts it (nothing is committed past genesis), so
// omitting it hides a working recovery from the operator.
assert!(
rendered.contains("--fresh"),
"#718: the recovery message must offer --fresh, which the guard exempts, got: {rendered}"
);
// No mutation: nothing indexed, no new durable transaction beyond the
// original sync_bootstrap.
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
assert_eq!(endpoint.data_docs().len(), 0);
// #718: `--dry-run` does not exempt the refusal. A graph-path dry run over
// the pending --no-graph genesis is refused the same way (there is no graph
// genesis projection to show), with the same recovery message, and mutates
// nothing — a dry run of a refused run is a refusal.
let mut graph_dry = graph;
graph_dry.dry_run = true;
let dry_error = run_index(graph_dry).unwrap_err();
let dry_rendered = format!("{dry_error:#}");
assert!(
dry_rendered.contains("genesis bootstrap") && dry_rendered.contains("--fresh"),
"#718: --dry-run over a genesis on the graph path is refused too, got: {dry_rendered}"
);
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
assert_eq!(endpoint.data_docs().len(), 0);
// Positive control: re-running in the SAME (--no-graph) mode continues
// the interrupted genesis normally.
no_graph.snapshot_max_bytes = 64 << 30;
assert_eq!(run_index(no_graph).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_bootstrap"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(endpoint.data_docs().len(), 1);
}
// ── #929: one dataset the server refuses must cost that dataset, not the run ──
/// A two-dataset corpus: tabular rows and prose. Which dataset the plan lists
/// first is an implementation detail, so the tests below refuse each in turn —
/// one of the two orders necessarily puts the refusal BEFORE an accepted
/// dataset, which is the order that used to lose the accepted one.
fn write_two_dataset_corpus(root: &Path) {
fs::write(root.join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
fs::write(root.join("b.csv"), "id,value\n3,gamma\n").unwrap();
fs::write(
root.join("notes.md"),
"# Notes\n\nTiered merge policy picks segments of similar size.\n",
)
.unwrap();
}
/// The two dataset indices that corpus produces, learned from a clean run so
/// the tests do not hard-code a slug scheme: `(tabular, prose)`.
fn two_dataset_indices() -> (String, String) {
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(
run_index(config).unwrap(),
0,
"the clean corpus indexes whole"
);
let docs = endpoint.data_docs();
let index_of = |path: &str| {
docs.iter()
.find(|doc| doc["ax_path"] == path)
.and_then(|doc| doc["ax_dataset"].as_str())
.map(|slug| format!("incremental-http-{slug}"))
.unwrap_or_else(|| panic!("{path} was not indexed by the clean run"))
};
let (tabular, prose) = (index_of("a.csv"), index_of("notes.md"));
assert_ne!(tabular, prose, "the fixture must produce two datasets");
(tabular, prose)
}
fn refused_detail(summary: &Value) -> Vec<Value> {
serde_json::from_str(
summary["refused_datasets_json"]
.as_str()
.expect("a run that refused a dataset carries refused_datasets_json"),
)
.unwrap()
}
/// #929, the defect itself. One dataset's mapping is refused with the literal
/// 400 from the report; the other dataset must be indexed, the run must end 3
/// (not 1), and the generation that commits must SAY it lacks a dataset — in
/// its summary, in the catalog's run document and on each file it cost.
#[test]
fn a_refused_dataset_mapping_costs_that_dataset_not_the_run() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, prose) = two_dataset_indices();
for (refused_index, lost, kept) in [
(&tabular, vec!["a.csv", "b.csv"], vec!["notes.md"]),
(&prose, vec!["notes.md"], vec!["a.csv", "b.csv"]),
] {
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().refused_dataset_mappings = vec![refused_index.clone()];
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config.clone())
.unwrap_or_else(|error| panic!("refusing {refused_index} aborted the run: {error:#}"));
assert_eq!(code, 3, "a refused dataset is exit 3, never 0 and never 1");
let summary = summary.expect("generated run returns its committed run projection");
// Every dataset's mapping was attempted: the loop did not stop at the
// refusal, whichever side of it the accepted dataset sat on.
let attempted = endpoint.state.lock().unwrap().dataset_mapping_puts.clone();
assert!(
attempted.contains(&tabular) && attempted.contains(&prose),
"both datasets must be attempted, got {attempted:?}"
);
// The rest of the corpus is indexed and nothing of the refused dataset is.
let docs = endpoint.data_docs();
assert_eq!(
paths(&docs),
kept,
"refusing {refused_index} must index exactly the other dataset"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
// The committed generation names what it lacks.
assert_eq!(summary["datasets_refused"], 1);
assert_eq!(summary["files_refused"], lost.len());
assert_eq!(summary["files_junk"], lost.len());
// Every file is still accounted for, and none is counted twice: what
// is indexed is exactly what was kept, and the rest is the refusal.
assert_eq!(summary["files_indexed"], kept.len(), "{summary}");
assert_eq!(summary["files_total"], kept.len() + lost.len(), "{summary}");
let detail = refused_detail(&summary);
assert_eq!(detail.len(), 1);
assert_eq!(detail[0]["index"], refused_index.as_str());
assert_eq!(detail[0]["files"], lost.len());
let reason = detail[0]["reason"].as_str().unwrap();
assert!(
reason.contains("400") && reason.contains("nested semantic_text field"),
"the server's own words travel with the refusal, got: {reason}"
);
// …and so does every file it cost, in the catalog a reader starts from.
let catalog = endpoint.catalog_docs();
let mut junked: Vec<&str> = catalog
.iter()
.filter(|doc| doc["doc_kind"] == "file" && doc["status"] == "junk")
.map(|doc| {
assert!(
doc["reason"]
.as_str()
.is_some_and(|reason| reason.contains("was refused by the server")),
"a refused file says why it is not indexed: {doc}"
);
doc["path"].as_str().or(doc["rel"].as_str()).unwrap_or("")
})
.collect();
junked.sort_unstable();
assert_eq!(junked, lost, "catalog file documents for the refused files");
assert!(
!catalog
.iter()
.any(|doc| doc["doc_kind"] == "dataset" && doc["index"] == refused_index.as_str()),
"a refused dataset must not be published as a dataset document"
);
// A no-op re-run is still exit 3, still names the dataset, and writes
// nothing: the refusal is part of the committed generation, not a log
// line only the first run printed.
let bulks = endpoint.data_bulk_requests();
let (again, again_summary) = run_index_report(config).unwrap();
assert_eq!(again, 3);
let again_summary = again_summary.unwrap();
assert_eq!(again_summary["generation"], 1);
assert_eq!(again_summary["datasets_refused"], 1);
assert_eq!(refused_detail(&again_summary), detail);
assert_eq!(endpoint.data_bulk_requests(), bulks);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
}
/// #929: the tolerance is narrow on purpose. A 503 on the same request is the
/// ENDPOINT failing — it says nothing about the dataset — so routing around it
/// would publish a corpus that silently lacks data for a transient reason.
#[test]
fn an_unavailable_dataset_mapping_still_aborts_the_run() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, _) = two_dataset_indices();
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().unavailable_dataset_mappings = vec![tabular.clone()];
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let error = run_index(config).unwrap_err();
let rendered = format!("{error:#}");
assert!(
rendered.contains("503") && rendered.contains(&tabular),
"an endpoint failure names the request that failed, got: {rendered}"
);
assert_eq!(endpoint.data_docs().len(), 0, "nothing is published");
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
}
/// #929: a refusal must not turn into "this corpus can never be re-indexed".
/// The refused files are junk in the committed plan, so an incremental run has
/// to carry the refusal forward — for the unchanged file, for a CHANGED file of
/// the refused shape, and for a NEW one — while everything else reconciles.
#[test]
fn a_refused_dataset_stays_refused_across_an_incremental_generation() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, _) = two_dataset_indices();
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().refused_dataset_mappings = vec![tabular.clone()];
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 3);
assert_eq!(paths(&endpoint.data_docs()), ["notes.md"]);
fs::write(
corpus.path().join("a.csv"),
"id,value\n1,alpha\n2,beta\n9,changed\n",
)
.unwrap();
fs::write(corpus.path().join("c.csv"), "id,value\n4,delta\n").unwrap();
fs::write(
corpus.path().join("more.md"),
"# More\n\nA log-structured merge tree compacts sorted runs.\n",
)
.unwrap();
let (code, summary) = run_index_report(config)
.unwrap_or_else(|error| panic!("the incremental run aborted: {error:#}"));
assert_eq!(code, 3);
let summary = summary.unwrap();
assert_eq!(summary["generation"], 2);
assert_eq!(summary["datasets_refused"], 1);
assert_eq!(
summary["files_refused"], 3,
"unchanged b.csv, changed a.csv and new c.csv are all refused"
);
let detail = refused_detail(&summary);
assert_eq!(detail[0]["index"], tabular.as_str());
assert_eq!(paths(&endpoint.data_docs()), ["more.md", "notes.md"]);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}
/// #929: once a generation is sealed a dataset can no longer leave it, so a
/// server that starts refusing a dataset it accepted before is fatal — but the
/// error names EVERY refused dataset and the one recovery that works, and the
/// committed generation is left exactly as it was.
#[test]
fn a_refusal_of_an_already_committed_dataset_is_fatal_and_names_it() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, prose) = two_dataset_indices();
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let committed_docs = endpoint.data_docs();
endpoint.state.lock().unwrap().refused_dataset_mappings = vec![tabular.clone(), prose.clone()];
fs::write(corpus.path().join("c.csv"), "id,value\n4,delta\n").unwrap();
let error = run_index(config).unwrap_err();
let rendered = format!("{error:#}");
assert!(
rendered.contains("2 dataset(s)") && rendered.contains("already sealed"),
"got: {rendered}"
);
for index in [&tabular, &prose] {
assert!(
rendered.contains(index.as_str()),
"every refused dataset is named, not just the first; missing {index} in: {rendered}"
);
}
assert!(rendered.contains("new --state-dir") && rendered.contains("new --prefix"));
assert_eq!(
endpoint.data_docs(),
committed_docs,
"no document was published"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
// ── #931: the generated path reports the phase it is actually in ─────────────
/// Every `xerj-progress` line of a captured stream, as `key -> value` maps, in
/// the order they were emitted.
fn progress_lines(stream: &str) -> Vec<BTreeMap<String, String>> {
stream
.lines()
.filter_map(|line| line.strip_prefix("xerj-progress "))
.map(|line| {
line.split_whitespace()
.filter_map(|pair| pair.split_once('='))
.map(|(key, value)| (key.to_owned(), value.to_owned()))
.collect()
})
.collect()
}
/// #931, the defect itself. On the generated `--no-graph` route the stream used
/// to say `phase=scan … pct=100.0 … eta_quality=stalled` from the end of the
/// scan until the run exited — through mapping installation, sealing, every
/// bulk request and the read-back barrier — which is byte for byte what the
/// real end-of-scan hang prints. Each of those steps is now a phase of its own
/// with a real denominator, so `scan` can only ever mean scan.
#[test]
fn the_generated_path_reports_indexing_as_indexing_not_as_a_stalled_scan() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let _sink_guard = crate::progress::SINK_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.quiet = false;
config.progress = crate::progress::ProgressMode::Plain;
let buffer = Arc::new(Mutex::new(Vec::new()));
let code = {
let _sink = crate::progress::install_test_sink(&buffer);
run_index(config).unwrap()
};
assert_eq!(code, 0);
let stream = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
let lines = progress_lines(&stream);
// The phases, de-duplicated in order of first appearance.
let mut phases: Vec<&str> = Vec::new();
for line in &lines {
let phase = line["phase"].as_str();
if phases.last() != Some(&phase) {
phases.push(phase);
}
}
assert_eq!(
phases,
[
"walk",
"hash",
"scan",
"prepare",
"snapshot",
"index",
"finalize-catalog",
// #1267: the `run_id`-scoped catalog read-back is its own phase.
// On a full-rewrite resume it runs for ~35 min, and used to do so
// behind the finished `finalize-catalog` ticker — a line that only
// climbed `since_progress_s`, i.e. looked exactly like a hang.
"finalize-catalog-readback",
"finalize-refresh",
"finalize-verify"
],
"each long step of the generated route is its own phase, in order:\n{stream}"
);
// #1267: that read-back phase opens at zero with a real denominator — the
// number of catalog documents this generation publishes (at least the run
// document), read by id. A count with no total is the frozen ticker.
let readback_opened = lines
.iter()
.find(|line| line["phase"] == "finalize-catalog-readback")
.expect("the catalog read-back walk opens a phase of its own");
assert_eq!(readback_opened["basis"], "items", "{readback_opened:?}");
let (done, total) = readback_opened["items"].split_once('/').unwrap();
assert_eq!(
done, "0",
"the read-back phase opens at zero: {readback_opened:?}"
);
assert!(
total.parse::<u64>().unwrap() > 0,
"the read-back phase opens with the engine's match total, not an unknown \
denominator: {readback_opened:?}"
);
// `scan` never reappears once the run has moved on — the exact symptom.
let left_scan = lines
.iter()
.position(|line| line["phase"] == "prepare")
.unwrap();
assert!(
lines[left_scan..]
.iter()
.all(|line| line["phase"] != "scan"),
"a line after `prepare` still claims the scan phase:\n{stream}"
);
// The `index` phase has an honest denominator in both units: the three
// files still to apply, and the sealed bulk bytes they will send.
let index_opened = lines
.iter()
.find(|line| line["phase"] == "index")
.expect("the index phase opens with a line of its own");
assert_eq!(index_opened["items"], "0/3", "{index_opened:?}");
assert_eq!(index_opened["basis"], "bytes", "{index_opened:?}");
let (done, total) = index_opened["bytes"].split_once('/').unwrap();
assert_eq!(done, "0");
assert!(
total.parse::<u64>().unwrap() > 0,
"the index phase counts sealed bulk bytes, got {index_opened:?}"
);
// `finalize-refresh` is counted in indices: both datasets and the catalog.
// Without a phase of its own this step held `finalize-verify` at `0/N`.
let refresh_opened = lines
.iter()
.find(|line| line["phase"] == "finalize-refresh")
.unwrap();
assert_eq!(refresh_opened["items"], "0/3", "{refresh_opened:?}");
// `snapshot` is denominated in the SOURCE bytes it copies and seals.
let source_bytes: u64 = ["a.csv", "b.csv", "notes.md"]
.iter()
.map(|name| fs::metadata(corpus.path().join(name)).unwrap().len())
.sum();
let snapshot_opened = lines
.iter()
.find(|line| line["phase"] == "snapshot")
.unwrap();
assert_eq!(snapshot_opened["items"], "0/3");
assert_eq!(snapshot_opened["bytes"], format!("0/{source_bytes}"));
// The run closes its own stream, successfully, exactly once.
let done: Vec<&str> = stream
.lines()
.filter(|line| line.starts_with("xerj-done "))
.collect();
assert_eq!(done.len(), 1, "{stream}");
assert!(
done[0].contains("ok=true") && done[0].contains("exit=0"),
"{}",
done[0]
);
}
/// #931 + #929 together: a refusal is announced on the progress surface (it is
/// a `warn`, which `--quiet` does not silence) and the terminal line carries
/// the counts, so an agent reading only the machine stream learns that the
/// corpus lacks a dataset without parsing prose.
#[test]
fn a_refusal_is_on_the_progress_stream_and_the_terminal_line() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let _sink_guard = crate::progress::SINK_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, _) = two_dataset_indices();
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().refused_dataset_mappings = vec![tabular.clone()];
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.quiet = false;
config.progress = crate::progress::ProgressMode::Plain;
let buffer = Arc::new(Mutex::new(Vec::new()));
let code = {
let _sink = crate::progress::install_test_sink(&buffer);
run_index(config).unwrap()
};
assert_eq!(code, 3);
let stream = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
assert!(
stream.contains("REFUSED by the server")
&& stream.contains("every other dataset continues"),
"the refusal is announced while the run continues:\n{stream}"
);
let done = stream
.lines()
.find(|line| line.starts_with("xerj-done "))
.expect("the run closes its stream");
assert!(
done.contains("ok=true")
&& done.contains("exit=3")
&& done.contains("datasets_refused=1")
&& done.contains("files_refused=2"),
"the terminal line carries the refusal counts, got: {done}"
);
// The index phase counts only what is left to index: the one prose file.
let index_opened = progress_lines(&stream)
.into_iter()
.find(|line| line["phase"] == "index")
.unwrap();
assert_eq!(index_opened["items"], "0/1", "{index_opened:?}");
}
/// #929, the degenerate end: the server refuses EVERY dataset. Nothing can be
/// indexed, and the run must say exactly that — a committed generation with no
/// documents, exit 3, every file accounted for as refused — rather than panic
/// on an empty plan or, worse, report an empty corpus as a clean success. A
/// wrapper that verifies with `_count > 0` (`xerj corpus index`) then keeps
/// its old index, which is the right outcome.
#[test]
fn refusing_every_dataset_commits_nothing_searchable_and_is_not_a_success() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let (tabular, prose) = two_dataset_indices();
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().refused_dataset_mappings = vec![tabular, prose];
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config)
.unwrap_or_else(|error| panic!("refusing every dataset aborted the run: {error:#}"));
assert_eq!(code, 3, "an empty corpus is never exit 0");
let summary = summary.unwrap();
assert_eq!(summary["datasets_refused"], 2, "{summary}");
assert_eq!(summary["files_refused"], 3, "{summary}");
assert_eq!(summary["files_indexed"], 0, "{summary}");
assert_eq!(summary["records_total"], 0, "{summary}");
assert_eq!(
endpoint.data_docs().len(),
0,
"nothing was searchable to publish"
);
assert_eq!(endpoint.data_bulk_requests(), 0);
}
/// #931: a resumed run must not credit itself with an earlier attempt's writes.
///
/// The replay is interrupted after two of three operations committed. The
/// resumed run then has to (a) open at `replay`, not at a `scan` it is not
/// doing, and (b) size its `index` phase by what is LEFT — one operation — so
/// it starts at 0% of the remaining work. Counting all three would open the
/// phase at 0/3 and finish it at 1/3: a bar that stops at 33% on a run that
/// completed. Counting the two committed ones as done would open it at 67% for
/// work this invocation never did.
#[test]
fn a_resumed_replay_opens_at_replay_and_counts_only_what_remains() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let _sink_guard = crate::progress::SINK_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_two_dataset_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().fail_data_bulk_number = Some(3);
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.quiet = false;
config.progress = crate::progress::ProgressMode::Plain;
let first = Arc::new(Mutex::new(Vec::new()));
{
let _sink = crate::progress::install_test_sink(&first);
run_index(config.clone()).expect_err("the third data bulk is refused");
}
assert_eq!(journal_events(state_dir.path(), "sync_begin"), 1);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
let first = String::from_utf8(first.lock().unwrap().clone()).unwrap();
assert!(
first
.lines()
.any(|line| line.starts_with("xerj-done ") && line.contains("ok=false")),
"the interrupted attempt closes its own stream as a failure:\n{first}"
);
let resumed = Arc::new(Mutex::new(Vec::new()));
let code = {
let _sink = crate::progress::install_test_sink(&resumed);
run_index(config).unwrap()
};
assert_eq!(code, 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(paths(&endpoint.data_docs()), ["a.csv", "b.csv", "notes.md"]);
let stream = String::from_utf8(resumed.lock().unwrap().clone()).unwrap();
let lines = progress_lines(&stream);
assert_eq!(
lines.first().map(|line| line["phase"].as_str()),
Some("replay"),
"a resumed run says it is replaying:\n{stream}"
);
assert!(
lines
.iter()
.all(|line| line["phase"] != "scan" && line["phase"] != "snapshot"),
"a resume re-scans and re-seals nothing, so it must not claim to:\n{stream}"
);
let index_lines: Vec<_> = lines
.iter()
.filter(|line| line["phase"] == "index")
.collect();
assert_eq!(
index_lines.first().map(|line| line["items"].as_str()),
Some("0/1"),
"two of three operations were already committed; one remains:\n{stream}"
);
let done = stream
.lines()
.find(|line| line.starts_with("xerj-done "))
.unwrap();
assert!(
done.contains("ok=true") && done.contains("exit=0"),
"{done}"
);
}
/// #944 on the generated (`--no-graph`) path: one data bulk comes back with
/// half its items answered 429 by the engine's memory circuit breaker.
/// `checked_bulk` used to abort the run on the spot — the literal end of a
/// 48,533-file run at 60%. The rejected half is re-sent, and only that half;
/// every record lands; the run commits its generation and exits 0; and the
/// summary says it happened.
#[test]
fn a_per_item_429_in_a_sealed_bulk_is_resent_and_the_generation_commits() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("rows.csv"),
"id,value\n1,first\n2,second\n3,third\n4,fourth\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
endpoint
.state
.lock()
.unwrap()
.throttle_next_data_bulk_odd_items = true;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
assert_eq!(code, 0, "a transient per-item 429 must not end the run");
let summary = summary.unwrap();
assert_eq!(summary["bulk_retries"], 1, "{summary}");
assert_eq!(summary["bulk_items_reissued"], 2, "{summary}");
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(
endpoint.data_docs().len(),
4,
"every rejected record landed on the re-send"
);
let counts = endpoint.state.lock().unwrap().data_bulk_item_counts.clone();
assert_eq!(
&counts[..2],
&[4, 2],
"the re-send carried exactly the two rejected items: {counts:?}"
);
}
/// #955 on the generated (`--no-graph`) path — the literal end of the
/// full-corpus verification run, scaled down. The engine refuses a `_bulk` of
/// more than `max_actions_per_bulk` actions whole, with ONE item of status 413.
/// The catalog projection holds a document per file and went out as one
/// request, so on a 48,533-file corpus (51,129 actions against a limit of
/// 50,000) the run ended `exit=1 reason=aborted` in `finalize-catalog`, after
/// all 47,444 operations had been applied:
///
/// ```text
/// error: prepared bulk contained 1 rejected items: {"type":"engine_exception",
/// "reason":"bulk request contains 102258 lines (~51129 actions); exceeds
/// max_actions_per_bulk of 50000","status":413}
/// ```
///
/// Here the limit is 2 actions and the corpus has seven files. Every refused
/// request — a data bulk and the catalog alike — is halved until it lands; the
/// generation commits; the catalog the run publishes is the one an unlimited
/// server gets; and the summary says the server pushed back on size.
#[test]
fn a_bulk_the_server_refuses_as_too_large_is_halved_and_the_generation_commits() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let write_corpus = |root: &Path| {
fs::write(
root.join("rows.csv"),
"id,value\n1,first\n2,second\n3,third\n4,fourth\n5,fifth\n",
)
.unwrap();
for name in ["a", "b", "c", "d", "e", "f"] {
fs::write(
root.join(format!("{name}.md")),
format!("# {name}\n\nNotes about {name}, long enough to be prose.\n"),
)
.unwrap();
}
};
let catalog_shape = |endpoint: &HttpEndpoint| {
let mut kinds: Vec<String> = endpoint
.catalog_docs()
.iter()
.map(|doc| {
format!(
"{}:{}:{}",
doc["doc_kind"].as_str().unwrap_or("?"),
doc["path"]
.as_str()
.or_else(|| doc["slug"].as_str())
.unwrap_or(""),
doc["records"]
.as_u64()
.or_else(|| doc["record_count"].as_u64())
.unwrap_or(0)
)
})
.collect();
kinds.sort();
kinds
};
// Control: the same corpus against a server with no limit.
let control_corpus = tempfile::tempdir().unwrap();
let control_state = tempfile::tempdir().unwrap();
write_corpus(control_corpus.path());
let control = HttpEndpoint::start();
let (code, summary) = run_index_report(cfg(
control_corpus.path(),
control_state.path(),
&control.url,
false,
))
.unwrap();
assert_eq!(code, 0);
assert!(
summary.unwrap().get("bulk_splits").is_none(),
"a run the server never refused for size reports nothing"
);
let expected_catalog = catalog_shape(&control);
assert!(
expected_catalog.len() > 4,
"the control catalog must be larger than the limit for this test to mean anything: \
{expected_catalog:?}"
);
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
write_corpus(corpus.path());
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().max_actions_per_bulk = Some(2);
let (code, summary) =
run_index_report(cfg(corpus.path(), state_dir.path(), &endpoint.url, false)).unwrap();
assert_eq!(
code, 0,
"a request the server calls too large must not end the run"
);
let summary = summary.unwrap();
assert!(
summary["bulk_splits"].as_u64().unwrap_or(0) >= 1,
"{summary}"
);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(
endpoint.data_docs().len(),
control.data_docs().len(),
"every record landed"
);
assert_eq!(
catalog_shape(&endpoint),
expected_catalog,
"the catalog is the one an unlimited server gets"
);
let locked = endpoint.state.lock().unwrap();
assert!(
!locked.oversize_bulks_refused.is_empty(),
"the limit was actually met"
);
assert!(
locked.bulk_line_counts.iter().all(|lines| *lines <= 4),
"{:?}",
locked.bulk_line_counts
);
// The bound is learned, not rediscovered by every request: with a limit
// of 2 the run halves its way down once per dimension it meets, not once
// per bulk.
assert!(
locked.oversize_bulks_refused.len() <= 6,
"refused {} requests: {:?}",
locked.oversize_bulks_refused.len(),
locked.oversize_bulks_refused
);
}
/// #944, second shape, on the generated path: the engine answers the whole
/// `_bulk` HTTP 429 — the literal end of the resumed full-corpus run
/// (`error: _bulk: HTTP 429 Too Many Requests`), where the transport retry gave
/// up after six attempts. Three whole-request 429s in a row are waited out
/// through the same patience as per-item ones, every record lands, the
/// generation commits, and the summary counts the re-sends.
#[test]
fn a_whole_request_429_on_a_sealed_bulk_is_resent_and_the_generation_commits() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("rows.csv"),
"id,value\n1,first\n2,second\n3,third\n4,fourth\n",
)
.unwrap();
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().throttle_whole_data_bulks = 3;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
assert_eq!(
code, 0,
"a whole-request 429 that clears must not end the run"
);
let summary = summary.unwrap();
assert_eq!(summary["bulk_retries"], 3, "{summary}");
assert_eq!(summary["bulk_items_reissued"], 12, "{summary}");
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
assert_eq!(
endpoint.data_docs().len(),
4,
"every record landed once the node accepted the re-send"
);
let counts = endpoint.state.lock().unwrap().data_bulk_item_counts.clone();
assert_eq!(
&counts[..4],
&[4, 4, 4, 4],
"the whole body is re-sent each time, nothing was accepted: {counts:?}"
);
}
// ===========================================================================
// `--watch`: the same incremental reconcile, driven by filesystem events.
//
// The contract these prove is the one that makes the feature safe to ship: after
// ANY sequence of changes, a watched session holds the index a fresh full
// `xerj autoindex` of the same tree would have produced — same document ids,
// same record count, same sources. They drive `watch::one_pass`, which is the
// same function the live session loop calls, with the burst a working watcher
// would have reported for the change the test just made. The watcher's own
// event plumbing is covered by `watch`'s unit tests and by the live measurement
// in docs/reference/autoindex-watch.md; what is proven HERE is convergence,
// which is the part a timing-dependent test could never prove reliably.
// ===========================================================================
/// Every published data document, keyed by `(index, _id)`, with the one
/// run-scoped field removed.
///
/// `ax_run` names the invocation that wrote the document, so it differs between
/// a watched session and a fresh run by construction; everything else must be
/// identical or the two indexes are not the same index.
fn published(endpoint: &HttpEndpoint) -> BTreeMap<(String, String), Value> {
endpoint
.state
.lock()
.unwrap()
.docs
.iter()
.filter(|((index, _), _)| index != catalog::CATALOG_INDEX)
.map(|((index, id), doc)| {
let mut doc = doc.clone();
if let Some(object) = doc.as_object_mut() {
object.remove("ax_run");
}
((index.clone(), id.clone()), doc)
})
.collect()
}
/// The property that actually guards the digest cache: after a watched pass, a
/// plain `xerj autoindex` re-run — which re-hashes every byte, trusting nothing
/// — must be a no-op that changes not one document.
///
/// This is stronger than comparing against a fresh index and it isolates exactly
/// what `--watch` adds. If a carried digest ever let a changed file skip its
/// re-hash, the re-run reads the bytes, sees the difference, and republishes;
/// this assertion fails. It also holds for corpus shapes where a FRESH index
/// legitimately differs (see `fresh_full_index`).
fn a_rerun_changes_nothing(config: &IndexCfg, endpoint: &HttpEndpoint, context: &str) {
let before = published(endpoint);
let code = run_index(config.clone()).unwrap();
assert!(
matches!(code, 0 | 3),
"{context}: the verifying re-run must complete (exit {code})"
);
assert_eq!(
published(endpoint),
before,
"{context}: a full-hash re-run after a watched pass must change nothing — \
the watched pass missed something"
);
}
/// A fresh, independent full index of `root`.
///
/// Useful as ground truth only where dataset identity cannot move. An
/// incremental run — watched or not — deliberately PRESERVES the committed
/// dataset and schema identity, while a fresh run re-elects dataset slugs from
/// the corpus it sees (`reconcile_plan.rs`, module docs). So after a change that
/// alters slug election — renaming the directory a dataset was named after — a
/// fresh index writes `to/one.csv` into a dataset called `to` where the
/// incremental corpus still calls it `from`. Same paths, same bytes, different
/// dataset name and therefore different index and document id. That is the
/// incremental route's rule, not the watcher's, and a manual re-run does exactly
/// the same thing; `a_rerun_changes_nothing` is what those cases assert instead.
fn fresh_full_index(root: &Path) -> (HttpEndpoint, tempfile::TempDir) {
let endpoint = HttpEndpoint::start();
let state_dir = tempfile::tempdir().unwrap();
let config = cfg(root, state_dir.path(), &endpoint.url, false);
assert!(
matches!(run_index(config), Ok(0) | Ok(3)),
"the reference full index must complete"
);
(endpoint, state_dir)
}
/// Deterministic, dependency-free PRNG. The seed is printed with every failure
/// so a red run is replayable.
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
// xorshift64*, enough for choosing between six edit kinds.
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
x.wrapping_mul(0x2545_f491_4f6c_dd1d)
}
fn below(&mut self, n: u64) -> u64 {
self.next() % n
}
}
#[test]
fn a_watch_pass_rehashes_only_what_changed_and_publishes_it() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(root.join("a.csv"), "id,value\n1,alpha\n").unwrap();
fs::write(root.join("b.csv"), "id,value\n2,beta\n").unwrap();
fs::write(root.join("c.csv"), "id,value\n3,gamma\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
// Pass 1 is the full one: nothing is cached yet.
let mut carry = crate::watch::Carry::default();
let (outcome, (carried, _, hashed, _)) =
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full());
assert_eq!(outcome.unwrap().0, 0);
assert_eq!(
(hashed, carried),
(3, 0),
"the first pass must hash the corpus"
);
assert_eq!(carry.len(), 3, "and cache what it hashed");
// Pass 2: one file changed, and the watcher says so.
fs::write(root.join("b.csv"), "id,value\n2,beta-rewritten\n").unwrap();
let burst = crate::watch::ChangeSet::from_paths(&[root.join("b.csv")]);
let (outcome, (carried, _, hashed, _)) =
crate::watch::one_pass(&config, &root, &mut carry, &burst);
assert_eq!(outcome.unwrap().0, 0);
assert_eq!(
(hashed, carried),
(1, 2),
"only the changed file may be read again"
);
// …and the new bytes are what the index holds.
let values: Vec<String> = endpoint
.data_docs()
.iter()
.filter_map(|doc| doc.get("value").and_then(Value::as_str))
.map(str::to_owned)
.collect();
assert!(
values.iter().any(|v| v == "beta-rewritten"),
"the changed file's new content must be live: {values:?}"
);
assert!(
!values.iter().any(|v| v == "beta"),
"and its old content must not: {values:?}"
);
// Nothing was skipped: a full-hash re-run changes nothing.
a_rerun_changes_nothing(&config, &endpoint, "one file changed");
// And on a corpus whose dataset identity cannot move, the watched index is
// byte-for-byte the index a fresh full run would have built.
let (reference, _reference_state) = fresh_full_index(&root);
assert_eq!(
published(&endpoint),
published(&reference),
"a watched index must equal a fresh full index"
);
}
fn joined_docs(endpoint: &HttpEndpoint) -> String {
endpoint
.data_docs()
.iter()
.map(|doc| doc.to_string())
.collect::<Vec<_>>()
.join("\n")
}
/// #2176: a Python file indexed as a comment and an import freezes `docs`
/// without a `code` field. Adding a function while `--watch` is running must
/// publish the new source, install `code` on the dataset, and still index a
/// sibling file that changed in the same pass. On the failing build this
/// pass returns the frozen-schema error and neither edit lands.
#[test]
fn adding_a_python_function_under_watch_indexes_the_new_field_and_sibling_edits() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(
root.join("module.py"),
"# The birchbefore marker identifies the cancellation module.\nimport math\n",
)
.unwrap();
fs::write(root.join("notes.md"), "birchdoc-before\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
let (outcome, _) =
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full());
assert_eq!(outcome.unwrap().0, 0, "the symbol-less file must index");
let first = joined_docs(&endpoint);
assert!(
first.contains("birchbefore"),
"the first pass must publish the original source: {first}"
);
assert!(first.contains("birchdoc-before"), "{first}");
fs::write(
root.join("module.py"),
"# The birchafter marker identifies the cancellation module.\n\
import math\n\
\n\
def cancel_request():\n\
return \"birchfunction\"\n",
)
.unwrap();
fs::write(root.join("notes.md"), "birchdoc-after\n").unwrap();
let burst =
crate::watch::ChangeSet::from_paths(&[root.join("module.py"), root.join("notes.md")]);
let (outcome, _) = crate::watch::one_pass(&config, &root, &mut carry, &burst);
let (code, _) = outcome
.expect("adding `code` to a frozen docs dataset must not fail the watch pass (#2176)");
assert_eq!(code, 0, "both files are compatible once the mapping grows");
let indexed = joined_docs(&endpoint);
assert!(
indexed.contains("birchafter"),
"the rewritten file must be live: {indexed}"
);
assert!(
indexed.contains("birchfunction"),
"the function body must be searchable: {indexed}"
);
assert!(
indexed.contains("birchdoc-after"),
"a sibling edit in the same pass must still be indexed: {indexed}"
);
assert!(
!indexed.contains("birchbefore"),
"the old source must not stay searchable: {indexed}"
);
assert!(
!indexed.contains("birchdoc-before"),
"the old note must not stay searchable: {indexed}"
);
assert!(
endpoint.data_docs().iter().any(|doc| {
doc.get("code")
.and_then(Value::as_str)
.is_some_and(|code| code.contains("cancel_request"))
}),
"a symbol document must carry the new `code` field: {indexed}"
);
let properties = endpoint
.dataset_properties("incremental-http-docs")
.expect("the docs index mapping must have been installed");
let code_type = properties["code"]["type"]
.as_str()
.expect("code must be a mapped field");
assert!(
code_type == "text" || code_type == "keyword",
"code must be a searchable scalar, got {code_type}"
);
a_rerun_changes_nothing(&config, &endpoint, "function added under watch");
}
/// A type change cannot be applied to a frozen field. Only that file is
/// skipped; the other file in the pass is published, and the printed rebuild
/// command is a real `autoindex` of a new state dir and prefix.
#[test]
fn a_type_change_skips_one_file_and_the_printed_rebuild_indexes_it() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(root.join("good.csv"), "id,value\n2,beta\n").unwrap();
fs::write(root.join("bad.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
assert_eq!(
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap()
.0,
0
);
// The data row keeps a number so the sniffer still treats the first line
// as a header. `id` stays the frozen `long` field and the string is a
// type conflict, not a new `col_1` schema.
fs::write(root.join("good.csv"), "id,value\n2,good-new\n").unwrap();
fs::write(root.join("bad.csv"), "id,value\nnot-a-number,2\n").unwrap();
let burst = crate::watch::ChangeSet::from_paths(&[root.join("good.csv"), root.join("bad.csv")]);
let (outcome, _) = crate::watch::one_pass(&config, &root, &mut carry, &burst);
let (code, summary) = outcome.expect("a type change must not fail the rest of the pass");
assert_eq!(code, 3, "the unmappable file is junk, and junk exits 3");
let skipped = summary.expect("the pass must report the skipped file")["skipped"].clone();
let rendered = skipped.to_string();
let command = incremental_schema_rebuild_command(&config, state_dir.path());
assert!(
rendered.contains("incompatible"),
"the reason must name the type conflict: {rendered}"
);
assert!(
rendered.contains("bad.csv"),
"the reason must name the file: {rendered}"
);
assert!(
rendered.contains(&command),
"the reason must print the rebuild command\nreason: {rendered}\ncommand: {command}"
);
assert!(
rendered.contains("Documents already indexed for this file are dropped"),
"{rendered}"
);
let indexed = joined_docs(&endpoint);
assert!(
indexed.contains("good-new"),
"the sibling file must be published: {indexed}"
);
assert!(
!indexed.contains("not-a-number"),
"the type-changed file must not be published: {indexed}"
);
assert!(
!indexed.contains("alpha"),
"documents already indexed for the skipped file are dropped: {indexed}"
);
let journal_before = fs::read(state_dir.path().join("journal.ndjson")).unwrap();
let rebuild_state = PathBuf::from(format!("{}-reindex", state_dir.path().display()));
fs::create_dir_all(&rebuild_state).unwrap();
let mut rebuild = config.clone();
rebuild.prefix = format!("{}-reindex", config.prefix);
rebuild.state_dir = Some(rebuild_state);
assert!(
matches!(run_index(rebuild).unwrap(), 0 | 3),
"the printed destination must accept a fresh index of the same folder"
);
let rebuilt = joined_docs(&endpoint);
assert!(
rebuilt.contains("not-a-number"),
"the rebuild must index the file the watcher skipped: {rebuilt}"
);
assert_eq!(
fs::read(state_dir.path().join("journal.ndjson")).unwrap(),
journal_before,
"the rebuild uses its own state dir and must not rewrite the watcher journal"
);
}
#[test]
fn a_deleted_files_records_stop_appearing() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(root.join("keep.csv"), "id,value\n1,keep\n").unwrap();
fs::write(root.join("gone.csv"), "id,value\n2,vanishes\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
assert_eq!(paths(&endpoint.data_docs()), vec!["gone.csv", "keep.csv"]);
fs::remove_file(root.join("gone.csv")).unwrap();
let burst = crate::watch::ChangeSet::from_paths(&[root.join("gone.csv")]);
crate::watch::one_pass(&config, &root, &mut carry, &burst)
.0
.unwrap();
assert_eq!(
paths(&endpoint.data_docs()),
vec!["keep.csv"],
"a deleted file's records must stop appearing in search"
);
a_rerun_changes_nothing(&config, &endpoint, "deleted file");
}
#[test]
fn a_deleted_directorys_documents_stop_appearing() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::create_dir_all(root.join("doomed/deeper")).unwrap();
fs::write(root.join("keep.csv"), "id,value\n1,keep\n").unwrap();
fs::write(root.join("doomed/a.csv"), "id,value\n2,doomed-a\n").unwrap();
fs::write(root.join("doomed/deeper/b.csv"), "id,value\n3,doomed-b\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
assert_eq!(
paths(&endpoint.data_docs()),
vec!["doomed/a.csv", "doomed/deeper/b.csv", "keep.csv"]
);
// A whole directory, gone. The watcher is told about the directory; the
// files inside it produce no events of their own.
fs::remove_dir_all(root.join("doomed")).unwrap();
let burst = crate::watch::ChangeSet::from_paths(&[root.join("doomed")]);
crate::watch::one_pass(&config, &root, &mut carry, &burst)
.0
.unwrap();
assert_eq!(
paths(&endpoint.data_docs()),
vec!["keep.csv"],
"every document under a deleted directory must stop appearing in search"
);
a_rerun_changes_nothing(&config, &endpoint, "deleted directory");
}
#[test]
fn an_atomic_save_rename_dance_lands_as_the_new_content() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
let target = root.join("notes.csv");
fs::write(&target, "id,value\n1,before\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
// What an editor actually does: write a sibling temp file, then rename it
// over the target. The inode changes, the size may not.
let temp = root.join("notes.csv.tmp~");
fs::write(&temp, "id,value\n1,after-\n").unwrap();
fs::rename(&temp, &target).unwrap();
let burst = crate::watch::ChangeSet::from_paths(&[temp.clone(), target.clone()]);
crate::watch::one_pass(&config, &root, &mut carry, &burst)
.0
.unwrap();
assert_eq!(
paths(&endpoint.data_docs()),
vec!["notes.csv"],
"the temp file must not survive as a document of its own"
);
a_rerun_changes_nothing(&config, &endpoint, "atomic save");
let (reference, _reference_state) = fresh_full_index(&root);
assert_eq!(published(&endpoint), published(&reference));
}
#[test]
fn a_file_replaced_by_a_directory_converges() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(root.join("thing"), "id,value\n1,a-file\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
fs::remove_file(root.join("thing")).unwrap();
fs::create_dir(root.join("thing")).unwrap();
fs::write(root.join("thing/inner.csv"), "id,value\n2,now-a-dir\n").unwrap();
let burst =
crate::watch::ChangeSet::from_paths(&[root.join("thing"), root.join("thing/inner.csv")]);
crate::watch::one_pass(&config, &root, &mut carry, &burst)
.0
.unwrap();
assert_eq!(paths(&endpoint.data_docs()), vec!["thing/inner.csv"]);
a_rerun_changes_nothing(&config, &endpoint, "file replaced by a directory");
}
#[test]
fn a_moved_directory_converges() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::create_dir(root.join("from")).unwrap();
fs::write(root.join("from/one.csv"), "id,value\n1,one\n").unwrap();
fs::write(root.join("from/two.csv"), "id,value\n2,two\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
fs::rename(root.join("from"), root.join("to")).unwrap();
// A directory rename is ONE event naming the two directories; the files
// inside it produce none.
let burst = crate::watch::ChangeSet::from_paths(&[root.join("from"), root.join("to")]);
crate::watch::one_pass(&config, &root, &mut carry, &burst)
.0
.unwrap();
assert_eq!(
paths(&endpoint.data_docs()),
vec!["to/one.csv", "to/two.csv"],
"a moved directory's documents must follow it"
);
a_rerun_changes_nothing(&config, &endpoint, "moved directory");
}
/// A pass that dies mid-publish must not lose or duplicate documents: the next
/// pass — which, after a restart, has no cache at all and therefore hashes in
/// full — has to arrive at the same index a fresh run would build.
#[test]
fn a_pass_that_fails_mid_publish_converges_on_the_next_one() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::write(root.join("a.csv"), "id,value\n1,alpha\n").unwrap();
fs::write(root.join("b.csv"), "id,value\n2,beta\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full())
.0
.unwrap();
// Change a file, and make the publish of that change fail.
fs::write(root.join("b.csv"), "id,value\n2,beta-two\n").unwrap();
endpoint.state.lock().unwrap().fail_next_data_bulk = true;
let burst = crate::watch::ChangeSet::from_paths(&[root.join("b.csv")]);
let (outcome, _) = crate::watch::one_pass(&config, &root, &mut carry, &burst);
assert!(
outcome.is_err(),
"the injected bulk failure must fail the pass"
);
// The process dies here: a restart starts with an empty cache and a full
// burst, which is exactly what `watch::run` does on its first pass.
let mut restarted = crate::watch::Carry::default();
crate::watch::one_pass(
&config,
&root,
&mut restarted,
&crate::watch::ChangeSet::full(),
)
.0
.unwrap();
let live = published(&endpoint);
let (reference, _reference_state) = fresh_full_index(&root);
assert_eq!(
live,
published(&reference),
"a crash mid-burst must not lose or duplicate documents"
);
let values: Vec<String> = endpoint
.data_docs()
.iter()
.filter_map(|doc| doc.get("value").and_then(Value::as_str))
.map(str::to_owned)
.collect();
assert!(values.iter().any(|v| v == "beta-two"), "{values:?}");
assert!(!values.iter().any(|v| v == "beta"), "{values:?}");
}
/// The property that justifies the feature: any sequence of creates, writes,
/// renames, deletes and recreates, applied one burst at a time, converges on the
/// index a fresh full run would produce.
#[test]
fn watch_converges_under_a_randomised_change_sequence() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
// Fixed seed: a property test that changes shape between runs cannot be
// debugged. Bump it deliberately to explore more sequences.
const SEED: u64 = 0x5745_5443_4831_3237;
let mut rng = Rng(SEED);
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let root = corpus.path().canonicalize().unwrap();
fs::create_dir(root.join("sub")).unwrap();
for i in 0..4 {
fs::write(
root.join(format!("f{i}.csv")),
format!("id,value\n{i},start-{i}\n"),
)
.unwrap();
}
let endpoint = HttpEndpoint::start();
let config = cfg(&root, state_dir.path(), &endpoint.url, false);
let mut carry = crate::watch::Carry::default();
let (outcome, _) =
crate::watch::one_pass(&config, &root, &mut carry, &crate::watch::ChangeSet::full());
assert!(matches!(outcome, Ok((0, _)) | Ok((3, _))), "seed {SEED:#x}");
let mut serial = 0u64;
for step in 0..14u64 {
let mut touched: Vec<PathBuf> = Vec::new();
let live: Vec<PathBuf> = walkdir::WalkDir::new(&root)
.into_iter()
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_file())
.map(|entry| entry.path().to_path_buf())
.collect();
match rng.below(6) {
// create
0 | 1 => {
serial += 1;
let dir = if rng.below(2) == 0 {
root.clone()
} else {
root.join("sub")
};
let path = dir.join(format!("new{serial}.csv"));
fs::write(&path, format!("id,value\n{serial},made-{serial}\n")).unwrap();
touched.push(path);
}
// modify
2 => {
if let Some(path) = live.first() {
serial += 1;
fs::write(path, format!("id,value\n{serial},edited-{serial}\n")).unwrap();
touched.push(path.clone());
}
}
// rename
3 => {
if let Some(path) = live.first() {
serial += 1;
let renamed = root.join(format!("moved{serial}.csv"));
fs::rename(path, &renamed).unwrap();
touched.push(path.clone());
touched.push(renamed);
}
}
// delete
4 => {
if let Some(path) = live.first() {
fs::remove_file(path).unwrap();
touched.push(path.clone());
}
}
// delete and recreate the same path with different content: the
// shape that makes a path-keyed cache wrong.
_ => {
if let Some(path) = live.first() {
serial += 1;
fs::remove_file(path).unwrap();
fs::write(path, format!("id,value\n{serial},reborn-{serial}\n")).unwrap();
touched.push(path.clone());
}
}
}
if touched.is_empty() {
continue;
}
let burst = crate::watch::ChangeSet::from_paths(&touched);
let (outcome, _) = crate::watch::one_pass(&config, &root, &mut carry, &burst);
assert!(
matches!(outcome, Ok((0, _)) | Ok((3, _))),
"seed {SEED:#x} step {step}: pass failed: {:?}",
outcome.err().map(|e| format!("{e:#}"))
);
}
// The property that always holds: a full-hash re-run after the sequence
// changes nothing, so no carried digest ever hid a real change.
a_rerun_changes_nothing(&config, &endpoint, &format!("seed {SEED:#x}"));
let (reference, _reference_state) = fresh_full_index(&root);
let watched = published(&endpoint);
let fresh = published(&reference);
assert_eq!(
watched.keys().collect::<Vec<_>>(),
fresh.keys().collect::<Vec<_>>(),
"seed {SEED:#x}: document ids diverged"
);
assert_eq!(
watched.len(),
fresh.len(),
"seed {SEED:#x}: record count diverged"
);
assert_eq!(watched, fresh, "seed {SEED:#x}: document contents diverged");
}
/// #933: every operation's `Started` journal record must precede its own
/// `Committed` — the durable order that makes a crash mid-window repeat
/// exactly the operations whose accepted apply was not recorded. Holds
/// trivially for the serial loop; the windowed scheduler is what could break
/// it, so it is asserted where the scheduler runs.
fn started_precedes_committed(state_dir: &Path) {
let mut started = std::collections::BTreeSet::new();
let mut committed = std::collections::BTreeSet::new();
for line in fs::read_to_string(state_dir.join("journal.ndjson"))
.unwrap()
.lines()
{
let Ok(event) = serde_json::from_str::<Value>(line) else {
continue;
};
if event.get("kind").and_then(Value::as_str) != Some("sync_operation_state") {
continue;
}
let Some(operation) = event.get("operation_id").and_then(Value::as_str) else {
continue;
};
match event.get("state").and_then(Value::as_str) {
// The journal serializes the state enum lowercase.
Some("started") => {
started.insert(operation.to_owned());
}
Some("committed") => {
committed.insert(operation.to_owned());
}
_ => {}
}
}
let unstarted: Vec<_> = committed.difference(&started).collect();
assert!(
unstarted.is_empty(),
"operations committed without a Started record first: {unstarted:?}"
);
}
/// #933: the `--no-graph` index phase used to apply one file at a time —
/// about 6 files/s on the reporter's 48,533-file corpus, ~3.5 h for the phase
/// alone. The durable replay is now a bounded window (`--workers` wide) over
/// operations that touch disjoint groups. End-to-end, against an endpoint
/// that holds every data bulk for a moment: a serial replay can never have
/// two data bulks inside the endpoint at once, a windowed one must. That
/// count is the whole regression — everything else here is the convergence
/// the change may not disturb.
#[test]
fn no_graph_replay_applies_files_in_parallel_and_converges() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
for file in 0..6 {
let rows: String = (0..5)
.map(|row| format!("{file}-{row},body of row {row} in file {file}\n"))
.collect();
fs::write(
corpus.path().join(format!("file{file}.csv")),
format!("id,body\n{rows}"),
)
.unwrap();
}
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().data_bulk_delay_ms = 200;
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.workers = 4;
assert_eq!(run_index(config).unwrap(), 0);
let state = endpoint.state.lock().unwrap();
assert!(
(2..=4).contains(&state.max_concurrent_data_bulks),
"a 4-wide replay over 200 ms-held data bulks must overlap; max concurrent data \
bulks was {} (1 forever is the fingerprint of the serial index phase this \
fixes)",
state.max_concurrent_data_bulks
);
drop(state);
assert_eq!(
endpoint.data_bulk_requests(),
6,
"one sealed bulk per file, no duplicate sends"
);
let docs = endpoint.data_docs();
assert_eq!(docs.len(), 6 * 5, "every record landed exactly once");
assert_eq!(paths(&docs).len(), 6, "every file's records are searchable");
started_precedes_committed(state_dir.path());
}
/// #933: a bulk failing while other operations are in flight — the state the
/// serial loop never had to think about. The run stops, everything already
/// dispatched is drained and journaled, and the same command resumes to an
/// exact, committed generation: no duplicate documents, no missing file.
#[test]
fn no_graph_parallel_replay_survives_a_bulk_failure_mid_window_and_resumes() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
for file in 0..6 {
let rows: String = (0..5)
.map(|row| format!("{file}-{row},body of row {row} in file {file}\n"))
.collect();
fs::write(
corpus.path().join(format!("file{file}.csv")),
format!("id,body\n{rows}"),
)
.unwrap();
}
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().data_bulk_delay_ms = 60;
endpoint.state.lock().unwrap().fail_data_bulk_number = Some(3);
let mut config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
config.workers = 4;
run_index(config.clone()).expect_err("the third data bulk is refused");
started_precedes_committed(state_dir.path());
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 0);
let committed = journal_events_of_state(state_dir.path(), "committed");
assert!(
(1..6).contains(&committed),
"some in-flight operations drained and committed, not all six: {committed}"
);
// Which file's bulk was refused is schedule-dependent; convergence is not.
assert_eq!(run_index(config).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
let docs = endpoint.data_docs();
assert_eq!(docs.len(), 6 * 5, "every record landed exactly once");
assert_eq!(paths(&docs).len(), 6);
started_precedes_committed(state_dir.path());
}
/// Counts `sync_operation_state` records carrying one specific state, so a
/// test can say how many operations reached `Committed` without caring which.
fn journal_events_of_state(state_dir: &Path, state: &str) -> usize {
fs::read_to_string(state_dir.join("journal.ndjson"))
.unwrap()
.lines()
.filter_map(|line| serde_json::from_str::<Value>(line).ok())
.filter(|event| {
event.get("kind").and_then(Value::as_str) == Some("sync_operation_state")
&& event.get("state").and_then(Value::as_str) == Some(state)
})
.count()
}
/// #1224 scaffolding: one upsert applied through the real executor against
/// the stateful endpoint, with everything hand-built so the only moving part
/// is what [`sync_executor::EsSyncBackend`] sends.
mod delete_gate {
use super::*;
use crate::progress::Progress;
use crate::state::{Plan, PlanDataset};
use crate::sync::{
CommittedManifest, ExecutionIdentity, GenerationManifest, ManifestGroup, ManifestPath,
SourceExecutionPolicy, SyncOperation, SyncOperationKind,
};
use crate::sync_executor::{
EsSyncBackend, PreparedArtifact, SnapshotFile, SnapshotFootprint, SourceSnapshot,
SyncOperationBackend,
};
use std::collections::BTreeMap;
const DS_INDEX: &str = "delete-gate-ds";
struct Fixture {
endpoint: HttpEndpoint,
/// Keeps the prepared artifact alive; dropped with the fixture. The
/// backend holds only a leaked path string, never a borrow of this.
_state_dir: tempfile::TempDir,
backend: EsSyncBackend<'static>,
desired: GenerationManifest,
snapshot: SourceSnapshot,
operation: SyncOperation,
}
fn execution() -> ExecutionIdentity {
ExecutionIdentity {
version: 2,
root_identity: "/corpus".into(),
url: "http://ignored".into(),
prefix: "xc-gate".into(),
follow_symlinks: false,
chunker_identity: "chunker".into(),
embedding_identity_sha256: "a".repeat(64),
embedding_backend: "lexical".into(),
embedding_dimension: None,
embedding_semantic_contract: "none".into(),
embedding_resumable: true,
graph_enabled: false,
brain: "none".into(),
detector_identity: "detector".into(),
schema_identity: "schema".into(),
index_identity: "index".into(),
source_policy: SourceExecutionPolicy::DurableSnapshot {
reference: "ref".into(),
snapshot_digest: "digest".into(),
},
}
}
fn plan() -> Plan {
Plan {
datasets: vec![PlanDataset {
slug: "reports".into(),
index: DS_INDEX.into(),
family: "json".into(),
group: None,
specs: Vec::new(),
time_field: None,
semantic_field: None,
text_analyzer: None,
scope: None,
sampled_records: 1,
file_count: 1,
}],
..Plan::default()
}
}
fn group(content_id: &str) -> ManifestGroup {
ManifestGroup {
group_id: "g1".into(),
content_id: content_id.into(),
content_digest: format!("digest-{content_id}"),
content_size: 10,
canonical: ManifestPath {
path_id: "unix:1".into(),
rel: "a.json".into(),
is_symlink: false,
},
aliases: Vec::new(),
dataset_slugs: vec!["reports".into()],
expected_records: 1,
expected_passages: 1,
expected_vectors: 1,
expected_junk_records: 0,
expected_records_by_dataset: BTreeMap::from([("reports".to_string(), 1)]),
}
}
/// One upsert whose prepared artifact holds one document.
fn fixture(content_id: &str, doc_body: &str) -> Fixture {
let endpoint = HttpEndpoint::start();
{
// The endpoint asserts a bulk never precedes mapping PUTs; this
// fixture tests the delete gate, not provisioning.
let mut st = endpoint.state.lock().unwrap();
st.saw_dataset_mapping_update = true;
st.saw_catalog_mapping_update = true;
}
let es = Es::with_bulk_timeout(&endpoint.url, None, 30).expect("es client");
let state_dir = tempfile::tempdir().unwrap();
let tx = "run-gate-0000-g1";
let ndjson = "prepared/0001.ndjson";
let prepared_dir = state_dir
.path()
.join("sync-snapshots")
.join(tx)
.join("prepared");
fs::create_dir_all(&prepared_dir).unwrap();
fs::write(
state_dir
.path()
.join("sync-snapshots")
.join(tx)
.join(ndjson),
format!("{{\"index\":{{\"_index\":\"{DS_INDEX}\",\"_id\":\"doc-1\"}}}}\n{doc_body}\n"),
)
.unwrap();
let content = group(content_id);
let plan = plan();
let desired = GenerationManifest {
generation: 1,
execution: Some(execution()),
plan: plan.clone(),
groups: vec![content.clone()],
};
let snapshot = SourceSnapshot {
version: 1,
tx_id: tx.into(),
started: "2026-10-07T00:00:00Z".into(),
preparation_contract_digest: "contract".into(),
footprint: SnapshotFootprint {
source_bytes: 10,
prepared_bytes: 10,
total_bytes: 20,
hard_budget_bytes: 1000,
},
files: vec![SnapshotFile {
content_id: content_id.into(),
content_digest: content.content_digest.clone(),
content_size: 10,
relative_blob: "blobs/0001".into(),
prepared: Some(PreparedArtifact {
relative_ndjson: ndjson.to_string(),
records: 1,
passages: 1,
vectors: 1,
junk: 0,
truncated: false,
records_by_dataset: BTreeMap::from([("reports".to_string(), 1)]),
bytes: 10,
digest: "prepared".into(),
}),
prepared_identity: None,
blob_released: false,
}],
snapshot_digest: "snapshot".into(),
};
let operation = SyncOperation {
operation_id: "upsert:g1".into(),
kind: SyncOperationKind::Upsert,
group_id: "g1".into(),
desired_content_id: Some(content_id.into()),
};
// The backend borrows the ES client, the progress surface and the
// state-dir path for 'a. Leak all three for the test's lifetime; the
// TempDir itself stays owned by the fixture so the directory still
// exists while the leaked path points at it.
let es: &'static mut Es = Box::leak(Box::new(es));
let pr: &'static mut Arc<Progress> = Box::leak(Box::new(Progress::silent()));
let dir: &'static Path = Box::leak(state_dir.path().to_path_buf().into_boxed_path());
let backend = EsSyncBackend::new(&*es, dir, 64 * 1024, pr);
Fixture {
endpoint,
_state_dir: state_dir,
backend,
desired,
snapshot,
operation,
}
}
fn delete_requests(endpoint: &HttpEndpoint) -> Vec<String> {
endpoint
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|(method, path)| method == "POST" && path.contains("/_delete_by_query"))
.map(|(_, path)| path.clone())
.collect()
}
/// #1224: an upsert whose target index holds NONE of the group's content
/// must not fire the `?refresh=true` delete_by_query at all. Every upsert
/// used to fire it unconditionally, and a one-record-per-file corpus pays
/// one upsert per FILE — cvelistV5 is 402,744 groups, each a defensive
/// delete that matched nothing and still cost 5-16 s of server refresh
/// under concurrent write load: ~1 op/s measured, ~74 h projected for a
/// corpus the node itself can bulk in minutes.
#[test]
fn upsert_skips_delete_by_query_when_the_index_holds_none_of_the_group() {
let mut f = fixture(
"content-fresh",
r#"{"ax_file":"content-fresh","text":"one"}"#,
);
f.backend
.apply(&f.operation, &base(), &f.desired, &f.snapshot)
.expect("apply");
let deletes = delete_requests(&f.endpoint);
// Copy out of the lock before asserting (#890's lesson: an assert
// under the endpoint lock poisons the mutex the server thread takes).
let (probed, landed) = {
let st = f.endpoint.state.lock().unwrap();
(
st.requests
.iter()
.any(|(m, p)| m == "POST" && *p == format!("/{DS_INDEX}/_search")),
st.docs
.contains_key(&(DS_INDEX.to_string(), "doc-1".into())),
)
};
assert!(
deletes.is_empty(),
"#1224: a first-time group has no partials to clear; the delete_by_query is a \
no-op that still forces a refresh. Sent: {deletes:?}"
);
// The probe IS the contract: the gate asks the index instead of
// assuming, and the document still lands.
assert!(probed, "the gate must ask the index, not assume");
assert!(landed, "the upsert's own document still lands");
}
/// The gate must not swallow the delete when it has real work: an index
/// that already holds documents of this content identity (a retried
/// operation's partial, or a prior generation's record) still gets the
/// delete_by_query before the replay.
#[test]
fn upsert_still_deletes_when_the_index_holds_the_group_content() {
let mut f = fixture("content-live", r#"{"ax_file":"content-live","text":"two"}"#);
f.endpoint.state.lock().unwrap().docs.insert(
(DS_INDEX.to_string(), "stale-partial".into()),
json!({"ax_file": "content-live", "text": "stale"}),
);
f.backend
.apply(&f.operation, &base(), &f.desired, &f.snapshot)
.expect("apply");
let deletes = delete_requests(&f.endpoint);
// TWO requests is the delete loop's contract, not a bug: a pass that
// reports deletions runs again until a pass reports 0, so a delete
// with real work is always followed by its empty confirmation pass.
assert_eq!(
deletes,
vec![
format!("/{DS_INDEX}/_delete_by_query?refresh=true"),
format!("/{DS_INDEX}/_delete_by_query?refresh=true"),
],
"#1224: the gate may skip no-op deletes, never real ones"
);
let st = f.endpoint.state.lock().unwrap();
let (stale_gone, landed) = (
!st.docs
.contains_key(&(DS_INDEX.to_string(), "stale-partial".into())),
st.docs
.contains_key(&(DS_INDEX.to_string(), "doc-1".into())),
);
drop(st);
assert!(stale_gone, "the stale partial is gone");
assert!(landed, "the replayed document landed");
}
/// A cap between the measured `code-and-docs` estimate and the default
/// estimate stops before any data bulk (exit 4). `--plan code-and-docs`
/// is the plan that fits, and the csv it drops is not indexed. The sizes
/// come from `budget::MEASURED`. An empty table fails here: skipping would
/// hide a gate that can no longer tell the plans apart.
#[test]
fn a_budget_too_small_for_the_default_plan_accepts_a_smaller_one() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("main.rs"),
"fn main() { println!(\"budget\"); }\n",
)
.unwrap();
let mut csv = String::from("id,value\n");
for i in 0..2000 {
csv.push_str(&format!("{i},row-{i}-budget-padding\n"));
}
fs::write(corpus.path().join("big.csv"), csv).unwrap();
let (files, _) = walk::walk_reporting_opts(
corpus.path(),
false,
false,
crate::ignore_rules::IgnoreOptions::default(),
)
.unwrap();
let priced = budget::assess(&budget::Request {
root: corpus.path().to_str().unwrap_or("/corpus"),
files: &files,
volume: None,
max_index_bytes: Some(u64::MAX),
no_semantic: true,
plan: None,
approve: None,
duplicate_prefixes: &[],
memory_budget: None,
});
let plan_bytes = |name: &str| -> u64 {
let plan = priced.body["plans"]
.as_array()
.and_then(|plans| plans.iter().find(|plan| plan["name"] == name))
.unwrap_or_else(|| panic!("missing plan {name} in {}", priced.body));
plan["index_bytes"].as_u64().unwrap_or_else(|| {
panic!(
"plan {name} has no measured index size. Fill budget::MEASURED from a real \
xerj run; an unmeasured kind must not be given a guessed ratio. {}",
priced.body
)
})
};
let default_bytes = plan_bytes("default");
let reduced_bytes = plan_bytes("code-and-docs");
assert!(
reduced_bytes < default_bytes,
"code-and-docs ({reduced_bytes}) has to be smaller than default ({default_bytes}) \
so the cap can sit between them"
);
let endpoint = HttpEndpoint::start();
let refused_state = tempfile::tempdir().unwrap();
let mut refused = cfg(corpus.path(), refused_state.path(), &endpoint.url, false);
refused.prefix = "budget-http".into();
refused.max_index_bytes = Some(reduced_bytes);
assert_eq!(run_index(refused).unwrap(), gate::EXIT_NEEDS_DECISION);
assert_eq!(
endpoint.data_bulk_requests(),
0,
"a plan that does not fit must not bulk-index"
);
let accepted_state = tempfile::tempdir().unwrap();
let mut accepted = cfg(corpus.path(), accepted_state.path(), &endpoint.url, false);
accepted.prefix = "budget-http".into();
accepted.max_index_bytes = Some(reduced_bytes);
accepted.budget_plan = Some("code-and-docs".into());
assert_eq!(run_index(accepted).unwrap(), 0);
let indexed = paths(&endpoint.data_docs());
assert!(
indexed.iter().any(|path| path.ends_with("main.rs")),
"the code file is the part the plan keeps: {indexed:?}"
);
assert!(
indexed.iter().all(|path| !path.ends_with("big.csv")),
"the csv is the part the plan drops: {indexed:?}"
);
}
/// A folder that was indexed in full, then re-run under a reduced plan.
///
/// `--plan code-and-docs` (and `--yes`, when it selects that plan) filters
/// the walk before reconcile. The csv is still on disk and already
/// published, but it is absent from the inventory, so the generation's
/// `Delete` operations would remove its documents. The same filter is what
/// the graph path's exclusion sweep would treat as a widened ignore. A
/// reduced plan must not do that unless `--drop-indexed` confirms it.
///
/// The refusal is before the journal is opened for write and before any
/// delete or bulk, so the committed generation and the live documents are
/// unchanged and a later full re-run still reconciles.
#[test]
fn a_reduced_plan_does_not_delete_datasets_already_indexed() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("main.rs"),
"fn main() { println!(\"kept\"); }\n",
)
.unwrap();
fs::write(corpus.path().join("notes.md"), "# notes\n\nstill indexed\n").unwrap();
fs::write(corpus.path().join("rows.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let state_dir = tempfile::tempdir().unwrap();
let mut full = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
full.prefix = "plan-removal".into();
assert_eq!(run_index(full).unwrap(), 0, "the full folder indexes");
let indexed = paths(&endpoint.data_docs());
assert!(
indexed.iter().any(|path| path.ends_with("main.rs")),
"code indexed: {indexed:?}"
);
assert!(
indexed.iter().any(|path| path.ends_with("notes.md")),
"prose indexed: {indexed:?}"
);
assert!(
indexed.iter().any(|path| path.ends_with("rows.csv")),
"the csv dataset indexed: {indexed:?}"
);
let journal_before = fs::read(state_dir.path().join("journal.ndjson")).unwrap();
let snapshots_before = snapshot_names(state_dir.path());
let deletes_before = endpoint
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|(method, path)| method == "POST" && path.contains("/_delete_by_query"))
.count();
let bulks_before = endpoint.data_bulk_requests();
let mut reduced = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
reduced.prefix = "plan-removal".into();
reduced.budget_plan = Some("code-and-docs".into());
assert_eq!(
run_index(reduced).unwrap(),
gate::EXIT_NEEDS_DECISION,
"a reduced plan must stop before it deletes indexed data"
);
assert_eq!(
paths(&endpoint.data_docs()),
indexed,
"the csv dataset is still live"
);
assert_eq!(
fs::read(state_dir.path().join("journal.ndjson")).unwrap(),
journal_before,
"the refusal must not commit a generation"
);
assert_eq!(
snapshot_names(state_dir.path()),
snapshots_before,
"the refusal must not seal or drop a snapshot"
);
let deletes_after = endpoint
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|(method, path)| method == "POST" && path.contains("/_delete_by_query"))
.count();
assert_eq!(
deletes_after, deletes_before,
"the refusal must not delete_by_query"
);
assert_eq!(
endpoint.data_bulk_requests(),
bulks_before,
"the refusal must not bulk"
);
let mut again = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
again.prefix = "plan-removal".into();
assert_eq!(
run_index(again).unwrap(),
0,
"after the refusal a full re-run still reconciles"
);
assert_eq!(
paths(&endpoint.data_docs()),
indexed,
"the full re-run keeps the csv dataset"
);
let mut confirmed = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
confirmed.prefix = "plan-removal".into();
confirmed.budget_plan = Some("code-and-docs".into());
confirmed.drop_indexed = true;
assert_eq!(
run_index(confirmed).unwrap(),
0,
"--drop-indexed is the confirmation that removes the plan's files"
);
let after = paths(&endpoint.data_docs());
assert!(
after.iter().any(|path| path.ends_with("main.rs"))
&& after.iter().any(|path| path.ends_with("notes.md")),
"the plan keeps code and prose: {after:?}"
);
assert!(
after.iter().all(|path| !path.ends_with("rows.csv")),
"the confirmed plan deletes the csv dataset: {after:?}"
);
let mut repeat = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
repeat.prefix = "plan-removal".into();
repeat.budget_plan = Some("code-and-docs".into());
assert_eq!(
run_index(repeat).unwrap(),
0,
"once the csv is gone, the same plan has nothing indexed left to delete"
);
assert_eq!(paths(&endpoint.data_docs()), after);
}
/// The graph path (the default, `no_graph: false`) has no generated
/// manifest; the prior plan is the legacy `plan.json`. A reduced plan
/// would make its exclusion sweep delete the csv documents. Same contract
/// as the `--no-graph` test above: exit 4 and nothing changed, then
/// `--drop-indexed` removes exactly the files the plan leaves out.
#[test]
fn a_reduced_plan_on_the_graph_path_does_not_delete_indexed_data() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
fs::write(
corpus.path().join("main.rs"),
"fn main() { println!(\"kept\"); }\n",
)
.unwrap();
fs::write(corpus.path().join("notes.md"), "# notes\n\nstill indexed\n").unwrap();
fs::write(corpus.path().join("rows.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let state_dir = tempfile::tempdir().unwrap();
let graph_cfg = || {
let mut c = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
c.prefix = "plan-removal-graph".into();
c.no_graph = false;
c
};
assert_eq!(
run_index(graph_cfg()).unwrap(),
0,
"the full folder indexes"
);
let indexed = paths(&endpoint.data_docs());
assert!(
indexed.iter().any(|path| path.ends_with("rows.csv")),
"the csv dataset indexed: {indexed:?}"
);
let deletes = |endpoint: &HttpEndpoint| {
endpoint
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|(method, path)| {
method == "DELETE" || (method == "POST" && path.contains("/_delete_by_query"))
})
.count()
};
let deletes_before = deletes(&endpoint);
let mut reduced = graph_cfg();
reduced.budget_plan = Some("code-and-docs".into());
assert_eq!(
run_index(reduced).unwrap(),
gate::EXIT_NEEDS_DECISION,
"a reduced plan on the graph path must stop before it deletes indexed data"
);
assert_eq!(
paths(&endpoint.data_docs()),
indexed,
"the csv is still live"
);
assert_eq!(
deletes(&endpoint),
deletes_before,
"the refusal must not delete"
);
let mut yes = graph_cfg();
yes.budget_plan = Some("code-and-docs".into());
yes.approve = Some(crate::gate::Approval::Proceed);
assert_eq!(
run_index(yes).unwrap(),
gate::EXIT_NEEDS_DECISION,
"--yes does not agree to the deletion"
);
assert_eq!(paths(&endpoint.data_docs()), indexed);
let mut confirmed = graph_cfg();
confirmed.budget_plan = Some("code-and-docs".into());
confirmed.drop_indexed = true;
assert_eq!(run_index(confirmed).unwrap(), 0, "--drop-indexed confirms");
let after = paths(&endpoint.data_docs());
assert!(
after.iter().any(|path| path.ends_with("main.rs"))
&& after.iter().any(|path| path.ends_with("notes.md")),
"the plan keeps code and prose: {after:?}"
);
assert!(
after.iter().all(|path| !path.ends_with("rows.csv")),
"the confirmed plan removes the csv: {after:?}"
);
// A repeated confirmed exclusion, then excluding everything, must not
// be mistaken for a lost destination merely because the journal keeps
// the old per-file completion records.
let mut repeat = graph_cfg();
repeat.budget_plan = Some("code-and-docs".into());
repeat.drop_indexed = true;
assert_eq!(run_index(repeat).unwrap(), 0);
assert_eq!(paths(&endpoint.data_docs()), after);
fs::write(corpus.path().join(".xerjignore"), "*\n").unwrap();
assert_eq!(run_index(graph_cfg()).unwrap(), 0);
assert!(
paths(&endpoint.data_docs()).is_empty(),
"all source-file records were deliberately excluded: {:?}",
paths(&endpoint.data_docs())
);
}
fn base() -> CommittedManifest {
CommittedManifest {
generation: 0,
manifest_digest: "genesis".into(),
plan: plan(),
groups: Vec::new(),
execution: None,
}
}
}
/// #2314: four identical empty files must not abort on the catalog
/// canonical/alias count. They are junk (no rows), recorded, and a rerun
/// confirms instead of failing the equality that a one-shot read used to
/// trip.
#[test]
fn four_identical_empty_files_do_not_fail_the_catalog_count() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
for name in ["a.txt", "b.txt", "c.txt", "d.txt"] {
fs::write(corpus.path().join(name), "").unwrap();
}
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let result = run_index_report(config.clone());
let message = match &result {
Ok((code, summary)) => format!("code={code} summary={summary:?}"),
Err(error) => format!("{error:#}"),
};
assert!(
!message.contains("catalog canonical/alias count disagrees"),
"{message}"
);
let (code, _) = result.expect("four identical empty files must not abort the run");
assert_eq!(code, 3, "empty files are recorded junk, not a failure");
assert_eq!(run_index(config).unwrap(), 3);
}
/// #2314: the catalog canonical/alias read can come back one document
/// short, the race a folder of identical files hit. The check refreshes
/// and recounts; the run completes and the alias documents stay.
#[test]
fn a_short_catalog_alias_page_is_recounted_instead_of_aborting() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let body = "id,value\n1,same\n";
for name in ["a.csv", "b.csv", "c.csv", "d.csv"] {
fs::write(corpus.path().join(name), body).unwrap();
}
let endpoint = HttpEndpoint::start();
endpoint.state.lock().unwrap().short_catalog_file_key_pages = 1;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config)
.expect("a one-document-short catalog page must be recounted, not abort the generation");
assert_eq!(code, 0, "{summary:?}");
let summary = summary.expect("the generation commits");
assert_eq!(summary["duplicate_files"], 3, "{summary}");
assert_eq!(
endpoint.state.lock().unwrap().short_catalog_file_key_pages,
0,
"the short page must have been consumed by the first count"
);
let refreshes = endpoint
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|(method, path)| {
method == "POST" && path.contains("/_refresh") && path.contains(catalog::CATALOG_INDEX)
})
.count();
assert!(
refreshes >= 1,
"the short count refreshes the catalog before recounting"
);
}
/// #2314: a file that changes size between the hash and the snapshot seal
/// is skipped. The rest of the folder is indexed, the run exits 3, and
/// the next run indexes the file's current bytes.
#[test]
fn a_file_that_changes_size_during_snapshot_seal_is_skipped() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("stable.csv"), "id,value\n1,stable\n").unwrap();
let changing = corpus.path().join("live.csv");
fs::write(&changing, "id,value\n1,before\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let grow = changing.clone();
sync_executor::arm_snapshot_source_hook(state_dir.path(), move || {
let mut file = fs::OpenOptions::new().append(true).open(&grow).unwrap();
file.write_all(b"2,after\n").unwrap();
});
let (code, summary) = run_index_report(config.clone())
.expect("one file changing size during the seal must not abort the run");
assert_eq!(
code, 3,
"the changing file is junk, not a failure: {summary:?}"
);
let indexed = paths(&endpoint.data_docs());
assert!(
indexed.iter().any(|path| path == "stable.csv"),
"{indexed:?}"
);
assert!(
indexed.iter().all(|path| path != "live.csv"),
"the changing file must not be published from a torn read: {indexed:?}"
);
let (again, _) = run_index_report(config).expect("the settled file indexes on the rerun");
assert_eq!(again, 0);
let indexed = paths(&endpoint.data_docs());
assert!(indexed.iter().any(|path| path == "live.csv"), "{indexed:?}");
assert!(
endpoint
.data_docs()
.iter()
.any(|doc| doc.get("value").and_then(Value::as_str) == Some("after")),
"the rerun indexes the bytes the file settled on"
);
}
/// #2577: a `--no-graph` run whose options differ from the committed
/// generation is refused before anything is copied, and the refusal names the
/// option. It used to seal the whole corpus and then report the durable state
/// as "not internally consistent", which it was not: the original options
/// still continue the corpus.
#[test]
fn changed_option_is_refused_before_sealing_and_named() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let mut sample = config.clone();
sample.sample += 1;
let mut symlinks = config.clone();
symlinks.follow_symlinks = true;
let cases = [
(sample, "build fingerprints them differently (--sample"),
(symlinks, "--follow-symlinks was off and is now on"),
];
for (round, (changed, named)) in cases.into_iter().enumerate() {
// Both reconcile arms: an unchanged folder and an edited one.
for edit in [false, true] {
if edit {
fs::write(
corpus.path().join("a.csv"),
format!("id,value\n1,alpha\n2,edit{round}\n"),
)
.unwrap();
}
let snapshots = snapshot_names(state_dir.path());
let bulks = endpoint.data_bulk_requests();
let begins = journal_events(state_dir.path(), "sync_begin");
let error = format!("{:#}", run_index(changed.clone()).unwrap_err());
let context = format!("edit={edit}: {error}");
assert!(
error.contains("execution configuration changed since the committed generation"),
"{context}"
);
assert!(error.contains(named), "{context}");
assert!(error.contains("Nothing was copied"), "{context}");
assert!(!error.contains("not internally consistent"), "{context}");
assert_eq!(snapshot_names(state_dir.path()), snapshots, "{context}");
assert_eq!(endpoint.data_bulk_requests(), bulks, "{context}");
assert_eq!(
journal_events(state_dir.path(), "sync_begin"),
begins,
"{context}"
);
}
// The committed state was never damaged: the original options
// continue it and publish the edit.
let commits = journal_events(state_dir.path(), "sync_commit");
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), commits + 1);
}
assert_eq!(endpoint.data_docs().len(), 2);
}
/// #2684: the `--dry-run` preview of a `--no-graph` rerun must refuse a
/// changed option the way the real run does. It used to print the plan (or
/// "already describes this folder") and exit 0 for a run that the real
/// command then refused, although this route's `--max-minutes` note points
/// callers at `--dry-run` to check a plan first.
#[test]
fn dry_run_refuses_a_changed_option_like_the_real_run() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
assert_eq!(run_index(config.clone()).unwrap(), 0);
let bulks = endpoint.data_bulk_requests();
let snapshots = snapshot_names(state_dir.path());
let journal_before = fs::read(state_dir.path().join("journal.ndjson")).unwrap();
let mut sample = config.clone();
sample.sample += 1;
sample.dry_run = true;
let mut symlinks = config.clone();
symlinks.follow_symlinks = true;
symlinks.dry_run = true;
let cases = [
(sample, "build fingerprints them differently (--sample"),
(symlinks, "--follow-symlinks was off and is now on"),
];
for (changed, named) in cases {
// Both preview arms: an unchanged folder and an edited one.
for edit in [false, true] {
let rows = if edit {
"1,alpha\n2,edit\n"
} else {
"1,alpha\n"
};
fs::write(corpus.path().join("a.csv"), format!("id,value\n{rows}")).unwrap();
let error = format!("{:#}", run_index(changed.clone()).unwrap_err());
let context = format!("edit={edit}: {error}");
assert!(
error.contains("execution configuration changed since the committed generation"),
"{context}"
);
assert!(error.contains(named), "{context}");
assert_eq!(endpoint.data_bulk_requests(), bulks, "{context}");
assert_eq!(snapshot_names(state_dir.path()), snapshots, "{context}");
assert_eq!(
fs::read(state_dir.path().join("journal.ndjson")).unwrap(),
journal_before,
"{context}"
);
}
}
// The preview with the committed options still works on both arms.
let mut dry = config.clone();
dry.dry_run = true;
assert_eq!(run_index(dry.clone()).unwrap(), 0);
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
assert_eq!(run_index(dry).unwrap(), 0);
assert_eq!(endpoint.data_bulk_requests(), bulks);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
}
/// #2720: a workbook rerun whose edit gives it a part the frozen datasets do
/// not have cannot join them. That file is set aside with the rebuild command
/// and every other file is still indexed; it used to abort the whole run with
/// "frozen assignment has 0 matches for group", as if the journal were corrupt.
fn sheet_edit_is_set_aside(edited: &[(&str, Vec<Vec<String>>)], part: &str) {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let book = corpus.path().join("book.xlsx");
let write = |sheets: &[(&str, Vec<Vec<String>>)]| {
let rows: Vec<Vec<Vec<&str>>> = sheets
.iter()
.map(|(_, rows)| {
rows.iter()
.map(|row| row.iter().map(String::as_str).collect())
.collect()
})
.collect();
let slices: Vec<Vec<&[&str]>> = rows
.iter()
.map(|rows| rows.iter().map(Vec::as_slice).collect())
.collect();
let named: Vec<(&str, &[&[&str]])> = sheets
.iter()
.zip(&slices)
.map(|((name, _), rows)| (*name, rows.as_slice()))
.collect();
crate::phase_a_grouping_tests::write_xlsx(&book, &named);
};
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
write(&[("Sales", sales_rows(false))]);
assert_eq!(run_index(config.clone()).unwrap(), 0);
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 1);
write(edited);
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n2,beta\n").unwrap();
let code = run_index(config.clone()).unwrap_or_else(|error| panic!("{error:#}"));
assert_eq!(code, 3, "a set-aside file is completed-with-junk");
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
let catalog = endpoint.catalog_docs();
let junk: Vec<&Value> = catalog
.iter()
.filter(|doc| doc["doc_kind"] == "file" && doc["status"] == "junk")
.collect();
assert_eq!(junk.len(), 1, "{junk:#?}");
assert_eq!(
junk[0]["path"].as_str().or(junk[0]["rel"].as_str()),
Some("book.xlsx")
);
let reason = junk[0]["reason"].as_str().unwrap();
for needle in [
format!("part {part:?} of this file is new or changed shape"),
"This file was not indexed; every other file in this pass still is".to_string(),
"rebuild".to_string(),
] {
assert!(
reason.contains(&needle),
"{needle:?} missing from: {reason}"
);
}
let docs = serde_json::to_string(&endpoint.data_docs()).unwrap();
assert!(docs.contains("beta"), "the edited CSV is still indexed");
assert!(
!docs.contains("region 59"),
"the set-aside workbook's old document is dropped with this generation"
);
}
/// 60 rows of text and numbers with no header: a document, not a table.
fn sales_rows(header: bool) -> Vec<Vec<String>> {
let mut rows = Vec::new();
if header {
rows.push(vec!["region".to_string(), "amount".to_string()]);
}
rows.extend((0..60).map(|i| vec![format!("region {i}"), (i * 10).to_string()]));
rows
}
/// A header row turns the sheet from a document into a table.
#[test]
fn a_sheet_that_changes_shape_is_set_aside_not_fatal() {
sheet_edit_is_set_aside(&[("Sales", sales_rows(true))], "Sales");
}
/// A new table sheet has no frozen dataset either.
#[test]
fn a_new_sheet_in_an_indexed_workbook_is_set_aside_not_fatal() {
let q4 = vec![
vec!["region".to_string(), "amount".to_string()],
vec!["north".to_string(), "5".to_string()],
];
sheet_edit_is_set_aside(&[("Sales", sales_rows(false)), ("Q4", q4)], "Q4");
}
/// #2740: a views folder holds dozens of similar templates. Sixteen or more
/// form an XML dataset of their own (`dataset::MIN_DATASET_RECORDS`), where
/// the strict XML reader junked every one at its first `<%` or unclosed tag
/// and dropped the text after it. Twenty ERB views and twenty HTML partials
/// must index cleanly, end of file included.
#[test]
fn a_folder_of_templates_and_html_fragments_is_indexed_not_junked() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::create_dir_all(corpus.path().join("views")).unwrap();
fs::create_dir_all(corpus.path().join("partials")).unwrap();
for i in 1..=20 {
fs::write(
corpus.path().join(format!("views/view{i}.erb")),
format!(
"<div class=\"profile\">\n <% if @user && @user.admin? %>\n \
<%= link_to \"Admin\", admin_path %>\n <% end %>\n</div>\n\
<p>lateerb{i}</p>\n"
),
)
.unwrap();
fs::write(
corpus.path().join(format!("partials/card{i}.html")),
format!(
"<div class=\"card\" *ngIf=\"items.length > 0 && !loading\">\n \
<input [(ngModel)]=\"query\">\n <span>{{{{ label }}}}</span>\n</div>\n\
<p>latehtml{i}</p>\n"
),
)
.unwrap();
}
let endpoint = HttpEndpoint::start();
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let (code, summary) = run_index_report(config).unwrap();
let summary = summary.expect("the run commits a generation");
assert_eq!(summary["junk_records_total"], 0, "{summary}");
assert_eq!(summary["files_junk"], 0, "{summary}");
assert_eq!(code, 0, "{summary}");
let docs: Vec<String> = endpoint.data_docs().iter().map(Value::to_string).collect();
for needle in ["lateerb7", "latehtml7"] {
assert!(
docs.iter().any(|d| d.contains(needle)),
"{needle} is not in any indexed record"
);
}
}
/// #2807 on the generated (`--no-graph`) path, which `xerj corpus index` uses.
/// That path seals the generation before it sends, so a file the server
/// refuses for size cannot be set aside there. The abort must at least name
/// the file and a way out that works; it used to say only "prepared bulk
/// contained 1 rejected items".
#[test]
fn a_record_refused_for_size_on_the_generated_path_names_its_file() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
let endpoint = HttpEndpoint::start();
endpoint
.state
.lock()
.unwrap()
.refuse_next_data_item_as_too_large = true;
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, false);
let error = format!("{:#}", run_index(config).unwrap_err());
assert!(
error.contains("from a.csv"),
"the file must be named: {error}"
);
assert!(error.contains("16590136 bytes"), "{error}");
assert!(
error.contains("--no-semantic") && error.contains("--exclude"),
"the abort must give a way out that works: {error}"
);
assert!(!error.contains("prepared bulk contained"), "{error}");
}
/// #2807: the server adds the semantic vectors to a record before it checks
/// the per-document limit, so a 2 MB generated `.d.ts` grows past it. The
/// generated (`--no-graph`) path, which `xerj corpus index` uses, seals before
/// it sends, so a refusal there stopped every rerun of the same generation.
/// Preparation now sizes each record as the server will see it and sets the
/// one that would not fit aside as a junk record: the run completes with
/// junk, the file's other records are indexed, and reruns complete too.
#[test]
fn a_record_too_large_once_vectors_are_added_is_set_aside_before_sealing() {
let _guard = HTTP_E2E_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let _replay_guard = sync_executor::REPLAY_FAILPOINT_TEST_LOCK
.lock()
.unwrap_or_else(|p| p.into_inner());
let corpus = tempfile::tempdir().unwrap();
let state_dir = tempfile::tempdir().unwrap();
fs::write(corpus.path().join("a.csv"), "id,value\n1,alpha\n").unwrap();
fs::create_dir(corpus.path().join("src")).unwrap();
let mut generated = String::from("declare namespace Generated {\n");
let mut n = 0;
while generated.len() < 2_000_000 {
generated.push_str(&format!(
" interface Shape{n} {{ id: number; label: string; parent?: Shape{}; tags: string[] }}\n",
n.max(1) - 1
));
n += 1;
}
generated.push_str("}\n");
fs::write(corpus.path().join("src/generated.d.ts"), generated).unwrap();
let endpoint = HttpEndpoint::start();
// Stands in for the server: the whole-file record is about 2.6 MB as
// sent, and the vectors take it past 8 MiB. Every other record is small.
endpoint.state.lock().unwrap().refuse_documents_over = Some(1 << 20);
let config = cfg(corpus.path(), state_dir.path(), &endpoint.url, true);
let (code, summary) =
run_index_report(config.clone()).unwrap_or_else(|error| panic!("{error:#}"));
let summary = summary.expect("the generation commits");
assert_eq!(code, 3, "completed-with-junk: {summary}");
assert_eq!(summary["junk_records_total"], 1, "{summary}");
let docs = serde_json::to_string(&endpoint.data_docs()).unwrap();
assert!(docs.contains("alpha"), "the other file is indexed");
assert!(
docs.contains("Shape1"),
"the file's symbol records are indexed"
);
let file_doc = endpoint
.catalog_docs()
.into_iter()
.find(|doc| doc["doc_kind"] == "file" && doc["path"] == "src/generated.d.ts")
.expect("the file keeps its catalog document");
assert_eq!(file_doc["junk"], 1, "{file_doc}");
// The rerun replays nothing stuck: it completes, and an edit lands.
fs::write(corpus.path().join("a.csv"), "id,value\n1,beta\n").unwrap();
let code = run_index(config).unwrap_or_else(|error| panic!("{error:#}"));
assert_eq!(code, 3);
let docs = serde_json::to_string(&endpoint.data_docs()).unwrap();
assert!(docs.contains("beta"), "the edit is indexed");
assert_eq!(journal_events(state_dir.path(), "sync_commit"), 2);
}