Give the demo loans real terms and schedule-driven payments

Draft
JA
2mo ago
+339-58

Summary

Closes #408.

A fresh install with demo data had three loans (Home Mortgage, Car Loan, Student Loan) created as bare Loan.new: no rate, term, rate type or start date. None was amortizable, so the demo showed none of the loan pages. On top of that:

  • the mortgage was paid twice a month, on two unrelated sets of figures;
  • its 320,000 arrived as a "Mortgage Principal" transaction rather than an opening valuation.

This ports upstream's fix (we-promise/sure#3327's demo half, plus we-promise/sure#3933's reload) from mirror/upstream-main. No adaptation was needed: every attribute and method it uses exists on #405's branch. app/models/demo/generator.rb is upstream's file except for one fork-side hunk: the "Long-term portfolio" demo goal's target, raised to 300,000 in a07186cf8 (see Risks).

LoanTerms
Home Mortgageadjustable, 6.25%, 360 months from 5 years ago, 320,000; 70,000 down; level-term insurance at 0.36; rate changes to 5.5% at month 24 and 6.75% at month 48
Car Loanfixed, 6.9%, 60 months from 37 months ago, 24,000; 4,000 down; decreasing-life insurance at 0.5
Student Loanfixed, 5.5%, 120 months, 42,000; one 2,000 extra payment a year ago, so it is ahead of schedule for a real reason
  • Opening valuations. open_demo_loan! gives each loan an opening-anchor valuation for its principal on its start date.
  • Payments from the schedule. generate_loan_payments! pays each loan's own schedule (principal and interest per payment) up to today.
  • What goes:
    • the housing step's flat mortgage lines (2,800 from checking plus 800 principal a month);
    • generate_loan_payments!'s flat 600/1,100;
    • the "Mortgage Principal" transaction.
  • Also from upstream, beyond the issue's list:
    • The previous car's flat 385 "Auto Loan Payment" stops the day before the car loan starts, so the car isn't paid twice.
    • Mortgage interest is filed under Rent/Mortgage, so that category stays in use: 60 transactions, up from 36.

Scope and dependencies

  • Issue: #408

  • Parent PR/branch, if stacked: #405 (claude/loans-184-engine-swap). The issue names both #402 and #405 as dependencies; #405's base already carries #402's columns:

    • The payment loop reads Payment#principal and Payment#interest, which exist only after #405's engine swap.
    • down_payment and the insurance columns come from #402.

    Merge after #405, then retarget to main.

  • Intentionally out of scope:

    • every other demo account, the generator's counts, and every goal target but "Long-term portfolio";
    • any loan model or engine change;
    • #404's docker-verify loan checks still create their own loan. Switching them to read a demo loan is possible now, but it is a separate change.
  • Merged into #404 at 455385584, cleanly; demo tests on that merge give 28 runs, 0 failures. #404's docker-verify / build-and-run passed at that head (run 38074559092): demo_data:default completes with this generator inside the built image.

  • Refreshed 2026-10-11. This branch merges #405's new head 22e65fb03 as afe0afd37. The merge was clean, and nothing in this PR's two files changed. #405's head carries the reviewed fixes from #393, #394, #397–#401 and #403, through its base.

    • #404 merges afe0afd37 as 8cf45291f, and a07186cf8 (the goal target) as bb0a101b8.
    • #404's docker-verify / build-and-run passed at bb0a101b8 (run 38142384473), so demo_data:default completes with the new target inside the built image.
  • Refreshed again 2026-10-12. This branch merges #405's new head 24746641d as f35c392be. The merge was clean, and this PR's two files are unchanged.

    • #405 brought in three nightly-review fixes from the integration base: #397's and #398's day-count rollbacks now refuse rather than rewrite loans, and #400's parser refuses "Rate change 0.25%"-style wording.
    • Migration, parser, rule-action and loan tests on f35c392be: 156 runs, 0 failures.
    • #404 merges f35c392be in 6836ad49d. Its full suite gives 12,671 runs, 1 failure (the known Provider::OpenaiTest string), and the contract gate catches all 18 mutations.
    • CI on f35c392be: all checks green.
  • Refreshed a third time 2026-10-12. This branch merges #405 at 185130824 (934b26563) and then at a344aaa25 (95ff79e4c). Both merges were clean, and this PR's two files are unchanged. They bring:

    • #394's softened minimum-payment caption: "The next repayment your loan's schedule sets, ...", no longer "your lender's minimum";
    • the payoff projection's offset fix, on #405's engine and #403's: it reads the offsets once per projection, not once per period, taking a 360-month loan page with two offsets from 1.74 s to 0.56 s.

    #404 merges 95ff79e4c in d838e0336. Its full suite gives 12,673 runs, 1 failure (the known Provider::OpenaiTest string), and the contract gate catches all 18 mutations.

  • Refreshed a fourth time 2026-10-13. This branch merges #405 at fc3ea844f as 78d6eb228. The merge was clean, and this PR's two files are unchanged. It brings:

    • #400's parser fixes: movement wording is refused unless a marker names the new rate, and "your rate is X%" is read as the new rate;
    • #400's and #402's added tests.

    Full bin/rails test on 78d6eb228: 12,651 runs, 0 failures, 0 errors, 33 skips. #404 merges 78d6eb228 in 16f59b96d.

