Stop claiming ambiguous pending matches on import

Merged
GF
17d ago
+421-14

Fixes #2013

Summary

Before: when a posted transaction arrives without a provider-supplied pending link, Account::ProviderImportAdapter#find_pending_transaction matches pending entries by amount, currency and a date window, and takes the most recent one. With two or more same-amount pendings (two ATM withdrawals of 20, a recurring charge), the posted transaction can overwrite the wrong pending. That pending's external id is then gone, so its own booking later arrives as a new row while the overwritten payment is lost (#2013). Entry.reconcile_pending_duplicates has the same blind spot from the other side: two same-amount pendings can both be excluded against a single booking. Enable Banking also never runs the pending cleanup that SimpleFIN runs after each sync, so unclaimed Enable Banking pendings stay visible forever.

After: a pending is only claimed when it is the single live (non-excluded) match. With two or more candidates, the posted transaction is imported as a new entry and the pendings stay untouched until their own booking or the stale-pending cleanup takes care of them. Enable Banking now runs the same reconciliation and 8-day stale-pending exclusion as SimpleFIN after processing each account.

This takes the same approach as the earlier PR #2015, which was closed during housekeeping rather than on merit, and adds the reverse check in the reconciliation and the Enable Banking wiring.

How

  • find_pending_transaction loads at most two non-excluded candidates and returns one only if it is unique. Excluded pendings are no longer claimable.
  • Entry.reconcile_pending_duplicates excludes a pending only when its booked match is unambiguous in both directions (new private Entry.sole_pending_for?), and both the exact and the fuzzy lookup are scoped to the pending's own source, so one provider's pending is never treated as the duplicate of another provider's booking. When the exact match is ambiguous, no fuzzy suggestion is stored either.
  • EnableBankingAccount::Processor#process runs Entry.reconcile_pending_duplicates and Entry.auto_exclude_stale_pending(mark: true) after transactions. The cleanup is skipped when the transaction batch raised. Single failed rows don't stop it: both steps only touch pendings with a unique booked twin or older than 8 days, and gating on full success would switch the cleanup off for good behind one row that keeps failing. Failures in the cleanup are logged (DebugLogEntry, Sentry) and do not fail the sync.
  • Some ASPSPs keep the same transaction id when a pending books. With mark: true, the stale-pending exclusion runs as one locked, set-based update and records auto_excluded_stale_pending in transactions.extra; when the booked version later arrives under the same id, the import adapter turns excluded off again. Each row is re-checked under a lock, so an entry the user excluded meanwhile gets no mark. Any other change of excluded (the user toggling it, a reconciliation) clears the mark, so user exclusions are never undone. An import_locked entry that is not user_modified is left excluded, because the protection check would keep it pending and the cleanup would exclude it again on every sync.
  • SimpleFIN keeps calling auto_exclude_stale_pending without mark:, so its behavior is unchanged apart from the stricter matching.

Impact

  • The matching change applies to every provider that uses the shared adapter (SimpleFIN, Enable Banking, Lunch Flow, Mercury, FinanceKit, …).
  • In an ambiguous case a pending stays visible until its own booking arrives or, for SimpleFIN and Enable Banking, 8 days pass. For providers without a cleanup step it can stay visible until removed by hand. This trades an occasional duplicate-looking pending for never overwriting the wrong transaction.
  • Balances, reports and budgets are unaffected; pending entries are already left out there.
  • No migration, no API change, no new setting.

Testing

  • Replaced "reconciles most recent pending when multiple exist" (it encoded the buggy behavior) with a no-claim test, and added the ATM scenario from #2013 and an excluded-pending test.
  • reconcile_pending_duplicates: single match excluded, two same-amount pendings with one booking both kept and given no fuzzy suggestion, other-provider booking ignored for exact and fuzzy matches.
  • Enable Banking: cleanup wiring, 8-day exclusion with mark, re-activation on booking under the same id, manual toggle ends the automatic state, user-excluded and import-locked entries stay excluded, cleanup skipped when the transaction batch raises, cleanup failure does not fail processing.
  • Each new test fails without its part of the fix.
  • Full bin/rails test: 11836 runs, 0 failures, 0 errors.
  • rubocop, erb_lint, brakeman clean; no JS changes.

Summary by CodeRabbit

  • Bug Fixes
    • Pending transactions are matched to booked transactions only when the match is unambiguous, reducing the risk of linking a booking to the wrong entry.
    • Stale pending entries are automatically excluded during successful imports, while recent entries and user-excluded entries are left unchanged.
    • When an automatically excluded pending entry later books, its automatic exclusion is cleared unless it was import-locked and unmodified.
    • Pending-entry cleanup no longer runs after a transaction import fails, and cleanup errors do not interrupt processing.
    • Pending matches and suggestions are limited to entries from the same source.