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(autoindex): suggest 127.0.0.1 when a localhost node is unreachable (#2688) (#2693)

Cristian Grozaa0d355a91004
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fac84924e..6d7f06204 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `xerj autoindex` and `xerj autoindex map`: when the node is unreachable at a `localhost` URL, the error now also suggests the same URL with `127.0.0.1`, since the node binds the IPv4 loopback by default and `localhost` can resolve to `::1` first (seen on Windows). (#2688)
- The node binds its ES-compat and REST listeners before it opens its
indices, and on plain-HTTP ports answers every request with
`503 node_starting_exception` (with `Retry-After: 1`) until the engine is
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index 76ebce6f3..d8c7ca76b 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -13275,15 +13275,36 @@ fn node_ping_failure(url: &str, err: &anyhow::Error) -> String {
if esclient::Es::ping_failure_is_auth(err) {
format!("the node at {url} answered, but authentication failed.\n{err:#}")
} else {
+ let loopback = loopback_retry_hint(url).unwrap_or_default();
format!(
"no XERJ node reachable at {url} ({err:#})\n\n\
Start one, then re-run this command:\n \
xerj --insecure --data-dir ./.xerj-data &\n\n\
- (or point at an existing node with --url / $XERJ_URL)"
+ (or point at an existing node with --url / $XERJ_URL){loopback}"
)
}
}
+/// #2688 — the client defaults to `http://localhost:9200` while the node binds
+/// `127.0.0.1` by default. Where `localhost` resolves to `::1` first (seen on
+/// Windows) a running node looks absent, so a `localhost` URL gets the same
+/// URL with the IPv4 loopback spelled out. Any other host gets nothing.
+fn loopback_retry_hint(url: &str) -> Option<String> {
+ let mut parsed = reqwest::Url::parse(url).ok()?;
+ if !parsed.host_str()?.eq_ignore_ascii_case("localhost") {
+ return None;
+ }
+ parsed.set_host(Some("127.0.0.1")).ok()?;
+ let mut retry = parsed.to_string();
+ if !url.ends_with('/') && retry.ends_with('/') {
+ retry.pop();
+ }
+ Some(format!(
+ "\n\nIf a node is running, `localhost` may have resolved to an address it does \
+ not listen on (it binds 127.0.0.1 by default). Try:\n --url {retry}"
+ ))
+}
+
#[cfg(test)]
mod node_ping_failure_tests {
use super::node_ping_failure;
@@ -13304,6 +13325,27 @@ mod node_ping_failure_tests {
);
assert!(msg.contains("--url"), "{msg}");
assert!(!msg.contains("authentication failed"), "{msg}");
+ assert!(!msg.contains("may have resolved"), "{msg}");
+ }
+
+ #[test]
+ fn a_refused_localhost_url_suggests_the_ipv4_loopback() {
+ let err = anyhow::anyhow!(
+ "endpoint unreachable: http://localhost:9200: Connection refused (os error 10061)"
+ );
+ let msg = node_ping_failure("http://localhost:9200", &err);
+ assert!(msg.ends_with("--url http://127.0.0.1:9200"), "{msg}");
+ assert!(
+ msg.contains("xerj --insecure --data-dir ./.xerj-data"),
+ "{msg}"
+ );
+ // The caller's scheme, case and port are kept.
+ let msg = node_ping_failure("https://LocalHost:9443", &err);
+ assert!(msg.ends_with("--url https://127.0.0.1:9443"), "{msg}");
+ // An auth failure on localhost is not a resolution problem.
+ let auth = anyhow::anyhow!("GET / failed (HTTP 401)");
+ let msg = node_ping_failure("http://localhost:9200", &auth);
+ assert!(!msg.contains("127.0.0.1"), "{msg}");
}
#[test]