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
fix(wal-tap): do not report skipped history as lag on a first look (#320)
xerj-orgb2831c793f3e
diff --git a/engine/crates/xerj-engine/src/wal_tap.rs b/engine/crates/xerj-engine/src/wal_tap.rsindex a400148d3..40bbe6743 100644--- a/engine/crates/xerj-engine/src/wal_tap.rs+++ b/engine/crates/xerj-engine/src/wal_tap.rs@@ -169,8 +169,12 @@ pub struct IndexTapStats {/// are how external versioning absorbs a redelivery. Counted separately so/// they never look like data loss.pub version_conflicts: u64,- /// Times a WAL generation was pruned before the tap read it. Each one is- /// unrecoverable data loss for the target.+ /// Times a poll found a WAL generation pruned before it could be shipped.+ /// Any value above zero means entries the target will never receive.+ ///+ /// Counted per observation, not per lost generation: while a tap is stuck+ /// behind a live gap, each poll re-observes it and this keeps climbing.+ /// That is the useful reading — "still losing data", not "lost data once".pub gaps: u64,/// `UpdateMapping` WAL entries seen. No current xerj code path writes one/// (only read sites exist), so this is expected to stay 0; if it ever@@ -578,10 +582,19 @@ impl WalTap {/// Poll one index once: read its WAL tail, ship it, advance its cursors.///+ /// `head_seq` is the index's highest reserved sequence number, used only to+ /// seed the lag watermark on a first look — see below.+ ////// Returns `true` when there is more to read immediately (the batch was/// truncated), so the caller can keep going without waiting a full poll/// interval.- async fn poll_index(&self, config: &WalTapConfig, name: &str, wal_root: PathBuf) -> bool {+ async fn poll_index(+ &self,+ config: &WalTapConfig,+ name: &str,+ wal_root: PathBuf,+ head_seq: u64,+ ) -> bool {let known: BTreeMap<String, WalTailCursor> = self.state.read()@@ -589,6 +602,7 @@ impl WalTap {.get(name).map(|c| c.shards.clone()).unwrap_or_default();+ let first_look = known.is_empty();let budget = config.max_batch_docs.max(1);let read = {@@ -656,7 +670,7 @@ impl WalTap {if batch.is_empty() {// Nothing to send, but the cursors may still have moved (a fresh- // "start from now" position, or frames that decoded to nothing).+ // start-at-the-end position, or frames that decoded to nothing).if new_cursors != known {self.state.write()@@ -665,6 +679,16 @@ impl WalTap {.or_default().shards = new_cursors;self.persist();+ if first_look {+ // The tap just positioned at the end of an established+ // index: it is caught up by definition, having deliberately+ // skipped the history it cannot backfill. Seeding the+ // watermark keeps `lag_seq` from reporting the whole of+ // that history as a backlog the tap intends to ship.+ self.stat(name, |s| {+ s.last_shipped_seq = s.last_shipped_seq.max(head_seq);+ });+ }}return more;}@@ -800,10 +824,16 @@ impl WalTap {continue;};let wal_root = index.wal_dir();+ // `current_seq_no` is the NEXT seq to be handed out, so the highest+ // reserved one is that minus 1.+ let head_seq = index.current_seq_no().saturating_sub(1);// Keep draining an index that is behind, but bounded: at most a// few batches per tick so one busy index cannot starve the others.for _ in 0..4 {- if !self.poll_index(&config, &name, wal_root.clone()).await {+ if !self+ .poll_index(&config, &name, wal_root.clone(), head_seq)+ .await+ {break;}}diff --git a/engine/crates/xerj-engine/tests/wal_tap_push.rs b/engine/crates/xerj-engine/tests/wal_tap_push.rsindex 29c0460fe..ce2a73586 100644--- a/engine/crates/xerj-engine/tests/wal_tap_push.rs+++ b/engine/crates/xerj-engine/tests/wal_tap_push.rs@@ -540,3 +540,47 @@ async fn recreating_an_index_under_the_same_name_does_not_stall_the_tap() {target.actions());}++/// `_stats` is the honesty surface, so `lag_seq` must not lie in the one case+/// where the naive arithmetic does: a tap that positions at the END of an+/// established index is caught up by definition — it deliberately skipped the+/// history it cannot backfill. Reporting that history as a backlog would send+/// an operator hunting a problem the tap does not have.+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn a_tap_that_started_at_the_end_does_not_report_history_as_lag() {+ let target = StubTarget::start().await;+ let dir = TempDir::new().unwrap();+ // Enabled but allowlisting nothing, so the index accumulates history the+ // tap never saw.+ let engine = engine_with_tap(&dir, tap_config(&target.url, &["nothing-*"]));+ write_docs(&engine, "edge-logs", &["1", "2", "3"]).await;++ // `flush()` force-rotates every WAL shard and prunes the generations whose+ // entries are now durable in a segment, which is exactly the state of an+ // established index: generation 0 is gone, so the "never pruned, read it+ // all" path does not apply and the tap must position at the end.+ let index = engine.get_index("edge-logs").unwrap();+ index.flush().await.unwrap();++ let mut config = engine.wal_tap.config();+ config.indices = vec!["edge-logs".into()];+ engine.wal_tap.set_config(config);+ engine.wal_tap.tick(&engine).await;++ let head = index.current_seq_no().saturating_sub(1);+ assert!(+ head > 0,+ "the index must actually have history for this to mean anything"+ );+ let shipped = engine+ .wal_tap+ .stats()+ .get("edge-logs")+ .map(|s| s.last_shipped_seq)+ .unwrap_or(0);+ assert!(+ shipped >= head,+ "a tap positioned at the end must report zero lag, not the whole \+ history it never intended to ship (head {head}, watermark {shipped})"+ );+}