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.

corpus add / xerj code: use a completed licence review when the detector reads UNKNOWN (#1910)

xerj-org8372d0d1f798
diff --git a/engine/crates/xerj-autoindex/src/xc.rs b/engine/crates/xerj-autoindex/src/xc.rs
index e6ebd9b9c..ca97b727b 100644
--- a/engine/crates/xerj-autoindex/src/xc.rs
+++ b/engine/crates/xerj-autoindex/src/xc.rs
@@ -519,6 +519,10 @@ fn run_corpus_add(args: &[String]) -> i32 {
// three-slot tuple tripped clippy's type_complexity on the pinned
// CI toolchain.
let mut pin_query: Option<manifest::QueryHints> = None;
+ // repo -> (sha, review) from the hub pin: a human's licence review of the
+ // repo at that sha, recorded when the checkout lands there (#1893).
+ let mut pin_reviews: std::collections::HashMap<String, (String, Value)> =
+ std::collections::HashMap::new();
let (name, rows): (String, Vec<(String, String, String, String)>) = if let Some(path) = &from {
let hub = match manifest::read_hub_manifest(Path::new(path)) {
Ok(h) => h,
@@ -536,6 +540,11 @@ fn run_corpus_add(args: &[String]) -> i32 {
};
println!("rebuilding corpus '{resolved}' from {path}");
pin_query = hub.query;
+ for r in &hub.rows {
+ if let (Some(review), false) = (&r.review, r.sha.is_empty()) {
+ pin_reviews.insert(r.repo.clone(), (r.sha.clone(), review.clone()));
+ }
+ }
(
resolved,
hub.rows
@@ -594,12 +603,21 @@ fn run_corpus_add(args: &[String]) -> i32 {
// review{} blocks are PRESERVED across a rebuild when repo+sha match —
// a human's licence review survives a re-clone (additive, never edited).
let previous = manifest::read_corpus_manifest(&dest.join("corpus.json")).ok();
+ // A hub pin's review of the same repo at the same sha comes next: it is
+ // the review the registry shipped for exactly this checkout (#1893).
let carry = |repo: &str, sha: &str| -> Option<Value> {
- let m = previous.as_ref()?;
- m.repos.iter().find_map(|r| {
- (r.repo == repo && r.sha == sha)
- .then(|| r.review.clone())
- .flatten()
+ let kept = previous.as_ref().and_then(|m| {
+ m.repos.iter().find_map(|r| {
+ (r.repo == repo && r.sha == sha)
+ .then(|| r.review.clone())
+ .flatten()
+ })
+ });
+ kept.or_else(|| {
+ pin_reviews
+ .get(repo)
+ .filter(|(pinned, _)| !sha.is_empty() && pinned == sha)
+ .map(|(_, review)| review.clone())
})
};
@@ -1034,16 +1052,30 @@ fn record(
" licence={lic} sha={} files={files}",
&sha[..12.min(sha.len())]
);
- if let Some(w) = xccode::licence::clone_warning_line(&lic) {
+ let review = carry(repo_name, &sha);
+ // The recorded `licence` stays the detector's reading; a completed human
+ // review fills in a licence the detector could not identify (#1893), and
+ // that is what `xerj code` labels passages with.
+ let shown = xccode::licence::effective_licence(&lic, review.as_ref());
+ if shown != lic {
+ let by = review
+ .as_ref()
+ .and_then(|r| r.get("by"))
+ .and_then(Value::as_str)
+ .unwrap_or("");
+ println!(
+ " licence {shown} per the manifest's review by {by} (the detector read {lic})"
+ );
+ }
+ if let Some(w) = xccode::licence::clone_warning_line(&shown) {
eprintln!("{w}");
}
// The detector is text-matching heuristics and has been wrong before.
// When a manifest disagrees with the checkout, say so rather than
// overwrite quietly.
- if !declared.is_empty() && declared != lic {
+ if !declared.is_empty() && declared != shown {
eprintln!(" ! manifest says licence={declared}, checkout reads {lic} — verify before copying");
}
- let review = carry(repo_name, &sha);
entries.push(manifest::ManifestRepo {
repo: repo_name.to_string(),
url: url.to_string(),
diff --git a/engine/crates/xerj-common/src/xccode/licence.rs b/engine/crates/xerj-common/src/xccode/licence.rs
index b883b0537..6c3560d2e 100644
--- a/engine/crates/xerj-common/src/xccode/licence.rs
+++ b/engine/crates/xerj-common/src/xccode/licence.rs
@@ -63,6 +63,56 @@ pub fn is_restricted(lic: &str) -> bool {
RESTRICTED.iter().any(|p| lic.contains(p))
}
+/// The licence a human REVIEW recorded for a repo at its pin (the `review`
+/// block a vetted hub manifest carries: `spdx`, `use`, `by`, `at`, `note`).
+/// Only a completed review counts: `by` must name the reviewer and `use` must
+/// not be `UNREVIEWED` (draft rows carry an `spdx` hint with an empty `by`).
+/// The SPDX string must look like one — letters, digits and `.+-/ ()`, at
+/// most 64 chars — so an untrusted manifest cannot put arbitrary text into
+/// the terminal through it.
+pub fn reviewed_spdx(review: Option<&serde_json::Value>) -> Option<String> {
+ let r = review?.as_object()?;
+ let field = |k: &str| {
+ r.get(k)
+ .and_then(serde_json::Value::as_str)
+ .unwrap_or("")
+ .trim()
+ };
+ let spdx = field("spdx");
+ let by = field("by");
+ let used = field("use");
+ let looks_spdx = !spdx.is_empty()
+ && spdx.len() <= 64
+ && spdx
+ .chars()
+ .all(|c| c.is_ascii_alphanumeric() || ".+-/ ()".contains(c));
+ if !looks_spdx || by.is_empty() || used.is_empty() || used.eq_ignore_ascii_case("UNREVIEWED") {
+ return None;
+ }
+ Some(spdx.to_string())
+}
+
+/// True when the detector could not name a licence at all (as opposed to
+/// naming a restricted one).
+pub fn is_unidentified(lic: &str) -> bool {
+ matches!(lic.trim(), "" | "UNKNOWN" | "NONE-FOUND")
+}
+
+/// The licence to label a repo's passages with: the recorded detector string,
+/// unless the detector could not identify one and a completed human review
+/// names it (issue #1893: the detector read UNKNOWN on a CC0-1.0 repo whose
+/// review verified CC0-1.0, and every passage said "do not copy"). A review
+/// never overrides a licence the detector DID identify — a restricted
+/// detection still warns.
+pub fn effective_licence(recorded: &str, review: Option<&serde_json::Value>) -> String {
+ if is_unidentified(recorded) {
+ if let Some(spdx) = reviewed_spdx(review) {
+ return spdx;
+ }
+ }
+ recorded.to_string()
+}
+
/// Find the licence without guessing: read the file(s) the project actually
/// ships. Dual-licensed crates (very common in Rust) ship LICENSE-MIT +
/// LICENSE-APACHE and no plain LICENSE, so every candidate is collected
@@ -231,6 +281,33 @@ mod tests {
assert!(clone_warning_line("Apache-2.0/MIT").is_none());
}
+ // ── a completed human review names an unidentified licence (#1893) ──
+
+ #[test]
+ fn a_completed_review_names_an_unknown_licence() {
+ let review = serde_json::json!({
+ "spdx": "CC0-1.0", "use": "adapt-with-attribution", "by": "xerj-org",
+ "at": "2026-10-13", "note": "LICENSE.md verified. Detector reported UNKNOWN."
+ });
+ assert_eq!(effective_licence("UNKNOWN", Some(&review)), "CC0-1.0");
+ assert_eq!(effective_licence("NONE-FOUND", Some(&review)), "CC0-1.0");
+ assert!(clone_warning_line(&effective_licence("UNKNOWN", Some(&review))).is_none());
+ // A licence the detector DID identify is never overridden.
+ assert_eq!(effective_licence("GPL-3.0", Some(&review)), "GPL-3.0");
+ assert_eq!(effective_licence("UNKNOWN", None), "UNKNOWN");
+ }
+
+ #[test]
+ fn a_draft_or_odd_review_does_not_count() {
+ let draft = serde_json::json!({ "spdx": "MIT", "use": "UNREVIEWED", "by": "", "at": "" });
+ assert_eq!(effective_licence("UNKNOWN", Some(&draft)), "UNKNOWN");
+ let no_by = serde_json::json!({ "spdx": "MIT", "use": "adapt-with-attribution", "by": "" });
+ assert_eq!(reviewed_spdx(Some(&no_by)), None);
+ let hostile = serde_json::json!({ "spdx": "MIT\u{1b}[2J", "use": "x", "by": "y" });
+ assert_eq!(reviewed_spdx(Some(&hostile)), None);
+ assert_eq!(reviewed_spdx(Some(&serde_json::json!("MIT"))), None);
+ }
+
// ── the detector's two ordering traps ──────────────────────────────────
#[test]
diff --git a/engine/crates/xerj-common/src/xccode/manifest.rs b/engine/crates/xerj-common/src/xccode/manifest.rs
index f7174f8eb..43f7379a2 100644
--- a/engine/crates/xerj-common/src/xccode/manifest.rs
+++ b/engine/crates/xerj-common/src/xccode/manifest.rs
@@ -101,17 +101,25 @@ pub fn read_corpus_manifest(path: &Path) -> Result<CorpusManifest, String> {
serde_json::from_str(&raw).map_err(|e| format!("{} is not valid JSON: {e}", path.display()))
}
-/// repo -> recorded licence for a corpus. ALWAYS the recorded string, never
+/// repo -> recorded licence for a corpus. ALWAYS the recorded data, never
/// re-derived at query time: the licence decision was made once, at clone
/// time, against the checkout — re-deriving it per query is how prose drift
-/// becomes load-bearing.
+/// becomes load-bearing. The recorded data includes the repo's human review:
+/// when the detector could not identify a licence and a completed review
+/// names one, the review's SPDX is the label (#1893, see
+/// [`super::licence::effective_licence`]).
pub fn licence_map(root: &Path, corpus: &str) -> HashMap<String, String> {
let path = root.join("corpora").join(corpus).join("corpus.json");
read_corpus_manifest(&path)
.map(|m| {
m.repos
.iter()
- .map(|r| (r.repo.clone(), r.licence.clone()))
+ .map(|r| {
+ (
+ r.repo.clone(),
+ super::licence::effective_licence(&r.licence, r.review.as_ref()),
+ )
+ })
.collect()
})
.unwrap_or_default()
@@ -205,6 +213,10 @@ pub struct HubRow {
pub url: String,
pub sha: String,
pub declared_licence: String,
+ /// The row's `review` block, verbatim (a human's licence review of the
+ /// repo at `sha`). `xerj corpus add` records it in the cloned corpus.json
+ /// when the checkout lands at that same sha (#1893).
+ pub review: Option<Value>,
}
/// A whole validated hub manifest: the corpus name plus its rows. The
@@ -275,6 +287,7 @@ pub fn read_hub_manifest(path: &Path) -> Result<HubManifest, String> {
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
+ review: r.get("review").filter(|v| v.is_object()).cloned(),
});
}
Ok(HubManifest {
@@ -391,6 +404,40 @@ mod tests {
assert!(licence_map(&dir, "never-cloned").is_empty());
}
+ #[test]
+ fn licence_map_uses_a_completed_review_for_an_unknown_licence() {
+ // #1893: 18f-content-guide's checkout reads UNKNOWN, its review says
+ // CC0-1.0; passages must be labelled CC0-1.0, not "do not copy".
+ let dir = tmp();
+ let pin = dir.join("18f.json");
+ let sha = "1b1723d3d5b8f91d92c16487c88b56265dc0ec3a";
+ let review = json!({ "spdx": "CC0-1.0", "use": "adapt-with-attribution", "by": "xerj-org", "at": "2026-10-13", "note": "LICENSE.md verified" });
+ std::fs::write(&pin, json!({ "corpus": "18f-content-guide", "repos": [ { "repo": "content-guide", "url": "https://github.com/18F/content-guide", "licence": "CC0-1.0", "sha": sha, "review": review } ] }).to_string()).unwrap();
+ let hub = read_hub_manifest(&pin).unwrap();
+ assert_eq!(hub.rows[0].review.as_ref(), Some(&review));
+
+ let cdir = dir.join("corpora").join("18f-content-guide");
+ std::fs::create_dir_all(&cdir).unwrap();
+ write_corpus_manifest_kind(
+ &cdir.join("corpus.json"),
+ "18f-content-guide",
+ None,
+ "2026-10-19T00:00:00Z",
+ &[ManifestRepo {
+ repo: "content-guide".into(),
+ url: "https://github.com/18F/content-guide".into(),
+ licence: "UNKNOWN".into(),
+ sha: sha.into(),
+ files: None,
+ bytes: None,
+ review: hub.rows[0].review.clone(),
+ }],
+ None,
+ );
+ let m = licence_map(&dir, "18f-content-guide");
+ assert_eq!(m.get("content-guide").map(String::as_str), Some("CC0-1.0"));
+ }
+
#[test]
fn hub_validation_rejects_path_escapes_and_short_shas() {
let dir = tmp();