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

Merged
JA
1mo ago
+1056-49

Closes #4393
Fork pre-flight: jaysbeekay/sure#528, from the same branch.

Ported from jaysbeekay/sure#247 (fork issue jaysbeekay/sure#223), adapted to upstream main.

Stacked on #4394 (the change that makes the Plaid loan processors respect user locks). This branch is cut from that one, and its first commit is that PR's commit. Review only the second commit, Date a Plaid variable rate change instead of moving the base rate. 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;
    • 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.

    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, and all of them pass.

  • Sync date. PlaidItem::Syncer#perform_sync reads Date.current 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.

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.

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: 7 tests for variable_rate_update_for: first sighting, a dated change, existing rows kept, rounding equality, a same-day repeat, arm treated as variable, and a fixed loan.
  • mortgage_processor_test.rb: 9 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.
  • 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: the syncer hands its date to process_accounts, and process_accounts hands it to every account's processor.

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 Loan tests fail with NoMethodError.

Mutations (each alone on the committed fix; Plaid, Plaid item, Loan and Redbark tests 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

Every mutation is caught.

Validation

  • Full suite: 12681 runs, 58538 assertions, 0 failures, 0 errors, 36 skips.
  • Targeted: test/models/plaid_account, plaid_item, loan_test.rb, loan/, redbark_account and redbark_item give 335 runs, 0 failures.
  • bin/rubocop on changed files: no offenses. bin/brakeman: 0 warnings. 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 two plumbing tests;
    • provider diagnostics go through DebugLogEntry.capture;
    • commit subject is 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.

Since review

  • 6c8ed739e: merged the updated #4394, so rejection logs carry the provider link and refusals reach Sentry here too.
  • 99408cb39: Loan#variable_rate_update_for ignores a reported rate outside 0..MAX_INTEREST_RATE, or one that is not finite. This applies to both the first sighting and a dated change, for every provider.
  • 1861ab141: no variable rate is dated while the Plaid item requires an update. ITEM_LOGIN_REQUIRED is swallowed by handle_plaid_error, and the syncer then processes the previous payload; a new syncer test reproduced a row dated today from it.
  • bd713ec1a: a provider rate that isn't a number ("n/a", "5.2%") is ignored like an out-of-range one, instead of raising and rolling back the rest of the payload.
  • e7ee2dc2e: a test that the student loan processor receives the sync date.
  • 8dbf2c589: the reported rate's bound is checked before it is rounded, so -0.0004 or 100.0004 is rejected rather than rounded onto 0 or 100.
  • 0bb918127: the same order fixed in RedbarkAccount::LoanDetailsProcessor#to_percentage, whose rounded value feeds variable_rate_update_for.
  • 6aad2d057 (CodeRabbit reviews 5619642519 and 5624901672): when a sync fetches no liabilities and a payload is stored, PlaidAccount::Importer#import clears it to {}. The processors no longer re-read an earlier sync's rates, so a variable loan no longer records an old rate as a change dated to this sync when Plaid stops serving liabilities for an item. An account that never had a payload is not written. This closes the known limit this description used to carry, without the fetched-at column it proposed.
  • Full suite: 12,702 runs, 0 failures, 0 errors, 36 skips, at 6aad2d057.

Follow-ups

  • Locking schedule writes for every writer is tracked in #4442.

Summary by CodeRabbit

  • New Features

    • Variable-rate loan updates record rate changes with their effective dates while preserving existing rate schedules.
    • Loan syncing uses one consistent date across accounts, based on the family’s local calendar date.
    • Fixed-rate loan updates continue to update the current interest rate.
  • Bug Fixes

    • Missing fields no longer overwrite existing loan values, while explicit zero values are retained.
    • Invalid, duplicate, or unchanged rate updates are not recorded; rate limits are checked before rounding.
    • Loan snapshot freshness is evaluated using the family’s local calendar date.
    • Failed loan updates roll back without leaving partial changes.