Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(hub): keep the gRPC Ask routing dir out of the index namespace

xerj-org3192b88bbf25
diff --git a/engine/crates/xerj-api/src/hub_api.rs b/engine/crates/xerj-api/src/hub_api.rs
index b4935abfd..ead261339 100644
--- a/engine/crates/xerj-api/src/hub_api.rs
+++ b/engine/crates/xerj-api/src/hub_api.rs
@@ -282,6 +282,9 @@ pub struct HubShared {
tuning: Tuning,
/// How many times the node-wide mappings were read for `search`.
mapping_reads: AtomicU64,
+ /// The answer flow's empty `cwd`/`hub_home`, made on the first `Ask` that
+ /// needs it and reused after; see [`HubShared::routing_root`].
+ routing_root: OnceLock<PathBuf>,
}
/// The outcome of reading the node-wide mappings for `search`.
@@ -336,6 +339,7 @@ impl HubShared {
search_fields: Mutex::new(None),
tuning,
mapping_reads: AtomicU64::new(0),
+ routing_root: OnceLock::new(),
})
}
@@ -2047,7 +2051,7 @@ pub async fn ask_answer(
args.json = true;
args.max_tokens = ask.max_tokens.map(|m| m as usize);
- let root = routing_root(&shared.app).map_err(|e| {
+ let root = shared.routing_root().map_err(|e| {
tracing::warn!(error = %e, "hub routing directory");
HubRefusal::new(
StatusCode::SERVICE_UNAVAILABLE,
@@ -2113,14 +2117,31 @@ pub async fn ask_answer(
})
}
-/// `<data_dir>/hub-routing`, an empty directory holding only a `.git` marker:
-/// the `cwd` and `hub_home` [`ask_answer`] hands the answer flow. The router
-/// reads lockfiles from `cwd` up to the first `.git`, and the pack cache from
-/// `hub_home/packs`; here there must be neither.
-fn routing_root(app: &AppState) -> std::io::Result<PathBuf> {
- let root = PathBuf::from(&app.config.server.data_dir).join("hub-routing");
- std::fs::create_dir_all(root.join(".git"))?;
- Ok(root)
+/// Where [`HubShared::routing_root`] lives under the data directory.
+///
+/// An index's store is `<data_dir>/<index name>`, so this name must be one no
+/// index can take: [`xerj_common::types::IndexName`] refuses a leading `_`
+/// (a name starts with a lowercase letter or `.`), the same reason the ML
+/// model store is `<data_dir>/_ml`. The old `hub-routing` was a legal index
+/// name, so an `Ask` wrote a `.git` into a user's `hub-routing` index.
+pub const ROUTING_DIR: &str = "_hub-routing";
+
+impl HubShared {
+ /// `<data_dir>/_hub-routing`, an empty directory holding only a `.git`
+ /// marker: the `cwd` and `hub_home` [`ask_answer`] hands the answer flow.
+ /// The router reads lockfiles from `cwd` up to the first `.git`, and the
+ /// pack cache from `hub_home/packs`; here there must be neither.
+ ///
+ /// Made once per node, on the first `Ask`, not on every request. A failed
+ /// attempt is not remembered, so the next `Ask` tries again.
+ fn routing_root(&self) -> std::io::Result<PathBuf> {
+ if let Some(root) = self.routing_root.get() {
+ return Ok(root.clone());
+ }
+ let root = PathBuf::from(&self.app.config.server.data_dir).join(ROUTING_DIR);
+ std::fs::create_dir_all(root.join(".git"))?;
+ Ok(self.routing_root.get_or_init(|| root).clone())
+ }
}
/// The answer flow's [`xerj_hub::ask::Backend`] on a serving node: "the local
diff --git a/engine/crates/xerj-api/tests/hub_api.rs b/engine/crates/xerj-api/tests/hub_api.rs
index 0e18cedc0..49ffbe07b 100644
--- a/engine/crates/xerj-api/tests/hub_api.rs
+++ b/engine/crates/xerj-api/tests/hub_api.rs
@@ -3096,3 +3096,54 @@ fn the_fixture_dir_is_the_shared_one() {
a.join("code-home").to_string_lossy()
);
}
+
+/// `Ask` hands its answer flow an empty directory with a `.git` marker as the
+/// `cwd`/`hub_home`. That directory sits under the data directory, where every
+/// index's store is `<data_dir>/<index name>` — so it must carry a name no
+/// index can take. It used to be `hub-routing`, a legal index name: an `Ask`
+/// wrote `.git` into a user's `hub-routing` index on every request.
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn ask_never_writes_into_a_user_index_named_like_its_routing_dir() {
+ let h = hub(Opts::new(false, false)).await;
+ let data = PathBuf::from(&h.state.config.server.data_dir);
+ // The old name, and the shape of the new one: an index may take the first,
+ // never the second.
+ h.create("hub-routing", docs_mapping()).await;
+ let r = h
+ .admin("PUT", &format!("/{}", hub_api::ROUTING_DIR), None)
+ .await;
+ assert!(
+ !r.status.is_success(),
+ "{} must not be a legal index name: {:?}",
+ hub_api::ROUTING_DIR,
+ r.json()
+ );
+
+ let shared = hub_api::HubShared::new(h.state.clone());
+ let ask = || hub_api::HubAsk {
+ question: "what is the value of MAX_LEN".to_string(),
+ corpus: Some("mini-c".to_string()),
+ mode: None,
+ max_tokens: None,
+ };
+ let _ = hub_api::ask_answer(&shared, ask(), None).await;
+
+ let user = data.join("hub-routing");
+ assert!(user.is_dir(), "the user's index is still there");
+ assert!(
+ !user.join(".git").exists(),
+ "an Ask wrote a .git marker into the user's hub-routing index"
+ );
+ let marker = data.join(hub_api::ROUTING_DIR).join(".git");
+ assert!(marker.is_dir(), "the routing dir is made on the first Ask");
+
+ // Made once, not on every request: a second Ask does not put it back.
+ std::fs::remove_dir(&marker).expect("remove marker");
+ let _ = hub_api::ask_answer(&shared, ask(), None).await;
+ assert!(!marker.exists(), "the routing dir is made once per node");
+ assert!(!user.join(".git").exists());
+
+ // And the user's index still answers.
+ let r = h.admin("GET", "/hub-routing/_count", None).await;
+ assert!(r.status.is_success(), "{:?}", r.json());
+}