Project a loan on the schedule's repayment, period by period

Draft
JA
2mo ago
+5122-1402

Summary

Part of #100: decision 1 (owner, 2026-09-08), "The projection pays the schedule's repayment, against the actual balance", delivered on the fork. #392 (#394) and #390 both name it as the sibling they leave alone.

The fork's payoff projection held AmortizationSchedule#monthly_payment, the repayment at origination, across every period. On a variable loan that ignores every rate change since. A loan whose rate had risen projected on a repayment the lender no longer accepts, often failing to converge, so no projection showed at all. A loan whose rate had fallen projected on more than the contract asks.

  • Loan::Simulator gains a :scheduled strategy that asks payment_amount_for every period.
  • Loan::PayoffProjection makes it the default. Each period pays the contracted schedule's repayment for that date: a recorded rate change resizes the projected repayment where the schedule resizes its own, and a variable loan ahead of schedule clears early instead of shrinking back onto the original maturity.
  • The schedule's last row settles its own rounding and drift; it isn't a repayment the contract asks of a loan in any other position. That row, and every period past maturity (a loan behind schedule still owes), pay the last level repayment, as :hold did. So a fixed loan projects byte-identically (asserted).
  • :hold stays available. :reamortize is unchanged.
  • PayoffProjection#monthly_payment is the first period's modelled repayment. RetirementPlan#seed_loans therefore seeds a variable loan with the repayment in force, not the one at origination. That answers #184's open question 1 the way the projection now pays; flagged below.

This differs from upstream's port in one respect, on purpose: upstream's projection stops at the original maturity, while the fork's discovers a late payoff (holding the last level repayment), which its "months behind" figures rely on.

Scope and dependencies

  • Issue: #100 (decision 1). Related: #392 / #394 (card on the schedule), #390 (the chart that draws this projection), #184 (open question 1).
  • Parent PR/branch, if stacked: none. Base main.
  • Intentionally out of scope: removing :hold or :reamortize, and any change to the schedule itself.

Blast radius. Every default PayoffProjection:

  • the Overview and Schedule cards (projected payoff, interest saved);
  • the payoff chart;
  • the Extra repayments comparison, where the extra rides on top of the scheduled repayment;
  • the API schedule endpoint's projection fields;
  • the retirement planner seed.

Fixed loans, and variable loans without recorded changes, are unchanged.

Siblings (grep -rn ":hold\|payment_strategy\|\.monthly_payment" app lib):

  • AmortizationSchedule uses :reamortize: unchanged.
  • RateChangeTable uses :reamortize on main: unchanged here; #394 removes it.
  • _schedule.html.erb:109 checks projection.monthly_payment.present?: still present.
  • The API jbuilder's monthly_payment reads the schedule's first row: unchanged.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests.
  • Any gate test has an observed failing mutation transcript attached.
  • Fixtures used as evidence assert internal consistency.
  • Financial calculations use BigDecimal and documented rounding points.
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

test/models/loan/scheduled_projection_test.rb, on #392's synthetic loan (5.50% → 6.43%, 50 payments made), red on main first:

