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
hub: one XERJ_HUB_PUBKEY test lock, restore the deleted cli test; timer home uses USERPROFILE (#1706)
xerj-org8ea52026afae
diff --git a/engine/crates/xerj-hub/src/catalogue.rs b/engine/crates/xerj-hub/src/catalogue.rsindex d23e42672..d67f265be 100644--- a/engine/crates/xerj-hub/src/catalogue.rs+++ b/engine/crates/xerj-hub/src/catalogue.rs@@ -583,15 +583,12 @@ fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> {#[cfg(test)]mod tests {use super::*;+ use crate::test_support::HubPubkeyEnv;use std::io::{Read, Write};- use std::sync::Mutex;use std::time::SystemTime;use xerj_autoindex::harvest::sign::generate_keypair;- /// `XERJ_HUB_PUBKEY` is process-global; serialise the tests that set it.- static ENV_LOCK: Mutex<()> = Mutex::new(());-/// A `.key`/`.pub` keypair for the tests (hex seed, hex public key).fn keypair() -> (String, String) {generate_keypair().unwrap()@@ -833,17 +830,17 @@ mod tests {#[test]fn load_offline_uses_fresh_cache_without_network() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let (bytes, sig, public) = fixture_bytes(fx.path());let cache = tempfile::tempdir().unwrap();seed_cache(cache.path(), &bytes, &sig);- std::env::set_var("XERJ_HUB_PUBKEY", &public);+ env.set(&public);// the hub URL is unreachable on purpose — offline must never dial itlet cat = load_catalogue("http://127.0.0.1:1", cache.path(), true).expect("offline load");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();assert_eq!(cat.corpora.len(), 2);assert_eq!(cat.corpora[0].slug, "alpha");@@ -885,7 +882,7 @@ mod tests {#[test]fn load_rejects_tampered_network_replacement_and_keeps_cache() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let (good_bytes, good_sig, public) = fixture_bytes(fx.path());@@ -899,9 +896,9 @@ mod tests {tampered[good_bytes.len() / 2] ^= 0x01;let (url, handle) = spawn_hub(tampered, good_sig.clone());- std::env::set_var("XERJ_HUB_PUBKEY", &public);+ env.set(&public);let cat = load_catalogue(&url, cache.path(), false).expect("falls back to cached copy");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();handle.join().unwrap();// the signature guarded the replacement: the GOOD cached copy is served,@@ -915,7 +912,7 @@ mod tests {#[test]fn load_accepts_verified_network_replacement_into_empty_cache() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let (good_bytes, good_sig, public) = fixture_bytes(fx.path());@@ -923,9 +920,9 @@ mod tests {let cache = tempfile::tempdir().unwrap();let (url, handle) = spawn_hub(good_bytes.clone(), good_sig.clone());- std::env::set_var("XERJ_HUB_PUBKEY", &public);+ env.set(&public);let cat = load_catalogue(&url, cache.path(), false).expect("verified fetch");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();handle.join().unwrap();assert_eq!(cat.corpora.len(), 2);@@ -936,7 +933,7 @@ mod tests {#[test]fn refresh_ignores_a_young_cache_and_fetches_the_newer_catalogue() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let inputs = two_inputs(fx.path());@@ -955,14 +952,14 @@ mod tests {let new_bytes = canonical_json(&new_cat).unwrap();let (url, handle) = spawn_hub(new_bytes.clone(), new_sig);- std::env::set_var("XERJ_HUB_PUBKEY", &new_public);+ env.set(&new_public);let trusted = load_catalogue(&url, cache.path(), false).expect("young cache is served");assert_eq!(trusted.generated_at, "2026-10-13T00:00:00Z","load_catalogue trusts a young cache without asking the hub");let refreshed = refresh_catalogue(&url, cache.path()).expect("forced refresh");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();handle.join().unwrap();assert_eq!(@@ -975,7 +972,7 @@ mod tests {#[test]fn refresh_reports_a_tampered_catalogue_instead_of_serving_the_cache() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let (good_bytes, good_sig, public) = fixture_bytes(fx.path());let cache = tempfile::tempdir().unwrap();@@ -985,10 +982,10 @@ mod tests {tampered[good_bytes.len() / 2] ^= 0x01;let (url, handle) = spawn_hub(tampered, good_sig.clone());- std::env::set_var("XERJ_HUB_PUBKEY", &public);+ env.set(&public);let err = refresh_catalogue(&url, cache.path()).expect_err("a refresh that cannot be verified is an error, not a stale answer");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();handle.join().unwrap();assert!(@@ -1026,16 +1023,16 @@ mod tests {#[test]fn an_html_page_served_as_the_catalogue_reads_as_unavailable() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();let fx = tempfile::tempdir().unwrap();let (_, _, public) = fixture_bytes(fx.path());let cache = tempfile::tempdir().unwrap();// One GET: the catalogue is refused before the signature is even asked for.let (url, handle) = spawn_html_site(1);- std::env::set_var("XERJ_HUB_PUBKEY", &public);+ env.set(&public);let err = load_catalogue(&url, cache.path(), false).expect_err("HTML is not a catalogue");- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();handle.join().unwrap();let msg = format!("{err:#}");@@ -1049,17 +1046,17 @@ mod tests {#[test]fn load_with_no_key_fetches_nothing_and_caches_nothing() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();// The loader used to fail OPEN here (cache the fetched copy unverified,// with a warning). With no key it now refuses before dialing the hub.- std::env::set_var("XERJ_HUB_PUBKEY", xerj_common::hub::HUB_PUBKEY_NONE);+ env.set(xerj_common::hub::HUB_PUBKEY_NONE);let hub = std::net::TcpListener::bind("127.0.0.1:0").unwrap();hub.set_nonblocking(true).unwrap();let url = format!("http://{}", hub.local_addr().unwrap());let cache = tempfile::tempdir().unwrap();let err = load_catalogue(&url, cache.path(), false).unwrap_err();- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();assert!(format!("{err:#}").contains("no hub public key"),@@ -1074,11 +1071,11 @@ mod tests {#[test]fn the_compiled_in_key_verifies_and_refuses_a_test_signed_catalogue() {- let _guard = ENV_LOCK.lock().unwrap();+ let env = HubPubkeyEnv::lock();// No override: the shipping key (HUB_PUBKEY_HEX) is what verifies. A// catalogue signed by any other key — here a throwaway test key — must be// refused and must not reach the cache.- std::env::remove_var("XERJ_HUB_PUBKEY");+ env.remove();let fx = tempfile::tempdir().unwrap();let (good_bytes, good_sig, _public) = fixture_bytes(fx.path());let cache = tempfile::tempdir().unwrap();diff --git a/engine/crates/xerj-hub/src/cli.rs b/engine/crates/xerj-hub/src/cli.rsindex 663332e14..2a95226ab 100644--- a/engine/crates/xerj-hub/src/cli.rs+++ b/engine/crates/xerj-hub/src/cli.rs@@ -1704,14 +1704,30 @@ mod tests {assert_eq!(run_hub_cli(&strs(&["config"])), 2);}- // NOTE: `hub_pubkey()` reads the process-global `XERJ_HUB_PUBKEY`, which the- // `catalogue` test module already mutates under its own `ENV_LOCK`. A unit- // test here that set/removed that var would race those tests (lib tests share- // one process and run in parallel, and the lock is private to that module).- // The no-key path (`XERJ_HUB_PUBKEY=none`) — its own exit code (5), no `.tmp` leaked- // and no node started — is covered end to end by the `nokey` scenario in- // `tests/pack_roundtrip.rs` instead. Everything below refuses BEFORE the key is- // looked at, so it needs no environment.+ /// Restored from d26ecf43, which deleted it because it wrote+ /// `XERJ_HUB_PUBKEY` without the `catalogue` tests' private lock. It now+ /// holds the crate's one lock ([`crate::test_support::HubPubkeyEnv`]).+ /// The no-key path end to end (exit 5, no `.tmp`, no node) stays covered+ /// by the `nokey` scenario in `tests/pack_roundtrip.rs`.+ #[test]+ fn hub_pubkey_prefers_the_env_override_and_refuses_with_none() {+ let env = crate::test_support::HubPubkeyEnv::lock();++ env.set("abcd");+ assert_eq!(hub_pubkey().unwrap(), "abcd");++ env.set(xerj_common::hub::HUB_PUBKEY_NONE);+ let msg = format!("{:#}", hub_pubkey().unwrap_err());+ assert!(msg.contains("no hub public key"), "{msg}");+ assert!(msg.contains("XERJ_HUB_PUBKEY=none"), "{msg}");++ env.remove();+ if xerj_common::hub::HUB_PUBKEY_HEX.is_empty() {+ assert!(hub_pubkey().is_err(), "a build with no key refuses");+ } else {+ assert_eq!(hub_pubkey().unwrap(), xerj_common::hub::HUB_PUBKEY_HEX);+ }+ }// ── the pull policy lives in the pull ────────────────────────────────────diff --git a/engine/crates/xerj-hub/src/lib.rs b/engine/crates/xerj-hub/src/lib.rsindex dc02d7541..6683811c2 100644--- a/engine/crates/xerj-hub/src/lib.rs+++ b/engine/crates/xerj-hub/src/lib.rs@@ -25,6 +25,8 @@ pub mod pack;pub mod router;pub mod sketch;pub mod sync;+#[cfg(test)]+mod test_support;pub use xerj_common::hub::{render, Answer, AnswerMode, AnsweredFrom, Catalogue, CatalogueEntry, DefRecord, Hit,diff --git a/engine/crates/xerj-hub/src/sync.rs b/engine/crates/xerj-hub/src/sync.rsindex 12d714149..dc570cb18 100644--- a/engine/crates/xerj-hub/src/sync.rs+++ b/engine/crates/xerj-hub/src/sync.rs@@ -863,10 +863,12 @@ pub struct TimerDirs {impl TimerDirs {pub fn from_env(env: &dyn Fn(&str) -> Option<OsString>) -> Result<TimerDirs> {- let home = env("HOME")- .filter(|h| !h.is_empty())- .map(PathBuf::from)- .ok_or_else(|| anyhow!("HOME is not set; cannot place a user timer"))?;+ // One home rule with `hub_root_from` (`HOME`, else `USERPROFILE`), so the+ // timer's paths and the hub home it syncs never come from different+ // variables.+ let home = xerj_common::home::home_dir_from(env).ok_or_else(|| {+ anyhow!("neither HOME nor USERPROFILE is set; cannot place a user timer")+ })?;// XDG says a relative XDG_CONFIG_HOME is invalid and must be ignored.let config = env("XDG_CONFIG_HOME").map(PathBuf::from)diff --git a/engine/crates/xerj-hub/src/sync/tests.rs b/engine/crates/xerj-hub/src/sync/tests.rsindex 501012aac..a83b22175 100644--- a/engine/crates/xerj-hub/src/sync/tests.rs+++ b/engine/crates/xerj-hub/src/sync/tests.rs@@ -1257,9 +1257,27 @@ fn timer_dirs_follow_xdg_config_home_and_ignore_a_relative_one() {TimerDirs::from_env(&env(&[("HOME", "/home/u"), ("XERJ_HUB_HOME", "/srv/hub")])).unwrap();assert_eq!(dirs.hub_home, PathBuf::from("/srv/hub"));+ // HOME unset or empty falls back to USERPROFILE, the same rule as the hub+ // home itself; both paths below come from the one variable.+ for e in [+ env(&[("USERPROFILE", "/Users/u")]),+ env(&[("HOME", ""), ("USERPROFILE", "/Users/u")]),+ ] {+ let dirs = TimerDirs::from_env(&e).unwrap();+ assert_eq!(+ dirs.launch_agents,+ PathBuf::from("/Users/u/Library/LaunchAgents")+ );+ assert_eq!(dirs.hub_home, PathBuf::from("/Users/u/.xerj/hub"));+ }+assert!(TimerDirs::from_env(&env(&[])).is_err(),- "no HOME, no user timer"+ "no HOME and no USERPROFILE, no user timer"+ );+ assert!(+ TimerDirs::from_env(&env(&[("HOME", ""), ("USERPROFILE", "")])).is_err(),+ "empty values count as unset");}diff --git a/engine/crates/xerj-hub/src/test_support.rs b/engine/crates/xerj-hub/src/test_support.rsnew file mode 100644index 000000000..63ca2b7e7--- /dev/null+++ b/engine/crates/xerj-hub/src/test_support.rs@@ -0,0 +1,118 @@+//! Test-only helpers shared by this crate's unit-test modules.+//!+//! The unit tests of every module run in ONE process, on parallel threads, so a+//! process-global environment variable is shared state. `XERJ_HUB_PUBKEY`+//! decides which key verifies catalogues and packs+//! ([`xerj_common::hub::hub_pubkey`]); a test that sets it while another test+//! reads it makes the second one verify against the wrong key. Each module+//! used to keep a private lock, so tests in different modules still raced+//! (a `cli` test was deleted in d26ecf43 for exactly that). [`HubPubkeyEnv`]+//! is the one lock, and `no_other_module_writes_the_hub_pubkey_var` keeps it+//! the only writer.++use std::ffi::{OsStr, OsString};+use std::sync::{Mutex, MutexGuard};++const VAR: &str = "XERJ_HUB_PUBKEY";++static HUB_PUBKEY_LOCK: Mutex<()> = Mutex::new(());++/// Exclusive use of `XERJ_HUB_PUBKEY` for one test. Taking it waits for every+/// other holder; dropping it puts back the value the variable had before, even+/// when the test panics, so one failing test cannot leave a throwaway key+/// behind for the next. A poisoned lock is recovered: the restore on drop is+/// what makes the state safe again, not the poison flag.+pub(crate) struct HubPubkeyEnv {+ prev: Option<OsString>,+ // Dropped after `Drop::drop` has restored the variable.+ _lock: MutexGuard<'static, ()>,+}++impl HubPubkeyEnv {+ pub(crate) fn lock() -> Self {+ let lock = HUB_PUBKEY_LOCK+ .lock()+ .unwrap_or_else(|poison| poison.into_inner());+ HubPubkeyEnv {+ prev: std::env::var_os(VAR),+ _lock: lock,+ }+ }++ /// Set `XERJ_HUB_PUBKEY` for the rest of this hold.+ pub(crate) fn set(&self, value: impl AsRef<OsStr>) {+ std::env::set_var(VAR, value);+ }++ /// Unset `XERJ_HUB_PUBKEY`: the compiled-in key applies.+ pub(crate) fn remove(&self) {+ std::env::remove_var(VAR);+ }+}++impl Drop for HubPubkeyEnv {+ fn drop(&mut self) {+ match self.prev.take() {+ Some(v) => std::env::set_var(VAR, v),+ None => std::env::remove_var(VAR),+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn drop_restores_the_previous_value_even_after_a_panic() {+ let outer = HubPubkeyEnv::lock();+ let orig = outer.prev.clone();+ outer.set("outer");+ // Blocks on the lock until `outer` is dropped, then panics while+ // holding it with a throwaway value set (and poisons the lock).+ let t = std::thread::spawn(|| {+ let inner = HubPubkeyEnv::lock();+ inner.set("inner");+ panic!("a failing test that set {VAR}");+ });+ drop(outer);+ assert!(t.join().is_err());+ // The poisoned lock is recovered, and neither "outer" nor "inner" is+ // left behind: the variable is what it was before either test.+ let after = HubPubkeyEnv::lock();+ assert_eq!(after.prev, orig);+ }++ /// Every write to `XERJ_HUB_PUBKEY` in this crate's sources goes through+ /// [`HubPubkeyEnv`]. A bare `std::env::set_var` elsewhere would race the+ /// tests that hold the lock, which is the bug this module exists to end.+ #[test]+ fn no_other_module_writes_the_hub_pubkey_var() {+ let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");+ let mut stack = vec![src];+ let mut offenders = Vec::new();+ while let Some(dir) = stack.pop() {+ for e in std::fs::read_dir(&dir).unwrap() {+ let p = e.unwrap().path();+ if p.is_dir() {+ stack.push(p);+ continue;+ }+ if p.extension() != Some(OsStr::new("rs")) || p.ends_with("test_support.rs") {+ continue;+ }+ let text = std::fs::read_to_string(&p).unwrap();+ for (n, line) in text.lines().enumerate() {+ let writes = line.contains("set_var(") || line.contains("remove_var(");+ if writes && line.contains(VAR) {+ offenders.push(format!("{}:{}", p.display(), n + 1));+ }+ }+ }+ }+ assert!(+ offenders.is_empty(),+ "write {VAR} through test_support::HubPubkeyEnv: {offenders:?}"+ );+ }+}