Date a Plaid variable rate change instead of overwriting the base rate

Merged
JA
2mo ago
+393-59

Summary

Fixes #223. Follows the triage plan and the owner's decision: option 3, with Redbark converged in the same PR.

The defect. When Plaid reported a new rate for a variable loan, LoanTermWriter replaced interest_rate, and nothing recorded when the rate changed. Loan#current_variable_rate(as_of) then returned today's rate for every past date. So AmortizationSchedule (through RateResolver), Loan::PayoffProjection and the Overview cards all charged the new rate over periods before it was in force.

The fix.

  • Loan#variable_rate_update_for(reported_rate, as_of:) holds the rules Redbark had in its own processor:

    • the rate is rounded to 3 decimal places, matching what the schedule stores;
    • a first sighting (no base rate yet) sets the base rate;
    • a rate equal to the one in force on as_of records nothing;
    • a change becomes a schedule row dated as_of;
    • a non-variable loan returns nil.

    It returns the attributes to write and writes nothing itself, so each provider's writer still handles locks, DataEnrichment provenance and refusal logging.

  • PlaidAccount::Liabilities::LoanTermWriter applies the other terms first, including rate_type, so a payload that reclassifies the loan is applied before its rate is judged. If the loan will be variable after this write (a locked rate_type stays the user's), the rate goes through variable_rate_update_for. Otherwise it is written exactly as before, in one batch.

  • Redbark. RedbarkAccount::LoanDetailsProcessor#apply_rate now delegates to the same method. Its record_first_sighting/record_change are removed. No Redbark test was edited, and all of them pass.

  • Date injection. PlaidItem::Syncer#perform_sync reads Date.current once and passes it on:

    • PlaidItem#process_accounts(as_of:);
    • PlaidAccount::Processor.new(plaid_account, as_of:);
    • the Mortgage and StudentLoan processors.

    Only the entry-point signatures default to Date.current, as the fork's rule allows. No method body reads the clock.

  • The two saves are atomic (695321c3, from CodeRabbit). The variable path writes the terms first and the dated rate second, because the rate is judged against the loan the terms produce. Split, a refused rate row no longer took the terms back with it, so a payload reclassifying a loan to variable could commit without its rate. Before this PR the single save was all or nothing. Both saves now share one transaction that rolls back on either refusal, and the refusal is logged after the rollback so the log entry survives it.

One deliberate removal. Redbark's separate same-day check (schedule.key?(key) && … == rate) is not carried over. A row dated as_of is always the rate in force on as_of, so the unchanged-rate check returns first, and the same-day check could never fire. The mutation run confirms it: removing the check broke no test, in Redbark's suite or the new one. A comment in Loan says so.

Scope and dependencies

  • Issue: #223 (the second half of #158; #222 shipped the locks half)
  • Parent PR/branch, if stacked: none. Branched from main at c89eb2e4.
  • Intentionally out of scope:
    • A fixed Plaid loan whose reported rate differs. Its base rate is still written (lock-respecting), as before. Redbark records nothing in that case and logs the disagreement. Aligning Plaid would change behaviour for fixed loans, which needs its own decision.
    • The student-loan rate_type: "fixed" literal stays, per the #222 decision. A student loan the user locked as variable is dated correctly, and there is a test for that.
    • Backfilling history that was already overwritten; SimpleFIN (#159); initial_balance and term_months.

Blast radius

  • PlaidItem#process_accounts: its one production caller is PlaidItem::Syncer. Tests call it with no date, and the default covers them.
  • PlaidAccount::Processor.new: called by process_accounts and by processor_test.rb, six times without a date, covered by the default. Only the two loan processors read as_of; the Credit, transactions and investments processors are untouched.
  • LoanTermWriter: included only by the Mortgage and StudentLoan processors. For a fixed loan, the batch is identical to before.
  • Loan#variable_rate_update_for: new, with two callers, Plaid's writer and Redbark's apply_rate.
  • variable_rate_schedule readers (current_variable_rate, next_rate_change_date, RateResolver, AmortizationSchedule, PayoffProjection, the loan form): no code change. For a variable Plaid loan they now see dated rows instead of a moved base rate.

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. Not applicable.
  • Any gate test has an observed failing mutation transcript attached. See Validation.
  • Fixtures used as evidence assert internal consistency. Each processor test sets the loan's rate, type and schedule explicitly and asserts the change against that state.
  • Financial calculations use BigDecimal and documented rounding points: 3 decimal places, matching quantize_variable_rate_schedule.
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Validation

Fail-first. The date plumbing went in first with the writer unchanged. Against that, the six new behaviour tests fail for the right reason, and the fixed-loan negative case passes:

TestResult before the fix
a variable mortgage's rate change is dated, not overwrittenthe base rate was overwritten. Expected: 4.5, Actual: 5.2
the dated row is keyed to the sync's date, not the clockExpected ["2026-01-15"], Actual []
the same variable rate on the next sync adds no second rowExpected {"2026-01-15"=>5.2}, Actual {}
a payload that turns a fixed loan variable dates the rate changeExpected 4.5, Actual 5.2
a locked schedule is not writtenExpected 4.5, Actual 5.2
a student loan the user locked as variable has its rate change datedExpected 4.5, Actual 5.2
a fixed mortgage's rate is still written as the base ratepasses (the negative case)

Before the plumbing, PlaidItem::SyncerTest failed with Expected 2026-01-15, Actual nil, and PlaidAccount::ProcessorTest and the process_accounts test failed on ArgumentError. The six Loan unit tests failed on the missing method.

Mutations. Each change was made on top of the fix:

MutationCaught by
drop the 3-dp roundingLoan unchanged-rate test
drop the unchanged-rate checkLoan test, the Plaid repeat-sync test, and 3 Redbark tests
key the row with Date.current4 Plaid, 2 Loan, 1 student-loan and 6 Redbark tests
variable loans overwrite the base rate again5 mortgage tests and the student-loan test
ignore the payload's rate_type when classifying3 mortgage tests
PlaidAccount::Processor derives its own dateProcessorTest the sync date reaches the liability processor
the syncer does not pass its datePlaidItem::SyncerTest
a first sighting writes a row instead of the base ratethe Loan test and 2 Redbark tests
drop Redbark's same-day checknothing: it cannot fire, as explained above, so it was removed

One existing test changed. a refused rate leaves the previously accepted provenance value in place (added in #222) used a variable loan. It is about refusing a base-rate write, and for a variable loan the rate is now a dated row. Its loan is now fixed, so it still exercises the base-rate path, and a comment says why.

Commands:

  • bin/rails test test/models/loan_test.rb test/models/plaid_account test/models/plaid_item test/models/redbark_account test/models/redbark_item test/models/concerns/enrichable_test.rb: 209 runs, 0 failures.
  • Full bin/rails test: 10,660 runs, 4 failures, 3 errors. All 7 are the known local Ruby 3.3.6 environment failures, the same set as on main.
  • bin/rubocop (13 files): no offences.
  • bin/brakeman: 0 warnings.
  • Checked against upstream AGENTS.md: the rules live on the model (Loan); Minitest; no new dependency, migration or user-facing string.

At 695321c3:

  • Three new mortgage tests. A move to variable whose rate row is refused changes nothing and an error between the terms and the rate leaves the terms unwritten both failed first (Expected: "fixed", Actual: "variable"). A move to variable with an acceptable rate still lands both is the positive control.
  • Mutations, each caught: rollback dropped; transaction dropped; refusal logged inside the transaction (the log entry is lost); in-memory reload dropped. For the last, the refused test asserts the writer's own loan object.
  • The six Loan#variable_rate_update_for tests moved out from under private (CodeRabbit), with no content change. loan_test plus the plaid_account, plaid_item, redbark_account, redbark_item and enrichable tests: 212 runs, 0 failures. bin/rubocop is clean on the three files.
  • This needs a fresh Gatekeeper pass at this head.

Risks, blockers, and follow-up

  • Behaviour change for variable Plaid loans: from the next sync, a moved rate becomes a dated row and the base rate stays. Existing overwritten history is not reconstructed.
  • Fixed-loan disagreement (Plaid overwrites, Redbark logs) remains a divergence to decide separately.
  • upstream:candidate: every file touched is upstream code.

Summary by CodeRabbit

  • Bug Fixes
    • Variable-rate loan updates are recorded with the date the account was synced, keeping rate histories aligned with reported information.
    • Unchanged rates are no longer added as duplicate history entries. Fixed-rate loans and locked rate settings retain their existing values when new rates are reported.
    • Loan terms and rate changes are applied together, so if either update is refused, the other is not saved.