feat(workflows): serve stale workflows list while revalidating#6965
Open
facumenzella wants to merge 2 commits into
Open
feat(workflows): serve stale workflows list while revalidating#6965facumenzella wants to merge 2 commits into
facumenzella wants to merge 2 commits into
Conversation
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>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist
purchases-androidand hybridsMotivation
Follow-up to #6961 (workflow detail stale-while-revalidate), addressing vegaro's review question: when the workflows list is stale,
getWorkflowsListblocks the caller on the network. Whoever waits ononComplete(offerings delivery on the fresh-offerings path) eats a round-trip even though a usableofferingId -> workflowIdmap is already cached.Description
getWorkflowsListnow 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 (andcachedWorkflowId(forOfferingId:)already does, regardless of staleness), but you can't serve a map you've never fetched.onCompleteimmediately (unchanged)onCompletenow), refresh + re-prefetch in the backgroundcachedWorkflowId(forOfferingId:)must resolve onceonCompletefires (this is whatdeliverEnsuringWorkflowsListrelies 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"]No new stale exposure: the in-memory map is already served stale by
cachedWorkflowId(forOfferingId:)between refreshes today. This just stops blockinggetOfferingson 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
facu/workflow-list-swr(stacked onfacu/workflow-detail-swr)Goal
Give the workflows list the same stale-while-revalidate behavior as the detail, so a stale-but-present
offeringId -> workflowIdmap 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
getWorkflowsright? 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
WorkflowsCache.hasCachedWorkflowsList(lock-free, present-regardless-of-staleness).fetchAndCacheWorkflowsList;getWorkflowsListnow branches fresh / stale-present (serve + background refresh) / cold (block).ifGeneration:param mirroringgetWorkflow.shouldStoreGetWorkflowsCompletions/completeStoredGetWorkflowstoMockWorkflowsAPIto control list-fetch timing.Human Decisions
Files / Symbols Touched
Sources/Purchasing/WorkflowManager.swift-getWorkflowsList, newfetchAndCacheWorkflowsList(... ifGeneration:).Sources/Caching/WorkflowsCache.swift-hasCachedWorkflowsList.Tests/UnitTests/Purchasing/WorkflowManagerTests.swift- 3 new tests.Tests/UnitTests/Mocks/MockWorkflowsAPI.swift- stored-completion support forgetWorkflows.Validation
xcodebuild test(iPhone 15 sim):WorkflowManagerTests(51),OfferingsManagerTests,PurchasesWorkflowTests,WorkflowsCacheTestsall green.swiftlinton changed files: clean.Validation Gaps
OfferingsManagerTestspass but were not confirmed to exercise the fresh-offerings + stale-but-present-list path specifically; that path is covered at theWorkflowManagerlevel.Review Focus
deliverEnsuringWorkflowsList's "map resolves right after getOfferings" invariant still holds.onCompleteon the SWR path; background write dropped on a racingclearCache().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
getWorkflowsListnow uses stale-while-revalidate for the workflows list, aligned with workflow detail and offerings: when the list is stale but already in memory,onCompleteruns 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
onCompleteuntil the fetch (and prefetches) finish, preserving the invariant thatcachedWorkflowId(forOfferingId:)can resolve aftergetOfferingswhen nothing was cached yet.WorkflowsCacheaddshasCachedWorkflowsListto distinguish present-vs-empty. List fetch logic moves intofetchAndCacheWorkflowsList, with the background refresh’s cache generation captured before serving so a late list write is dropped after login/logout.MockWorkflowsAPIgains deferredgetWorkflowscompletions; 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.