Skip to content

feat(workflows): serve stale workflows list while revalidating#6965

Open
facumenzella wants to merge 2 commits into
mainfrom
facu/workflow-list-swr
Open

feat(workflows): serve stale workflows list while revalidating#6965
facumenzella wants to merge 2 commits into
mainfrom
facu/workflow-list-swr

Conversation

@facumenzella

@facumenzella facumenzella commented Jun 9, 2026

Copy link
Copy Markdown
Member

Checklist

  • If applicable, unit tests
  • If applicable, create follow-up issues for purchases-android and hybrids

Motivation

Follow-up to #6961 (workflow detail stale-while-revalidate), addressing vegaro's review question: when the workflows list is stale, getWorkflowsList blocks the caller on the network. Whoever waits on onComplete (offerings delivery on the fresh-offerings path) eats a round-trip even though a usable offeringId -> workflowId map is already cached.

Stacked on #6961. Review/merge that first; this branches off it.

Description

getWorkflowsList now uses the same stale-while-revalidate shape as the detail, with one twist: for the list, the case that must block is an empty cache, not a stale one. You can serve a stale map (and cachedWorkflowId(forOfferingId:) already does, regardless of staleness), but you can't serve a map you've never fetched.

  • fresh: onComplete immediately (unchanged)
  • stale but present: serve the cached map (onComplete now), refresh + re-prefetch in the background
  • cold / empty: block on the fetch, since cachedWorkflowId(forOfferingId:) must resolve once onComplete fires (this is what deliverEnsuringWorkflowsList relies on)
flowchart TD
    A([getWorkflowsList]) --> B{list cache stale?}
    B -->|no, fresh| FRESH["onComplete now"]
    B -->|yes| P{map already cached?}
    P -->|"yes (stale but present)"| SWR["capture generation G,<br/>onComplete now,<br/>refresh + re-prefetch in background"]
    P -->|"no (cold / empty)"| COLD["block: fetch, then onComplete<br/>(map must resolve before getOfferings returns)"]
    SWR --> WRITE["guarded list write"]
    COLD --> WRITE
    WRITE --> G{generation still G?}
    G -->|yes| OK["update map + prefetch details"]
    G -->|"no: clearCache on login/logout"| DROP["drop write,<br/>keeps prev user's map out"]
Loading

No new stale exposure: the in-memory map is already served stale by cachedWorkflowId(forOfferingId:) between refreshes today. This just stops blocking getOfferings on the refresh. The background list write stays generation-guarded (and the guarding generation is captured before serving), so the #6961 cross-user guarantee holds.

AI session context

Metadata

Goal

Give the workflows list the same stale-while-revalidate behavior as the detail, so a stale-but-present offeringId -> workflowId map is served immediately instead of blocking the caller on the network.

Initial Prompt

vegaro approved #6961 and asked "We need to do the same for getWorkflows right? Otherwise it blocks on a stale cache." After confirming the cache exposes a present-vs-empty distinction (cachedList.value != nil), the human asked to open this follow-up.

Agent Contribution

  • Added WorkflowsCache.hasCachedWorkflowsList (lock-free, present-regardless-of-staleness).
  • Split the list fetch body into fetchAndCacheWorkflowsList; getWorkflowsList now branches fresh / stale-present (serve + background refresh) / cold (block).
  • Captured the background refresh's generation guard before serving (same fix codex flagged on feat(workflows): serve stale workflow detail while revalidating #6961's detail path), with an ifGeneration: param mirroring getWorkflow.
  • Added shouldStoreGetWorkflowsCompletions / completeStoredGetWorkflows to MockWorkflowsAPI to control list-fetch timing.
  • TDD: serve-stale-immediately test written first, watched fail (caller blocked) before implementing.

Human Decisions

Files / Symbols Touched

  • Sources/Purchasing/WorkflowManager.swift - getWorkflowsList, new fetchAndCacheWorkflowsList(... ifGeneration:).
  • Sources/Caching/WorkflowsCache.swift - hasCachedWorkflowsList.
  • Tests/UnitTests/Purchasing/WorkflowManagerTests.swift - 3 new tests.
  • Tests/UnitTests/Mocks/MockWorkflowsAPI.swift - stored-completion support for getWorkflows.

