Order sync conflicts by the server's record date, and resume zone fetches from a stored token !7

merged merged by cmc on 2026-10-24 03:52 UTC · krz/domain-dig:sync-server-dates into main

Discussion

cmc

Two of the three changes asked for. The tombstone ageing is deliberately not here; see below.

Server dates decide conflicts

Every merge decision compared updatedAt/deletedAt, both minted by Date() on the writing device, so which device won depended on whose clock was right. CKRecord.modificationDate is now carried through decodeRecords and is the authority; the payload dates remain display metadata.

The nil case is explicit: a row has no server date until it has been pushed, and such a row wins, because the alternative discards edits made offline. To tell a pushed row from an edited one, the server date seen for each record is persisted under cloudSync.rowAuthority together with the payload date the row carried at that moment. A row whose payload date has since moved reads as unpushed. Both dates in that comparison come from the same device, so no cross-device clock comparison is left.

A tombstone and a row with equal authority dates keep the row deleted, matching the previous > comparison: resurrecting a deleted row is worse than losing a re-add.

The decision logic moved to CloudSyncMerge as pure static functions. CloudSyncService is a @MainActor @Observable singleton wired to CKContainer.default(), so nothing on it can be instantiated in a test.

Change token

fetchZoneChanges always started from a nil previousServerChangeToken and never kept the token it reached, so every launch refetched the whole zone. Tokens are now stored per zone and database scope under cloudSync.changeToken.* and committed only after the merge is on disk and the push has succeeded. CKError.changeTokenExpired drops the token and refetches that zone in full, once per zone per sync.

Tests

17 new tests in DomainDigTests/CloudSyncMergeTests.swift; 98 total, 0 failures.

Covered: row-versus-row ordering in both argument orders; the clock-skew regression (a future updatedAt with an older server date loses); unpushed-row-wins; delete-then-re-add and re-add-then-delete; the tombstone/row tie; an unpushed deletion beating a row the server still holds; workflow deletes; latestTombstones deduplication; the 200-row cap keeping the newest; notes and history metadata following the same rule, including against a tombstone.

Not tested

A unit test cannot exercise CloudKit, so everything below is reasoned about rather than verified:

  • That CKRecord.modificationDate is populated on fetched records and on the records modifyRecords returns, which is where every server date in the new bookkeeping comes from.
  • Change token persistence, resumption, and the changeTokenExpired recovery path, including the moreComing drain across more than one pass.
  • The round trip of cloudSync.rowAuthority: that a row's payload date, after applyMergedPayload writes it and the next sync reads it back, still equals the date recorded with its server date. The authority is recorded from a reload for exactly this reason, since the local stores round-trip dates through ISO-8601 and through JSONEncoder.
  • Anything involving a real CKShare participant.

Known limitations

  • The first sync after this lands has no recorded server dates, so every local row counts as unpushed and wins that one merge. Thereafter the bookkeeping is in place. This is no worse than the clock-dependent behaviour it replaces, but it does mean the last device to sync after the update wins once.
  • pushMergedPayload still saves every record on every sync, so the change token's benefit is limited: a device's own push bumps the server dates the next fetch will see. Making the push conditional on the recorded authority is a separate change and was left out of this one.

Tombstone ageing: not implemented

Nothing deletes a SyncTombstone record from the zone, so old ones are re-fetched and re-merged forever. That is unchanged here.

A per-zone last-successful-sync timestamp only records when this device last synced. It says nothing about when the account's other devices, or a CKShare participant, last consumed a tombstone, and deleting one before they have resurrects the row on those devices. There is no per-device or per-participant acknowledgement anywhere in the zone to drive the deletion off, and adding one needs a new record type (or a new queryable field) deployed to the production CloudKit schema for iCloud.net.cleberg.DomainDig — a dashboard change that is not in this MR and that the code must not assume. Expiring an acknowledgement that stops arriving would also reintroduce the guessed age window that was ruled out.

A tombstone that lives too long costs a record and a merge pass. A resurrected row costs the user their data. Left as is.

No schema change

No new record types and no new fields: the server dates come from CKRecord metadata, and the local bookkeeping is in UserDefaults.