Modern Apps git

← Modern-Apps.git

contacts-wear: chunks arriving before their manifest are kept for their own generation

Commit
c3ba42c7498d0aab74eaabe25dde90ec8d5c7be9
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:48 UTC (5 days ago)
Parents
6a7fb73
WearSnapshotReassembler discarded any chunk whose generation
differed from the current manifest. The protocol says chunks
'arrive in any order', but after gen 5 completed, gen 6's
chunks arriving before its manifest hit the gen-5 manifest and
were dropped unstaged; onManifest(gen 6) then cleared the
cache and could never complete. No re-delivery follows, so
the watch stayed at gen 5 forever: renamed contacts stale,
deleted contacts still shown and tappable, new ones missing.

onChunk now caches newer-than-manifest chunks (superseding
older cached generations, ignoring older chunks), and
onManifest retains only chunks matching the new generation
before completing. Two regression tests: gen-6-chunks-before-
manifest applies; a stale gen-4 chunk cached pre-manifest
does not pollute gen 5. Search, detail lookup and action
targeting were audited and cleared. Verified: :contacts-wear:
testDebugUnitTest 10 tests and lint BUILD SUCCESSFUL.

Diff

contacts-wear/src/main/java/com/vayunmathur/contacts/wear/data/WearContactsStore.kt
diff --git a/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/data/WearContactsStore.kt b/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/data/WearContactsStore.ktindex 1bcf076bbd..4a171febdd 100644--- a/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/data/WearContactsStore.kt+++ b/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/data/WearContactsStore.kt@@ -28,20 +28,29 @@ class WearSnapshotReassembler {      /** Offer the manifest; returns the completed snapshot, or null while incomplete. */     fun onManifest(newManifest: WearContactsManifest): WearSnapshot? {-        if (manifest != null && manifest?.generation != newManifest.generation) chunks.clear()         manifest = newManifest+        // Chunks may arrive before their manifest (any-order delivery): keep the+        // ones for this generation, drop every other generation's leftovers.+        chunks.values.removeAll { it.generation != newManifest.generation }         return completeOrNull()     }      /** Offer one chunk; stale generations are ignored. */     fun onChunk(chunk: WearContactsChunk): WearSnapshot? {-        val current = manifest ?: run {+        val current = manifest+        if (current != null && chunk.generation < current.generation) return null+        if (current != null && chunk.generation == current.generation) {             chunks[chunk.index] = chunk-            return null+            return completeOrNull()         }-        if (chunk.generation != current.generation) return null+        // No manifest yet, or a newer generation whose manifest has not arrived:+        // cache the chunk instead of dropping it. A chunk older than the newest+        // cached generation is stale; a newer one supersedes cached leftovers.+        val cachedGeneration = chunks.values.maxOfOrNull { it.generation }+        if (cachedGeneration != null && chunk.generation < cachedGeneration) return null+        if (cachedGeneration != null && chunk.generation > cachedGeneration) chunks.clear()         chunks[chunk.index] = chunk-        return completeOrNull()+        return null     }      private fun completeOrNull(): WearSnapshot? {
contacts-wear/src/test/java/com/vayunmathur/contacts/wear/data/WearSnapshotReassemblerTest.kt
diff --git a/contacts-wear/src/test/java/com/vayunmathur/contacts/wear/data/WearSnapshotReassemblerTest.kt b/contacts-wear/src/test/java/com/vayunmathur/contacts/wear/data/WearSnapshotReassemblerTest.ktindex 569ba1377b..78e820929d 100644--- a/contacts-wear/src/test/java/com/vayunmathur/contacts/wear/data/WearSnapshotReassemblerTest.kt+++ b/contacts-wear/src/test/java/com/vayunmathur/contacts/wear/data/WearSnapshotReassemblerTest.kt@@ -23,6 +23,39 @@ class WearSnapshotReassemblerTest {         assertEquals(listOf("A", "B", "C"), done?.contacts?.map { it.name })     } +    @Test+    fun `next generation chunks arriving before its manifest are applied`() {+        val r = WearSnapshotReassembler()+        r.onManifest(manifest)+        r.onChunk(c0)+        assertEquals(3, r.onChunk(c1)?.contacts?.size)++        // Any-order delivery on the next sync: both gen-6 chunks land while the+        // gen-5 manifest is still current, then the gen-6 manifest arrives.+        // Previously the gen-6 chunks were dropped as "stale" and the watch+        // kept showing the gen-5 snapshot forever.+        val manifest6 = WearContactsManifest(6L, 2, 3, 3, false, 2L)+        val d0 = WearContactsChunk(6L, 0, listOf(WearContact(4L, "D"), WearContact(5L, "E")))+        val d1 = WearContactsChunk(6L, 1, listOf(WearContact(6L, "F")))+        assertNull(r.onChunk(d0))+        assertNull(r.onChunk(d1))+        val done = r.onManifest(manifest6)+        assertEquals(listOf("D", "E", "F"), done?.contacts?.map { it.name })+        assertEquals(6L, done?.manifest?.generation)+    }++    @Test+    fun `stale chunk cached before any manifest does not pollute the next generation`() {+        val r = WearSnapshotReassembler()+        assertNull(r.onChunk(c0.copy(generation = 4L)))+        assertNull(r.onChunk(c1))+        // Gen-4 chunk at index 0 was superseded by the gen-5 cache and the+        // gen-5 manifest must not complete from it.+        assertNull(r.onManifest(manifest))+        val done = r.onChunk(c0)+        assertEquals(listOf("A", "B", "C"), done?.contacts?.map { it.name })+    }+     @Test     fun `stale generation chunks are ignored`() {         val r = WearSnapshotReassembler()