From 9c299eb7680728a6e9a69a627bf0df457d94e23f Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 07:40:21 -0700 Subject: [PATCH 1/9] fix(package): the IIF export keeps its typed refusal `export_iif` turned `journal_to_iif`'s refusal into a string, so a caller could not tell why the file was refused without reading the message. An entry on more than one receivable or payable account is BS-format-10's own refusal (the receiving program will not take the transaction), while every other IIF refusal is BS-format-2's; the bookspec adapter must name the one rule an example expects. Returning `IifError` keeps the two apart by type. The package and the MCP tool still hand the human a string. Co-Authored-By: DeepSeek V4.1 Flash --- crates/ledger-mcp/src/tools/reports.rs | 2 +- crates/ledger-package/src/iif.rs | 4 ++-- crates/ledger-package/src/package.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ledger-mcp/src/tools/reports.rs b/crates/ledger-mcp/src/tools/reports.rs index 30ccf3f4..59d0569d 100644 --- a/crates/ledger-mcp/src/tools/reports.rs +++ b/crates/ledger-mcp/src/tools/reports.rs @@ -1513,7 +1513,7 @@ impl BooksServer { } else { cash }; - ledger_package::export_iif(store.ledger(), cash, as_of) + ledger_package::export_iif(store.ledger(), cash, as_of).map_err(|e| e.to_string()) } /// The general journal as JSON or CSV: each entry posted in the /// period (BS-period-3, as the XLSX journal), in posting order. diff --git a/crates/ledger-package/src/iif.rs b/crates/ledger-package/src/iif.rs index 82797604..fdfe2f24 100644 --- a/crates/ledger-package/src/iif.rs +++ b/crates/ledger-package/src/iif.rs @@ -38,7 +38,7 @@ pub fn export_iif( ledger: &Ledger, cash_codes: &[String], as_of: NaiveDate, -) -> Result { +) -> Result { use ledger_import::{QbAccountType as Qb, iif_accounts, iif_classes, journal_to_iif}; let fixed: std::collections::HashSet = ledger .registered_assets() @@ -71,5 +71,5 @@ pub fn export_iif( .posted_entries() .filter(|e| e.date() <= as_of && !closes.contains(&e.id())) .collect(); - journal_to_iif(&accounts, &iif_classes(ledger), &entries).map_err(|e| e.to_string()) + journal_to_iif(&accounts, &iif_classes(ledger), &entries) } diff --git a/crates/ledger-package/src/package.rs b/crates/ledger-package/src/package.rs index e7ce1aab..00307d67 100644 --- a/crates/ledger-package/src/package.rs +++ b/crates/ledger-package/src/package.rs @@ -564,7 +564,7 @@ pub fn build_package( let seq = books.journal_seq(); // The journal through as_of for QuickBooks, closes left out, and what // it writes differently (INVARIANT 8). - let iif = export_iif(ledger, cash_codes, as_of)?; + let iif = export_iif(ledger, cash_codes, as_of).map_err(|e| e.to_string())?; let (warnings, iif_changes) = (iif.warnings(), iif.changes.len()); let receivable_payable: Vec = iif .receivable_payable From 7ac1e644b2236c985c3c2b5d352981df251d072c Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 07:40:26 -0700 Subject: [PATCH 2/9] test(core): the adapter runs refused dates, amounts, periods, roles, exports, 1099 years and returns The bookspec examples now spell the input an engine has to refuse, and the adapter takes each through the real books: - a date past 9999 is refused by `Store::create_entry`'s own `try_create_entry` (DateOutOfRange), and a line past one quadrillion by `add_line` (AmountOutOfRange), each on a rehearsal so a refused entry leaves no draft; - a close whose period partly overlaps another creates it through `Store::create_period` and reads PeriodsOverlap; - `assign_role` is `Store::assign_role`, the journaled command, so a role on a header is refused by `check_assign_role` (ControlAccountMismatch) and the chart stays as it was; - an IIF export with an entry on a receivable and a payable account is refused with BS-format-10's word, which is why `export_iif` keeps its typed error; - a 2027 worksheet is refused through `RuleNotKnown` as `unknown_1099_year`, never guessed; - `return_tin` reads the full number from the real `ledger_vault::Vault` the example's `setup.vault_tins` fills, and the books' own record keeps only the last four. The adapter skips sixteen examples now (three more, each for an option wanderbooks did not pick): allow-partial-overlap, carry-the-last-amount and nowhere. Every one of the 95 it runs passes. Co-Authored-By: DeepSeek V4.1 Flash --- Cargo.lock | 1 + crates/ledger-core/Cargo.toml | 1 + crates/ledger-core/tests/bookspec/books.rs | 34 ++++++++++ crates/ledger-core/tests/bookspec/check.rs | 28 ++++++++- crates/ledger-core/tests/bookspec/common.rs | 13 ++++ crates/ledger-core/tests/bookspec/events.rs | 12 +++- crates/ledger-core/tests/bookspec/examples.rs | 10 ++- crates/ledger-core/tests/bookspec/main.rs | 1 + .../tests/bookspec/queries/formats.rs | 28 +++++++-- .../ledger-core/tests/bookspec/queries/mod.rs | 1 + .../tests/bookspec/queries/parties.rs | 63 ++++++++++++++++++- 11 files changed, 178 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25d3aad0..50e5316f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3977,6 +3977,7 @@ dependencies = [ "ledger-import", "ledger-package", "ledger-store", + "ledger-vault", "num-bigint", "num-rational", "proptest", diff --git a/crates/ledger-core/Cargo.toml b/crates/ledger-core/Cargo.toml index 0ae1d012..64607e61 100644 --- a/crates/ledger-core/Cargo.toml +++ b/crates/ledger-core/Cargo.toml @@ -38,6 +38,7 @@ bookspec-runner = { git = "https://git.swaits.com/swaits/bookspec", rev = "2b3b6 ledger-package = { path = "../ledger-package" } ledger-import = { path = "../ledger-import" } ledger-store = { path = "../ledger-store" } +ledger-vault = { path = "../ledger-vault" } tempfile = "3" [[bench]] diff --git a/crates/ledger-core/tests/bookspec/books.rs b/crates/ledger-core/tests/bookspec/books.rs index 29c794a5..8d0ae520 100644 --- a/crates/ledger-core/tests/bookspec/books.rs +++ b/crates/ledger-core/tests/bookspec/books.rs @@ -39,6 +39,11 @@ pub(crate) struct Books<'a> { /// What each disposal took, by asset id, as the engine computed it /// just before posting it. pub(crate) disposals: BTreeMap, + /// The TIN vault outside the books, when the setup puts a full TIN + /// there (BS-partner-8's outside-the-books option): the real + /// `ledger_vault::Vault`, in the books' own directory, holding only + /// what the example spells. + pub(crate) vault: Option, } /// A store command's result, unwrapped: the example's books take it. @@ -289,6 +294,7 @@ impl<'a> Books<'a> { posted: Vec::new(), runs: BTreeMap::new(), disposals: BTreeMap::new(), + vault: None, } } @@ -443,6 +449,34 @@ impl<'a> Books<'a> { assert_eq!(total, Decimal::ONE, "{}: the shares sum to 1", ex.id); } let parties = add_parties(ex, &mut self.store); + // The vault outside the books (BS-partner-8's outside-the-books + // option): the real encrypted store, in the books' own directory, + // holding the full TINs the example puts there and nothing else. + if !s.vault_tins.is_empty() { + let vault = Vault::open( + &self._dir.path().join("vault"), + &self._dir.path().join("vault.key"), + true, + ) + .unwrap_or_else(|e| panic!("{}: the TIN vault: {e}", ex.id)); + for v in &s.vault_tins { + let id = *parties.get(&v.party).unwrap_or_else(|| { + panic!("{}: vault TIN: party {:?} not in setup", ex.id, v.party) + }); + let digits: String = v.tin.chars().filter(|c| *c != '-').collect(); + let kind = if digits.starts_with('9') { + ledger_vault::TinType::Itin + } else { + ledger_vault::TinType::Ssn + }; + let tin = Tin::new(&v.tin, kind) + .unwrap_or_else(|e| panic!("{}: vault TIN {:?}: {e}", ex.id, v.tin)); + vault + .put_tin(id, &tin) + .unwrap_or_else(|e| panic!("{}: vault TIN: {e}", ex.id)); + } + self.vault = Some(vault); + } // A member who is a party (by name, with the member role), as the // settings' members are (settings::Company::party_members). let party_members: Vec = members diff --git a/crates/ledger-core/tests/bookspec/check.rs b/crates/ledger-core/tests/bookspec/check.rs index b3411743..f82063a9 100644 --- a/crates/ledger-core/tests/bookspec/check.rs +++ b/crates/ledger-core/tests/bookspec/check.rs @@ -38,7 +38,12 @@ pub(crate) fn check(b: &Books<'_>, x: &Expect) -> Option<&'static str> { only( x.party.is_some(), "party", - &["party_part", "party_totals", "party_tin_last4"], + &[ + "party_part", + "party_totals", + "party_tin_last4", + "return_tin", + ], ); only(x.basis.is_some(), "basis", &["party_totals"]); only(x.payee.is_some(), "payee", &["vendor_1099"]); @@ -65,6 +70,10 @@ pub(crate) fn check(b: &Books<'_>, x: &Expect) -> Option<&'static str> { "add_account", "set_tax_category", "period", + "vendor_1099", + "iif_lines", + "iif_changes", + "return_tin", ], ); if COMMANDS.contains(&q) { @@ -77,12 +86,26 @@ pub(crate) fn check(b: &Books<'_>, x: &Expect) -> Option<&'static str> { check_command(ex, l, x); return None; } - // Anything else shows its values; a period may be refused instead. + // Anything else shows its values; a period may be refused instead, and + // so may a 1099 worksheet, an export and a return (BS-1099-2, + // BS-format-10, BS-partner-8). The refusal is the engine's own. assert!( x.refused.is_some() != x.values.as_ref().is_some_and(|v| !v.is_empty()), "{}: {q} expects values (a period: values or refused)", ex.id ); + if let Some(want) = &x.refused + && matches!(q, "vendor_1099" | "iif_lines" | "iif_changes" | "return_tin") + { + match b.answer(&x.query()) { + Err(r) => assert_eq!(&r.reason, want, "{}: {q} refused ({})", ex.id, r.reason), + Ok(v) => panic!("{}: {q} answered {v}, expected refused: {want}", ex.id), + } + if matches!(q, "iif_lines" | "iif_changes") { + check_format_refused(ex, x); + } + return None; + } match q { "period" => check_period(ex, x), "day" => check_day(ex, x), @@ -110,6 +133,7 @@ pub(crate) fn check(b: &Books<'_>, x: &Expect) -> Option<&'static str> { "party_part" => check_party_part(b, x), "party_totals" => check_party_totals(b, x), "party_tin_last4" => check_party_tin_last4(b, x), + "return_tin" => check_return_tin(b, x), "project" => check_project(b, x), "aging" => check_aging(b, x), "document_dates" => check_document_dates(b, x), diff --git a/crates/ledger-core/tests/bookspec/common.rs b/crates/ledger-core/tests/bookspec/common.rs index 780fa2d3..a5a7358f 100644 --- a/crates/ledger-core/tests/bookspec/common.rs +++ b/crates/ledger-core/tests/bookspec/common.rs @@ -163,6 +163,9 @@ pub(crate) fn refusal(e: &LedgerError) -> String { LedgerError::ParentTypeMismatch { .. } => "parent_type_mismatch", LedgerError::EntryAlreadyReversed(..) => "already_reversed", LedgerError::ClosedPeriod(..) => "period_closed", + // A period that shares days with another while neither contains the + // other (BS-period-4, and BS-period-2 for an engine that nests). + LedgerError::PeriodsOverlap { .. } => "period_overlaps", // A line on a header account (a placeholder takes no postings), // BS-entry-1 and BS-entry-6. LedgerError::PlaceholderAccount(_) => "header_account", @@ -174,6 +177,16 @@ pub(crate) fn refusal(e: &LedgerError) -> String { LedgerError::EntryAlreadyPosted(_) | LedgerError::PostedEntryImmutable | LedgerError::UnknownEntry(_) => "not_drafted", + // A date outside 0001-01-01 through 9999-12-31, or an amount past + // a line's cap: refused rather than wrapped (BS-entry-4, + // BS-entry-7). + LedgerError::DateOutOfRange(_) => "date_out_of_range", + LedgerError::AmountOutOfRange => "amount_out_of_range", + // A role assigned to an account that is not postable or not of the + // role's type (BS-ledger-19). + LedgerError::ControlAccountMismatch { .. } => "role_account_mismatch", + // A 1099 threshold for a year the books do not know (BS-1099-2). + LedgerError::RuleNotKnown { .. } => "unknown_1099_year", other => return format!("another refusal: {other}"), } .to_string() diff --git a/crates/ledger-core/tests/bookspec/events.rs b/crates/ledger-core/tests/bookspec/events.rs index 6d8fdf08..8b82d9f9 100644 --- a/crates/ledger-core/tests/bookspec/events.rs +++ b/crates/ledger-core/tests/bookspec/events.rs @@ -24,11 +24,12 @@ impl Books<'_> { ("set_draft_state", e.set_draft_state.is_some()), ("attach_receipt", e.attach_receipt.is_some()), ("discard_draft", e.discard_draft.is_some()), + ("assign_role", e.assign_role.is_some()), ]; match kinds.iter().filter(|(_, given)| *given).collect::>()[..] { [(kind, _)] => kind, _ => panic!( - "{} event {n}: an event is one of lines, reverses, close, bill, invoice, write_off, payment, settlement, recovery, run_depreciation, dispose_asset, run_schedules, register_schedule, post_draft, set_draft_state, attach_receipt or discard_draft", + "{} event {n}: an event is one of lines, reverses, close, bill, invoice, write_off, payment, settlement, recovery, run_depreciation, dispose_asset, run_schedules, register_schedule, post_draft, set_draft_state, attach_receipt, discard_draft or assign_role", self.ex.id ), } @@ -467,6 +468,8 @@ impl Books<'_> { // then thrown away. The refusal is the engine's, never // rebuilt here. let mut rehearsal = self.rehearsal(what); + // The books' own new-work entry point, so a date past the + // calendar (BS-entry-7) is refused there, not rebuilt here. let entry = rehearsal.create_entry(day(), description)?; for (i, line) in e.lines.iter().enumerate() { let side = match line.side.as_str() { @@ -817,6 +820,13 @@ impl Books<'_> { ); Ok(()) } + "assign_role" => { + let a = e.assign_role.as_ref().expect("checked: a role assignment"); + let account = self.id(&a.account); + // The books' own journaled command: it checks the role's + // account before it writes anything (BS-ledger-19). + self.store.assign_role(&a.role, Some(account)) + } other => unreachable!("{other} is no event"), } } diff --git a/crates/ledger-core/tests/bookspec/examples.rs b/crates/ledger-core/tests/bookspec/examples.rs index dee2e120..f741ecb3 100644 --- a/crates/ledger-core/tests/bookspec/examples.rs +++ b/crates/ledger-core/tests/bookspec/examples.rs @@ -165,10 +165,14 @@ pub(crate) fn every_bookspec_example_holds() { // BS-prepaid-2's equal-months and BS-ledger-20's refuse (each option's // example, so no allow-list grows), BS-entry-6's check-only-when-posting // and BS-ledger-17's reversal-needs-its-own (wanderbooks refuses a line on - // a header as it is added, and a reversal needs no evidence of its own). + // a header as it is added, and a reversal needs no evidence of its own), + // and BS-period-4's allow-partial-overlap, BS-1099-2's + // carry-the-last-amount and BS-partner-8's nowhere (wanderbooks nests + // periods, refuses a year whose threshold it does not know, and keeps + // the full TIN outside the books). assert_eq!( skipped.len(), - 13, - "the thirteen unpicked-option examples: {skipped:?}" + 16, + "the sixteen unpicked-option examples: {skipped:?}" ); } diff --git a/crates/ledger-core/tests/bookspec/main.rs b/crates/ledger-core/tests/bookspec/main.rs index 8e24dd45..be75bacf 100644 --- a/crates/ledger-core/tests/bookspec/main.rs +++ b/crates/ledger-core/tests/bookspec/main.rs @@ -59,6 +59,7 @@ pub(crate) use ledger_core::{ trial_balance_as_of, vendor_1099_report, }; pub(crate) use ledger_store::{DepreciationRun, DisposalPreview, Store, StoreError}; +pub(crate) use ledger_vault::{Tin, Vault}; pub(crate) use rust_decimal::Decimal; pub(crate) use bookspec_checker::{ diff --git a/crates/ledger-core/tests/bookspec/queries/formats.rs b/crates/ledger-core/tests/bookspec/queries/formats.rs index 2c8f46d9..37f1216c 100644 --- a/crates/ledger-core/tests/bookspec/queries/formats.rs +++ b/crates/ledger-core/tests/bookspec/queries/formats.rs @@ -3,8 +3,16 @@ use crate::*; impl Books<'_> { pub(crate) fn answer_iif(&self, q: &Query) -> Result { let as_of = self.q_day(q)?; - let iif = export_iif(self.l(), &self.ex.setup.cash_accounts, as_of) - .map_err(|e| engine_refusal(RuleId::new(Area::Format, 2), e))?; + let iif = export_iif(self.l(), &self.ex.setup.cash_accounts, as_of).map_err(|e| { + // An entry on more than one A/R or A/P account is BS-format-10's + // refusal; every other IIF refusal stays BS-format-2's. + match e { + ledger_import::IifError::SeveralReceivablePayableAccounts { .. } => { + Refusal::of_reason("several_receivable_payable_accounts") + } + other => engine_refusal(RuleId::new(Area::Format, 2), other), + } + })?; match q.name.as_str() { "iif_lines" => { let mut lines: Vec<&str> = iif.text.split("\r\n").collect(); @@ -56,9 +64,9 @@ pub(crate) const FORMAT_QUERIES: [(&str, &str); 2] = [ ), ]; -/// A format query's shape, checked here; its values are checked by -/// [`FORMAT_QUERIES`]' test. -pub(crate) fn check_format_shape(ex: &Example, x: &Expect) { +/// A refused format query (BS-format-10): the appendix and the day, no +/// values. +pub(crate) fn check_format_refused(ex: &Example, x: &Expect) { assert_eq!( ex.appendix.as_deref(), Some("formats"), @@ -66,7 +74,6 @@ pub(crate) fn check_format_shape(ex: &Example, x: &Expect) { ex.id, x.query ); - // ledger-package reads the as_of itself: given, never defaulted. assert!(x.as_of.is_some(), "{}: {} gives as_of", ex.id, x.query); day(ex, x); assert!( @@ -75,6 +82,15 @@ pub(crate) fn check_format_shape(ex: &Example, x: &Expect) { ex.id, x.query ); +} + +/// A format query's shape, checked here; its values are checked by +/// [`FORMAT_QUERIES`]' test. +pub(crate) fn check_format_shape(ex: &Example, x: &Expect) { + check_format_refused(ex, x); + if x.refused.is_some() { + return; + } match (x.query.as_str(), x.values(&ex.id)) { ("iif_lines", Values::Lines(_)) => {} ("iif_changes", Values::Rows(rows)) => { diff --git a/crates/ledger-core/tests/bookspec/queries/mod.rs b/crates/ledger-core/tests/bookspec/queries/mod.rs index 87abcf98..5b923b5b 100644 --- a/crates/ledger-core/tests/bookspec/queries/mod.rs +++ b/crates/ledger-core/tests/bookspec/queries/mod.rs @@ -85,6 +85,7 @@ impl Books<'_> { "party_part" => self.answer_party_part(q), "party_totals" => self.answer_party_totals(q), "party_tin_last4" => self.answer_party_tin_last4(q), + "return_tin" => self.answer_return_tin(q), "project" => self.answer_project(q), "aging" => self.answer_aging(q), "document_dates" => self.answer_document_dates(q), diff --git a/crates/ledger-core/tests/bookspec/queries/parties.rs b/crates/ledger-core/tests/bookspec/queries/parties.rs index fb33da31..fb0443c0 100644 --- a/crates/ledger-core/tests/bookspec/queries/parties.rs +++ b/crates/ledger-core/tests/bookspec/queries/parties.rs @@ -7,7 +7,13 @@ impl Books<'_> { let l = self.l(); let sources = self.payment_accounts()?; let r = vendor_1099_report(l, year, None, &sources, &self.party_members, Some(to)) - .map_err(|e| engine_refusal(RuleId::new(Area::Tax1099, 1), e))?; + .map_err(|e| match e { + // A year whose threshold is not published is BS-1099-2's + // refusal, never a guess (the engine picks + // refuse-an-unknown-year). + LedgerError::RuleNotKnown { .. } => Refusal::of_reason("unknown_1099_year"), + other => engine_refusal(RuleId::new(Area::Tax1099, 1), other), + })?; let _ = from; let mut m = JsonMap::new(); for (name, vendor) in &self.payees { @@ -41,7 +47,10 @@ impl Books<'_> { &self.party_members, Some(to), ) - .map_err(|e| engine_refusal(RuleId::new(Area::Tax1099, 1), e))?; + .map_err(|e| match e { + LedgerError::RuleNotKnown { .. } => Refusal::of_reason("unknown_1099_year"), + other => engine_refusal(RuleId::new(Area::Tax1099, 1), other), + })?; let line = r.vendors.iter().find(|l| l.vendor_id == v).ok_or_else(|| { Refusal::new( RuleId::new(Area::Tax1099, 1), @@ -159,6 +168,34 @@ impl Books<'_> { Ok(json_figures([("tin_last4".to_string(), got.to_string())])) } + /// The full TIN a return produced for `party` carries (BS-partner-8, + /// outside-the-books): read from the vault outside the books, never + /// from the books' records, which hold only the last four. A party + /// whose number the vault does not hold is refused `tin_not_held`. + pub(crate) fn return_tin(&self, party: PartyId) -> Result { + let vault = self + .vault + .as_ref() + .ok_or_else(|| Refusal::of_reason("tin_not_held"))?; + let tin = vault + .tin(party) + .map_err(|_| Refusal::of_reason("tin_not_held"))? + .ok_or_else(|| Refusal::of_reason("tin_not_held"))?; + Ok(tin.formatted()) + } + + pub(crate) fn answer_return_tin(&self, q: &Query) -> Result { + let name = q.party.as_deref().ok_or_else(|| self.missing("party"))?; + let party = *self.parties.get(name).ok_or_else(|| { + Refusal::new( + RuleId::new(Area::Partner, 8), + format!("party {name:?} not in setup"), + ) + })?; + let tin = self.return_tin(party)?; + Ok(json_figures([("tin".to_string(), tin)])) + } + pub(crate) fn answer_project(&self, q: &Query) -> Result { let name = q .project @@ -475,6 +512,28 @@ pub(crate) fn check_project(b: &Books<'_>, x: &Expect) { /// Check one `party_tin_last4` expectation: the last four of the party's /// TIN the books keep (its TIN facts', else its W-9's; `""` for none: /// BS-partner-2). +/// Check one `return_tin` expectation: the full TIN a return produced for +/// the party carries, taken from the vault outside the books +/// (BS-partner-8's outside-the-books option). +pub(crate) fn check_return_tin(b: &Books<'_>, x: &Expect) { + let ex = b.ex; + assert!( + x.from.is_none() && x.to.is_none() && x.as_of.is_none(), + "{}: return_tin takes no dates", + ex.id + ); + let party = expect_party(b, x); + exactly(ex, x, &["tin".to_string()].into()); + let got = b + .return_tin(party) + .unwrap_or_else(|r| panic!("{}: return_tin refused ({})", ex.id, r.reason)); + assert_eq!( + got, x.figures(&ex.id)["tin"], + "{}: {:?}'s return TIN", + ex.id, x.party + ); +} + pub(crate) fn check_party_tin_last4(b: &Books<'_>, x: &Expect) { let ex = b.ex; assert!( From ad55ede8e4c4d6e70d791d382a58e6c5d017eea5 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 07:40:31 -0700 Subject: [PATCH 3/9] docs(claims): refused dates, amounts, periods, roles, exports, 1099 years and returns Each rule's Checked line names the bookspec example that now shows it, the enforcement site it goes through, and the test that runs it: - B-28 (BS-entry-4, BS-entry-7): the date past 9999 and the line past the cap, through `try_create_entry` and `add_line`; - B-12 (BS-period-4): the partly overlapping quarter, through `create_period`'s PeriodsOverlap; - B-23 (BS-ledger-19): the role on a header, through `Store::assign_role` and `check_assign_role`; - BS-format-10: the export refused through `IifError`; - B-19 (BS-1099-2): the 2027 worksheet refused through `RuleNotKnown`; - B-26 (BS-partner-8): the return taking 123-45-6789 from the vault. Co-Authored-By: DeepSeek V4.1 Flash --- docs/spec/claims/1099.md | 9 ++++++++- docs/spec/claims/entry.md | 8 +++++++- docs/spec/claims/format.md | 9 ++++++++- docs/spec/claims/ledger.md | 8 +++++++- docs/spec/claims/partner.md | 9 ++++++++- docs/spec/claims/period.md | 10 +++++++++- 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/docs/spec/claims/1099.md b/docs/spec/claims/1099.md index 9ca2e6da..1252a7fd 100644 --- a/docs/spec/claims/1099.md +++ b/docs/spec/claims/1099.md @@ -62,7 +62,14 @@ Kani `payment_bucket_is_the_table`, `payment_split_is_exact`, `the_untagged_note_counts_only_money_out`, `the_other_receipts_note_says_what_each_entry_did` (ledger-package); the MCP tests through a real books server (ledger-cli); bookspec -`examples/1099-*.toml`. +`examples/1099-*.toml`, including +`1099-a-worksheet-for-an-unpublished-year-is-refused` (BS-1099-2, the +option picked: a 2027 worksheet is refused through +`LedgerError::RuleNotKnown` as unknown_1099_year, never guessed), run by +`every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`; the example for the option +not picked, `1099-a-worksheet-for-an-unpublished-year-carries-the-last-amount`, +is skipped. ### Product rules diff --git a/docs/spec/claims/entry.md b/docs/spec/claims/entry.md index c1556792..657b8ed2 100644 --- a/docs/spec/claims/entry.md +++ b/docs/spec/claims/entry.md @@ -107,7 +107,13 @@ replay pair; `a_foreign_amount_and_a_rate_take_at_most_ten_places`), `out_of_range_amount_is_rejected`; `ledger-store` `tests/bounds.rs`, `tests/store.rs` `a_journaled_foreign_line_finer_than_ten_places_still_opens`; `ledger-cli` `server.rs` `periods_are_a_year_and_a_month`, -`dates_are_four_digit_years_and_real_days`. +`dates_are_four_digit_years_and_real_days`; bookspec examples +`entry-a-date-past-9999-is-refused` and +`entry-an-amount-past-the-line-cap-is-refused` (BS-entry-7, the option picked: +the books refuse `try_create_entry`'s date past 9999 and `add_line`'s amount +past the cap, read off `LedgerError::DateOutOfRange` and +`LedgerError::AmountOutOfRange`), run by `every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`. ### Product rules diff --git a/docs/spec/claims/format.md b/docs/spec/claims/format.md index fd2b2db6..e0935d5d 100644 --- a/docs/spec/claims/format.md +++ b/docs/spec/claims/format.md @@ -289,7 +289,14 @@ a file. **Checked:** `ledger-import` `tests/spec_import.rs` `tests/package.rs` (`the_package_refuses_an_entry_on_two_receivable_or_payable_accounts`); `ledger-cli` `oracle_mcp.rs` -(`iif_and_the_package_refuse_an_entry_on_two_receivable_or_payable_accounts`). +(`iif_and_the_package_refuse_an_entry_on_two_receivable_or_payable_accounts`); +bookspec example +`format-an-export-of-an-entry-on-two-control-accounts-is-refused` +(BS-format-10, the option picked: an entry an earlier version wrote on a +receivable and a payable account is refused for export, read off +`IifError::SeveralReceivablePayableAccounts`), run by +`every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`. **Enforced:** `ledger_import::iif` (`field`, `iif_accounts`, `iif_classes`, `journal_to_iif`, `Iif::warnings`, `Iif::changes`, `Iif::receivable_payable`), diff --git a/docs/spec/claims/ledger.md b/docs/spec/claims/ledger.md index cffad93e..8e227a7c 100644 --- a/docs/spec/claims/ledger.md +++ b/docs/spec/claims/ledger.md @@ -191,7 +191,13 @@ None. `check_assign_role`, `Ledger::check_renumber_account` (an assigned role frees its code); the control guard and `Ledger::control_check` (B-38). **Checked:** `ledger-core` `tests/spec_roles.rs`, `tests/spec_documents.rs`; -`ledger-store` replay of `role.assigned`. +`ledger-store` replay of `role.assigned`; bookspec example +`ledger-a-role-on-an-account-of-the-wrong-type-is-refused` (BS-ledger-19, +the option picked: the receivables role on a header account is refused +through `Store::assign_role` and `Ledger::check_assign_role`'s +`ControlAccountMismatch`, and the chart is as it was), run by +`every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`. ### Product rules diff --git a/docs/spec/claims/partner.md b/docs/spec/claims/partner.md index 55313a03..6ae557c8 100644 --- a/docs/spec/claims/partner.md +++ b/docs/spec/claims/partner.md @@ -128,7 +128,14 @@ the books' last fours). `tests/spec_parties.rs`, `tests/spec_vault.rs` (every response, every file handed over, the server's log, and every file in the books' folder outside `vault/` and the files kept for their download scanned for the -TINs in any formatting), `tests/tin_files.rs` +TINs in any formatting), bookspec examples +`partner-a-return-takes-the-full-tin-from-the-vault` (BS-partner-8, the +option picked: the books hold 6789 and the return takes 123-45-6789 from +the vault) and `partner-a-return-with-no-full-tin-is-refused` (the nowhere +option, which wanderbooks does not pick: the return is refused +tin_not_held), the first run by `every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`, the second skipped; +`tests/tin_files.rs` (`a_tin_bearing_file_is_refused_from_every_session_and_after_a_restart`, `text_with_a_vault_tin_or_a_printed_one_is_refused_and_a_bank_export_is_not`, `text_probing_for_a_tin_is_refused_alike_by_every_filing_tool`), and the diff --git a/docs/spec/claims/period.md b/docs/spec/claims/period.md index 386ac05d..da827d21 100644 --- a/docs/spec/claims/period.md +++ b/docs/spec/claims/period.md @@ -37,7 +37,15 @@ decision, D-73, 2026-11-08). **Checked:** Kani store property `refused_commands_never_reach_the_journal` (no draft in a closed period); `periods_nest_but_never_partially_overlap` (`crates/ledger-core/tests/invariants.rs`) and `periods_nest` -(`crates/ledger-core/tests/spec_ledger.rs`). +(`crates/ledger-core/tests/spec_ledger.rs`); bookspec example +`period-a-period-that-partly-overlaps-another-is-refused` (BS-period-4, the +option picked: a February through April period that shares days with a +closed quarter is refused through `Ledger::create_period`'s +`PeriodsOverlap`, and the quarter stands), run by +`every_bookspec_example_holds` and +`the_runner_reports_every_claimed_rule_green`; the example for the option +not picked, `period-a-period-that-partly-overlaps-another-is-allowed`, is +skipped. ### Product rules From b3ee97a94fea25893c190de63c10e65624d16e28 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 08:49:18 -0700 Subject: [PATCH 4/9] docs(plan): S-E5, the old Rule n labels in wanderbooks move to statement positions S-C8 dropped BS-statement-3's and BS-partner-3's Rule n labels, which had drifted from their positions; wanderbooks' cash-flow code and tests still cite them by label (Rule 11 is statement 16). Co-Authored-By: Claude Opus 5.5 --- plan/p14-bookspec.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plan/p14-bookspec.md b/plan/p14-bookspec.md index 6880e91c..2f00a894 100644 --- a/plan/p14-bookspec.md +++ b/plan/p14-bookspec.md @@ -87,6 +87,7 @@ workers have been faster). | **S-C8** | bookspec's records cite their own parts by position: BS-format-2's statements cite each other as "(item 22)", "(item 18)", "(item 24)" (the old word, though the number is the position), and BS-statement-3's and BS-partner-3's statements open with "Rule n" labels that no longer equal their positions (BS-statement-3 statement 16 is "Rule 11"). Rewrite the cross-references as "statement k", and drop the labels or keep them only where an outside text cites them (then say so); extend the checker so a record's internal "statement k" names a part it has (found by S-E3, 2026-11-09) | S-E3 merged | bookspec `rules/format/BS-format-2.toml`, `rules/statement/BS-statement-3.toml`, `rules/partner/BS-partner-3.toml`, the checker | S | | **S-E4** | Retired bookspec ids cited as authority: 35 `SOURCE:` clauses across four crates still cite a retired record (`BS-format-1` x23, `BS-ledger-13` x8, `BS-ledger-12` x4; found by V-D2, 2026-11-09). Each cites what states the rule now: the product rule the retirement names (`BS-ledger-13` is `WB-ledger-16`, `BS-ledger-12` is `WB-ledger-15`), and for `BS-format-1`, whose retirement names no successor, the rule that does state each clause's fact (read the retirement reason and the old B-27 text). Then `tests/bookspec/source_refs.rs` refuses a retired id in a `SOURCE:` clause, with a self-test | V-D2 merged | the 35 sites, `tests/bookspec/source_refs.rs` | S | | **V-D3** | V-D2's audit findings: `audit::closes` takes its unit as `minor_units().unwrap_or(2)` while the allocation uses `allocation_places()`, so for a currency with no minor unit the audit's bound is looser than the split it checks; and before a count is trued up, a legacy last-absorbs close with four or more members may drift past one unit (BS-partner-1 statement 6 allows it until the true-up), which the audit's loose bound would still flag. Decide each from the rule, fix, and test; the replayed foreign original (no bound on replay, by design, since old books must read) stays documented | V-D2 merged | `audit.rs`, its tests | S | +| **S-E5** | Wanderbooks' bare "Rule n" / "rule n" mentions of BS-statement-3's and BS-partner-3's old labels (35 capitalised and about 78 lowercase, mostly in `cashflow.rs`, `tests/spec_cash_flow.rs`, `tests/spec_reports.rs`, `docs/spec/claims/statement.md`) move to `BS-statement-3 statement k` / `BS-partner-3 statement k`: S-C8 dropped the labels, which had drifted from the positions (Rule 1 is statement 2, Rule 11 statement 16, 5a-5d statements 7-10; S-C8's report has the line-by-line map). Then `tests/bookspec/sub_refs.rs` refuses a bare "Rule n" beside a cash-flow or party citation | S-C8 merged and pinned | those files, `sub_refs.rs` | S | | **K-U10** | `impl Amount for Decimal`'s `checked_add` and `checked_mul` exact or None (BS-ledger-1: a product the arithmetic can't hold MUST be refused; BS-entry-4): `checked_mul` rounds a product whose mantissa passes 96 bits (K-U1's fix, 2026-11-08: `1050000010.0000000001 × 9.9999999999` books 10500000099.90 for an exact 10500000099.8949…, both inputs within D-75's ten places; pinned by `the_mantissa_edge_is_outside_the_precondition_and_pinned`), and `checked_add`: `rust_decimal` rounds dust away within one unit of `Decimal::MAX` (`MAX + 0.4` gives `Some(MAX)`), breaking the kernel's exact-or-None contract (found by K-U4, 2026-11-08; pinned by the canary `finding_decimal_add_rounds_dust_at_the_edge`): refuse a sum whose result scale fell below the operands'; after the sweep units merge (the file is shared). Also from K-U3's review (2026-11-09): `split_by_weights_into` ranks remainders by Decimal's rounded quotient, so an exact tie is decided by the 28th digit, not given to the earlier part as the docs and the Lean model say (20.00 by 4/1/1 gives 13.33, 3.34, 3.33 where exact ranking gives 13.34, 3.33, 3.33); the pre-U3 sites did the same, and K-U3 kept it and pinned both cases. Ranking by the exact integer remainder (`whole x w - part x total`, as `share_floor_with` does) moves a cent between parties in reports recomputed for past periods: an owner decision, put with the cents it moves before it lands | K-U4 merged | `kernel/entries.rs`, the canary, the bridge | S | | **V-D** | Coverage report and floor; `rust_decimal` checked against exact rationals (task D) | none | in flight 2026-11-08 | S | | **V-D2** | Tests where coverage is thinnest (the V-D findings, `docs/coverage-floor.md`): `ledger-store` `reconcilable.rs` (0% from the store's own suite), `ledger-core` `evidence.rs` (34% lines, 9% branches from the core's suite), `ledger.rs`'s command steps (`mark_sent_steps`, `settle_steps`, `run_payroll_steps`, …) and refusal arms, `conventions.rs` and `settings.rs` validation refusals, `audit.rs` branches; each test encodes the rule it covers; the floor rises with them | V-D | the crates' tests | M | From eb073b53c694337cdb1a2ec9ac9c3d1ee871d62b Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 08:58:16 -0700 Subject: [PATCH 5/9] style(core): rustfmt the refused-input adapter additions The X-H1d adapter commits left two expressions the workspace's rustfmt reflows: the `matches!` in `check.rs` and the `assert_eq!` arguments in `queries/parties.rs`. No behavior changes; `cargo fmt --all -- --check` now passes. Co-Authored-By: DeepSeek V4.1 Flash --- crates/ledger-core/tests/bookspec/check.rs | 5 ++++- crates/ledger-core/tests/bookspec/queries/parties.rs | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/ledger-core/tests/bookspec/check.rs b/crates/ledger-core/tests/bookspec/check.rs index f82063a9..1c6fd669 100644 --- a/crates/ledger-core/tests/bookspec/check.rs +++ b/crates/ledger-core/tests/bookspec/check.rs @@ -95,7 +95,10 @@ pub(crate) fn check(b: &Books<'_>, x: &Expect) -> Option<&'static str> { ex.id ); if let Some(want) = &x.refused - && matches!(q, "vendor_1099" | "iif_lines" | "iif_changes" | "return_tin") + && matches!( + q, + "vendor_1099" | "iif_lines" | "iif_changes" | "return_tin" + ) { match b.answer(&x.query()) { Err(r) => assert_eq!(&r.reason, want, "{}: {q} refused ({})", ex.id, r.reason), diff --git a/crates/ledger-core/tests/bookspec/queries/parties.rs b/crates/ledger-core/tests/bookspec/queries/parties.rs index fb0443c0..9fcf80c1 100644 --- a/crates/ledger-core/tests/bookspec/queries/parties.rs +++ b/crates/ledger-core/tests/bookspec/queries/parties.rs @@ -528,9 +528,11 @@ pub(crate) fn check_return_tin(b: &Books<'_>, x: &Expect) { .return_tin(party) .unwrap_or_else(|r| panic!("{}: return_tin refused ({})", ex.id, r.reason)); assert_eq!( - got, x.figures(&ex.id)["tin"], + got, + x.figures(&ex.id)["tin"], "{}: {:?}'s return TIN", - ex.id, x.party + ex.id, + x.party ); } From 75533aeb4567687da988acf7a44c01ebbcbe1531 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 08:58:25 -0700 Subject: [PATCH 6/9] test(core): every role the schema lists is a role the engine assigns The schema's `ACCOUNT_ROLES` is now the list BS-ledger-8 names, not a list read off any engine. The adapter passes `assign_role`'s key straight to `Store::assign_role`, so a listed key this engine spelled differently would turn an example into a refused assignment it did not ask for. The new test walks the schema's list and requires `control_account` to resolve every key, which is also the check that `other_deductions_payable` is now listed and the internal-only roles are not. Co-Authored-By: DeepSeek V4.1 Flash --- crates/ledger-core/tests/bookspec/events.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/ledger-core/tests/bookspec/events.rs b/crates/ledger-core/tests/bookspec/events.rs index 8b82d9f9..40ecbfee 100644 --- a/crates/ledger-core/tests/bookspec/events.rs +++ b/crates/ledger-core/tests/bookspec/events.rs @@ -928,3 +928,18 @@ impl Books<'_> { .unwrap_or_else(|r| panic!("{what}: {}", r.reason)); } } + +/// Every role the schema's list names is a role this engine's command +/// takes, spelled the same: the adapter passes `assign_role`'s key +/// straight to `Store::assign_role`, so a key this engine spelled +/// differently would make every example that named it a refused +/// assignment the example did not ask for. +#[test] +fn every_listed_role_is_the_engine_role() { + for &(role, _) in bookspec_checker::ACCOUNT_ROLES { + assert!( + ledger_core::control_account(role).is_some(), + "{role}: the schema names a role this engine does not have" + ); + } +} From 9c2e586733a2f5a9737ff86a3f9b30cf418a4d94 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 08:58:25 -0700 Subject: [PATCH 7/9] docs(claims): BS-ledger-19's example shows both refusals The example is renamed to what it now shows, and it refuses two assignments: the receivables role on a header account (not postable) and the payables role on a postable revenue account (wrong type). The claims entry names both and the new example id. Co-Authored-By: DeepSeek V4.1 Flash --- docs/spec/claims/ledger.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/spec/claims/ledger.md b/docs/spec/claims/ledger.md index 8e227a7c..514a23da 100644 --- a/docs/spec/claims/ledger.md +++ b/docs/spec/claims/ledger.md @@ -192,10 +192,11 @@ None. frees its code); the control guard and `Ledger::control_check` (B-38). **Checked:** `ledger-core` `tests/spec_roles.rs`, `tests/spec_documents.rs`; `ledger-store` replay of `role.assigned`; bookspec example -`ledger-a-role-on-an-account-of-the-wrong-type-is-refused` (BS-ledger-19, -the option picked: the receivables role on a header account is refused -through `Store::assign_role` and `Ledger::check_assign_role`'s -`ControlAccountMismatch`, and the chart is as it was), run by +`ledger-a-role-on-a-header-or-wrong-typed-account-is-refused` (BS-ledger-19, +the option picked: the receivables role on a header account and the payables +role on a postable revenue account are refused through `Store::assign_role` +and `Ledger::check_assign_role`'s `ControlAccountMismatch`, and the chart is +as it was), run by `every_bookspec_example_holds` and `the_runner_reports_every_claimed_rule_green`. From b72ba2514a574693be71e4838ebed44362c8209b Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 09:00:48 -0700 Subject: [PATCH 8/9] docs: SOURCE lines cite what states the rule now, not a retired record Unit S-E4: 37 SOURCE clauses across ledger-core, ledger-gui, ledger-mcp and ledger-store still cited records bookspec retired on 2026-11-08 (D-71). A retired id names what a rule was, not what governs now, so each clause cites its successor: BS-ledger-13 is WB-ledger-16 (the audit), BS-ledger-12 is WB-ledger-15 (evidence read from the bytes), and BS-format-1, whose retirement names no successor, becomes the WB-format rule that states each clause's fact: WB-format-1 (the format gate), WB-format-2 (a journal feature declared before its first line), WB-format-3 (the owner's format upgrade) or WB-format-4 (each projection schema in a file of its own). No clause's fact was left without a live rule, so no claim was added. Co-Authored-By: DeepSeek V4.1 Flash --- crates/ledger-core/src/ledger.rs | 2 +- crates/ledger-core/tests/document_commands.rs | 2 +- crates/ledger-gui/tests/newer_journal.rs | 2 +- crates/ledger-mcp/src/tools/audit.rs | 14 ++++++------- crates/ledger-mcp/src/tools/evidence.rs | 8 ++++---- crates/ledger-mcp/src/tools/projects.rs | 2 +- crates/ledger-store/src/audit.rs | 2 +- crates/ledger-store/src/form1099.rs | 2 +- crates/ledger-store/src/format.rs | 4 ++-- crates/ledger-store/src/journal.rs | 4 ++-- crates/ledger-store/src/store.rs | 20 +++++++++---------- .../ledger-store/tests/document_projects.rs | 2 +- crates/ledger-store/tests/form1099.rs | 2 +- crates/ledger-store/tests/format.rs | 2 +- crates/ledger-store/tests/format_upgrade.rs | 2 +- crates/ledger-store/tests/projects.rs | 2 +- .../ledger-store/tests/schedule_minor_unit.rs | 2 +- 17 files changed, 37 insertions(+), 37 deletions(-) diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index 0de607ec..f7dcd0bc 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -6122,7 +6122,7 @@ impl Ledger { /// GOAL: record a send as a fact of the document's life, without /// touching a figure: a send moves no money. /// SOURCE: BS-arap-2 (a document keeps no figure of its - /// own), BS-format-1 (the invoice.sent line, feature documents_v2); + /// own), WB-format-2 (the invoice.sent line, feature documents_v2); /// plan P4b S5, A-4 (the books record sends; they never send). /// INVARIANTS: /// 1. The invoice MUST exist, and a send MUST NOT be dated before it. diff --git a/crates/ledger-core/tests/document_commands.rs b/crates/ledger-core/tests/document_commands.rs index e33e7bbc..b7d9e86a 100644 --- a/crates/ledger-core/tests/document_commands.rs +++ b/crates/ledger-core/tests/document_commands.rs @@ -12,7 +12,7 @@ //! SOURCE: BS-arap-2 (a document keeps no figure of its own); BS-arap-5 //! (the direct write-off); BS-arap-6 (settlement); BS-ledger-5, //! BS-ledger-6 (reversal and void); BS-period-2 (a closed period -//! takes no postings); BS-format-1 (the invoice.sent line). +//! takes no postings); WB-format-2 (the invoice.sent line). //! ``` //! //! Every test compares the whole ledger to a clone taken before a refused diff --git a/crates/ledger-gui/tests/newer_journal.rs b/crates/ledger-gui/tests/newer_journal.rs index 15d37fd7..cb5e1cb5 100644 --- a/crates/ledger-gui/tests/newer_journal.rs +++ b/crates/ledger-gui/tests/newer_journal.rs @@ -4,7 +4,7 @@ //! ```text //! GOAL: Tobin, Mo or the CPA, on an older viewer, learn to update it, //! not that the books are damaged. -//! SOURCE: BS-format-1; plan/served.md 5.1 (the viewer reads the journal +//! SOURCE: WB-format-1; plan/served.md 5.1 (the viewer reads the journal //! directly, so it meets the same gate). //! ``` diff --git a/crates/ledger-mcp/src/tools/audit.rs b/crates/ledger-mcp/src/tools/audit.rs index 0f0055d8..2f011048 100644 --- a/crates/ledger-mcp/src/tools/audit.rs +++ b/crates/ledger-mcp/src/tools/audit.rs @@ -6,7 +6,7 @@ //! that gives the same findings and the same workpaper for the same //! books, and attestations that rest on a head that has not moved. //! SOURCE: IRS Pub. 583 (records); Rev. Proc. 98-25 §5.01 (the audit -//! trail); BS-ledger-13; DECISION D-9 (the bar), D-53 +//! trail); WB-ledger-16; DECISION D-9 (the bar), D-53 //! (an audit is a run, not a job), IZ-1k (the head). //! INVARIANTS: //! 1. `books.audit` previews (what it will check; nothing changes), @@ -356,7 +356,7 @@ impl BooksServer { /// ```text /// GOAL: the whole programme of checks over the books through a day, /// its workpaper filed, the run journaled. - /// SOURCE: plan/served.md P8; BS-ledger-13; D-9, D-53. + /// SOURCE: plan/served.md P8; WB-ledger-16; D-9, D-53. /// INVARIANTS: /// 1. Without `confirmed`, a preview that lists the checks and /// changes nothing; with it, the preview's digest is required. @@ -529,7 +529,7 @@ impl BooksServer { /// ```text /// GOAL: an audit run's workpaper, whole. - /// SOURCE: plan/served.md P8 step 3; BS-ledger-13. + /// SOURCE: plan/served.md P8 step 3; WB-ledger-16. /// INVARIANTS: /// 1. Read-only; the JSON is the one filed, the PDF is handed over. /// ``` @@ -581,7 +581,7 @@ impl BooksServer { /// ```text /// GOAL: a checker's claim that a receipt supports an entry. - /// SOURCE: plan/served.md P8 step 4; BS-ledger-13; D-9. + /// SOURCE: plan/served.md P8 step 4; WB-ledger-16; D-9. /// INVARIANTS: /// 1. The reading is compared with the entry's amount and date; a /// difference is recorded, marked, and never counts. @@ -619,7 +619,7 @@ impl BooksServer { /// ```text /// GOAL: a checker's claim that it reviewed an entry whole. - /// SOURCE: plan/served.md P8 step 4; BS-ledger-13. + /// SOURCE: plan/served.md P8 step 4; WB-ledger-16. /// ``` #[tool( name = "books.review_entry", @@ -649,7 +649,7 @@ impl BooksServer { /// ```text /// GOAL: a checker's claim that a period is right, resting on an /// audit run at the books' current head. - /// SOURCE: plan/served.md P8 step 4; BS-ledger-13; D-9; IZ-1k. + /// SOURCE: plan/served.md P8 step 4; WB-ledger-16; D-9; IZ-1k. /// INVARIANTS: /// 1. Refused unless an audit through the period's end stands at /// the current head. @@ -697,7 +697,7 @@ impl BooksServer { /// ```text /// GOAL: every claim that someone checked, and whether it counts. - /// SOURCE: plan/served.md P8 step 4; BS-ledger-13; D-9. + /// SOURCE: plan/served.md P8 step 4; WB-ledger-16; D-9. /// INVARIANTS: /// 1. `counted` is read against the bar as it stands now. /// ``` diff --git a/crates/ledger-mcp/src/tools/evidence.rs b/crates/ledger-mcp/src/tools/evidence.rs index f6e69123..ff33e10f 100644 --- a/crates/ledger-mcp/src/tools/evidence.rs +++ b/crates/ledger-mcp/src/tools/evidence.rs @@ -7,7 +7,7 @@ //! wherever they are evidence. //! SOURCE: plan/served.md P6 step 4 (defect 4) and step 5 (defect 10); //! IZ-18 (no FIFO guess: `entry.settles` only); Rev. Proc. 98-25 -//! (an audit trail); BS-ledger-12. +//! (an audit trail); WB-ledger-15. //! ``` use super::*; @@ -23,7 +23,7 @@ impl BooksServer { /// ```text /// GOAL: find a filed file by its exact bytes, its receipt number, or /// what its text says. - /// SOURCE: plan/served.md P6 step 4; BS-ledger-12. + /// SOURCE: plan/served.md P6 step 4; WB-ledger-15. /// INVARIANTS: /// 1. EXACTLY one of sha256, receipt_number and text is given. /// 2. Withdrawn files are listed too, marked (a duplicate is found @@ -96,7 +96,7 @@ impl BooksServer { /// ```text /// GOAL: record what a human read off a file, without touching the file /// or the ledger's amounts. - /// SOURCE: plan/served.md P6 step 5 (defect 10); BS-ledger-12. + /// SOURCE: plan/served.md P6 step 5 (defect 10); WB-ledger-15. /// INVARIANTS: /// 1. Journaled as `attachment.annotated`; a refusal journals nothing. /// 2. The fields are the closed list of `AnnotationFieldsParam`; at @@ -206,7 +206,7 @@ impl BooksServer { /// GOAL: hold identical files once, linked wherever they were evidence, /// without ever leaving an entry the receipt policy covers with /// none. - /// SOURCE: plan/served.md P6 step 4 (defect 4); BS-ledger-12. + /// SOURCE: plan/served.md P6 step 4 (defect 4); WB-ledger-15. /// INVARIANTS: /// 1. Records sharing one blob SHA are one set; the oldest is kept /// and linked to every subject the others had. diff --git a/crates/ledger-mcp/src/tools/projects.rs b/crates/ledger-mcp/src/tools/projects.rs index 637d634a..34a0bac0 100644 --- a/crates/ledger-mcp/src/tools/projects.rs +++ b/crates/ledger-mcp/src/tools/projects.rs @@ -9,7 +9,7 @@ //! the viewer and bookspec use. //! SOURCE: BS-statement-5 (DECISION, owner, 2026-10-21; job //! costing; ASC 450-30-25-1 for on-spec work), BS-period-3 (omitted -//! dates), BS-format-1 (journal feature `projects`). +//! dates), WB-format-2 (journal feature `projects`). //! INVARIANTS: //! 1. Every field is checked before anything is written; a refusal //! journals nothing. diff --git a/crates/ledger-store/src/audit.rs b/crates/ledger-store/src/audit.rs index 43a58824..a07baeb8 100644 --- a/crates/ledger-store/src/audit.rs +++ b/crates/ledger-store/src/audit.rs @@ -7,7 +7,7 @@ //! head, so the same books give the same run once, and an //! attestation rests on a head that has not moved. //! SOURCE: IRS Pub. 583 (records); Rev. Proc. 98-25 §5.01 (the audit -//! trail); BS-ledger-13; DECISION D-9 (the bar), D-53 +//! trail); WB-ledger-16; DECISION D-9 (the bar), D-53 //! (an audit is a run, not a job), IZ-1k (the head). //! INVARIANTS: //! 1. The figure head is the seq of the last journal line that bears on diff --git a/crates/ledger-store/src/form1099.rs b/crates/ledger-store/src/form1099.rs index 4d14f1a2..9ecf11ed 100644 --- a/crates/ledger-store/src/form1099.rs +++ b/crates/ledger-store/src/form1099.rs @@ -7,7 +7,7 @@ //! GOAL: every event round-trips: decode(encode(x)) == x, and every line //! written before decisions were kept reads as it always meant. //! SOURCE: plan/served.md 5.2, 5.5 (a legacy null is undetermined); -//! BS-format-1 (an older reader says "newer", never "corrupt"). +//! WB-format-1 (an older reader says "newer", never "corrupt"). //! INVARIANTS: //! 1. vendor.1099_set {vendor_id, form_1099: "nec"|"misc"| //! "not_reportable"|null, reason_kind?, reason?, effective?, diff --git a/crates/ledger-store/src/format.rs b/crates/ledger-store/src/format.rs index a0faf19a..79ac76f5 100644 --- a/crates/ledger-store/src/format.rs +++ b/crates/ledger-store/src/format.rs @@ -377,7 +377,7 @@ impl JournalFormat { /// ```text /// GOAL: an older wanderbooks never misreads newer books, and never /// calls them corrupt. - /// SOURCE: BS-format-1; plan/served.md 5.1 (format gate). + /// SOURCE: WB-format-1; plan/served.md 5.1 (format gate). /// INVARIANTS: /// 1. Only lines that parsed, chained and kept their seq reach the /// gate: damage stays JournalCorrupt. @@ -647,7 +647,7 @@ fn newer(line: &JournalLine, need: String) -> StoreError { /// ```text /// GOAL: damage is never reported as books from a newer wanderbooks, and /// books from a newer wanderbooks never as damage. -/// SOURCE: BS-format-1 (the envelope and its hash are frozen across +/// SOURCE: WB-format-1 (the envelope and its hash are frozen across /// formats); docs/receipt-store.md "Line format" (a line's hash is /// checked only by the next line's prev). /// INVARIANTS: diff --git a/crates/ledger-store/src/journal.rs b/crates/ledger-store/src/journal.rs index de106dbb..43bb4058 100644 --- a/crates/ledger-store/src/journal.rs +++ b/crates/ledger-store/src/journal.rs @@ -84,7 +84,7 @@ impl JournalLine { /// hash chain covers) is a fixed point. /// SOURCE: R-2 (no floats in money paths); found by fuzz_journal_line /// (2026-10-13): a float payload re-canonicalized differently; - /// BS-format-1 (the envelope is frozen). + /// WB-format-1 (the envelope is frozen). /// INVARIANTS: /// 1. NEVER accepts a floating-point number anywhere in the line: /// amounts are decimal strings and ids are integers. @@ -267,7 +267,7 @@ pub fn read_and_verify_journal(path: &Path) -> Result, StoreErr /// /// ```text /// GOAL: books read whole and verified, or refused with the reason. -/// SOURCE: docs/receipt-store.md "Line format"; BS-format-1. +/// SOURCE: docs/receipt-store.md "Line format"; WB-format-1. /// INVARIANTS: /// 1. Line by line, in order: parse, seq, chain, then the format gate. /// 2. Damage before the first line this version can't read is diff --git a/crates/ledger-store/src/store.rs b/crates/ledger-store/src/store.rs index 377652fc..428e7ebf 100644 --- a/crates/ledger-store/src/store.rs +++ b/crates/ledger-store/src/store.rs @@ -72,7 +72,7 @@ pub const LEGACY_DB_FILE: &str = "wanderbooks.db"; /// open (plan/served.md 3.9: before consent, the way back is always /// there), however far this version's projection has moved. /// SOURCE: plan/served.md 3.9, the lead's decision (2026-10-21: each -/// schema in a file of its own); BS-format-1. +/// schema in a file of its own); WB-format-4. /// INVARIANTS: /// 1. A version opens or creates only its own schema's file (or the /// legacy file when that already holds its schema); a missing one is @@ -909,7 +909,7 @@ impl Store { /// /// ```text /// GOAL: books that tell every reader which format they are in. - /// SOURCE: BS-format-1; plan/served.md 5.1 (format gate). + /// SOURCE: WB-format-1; plan/served.md 5.1 (format gate). /// SCENARIOS: /// 1. IF the format is newer than SUPPORTED_JOURNAL_FORMAT, or 0: /// SHOULD refuse (FormatRefused), journaling nothing @@ -969,7 +969,7 @@ impl Store { /// instead of misreading a line it thinks it knows, and books /// move past what their owner's older version reads only when /// the owner says so. - /// SOURCE: BS-format-1; plan/served.md 5.1 (format gate), 3.9 and + /// SOURCE: WB-format-1, WB-format-3; plan/served.md 5.1 (format gate), 3.9 and /// D-39 (the format upgrade). /// SCENARIOS: /// 1. IF the books declared it: SHOULD pass. @@ -1001,7 +1001,7 @@ impl Store { /// GOAL: older books move to this version's format at once, on the /// record, exactly once. /// SOURCE: plan/served.md 3.9 ("Format changes are the server's - /// gate"); D-39; BS-format-1. + /// gate"); D-39; WB-format-3. /// INVARIANTS: /// 1. One journal line declares the whole format: the format moves /// exactly once, never feature by feature. @@ -1512,7 +1512,7 @@ impl Store { /// ```text /// GOAL: a draft that is finished and waiting on its receipt says so, /// durably, and replays the same. - /// SOURCE: BS-format-1, BS-entry-5; plan P4. + /// SOURCE: WB-format-2, BS-entry-5; plan P4. /// SCENARIOS: /// 1. IF the engine refuses it (unknown or posted entry, a note the /// books won't keep): SHOULD refuse, journaling nothing. @@ -3319,7 +3319,7 @@ impl Store { /// GOAL: a document's new fields are journaled with it, so replay can /// never lose them, and a document naming none is written byte /// for byte as an earlier version wrote it. - /// SOURCE: BS-format-1, BS-arap-4 (plan P4b S4.3); A-2, A-13. + /// SOURCE: WB-format-2, BS-arap-4 (plan P4b S4.3); A-2, A-13. /// INVARIANTS: /// 1. A write naming a number, terms, a snapshot, a reference, a memo, /// a line account, quantity or unit price MUST need journal feature @@ -3811,7 +3811,7 @@ impl Store { /// ```text /// GOAL: a payment is journaled once and replays to the same figures, /// in any version that can open the books. - /// SOURCE: BS-format-1, BS-arap-2; plan/served.md P4b.7 + /// SOURCE: WB-format-2, BS-arap-2; plan/served.md P4b.7 /// (document_standing). /// SCENARIOS: /// 1. IF the engine refuses it (overpaid, before the invoice, a @@ -4207,7 +4207,7 @@ impl Store { /// ```text /// GOAL: a send is journaled once and replays to the same record, in /// every version that can read the books. - /// SOURCE: BS-format-1, BS-arap-2; plan P4b S5, A-4. + /// SOURCE: WB-format-2, BS-arap-2; plan P4b S5, A-4. /// SCENARIOS: /// 1. IF the engine refuses it (unknown or void invoice, a date /// before the invoice, no recipients, a text the books won't @@ -4534,7 +4534,7 @@ impl Store { /// ```text /// GOAL: a project in one command, journaled as two events only when /// it has facts, so a name-only project is a plain class. - /// SOURCE: WB-statement-5; BS-format-1 (journal feature + /// SOURCE: WB-statement-5; WB-format-2 (journal feature /// `projects`). /// INVARIANTS: /// 1. A refusal journals nothing: the whole record is rehearsed, and @@ -4971,7 +4971,7 @@ impl Store { /// ```text /// GOAL: a new monthly-even schedule's months are whole units of its /// currency, and what an earlier version journaled never moves. - /// SOURCE: BS-prepaid-4, BS-format-1, BS-entry-8; D-72 (owner, + /// SOURCE: BS-prepaid-4, WB-format-2, BS-entry-8; D-72 (owner, /// 2026-11-08). /// INVARIANTS: /// 1. The rule is a fact of the books when the schedule is written: diff --git a/crates/ledger-store/tests/document_projects.rs b/crates/ledger-store/tests/document_projects.rs index eef6fa81..e02de341 100644 --- a/crates/ledger-store/tests/document_projects.rs +++ b/crates/ledger-store/tests/document_projects.rs @@ -9,7 +9,7 @@ //! ```text //! GOAL: a document's projects are journaled once, replay exactly, and //! never reach books whose owner hasn't consented to the format. -//! SOURCE: BS-statement-5 statement 6, BS-format-1, BS-entry-4; plan/served.md +//! SOURCE: BS-statement-5 statement 6, WB-format-2, BS-entry-4; plan/served.md //! D-39 (the owner's upgrade). //! ``` diff --git a/crates/ledger-store/tests/form1099.rs b/crates/ledger-store/tests/form1099.rs index 1ae8af53..97a59653 100644 --- a/crates/ledger-store/tests/form1099.rs +++ b/crates/ledger-store/tests/form1099.rs @@ -8,7 +8,7 @@ //! ```text //! GOAL: the decision ("no 1099" included) and how each payment was made //! are kept as records, and no version ever misreads them. -//! SOURCE: BS-1099-1, BS-format-1; ledger_store::form1099 INVARIANTS 1-5. +//! SOURCE: BS-1099-1, WB-format-2; ledger_store::form1099 INVARIANTS 1-5. //! ``` use chrono::NaiveDate; diff --git a/crates/ledger-store/tests/format.rs b/crates/ledger-store/tests/format.rs index 7e1e1e28..c78d79d4 100644 --- a/crates/ledger-store/tests/format.rs +++ b/crates/ledger-store/tests/format.rs @@ -5,7 +5,7 @@ //! GOAL: an older wanderbooks tells the human to update, not that the //! books are damaged, and never shows books that differ from the //! record without saying so. -//! SOURCE: BS-format-1; plan/served.md 5.1 (format gate). +//! SOURCE: WB-format-1; plan/served.md 5.1 (format gate). //! INVARIANTS: //! 1. A whole, chained line of a type this version doesn't know, or a //! journal.format_set newer than it reads, is NewerJournal: from diff --git a/crates/ledger-store/tests/format_upgrade.rs b/crates/ledger-store/tests/format_upgrade.rs index d94bcbbd..2613226b 100644 --- a/crates/ledger-store/tests/format_upgrade.rs +++ b/crates/ledger-store/tests/format_upgrade.rs @@ -9,7 +9,7 @@ //! without the owner's go-ahead, and an upgrade changes nothing of the //! books but what may be written from then on. //! SOURCE: plan/served.md 3.9 ("Format changes are the server's gate, not -//! the updater's"); D-39; BS-format-1. +//! the updater's"); D-39; WB-format-3. //! INVARIANTS: //! 1. New books declare this version's format, every feature it knows, //! as they are created: they wait for no upgrade. diff --git a/crates/ledger-store/tests/projects.rs b/crates/ledger-store/tests/projects.rs index 903d26f5..24b9565e 100644 --- a/crates/ledger-store/tests/projects.rs +++ b/crates/ledger-store/tests/projects.rs @@ -6,7 +6,7 @@ //! GOAL: a project's record is journaled once, whole, replays exactly, //! and books waiting for their format upgrade can still add //! projects by name but write nothing an older reader would misread. -//! SOURCE: BS-statement-5, BS-format-1; D-39 (the owner's upgrade). +//! SOURCE: BS-statement-5, WB-format-2; D-39 (the owner's upgrade). //! ``` use chrono::NaiveDate; diff --git a/crates/ledger-store/tests/schedule_minor_unit.rs b/crates/ledger-store/tests/schedule_minor_unit.rs index 5cd33940..d0446a64 100644 --- a/crates/ledger-store/tests/schedule_minor_unit.rs +++ b/crates/ledger-store/tests/schedule_minor_unit.rs @@ -5,7 +5,7 @@ //! ```text //! GOAL: a yen schedule's months are whole yen, and what an earlier //! version journaled never moves. -//! SOURCE: BS-prepaid-4, BS-format-1, BS-entry-8; plan/served.md D-72 +//! SOURCE: BS-prepaid-4, WB-format-2, BS-entry-8; plan/served.md D-72 //! (owner, 2026-11-08). //! INVARIANTS: //! 1. In books that declared the feature, a monthly_even schedule's From a63e02b810794725c0578663c9c98d48651d5f9c Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 09:00:52 -0700 Subject: [PATCH 9/9] test(core): a retired bookspec id is no source source_refs checked existence only, so a SOURCE clause could rest on a record that bookspec retired (D-71) as long as the record still existed. It now refuses a retired BS id unless the same clause names a live successor, so a historical mention can still read "formerly BS-ledger-13, now WB-ledger-16" while a bare retired id fails. The self-tests pin both sides. The tree after S-E4 has zero problems, and the free-text count stays 56 because every rewritten clause cites a product rule rather than falling through to free text. Co-Authored-By: DeepSeek V4.1 Flash --- .../ledger-core/tests/bookspec/source_refs.rs | 105 +++++++++++++++++- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/crates/ledger-core/tests/bookspec/source_refs.rs b/crates/ledger-core/tests/bookspec/source_refs.rs index b0d0634a..be611814 100644 --- a/crates/ledger-core/tests/bookspec/source_refs.rs +++ b/crates/ledger-core/tests/bookspec/source_refs.rs @@ -12,6 +12,12 @@ //! branch are counted, and that count is a floor-style constant: it //! changes only on purpose, in the same commit as the constant. //! +//! A bookspec id whose record is retired is no source: the retired +//! record states what the rule was, not what governs now. It is kept +//! only in a historical mention that names a live successor in the same +//! clause ("formerly BS-ledger-13, now WB-ledger-16"), so a reader can +//! follow the rule forward without the code resting on a retired id. +//! //! 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 @@ -146,6 +152,8 @@ fn is_path_like(token: &str) -> bool { struct ClauseCtx<'a> { root: &'a Path, bs_ids: &'a BTreeSet, + /// The retired records' ids and why each was retired. + retired_bs: &'a BTreeMap, wb_ids: &'a BTreeSet, identifiers: &'a BTreeSet, basenames: &'a BTreeSet, @@ -158,15 +166,29 @@ fn check_clause(clause: &str, ctx: &ClauseCtx<'_>, bad: &mut Vec, at: &s }; let mut errors: Vec = Vec::new(); let mut cited = false; - for id in ids_in(text, "BS-") { + let bs_ids = ids_in(text, "BS-"); + let wb_ids = ids_in(text, "WB-"); + // A live rule named in the same clause is a retired id's successor: + // the retirement names it, or the claims file does. + let successor = wb_ids.iter().any(|id| ctx.wb_ids.contains(id)) + || bs_ids + .iter() + .any(|id| ctx.bs_ids.contains(id) && !ctx.retired_bs.contains_key(id)); + for id in &bs_ids { cited = true; - if !ctx.bs_ids.contains(&id) { + if !ctx.bs_ids.contains(id) { errors.push(format!("unknown bookspec id {id}")); + } else if let Some(reason) = ctx.retired_bs.get(id) + && !successor + { + errors.push(format!( + "retired bookspec id {id} is no source ({reason}); cite the rule that states it now, or name its successor in this clause" + )); } } - for id in ids_in(text, "WB-") { + for id in &wb_ids { cited = true; - if !ctx.wb_ids.contains(&id) { + if !ctx.wb_ids.contains(id) { errors.push(format!("unknown product rule {id}")); } } @@ -256,6 +278,24 @@ pub(crate) fn every_source_line_cites_an_existing_rule_or_source() { .collect(); assert!(!bs_ids.is_empty(), "the pinned bookspec has rules"); + let retired_bs: BTreeMap = bookspec_checker::records() + .iter() + .filter(|r| r.is_retired()) + .map(|r| { + ( + r.id.to_string(), + r.retired + .as_ref() + .map(|retired| retired.reason.clone()) + .unwrap_or_default(), + ) + }) + .collect(); + assert!( + !retired_bs.is_empty(), + "the pinned bookspec has retired 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") { @@ -350,6 +390,7 @@ pub(crate) fn every_source_line_cites_an_existing_rule_or_source() { let ctx = ClauseCtx { root: &root, bs_ids: &bs_ids, + retired_bs: &retired_bs, wb_ids: &wb_ids, identifiers: &identifiers, basenames: &basenames, @@ -377,6 +418,62 @@ pub(crate) fn every_source_line_cites_an_existing_rule_or_source() { ); } +#[test] +fn a_retired_bookspec_id_is_no_source() { + let bs_ids: BTreeSet = ["BS-format-1", "BS-ledger-12", "BS-ledger-13", "BS-ledger-7"] + .into_iter() + .map(str::to_string) + .collect(); + let retired: BTreeMap = [ + ("BS-format-1", "wholly a product rule"), + ("BS-ledger-12", "kept as WB-ledger-15"), + ("BS-ledger-13", "kept as WB-ledger-16"), + ] + .into_iter() + .map(|(id, why)| (id.to_string(), why.to_string())) + .collect(); + let wb_ids: BTreeSet = ["WB-format-1", "WB-ledger-15", "WB-ledger-16"] + .into_iter() + .map(str::to_string) + .collect(); + let root = repo_root(); + let empty: BTreeSet = BTreeSet::new(); + let ctx = ClauseCtx { + root: &root, + bs_ids: &bs_ids, + retired_bs: &retired, + wb_ids: &wb_ids, + identifiers: &empty, + basenames: &empty, + }; + let problems = |text: &str| { + let mut bad = Vec::new(); + check_clause(text, &ctx, &mut bad, "t"); + bad + }; + + // A bare retired id is no source, however much plan or decision + // text surrounds it. + assert_eq!(problems("SOURCE: BS-ledger-13.").len(), 1); + assert_eq!( + problems("SOURCE: BS-ledger-12; plan/served.md P6.").len(), + 1 + ); + assert_eq!(problems("SOURCE: BS-format-1; D-39.").len(), 1); + + // A historical mention that names a live successor in the same + // clause is allowed, so the rule can be followed forward. + assert!(problems("SOURCE: formerly BS-ledger-13, now WB-ledger-16.").is_empty()); + assert!(problems("SOURCE: formerly BS-ledger-12, now WB-ledger-15.").is_empty()); + assert!(problems("SOURCE: formerly BS-format-1, now WB-format-1.").is_empty()); + assert!( + problems("SOURCE: formerly BS-ledger-12, now BS-ledger-7 and WB-ledger-15.").is_empty() + ); + + // A live rule alone passes as it always did. + assert!(problems("SOURCE: BS-ledger-7.").is_empty()); +} + fn retired_count(text: &str) -> usize { let chars: Vec = text.chars().collect(); let mut count = 0usize;