Rescue #1775: loans overview new insights

Closed
JA
2mo ago
+1431-3

Fork pre-flight for we-promise/sure#3327, which rescues the abandoned we-promise/sure#1775.

Same branch, same commit. The head is 7cb25293, identical to the upstream head, because #3327 is opened from this branch. It is not a cherry-pick. This PR exists so the change carries this fork's CI and Gatekeeper history, which can't be seen from upstream. The head moved from 79e8c2b1 on 2026-09-26 with fixes for six of cubic's seven findings; see the disposition comment.

Why this is opening late

The rule 8 cross-link audit found #3327 missing both halves: no pre-flight link and no issue reference. This closes the pre-flight half. The issue half cannot be closed the same way and is flagged below.

Like #228, it was blocked until 2026-09-25 by the stale mirror (#227).

The issue link is genuinely missing, and I am not inventing one

Rule 8 wants the upstream issue a PR answers. There is no issue anywhere in this chain.

  • #3327 rescues PR #1775.
  • #1775's body says it "replaces #1497", which is also a PR.
  • So the chain is PR → PR → PR. #3327's body says Rescues #1775, which names a pull request rather than an issue and will not close anything.

There were two ways to resolve it:

  • Raise an upstream issue describing the loan-overview gap, and have #3327 reference it. That is honest, and it gives maintainers the programme context rule 8 exists to provide.
  • Treat a PR rescue as an explicit exception to the issue-link half, and say so in #3327's body so the next audit does not re-flag it.

Decided: the second. #3327's body now records the exception explicitly, so a future audit reads it as deliberate rather than as a missing reference. The context an issue would carry is already in #3327 and #1775, and raising one purely to be pointed at would be paperwork.

What the change does

  • Loan#down_payment makes leverage at drawdown readable. It deliberately sits outside the amortisation.
  • insurance_rate and insurance_rate_type: level-term insurance is charged on the original principal, decreasing on the outstanding balance. A rate with no type reads as decreasing, which never overstates a policy nobody has described. A blank type is stored as NULL.
  • Loan::Insurance charges the premium alongside the instalment, never inside it, so Loan::AmortizationSchedule rows still agree with a lender's own table. #total_cost returns nil, rather than principal plus interest, when there is no schedule: a cost figure with no interest in it understates the loan instead of declining to answer.
  • Progress readers:
    • months_elapsed is measured from #origination_date, so a loan with no start_date still works.
    • Added remaining_months, finished?, remaining_balance_at, payment_breakdown and balance_paid_ratio.
    • Paydown is measured against the account balance, not the schedule: the schedule says what was promised, the balance says what happened.
    • A finished loan no longer reports a paid instalment as current.
  • Overview UI: three cards and a repayment section, each shown only when the loan actually records what it needs. A card reading zero insurance would tell a borrower their policy is free rather than unrecorded. The original amount is privacy-masked.
  • Plaid fix: StudentLoanProcessor parsed origination_date to derive the term, then discarded it, so an imported loan read as originated on its import date. The drawdown date is now written to start_date, and never over a date already recorded.
  • The form now permits the new fields. down_payment and the insurance fields were being dropped on save.

Migrations

There are two, both additive and nullable, with no backfill:

  • down_payment (decimal 19,4);
  • insurance_rate (decimal 8,4) and insurance_rate_type (string).

Each has a check constraint matching the pattern loans already uses. Existing loans read exactly as before, and the new cards stay hidden until the fields are filled in.

Verification

  • At 7cb25293:
    • Targeted loan, insurance, progress, demo generator, student-loan processor and accounts controller tests: 197 runs, 0 failures (local, Ruby 3.3.6).
    • Every one of the six fixes has a test that failed first and a deliberate break that it catches.
    • Rubocop, erb_lint and brakeman are clean.
  • Fork CI is green on this head: test_unit, test_system, lint, lint_js, scan_ruby, scan_js and Codacy.
  • Upstream CI was green on the earlier head 79e8c2b1. It re-runs on #3327 for this head.
  • Gatekeeper: the 2026-09-25 APPROVE was given at 79e8c2b1, before these fixes, so a fresh pass is needed.
  • All seven cubic threads are resolved. Six were fixed. Thread 7 (the demo books no insurance premiums) was declined by the owner on 2026-09-26, because Loan::Insurance is a projection and nothing in the app books premiums as transactions.
  • One known local-only failure is worth naming so nobody chases it. RecurringTransaction::PipelineTest errors with a PostgreSQL authentication failure on one machine here, on any branch including main. It is tracked as #168 and unrelated to this change.
  • Loan locale files: upstream has since added ru, tr and uk loan locale files, and the new keys are not in them yet, so those locales fall back to English.

Note on the base

CodeRabbit does not review a non-default base and will not review this one. #3327 carries CodeRabbit's review upstream, where the change lands.

Review follow-up

Pushed on 2026-09-29 with the owner's go-ahead: head 5d44e480 (merge 65e2d393, fixes 21486a1d and 5d44e480). This branch is the head of we-promise/sure#3327, so that PR carries the same commits (same branch, same commit).

Current head: 8a34bde8 (2026-09-29). After 5d44e480, merge 87096691 brought in 30 upstream commits with no conflicts, and 8a34bde8 implements finding 5 as option (a). Both are on we-promise/sure#3327 too, since it is the same branch.

