Modern Apps git

← Modern-Apps.git

findfamily-wear: snapshot alerts are filtered to the people the snapshot keeps

Commit
fd95c0609c8d52aab0d16f5b26d3c9699c7d0ae4
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:57 UTC (5 days ago)
Parents
ec6a30a
buildWearSnapshot dropped people to fit the byte cap but
passed alerts through verbatim, so a capped-out person's
AT_PLACE (or NOSHOW) alert survived with nobody to name:
the watch rendered '? at Home' / '? did not arrive at
School', and since alerts never shrank, the snapshot could
exceed the documented 90 KB cap even after every person was
dropped. snap() now filters alerts to the kept person ids
and caps alert place labels like every other field. The
phone copy of the protocol file needs the identical mirror
โ€” a follow-up is spawned. Two regression tests (2,000
candidates under the cap; an alert for an unknown person)
failed pre-fix. Ordering, ops and connection honesty were
audited and cleared. Verified: :findfamily-wear:
testDebugUnitTest 13/13, lint and assembleDev pass.

Diff

findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocol.kt
diff --git a/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocol.kt b/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocol.ktindex 579a32e156..83f8c51ce5 100644--- a/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocol.kt+++ b/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocol.kt@@ -247,7 +247,9 @@ fun wearRegionFor(people: List<WearPerson>): WearRegion {  * Builds the all-people snapshot: most-recently-updated first, field caps  * applied, then โ€” only to fit [WearFindFamilyProtocol.MAX_JSON_BYTES] โ€”  * tighter summarising, and only then dropping least-recent people with- * `truncated`/`droppedCount` set. Normal circles lose nobody.+ * `truncated`/`droppedCount` set. Normal circles lose nobody. Alerts are+ * kept only for people present in the snapshot (an alert the watch cannot+ * attribute to a named person is dropped, not rendered nameless).  */ fun buildWearSnapshot(     candidates: List<WearPerson>,@@ -263,16 +265,25 @@ fun buildWearSnapshot(             placeLabel = it.placeLabel.take(WearFindFamilyProtocol.MAX_PLACE_CHARS),         )     }-    fun snap(people: List<WearPerson>, dropped: Int) = WearFindFamilySnapshot(-        generatedAtMillis = generatedAtMillis,-        people = people,-        alerts = alerts,-        sharingEnabled = sharingEnabled,-        region = wearRegionFor(people),-        truncated = dropped > 0,-        droppedCount = dropped,-        totalPeople = sorted.size,-    )+    fun snap(people: List<WearPerson>, dropped: Int): WearFindFamilySnapshot {+        val keptIds = people.mapTo(HashSet()) { it.id }+        return WearFindFamilySnapshot(+            generatedAtMillis = generatedAtMillis,+            people = people,+            // An alert is only meaningful beside its person: drop alerts+            // whose person was capped out (or was never a candidate), so+            // the watch never renders an alert it cannot attribute, and+            // shrinking people also shrinks the payload toward the cap.+            alerts = alerts.filter { it.personId in keptIds }.map {+                it.copy(placeLabel = it.placeLabel.take(WearFindFamilyProtocol.MAX_PLACE_CHARS))+            },+            sharingEnabled = sharingEnabled,+            region = wearRegionFor(people),+            truncated = dropped > 0,+            droppedCount = dropped,+            totalPeople = sorted.size,+        )+    }     var kept = sorted     var snapshot = snap(kept, 0)     if (WearFindFamilyProtocol.json.encodeToString(snapshot).encodeToByteArray().size <=
findfamily-wear/src/test/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocolTest.kt
diff --git a/findfamily-wear/src/test/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocolTest.kt b/findfamily-wear/src/test/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocolTest.ktindex aade0d480b..5679333a6b 100644--- a/findfamily-wear/src/test/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocolTest.kt+++ b/findfamily-wear/src/test/java/com/vayunmathur/findfamily/wear/domain/WearFindFamilyProtocolTest.kt@@ -102,6 +102,42 @@ class WearFindFamilyProtocolTest {         assertTrue(bytes <= WearFindFamilyProtocol.MAX_JSON_BYTES, "snapshot $bytes over cap")     } +    @Test+    fun hardCapDropsAlertsForDroppedPeople() {+        val candidates = (1L..2_000L).map {+            person(it, it, name = "N".repeat(100), place = "P".repeat(120))+        }+        // The phone attaches one AT_PLACE alert per located person; the+        // oldest person also has a fired no-show. The byte cap drops the+        // oldest people, so their alerts must go with them: the watch+        // must never render an alert it cannot attribute to a person.+        val alerts = candidates.map {+            WearAlert(WearAlertKind.AT_PLACE, it.id, it.placeLabel, it.placeSinceMillis)+        } + WearAlert(WearAlertKind.NOSHOW, 1L, "School", 42L, fired = true)+        val snapshot = buildWearSnapshot(candidates, alerts = alerts, generatedAtMillis = 0L)+        assertTrue(snapshot.truncated)+        val keptIds = snapshot.people.map { it.id }.toSet()+        assertTrue(snapshot.alerts.isNotEmpty())+        assertTrue(snapshot.alerts.all { it.personId in keptIds })+        assertTrue(snapshot.alerts.none { it.kind == WearAlertKind.NOSHOW })+        val bytes = WearFindFamilyProtocol.json.encodeToString(snapshot).encodeToByteArray().size+        assertTrue(bytes <= WearFindFamilyProtocol.MAX_JSON_BYTES, "snapshot $bytes over cap")+    }++    @Test+    fun alertsForUnknownPeopleAreDropped() {+        val snapshot = buildWearSnapshot(+            listOf(person(1L, 10L), person(2L, 20L)),+            alerts = listOf(+                WearAlert(WearAlertKind.AT_PLACE, 1L, "Home", 5L),+                WearAlert(WearAlertKind.NOSHOW, 999L, "School", 6L, fired = true),+            ),+            generatedAtMillis = 0L,+        )+        assertFalse(snapshot.truncated)+        assertEquals(listOf(1L), snapshot.alerts.map { it.personId })+    }+     @Test     fun summarisingCapsFieldsBeforeDropping() {         val snapshot = buildWearSnapshot(