Repository navigation
fix(validate): apply the E3002 rule per currency group, not to the total - #1918
Merged
Merged
Conversation
A posting whose units number is elided beside a per-unit cost is solved
as `residual / cost_per_unit` and written in the posting's own commodity.
`HOOL {300.00 USD}` against `-600.00 USD` now books `2 HOOL {300.00 USD}`
where it previously booked nothing and reported the whole other leg as an
imbalance. The same inversion covers a per-unit price, which had the
identical gap.
The root cause was one wrong premise, not a missing feature. A
units-missing posting was registered under its UNITS currency, but a
posting carrying a cost weighs in the COST currency. It therefore
absorbed a residual that was none of its business, which fabricated lots
rather than merely failing to fill them:
A HOOL {300.00 USD} / B -600.00 USD / D 5.00 HOOL
was A = -5.00 HOOL (wrong sign AND magnitude, from D's residual)
now A = +2.00 HOOL
A {300.00 USD} / B -600.00 USD
was A = 600.00 USD {300.00 USD}, an invented lot whose imbalance was
then reported as an invented `179400.0000 USD`
now refused: a cost spec names the cost currency, not the commodity
Registering the unknown under the weight currency also fixes the count
rule, which now collides with a plain elided posting in that same
currency exactly as beancount's InterpolationError does.
Only a PER-UNIT factor is inverted. A total cost `{{600 USD}}`, a total
price `@@`, a zero factor, an empty `{}` and a compound `{a # b}` are
refused with a reason naming the shape: the first four contribute a
weight that does not vary with the units at all, and the compound form
admits two roots. These MUST error rather than be left unfilled --
validate_transaction_balance deliberately skips a transaction containing
an unfilled posting, on the documented assumption that interpolation
already reported the failure, so a silent skip accepts an unbalanced
transaction. An early draft did exactly that and turned four erroring
cases green.
Not copied from beancount, which crashes on much of this space rather
than diagnosing it: `TypeError: bad operand type for abs(): 'type'` on a
total or zero cost, and `AssertionError: Internal error; residual
currency different than missing currency` on a zero price. The rule here
is derived from the weight algebra in `cost_number_weight_generic`, and
`solved_units_reproduce_the_canonical_cost_weight` guards the inverse
against that canonical so the two cannot drift.
Value oracle over the 560-file comparable corpus: 4 divergences -> 3,
the one removed being test-cases_IncompleteInputs.UnitsMissingNumberWithCost
(`beancount=2 rledger=None`). No new divergence, same eligibility.
All 7 new tests were confirmed to fail against the pre-fix logic.
Closes #1911
The rustdoc build denies warnings, and is not something `cargo test` catches. Describe the private UnitsWeight cases inline instead of linking to them.
Interpolation solves one unknown per currency group, so two elided
postings are only ambiguous when they compete for the SAME residual. The
E3002 validator built a per-currency map and then summed across every
group, rejecting postings that were in different currencies entirely:
2010-05-28 * "two elided postings, different currencies"
Assets:A USD
Assets:B EUR
Assets:C -600.00 USD
Assets:D -50.00 EUR
was E3002 Transaction has 2 postings with missing amounts
now A = 600.00 USD, B = 50.00 EUR (what beancount does, and what
rledger's own interpolate already computed)
The grouping was also never populated. The currency hint came from
`.and_then(|u| u.as_amount())`, which is None for exactly the partial-units
postings the rule is about, so every unknown landed in one bucket and the
map had at most one meaningful key.
The grouping is no longer re-derived here. `elided_unknown_groups` is new
in rustledger-booking and is what `interpolate` itself solves by, so the
pre-booking diagnostic and the solver cannot disagree. That matters more
than usual since #1911: a per-unit cost redenominates the weight, so
`HOOL {300.00 USD}` is an unknown in USD, not in HOOL, and no amount of
looking at the posting's units reveals that.
A fully-elided posting keeps the old strictness. It has no currency of its
own, so it can absorb any residual including one another unknown is
claiming; beancount refuses that combination outright
("CategorizationError") and so do we. One bare posting alone is still
fine, including when it splits across several currencies.
Checked against beancount on nine shapes covering both elided kinds, bare
postings, cost-carrying postings, same-group and disjoint-group pairs:
all nine now agree, where two disagreed before. Booked VALUES also match,
including the multi-currency split.
No corpus movement: over 758 files not one changes accept/reject status,
and the value oracle is unchanged at 3. The shape simply does not occur
there, so the corpus is evidence of no regression rather than evidence
for the fix; the synthetic cases and unit tests carry that.
Both halves were confirmed to fail when neutered - restoring the
total-count rule fails the validator test, and grouping by the units
currency fails the drift guard.
Closes #1914
Contributor
📊 Benchmark Results
vs main branch (2026-08-01)
What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes E3002 (“multiple interpolation unknowns”) to be enforced per interpolation currency group (the same grouping the booking/interpolation solver uses), instead of incorrectly summing unknowns across all groups and rejecting valid multi-currency transactions.
Changes:
- Update
rustledger-validate’s E3002 check to userustledger_booking::elided_unknown_groups(txn)and enforce “≤1 unknown per group”, while keeping fully-elided (“unassigned”) postings strict when combined with any other unknown. - Introduce a canonical, solver-aligned unknown grouping API (
UnknownGroup+elided_unknown_groups) inrustledger-bookingto prevent validator/solver drift. - Add regression tests for the multi-currency E3002 behavior and an end-to-end loader pipeline test for the cost-weight currency solve path.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rustledger-validate/src/validators/transaction.rs | Switch E3002 enforcement to canonical per-group counting (including strict handling of unassigned unknowns) and improve diagnostic messaging. |
| crates/rustledger-validate/src/lib.rs | Add regression tests covering “two elided postings in different currencies OK” and “same currency still rejected”. |
| crates/rustledger-loader/tests/loader_test.rs | Add end-to-end test that the full load→interpolate→book→validate pipeline books units-elided-with-cost correctly. |
| crates/rustledger-booking/src/lib.rs | Re-export canonical unknown grouping surface (UnknownGroup, elided_unknown_groups) for cross-crate use by validators. |
| crates/rustledger-booking/src/interpolate.rs | Implement canonical unknown grouping and ensure interpolation/validation share the same weight-currency classification to avoid drift. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
|
| Metric | Result |
|---|---|
| Check Compatibility | 100% |
| BQL Compatibility | 93% |
| 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 - Files: ~800 test files downloaded from 10+ beancount repositories
Full results available in workflow artifacts
This was referenced Aug 1, 2026
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
Interpolation solves one unknown per currency group, so two elided postings are only ambiguous when they compete for the same residual. The E3002 validator built a per-currency map and then summed across every group.
E3002 Transaction has 2 postings with missing amounts; at most one is allowedA = 600.00 USD,B = 50.00 EURThat is what beancount does, and what rledger's own
interpolatealready computed — the validator was rejecting a transaction the solver would have handled correctly.The grouping was never actually populated
The currency hint came from
.and_then(|u| u.as_amount()), which returnsNonefor exactly the partial-units postings this rule is about. So every unknown landed in theNonebucket and the map had at most one meaningful key. The grouping was not merely discarded by the sum; it was never there.Not re-derived any more
elided_unknown_groupsis new inrustledger-bookingand is the same groupinginterpolatesolves by — both go through the sameunits_weightclassifier. Per the Canonical-Function Discipline in CLAUDE.md, the pre-booking diagnostic and the solver now cannot disagree.This matters more than usual since #1911: a per-unit cost redenominates the weight, so
HOOL {300.00 USD}is an unknown in USD, not in HOOL. Nothing about the posting's units reveals that, which is precisely why a hand-rolled grouping in the validator drifts.What stays strict
A fully-elided posting (
Assets:Cash, no amount at all) has no currency of its own, so it can absorb any residual — including one another unknown is claiming. beancount refuses that combination outright (CategorizationError) and so do we. One bare posting alone is still fine, including when it splits across several currencies.Verification
Checked against beancount on nine shapes — both elided kinds, bare postings, cost-carrying postings, same-group and disjoint-group pairs:
All nine agree now; two disagreed before. Booked values also match beancount, including the multi-currency split where one bare posting becomes two.
test_e3002_two_missing_in_different_currencies_ok; grouping by the units currency fails the drift guardelided_groups_agree_with_what_interpolate_solves.-D warnings.rustdoc -D warningsclean.Closes #1914