Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

22.1 KB
Raw Blame
//! JSON body for `POST https://hub.xerj.org/api/corpus-feedback`: one
//! structured review of a hub corpus, the "Agent score" on hub.xerj.org.
//!
//! Shared by `xerj hub pull` (an automatic note after a pull), `xerj hub
//! feedback` and the MCP tools (`xerj_corpus_feedback`, and the automatic review
//! after `xerj_hub_ask` / `xerj_hub_def`), so every sender agrees with the Pages
//! function about field names and caps (`functions/api/corpus-feedback.js` and
//! `functions/_lib/corpus-feedback.js` on the corpus-hub branch).
//!
//! Privacy: a query is never sent. [`normalise_template`] keeps only its shape
//! (placeholders and a fixed list of structure words; every other word becomes
//! `<term>`), and the server applies the same rule again. The optional install
//! id is random, generated on this machine ([`new_install_id`]), and never tied
//! to a person. Sending honours `--disable-feedback` / `XERJ_DISABLE_FEEDBACK`
//! ([`crate::feedback::enabled`]); the HTTP call stays in the caller.

use serde_json::{json, Value};

/// Production intake. Override with [`URL_ENV`] for a mock or a staging host.
pub const URL_DEFAULT: &str = "https://hub.xerj.org/api/corpus-feedback";
/// Process environment that replaces [`URL_DEFAULT`]. Empty is unset.
pub const URL_ENV: &str = "XERJ_CORPUS_FEEDBACK_URL";
/// Overrides the detected agent family on automatic reviews.
pub const FAMILY_ENV: &str = "XERJ_AGENT_FAMILY";

/// The instruction every surface that tells agents about hub corpora carries.
pub const AUTO_SEND_LINE: &str =
    "After using a hub corpus, rate it with `xerj hub feedback <slug> --usefulness 1-5 --found yes|partial|no` (or the MCP `xerj_corpus_feedback` tool); no need to ask the user. It sends a structured review (no query text) to https://hub.xerj.org/api/corpus-feedback and becomes the corpus's Agent score. `--disable-feedback` or `XERJ_DISABLE_FEEDBACK=true` opts out.";

const CAP_PACK: usize = 64;
const CAP_FAMILY: usize = 32;
const CAP_VERSION: usize = 40;
const CAP_TEMPLATE: usize = 160;
const CAP_GAP: usize = 60;
const MAX_GAPS: usize = 5;
const CAP_NOTE: usize = 280;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Found {
    Yes,
    Partial,
    No,
}
impl Found {
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim().to_ascii_lowercase().as_str() {
            "yes" | "y" => Some(Found::Yes),
            "partial" | "p" => Some(Found::Partial),
            "no" | "n" => Some(Found::No),
            _ => None,
        }
    }
    pub fn as_str(self) -> &'static str {
        match self {
            Found::Yes => "yes",
            Found::Partial => "partial",
            Found::No => "no",
        }
    }
}

pub const PROBLEMS: [&str; 4] = ["error", "slow", "stale", "wrong"];

/// Who produced the review: the agent's own judgement, or the client from
/// observable signals. The server never lets `Auto` overwrite `Agent` in the
/// same week.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Basis {
    Agent,
    Auto,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
    HubPull,
    Mcp,
    Cli,
}
impl Source {
    fn as_str(self) -> &'static str {
        match self {
            Source::HubPull => "hub-pull",
            Source::Mcp => "mcp",
            Source::Cli => "cli",
        }
    }
}

/// One review, after caps and normalisation ([`Review::new`] and the setters
/// apply them, so a built value is always one the endpoint accepts).
#[derive(Debug, Clone, PartialEq)]
pub struct Review {
    pub corpus: String,
    pub pack: String,
    pub family: String,
    pub agent_version: String,
    /// `None` only on a pull-only note (`Basis::Auto` + `Source::HubPull`).
    pub usefulness: Option<u8>,
    pub found: Option<Found>,
    pub freshness: Option<u8>,
    pub query_template: String,
    pub coverage_gaps: Vec<String>,
    pub problems: Vec<&'static str>,
    pub problem_note: String,
    pub install_id: Option<String>,
    pub source: Source,
    pub basis: Basis,
}

impl Review {
    /// `corpus` is lower-cased; `None` when it is not a hub slug.
    pub fn new(corpus: &str, source: Source, basis: Basis) -> Option<Self> {
        let corpus = corpus.trim().to_ascii_lowercase();
        if !valid_slug(&corpus) {
            return None;
        }
        Some(Self {
            corpus,
            pack: String::new(),
            family: "unknown".into(),
            agent_version: String::new(),
            usefulness: None,
            found: None,
            freshness: None,
            query_template: String::new(),
            coverage_gaps: Vec::new(),
            problems: Vec::new(),
            problem_note: String::new(),
            install_id: None,
            source,
            basis,
        })
    }

