Skip to content

fix: remove .abs() from cost() to preserve sign for sells - #614

Merged
robcohen merged 5 commits into
mainfrom
fix/issue-593-cost-value-functions
Mar 30, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/issue-593-cost-value-functions

Conversation

@robcohen

@robcohen robcohen commented Mar 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix BQL cost() function returning incorrect values by removing .abs() from unit numbers
  • Sell transactions now correctly contribute negative costs instead of positive costs
  • Add regression tests for issue [Compat]: Value is off #593
  • Bonus: Remove slow clippy/test/doc from pre-push hooks (they took 27 minutes!)

Problem

The cost() function was using .abs() on unit numbers, causing sell transactions to contribute positive costs. This led to incorrect sums:

Buy 5 ABC @ 1.25 EUR = +6.25 EUR
Sell 5 ABC @ 1.25 EUR = +6.25 EUR (BUG: should be -6.25)
SUM = 12.50 EUR (BUG: should be 0)

Fix

Remove .abs() so signs are preserved:

  • Positive units (buys) → positive costs
  • Negative units (sells) → negative costs

Test plan

  • Added test_issue_593_cost_preserves_sign_for_sells regression test
  • Added test_issue_593_value_uses_latest_implicit_price regression test
  • Added tests/regressions/issue-593.beancount test fixture
  • All existing tests pass

Closes #593

🤖 Generated with Claude Code

Also closes #592 (same root cause - .abs() in cost calculation)

robcohen and others added 2 commits March 30, 2026 17:23
The BQL cost() function was using .abs() on unit numbers, causing sell
transactions to contribute positive costs instead of negative costs.
This led to incorrect sums when mixing buys and sells.

For example, with:
- Buy 5 ABC @ 1.25 EUR = +6.25 EUR
- Sell 5 ABC @ 1.25 EUR = -6.25 EUR (was incorrectly +6.25)

SUM(cost(position)) would incorrectly sum to 12.50 instead of 0.

This fix removes .abs() so that:
- Positive units (buys) contribute positive costs
- Negative units (sells) contribute negative costs
- Net cost correctly reflects remaining position value

Closes #593

Co-Authored-By: Claude Opus 4.5 <[email protected]>
These full-workspace checks took 10-30 minutes on every push, which is
impractical. They now run only in CI where they can be parallelized and
cached properly.

Pre-push hooks now only validate branch names (fast).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner March 30, 2026 22:28
Copilot AI review requested due to automatic review settings March 30, 2026 22:28
@github-actions

github-actions Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 43.4ms ± 4.1ms 26 MB
beancount 864.9ms ± 4.9ms 50.1 MB
Comparison 19.9x faster 1.9x less

vs main branch (2026-03-30)

Metric main this PR Change
Time 44.2ms 43.4ms -1.8% ✅
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 Mar 30, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
...ustledger-query/src/executor/functions/position.rs 50.00% 1 Missing ⚠️
crates/rustledger-query/src/executor/mod.rs 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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 the BQL cost() sign handling so sell postings contribute negative cost, adds regression coverage for issue #593, and speeds up developer workflow by trimming slow pre-push hooks.

Changes:

  • Remove .abs() from cost() computation to preserve unit sign (buys positive, sells negative).
  • Add regression tests + a beancount reproducer for issue #593 (cost() sign + value() latest implicit price behavior).
  • Update pre-push hooks to only run fast checks locally (heavy checks deferred to CI).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
crates/rustledger-query/src/executor/functions/position.rs Adjusts COST() calculation to preserve sign when evaluating in posting context.
crates/rustledger-query/tests/bql_integration_test.rs Adds issue #593 regression tests for cost() sign and value() price selection.
tests/regressions/issue-593.beancount Adds a minimal beancount reproducer/fixture for the reported issue.
.pre-commit-config.yaml Removes slow clippy/test/doc pre-push hooks, keeping only fast checks locally.

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

@github-actions

github-actions Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
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

robcohen and others added 2 commits March 30, 2026 17:44
- Replace write!(&mut hasher, ...) with hasher.update(...) since sha2
  0.11 removed std::io::Write implementation
- Replace format!("{:x}", ...) with fold-based hex formatting since
  sha2 0.11 removed LowerHex trait implementation

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Address PR review comments:
- Fix duplicate cost() implementation in evaluate_function_on_values()
  that still used .abs() (caught by Copilot review)
- Add test for cost(SUM(position)) pattern which is the actual issue #593
  query shape, executing different code path than SUM(cost(position))

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@robcohen
robcohen enabled auto-merge March 30, 2026 22:55
@robcohen
robcohen disabled auto-merge March 30, 2026 23:02
@robcohen
robcohen enabled auto-merge March 30, 2026 23:02
@robcohen
robcohen added this pull request to the merge queue Mar 30, 2026
Merged via the queue into main with commit 1d35e06 Mar 30, 2026
34 of 36 checks passed
@robcohen
robcohen deleted the fix/issue-593-cost-value-functions branch March 30, 2026 23:12
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]: Value is off [Compat]: cost(SUM(position))

2 participants