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
feat(api): POST /_ask + xerj_plan MCP tool — prompt in, validated DSL out (#1056)
xerj-org7f1d581516be
diff --git a/engine/crates/xerj-api/src/ask_api.rs b/engine/crates/xerj-api/src/ask_api.rsnew file mode 100644index 000000000..b8c8cfb32--- /dev/null+++ b/engine/crates/xerj-api/src/ask_api.rs@@ -0,0 +1,2499 @@+//! `POST /_ask` — prompt in, validated query DSL out (issue #1056).+//!+//! An agent sends `{index or pattern, prompt}` and gets back+//! `{query, plan[], confidence, indices[]}` where `query` is an ES query+//! clause it can paste straight into `POST /{index}/_search` as+//! `{"query": …}`. The point is tokens: the case study behind the issue+//! measured 9,982 output tokens for retrieval-assisted planning versus+//! 26,477 for hand-written DSL — this endpoint exists so an agent never+//! hand-writes DSL for a structured filter again.+//!+//! # Contract (the issue's hard lines, all enforced here)+//!+//! - **Zero invalid DSL out.** Every response is assembled into a full+//! search body (`{"query": …}`) and passed through+//! [`xerj_query::parse_request`] BEFORE the response is returned. A+//! planner bug surfaces as a 422 naming the planner, never as a 200+//! carrying a query the engine would 400 on.+//! - **Unresolved phrases are 422s naming them.** A value the index does+//! not hold, a field that does not exist, a pattern that matches nothing,+//! a multi-index pattern with no catalog to route by: each is a 422 whose+//! body names the phrase. No silent guess, ever — the negative class in+//! `benchmarks/ask-plan` is exactly this.+//! - **Deterministic.** Same request over the same data → byte-identical+//! response. Every collection is ordered (`Vec`, `BTreeMap`) or reduced+//! under an explicit total order, and the response carries no clock, no+//! timings, no per-run ids (which is why there is no `took_ms`).+//! - **Index routing over `ax-*` by catalog description.** When the pattern+//! matches several indices, each candidate is scored against its+//! `autoindex-catalog` dataset description (slug, field names, examples,+//! notes) and the top scorer wins (ties: lexicographic index name). The+//! catalog is read through the engine's own index APIs — one search of+//! the `autoindex-catalog` index — NOT through a `xerj-autoindex` crate+//! dependency, which this crate deliberately does not have (the constant+//! below mirrors `xerj_autoindex::catalog::CATALOG_INDEX`).+//! - **Field gating by one batched typed call.** The target index's field+//! list comes from ONE `Index::schema()` read (typed), merged with the+//! catalog's per-field specs when a dataset doc exists. No per-field+//! round trips.+//! - **Values from a `terms` agg while cardinality allows it.** ONE batched+//! aggregation fetches the distinct values of every candidate keyword+//! field at once (≤ `TERMS_AGG_SIZE` buckets — the issue's ≤ 1,000 line;+//! catalog `cardinality_est` above that excludes the field from the+//! batch). A value found there is returned in its CANONICAL casing. When+//! a field's distinct values overflow the agg (`sum_other_doc_count > 0`)+//! or the catalog over-estimates it, the value is verified by a BM25+//! `match` probe instead and the clause is a `match` — the "BM25 over+//! distinct values" arm.+//! - **Numeric and date thresholds are never generated.** Every bound in a+//! returned `range` is a literal the prompt supplied (or the day/year of+//! a date literal the prompt supplied). No "last week", no invented+//! buckets: a prompt whose threshold would have to be generated is a 422+//! naming the phrase. `date_min`/`date_max` from catalog specs are the+//! release-time "offer as choices" surface, not an input to planning.+//!+//! # What the planner is (honesty)+//!+//! A deterministic rule-based parser: date literals and year phrases,+//! comparator phrases (`or greater`, `deeper than`, `between … and …`,+//! `exactly`), a unit lexicon (`km`, `K`, `days`, `Earth radii`), a scale+//! lexicon (`million` → ×10⁶) and a concept lexicon (`magnitude` → `mag`,+//! `network` → `net`, `life expectancy` → `lifeExp`, `GDP per capita` →+//! `gdpPercap`, …) resolve field phrases by scored name matching against+//! the index's real fields; equality values resolve against the live value+//! map built by the batched agg. It is NOT an LLM and not fuzzy retrieval:+//! anything it cannot ground in the index is refused by name. `confidence`+//! reports how much soft matching the plan needed — 0.9 for a fully exact+//! plan, lower per fallback, 0.5 for a bare `match_all`.+//!+//! # Authorization posture+//!+//! `/_ask` names its index in the BODY, like `/_decide` and `/_msearch`; the+//! authz middleware classifies the path as a cluster verb and POST is not a+//! cluster read, so a scoped key is refused (fail-closed) unless it holds+//! the general surface — the exact posture of `POST /_decide`. Reads only:+//! this handler executes searches (the value agg, optional BM25 probes) and+//! never writes.+//!+//! # Not claimed here+//!+//! The p50 ≤ 300 ms CPU budget and the harness gates (macro result-set F1+//! ≥ 0.9 over ≥ 200 pairs, agent token parity) are measured by+//! `benchmarks/ask-plan` (PR #1071) at release time — nothing in this file+//! asserts them.++use std::collections::BTreeMap;+use std::mem;+use std::sync::Arc;++use axum::extract::State;+use axum::http::StatusCode;+use axum::response::{IntoResponse, Response};+use axum::Json;+use chrono::Datelike;+use serde_json::{json, Map, Value};+use xerj_engine::index::Index;+use xerj_query::parse_request;++use crate::state::AppState;++/// The autoindex catalog index, read here through the engine's own APIs.+/// Mirrors `xerj_autoindex::catalog::CATALOG_INDEX` — kept as a literal+/// because xerj-api intentionally does not depend on that crate.+const CATALOG_INDEX: &str = "autoindex-catalog";+/// The catalog doc kind that carries a dataset description.+const CATALOG_DATASET_KIND: &str = "dataset";++/// One prompt cannot make the node scan a novel; same philosophy as the+/// rerank stage's ceiling on caller-chosen cost.+const MAX_PROMPT_CHARS: usize = 4_000;+const MAX_PROMPT_TOKENS: usize = 400;+/// The issue's cardinality line: distinct values come from a `terms` agg+/// only while the field has at most this many of them.+const TERMS_AGG_SIZE: usize = 1_000;+/// Bound the batched value agg. Fields are ordered low-cardinality-first so+/// a wide index keeps its most filterable fields inside the batch.+const MAX_VALUE_FIELDS: usize = 24;+/// At most this many BM25 fallback probes per request (the+/// high-cardinality arm). Keeps the worst case one request, not one per+/// phrase.+const MAX_BM25_PROBES: usize = 4;++// ─────────────────────────────────────────────────────────────────────────────+// POST /_ask+// ─────────────────────────────────────────────────────────────────────────────++pub async fn ask(State(state): State<AppState>, Json(body): Json<Value>) -> Response {+ let started = std::time::Instant::now();+ let obj = match body.as_object() {+ Some(o) => o,+ None => return bad_request("`/_ask` body must be a JSON object"),+ };+ let prompt_raw = match obj.get("prompt").and_then(Value::as_str) {+ Some(p) if !p.trim().is_empty() => p.to_string(),+ _ => return bad_request("`prompt` must be a non-empty string"),+ };+ if prompt_raw.len() > MAX_PROMPT_CHARS {+ return bad_request(&format!(+ "`prompt` must be at most {MAX_PROMPT_CHARS} chars (got {})",+ prompt_raw.len()+ ));+ }+ let pattern = match obj.get("index") {+ None | Some(Value::Null) => "ax-*".to_string(),+ Some(Value::String(s)) if !s.trim().is_empty() => s.trim().to_string(),+ Some(_) => return bad_request("`index` must be a string (name or pattern)"),+ };++ // ── 1. Route: expand the pattern, then narrow by catalog description ──+ let mut matched: Vec<String> = state+ .engine+ .list_indices()+ .await+ .iter()+ .filter(|info| pattern.split(',').any(|p| glob_match(p.trim(), &info.name)))+ .map(|info| info.name.clone())+ .collect();+ matched.sort();+ if matched.is_empty() {+ return unresolved(+ std::slice::from_ref(&pattern),+ &format!(+ "`index` pattern `{pattern}` matches no index on this node; pass an index \+ name (or a pattern like `ax-*`) that exists"+ ),+ );+ }++ let mut plan_steps: Vec<Value> = Vec::new();+ let catalog = load_catalog(&state, &matched).await;+ let index = match matched.len() {+ 1 => {+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "route",+ "index": matched[0],+ "how": "explicit-name",+ }));+ matched[0].clone()+ }+ _ => match route_by_description(&matched, &catalog, &prompt_raw) {+ Some((routed, score)) => {+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "route",+ "index": routed,+ "how": "catalog-description-top1",+ "score": (score * 1000.0).round() / 1000.0,+ "candidates": matched.len(),+ }));+ routed+ }+ None => {+ let listed = matched+ .iter()+ .map(|n| format!("`{n}`"))+ .collect::<Vec<_>>()+ .join(", ");+ return unresolved(+ std::slice::from_ref(&pattern),+ &format!(+ "`index` pattern `{pattern}` matches {} indices ({listed}) and this \+ node has no `{CATALOG_INDEX}` dataset description covering them to \+ route by — name the index explicitly",+ matched.len()+ ),+ );+ }+ },+ };++ // ── 2. Fields: ONE batched typed read (schema), merged with catalog ──+ let idx = match state.engine.get_index(&index) {+ Ok(i) => i,+ Err(e) => {+ return unresolved(+ std::slice::from_ref(&index),+ &format!("index `{index}` could not be opened: {e}"),+ )+ }+ };+ let schema = idx.schema().await;+ let mut fields: Vec<FieldView> = schema+ .fields+ .iter()+ .filter(|f| !f.name.starts_with('_'))+ .map(|f| FieldView::from_type(&f.name, &f.field_type))+ .collect();+ fields.sort_by(|a, b| a.name.cmp(&b.name));+ let spec = catalog.as_ref().and_then(|c| c.dataset_for(&index));+ let time_field_hint = spec.and_then(|s| s.time_field.clone());+ if let Some(spec) = &spec {+ for f in fields.iter_mut() {+ if let Some(s) = spec.field_for(&f.name) {+ f.cardinality_est = s.cardinality_est;+ f.examples = s.examples.clone();+ f.in_catalog = true;+ }+ }+ }+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "fields",+ "count": fields.len(),+ "source": if spec.is_some() { "catalog+mapping" } else { "mapping" },+ }));+ if fields.is_empty() {+ return unresolved(+ std::slice::from_ref(&index),+ &format!("index `{index}` declares no fields — `/_ask` cannot ground any phrase in it"),+ );+ }++ // ── 3. Values: ONE batched terms agg over the candidate keyword fields ──+ let mut ctx = PlanCtx {+ values: BTreeMap::new(),+ incomplete: BTreeMap::new(),+ soft: 0,+ constraints: Vec::new(),+ ignored: Vec::new(),+ };+ let value_fields = value_agg_fields(&fields);+ if !value_fields.is_empty() {+ // Catalog over-estimates exclude a field from the batch up front:+ // the issue's "> 1,000 → BM25, not a truncated agg" line.+ for f in fields.iter().filter(|f| value_fields.contains(&f.name)) {+ if f.cardinality_est.unwrap_or(0) > TERMS_AGG_SIZE as u64 {+ ctx.incomplete.insert(f.name.clone(), true);+ }+ }+ let batched: Vec<&String> = value_fields+ .iter()+ .filter(|n| !ctx.incomplete.contains_key(*n))+ .collect();+ if !batched.is_empty() {+ let mut aggs = Map::new();+ for (i, f) in batched.iter().enumerate() {+ aggs.insert(+ format!("v{i}"),+ json!({ "terms": { "field": f, "size": TERMS_AGG_SIZE } }),+ );+ }+ let body = json!({ "size": 0, "aggs": Value::Object(aggs) });+ let req = match parse_request(&body) {+ Ok(r) => r,+ Err(e) => {+ return unresolved(+ std::slice::from_ref(&index),+ &format!("internal value aggregation would not parse: {e}"),+ )+ }+ };+ match idx.search(&req).await {+ Ok(res) => {+ if let Some(aggs_out) = &res.aggs {+ for (i, f) in batched.iter().enumerate() {+ read_value_agg(aggs_out.get(format!("v{i}")), f, &mut ctx);+ }+ }+ }+ Err(e) => {+ return unresolved(+ std::slice::from_ref(&index),+ &format!("value aggregation over `{index}` failed: {e}"),+ )+ }+ }+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "values",+ "via": "terms-agg",+ "fields": batched.len(),+ "size": TERMS_AGG_SIZE,+ }));+ }+ }++ // ── 4. Plan ──+ let tokens = tokenize(&prompt_raw);+ let mut used = vec![false; tokens.len()];+ let outcome = plan_prompt(+ &tokens,+ &fields,+ time_field_hint.as_deref(),+ &mut used,+ &mut ctx,+ &idx,+ )+ .await;+ let (clause, confidence) = match outcome {+ Ok(p) => p,+ Err(r) => return unresolved(&r.0, &r.1),+ };+ for c in ctx.constraints.iter() {+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "constraint",+ "query": c.clause,+ "via": c.via,+ "phrase": c.phrase,+ }));+ }+ for phrase in &ctx.ignored {+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "ignored",+ "phrase": phrase,+ "why": "lowercase modifier with no field or value match; another constraint \+ grounded the prompt",+ }));+ }++ // ── 5. Validate BEFORE returning: zero invalid DSL out ──+ let full = json!({ "query": clause, "size": 1 });+ if let Err(e) = parse_request(&full) {+ // Unreachable by construction — the clause is assembled from typed,+ // field-gated parts. Refuse rather than emit it.+ return unresolved(+ std::slice::from_ref(&prompt_raw),+ &format!(+ "internal planner error: the assembled query failed this node's own parser \+ ({e}); nothing is returned. This is a bug in /_ask — please report it"+ ),+ );+ }+ plan_steps.push(json!({+ "step": plan_steps.len() + 1,+ "action": "validate",+ "parser": "xerj_query::parse_request",+ "passed": true,+ }));++ state+ .metrics+ .record_query(&index, "ask", started.elapsed().as_secs_f64());+ let confidence =+ (confidence - 0.1 * ctx.soft as f64 - 0.05 * ctx.ignored.len() as f64).clamp(0.05, 0.95);+ Json(json!({+ "query": clause,+ "plan": Value::Array(plan_steps),+ "confidence": (confidence * 100.0).round() / 100.0,+ "indices": [index],+ }))+ .into_response()+}++// ─────────────────────────────────────────────────────────────────────────────+// Errors — every one names its cause and never guesses an answer+// ─────────────────────────────────────────────────────────────────────────────++fn bad_request(reason: &str) -> Response {+ (+ StatusCode::BAD_REQUEST,+ Json(json!({ "error": { "type": "illegal_argument", "reason": reason } })),+ )+ .into_response()+}++/// The issue's refusal shape: 422, `unresolved_phrase`, the phrases named.+fn unresolved(phrases: &[String], reason: &str) -> Response {+ (+ StatusCode::UNPROCESSABLE_ENTITY,+ Json(json!({+ "error": {+ "type": "unresolved_phrase",+ "reason": reason,+ "phrases": phrases,+ }+ })),+ )+ .into_response()+}++/// `(phrases to name, full reason)` — a planner refusal.+type Refusal = (Vec<String>, String);++// ─────────────────────────────────────────────────────────────────────────────+// Field views + the catalog+// ─────────────────────────────────────────────────────────────────────────────++/// One index field as the planner sees it: typed (from the schema — the one+/// batched call), optionally enriched by the catalog's `FieldSpec`.+struct FieldView {+ name: String,+ /// The comparison type: keyword/text/long/double/date/none(unusable).+ es_type: &'static str,+ /// The name split into comparison tokens: `pl_orbper` → [pl, orbper],+ /// `lifeExp` → [life, exp], `st_teff` → [st, teff].+ tokens: Vec<String>,+ /// lowercase alphanumeric-only name, the matching key.+ norm: String,+ cardinality_est: Option<u64>,+ examples: Vec<String>,+ in_catalog: bool,+}++impl FieldView {+ fn from_type(name: &str, t: &xerj_common::types::FieldType) -> Self {+ use xerj_common::types::FieldType as F;+ let es_type = match t {+ F::Text => "text",+ F::Keyword => "keyword",+ F::Long => "long",+ F::Double => "double",+ F::Boolean => "boolean",+ F::Date => "date",+ F::Ip => "ip",+ // Not filterable by this planner; kept in the view so the field+ // count in the plan is honest.+ _ => "none",+ };+ Self::new(name.to_string(), es_type)+ }++ fn new(name: String, es_type: &'static str) -> Self {+ let tokens = name_tokens(&name);+ let norm = norm_text(&name);+ Self {+ name,+ tokens,+ norm,+ es_type,+ cardinality_est: None,+ examples: Vec::new(),+ in_catalog: false,+ }+ }++ fn numeric(&self) -> bool {+ matches!(self.es_type, "long" | "double")+ }+ fn date(&self) -> bool {+ self.es_type == "date"+ }+ fn categorical(&self) -> bool {+ self.es_type == "keyword"+ }+}++/// The catalog as `/_ask` reads it: the dataset docs of `autoindex-catalog`+/// whose `index_name` is among the routed candidates.+struct Catalog {+ datasets: Vec<DatasetSpec>,+}++struct DatasetSpec {+ index: String,+ #[allow(dead_code)]+ slug: String,+ /// Every description token (slug, index name, field names, examples,+ /// notes), normed — the routing vocabulary.+ text: Vec<String>,+ fields: Vec<CatalogField>,+ time_field: Option<String>,+}++struct CatalogField {+ name: String,+ cardinality_est: Option<u64>,+ examples: Vec<String>,+}++impl DatasetSpec {+ fn field_for(&self, name: &str) -> Option<&CatalogField> {+ self.fields.iter().find(|f| f.name == name)+ }+}++impl Catalog {+ fn dataset_for(&self, index: &str) -> Option<&DatasetSpec> {+ self.datasets.iter().find(|d| d.index == index)+ }+}++/// Read the catalog through the engine's own index APIs: one search over+/// `autoindex-catalog` for dataset docs naming the candidate indices. A+/// missing catalog index or a failed search is NOT an error — it degrades to+/// mapping-only planning (and multi-index routing refuses instead of+/// guessing).+async fn load_catalog(state: &AppState, candidates: &[String]) -> Option<Catalog> {+ let idx = state.engine.get_index(CATALOG_INDEX).ok()?;+ let body = json!({+ "query": { "term": { "doc_kind": CATALOG_DATASET_KIND } },+ "size": 500,+ "_source": true,+ });+ let req = parse_request(&body).ok()?;+ let res = idx.search(&req).await.ok()?;+ let mut datasets = Vec::new();+ for hit in &res.hits {+ let src = &hit.source;+ let Some(index) = src.get("index_name").and_then(Value::as_str) else {+ continue;+ };+ if !candidates.iter().any(|c| c == index) {+ continue;+ }+ let slug = src+ .get("slug")+ .and_then(Value::as_str)+ .unwrap_or("")+ .to_string();+ let time_field = src+ .get("time_field")+ .and_then(Value::as_str)+ .filter(|s| !s.is_empty())+ .map(str::to_string);+ let mut fields = Vec::new();+ let mut text: Vec<String> = vec![norm_text(&slug), norm_text(index)];+ if let Some(Value::Array(notes)) = src.get("notes") {+ for n in notes {+ if let Some(s) = n.as_str() {+ text.extend(tokenize_normed(s));+ }+ }+ }+ if let Some(Value::String(fj)) = src.get("fields_json") {+ // `fields_json` is a serialized Vec<FieldSpec> (autoindex+ // catalog.rs). Parsed as untyped JSON on purpose: no+ // xerj-autoindex dependency, and the planner only needs three+ // keys of it.+ if let Ok(Value::Array(list)) = serde_json::from_str::<Value>(fj) {+ for f in list {+ let Some(name) = f.get("name").and_then(Value::as_str) else {+ continue;+ };+ let ex = f+ .get("examples")+ .and_then(Value::as_array)+ .map(|a| {+ a.iter()+ .filter_map(Value::as_str)+ .map(str::to_string)+ .collect::<Vec<String>>()+ })+ .unwrap_or_default();+ fields.push(CatalogField {+ name: name.to_string(),+ cardinality_est: f.get("cardinality_est").and_then(Value::as_u64),+ examples: ex.clone(),+ });+ text.push(norm_text(name));+ text.extend(ex.iter().map(|e| norm_text(e)));+ }+ }+ }+ datasets.push(DatasetSpec {+ index: index.to_string(),+ slug,+ text,+ fields,+ time_field,+ });+ }+ datasets.sort_by(|a, b| a.index.cmp(&b.index));+ if datasets.is_empty() {+ None+ } else {+ Some(Catalog { datasets })+ }+}++/// Score every candidate by description-token overlap with the prompt and+/// return the top scorer. `candidates` arrives name-sorted, so on a score+/// tie the lexicographically first wins — deterministic. `None` when no+/// candidate's description covers any prompt token: refuse rather than+/// guess.+fn route_by_description(+ candidates: &[String],+ catalog: &Option<Catalog>,+ prompt: &str,+) -> Option<(String, f64)> {+ let catalog = catalog.as_ref()?;+ let content: Vec<String> = tokenize_normed(prompt)+ .into_iter()+ .filter(|t| t.len() >= 3 && !FUNCTION_WORDS.contains(&t.as_str()))+ .collect::<Vec<_>>();+ if content.is_empty() {+ return None;+ }+ let mut best: Option<(String, f64)> = None;+ for cand in candidates {+ let Some(spec) = catalog.dataset_for(cand) else {+ continue;+ };+ let hits = content+ .iter()+ .filter(|t| spec.text.iter().any(|d| d == *t || d.contains(t.as_str())))+ .count();+ if hits == 0 {+ continue;+ }+ let score = hits as f64 / content.len() as f64;+ if best.as_ref().is_none_or(|(_, s)| score > *s) {+ best = Some((cand.clone(), score));+ }+ }+ best+}++// ─────────────────────────────────────────────────────────────────────────────+// Value map (the terms-agg arm) + the BM25 fallback+// ─────────────────────────────────────────────────────────────────────────────++/// Planner state carried across the passes. Ordered collections only — the+/// determinism contract.+struct PlanCtx {+ /// field → (normed value → canonical value).+ values: BTreeMap<String, BTreeMap<String, Value>>,+ /// Fields whose distinct values did NOT fit the agg (or the catalog+ /// over-estimates them) — the BM25 arm.+ incomplete: BTreeMap<String, bool>,+ /// Count of soft (fallback) resolutions — lowers confidence.+ soft: usize,+ /// Resolved constraints, in prompt order (assembly re-orders eq-first).+ constraints: Vec<Constraint>,+ /// Lowercase decoration tokens ignored, reported in the plan.+ ignored: Vec<String>,+}++/// One resolved constraint.+#[derive(Clone)]+struct Constraint {+ clause: Value,+ phrase: String,+ via: &'static str,+ /// range-shaped (sorts after every eq constraint, matching the gold+ /// ordering: eq clauses in prompt order, then ranges in prompt order).+ is_range: bool,+ /// Prompt token position of the constraint's first token.+ pos: usize,+}++/// Which fields the batched value agg covers: keyword fields ordered by+/// (catalog cardinality ascending, then name) — low cardinality is what+/// makes a field filterable, so a wide index keeps the right ones inside the+/// cap.+fn value_agg_fields(fields: &[FieldView]) -> Vec<String> {+ let mut cands: Vec<&FieldView> = fields.iter().filter(|f| f.categorical()).collect();+ cands.sort_by(|a, b| {+ a.cardinality_est+ .unwrap_or(u64::MAX)+ .cmp(&b.cardinality_est.unwrap_or(u64::MAX))+ .then_with(|| a.name.cmp(&b.name))+ });+ cands+ .into_iter()+ .take(MAX_VALUE_FIELDS)+ .map(|f| f.name.clone())+ .collect()+}++/// Fold one terms-agg result into the value map. Deterministic under bucket+/// ties: a norm collision reduces to the lexicographically smallest+/// canonical value.+fn read_value_agg(agg: Option<&Value>, field: &str, ctx: &mut PlanCtx) {+ let Some(buckets) = agg.and_then(|a| a.get("buckets")).and_then(Value::as_array) else {+ return;+ };+ let other = agg+ .and_then(|a| a.get("sum_other_doc_count"))+ .and_then(Value::as_u64)+ .unwrap_or(0);+ let map = ctx.values.entry(field.to_string()).or_default();+ for b in buckets {+ let Some(key) = b.get("key") else { continue };+ let canon = match key {+ Value::String(s) => Value::String(s.clone()),+ other => other.clone(),+ };+ let norm = norm_value(key);+ let replace = match map.get(&norm) {+ None => true,+ Some(Value::String(prev)) => Some(prev.as_str()) < canon.as_str(),+ Some(_) => false,+ };+ if replace {+ map.insert(norm, canon);+ }+ }+ if other > 0 {+ ctx.incomplete.insert(field.to_string(), true);+ }+}++impl PlanCtx {+ /// Exact value lookup for one field.+ fn lookup_exact(&self, field: &str, phrase_normed: &str) -> Option<Value> {+ self.values.get(field)?.get(phrase_normed).cloned()+ }++ /// Prefix value lookup for one field: the prompt's adjectival form of a+ /// stored value ("European" → "Europe"). Allowed only when the shared+ /// prefix is ≥ 4 chars AND both sides are digit-free — a digit makes a+ /// value an identifier, and "v9999" → "v999" would be a guess about a+ /// DIFFERENT identifier, not a spelling of the same one. Deterministic:+ /// the smallest matching key.+ fn lookup_prefix(&self, field: &str, phrase_normed: &str) -> Option<Value> {+ let has_digit = |s: &str| s.chars().any(|c| c.is_ascii_digit());+ if has_digit(phrase_normed) {+ return None;+ }+ let map = self.values.get(field)?;+ let mut best: Option<(&String, &Value)> = None;+ for (k, v) in map {+ if k.len() >= 4+ && !has_digit(k)+ && phrase_normed.starts_with(k)+ && best.is_none_or(|(bk, _)| k < bk)+ {+ best = Some((k, v));+ }+ }+ best.map(|(_, v)| v.clone())+ }++ /// The field's value map is complete (every distinct value seen).+ fn complete(&self, field: &str) -> bool {+ !self.incomplete.contains_key(field)+ }++ /// BM25 fallback for a field whose values did not fit the agg: one+ /// `match` probe; a hit means the phrase exists in the field and a+ /// `match` clause is honest.+ async fn bm25_probe(&self, idx: &Arc<Index>, field: &str, phrase: &str) -> Option<Value> {+ let body = json!({ "query": { "match": { field: phrase } }, "size": 0 });+ let req = parse_request(&body).ok()?;+ let res = idx.search(&req).await.ok()?;+ (res.total.value > 0).then(|| match_clause(field, phrase))+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// Clause builders (json! cannot take dynamic keys safely; these can)+// ─────────────────────────────────────────────────────────────────────────────++fn term_clause(field: &str, value: Value) -> Value {+ let mut inner = Map::new();+ inner.insert(field.to_string(), value);+ let mut outer = Map::new();+ outer.insert("term".to_string(), Value::Object(inner));+ Value::Object(outer)+}++fn match_clause(field: &str, value: &str) -> Value {+ let mut inner = Map::new();+ inner.insert(field.to_string(), json!(value));+ let mut outer = Map::new();+ outer.insert("match".to_string(), Value::Object(inner));+ Value::Object(outer)+}++fn range_clause_1(field: &str, op: &str, value: Value) -> Value {+ let mut bound = Map::new();+ bound.insert(op.to_string(), value);+ let mut inner = Map::new();+ inner.insert(field.to_string(), Value::Object(bound));+ let mut outer = Map::new();+ outer.insert("range".to_string(), Value::Object(inner));+ Value::Object(outer)+}++fn range_clause_2(field: &str, lo: Value, lo_op: &str, hi: Value, hi_op: &str) -> Value {+ let mut bound = Map::new();+ bound.insert(lo_op.to_string(), lo);+ bound.insert(hi_op.to_string(), hi);+ let mut inner = Map::new();+ inner.insert(field.to_string(), Value::Object(bound));+ let mut outer = Map::new();+ outer.insert("range".to_string(), Value::Object(inner));+ Value::Object(outer)+}++// ─────────────────────────────────────────────────────────────────────────────+// Lexicons+// ─────────────────────────────────────────────────────────────────────────────++/// Words that carry grammar, not content. Never part of a field phrase, a+/// value, or a leftover run. Comparator vocabulary lives in the grammar+/// tables and is consumed there; listing it here too is safe because the+/// comparator passes run first.+const FUNCTION_WORDS: &[&str] = &[+ "the",+ "a",+ "an",+ "all",+ "every",+ "any",+ "each",+ "that",+ "which",+ "who",+ "whose",+ "with",+ "of",+ "for",+ "in",+ "on",+ "at",+ "by",+ "via",+ "from",+ "to",+ "and",+ "or",+ "is",+ "are",+ "was",+ "were",+ "be",+ "been",+ "than",+ "then",+ "there",+ "their",+ "its",+ "it",+ "this",+ "these",+ "those",+ "where",+ "when",+ "during",+ "through",+ "into",+ "reported",+ "discovered",+ "discovery",+ "found",+ "first",+ "observed",+ "happened",+ "occurred",+ "have",+ "has",+ "had",+ "having",+ "did",+ "does",+ "do",+ "please",+ "give",+ "show",+ "list",+ "find",+ "me",+ "only",+ "also",+ "both",+ "utc",+ "gmt",+ "between",+ "greater",+ "less",+ "more",+ "fewer",+ "most",+ "least",+ "exactly",+ "equal",+ "equals",+ "over",+ "under",+ "above",+ "below",+ "up",+ "down",+ "onward",+ "onwards",+ "later",+ "earlier",+ "inclusive",+ "including",+ "strictly",+ "stronger",+ "weaker",+ "deeper",+ "shallower",+ "hotter",+ "colder",+ "cooler",+ "warmer",+ "larger",+ "smaller",+ "bigger",+ "longer",+ "shorter",+ "higher",+ "lower",+ "exceeding",+ "exceeded",+ "exceeds",+ "newer",+ "older",+ "million",+ "billion",+ "thousand",+];++/// Generic head nouns / droppable modifiers. These never name a field or a+/// value; they end a leftover run so the value phrase inside survives.+const GENERIC_WORDS: &[&str] = &[+ "events",+ "event",+ "records",+ "record",+ "rows",+ "row",+ "documents",+ "document",+ "docs",+ "doc",+ "items",+ "item",+ "entries",+ "entry",+ "planets",+ "planet",+ "countries",+ "country",+ "systems",+ "system",+ "quakes",+ "quake",+ "network",+ "seismic",+ "method",+ "type",+ "known",+ "host",+ "hosts",+ "around",+ "orbiting",+ "orbit",+ "about",+ "probe",+];++/// Concept phrases → preferred field-name tokens, longest-first. All+/// matching is on normed (joined, lowercase, alphanumeric) phrases.+const CONCEPTS: &[(&str, &[&str])] = &[+ ("magnitudetype", &["magtype"]),+ ("magnitude", &["mag"]),+ ("depths", &["depth"]),+ ("depth", &["depth"]),+ ("network", &["net"]),+ ("lifeexpectancy", &["lifeexp"]),+ ("lifeexpectancies", &["lifeexp"]),+ ("orbitalperiods", &["orbper"]),+ ("orbitalperiod", &["orbper"]),+ ("periods", &["orbper", "period"]),+ ("period", &["orbper", "period"]),+ ("earthradii", &["rade", "radius"]),+ ("radii", &["rade", "radius"]),+ ("radius", &["rade", "radius"]),+ ("stars", &["snum"]),+ ("star", &["snum"]),+ ("planets", &["pnum"]),+ ("planet", &["pnum"]),+ ("temperature", &["teff", "temp"]),+ ("population", &["pop"]),+ ("gdppercapita", &["gdppercap"]),+ ("year", &["year"]),+ ("years", &["year"]),+ ("continent", &["continent"]),+ ("country", &["country"]),+ ("place", &["place"]),+ ("status", &["status"]),+ ("state", &["state"]),+];++/// Comparative adjectives that imply a quantity on their own.+const COMPARATIVE_CONCEPTS: &[(&str, &str)] = &[+ ("deeper", "depth"),+ ("shallower", "depth"),+ ("hotter", "temperature"),+ ("warmer", "temperature"),+ ("colder", "temperature"),+ ("cooler", "temperature"),+ ("stronger", "magnitude"),+ ("weaker", "magnitude"),+];++/// Unit tokens → preferred field tokens. The unit decides the field when the+/// prompt's field phrase is absent or names the wrong thing ("host stars+/// hotter than 6000 K" is a temperature, not a star count).+const UNITS: &[(&str, &[&str])] = &[+ ("km", &["depth"]),+ ("kilometer", &["depth"]),+ ("kilometers", &["depth"]),+ ("k", &["teff", "temp"]),+ ("kelvin", &["teff", "temp"]),+ ("celsius", &["teff", "temp"]),+ ("days", &["orbper", "period"]),+ ("day", &["orbper", "period"]),+ ("earthradii", &["rade", "radius"]),+ ("radii", &["rade", "radius"]),+];++/// Scale words directly after a number: "100 million" → 10⁸.+const SCALES: &[(&str, i64)] = &[+ ("thousand", 1_000),+ ("million", 1_000_000),+ ("billion", 1_000_000_000),+];++/// op phrases that START at the token AFTER a number (longest first).+const OP_AFTER: &[(&[&str], Op)] = &[+ (&["or", "greater", "than"], Op::Gte),+ (&["or", "greater"], Op::Gte),+ (&["or", "higher"], Op::Gte),+ (&["or", "more"], Op::Gte),+ (&["or", "later"], Op::Gte),+ (&["or", "newer"], Op::Gte),+ (&["or", "above"], Op::Gte),+ (&["and", "above"], Op::Gte),+ (&["and", "up"], Op::Gte),+ (&["onward"], Op::Gte),+ (&["onwards"], Op::Gte),+ (&["or", "less"], Op::Lte),+ (&["or", "lower"], Op::Lte),+ (&["or", "fewer"], Op::Lte),+ (&["or", "earlier"], Op::Lte),+ (&["or", "older"], Op::Lte),+ (&["and", "below"], Op::Lte),+ (&["up", "to", "and", "including"], Op::Lte),+];++/// op phrases that END at the token BEFORE a number (longest first).+const OP_BEFORE: &[(&[&str], Op)] = &[+ (&["up", "to", "and", "including"], Op::Lte),+ (&["at", "least"], Op::Gte),+ (&["at", "most"], Op::Lte),+ (&["greater", "than"], Op::Gt),+ (&["more", "than"], Op::Gt),+ (&["larger", "than"], Op::Gt),+ (&["bigger", "than"], Op::Gt),+ (&["hotter", "than"], Op::Gt),+ (&["warmer", "than"], Op::Gt),+ (&["deeper", "than"], Op::Gt),+ (&["longer", "than"], Op::Gt),+ (&["stronger", "than"], Op::Gt),+ (&["higher", "than"], Op::Gt),+ (&["older", "than"], Op::Gt),+ (&["newer", "than"], Op::Gt),+ (&["less", "than"], Op::Lt),+ // bare "before 2010" / "after 2018" — comparators in their own right+ (&["before"], Op::Lt),+ (&["after"], Op::Gt),+ (&["fewer", "than"], Op::Lt),+ (&["smaller", "than"], Op::Lt),+ (&["shorter", "than"], Op::Lt),+ (&["shallower", "than"], Op::Lt),+ (&["cooler", "than"], Op::Lt),+ (&["colder", "than"], Op::Lt),+ (&["exceeding"], Op::Gt),+ (&["exceeded"], Op::Gt),+ (&["exceeds"], Op::Gt),+ (&["at", "minimum"], Op::Gte),+ (&["at", "maximum"], Op::Lte),+ (&["exactly"], Op::Eq),+ (&["equal", "to"], Op::Eq),+ (&["equals"], Op::Eq),+ (&["over"], Op::Gt),+ (&["above"], Op::Gt),+ (&["under"], Op::Lt),+ (&["below"], Op::Lt),+ (&["beneath"], Op::Lt),+ (&["up", "to"], Op::Lte),+];++/// Connectors that make a bare 4-digit number a year ("discovered in 1992").+const YEAR_CONNECTORS: &[&str] = &[+ "in", "from", "during", "by", "between", "since", "on", "year", "years", "after", "before",+ "until",+];++#[derive(Clone, Copy, PartialEq, Debug)]+enum Op {+ Gt,+ Gte,+ Lt,+ Lte,+ Eq,+}++impl Op {+ fn key(self) -> &'static str {+ match self {+ Op::Gt => "gt",+ Op::Gte => "gte",+ Op::Lt => "lt",+ Op::Lte => "lte",+ Op::Eq => "eq",+ }+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// Tokens+// ─────────────────────────────────────────────────────────────────────────────++/// One prompt token, pre-normalised three ways.+#[derive(Clone, Debug)]+struct Tok {+ raw: String,+ norm: String,+ number: Option<f64>,+ /// Integral literals keep int fidelity ("4" stays 4, not 4.0).+ int_literal: Option<i64>,+ word_number: Option<f64>,+ /// concept implied by a spelled star-count word ("triple" → snum).+ concept: Option<&'static str>,+ capitalized: bool,+}++impl Tok {+ /// Numbers this planner will reason about. Non-finite literals ("1e999")+ /// are deliberately NOT numbers here: no clause may carry a bound the+ /// parser would reject, so they fall through to the leftover pass and are+ /// named in a 422 instead.+ fn is_num(&self) -> bool {+ self.number.is_some_and(|v| v.is_finite()) || self.word_number.is_some()+ }+ fn num_f64(&self) -> Option<f64> {+ self.number.or(self.word_number)+ }+ fn num_value(&self) -> Option<Value> {+ if let Some(i) = self.int_literal {+ return Some(json!(i));+ }+ self.num_f64()+ .and_then(serde_json::Number::from_f64)+ .map(Value::Number)+ }+ fn is_date(&self) -> bool {+ date_parts(&self.raw).is_some()+ }+}++fn tokenize(prompt: &str) -> Vec<Tok> {+ let mut out = Vec::new();+ for w in prompt.split_whitespace() {+ let raw = w+ .trim_matches(|c: char| ".,;:()[]{}\"'!?".contains(c))+ .to_string();+ if raw.is_empty() {+ continue;+ }+ let lower = raw.to_lowercase();+ let norm = norm_text(&raw);+ let number = lower.parse::<f64>().ok();+ let int_literal = lower.parse::<i64>().ok();+ let (word_number, concept) = word_number(&norm);+ let capitalized = raw+ .chars()+ .next()+ .map(|c| {+ c.is_uppercase()+ && raw+ .chars()+ .all(|c| c.is_alphanumeric() || c == '-' || c == '_')+ })+ .unwrap_or(false);+ out.push(Tok {+ raw,+ norm,+ number,+ int_literal,+ word_number,+ concept,+ capitalized,+ });+ if out.len() == MAX_PROMPT_TOKENS {+ break;+ }+ }+ out+}++/// Normed tokens of free text (catalog descriptions, notes).+fn tokenize_normed(s: &str) -> Vec<String> {+ s.split_whitespace()+ .map(norm_text)+ .filter(|t| !t.is_empty())+ .collect()+}++/// lowercase, ascii-folded, alphanumeric only — the comparison key for+/// fields, concepts, and values.+fn norm_text(s: &str) -> String {+ s.chars()+ .filter_map(|c| {+ if c.is_ascii_alphanumeric() {+ Some(c.to_ascii_lowercase())+ } else {+ None+ }+ })+ .collect()+}++fn norm_value(v: &Value) -> String {+ match v {+ Value::String(s) => norm_text(s),+ other => norm_text(&other.to_string()),+ }+}++/// field name → comparison tokens: split on `_`, `-`, `.`, and camelCase+/// (`pl_orbper` → [pl, orbper], `lifeExp` → [life, exp]).+fn name_tokens(name: &str) -> Vec<String> {+ let mut out: Vec<String> = Vec::new();+ let mut cur = String::new();+ let mut prev_lower = false;+ for c in name.chars() {+ if c == '_' || c == '-' || c == '.' {+ if !cur.is_empty() {+ out.push(mem::take(&mut cur));+ }+ prev_lower = false;+ } else if c.is_uppercase() {+ if prev_lower && !cur.is_empty() {+ out.push(mem::take(&mut cur));+ }+ cur.push(c.to_ascii_lowercase());+ prev_lower = false;+ } else {+ cur.push(c);+ prev_lower = c.is_lowercase() || c.is_ascii_digit();+ }+ }+ if !cur.is_empty() {+ out.push(cur);+ }+ out+}++/// Spelled numbers, plus the star-count words astronomy uses ("single",+/// "binary", "triple"… carry their own concept).+fn word_number(w: &str) -> (Option<f64>, Option<&'static str>) {+ // Hyphenated compounds like `triple-star`/`binary-star` norm to+ // `triplestar` and count the same way: the `-star` suffix is the unit+ // the number names.+ let stripped = w+ .strip_suffix("stars")+ .or_else(|| w.strip_suffix("star"))+ .unwrap_or(w);+ match stripped {+ "one" => (Some(1.0), None),+ "single" => (Some(1.0), Some("snum")),+ "two" => (Some(2.0), None),+ "binary" => (Some(2.0), Some("snum")),+ "three" => (Some(3.0), None),+ "triple" => (Some(3.0), Some("snum")),+ "four" => (Some(4.0), None),+ "quadruple" => (Some(4.0), Some("snum")),+ "five" => (Some(5.0), None),+ "quintuple" => (Some(5.0), Some("snum")),+ "six" => (Some(6.0), None),+ "seven" => (Some(7.0), None),+ "eight" => (Some(8.0), None),+ "nine" => (Some(9.0), None),+ "ten" => (Some(10.0), None),+ _ => (None, None),+ }+}++/// `2026-09-01` → a valid (y, m, d).+fn date_parts(word: &str) -> Option<(i32, u32, u32)> {+ let mut it = word.split('-');+ let y: i32 = it.next()?.parse().ok()?;+ let m: u32 = it.next()?.parse().ok()?;+ let d: u32 = it.next()?.parse().ok()?;+ if it.next().is_some() {+ return None;+ }+ chrono::NaiveDate::from_ymd_opt(y, m, d)?;+ Some((y, m, d))+}++fn day_start(y: i32, m: u32, d: u32) -> String {+ format!("{y:04}-{m:02}-{d:02}T00:00:00Z")+}++fn next_day(y: i32, m: u32, d: u32) -> String {+ chrono::NaiveDate::from_ymd_opt(y, m, d)+ .and_then(|date| date.succ_opt())+ .map(|next| day_start(next.year(), next.month(), next.day()))+ .unwrap_or_else(|| day_start(y, m, d))+}++fn year_start(y: i64) -> String {+ format!("{y:04}-01-01T00:00:00Z")+}++fn next_year_start(y: i64) -> String {+ format!("{:04}-01-01T00:00:00Z", y + 1)+}++/// `*` wildcard match (the only wildcard the ES-compat surface promises for+/// index names; multiple stars each match any run, possibly empty —+/// `ax-*-2026-*` is a legal pattern). Everything else is literal. Classic+/// two-pointer backtrack: pure, allocation-free, deterministic.+fn glob_match(pattern: &str, name: &str) -> bool {+ let p: Vec<char> = pattern.chars().collect();+ let s: Vec<char> = name.chars().collect();+ let (mut pi, mut si) = (0usize, 0usize);+ let (mut star, mut mark) = (usize::MAX, 0usize);+ while si < s.len() {+ if pi < p.len() && p[pi] == s[si] {+ pi += 1;+ si += 1;+ } else if pi < p.len() && p[pi] == '*' {+ star = pi;+ mark = si;+ pi += 1;+ } else if star != usize::MAX {+ mark += 1;+ si = mark;+ pi = star + 1;+ } else {+ return false;+ }+ }+ while pi < p.len() && p[pi] == '*' {+ pi += 1;+ }+ pi == p.len()+}++// ─────────────────────────────────────────────────────────────────────────────+// Field-phrase resolution+// ─────────────────────────────────────────────────────────────────────────────++/// Concept lookup over a joined normed phrase → preferred field tokens.+fn concept_for(joined: &str) -> Option<&'static [&'static str]> {+ CONCEPTS+ .iter()+ .find(|(key, _)| {+ joined == *key+ || (joined.len() >= 4 && key.ends_with(joined))+ || (key.len() >= 4 && joined.ends_with(key))+ })+ .map(|(_, targets)| *targets)+}++/// Position (name order) of the first field carrying one of these targets and+/// passing the gate. A target matches a whole name token OR the full normed+/// name — concept tables speak both spellings: `mag` is a token of `mag`, but+/// `lifeexp`/`gdppercap` are the FULL names `lifeExp`/`gdpPercap`, whose+/// comparison tokens split differently ([life, exp], [gdp, percap]).+fn field_by_tokens(+ targets: &[&str],+ fields: &[FieldView],+ gate: impl Fn(&FieldView) -> bool,+) -> Option<usize> {+ fields.iter().position(|f| {+ gate(f)+ && targets+ .iter()+ .any(|t| f.tokens.iter().any(|ft| ft == t) || &f.norm == t)+ })+}++/// Scored name match between a joined normed phrase and one field.+fn name_score(joined: &str, phrase_tokens: &[String], f: &FieldView) -> Option<u32> {+ if joined == f.norm {+ return Some(1000);+ }+ if f.norm.len() >= 3 && joined.starts_with(&f.norm) {+ // mag ⊂ magnitude, pop ⊂ population, gdppercap ⊂ gdppercapita+ return Some(800 + f.norm.len() as u32);+ }+ if joined.len() >= 3 && f.norm.starts_with(joined) {+ return Some(700 + joined.len() as u32);+ }+ let matched = phrase_tokens+ .iter()+ .filter(|p| {+ f.tokens.iter().any(|ft| {+ ft == *p+ || (ft.len() >= 3 && p.starts_with(ft))+ || (p.len() >= 3 && ft.starts_with(p.as_str()))+ })+ })+ .count();+ if matched > 0 && matched == phrase_tokens.len() {+ return Some(600 + matched as u32);+ }+ None+}++/// Resolve a field phrase (token indexes) under a gate: concept table first,+/// then scored name matching. Ties resolve to the first field in name order+/// (fields arrive sorted) — deterministic.+fn resolve_field(+ toks: &[Tok],+ idxs: &[usize],+ fields: &[FieldView],+ gate: impl Fn(&FieldView) -> bool + Copy,+) -> Option<usize> {+ let phrase_tokens: Vec<String> = idxs.iter().map(|&i| toks[i].norm.clone()).collect();+ let joined = phrase_tokens.concat();+ if joined.is_empty() {+ return None;+ }+ if let Some(targets) = concept_for(&joined) {+ if let Some(pos) = fields+ .iter()+ .position(|f| gate(f) && targets.iter().any(|t| f.tokens.iter().any(|ft| ft == t)))+ {+ return Some(pos);+ }+ }+ let mut best: Option<(u32, usize)> = None;+ for (pos, f) in fields.iter().enumerate() {+ if !gate(f) {+ continue;+ }+ if let Some(score) = name_score(&joined, &phrase_tokens, f) {+ if best.is_none_or(|(bs, _)| score > bs) {+ best = Some((score, pos));+ }+ }+ }+ best.map(|(_, pos)| pos)+}++/// The year-ish field: a numeric field whose name carries "year" (gapminder+/// `year`, nasa `disc_year`), else a date field (then a year becomes a+/// year-wide date range — still only prompt-supplied bounds).+fn year_field(fields: &[FieldView]) -> Option<usize> {+ fields+ .iter()+ .position(|f| f.numeric() && f.tokens.iter().any(|t| t == "year"))+ .or_else(|| fields.iter().position(|f| f.date()))+}++/// The date field: the catalog's `time_field` when present, else date-typed+/// fields by name preference (time > date > timestamp) then name order.+fn date_field(fields: &[FieldView], hint: Option<&str>) -> Option<usize> {+ if let Some(h) = hint {+ if let Some(pos) = fields.iter().position(|f| f.name == h && f.date()) {+ return Some(pos);+ }+ }+ let pref = |f: &FieldView| {+ if f.tokens.iter().any(|t| t == "time") {+ 0+ } else if f.tokens.iter().any(|t| t == "date") {+ 1+ } else if f.tokens.iter().any(|t| t == "timestamp" || t == "ts") {+ 2+ } else {+ 3+ }+ };+ fields+ .iter()+ .enumerate()+ .filter(|(_, f)| f.date())+ .min_by_key(|(pos, f)| (pref(f), *pos))+ .map(|(pos, _)| pos)+}++// ─────────────────────────────────────────────────────────────────────────────+// The passes+// ─────────────────────────────────────────────────────────────────────────────++async fn plan_prompt(+ toks: &[Tok],+ fields: &[FieldView],+ time_field_hint: Option<&str>,+ used: &mut [bool],+ ctx: &mut PlanCtx,+ idx: &Arc<Index>,+) -> Result<(Value, f64), Refusal> {+ // Pass 0: the "multi-" quantifier ("multi-planet systems", "multi-star+ // systems") — "more than one" of the concept the suffix names. The bound+ // is the word's own meaning (as with "million" → 10⁶), not generated:+ // "multi" is strictly-greater-than one, which over integer counts is the+ // same set as ≥ 2.+ multi_pass(toks, fields, used, ctx);+ // Pass B first: comparator phrases claim their tokens, so "or greater"+ // never survives as a leftover value phrase.+ numeric_pass(toks, fields, used, ctx)?;+ // Pass A: date literals and bare years.+ date_pass(toks, fields, time_field_hint, used, ctx)?;+ // Pass C: equality from the value map (field+value splits, then values).+ let mut probes = 0usize;+ let leftovers = value_pass(toks, fields, used, ctx, idx, &mut probes).await;+ let grounded = !ctx.constraints.is_empty();+ settle_leftovers(toks, &leftovers, grounded, ctx)?;++ let mut constraints = ctx.constraints.clone();+ // Gold ordering, pinned by the harness: eq clauses in prompt order+ // first, then range clauses in prompt order. Deterministic either way,+ // but matching the reference makes exact-diff tests possible.+ constraints.sort_by_key(|c| (c.is_range, c.pos));+ let clause = match constraints.len() {+ 0 => json!({ "match_all": {} }),+ 1 => constraints.remove(0).clause,+ _ => {+ let filters: Vec<Value> = constraints.iter().map(|c| c.clause.clone()).collect();+ let mut inner = Map::new();+ inner.insert("filter".to_string(), Value::Array(filters));+ let mut outer = Map::new();+ outer.insert("bool".to_string(), Value::Object(inner));+ Value::Object(outer)+ }+ };+ let confidence = if ctx.constraints.is_empty() { 0.5 } else { 0.9 };+ Ok((clause, confidence))+}++/// `multi-<concept>` → `> 1` on the concept's field. Fires only when the+/// remainder names a concept with a numeric field in this index; anything+/// else ("multis") is left for the ordinary passes.+fn multi_pass(toks: &[Tok], fields: &[FieldView], used: &mut [bool], ctx: &mut PlanCtx) {+ for (i, t) in toks.iter().enumerate() {+ if used[i] {+ continue;+ }+ let Some(rest) = t.norm.strip_prefix("multi") else {+ continue;+ };+ if rest.len() < 4 {+ continue;+ }+ let Some(targets) = concept_for(rest) else {+ continue;+ };+ let Some(fpos) = field_by_tokens(targets, fields, |f| f.numeric()) else {+ continue;+ };+ let f = &fields[fpos];+ ctx.constraints.push(Constraint {+ clause: range_clause_1(&f.name, "gt", json!(1)),+ phrase: t.raw.clone(),+ via: "quantifier+concept",+ is_range: true,+ pos: i,+ });+ used[i] = true;+ }+}++/// Pass B: numeric comparisons. Every bound is a prompt literal; the scale+/// lexicon ("100 million" → 10⁸) is the only arithmetic, and it is the+/// prompt's own words.+fn numeric_pass(+ toks: &[Tok],+ fields: &[FieldView],+ used: &mut [bool],+ ctx: &mut PlanCtx,+) -> Result<(), Refusal> {+ let mut i = 0;+ while i < toks.len() {+ if used[i] || !toks[i].is_num() {+ i += 1;+ continue;+ }+ // A spelled star-count word carries its own field ("triple" → snum).+ if let Some(concept) = toks[i].concept {+ if let Some(f) =+ field_by_tokens(&[concept], fields, |f| f.numeric()).map(|pos| &fields[pos])+ {+ if let Some(v) = toks[i].num_value() {+ ctx.constraints.push(Constraint {+ clause: term_clause(&f.name, v),+ phrase: toks[i].raw.clone(),+ via: "literal+concept",+ is_range: false,+ pos: i,+ });+ used[i] = true;+ i += 1;+ continue;+ }+ }+ }++ // (1) pair: from/between … <num> and|to|through <num>+ if let Some((second, connector)) = pair_at(toks, used, i) {+ let (lo, _lo_x) = number_span(toks, i);+ let (hi, hi_x) = number_span(toks, second);+ // "magnitude from 4" hides the phrase before `from`;+ // "between magnitude 4" hides it after `between` — try both.+ let mut bwd = backward_phrase(toks, used, i);+ if bwd.is_empty() {+ if let Some(c) = connector {+ bwd = backward_phrase(toks, used, c);+ }+ }+ let unit = unit_after(toks, second + 1 + hi_x).map(|(u, _)| u);+ let field = resolve_comparison(toks, &bwd, unit, None, &[], fields).or_else(|| {+ if is_yearish(&toks[i]) && is_yearish(&toks[second]) {+ year_field(fields)+ } else {+ None+ }+ });+ let Some(fpos) = field else {+ return Err(comparison_refusal(toks, &bwd, i));+ };+ let f = &fields[fpos];+ // A year pair against a date field stays prompt-supplied: the+ // years of the prompt, as year-wide bounds.+ let clause = if f.date() {+ let y1 = toks[i].int_literal.unwrap_or_default();+ let y2 = toks[second].int_literal.unwrap_or_default();+ range_clause_2(+ &f.name,+ json!(year_start(y1)),+ "gte",+ json!(next_year_start(y2)),+ "lt",+ )+ } else {+ range_clause_2(&f.name, lo, "gte", hi, "lte")+ };+ let phrase = format!(+ "{}… {}",+ if bwd.is_empty() {+ String::new()+ } else {+ format!("{} ", phrase_text(toks, &bwd))+ },+ toks[second].raw+ );+ ctx.constraints.push(Constraint {+ clause,+ phrase: phrase.trim().to_string(),+ via: "range-pair",+ is_range: true,+ pos: bwd.first().copied().unwrap_or(i),+ });+ for &b in &bwd {+ used[b] = true;+ }+ for u in &mut used[i..=second + hi_x] {+ *u = true;+ }+ i = second + 1 + hi_x;+ continue;+ }++ // (2) op-after ("2.75 or greater", "2018 or later")+ let (val, sx) = number_span(toks, i);+ if let Some((olen, op)) = op_after_at(toks, i + 1 + sx) {+ let bwd = backward_phrase(toks, used, i);+ let unit = unit_after(toks, i + 1 + sx + olen).map(|(u, _)| u);+ let fwd = forward_phrase(toks, used, i + 1 + sx + olen, 3);+ let field = resolve_comparison(toks, &bwd, unit, None, &fwd, fields)+ .or_else(|| yearish_fallback(&toks[i], fields));+ let Some(fpos) = field else {+ return Err(comparison_refusal(toks, &bwd, i));+ };+ push_comparison(ctx, &fields[fpos], op, &toks[i], val, &bwd, i, toks);+ for u in &mut used[i..i + 1 + sx + olen] {+ *u = true;+ }+ for &b in &bwd {+ used[b] = true;+ }+ i += 1 + sx + olen;+ continue;+ }++ // (3) op-before ("deeper than 50", "at least 2 known planets"),+ // including the separated form where the field phrase sits between+ // the op and the number ("stronger than magnitude 3.5").+ let mut gap_hit: Option<Vec<usize>> = None;+ let mut op_hit = op_before_at(toks, used, i);+ if op_hit.is_none() {+ if let Some((start, olen, op, gap)) = op_before_gap_at(toks, used, i) {+ op_hit = Some((start, olen, op));+ gap_hit = Some(gap);+ }+ }+ if let Some((start, olen, op)) = op_hit {+ let gap = gap_hit.clone().unwrap_or_default();+ let bwd = backward_phrase(toks, used, start);+ let unit_hit = unit_after(toks, i + 1 + sx);+ let compar = comparative_concept(toks, start, olen);+ let fwd = forward_phrase(toks, used, i + 1 + sx, 3);+ let field =+ resolve_comparison(toks, &bwd, unit_hit.map(|(u, _)| u), compar, &fwd, fields)+ .or_else(|| resolve_field(toks, &gap, fields, |f| f.numeric()))+ .or_else(|| yearish_fallback(&toks[i], fields));+ let Some(fpos) = field else {+ return Err(comparison_refusal(toks, &bwd, i));+ };+ push_comparison(ctx, &fields[fpos], op, &toks[i], val, &bwd, i, toks);+ for u in &mut used[start..=i + sx] {+ *u = true;+ }+ if let Some((_, uidx)) = unit_hit {+ used[uidx] = true;+ }+ for &b in &fwd {+ used[b] = true;+ }+ for &b in &bwd {+ used[b] = true;+ }+ for &b in &gap {+ used[b] = true;+ }+ i += 1 + sx;+ continue;+ }++ // (4) bare number with a forward field phrase ("2 stars", "single+ // … " handled above). No comparator, no scale.+ let fwd = forward_phrase(toks, used, i + 1, 3);+ if let Some(fpos) = resolve_field(toks, &fwd, fields, |f| f.numeric()) {+ if let Some(v) = toks[i].num_value() {+ let f = &fields[fpos];+ let phrase = format!("{} {}", toks[i].raw, phrase_text(toks, &fwd));+ ctx.constraints.push(Constraint {+ clause: term_clause(&f.name, v),+ phrase,+ via: "literal+field",+ is_range: false,+ pos: i,+ });+ used[i] = true;+ for &k in &fwd {+ used[k] = true;+ }+ i += 1;+ continue;+ }+ }++ // Bare years fall through to the date pass; anything else is named+ // by the leftover pass.+ i += 1;+ }+ Ok(())+}++/// Emit one comparison constraint. Every bound is the prompt's own literal;+/// a year compared against a date field becomes year-wide bounds.+#[allow(clippy::too_many_arguments)]+fn push_comparison(+ ctx: &mut PlanCtx,+ f: &FieldView,+ op: Op,+ tok: &Tok,+ val: Value,+ bwd: &[usize],+ num_i: usize,+ toks: &[Tok],+) {+ let prefix = if bwd.is_empty() {+ String::new()+ } else {+ format!("{} ", phrase_text(toks, bwd))+ };+ if op == Op::Eq {+ ctx.constraints.push(Constraint {+ clause: term_clause(&f.name, val),+ phrase: format!("{prefix}{}", tok.raw),+ via: "literal+field",+ is_range: false,+ pos: bwd.first().copied().unwrap_or(num_i),+ });+ } else if f.date() && is_yearish(tok) {+ // A year compared against a date field: year-wide bounds, still+ // only the prompt's own number.+ let y = tok+ .int_literal+ .unwrap_or_else(|| tok.num_f64().unwrap_or(0.0) as i64);+ let clause = match op {+ Op::Gt => range_clause_1(&f.name, "gt", json!(next_year_start(y))),+ Op::Gte => range_clause_1(&f.name, "gte", json!(year_start(y))),+ Op::Lt => range_clause_1(&f.name, "lt", json!(year_start(y))),+ Op::Lte => range_clause_1(&f.name, "lte", json!(next_year_start(y))),+ Op::Eq => range_clause_2(+ &f.name,+ json!(year_start(y)),+ "gte",+ json!(next_year_start(y)),+ "lt",+ ),+ };+ ctx.constraints.push(Constraint {+ clause,+ phrase: format!("{prefix}{}", tok.raw),+ via: "comparator",+ is_range: true,+ pos: bwd.first().copied().unwrap_or(num_i),+ });+ } else {+ ctx.constraints.push(Constraint {+ clause: range_clause_1(&f.name, op.key(), val),+ phrase: format!("{prefix}{}", tok.raw),+ via: "comparator",+ is_range: true,+ pos: bwd.first().copied().unwrap_or(num_i),+ });+ }+}++/// `<num> and|to|through <num>` after i, plus the from/between connector+/// governing the pair (scanned back over content tokens).+fn pair_at(toks: &[Tok], used: &[bool], i: usize) -> Option<(usize, Option<usize>)> {+ let conn = i + 1;+ if conn + 1 >= toks.len() || used[conn] {+ return None;+ }+ match toks[conn].norm.as_str() {+ "and" | "to" | "through" => {}+ _ => return None,+ }+ let second = conn + 1;+ if second >= toks.len() || used[second] || !toks[second].is_num() {+ return None;+ }+ // connector: scan back over content tokens (up to 4) for from/between+ let mut connector = None;+ let mut j = i;+ let mut steps = 0;+ while j > 0 && steps < 4 {+ j -= 1;+ steps += 1;+ if used[j] {+ break;+ }+ match toks[j].norm.as_str() {+ "between" | "from" => {+ connector = Some(j);+ break;+ }+ _ if FUNCTION_WORDS.contains(&toks[j].norm.as_str()) => continue,+ _ => continue,+ }+ }+ Some((second, connector))+}++/// Scale handling: the effective value of the number at `i` plus how many+/// extra tokens it consumed ("100 million" → (10⁸, 1)).+fn number_span(toks: &[Tok], i: usize) -> (Value, usize) {+ if i + 1 < toks.len() {+ if let Some((_, mult)) = SCALES.iter().find(|(w, _)| toks[i + 1].norm == *w) {+ if let Some(base) = toks[i].int_literal {+ let v = base.saturating_mul(*mult);+ return (json!(v), 1);+ }+ if let Some(f) = toks[i].num_f64() {+ if let Some(n) = serde_json::Number::from_f64(f * (*mult as f64)) {+ return (Value::Number(n), 1);+ }+ }+ }+ }+ (toks[i].num_value().unwrap_or(Value::Null), 0)+}++/// op phrase starting exactly at `at`.+fn op_after_at(toks: &[Tok], at: usize) -> Option<(usize, Op)> {+ for (words, op) in OP_AFTER {+ if words.iter().enumerate().all(|(k, w)| {+ let idx = at + k;+ idx < toks.len() && toks[idx].norm == *w+ }) {+ return Some((words.len(), *op));+ }+ }+ None+}++/// op phrase ending right before the number at `i`; (start, len, op).+fn op_before_at(toks: &[Tok], used: &[bool], i: usize) -> Option<(usize, usize, Op)> {+ for (words, op) in OP_BEFORE {+ let len = words.len();+ if i < len {+ continue;+ }+ let start = i - len;+ if words+ .iter()+ .enumerate()+ .all(|(k, w)| toks[start + k].norm == *w && !used[start + k])+ {+ return Some((start, len, *op));+ }+ }+ None+}++/// The separated form: op phrase, then the FIELD PHRASE, then the number —+/// "stronger than MAGNITUDE 3.5". Scans the number back over up to 3+/// content tokens to find an op phrase ending before them; the tokens+/// between the op and the number come back as the gap, a field-phrase+/// candidate `op_before_at` alone cannot express. Purely positional, so it+/// stays deterministic.+fn op_before_gap_at(+ toks: &[Tok],+ used: &[bool],+ i: usize,+) -> Option<(usize, usize, Op, Vec<usize>)> {+ for gap_len in 1..=3 {+ // `i - gap_len` must not underflow: a number early in the prompt+ // (gapminder-052: "in 1952, countries…") has no room for a gap at+ // all. Larger gap_len only makes it worse, so stop.+ if i <= gap_len {+ break;+ }+ let number_at = i - gap_len;+ let Some((start, olen, op)) = op_before_at(toks, used, number_at) else {+ continue;+ };+ let gap: Vec<usize> = (start + olen..i).collect();+ if gap+ .iter()+ .any(|&k| used[k] || toks[k].is_num() || toks[k].is_date())+ {+ continue;+ }+ return Some((start, olen, op, gap));+ }+ None+}++/// A unit token at/after `at` (skipping one "of"): (unit key, token index).+/// "Earth radii" is two tokens.+fn unit_after(toks: &[Tok], at: usize) -> Option<(&'static str, usize)> {+ let mut k = at;+ if k < toks.len() && toks[k].norm == "of" {+ k += 1;+ }+ if k >= toks.len() {+ return None;+ }+ let norm = toks[k].norm.as_str();+ if let Some((u, _)) = UNITS.iter().find(|(u, _)| *u == norm) {+ return Some((u, k));+ }+ if norm == "earth" && k + 1 < toks.len() && toks[k + 1].norm == "radii" {+ return Some(("earthradii", k));+ }+ None+}++/// The quantity a comparative adjective names, when the op phrase carries+/// one ("deeper than" → depth).+fn comparative_concept(toks: &[Tok], start: usize, len: usize) -> Option<&'static str> {+ COMPARATIVE_CONCEPTS+ .iter()+ .find(|(w, _)| (start..start + len).any(|k| toks[k].norm == *w))+ .map(|(_, c)| *c)+}++fn is_yearish(t: &Tok) -> bool {+ matches!(t.number, Some(v) if (1500.0..=2100.0).contains(&v) && v == v.trunc())+}++/// Years with a comparator ("2018 or later") fall back to the year/date+/// field — the number is still the prompt's own.+fn yearish_fallback(tok: &Tok, fields: &[FieldView]) -> Option<usize> {+ if is_yearish(tok) {+ year_field(fields)+ } else {+ None+ }+}++/// Copulas/auxiliaries: transparent in the backward scan ("life expectancy+/// WAS below 45" still finds `life expectancy`), unlike other function words+/// which terminate the phrase.+const AUX_WORDS: &[&str] = &[+ "is", "are", "was", "were", "be", "been", "being", "do", "does", "did", "has", "have", "had",+];++/// Content tokens ending just before `stop` (up to 3): the field phrase of+/// a comparison. Function and generic words terminate the scan, auxiliaries+/// are skipped over.+fn backward_phrase(toks: &[Tok], used: &[bool], stop: usize) -> Vec<usize> {+ let mut out = Vec::new();+ let mut j = stop;+ while j > 0 && out.len() < 3 {+ j -= 1;+ if used[j] {+ break;+ }+ let t = &toks[j];+ if AUX_WORDS.contains(&t.norm.as_str()) {+ continue;+ }+ if FUNCTION_WORDS.contains(&t.norm.as_str())+ || GENERIC_WORDS.contains(&t.norm.as_str())+ || t.is_num()+ || t.is_date()+ {+ break;+ }+ out.push(j);+ }+ out.reverse();+ out+}++/// Up to `max` content tokens after `at`; generic words are skipped over —+/// EXCEPT when the generic word itself names a quantity concept ("2 known+/// planets": `planets` → pnum), in which case it joins the phrase, because+/// "at least 2 known planets" carries its field only in that word. Function+/// words end the phrase.+fn forward_phrase(toks: &[Tok], used: &[bool], at: usize, max: usize) -> Vec<usize> {+ let mut out = Vec::new();+ let mut j = at;+ while j < toks.len() && out.len() < max {+ if used[j] {+ break;+ }+ let t = &toks[j];+ if FUNCTION_WORDS.contains(&t.norm.as_str()) {+ break;+ }+ if GENERIC_WORDS.contains(&t.norm.as_str()) {+ let carries_concept = concept_for(&t.norm).is_some();+ if !carries_concept {+ j += 1;+ continue;+ }+ }+ if t.is_num() || t.is_date() {+ break;+ }+ out.push(j);+ j += 1;+ }+ out+}++/// Field resolution for a comparison, in priority order: the unit decides+/// ("6000 K" is a temperature even when the phrase says "stars"), then the+/// backward field phrase, then the comparative's own concept ("deeper"+/// without a phrase still means depth), then the forward phrase ("at least+/// 2 known planets" carries its field after the number).+fn resolve_comparison(+ toks: &[Tok],+ bwd: &[usize],+ unit: Option<&str>,+ compar: Option<&str>,+ fwd: &[usize],+ fields: &[FieldView],+) -> Option<usize> {+ if let Some(u) = unit {+ let targets = UNITS.iter().find(|(key, _)| *key == u).map(|(_, t)| *t)?;+ if let Some(pos) = field_by_tokens(targets, fields, |f| f.numeric()) {+ return Some(pos);+ }+ }+ if let Some(pos) = resolve_field(toks, bwd, fields, |f| f.numeric()) {+ return Some(pos);+ }+ if let Some(c) = compar {+ if let Some(targets) = concept_for(c) {+ if let Some(pos) = field_by_tokens(targets, fields, |f| f.numeric()) {+ return Some(pos);+ }+ }+ }+ resolve_field(toks, fwd, fields, |f| f.numeric())+}++fn comparison_refusal(toks: &[Tok], bwd: &[usize], i: usize) -> Refusal {+ let phrase = if bwd.is_empty() {+ toks[i].raw.clone()+ } else {+ format!("{} {}", phrase_text(toks, bwd), toks[i].raw)+ };+ (+ vec![phrase.clone()],+ format!(+ "comparison on `{phrase}` resolves to no numeric or date field in this index — \+ refusing rather than guessing. Name a field that exists"+ ),+ )+}++fn phrase_text(toks: &[Tok], idxs: &[usize]) -> String {+ idxs.iter()+ .map(|&k| toks[k].raw.clone())+ .collect::<Vec<_>>()+ .join(" ")+}++/// Pass A: date literals (single days and from/through pairs) and bare+/// years with a year connector. Bounds are the prompt's own days/years — a+/// year lands on the year FIELD when one exists (gapminder `year`, nasa+/// `disc_year`) and on the date field's year bounds otherwise.+fn date_pass(+ toks: &[Tok],+ fields: &[FieldView],+ time_field_hint: Option<&str>,+ used: &mut [bool],+ ctx: &mut PlanCtx,+) -> Result<(), Refusal> {+ let dpos = date_field(fields, time_field_hint);+ let ypos = fields+ .iter()+ .position(|f| f.numeric() && f.tokens.iter().any(|t| t == "year"));+ let mut i = 0;+ while i < toks.len() {+ if used[i] {+ i += 1;+ continue;+ }+ if let Some((y, m, d)) = date_parts(&toks[i].raw) {+ let Some(dfield) = dpos.map(|p| &fields[p]) else {+ // No date field: leave the literal to be named by the+ // leftover pass rather than silently dropped.+ i += 1;+ continue;+ };+ // pair: <date1> to|through <date2>+ if i + 2 < toks.len()+ && matches!(toks[i + 1].norm.as_str(), "to" | "through")+ && !used[i + 1]+ && !used[i + 2]+ {+ if let Some((y2, m2, d2)) = date_parts(&toks[i + 2].raw) {+ ctx.constraints.push(Constraint {+ clause: range_clause_2(+ &dfield.name,+ json!(day_start(y, m, d)),+ "gte",+ json!(next_day(y2, m2, d2)),+ "lt",+ ),+ phrase: format!("{} … {}", toks[i].raw, toks[i + 2].raw),+ via: "date-range",+ is_range: true,+ pos: i,+ });+ used[i] = true;+ used[i + 1] = true;+ used[i + 2] = true;+ i += 3;+ continue;+ }+ }+ // single day+ ctx.constraints.push(Constraint {+ clause: range_clause_2(+ &dfield.name,+ json!(day_start(y, m, d)),+ "gte",+ json!(next_day(y, m, d)),+ "lt",+ ),+ phrase: toks[i].raw.clone(),+ via: "date-day",+ is_range: true,+ pos: i,+ });+ used[i] = true;+ i += 1;+ continue;+ }+ // bare year with a connector nearby+ if is_yearish(&toks[i]) && year_context(toks, used, i) {+ let y = toks[i].int_literal.unwrap_or_default();+ if let Some(yp) = ypos {+ let f = &fields[yp];+ ctx.constraints.push(Constraint {+ clause: term_clause(&f.name, json!(y)),+ phrase: toks[i].raw.clone(),+ via: "year",+ is_range: false,+ pos: i,+ });+ used[i] = true;+ i += 1;+ continue;+ }+ if let Some(dp) = dpos {+ let f = &fields[dp];+ ctx.constraints.push(Constraint {+ clause: range_clause_2(+ &f.name,+ json!(year_start(y)),+ "gte",+ json!(next_year_start(y)),+ "lt",+ ),+ phrase: toks[i].raw.clone(),+ via: "year",+ is_range: true,+ pos: i,+ });+ used[i] = true;+ i += 1;+ continue;+ }+ }+ i += 1;+ }+ Ok(())+}++/// Is a year connector adjacent (one token either side) to position i?+fn year_context(toks: &[Tok], used: &[bool], i: usize) -> bool {+ let check = |k: usize| !used[k] && YEAR_CONNECTORS.iter().any(|c| toks[k].norm == *c);+ (i > 0 && check(i - 1)) || (i + 1 < toks.len() && check(i + 1))+}++/// Pass C: equality against the live value map. Returns the leftover runs+/// it could not ground, for the decoration/refusal decision.+async fn value_pass(+ toks: &[Tok],+ fields: &[FieldView],+ used: &mut [bool],+ ctx: &mut PlanCtx,+ idx: &Arc<Index>,+ probes: &mut usize,+) -> Vec<Vec<usize>> {+ let mut unresolved: Vec<Vec<usize>> = Vec::new();+ // Leftover runs: contiguous content tokens (function and generic words+ // break, so the value phrase inside "… for cities in Switzerland" and+ // "the ak network" survives on its own).+ let mut runs: Vec<Vec<usize>> = Vec::new();+ let mut cur: Vec<usize> = Vec::new();+ for (k, t) in toks.iter().enumerate() {+ if used[k]+ || FUNCTION_WORDS.contains(&t.norm.as_str())+ || GENERIC_WORDS.contains(&t.norm.as_str())+ {+ if !cur.is_empty() {+ runs.push(mem::take(&mut cur));+ }+ } else {+ cur.push(k);+ }+ }+ if !cur.is_empty() {+ runs.push(cur);+ }++ 'runs: for run in runs {+ // (a) field + value split ("magnitude type mb" with a non-generic+ // "type", "continent Europe")+ for split in (1..run.len()).rev() {+ let (left, right) = run.split_at(split);+ let Some(fpos) = resolve_field(toks, left, fields, |f| f.categorical()) else {+ continue;+ };+ let f = &fields[fpos];+ let joined = right+ .iter()+ .map(|&k| toks[k].norm.clone())+ .collect::<String>();+ if let Some(v) = ctx.lookup_exact(&f.name, &joined) {+ ctx.constraints.push(Constraint {+ clause: term_clause(&f.name, v),+ phrase: phrase_text(toks, &run),+ via: "field+value",+ is_range: false,+ pos: run[0],+ });+ for &k in &run {+ used[k] = true;+ }+ continue 'runs;+ }+ // BM25 arm: the field's values did not fit the agg.+ if !ctx.complete(&f.name) && *probes < MAX_BM25_PROBES {+ let phrase = phrase_text(toks, right);+ if let Some(clause) = ctx.bm25_probe(idx, &f.name, &phrase).await {+ *probes += 1;+ ctx.soft += 1;+ ctx.constraints.push(Constraint {+ clause,+ phrase,+ via: "field+value-bm25",+ is_range: false,+ pos: run[0],+ });+ for &k in &run {+ used[k] = true;+ }+ continue 'runs;+ }+ }+ }+ // (b) value alone, longest subrun first, fields in name order;+ // prefix matching covers adjectival forms ("European" → "Europe").+ for len in (1..=run.len()).rev() {+ for start in 0..=(run.len() - len) {+ let sub = &run[start..start + len];+ let joined = sub+ .iter()+ .map(|&k| toks[k].norm.clone())+ .collect::<String>();+ let mut hit: Option<(String, Value, bool)> = None;+ for fname in ctx.values.keys() {+ if let Some(v) = ctx.lookup_exact(fname, &joined) {+ hit = Some((fname.clone(), v, false));+ break;+ }+ if let Some(v) = ctx.lookup_prefix(fname, &joined) {+ if hit.is_none() {+ hit = Some((fname.clone(), v, true));+ }+ }+ }+ if let Some((fname, v, soft)) = hit {+ if soft {+ ctx.soft += 1;+ }+ ctx.constraints.push(Constraint {+ clause: term_clause(&fname, v),+ phrase: phrase_text(toks, sub),+ via: if soft { "value-prefix" } else { "value" },+ is_range: false,+ pos: sub[0],+ });+ for &k in sub {+ used[k] = true;+ }+ continue 'runs;+ }+ }+ }+ unresolved.push(run);+ }+ unresolved+}++/// The decoration/refusal decision over the runs the passes could not+/// ground. A capitalized (proper-noun) run is a 422 naming it; so is any+/// multi-token run, and any run at all when nothing else grounded the+/// prompt. A single lowercase token next to a resolved constraint is+/// decoration, reported in the plan.+fn settle_leftovers(+ toks: &[Tok],+ unresolved: &[Vec<usize>],+ grounded: bool,+ ctx: &mut PlanCtx,+) -> Result<(), Refusal> {+ if unresolved.is_empty() {+ return Ok(());+ }+ for run in unresolved {+ let single = run.len() == 1;+ let capitalized = toks[run[0]].capitalized;+ if capitalized || !single || !grounded {+ let phrase = phrase_text(toks, run);+ return Err((+ vec![phrase.clone()],+ format!(+ "phrase `{phrase}` resolves to no field and no value in this index — \+ refusing rather than guessing. Name a field that exists, or a value \+ the index holds"+ ),+ ));+ }+ ctx.ignored.push(phrase_text(toks, run));+ }+ Ok(())+}++// ─────────────────────────────────────────────────────────────────────────────+// Tests for the pure pieces (route-level tests: tests/ask_endpoint.rs)+// ─────────────────────────────────────────────────────────────────────────────++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn glob_matches_star_and_exact() {+ assert!(glob_match("ax-*", "ax-usgs-earthquakes"));+ assert!(glob_match("ax-*", "ax-"));+ assert!(!glob_match("ax-*", "sales"));+ assert!(glob_match("ax-quakes", "ax-quakes"));+ assert!(!glob_match("ax-quakes", "ax-quakes2"));+ assert!(glob_match("*", "anything"));+ assert!(glob_match("a*b*c", "aXXbYYc"));+ assert!(!glob_match("a*b*c", "aXXbYY"));+ }++ #[test]+ fn name_tokens_splits_snake_and_camel() {+ assert_eq!(name_tokens("pl_orbper"), vec!["pl", "orbper"]);+ assert_eq!(name_tokens("lifeExp"), vec!["life", "exp"]);+ assert_eq!(name_tokens("st_teff"), vec!["st", "teff"]);+ assert_eq!(name_tokens("magType"), vec!["mag", "type"]);+ assert_eq!(name_tokens("gdpPercap"), vec!["gdp", "percap"]);+ }++ #[test]+ fn date_parts_validates() {+ assert_eq!(date_parts("2026-09-01"), Some((2026, 9, 1)));+ assert_eq!(date_parts("2026-13-01"), None);+ assert_eq!(date_parts("2026-9-1"), Some((2026, 9, 1)));+ assert_eq!(date_parts("1992"), None);+ assert_eq!(day_start(2026, 9, 1), "2026-09-01T00:00:00Z");+ assert_eq!(next_day(2026, 9, 1), "2026-09-02T00:00:00Z");+ assert_eq!(next_day(2026, 12, 31), "2027-01-01T00:00:00Z");+ }++ #[test]+ fn tokenizer_marks_numbers_years_and_dates() {+ let t = tokenize("events deeper than 50 km on 2026-09-01 (UTC)");+ let nums: Vec<&Tok> = t.iter().filter(|x| x.is_num()).collect();+ assert_eq!(nums.len(), 1);+ assert_eq!(nums[0].int_literal, Some(50));+ let dates: Vec<&Tok> = t.iter().filter(|x| x.is_date()).collect();+ assert_eq!(dates.len(), 1);+ assert_eq!(dates[0].raw, "2026-09-01");+ // parens trimmed off UTC, which is a function word+ assert!(t.iter().any(|x| x.raw == "UTC"));+ }++ #[test]+ fn number_span_applies_scale_words() {+ let t = tokenize("population above 100 million");+ let i = t.iter().position(|x| x.int_literal == Some(100)).unwrap();+ let (v, extra) = number_span(&t, i);+ assert_eq!(v, json!(100_000_000));+ assert_eq!(extra, 1);+ let t = tokenize("magnitude 4.5");+ let i = t.iter().position(|x| x.number == Some(4.5)).unwrap();+ let (v, extra) = number_span(&t, i);+ assert_eq!(v, json!(4.5));+ assert_eq!(extra, 0);+ }++ #[test]+ fn concept_table_prefers_the_longest_phrase() {+ assert_eq!(concept_for("magnitudetype"), Some(&["magtype"][..]));+ assert_eq!(concept_for("magnitude"), Some(&["mag"][..]));+ assert_eq!(concept_for("lifeexpectancy"), Some(&["lifeexp"][..]));+ assert_eq!(concept_for("orbitalperiods"), Some(&["orbper"][..]));+ assert_eq!(concept_for("population"), Some(&["pop"][..]));+ assert_eq!(concept_for("gdppercapita"), Some(&["gdppercap"][..]));+ assert_eq!(concept_for("nothingknown"), None);+ }++ #[test]+ fn value_agg_fields_orders_by_cardinality_then_name() {+ let mut fields = vec![+ FieldView::new("continent".to_string(), "keyword"),+ FieldView::new("country".into(), "keyword"),+ FieldView::new("place".into(), "keyword"),+ FieldView::new("mag".into(), "double"),+ ];+ fields[2].cardinality_est = Some(5); // place, lowest+ fields[0].cardinality_est = Some(50);+ let got = value_agg_fields(&fields);+ assert_eq!(got, vec!["place", "continent", "country"]);+ }+}diff --git a/engine/crates/xerj-api/src/lib.rs b/engine/crates/xerj-api/src/lib.rsindex e0cdb2fcb..822a87653 100644--- a/engine/crates/xerj-api/src/lib.rs+++ b/engine/crates/xerj-api/src/lib.rs@@ -63,6 +63,7 @@// crate-level allows in xerj-engine and xerj-console-api).#![allow(clippy::result_large_err)]+pub mod ask_api;pub mod audit_mw;pub mod auth;pub mod authz;diff --git a/engine/crates/xerj-api/src/router.rs b/engine/crates/xerj-api/src/router.rsindex cd88ff64a..b08bbf087 100644--- a/engine/crates/xerj-api/src/router.rs+++ b/engine/crates/xerj-api/src/router.rs@@ -28,8 +28,8 @@ use uuid::Uuid;use xerj_common::config::CorsConfig;use crate::{- audit_mw, auth::auth_middleware, authz, es_compat, graph_api, ism_api, memory_api, native,- share, state::AppState, systemone_api, wal_tap_api,+ ask_api, audit_mw, auth::auth_middleware, authz, es_compat, graph_api, ism_api, memory_api,+ native, share, state::AppState, systemone_api, wal_tap_api,};// ─────────────────────────────────────────────────────────────────────────────@@ -129,6 +129,10 @@ pub fn build_native_router(state: AppState) -> Router {// set — and then nothing leaves the node to answer it..route("/v1/systemone", post(systemone_api::systemone)).route("/v1/models", get(systemone_api::models))+ // Prompt in, validated query DSL out (#1056): the endpoint an agent+ // calls instead of hand-writing ES DSL. Same authz posture as+ // /_decide (cluster verb, POST, fail-closed for scoped keys).+ .route("/v1/ask", post(ask_api::ask))// Admin: cluster-wide flush + backup (snapshot to disk).route("/v1/admin/flush", post(native::admin_flush)).route("/v1/admin/backup", post(native::admin_backup))@@ -296,6 +300,9 @@ pub fn build_es_compat_router(state: AppState) -> Router {// The audit twin of /v1/systemone: same vote, named index, neighbours// and abstain instead of a wire-shaped error..route("/_decide", post(systemone_api::decide))+ // #1056, ES-compat surface: same handler as /v1/ask; the index+ // rides in the body, exactly like /_decide and /_msearch.+ .route("/_ask", post(ask_api::ask)).route("/_mget", post(es_compat::mget))// Index-scoped multi-get — the path index defaults entries that omit `_index`..route(diff --git a/engine/crates/xerj-api/tests/ask_endpoint.rs b/engine/crates/xerj-api/tests/ask_endpoint.rsnew file mode 100644index 000000000..c306577c4--- /dev/null+++ b/engine/crates/xerj-api/tests/ask_endpoint.rs@@ -0,0 +1,702 @@+//! Route-level tests for `POST /_ask` (issue #1056) — the four hard lines of+//! the contract, exercised over the real router with real indexes:+//!+//! 1. **zero invalid DSL out** — for a battery of adversarial prompts, every+//! 200 response's `query` re-parses through `xerj_query::parse_request`;+//! 2. **unresolved phrases are 422s naming them** — unknown values, unknown+//! fields, unroutable patterns;+//! 3. **deterministic** — the same request three times is byte-identical;+//! 4. values come back in their CANONICAL casing from the terms agg, and+//! combos assemble eq-then-range like the gold ordering.+//!+//! The high-cardinality BM25 arm is covered with a real 1,006-distinct-value+//! field whose agg provably overflows (`sum_other_doc_count > 0`), and the+//! catalog-routing arm with a real `autoindex-catalog` index.++use axum::body::Body;+use axum::http::{Request, StatusCode};+use serde_json::{json, Value};+use tower::ServiceExt;++async fn app() -> (axum::Router, tempfile::TempDir) {+ let dir = tempfile::tempdir().expect("tempdir");+ let mut config = xerj_common::config::Config::default();+ config.server.data_dir = dir.path().to_string_lossy().into_owned();+ config.storage.wal_sync = xerj_common::config::WalSync::Async;+ let metrics = xerj_common::metrics::Metrics::new().expect("metrics");+ let engine = xerj_engine::Engine::new(config.clone()).expect("engine");+ let state = xerj_api::state::AppState::new(config, engine, metrics);+ (xerj_api::router::build_es_compat_router(state), dir)+}++async fn native_app() -> (axum::Router, tempfile::TempDir) {+ let dir = tempfile::tempdir().expect("tempdir");+ let mut config = xerj_common::config::Config::default();+ config.server.data_dir = dir.path().to_string_lossy().into_owned();+ config.storage.wal_sync = xerj_common::config::WalSync::Async;+ let metrics = xerj_common::metrics::Metrics::new().expect("metrics");+ let engine = xerj_engine::Engine::new(config.clone()).expect("engine");+ let state = xerj_api::state::AppState::new(config, engine, metrics);+ (xerj_api::router::build_native_router(state), dir)+}++async fn json_req(+ app: &axum::Router,+ method: &str,+ path: &str,+ body: Value,+) -> (StatusCode, Value) {+ let response = app+ .clone()+ .oneshot(+ Request::builder()+ .method(method)+ .uri(path)+ .header("content-type", "application/json")+ .body(Body::from(body.to_string()))+ .expect("request"),+ )+ .await+ .expect("response");+ let status = response.status();+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)+ .await+ .expect("body");+ (+ status,+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),+ )+}++/// NDJSON and other raw bodies (the `_bulk` wire format is not a JSON+/// document — sending it through `Value` would escape every newline).+async fn raw_req(+ app: &axum::Router,+ method: &str,+ path: &str,+ body: String,+) -> (StatusCode, Value) {+ let response = app+ .clone()+ .oneshot(+ Request::builder()+ .method(method)+ .uri(path)+ .header("content-type", "application/x-ndjson")+ .body(Body::from(body))+ .expect("request"),+ )+ .await+ .expect("response");+ let status = response.status();+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)+ .await+ .expect("body");+ (+ status,+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),+ )+}++async fn ask(app: &axum::Router, index: &str, prompt: &str) -> (StatusCode, Value) {+ json_req(+ app,+ "POST",+ "/_ask",+ json!({ "index": index, "prompt": prompt }),+ )+ .await+}++/// The USGS-earthquakes fixture shape (the harness dataset): typed fields,+/// a date `time`, and a handful of keyword values.+async fn seeded_quakes() -> (axum::Router, tempfile::TempDir) {+ let (app, dir) = app().await;+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-quakes",+ json!({+ "mappings": { "properties": {+ "time": { "type": "date" },+ "place": { "type": "keyword" },+ "mag": { "type": "double" },+ "depth": { "type": "double" },+ "magType": { "type": "keyword" },+ "net": { "type": "keyword" }+ }}+ }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "create ax-quakes failed: {body}");+ let mut ndjson = String::new();+ for (i, (net, magtype, mag, depth, time)) in [+ ("ak", "mb", 3.1, 40.0, "2026-09-01T05:00:00Z"),+ ("ak", "ml", 2.4, 10.0, "2026-09-01T09:30:00Z"),+ ("ci", "md", 1.9, 5.0, "2026-09-02T02:10:00Z"),+ ("hv", "mb", 4.6, 90.0, "2026-09-03T22:45:00Z"),+ ("ci", "mwr", 2.0, 25.0, "2026-09-05T11:00:00Z"),+ ("us", "mww", 5.2, 120.0, "2026-09-05T23:59:59Z"),+ ]+ .into_iter()+ .enumerate()+ {+ ndjson.push_str(&format!(+ "{{\"index\":{{\"_index\":\"ax-quakes\",\"_id\":\"q{i}\"}}}}\n{}\n",+ json!({ "net": net, "magType": magtype, "mag": mag, "depth": depth,+ "time": time, "place": format!("{net} region") })+ ));+ }+ let (status, body) = raw_req(&app, "POST", "/_bulk", ndjson).await;+ assert_eq!(status, StatusCode::OK, "bulk failed: {body}");+ assert_eq!(body["errors"], false, "bulk reported errors: {body}");+ (app, dir)+}++/// The gapminder fixture shape: a numeric `year`, `pop`, `lifeExp`,+/// `gdpPercap`, and keyword `continent`/`country`.+async fn seeded_gapminder() -> (axum::Router, tempfile::TempDir) {+ let (app, dir) = app().await;+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-gap",+ json!({+ "mappings": { "properties": {+ "country": { "type": "keyword" },+ "continent": { "type": "keyword" },+ "year": { "type": "long" },+ "lifeExp": { "type": "double" },+ "pop": { "type": "long" },+ "gdpPercap": { "type": "double" }+ }}+ }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "create ax-gap failed: {body}");+ let mut ndjson = String::new();+ for (i, (country, continent, year, life, pop, gdp)) in [+ ("Japan", "Asia", 2007, 82.6, 127_467_972, 31_656.07),+ (+ "United States",+ "Americas",+ 2007,+ 78.2,+ 301_139_947,+ 42_944.59,+ ),+ ("Kenya", "Africa", 2007, 54.1, 35_610_297, 1_463.25),+ ("Germany", "Europe", 2007, 79.4, 82_369_548, 32_170.44),+ ("Japan", "Asia", 1952, 63.0, 86_459_825, 3_216.28),+ ("Brazil", "Americas", 1952, 50.9, 56_614_160, 2_108.06),+ ("Australia", "Oceania", 1982, 74.7, 14_819_847, 19_477.01),+ ]+ .into_iter()+ .enumerate()+ {+ ndjson.push_str(&format!(+ "{{\"index\":{{\"_index\":\"ax-gap\",\"_id\":\"g{i}\"}}}}\n{}\n",+ json!({ "country": country, "continent": continent, "year": year,+ "lifeExp": life, "pop": pop, "gdpPercap": gdp })+ ));+ }+ let (status, body) = raw_req(&app, "POST", "/_bulk", ndjson).await;+ assert_eq!(status, StatusCode::OK, "bulk failed: {body}");+ assert_eq!(body["errors"], false);+ (app, dir)+}++// ─── happy paths ─────────────────────────────────────────────────────────────++#[tokio::test]+async fn value_term_comes_back_in_canonical_casing() {+ let (app, _dir) = seeded_quakes().await;+ // "AK" in the prompt, "ak" in the index: canonical value wins.+ let (status, body) = ask(&app, "ax-quakes", "events reported by the AK network").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"], json!({ "term": { "net": "ak" } }));+ assert_eq!(body["indices"], json!(["ax-quakes"]));+ assert!(+ body["plan"].as_array().unwrap().len() >= 3,+ "plan too thin: {body}"+ );+ assert!(+ body["confidence"].is_f64(),+ "confidence is a number: {body}"+ );+}++#[tokio::test]+async fn comparators_units_and_field_concepts() {+ let (app, _dir) = seeded_quakes().await;+ for (prompt, want) in [+ (+ "all events with magnitude 2.75 or greater",+ json!({ "range": { "mag": { "gte": 2.75 } } }),+ ),+ (+ "events deeper than 50 km",+ json!({ "range": { "depth": { "gt": 50 } } }),+ ),+ (+ "events shallower than 20 km",+ json!({ "range": { "depth": { "lt": 20 } } }),+ ),+ (+ "events with magnitude exactly 4.6",+ json!({ "term": { "mag": 4.6 } }),+ ),+ (+ "events between magnitude 2 and 5",+ json!({ "range": { "mag": { "gte": 2, "lte": 5 } } }),+ ),+ (+ "events with magnitude type mb",+ json!({ "term": { "magType": "mb" } }),+ ),+ (+ // the separated form: field phrase BETWEEN the op and the number+ "events strictly stronger than magnitude 3.5",+ json!({ "range": { "mag": { "gt": 3.5 } } }),+ ),+ ] {+ let (status, body) = ask(&app, "ax-quakes", prompt).await;+ assert_eq!(status, StatusCode::OK, "{prompt} -> {body}");+ assert_eq!(body["query"], want, "prompt: {prompt}");+ }+}++#[tokio::test]+async fn combos_assemble_eq_first_then_ranges() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = ask(&app, "ax-quakes", "mb events deeper than 100 km").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "bool": { "filter": [+ { "term": { "magType": "mb" } },+ { "range": { "depth": { "gt": 100 } } },+ ]}})+ );+ // A range BEFORE the eq in the prompt still lands after it — the gold+ // ordering the harness diffs against.+ let (status, body) = ask(+ &app,+ "ax-quakes",+ "events during 2026-09-05 UTC with depth greater than 100 km",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"]["bool"]["filter"].as_array().unwrap().len(), 2);+ assert_eq!(+ body["query"]["bool"]["filter"][0],+ json!({ "range": { "time": {+ "gte": "2026-09-05T00:00:00Z", "lt": "2026-09-06T00:00:00Z"+ }}})+ );+ assert_eq!(+ body["query"]["bool"]["filter"][1],+ json!({ "range": { "depth": { "gt": 100 } } })+ );+}++#[tokio::test]+async fn date_literals_become_day_bounds() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = ask(&app, "ax-quakes", "events on 2026-09-01 (UTC)").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "range": { "time": {+ "gte": "2026-09-01T00:00:00Z", "lt": "2026-09-02T00:00:00Z"+ }}})+ );+ let (status, body) = ask(+ &app,+ "ax-quakes",+ "events from 2026-09-01 through 2026-09-03 UTC",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "range": { "time": {+ "gte": "2026-09-01T00:00:00Z", "lt": "2026-09-04T00:00:00Z"+ }}})+ );+}++#[tokio::test]+async fn scale_words_years_and_prefix_values() {+ let (app, _dir) = seeded_gapminder().await;+ let (status, body) = ask(+ &app,+ "ax-gap",+ "countries with population above 100 million in 2007",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "bool": { "filter": [+ { "term": { "year": 2007 } },+ { "range": { "pop": { "gt": 100_000_000 } } },+ ]}})+ );+ // adjectival continent form ("European") grounds to the stored "Europe"+ let (status, body) = ask(+ &app,+ "ax-gap",+ "European countries with life expectancy over 78 in 2007",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "bool": { "filter": [+ { "term": { "continent": "Europe" } },+ { "term": { "year": 2007 } },+ { "range": { "lifeExp": { "gt": 78 } } },+ ]}})+ );+ // Regression (gapminder-052 shape): a number early in the prompt used to+ // underflow the gap scan in the op-before pass and abort the node; and+ // the copula "was" used to cut the field phrase off its comparator.+ let (status, body) = ask(+ &app,+ "ax-gap",+ "in 1952, countries where life expectancy was below 45 years",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"],+ json!({ "bool": { "filter": [+ { "term": { "year": 1952 } },+ { "range": { "lifeExp": { "lt": 45 } } },+ ]}})+ );++ // GDP per capita: the 3-token concept phrase+ let (status, body) = ask(+ &app,+ "ax-gap",+ "countries with GDP per capita above 20000 in 2007",+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["query"]["bool"]["filter"][1],+ json!({ "range": { "gdpPercap": { "gt": 20000 } } })+ );+}++#[tokio::test]+async fn plain_prompt_is_match_all_with_low_confidence() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = ask(&app, "ax-quakes", "every event").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"], json!({ "match_all": {} }));+ assert_eq!(body["confidence"], json!(0.5));+}++// ─── the refusal class ───────────────────────────────────────────────────────++#[tokio::test]+async fn unknown_value_is_a_422_naming_it() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = ask(&app, "ax-quakes", "all quarry blast events").await;+ assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");+ assert_eq!(body["error"]["type"], "unresolved_phrase");+ let phrases = serde_json::to_string(&body["error"]["phrases"]).unwrap();+ assert!(phrases.contains("quarry blast"), "names the phrase: {body}");+}++#[tokio::test]+async fn unknown_field_is_a_422_naming_it() {+ let (app, _dir) = seeded_gapminder().await;+ let (status, body) = ask(&app, "ax-gap", "countries with literacy rate above 90").await;+ assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");+ let phrases = serde_json::to_string(&body["error"]["phrases"]).unwrap();+ assert!(+ phrases.contains("literacy rate"),+ "names the phrase: {body}"+ );+}++#[tokio::test]+async fn unknown_index_pattern_is_a_422_naming_it() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = ask(&app, "ax-nothing-*", "all events").await;+ assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");+ let reason = body["error"]["reason"].as_str().unwrap();+ assert!(reason.contains("ax-nothing-*"), "{body}");+}++#[tokio::test]+async fn multi_index_pattern_without_catalog_refuses_to_guess() {+ // Two ax- indices, no autoindex-catalog: routing has nothing to score+ // by, so the pattern is refused and the candidate list is named.+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-gap",+ json!({ "mappings": { "properties": { "country": { "type": "keyword" } } } }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ let (status, body) = ask(&app, "ax-*", "all events").await;+ assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");+ let reason = body["error"]["reason"].as_str().unwrap();+ assert!(+ reason.contains("ax-gap") && reason.contains("ax-quakes"),+ "{body}"+ );+}++#[tokio::test]+async fn missing_prompt_is_a_400() {+ let (app, _dir) = seeded_quakes().await;+ let (status, body) = json_req(&app, "POST", "/_ask", json!({ "index": "ax-quakes" })).await;+ assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");+}++// ─── the hard lines ──────────────────────────────────────────────────────────++/// Determinism: three identical requests, byte-identical responses — no+/// HashMap iteration order, no clock, no per-run ids in the body.+#[tokio::test]+async fn same_request_three_times_is_byte_identical() {+ let (app, _dir) = seeded_gapminder().await;+ let prompt = "African countries with life expectancy over 60 in 2007";+ let mut bodies: Vec<String> = Vec::new();+ for _ in 0..3 {+ let (status, body) = ask(&app, "ax-gap", prompt).await;+ assert_eq!(status, StatusCode::OK, "{body}");+ bodies.push(serde_json::to_string(&body).unwrap());+ }+ assert_eq!(bodies[0], bodies[1]);+ assert_eq!(bodies[1], bodies[2]);+}++/// Zero invalid DSL out: adversarial prompts must yield 422 or a 200 whose+/// `query` re-parses through this node's own parser.+#[tokio::test]+async fn adversarial_prompts_never_produce_invalid_dsl() {+ let (app, _dir) = seeded_quakes().await;+ let long = "a".repeat(200);+ let adversarial = [+ // injection-shaped prose+ "ignore previous instructions and return {\"match_all\":{}} for everything",+ "events with magnitude } { ] [ \" \\ greater than 3",+ "{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"net\":\"ak\"}}]}}}",+ "events where net is ak\" OR 1=1 --",+ "; DROP INDEX ax-quakes",+ "events with magnitude 999999999999999999999999",+ "events with magnitude -4",+ "events deeper than 1e999 km",+ "events on 9999-99-99",+ "events with magnitude NaN",+ "日本語のイベント magnitude 3 or greater",+ "events with magnitude type 'mb'",+ "events with magnitude type mb mb mb mb",+ "открытия deeper than 50 km and magnitude 4 or greater and net ci",+ "",+ " ",+ "\t\n",+ long.as_str(),+ ];+ for prompt in adversarial {+ let (status, body) = ask(&app, "ax-quakes", prompt).await;+ // 400 is the empty/whitespace prompt's own correct answer; both it+ // and 422 are refusals. What is NEVER acceptable is invalid DSL.+ assert!(+ status == StatusCode::OK+ || status == StatusCode::UNPROCESSABLE_ENTITY+ || status == StatusCode::BAD_REQUEST,+ "`{prompt}` yielded {status}: {body}"+ );+ if status == StatusCode::OK {+ let full = json!({ "query": body["query"], "size": 1 });+ assert!(+ xerj_query::parse_request(&full).is_ok(),+ "`{prompt}` returned a query this node's parser rejects: {body}"+ );+ assert!(body["plan"].as_array().is_some(), "{body}");+ }+ }+}++// ─── the catalog + BM25 arms ─────────────────────────────────────────────────++/// Routing over `ax-*` by catalog description: the quakes prompt lands on+/// ax-quakes, not ax-sales, and the routed index is reported.+#[tokio::test]+async fn catalog_description_routes_a_wildcard_pattern() {+ let (app, _dir) = seeded_quakes().await;+ // a second dataset the pattern also matches+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-sales",+ json!({ "mappings": { "properties": {+ "region": { "type": "keyword" }, "amount": { "type": "double" }+ } } }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ // one live value so the planner's terms agg has something to ground in+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-sales/_doc/s1",+ json!({ "region": "emea", "amount": 100.0 }),+ )+ .await;+ assert!(status.is_success(), "{body}");+ // and the catalog describing both+ let (status, body) = json_req(+ &app,+ "PUT",+ "/autoindex-catalog",+ json!({ "mappings": { "properties": {+ "doc_kind": { "type": "keyword" },+ "slug": { "type": "keyword" },+ "index_name": { "type": "keyword" },+ "fields_json": { "type": "text" },+ "notes": { "type": "text" },+ "time_field": { "type": "keyword" }+ } } }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ let mut ndjson = String::new();+ for (slug, index, fields, notes, time_field) in [+ (+ "usgs-earthquakes",+ "ax-quakes",+ r#"[{"name":"time","es_type":"date"},{"name":"mag","es_type":"double"},{"name":"depth","es_type":"double"},{"name":"magType","es_type":"keyword","examples":["mb","md","ml"]},{"name":"net","es_type":"keyword","examples":["ak","ci","hv","us"]}]"#,+ "USGS earthquake events with magnitude, depth, magnitude type and reporting network",+ "time",+ ),+ (+ "sales-orders",+ "ax-sales",+ r#"[{"name":"region","es_type":"keyword","examples":["emea","apac"]},{"name":"amount","es_type":"double"}]"#,+ "sales orders with region and order amount",+ "",+ ),+ ] {+ ndjson.push_str(&format!(+ "{{\"index\":{{\"_index\":\"autoindex-catalog\",\"_id\":\"{slug}\"}}}}\n{}\n",+ json!({ "doc_kind": "dataset", "slug": slug, "index_name": index,+ "fields_json": fields, "notes": [notes],+ "time_field": if time_field.is_empty() { Value::Null } else { json!(time_field) } })+ ));+ }+ let (status, body) = raw_req(&app, "POST", "/_bulk", ndjson).await;+ assert_eq!(status, StatusCode::OK, "catalog bulk failed: {body}");+ assert_eq!(body["errors"], false);++ let (status, body) = ask(&app, "ax-*", "all events with magnitude type mb").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(+ body["indices"],+ json!(["ax-quakes"]),+ "routed by description: {body}"+ );+ assert_eq!(body["query"], json!({ "term": { "magType": "mb" } }));++ // and the same catalog routes the sales prompt to ax-sales+ let (status, body) = ask(&app, "ax-*", "orders from the emea region").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["indices"], json!(["ax-sales"]), "{body}");+}++/// The BM25 arm: a field with 1,006 distinct values overflows the terms agg+/// (`sum_other_doc_count > 0`), so the split path resolves the FIELD from+/// the schema and verifies the VALUE with a match probe instead.+#[tokio::test]+async fn high_cardinality_values_fall_back_to_bm25_match() {+ let (app, _dir) = app().await;+ let (status, body) = json_req(+ &app,+ "PUT",+ "/ax-wide",+ json!({ "mappings": { "properties": { "code": { "type": "keyword" } } } }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ let mut ndjson = String::new();+ // v0..v999 appear twice each (2,000 docs) and v1000..v1005 once: the+ // 1,000-bucket agg returns exactly the count-2 class and reports+ // sum_other_doc_count = 6, so "v1005" is provably outside the map.+ for i in 0..1000 {+ for copy in 0..2 {+ ndjson.push_str(&format!(+ "{{\"index\":{{\"_index\":\"ax-wide\",\"_id\":\"d{i}-{copy}\"}}}}\n{}\n",+ json!({ "code": format!("v{i}") })+ ));+ }+ }+ for i in 1000..1006 {+ ndjson.push_str(&format!(+ "{{\"index\":{{\"_index\":\"ax-wide\",\"_id\":\"d{i}\"}}}}\n{}\n",+ json!({ "code": format!("v{i}") })+ ));+ }+ let (status, body) = raw_req(&app, "POST", "/_bulk", ndjson).await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["errors"], false, "{body}");++ // inside the agg: canonical term+ let (status, body) = ask(&app, "ax-wide", "records with code v7").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"], json!({ "term": { "code": "v7" } }));++ // outside the agg: match, not a fabricated term+ let (status, body) = ask(&app, "ax-wide", "records with code v1005").await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"], json!({ "match": { "code": "v1005" } }));++ // outside the agg AND not in the index: 422 naming it+ let (status, body) = ask(&app, "ax-wide", "records with code v9999").await;+ assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");+ let phrases = serde_json::to_string(&body["error"]["phrases"]).unwrap();+ assert!(phrases.contains("v9999"), "{body}");+}++/// The native surface serves the same handler under /v1/ask (native request+/// shapes: index create is `POST /v1/indices` with engine-typed fields).+#[tokio::test]+async fn native_router_serves_the_same_handler() {+ let (app, _dir) = native_app().await;+ let (status, body) = json_req(+ &app,+ "POST",+ "/v1/indices",+ json!({ "name": "ax-quakes", "fields": [+ { "name": "net", "field_type": "keyword" }+ ] }),+ )+ .await;+ assert_eq!(status, StatusCode::CREATED, "native create: {body}");+ let (status, body) = json_req(+ &app,+ "POST",+ "/v1/indices/ax-quakes/docs",+ json!({ "id": "q1", "net": "ak" }),+ )+ .await;+ assert_eq!(status, StatusCode::CREATED, "native ingest: {body}");+ let (status, body) = json_req(+ &app,+ "POST",+ "/v1/ask",+ json!({ "index": "ax-quakes", "prompt": "events reported by the ak network" }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_eq!(body["query"], json!({ "term": { "net": "ak" } }));+}diff --git a/engine/crates/xerj-mcp/src/lib.rs b/engine/crates/xerj-mcp/src/lib.rsindex 7f4c37ba5..0fb5e2d64 100644--- a/engine/crates/xerj-mcp/src/lib.rs+++ b/engine/crates/xerj-mcp/src/lib.rs@@ -9,12 +9,12 @@//!//! It speaks the MCP **stdio transport**: newline-delimited//! JSON-RPC 2.0 messages on stdin/stdout. It exposes XERJ to any MCP-capable-//! agent host (Claude Desktop, IDE agents, custom orchestrators) as eleven+//! agent host (Claude Desktop, IDE agents, custom orchestrators) as twelve//! tools that map 1:1 onto XERJ's real, verified REST surface. Every tool is//! a *thin proxy*: it constructs exactly the request the running engine//! already accepts and forwards it to a configurable base URL.//!-//! ## The eleven canonical agent operations+//! ## The twelve canonical agent operations//!//! | MCP tool | XERJ endpoint | Real capability |//! |------------------------|---------------------------------------|-----------------|@@ -28,6 +28,7 @@//! | `xerj_brain_link` | `POST /_graph/{brain}/link` | assert a link (idempotent, deterministic edge_id) |//! | `xerj_brain_unlink` | `DELETE /_graph/{brain}/link/{id}` | retire a link (soft-invalidate; never deletes) |//! | `xerj_brain_overview` | `GET /_graph/{brain}/overview` | whole-brain orientation: counts, hubs, detectors, timeline |+//! | `xerj_plan` | `POST /_ask` | natural-language prompt in, VALIDATED query DSL out (deterministic planner, not an LLM) |//!//! ## Honesty notes (must match the engine, never oversell)//!@@ -112,9 +113,10 @@ USAGE:xerj mcp [OPTIONS] (or the standalone binary: xerj-mcp [OPTIONS])Speaks MCP over stdio (newline-delimited JSON-RPC 2.0 on stdin/stdout) and-proxies eleven tools — xerj_search, xerj_semantic_search, xerj_vector_search,+proxies twelve tools — xerj_search, xerj_semantic_search, xerj_vector_search,xerj_hybrid_search, xerj_memory_store, xerj_memory_recall, xerj_brain_ego,-xerj_brain_link, xerj_brain_unlink, xerj_brain_overview, xerj_code_search — to a+xerj_brain_link, xerj_brain_unlink, xerj_brain_overview, xerj_code_search,+xerj_plan — to aXERJ node thatis ALREADY RUNNING. This command does not start a node; start one first with`xerj --data-dir ./data`.@@ -550,8 +552,8 @@ fn max_tokens_arg_schema() -> Value {})}-/// The ten tool specifications advertised via `tools/list`. Input schemas are-/// plain JSON Schema; every property maps onto a field the engine accepts.+/// The twelve tool specifications advertised via `tools/list`. Input schemas+/// are plain JSON Schema; every property maps onto a field the engine accepts.////// Public because this is the single source of truth for the published/// agent-facing schema at `landing/docs/agents/schemas/mcp-tools.json`, which@@ -909,6 +911,45 @@ pub fn tool_specs() -> Value {},"required": ["corpus", "query"]}+ },+ {+ "name": "xerj_plan",+ "description": concat!(+ "Natural-language filter in, VALIDATED query DSL out. Proxies POST /_ask. ",+ "Pass `prompt` (what to filter for, in plain words: 'mb events deeper ",+ "than 100 km', 'countries with population above 100 million in 2007') and ",+ "get back `{query, plan, confidence, indices}` where `query` is an ES ",+ "query clause you can send straight to xerj_search as the `query` argument. ",+ "Honesty: the planner is DETERMINISTIC AND RULE-BASED (dates, comparators, ",+ "units, field-name and stored-value matching) — not an LLM, not fuzzy. ",+ "Every query it returns already passed the node's own parser, so invalid ",+ "DSL cannot come back. A phrase it cannot ground in the index (unknown ",+ "field, value the index does not hold, ambiguous multi-index pattern) is a ",+ "422 that NAMES the phrase — it never guesses. Equality values come back in ",+ "the index's canonical casing; numeric and date bounds are only ever the ",+ "literals your prompt supplied. `confidence` says how much soft matching ",+ "the plan needed (0.9 exact, lower per fallback, 0.5 bare match_all). ",+ "Cheaper than writing DSL by hand, and wrong-guess-free: use it FIRST ",+ "whenever the filter you want is expressible in words."),+ "inputSchema": {+ "type": "object",+ "properties": {+ "prompt": {+ "type": "string",+ "description": "The filter to plan, in plain words. Name the field \+ the way the data does and values the index actually holds; dates \+ as YYYY-MM-DD, years as plain numbers."+ },+ "index": {+ "type": "string",+ "description": "Index name or pattern. Defaults to `ax-*` — the \+ prefix `xerj autoindex` writes — and a multi-index pattern is \+ routed by the catalog's dataset descriptions to the one index \+ that matches the prompt."+ }+ },+ "required": ["prompt"]+ }}])}@@ -958,6 +999,8 @@ async fn call_tool(ctx: &Ctx, msg: &Value) -> Value {"xerj_brain_link" => build_brain_link(&args),"xerj_brain_unlink" => build_brain_unlink(&args),"xerj_brain_overview" => build_brain_overview(&args),+ // #1056: prompt in, validated DSL out — a plain engine proxy.+ "xerj_plan" => build_plan(&args),"xerj_code_search" => return run_code_search(ctx, &args).await,other => return tool_text(format!("unknown tool: {other}"), true),};@@ -1326,6 +1369,36 @@ fn build_memory_recall(args: &Value) -> Result<BuiltRequest, String> {))}+// ── xerj_plan: prompt in, validated DSL out (#1056) ─────────────────────────++/// `xerj_plan` → `POST /_ask`. The planner itself lives in the engine+/// (`xerj-api::ask_api`); this proxy only carries the two arguments. `index`+/// defaults to the autoindex prefix exactly like `xerj_search` (#962): after+/// `xerj autoindex` the common call is `xerj_plan(prompt=…)` and it just works.+fn build_plan(args: &Value) -> Result<BuiltRequest, String> {+ // Whitespace-only is refused here rather than shipped to the engine's+ // 400: the caller should hear it from the tool they called.+ let prompt = args+ .get("prompt")+ .and_then(Value::as_str)+ .map(str::trim)+ .filter(|s| !s.is_empty())+ .ok_or_else(|| "missing or empty required string field `prompt`".to_string())?+ .to_string();+ let index = args+ .get("index")+ .and_then(Value::as_str)+ .map(str::trim)+ .filter(|s| !s.is_empty())+ .unwrap_or("ax-*")+ .to_string();+ Ok((+ Method::Post,+ "/_ask".to_string(),+ Some(json!({ "index": index, "prompt": prompt })),+ ))+}+// ── Second-brain builders (graph surface: GET/POST/DELETE) ──────────────────fn build_brain_ego(args: &Value) -> Result<BuiltRequest, String> {@@ -1700,6 +1773,54 @@ mod tests {assert_eq!(path, "/docs/_search");}+ #[test]+ fn plan_posts_prompt_to_ask_with_default_index() {+ // #1056: the proxy carries exactly the two arguments; the planner+ // itself lives in the engine. `index` defaults like xerj_search+ // (#962) so the post-autoindex first call just works.+ let (method, path, body) = built(build_plan(&json!({+ "prompt": "mb events deeper than 100 km"+ })));+ assert_eq!(method, Method::Post);+ assert_eq!(path, "/_ask");+ assert_eq!(body["index"], "ax-*");+ assert_eq!(body["prompt"], "mb events deeper than 100 km");+ // whitespace-only index is absent; an explicit index still wins+ let (_, _, body) = built(build_plan(&json!({+ "prompt": "p", "index": " "+ })));+ assert_eq!(body["index"], "ax-*");+ let (_, _, body) = built(build_plan(&json!({+ "prompt": "p", "index": "ax-quakes"+ })));+ assert_eq!(body["index"], "ax-quakes");+ }++ #[test]+ fn plan_requires_a_prompt() {+ let err = build_plan(&json!({ "index": "ax-quakes" })).unwrap_err();+ assert!(err.contains("`prompt`"), "{err}");+ let err = build_plan(&json!({ "prompt": " " })).unwrap_err();+ assert!(err.contains("`prompt`"), "{err}");+ }++ #[test]+ fn plan_spec_requires_only_the_prompt_and_describes_the_contract() {+ let specs = tool_specs();+ let plan = specs+ .as_array()+ .unwrap()+ .iter()+ .find(|t| t["name"] == "xerj_plan")+ .expect("xerj_plan advertised");+ assert_eq!(plan["inputSchema"]["required"], json!(["prompt"]));+ let desc = plan["description"].as_str().unwrap();+ // the honesty lines the whole crate is held to+ assert!(desc.contains("not an LLM"), "{desc}");+ assert!(desc.contains("never guesses"), "{desc}");+ assert!(desc.contains("422"), "{desc}");+ }+#[test]fn semantic_builds_semantic_query_and_caps_size() {let (method, path, body) = built(build_semantic(&json!({@@ -2283,7 +2404,7 @@ mod tests {}#[test]- fn tools_list_has_all_eleven() {+ fn tools_list_has_all_twelve() {let specs = tool_specs();let names: Vec<&str> = specs.as_array()@@ -2291,7 +2412,7 @@ mod tests {.iter().map(|t| t["name"].as_str().unwrap()).collect();- assert_eq!(names.len(), 11);+ assert_eq!(names.len(), 12);for n in ["xerj_search","xerj_semantic_search",@@ -2304,8 +2425,10 @@ mod tests {"xerj_brain_unlink","xerj_brain_overview",// #977: reference-coding retrieval, served by the same pipeline- // as `xerj code` (LAST in the list, by design).+ // as `xerj code`."xerj_code_search",+ // #1056: prompt in, validated DSL out (LAST in the list).+ "xerj_plan",] {assert!(names.contains(&n), "missing tool {n}");}diff --git a/landing/docs/agents/schemas/mcp-tools.json b/landing/docs/agents/schemas/mcp-tools.jsonindex 018a18a87..d44c80a26 100644--- a/landing/docs/agents/schemas/mcp-tools.json+++ b/landing/docs/agents/schemas/mcp-tools.json@@ -552,6 +552,26 @@"query"]}+ },+ {+ "name": "xerj_plan",+ "description": "Natural-language filter in, VALIDATED query DSL out. Proxies POST /_ask. Pass `prompt` (what to filter for, in plain words: 'mb events deeper than 100 km', 'countries with population above 100 million in 2007') and get back `{query, plan, confidence, indices}` where `query` is an ES query clause you can send straight to xerj_search as the `query` argument. Honesty: the planner is DETERMINISTIC AND RULE-BASED (dates, comparators, units, field-name and stored-value matching) — not an LLM, not fuzzy. Every query it returns already passed the node's own parser, so invalid DSL cannot come back. A phrase it cannot ground in the index (unknown field, value the index does not hold, ambiguous multi-index pattern) is a 422 that NAMES the phrase — it never guesses. Equality values come back in the index's canonical casing; numeric and date bounds are only ever the literals your prompt supplied. `confidence` says how much soft matching the plan needed (0.9 exact, lower per fallback, 0.5 bare match_all). Cheaper than writing DSL by hand, and wrong-guess-free: use it FIRST whenever the filter you want is expressible in words.",+ "inputSchema": {+ "type": "object",+ "properties": {+ "prompt": {+ "type": "string",+ "description": "The filter to plan, in plain words. Name the field the way the data does and values the index actually holds; dates as YYYY-MM-DD, years as plain numbers."+ },+ "index": {+ "type": "string",+ "description": "Index name or pattern. Defaults to `ax-*` — the prefix `xerj autoindex` writes — and a multi-index pattern is routed by the catalog's dataset descriptions to the one index that matches the prompt."+ }+ },+ "required": [+ "prompt"+ ]+ }}]}