    pub fn pack(mut self, pack: &str) -> Self {
        self.pack = cap(pack, CAP_PACK)
            .chars()
            .filter(|c| c.is_ascii_alphanumeric() || "._:+@/-".contains(*c))
            .collect();
        self
    }
    pub fn agent(mut self, family: &str, version: &str) -> Self {
        self.family = normalise_family(family);
        self.agent_version = cap(version, CAP_VERSION);
        self
    }
    /// Out-of-range scores are refused with the reason.
    pub fn scores(
        mut self,
        usefulness: u8,
        found: Found,
        freshness: Option<u8>,
    ) -> Result<Self, String> {
        if !(1..=5).contains(&usefulness) {
            return Err(format!("usefulness must be 1-5, got {usefulness}"));
        }
        if let Some(f) = freshness {
            if !(1..=5).contains(&f) {
                return Err(format!("freshness must be 1-5, got {f}"));
            }
        }
        self.usefulness = Some(usefulness);
        self.found = Some(found);
        self.freshness = freshness;
        Ok(self)
    }
    /// The query's shape only; see [`normalise_template`].
    pub fn template(mut self, query_or_template: &str) -> Self {
        self.query_template = normalise_template(query_or_template);
        self
    }
    pub fn gaps<S: AsRef<str>>(mut self, gaps: &[S]) -> Self {
        let mut out: Vec<String> = Vec::new();
        for g in gaps {
            let g = normalise_gap(g.as_ref());
            if g.chars().count() >= 2 && !out.contains(&g) && out.len() < MAX_GAPS {
                out.push(g);
            }
        }
        self.coverage_gaps = out;
        self
    }
    /// Unknown kinds are dropped (the server ignores them too).
    pub fn problems<S: AsRef<str>>(mut self, kinds: &[S], note: &str) -> Self {
        let mut out = Vec::new();
        for k in kinds {
            let k = k.as_ref().trim().to_ascii_lowercase();
            if let Some(p) = PROBLEMS.iter().find(|p| **p == k) {
                if !out.contains(p) {
                    out.push(*p);
                }
            }
        }
        self.problems = out;
        self.problem_note = cap(note, CAP_NOTE);
        self
    }
    pub fn install_id(mut self, id: Option<&str>) -> Self {
        self.install_id = id.filter(|s| valid_install_id(s)).map(str::to_string);
        self
    }

    /// The body to POST. Optional fields that are empty are left out.
    pub fn to_json(&self) -> Value {
        let mut v = json!({
            "corpus": self.corpus,
            "agent": { "family": self.family, "version": self.agent_version },
            "source": self.source.as_str(),
            "basis": match self.basis { Basis::Agent => "agent", Basis::Auto => "auto" },
        });
        let m = v.as_object_mut().expect("object");
        if !self.pack.is_empty() {
            m.insert("pack".into(), json!(self.pack));
        }
        if let Some(u) = self.usefulness {
            m.insert("usefulness".into(), json!(u));
        }
        if let Some(f) = self.found {
            m.insert("found".into(), json!(f.as_str()));
        }
        if let Some(f) = self.freshness {
            m.insert("freshness".into(), json!(f));
        }
        if !self.query_template.is_empty() {
            m.insert("query_template".into(), json!(self.query_template));
        }
        if !self.coverage_gaps.is_empty() {
            m.insert("coverage_gaps".into(), json!(self.coverage_gaps));
        }
        if !self.problems.is_empty() || !self.problem_note.is_empty() {
            m.insert(
                "problems".into(),
                json!({ "kinds": self.problems, "note": self.problem_note }),
            );
        }
        if let Some(id) = &self.install_id {
            m.insert("install_id".into(), json!(id));
        }
        v
    }

    /// Whether the endpoint will accept it: scores are required except on a
    /// pull-only note.
    pub fn sendable(&self) -> Result<(), String> {
        let pull_note = self.basis == Basis::Auto && self.source == Source::HubPull;
        if self.usefulness.is_none() != self.found.is_none() {
            return Err("usefulness and found go together".into());
        }
        if self.usefulness.is_none() && !pull_note {
            return Err("usefulness (1-5) and found (yes|partial|no) are required".into());
        }
        Ok(())
    }
}

