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 {
417 lastAlertAt: trackedDomain.lastAlertAt, 417 lastAlertAt: trackedDomain.lastAlertAt,
418 monitoringState: trackedDomain.monitoringState, 418 monitoringState: trackedDomain.monitoringState,
419 pendingMonitoringAlerts: trackedDomain.pendingMonitoringAlerts, 419 pendingMonitoringAlerts: trackedDomain.pendingMonitoringAlerts,
420 collaboration: trackedDomain.collaboration 420 collaboration: trackedDomain.collaboration,
421 tags: trackedDomain.tags
421 ) 422 )
422 } 423 }
423 424
@@ -475,7 +476,8 @@ enum CloudSyncMerge {
475 lastAlertAt: [lhs.value.lastAlertAt, rhs.value.lastAlertAt].compactMap { $0 }.max(), 476 lastAlertAt: [lhs.value.lastAlertAt, rhs.value.lastAlertAt].compactMap { $0 }.max(),
476 monitoringState: preferredMonitoringState(lhs.value.monitoringState, rhs.value.monitoringState), 477 monitoringState: preferredMonitoringState(lhs.value.monitoringState, rhs.value.monitoringState),
477 pendingMonitoringAlerts: deduplicatedPendingAlerts(lhs.value.pendingMonitoringAlerts + rhs.value.pendingMonitoringAlerts), 478 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)
479 ), 481 ),
480 orderKey: mergedOrderKey(lhs.orderKey, rhs.orderKey) 482 orderKey: mergedOrderKey(lhs.orderKey, rhs.orderKey)
481 ) 483 )
@@ -488,6 +490,13 @@ enum CloudSyncMerge {
488 SyncOrderKey.isAtLeastAsRecent(lhs, rhs) ? lhs : rhs 490 SyncOrderKey.isAtLeastAsRecent(lhs, rhs) ? lhs : rhs
489 } 491 }
490 492
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
491 private static func preferredTrackedDomain( 500 private static func preferredTrackedDomain(
492 _ lhs: SyncRow<TrackedDomain>, 501 _ lhs: SyncRow<TrackedDomain>,
493 _ rhs: SyncRow<TrackedDomain> 502 _ rhs: SyncRow<TrackedDomain>
DomainDigTests/CloudSyncMergeTests.swift +43 −1
@@ -204,10 +204,51 @@ final class CloudSyncMergeTests: XCTestCase {
204 Date(timeIntervalSinceReferenceDate: offset) 204 Date(timeIntervalSinceReferenceDate: offset)
205 } 205 }
206 206
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
207 private func trackedDomainRow( 247 private func trackedDomainRow(
208 _ domain: String, 248 _ domain: String,
209 note: String? = nil, 249 note: String? = nil,
210 isPinned: Bool = false, 250 isPinned: Bool = false,
251 tags: [String] = [],
211 serverDate: Date?, 252 serverDate: Date?,
212 deviceDate: Date 253 deviceDate: Date
213 ) -> SyncRow<TrackedDomain> { 254 ) -> SyncRow<TrackedDomain> {
@@ -217,7 +258,8 @@ final class CloudSyncMergeTests: XCTestCase {
217 createdAt: at(0), 258 createdAt: at(0),
218 updatedAt: deviceDate, 259 updatedAt: deviceDate,
219 note: note, 260 note: note,
220 isPinned: isPinned 261 isPinned: isPinned,
262 tags: tags
221 ), 263 ),
222 orderKey: SyncOrderKey(serverDate: serverDate, deviceDate: deviceDate) 264 orderKey: SyncOrderKey(serverDate: serverDate, deviceDate: deviceDate)
223 ) 265 )