feat(drafts): a draft has a state and can be discarded (P4)

Accounting reason: an entry that is finished but waits on its receipt is
a different thing from one still being built, and a draft that will
never post must have a way out, or the period that holds it open can
never close (the Lean hypothesis hdrafts). JournalEntry gains
DraftState { Building, AwaitingEvidence { note } }; Ledger gains
set_draft_state (a note: no line, balance or report moves),
discard_draft (a draft only; a posted entry is never removed, B-8; ids
are never reused; what was recorded about the draft goes with it) and
posted_seq (the journal line that posted an entry, for the digest's
since_seq). Lean Step.discard with its preservation proof and three
theorems (a discard moves no figure, removes only its draft, and
unblocks a close), listed in Audit.lean.

Tests: discard_unblocks_close (ledger-core invariants).

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:17 -06:00
1 parent 75882c5e07
commit be8bcf340e
8 files changed
+347 -1

No files matched your search

+132
View File
@@ -355,6 +355,11 @@ pub struct Ledger {
/// What each entry settles (`entry.settles`, plan P6): the one record
/// a member-paid entry's "reimbursed" is derived from (IZ-18).
entry_settles: std::collections::BTreeMap<EntryId, Vec<EntryId>>,
/// The journal line that posted each posted entry (`entry.posted` or
/// `entry.reversed`, plan P4): what "posted since seq N" is read from,
/// backdated entries included. An entry the journal's own posting
/// line doesn't name (none today) has no place here.
posted_seq: std::collections::BTreeMap<EntryId, u64>,
}
impl Ledger {
@@ -411,6 +416,7 @@ impl Ledger {
conventions: crate::conventions::Conventions::default(),
annotations: std::collections::BTreeMap::new(),
entry_settles: std::collections::BTreeMap::new(),
posted_seq: std::collections::BTreeMap::new(),
}
}
@@ -826,6 +832,7 @@ impl Ledger {
("conventions", self.conventions == other.conventions),
("annotations", self.annotations == other.annotations),
("entry_settles", self.entry_settles == other.entry_settles),
("posted_seq", self.posted_seq == other.posted_seq),
]
.into_iter()
.filter(|(_, same)| !same)
@@ -1977,6 +1984,131 @@ impl Ledger {
Ok(())
}
/// Set where a draft stands: still being built, or awaiting its
/// evidence (plan P4, rules.md B-43). A state is a note on the draft
/// and moves no figure; the draft's lines are what they were.
///
/// ```text
/// GOAL: let a draft say it is finished and waiting on a document,
/// so it is neither lost nor mistaken for work in progress.
/// SOURCE: docs/spec/rules.md B-43 (DECISION D-47: a draft waits on
/// evidence; the evidence is checked when it posts); IRS
/// Pub. 583 "supporting documents".
/// INVARIANTS:
/// 1. Only a draft has a state: a posted entry is refused
/// (PostedEntryImmutable), an unknown one too (UnknownEntry).
/// 2. The note passes parties::check_text (no full TIN, no control
/// characters, at most 500 characters).
/// 3. No line, balance or report moves; a refusal changes nothing.
/// ```
pub fn set_draft_state(
&mut self,
entry: EntryId,
state: crate::DraftState,
) -> Result<(), LedgerError> {
if let crate::DraftState::AwaitingEvidence { note } = &state {
crate::parties::check_text("the note", note).map_err(LedgerError::InvalidDraft)?;
}
self.draft_mut(entry)?.set_draft_state(state);
Ok(())
}
/// [`Self::set_draft_state`] as journaled, for replay (rules.md
/// B-28): the state is restored on a draft that exists; a state
/// naming an entry since posted or discarded is refused like the
/// command, which journal order makes impossible.
#[doc(hidden)]
pub fn replay_set_draft_state(
&mut self,
entry: EntryId,
state: crate::DraftState,
) -> Result<(), LedgerError> {
self.draft_mut(entry)?.set_draft_state(state);
Ok(())
}
/// Throw a draft away, with the reason (plan P4, rules.md B-43): the
/// only way out of an open draft that will never post, and so the way
/// a period it holds open can close. The draft was never in the books,
/// so no balance moves; the id is spent (ids are never reused); the
/// journal still holds its lines and why it went.
///
/// ```text
/// GOAL: remove a draft that will not post, leaving the books and
/// every id as they were.
/// SOURCE: docs/spec/rules.md B-43; B-8 (a posted entry is never
/// removed: only a draft is); Lean Step.discard.
/// INVARIANTS:
/// 1. Only a draft is discarded: a posted entry is refused
/// (EntryAlreadyPosted), an unknown one too (UnknownEntry).
/// 2. The reason is not empty and passes parties::check_text.
/// 3. Posted entries, their order, every balance and every id
/// allocator are as they were; what was recorded about the draft
/// itself (its vendor, instrument, parties, settlements) goes
/// with it.
/// SCENARIOS:
/// 1. IF a period holds the draft open: after it, the period can
/// close (the Lean hypothesis hdrafts).
/// ```
pub fn discard_draft(&mut self, entry: EntryId, reason: &str) -> Result<(), LedgerError> {
if reason.trim().is_empty() {
return Err(LedgerError::InvalidDraft(
"a discarded draft needs a reason".to_string(),
));
}
crate::parties::check_text("the reason", reason).map_err(LedgerError::InvalidDraft)?;
self.replay_discard_draft(entry)
}
/// [`Self::discard_draft`] as journaled, for replay: the draft goes.
#[doc(hidden)]
pub fn replay_discard_draft(&mut self, entry: EntryId) -> Result<(), LedgerError> {
let e = self
.entries
.get(&entry)
.ok_or(LedgerError::UnknownEntry(entry))?;
if e.status() != EntryStatus::Draft {
return Err(LedgerError::EntryAlreadyPosted(entry));
}
self.entries.remove(&entry);
self.entry_vendors.remove(&entry);
self.entry_instruments.remove(&entry);
self.entry_parties.remove(&entry);
self.entry_no_party.remove(&entry);
self.entry_settles.remove(&entry);
Ok(())
}
/// The journal line (`seq`) that posted `entry`: `entry.posted` or
/// `entry.reversed`. `None` for a draft, an unknown entry, or books
/// read by a path that doesn't carry the sequence (plan P4).
pub fn posted_seq(&self, entry: EntryId) -> Option<u64> {
self.posted_seq.get(&entry).copied()
}
/// Record the journal line that posted `entry` (the store, live and
/// on replay, after the posting line's own command). Idempotent for
/// the same line; a second, different line is ignored (the first
/// posting stands).
#[doc(hidden)]
pub fn note_posted_seq(&mut self, entry: EntryId, seq: u64) {
self.posted_seq.entry(entry).or_insert(seq);
}
/// Draft entries in id order, with their state.
pub fn drafts(&self) -> Vec<&JournalEntry> {
let mut ids: Vec<EntryId> = self
.entries
.values()
.filter(|e| e.status() == EntryStatus::Draft)
.map(|e| e.id())
.collect();
ids.sort_unstable();
ids.into_iter()
.filter_map(|i| self.entries.get(&i))
.collect()
}
/// Look up an entry by id (draft or posted).
pub fn get_entry(&self, entry: EntryId) -> Option<&JournalEntry> {
self.entries.get(&entry)