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): #768 route the wrong-node warning through pr.warn, not a bare eprintln

xerj-orgb6ef0fc3c261
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index 23026074d..0be875f20 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -3531,13 +3531,6 @@ pub fn run_index_report(cfg: IndexCfg) -> Result<(i32, Option<Value>)> {
// The very first statement of the function, deliberately: `started` must
// be when this invocation began, not when its summary was built.
let invocation_started = chrono::Utc::now();
- // #768: the wrong-node warning is a safety signal, not progress chatter, so
- // it goes to stderr unconditionally (surviving --quiet, which silences the
- // resource_notes/pr.note surface) and before any endpoint I/O — the same
- // delivery map/status use. It is also mirrored into the --json result below.
- if let Some(note) = &cfg.xerj_url_note {
- eprintln!("autoindex: {note}");
- }
// Fix the phase-A pool width BEFORE anything parallel starts: hashing and
// sniffing are the CPU-bound phase, and they used to take every core no
// matter what the caller asked for (#240 §2).
@@ -3564,6 +3557,16 @@ pub fn run_index_report(cfg: IndexCfg) -> Result<(i32, Option<Value>)> {
.unwrap_or_else(|| progress::default_interval(surface)),
);
let ticker = pr.spawn_ticker();
+ // #768: the wrong-node warning is a safety signal, not routine progress, so
+ // it goes out FIRST and through `pr.warn` — which, unlike `pr.note`, is not
+ // silenced by `--quiet`/`Surface::Silent` (a "you may be writing to the
+ // wrong node" message must still reach the operator) yet stays a well-formed
+ // event on `--progress json` instead of a bare line. It is emitted before
+ // any endpoint I/O below, and mirrored into the --json result. map/status
+ // deliver the same note; they have no progress surface, so they `eprintln`.
+ if let Some(note) = &cfg.xerj_url_note {
+ pr.warn(&format!("autoindex: {note}"));
+ }
// One throughput meter for the whole run: both phase-A routes (the legacy
// scan and the generated route's `project_reconcile_plan`) feed it, so the
// estimate is built from whatever this invocation actually parsed.
diff --git a/engine/crates/xerj-autoindex/src/progress.rs b/engine/crates/xerj-autoindex/src/progress.rs
index 430868e3b..992efd393 100644
--- a/engine/crates/xerj-autoindex/src/progress.rs
+++ b/engine/crates/xerj-autoindex/src/progress.rs
@@ -558,6 +558,33 @@ impl Progress {
}
}
+ /// Like [`Self::note`] but for a safety warning that must not be silenced.
+ /// A "you may be writing to the wrong node" message (#768) still has to reach
+ /// the operator under [`Surface::Silent`] (`--quiet` / `--progress none`),
+ /// where routine notes are dropped, so this does NOT early-return there — it
+ /// writes a plain line to stderr instead. On [`Surface::Json`] it stays a
+ /// well-formed `{"event":"warning",…}` object rather than a bare line, so a
+ /// `--progress json` consumer's one-object-per-line stderr contract holds.
+ pub fn warn(&self, message: &str) {
+ let message = &sanitize(message, SAFE_NOTE_MAX);
+ match self.surface {
+ Surface::Silent | Surface::Plain => self.sink.write(format!("{message}\n").as_bytes()),
+ Surface::Json => {
+ let line = serde_json::json!({"event": "warning", "message": message});
+ self.sink.write(format!("{line}\n").as_bytes());
+ }
+ Surface::Tty => {
+ let mut state = self.state.lock().unwrap();
+ let mut out = clear_tty(state.tty_width);
+ state.tty_width = 0;
+ drop(state);
+ out.push_str(message);
+ out.push('\n');
+ self.sink.write(out.as_bytes());
+ }
+ }
+ }
+
/// Render one progress line now.
pub fn tick(&self) {
self.emit();
@@ -1360,6 +1387,51 @@ mod tests {
String::from_utf8(buffer.lock().unwrap().clone()).unwrap()
}
+ /// #768: a safety warning must reach the operator even under `--quiet`
+ /// (`Surface::Silent`), where a routine `note` is dropped — and on
+ /// `--progress json` it must stay a well-formed event, not a bare line that
+ /// corrupts the one-object-per-line stderr contract.
+ #[test]
+ fn warn_pierces_quiet_and_stays_json_on_the_json_surface() {
+ // Silent: a plain `note` emits nothing, but `warn` still reaches stderr.
+ let (progress, buffer) = Progress::capture(Surface::Silent, Duration::from_secs(3600));
+ progress.note("routine: suppressed under --quiet");
+ assert_eq!(
+ captured(&buffer),
+ "",
+ "a note must stay silent under --quiet"
+ );
+ progress.warn("autoindex: XERJ_URL=http://h:9200 is set but ignored");
+ let out = captured(&buffer);
+ assert!(
+ out.contains("XERJ_URL=http://h:9200 is set but ignored"),
+ "warn must pierce --quiet: {out:?}"
+ );
+ assert!(
+ !out.trim_start().starts_with('{'),
+ "Silent warn is a plain line, not JSON"
+ );
+
+ // Json: `warn` is a well-formed {"event":"warning",...} object, one per
+ // line — never a bare line that would break a --progress json consumer.
+ let (progress, buffer) = Progress::capture(Surface::Json, Duration::from_secs(3600));
+ progress.warn("autoindex: XERJ_URL is set but ignored");
+ let out = captured(&buffer);
+ for line in out.lines().filter(|l| !l.trim().is_empty()) {
+ let v: serde_json::Value = serde_json::from_str(line)
+ .unwrap_or_else(|e| panic!("non-JSON line {line:?}: {e}"));
+ assert_eq!(
+ v.get("event").and_then(|e| e.as_str()),
+ Some("warning"),
+ "{line}"
+ );
+ }
+ assert!(
+ out.contains("XERJ_URL is set but ignored"),
+ "message must survive: {out:?}"
+ );
+ }
+
#[test]
fn auto_is_a_terminal_surface_only_on_a_real_terminal() {
assert_eq!(