Date a Plaid variable rate change instead of moving the base rate
Part of jaysbeekay/sure#223 (fixed on fork main by jaysbeekay/sure#247).
Upstream pre-flight for issue we-promise/sure#4393, on a branch cut from upstream main. Stacked on the #222 pre-flight (jaysbeekay/sure#527), whose branch this PR targets. Upstream PR: we-promise/sure#4395 (ready for review), from the same branch and commit.
Ported from jaysbeekay/sure#247 (fork issue jaysbeekay/sure#223), adapted to upstream main.
Stacked on we-promise/sure#4394 (the change that makes the Plaid loan processors respect user locks). This branch is cut from that one and has since merged it in once (6c8ed739e). Review this PR's own commits, against #527's head:
11017b61Date a Plaid variable rate change instead of moving the base rate99408cb39Bound a provider's variable rate to the form's range1861ab141Do not date a Plaid rate from an import that needed a logina2cd431fDate a provider sync in the family's timezonebd713ec1aIgnore a provider rate that is not a numbere7ee2dc2eTest that the sync date reaches the student loan processor8dbf2c589Check a reported variable rate's bound before rounding0bb918127Check a Redbark rate's bound before rounding it6aad2d057Clear a stored liabilities payload a sync did not fetch (CodeRabbit reviews 5619642519 and 5624901672 on we-promise/sure#4395)
The dependency is real: the rate is written through the lock-respecting LoanTermWriter that PR introduces.
Problem
When Plaid reports a new rate for a variable loan, the mortgage and student-loan processors replace interest_rate. Nothing records when the rate changed. Loan#current_variable_rate(as_of) falls back to interest_rate when no schedule row applies, so it returns the new rate for every date. The amortisation schedule (through Loan::RateResolver) and the Overview then charge the new rate over periods before it was in force.
Redbark already avoids this. RedbarkAccount::LoanDetailsProcessor records a change as a variable_rate_schedule row dated to the sync's as_of, which RedbarkItem#process_accounts captures once per sync. Plaid has neither the rule nor a sync date.
Change
-
Loan#variable_rate_update_for(reported_rate, as_of:)holds the rule Redbark applied in its ownrecord_first_sighting/record_change. It returns the attributes to write, or nil, and writes nothing itself:- a first sighting (no base rate) sets the base rate;
- a rate equal to the one in force on
as_ofwrites nothing; - otherwise it adds a schedule row dated
as_ofand leaves the base rate as it is; - a loan that is not variable (
variable_rate_type?, so Plaid's ownarmcounts) gets nil; - a rate outside
0..MAX_INTEREST_RATE, or not finite, gets nil (99408cb39). The form rejects one, and nothing checks a schedule written any other way, so a provider reporting -1% or 150% has sent something other than a rate; - the rate is rounded to 3 places, the precision of
interest_rate, and stored withto_s("F"), as the loan form stores rows.
-
PlaidAccount::Liabilities::LoanTermWriterwrites the other terms first, so a payload that reclassifies the loan is applied before its rate is judged. It then writes the rate:- for a variable loan, through
variable_rate_update_for; - for any other loan, as the base rate, as before;
- for a variable loan, nothing when the item
requires_update?(1861ab141).PlaidItem::ImporterswallowsITEM_LOGIN_REQUIREDand marks the item, and the sync still processes, so the stored payload is the previous sync's. Its rate is not a change the bank reported onas_of. The status means "this import" becausePlaidItem::Importer#importputs it back to good after a fetch that succeeds. The guard sits after the fixed branch on purpose: a stale fixed rate is the value the last sync already wrote, and a user's correction to it is locked, so rewriting it changes nothing.
Both saves run in one
transaction(requires_new: true). A refused rate row rolls the terms back with it, as the single save did. The loan is reloaded, and the refusal is logged after the rollback so the log entry is kept. - for a variable loan, through
-
RedbarkAccount::LoanDetailsProcessor#apply_ratecalls the same Loan method.record_first_sightingandrecord_changeare removed. Redbark's separate same-day check is not carried over: a row datedas_ofis the rate in force onas_of(RateResolver#accrual_rate_fortakes the latest change on or before the date), so the unchanged-rate check already returns first. No Redbark test was changed for this, and all of them pass. -
Sync date, in the family's timezone (
a2cd431f).PlaidItem::Syncer#perform_syncreads the date once and passes it along:PlaidItem#process_accounts(as_of:)→PlaidAccount::Processor.new(plaid_account, as_of:)→ the mortgage and student-loan processors. This mirrorsRedbarkItem#process_accounts. Every signature defaults toDate.current.- The date is
Family#local_date, the date the moment falls on in the family's zone, falling back to the app's zone for a blank or unknown one asLocalize#resolved_timezonedoes. A Sidekiq job runs in the app's zone (UTC;config.time_zoneis unset and nothing on the job path switches zones), soDate.currentdated a change seen at 06:00 on 16 Jan in Sydney as the 15th, one period early. - Sibling:
RedbarkItem::Syncerhad the same defect (synced_at.to_date) and now derivesas_offrom its one clock throughlocal_date. Its freshness gate,LoanDetailsProcessor#details_fetched_this_sync?, comparedfetched_at.to_date(app zone) against that date, which would have rejected a snapshot a Sydney family fetched after 13:00 UTC; it now reads the stamp in the family's zone too.
- The date is
Blast radius.
PlaidItem#process_accounts: its one production caller isPlaidItem::Syncer.PlaidAccount::Processor.new: its one production caller isprocess_accounts. Its existing tests call it without a date, and the default covers them.- Only the two loan processors read
as_of. The credit, transaction and investment steps are unchanged. LoanTermWriteris included only by the two loan processors. For a fixed loan the values written are the same as before.variable_rate_update_foris new and has two callers: the Plaid writer and Redbark'sapply_rate.Family#local_dateis new; its callers are the two syncers and the Redbark freshness gate. TheDate.currentdefaults onPlaidItem#process_accountsandRedbarkItem#process_accountshave no other production caller and are left as they are.
Siblings: what else reads variable_rate_schedule? current_variable_rate, RateResolver, AmortizationSchedule and the loan form need no code change. For a variable Plaid loan they now see dated rows instead of a moved base rate. Redbark now stores rates as "6.75" rather than BigDecimal's default "0.675e1". Loan#variable_rates parses both forms, so existing rows read the same.
Left out.
- A fixed Plaid loan whose reported rate differs still has its base rate updated, lock-respecting, as before. Redbark records nothing in that case. Aligning the two would change behaviour for fixed loans and needs its own decision.
- The student-loan processor still sends
rate_type: "fixed". A student loan the user locked as variable is dated correctly, and a test covers it. - Rates already overwritten by past syncs are not backfilled.
Tests
test/models/loan_test.rb: 9 tests forvariable_rate_update_for: first sighting, a dated change, existing rows kept, rounding equality, a same-day repeat,armtreated as variable, a fixed loan, a rate outside the bounds, and both ends of the bounds.mortgage_processor_test.rb: 10 tests:- a variable change is dated;
armis dated;- the key is the sync date, not the clock;
- a repeat sync adds no row;
- fixed → variable dates the change;
- negative: a fixed rate is still the base rate;
- a locked schedule is not written;
- a refused rate row rolls the reclassification back, with no provenance and one log entry;
- fixed → variable with a valid rate lands both;
- a rate from an import that needed a fresh login is not dated.
student_loan_processor_test.rb: a student loan locked as variable has its change dated.processor_test.rb: the date reaches the mortgage processor.- New
test/models/plaid_item/syncer_test.rb, 4 tests: the syncer hands its date toprocess_accounts; the date is the family's (19:00 UTC on 15 Jan is 16 Jan in Sydney);process_accountshands it to every account's processor; a sync whose import hitITEM_LOGIN_REQUIREDdates no rate change, end to end. test/models/redbark_item/syncer_test.rb: the processing date is the family's date for the sync's clock.test/models/redbark_account/loan_details_processor_test.rb: a snapshot is judged fresh on the family's calendar.test/models/family_test.rb:local_datein two zones either side of UTC, and the fallback for a blank or unknown zone.
Fail-first.
- (a) The date plumbing and the Loan method in place, the writer as on the parent branch: 9 failures, every new processor test except the fixed-rate negative case, which passes. For example,
the base rate was overwritten. Expected: 4.5, Actual: 5.2, andExpected ["2026-01-15"], Actual []. - (b) All app code as on the parent branch: the syncer test fails with
unexpected invocation: process_accounts(), andprocess_accountsandPlaidAccount::Processor.newfail withArgumentError(wrong number of arguments). The seven original Loan tests fail withNoMethodError. - (c) The timezone tests against
1861ab141's app code: each fails with the UTC date (M14 to M16 below are those reverts).
Mutations (each alone on the committed fix; the named test files run, then reverted):
| # | Mutation | Caught by |
|---|---|---|
| M0 | writer reverted to the parent branch | 9 Plaid processor tests |
| M1 | 3-place rounding removed | Loan records nothing when the rate in force is unchanged |
| M2 | unchanged-rate check removed | 2 Loan, 1 Plaid repeat-sync and 3 Redbark tests |
| M3 | row keyed by Date.current | 19 tests: 3 Loan, 8 Plaid and 8 Redbark |
| M4 | a variable loan's rate overwrites the base rate | 8 mortgage tests, 1 student-loan test |
| M5 | rate judged before the terms are applied | 5 mortgage tests, including fixed → variable |
| M6 | PlaidAccount::Processor passes its own date | the sync date reaches the liability processor |
| M7 | the syncer does not pass its date | the processing phase is given the sync's date |
| M8 | a first sighting writes a row instead of the base rate | 1 Loan, 1 Plaid and 4 Redbark tests |
| M9 | the rollback removed | 3 mortgage tests, including the refused move to variable |
| M10 | loan.reload after a refusal removed | 3 mortgage tests |
| M11 | errors.clear removed | a refusal leaves no errors behind for a locked write to report |
| M12 | DebugLogEntry.capture removed | 3 mortgage tests |
| M13 | process_accounts does not pass its date | process_accounts hands its date to every account's processor |
| M14 | Plaid syncer back to Date.current | the sync's date is the family's date, not the job's |
| M15 | Redbark syncer back to synced_at.to_date | the processing date is the family's date for the sync's clock |
| M16 | Redbark freshness gate back to fetched_at.to_date | a snapshot is judged fresh on the family's calendar |
| M17 | local_date ignores the family's zone | 4 tests: the Family test and M14 to M16's tests |
| M18 | local_date passes an unknown zone straight through | local_date falls back to the app's zone for a blank or unknown zone (ArgumentError) |
| M19 | the rate bounds removed from variable_rate_update_for | Loan records nothing for a rate outside the form's bounds |
| M20 | the requires_update? guard removed | a rate from an import that needed a fresh login is not dated, a sync whose import needed a fresh login dates no rate change |
Every mutation is caught. M14 to M20 were run on a2cd431f.
Validation
- At
6aad2d057, full suite: 12,702 runs, 0 failures, 0 errors, 36 skips. Its two new importer tests: "a sync without liabilities data clears the stored liabilities payload" fails on the parent (the old payload is kept); "...leaves an account that never had them unwritten" passes there as a guard and catches the clear being made unconditional. - At
a2cd431f:test/models/family_test.rb,plaid_item,plaid_account,redbark_item,redbark_accountandloan_test.rbgive 287 runs, 0 failures. CI on the head is the full-suite record for this commit. - Before
a2cd431f, full suite:12681 runs, 58538 assertions, 0 failures, 0 errors, 36 skips. bin/rubocopon changed files: no offenses. No ERB, JavaScript, migrations or user-facing strings.- Checked against
AGENTS.md:- logic is in the model and a concern;
- tests are Minitest and fixtures, with Mocha for the plumbing tests;
- provider diagnostics go through
DebugLogEntry.capture; - commit subjects are under 72 characters.
Rounding on first sighting (raised in fork Light review 6316719046). Loan#variable_rate_update_for rounds the reported rate to three places before it compares and writes. Redbark's rate was already rounded to three places by its to_percentage before this change, and loans.interest_rate is decimal(10, 3) (db/schema.rb:1589), so no stored value changes.