Reconcile drivers to the currency's smallest unit

JA
7d ago
+30-12

Closes #5630
Part of #4078 (part 3, returns engine)
Fork pre-flight: jaysbeekay/sure#942 (same commits, head 63c12b16d)
Stacked on #5624 (top of the returns stack). It fixes code from #5091; it sits on top rather than above #5091 so no other PR in the stack changes.

What: Portfolio::Drivers#reconciles? now allows one unit of the series' currency where that unit is coarser than a cent. A 0.3 JPY residual now reconciles.
Where to start: Portfolio::Drivers.tolerance_for in app/models/portfolio/drivers.rb.
Key test: drivers_test.rb "reconciles allows a residual below the currency's smallest unit".
Behaviour change: applies only to currencies with fewer than two decimals. The cent stays as the floor, so currencies with three or more decimals keep the tolerance they had. MGA and MRU move from 0.01 to 0.1 (default_precision: 1).
Stack: top of the portfolio returns stack. The merge commit from the PR below resolves one test-file conflict (both sides append tests); the fix commits are unchanged.

Evidence
  • Red: the new test failed before the fix on the 0.3 JPY line. It also pins the 1 JPY boundary, 1.5 JPY and 0.3 USD as not reconciling, and the KWD cent floor (0.004 reconciles, 0.015 does not).
  • Mutations (each caught): reconciles? defaulting to the cent again; dropping the cent floor; ignoring the currency; < instead of <=.
  • Full unit suite on 931c24871: 17,828 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest (no node). On the merged head 63c12b16d: 18,014 runs, 0 failures, the same 8 errors.
  • Siblings: nothing in app code reads #reconciles?. Portfolio::Performance stores drivers.to_h, which carries no tolerance, so its cache is unaffected.
  • Migration: none.

Summary by cubic

Makes Portfolio::Drivers#reconciles? accept a residual up to one unit of the series' currency where that unit is coarser than a cent, so a 0.3 JPY residual no longer fails the check and prints "Unexplained JPY 0". Closes #5630.

  • Tolerance now comes from Portfolio::Drivers.tolerance_for(currency); the cent (0.01) remains the floor for all currencies.
  • Only currencies with fewer than two decimals change behavior; MGA and MRU move from 0.01 to 0.1.
  • No migration or cache impact: Portfolio::Performance stores drivers.to_h, which carries no tolerance.
  • Merged to the top of the returns stack; the test-file conflicts from the merges are resolved by keeping both appended test sets.

Summary by CodeRabbit

  • Bug Fixes
    • Reconciliation now uses a tolerance based on the currency’s smallest denomination, with a minimum of one cent. This supports currencies with coarser denominations while retaining existing precision for currencies such as USD.
  • Tests
    • Added checks for reconciliation boundaries across currencies with different precision levels, including cases that should pass and fail.