Commit 6ede948cc1

6ede948cc1f1578b6dc2da759210bdb6ae7c2341

parent: 8391ea9358

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 04:08 UTC

Carry tags through the sync merge

payloadTrackedDomain and mergedTrackedDomain rebuilt TrackedDomain field by
field and omitted tags, so it fell back to the initialiser's empty default.
applyMergedPayload writes the merged payload back through saveTrackedDomains,
so the first sync emptied every tracked domain's tags and the watchlist tag
filter with them.

The merge treats an empty list as "nothing to say" rather than "cleared":
records written before this decode with no tags at all, so a winning side
that has none must not erase the other's.

Layout: unified · split

DomainDig/CloudSyncMerge.swift +11 −2
@@ -417,7 +417,8 @@ enum CloudSyncMerge {
417417 lastAlertAt: trackedDomain.lastAlertAt,
418418 monitoringState: trackedDomain.monitoringState,
419419 pendingMonitoringAlerts: trackedDomain.pendingMonitoringAlerts,
420 collaboration: trackedDomain.collaboration
420 collaboration: trackedDomain.collaboration,
421 tags: trackedDomain.tags
421422 )
422423 }
423424
@@ -475,7 +476,8 @@ enum CloudSyncMerge {
475476 lastAlertAt: [lhs.value.lastAlertAt, rhs.value.lastAlertAt].compactMap { $0 }.max(),
476477 monitoringState: preferredMonitoringState(lhs.value.monitoringState, rhs.value.monitoringState),
477478 pendingMonitoringAlerts: deduplicatedPendingAlerts(lhs.value.pendingMonitoringAlerts + rhs.value.pendingMonitoringAlerts),
478 collaboration: preferredCollaboration(lhs.value.collaboration, rhs.value.collaboration)
479 collaboration: preferredCollaboration(lhs.value.collaboration, rhs.value.collaboration),
480 tags: firstNonEmpty(winner.tags, lhs.value.tags, rhs.value.tags)
479481 ),
480482 orderKey: mergedOrderKey(lhs.orderKey, rhs.orderKey)
481483 )
@@ -488,6 +490,13 @@ enum CloudSyncMerge {
488490 SyncOrderKey.isAtLeastAsRecent(lhs, rhs) ? lhs : rhs
489491 }
490492
493 /// Records written before tags were carried decode with none at all, so an
494 /// empty list means "this side has nothing to say" rather than "cleared".
495 /// Without the fallback the first sync after the fix would still drop them.
496 private static func firstNonEmpty(_ candidates: [String]...) -> [String] {
497 candidates.first { !$0.isEmpty } ?? []
498 }
499
491500 private static func preferredTrackedDomain(
492501 _ lhs: SyncRow<TrackedDomain>,
493502 _ rhs: SyncRow<TrackedDomain>
DomainDigTests/CloudSyncMergeTests.swift +43 −1
@@ -204,10 +204,51 @@ final class CloudSyncMergeTests: XCTestCase {
204204 Date(timeIntervalSinceReferenceDate: offset)
205205 }
206206
207 // MARK: Tags
208
209 /// `payloadTrackedDomain` rebuilds the row field by field, and every field
210 /// it forgets is written back to the local store by `applyMergedPayload`.
211 /// It used to forget `tags`, so one sync emptied every tracked domain's
212 /// tags and the watchlist's tag filter with them.
213 func testTagsSurviveThePayloadSanitizer() {
214 let trackedDomain = TrackedDomain(
215 domain: "example.com",
216 createdAt: at(0),
217 updatedAt: at(100),
218 tags: ["client-a", "production"]
219 )
220
221 let sanitized = CloudSyncMerge.payloadTrackedDomain(trackedDomain)
222
223 XCTAssertEqual(sanitized.tags, ["client-a", "production"])
224 }
225
226 func testTagsSurviveAMerge() {
227 let local = trackedDomainRow("example.com", tags: ["client-a"], serverDate: at(100), deviceDate: at(100))
228 let remote = trackedDomainRow("example.com", tags: ["client-b"], serverDate: at(200), deviceDate: at(200))
229
230 let merged = CloudSyncMerge.mergeTrackedDomains([local], [remote])
231
232 XCTAssertEqual(merged.domains.first?.value.tags, ["client-b"])
233 }
234
235 /// Records written before tags were carried decode with none at all, so an
236 /// empty list means "this side has nothing to say", not "cleared".
237 /// Otherwise the first sync after the fix would still drop them.
238 func testTagsFallBackWhenTheWinningSideHasNone() {
239 let local = trackedDomainRow("example.com", tags: ["client-a"], serverDate: at(100), deviceDate: at(100))
240 let legacyRemote = trackedDomainRow("example.com", tags: [], serverDate: at(200), deviceDate: at(200))
241
242 let merged = CloudSyncMerge.mergeTrackedDomains([local], [legacyRemote])
243
244 XCTAssertEqual(merged.domains.first?.value.tags, ["client-a"])
245 }
246
207247 private func trackedDomainRow(
208248 _ domain: String,
209249 note: String? = nil,
210250 isPinned: Bool = false,
251 tags: [String] = [],
211252 serverDate: Date?,
212253 deviceDate: Date
213254 ) -> SyncRow<TrackedDomain> {
@@ -217,7 +258,8 @@ final class CloudSyncMergeTests: XCTestCase {
217258 createdAt: at(0),
218259 updatedAt: deviceDate,
219260 note: note,
220 isPinned: isPinned
261 isPinned: isPinned,
262 tags: tags
221263 ),
222264 orderKey: SyncOrderKey(serverDate: serverDate, deviceDate: deviceDate)
223265 )