Skip to content

fix: restore "not enough" phrasing for InsufficientUnits booking error - #749

Merged
robcohen merged 1 commit into
mainfrom
fix/issue-748-not-enough-units
Apr 11, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/issue-748-not-enough-units

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Fixes the pta-standards reduction-exceeds-inventory conformance regression introduced by #745.

PR #745 made the booking layer propagate BookingError::InsufficientUnits directly to the user instead of letting the validator's "Not enough units in ..." message win. The booking variant's Display string ("insufficient units: need X but only Y available") didn't contain "not enough", and the conformance test asserts on error_contains: ["not enough"]. The error itself is still correctly detected — only the wording diverged.

This PR aligns the booking layer's #[error(...)] to the validator's pre-existing phrasing verbatim. The account field is already on the variant (added in #745 for exactly this purpose), so no struct change is needed.

Before:

error[E4002]: insufficient units: need 15 but only 10 available

After:

error[E4002]: Not enough units in Assets:Stock: requested 15, available 10; not enough to reduce

Why option 1, not the bigger refactor

#748 outlined two fixes:

  1. Align the Display string (this PR).
  2. Standardize on a single shared InsufficientUnits type between rustledger-booking and rustledger-validate.

Option 2 is the right longer-term cleanup, but it's invasive and out of scope for a regression fix. Option 1 is one-line and pins behavior with a regression test. Option 2 should get its own tracking issue once this lands.

Regression test

Added test_insufficient_units_display_contains_not_enough in crates/rustledger-booking/src/book.rs. Asserts the rendered Display string contains:

  • the literal phrase "not enough" (the conformance assertion)
  • the account name
  • both the requested and available numbers

The test docstring references #748 so the next person to touch the variant understands why the exact phrasing is load-bearing. A doc comment on the InsufficientUnits variant itself also explains the constraint.

Test plan

  • cargo test -p rustledger-booking — all 76 tests pass (75 existing + 1 new)
  • cargo test --workspace --all-features — full suite green
  • cargo clippy --all-features --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean
  • pta-standards conformance run — verify reduction-exceeds-inventory flips back to passing once this lands

Closes #748

PR #745 made the booking layer propagate `InsufficientUnits` errors
directly to the user instead of letting them be silently swallowed and
re-emitted by the validator. That was the right architectural move, but
the booking variant's Display string ("insufficient units: need X but
only Y available") differed from the validator's pre-existing phrasing
("Not enough units in <account>: requested X, available Y; not enough
to reduce"), and the conformance suite (and downstream user tooling)
matches on "not enough".

Result: pta-standards `reduction-exceeds-inventory` regressed from
passing to failing on the error_contains assertion, even though the
error itself is still correctly detected.

Align the booking layer's Display to match the validator's wording
verbatim. The `account` field is already on the variant (added in #745
for exactly this purpose), so no struct change is needed.

Add a regression test that pins the Display string against the three
assertions that matter: contains "not enough", contains the account
name, contains both numbers. The test references #748 in its docstring
so the next person to touch the variant understands why the exact
phrasing is load-bearing.

Closes #748

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner April 11, 2026 01:31
Copilot AI review requested due to automatic review settings April 11, 2026 01:31

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

Restores the beancount/validator-compatible “not enough” phrasing for BookingError::InsufficientUnits in rustledger-booking, fixing a pta-standards conformance regression introduced when booking errors began propagating directly to users.

Changes:

  • Update BookingError::InsufficientUnits’s Display string to match the validator’s existing “Not enough units in …; not enough to reduce” wording (including the account name).
  • Add documentation explaining why the exact phrasing is load-bearing for conformance/tooling.
  • Add a regression test asserting the rendered error contains “not enough”, the account name, and requested/available numbers.

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

@robcohen
robcohen enabled auto-merge April 11, 2026 01:34
@codecov

codecov Bot commented Apr 11, 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

Tool Time Memory
rustledger 41.9ms ± 0.7ms 23 MB
beancount 799.6ms ± 9.4ms 50.2 MB
Comparison 19.1x faster 2.2x less

vs main branch (2026-04-10)

Metric main this PR Change
Time 45.8ms 41.9ms -8.5% 🟢
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

@robcohen

Copy link
Copy Markdown
Member Author

Filed #750 to track the broader unification (option 2 from #748). That's the longer-term cleanup; this PR remains scoped to the regression fix.

@robcohen
robcohen added this pull request to the merge queue Apr 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 100%
Full AST Match 98%
Total Files 784
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

Merged via the queue into main with commit 4021246 Apr 11, 2026
38 of 40 checks passed
@robcohen
robcohen deleted the fix/issue-748-not-enough-units branch April 11, 2026 01:42
robcohen added a commit that referenced this pull request Apr 11, 2026
Before this change, the same logical failure ("reduction exceeds
available inventory") was encoded as three separate error types with
three separate hand-written Display strings across three crates:

  - `rustledger_core::BookingError` — inventory-level, no account
  - `rustledger_booking::BookingError` — wraps core, adds account
  - `rustledger_validate::ValidationError` — hand-formatted from core

Twelve match arms across three crates all encoded the same 4-way
structure (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`,
`CurrencyMismatch`), and the three Display strings drifted apart —
that was the root cause of #748, where the booking layer's "insufficient
units: need X but only Y available" bypassed the validator's "Not enough
units in ..." and dropped the phrasing the pta-standards conformance
suite asserts on.

This commit unifies the user-facing wording into a single
`rustledger_core::AccountedBookingError { error, account }` type with
one canonical `Display` impl per variant, constructed via
`BookingError::with_account(self, account)`:

  - `rustledger-core`: add `AccountedBookingError` struct + Display +
    `BookingError::with_account` constructor. The raw `BookingError`
    still exists for `Inventory::reduce` (which doesn't know its own
    account), but every user-facing rendering goes through the new type.

  - `rustledger-booking`: collapse `BookingError::{InsufficientUnits,
    NoMatchingLot, AmbiguousMatch}` (3 variants) into a single
    `BookingError::Inventory(AccountedBookingError)` variant with
    transparent Display. `convert_core_booking_error` becomes a
    one-liner: `BookingError::Inventory(err.with_account(account))`.

  - `rustledger-validate`: stop hand-formatting inventory errors.
    Each match arm now calls `format!("{}", err.with_account(...))`
    and assigns the appropriate `ErrorCode`. ~15 lines of hand-rolled
    format strings deleted.

  - `rustledger`: `cmd/check.rs` `code_for` now dispatches on the
    inner `core::BookingError` variant of the unified
    `BookingError::Inventory` arm.

Net: 8 of 12 match arms eliminated, and no independent Display strings
remain that can drift apart. Adding a fifth booking failure variant
now means touching one crate, not three.

The #748 regression test from #749 still passes unchanged (it exercises
the new path via `BookingError::Inventory(core_err.with_account(...))`,
which is the same code path `rledger check` and the validator now use).

Canonical Display strings (locked by the test and by pta-standards):
  - InsufficientUnits: "Not enough units in {account}: requested {req},
    available {avail}; not enough to reduce"
  - NoMatchingLot:     "No matching lot for {currency} in {account}"
  - AmbiguousMatch:    "Ambiguous lot match for {currency}: {n} lots
    match in {account}"
  - CurrencyMismatch:  "Currency mismatch in {account}: expected {e},
    got {g}"

Closes #750

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
robcohen added a commit that referenced this pull request Apr 11, 2026
Before this change, the same logical failure ("reduction exceeds
available inventory") was encoded as three separate error types with
three separate hand-written Display strings across three crates:

  - `rustledger_core::BookingError` — inventory-level, no account
  - `rustledger_booking::BookingError` — wraps core, adds account
  - `rustledger_validate::ValidationError` — hand-formatted from core

Twelve match arms across three crates all encoded the same 4-way
structure (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`,
`CurrencyMismatch`), and the three Display strings drifted apart —
that was the root cause of #748, where the booking layer's "insufficient
units: need X but only Y available" bypassed the validator's "Not enough
units in ..." and dropped the phrasing the pta-standards conformance
suite asserts on.

This commit unifies the user-facing wording into a single
`rustledger_core::AccountedBookingError { error, account }` type with
one canonical `Display` impl per variant, constructed via
`BookingError::with_account(self, account)`:

  - `rustledger-core`: add `AccountedBookingError` struct + Display +
    `BookingError::with_account` constructor. The raw `BookingError`
    still exists for `Inventory::reduce` (which doesn't know its own
    account), but every user-facing rendering goes through the new type.

  - `rustledger-booking`: collapse `BookingError::{InsufficientUnits,
    NoMatchingLot, AmbiguousMatch}` (3 variants) into a single
    `BookingError::Inventory(AccountedBookingError)` variant with
    transparent Display. `convert_core_booking_error` becomes a
    one-liner: `BookingError::Inventory(err.with_account(account))`.

  - `rustledger-validate`: stop hand-formatting inventory errors.
    Each match arm now calls `format!("{}", err.with_account(...))`
    and assigns the appropriate `ErrorCode`. ~15 lines of hand-rolled
    format strings deleted.

  - `rustledger`: `cmd/check.rs` `code_for` now dispatches on the
    inner `core::BookingError` variant of the unified
    `BookingError::Inventory` arm.

Net: 8 of 12 match arms eliminated, and no independent Display strings
remain that can drift apart. Adding a fifth booking failure variant
now means touching one crate, not three.

The #748 regression test from #749 still passes unchanged (it exercises
the new path via `BookingError::Inventory(core_err.with_account(...))`,
which is the same code path `rledger check` and the validator now use).

Canonical Display strings (locked by the test and by pta-standards):
  - InsufficientUnits: "Not enough units in {account}: requested {req},
    available {avail}; not enough to reduce"
  - NoMatchingLot:     "No matching lot for {currency} in {account}"
  - AmbiguousMatch:    "Ambiguous lot match for {currency}: {n} lots
    match in {account}"
  - CurrencyMismatch:  "Currency mismatch in {account}: expected {e},
    got {g}"

Closes #750

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
robcohen added a commit that referenced this pull request Apr 11, 2026
Bumps the whole workspace from 0.11.0 to 0.12.0 (minor version bump
under pre-1.0 semver because the cycle since 0.11.0 includes
user-visible breaking changes that release-plz's auto-detection
missed).

release-plz auto-generated a v0.11.1 patch bump from the `fix:` /
`feat:` commits since 0.11.0. That's too conservative. The cycle
since 0.11.0 shipped the following breaking changes, all merged as
`refactor:` / `fix:` without the `!` conventional-commit marker:

- **`rustledger_booking::BookingError`** collapsed three variants
  (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`) into a
  single `Inventory(AccountedBookingError)` variant (#751). Out-of-tree
  consumers pattern-matching on the old variants break.

- **`rustledger_core::AccountedBookingError`** added as a new
  canonical user-facing type with `BookingError::with_account()` as
  the constructor (#751). Not a break on its own, but part of the
  unification.

- **`rustledger_lsp::handlers::diagnostics::all_diagnostics`**
  signature changed to take an additional `other_buffer_overlays:
  &[(u16, &[Spanned<Directive>])]` parameter for multi-buffer LSP
  overlays (#760 / #764).

- **`rustledger_lsp::handlers::diagnostics::validation_errors_to_diagnostics`**
  signature changed to take an owned `Vec<Spanned<Directive>>`
  instead of borrowed slices (#758). Optimizes a hot path but breaks
  the function's public signature.

- **`rustledger_loader::LoadError::IncludeCycle`** Display format
  rewritten to match Python beancount's `"Duplicate filename
  parsed: ..."` wording (#767). Substring-matching consumers break.

Under pre-1.0 semver, any breaking change should bump the minor
version (0.x → 0.(x+1)). So 0.11.0 → 0.12.0 is correct.

Highlights since 0.11.0:

### Breaking

- Unified `BookingError` under a single `Inventory` variant with one
  canonical `Display` impl, preventing the wording drift that
  produced #748 (#751).
- `LoadError::IncludeCycle` Display now begins with `"Duplicate
  filename parsed: "` to match beancount's wording for conformance
  (#767).
- LSP `all_diagnostics` and `validation_errors_to_diagnostics`
  signatures changed to support the multi-buffer overlay and avoid
  double-cloning directive lists (#758 / #764).

### Features

- LSP live overlay so in-memory buffer edits reflect in diagnostics
  before save, for both the current file (#685 / #758) and any other
  open buffer in a multi-file ledger (#760 / #764).
- Parser `Vec::with_capacity` hints with DoS caps on `source.len()`
  heuristics (#754).
- `working_inventories` cloning in the booking engine now only
  touches accounts actually referenced in the transaction, and
  dedupes repeated account references (#756).

### Fixes

- `InsufficientUnits` booking error wording restored to `"Not enough
  units in ..."` so pta-standards `reduction-exceeds-inventory`
  conformance assertion passes (#748 / #749).
- `noduplicates` plugin now hashes flag, tags, links, cost, and
  price in addition to date and narration — complete beancount
  `hash_entry` parity (#746 / #747).
- Parser test suite: 14 fake-coverage tests replaced with real tests
  that exercise the functions they claim to cover (#766).
- All Copilot inline review comments on #754-#763, #766, #767
  addressed in fix commits.
- Closed PRs #768 (dead incremental parser) and #769 (hallucinated
  simdutf8 API) — no code shipped from these.

### Internal

- Forbid-unsafe-code invariant now has a dedicated CI check and
  pre-push hook (#770).
- New pre-push `cargo check` hook to catch compile errors before PRs
  are opened (#770).

This commit supersedes release-plz's auto-generated v0.11.1 proposal
to produce the minor bump the breaking changes require.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.

Booking layer regression: InsufficientUnits message no longer contains 'not enough'

2 participants