Skip to content

test(returns): messy-imported-ledger suite (#1850 §6, §5) - #1853

Merged
robcohen merged 2 commits into
mainfrom
feat/returns-messy-ledger-tests
Jul 23, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/returns-messy-ledger-tests

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Adds #1850 §6 — a messy-imported-ledger test suite proving the net-units returns capability end-to-end — plus the §5 edge cases and a §2 doc correction.

⚠️ Stacked on #1852 → #1851. Base branch is feat/returns-by-group-partial; review #1851 and #1852 first. The diff here is only the new test file + a doc-comment fix. Retarget down the stack as each parent merges.

What it tests

The suite (crates/rustledger-query/tests/messy_returns_test.rs) drives the shared scope_returns / scopes_returns composition — the exact path the CLI report returns and the WIT component session.returns take — so it also pins real PriceDatabase price resolution, not just the engine in isolation.

Case Asserts
Over-sell of an empty-cost {} lot net −5, valued at market (−600), tolerated
Missing opening buy (importer started mid-history) short net position, valued at market
Clean empty-cost {} reduction net units valued identically (no lot-matching)
Falling prices a real negative money- & time-weighted rate
Near-total loss defined large-negative rate, no crash
Missing intermediate / late-first price MWR computes; TWR degrades to None, not an error (§5 policy)
Per-scope isolation one elided-in-scope scope fails alone (UnbookedInput), a clean scope computes — over one shared accumulation

Where we exceed beangrow

beangrow crashes on falling prices / negative returns ("complex rate of return", beangrow#4 / #16). Our Newton+Brent xirr returns a genuine Option<f64> — the falling_prices_… and near_total_loss_… cases lock that in as a documented divergence.

Also (§2 doc fix)

Corrects the scope_returns / scopes_returns doc comments, which still carried the #1849 strict-clean language now contradicted by the net-units model: cost-basis/lot errors are tolerated, and UnbookedInput is per-scope, not a whole-ledger abort (the scopes_are_isolated_… test proves it).

Verification

  • full rustledger-query suite (170 lib + 402 + the 7 new tests + others) — green
  • clippy @1.97.0, rustdoc -D warnings, rustfmt, typos, gitleaks — clean

Remaining #1850 increments

§7 — docs/commands/report.md model docs (the last piece).

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner July 23, 2026 02:14
@robcohen
robcohen force-pushed the feat/returns-by-group-partial branch from 8debb86 to 6c83db0 Compare July 23, 2026 13:12
@robcohen
robcohen force-pushed the feat/returns-messy-ledger-tests branch from f5b16b4 to aa666f4 Compare July 23, 2026 13:12
@robcohen
robcohen force-pushed the feat/returns-by-group-partial branch from 6c83db0 to d115ff6 Compare July 23, 2026 13:58
@robcohen
robcohen force-pushed the feat/returns-messy-ledger-tests branch from aa666f4 to 23bc54f Compare July 23, 2026 13:59
@robcohen
robcohen force-pushed the feat/returns-by-group-partial branch from d115ff6 to c19aa6d Compare July 23, 2026 14:47
Base automatically changed from feat/returns-by-group-partial to main July 23, 2026 17:32
robcohen and others added 2 commits July 23, 2026 17:33
#1850 §6 (with §5 edge cases): a suite exercising net-units returns over the
imperfect/imported ledgers this effort targets, through the SHARED
scope_returns/scopes_returns composition (the exact path the CLI and the WIT
component take), so it also pins real PriceDatabase price resolution.

Cases (crates/rustledger-query/tests/messy_returns_test.rs):
- over-sell of an empty-cost {} lot → net −5, valued at market (−600), tolerated;
- missing opening buy (importer started mid-history) → short net position;
- clean empty-cost {} reduction → net units valued identically;
- falling prices → a real NEGATIVE money- and time-weighted rate (beangrow
  CRASHES here — beangrow#4 / #16; our Newton+Brent xirr returns Option<f64>);
- near-total loss → defined large-negative rate, no crash;
- missing intermediate price (late first price) → MWR computes from cash flows +
  priced terminal, TWR degrades to None (not an error) — the §5 missing-price
  policy;
- per-scope isolation: an elided in-scope scope fails alone (UnbookedInput) while
  a clean scope computes, over one shared accumulation (what --by-group relies on).

Also corrects the scope_returns/scopes_returns doc comments (§2), which still
carried the #1849 strict-clean language — now contradicted by the net-units
model: cost-basis/lot errors are tolerated, and UnbookedInput is PER-SCOPE, not a
whole-ledger abort.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
…ct (review 2)

Second review pass doc drift, query composition root:
- scope_returns summary said "from a booked, pad-expanded stream" -> interpolated
  (booking not required; net units valued at market).
- scope_returns/scopes_returns # Errors described UnbookedInput as firing only for
  an in-scope elided holding; the elided-external-leg fix also fires it for an
  external boundary leg (unknown cash flow), so both now name BOTH shapes.

Doc-only; no behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 23, 2026 17:34
@robcohen
robcohen force-pushed the feat/returns-messy-ledger-tests branch from 23bc54f to f0b148a Compare July 23, 2026 17:34
@github-actions

github-actions Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.1ms ± 0.7ms 23.7 MB
beancount 778.8ms ± 5.8ms 50.7 MB
Comparison 21.6x faster 2.1x less

vs main branch (2026-07-23)

Metric main this PR Change
Time 36.8ms 36.1ms -1.9% ✅
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

Adds an end-to-end “messy imported ledger” test suite for the returns feature (#1850 §6 + §5 edge cases), validating that net-units valuation computes robust returns even when cost-basis/lot-matching would fail, and updates scope_returns / scopes_returns docs to reflect the net-units + per-scope error isolation model.

Changes:

  • Introduces messy_returns_test.rs covering oversells/short positions, falling prices (negative returns), near-total loss, missing intermediate prices (TWR → None), and per-scope isolation of UnbookedInput.
  • Updates rustledger-query returns composition docs to clarify that booking/lot-matching is not required, cost-basis/lot errors are tolerated, and both MissingPrice and UnbookedInput are per-scope in multi-scope evaluation.

Reviewed changes

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

File Description
crates/rustledger-query/tests/messy_returns_test.rs New integration-style tests exercising shared scope_returns/scopes_returns composition over imperfect ledgers, including negative-return and missing-price edge cases.
crates/rustledger-query/src/returns.rs Doc comment corrections aligning the public composition helpers with the net-units valuation model and per-scope error isolation behavior.

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

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

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

@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit a7c0402 into main Jul 23, 2026
92 of 96 checks passed
@robcohen
robcohen deleted the feat/returns-messy-ledger-tests branch July 23, 2026 18:10
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