iCloud sync: the merge core, no transport yet - #47
Closed
sarthakagrawal927 wants to merge 1 commit into
Closed
Conversation
Training currently lives on one device, which every public surface now admits. This is the first half of fixing that: the merge, built and tested before any CloudKit code exists, because the merge is the only part of syncing that can silently destroy recorded training. Pure functions with an injected clock, so the rules can be reviewed and tested without a container or a network. Taking per-record CloudKit rather than SwiftData+CloudKit, which is the recommendation on #44 and is stated there as an assumption rather than a settled decision. A whole-document copy would force a person to choose between two versions of their training whenever two devices both wrote — a decision nobody can make correctly mid-workout. Per record the common cases resolve themselves. Rules, each with a test: - History is append-only and never resolves by time, so two phones that recorded different sessions end up with both, and a device with a wrong clock or one that never saw a workout cannot erase it. A session tombstone cannot win. - Templates and goals are last-writer-wins, with tombstones, so a delete propagates instead of the other device pushing the entity straight back. - Timestamp ties break on payload bytes, so two devices merging the same pair reach the same answer rather than disagreeing forever. - The active session is never synced. A workout in progress belongs to the phone in your hand. - Bundled templates are not records; shipping content should not travel through somebody's iCloud account. - Merging an already merged result proposes no further work. The round-trip test caught a real defect: `.iso8601` truncates to whole seconds, so a record did not decode back to the value it was encoded from. Every record would have looked freshly edited on each trip, and two edits inside one second would have tied on timestamp and fallen through to the arbitrary tie-break. Sync payloads now encode dates exactly. The export keeps ISO 8601, which is right for a file a person may open. `WorkoutTemplate` and `ExerciseGoal` deliberately gained no `updatedAt`. That puts sync bookkeeping inside the training model, where every mutation site has to maintain it and will be wrong the first time one forgets. A ledger fingerprints the encoded payload instead, which cannot be forgotten. No caller yet, by design. Transport, the iCloud entitlement, ledger persistence and real `CKAccountStatus` in Settings remain on #44, and sync stays unshipped until two-device convergence is verified on hardware. Settings still says sync is not active, which is still true. Native gate: 80 unit and 11 UI tests pass, release build succeeds, coverage 84.3236%. Duplication zero, complexity unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Stacked on #40 so its diff shows only the sync work. Draft because sync is not usable yet and must not be reported as working.
Part of #44.
Why the merge first, and alone
The merge is the only part of syncing that can silently destroy recorded training. So it is built and tested before any CloudKit code exists — pure functions with an injected clock, reviewable without a container, a network, or a device.
Taking per-record CloudKit over the originally planned SwiftData+CloudKit. That is stated as an assumption on #44 rather than a settled decision; the rules here are transport-agnostic and survive the change if you prefer SwiftData. SwiftData would mean rewriting the value-type domain into
@Modelreference types and breaking the 64 core tests, and a whole-document copy forces a person to choose between two versions of their training whenever two devices both wrote.The rules, each with a test
One real defect this caught
The round-trip test failed:
.iso8601truncates dates to whole seconds, so a record did not decode back to the value it was encoded from. Left in, every record would look freshly edited on each trip, and two edits inside one second would tie on timestamp and fall through to the arbitrary tie-break. Sync payloads now encode dates exactly; the export keeps ISO 8601, which is right for a file a person may open.A deliberate non-change
WorkoutTemplateandExerciseGoalgained noupdatedAtfield. That puts sync bookkeeping inside the training model, where every mutation site must maintain it and will be wrong the first time one forgets. ASyncLedgerfingerprints the encoded payload instead, which cannot be forgotten.Verification
Not done, and not claimed
Transport (container, zone,
CKRecordmapping, change tokens, retry), thecom.apple.developer.icloud-container-identifiersentitlement — which needs the Apple Developer account and so is an owner step — ledger persistence, and realCKAccountStatusin Settings. Settings still says sync is not active, which is still true.Two-device convergence cannot be verified from a simulator, so sync stays unshipped until it is checked on hardware.
🤖 Generated with Claude Code