Skip to content

test(booking): stop the rollback property failing at random - #2087

Merged
robcohen merged 3 commits into
mainfrom
fix/rollback-proptest-flake
Aug 18, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/rollback-proptest-flake

Conversation

@robcohen

Copy link
Copy Markdown
Member

A latent CI flake in apply_rollback_properties, found while measuring something unrelated.

What happens

The property builds a randomized prefix of legs, appends a doomed final leg that oversells STK by 100,000, and asserts the transaction is rejected and the account untouched.

The doomed leg does not always fail. A prefix of {*} merges can empty the account, and an empty cost spec against an empty account is an augmentation, not a failed reduction — so the transaction succeeds and the property reports:

Test failed: the doomed leg must reject the transaction
minimal failing input: seed_costs = [100],
  legs = [Merge { units: 10 }, Merge { units: 4 }, Merge { units: 19 }, Merge { units: 17 }]

Whether CI sees it depends on which prefix proptest generates that run.

The product is right; the test was wrong

Beancount's book_reductions makes the same call — a posting held at cost is a reduction only if balance.is_reduced_by(units), otherwise it is booked as an augmentation. rustledger matches. So an all-Merge prefix draining the account and the doomed leg then augmenting is correct behavior, and the assertion was asserting something that was never true.

Fix

The doomed posting now reduces a commodity the generated legs never mention, seeded once up front. "This leg always fails" becomes a fact about the fixture rather than a hope about the prefix, and the prefix still exercises every mutation shape the undo log has to cover (push a slot, write a lot in place, drain one, and {*} dropping every matched lot at once).

Verification

  • The seed that fails on main passes here.
  • Eight randomized runs pass.
  • The property still has teeth: neutering has_reduction so the undo log is never armed fails it.
  • 146 suites green, clippy/fmt clean.

Age

It reproduces on every commit I tried, including before #2085 and #2081, so it has been latent since the file was added in #2067. Not a regression from recent work — it simply needed proptest to roll four merges in a row.

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

`a_failed_transaction_restores_the_account_exactly` assumed its doomed
final leg — overselling `STK` by 100,000 — always fails. It does not. A
generated prefix of `{*}` merges can empty the account, and an empty cost
spec against an empty account is an AUGMENTATION rather than a failed
reduction. Beancount's `book_reductions` makes the same call: reduction
only `if balance.is_reduced_by(units)`, else augment. So the transaction
succeeded, the property reported "the doomed leg must reject the
transaction", and whether CI saw it depended on which prefix proptest
happened to generate.

Found by proptest generating `[Merge, Merge, Merge, Merge]` while I was
measuring something unrelated. It reproduces on every commit I tried,
including before #2085 and #2081, so it has been latent since the file
was added in #2067.

The doomed posting now reduces a commodity the generated legs never
mention, seeded once up front. That makes "this leg always fails" a fact
about the fixture rather than a hope about the prefix, and the prefix
still exercises every mutation shape the undo log has to cover.

Verified: the seed that fails on main passes here, eight randomized runs
pass, and the property still catches a rollback bug — neutering
`has_reduction` so the undo log is never armed fails it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Copilot AI lite review requested due to automatic review settings August 18, 2026 21:44
@robcohen
robcohen requested a review from a team as a code owner August 18, 2026 21:44

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

This PR fixes a flaky proptest in rustledger-booking by making the “doomed” posting deterministically fail regardless of the randomly generated prefix of legs, ensuring the rollback invariant is tested reliably.

Changes:

  • Adds an “anchor” commodity position that generated legs never touch, so the final doomed posting is always a true reduction attempt (and thus always rejects).
  • Switches the doomed posting to oversell the anchor commodity rather than the generated commodity, eliminating random passes/failures.
Suppressed comments (1)

crates/rustledger-booking/tests/apply_rollback_properties.rs:168

  • This comment says it used to oversell STK, but the generated commodity in this test is CURRENCY ("X"). Updating the wording avoids confusion when debugging failures.
        // Doomed against the ANCHOR commodity, not the generated one.
        //
        // It used to oversell `STK`, on the assumption that overselling always
        // fails. It does not: a prefix of `{*}` merges can empty the account,

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 30.1ms ± 0.9ms 24.1 MB
beancount 610.7ms ± 5.4ms 50.7 MB
Comparison 20.3x faster 2.1x less

vs main branch (2026-08-18)

Metric main this PR Change
Time 39.1ms 30.1ms -23.0% 🟢
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 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 170 masked)
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
    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

robcohen and others added 2 commits August 18, 2026 22:12
…ty name

Copilot's review. The anchor lot spelled out a `CostSpec` that `per_unit`
already builds, which is how a fixture drifts from the shape the rest of
the file uses; and two comments called the generated commodity `STK`
while the constant has been `X` since the file was written.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Review of my own change. `holdings` compared units, cost and lot date but
not the commodity, which was harmless while the account held one. This
change gave it two — the generated legs work on one, the doomed leg
reduces the other — so a rollback that restored the right numbers against
the wrong commodity would have compared equal.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen merged commit 8264c58 into main Aug 18, 2026
57 of 59 checks passed
@robcohen
robcohen deleted the fix/rollback-proptest-flake branch August 18, 2026 23:04
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