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:
1 parent
6d7044c284
commit
d05d1ed7ec
2 files changed
+84
-33
No files matched your search
@@ -323,6 +323,23 @@ fn cpa_package_refuses_without_a_counted_audit_unless_a_reason_is_given() {
|
|||||||
before["decisions"].as_array().unwrap().len() + 1
|
before["decisions"].as_array().unwrap().len() + 1
|
||||||
);
|
);
|
||||||
assert!(after.to_string().contains("without a counted audit"));
|
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.
|
// An audit alone does not open it; nor one attested below the bar.
|
||||||
answer_of(&w.call(
|
answer_of(&w.call(
|
||||||
"books.create_period",
|
"books.create_period",
|
||||||
|
|||||||
@@ -1142,7 +1142,21 @@ impl BooksServer {
|
|||||||
/// 3. A locked vault or a missing TIN never fails the package: the
|
/// 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
|
/// row says why (vault_tin), the README lists it, and so does
|
||||||
/// tin_gaps (members, and 1099 payees that are reportable).
|
/// 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(
|
#[tool(
|
||||||
name = "books.export_cpa_package",
|
name = "books.export_cpa_package",
|
||||||
@@ -1162,6 +1176,9 @@ impl BooksServer {
|
|||||||
let as_of = self.as_of(p.as_of.as_deref())?;
|
let as_of = self.as_of(p.as_of.as_deref())?;
|
||||||
self.backups.hold("books.export_cpa_package");
|
self.backups.hold("books.export_cpa_package");
|
||||||
let mut store = self.lock()?;
|
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
|
// A package no counted audit stands behind is not final either
|
||||||
// (plan P8, D-9): on books that declared `audits`, refused under
|
// (plan P8, D-9): on books that declared `audits`, refused under
|
||||||
// policy.cpa_package_requires_audit = refuse unless a reason is
|
// policy.cpa_package_requires_audit = refuse unless a reason is
|
||||||
@@ -1191,20 +1208,18 @@ impl BooksServer {
|
|||||||
let by = CALLER
|
let by = CALLER
|
||||||
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
|
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
|
||||||
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
|
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
|
||||||
store
|
decisions.push(ledger_core::conventions::NewDecision {
|
||||||
.record_decision(&ledger_core::conventions::NewDecision {
|
subject: format!("CPA package as of {as_of} without a counted audit"),
|
||||||
subject: format!("CPA package as of {as_of} without a counted audit"),
|
decision_md: format!(
|
||||||
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}"
|
||||||
"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_by: vec![by],
|
decided_on: today(),
|
||||||
decided_on: today(),
|
evidence_attachment: None,
|
||||||
evidence_attachment: None,
|
supersedes: None,
|
||||||
supersedes: None,
|
citation: None,
|
||||||
citation: None,
|
projects: Vec::new(),
|
||||||
projects: Vec::new(),
|
});
|
||||||
})
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
} else {
|
} else {
|
||||||
audit_warning = Some(format!(
|
audit_warning = Some(format!(
|
||||||
"No counted audit stands behind this package: {why}."
|
"No counted audit stands behind this package: {why}."
|
||||||
@@ -1241,23 +1256,39 @@ impl BooksServer {
|
|||||||
let by = CALLER
|
let by = CALLER
|
||||||
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
|
.with(|c| c.borrow().as_ref().map(|(_, name)| name.clone()))
|
||||||
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
|
.unwrap_or_else(|| ledger_store::auth::DEFAULT_NAME.to_string());
|
||||||
store
|
decisions.push(ledger_core::conventions::NewDecision {
|
||||||
.record_decision(&ledger_core::conventions::NewDecision {
|
subject: format!("CPA package as of {as_of} with open drafts"),
|
||||||
subject: format!("CPA package as of {as_of} with open drafts"),
|
decision_md: format!(
|
||||||
decision_md: format!(
|
"The CPA package as of {as_of} was exported with {} draft(s) open: {}. Reason: {}",
|
||||||
"The CPA package as of {as_of} was exported with {} draft(s) open: {}. Reason: {}",
|
open_drafts.len(),
|
||||||
open_drafts.len(),
|
named.join("; "),
|
||||||
named.join("; "),
|
allow.reason.trim()
|
||||||
allow.reason.trim()
|
),
|
||||||
),
|
decided_by: vec![by],
|
||||||
decided_by: vec![by],
|
decided_on: today(),
|
||||||
decided_on: today(),
|
evidence_attachment: None,
|
||||||
evidence_attachment: None,
|
supersedes: None,
|
||||||
supersedes: None,
|
citation: None,
|
||||||
citation: None,
|
projects: Vec::new(),
|
||||||
projects: Vec::new(),
|
});
|
||||||
})
|
}
|
||||||
.map_err(|e| e.to_string())?;
|
// 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);
|
let book = book_name(&store);
|
||||||
// The vault, opened once and never creating a key: locked, each
|
// 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
|
// (ledger_package INVARIANT 1). Every stored file it holds is read
|
||||||
// one at a time, and a close stops it between two (local.rs
|
// one at a time, and a close stops it between two (local.rs
|
||||||
// INVARIANT 10).
|
// 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(
|
let package = ledger_package::build_package(
|
||||||
&*store,
|
&*store,
|
||||||
&package_settings(&self.settings()),
|
&package_settings(&self.settings()),
|
||||||
|
|||||||
Reference in new issue
Block a user