Skip to content

fix(report): keep journal order when --limit is given - #2123

Merged
robcohen merged 1 commit into
mainfrom
fix/journal-limit-order
Aug 24, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/journal-limit-order

Conversation

@robcohen

Copy link
Copy Markdown
Member

Closes #2122.

--limit N reached the most recent N with .rev().take(n) and wrote the result straight out, so the rows were reversed as well as selected:

$ rledger report ledger.beancount journal
first, second, third, fourth

$ rledger report ledger.beancount journal --limit 2
fourth, third        # reversed

Taking the last N is a fair reading of --limit — "the most recent N" is usually what you want. Printing them backwards is not, and tail does not either. The selection was right; the presentation was wrong.

sort_by_key is stable, so the same reversal also undid the file order of same-date transactions. That is the order booking uses (#2093), so a limited journal disagreed with the ledger about which of two same-day entries came first — which is why this is more than cosmetic.

split_off(len - n) takes the tail without reversing it. All three output formats share the one selection point, so all three are fixed together; I checked text, CSV and JSON.

The test asserts a property, not a transcript

It requires the limited output to be the tail of the unlimited output, unchanged, for every limit from 1 to the entry count. A test pinning literal expected output would pass just as happily on a reversed list with the expectation written backwards — that is roughly how this survived.

Verified it fails against the old code, on the tie rather than the obvious case:

assertion `left == right` failed: --limit 2 must be the tail of the unlimited report, in the same order
  left: ["same-day B", "same-day A"]
 right: ["same-day A", "same-day B"]

Provenance

Found by variant analysis from #2115 — the same shape one layer up: .rev() over a stably sorted list, reversing the tiebreak along with the sort key. I swept the other .rev() sites at the same time; report_cmd/mod.rs (string truncation, rev → take → rev back), the LSP edit appliers and the undo-log replay are all the correct back-to-front idiom.

Independent of #2119, #2120 and #2121 — branched from main and touches only the report path.

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

`--limit N` reached the most recent N with `.rev().take(n)` and wrote the
result straight out, so the rows came out reversed as well as selected: the
unlimited journal reads oldest-first and adding a limit silently flipped it
newest-first. Taking the last N is a fair reading of `--limit`; printing
them backwards is not, and `tail` does not either.

`sort_by_key` is stable, so the same reversal also undid the file order of
same-date transactions. That is the order booking uses (#2093), so a limited
journal disagreed with the ledger about which of two same-day entries came
first -- not only cosmetic.

`split_off` takes the tail without reversing it. All three output formats
share the selection, so all three are fixed at once, and all three were
checked.

The test asserts the PROPERTY -- limited output is the tail of unlimited
output, unchanged -- across every limit from 1 to the entry count, rather
than pinning a transcript. A transcript test passes just as happily on a
reversed list with the expectation written backwards. Against the old code
it fails on the same-date pair: got ["same-day B", "same-day A"], want
["same-day A", "same-day B"].

Found by variant analysis from #2115, which was this same shape -- `.rev()`
over a stably sorted list, reversing the tiebreak along with the sort key --
in the booking path.

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

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

Fixes report journal --limit N to preserve the selected entries’ original order.

Changes:

  • Uses split_off to retain chronological and same-date file order.
  • Adds regression coverage for all limits.
  • Documents the fix in the changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
crates/rustledger/tests/cli_commands_test.rs Verifies limited output preserves order.
crates/rustledger/src/cmd/report_cmd/journal.rs Corrects limited journal selection order.
CHANGELOG.md Documents the journal ordering fix.

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

@codecov

codecov Bot commented Aug 24, 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

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 49.9ms ± 4.8ms 837.0ms ± 7.2ms 16.8x 55.2 MB / 50.0 MB
investment 72.2ms ± 4.9ms 3125.2ms ± 34.7ms 43.3x 59.1 MB / 58.1 MB
fifo 70.6ms ± 3.1ms 15951.0ms ± 241.0ms 225.8x 58.9 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 48.5ms ± 4.9ms 49.9ms ± 4.8ms +2.9% ✅ within noise 55.1 MB → 55.2 MB (+0.2%)
investment 70.3ms ± 2.8ms 72.2ms ± 4.9ms +2.7% ✅ within noise 59.1 MB → 59.1 MB (+0.1%)
fifo 69.4ms ± 2.8ms 70.6ms ± 3.1ms +1.7% ✅ within noise 61.1 MB → 58.9 MB (-3.6%)

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

@robcohen

Copy link
Copy Markdown
Member Author

Self-review: checked the boundaries, since split_off panics if the index exceeds the length and the old .take(n) never could.

--limit result
0 empty, no panic (old behavior too)
1 fourth — the last entry
4 (= count) all four, in order
99 (> count) all four, in order — saturating_sub clamps to 0

Only 99 changes meaning versus the old code, and that is the fix: it previously returned all four reversed.

@robcohen
robcohen merged commit c9f6bb6 into main Aug 24, 2026
50 checks passed
@robcohen
robcohen deleted the fix/journal-limit-order branch August 24, 2026 10:38
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.

[Bug]: report journal --limit N reverses the output order, including same-date ties

2 participants