diff --git a/crates/ledger-core/tests/oracle_gnucash.rs b/crates/ledger-core/tests/oracle_gnucash.rs index 2b238a46..86294577 100644 --- a/crates/ledger-core/tests/oracle_gnucash.rs +++ b/crates/ledger-core/tests/oracle_gnucash.rs @@ -4,7 +4,8 @@ //! spec (docs/oracle.md, "Who is right"). This test builds random books //! through wanderbooks' engine (entries, foreign-currency lines, reversals, //! period closes), hands their posted entries to GnuCash 5.10 running in a -//! pinned container (oracle/gnucash/), and compares every account's +//! pinned container (bookspec's `gnucash/oracle/`, image +//! `bookspec-gnucash-oracle`), and compares every account's //! balance at month ends and random dates, exactly. A difference is a //! finding to adjudicate from principles (rules.md), never a reason to //! change wanderbooks to match. @@ -188,7 +189,7 @@ fn ours(l: &Ledger, as_of: NaiveDate) -> BTreeMap { fn gnucash(books: &[Value]) -> Vec { let mut child = Command::new("docker") - .args(["run", "--rm", "-i", "wanderbooks-gnucash-oracle"]) + .args(["run", "--rm", "-i", "bookspec-gnucash-oracle"]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) diff --git a/docs/architecture.md b/docs/architecture.md index 8d393f51..d81de4ff 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -46,7 +46,7 @@ the core depends on another crate of ours. UI concerns (formatting, locale, widgets) never leak into the core; the core speaks `Decimal` and ISO codes. A crate that was planned and never created is not missing: `ledger-export` became part of `ledger-import`; `ledger-oracle` is the -test `crates/ledger-core/tests/oracle_gnucash.rs` with `oracle/gnucash/` +test `crates/ledger-core/tests/oracle_gnucash.rs` with bookspec's `gnucash/oracle/` (`just oracle`); `ledger-view` and `ledger-extract` were closed by D-53 (`PackageBooks` is the shared view; extraction is in-process in `ledger-import`, P6). diff --git a/docs/oracle.md b/docs/oracle.md index 2ce82529..d43a88ef 100644 --- a/docs/oracle.md +++ b/docs/oracle.md @@ -89,7 +89,7 @@ engine bugs that Layers 1–4 had all missed. **Layer 5 — Cross-implementation diff harness (live: GnuCash).** `just oracle` builds GnuCash 5.10 in a pinned container -(`oracle/gnucash/`) and runs `tests/oracle_gnucash.rs`: 200 random books +(bookspec's `gnucash/oracle/`, built from the pinned rev) and runs `tests/oracle_gnucash.rs`: 200 random books built through wanderbooks (entries, foreign lines, reversals, closes), every account's balance at month ends and random dates, and each month's P&L, compared exactly (15,800 comparisons). Its first run found one real diff --git a/docs/testing.md b/docs/testing.md index bae3609c..2b8dea30 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -22,7 +22,7 @@ promise. | Concurrency (store) | a viewer reading while the books are written sees only whole books, never a false "differs" (2) | `ledger-store/tests/concurrency.rs` | `just check` | <1s | every change | | Model-based stateful | the Lean state machine, transcribed (3) | `tests/model.rs` | `just check` | ~3s | every change | | Differential: Decimal vs i64 | the kernel at two types (3) | `kernel_bridge/` | `just check` | <1s | every change | -| Differential: GnuCash | another implementation, adjudicated (3) | `tests/oracle_gnucash.rs`, `oracle/gnucash/` | `just oracle` | ~2 min | weekly | +| Differential: GnuCash | another implementation, adjudicated (3) | `tests/oracle_gnucash.rs`, bookspec `gnucash/oracle/` | `just oracle` | ~2 min | weekly | | Formal proofs | Kani (bounded) and Lean (unbounded) | `src/proofs/`, `verify/lean/` | `just prove` (51 harnesses and Lean), `just prove-slow` (the 9 in the justfile's `kani_slow`, each under its own 30-minute cap); 60 harnesses in all with the checker canary (2026-10-28) | prove ~5 min and prove-slow ~8 min at four jobs on a quiet laptop (each ~25 min under heavy load; docs/verification.md, "Running it") | weekly (CI runs both) | | Fuzzing | crash, hang, broken books (4); for `fuzz_store_replay` (validly chained journals with fuzz-chosen events): books that open must verify, and the viewer must accept exactly what the writer does | `fuzz/fuzz_targets/`, corpora in `fuzz/corpus/` | `just fuzz ` | 10 min each | weekly | | Miri | undefined behavior in our use of dependencies (4) | the core's unit, bridge, invariant tests (the exhaustive calendar walk aside) | `just miri` | ~4 min | on demand | diff --git a/justfile b/justfile index a4866367..65758273 100644 --- a/justfile +++ b/justfile @@ -179,8 +179,25 @@ _kani set: echo "Kani ({{set}}): $(wc -l <<<"$names") harnesses verified" # Random books through wanderbooks and GnuCash 5.10, compared exactly (Docker) +# The oracle's Dockerfile and runner live in bookspec (`gnucash/oracle`, +# image `bookspec-gnucash-oracle`): one source of truth. This builds that +# image from the pinned bookspec checkout (the rev in ledger-core's +# Cargo.toml), so no copy is kept here. oracle: - docker build -q -t wanderbooks-gnucash-oracle oracle/gnucash + #!/usr/bin/env bash + set -euo pipefail + # The oracle's container lives in bookspec (one source of truth): find + # the pinned checkout through cargo itself, whatever its directory name. + cargo fetch --locked + manifest=$(cargo metadata --format-version 1 --locked \ + | jq -r '.packages[] | select(.name == "bookspec-checker") | .manifest_path' | head -1) + [ -n "$manifest" ] || { echo "oracle: bookspec-checker is not a dependency of this workspace" >&2; exit 1; } + root=${manifest%/checker/Cargo.toml} + [ -f "$root/gnucash/oracle/Dockerfile" ] || { + echo "oracle: the pinned bookspec ($root) has no gnucash/oracle; move the pin to a bookspec rev that includes it (P14 unit S-K1)" >&2 + exit 1 + } + docker build -q -t bookspec-gnucash-oracle "$root/gnucash/oracle" cargo test -p ledger-core --test oracle_gnucash -- --ignored --nocapture # Fuzz one target for some seconds (nightly; corpus in fuzz/corpus/) diff --git a/mise.toml b/mise.toml index dda8c262..55038094 100644 --- a/mise.toml +++ b/mise.toml @@ -9,6 +9,8 @@ rust = [ { version = "nightly-2026-10-01", components = "miri,rust-src" }, ] just = "1" +# `just oracle` reads the pinned bookspec's path from `cargo metadata`. +jq = "1.7.1" # `just deploy` runs the Netlify CLI through npx. node = "26" # `just prove`: Kani model-checks the accounting kernel. Kani fetches its diff --git a/oracle/gnucash/Dockerfile b/oracle/gnucash/Dockerfile deleted file mode 100644 index 85d37a25..00000000 --- a/oracle/gnucash/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# GnuCash as a cross-check oracle (docs/oracle.md, "Who is right"): one -# independent implementation of double entry we diff against. Its answers -# are evidence, never the spec. Pinned so every run uses the same GnuCash. -FROM debian@sha256:a29215f6a35e51e22adffa17f89e9d2ef06214e64a2bad10d765c46aea49f11f -RUN apt-get update \ - && apt-get install -y --no-install-recommends \ - gnucash=1:5.10-0.1+b1 python3-gnucash=1:5.10-0.1+b1 \ - && rm -rf /var/lib/apt/lists/* -COPY books.py /oracle/books.py -ENTRYPOINT ["python3", "/oracle/books.py"] diff --git a/oracle/gnucash/books.py b/oracle/gnucash/books.py deleted file mode 100644 index 75cff3df..00000000 --- a/oracle/gnucash/books.py +++ /dev/null @@ -1,96 +0,0 @@ -"""Build books in GnuCash and report its balances. - -stdin: a JSON list of books: - {"accounts": [{"id": 1, "code": "1000", "name": "Cash", "type": "Asset"}], - "entries": [{"date": "2026-01-15", "description": "...", - "lines": [{"account": 1, "side": "debit", "amount": "12.34"}]}], - "as_of": ["2026-01-31", ...]} -stdout: a JSON list, one per book: - {"balances": {"2026-01-31": {"1": "12.34", ...}, ...}} -Balances are GnuCash's own: the sum of split values on the account through -the end of that day, debits positive, as an exact decimal string. -""" -import datetime, json, os, sys, tempfile -from decimal import Decimal -import gnucash -from gnucash import Account, GncNumeric, Session, SessionOpenMode, Split, Transaction - -TYPES = { - "Asset": gnucash.ACCT_TYPE_ASSET, - "Liability": gnucash.ACCT_TYPE_LIABILITY, - "Equity": gnucash.ACCT_TYPE_EQUITY, - "Revenue": gnucash.ACCT_TYPE_INCOME, - "Expense": gnucash.ACCT_TYPE_EXPENSE, -} - - -def numeric(amount: str) -> GncNumeric: - d = Decimal(amount) - sign, digits, exp = d.as_tuple() - denom = 10 ** max(0, -exp) - num = int(d * denom) - return GncNumeric(num, denom) - - -def decimal_of(n) -> str: - return str(Decimal(n.num()) / Decimal(n.denom())) - - -def end_of_day(date: str) -> int: - d = datetime.date.fromisoformat(date) - return int(datetime.datetime(d.year, d.month, d.day, 23, 59, 59, - tzinfo=datetime.timezone.utc).timestamp()) - - -def run(book_spec, path): - s = Session(f"xml://{path}", SessionOpenMode.SESSION_NEW_STORE) - try: - book = s.book - root = book.get_root_account() - usd = book.get_table().lookup("CURRENCY", "USD") - accounts = {} - for a in book_spec["accounts"]: - acct = Account(book) - acct.SetName(f'{a["code"]} {a["name"]}') - acct.SetCode(a["code"]) - acct.SetType(TYPES[a["type"]]) - acct.SetCommodity(usd) - root.append_child(acct) - accounts[a["id"]] = acct - for e in book_spec["entries"]: - d = datetime.date.fromisoformat(e["date"]) - tx = Transaction(book) - tx.BeginEdit() - tx.SetCurrency(usd) - tx.SetDate(d.day, d.month, d.year) - tx.SetDescription(e.get("description", "")) - for line in e["lines"]: - value = numeric(line["amount"]) - if line["side"] == "credit": - value = value.neg() - sp = Split(book) - sp.SetParent(tx) - sp.SetAccount(accounts[line["account"]]) - sp.SetValue(value) - sp.SetAmount(value) - tx.CommitEdit() - out = {} - for date in book_spec["as_of"]: - t = end_of_day(date) - out[date] = {str(i): decimal_of(a.GetBalanceAsOfDate(t)) for i, a in accounts.items()} - return {"balances": out} - finally: - s.end() - - -def main(): - books = json.load(sys.stdin) - results = [] - with tempfile.TemporaryDirectory() as tmp: - for i, b in enumerate(books): - results.append(run(b, os.path.join(tmp, f"book{i}.gnucash"))) - json.dump(results, sys.stdout) - - -if __name__ == "__main__": - main()