Skip to content

fix(validate): catch postings dated before their account's open - #1742

Merged
robcohen merged 3 commits into
mainfrom
fix/account-lifecycle-check
Jul 10, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/account-lifecycle-check

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026

Copy link
Copy Markdown
Member

What

Fixes a silent-pass correctness bug: rledger check accepted 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:

2020-01-01 open Equity:Opening
2020-01-15 * "Too early"
  Assets:Bank  100 USD
  Equity:Opening
2020-02-01 open Assets:Bank USD

→ 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:

  • 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 late there (so account-renaming regular plugins aren't falsely flagged on pre-rewrite names).
  • By the late phase, state.accounts holds 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_deferred set; the late found-branch runs the full lifecycle (open and close date) check for exactly those postings.

Two sharp edges handled:

  • No double-reporting: postings whose account existed early already had lifecycle checked there — re-running all of them in late would double-report posting-after-close. Only deferred postings are re-checked.
  • Key includes the account: synthesized postings share a sentinel span, so a (file_id, span) key would collide — one deferred synthesized posting would re-run lifecycle for every synthesized posting and double-report AccountClosed. 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

  • Repro now errors with exact bean-check parity (one error, same line): E1001 — Account Assets:Bank used on 2020-01-15 but not opened until 2020-02-01.
  • test_account_lifecycle_consistency passes again.
  • New unit tests drive the full two-phase pipeline: exactly-once reporting for explicit-before-open and elided-before-open, no doubled AccountClosed for posting-after-close, and same-date use stays clean.
  • Full suites green: validate, loader, plugin, ffi-wasi, CLI. clippy/fmt/rustdoc clean.

🤖 Generated with Claude Code

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]>
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 22:06
Copilot AI review requested due to automatic review settings July 9, 2026 22:06
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33.1ms ± 0.7ms 23.6 MB
beancount 886.3ms ± 14.3ms 50.7 MB
Comparison 26.8x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 33.1ms -1.8% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_deferred set.
  • In late validation, when an account is found, re-run validate_account_lifecycle only 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.

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

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

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.51852% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-validate/src/lib.rs 98.13% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 3acc3cf into main Jul 10, 2026
43 of 44 checks passed
@robcohen
robcohen deleted the fix/account-lifecycle-check branch July 10, 2026 10:14
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants