Date a Plaid variable rate change instead of overwriting the base rate
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_ofrecords 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,
DataEnrichmentprovenance and refusal logging. -
PlaidAccount::Liabilities::LoanTermWriterapplies the other terms first, includingrate_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 lockedrate_typestays the user's), the rate goes throughvariable_rate_update_for. Otherwise it is written exactly as before, in one batch. -
Redbark.
RedbarkAccount::LoanDetailsProcessor#apply_ratenow delegates to the same method. Itsrecord_first_sighting/record_changeare removed. No Redbark test was edited, and all of them pass. -
Date injection.
PlaidItem::Syncer#perform_syncreadsDate.currentonce 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
mainatc89eb2e4. - 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_balanceandterm_months.
Blast radius
PlaidItem#process_accounts: its one production caller isPlaidItem::Syncer. Tests call it with no date, and the default covers them.PlaidAccount::Processor.new: called byprocess_accountsand byprocessor_test.rb, six times without a date, covered by the default. Only the two loan processors readas_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'sapply_rate.variable_rate_schedulereaders (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
BigDecimaland documented rounding points: 3 decimal places, matchingquantize_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:
| Test | Result before the fix |
|---|---|
| a variable mortgage's rate change is dated, not overwritten | the base rate was overwritten. Expected: 4.5, Actual: 5.2 |
| the dated row is keyed to the sync's date, not the clock | Expected ["2026-01-15"], Actual [] |
| the same variable rate on the next sync adds no second row | Expected {"2026-01-15"=>5.2}, Actual {} |
| a payload that turns a fixed loan variable dates the rate change | Expected 4.5, Actual 5.2 |
| a locked schedule is not written | Expected 4.5, Actual 5.2 |
| a student loan the user locked as variable has its rate change dated | Expected 4.5, Actual 5.2 |
| a fixed mortgage's rate is still written as the base rate | passes (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:
| Mutation | Caught by |
|---|---|
| drop the 3-dp rounding | Loan unchanged-rate test |
| drop the unchanged-rate check | Loan test, the Plaid repeat-sync test, and 3 Redbark tests |
key the row with Date.current | 4 Plaid, 2 Loan, 1 student-loan and 6 Redbark tests |
| variable loans overwrite the base rate again | 5 mortgage tests and the student-loan test |
ignore the payload's rate_type when classifying | 3 mortgage tests |
PlaidAccount::Processor derives its own date | ProcessorTest the sync date reaches the liability processor |
| the syncer does not pass its date | PlaidItem::SyncerTest |
| a first sighting writes a row instead of the base rate | the Loan test and 2 Redbark tests |
| drop Redbark's same-day check | nothing: 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 onmain. 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_fortests moved out from underprivate(CodeRabbit), with no content change.loan_testplus theplaid_account,plaid_item,redbark_account,redbark_itemandenrichabletests: 212 runs, 0 failures.bin/rubocopis 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.