Start a Mercury import at the sync's window when it has one

JA
6d ago
+70-5

Summary

Mercury convergence, C2b: a Mercury sync now honours its sync window, as Brex's does. When a sync has an explicit window_start_date, every account's transaction fetch starts there. Without one, nothing changes.

Part of jaysbeekay/sure#670 (Track B, Mercury convergence). Stacked on C2a (jaysbeekay/sure#729) in fork stack #687. Mercury has no C2c: its accounts cache is already per connection and already dropped on a credentials change (see #729).

Upstream draft PR: https://github.com/we-promise/sure/pull/5241 (same branch, same commit), in native stack we-promise/sure#5268 on refactor/provider-kit.

Behaviour delta (Mercury)

SyncBeforeAfter
no window (scheduled and manual syncs)a week before the last completed sync, or up to 90 days back for an account with no stored transactionsunchanged
with window_start_datethe window was ignoredeach fetch starts at the window's start, overriding both starts above, exactly as BrexItem::Importer#determine_sync_start_date does

What changed

  • MercuryItem::Syncer passes sync.window_start_date to import_latest_mercury_data(sync_start_date:), which passes it to MercuryItem::Importer.
  • MercuryItem::Importer#determine_sync_start_date returns the window's start when given.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first):

    • New MercuryItem::ImporterSyncWindowTest:
      • a window sets where a later fetch starts, over the incremental start;
      • a window sets where a first fetch starts, over the 90-day first-sync start;
      • without a window, a later fetch still starts a week before the last sync (guard);
      • the connection hands its import the window.
    • MercuryItem::SyncerTest: "the import starts from the sync's window".

    Before the change they fail: unknown keyword: :sync_start_date, wrong number of arguments, and unexpected invocation: import_latest_mercury_data().

  • Existing tests unchanged: MercuryItem::ImporterTest (the unwindowed start-date rules) passes unedited.

  • Mutations, each caught: the window ignored by the importer; the window used even when nil (caught by the guard and two existing importer tests); the connection dropping the window; the syncer dropping the window.

  • bin/rails test (full, at the C2b commit): 16,330 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node in the test image).

  • bin/rubocop on changed files: no offenses. bin/brakeman: no warnings.

  • Size: non-test +10 / -5.

  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • import_latest_mercury_data has one caller, the syncer, and its keyword is optional.
  • MercuryItem::Importer.new has one app caller.
  • Siblings: Brex's importer already does this; Lunch Flow's does since C1b.

Review notes

A code review of the diff before commit raised these; none changed it:

  • A window narrower than the incremental start, or a windowed first sync, leaves older transactions unfetched (the next unwindowed sync starts from the new last sync, and an account with stored transactions no longer takes the 90-day start). True, and the same as Brex today; the convergence decision is "as Brex's does". Lunch Flow avoids the second case with its history_synced_from marker, which Mercury and Brex don't have. In practice no app path windows a Mercury sync today: an item sync gets a window only from its family sync (Family::Syncer), and every family.sync_later call passes none (AutoSync, AccountsController#sync_all, SyncAllJob). If the kit should guard first syncs as Lunch Flow does, that is one follow-up for the kit rather than a Mercury-only change. This is latent, not harmless: it goes live with the first trigger that windows a Mercury sync, so that trigger has to bring the guard with it (full review 6946821282, residual 1).
  • Only the window's start reaches the import, not its end; a window far back may exceed one page of Mercury results: both as Brex. The one-page fetch is tracked in jaysbeekay/sure#818.
  • The window start is returned as a Date while the other branches return a Time: as Brex; Provider::Mercury#get_account_transactions calls to_date on it.
  • The tests call the private fetch_and_store_transactions and set the fake sync's window directly: as the Lunch Flow C1b tests do.