Blast radius: who consumes demo data, and what changes for them

  • Who runs the generator:

    • demo_data:default and generate_new_user_data_for!;
    • DemoFamilyRefreshJob (app/jobs/demo_family_refresh_job.rb:43) and FamilyResetJob (:12).

    The two un-skipped tests cover both transaction paths.

  • Demo::FinancekitGenerator funds from checking, but it doesn't read checking's balance (app/models/demo/financekit_generator.rb:170-183).

  • Checking ends about 53.6k higher (151,858 → 205,486 in a seed-42 run), because the 2,800-a-month mortgage line is gone. The goal target change below is its one visible effect.

  • The generator's own end-of-run checks still pass: 8,555 entries (8k–12k required) and 87.9% categorised (75% or more required; it was 88.1%).

Future upstream syncs. I found no automation that treats generator.rb as fork-side:

  • bin/upstream-sync:23 stops on any conflict.
  • .github/workflows/upstream-sync-reminder.yml says conflicts are resolved by hand.
  • The "fork-side" choice exists only in sync merge messages: 798002ee8 listed generator.rb and its test under "Resolved to the fork's side", and 959bf6f3e did the same for #3933's reload.

Decision: from now on, take upstream's side for generator.rb, except the "Long-term portfolio" target hunk: keep the fork's 300,000 until upstream moves its own. An upstream-only change elsewhere in the file merges with no conflict. For generator_test.rb, take upstream's hunks and keep the fork-only block, which sits just before private, and the fork-only seed_long_term_portfolio helper just after it.

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. (No contract row changed.)
  • Any gate test has an observed failing mutation transcript attached. (Not a gate; mutations below anyway.)
  • Fixtures used as evidence assert internal consistency. (No fixtures changed.)
  • Financial calculations use BigDecimal and documented rounding points. (Upstream's amounts come from the schedule's Money values.)
  • De-identification uses token-shape allowlists; no personal data is included. (Demo data only.)
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Tests (test/models/demo/generator_test.rb)

  • Un-skipped: the two tests the fork had skipped because demo loans "carry no terms and pay flat figures".
  • Ported verbatim from upstream:
    • "demo loans carry the terms and principal a schedule is built from"
    • "demo loans owe what their own schedules say, the student loan ahead by its extra payment"
    • "demo loans project payoffs that match their balances"
  • Fork-only:
    • "demo loans carry the terms the issue specifies". Upstream's tests can't see a dropped rate change, since the schedule then agrees with itself.
    • "the mortgage is paid once per scheduled date, by its schedule alone". It measures the housing step's delta on the mortgage, not presence.
    • "the previous car's flat payments stop before the demo car loan starts"
    • "every demo loan has an applicable payoff projection"
    • "the long-term portfolio goal reads on track at the demo's highest checking balance" and "... lowest checking balance" (a07186cf8). Each seeds the goal matrix with the goal holding an exact balance (pinned by a precondition) and a 7,000-a-month 90-day pace, and asserts :on_track.

Fail first, on the unchanged generator:

  • the un-skipped tests and "owe what their own schedules say": NoMethodError: undefined method 'payments' for nil (no schedule);
  • "carry the terms and principal": "the demo mortgage should be adjustable";
  • "project payoffs": the projection must clear the balance;
  • "the terms the issue specifies": all nine terms nil;
  • "paid once per scheduled date": the housing step added 36 mortgage entries;
  • "applicable payoff projection": no projection;
  • "reads on track at the demo's highest checking balance", at the 200,000 target: :reached.