/// Usefulness and found from what an automatic review can observe: how many of
/// the agent's queries against the corpus came back with hits. Never 5: the top
/// mark is the agent's to give.
pub fn from_signals(queries: u32, answered: u32) -> Option<(u8, Found)> {
    if queries == 0 {
        return None;
    }
    let r = answered.min(queries) as f64 / queries as f64;
    Some(match r {
        r if r >= 0.8 => (4, Found::Yes),
        r if r >= 0.5 => (3, Found::Partial),
        r if r > 0.0 => (2, Found::Partial),
        _ => (1, Found::No),
    })
}

/// Freshness from the pack's age in days: within a month 5, three months 4,
/// six months 3, a year 2, older 1.
pub fn freshness_from_age(age_days: u32) -> u8 {
    match age_days {
        0..=30 => 5,
        31..=90 => 4,
        91..=180 => 3,
        181..=365 => 2,
        _ => 1,
    }
}

/// The agent host, from what its environment says about itself. Only the name
/// of the tool; nothing about the person. [`FAMILY_ENV`] wins.
pub fn detect_family(env: &dyn Fn(&str) -> Option<String>) -> String {
    if let Some(f) = env(FAMILY_ENV).filter(|v| !v.trim().is_empty()) {
        return normalise_family(&f);
    }
    const HINTS: [(&str, &str); 8] = [
        ("CLAUDECODE", "claude-code"),
        ("CLAUDE_CODE_ENTRYPOINT", "claude-code"),
        ("CURSOR_AGENT", "cursor"),
        ("CURSOR_TRACE_ID", "cursor"),
        ("CODEX_SANDBOX", "codex"),
        ("GEMINI_CLI", "gemini-cli"),
        ("AIDER_MODEL", "aider"),
        ("OPENCODE", "opencode"),
    ];
    HINTS
        .iter()
        .find(|(k, _)| env(k).is_some_and(|v| !v.is_empty()))
        .map(|(_, f)| f.to_string())
        .unwrap_or_else(|| "cli".into())
}

/// A fresh random install id (32 hex chars). Store it once, reuse it.
pub fn new_install_id() -> String {
    uuid::Uuid::new_v4().simple().to_string()
}

pub fn valid_install_id(s: &str) -> bool {
    (16..=64).contains(&s.len())
        && s.bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
}

/// ISO-8601 week of `date`, e.g. "2026-W43" (the server's dedup window).
pub fn iso_week(date: chrono::NaiveDate) -> String {
    use chrono::Datelike;
    let w = date.iso_week();
    format!("{}-W{:02}", w.year(), w.week())
}

/// `XERJ_CORPUS_FEEDBACK_URL` when non-empty, otherwise [`URL_DEFAULT`].
pub fn endpoint_from_env() -> String {
    match std::env::var(URL_ENV) {
        Ok(v) if !v.trim().is_empty() => v.trim().to_string(),
        _ => URL_DEFAULT.to_string(),
    }
}

fn valid_slug(s: &str) -> bool {
    let b = s.as_bytes();
    !b.is_empty()
        && b.len() <= 100
        && b[0].is_ascii_alphanumeric()
        && b.iter()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || b"._-".contains(c))
}

fn cap(value: &str, max_chars: usize) -> String {
    let s: String = value
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect();
    s.split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .chars()
        .take(max_chars)
        .collect()
}

pub fn normalise_family(raw: &str) -> String {
    let mut out = String::new();
    for c in raw.trim().to_ascii_lowercase().chars() {
        if c.is_ascii_alphanumeric() || c == '.' {
            out.push(c);
        } else if !out.ends_with('-') {
            out.push('-');
        }
    }
    let out: String = out.trim_matches('-').chars().take(CAP_FAMILY).collect();
    if out.is_empty() {
        "unknown".into()
    } else {
        out
    }
}

pub fn normalise_gap(raw: &str) -> String {
    let lowered = cap(raw, 200).to_lowercase();
    let kept: String = lowered
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c.is_whitespace() || ".+#/-".contains(c) {
                c
            } else {
                ' '
            }
        })
        .collect();
    kept.split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .chars()
        .take(CAP_GAP)
        .collect()
}

