Skip to content

docs(core): cross-file booking order is not Python's, on purpose - #2197

Merged
robcohen merged 2 commits into
mainfrom
docs/cross-file-booking-order
Aug 31, 2026
Merged

robcohen merged 2 commits into
mainfrom
docs/cross-file-booking-order

Conversation

@robcohen

Copy link
Copy Markdown
Member

Closes #2149.

The wrong claim

sort_directives said its stable sort matched Python's (date, type_priority, lineno). That holds inside one file, where parse order and line number rise together. Across includes it does not — and booking_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:

include order beancount rustledger
first, second -10.00 -20.00
second, first -10.00 -10.00

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

  • The three tests that exercise this ordering (test_sort_directives_by_date, test_sort_directives_by_type_same_date, test_sort_directives_pad_before_balance) do not mention a file or an include.
  • The compatibility corpus cannot reach it: of 761 .beancount files, one contains a single include and 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.md alongside the other deliberate divergences.

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. So the control lands on the real alternative rather than on noise.

Workspace green, clippy 1.97 clean, fmt/typos/rustdoc clean, corpus_parity clean against a freshly built wasip2 component.

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

`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
@robcohen
robcohen requested a review from a team as a code owner August 31, 2026 03:10
Copilot AI lite review requested due to automatic review settings August 31, 2026 03:10

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.

🟢 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-include divergence 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.

Comment thread docs/reference/compatibility.md Resolved
@codecov

codecov Bot commented Aug 31, 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 Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

🔴 Slower than base on: simple

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 48.0ms ± 4.6ms 798.9ms ± 4.1ms 16.6x 55.1 MB / 50.1 MB
investment 69.3ms ± 4.2ms 3043.5ms ± 24.4ms 43.9x 59.2 MB / 58.1 MB
fifo 66.8ms ± 2.1ms 15767.9ms ± 461.5ms 236.1x 61.0 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 45.5ms ± 0.8ms 48.0ms ± 4.6ms +5.6% 🟡 slower 54.9 MB → 55.1 MB (+0.3%)
investment 67.2ms ± 1.1ms 69.3ms ± 4.2ms +3.1% ✅ within noise 59.1 MB → 59.2 MB (+0.2%)
fifo 68.7ms ± 4.0ms 66.8ms ± 2.1ms -2.7% ✅ within noise 63.0 MB → 61.0 MB (-3.1%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 2.1ms, investment 2.0ms, fifo 2.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

@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

Review nit, in both places the sentence appears.
@robcohen

Copy link
Copy Markdown
Member Author

Fixed in f797b1f — "nor" in both places.

@robcohen
robcohen merged commit 5fd22cc into main Aug 31, 2026
64 of 65 checks passed
@robcohen
robcohen deleted the docs/cross-file-booking-order branch August 31, 2026 13: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.

Same-date directives from different included files book in a different order than beancount, changing realized gains

2 participants