Skip to content

test(loader): pin that diagnostics do not depend on input order - #1962

Merged
robcohen merged 2 commits into
mainfrom
feat/validate-determinism-properties
Aug 5, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/validate-determinism-properties

Conversation

@robcohen

@robcohen robcohen commented Aug 4, 2026

Copy link
Copy Markdown
Member

#1902 Phase 1 proposes "validation is deterministic and order-independent". The determinism half is already covered three times over in rustledger-validate — validation_pipeline_is_deterministic, coupled_pipeline_is_deterministic, prop_validation_deterministic — and every one runs the same input twice. That pins repeat-run stability and says nothing about ordering.

This adds the order half: 720 permutations, exhaustive over a six-block ledger, asserting the diagnostics are identical.

Two earlier framings were discarded, both because they could not fail

First, I compared the loader's directive sequence across permutations, assuming the loader canonicalizes order. It does not — LoadResult.directives preserves source order, and the date sort happens downstream in query, balance views and booking. That's worth knowing; it's documented nowhere. That version also failed for a reason unrelated to ordering: directives carry source metadata, so renumbering the file changes every line number and a Debug comparison reports it as a difference.

Second, I compared diagnostics but couldn't falsify it by deleting the loader's sorts — because the validators are order-insensitive in their own right. The sorts are not what makes the property hold.

Both were dropped rather than shipped. A guard I can't make fail is decoration, and this session has already turned up four tests that were pinning the wrong thing.

What ships, with a demonstrated failure mode

A validator that reads directives.first() makes it fail:

input order [3, 1, 2, 0, 4, 5] changed the diagnostics

That's the shape a real regression would take — a validator consulting input position instead of ledger state — and it's why the test is worth having even though nothing today violates it.

Exhaustive rather than sampled

720 orderings is cheap, and exhaustive means a failure is reproducible without a seed. A randomized property is at its most annoying exactly when it finally trips.

Carries a non-vacuity assertion: the fixture must produce diagnostics, or the comparison is between two empty vectors forever.

Verification

  • loader (with validation) and validate suites: 0 failures
  • clippy at 1.97.0 clean, cargo fmt --check clean

Refs #1902.

#1902 Phase 1 proposes "validation is deterministic and order-independent".
The determinism half is already covered THREE times over in
rustledger-validate - validation_pipeline_is_deterministic,
coupled_pipeline_is_deterministic, prop_validation_deterministic - and every
one of them runs the SAME input twice. That pins repeat-run stability and
says nothing about ordering.

This adds the order half: 720 permutations, exhaustive over a six-block
ledger, asserting the diagnostics are identical.

TWO EARLIER FRAMINGS WERE DISCARDED, BOTH BECAUSE THEY COULD NOT FAIL

First I compared the LOADER's directive sequence across permutations, on the
assumption that the loader canonicalizes order. It does not:
`LoadResult.directives` preserves source order and the date sort happens
downstream, in query, balance views and booking. Worth knowing; it is
documented nowhere. That version also failed for a reason unrelated to
ordering - directives carry source metadata, so renumbering the file changes
every line number, and a Debug comparison reports that as a difference.

Then I compared diagnostics but could not falsify it by deleting the loader's
sorts, because the validators are order-insensitive in their own right. The
sorts are not what makes the property hold.

What ships is the user-facing claim WITH a demonstrated failure mode: a
validator that reads `directives.first()` makes it fail.

  input order [3, 1, 2, 0, 4, 5] changed the diagnostics

That is the shape a real regression would take - a validator that consults
input position instead of ledger state - and it is why the test is worth
having even though nothing today violates it.

EXHAUSTIVE RATHER THAN SAMPLED

720 orderings is cheap, and exhaustive means a failure is reproducible without
a seed. A randomized property is at its most annoying exactly when it finally
trips.

Carries a non-vacuity assertion: the fixture must produce diagnostics, or the
comparison is between two empty vectors forever.

Refs #1902.
Copilot AI review requested due to automatic review settings August 4, 2026 20:35
@robcohen
robcohen requested a review from a team as a code owner August 4, 2026 20:35
@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.2ms ± 2.5ms 24.1 MB
beancount 894.9ms ± 6.2ms 50.5 MB
Comparison 25.4x faster 2.1x less

vs main branch (2026-08-04)

Metric main this PR Change
Time 31.5ms 35.2ms +11.7% 🔴
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

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Adds an integration test in rustledger-loader to assert that validation diagnostics are invariant under permutations of directive (block) order, supporting the “order-independent” half of the determinism claim tracked in #1902.

Changes:

  • Introduces an exhaustive-permutation test harness that rewrites a temp ledger file for each ordering and compares the resulting validation outputs.
  • Adds a small permutation generator and a non-vacuity assertion to ensure the fixture actually produces diagnostics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@robcohen
robcohen enabled auto-merge (squash) August 4, 2026 20:43
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 95% (raw 89%, 162 masked)
Full AST Match 100%
Total Files 812
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Three review points, all real:

- The module doc opened by claiming the loader produces a canonical
  directive order. It does not, and the body two paragraphs down says
  so - leftover from a framing that was discarded for that reason.

- Codes were collected via Debug, i.e. the enum variant name. Renaming
  a variant is a refactor; here it would have read as a behavior
  change. Compare ErrorCode::code(), the stable rendered E-number.

- blocks() defines eight blocks but only the first six were permuted,
  so the balance directive and the unopened-account transaction never
  moved, despite the fixture comments calling out the same-date pad and
  balance as the pair most likely to be order-sensitive. E1001 was not
  produced at all. Now exhaustive over all 40320 orderings (18s).

Non-vacuity tightened from !is_empty() to a per-code check, since the
unbalanced transaction alone satisfied the old form. Verified the test
can still report dirty by dropping the last directive in run_early: it
fails on [0, 1, 2, 3, 4, 6, 7, 5], a permutation only reachable now.
Copilot AI review requested due to automatic review settings August 5, 2026 01:57

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Suppressed comments (3)

crates/rustledger-loader/tests/validation_order_independence.rs:77

  • ValidationSession::run_late is documented as requiring booking to have run between Early and Late, but the test calls run_late(&plain, ...) on the pre-booking directives. This can silently become incorrect if the fixture (or validators) starts relying on booked/interpolated transactions, and it doesn’t match the real pipeline shape (Early → book → Late → finalize).
    let session = rustledger_validate::ValidationSession::new(options);
    let (session, early) = session.run_early(&plain, today);
    let (session, late) = session.run_late(&plain, today);
    let pad = session.finalize();

crates/rustledger-loader/tests/validation_order_independence.rs:102

  • The doc comment says “720 orderings”, but the test currently permutes blocks().len() items (8 blocks), and even references “40320 orderings” later. This is misleading and also conflicts with the PR description’s stated 720-permutation scope; either update the docs to match the actual 8! = 40,320 permutations or reduce the fixture to 6 blocks if 720 is the intended bound.
/// 720 orderings is cheap, and exhaustive means a failure is reproducible
/// without a seed — the thing that makes a randomized property annoying to act
/// on when it finally trips.

crates/rustledger-loader/tests/validation_order_independence.rs:49

  • blocks() allocates a fresh Vec every time it’s called, but it’s invoked inside codes_for (once per permutation). With 40,320 permutations, that’s a lot of unnecessary allocations; using a const array (or a static slice) avoids repeated heap work and reduces test runtime noise.
fn blocks() -> Vec<&'static str> {
    vec![
        "2024-01-01 open Assets:Bank",
        "2024-01-01 open Expenses:Food",
        "2024-01-01 open Equity:Open",

@robcohen
robcohen merged commit 838170d into main Aug 5, 2026
75 of 76 checks passed
@robcohen
robcohen deleted the feat/validate-determinism-properties branch August 5, 2026 02:25
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