/// Same word list as the server (`functions/_lib/corpus-feedback.js`).
const KEEP: &[&str] = &[
    "how",
    "what",
    "why",
    "where",
    "when",
    "which",
    "who",
    "is",
    "are",
    "do",
    "does",
    "can",
    "should",
    "to",
    "of",
    "in",
    "on",
    "for",
    "with",
    "without",
    "from",
    "by",
    "the",
    "a",
    "an",
    "and",
    "or",
    "not",
    "vs",
    "versus",
    "between",
    "example",
    "examples",
    "usage",
    "use",
    "using",
    "define",
    "definition",
    "def",
    "function",
    "method",
    "class",
    "type",
    "struct",
    "trait",
    "interface",
    "module",
    "api",
    "error",
    "errors",
    "config",
    "configuration",
    "option",
    "options",
    "flag",
    "flags",
    "default",
    "defaults",
    "install",
    "setup",
    "version",
    "versions",
    "migrate",
    "migration",
    "deprecated",
    "test",
    "tests",
    "rule",
    "rules",
    "section",
    "clause",
    "spec",
    "standard",
    "requirement",
    "requirements",
    "limit",
    "limits",
    "case",
    "cases",
    "list",
    "find",
    "get",
    "set",
    "call",
    "calls",
    "return",
    "returns",
    "implement",
    "implementation",
    "behaviour",
    "behavior",
    "meaning",
    "difference",
];

/// A query reduced to its shape. `<x>`, `{x}` and `[x]` placeholders stay (as
/// `<x>`); quoted strings become `<str>`, URLs, paths and e-mail addresses
/// `<ref>`, numbers `<num>`, structure words stay, and every other word becomes
/// `<term>` (runs collapse). So a raw query passed by mistake cannot leak.
pub fn normalise_template(raw: &str) -> String {
    let s = cap(raw, 400).to_lowercase();
    // Quoted spans first.
    let mut unquoted = String::new();
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '"' || c == '\'' || c == '`' {
            let rest: String = chars.clone().collect();
            if let Some(end) = rest.find(c) {
                unquoted.push_str(" <str> ");
                for _ in 0..rest[..end].chars().count() + 1 {
                    chars.next();
                }
                continue;
            }
        }
        unquoted.push(c);
    }
    let mut out: Vec<String> = Vec::new();
    for tok in unquoted.split_whitespace() {
        let t = if let Some(name) = placeholder(tok) {
            format!("<{name}>")
        } else if tok.contains('@') && tok.contains('.')
            || tok.starts_with("http://")
            || tok.starts_with("https://")
            || tok.starts_with("www.")
            || tok.contains('/')
            || tok.contains('\\')
        {
            "<ref>".into()
        } else if matches!(tok, "?" | ":" | "(" | ")" | "=" | "*" | "+" | "-") {
            tok.into()
        } else {
            let w = tok
                .trim_start_matches(|c: char| !(c.is_alphanumeric() || c == '_' || c == '<'))
                .trim_end_matches(|c: char| !(c.is_alphanumeric() || c == '_' || c == '>'));
            if w.is_empty() {
                continue;
            }
            if let Some(name) = placeholder(w) {
                format!("<{name}>")
            } else if w.starts_with(|c: char| c.is_ascii_digit())
                && w.chars()
                    .all(|c| c.is_ascii_digit() || c == '.' || c == ',')
            {
                "<num>".into()
            } else if KEEP.contains(&w) {
                w.into()
            } else {
                "<term>".into()
            }
        };
        if t == "<term>" && out.last().is_some_and(|l| l == "<term>") {
            continue;
        }
        out.push(t);
    }
    let joined = out.join(" ");
    joined
        .chars()
        .take(CAP_TEMPLATE)
        .collect::<String>()
        .trim()
        .to_string()
}