"The previous car's flat payments" failed only on its guard (the old car loan had no start_date); mutation M6 shows it failing for the right reason. "Reads on track at the demo's lowest checking balance" passes at 200,000 by design: it guards the other side, a target set so high the goal falls behind (M14).

Mutations, each run against the whole file, with the file restored and compared with cmp afterwards:

MutationCaught by
M1 drop the extra paymentowe-schedules; project-payoffs
M2 pay each schedule payment twice6 tests, including paid-once and both un-skipped
M3 put back the housing 2,800/800 linespaid-once; owe-schedules; project-payoffs
M4 drop the mortgage's opening valuationowe-schedules; applicable; paid-once; project-payoffs
M5 drop the 6.75% rate changeterms-the-issue-specifies only (why it was added)
M6 put back the old car payment's end dateprevious-car
M7 put back the "Mortgage Principal" transactionpaid-once
M8 book a flat 100 interestowe-schedules
M9 drop .reloadboth un-skipped tests
M10 make the mortgage fixedcarry-terms; terms-the-issue-specifies
M11 pay past today6 tests
M12 down payment 60,000 with the deposit left at 70,000carry-terms; terms-the-issue-specifies
M13 goal target 200,000 or 210,000on-track at the highest balance (:reached)
M14 goal target 600,000on-track at the lowest balance (:behind)

Targets of 220,000, 400,000 and 500,000 pass both goal tests; 300,000 sits inside that range with room on both sides.

Demo run (generate_default_data!, seed 42, scratch database, before and after):

BeforeAfter
Home Mortgage balance / schedule balance today269,000 / no schedule297,813.13 / 297,813.13
Car Loanno schedule10,186.50 / 10,186.50
Student Loanno schedule29,409.08, schedule 31,409.08 (2,000 ahead, 6 months saved)
Payoff projections applicable?false for all threetrue for all three, all converging

Demo goal, full runs across seeds ("Long-term portfolio", 60 months out):

SeedGoal balance90-day pace / monthAt 200,000At 300,000 (needs / month)
42200,653.937,056.60reachedon track (1,632.20)
1212,392.019,081.58reachedon track (1,439.35)
7165,872.4511,318.84on trackon track (2,203.63)
2024184,191.9714,721.98on trackon track (1,902.66)

On every seed and both targets the other eight goals keep their designed states:

  • Vacation in Italy: behind;
  • Wedding fund: reached;
  • Emergency fund: no target date;
  • House downpayment: behind;
  • Tax prep buffer: on track;
  • Sabbatical: paused;
  • Old laptop fund: archived;
  • Paid-off car: completed.

Validation

  • CI on 95ff79e4c: all 7 checks green, on the first attempt.
  • CI on f35c392be: all checks green.
  • CI on a07186cf8: all 8 checks green, including ci / test_unit, ci / test_system on its first attempt, github-advanced-security and Codacy ("Up to standards").
  • CI on afe0afd37: all 8 checks green, the same set.
  • Local, on a07186cf8:
    • full bin/rails test: 12,629 runs, 53,735 assertions, 1 failure, 0 errors, 33 skips. The failure is the known local-only Provider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-style (Ruby 3.3 here; CI pins 3.4);
    • demo suite: 30 runs, 0 failures;
    • bin/rubocop on both files: no offenses. bin/brakeman --no-pager -q: 0 warnings.
  • Local full suite on afe0afd37: 12,627 runs, 1 failure (the same Provider::OpenaiTest).
  • CI on a6adefc3a (the earlier head): all checks green.
    • The first attempt of ci / test_system had one failure in 220 runs: PropertiesEditTest#test_can_persist_property_subtype ("expected to find field account_accountable_attributes_subtype that is not disabled but there were no matches").
    • That test opens a property account's edit dialog. Nothing in this PR reaches it, and its own comments describe the Turbo morph-refresh race it retries around.
    • A single re-run of the failed job passed. No code changed.
  • Full bin/rails test on a6adefc3a: 12,608 runs, 53,671 assertions, 1 failure (the same Provider::OpenaiTest), 0 errors, 33 skips.
  • Loans, components and the loans controller: 679 runs green.
  • erb_lint, Biome: not applicable.
  • bin/rails demo_data:default was not run locally.
    • generate_default_data! was called directly, so the task's summary block didn't print; its figures were computed separately (above).
    • #404's container check runs the task itself, and it passed at bb0a101b8, which carries this head.
  • docs/llm-guides/goals.md describes the goal matrix without figures, so it needs no change.
  • Checked against upstream AGENTS.md. The loan half converges a fork file on upstream's copy. The goal target is a fork-side divergence; upstream's demo has the same "reached" state, so the same change could be proposed there.

