Repository navigation
fix(validate): catch postings dated before their account's open - #1742
Merged
Merged
Conversation
rledger check silently accepted a ledger that posts to an account before its open date (Python bean-check rejects it; the compat integration test test_account_lifecycle_consistency has been failing whenever the Python container is available). Root cause: directives are validated as a date-sorted stream through two phases sharing one LedgerState. A use-before-open posting always streams BEFORE its open, so in the early phase the account is absent from state.accounts — and explicit postings are deliberately deferred to the late phase there (so account-renaming regular plugins are not falsely flagged on pre-rewrite names). But by the late phase state.accounts holds every open in the ledger regardless of date, so the account is FOUND, and the found-branch ran only the currency check. Presence proved nothing about dates, validate_account_lifecycle never ran, and the whole use-before-open-by-date class passed. Fix: the early phase records each deferred explicit posting in a new lifecycle_deferred set, and the late found-branch runs the full lifecycle (open/close date) check for exactly those postings. Postings whose account existed early already had lifecycle checked there — re-running all of them would double-report posting-after-close. The set is keyed by (file_id, span, account), not source identity alone: synthesized postings share a sentinel span, and without the account in the key one deferred synthesized posting would re-run lifecycle for every synthesized posting, double-reporting AccountClosed. (The account key also means a plugin-renamed posting is not re-checked against its new account's dates — unchanged from the pre-fix behavior for that edge.) Output now matches bean-check on the repro (one error, same line): E1001 "Account Assets:Bank used on 2020-01-15 but not opened until 2020-02-01". New unit tests pin the class end-to-end through both phases: exactly-once reporting for explicit-before-open and elided-before-open, no double AccountClosed for after-close, and same-date use stays clean. test_account_lifecycle_consistency passes again. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
📊 Benchmark Results
vs main branch (2026-07-10)
What do these numbers mean?
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a correctness gap in rustledger-validate’s two-phase (early/late) transaction validation where explicit postings could silently pass even when dated before their account’s open date, due to account presence being re-checked late without re-running lifecycle (open/close date) validation.
Changes:
- Track explicit postings deferred in early validation (account absent at that point in the sorted stream) via a new
LedgerState::lifecycle_deferredset. - In late validation, when an account is found, re-run
validate_account_lifecycleonly for postings recorded as deferred to avoid double-reporting. - Add unit tests covering the fixed silent-pass scenario and guarding against duplicate lifecycle reporting.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/rustledger-validate/src/validators/transaction.rs |
Records deferred explicit postings during early validation and conditionally re-runs lifecycle checks for them during late validation. |
crates/rustledger-validate/src/lib.rs |
Adds lifecycle_deferred to LedgerState and introduces unit tests to validate the corrected behavior across the full early/late pipeline. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…eep review) Synthesized postings all share the sentinel (SYNTHESIZED_FILE_ID, Span::ZERO) identity, so one deferred key matched every synthesized posting to the same account across the ledger — the late pass would re-run lifecycle on postings whose account existed early and double-report posting-after-close (reachable through the validate crate's programmatic API; parsed input has unique spans). Synthesized postings are now skipped at the deferral site — a programmatically built use-before-open posting stays unchecked, the same documented gap as plugin-added postings, pinned by a new test so a future change revisits the trade-off consciously. The field doc no longer overclaims what the account-in-key component fixes. Test fixtures now build postings with unique real spans (like parsed input) so the deferral under test actually arms. Co-Authored-By: Claude Fable 5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
robcohen
added a commit
that referenced
this pull request
Jul 10, 2026
) * ci: run the python-gated cargo tests with bean-check installed Several cargo test suites compare rledger against Python beancount directly (Command::new("bean-check")) and skip silently when the binary is absent — which is every CI environment today. The use-before-open silent-pass bug (#1742) sat on main behind exactly that skip: test_account_lifecycle_consistency would have caught it, but nothing ever ran it with bean-check on PATH. Add a python-gated-cargo-tests job to the Compatibility workflow (which already runs on push/PR/nightly and owns the python setup): - pip install beancount, then ASSERT bean-check is runnable before testing — if the binary breaks, the job goes red at the assert instead of the tests green-skipping; - run the two python-gated suites (rustledger integration_test, rustledger-core synthetic_generation), with a comment pointing at the grep that enumerates them; - a final belt-and-braces step greps a rerun for the skip marker, so a future availability-probe drift (different binary, container) cannot reintroduce the silent skip. Verified locally with bean-check available: the job catches exactly the #1742 failure on current main (18 passed / 1 failed), so MERGE ORDER MATTERS — land #1742 first or this job is red on arrival. That red is the proof the gate works. Co-Authored-By: Claude Fable 5 <[email protected]> * ci: cover bean_price_compat; check skip markers from the primary run (review) Two review catches, both real: - The grep hint the job carried ('Command::new("bean-') matches the bean_price_compat suite too — which turned out to be a third python-gated suite the job wasn't running at all. Include it (pip install beanprice, assert bean-price --version) rather than narrowing the hint. - The "Fail on silent skips" step re-ran only integration_test inside an `if`, masking a failing rerun and covering one suite. The primary run now tees to a log (set -o pipefail so tee can't mask a red cargo), and the skip check greps that log for every suite's actual skip marker. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Jul 10, 2026
* docs: canonical-function discipline and toolchain-bump runbook Distills the 2026-07 duplication review (PRs #1731-#1743) into standing guidance: the one anti-pattern every found bug traced to, the ranked remedies (call the canonical / document divergence at both sites / drift-guard test), the current canonical inventory, the two supporting test-hygiene rules (#1729, #1742), and the lockstep toolchain-bump procedure learned from #1745/#1751. Co-Authored-By: Claude Fable 5 <[email protected]> * docs: scope the pin claim to stable-channel sites (review) Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a silent-pass correctness bug:
rledger checkaccepted ledgers that post to an account before its open date. Python bean-check rejects them, and the compat integration test (test_account_lifecycle_consistency) has been failing whenever the Python container is available — that failing test is how the bug surfaced (during #1741's test run).Repro on main:
→ main:
✓ No errors found. bean-check:Invalid reference to inactive account 'Assets:Bank'.Root cause
Directives are validated as a date-sorted stream through two phases sharing one
LedgerState:open, so in the early phase the account is absent fromstate.accounts— and explicit postings are deliberately deferred to late there (so account-renaming regular plugins aren't falsely flagged on pre-rewrite names).state.accountsholds every open in the ledger regardless of date. The account is found, and the found-branch ran only the currency check. Presence proved nothing about dates;validate_account_lifecycle(which has the open/close date checks, and its own passing unit tests) never ran for this class.Net effect: the sorted-stream architecture structurally guaranteed the open-date check could never fire for explicit postings.
Fix
The early phase records each deferred explicit posting in a new
lifecycle_deferredset; the late found-branch runs the full lifecycle (open and close date) check for exactly those postings.Two sharp edges handled:
(file_id, span)key would collide — one deferred synthesized posting would re-run lifecycle for every synthesized posting and double-reportAccountClosed. Keying by(file_id, span, account)prevents that. (Consequence: a posting renamed by a regular plugin isn't re-checked against its new account's dates — identical to pre-fix behavior for that edge, documented on the field.)Testing
E1001 — Account Assets:Bank used on 2020-01-15 but not opened until 2020-02-01.test_account_lifecycle_consistencypasses again.AccountClosedfor posting-after-close, and same-date use stays clean.🤖 Generated with Claude Code