fix(store,gui,package): every reader that derives facts reads the committed journal (P15 T1 review)

A failed call's ids are handed out again (nothing it wrote counted), so a
reader of the raw journal pins dead lines on whatever takes the id next: a
failed books.create_bill for entry 42 then a manual entry 42 showed as a
Bill. One accessor, calls::committed (lines outside a call and of calls
that closed ok, the frame left out), now feeds the viewer's books (entry
events and origin, asset trails, party events), the package feed attention
reads, and the adopt preview's settings check. The journal page and
journal_history keep every line and say in words of each line of a call
that failed, never finished or is still running that it did not count.

A call's closing line is now bounded (ANSWER_CAP 4 KiB: above it, the
answer's SHA-256 and length), a call that wrote no line leaves no line (its
key and answer live in a bounded in-memory table), and the journaled
argument digest is an HMAC under the books' own call key, so two requests
that differ only in a tax id are two requests and nothing journaled lets a
guess of one be tested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 10:09:17 -07:00
1 parent e542319e63
commit d4c0036599
11 files changed
+950 -120

No files matched your search

+1 -1
View File
@@ -89,7 +89,7 @@ impl ledger_package::PackageBooks for View<'_> {
}
fn journal_seq(&self) -> u64 {
self.0.journal.len() as u64
self.0.head_seq
}
fn journal(&self) -> Result<Vec<JournalLine>, StoreError> {
+25 -3
View File
@@ -274,7 +274,20 @@ pub struct Books {
pub settings: BooksSettings,
pub data_dir: PathBuf,
pub attachments: Vec<Attachment>,
/// The committed journal: the lines that are facts of the books (those
/// outside any call and those of calls that closed `ok`), the frame
/// lines left out. Every fact the viewer derives from the journal
/// (an entry's events and origin, an asset's trail, a party's events)
/// comes from here, never from [`Books::history`]: ids are handed out
/// again after a failed call, so a dead line would be pinned on
/// whatever takes the id next (ledger_store::calls).
pub journal: Vec<JournalLine>,
/// Every line of the journal, in order (seq = index + 1), and each
/// line's standing: for the journal page, which shows history and
/// says of a line that did not count that it did not.
pub history: Vec<(JournalLine, ledger_store::calls::Standing)>,
/// The seq of the journal's last line.
pub head_seq: u64,
pub head: String,
pub today: NaiveDate,
pub loaded_at: chrono::DateTime<chrono::Utc>,
@@ -605,7 +618,14 @@ impl Books {
) -> Result<Books, String> {
let ledger = snap.ledger().clone();
let attachments = snap.attachments(None).map_err(|e| e.to_string())?;
let journal = snap.journal().to_vec();
let journal = snap.committed_journal().map_err(|e| e.to_string())?;
let history: Vec<_> = snap
.journal()
.iter()
.cloned()
.zip(snap.journal_standings().map_err(|e| e.to_string())?)
.collect();
let head_seq = snap.journal_seq();
// The server's own rule, so the viewer flags exactly what the
// server would refuse: the books' settings.
let policy = ledger_config::codec::receipt_policy(ledger.settings());
@@ -676,7 +696,7 @@ impl Books {
let statements = snap.statements(None).map_err(|e| e.to_string())?;
let reconciliations = snap.reconciliations(None).map_err(|e| e.to_string())?;
let extractions = snap.extractions().map_err(|e| e.to_string())?;
let fresh_from_seq = prev_seq.unwrap_or(journal.len() as u64);
let fresh_from_seq = prev_seq.unwrap_or(head_seq);
let mut fresh = HashSet::new();
let closings = ledger.closing_entry_ids();
let mut entries = Vec::new();
@@ -817,6 +837,8 @@ impl Books {
data_dir: snap.data_dir().to_path_buf(),
attachments,
journal,
history,
head_seq,
head: snap.head_hash().to_string(),
today,
loaded_at: chrono::Utc::now(),
@@ -864,7 +886,7 @@ impl Books {
}
pub fn seq(&self) -> u64 {
self.journal.len() as u64
self.head_seq
}
pub fn entry(&self, id: EntryId) -> Option<&EntryInfo> {
+35 -8
View File
@@ -1270,6 +1270,24 @@ pub fn describe(b: &Books, line: &JournalLine) -> Option<Event> {
}
}
/// A journal line's event for the journal page: the sentence the books
/// give it when it counts; for a line of a call that did not count, its
/// plain description from the journal's own words and the note that it did
/// not count, never a sentence built from the books' present state (ids
/// are handed out again after a failed call, so "Posted #42" would name
/// whatever took 42 next), and no link.
pub fn event_for(b: &Books, line: &JournalLine, standing: ledger_store::calls::Standing) -> Event {
match standing.note() {
None => describe_any(b, line),
Some(note) => Event {
icon: icon::WARNING,
text: format!("{} ({note})", ledger_store::describe::describe(line)),
route: None,
also: None,
},
}
}
/// Like [`describe`], but every event gets a sentence.
pub fn describe_any(b: &Books, line: &JournalLine) -> Event {
if let Some(e) = describe(b, line) {
@@ -1342,7 +1360,7 @@ pub fn describe_any(b: &Books, line: &JournalLine) -> Event {
pub fn show(ui: &mut Ui, b: &Books, selected: Option<u64>, shell: &mut Shell<'_>) {
if let Some(seq) = selected
&& let Some(line) = b.journal.get(seq.saturating_sub(1) as usize)
&& let Some((line, standing)) = b.history.get(seq.saturating_sub(1) as usize)
{
super::split(
ui,
@@ -1350,7 +1368,7 @@ pub fn show(ui: &mut Ui, b: &Books, selected: Option<u64>, shell: &mut Shell<'_>
480.0,
shell,
|ui, shell| list(ui, b, selected, shell),
|ui, shell| detail(ui, b, line, shell),
|ui, shell| detail(ui, b, line, *standing, shell),
);
} else {
list(ui, b, None, shell);
@@ -1365,7 +1383,7 @@ fn list(ui: &mut Ui, b: &Books, selected: Option<u64>, shell: &mut Shell<'_>) {
"Journal",
&format!(
"Every change to the books, in order: {} events, each hash-chained to the one before. Nothing is ever edited or deleted; corrections are new events.",
b.journal.len()
b.history.len()
),
);
let st = &mut shell.state.journal;
@@ -1381,11 +1399,11 @@ fn list(ui: &mut Ui, b: &Books, selected: Option<u64>, shell: &mut Shell<'_>) {
let q = st.query.to_lowercase();
let hide = st.hide_steps;
let rows: Vec<(&JournalLine, Event)> = b
.journal
.history
.iter()
.rev()
.filter(|l| !(hide && describe(b, l).is_none()))
.map(|l| (l, describe_any(b, l)))
.filter(|(l, st)| !(hide && st.counts() && describe(b, l).is_none()))
.map(|(l, st)| (l, event_for(b, l, *st)))
.filter(|(l, e)| {
q.is_empty() || e.text.to_lowercase().contains(&q) || l.line_type.contains(&q)
})
@@ -1461,9 +1479,15 @@ fn list(ui: &mut Ui, b: &Books, selected: Option<u64>, shell: &mut Shell<'_>) {
});
}
fn detail(ui: &mut Ui, b: &Books, line: &JournalLine, shell: &mut Shell<'_>) {
fn detail(
ui: &mut Ui,
b: &Books,
line: &JournalLine,
standing: ledger_store::calls::Standing,
shell: &mut Shell<'_>,
) {
let p = pal(ui.ctx());
let ev = describe_any(b, line);
let ev = event_for(b, line, standing);
egui::ScrollArea::vertical()
.id_salt("event_detail")
.show(ui, |ui| {
@@ -1485,6 +1509,9 @@ fn detail(ui: &mut Ui, b: &Books, line: &JournalLine, shell: &mut Shell<'_>) {
ui.add(egui::Label::new(RichText::new(v).font(theme::regular(12.5))).wrap());
});
};
if let Some(note) = standing.note() {
fact(ui, "Counts", note);
}
fact(ui, "Type", &line.line_type);
fact(ui, "Recorded", &fmt::timestamp(&line.ts));
fact(ui, "Previous", &line.prev);
+114
View File
@@ -986,3 +986,117 @@ fn the_viewer_holds_a_numbered_invoice_as_the_store_does() {
assert_eq!(shown.terms.as_ref().unwrap().describe(), "2/10 net 30");
assert_eq!(shown.lines[0].quantity, Some(Decimal::new(3, 0)));
}
/// A call that failed after writing lines, followed by a call that takes
/// the ids it handed out again (nothing it wrote counted): the viewer's
/// books, the package feed that attention reads, and the history all say
/// the dead lines are not facts, and nothing of the dead call is pinned on
/// what took its ids (WB-format-12).
#[test]
fn a_failed_calls_lines_are_not_facts_when_its_ids_are_used_again() {
use ledger_store::calls::{CallEnd, CallId, Standing};
let (d, mut s) = store_at("failed-call");
let today = NaiveDate::from_ymd_opt(2026, 5, 20).unwrap();
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let cap = s
.add_account("3000", "Capital", AccountType::Equity, None, false)
.unwrap();
let id = |key: &str, tool: &str| CallId {
token: "t".into(),
tool: tool.into(),
key: key.into(),
args: "a".repeat(64),
};
let lines = |amount: i64| {
vec![DocumentLine {
description: "x".into(),
amount: Decimal::new(amount, 2),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}]
};
let before = s.journal_seq();
// A failed books.create_bill: a vendor, a bill, an entry (id 1).
s.begin_call(id("key-dead-0001", "books.create_bill"))
.unwrap();
let dead_vendor = s.add_vendor("Dead Vendor").unwrap();
s.create_bill(dead_vendor, today, today + Days::new(30), lines(5_000))
.unwrap();
s.close_call(CallEnd::Failed("the call failed after its lines"))
.unwrap();
let dead_end = s.journal_seq();
assert!(dead_end > before + 3);
// A manual entry, which takes entry id 1 again, and a vendor, which
// takes party id 1 again.
let e = s.create_entry(today, "Opening deposit").unwrap();
assert_eq!(e, 1, "the failed call's entry id is handed out again");
let m = Money::new(Decimal::new(100_000, 2), Currency::USD);
s.add_line(e, cash, Side::Debit, m).unwrap();
s.add_line(e, cap, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
let live_vendor = s.add_vendor("Real Vendor").unwrap();
assert_eq!(live_vendor, dead_vendor);
drop(s);
let source = Source {
data_dir: d.join("books-data"),
today: Some(today),
};
let b = data::load_once(&source).unwrap();
// The entry is a manual one: not a "Bill", which is what the dead
// bill.created line (for entry 1) would have made of it.
let entry = b.entry(e).unwrap();
assert_eq!(entry.origin, "Journal entry");
assert!(
entry
.events
.iter()
.all(|&i| !(before + 1..=dead_end).contains(&b.journal[i].seq)),
"an entry's events include a dead line"
);
// No fact the viewer derives holds a dead line: the committed journal
// has none of them, and none of the frame.
assert!(
b.journal
.iter()
.all(|l| !(before + 1..=dead_end).contains(&l.seq) && !l.line_type.starts_with("call."))
);
// The party that took the id again has none of the dead vendor's events.
let party = &b.parties[&live_vendor];
assert!(
party
.events
.iter()
.all(|&i| !(before + 1..=dead_end).contains(&b.journal[i].seq))
);
// The package feed attention reads is the same committed journal.
let snap = ledger_store::Snapshot::load(&d.join("books-data")).unwrap();
let fed = ledger_package::PackageBooks::journal(&snap).unwrap();
assert_eq!(fed.len(), b.journal.len());
assert!(
fed.iter()
.all(|l| !(before + 1..=dead_end).contains(&l.seq))
);
let store = Store::open(&d.join("books-data")).unwrap();
let fed = ledger_package::PackageBooks::journal(&store).unwrap();
assert_eq!(fed.len(), b.journal.len());
// The viewer's seq is the journal's head, frame lines counted.
assert_eq!(b.seq(), b.history.len() as u64);
// History shows every line, and says of the dead ones in words that
// they did not count; the viewer never names a dead line by the live
// entry that took its id.
let dead: Vec<_> = b
.history
.iter()
.filter(|(_, st)| *st == Standing::Failed)
.collect();
assert!(dead.len() >= 3);
for (line, st) in dead {
let ev = ledger_gui::pages::journal::event_for(&b, line, *st);
assert!(ev.text.contains("not counted"), "{}", ev.text);
assert!(ev.route.is_none());
}
}