Files
wanderbooks/crates/ledger-core/tests/bookspec/source_refs.rs
T
swaitsandClaude Opus 5.5 8d3cc5056f test(core): no retired rule number anywhere, SOURCE clauses checked whole
The retired-reference test now scans the whole text of every tracked file (git ls-files, skipping plan/, docs/spec/rules.md and docs/spec/claims/) for any word-boundary retired number from 1 to 45, wrapped references included, with an exact-count allow-list for the deliberately kept user-visible and golden strings; a count rising or falling fails. The SOURCE test now reads each whole clause with its continuations (crates/*/src, crates/*/tests and fuzz/), checks every bookspec and product-rule id in it, requires every path-like token to exist and every crate code reference to name a real item, counts clauses passing only through the free-text branch against a constant (56; 353 clauses checked), and keeps a floor on the total. The repo_root doc comment became the module header.

Lines the tightened check made fail, fixed in the lines not the test: relative sources/ and claims/ paths in form1099, instruments, kernel entries, money, kernel round and the 1099 tests now give full docs/spec paths; wrapped docs/spec/sources/ and docs/parties-and-attachments.md paths in ledger, store, reconciliation, tins and the GUI books module are joined; settings.rs names the real parser path instead of a runtime filename; the bounds tests name the real kernel and calendar files, and spec_bounds no longer cites rules.md for the line bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:02:08 -07:00

481 lines
16 KiB
Rust

