Skip to content

test(e2e): close the N19 clear-cache resumeCandidate load race + harden the Cancel half - #957

Merged
wei18 merged 1 commit into
mainfrom
test/stabilize-n19-flake-956
Jul 28, 2026
Merged

wei18 merged 1 commit into
mainfrom
test/stabilize-n19-flake-956

Conversation

@wei18

@wei18 wei18 commented Jul 28, 2026

Copy link
Copy Markdown
Owner

Closes #956

Root cause (corrected from the issue's original hypothesis)

The issue suspected the Cancel half's fixed backdrop-tap coordinate. That
turned out to be wrong: repeat-running test_clearCacheCancelAndFailureToast_N19
under contention reproduced the flake at a consistent ~15-50% rate, always
at the SAME assertion (should raise the failure toast), and the
Cancel/backdrop-dismiss half succeeded in every single reproduction.

The real bug is a precondition race in SettingsViewModel.clearCache()
(Packages/GameAppKit/Sources/GameAppKit/SettingsViewModel.swift):
deleteAbandoned only runs if let candidate = resumeCandidate, and
resumeCandidate is populated by bootstrap()'s async
persistence.latestInProgress() fetch, fired from SettingsView's .task.
If that fetch hadn't resolved by the time the test tapped Confirm — worse
under host CPU contention — clearCache() silently fell through to the
unconditional SUCCESS toast branch instead of the failure one, so the
failure toast the test waits for never appeared.

What changed

Product (closes the race):

  • SettingsViewModel.isCacheStateReady: Bool — flips true once
    bootstrap() has resolved resumeCandidate either way.
  • SettingsStorageSection renders a zero-size, non-localized E2E
    precondition anchor (settings.storage.cacheReady) as a .background
    SIBLING marker (not a container id — test(e2e): cover sudoku hub failure flows N3-N5 (#935) #937 lesson) only while
    isCacheStateReady is true, mirroring DailyHubView's
    sudoku.dailyHub.root pattern. No user-visible text, so no L10n work.
  • Threaded through SettingsStorageSection → SettingsScreen →
    GameAppKit.SettingsView (both apps share this composition — mirror
    principle). Default true on the intermediate inits keeps every
    preview/snapshot/unit-test call site unchanged.

Test (App/UITestsShared/NegativeNavigationE2ESupport+ClearCache.swift):

  • Waits for settings.storage.cacheReady (15s, matching this file's own
    ceiling for "an app-level async load settled" waits) BEFORE tapping the
    Clear cache row at all — closes the race structurally instead of hoping
    the timing works out.
  • Also hardened the Cancel/backdrop half per the issue's direction (a),
    confirmed correct by repeat-running (every pre-fix failure was at the
    toast assertion, never here): wait for the confirm button to be
    genuinely isHittable (not merely exists) before every tap, and
    re-query a FRESH handle after reopening instead of reusing the
    pre-Cancel one.
  • Cancel now taps the popover's own PopoverDismissRegion backdrop
    accessibility element (discovered via idb ui_describe_all — a
    discrete, full-screen element the system already exposes) instead of a
    fixed normalized-offset coordinate, per direction (b) — self-adapts to
    layout instead of guessing a safe point.
  • Split into NegativeNavigationE2ESupport+ClearCache.swift to keep the
    main file under the 400-line SwiftLint ceiling (same pattern as the
    existing +Restore.swift split).

Docs: docs/screen-contracts.md — SETTINGS storage row inventory +
CLEAR-CACHE-DIALOG note updated with the real root cause and the new
anchor, replacing the incorrect backdrop-coordinate framing.

Considered and rejected

Resolving the DEBUG fake's latestInProgress() synchronously/eagerly,
bypassing the Task hop for the E2E path specifically — rejected. The race
is in the VIEW MODEL's .task/Task scheduling, not the fake's own latency,
so a synchronous fake would still be a Task hop behind the confirm tap; it
wouldn't remove the race. It would also make the E2E path exercise a
different load sequence than production, which defeats the point of a
host-driven E2E test in the first place.

Verification

Diagnosis (pre-fix): reproduced 7/16 under deliberate dual-suite
concurrent-build contention; also 3/16 in a clean single-suite sequential
run with no deliberate contention (1 Sudoku, 2 Minesweeper) — confirming
it's a genuine race, not purely a contention artifact.

Post-fix, 10x -only-testing: sequential per app (raw counts, not
cherry-picked):

  • Sudoku: 8/10 pass (iterations 9, 10 failed)
  • Minesweeper: 9/10 pass (iteration 8 failed)
  • 0/20 recurrences of the original "should raise the failure toast"
    assertion
    — the bug is structurally closed. All 3 residual failures
    are at the NEW isCacheStateReady anchor wait (a bounded 15s wait),
    measured during a period of load average 18-22 (uptime) — this
    session was running ~12 concurrent Claude Code agents at the time. A
    bounded wait can be exceeded by an unbounded contention source; that's
    a different failure class than test(e2e): stabilize the N19 clear-cache backdrop-tap flake #956 and not something a larger timeout
    structurally fixes.

Final confirmation pass at settled load (uptime: load averages 6.31 8.19
11.17 — single digits): mise run test:ui all → both suites clean —
Sudoku-E2E 19/19 ("Executed 19 tests, with 0 failures"), Minesweeper-E2E
15/15 ("Executed 15 tests, with 0 failures"), test_clearCacheCancelAndFailureToast_N19
passing in both (21.4s / 19.8s). This converts the contention attribution
above into a measurement: at normal load both suites are clean; all 3
residual failures across the 20x batches happened only during a load spike
(18-22).

mise run lint:swift_strict on all changed files → 0 violations.

Unit/snapshot tests unaffected: swift test --package-path Packages/SettingsKit
(51/51), swift test --package-path Packages/SudokuKit --filter SettingsView
(22/22, including snapshots — the zero-size marker doesn't change rendered
pixels), swift test --package-path Packages/MinesweeperKit --filter SettingsView
(9/9).

🤖 Generated with Claude Code

…en the Cancel half (#956)

Re-diagnosed the intermittent test_clearCacheCancelAndFailureToast_N19
failure: the issue's original "fixed backdrop coordinate" hypothesis was
wrong. Repeat-running under contention reproduced the flake at ~15-50%,
always at the same assertion ("should raise the failure toast"), with the
Cancel/backdrop-dismiss half always succeeding. Root cause: a race in
SettingsViewModel.clearCache() — deleteAbandoned only runs `if let
candidate = resumeCandidate`, and resumeCandidate is populated by
bootstrap()'s async persistence.latestInProgress() fetch. If that hadn't
resolved by the time the test tapped Confirm, clearCache() silently fell
through to the unconditional success toast instead of the failure one.

Closes the race product-side: SettingsViewModel.isCacheStateReady flips
true once bootstrap() settles either way; SettingsStorageSection renders a
zero-size, non-localized settings.storage.cacheReady sibling marker only
while it's true (mirrors DailyHubView's sudoku.dailyHub.root pattern, no
container id per the #937 lesson, no L10n work needed). Threaded through
SettingsScreen -> GameAppKit.SettingsView (both apps share the composition).
The E2E test now waits for that anchor before touching the row at all.

Also hardens the Cancel/backdrop half per the issue's directions: waits
for the confirm button to be isHittable (not merely exists) before every
tap, re-queries a fresh handle after reopening instead of reusing the
pre-Cancel one, and taps the popover's own PopoverDismissRegion backdrop
element (discovered via idb ui_describe_all) instead of a fixed
normalized-offset coordinate.

Split the clear-cache helper into NegativeNavigationE2ESupport+ClearCache.swift
to keep the main file under the 400-line SwiftLint ceiling, and updated
docs/screen-contracts.md accordingly.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wei18
wei18 merged commit 972d7a7 into main Jul 28, 2026
5 checks passed
@wei18
wei18 deleted the test/stabilize-n19-flake-956 branch July 28, 2026 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(e2e): stabilize the N19 clear-cache backdrop-tap flake

1 participant