Generate the projection's calendar the way the schedule does
Closes #257. Follows the triage plan.
The defect
Two implementations of the payment calendar that disagree after a short month.
Loan::AmortizationSchedule#scheduled_payment_dates steps from the previous date, so a clamp never recovers:
date = loan.start_date || loan.account_opening_anchor_date
Array.new(loan.term_months) { date = date.next_month } # 29 Jan -> 28 Feb -> 28 Mar
Loan::PayoffProjection#projected_payment_dates offset from the anchor, and >> does recover it:
Array.new(periods) { |index| first_date >> index } # 29 Jan -> 28 Feb -> 29 Mar
For a loan drawn down on 29 September, the two agree for the first five payments. After that they disagree on 334 of the 360: every later date except the non-leap Februaries, where both clamp to the 28th. Accrual is daily (SCHEDULE_DAILY_ACCRUAL = true), so each shifted boundary moves interest:
| Drawdown | interest_saved | diverges_from_schedule? |
|---|---|---|
| 2026-09-28 | −1.10 (the cleanup artefact) | false |
| 2026-09-29 | −127.04 | true |
| 2026-09-30 | −249.47 | true |
A borrower exactly on contract was told they were behind it, and the Projected Payoff and Interest Saved cards were wrong for the life of the loan.
The fix
The projection steps its calendar the way the schedule does. Row C5 of docs/loans/calculation-contract.md specifies the schedule's behaviour, so the projection is the side that was out of contract. No persisted schedule changes, no ALGORITHM_VERSION bump, no prebuild.
It holds from any starting point because the step is monotone: once a date is clamped to the 28th it stays there, so stepping from a contracted row reproduces that row's own tail.
Tests
Three new, each observed to fail against the old generator, and all with pinned dates per CLAUDE.md:
the projected calendar is the contracted calendar, for a month-end start— element-for-element against the contracted rows.an on-contract month-end loan does not read as diverging— 29 and 30 September and 31 March;diverges_from_schedule?false andinterest_savedinside the artefact.the calendars still agree when the projection starts mid-loan— at +2 months, where the clamp is still ahead, and at +40, where the step starts from an already-clamped row.
matches the original schedule ... built its loan with start_date: Date.current, so it failed on the 29th and 30th of any month and passed otherwise. That is why main's ci / test_unit is red today. Its date is pinned now and the month-end case is a test of its own.
Blast radius
projected_payment_dates has three readers inside PayoffProjection: the simulator's payment_schedule, max_iterations, and Loan::RepaymentPlan.for(..., closes_on:). The last is the one behavioural edge beyond the defect — for a month-end loan with a scenario, a dated lump sum falling on the 29th or 30th of a post-February month now lands in the previous half-open window. payoff_projection.rb:363 (as_of.next_month) already stepped; this was the only >> in the loan tree.
Verification
Head 99eb8becf, Ruby 3.4.9, PostgreSQL 16. test/models/loan, loan_test, loans_controller_test and test/components: 371 runs, 3,037 assertions, 0 failures. Rubocop clean on both changed files.
Worth a separate decision
Upstream solved this differently and does not have the bug: AmortizationSchedule#payment_schedule there is (1..term_months).map { |n| start_date >> n }, and PayoffProjection#remaining_payment_dates reads its dates from the schedule, so there is only one calendar. Upstream therefore uses the recovering semantics — the opposite of C5 — and cannot disagree with itself by construction.
So this fix makes the fork internally consistent and, in the same move, moves it further from upstream. Which convention is right is a real question (a lender does not permanently move a 29th payment to the 28th), and C5's own verification column still reads "Confirm against statement payment dates". That belongs with #184, not here: changing C5 restages every persisted schedule.
Summary by CodeRabbit
- Bug Fixes
- Corrected projected payment dates for loans with monthly schedules that begin near the end of a month. Projections now better match the contracted schedule, helping prevent false reports of schedule divergence and keeping estimated interest differences below $5 for on-schedule loans.