Skip to content

refactor: single-source the booking-order comparator (LSP + validator) - #1734

Merged
robcohen merged 5 commits into
mainfrom
refactor/lsp-shared-pipeline
Jul 10, 2026
Merged

robcohen merged 5 commits into
mainfrom
refactor/lsp-shared-pipeline

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026

Copy link
Copy Markdown
Member

From the Phase-0 duplication sweep (P1, finalize-consumers family) — right-sized after investigation.

What the sweep flagged vs what investigation found

The sweep flagged the LSP as "re-implements the loader pipeline inline" (the #1648 pattern). Investigation showed the LSP's single-pass pipeline is a deliberate, documented approximation — the overlay model swaps per-buffer directive sets that process() (which ingests a raw LoadResult) cannot accept, and finalize's only value-touching step (normalize_prices) runs after Late validation in the loader too, so its absence from the LSP does not change diagnostics. Wholesale pipeline replacement would be a risky rewrite of an engineered trade-off for zero diagnostic delta — deliberately not done.

What WAS real duplication (fixed)

The booking-order comparator (date, priority, cost-reduction-last) existed three times: canonical rustledger_core::booking_sort_key, an inline tuple closure in LSP diagnostics, and a chained-cmp closure in the validator's sort (the sweep undercounted — the validator copy was found during implementation). Both copies now delegate to the canonical fn — booking-order changes can no longer silently skew LSP diagnostics or validation relative to the loader/booking engine.

Also

The LSP approximation doc now records the review verdict, names the canonical pipeline, and states the exact condition requiring revisit (any future finalize step that feeds validation) — greppable at the site.

Verified

  • Behavior identical (comparators were value-equal; this removes future drift, not present drift)
  • LSP 261 unit + protocol suites green, validator suites green, clippy 0, fmt clean

🤖 Generated with Claude Code

The booking order (date, priority, cost-reduction-last) had THREE
implementations: the canonical rustledger_core::booking_sort_key, an
inline tuple closure in LSP diagnostics, and a chained-cmp closure in
the validator's sort. Found by the Phase-0 duplication sweep (P1,
finalize-consumers family). Both copies now delegate to the canonical
fn, so a change to booking order cannot silently skew LSP diagnostics
or validation relative to the loader and booking engine.

Also strengthens the LSP pipeline-approximation doc with the sweep's
verdict: the LSP's single-pass pipeline (sort -> book -> all-plugins ->
validate) remains a DELIBERATE approximation of the loader's
interleaved phases — the overlay model swaps per-buffer directive sets
that process() (which ingests a raw LoadResult) cannot accept, and
finalize's only value-touching step (normalize_prices) runs AFTER Late
validation in the loader too, so its absence in the LSP does not change
diagnostics. The doc now names the canonical pipeline and the condition
under which the approximation must be revisited (any future finalize
step that feeds validation).

LSP 261+ tests and validator suites green; behavior identical (the
comparators were value-equal — this removes the possibility of future
drift, not present drift).

Co-Authored-By: Claude Fable 5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 16:56
Copilot AI review requested due to automatic review settings July 9, 2026 16:56
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33ms ± 0.2ms 23.7 MB
beancount 759.1ms ± 2.9ms 50.7 MB
Comparison 23x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 33ms -2.1% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

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

This PR removes duplicated implementations of the “booking order” comparator across the codebase by delegating to the canonical rustledger_core::booking_sort_key, ensuring consistent directive ordering between the loader/booking engine, validator, and LSP diagnostics.

Changes:

  • Validator sorting now compares directives using rustledger_core::booking_sort_key.
  • LSP diagnostics sorting now uses rustledger_core::booking_sort_key (cached-key sort) to match canonical booking order.
  • Expanded LSP documentation clarifying why its validation/processing pipeline is an intentional approximation of the loader pipeline and when it must be revisited.

Reviewed changes

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

File Description
crates/rustledger-validate/src/lib.rs Replaces a local chained comparator with the canonical booking_sort_key ordering.
crates/rustledger-lsp/src/handlers/diagnostics.rs Switches directive sort key to booking_sort_key and documents the deliberate pipeline approximation and revisit condition.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-validate/src/lib.rs Resolved
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
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
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 4 commits July 10, 2026 02:27
The comparator recomputed both full keys per comparison, paying the
O(postings) cost-reduction walk even on date mismatches where the old
then_with chain short-circuited. Decorate-sort-undecorate computes
each key once; both sorts are stable so equal-key source order is
unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-validate/src/lib.rs 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 81c39a5 into main Jul 10, 2026
43 of 44 checks passed
@robcohen
robcohen deleted the refactor/lsp-shared-pipeline branch July 10, 2026 09:13
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.

2 participants