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(feedback): scope the catalog sentence to the latest run and keep --agent (#1627)

xerj-org5d3c7a266388
diff --git a/engine/crates/xerj-autoindex/src/feedback.rs b/engine/crates/xerj-autoindex/src/feedback.rs
index 2dd1ad163..328042205 100644
--- a/engine/crates/xerj-autoindex/src/feedback.rs
+++ b/engine/crates/xerj-autoindex/src/feedback.rs
@@ -355,9 +355,17 @@ pub fn slugify(s: &str) -> String {
pub fn pr_commands(d: &Draft) -> String {
let relpath = report_relpath(&d.date, &d.slug);
let branch = format!("field-report/{}", d.slug);
+ // The placeholder was never passed on the command line. Reprinting it
+ // would make the next run look filled in. A real `--agent` has to survive,
+ // or the suggested command silently drops the model that wrote the report.
+ let agent_line = if d.agent == PH_AGENT {
+ String::new()
+ } else {
+ format!(" \\\n --agent {:?}", d.agent)
+ };
format!(
"# Ready-to-run — files ONLY the field report, nothing else:\n\
- xerj feedback \\\n\
+ xerj feedback{agent_line} \\\n\
\x20 --used-for {used_for:?} --pointed-at {pointed_at:?} \\\n\
\x20 --verdict {verdict:?} --numbers {numbers:?} \\\n\
\x20 -o {relpath}\n\
@@ -789,6 +797,108 @@ mod tests {
assert_eq!(d.pointed_at, "my own words");
}
+ #[test]
+ fn the_summary_counts_only_the_latest_runs_datasets() {
+ // Last run recorded 10 records in 2 datasets. Two older datasets from
+ // another run sit on the same catalog. The sentence must not say
+ // "10 records across 4 dataset(s)".
+ let summary = crate::CatalogSummary {
+ run: Some(serde_json::json!({
+ "run_id": "run-b",
+ "records_total": 10,
+ "root": "/repo",
+ })),
+ datasets: vec![
+ serde_json::json!({"run_id": "run-a", "record_count": 1000}),
+ serde_json::json!({"run_id": "run-a", "record_count": 1000}),
+ serde_json::json!({"run_id": "run-b", "record_count": 4}),
+ serde_json::json!({"run_id": "run-b", "record_count": 6}),
+ ],
+ };
+ assert_eq!(
+ summary.one_line().as_deref(),
+ Some("10 records across 2 dataset(s) under /repo (autoindex run run-b)")
+ );
+
+ // Dataset docs written before `run_id` was stored cannot be split by
+ // run, so the whole list is still the only count available.
+ let legacy = crate::CatalogSummary {
+ run: Some(serde_json::json!({
+ "run_id": "run-old",
+ "records_total": 5,
+ })),
+ datasets: vec![
+ serde_json::json!({"record_count": 2}),
+ serde_json::json!({"record_count": 3}),
+ ],
+ };
+ assert_eq!(
+ legacy.one_line().as_deref(),
+ Some("5 records across 2 dataset(s) (autoindex run run-old)")
+ );
+
+ // Tagged docs from a different run are not this run's datasets. The
+ // record total still comes from the run document; the dataset count
+ // is omitted rather than borrowed from the other run.
+ let others = crate::CatalogSummary {
+ run: Some(serde_json::json!({
+ "run_id": "run-new",
+ "records_total": 7,
+ "root": "/x",
+ })),
+ datasets: vec![serde_json::json!({"run_id": "run-old", "record_count": 99})],
+ };
+ assert_eq!(
+ others.one_line().as_deref(),
+ Some("7 records under /x (autoindex run run-new)")
+ );
+
+ // No run document: sum the dataset docs that were actually fetched.
+ let datasets_only = crate::CatalogSummary {
+ run: None,
+ datasets: vec![
+ serde_json::json!({"record_count": 2}),
+ serde_json::json!({"record_count": 3}),
+ ],
+ };
+ assert_eq!(
+ datasets_only.one_line().as_deref(),
+ Some("5 records across 2 dataset(s)")
+ );
+ assert!(crate::CatalogSummary {
+ run: None,
+ datasets: Vec::new(),
+ }
+ .one_line()
+ .is_none());
+ }
+
+ #[test]
+ fn the_rerun_command_keeps_a_real_agent_and_drops_the_placeholder() {
+ let filled = resolve_draft(
+ &Cfg {
+ agent: Some("Claude Code".into()),
+ ..base_cfg()
+ },
+ None,
+ "2026-08-18",
+ );
+ let commands = pr_commands(&filled);
+ assert!(
+ commands.contains("--agent \"Claude Code\""),
+ "a passed --agent must be reprinted, got:\n{commands}"
+ );
+ assert!(commands.contains("xerj feedback \\\n --agent \"Claude Code\""));
+
+ let placeholder = resolve_draft(&base_cfg(), None, "2026-08-18");
+ let commands = pr_commands(&placeholder);
+ assert!(
+ !commands.contains("--agent"),
+ "the placeholder agent must not be reprinted as a flag:\n{commands}"
+ );
+ assert!(!commands.contains(PH_AGENT), "{commands}");
+ }
+
#[test]
fn slug_and_path_are_derived_correctly() {
assert_eq!(
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index 8e85831f1..8c7764766 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -9726,8 +9726,12 @@ fn run_map(cfg: MapCfg) -> Result<i32> {
pub struct CatalogSummary {
/// The most recent `doc_kind: run` document, if any run has been recorded.
pub run: Option<Value>,
- /// Every `doc_kind: dataset` document, most records first — the same order
- /// `run_map` renders them in.
+ /// Dataset documents for the sentence in [`CatalogSummary::one_line`].
+ ///
+ /// When the latest run carries a `run_id` and the catalog has dataset docs
+ /// stamped with that id, this is only those docs (most records first). A
+ /// catalog that predates `run_id` on dataset docs keeps the unscoped list,
+ /// because those docs cannot be told apart from this run.
pub datasets: Vec<Value>,
}
@@ -9737,26 +9741,39 @@ impl CatalogSummary {
/// rather than a sentence that says "0 records"). Never fabricates: every
/// number here comes straight from the catalog the server wrote.
pub fn one_line(&self) -> Option<String> {
- if self.datasets.is_empty() {
+ // The record total and the dataset count have to describe the same
+ // run. Pairing `records_total` from the newest run document with
+ // `datasets.len()` counted every `doc_kind: dataset` doc on the shared
+ // catalog (other prefixes, older runs), so a field report said the
+ // last run's records were spread across the whole node.
+ let scoped = self.datasets_for_latest_run();
+ if scoped.is_empty() && self.datasets.is_empty() {
return None;
}
let get = |v: &Value, k: &str| v.get(k).cloned().unwrap_or(Value::Null);
- // Prefer the run document's own total; fall back to summing datasets so
- // a node whose run doc predates that field still reports a real count.
- let records = self
+ let records_from_run = self
.run
.as_ref()
- .and_then(|r| get(r, "records_total").as_u64())
- .unwrap_or_else(|| {
- self.datasets
+ .and_then(|r| get(r, "records_total").as_u64());
+ let (records, dataset_count) = if scoped.is_empty() {
+ // Dataset docs are present, but none belong to this run. Report
+ // the run document's own total and omit the dataset count. With
+ // no total, say nothing rather than summing the other runs.
+ let records = records_from_run?;
+ (records, None)
+ } else {
+ let records = records_from_run.unwrap_or_else(|| {
+ scoped
.iter()
.filter_map(|d| d.get("record_count").and_then(Value::as_u64))
.sum()
});
- let mut sentence = format!(
- "{records} records across {} dataset(s)",
- self.datasets.len()
- );
+ (records, Some(scoped.len()))
+ };
+ let mut sentence = match dataset_count {
+ Some(n) => format!("{records} records across {n} dataset(s)"),
+ None => format!("{records} records"),
+ };
if let Some(root) = self
.run
.as_ref()
@@ -9777,6 +9794,42 @@ impl CatalogSummary {
}
Some(sentence)
}
+
+ /// Dataset docs that belong to the latest run.
+ ///
+ /// A matching `run_id` wins. When no dataset doc carries that id, untagged
+ /// docs (catalogs written before dataset docs stored `run_id`) are kept,
+ /// and docs tagged with a *different* run are dropped so they cannot inflate
+ /// the count.
+ fn datasets_for_latest_run(&self) -> Vec<&Value> {
+ let run_id = self
+ .run
+ .as_ref()
+ .and_then(|r| r.get("run_id"))
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty());
+ let Some(run_id) = run_id else {
+ return self.datasets.iter().collect();
+ };
+ let matched: Vec<&Value> = self
+ .datasets
+ .iter()
+ .filter(|d| d.get("run_id").and_then(Value::as_str) == Some(run_id))
+ .collect();
+ if !matched.is_empty() {
+ return matched;
+ }
+ let any_other_run = self.datasets.iter().any(|d| {
+ d.get("run_id")
+ .and_then(Value::as_str)
+ .is_some_and(|s| !s.is_empty())
+ });
+ if any_other_run {
+ Vec::new()
+ } else {
+ self.datasets.iter().collect()
+ }
+ }
}
/// Fetch the [`CatalogSummary`] from a running node's `autoindex-catalog`.
@@ -9804,11 +9857,6 @@ pub fn fetch_catalog_summary(url: &str, api_key: Option<String>) -> Result<Catal
})
.unwrap_or_default())
};
- let datasets = fetch(
- json!({"term": {"doc_kind": "dataset"}}),
- 500,
- Some(json!([{"record_count": "desc"}])),
- )?;
let mut runs = fetch(json!({"term": {"doc_kind": "run"}}), 50, None)?;
runs.sort_by_key(|r| {
std::cmp::Reverse(
@@ -9818,10 +9866,36 @@ pub fn fetch_catalog_summary(url: &str, api_key: Option<String>) -> Result<Catal
.to_string(),
)
});
- Ok(CatalogSummary {
- run: runs.into_iter().next(),
- datasets,
- })
+ let run = runs.into_iter().next();
+ let run_id = run
+ .as_ref()
+ .and_then(|r| r.get("run_id"))
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .map(str::to_string);
+ // Scope the dataset query to the latest run so a catalog with more than
+ // the fetch cap of older datasets cannot crowd this run's docs out of the
+ // page. An empty scoped page falls back to the unscoped list: catalogs
+ // written before dataset docs stored `run_id` have nothing to match, and
+ // [`CatalogSummary::one_line`] still refuses to count docs tagged with a
+ // different run.
+ let dataset_sort = Some(json!([{"record_count": "desc"}]));
+ let mut datasets = if let Some(id) = &run_id {
+ fetch(
+ json!({"bool": {"filter": [
+ {"term": {"doc_kind": "dataset"}},
+ {"term": {"run_id": id}},
+ ]}}),
+ 500,
+ dataset_sort.clone(),
+ )?
+ } else {
+ Vec::new()
+ };
+ if datasets.is_empty() {
+ datasets = fetch(json!({"term": {"doc_kind": "dataset"}}), 500, dataset_sort)?;
+ }
+ Ok(CatalogSummary { run, datasets })
}
// ─── status subcommand ───────────────────────────────────────────────────