Skip to content

fix(plugin): long_short falls through cleanly when reduction lacks cost date - #1014

Merged
robcohen merged 2 commits into
mainfrom
fix/long-short-no-cost-date
May 6, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/long-short-no-cost-date

Conversation

@robcohen

@robcohen robcohen commented May 6, 2026

Copy link
Copy Markdown
Member

fix(plugin): long_short falls through cleanly when reduction lacks cost 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]

…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]>
Copilot AI review requested due to automatic review settings May 6, 2026 15:57
@robcohen
robcohen requested a review from a team as a code owner May 6, 2026 15:57

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

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_short when 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-Gains posting.

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.

@github-actions

github-actions Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 29.9ms ± 0.7ms 21.6 MB
beancount 859.7ms ± 2.8ms 50.3 MB
Comparison 28.8x faster 2.3x less

vs main branch (2026-05-06)

Metric main this PR Change
Time 30.9ms 29.9ms -3.2% ✅
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

@codecov

codecov Bot commented May 6, 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

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

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

@robcohen
robcohen merged commit 1ca73bd into main May 6, 2026
32 checks passed
@robcohen
robcohen deleted the fix/long-short-no-cost-date branch May 6, 2026 17:01
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.

fix(plugin): long_short silently drops generic Income:Capital-Gains posting when cost has no date

2 participants