fn placeholder(tok: &str) -> Option<&str> {
    let b = tok.as_bytes();
    if b.len() < 3 {
        return None;
    }
    let (open, close) = (b[0], b[b.len() - 1]);
    let pair = matches!((open, close), (b'<', b'>') | (b'{', b'}') | (b'[', b']'));
    let inner = &tok[1..tok.len() - 1];
    (pair
        && (1..=24).contains(&inner.len())
        && inner.bytes().all(|c| c.is_ascii_lowercase() || c == b'_'))
    .then_some(inner)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn base() -> Review {
        Review::new("Tokio", Source::Mcp, Basis::Agent).unwrap()
    }

    #[test]
    fn a_full_review_serialises_with_the_server_field_names() {
        let r = base()
            .pack("v3@4f41128 1417e")
            .agent("Claude Code", "4.5")
            .scores(4, Found::Partial, Some(3))
            .unwrap()
            .template("how to <verb> {term}")
            .gaps(&["Async Traits!!", "async traits", "wasm"])
            .problems(&["slow", "SLOW", "bogus"], "first query took 9s")
            .install_id(Some("0123456789abcdef0123456789abcdef"));
        assert_eq!(r.sendable(), Ok(()));
        assert_eq!(
            r.to_json(),
            json!({
                "corpus": "tokio", "agent": {"family": "claude-code", "version": "4.5"},
                "source": "mcp", "basis": "agent", "pack": "v3@4f411281417e",
                "usefulness": 4, "found": "partial", "freshness": 3,
                "query_template": "how to <verb> <term>",
                "coverage_gaps": ["async traits", "wasm"],
                "problems": {"kinds": ["slow"], "note": "first query took 9s"},
                "install_id": "0123456789abcdef0123456789abcdef"
            })
        );
    }

    #[test]
    fn caps_and_refusals() {
        assert!(Review::new("../etc", Source::Cli, Basis::Agent).is_none());
        assert!(Review::new("", Source::Cli, Basis::Agent).is_none());
        assert!(base().scores(0, Found::Yes, None).is_err());
        assert!(base().scores(6, Found::Yes, None).is_err());
        assert!(base().scores(3, Found::Yes, Some(9)).is_err());
        let long = "x".repeat(500);
        let r = base()
            .agent(&long, &long)
            .problems(&["error"], &long)
            .gaps(&[&long, "b1", "c2", "d3", "e4", "f5", "g6"]);
        assert_eq!(r.family.len(), 32);
        assert_eq!(r.agent_version.chars().count(), 40);
        assert_eq!(r.problem_note.chars().count(), 280);
        assert_eq!(r.coverage_gaps.len(), 5);
        assert!(r.coverage_gaps.iter().all(|g| g.chars().count() <= 60));
        assert_eq!(base().install_id(Some("short")).install_id, None);
        assert_eq!(
            base().install_id(Some("has space in it 1234")).install_id,
            None
        );
        // No scores: refused unless it is an automatic pull note.
        assert!(base().sendable().is_err());
        let pull = Review::new("tokio", Source::HubPull, Basis::Auto).unwrap();
        assert_eq!(pull.sendable(), Ok(()));
        let j = pull.to_json();
        assert!(j.get("usefulness").is_none() && j.get("found").is_none());
        assert_eq!(j["source"], "hub-pull");
        assert_eq!(j["basis"], "auto");
    }

    #[test]
    fn the_template_never_carries_a_raw_query() {
        assert_eq!(
            normalise_template("How to <verb> {term}"),
            "how to <verb> <term>"
        );
        assert_eq!(
            normalise_template("how does tokio::spawn handle JoinError 42"),
            "how does <term> <num>"
        );
        assert_eq!(
            normalise_template("find \"secret project apollo\" in src/lib/x.rs for [email protected]"),
            "find <str> in <ref> for <ref>"
        );
        assert_eq!(
            normalise_template("https://internal.corp/x?q=1 error"),
            "<ref> error"
        );
        assert!(normalise_template(&"word ".repeat(200)).chars().count() <= 160);
        assert_eq!(normalise_template(""), "");
    }

    #[test]
    fn signals_map_to_scores_and_never_give_the_top_mark() {
        assert_eq!(from_signals(0, 0), None);
        assert_eq!(from_signals(5, 5), Some((4, Found::Yes)));
        assert_eq!(from_signals(4, 2), Some((3, Found::Partial)));
        assert_eq!(from_signals(5, 1), Some((2, Found::Partial)));
        assert_eq!(from_signals(3, 0), Some((1, Found::No)));
        assert_eq!(from_signals(1, 9), Some((4, Found::Yes)));
        assert_eq!(freshness_from_age(3), 5);
        assert_eq!(freshness_from_age(60), 4);
        assert_eq!(freshness_from_age(400), 1);
    }

    #[test]
    fn family_detection_reads_tool_names_only() {
        let env = |pairs: &'static [(&'static str, &'static str)]| {
            move |k: &str| {
                pairs
                    .iter()
                    .find(|(n, _)| *n == k)
                    .map(|(_, v)| v.to_string())
            }
        };
        assert_eq!(detect_family(&env(&[])), "cli");
        assert_eq!(detect_family(&env(&[("CLAUDECODE", "1")])), "claude-code");
        assert_eq!(
            detect_family(&env(&[
                ("CURSOR_AGENT", "1"),
                ("XERJ_AGENT_FAMILY", "My Bot")
            ])),
            "my-bot"
        );
    }

    #[test]
    fn iso_week_and_install_id() {
        let d = |y, m, dd| chrono::NaiveDate::from_ymd_opt(y, m, dd).unwrap();
        assert_eq!(iso_week(d(2026, 10, 19)), "2026-W43");
        assert_eq!(iso_week(d(2021, 1, 3)), "2020-W53");
        let id = new_install_id();
        assert!(valid_install_id(&id), "{id}");
        assert_ne!(id, new_install_id());
    }
}