Skip to content

fix(booking): infer augmenting empty-{} cost from the residual - #1706

Closed
yurenju wants to merge 1 commit into
rustledger:mainfrom
yurenju:claude/happy-spence-215813
Closed

yurenju wants to merge 1 commit into
rustledger:mainfrom
yurenju:claude/happy-spence-215813

Conversation

@yurenju

@yurenju yurenju commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

An augmenting posting with an empty cost spec {} was accepted by bean-check but rejected by rledger check — rledger now infers the lot's per-unit cost from the transaction balance, matching beancount.

Root cause

The pipeline books first, then interpolates, but interpolate only ever filled missing units, never a cost number. An augmenting {} was counted as one cost-unknown and then never solved — 1000 USD {} + -900 EUR was accepted with no cost basis (the issue's empty cost_number), so a later -100 USD {} reducing that lot could no longer match a cost-bearing lot and became a spurious second unknown beside the elided PnL leg (the 2 unknowns error). Fixing the augmentation fixes the reduction by cascade.

The fix

interpolate.rs only: for an empty, price-less {} that is the sole unknown in its cost currency group, infer the per-unit cost from the residual (total = -residual · signum(units), stored as PerUnitFromTotal to preserve precision). The structural rule (at most one unknown per currency) is unchanged — the unknown is now solved, not left empty. A priced {} still defers to booking-time lot matching (keeps #1026). Compat-policy bucket 1 — not a breaking change.

Before: error[BOOK]: interpolation failed: ... (2 unknowns). After: ✓ No errors found, the augmenting lot books at 0.90 EUR (matches bean-query) and PnL solves to -5 EUR. Regression tests: test_interpolate_augmenting_empty_cost_inferred_from_residual · test_augmenting_empty_cost_then_reduce. Green: booking / loader / validate / query / core · clippy -D warnings (booking) · fmt.

Note

Full disclosure: I don't write Rust — this fix was authored entirely by Claude (Claude Code), and I'm relaying it. The one judgement call worth a close look is the "no price" heuristic used to tell an augmentation from a reduction at interpolation time. Happy to iterate on anything.

Closes #1705

🤖 Generated with Claude Code

An augmenting posting with an empty cost spec `{}` was accepted by
bean-check but rejected by `rledger check` as "2 unknowns". Beancount
books, then interpolates the single remaining unknown, so `1000 USD {}`
against one `-900 EUR` leg is booked at the inferred 0.90 EUR/unit.

Root cause: the pipeline books before interpolating, but `interpolate`
only ever filled missing *units*, never a cost *number* — an augmenting
`{}` was counted as one unknown and then never solved. The lot entered
inventory with no cost basis, so a later `-100 USD {}` reducing it
could no longer match a cost-bearing lot and became a spurious second
unknown beside the elided PnL leg. Fixing the augmentation fixes the
reduction by cascade.

Fix (interpolate.rs only): for an empty, price-less `{}` that is the
sole unknown in its cost currency group, infer the per-unit cost from
the residual (stored as `PerUnitFromTotal` to preserve precision). A
priced `{}` still defers to booking-time lot matching (keeps rustledger#1026).
Compat-policy bucket 1 — not a breaking change.

Closes rustledger#1705

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@yurenju
yurenju requested a review from a team as a code owner July 3, 2026 13:35
@CLAassistant

CLAassistant commented Jul 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@robcohen robcohen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the root cause and the core mechanism here are exactly right, and validated: I ran this branch against a 17-fixture matrix cross-checked with real bean-check 3.2.3 (matrix + methodology in the #1705 plan comment). The residual solve (e01) and the NONE-method case both book {0.90 EUR} identical to beancount, and constructing PerUnitFromTotal via BookedCost::new directly is the right shape (exact total preserved for the BigDecimal residual tier).

Four things before this can land — one merge blocker, three semantic divergences the matrix caught:

Blocker: #1708 (merged after this branch) makes the solver dead code

Main now has an interim guard in book() that errors on number-less augmenting cost specs — and the pipeline books before interpolating, so on a rebase every path this PR fixes errors in the guard before your solver runs (verified: all 11 augmentation fixtures die with EmptyCostAugmentation on the merge). The rebase should remove the guard (this PR supersedes it) — but see the next two points first, because two shapes the guard currently catches must keep erroring.

1. posting.price.is_none() skip diverges from beancount

Beancount solves from the residual even when a price annotation is present — including when they disagree:

  • 1000 USD {} @ 0.90 EUR vs -900 EUR → bean books {0.90}; this PR books uncosted (the exact silent corruption #1705 is about).
  • 1000 USD {} @ 0.90 EUR vs -950 EUR → bean books {0.95} — residual wins, silently; this PR books uncosted.

Suggest dropping the price.is_none() condition. (The price annotation still feeds implicit-price directives; it just never participates in cost solving — also verified: {} @ price + auto-posting is a 2-unknowns REJECT in bean, price never substitutes as a weight.)

2. Negative solved cost must reject

1000 USD {} against +900 EUR → bean-check: Cost is negative error; this PR books a {-0.90 EUR} lot. Needs an explicit rejection.

3. Ambiguous residual currency must reject

1000 USD {} + -500 EUR + -100 GBP → bean-check: Failed to categorize posting error; this PR picks a currency and books a wrong-valued lot. The solve should require the cost currency to be unambiguous (spec-given, or a unique candidate).

Also

  • Please add the matrix fixtures as committed differential corpus (tests/compatibility/files/interpolation/, same pattern as costs/) — happy to hand you the 17 files; they encode all of the above as regression tests including the issue's original file end-to-end.
  • The two rejects (negative, ambiguous) can reuse or adapt the #1708 error path rather than inventing new wording.

With those four addressed this is exactly the fix from the validated plan — looking forward to landing it.

@robcohen

robcohen commented Jul 3, 2026

Copy link
Copy Markdown
Member

Fixtures ready to cherry-pick: branch chore/1705-edge-fixtures — one commit adding all 17 as tests/compatibility/files/interpolation/ (committed differential corpus, gitignore exception included, bean-check 3.2.3 cross-checked). git cherry-pick 14d12283e534909e89cab8669fd019a6ea95bedd onto your branch and the compat suite covers every case from the review.

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.77778% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-booking/src/interpolate.rs 96.22% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

robcohen added a commit that referenced this pull request Jul 3, 2026
…etes #1706) (#1709)

* fix(booking): infer augmenting empty-{} cost from the residual

An augmenting posting with an empty cost spec `{}` was accepted by
bean-check but rejected by `rledger check` as "2 unknowns". Beancount
books, then interpolates the single remaining unknown, so `1000 USD {}`
against one `-900 EUR` leg is booked at the inferred 0.90 EUR/unit.

Root cause: the pipeline books before interpolating, but `interpolate`
only ever filled missing *units*, never a cost *number* — an augmenting
`{}` was counted as one unknown and then never solved. The lot entered
inventory with no cost basis, so a later `-100 USD {}` reducing it
could no longer match a cost-bearing lot and became a spurious second
unknown beside the elided PnL leg. Fixing the augmentation fixes the
reduction by cascade.

Fix (interpolate.rs only): for an empty, price-less `{}` that is the
sole unknown in its cost currency group, infer the per-unit cost from
the residual (stored as `PerUnitFromTotal` to preserve precision). A
priced `{}` still defers to booking-time lot matching (keeps #1026).
Compat-policy bucket 1 — not a breaking change.

Closes #1705

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* test: 17-fixture differential edge matrix for #1705 ({} cost solving)

Every fixture cross-checked against bean-check 3.2.3 (matrix in the
#1705 plan comment). Encodes: residual solve, currency/date-only specs,
2-unknown rejections, price-vs-residual precedence (residual wins),
negative-cost rejection, ambiguous-currency rejection, NONE method,
STRICT ambiguity, multi-lot FIFO reduce, missing-units sell-all.

For cherry-pick into #1706.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* fix(booking): complete empty-{} cost inference — price precedence, negative and ambiguous-currency rejection

Finishes #1706 (the solver core is @yurenju's work, preserved as the
parent commit) with the four items from its review, each pinned against
bean-check 3.2.3 via the 17-fixture matrix (committed here as
tests/compatibility/files/interpolation/):

- Remove the #1708 interim guard: it fired in book() before
  interpolation could run, making the solver dead code on rebase. Its
  book.rs test returns to not-an-error with the full history documented.
- Solve regardless of price annotations: beancount infers from the
  residual even when a price is present AND when they disagree
  ({} @ 0.90 with a 0.95 residual books {0.95} — verified, silent).
- Reject a negative solved cost (bean: "Cost is negative") instead of
  booking a negative-cost lot.
- Reject an ambiguous cost currency: when the spec names none and more
  than one candidate currency exists among complete cost-less postings,
  bean fails to categorize. Candidates exclude currencies owning their
  own missing-amount unknown (disjoint groups stay legal), and the check
  defers to the more specific unassigned+cost-unknown rejection when a
  fully-unassigned posting exists.

Matrix: 17/17 accept/reject parity with bean-check; solved values match
to the cent (0.90 inferred; 0.95 residual-over-price). The issue's
original file passes end-to-end.

Closes #1705
Closes #1706

Co-Authored-By: yurenju <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Yuren Ju <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
robcohen added a commit that referenced this pull request Jul 3, 2026
Patch release: augmenting empty-{} cost specs now infer the per-unit
cost from the transaction residual, matching beancount exactly (#1705,
reported and first implemented by @yurenju in #1706, completed in #1709
with bean-check-verified edge semantics: residual wins over a
disagreeing price annotation; negative and ambiguous-currency solves
are rejected). The #1708 interim guard is superseded. No wire or API
changes.

Workspace 0.20.0 -> 0.20.1; packages/mcp-server bumped to match.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen robcohen mentioned this pull request Jul 3, 2026
robcohen added a commit that referenced this pull request Jul 3, 2026
Patch release: augmenting empty-{} cost specs now infer the per-unit
cost from the transaction residual, matching beancount exactly (#1705,
reported and first implemented by @yurenju in #1706, completed in #1709
with bean-check-verified edge semantics: residual wins over a
disagreeing price annotation; negative and ambiguous-currency solves
are rejected). The #1708 interim guard is superseded. No wire or API
changes.

Workspace 0.20.0 -> 0.20.1; packages/mcp-server bumped to match.

Co-authored-by: Claude Opus 4.8 <[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.

[Compat]: augmenting posting with empty cost spec {} is not interpolated (rejected as "2 unknowns")

3 participants