//! The source-citation checks (unit S-E, tightened in review).
//!
//! Every spec block's source clause -- the line that introduces it plus
//! its continuation lines in the same comment block, up to the next
//! blank comment line or the next uppercase heading -- names a rule or
//! a source that exists: a bookspec id the pinned bookspec has, a
//! product-rule id in `docs/spec/claims/`, a repo path that exists, a
//! code reference that names a real item, or a declared external or
//! free-text citation. Path-like and code-reference tokens are checked
//! even when a rule id is also cited; they never fall through to the
//! free-text branch. Clauses that pass only through the free-text
//! branch are counted, and that count is a floor-style constant: it
//! changes only on purpose, in the same commit as the constant.
//!
//! The second check flags any retired rule number (the old wanderbooks
//! numbering, a word-boundary capital letter, hyphen and a number from
//! 1 to 45) in every tracked text file, outside the excluded index and
//! claims files. A deliberately kept occurrence is allow-listed below
//! with its exact count per file; the count failing in either
//! direction means the list changes only on purpose. Scanning the
//! whole file text, not line by line, also catches a reference wrapped
//! across lines, filename at the end of one line and the number at the
//! start of the next behind a comment prefix.
use crate::*;
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}
fn collect_rs(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_rs(&path, out);
} else if path.extension().is_some_and(|e| e == "rs") {
out.push(path);
}
}
}
fn collect_all(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name();
let name = name.to_string_lossy();
if name == "target" || name == ".git" {
continue;
}
if path.is_dir() {
collect_all(&path, out);
} else {
out.push(path);
}
}
}
/// The ids in `text` that start with `prefix`, each cut at the first
/// character that cannot be in an id.
fn ids_in(text: &str, prefix: &str) -> Vec<String> {
let mut ids = Vec::new();
let mut rest = text;
while let Some(at) = rest.find(prefix) {
let tail: String = rest[at..]
.chars()
.take_while(|c| c.is_ascii_alphanumeric() || *c == '-')
.collect();
let body = &tail[prefix.len()..];
if body
.rsplit('-')
.next()
.is_some_and(|n| !n.is_empty() && n.chars().all(|c| c.is_ascii_digit()))
&& body.contains('-')
{
ids.push(tail);
}
rest = &rest[at + prefix.len()..];
}
ids
}
const EXTERNAL: &[&str] = &[
"U.S.C.",
"§",
"ASC ",
"IRS",
"Pub.",
"Form ",
"Treas.",
"Rev. Proc.",
"IRC",
"ISO 4217",
"Unicode",
"Pacioli",
"Instructions for",
"RFC ",
];
/// The number of source clauses checked must stay at or above this.
const CHECKED_FLOOR: usize = 300;
/// Clauses passing only through the declared or external free-text
/// branch. Edit this constant, on purpose, in the same commit that
/// changes the count.
const FREE_TEXT_COUNT: usize = 56;
fn strip_comment(line: &str) -> Option<String> {
let t = line.trim_start();
t.strip_prefix("//!")
.or_else(|| t.strip_prefix("///"))
.or_else(|| t.strip_prefix("//"))
.map(|rest| rest.trim().to_string())
}
fn is_heading(content: &str) -> bool {
let Some((head, _)) = content.split_once(':') else {
return false;
};
!head.is_empty()
&& head.len() <= 32
&& head.chars().filter(|c| c.is_ascii_uppercase()).count() >= 2
&& head
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || " _-/()".contains(c))
}
fn clean_token(token: &str) -> String {
token
.trim_start_matches(['`', '"', '\'', '(', '[', '{'])
.trim_end_matches(['`', '"', '\'', ')', ']', '}', ',', ';', ':', '.'])
.to_string()
}
fn is_path_like(token: &str) -> bool {
[".md", ".rs", ".toml", ".json"]
.iter()
.any(|e| token.ends_with(e))
}
struct ClauseCtx<'a> {
root: &'a Path,
bs_ids: &'a BTreeSet<String>,
wb_ids: &'a BTreeSet<String>,
identifiers: &'a BTreeSet<String>,
basenames: &'a BTreeSet<String>,
}
fn check_clause(clause: &str, ctx: &ClauseCtx<'_>, bad: &mut Vec<String>, at: &str) -> bool {
// Returns true when the clause passes only through the free-text branch.
let Some(text) = clause.split("SOURCE:").nth(1) else {
return false;
};
let mut errors: Vec<String> = Vec::new();
let mut cited = false;
for id in ids_in(text, "BS-") {
cited = true;
if !ctx.bs_ids.contains(&id) {
errors.push(format!("unknown bookspec id {id}"));
}
}
for id in ids_in(text, "WB-") {
cited = true;
if !ctx.wb_ids.contains(&id) {
errors.push(format!("unknown product rule {id}"));
}
}
let mut has_path = false;
let mut has_ref = false;
for raw in text.split_whitespace() {
let token = clean_token(raw);
if token.contains("://") {
continue;
}
if is_path_like(&token) {
let exists = if token.contains('/') {
ctx.root.join(&token).exists()
} else {
ctx.basenames.contains(&token)
};
if exists {
has_path = true;
} else {
errors.push(format!("path {token} does not exist"));
}
}
if token.contains("::") {
let first = token.split("::").next().unwrap_or("");
if first == "crate" || first.starts_with("ledger_") {
let last = token.rsplit("::").next().unwrap_or("");
let ident: String = last
.chars()
.take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
.collect();
if ctx.identifiers.contains(&ident) {
has_ref = true;
} else {
errors.push(format!("code reference {token} names nothing real"));
}
}
}
}
if !errors.is_empty() {
for e in errors {
bad.push(format!("{at}: {e}"));
}
return false;
}
if cited || has_path || has_ref {
return false;
}
if EXTERNAL.iter().any(|m| text.contains(m)) {
return true;
}
let declared = text.contains("DECISION")
|| text.contains("CONVENTION")
|| text.contains("CHOICE")
|| text.contains("::")
|| text.contains("plan ")
|| text.contains("plan/")
|| text.contains("server.rs")
|| text.contains("the module's rule")
|| text.contains("the books.")
|| text.contains("owner")
|| text
.split(|c: char| !c.is_ascii_alphanumeric() && c != '-')
.any(|t| {
(t.starts_with("R-")
|| t.starts_with("D-")
|| t.starts_with("I-")
|| t.starts_with("A-"))
&& t.len() > 2
&& t[2..].chars().next().is_some_and(|c| c.is_ascii_digit())
&& t[2..].chars().all(|c| c.is_ascii_alphanumeric())
});
if declared {
return true;
}
bad.push(format!(
"{at}: source clause cites no rule or source that exists"
));
false
}
#[test]
pub(crate) fn every_source_line_cites_an_existing_rule_or_source() {
let root = repo_root();
let bs_ids: BTreeSet<String> = bookspec_checker::rules()
.iter()
.map(|r| r.id.to_string())
.collect();
assert!(!bs_ids.is_empty(), "the pinned bookspec has rules");
let claims_dir = root.join("docs/spec/claims");
let mut wb_ids = BTreeSet::new();
for entry in std::fs::read_dir(&claims_dir).expect("docs/spec/claims") {
let path = entry.expect("a claims entry").path();
if path.extension().is_some_and(|e| e == "md") {
let text = std::fs::read_to_string(&path).expect("a claims file");
wb_ids.extend(ids_in(&text, "WB-"));
}
}
assert!(!wb_ids.is_empty(), "the claims files hold product rules");
// Identifiers declared anywhere under crates/, and file basenames
// anywhere in the repo, for the path and code-reference checks.
let mut crate_files = Vec::new();
collect_all(&root.join("crates"), &mut crate_files);
let mut identifiers = BTreeSet::new();
for path in crate_files
.iter()
.filter(|p| p.extension().is_some_and(|e| e == "rs"))
{
let Ok(text) = std::fs::read_to_string(path) else {
continue;
};
let words: Vec<&str> = text
.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.filter(|w| !w.is_empty())
.collect();
for pair in words.windows(2) {
if matches!(
pair[0],
"fn" | "struct" | "enum" | "mod" | "const" | "type" | "trait"
) {
identifiers.insert(pair[1].to_string());
}
}
}
let mut all_files = Vec::new();
collect_all(&root, &mut all_files);
let basenames: BTreeSet<String> = all_files
.iter()
.filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
.collect();
let mut files = Vec::new();
for crate_dir in std::fs::read_dir(root.join("crates")).expect("crates") {
let base = crate_dir.expect("a crate").path();
for sub in ["src", "tests"] {
let dir = base.join(sub);
if dir.is_dir() {
collect_rs(&dir, &mut files);
}
}
}
let fuzz_dir = root.join("fuzz");
if fuzz_dir.is_dir() {
collect_rs(&fuzz_dir, &mut files);
}
files.sort();
assert!(!files.is_empty(), "crates and fuzz hold sources");
let mut bad = Vec::new();
let mut checked = 0usize;
let mut free_text = 0usize;
for path in &files {
let text = std::fs::read_to_string(path).expect("a source file");
let lines: Vec<&str> = text.lines().collect();
let mut i = 0usize;
while i < lines.len() {
let Some(content) = strip_comment(lines[i]) else {
i += 1;
continue;
};
if !content.contains("SOURCE:") {
i += 1;
continue;
}
checked += 1;
let at = format!("{}:{}", path.display(), i + 1);
let mut clause = content.clone();
let mut j = i + 1;
while j < lines.len() {
let Some(next) = strip_comment(lines[j]) else {
break;
};
if next.is_empty() || is_heading(&next) || next.contains("SOURCE:") {
break;
}
clause.push(' ');
clause.push_str(&next);
j += 1;
}
let ctx = ClauseCtx {
root: &root,
bs_ids: &bs_ids,
wb_ids: &wb_ids,
identifiers: &identifiers,
basenames: &basenames,
};
if check_clause(&clause, &ctx, &mut bad, &at) {
free_text += 1;
}
i = j.max(i + 1);
}
}
println!("source clauses checked: {checked}");
println!("source clauses passing only through the free-text branch: {free_text}");
assert!(
checked >= CHECKED_FLOOR,
"source clauses were found ({checked})"
);
assert_eq!(
free_text, FREE_TEXT_COUNT,
"free-text source clauses changed on purpose only: edit FREE_TEXT_COUNT in the same commit"
);
assert!(
bad.is_empty(),
"every source clause cites a rule or source that exists:\n{}",
bad.join("\n")
);
}
fn retired_count(text: &str) -> usize {
let chars: Vec<char> = text.chars().collect();
let mut count = 0usize;
let mut i = 0usize;
while i < chars.len() {
if chars[i] == 'B'
&& (i == 0 || !(chars[i - 1].is_alphanumeric() || chars[i - 1] == '_'))
&& chars.get(i + 1) == Some(&'-')
{
let mut j = i + 2;
let mut value = 0usize;
let mut digits = 0usize;
while j < chars.len() && chars[j].is_ascii_digit() {
value = value * 10 + chars[j].to_digit(10).unwrap_or(0) as usize;
digits += 1;
j += 1;
}
if digits > 0 && (1..=45).contains(&value) {
count += 1;
}
i = j.max(i + 1);
} else {
i += 1;
}
}
count
}
#[test]
pub(crate) fn no_retired_rule_reference_remains() {
let root = repo_root();
// Deliberately kept occurrences: path and exact count. Any change
// in either direction edits this list, on purpose, in that commit.
let allow: &[(&str, usize)] = &[
// Kept: user-visible evidence-source strings in program output.
("crates/ledger-core/src/ledger.rs", 8),
// Kept: goldens are hand-verified expected output.
(
"crates/ledger-store/tests/golden/draft_states_and_evidence_gate.json",
2,
),
// Kept: user-visible CPA package text in program output.
("crates/ledger-package/src/package.rs", 1),
// Kept: user-visible tool description text in program output.
("crates/ledger-mcp/src/tools/settlements.rs", 1),
// Kept: user-visible on-screen contribution note.
("crates/ledger-gui/src/pages/projects.rs", 1),
];
let output = std::process::Command::new("git")
.args(["ls-files", "-z"])
.current_dir(&root)
.output()
.expect("git ls-files");
assert!(output.status.success(), "git ls-files succeeds");
let listing = String::from_utf8_lossy(&output.stdout);
let mut bad = Vec::new();
let mut seen_allow: BTreeSet<&str> = BTreeSet::new();
for rel in listing.split('\0').filter(|s| !s.is_empty()) {
if rel.starts_with("plan/")
|| rel == "docs/spec/rules.md"
|| rel.starts_with("docs/spec/claims/")
{
continue;
}
let path = root.join(rel);
let Ok(text) = std::fs::read_to_string(&path) else {
continue;
};
if text.contains('\0') {
continue;
}
// Whole-text scan: a reference wrapped across a newline and a
// comment prefix is still one occurrence of the number.
let count = retired_count(&text);
if count == 0 {
continue;
}
match allow.iter().find(|(p, _)| *p == rel) {
Some((_, expected)) => {
seen_allow.insert(rel);
if count != *expected {
bad.push(format!(
"{rel}: {count} kept occurrences, allow-list says {expected}"
));
}
}
None => bad.push(format!("{rel}: {count} retired rule reference(s)")),
}
}
for (p, _) in allow {
if !seen_allow.contains(p) {
bad.push(format!(
"{p}: allow-listed file has no kept occurrence left"
));
}
}
assert!(
bad.is_empty(),
"no retired rule reference remains outside the map files and the allow-list:\n{}",
bad.join("\n")
);
}