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

Closed
JA
1mo ago
+711-61

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:

  • 11017b61 Date a Plaid variable rate change instead of moving the base rate
  • 99408cb39 Bound a provider's variable rate to the form's range
  • 1861ab141 Do not date a Plaid rate from an import that needed a login
  • a2cd431f Date a provider sync in the family's timezone
  • bd713ec1a Ignore a provider rate that is not a number
  • e7ee2dc2e Test that the sync date reaches the student loan processor
  • 8dbf2c589 Check a reported variable rate's bound before rounding
  • 0bb918127 Check a Redbark rate's bound before rounding it
  • 6aad2d057 Clear 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 own record_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_of writes nothing;
    • otherwise it adds a schedule row dated as_of and leaves the base rate as it is;
    • a loan that is not variable (variable_rate_type?, so Plaid's own arm counts) 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 with to_s("F"), as the loan form stores rows.
  • PlaidAccount::Liabilities::LoanTermWriter writes 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::Importer swallows ITEM_LOGIN_REQUIRED and 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 on as_of. The status means "this import" because PlaidItem::Importer#import puts 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.

  • RedbarkAccount::LoanDetailsProcessor#apply_rate calls the same Loan method. record_first_sighting and record_change are removed. Redbark's separate same-day check is not carried over: a row dated as_of is the rate in force on as_of (RateResolver#accrual_rate_for takes 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_sync reads 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 mirrors RedbarkItem#process_accounts. Every signature defaults to Date.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 as Localize#resolved_timezone does. A Sidekiq job runs in the app's zone (UTC; config.time_zone is unset and nothing on the job path switches zones), so Date.current dated a change seen at 06:00 on 16 Jan in Sydney as the 15th, one period early.
    • Sibling: RedbarkItem::Syncer had the same defect (synced_at.to_date) and now derives as_of from its one clock through local_date. Its freshness gate, LoanDetailsProcessor#details_fetched_this_sync?, compared fetched_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.

Blast radius.

  • PlaidItem#process_accounts: its one production caller is PlaidItem::Syncer.
  • PlaidAccount::Processor.new: its one production caller is process_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.
  • LoanTermWriter is included only by the two loan processors. For a fixed loan the values written are the same as before.
  • variable_rate_update_for is new and has two callers: the Plaid writer and Redbark's apply_rate.
  • Family#local_date is new; its callers are the two syncers and the Redbark freshness gate. The Date.current defaults on PlaidItem#process_accounts and RedbarkItem#process_accounts have 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 for variable_rate_update_for: first sighting, a dated change, existing rows kept, rounding equality, a same-day repeat, arm treated 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;
    • arm is 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 to process_accounts; the date is the family's (19:00 UTC on 15 Jan is 16 Jan in Sydney); process_accounts hands it to every account's processor; a sync whose import hit ITEM_LOGIN_REQUIRED dates 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_date in 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, and Expected ["2026-01-15"], Actual [].
  • (b) All app code as on the parent branch: the syncer test fails with unexpected invocation: process_accounts(), and process_accounts and PlaidAccount::Processor.new fail with ArgumentError (wrong number of arguments). The seven original Loan tests fail with NoMethodError.
  • (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):

#MutationCaught by
M0writer reverted to the parent branch9 Plaid processor tests
M13-place rounding removedLoan records nothing when the rate in force is unchanged
M2unchanged-rate check removed2 Loan, 1 Plaid repeat-sync and 3 Redbark tests
M3row keyed by Date.current19 tests: 3 Loan, 8 Plaid and 8 Redbark
M4a variable loan's rate overwrites the base rate8 mortgage tests, 1 student-loan test
M5rate judged before the terms are applied5 mortgage tests, including fixed → variable
M6PlaidAccount::Processor passes its own datethe sync date reaches the liability processor
M7the syncer does not pass its datethe processing phase is given the sync's date
M8a first sighting writes a row instead of the base rate1 Loan, 1 Plaid and 4 Redbark tests
M9the rollback removed3 mortgage tests, including the refused move to variable
M10loan.reload after a refusal removed3 mortgage tests
M11errors.clear removeda refusal leaves no errors behind for a locked write to report
M12DebugLogEntry.capture removed3 mortgage tests
M13process_accounts does not pass its dateprocess_accounts hands its date to every account's processor
M14Plaid syncer back to Date.currentthe sync's date is the family's date, not the job's
M15Redbark syncer back to synced_at.to_datethe processing date is the family's date for the sync's clock
M16Redbark freshness gate back to fetched_at.to_datea snapshot is judged fresh on the family's calendar
M17local_date ignores the family's zone4 tests: the Family test and M14 to M16's tests
M18local_date passes an unknown zone straight throughlocal_date falls back to the app's zone for a blank or unknown zone (ArgumentError)
M19the rate bounds removed from variable_rate_update_forLoan records nothing for a rate outside the form's bounds
M20the requires_update? guard removeda 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_account and loan_test.rb give 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/rubocop on 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.