fix(mcp): a package's decisions are journaled only once it will be made

books.export_cpa_package journals the reason given to export without an audit
or with open drafts as a decision, and did so before it knew the package
would be made: a later refusal (open drafts after an audit reason) or a close
that stopped the build left a decision on record for a package that was never
made.

Every refusal is now decided first, then the decisions are journaled. They stay
ahead of the build for a reason the code states: the package counts them (its
README and every report footer carry the journal's event count at the build),
so moving them after would change the package's contents. A close is looked
for once, immediately before the first decision is journaled; from there to the
hand-over nothing looks at the stop (the build is entered without the close's
cancel scope), so a decision on record is a package handed over. What is
journaled and what the package holds are unchanged.

Test: an open draft that refuses the package after an audit reason journals no
decision (mutation-checked: journaling before the refusal fails it).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 19:57:16 -07:00
1 parent 6d7044c284
commit d05d1ed7ec
2 files changed
+84 -33

No files matched your search

+17
View File
@@ -323,6 +323,23 @@ fn cpa_package_refuses_without_a_counted_audit_unless_a_reason_is_given() {
before["decisions"].as_array().unwrap().len() + 1
);
assert!(after.to_string().contains("without a counted audit"));
// A refusal that comes after the reason (an open draft, with no reason of
// its own) journals nothing: no decision stands for a package that was
// never made.
let open = post(&mut w, "2026-03-25", "Still open", "3.00");
let held_off = export(
&mut w,
json!({"without_audit_reason": "the CPA asked for it early"}),
);
let err: Value = serde_json::from_str(&answer_of(&held_off).unwrap_err()).unwrap();
assert_eq!(err["code"], "open_drafts", "{err}");
let still = answer_of(&w.call("books.decisions", json!({}))).unwrap();
assert_eq!(
still["decisions"].as_array().unwrap().len(),
after["decisions"].as_array().unwrap().len(),
"a decision was journaled for a package that was refused"
);
confirm_post(&mut w, open);
// An audit alone does not open it; nor one attested below the bar.
answer_of(&w.call(
"books.create_period",
+67 -33
View File
@@ -1142,7 +1142,21 @@ impl BooksServer {
/// 3. A locked vault or a missing TIN never fails the package: the
/// row says why (vault_tin), the README lists it, and so does
/// tin_gaps (members, and 1099 payees that are reportable).
/// 4. Read-only: nothing is journaled.
/// 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
/// 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
/// counts the decisions. A close that has begun is looked for once,
/// immediately before the first decision is journaled: it refuses
/// 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.
/// ```
#[tool(
name = "books.export_cpa_package",
@@ -1162,6 +1176,9 @@ impl BooksServer {
let as_of = self.as_of(p.as_of.as_deref())?;
self.backups.hold("books.export_cpa_package");
let mut store = self.lock()?;
// The decisions an override records, journaled below only once every
// refusal has been decided (INVARIANT 4).
let mut decisions: Vec<ledger_core::conventions::NewDecision> = Vec::new();
// A package no counted audit stands behind is not final either
// (plan P8, D-9): on books that declared `audits`, refused under
// policy.cpa_package_requires_audit = refuse unless a reason is
@@ -1191,20 +1208,18 @@ impl BooksServer {
let by = CALLER
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
store
.record_decision(&ledger_core::conventions::NewDecision {
subject: format!("CPA package as of {as_of} without a counted audit"),
decision_md: format!(
"The CPA package as of {as_of} was exported without an audit at the books' current head attested at the bar. Reason: {reason}"
),
decided_by: vec![by],
decided_on: today(),
evidence_attachment: None,
supersedes: None,
citation: None,
projects: Vec::new(),
})
.map_err(|e| e.to_string())?;
decisions.push(ledger_core::conventions::NewDecision {
subject: format!("CPA package as of {as_of} without a counted audit"),
decision_md: format!(
"The CPA package as of {as_of} was exported without an audit at the books' current head attested at the bar. Reason: {reason}"
),
decided_by: vec![by],
decided_on: today(),
evidence_attachment: None,
supersedes: None,
citation: None,
projects: Vec::new(),
});
} else {
audit_warning = Some(format!(
"No counted audit stands behind this package: {why}."
@@ -1241,23 +1256,39 @@ impl BooksServer {
let by = CALLER
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
store
.record_decision(&ledger_core::conventions::NewDecision {
subject: format!("CPA package as of {as_of} with open drafts"),
decision_md: format!(
"The CPA package as of {as_of} was exported with {} draft(s) open: {}. Reason: {}",
open_drafts.len(),
named.join("; "),
allow.reason.trim()
),
decided_by: vec![by],
decided_on: today(),
evidence_attachment: None,
supersedes: None,
citation: None,
projects: Vec::new(),
})
.map_err(|e| e.to_string())?;
decisions.push(ledger_core::conventions::NewDecision {
subject: format!("CPA package as of {as_of} with open drafts"),
decision_md: format!(
"The CPA package as of {as_of} was exported with {} draft(s) open: {}. Reason: {}",
open_drafts.len(),
named.join("; "),
allow.reason.trim()
),
decided_by: vec![by],
decided_on: today(),
evidence_attachment: None,
supersedes: None,
citation: None,
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.
let journaled = !decisions.is_empty();
if journaled {
if self.store.hurried() {
return Err(crate::server::stopped_by_close());
}
for decision in &decisions {
store.record_decision(decision).map_err(|e| e.to_string())?;
}
// The test knob's seam (`--timer-hold`, `call:books.export_cpa_
// package:journaled`): a close that begins here must not stop
// the build.
self.backups.hold("books.export_cpa_package:journaled");
}
let book = book_name(&store);
// The vault, opened once and never creating a key: locked, each
@@ -1271,7 +1302,10 @@ impl BooksServer {
// (ledger_package INVARIANT 1). Every stored file it holds is read
// one at a time, and a close stops it between two (local.rs
// INVARIANT 10).
let _stop = ledger_store::cancel::scope(self.store.hurry_flag());
// Not once a decision is journaled for it (INVARIANT 4): then it runs
// to the hand-over, which local.rs INVARIANT 10 names as a call a
// close does not stop.
let _stop = (!journaled).then(|| ledger_store::cancel::scope(self.store.hurry_flag()));
let package = ledger_package::build_package(
&*store,
&package_settings(&self.settings()),