Back-port the XIRR hardening to the line that runs it

Merged
JA
3mo ago
+788-59

Stacked on #185 (the A1 catch-up sync), which must merge first. Base is chore/sync-upstream-main-v2, so this diff is the XIRR back-port alone.

Phase A2 of the delivery plan in #118.

What this fixes

Portfolio::Xirr exists twice:

lineswhere
the original196fork main — what Portfolio::Performance#money_weighted actually calls
the hardened one478up/portfolio-xirr, built for we-promise/sure#3611

Eleven commits of fixes landed only on the branch being shown to maintainers, and never on the line being built on. This is the divergence recorded as the single-copy rule in #118, and the reason git merge-tree cannot catch it: two copies of one file both apply cleanly, so mergeability says nothing about equivalence.

What the hardening brings

  • The bracket's low end derived per series rather than pinned at -0.999999, which underflowed past ~51 units
  • Same-date flows summed before the sign-change guard, so a pair that cancels is not counted as a sign change
  • A root sitting exactly on a bracket endpoint returned rather than walked past
  • The residual tolerance relative to the series' size, so the answer does not depend on the unit the amounts were written in
  • Sub-brackets scanned when the widest bracket shows no sign change, so a two-root series is solved rather than refused
  • The flows array frozen, since every figure on the instance is memoised from it

Reachability — measured, not asserted

This is the part worth reviewing. Performance#money_weighted is the only caller, and it always passes the period's own length as days_per_unit — so the multi-unit underflow, which is the headline fix at the class level, is not reachable through it.

To find what is, 4,000 series were generated in exactly the shape that method emits (-opening at the first row's date, -flow per row, +closing at the last date plus one), and evaluated under both classes:

old nil -> new rate : 16
old rate -> new nil : 0
both rate, differ   : 0

Purely additive at the caller. 0.4% of these shapes — all heavy losses — stop reading "not available" and get a figure. No figure the hub shows today can move, and nothing that currently produces a rate starts refusing.

No Performance-level test, deliberately

Three attempts at one rested on premises that turned out false:

  1. weekly units the caller never passes;
  2. a same-date collision whose two flows share a sign, so no spurious sign change;
  3. a near-total loss both classes solve identically.

Each passed against the defect it was supposed to name. A test that cannot fail for the right reason is worse than none, so the contract is left pinned by the 33 class-level tests that come with the class, each mutation-checked when written. The sweep above is the reachability evidence in their place.

Validation

  • bin/rails test test/models/portfolio: 164 runs, 656 assertions, 0 failures
  • bin/rubocop: no offenses

Blast radius

Who else hits this line, and what do they pass? Portfolio::Performance#money_weighted is the only caller in app/, lib/ or config/, and it uses Flow.new(date:, amount:) and rate_or_nil(flows, days_per_unit:) — both unchanged in signature. The sweep above bounds the behavioural effect at that caller.