Fix reverse balance opening boundary adjustments - #2502
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 9138f342a6650f43acb916366e1f1cf82edfec58 and e53ef25. 📒 Files selected for processing (5)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughReverse balance calculation now emits explicit opening-boundary cash and non-cash adjustments, and reverse materialization/tests now assert the adjusted persisted balances. ChangesReverse balance opening-boundary adjustment
Sequence Diagram(s)sequenceDiagram
participant BalanceMaterializer
participant ReverseCalculator
participant BalancesTable
BalanceMaterializer->>ReverseCalculator: strategy :reverse
ReverseCalculator->>ReverseCalculator: detect opening boundary day
ReverseCalculator->>BalancesTable: persist cash_adjustments and non_cash_adjustments
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
1 similar comment
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
10e8edb to
e8c2d2d
Compare
|
Good fix — surfacing the opening-boundary discrepancy as an explicit adjustment rather than a silent cross-day balance jump makes the history auditable and consistent with how other adjustments are handled. Two things worth double-checking: Liability account coverage. The Interaction with reconciliation waypoints. The PR updates the existing "reconciliation waypoint" test assertions to reflect that the day after the opening anchor now carries a Generated by Claude Code |
e8c2d2d to
9138f34
Compare
There was a problem hiding this comment.
Review
Solid fix. Surfacing the opening-boundary discrepancy as an explicit cash_adjustments/non_cash_adjustments value on opening_anchor_date + 1 (instead of a silent cross-day jump) makes the reverse-sync history auditable and consistent with how other adjustments are handled.
I verified the adjustment math reproduces the DB generated columns (db/schema.rb end_cash_balance/end_non_cash_balance/end_balance): with the boundary day's start = opening and cash_adjustments = end_cash − opening_cash − (cash_inflows − cash_outflows)·flows_factor, the generated end_cash_balance collapses back exactly to the calculator's value. The CreditCard test confirms the liability sign path, and the investment cases correctly route the plug to cash while non-cash nets to 0. CI is green and I found no correctness bugs.
The change requests below are all minor / non-blocking cleanups — happy to approve once they're addressed or explicitly waved off.
Requested changes
-
Deduplicate the flows-factor expression.
flows_factorinapp/models/balance/reverse_calculator.rbre-implementsaccount.classification == "asset" ? 1 : -1, which already exists inBalance::BaseCalculator#build_balance(base_calculator.rb:162). Consider lifting it to a single shared method on the base calculator so the convention lives in one place. -
Consider reusing the existing adjustment helpers.
base_calculator.rbalready definescash_adjustments_for_date/non_cash_adjustments_for_date(lines 57–67), which encode the sameend − start − net_flowsshape (withbalance_typegating). Reusing them inopening_boundary_adjustmentwould keep the adjustment logic centralized and the non-cash gating consistent for edge account types (e.g. loans). Please double-check behavior parity before refactoring. -
Add doc comments to the new helpers. The rest of
reverse_calculator.rbdocuments each branch/method; the new helpers (opening_boundary_adjustment,opening_balance_components,cash_flows_total,non_cash_flows_total,flows_factor) are undocumented, which is also what triggers the CodeRabbit docstring-coverage warning. A one-line comment each would match the file's style.
Generated by Claude Code
9138f34 to
e53ef25
Compare
|
Addressed the review cleanup in e53ef25:
Verification:
|
|
e53ef25 addresses all three items from the changes-requested review: Generated by Claude Code |
Summary
Fixes #2497.
Reverse balance sync now surfaces opening-boundary discrepancies as explicit balance adjustments instead of silently creating an unexplained cross-day jump after the opening anchor.
Changes
opening_anchor_date + 1during reverse balance calculation.balancestable generated columns.Verification
bin/rails test test/models/balance/materializer_test.rbbin/rails test test/models/balancebin/rubocop app/models/balance/reverse_calculator.rb test/models/balance/reverse_calculator_test.rb test/models/balance/materializer_test.rbbin/rails testgit diff --checkSummary by CodeRabbit
Bug Fixes
Refactor
Tests