Repository navigation
fix(plugin): long_short falls through cleanly when reduction lacks cost date - #1014
Merged
Merged
Conversation
…st date Closes #1010. ## Bug The `long_short` capital-gains classifier silently dropped the generic `Income:Capital-Gains` posting from a transaction whenever any reduction posting (cost+units+price) had `cost.date == None`. Trace: 1. `has_generic = true` → plugin enters the rewrite path 2. `reductions` is non-empty → past the early-return 3. The reduction loop's `if let Some(cost_date) = cost_date` skips gain accumulation when the date is missing → `short_gains` and `long_gains` stay at 0 4. `new_postings` is built by FILTERING OUT the matching generic postings 5. Both `short_gains != 0` and `long_gains != 0` are false → no :Short / :Long replacement is emitted 6. The synthesized transaction is pushed with the generic Income:Capital-Gains posting silently removed → unbalanced ## Fix Detect the condition early and fall through the entire transaction unchanged. Right after parsing `entry_date`: let any_missing_cost_date = reductions.iter().any(|p| { p.cost.as_ref() .and_then(|c| c.date.as_ref()) .and_then(|d| d.parse::<NaiveDate>().ok()) .is_none() }); if any_missing_cost_date { new_directives.push(directive); continue; } Matches the early-return pattern already used elsewhere in the plugin when classification is impossible (e.g., unparsable `entry_date`). ## Test Renamed and inverted `test_capital_gains_long_short_no_cost_date_drops_generic_posting` → `..._passes_through_unchanged`. Now asserts: - No :Short or :Long replacement is emitted (gains stay at 0) - The generic `Income:Capital-Gains` posting IS preserved - The transaction's full posting count is unchanged (3 → 3) Pre-fix the test pinned the buggy behavior with a comment noting "this is almost certainly a plugin bug." The PR that introduced the test (#1007) is referenced in the issue. ## Verified - 143/143 plugin integration tests pass - Lint OK, clippy clean Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a bug in the capital_gains_classifier::long_short native plugin where transactions could become unbalanced if any reduction posting had a missing/unparseable cost.date, by ensuring the plugin cleanly falls through without rewriting the transaction in that case.
Changes:
- Add an early “fall through unchanged” guard in
long_shortwhen any reduction lacks a parseable cost date (prevents silently dropping the generic gains posting). - Update/rename the regression test to assert the corrected pass-through behavior and preservation of the generic
Income:Capital-Gainsposting.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/rustledger-plugin/src/native/plugins/capital_gains_classifier.rs | Adds an early guard to skip rewriting when reductions don’t have parseable cost dates, preventing unbalanced output. |
| crates/rustledger-plugin/tests/native_plugins_test.rs | Updates the regression test to validate pass-through behavior and ensure the generic gains posting is preserved. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
📊 Benchmark Results
vs main branch (2026-05-06)
What do these numbers mean?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
|
| Metric | Result |
|---|---|
| Check Compatibility | 99% |
| BQL Compatibility | 88% |
| Full AST Match | 99% |
| Total Files | 786 |
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
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(plugin): long_short falls through cleanly when reduction lacks cost date
Closes #1010.
Bug
The
long_shortcapital-gains classifier silently dropped thegeneric
Income:Capital-Gainsposting from a transaction wheneverany reduction posting (cost+units+price) had
cost.date == None.Trace:
has_generic = true→ plugin enters the rewrite pathreductionsis non-empty → past the early-returnif let Some(cost_date) = cost_dateskipsgain accumulation when the date is missing →
short_gainsandlong_gainsstay at 0new_postingsis built by FILTERING OUT the matching genericpostings
short_gains != 0andlong_gains != 0are false → no:Short / :Long replacement is emitted
Income:Capital-Gains posting silently removed → unbalanced
Fix
Detect the condition early and fall through the entire transaction
unchanged. Right after parsing
entry_date:Matches the early-return pattern already used elsewhere in the
plugin when classification is impossible (e.g., unparsable
entry_date).Test
Renamed and inverted
test_capital_gains_long_short_no_cost_date_drops_generic_posting→
..._passes_through_unchanged. Now asserts:Income:Capital-Gainsposting IS preservedPre-fix the test pinned the buggy behavior with a comment noting
"this is almost certainly a plugin bug." The PR that introduced
the test (#1007) is referenced in the issue.
Verified
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]