Review findings

  • "code master" review (comment 6106127138), mergeable-after-fixes, on a6adefc: nothing to change.

    • At that head, app/models/demo/generator.rb was upstream's file exactly: object d11f562b6 both here and on origin/mirror/upstream-main (94e71a8c2), with an empty diff.
    • So every finding is about upstream's code, and a fork-only fix would re-diverge it. Each was still checked on its merits; generator_test.rb and amortization_schedule_test.rb give 73 runs, 0 failures.
    1. Loans never paid if payments came back newest first: declined. The schedule is oldest first by construction: the dates come from (1..term_months).map { |n| start_date >> n } (amortization_schedule.rb:136), and #payments is documented "oldest first" (:76). Reversing the loop on purpose fails 5 of the 14 demo tests, so it couldn't go unnoticed.
    2. About 900 writes, so a web request could time out: declined.
      • It is 134 payments (60 on the mortgage, 37 each on the car and student loans), not about 300. That is about 405 entries, against about 406 before this PR.
      • Every web caller queues FamilyResetJob.perform_later (users_controller.rb:56,61, api/v1/users_controller.rb:11); otherwise the generator runs only in jobs and rake tasks.
    3. The extra student-loan payment is a transfer, not a loan payment: declined. make_loan_payment! books every scheduled payment's principal through the same create_transfer! (generator.rb:1111-1117). The extra payment is all principal, so it looks exactly like that part of every other payment.
    4. Mortgage interest moved to Rent/Mortgage, undocumented: already recorded above ("Mortgage interest is filed under Rent/Mortgage, so that category stays in use"). Rent/Mortgage sits under Housing (generator.rb:239), so Housing totals still include it. Demo data is regenerated, not upgraded.
    5. No guard if amortization_schedule returns nil: declined. The generator gives all three loans full terms just before paying them, and "demo loans carry the terms the issue specifies" pins them. A guard would bring back finding 1's silent no-op; failing loudly inside the generation transaction is right.
  • Nightly review (comment 6120444214), READY WITH CONDITIONS, on a07186cf8: no blocking findings.

    • MEDIUM, sequencing: agreed. It is already the plan ("Stacked on #405", under Risks):

      1. merge #405 and its dependencies;
      2. retarget this PR to main;
      3. run the generator and loan tests at the resulting head before this PR merges.

      The retarget leaves this PR's two files as they are, but it changes the base under them. So the green runs on #405's branch are not taken as evidence for main. The closest check available today is #404, which carries this head together with #405 and the rest of the stack. Its full suite passed at d838e0336, which carries 95ff79e4c.

  • Nightly review (comment 6134299716), READY WITH CONDITIONS, on 3848dd0f0. No blocking findings. MEDIUM, renew the evidence on the final base: agreed, and it is already the plan. Once #405 and its prerequisites merge, this PR is retargeted to main, and the generator and loan suites run again at that head. Nothing to change now.

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

Risks, blockers, and follow-up

  • The demo goal: fixed fork-side in a07186cf8, at the owner's request (2026-10-11: "Fix the demo goal target fork-side in #415").
    • With checking higher, "Long-term portfolio" read reached instead of its designed on track on two of four seeds, seed 42 included.
    • docs/llm-guides/goals.md:337-339 says goal targets must move when demo balances change. The target is now 300,000; the measurements are under Evidence.
    • Upstream's generate_goals! is identical, so upstream's demo shows the same effect. Proposing the same change there is optional, and is left to you.
  • The car loan's 4,000 down_payment doesn't match the existing "Car Down Payment" transaction (3,000, six years ago, for the previous car). Upstream only checks the mortgage deposit against its down payment, so I left it alone.
  • Stacked on #405. It retargets to main once #405 merges, then the generator and loan tests run again at that head (nightly review above).
  • No Issue Gatekeeper review before building: your "Build #406, 408 and 409" is the override I acted on.
  • Next unblocked issue: none.