Skip to content

fix(query): make the MIN/MAX property test exact on tied values (flaky in CI) - #2535

Merged
robcohen merged 1 commit into
mainfrom
fix/minmax-property-ties
Oct 8, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/minmax-property-ties

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026

Copy link
Copy Markdown
Member

What failed

min_max_order_by_property_test::min_max_equal_order_by_ends, added in #2493, failed intermittently in CI on unrelated PRs (#2526, #2527); both passed on rerun. The two shrunk cases from the CI logs:

  • Assets:G0 8 X {18 USD} on 2024-02-02 and Assets:G1 8 X {18 USD} on 2024-02-03: the per-account inventories from a subquery, then max(s).
  • Assets:G0 4 X {14 USD}, and Assets:G1 holding 4 X {14 USD} plus USD postings that net to zero.

Diagnosis: a flaw in the property; MIN/MAX and ORDER BY are correct

In both cases the two inventories tie under the order without being equal. The position order is beancount's Position.sortkey (currency rank, cost number, cost currency, units number). It ignores a lot's date and label, so the two lots compare equal. Checked in beancount 3.2.3:

  • ORDER BY is stable in both tools, so a tie group stays in input order.
  • MIN and MAX replace their current value only on a strict comparison, so each keeps the first value of its extreme tie group.
  • The property's flaw: it expected MAX to equal the last row of ORDER BY v. With a tie that is the second lot. That holds only when the top value is unique.

Not a MIN/MAX/ORDER BY disagreement on untied values. With the corrected property, a 3,000-case release run finds none.

Should lots differing only in date tie? Yes. This is beancount's own Position.sortkey, which bean-query's ORDER BY uses, and ties already keep input order, which is deterministic. Breaking ties by date would reorder rows away from bean-query for no correctness gain. Unchanged.

bean-query on the same tie (8 X {18 USD, 2024-01-01} then 8 X {18 USD, 2024-02-03}):

  • MIN returns the first lot, as rustledger does.
  • MAX returns the later-dated lot: its > falls back to plain tuple comparison, which compares the cost date. This is part of the already documented MAX divergence (§15). rustledger returns the first lot for both.

Fix

  • Exact property: MIN must equal the first non-NULL value under ORDER BY v, and MAX the first under ORDER BY v DESC. Because ORDER BY is stable and the aggregates use strict comparisons, this holds exactly, ties included. The test is renamed min_max_equal_first_value_of_each_sort_direction.
  • Both CI seeds are committed in min_max_order_by_property_test.proptest-regressions, next to the test, as the booking and core crates do. They re-run deterministically before any random cases.
  • New pinned test min_max_return_the_first_of_tied_lots: MIN and MAX both return the first of two date-only-different lots, and that lot is the first row of ORDER BY in both directions.
  • Docs:
  • Case count 64 → 32: each case now runs both sort directions, so this keeps the runtime roughly where it was. On this machine under load (load average about 16): about 4 s in debug and about 1.8 s in release. The old version at 64 cases measured 1.4 s debug on an idle machine.

No query output changes.

Verification

  • Fail-without-fix: with the regressions file added and the old property, the test fails on unmodified main 3 out of 3 runs, deterministically. With the new property it passes on every run.
  • The new property still catches real bugs. Each mutation below fails it:
    • (A) amounts compared by number only, a route that skips ORDER BY's order;
    • (B) MAX replacing its value on ties, which would make it "last of the tie group".
  • cargo test -p rustledger-query --all-features: 810 passed across 38 binaries. Workspace clippy -D warnings, fmt and cargo doc -D warnings are clean.

Refs #2447

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

min_max_order_by_property_test compared MAX with the LAST value ORDER BY
gives. The position order ties values that are not equal, as
beancount's Position.sortkey does for two lots differing only in date;
the stable sort keeps them in input order, so its last value was the
second lot while MAX (strict comparison) keeps the first. It failed
whenever proptest drew such a tie, at random on unrelated PRs.

The property now says MIN is the first value under ORDER BY and MAX the
first under ORDER BY ... DESC, which is exact with ties. The two CI
seeds are committed in the proptest-regressions file. A 3,000-case run
finds no disagreement on untied values. Docs state the tie rule; an
explicit test pins it. Cases go from 64 to 32 because each case now
runs both sort directions.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 8, 2026 20:13
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 46.4ms ± 2.0ms 849.4ms ± 9.1ms 18.3x 51.1 MB / 50.2 MB
investment 69.8ms ± 1.7ms 3166.9ms ± 65.0ms 45.4x 55.0 MB / 58.0 MB
fifo 67.1ms ± 1.5ms 17587.7ms ± 1787.7ms 262.3x 55.1 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 46.8ms ± 4.8ms 46.4ms ± 2.0ms -0.9% ✅ within noise 51.0 MB → 51.1 MB (+0.2%)
investment 72.0ms ± 1.5ms 69.8ms ± 1.7ms -3.0% ✅ within noise 55.1 MB → 55.0 MB (-0.1%)
fifo 69.3ms ± 1.7ms 67.1ms ± 1.5ms -3.2% ✅ within noise 59.1 MB → 55.1 MB (-6.7%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 2.3ms, investment 1.0ms, fifo 1.0ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Oct 8, 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

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
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

robcohen commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

Review: no changes needed

  1. The corrected property is not weaker where it matters. Three mutations to value_less_than, each run 3 times, fail every time:

    • (A) amounts compared by number only;
    • (B) MAX replacing its value on ties;
    • (C) a comparator that disagrees with ORDER BY only on values that do not tie: the order is reversed whenever two amounts or positions are in different currencies, and same-currency pairs, including every tie, are untouched. Shrunk failing case: 1 USD and 1 EUR @ 1.5 USD. This is the class of bug the property exists to catch.
  2. No leftover flake. 20 consecutive runs with fresh random seeds all pass. That is 640 random ledgers plus the two committed seeds each run, on top of the 3,000-case release run reported in the PR body.

  3. The regressions file is used by CI's runner. CI runs cargo nextest run --all-features from the workspace root, and nextest prints nothing about the file when the test passes. So I checked it locally with that runner and the same layout, using the old property:

    • with the committed regressions file: fails 3 out of 3 runs, on the committed seed's case;
    • with the file removed: passes 10 out of 10 runs.

    That shows the file is found by path and its seeds run first. In CI, the original failure saved its seed to that same path, crates/rustledger-query/tests/min_max_order_by_property_test.proptest-regressions, so CI resolves the same location.

Required checks (build, Python-gated cargo tests) are green on f7cfaef.

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.

1 participant