From 3fa0bddfda060e3453590ce8ac40fa848927a60c Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 10:38:59 -0700 Subject: [PATCH 1/2] test(core): golden file for the audit workpaper The audit's statements programme (rules.md B-45) now has a golden for one small book, hand-worked from B-45 in plan/golden-worksheets/audit-workpaper.md: 2,000.00 of capital, a 40.00 purchase with no receipt, a year of prepaid insurance never posted, a 5.00 draft, and one Revenue account with no tax category. Through 2026-03-31 that gives one failure (evidence_missing, entry 2, debits 40.00 over the 25.00 threshold) and four warnings: draft 4 on 2026-02-10, schedule 1 with 3 months / 300.00 due from 2026-01-31, account 4100, and vendor Tobin Harlow Supply at 900.00. Trial balance, equation, P&L articulation, control accounts, closes, assets, settings and the receipted entries are all silent. The golden holds the workpaper's statements data (scope, through, summary, findings). Hashes, the figure head and the programme version are not hand-verifiable, and the static check catalogue is not a finding, so they stay out; the worksheet says so. The first draft mis-marked evidence_missing a warning: the table and CODES say failure, the prose was wrong, and both the worksheet and the golden were corrected to the books. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/audit_workpaper.json | 52 +++++ crates/ledger-core/tests/golden_audit.rs | 186 ++++++++++++++++ plan/golden-worksheets/audit-workpaper.md | 198 ++++++++++++++++++ 3 files changed, 436 insertions(+) create mode 100644 crates/ledger-core/tests/golden/audit_workpaper.json create mode 100644 crates/ledger-core/tests/golden_audit.rs create mode 100644 plan/golden-worksheets/audit-workpaper.md diff --git a/crates/ledger-core/tests/golden/audit_workpaper.json b/crates/ledger-core/tests/golden/audit_workpaper.json new file mode 100644 index 00000000..0675e051 --- /dev/null +++ b/crates/ledger-core/tests/golden/audit_workpaper.json @@ -0,0 +1,52 @@ +{ + "scope": "month", + "through": "2026-03-31", + "summary": "1 failure(s), 4 warning(s), 0 note(s)", + "findings": [ + { + "code": "evidence_missing", + "severity": "failure", + "subject": { + "kind": "entry", + "id": 2 + }, + "message": "entry 2 (2026-01-05) has no receipt: its debits 40.00 are strictly greater than the 25.00 threshold" + }, + { + "code": "draft_open", + "severity": "warning", + "subject": { + "kind": "entry", + "id": 4 + }, + "message": "draft 4 (2026-02-10) \"Stamps\" is not posted" + }, + { + "code": "schedule_behind", + "severity": "warning", + "subject": { + "kind": "schedule", + "id": 1 + }, + "message": "\"Prepaid insurance\" has 3 month(s) due and not posted, from 2026-01-31, 300.00 in all" + }, + { + "code": "tax_category_missing", + "severity": "warning", + "subject": { + "kind": "account", + "id": 5 + }, + "message": "account 4100 Other income has no tax category" + }, + { + "code": "vendor_1099_undecided", + "severity": "warning", + "subject": { + "kind": "vendor", + "id": "Tobin Harlow Supply" + }, + "message": "Tobin Harlow Supply was paid 900.00 in a way that could need a 1099 and has no decision" + } + ] +} diff --git a/crates/ledger-core/tests/golden_audit.rs b/crates/ledger-core/tests/golden_audit.rs new file mode 100644 index 00000000..7051c374 --- /dev/null +++ b/crates/ledger-core/tests/golden_audit.rs @@ -0,0 +1,186 @@ +//! The audit workpaper's findings for one small book, hand-verified +//! (rules.md B-45; plan/golden-worksheets/audit-workpaper.md). +//! +//! The fixture is a bank, prepaid insurance, capital, two revenue and two +//! expense accounts, and a 12-month prepaid plan. It seeds five defects in +//! five check areas (`draft_open`, `evidence_missing`, `schedule_behind`, +//! `tax_category_missing`, `vendor_1099_undecided`) and leaves every other +//! check silent: the trial balance balances, the equation holds, the P&L +//! articulates, no control account or document exists, no close has run, no +//! asset is registered, and the entries the receipt policy reaches have +//! their receipts. +//! +//! The golden holds the workpaper's statements data — scope, through, +//! summary and findings — not the store's hashes, the figure head or the +//! programme's version (see the worksheet's "Scope of this golden"). + +use std::collections::BTreeSet; + +use chrono::NaiveDate; +use ledger_core::audit::{self, AuditInput, Severity}; +use ledger_core::settings::Settings; +use ledger_core::{ + AccountType, Currency, Ledger, Money, NewSchedule, ScheduleConvention, ScheduleKind, Side, +}; +use rust_decimal::Decimal; + +fn d(month: u32, day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(2026, month, day).unwrap() +} + +fn usd(amount: &str) -> Money { + Money::new(amount.parse::().unwrap(), Currency::USD) +} + +/// Post a balanced entry in one call and return its id. +fn post( + ledger: &mut Ledger, + date: NaiveDate, + description: &str, + lines: &[(u64, Side, &str)], +) -> u64 { + let id = ledger.create_entry(date, description); + for (account, side, amount) in lines { + ledger.add_line(id, *account, *side, usd(amount)).unwrap(); + } + ledger.post_entry(id).unwrap(); + id +} + +/// The worksheet's fixture: accounts in creation order (ids 1–7), then the +/// opening, the unreceipted purchase, the prepaid payment, and a draft. +fn audit_company() -> Ledger { + let mut ledger = Ledger::new(Currency::USD); + let bank = ledger + .add_account("1000", "Bank", AccountType::Asset, None, false) + .unwrap(); + let prepaid = ledger + .add_account("1300", "Prepaid Insurance", AccountType::Asset, None, false) + .unwrap(); + let capital = ledger + .add_account("3000", "Capital", AccountType::Equity, None, false) + .unwrap(); + let sales = ledger + .add_account("4000", "Sales", AccountType::Revenue, None, false) + .unwrap(); + let other_income = ledger + .add_account("4100", "Other income", AccountType::Revenue, None, false) + .unwrap(); + let supplies = ledger + .add_account("6000", "Supplies", AccountType::Expense, None, false) + .unwrap(); + let insurance = ledger + .add_account("6100", "Insurance", AccountType::Expense, None, false) + .unwrap(); + // Every Revenue/Expense account but 4100 gets a category: 4100 is the + // seeded tax-category defect and nothing else is uncategorized. + for (account, category) in [ + (sales, "gross_receipts"), + (supplies, "supplies"), + (insurance, "insurance"), + ] { + ledger + .set_account_tax(account, Some(category), None) + .unwrap(); + } + + // 1. Opening: 2,000.00 of capital into the bank. + post( + &mut ledger, + d(1, 1), + "Opening", + &[ + (bank, Side::Debit, "2000.00"), + (capital, Side::Credit, "2000.00"), + ], + ); + // 2. Printer paper, 40.00, no receipt: the evidence defect. + post( + &mut ledger, + d(1, 5), + "Printer paper", + &[ + (supplies, Side::Debit, "40.00"), + (bank, Side::Credit, "40.00"), + ], + ); + // 3. A year of insurance paid up front, with its receipt. The plan is + // never posted, so its months are the schedule defect. + post( + &mut ledger, + d(1, 1), + "Prepaid insurance", + &[ + (prepaid, Side::Debit, "1200.00"), + (bank, Side::Credit, "1200.00"), + ], + ); + // 4. A draft dated before the audit, never posted: the draft defect. + let draft = ledger.create_entry(d(2, 10), "Stamps"); + ledger + .add_line(draft, supplies, Side::Debit, usd("5.00")) + .unwrap(); + ledger + .add_line(draft, bank, Side::Credit, usd("5.00")) + .unwrap(); + + ledger + .register_schedule(NewSchedule { + description: "Prepaid insurance".into(), + kind: ScheduleKind::PrepaidExpense, + total: usd("1200.00"), + start: d(1, 1), + months: 12, + balance_account: prepaid, + pl_account: insurance, + convention: ScheduleConvention::MonthlyEven, + }) + .unwrap(); + + let _ = other_income; + ledger +} + +/// By-hand worksheet: `plan/golden-worksheets/audit-workpaper.md`. +#[test] +fn golden_audit_workpaper() { + let ledger = audit_company(); + let settings = Settings::default(); + // Entries 1 and 3 carry receipts; entry 2 is the evidence defect. + let receipts: BTreeSet = [1, 3].into_iter().collect(); + let undecided_1099 = [("Tobin Harlow Supply".to_string(), usd("900.00").amount)]; + let through = d(3, 31); + + let findings = audit::statements( + &ledger, + &AuditInput { + settings: &settings, + receipts: &receipts, + cash_flow: None, + settings_problems: &[], + undecided_1099: &undecided_1099, + }, + through, + ); + + let count = |severity: Severity| findings.iter().filter(|f| f.severity == severity).count(); + let summary = format!( + "{} failure(s), {} warning(s), {} note(s)", + count(Severity::Failure), + count(Severity::Warning), + count(Severity::Info) + ); + + // The workpaper's statements data: what `books.audit` files, minus the + // fields that are not hand-verifiable (hashes, figure head, build + // version) or not findings (company name, the static check catalogue). + let got = serde_json::json!({ + "scope": "month", + "through": through.to_string(), + "summary": summary, + "findings": serde_json::to_value(&findings).unwrap(), + }); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/audit_workpaper.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/audit-workpaper.md b/plan/golden-worksheets/audit-workpaper.md new file mode 100644 index 00000000..58f73e3f --- /dev/null +++ b/plan/golden-worksheets/audit-workpaper.md @@ -0,0 +1,198 @@ +# Golden worksheet — the audit workpaper's findings + +Programme: `ledger_core::audit::statements` (rules.md B-45), the statements +half of `books.audit`; the store's integrity checks are `ledger_store::audit` +and are not in this golden (see "Scope of this golden"). +Golden: `crates/ledger-core/tests/golden/audit_workpaper.json`. +Test: `golden_audit_workpaper` in `crates/ledger-core/tests/golden_audit.rs`. + +## Why a new fixture + +`audit_detects_each_seeded_defect` (`crates/ledger-core/tests/audit.rs`) seeds +each code in a *separate*, tiny book, and the store's +`four_defects_give_exactly_four_findings` needs raw journal surgery to file a +duplicate blob. Neither is one small book a person can audit whole. This +fixture is new and deliberately small: five defects in five different check +areas, with every other check passing. Every command is listed below. + +## Fixture + +Currency USD, `Ledger::new(Currency::USD)`, default `Settings` (unset means: +receipts required strictly above **25.00**, no required or exempt accounts, +a missing tax category is a warning). `through = 2026-03-31`. + +### Accounts, in creation order + +`Ledger::add_account` numbers accounts from 1; every account takes the next +number. + +| Id | Code | Name | Type | Tax category | +|---|---|---|---|---| +| 1 | 1000 | Bank | Asset | — | +| 2 | 1300 | Prepaid Insurance | Asset | — | +| 3 | 3000 | Capital | Equity | — | +| 4 | 4000 | Sales | Revenue | `gross_receipts` | +| 5 | 4100 | Other income | Revenue | **(none)** | +| 6 | 6000 | Supplies | Expense | `supplies` | +| 7 | 6100 | Insurance | Expense | `insurance` | + +Only 4100 is a Revenue or Expense account with no category, so the +tax-category check has exactly one thing to say. + +### Entries, in posting order + +`Ledger::create_entry` numbers entries from 1. `post` = create, add the +lines, `post_entry`. A 2,000.00 opening keeps the bank positive after the +prepaid payment. + +| Id | Date | Description | Lines | Receipt filed | Status | +|---|---|---|---|---|---| +| 1 | 2026-01-01 | Opening | Dr 1000 Bank 2,000.00; Cr 3000 Capital 2,000.00 | yes | posted | +| 2 | 2026-01-05 | Printer paper | Dr 6000 Supplies 40.00; Cr 1000 Bank 40.00 | **no** | posted | +| 3 | 2026-01-01 | Prepaid insurance | Dr 1300 Prepaid Insurance 1,200.00; Cr 1000 Bank 1,200.00 | yes | posted | +| 4 | 2026-02-10 | Stamps | Dr 6000 Supplies 5.00; Cr 1000 Bank 5.00 | — | **draft** | + +The receipts handed to the audit (`AuditInput::receipts`) are entries +**{1, 3}** only. Entry 2 is the evidence defect. The draft is never posted, +so the receipt policy does not reach it. + +### Schedule + +Registered (not posted): + +`NewSchedule { description: "Prepaid insurance", kind: PrepaidExpense, +total: 1,200.00, start: 2026-01-01, months: 12, balance_account: 2 (1300), +pl_account: 7 (6100), convention: MonthlyEven }`. + +MonthlyEven: 1,200.00 / 12 = 100.00 each, dated at each month's end, the +last month absorbing rounding (`amortization_plan`). Nothing is posted, so +every month due through 2026-03-31 is unposted: 2026-01-31, 2026-02-28, +2026-03-31 — **3 months, 300.00 in all**. + +### Facts the caller hands in + +- `cash_flow: None` — the default settings name no cash accounts, so the + server computes no statement and the check has nothing to state (its + `None` arm is a silent pass by design; an audit does say when a statement + *cannot* be made, but no statement was asked for). +- `settings_problems: &[]` — default settings name no account or role. +- `undecided_1099: &[("Tobin Harlow Supply", 900.00)]` — one vendor paid + 900.00 in a way that could need a 1099, with no decision. (This is the + package layer's reading; the core takes it as input, B-45's programme + includes the 1099 decisions.) + +## The books through 2026-03-31, by hand + +Trial balance (posted entries only; the draft is excluded): + +| Code | Debits | Credits | +|---|---|---| +| 1000 Bank | 2,000.00 − 40.00 − 1,200.00 = **760.00** | | +| 1300 Prepaid Insurance | **1,200.00** | | +| 3000 Capital | | **2,000.00** | +| 6000 Supplies | **40.00** | | + +Debits 760 + 1,200 + 40 = **2,000.00**; credits **2,000.00**. Balanced. + +Balance sheet: assets 760 + 1,200 = **1,960.00**; liabilities 0; equity = +capital 2,000.00 + current earnings (−40.00) = **1,960.00**. Balanced. + +P&L 2026-01-01 to 2026-03-31: revenue 0.00, expenses 40.00 (supplies; the +prepaid months were never posted), net income **−40.00**. Current earnings +on the balance sheet are −40.00 and no close has run, so closed net is 0.00: +−40.00 = −40.00 + 0.00. Articulated. + +## Every check, from B-45's programme + +`audit::statements` runs them in this order; `sort` then puts failures +first, and within a severity by (code, subject, message). There is **one +failure** (`evidence_missing`; a required receipt that is not there is the +books being wrong, not unfinished work). Within the warnings the order is +by code. + +| Code | Severity | Verdict | Why | +|---|---|---|---| +| `trial_balance_unbalanced` | failure | pass | debits 2,000.00 = credits 2,000.00 exactly. | +| `equation_broken` | failure | pass | assets 1,960.00 = liabilities 0.00 + equity 1,960.00. | +| `pnl_not_articulated` | failure | pass | −40.00 = −40.00 + 0.00 (see above). | +| `cash_flow_unfooted` | failure | pass | no statement asked for (`None`). | +| `control_difference` | failure | pass | no role account is assigned and no document posts a line, so the control check has no rows. | +| `unattached_control_line` | warning | pass | same: no control account, so no unattached line. | +| `document_overpaid` | warning | pass | no invoice or bill exists. | +| `close_split_drifts` | failure | pass | no period is closed. | +| `schedule_behind` | warning | **FAIL** | the 12-month plan has 3 months due and unposted, 300.00. | +| `depreciation_behind` | warning | pass | no asset is registered, so nothing is scheduled. | +| `register_differs` | failure | pass | the register holds no cost and no cost account has a balance: 0.00 = 0.00. | +| `evidence_missing` | failure | **FAIL** | entry 2 posted 40.00 of debits, strictly over 25.00, with no receipt. Entries 1 and 3 have receipts; the draft is not posted, so it is not checked here. | +| `legacy_reversal_exempt` | info | pass | no reversal exists. | +| `draft_open` | warning | **FAIL** | entry 4 is a draft dated 2026-02-10, on or before 2026-03-31. | +| `tax_category_missing` | warning | **FAIL** | account 4100 (Revenue) has no category; 4000, 6000, 6100 do. | +| `vendor_1099_undecided` | warning | **FAIL** | Tobin Harlow Supply, 900.00, no decision (handed in). | +| `settings_problem` | warning | pass | no settings problems handed in. | + +Failures 1, warnings 4, notes 0 → summary +`"1 failure(s), 4 warning(s), 0 note(s)"`. + +## The five findings, in the golden's order + +The failure first, then the four warnings by code (draft_ < schedule_ < +tax_ < vendor_). + +1. `evidence_missing`, **failure**, subject `{"kind":"entry","id":2}`: + `entry 2 (2026-01-05) has no receipt: its debits 40.00 are strictly greater than the 25.00 threshold` +2. `draft_open`, warning, subject `{"kind":"entry","id":4}`: + `draft 4 (2026-02-10) "Stamps" is not posted` +3. `schedule_behind`, warning, subject `{"kind":"schedule","id":1}` (the first + registered schedule): + `"Prepaid insurance" has 3 month(s) due and not posted, from 2026-01-31, 300.00 in all` +4. `tax_category_missing`, warning, subject `{"kind":"account","id":5}`: + `account 4100 Other income has no tax category` +5. `vendor_1099_undecided`, warning, subject `{"kind":"vendor","id":"Tobin Harlow Supply"}`: + `Tobin Harlow Supply was paid 900.00 in a way that could need a 1099 and has no decision` + +### Correction made while checking + +The first pass of this worksheet's prose said "no failures, five warnings" +even though its own table above marked `evidence_missing` a failure. The +programme returned 1 failure / 4 warnings, matching the table and +`CODES`; the prose and the first draft of the golden were the error, and +were corrected (a required receipt that is absent is B-45's "books are +wrong", and `CODES` is where the programme keeps that severity). + +## Scope of this golden: what is in it, and what is left out + +The workpaper `books.audit` files (`ledger-mcp/src/tools/audit.rs`) is + +``` +company, title, scope, through, through_seq, program_version, +tb_hash, findings_sha256, summary, findings, checks +``` + +This golden holds the part the core programme states and a person can check +by hand: + +``` +scope, through, summary, findings +``` + +Left out, and why: + +- `company` — the settings' company name (`"Unnamed company"` by default), + pure but not a finding, and it would pin the settings' default; +- `through_seq` (the figure head) and `tb_hash`/`findings_sha256` — + deterministic, but they are journal seqs and SHA-256 digests, not figures + anyone can work out from B-45; a golden that pins a hash cannot be + hand-verified, so it does not belong here. The head is already covered by + the store's `a_run_is_idempotent_on_the_figure_head`; +- `program_version` — `concat!("audit-", CARGO_PKG_VERSION, "-1")` changes + with every release, so it is a property of the build, not of the books; +- `checks` — the programme's static catalogue (`CODES`: code, severity, + predicate); it has no books in it and `audit_detects_each_seeded_defect` + already pins that every row has a seed. The worksheet's table above + records the catalogue's verdict on these books instead. +- the store's integrity findings (`ledger_store::audit`) — a golden of those + needs a store directory, JSON journal and blobs to be hand-checked for + content that is by construction "nothing was tampered with"; the store's + `integrity_detects_each_seeded_defect` and + `books_the_version_before_p8_wrote_wait_for_the_upgrade` already pin the + clean case. The statements half is where the accounting figures are. From 5e526d1cd94afb5021093d2b93271d881222df94 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 11:59:13 -0700 Subject: [PATCH 2/2] test(core): the audit golden derives its 1099 finding from a posted payment The audit golden used to hand the vendor and amount to the audit; the ledger held no payment to that vendor. Now the fixture adds the vendor (Ledger::add_vendor, which makes its party) and posts entry 5, 900.00 of supplies paid from the bank on 2026-03-10 with a receipt, 1099 decision left undecided. The audit's 1099 input is built as ledger-mcp's audit_findings builds it: ledger_package::vendor_1099_report(year 2026, as of 2026-03-31), keeping the vendors with decision_needed. Because the books name the bank as their cash account, the cash-flow footing is tried the same way instead of handed in as None. Recomputed by hand (worksheet): opening raised to 3,000.00 so the bank stays positive (860.00 = 3,000 - 40 - 1,200 - 900); trial balance 3,000.00 = 3,000.00; assets 2,060.00 = equity 3,000 - 940; net income -940.00 = current earnings; cash flow operating -2,140.00 + financing 3,000.00 = +860.00 = change in cash. The vendor's 900.00 is under the 2026 threshold of 2,000.00 (B-19, status under_threshold), yet decision_needed is has_undecided && hi(U) > 0, so the code flags it; the worksheet records that this is the code's rule, which B-19 does not state separately. The payment debits Supplies (already categorized) and is entry 5 so the draft stays entry 4: no other finding changes, and the golden JSON is unchanged. The worksheet no longer says B-45 gives the severities or the checks' list: B-45 asks for a deterministic, fixed-order list; the severities are the CODES table's, the code's own CONVENTION, which this golden pins. It also lists which checks the golden leaves silent and that tests/audit.rs seeds each one. Co-Authored-By: Claude Sonnet 5.5 --- crates/ledger-core/tests/golden_audit.rs | 121 ++++++++++-- plan/golden-worksheets/audit-workpaper.md | 213 +++++++++++++++++----- 2 files changed, 274 insertions(+), 60 deletions(-) diff --git a/crates/ledger-core/tests/golden_audit.rs b/crates/ledger-core/tests/golden_audit.rs index 7051c374..dd075bc2 100644 --- a/crates/ledger-core/tests/golden_audit.rs +++ b/crates/ledger-core/tests/golden_audit.rs @@ -2,13 +2,15 @@ //! (rules.md B-45; plan/golden-worksheets/audit-workpaper.md). //! //! The fixture is a bank, prepaid insurance, capital, two revenue and two -//! expense accounts, and a 12-month prepaid plan. It seeds five defects in -//! five check areas (`draft_open`, `evidence_missing`, `schedule_behind`, -//! `tax_category_missing`, `vendor_1099_undecided`) and leaves every other -//! check silent: the trial balance balances, the equation holds, the P&L -//! articulates, no control account or document exists, no close has run, no +//! expense accounts, a 12-month prepaid plan, and a vendor paid from the +//! bank. It seeds five defects in five check areas (`draft_open`, +//! `evidence_missing`, `schedule_behind`, `tax_category_missing`, +//! `vendor_1099_undecided`) and leaves every other check silent: the trial +//! balance balances, the equation holds, the P&L articulates, the cash +//! flow foots, no control account or document exists, no close has run, no //! asset is registered, and the entries the receipt policy reaches have -//! their receipts. +//! their receipts. The `vendor_1099_undecided` input is derived from the +//! books the way `ledger-mcp`'s `audit_findings` does it, not handed in. //! //! The golden holds the workpaper's statements data — scope, through, //! summary and findings — not the store's hashes, the figure head or the @@ -16,11 +18,12 @@ use std::collections::BTreeSet; -use chrono::NaiveDate; +use chrono::{Datelike, NaiveDate}; use ledger_core::audit::{self, AuditInput, Severity}; -use ledger_core::settings::Settings; +use ledger_core::settings::{Company, Settings}; use ledger_core::{ AccountType, Currency, Ledger, Money, NewSchedule, ScheduleConvention, ScheduleKind, Side, + Status1099, }; use rust_decimal::Decimal; @@ -48,7 +51,8 @@ fn post( } /// The worksheet's fixture: accounts in creation order (ids 1–7), then the -/// opening, the unreceipted purchase, the prepaid payment, and a draft. +/// opening, the unreceipted purchase, the prepaid payment, a draft, and the +/// vendor's payment (entry 5, after the draft so the draft stays entry 4). fn audit_company() -> Ledger { let mut ledger = Ledger::new(Currency::USD); let bank = ledger @@ -84,14 +88,14 @@ fn audit_company() -> Ledger { .unwrap(); } - // 1. Opening: 2,000.00 of capital into the bank. + // 1. Opening: 3,000.00 of capital into the bank. post( &mut ledger, d(1, 1), "Opening", &[ - (bank, Side::Debit, "2000.00"), - (capital, Side::Credit, "2000.00"), + (bank, Side::Debit, "3000.00"), + (capital, Side::Credit, "3000.00"), ], ); // 2. Printer paper, 40.00, no receipt: the evidence defect. @@ -124,6 +128,22 @@ fn audit_company() -> Ledger { .add_line(draft, bank, Side::Credit, usd("5.00")) .unwrap(); + // 5. 900.00 of supplies paid from the bank to a vendor (a party of its + // own), with its receipt; the vendor's 1099 decision is left + // undecided: the 1099 defect. + let vendor = ledger.add_vendor("Tobin Harlow Supply").unwrap(); + assert!(ledger.party_of_vendor(vendor).is_some()); + let paid = post( + &mut ledger, + d(3, 10), + "Supplies from Tobin Harlow", + &[ + (supplies, Side::Debit, "900.00"), + (bank, Side::Credit, "900.00"), + ], + ); + ledger.set_entry_vendor(paid, Some(vendor)).unwrap(); + ledger .register_schedule(NewSchedule { description: "Prepaid insurance".into(), @@ -146,17 +166,86 @@ fn audit_company() -> Ledger { fn golden_audit_workpaper() { let ledger = audit_company(); let settings = Settings::default(); - // Entries 1 and 3 carry receipts; entry 2 is the evidence defect. - let receipts: BTreeSet = [1, 3].into_iter().collect(); - let undecided_1099 = [("Tobin Harlow Supply".to_string(), usd("900.00").amount)]; + // Entries 1, 3 and 5 carry receipts; entry 2 is the evidence defect. + let receipts: BTreeSet = [1, 3, 5].into_iter().collect(); let through = d(3, 31); + // The books name the bank (1000) as their one cash account + // (`books.cash_accounts`); the package reads the roles from there. + let company = Company::default(); + let cash_accounts = vec!["1000".to_string()]; + let package = ledger_package::Settings { + company: &company, + cash_accounts: &cash_accounts, + financing_accounts: &[], + investing_accounts: &[], + }; + + // The statement of cash flows, from the first entry through `through`, + // with the roles: as `ledger-mcp`'s `audit_findings` tries it. + let first = ledger + .posted_entries() + .filter(|e| e.date() <= through) + .map(|e| e.date()) + .min() + .expect("the fixture posts entries"); + let statement = ledger_package::cash_flow_for( + &ledger, + &package, + Some(&first.to_string()), + Some(&through.to_string()), + &[], + &[], + &[], + through, + ); + let cash = Some(statement.clone().map(|s| s.reconciles)); + + // Vendors paid in a way that could need a 1099 and undecided: the same + // derivation as `ledger-mcp`'s `audit_findings`, from the ledger. + let report = + ledger_package::vendor_1099_report(&ledger, &package, through.year(), None, Some(through)); + let undecided_1099: Vec<(String, Decimal)> = report + .as_ref() + .map(|r| { + r.vendors + .iter() + .filter(|v| v.decision_needed) + .map(|v| (v.name.clone(), v.paid)) + .collect() + }) + .unwrap_or_default(); + + // The worksheet's hand values for the two derived inputs (see "The + // vendor's 1099 and the cash flow, by hand"): the statement foots at + // +860.00 (operating -2,140.00, financing +3,000.00); the vendor's + // report is 900.00 of undecided payments against the 2026 threshold + // of 2,000.00, so under threshold, yet a decision is asked for. + let statement = statement.unwrap(); + assert_eq!(statement.cash_from_operations, usd("-2140.00").amount); + assert_eq!(statement.cash_from_investing, Decimal::ZERO); + assert_eq!(statement.cash_from_financing, usd("3000.00").amount); + assert_eq!(statement.cash_beginning, Decimal::ZERO); + assert_eq!(statement.cash_ending, usd("860.00").amount); + let footing = cash.as_ref().unwrap().as_ref().unwrap(); + assert_eq!(footing.sections_sum, usd("860.00").amount); + assert_eq!(footing.net_change, usd("860.00").amount); + assert_eq!(footing.difference, Decimal::ZERO); + let report = report.unwrap(); + assert_eq!(report.threshold, usd("2000.00").amount); + assert_eq!(report.vendors.len(), 1); + assert_eq!(report.vendors[0].status, Status1099::UnderThreshold); + assert_eq!( + undecided_1099, + [("Tobin Harlow Supply".to_string(), usd("900.00").amount)] + ); + let findings = audit::statements( &ledger, &AuditInput { settings: &settings, receipts: &receipts, - cash_flow: None, + cash_flow: cash.as_ref(), settings_problems: &[], undecided_1099: &undecided_1099, }, diff --git a/plan/golden-worksheets/audit-workpaper.md b/plan/golden-worksheets/audit-workpaper.md index 58f73e3f..9ac9d000 100644 --- a/plan/golden-worksheets/audit-workpaper.md +++ b/plan/golden-worksheets/audit-workpaper.md @@ -2,7 +2,12 @@ Programme: `ledger_core::audit::statements` (rules.md B-45), the statements half of `books.audit`; the store's integrity checks are `ledger_store::audit` -and are not in this golden (see "Scope of this golden"). +and are not in this golden (see "Scope of this golden"). B-45 (invariant 1) +asks only that the findings be a deterministic list in a fixed order and +that a check which cannot be made be a finding; it neither lists the checks +nor states severities. The checks, their order and their severities are the +code's: `audit::statements` and the `CODES` table in +`crates/ledger-core/src/audit.rs` (see "Where the severities come from"). Golden: `crates/ledger-core/tests/golden/audit_workpaper.json`. Test: `golden_audit_workpaper` in `crates/ledger-core/tests/golden_audit.rs`. @@ -13,13 +18,18 @@ each code in a *separate*, tiny book, and the store's `four_defects_give_exactly_four_findings` needs raw journal surgery to file a duplicate blob. Neither is one small book a person can audit whole. This fixture is new and deliberately small: five defects in five different check -areas, with every other check passing. Every command is listed below. +areas, with every other check passing. Every command is listed below. The +1099 finding is derived from a payment posted in the books, through the same +derivation `books.audit` uses, not handed in. ## Fixture -Currency USD, `Ledger::new(Currency::USD)`, default `Settings` (unset means: -receipts required strictly above **25.00**, no required or exempt accounts, -a missing tax category is a warning). `through = 2026-03-31`. +Currency USD, `Ledger::new(Currency::USD)`, default `Settings` for the audit +(unset means: receipts required strictly above **25.00**, no required or +exempt accounts, a missing tax category is a warning). The books name the +bank, **1000**, as their one cash account (`books.cash_accounts`; no +financing or investing accounts), which the 1099 report and the cash-flow +statement need. `through = 2026-03-31`. ### Accounts, in creation order @@ -42,19 +52,29 @@ tax-category check has exactly one thing to say. ### Entries, in posting order `Ledger::create_entry` numbers entries from 1. `post` = create, add the -lines, `post_entry`. A 2,000.00 opening keeps the bank positive after the -prepaid payment. +lines, `post_entry`. A 3,000.00 opening keeps the bank positive after the +prepaid payment and the vendor's payment (2,000.00 would leave it at +−140.00). | Id | Date | Description | Lines | Receipt filed | Status | |---|---|---|---|---|---| -| 1 | 2026-01-01 | Opening | Dr 1000 Bank 2,000.00; Cr 3000 Capital 2,000.00 | yes | posted | +| 1 | 2026-01-01 | Opening | Dr 1000 Bank 3,000.00; Cr 3000 Capital 3,000.00 | yes | posted | | 2 | 2026-01-05 | Printer paper | Dr 6000 Supplies 40.00; Cr 1000 Bank 40.00 | **no** | posted | | 3 | 2026-01-01 | Prepaid insurance | Dr 1300 Prepaid Insurance 1,200.00; Cr 1000 Bank 1,200.00 | yes | posted | | 4 | 2026-02-10 | Stamps | Dr 6000 Supplies 5.00; Cr 1000 Bank 5.00 | — | **draft** | +| 5 | 2026-03-10 | Supplies from Tobin Harlow | Dr 6000 Supplies 900.00; Cr 1000 Bank 900.00; vendor **Tobin Harlow Supply** | yes | posted | + +The vendor is added with `Ledger::add_vendor("Tobin Harlow Supply")`, which +also makes it a party of its own (`party_of_vendor` is `Some`), and entry 5 +is tagged with it (`set_entry_vendor`). Its 1099 decision is never set, so it +reads *undetermined*. The payment is created after the draft so the draft +stays entry 4. It debits Supplies (6000), which already has a tax category, +so no other finding changes. The receipts handed to the audit (`AuditInput::receipts`) are entries -**{1, 3}** only. Entry 2 is the evidence defect. The draft is never posted, -so the receipt policy does not reach it. +**{1, 3, 5}**. Entry 2 is the evidence defect. The draft is never posted, +so the receipt policy does not reach it. Entry 5 debits 900.00, over the +25.00 threshold, so it needs its receipt, and has one. ### Schedule @@ -71,15 +91,23 @@ every month due through 2026-03-31 is unposted: 2026-01-31, 2026-02-28, ### Facts the caller hands in -- `cash_flow: None` — the default settings name no cash accounts, so the - server computes no statement and the check has nothing to state (its - `None` arm is a silent pass by design; an audit does say when a statement - *cannot* be made, but no statement was asked for). -- `settings_problems: &[]` — default settings name no account or role. -- `undecided_1099: &[("Tobin Harlow Supply", 900.00)]` — one vendor paid - 900.00 in a way that could need a 1099, with no decision. (This is the - package layer's reading; the core takes it as input, B-45's programme - includes the 1099 decisions.) +Two of the inputs are derived from the ledger exactly as +`ledger-mcp/src/tools/audit.rs` (`audit_findings`) derives them, with +`ledger_package` (a dev-dependency of the core), and nothing in them is +typed in: + +- `cash_flow` — because the books name cash accounts, the server tries the + statement of cash flows from the first entry (2026-01-01) through the + date and hands in its footing: `ledger_package::cash_flow_for(...)` + `.map(|s| s.reconciles)`. By hand, see "The vendor's 1099 and the cash + flow, by hand". +- `undecided_1099` — `ledger_package::vendor_1099_report(ledger, settings, + 2026, None, Some(through))`, keeping each vendor line with + `decision_needed` as `(name, paid)`. By hand, see below. +- `settings_problems: &[]` — the settings name an account (1000) that the + chart has, so there is no problem to report; the test hands in none (the + real caller computes `settings_problems(ledger, &cfg)`, which needs + `ledger-mcp`'s config, and this golden leaves that check silent). ## The books through 2026-03-31, by hand @@ -87,35 +115,95 @@ Trial balance (posted entries only; the draft is excluded): | Code | Debits | Credits | |---|---|---| -| 1000 Bank | 2,000.00 − 40.00 − 1,200.00 = **760.00** | | +| 1000 Bank | 3,000.00 − 40.00 − 1,200.00 − 900.00 = **860.00** | | | 1300 Prepaid Insurance | **1,200.00** | | -| 3000 Capital | | **2,000.00** | -| 6000 Supplies | **40.00** | | +| 3000 Capital | | **3,000.00** | +| 6000 Supplies | 40.00 + 900.00 = **940.00** | | -Debits 760 + 1,200 + 40 = **2,000.00**; credits **2,000.00**. Balanced. +Debits 860 + 1,200 + 940 = **3,000.00**; credits **3,000.00**. Balanced. -Balance sheet: assets 760 + 1,200 = **1,960.00**; liabilities 0; equity = -capital 2,000.00 + current earnings (−40.00) = **1,960.00**. Balanced. +Balance sheet: assets 860 + 1,200 = **2,060.00**; liabilities 0; equity = +capital 3,000.00 + current earnings (−940.00) = **2,060.00**. Balanced. -P&L 2026-01-01 to 2026-03-31: revenue 0.00, expenses 40.00 (supplies; the -prepaid months were never posted), net income **−40.00**. Current earnings -on the balance sheet are −40.00 and no close has run, so closed net is 0.00: -−40.00 = −40.00 + 0.00. Articulated. +P&L 2026-01-01 to 2026-03-31: revenue 0.00, expenses 940.00 (supplies 40.00 +and 900.00; the prepaid months were never posted), net income **−940.00**. +Current earnings on the balance sheet are −940.00 and no close has run, so +closed net is 0.00: −940.00 = −940.00 + 0.00. Articulated. -## Every check, from B-45's programme +What the earlier version of this fixture (2,000.00 opening, no payment to +a vendor) had: bank 760.00, equity 1,960.00, expenses 40.00. The only +changes are the opening (+1,000.00 of capital and bank), the vendor's +payment (−900.00 bank, +900.00 supplies), and the figures that follow from +them; no finding's text or subject changes. + +## The vendor's 1099 and the cash flow, by hand + +**The 1099 report** (`vendor_1099_report`, rules.md B-19), year 2026, +as of 2026-03-31, cash account 1000: + +- Threshold: B-19 gives $2,000 for payments made after Dec. 31, 2025 (the + default for 2026), so **T = 2,000.00**. +- Entry 5 is dated 2026-03-10, in 2026 and on or before 2026-03-31, posted + and tagged with the vendor, so it counts toward it. Entries 2 and 3 also + pay money out of the bank but carry no vendor tag: they are not the + vendor's (they only show in the report's untagged note). +- Bucket (B-19's table): its credit line is the cash account 1000 with no + payment method recorded, so it is a cash account line: **counts**, + presumed check, ACH or transfer (CHOICE D-40). The debit to Supplies is + an expense, so "no payment line". Counts = 900.00; card = 0; unknown = 0. +- The vendor's decision is the default, *undetermined*, so the 900.00 goes + to U (payments made while undetermined), none to F (decided for a + 1099) and none to *not reportable*. +- Status (B-19): anything in U is *undetermined* when hi(F) + max(hi(U), 0) + ≥ T, else *under threshold*. Here 0 + 900.00 = 900.00 < 2,000.00, so the + status is **under_threshold**. +- `paid` = F + U = **900.00**. + +**Is it flagged?** The audit takes each vendor line with `decision_needed`. +`decision_needed` is `has_undecided && hi(U) > 0` (`form1099.rs`): any +undecided payment above zero, **not** compared with the threshold. Here the +vendor has undecided payments and hi(U) = 900.00 > 0, so `decision_needed` +is true and the vendor is handed to the audit as `("Tobin Harlow Supply", +900.00)`, one `vendor_1099_undecided` finding. So the finding is raised by +the code's rule although the status is under_threshold. B-19's status +sentence puts "(a decision is needed)" on the at-or-over-threshold case and +does not define the `decision_needed` flag separately, so this golden pins +the **code's** rule here, and that is a convention of the caller, not a +statement of B-19. (An earlier worry that the audit's message, "could need +a 1099", over-reports a vendor under the threshold is a question for the +rule, not for this golden.) + +**The cash flow** (rules.md B-14, direct method; accounts 1000 is the only +cash account, none financing or investing), 2026-01-01 to 2026-03-31: + +| Entry | Cash moved | Section | Why | +|---|---|---|---| +| 1 Opening | +3,000.00 | financing | capital contributed by the owner (equity) | +| 2 Printer paper | −40.00 | operating | paid to suppliers | +| 3 Prepaid insurance | −1,200.00 | operating | paid to suppliers (a prepaid asset is operating when no investing account lists it) | +| 5 Tobin Harlow | −900.00 | operating | paid to suppliers | + +Operating **−2,140.00**, investing 0.00, financing **+3,000.00**; the +sections sum to **+860.00**. Cash at the start 0.00, at the end 860.00 (the +bank's balance above), so the change is **+860.00**: the sections foot +(difference 0.00, B-14 rule 9) and `cash_flow_unfooted` stays silent. The +draft moved no cash. The test asserts these hand values on the statement +and the footing before it asserts the findings. + +## Every check, in the order `audit::statements` runs them `audit::statements` runs them in this order; `sort` then puts failures -first, and within a severity by (code, subject, message). There is **one -failure** (`evidence_missing`; a required receipt that is not there is the -books being wrong, not unfinished work). Within the warnings the order is -by code. +first, and within a severity by (code, subject, message). The Severity +column is the `CODES` table's (see "Where the severities come from"). There +is **one failure** (`evidence_missing`: `CODES` files a required receipt +that is not there as a failure). Within the warnings the order is by code. | Code | Severity | Verdict | Why | |---|---|---|---| -| `trial_balance_unbalanced` | failure | pass | debits 2,000.00 = credits 2,000.00 exactly. | -| `equation_broken` | failure | pass | assets 1,960.00 = liabilities 0.00 + equity 1,960.00. | -| `pnl_not_articulated` | failure | pass | −40.00 = −40.00 + 0.00 (see above). | -| `cash_flow_unfooted` | failure | pass | no statement asked for (`None`). | +| `trial_balance_unbalanced` | failure | pass | debits 3,000.00 = credits 3,000.00 exactly. | +| `equation_broken` | failure | pass | assets 2,060.00 = liabilities 0.00 + equity 2,060.00. | +| `pnl_not_articulated` | failure | pass | −940.00 = −940.00 + 0.00 (see above). | +| `cash_flow_unfooted` | failure | pass | the statement can be made and its sections sum to +860.00 = the cash change (see above). | | `control_difference` | failure | pass | no role account is assigned and no document posts a line, so the control check has no rows. | | `unattached_control_line` | warning | pass | same: no control account, so no unattached line. | | `document_overpaid` | warning | pass | no invoice or bill exists. | @@ -123,11 +211,11 @@ by code. | `schedule_behind` | warning | **FAIL** | the 12-month plan has 3 months due and unposted, 300.00. | | `depreciation_behind` | warning | pass | no asset is registered, so nothing is scheduled. | | `register_differs` | failure | pass | the register holds no cost and no cost account has a balance: 0.00 = 0.00. | -| `evidence_missing` | failure | **FAIL** | entry 2 posted 40.00 of debits, strictly over 25.00, with no receipt. Entries 1 and 3 have receipts; the draft is not posted, so it is not checked here. | +| `evidence_missing` | failure | **FAIL** | entry 2 posted 40.00 of debits, strictly over 25.00, with no receipt. Entries 1, 3 and 5 have receipts; the draft is not posted, so it is not checked here. | | `legacy_reversal_exempt` | info | pass | no reversal exists. | | `draft_open` | warning | **FAIL** | entry 4 is a draft dated 2026-02-10, on or before 2026-03-31. | | `tax_category_missing` | warning | **FAIL** | account 4100 (Revenue) has no category; 4000, 6000, 6100 do. | -| `vendor_1099_undecided` | warning | **FAIL** | Tobin Harlow Supply, 900.00, no decision (handed in). | +| `vendor_1099_undecided` | warning | **FAIL** | Tobin Harlow Supply: 900.00 paid by entry 5, undetermined, so `decision_needed` (see above). | | `settings_problem` | warning | pass | no settings problems handed in. | Failures 1, warnings 4, notes 0 → summary @@ -150,14 +238,51 @@ tax_ < vendor_). 5. `vendor_1099_undecided`, warning, subject `{"kind":"vendor","id":"Tobin Harlow Supply"}`: `Tobin Harlow Supply was paid 900.00 in a way that could need a 1099 and has no decision` +### Where the severities come from + +B-45 states no severities (see the top of this worksheet). Each code's +severity is the `CODES` table's in `crates/ledger-core/src/audit.rs`, and +the meaning is the `Severity` doc comment there: a **failure** is "the books are wrong or cannot be shown +right: an audit with one is not clean"; a **warning** is "unfinished work or +a policy not met"; an **info** is "listed so the workpaper is whole; not a +defect". That table is the code's own **CONVENTION**, not an authority +(nothing in the IRS or FASB sources assigns a severity); this golden pins +it, so a change of a code's severity fails the golden and is a decision to +make on purpose. + ### Correction made while checking The first pass of this worksheet's prose said "no failures, five warnings" even though its own table above marked `evidence_missing` a failure. The -programme returned 1 failure / 4 warnings, matching the table and -`CODES`; the prose and the first draft of the golden were the error, and -were corrected (a required receipt that is absent is B-45's "books are -wrong", and `CODES` is where the programme keeps that severity). +programme returned 1 failure / 4 warnings, matching the table and `CODES`; +the prose and the first draft of the golden were the error, and were +corrected. (The correction at the time said the severity came from B-45; +it does not: it is `CODES`'s, as above.) + +### Checks this golden leaves silent by construction + +Twelve of the seventeen codes are silent here. Eight are silent because the +fixture has nothing for them to check, and only a book built for it makes +them speak: + +- the control accounts: `control_difference`, `unattached_control_line`, + `document_overpaid` (no role account, no invoice or bill); +- closes: `close_split_drifts` (no period is closed); +- assets: `depreciation_behind`, `register_differs` (no asset is + registered); +- reversals: `legacy_reversal_exempt` (no reversal exists); +- the settings: `settings_problem` (the test hands in no problem). + +The other four, `trial_balance_unbalanced`, `equation_broken`, +`pnl_not_articulated` and `cash_flow_unfooted`, are silent because the books +are right (shown by hand above); a posted ledger cannot be made wrong +through the engine's commands, so the golden can only show them passing. + +`audit_detects_each_seeded_defect` (`crates/ledger-core/tests/audit.rs`) +seeds every row of `CODES` separately, a book or a figure of its own for +each (the four statement checks by handing the check function a wrong +figure), and checks exactly that finding comes out. So each code that is +silent here is pinned to fire elsewhere. ## Scope of this golden: what is in it, and what is left out