From 30e5fcf6da8640b4c69ba6823c30d1c3dcf8ea27 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 20:18:39 -0700 Subject: [PATCH] fix(mcp): a CPA package the build will refuse leaves no decision on record The override decisions (exporting without an audit, with open drafts) were journaled once only the two named refusals were past, but build_package can still refuse for what the books say, such as a member whose capital account is not in the chart. That left 'was exported' on record with no package, and a new decision on every retry. When a decision is to be recorded, the build now runs once first, in memory: it is read-only (the books by shared reference, written nowhere), so it changes nothing, and it is left without the stored files and the vault, which only a failing machine can refuse. Only if it succeeds is the decision journaled and the package built for real. The spec block says what can still fail after a decision (the machine) and that the decision then stands, with the reason in the access log. Test: a package refused for an unknown capital account, with an override reason, journals nothing, on the first try and on the retry; it fails without the dry run (the journal grows). Co-Authored-By: Claude Sonnet 5.5 --- crates/ledger-cli/tests/drafts.rs | 34 ++++++++++++++ crates/ledger-mcp/src/tools/reports.rs | 61 ++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/crates/ledger-cli/tests/drafts.rs b/crates/ledger-cli/tests/drafts.rs index 438c62d0..4bc9ebf9 100644 --- a/crates/ledger-cli/tests/drafts.rs +++ b/crates/ledger-cli/tests/drafts.rs @@ -333,6 +333,40 @@ fn cpa_package_refuses_open_draft() { assert!(journal.contains("the CPA wants it before the receipt comes")); } +/// A package the build will refuse is refused before any decision is +/// journaled (tools/reports.rs INVARIANT 4): the books name a member whose +/// capital account isn't in the chart, so the build can never succeed, and +/// an override (the reason for an open draft) must not leave "was exported" +/// on record, once or on a retry, with no package. The journal does not move. +#[test] +fn a_package_the_build_refuses_journals_no_decision_even_with_a_reason() { + let settings = format!( + "{SETTINGS}[[company.members]]\nname = \"Mo\"\nshare = \"100%\"\ncapital_account = \"3999\"\n" + ); + let books = Books::with("p4-cpa-refused", "USD", Some(&settings)); + let mut w = books.session("p4"); + chart(&mut w); + draft(&mut w, "2026-03-10", "Open", "40.00"); + let mut args = json!({"as_of": "2026-03-31", "cash_accounts": ["1000"]}); + args["allow_open_drafts"] = json!({"reason": "the CPA wants it before the receipt comes"}); + let before = books.journal().len(); + for _ in 0..2 { + let why = answer_of(&w.call("books.export_cpa_package", args.clone())) + .expect_err("the build refuses an unknown capital account"); + assert!(why.contains("unknown capital account 3999"), "{why}"); + assert_eq!( + books.journal().len(), + before, + "a decision was journaled for a package that was refused" + ); + } + assert!( + !String::from_utf8(books.journal()) + .unwrap() + .contains("decision.recorded") + ); +} + /// D-50: a confirm takes the preview it was shown; without it, or after a /// changed argument or a write by another session, it is refused with /// `stale_preview` before anything is written. diff --git a/crates/ledger-mcp/src/tools/reports.rs b/crates/ledger-mcp/src/tools/reports.rs index 7850aa3d..2a3bb2f0 100644 --- a/crates/ledger-mcp/src/tools/reports.rs +++ b/crates/ledger-mcp/src/tools/reports.rs @@ -1145,8 +1145,19 @@ impl BooksServer { /// 4. Nothing is journaled but the decisions an override records (a /// reason for exporting without an audit, a reason for exporting /// with open drafts), and only once the package is certain to be - /// made. Every refusal is decided first, so none can follow a - /// decision. The decisions stay ahead of the build because the + /// made. Every refusal that can be known from the books as they + /// stand is decided first: the two named ones (the audit, the open + /// drafts) and every one the build itself can make (a member's + /// unknown capital account, a report that can't be built), by + /// running the build once, read-only and in memory, before the + /// decision is journaled (without the stored files and the vault, + /// which a machine fails at, not the books). So a package refused + /// for what the books say journals nothing, on every retry. What + /// can still fail after a decision is journaled is only a failure + /// of the machine (the disk reading a stored file, memory, the + /// hand-over); the decision is then on record, with the reason in + /// the access log, and a later export that succeeds records its + /// own. The decisions stay ahead of the build because the /// package describes the books it is made from, itself included: /// its README and every report's footer carry the journal's event /// count at the build (ledger_package::package, report_meta), which @@ -1155,8 +1166,8 @@ impl BooksServer { /// then, journaling nothing. From the journaling to the hand-over /// nothing looks at the close's stop (the build is no longer /// stoppable, local.rs INVARIANT 10), so a decision on record is a - /// package handed over, unless the build or the hand-over itself - /// fails, which a close cannot make it do. + /// package handed over, unless the machine fails, which a close + /// cannot make it do. /// ``` #[tool( name = "books.export_cpa_package", @@ -1272,11 +1283,43 @@ impl BooksServer { projects: Vec::new(), }); } - // Every refusal is behind us. The decisions go on record now, ahead - // of the build (the package counts them: INVARIANT 4), and the close - // is looked for once more, immediately before the first: past it the - // package is made whatever a close does, so no decision is on record - // for a package that was never made. + // Every refusal the build can make from the books as they stand is + // decided before a decision goes on record (INVARIANT 4): the build + // is run once, in memory, to see that it will succeed. It is + // read-only (ledger_package INVARIANT 5: it takes the books by + // shared reference and writes nowhere, and it leaves its result to + // be dropped), so it changes nothing. It leaves out what only the + // machine can fail at: the stored files (include_receipts false) and + // the vault (no TIN source: a TIN it can't give never fails a + // package, ledger_package INVARIANT 3). A close that begins during + // it stops it, before anything is journaled. + if !decisions.is_empty() { + let _stop = ledger_store::cancel::scope(self.store.hurry_flag()); + ledger_package::build_package( + &*store, + &package_settings(&self.settings()), + &ledger_package::PackageRequest { + as_of, + prepared_on: today(), + cash_accounts: &p.cash_accounts, + financing_accounts: &p.financing_accounts, + investing_accounts: &p.investing_accounts, + include_receipts: false, + tins: None, + }, + ) + .map_err(|e| { + if e == ledger_store::StoreError::Cancelled.to_string() { + crate::server::stopped_by_close() + } else { + e + } + })?; + } + // The decisions go on record now, ahead of the build (the package + // counts them: INVARIANT 4), and the close is looked for once more, + // immediately before the first: past it the package is made + // whatever a close does. let journaled = !decisions.is_empty(); if journaled { if self.store.hurried() {