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(api): one cached decide answer per decided text (#1094 follow-up)
xerj-org6f31e9542020
diff --git a/engine/crates/xerj-api/src/systemone_api.rs b/engine/crates/xerj-api/src/systemone_api.rsindex 160122afe..b82f88c7a 100644--- a/engine/crates/xerj-api/src/systemone_api.rs+++ b/engine/crates/xerj-api/src/systemone_api.rs@@ -282,6 +282,21 @@ fn write_back_doc(answer: &CachedAnswer, cfg: &DecisionsConfig, ts: &str) -> ValValue::Object(doc)}+/// The document id one cached answer is written under: a hash of the text+/// that was decided, so each distinct payload holds at most ONE cached answer+/// per index. Since #1094 a low-share history vote escalates to the local+/// head, and the same question asked again can escalate again (its own cached+/// answer is one neighbour among several); appending a fresh document each+/// time would let the local head out-vote the history — human corrections+/// included — by repetition alone. Re-deciding the same text overwrites the+/// earlier cached answer instead. Pure, and unit-tested.+fn write_back_id(text: &str) -> String {+ format!(+ "decide-{:032x}",+ xxhash_rust::xxh3::xxh3_128(text.as_bytes())+ )+}+/// Cache `answers` into the configured decisions index — the flywheel's/// write-back. Spawned and never awaited by the request, so it can neither/// delay nor fail it: every error (index cannot be created or reached,@@ -313,7 +328,10 @@ fn spawn_write_back(state: &AppState, cfg: &DecisionsConfig, answers: Vec<Cachedfor answer in &answers {let ts = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);match idx- .index_document(None, write_back_doc(answer, &cfg, &ts))+ .index_document(+ Some(write_back_id(&answer.text)),+ write_back_doc(answer, &cfg, &ts),+ ).await{Ok(_) => state.metrics.record_doc_indexed(&cfg.index),@@ -2178,6 +2196,17 @@ mod tests {/// configured text/label fields (so it votes like any seeded example) and/// the fixed p / source / ts the issue names. Nothing else — a cached/// answer is NOT marked human, or it would outrank itself forever.+ #[test]+ fn write_back_id_is_one_per_decided_text() {+ let a = write_back_id("refund money back check");+ assert_eq!(a, write_back_id("refund money back check"));+ assert_ne!(a, write_back_id("refund money back check!"));+ assert!(+ a.starts_with("decide-") && a.len() == "decide-".len() + 32,+ "{a}"+ );+ }+#[test]fn write_back_doc_carries_the_flywheel_fields() {let cfg = DecisionsConfig {diff --git a/engine/crates/xerj-api/tests/decide_flywheel.rs b/engine/crates/xerj-api/tests/decide_flywheel.rsindex 421d330fa..1bb9d9ac5 100644--- a/engine/crates/xerj-api/tests/decide_flywheel.rs+++ b/engine/crates/xerj-api/tests/decide_flywheel.rs@@ -352,6 +352,15 @@ mod write_back {/// The same armed node as `systemone_local_decide.rs`: a tiny untrained/// fixture model, `[decisions] index` set to `index`.async fn local_node(index: &str, min_confidence: f64) -> LocalNode {+ local_node_escalating(index, min_confidence, 0.8).await+ }++ /// [`local_node`] with `[decisions] escalate_below` set (#1094).+ async fn local_node_escalating(+ index: &str,+ min_confidence: f64,+ escalate_below: f64,+ ) -> LocalNode {let dir = tempfile::tempdir().expect("tempdir");let model_dir = tempfile::tempdir().expect("model tempdir");xerj_ai::decide::testing::write_fixture(model_dir.path())@@ -362,6 +371,7 @@ mod write_back {config.storage.wal_sync = xerj_common::config::WalSync::Async;config.decisions.index = index.to_string();config.decisions.min_confidence = min_confidence;+ config.decisions.escalate_below = escalate_below;let metrics = xerj_common::metrics::Metrics::new().expect("metrics");let engine = xerj_engine::Engine::new(config.clone()).expect("engine");let mut state = AppState::new(config, engine, metrics);@@ -605,4 +615,70 @@ mod write_back {"/v1/systemone escalation wrote back to history_s1: {docs2:?}");}++ /// #1094 follow-up: a question that keeps escalating (its own cached+ /// answer is one neighbour among split ones) must not append a fresh+ /// cached document every time — repetition alone would then let the+ /// local head out-vote the history. Re-deciding the same text overwrites+ /// its one cached answer. `escalate_below = 1.0` makes every split vote+ /// escalate, so the second request is guaranteed to reach the local head.+ #[tokio::test]+ async fn re_escalating_the_same_question_keeps_one_cached_answer() {+ let ln = local_node_escalating("history", 0.0, 1.0).await;+ let node = &ln.node;+ node.create_index("history").await;+ node.put_doc(+ "history",+ "h_true",+ json!({"text": "refund money back request", "label": "true"}),+ )+ .await;+ node.put_doc(+ "history",+ "h_false",+ json!({"text": "refund inquiry status", "label": "not true"}),+ )+ .await;+ node.refresh("history").await;++ let question = json!({"index": "history", "question": "refund money back check"});+ let (st, r) = node.decide(question.clone()).await;+ assert!(st.is_success(), "{st} {r}");+ assert_eq!(r["tier"], "local", "{r}");+ let docs = node.wait_for_docs("history", 3).await;+ let cached_ts = |docs: &[Value]| -> Vec<String> {+ docs.iter()+ .filter(|d| d["source"] == "local")+ .map(|d| d["ts"].as_str().unwrap_or_default().to_string())+ .collect()+ };+ let first = cached_ts(&docs);+ assert_eq!(first.len(), 1, "one cached answer: {docs:?}");++ // Same question again: still a split vote, so it escalates again.+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;+ let (st, r) = node.decide(question).await;+ assert!(st.is_success(), "{st} {r}");+ assert_eq!(r["tier"], "local", "the re-asked split vote escalates: {r}");+ // Wait for the second write-back to land (its ts differs), then the+ // index must still hold exactly one cached answer for that text.+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);+ loop {+ let docs = node.docs("history").await;+ let ts = cached_ts(&docs);+ assert!(+ ts.len() <= 1,+ "a repeat escalation appended a duplicate: {docs:?}"+ );+ if ts.len() == 1 && ts[0] != first[0] {+ assert_eq!(docs.len(), 3, "{docs:?}");+ break;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "the second write-back never landed: {docs:?}"+ );+ tokio::time::sleep(std::time::Duration::from_millis(25)).await;+ }+ }}