Finding (source)Status
1. Two different "Total Cost" figures on one page (PRR, HIGH)Fixed in 21486a1d. Loan#total_cost is now schedule.total_paid + total_insurance. With no premium it equals the Schedule tab's figure exactly. With a premium, the Overview card is titled "Total Cost incl. Insurance". Test: the overview's total cost agrees with the schedule's and names the premium it adds. Before the fix it failed with two cards titled "Total Cost": $654,392.91 and $636,392.91.
2. payment_breakdown derives its own date (PRR, HIGH)Fixed in 21486a1d. It now takes payment_breakdown(payment_number: nil, as_of: Date.current), and the Overview passes its single captured today to _repayment_progress. Tests: the current instalment is the one for the date asked about (model), and LoanOverviewViewTest, which renders the Overview with as_of 2026-04-20 under a 2026-09-28 clock and gets Payment 4, not Payment 9.
3. Negative opening valuation renders leverage_band_ missing translation (PRR, MEDIUM)Fixed in 21486a1d. initial_leverage_ratio now returns nil unless original_balance is positive. Tests: model, and a controller test asserting no translation missing: en.loans and no Leverage card.
4. Plaid term days / 30 (PRR, MEDIUM)Fixed in 5d44e480. The term is counted in whole served calendar months, the same way Loan#months_elapsed counts, so 30 years is 360 months instead of 365. Boundary tests: payoff on 02-24 → nil, 02-25 → 1, and 2027-01-24 → 11 for a 2026-01-25 origination.
5. balance_paid_ratio, leverage and total cost use the first valuation rather than Plaid's initial_balance (PRR, MEDIUM)Fixed as option (a) in 8a34bde8, by owner decision (see below). Loan#original_balance now prefers the recorded principal (initial_balance) when it is positive, and falls back to the account's first tracked valuation when it is nil, zero or negative. That covers every import that sends no principal. It is one reader, so the schedule, the insurance base, the repayment ring and the leverage figure cannot disagree about what was borrowed. Tests, each observed to fail on its own before the fix: the principal is the recorded one, not the first tracked balance, the schedule amortises what was borrowed, repaid is measured against what was borrowed, leverage is measured against what was borrowed and a level-term premium is charged on what was borrowed. On a 20,000 loan imported with 10,000 outstanding, these read 20,000, 50% repaid, 4.0× leverage on a 5,000 deposit and a level premium of 6, where the old code gave 10,000, 0%, 2× and 3. There is also an end-to-end test through StudentLoanProcessor (an imported loan measures its figures against the principal Plaid sent), and two fallback tests covering nil, zero and negative.
6. A recorded premium disappears when a loan has no schedule (PRR, MEDIUM)Fixed in 21486a1d. The Insurance card now shows the recorded rate ("0.36% a year"). Total Cost still reads Unknown, because there is no interest figure to include.
7. 17 of 19 loan locales lack the new keys (PRR, MEDIUM)Declined, with the owner free to override. config.i18n.fallbacks = true (config/application.rb:28, production.rb:96), so those locales show the English strings. Upstream docs/llm-guides/ui.md asks for t() and the feature's locale files, not every locale. Adding 17 unreviewed translations of financial terms would be worse than the fallback. en and fr are complete.
8. Merge conflict with the mirror in app/models/loan.rb (PRR, LOW)Fixed by merge commit 65e2d393. Both hunks are kept, with payoff_projection placed after leverage_band. The loan suites were re-run on the merge: 182 runs, 0 failures.
9. payment_breakdown clamps to a paid instalment when the schedule is shorter than the term (PRR, LOW)Fixed in 21486a1d. It returns nil once months_elapsed >= payments.size. Test: a loan whose schedule ended before its term has no current instalment.
10. About 12 identical first_valuation queries per page (PRR, LOW)Declined, based on measurement. On a loan page with insurance and leverage there is 1 database query and 18 query-cache hits. A memo on Account/Loan would add a staleness path, since the value is also read after valuations change, and would save no round trips.
11. Future-dated Plaid origination_date raises inside the sync (PRR, LOW)Fixed in 5d44e480. A future origination date is not recorded, and the rest of the payload still lands. Test: a future origination date is not recorded and does not fail the sync.
PRR "vacuous" test: total cost restates the implementationFixed in 21486a1d. The test now asserts that the no-premium total equals schedule.total_paid, and that adding the policy increases the total by exactly 144.
cubic threads 1-7Already addressed in 9ec404f2 and 7cb25293, per the disposition comment. All seven are resolved. Thread 7 was declined by the owner on 2026-09-26.
Gatekeeper APPROVE (2026-09-25, at 79e8c2b1)Stale. It predates 9ec404f2, 7cb25293 and this follow-up, so a fresh pass is needed; marking this PR ready starts one.

Owner decision: finding 5 — option (a), implemented in 8a34bde8. Loan#original_balance used to be account.first_valuation_amount. For a Plaid loan that is the balance on the day the account was linked, not the principal Plaid sends (loans.initial_balance), so the ring read 0% repaid and leverage, the schedule and total cost were all based on the link-day balance. The options were:

  • (a) have original_balance prefer initial_balance when present;
  • (b) use initial_balance only in balance_paid_ratio/leverage;
  • (c) leave it and raise it as a separate upstream issue.

The earlier recommendation here was (c), and the 2026-09-29 11:08 Gatekeeper pass accepted it as a deferral. That is superseded: the owner chose (a), and it is now in this PR. This deliberately widens the PR's scope to a loan-model change. AmortizationSchedule, Loan::Insurance (the level-term base) and PayoffProjection all read original_balance, so they now measure against the recorded principal whenever one exists. For a loan created through the form, the two figures agree at creation, because the form writes initial_balance and the opening valuation together. They can only diverge if one is edited later, and then the recorded principal wins. No follow-up upstream issue is needed.

Evidence. Each fix was mutated back and a named test failed: overview title, premium not added, partial ignoring as_of, model ignoring as_of, leverage guard removed, days / 30 restored, served-month adjustment removed, rate-only card removed, short-schedule guard removed, future date recorded. Rubocop, erb_lint and brakeman are clean.