Let a user's correction survive the next Plaid sync

Merged
JA
2mo ago
+333-2

Closes #158. Follows the triage plan and the owner's sequencing decision recorded in it.

The plan was written on 2026-09-15 and validated at 63dc67d4. Before starting, I re-validated both processor files against main at 5fe89441, and they are byte-for-byte what the plan describes.

Three defects in one update!

Both processors wrote the Plaid payload straight onto the loan. That single call caused three distinct problems, which the code had no way to tell apart:

A locked value was overwritten. A correction made on the loan form is locked by Account#lock_saved_attributes! → accountable.lock_saved_attributes!. update! ignores locks entirely, so the next sync reverted the user's edit: silently, on every sync, indefinitely. The student-loan processor is the worst case. It sent rate_type: "fixed" unconditionally, so a loan the user marked variable reverted on every single sync.

Nothing recorded the author. There was no DataEnrichment, so a later writer had no way to know whose value it was looking at.

An omitted field blanked a stored one. update! received nil for anything the payload didn't carry. A mortgage without a percentage erased the rate the user typed in. A student loan missing either date erased the term.

The fix

One shared PlaidAccount::Liabilities::LoanTermWriter, included by both processors. It drops the keys the payload didn't carry, then hands the rest to Enrichable#enrich_attributes. That method skips locked attributes and records provenance for each attribute written.

compact, not compact_blank. A rate of exactly 0 is a real figure: providers do report interest-free loans. compact_blank would drop it along with the nils. The distinction being drawn is "the payload did not say" against "the payload said zero", and one test covers that boundary specifically.

The failure path needed explicit handling. enrich_attributes calls save, not save!. So an invalid value now returns false, where update! used to raise into PlaidAccount::Processor#process_liabilities's rescue and get reported. Without an explicit DebugLogEntry, the failure would have become silent, which is worse than the defect being fixed. false alone isn't a failure, because Enrichable also returns it for locked and unchanged values. So only a populated errors is logged.

Provenance on refusal. A refused write must not leave a DataEnrichment row attributing the rejected value to Plaid. This is fixed once in Enrichable by #231, now merged and on this branch through ff6783c3: it rolls back a refused save inside a requires_new savepoint, and writes provenance only after a successful save. LoanTermWriter needs no change of its own for this.

Tests

Fourteen new tests (ten in the mortgage file and four in the student-loan file):

  • eleven against the numbered rows of the plan's spec;
  • the two refusal-cleanup tests added in 2bbbbdb6;
  • the provenance-value test added in 3e94c573.

Eight mutations, each caught by exactly the tests predicted:

MutationTests that fail
back to update!rows 1, 2, 6, 7: the four lock cases
.compact droppedrows 4, 8, 9: the omitted-field cases
a filter treating 0 as absentrow 5
the source mislabelled as another valid providerrow 3
the DebugLogEntry capture removedrow 11
errors.clear removeda refusal leaves nothing behind for a locked write to report: the stale errors log a second, spurious entry
restore_attributes removeda refused value does not cost the next write its change: the rejected value stays assigned, and the next valid write is refused because of it
Enrichable back to its pre-#231 versiona refused rate leaves the previously accepted provenance value in place: Expected: 5.2, Actual: 150.0

A note on the fourth mutation. I first tried an invalid source string, which raised an error and took twelve tests down with it. That was a loud result that proved nothing about provenance. Swapping to another valid provider name makes it fail cleanly on the one row that checks attribution.

The last test asserts the stored value, not a row count. log_enrichment reuses the row an earlier accepted write made, so a refusal would overwrite its value, which a count cannot see.

The five pre-existing tests are unchanged and pass (row 12).

Deliberately left out

The dated variable_rate_schedule row for a changed variable rate. The owner's sequencing decision on #158 was locks now, dated rows later. It is tracked as #223.

The student-loan rate_type: "fixed" literal stays. An unlocked student loan still takes the provider's default, and the user's lock is what protects a correction (row 6).

Also out: unlocking UI, CreditProcessor, SimpleFIN (#159), Redbark (#142, #160).

Verification

  • At 3e94c573, CI passes all 13 checks, including test_unit and test_system.
  • Locally, test/models/plaid_account, test/models/redbark_account and enrichable_test.rb give 123 runs, 0 failures. rubocop is clean on the changed test file.
  • The earlier full-suite run, at 2bbbbdb6, was 10,133 runs, 0 failures. brakeman reports 0 warnings.

Both processor files are byte-identical to upstream main, so this is upstream:candidate, as the issue is labelled.