Repository navigation
docs(core): cross-file booking order is not Python's, on purpose - #2197
Conversation
`sort_directives` claimed its stable sort matched Python's
`(date, type_priority, lineno)`. That holds inside one file, where parse
order and line number rise together. Across `include`s it does not, and
`booking_sort_key` -- the doc that calls itself the single source of the
booking order -- repeated the same claim.
Python compares line numbers taken from DIFFERENT files, so a directive
on line 1 of a file included second sorts ahead of one on line 5 of a
file included first. This sort keeps include order.
Measured, not assumed. Against beancount 3.2.3 on the new fixture --
one buy at 10.00 on line 5 of the first included file, one at 20.00 on
line 1 of the second, then a FIFO sale of one unit:
include first,second beancount -10.00 rustledger -20.00
include second,first beancount -10.00 rustledger -10.00
A 2x difference in reported gain, decided by which file a buy lives in
and what line it sits on, with no error or warning either side.
Include order is kept. Ordering two directives by comparing a line
number from one file against a line number from a different file is not
a fact about the ledger: adding a comment to one file silently changes
which lot a sale in another file consumes. Beancount's rule is not
purely line-number driven either -- directives sharing a line number
across files fall back to include order through its own stable sort.
Nothing pinned this in either direction. The three tests that exercise
this ordering do not mention a file or an include, and the corpus cannot
reach it: of 761 `.beancount` files, one contains a single `include` and
none contains two, so no corpus file can express the shape. Hence an
in-tree fixture.
The test is verified to fail: dropping `file_id` from
`canonical_sort_key` so the span offset decides across files produces
-10.00 -- beancount's answer exactly, not merely a different one.
Closes #2149
There was a problem hiding this comment.
🟢 Approval recommended
Changes are documentation/tests/fixtures only and the new test clearly pins the intended behavior with low risk to production code paths.
Pull request overview
This PR documents and pins rustledger’s deliberate choice to keep include/parse order (not Python beancount’s cross-file lineno ordering) when booking same-date directives, because the difference can materially affect realized gains in FIFO booking across includes.
Changes:
- Corrects/clarifies booking-order documentation (
sort_directives/booking_sort_key) to explicitly describe the cross-includedivergence and rationale. - Adds an in-tree two-include fixture that makes the divergence observable in realized gains.
- Adds an integration test that pins the include-order behavior and records the divergence in the compatibility reference docs.
File summaries
| File | Description |
|---|---|
crates/rustledger-core/src/directive.rs |
Updates rustdoc for sort_directives and booking_sort_key to accurately describe cross-include ordering and reference the pinned test/docs. |
crates/rustledger-loader/tests/cross_file_booking_order.rs |
Adds an integration test that loads the fixture and asserts realized gains reflect include order. |
docs/reference/compatibility.md |
Documents the deliberate divergence (“Cross-File Booking Order”) with an illustrative example and fixture/test references. |
tests/fixtures/cross-file-order/ledger.beancount |
Adds a top-level fixture ledger with two includes and a sale that makes ordering observable via realized gains. |
tests/fixtures/cross-file-order/first.beancount |
Adds the “buy-at-10” included file with an intentionally later line number. |
tests/fixtures/cross-file-order/second.beancount |
Adds the “buy-at-20” included file with an intentionally early line number. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Review nit, in both places the sentence appears.
|
Fixed in f797b1f — "nor" in both places. |
Closes #2149.
The wrong claim
sort_directivessaid its stable sort matched Python's(date, type_priority, lineno). That holds inside one file, where parse order and line number rise together. Acrossincludes it does not — andbooking_sort_key, the doc comment that calls itself the single source of the booking order, repeated it.Python compares line numbers taken from different files, so a directive on line 1 of a file included second sorts ahead of one on line 5 of a file included first. This sort keeps include order.
Measured against real beancount
Not taken from the issue — reproduced against beancount 3.2.3. One buy at 10.00 on line 5 of the first included file, one at 20.00 on line 1 of the second, then a FIFO sale of one unit:
Swapping the includes pins each rule down: beancount is invariant because it re-sorts by line number; ours tracks how the ledger was assembled. A 2x difference in reported gain, decided by which file a buy lives in and what line it happens to sit on, with no error or warning either side.
Which rule stands
Include order, deliberately. Ordering two directives by comparing a line number from one file against a line number from a different file is not a fact about the ledger: adding a comment to one file silently changes which lot a sale in another file consumes. Include order at least reflects how the author assembled the ledger.
Beancount's rule is not purely line-number driven either — directives sharing a line number across files fall back to include order through its own stable sort. So this is not "match upstream or not", it is choosing between two tiebreaks neither of which is a fact.
Why nothing caught it
test_sort_directives_by_date,test_sort_directives_by_type_same_date,test_sort_directives_pad_before_balance) do not mention a file or aninclude..beancountfiles, one contains a singleincludeand none contains two. The divergence needs two included files with same-date directives at differing line numbers, so no corpus file can express the shape. This is not a gap the corpus happens to miss — it is one it cannot currently express, which is why the fixture ships in-tree.What this changes
No behavior change. Both doc comments corrected, an in-tree fixture, a test pinning the rule, and an entry in
docs/reference/compatibility.mdalongside the other deliberate divergences.The test is verified to fail: dropping
file_idfromcanonical_sort_keyso the span offset decides across files produces-10.00— beancount's answer exactly, not merely a different one. So the control lands on the real alternative rather than on noise.Workspace green, clippy 1.97 clean, fmt/typos/rustdoc clean,
corpus_parityclean against a freshly built wasip2 component.🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr