everysync: fold relay log latest-wins so edits and deletes propagate
- Commit
f59e7c71af7a0120671b81283057937f016a7139- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 10:53 UTC (29 days ago)
- Parents
2e44ac6
Bug: RelayMerge.indexRemote kept the FIRST envelope seen for a sync id and routed every later envelope for that id into the blank-id adoption path. The relay log is append-only and every edit/tombstone is a new append for the same id, so: - a peer's edit never replaced the original record (the fold always presented the original to the merge), and - the newer envelope was 'adopted' under a fresh random id, duplicating the record on every receiving device, while tombstones were shadowed the same way, so deletes never propagated either. Fix: the fold is latest-wins per sync id; only envelopes with a blank id go through adoption (the legacy-client path it exists for). Regression tests: RelayMergeFoldTest (later envelope wins, later tombstone deletes, identical duplicates merge once) plus the E2eeDriveTest notes cycle, where a remotely appended edit and tombstone must land locally. Existing RelayMerge tests still pass (the duplicateRemoteIdsSecondIsAdopted case merges identical duplicates, which both folds handle). (cherry picked from commit 825c27459771718c63176e290b434ceddef11f67)
Diff
everysync/src/main/java/com/vayunmathur/everysync/sync/RelayMerge.kt
diff --git a/everysync/src/main/java/com/vayunmathur/everysync/sync/RelayMerge.kt b/everysync/src/main/java/com/vayunmathur/everysync/sync/RelayMerge.ktindex d542da3df8..ebfd676ea7 100644--- a/everysync/src/main/java/com/vayunmathur/everysync/sync/RelayMerge.kt+++ b/everysync/src/main/java/com/vayunmathur/everysync/sync/RelayMerge.kt@@ -241,7 +241,13 @@ object RelayMerge { val unidentified = mutableListOf<T>() for (entity in remoteEntities) { val id = kind.syncId(entity).takeIf { it.isNotBlank() }- if (id != null && id !in remote) {+ if (id != null) {+ // The relay log is append-only: a later envelope for the same+ // id is the newer state of that record, so the fold is+ // latest-wins. (First-wins would shadow every edit and+ // tombstone behind the original append, and route the newer+ // envelope into the blank-id adoption path, duplicating the+ // record under a fresh random id.) remote[id] = entity } else { unidentified += entity
everysync/src/test/java/com/vayunmathur/everysync/sync/RelayMergeFoldTest.kt
diff --git a/everysync/src/test/java/com/vayunmathur/everysync/sync/RelayMergeFoldTest.kt b/everysync/src/test/java/com/vayunmathur/everysync/sync/RelayMergeFoldTest.ktnew file mode 100644index 0000000000..2257878b13--- /dev/null+++ b/everysync/src/test/java/com/vayunmathur/everysync/sync/RelayMergeFoldTest.kt@@ -0,0 +1,48 @@+package com.vayunmathur.everysync.sync++import com.vayunmathur.everysync.data.RemoteTodo+import com.vayunmathur.everysync.sync.adapters.TodoKind+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * Regression tests for the relay-log fold in [RelayMerge.merge]: the log is+ * append-only, so when one sync id appears more than once the LATER envelope+ * is the record's current state.+ */+class RelayMergeFoldTest {+ private fun todo(id: String, title: String, at: Long, deleted: Boolean = false) =+ RemoteTodo(uid = id, title = title, updatedAt = at, deleted = deleted)++ private fun base(e: RemoteTodo) = RelaySnapshot(+ syncId = e.uid, kind = TodoKind.kind, contentHash = TodoKind.contentHash(e),+ localUpdatedAt = e.updatedAt, remoteModified = e.updatedAt, lastSyncedAt = 0L,+ deleted = e.deleted,+ )++ @Test fun laterEnvelopeForSameIdWinsOverEarlier() {+ val v1 = todo("a", "Original", 100)+ val v2 = todo("a", "Edited", 200)+ val plan = RelayMerge.merge(TodoKind, listOf(v1), listOf(v1, v2), mapOf("a" to base(v1)), 1_000)+ // The edit is pulled as an update of "a" — not adopted as a new record+ assertEquals(1, plan.pulled)+ assertEquals(listOf(v2), plan.localUpserts)+ assertEquals(0, plan.pushed)+ }++ @Test fun laterTombstoneForSameIdDeletesLocally() {+ val live = todo("a", "Original", 100)+ val tomb = todo("a", "Original", 200, deleted = true)+ val plan = RelayMerge.merge(TodoKind, listOf(live), listOf(live, tomb), mapOf("a" to base(live)), 1_000)+ assertTrue(plan.localDeletes.any { it.uid == "a" } || plan.localUpserts.any { it.deleted },+ "deletes=${plan.localDeletes} upserts=${plan.localUpserts}")+ }++ @Test fun identicalDuplicatesStillMergeOnce() {+ val a = todo("x", "One", 100)+ val plan = RelayMerge.merge(TodoKind, emptyList(), listOf(a, a.copy()), emptyMap(), 1_000)+ assertEquals(1, plan.localUpserts.size)+ assertEquals("x", plan.localUpserts.single().uid)+ }+}