Skip to content

fix(persistence): generate puzzle locally when iCloud unavailable — offline-playable (#512) - #514

Merged
wei18 merged 3 commits into
mainfrom
fix/512-offline-puzzle-load
Jun 15, 2026
Merged

wei18 merged 3 commits into
mainfrom
fix/512-offline-puzzle-load

Conversation

@wei18

@wei18 wei18 commented Jun 15, 2026

Copy link
Copy Markdown
Owner

PR body placeholder

wei18 and others added 2 commits June 16, 2026 00:35
…ffline-playable (#512)

In SavedGameStore.loadOrCreate, wrap gateway.fetch in do-catch so any
error (notAuthenticated, network, zoneNotProvisioned) is treated as
"no existing save" instead of propagating to the caller. The
deterministic puzzleLoader path is now always reachable regardless of
iCloud state. The initial save after generating a fresh snapshot is
demoted to try? (best-effort) so a save failure also cannot block the
caller.

Swallowed fetch errors are reported via the existing gameSaveFailed
telemetry event (reason: "fetchFailed") so the iCloud-out event stays
observable in OSLog.

New tests in SavedGameCRUDTests:
- loadOrCreate_fetchError_returnsFreshLocalSnapshot
- loadOrCreate_fetchAndSaveError_returnsFreshLocalSnapshot

PersistenceKit: 63 tests green. SudokuKit: 272 tests green.
Protocol signature, PuzzleStore, and frozen seed vectors unchanged.
Lazy-CloudKit contract (PrivateCKGatewayFactory) untouched.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…oss (#512 CR)

Dual-model CR found a data-loss bug in the original #514 fix: treating any
fetch error as "no existing save" and then unconditionally saving the fresh
idle snapshot could overwrite a real in-progress game when the fetch failed
*transiently* (network blip) but the save then succeeded.

Option A (mirrors save-on-state-change, not save-on-load): set a fetchFailed
flag in the catch arm and only perform the initial best-effort save when
!fetchFailed. When the fetch errored we cannot confirm the absence of a
remote record, so we return the fresh snapshot for play WITHOUT persisting
it. The VM saves on the first move through the conflict-resolved path, so a
genuinely-new offline game still persists once the user acts, and we never
clobber an existing record.

CR follow-ups:
- FakePrivateCKGateway.fetchError is now private (mutated only via
  setFetchError(_:)) for Swift 6 actor isolation.
- Extracted the "fetchFailed" telemetry reason to a private static let
  (greppable contract, matches reason(for:) precedent).
- Reworded the stale comment that claimed the private save covers telemetry;
  we now skip the save entirely on fetch error.

Tests: replaced the weak both-fail test with a data-loss regression —
loadOrCreate_fetchErrorWithExistingSave_doesNotOverwrite seeds an existing
in-progress record, injects a transient fetch error, and asserts (a) a fresh
playable snapshot returns without throwing and (b) zero save ops occur / the
stored record is byte-identical. Kept the signed-out happy-path test.

PersistenceKit: 63 tests green. SudokuKit: 272 tests green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@wei18
wei18 merged commit 5d9be40 into main Jun 15, 2026
4 checks passed
@wei18
wei18 deleted the fix/512-offline-puzzle-load branch June 15, 2026 16:55
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.

1 participant