fix(core,store): child accounts share their parent's type; periods nest; lines checked when added
Three spec decisions from Stephen (2026-10-13), each test-first:
- A child account has its parent's type (ParentTypeMismatch): a child
subdivides its parent. Test: a_child_account_has_its_parents_type.
- Periods nest (a month in a quarter in a year) but never partially
overlap (PeriodsOverlap; a refusal uses no id). Test:
periods_nest_but_never_partially_overlap.
- A line's account must exist and take postings when the line is added,
not only at post (UnknownAccount / PlaceholderAccount from add_line
and add_line_fx); posting still re-checks. Two older tests that pinned
the post-time refusal now check both (the post-time check is reached
through the unchecked replay path, as old journals can).
Compatibility: replay reads history through new Ledger::replay_*
commands (add_account, add_line, add_line_fx, create_period) that skip
checks added later, so books written before these rules still open.
The contract test books_written_under_older_rules_still_open writes such
a journal directly and found, on its first run, that the SQLite
cross-check rebuild used the checked commands and would have failed
verification ("sqlite projection can't be read back"); it now replays
too. Documented in docs/spec/README.md; decisions recorded in rules.md
(B-1, B-4, B-12).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GWVo7x3UHw5kwCUaWxiejX
This commit is contained in:
1 parent
df38dcccf9
commit
925c3ba523
7 files changed
+362
-12
No files matched your search
@@ -2374,6 +2374,10 @@ impl Store {
|
||||
}
|
||||
|
||||
/// Rebuild a ledger purely from the SQLite projection (used by verify).
|
||||
/// Rebuild the ledger from the SQLite projection, for the cross-check.
|
||||
/// Like journal replay, it reads history, so it uses the engine's
|
||||
/// `replay_*` commands: rules added later never refuse books written
|
||||
/// before them.
|
||||
fn load_ledger_from_sqlite(&self) -> Result<Ledger, StoreError> {
|
||||
let mut ledger = Ledger::new(self.base_currency);
|
||||
let mut stmt = self.conn.prepare(
|
||||
@@ -2400,7 +2404,7 @@ impl Store {
|
||||
"account {code} currency {currency} != base"
|
||||
)));
|
||||
}
|
||||
let got = ledger.add_account(
|
||||
let got = ledger.replay_add_account(
|
||||
&code,
|
||||
&name,
|
||||
account_type,
|
||||
@@ -2477,7 +2481,7 @@ impl Store {
|
||||
})?;
|
||||
for (idx, lr) in lrows.enumerate() {
|
||||
let (account, side, amount, currency, class_id) = lr?;
|
||||
ledger.add_line(
|
||||
ledger.replay_add_line(
|
||||
got,
|
||||
account as u64,
|
||||
parse_side(&side)?,
|
||||
@@ -2883,7 +2887,7 @@ pub(crate) fn replay_line_into_ledger(
|
||||
let want = get_u64(p, "id")?;
|
||||
let account_type: AccountType = serde_json::from_value(p["account_type"].clone())?;
|
||||
let parent = p.get("parent").and_then(|v| v.as_u64());
|
||||
let got = ledger.add_account(
|
||||
let got = ledger.replay_add_account(
|
||||
get_str(p, "code")?,
|
||||
get_str(p, "name")?,
|
||||
account_type,
|
||||
@@ -2928,7 +2932,7 @@ pub(crate) fn replay_line_into_ledger(
|
||||
let fx_rate: Decimal = get_str(p, "fx_rate")?
|
||||
.parse()
|
||||
.map_err(|_| StoreError::JournalCorrupt("bad fx rate".into()))?;
|
||||
ledger.add_line_fx(
|
||||
ledger.replay_add_line_fx(
|
||||
id,
|
||||
account,
|
||||
side,
|
||||
@@ -2940,7 +2944,7 @@ pub(crate) fn replay_line_into_ledger(
|
||||
)?;
|
||||
}
|
||||
None => {
|
||||
ledger.add_line(id, account, side, Money::new(amount, currency))?;
|
||||
ledger.replay_add_line(id, account, side, Money::new(amount, currency))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2957,7 +2961,7 @@ pub(crate) fn replay_line_into_ledger(
|
||||
for l in p.get("lines").and_then(|v| v.as_array()).ok_or_else(|| {
|
||||
StoreError::JournalCorrupt("entry.posted without lines".into())
|
||||
})? {
|
||||
ledger.add_line(
|
||||
ledger.replay_add_line(
|
||||
id,
|
||||
get_u64(l, "account")?,
|
||||
parse_side(get_str(l, "side")?)?,
|
||||
@@ -3132,7 +3136,7 @@ pub(crate) fn replay_line_into_ledger(
|
||||
}
|
||||
"period.created" => {
|
||||
let want = get_u64(p, "id")?;
|
||||
let got = ledger.create_period(
|
||||
let got = ledger.replay_create_period(
|
||||
get_str(p, "name")?,
|
||||
parse_date(get_str(p, "start")?)?,
|
||||
parse_date(get_str(p, "end")?)?,
|
||||
|
||||
@@ -1000,3 +1000,55 @@ mod refusals {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Contract (2026-10-13): rules added to the engine never lock anyone out
|
||||
/// of books written before them. A journal holding what the new rules
|
||||
/// refuse (a child account of another type, partially overlapping periods,
|
||||
/// a draft line on a missing account) must still open and verify; replay
|
||||
/// goes through Ledger::replay_*.
|
||||
#[test]
|
||||
fn books_written_under_older_rules_still_open() {
|
||||
use serde_json::json;
|
||||
let dir = test_dir("older-rules");
|
||||
let mut store = Store::create(&dir, Currency::USD).unwrap();
|
||||
let cash = store
|
||||
.add_account("1000", "Cash", AccountType::Asset, None, true)
|
||||
.unwrap();
|
||||
drop(store);
|
||||
// What an older wanderbooks could have written.
|
||||
let mut w = JournalWriter::open(&dir.join(ledger_store::JOURNAL_FILE)).unwrap();
|
||||
w.append(
|
||||
"account.added",
|
||||
json!({"id": 2, "code": "1010", "name": "Rent", "account_type": "Expense",
|
||||
"parent": cash, "placeholder": false, "currency": "USD"}),
|
||||
)
|
||||
.unwrap();
|
||||
w.append(
|
||||
"period.created",
|
||||
json!({"id": 1, "name": "2026-01", "start": "2026-01-01", "end": "2026-01-31"}),
|
||||
)
|
||||
.unwrap();
|
||||
w.append(
|
||||
"period.created",
|
||||
json!({"id": 2, "name": "straddle", "start": "2026-01-15", "end": "2026-02-15"}),
|
||||
)
|
||||
.unwrap();
|
||||
w.append(
|
||||
"entry.created",
|
||||
json!({"id": 1, "date": "2026-03-01", "description": "draft"}),
|
||||
)
|
||||
.unwrap();
|
||||
w.append(
|
||||
"entry.line_added",
|
||||
json!({"id": 1, "account": 999, "side": "debit", "amount": "10.00", "currency": "USD"}),
|
||||
)
|
||||
.unwrap();
|
||||
drop(w);
|
||||
// Opening rebuilds from the journal; it must accept all of it.
|
||||
let _ = std::fs::remove_file(dir.join(ledger_store::DB_FILE));
|
||||
let store = Store::open(&dir).expect("older books still open");
|
||||
store.verify().unwrap();
|
||||
assert_eq!(store.ledger().chart().len(), 2);
|
||||
assert_eq!(store.ledger().list_periods().len(), 2);
|
||||
assert_eq!(store.ledger().get_entry(1).unwrap().lines().len(), 1);
|
||||
}
|
||||
Reference in new issue
Block a user