TestOn main
pays the schedule's repayment in force, not the first contracted one2,271.16 (origination) ≠ 2,504.42
a future recorded rate change resizes the projected repayment where the schedule doesrepayment doesn't move
a variable loan ahead of schedule clears before the schedule does, with no extra (decision 1's acceptance criterion)passes on both; it's the criterion, not the discriminator
a loan behind schedule holds the last level repayment past maturity2,271.16 ≠ 2,504.42
an extra repayment rides on top of the scheduled onepasses on both; load-bearing under M4
a fixed loan projects exactly as the held repayment did (negative)passes on both
the modelled monthly payment is the one in force nowpasses on both; equal by construction

Changed tests, with reasons

  • "extra-payment projection applies recorded variable rates while holding the repayment" pinned "recorded rate changes must not replace the held repayment in the what-if", which decision 1 reverses. It now asserts scheduled + extra per period, and that :hold still holds.
  • "the default projection is unchanged by the new option" compared the default with :hold. It now asserts :hold holds and the default follows the schedule (its fixture records a change two months out). The comparison reads the payment the change resizes, found by date, not by position (see Review findings).
  • Two RateChangeTable preconditions said "the HELD projection" while calling the default; they now pass payment_strategy: :hold.

Mutations, restored from copies:

MutationCaught by
M1: the settlement row counts as a level repaymentpast-maturity test
M2: ask once per segment instead of per periodsurvives, by construction: the schedule's repayment only changes at rate-change segment boundaries, which is where a per-segment ask re-asks anyway. Kept per-period so the contract doesn't depend on that.
M3: default back to :hold3 tests, and the rewritten default-projection test
M4: drop the extra from the scheduled lambdaextra test

Golden master. Only the two rate-change files move, in their projection and planner_seed_monthly_payment only. Schedules are unchanged.

FileChange
variable_change_mid_period.jsonthe rate went 6% → 7.5% → 5.25% (2026-06-20), so the contract now asks 1,670.03, not the origination 1,798.65; payoff 2049-08-15 → 2053-04-15; seed 1,798.65 → 1,670.03
variable_change_on_payment_date.json6% → 7.5%: the held 1,798.65 couldn't service 7.5%, so there was no projection (applicable: false); on 2,091.16 it clears 2052-08-15, ahead of the schedule's 2053-12-15 because the balance is below schedule; seed 1,798.65 → 2,091.16

With #394 and #399 also in (the integration branch), every golden-master loan quotes one figure in its card, its projection's first repayment and its planner seed: for example 2,087.74 on the on-date loan.

Validation

  • Loan, loans/accounts/API controller, overview-label, loan component and retirement suites: 701 runs, 0 failures.
  • After the review fix: payoff_projection_test.rb and scheduled_projection_test.rb, 53 runs, 0 failures.
  • After the Gatekeeper fix (2290b1b): loan model, component, loans controller and retirement-plan suites, 536 runs, 0 failures.
  • Full suite on the integration branch: 12,468 runs, 1 failure (the known Provider::OpenaiTest Ruby-version string, unrelated).
  • bin/rubocop on changed files: no offenses.
  • Checked against upstream AGENTS.md.

Review findings

  • cubic, the default-projection test fails on the 29th–31st: valid. Fixed in a214013.

    • The test loan's schedule dates drift onto the 28th, so a change dated Date.current + 2.months from the 29th–31st resizes the third payment, not the second. The assertion now reads the first payment on or after the change.
    • Checked by wrapping the test in travel_to: the old assertion fails on 2026-10-30. The new one passes on 2026-10-09, 2026-10-30 and 2027-01-31, and still fails with the default strategy mutated to :hold.
  • Gatekeeper light review (comment 6092751828), MINOR, PAYMENT_STRATEGIES defined twice: valid. Fixed in 2290b1b: PayoffProjection::PAYMENT_STRATEGIES is now the simulator's list.

    • New test "the projection accepts exactly the strategies the simulator runs" asserts they are the same object. It failed before the fix (Expected [:scheduled, :hold, :reamortize] to be the same as [:hold, :reamortize, :scheduled]).
    • grep -rn PAYMENT_STRATEGIES app lib test finds no other definition.
    • On the integration branch (#404) and #405, the engine swap already removed the projection's copy, so the merge there keeps one list.
  • Same review, recommendation: prove scheduled_payment_on's reverse_each fallback is exercised: already covered.

    • A projected date past maturity is never a schedule-row date, so "a loan behind schedule holds the last level repayment past maturity" goes through that branch.
    • Mutation (fallback replaced with nil): that test fails, Expected: [2504.42] Actual: [2271.16].
    • The final || level_rows.first is unreachable: projected dates start at the first remaining schedule row, or at a month past today when every row is past, so none falls before the first level row.
  • "code master" review (comment 6105930380), HOLD, on 2290b1b: no defect confirmed. One stale string the review didn't raise is fixed in f7f9746e2, pushed on the owner's instruction (2026-10-11: "Push the held commits; go with your recommendations").

    1. A silent default change breaks callers: already decided. This is #100 decision 1 (owner, 2026-09-08): "The projection pays the schedule's repayment, against the actual balance". The callers are listed above. One correction to the review: 2049-08 → 2053-04 is 3 years 8 months (271 → 315 rows), not 4 years 8 months.
    2. The applicability gate moved: already decided; this is the intended fix. #100's architecture review §3a: under :hold the green forward line "vanishes on exactly the loan that most needs it".
    3. A nil index crash past the schedule dates: declined. It can't happen.
      • The simulator stops before asking for a payment once payment_number > @max_iterations (simulator.rb:100).
      • @max_iterations is bounded by the same date array the lambda reads (simulator.rb:64, payoff_projection.rb:514).
      • Probes that ran +25k, +150k and +300k behind schedule, a matured loan, and a 1,200-month term raised no error.
    4. The memo goes stale: declined. The rows are memoised for the instance's lifetime (payoff_projection.rb:368-370), so the index built from them can't diverge. Loan#payoff_projection builds a new instance when its inputs change.
    5. An empty schedule gives a zero payment: not worth its code. Only a non-amortizable loan has empty rows, and every caller checks applicable? or amortizable? first. Under :hold the same input raised NoMethodError, so :scheduled is the safer of the two.
    6. O(n²) fallback: declined, measured. All 720 calls take 2.1 ms in total, 410 of them on the fallback, against about 550 ms for the simulation.
    7. monthly_payment changes for the same loan: already decided. No card shows projection.monthly_payment; the cards read current_minimum_payment. The planner seed is flagged under Risks.
    8. A test precondition reversed: already decided. "Changed tests, with reasons" records it, and the callers that need :hold pass it explicitly.
    9. Shared PAYMENT_STRATEGIES: not worth its code. The hazard is hypothetical. The reviewer's mechanism is also wrong: an unknown strategy would fall through to :reamortize, not fail at runtime.

    Not raised by the review; fixed in f7f9746e2:

    • The Extra repayments notice (extra_repayments.variable_rate_notice, en and de) still said the projection "keeps the current repayment", which :scheduled no longer does. It now says the projection pays the schedule's repayment, which changes with each recorded rate change.
    • Four class comments in PayoffProjection and the view comment still described the held payment. They now match :scheduled.
    • Copy and comments only. accounts_controller_test.rb, i18n_test.rb and payoff_projection_test.rb: 172 runs, 0 failures. erb_lint and RuboCop: clean.
    • The ru, ca, tr and uk keys with the same name belong to the Schedule tab, which re-amortises and still reads correctly.
  • Nightly review (comment 6120557301), READY WITH CONDITIONS, on f7f9746e2: no blocking findings.

    • MEDIUM, integration dependency with #399: agreed that this PR's own CI can't establish the combined output.
    • How the combined output is checked:
      • The golden master has been regenerated with #394, #399 and this PR together on the integration branch, then again by #405, each moved figure explained there.
      • The golden master test passes on #404's current head 6836ad49d, which carries all of them: its full suite gives 12,671 runs and 1 failure, the known Provider::OpenaiTest string.
      • The test runs in CI on every PR, so it runs again at each eventual merge head. Whichever of #399 and this PR merges second must pass it with the other already in.

Review responses are recorded here rather than as thread replies, because replies posted from this session get an assistant footer appended, which this repository's attribution rule forbids.

Risks, blockers, and follow-up

  • Planner seeding (#184 open question 1) is answered here as "the repayment in force now". If the owner prefers the origination figure, RetirementPlan#seed_loans can pass payment_strategy: :hold.
  • Interaction with #399 (straddle-aware resize): both change the variable-loan golden master; the integration branch regenerates it with both in.