Files
wanderbooks/crates/ledger-core/tests/bookspec/pin.rs
T
swaitsandDeepSeek V4.1 Flash e431791bbc build(lean): verify/lean requires bookspec's Lean; the laws live once
D-69 puts the accounting laws in one place: bookspec's Lean, namespace
`Bookspec`. L-F1 landed there every file of `verify/lean/Wanderbooks/`.
Before deleting the copies, each was checked: comments stripped, namespace
normalised, blank lines dropped, and diffed against its bookspec peer --
all ten are identical (Basic, Entry, Statements, Closing, Ledger,
Apportion, Parties, Prorate, Deductible, Documents). There is nothing
wanderbooks-only left to keep.

So `verify/lean/lakefile.toml` now `[[require]]`s bookspec as a git
dependency at 77e046334eea29e082b53bbe907017d9ffef5a0a -- the same rev
`crates/ledger-core/Cargo.toml` pins the Rust checker to -- with
`subDir = "lean"`. The generated `lake-manifest.json` is committed, because
Lake builds from it and it must not float.

`Wanderbooks.lean` is now a thin local target that imports `Bookspec`:
`lake build` builds and checks the whole law set, and the file says where
a law is edited and how wanderbooks picks it up (move the pin). The
duplicated modules and the `Wanderbooks/` directory are gone.

`Audit.lean` imports `Bookspec` and audits all 151 theorems (the 118
headline laws plus the 33 named laws in `Laws.lean` that cite them) --
the whole set bookspec itself audits. `just prove` still checks standard
axioms only; its summary now says "theorems", since the set is more than
headline ones.

The new `tests/bookspec/pin.rs` reads the rev out of the lakefile, the
manifest, and both crates' Cargo.toml, and fails unless all four name the
same repository at the same commit, and bookspec's `lean/` package. Without
it the Lean laws and the Rust checker could silently be different spec
versions.

Verified: `lake build` from a clean `.lake` (fetches and builds in 7 s);
`just prove` 871 s (Kani 53 harnesses, then 151 theorems, standard axioms
only); `cargo test -p ledger-core --test bookspec pin`; `cargo fmt --check`;
`cargo clippy -p ledger-core --tests -D warnings`.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
2026-11-08 11:35:07 -07:00

151 lines
5.4 KiB
Rust

use crate::*;
/// The Lean laws and the Rust checker must come from one bookspec rev
/// (D-69): `verify/lean` requires bookspec's Lean at a git rev, and the
/// crates depend on bookspec's checker and runner at a git rev. If the two
/// drift, `just prove` audits laws the tests do not run against — so this
/// test fails unless every pin names the same repository at the same commit.
///
/// The revs live in four files; the Lean one is in two of them (the
/// lakefile's `[[require]]` and the generated `lake-manifest.json`, which
/// Lake builds from, so both must agree).
fn repo_root() -> std::path::PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}
/// A pin as one of the files states it: where it was found, the URL and
/// the commit.
struct Pin {
source: String,
url: String,
rev: String,
}
fn lean_pins(root: &Path) -> Vec<Pin> {
let mut pins = Vec::new();
let lakefile = std::fs::read_to_string(root.join("verify/lean/lakefile.toml"))
.expect("verify/lean/lakefile.toml");
let doc: toml::Value = toml::from_str(&lakefile).expect("lakefile.toml parses");
let require = doc
.get("require")
.and_then(toml::Value::as_array)
.expect("lakefile.toml has [[require]]");
let bookspec = require
.iter()
.find(|r| r.get("name").and_then(toml::Value::as_str) == Some("bookspec"))
.expect("lakefile.toml requires bookspec");
assert_eq!(
bookspec.get("subDir").and_then(toml::Value::as_str),
Some("lean"),
"verify/lean/lakefile.toml requires bookspec from its lean/ package"
);
pins.push(Pin {
source: "verify/lean/lakefile.toml".into(),
url: bookspec
.get("git")
.and_then(toml::Value::as_str)
.expect("the require names a git url")
.into(),
rev: bookspec
.get("rev")
.and_then(toml::Value::as_str)
.expect("the require names a rev")
.into(),
});
let manifest = std::fs::read_to_string(root.join("verify/lean/lake-manifest.json"))
.expect("verify/lean/lake-manifest.json (commit it: Lake builds from it)");
let doc: serde_json::Value = serde_json::from_str(&manifest).expect("manifest parses");
let bookspec = doc
.get("packages")
.and_then(serde_json::Value::as_array)
.expect("manifest has packages")
.iter()
.find(|p| p.get("name").and_then(serde_json::Value::as_str) == Some("bookspec"))
.expect("the manifest records bookspec");
assert_eq!(
bookspec.get("subDir").and_then(serde_json::Value::as_str),
Some("lean"),
"verify/lean/lake-manifest.json records bookspec's lean/ package"
);
pins.push(Pin {
source: "verify/lean/lake-manifest.json".into(),
url: bookspec
.get("url")
.and_then(serde_json::Value::as_str)
.expect("the manifest records a url")
.into(),
rev: bookspec
.get("rev")
.and_then(serde_json::Value::as_str)
.expect("the manifest records a rev")
.into(),
});
pins
}
/// Every `bookspec-*` dependency in a crate's manifest, from whichever
/// table it is declared in.
fn cargo_pins(root: &Path, manifest: &str) -> Vec<Pin> {
let path = root.join(manifest);
let text = std::fs::read_to_string(&path).unwrap_or_else(|_| panic!("{manifest}"));
let doc: toml::Value = toml::from_str(&text).unwrap_or_else(|_| panic!("{manifest} parses"));
let mut pins = Vec::new();
for table in ["dependencies", "dev-dependencies", "build-dependencies"] {
let Some(deps) = doc.get(table).and_then(toml::Value::as_table) else {
continue;
};
for (name, dep) in deps {
if !name.starts_with("bookspec-") {
continue;
}
let Some(dep) = dep.as_table() else { continue };
let url = dep
.get("git")
.and_then(toml::Value::as_str)
.unwrap_or_else(|| panic!("{manifest}: {name} is not a git dependency"));
let rev = dep
.get("rev")
.and_then(toml::Value::as_str)
.unwrap_or_else(|| panic!("{manifest}: {name} is pinned to a rev, not a branch"));
pins.push(Pin {
source: format!("{manifest}: {name}"),
url: url.into(),
rev: rev.into(),
});
}
}
assert!(!pins.is_empty(), "{manifest} pins no bookspec crate");
pins
}
#[test]
pub(crate) fn bookspec_rev_is_pinned_once() {
let root = repo_root();
let mut pins = lean_pins(&root);
pins.extend(cargo_pins(&root, "crates/ledger-core/Cargo.toml"));
pins.extend(cargo_pins(&root, "crates/ledger-package/Cargo.toml"));
let first = &pins[0];
let differing: Vec<String> = pins
.iter()
.filter(|p| p.rev != first.rev || p.url != first.url)
.map(|p| format!(" {}: {} @ {}", p.source, p.url, p.rev))
.collect();
assert!(
differing.is_empty(),
"the Lean laws and the Rust checker name different bookspec pins \
(all four must be {} @ {}):\n{}",
first.url,
first.rev,
differing.join("\n")
);
assert_eq!(first.url, "https://git.swaits.com/swaits/bookspec");
assert_eq!(
first.rev.len(),
40,
"a bookspec pin is a full commit: {}",
first.rev
);
}