Cache Lunch Flow's account list per connection

Merged
JA
2d ago
+180-22

Summary

Lunch Flow convergence, C1c: Lunch Flow's cached accounts list is keyed per connection, under the provider kit's key, and dropped when the connection's credentials change, as Brex's is.

Part of #5003 (Track B, Lunch Flow convergence).

Stacked on #5238. Fork pre-flight: jaysbeekay/sure#728 (same branch, same commit; its CI, Gatekeeper review and screenshot history live there).

Behaviour delta (Lunch Flow)

BeforeAfter
Cache key for the accounts list (preload, select accounts, select an existing account)lunchflow_accounts_<family>lunchflow_accounts_<family>_<connection>. The connection is the one whose API key lists the accounts, as Provider::LunchflowAdapter.build_provider picks it.
Changing the connection's API key or base URLthe old list stayed cached for up to 5 minutesthe connection's cached list is dropped
Renaming the connection or changing its start datethe cache is kept only when the request leaves out api_key and base_url. The settings panel always submits both, so every panel save drops it: one extra provider call per save, never stale data.

Entries under the old family key are simply no longer read, and expire within 5 minutes. Lunch Flow still uses one credentialed connection per family; the kit treats a family with a single credentialed connection the same way, so selection behaves as before.

What changed

  • LunchflowItemsController: the three cache reads and writes use lunchflow_accounts_cache_key(credentialed_lunchflow_item), and update deletes that key when api_key or base_url is submitted and saved.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): new LunchflowItemsAccountCacheTest:

    • preloading caches under the connection's key, not the family's;
    • selecting accounts reads the connection's cached list;
    • changing the API key drops it;
    • changing the base URL drops it;
    • renaming keeps it.

    Before the change, the preload, select and API-key tests fail. The rename test passes before and after, as a guard.

  • Existing tests unchanged: LunchflowItemsControllerTest and the Lunch Flow model tests pass unedited (70 runs, 0 failures).

  • Mutations, each caught: the family-wide key again; the cache never dropped on a credentials change; dropped on every update; dropped only for the API key, not the base URL.

  • bin/rails test (full, at the C1c commit): 16,318 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 +16 / -4.

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

Blast radius

  • Only LunchflowItemsController reads or writes lunchflow_accounts_*.
  • fetch_lunchflow_accounts_from_api (setup) doesn't use the cache and is unchanged.

Summary by cubic

Keys Lunch Flow's cached accounts list per connection and prevents stale accounts from being re-cached after credentials change.

The cache key becomes lunchflow_accounts_<family>_<connection>; updates that submit api_key or base_url drop it, while renames and sync start date changes keep it. The settings panel always submits both fields, so every panel save drops the cache—one extra provider call, never stale data.

The provider kit's account flow, and the equivalent Lunch Flow controller helper, now record cache-sensitive credentials before fetching and write the list only if they still match afterwards, so a fetch that finishes after an update can't write the old credentials' accounts back into the cache. The re-read runs uncached because the request's query cache would otherwise answer with the stale row, and a fetch started with no API key on record caches nothing.

Old family-key entries expire within 5 minutes and are no longer read. Selection behavior is unchanged since Lunch Flow uses one credentialed connection per family.

Summary by CodeRabbit

  • Bug Fixes
    • Account lists are now kept separate for each connected account, preventing one connection’s results from appearing under another.
    • Changing connection credentials or the base URL clears the affected cached account list.
    • Account results are no longer cached if connection credentials change while they’re being fetched.
    • Renaming a connection preserves its cached account list.