Validation

  • xcodebuild test (iPhone 15 sim): WorkflowManagerTests (51), OfferingsManagerTests, PurchasesWorkflowTests, WorkflowsCacheTests all green.
  • swiftlint on changed files: clean.
  • Manual verification: Not run.

Validation Gaps

  • The drop-after-clear test drives the generation guard directly; the true sub-statement race (a clear between reading the served map and capturing the generation) is the same lock-free read window the cache accepts by design and isn't deterministically reproducible here.
  • OfferingsManagerTests pass but were not confirmed to exercise the fresh-offerings + stale-but-present-list path specifically; that path is covered at the WorkflowManager level.

Review Focus

  • The block case is empty/cold, not stale (you can't serve a map you don't have); deliverEnsuringWorkflowsList's "map resolves right after getOfferings" invariant still holds.
  • Generation captured before onComplete on the SWR path; background write dropped on a racing clearCache().

Non-goals / Out of Scope


Note

Medium Risk
Changes offerings-adjacent list delivery and identity-generation guarding on background writes; behavior is well-tested but affects when getOfferings unblocks relative to network refresh.

Overview
getWorkflowsList now uses stale-while-revalidate for the workflows list, aligned with workflow detail and offerings: when the list is stale but already in memory, onComplete runs immediately and list fetch plus prefetch run in the background, so offerings delivery is not blocked on a network round-trip for a map that was already usable.

Only a cold/empty cache still blocks onComplete until the fetch (and prefetches) finish, preserving the invariant that cachedWorkflowId(forOfferingId:) can resolve after getOfferings when nothing was cached yet.

WorkflowsCache adds hasCachedWorkflowsList to distinguish present-vs-empty. List fetch logic moves into fetchAndCacheWorkflowsList, with the background refresh’s cache generation captured before serving so a late list write is dropped after login/logout. MockWorkflowsAPI gains deferred getWorkflows completions; three unit tests cover immediate stale serve, cold blocking, and generation-guarded drop on clear.

Reviewed by Cursor Bugbot for commit b57a78c. Bugbot is set up for automated code reviews on this repo. Configure here.

facumenzella and others added 2 commits June 9, 2026 12:17
Port of purchases-android#3540. getWorkflow now uses stale-while-revalidate
like OfferingsManager: a stale-but-present detail is served immediately and
the cache is refreshed in the background (a success updates the cache, a
failure is logged and swallowed). Prefetch opts out via
staleWhileRevalidate: false, so it keeps forcing a fresh fetch and persisting
its envelope instead of serving a stale value.

The background refresh captures its generation guard before serving the
cached value, so a clearCache() (login/logout) racing the serve still drops
the write, keeping the previous user's detail out of the new user's cache.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Follow-up to the workflow detail stale-while-revalidate change. getWorkflowsList
now serves a stale-but-present offeringId -> workflowId map immediately and
refreshes it in the background, so a caller waiting on onComplete (e.g. offerings
delivery) isn't blocked on the network. Only a cold/empty cache still blocks, since
there's no map to serve yet and cachedWorkflowId(forOfferingId:) must resolve once
onComplete fires.

The background refresh captures its generation guard before serving, so a
clearCache() (login/logout) racing the serve still drops the write, keeping the
previous user's map out of the new user's cache.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@facumenzella facumenzella added the pr:feat A new feature label Jun 9, 2026
@facumenzella facumenzella requested a review from vegaro June 9, 2026 15:39
@facumenzella facumenzella marked this pull request as ready for review June 9, 2026 15:39
@facumenzella facumenzella requested a review from a team as a code owner June 9, 2026 15:39
Base automatically changed from facu/workflow-detail-swr to main June 10, 2026 09:01
@facumenzella facumenzella added pr:other and removed pr:feat A new feature labels Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant