feat(drafts): the drafts tools, the preview digest on every confirm, the digest tool (P4)

Accounting reason: a confirm must act on the thing that was previewed.
Every tool with confirmed (post_entry, reverse_entry, run_schedules,
dispose_asset, reconcile, close_period, create_invoice, create_bill,
write_off_invoice, purge_tin, consolidate_duplicates, and the S6
settlement tools receive_payment and pay_bill, which gain confirmed and
preview on a clone of the ledger) returns preview (SHA-256 of the
canonical preview) and refuses a confirm whose preview is missing or
stale (stale_preview, before anything is written). A draft's digest is
its own state and the settings seq; an operation's is the tool, its
arguments as sent, the settings seq and the journal head seq, so another
session's write forces a re-preview (D-50).

New tools books.list_drafts, books.set_draft_state, books.discard_draft,
books.preview_entry; reports carry as_of_status (drafts open on or before
their end, unreconciled accounts, settings problems) and take strict; the
CPA package is refused while a draft dated in it is open unless
allow_open_drafts gives a reason, journaled as a decision;
books.digest takes since_seq (what was posted since a journal line,
backdated entries included: defect 6) and lists zero_net_accounts,
drafts_by_state and the reconciliation split. Needs attention gains
'draft awaiting receipt' beside 'draft not posted'; the drafts handbook
is rewritten. Every over-the-wire harness confirms the way an agent does.

Docs: mcp-interface, rules B-43, parity, testing (with the For-P12
paragraph), architecture, and the unit's As-built block.

Tests: drafts.rs (a_draft_waits_for_its_receipt_and_posts,
the_gate_moved_and_the_mcp_error_is_unchanged,
a_reversal_needs_no_receipt_of_its_own, discard_unblocks_the_close_over_the_wire,
report_warns_on_draft_before_as_of, cpa_package_refuses_open_draft,
stale_preview_refused, every_confirm_tool_takes_a_preview,
digest_since_seq_is_posting_time, legacy_reversed_replays_identically,
gui_and_server_gate_agree), attention_parity.rs, format_upgrade.rs.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HS8WZziDnyxunC23MUxrLp
This commit is contained in:
swaitsandClaude Fable 5.1 committed 2026-10-29 13:55:38 -06:00
1 parent 7299cf90f2
commit 6a66a2855b
49 files changed
+2526 -202

No files matched your search

+41 -46
View File
@@ -340,7 +340,7 @@ impl BooksServer {
open_world_hint = false
)
)]
async fn post_entry(
pub(super) async fn post_entry(
&self,
params: Parameters<PostEntryParams>,
) -> Result<Json<PostEntryOut>, String> {
@@ -368,20 +368,14 @@ impl BooksServer {
));
}
}
let mut receipt_required = self
.settings()
.policy
.receipt_required(store.ledger(), &entry);
// One function decides (plan P4, I-5): the engine's. A draft that
// joins a split group is covered too (anti-splitting).
let receipt_required =
ledger_core::evidence::requirement(store.ledger(), &entry, store.ledger().settings())
.is_required();
let receipts = preview_receipts(&store, p.entry_id)?;
// Anti-splitting: if this entry looks like part of a split group,
// the receipt policy applies to the group total.
let split_group = self
.settings()
.policy
let split_group = ledger_core::evidence::Policy::from_settings(store.ledger().settings())
.split_group(store.ledger(), p.entry_id, &entry);
if !split_group.is_empty() {
receipt_required = true;
}
let receipt_missing = receipt_required && receipts.is_empty();
let party_policy = self.settings().parties;
let party_id = store.ledger().entry_party(p.entry_id);
@@ -399,7 +393,8 @@ impl BooksServer {
} else {
Vec::new()
};
let out = PostEntryOut {
let mut out = PostEntryOut {
preview: None,
status: "preview".to_string(),
entry_id: p.entry_id,
date: entry.date().to_string(),
@@ -428,6 +423,26 @@ impl BooksServer {
.map(|(rule, approver)| ApprovalNeededOut { rule, approver })
.collect(),
};
// The digest of the draft as it stands (plan P4 step 4, D-50): its
// lines, party, vendor, instrument and evidence, and the settings
// seq. Another session's unrelated post does not disturb it.
let payload = serde_json::json!({
"tool": "books.post_entry",
"entry": &entry,
"party": out.party_id,
"parties": &out.parties,
"no_party_reason": &out.no_party_reason,
"vendor": store.ledger().entry_vendor(p.entry_id),
"instrument": store.ledger().entry_instrument(p.entry_id),
"evidence": out.receipts.iter().map(|r| r.attachment_id).collect::<Vec<_>>(),
"settings_seq": store.ledger().settings().last_seq(),
});
out.preview = Some(Preview::check(
"books.post_entry",
&payload,
p.confirmed.unwrap_or(false),
p.preview.as_deref(),
)?);
if !p.confirmed.unwrap_or(false) {
return Ok(Json(out));
}
@@ -480,38 +495,11 @@ impl BooksServer {
})
.to_string());
}
if receipt_missing {
let split_note = if split_group.is_empty() {
String::new()
} else {
format!(
" Anti-splitting: this looks like part of a split group with entries {:?}; the policy applies to the group total.",
split_group
)
};
return Err(ReceiptRequiredError {
code: "receipt_required".to_string(),
message: format!(
"entry {} moves {} in debits ({}); attach a receipt with books.attach_receipt before posting.{}",
p.entry_id,
money(total),
self.settings().policy.describe(),
split_note
),
entry_id: Some(p.entry_id),
asset_id: None,
debits: total.to_string(),
policy: self.settings().policy.describe(),
remedy_tool: "books.attach_receipt".to_string(),
remedy_hint: format!(
"attach a receipt to entry {} with books.attach_receipt, then retry books.post_entry with confirmed=true",
p.entry_id
),
split_group,
}
.to_tool_error());
}
store.post_entry(p.entry_id).map_err(|e| e.to_string())?;
// The evidence gate, in the store (plan P4, I-5): the same
// decision the preview showed, made again at the moment it posts.
store
.post_entry_checked(p.entry_id)
.map_err(|e| receipt_required_text(e, &self.settings().policy))?;
fix_derived_shares(&mut store, &self.settings(), p.entry_id)?;
let mut posted = out;
posted.status = "posted".to_string();
@@ -560,7 +548,14 @@ impl BooksServer {
(l.account, side, l.amount)
})
.collect();
let digest = self.preview_of(
&store,
"books.reverse_entry",
p.confirmed.unwrap_or(false),
p.preview.as_deref(),
)?;
let out = ReverseEntryOut {
preview: Some(digest),
status: "preview".to_string(),
reversal_id: 0, // filled in on post
reverses: p.entry_id,