findfamily: latest-per-person is one row, tie-broken by storage order
- Commit
f71eca408d7b6b0df00a02192af62d634380a0f1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 07:06 UTC (5 days ago)
- Parents
33f5670
Room persists Instants as epoch seconds, so a live fix and the shutdown parting report that supersedes it (fresh send stamp, deliberately stale measured time) can share one second. The old getLatest query matched every row at MAX(reportedAt) with no tiebreak, returning two 'latest' rows for one person; every consumer then associateBy's them into whichever the unordered query happened to return last. The parting report — documented to win so the phone doesn't show as live at a stale pin — could silently lose to the heartbeat it superseded, on map pins, cards, the no-show scheduler and the wear snapshot alike. The query now selects exactly one row per user: highest reportedAt, ties to the last-stored row (storage order is ingest-acceptance order per sender, hence the newest report). Deliberately not tiebroken on timestamp: a parting report's measured time is older by design and must not lose. IngestSequencing's reader-behaviour paragraph is corrected. Two regression tests failed pre-fix on the tie. Sharing, geofences, spools and member cascades were audited and cleared. Verified: :findfamily:testDevUnitTest 345 tests pass; lint errors are pre-existing on untouched files.
Diff
findfamily/src/main/java/com/vayunmathur/findfamily/data/FFDatabase.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/data/FFDatabase.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/data/FFDatabase.ktindex bcdf69955b..d0caf5453c 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/data/FFDatabase.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/data/FFDatabase.kt@@ -23,10 +23,22 @@ interface LocationValueDao { * `timestamp` would let the live heartbeat that preceded them win and the parting report * would never reach the UI — stored, but invisible. For live-only data the two columns are * equal and this behaves exactly as it always did.+ *+ * Exactly one row per user, even when two reports tie on the stored+ * `reportedAt`: instants persist as epoch seconds, so a live heartbeat+ * and a parting report sent later in the same second (or an equal-stamp+ * ingest hash-win stored by a later batch) share the maximum. The tie+ * goes to the row stored last (highest `id`): per sender, storage order+ * is acceptance order under the ingest sequencing, so the last-stored+ * tied row is the newest report. Returning both rows instead would let+ * each consumer's `associateBy { userid }` pick one in unspecified+ * query order, and the parting report could silently lose to the live+ * fix it superseded. */ @Query(- "SELECT * FROM LocationValue WHERE (userid, reportedAt) IN " +- "( SELECT userid, MAX(reportedAt) FROM LocationValue GROUP BY userid )"+ "SELECT * FROM LocationValue AS latest WHERE latest.id = ( SELECT candidate.id " ++ "FROM LocationValue AS candidate WHERE candidate.userid = latest.userid " ++ "ORDER BY candidate.reportedAt DESC, candidate.id DESC LIMIT 1 )" ) fun getLatest(): Flow<List<LocationValue>>
findfamily/src/main/java/com/vayunmathur/findfamily/domain/IngestSequencing.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/domain/IngestSequencing.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/domain/IngestSequencing.ktindex 26b3eed235..30f7e40ed3 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/domain/IngestSequencing.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/domain/IngestSequencing.kt@@ -53,12 +53,11 @@ import kotlin.time.Instant * appending a second same-second row. A win on time appends normally — * a batch's genuine progression stays in history. *- * One residue is deliberately left alone: if a higher-hash fix arrives in a- * *later batch* than an equal-stamp lower-hash fix, both rows exist in- * history (history is append-only by design — the scrubber needs it), and- * `getLatest()`'s MAX(reportedAt) tie between them is the pre-existing- * reader behaviour. It self-resolves on the sender's next fix and is bounded- * to same-second collisions.+ * If a higher-hash fix arrives in a *later batch* than an equal-stamp+ * lower-hash fix, both rows exist in history (history is append-only by+ * design — the scrubber needs it). `getLatest()` breaks the stored-second+ * tie by storage order, so the later batch's winning fix is the one every+ * reader sees. * * ## Granularity: one second, because that is what storage keeps *
findfamily/src/test/java/com/vayunmathur/findfamily/data/GetLatestTiebreakTest.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/data/GetLatestTiebreakTest.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/data/GetLatestTiebreakTest.ktnew file mode 100644index 0000000000..6fe40e28d2--- /dev/null+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/data/GetLatestTiebreakTest.kt@@ -0,0 +1,100 @@+package com.vayunmathur.findfamily.data++import androidx.room3.Room+import androidx.test.core.app.ApplicationProvider+import androidx.test.ext.junit.runners.AndroidJUnit4+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.test.runTest+import org.junit.runner.RunWith+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.time.Instant++/**+ * Regression tests for [LocationValueDao.getLatest] when two stored reports+ * for one person tie on the stored `reportedAt`.+ *+ * Room persists instants as epoch seconds, so a live heartbeat sent at+ * x.4 s and a shutdown (or battery-low) parting report sent at x.6 s — the+ * parting report deliberately republishes an older measured fix under a+ * fresh send stamp — land in the same stored second. An equal-stamp+ * hash-win arriving in a later ingest batch (see `IngestSequencing`) stores+ * a second row the same way. The latest-per-user query must still return+ * exactly one row per person: the one stored last, which is the one the+ * ingest ordering accepted last. Returning both rows lets every consumer's+ * `associateBy { userid }` pick one in unspecified query order, so the+ * parting report can silently lose to the live fix it superseded and the+ * person keeps showing as live at a stale position.+ */+@RunWith(AndroidJUnit4::class)+class GetLatestTiebreakTest {++ private lateinit var db: FFDatabase++ @BeforeTest+ fun open() {+ db = Room.inMemoryDatabaseBuilder(+ ApplicationProvider.getApplicationContext(),+ FFDatabase::class.java,+ ).allowMainThreadQueries().build()+ }++ @AfterTest+ fun close() {+ db.close()+ }++ private fun fix(+ userid: Long,+ sentAtMs: Long,+ measuredAtMs: Long,+ lat: Double,+ source: LocationSource = LocationSource.LIVE,+ ) = LocationValue(+ userid = userid,+ coord = Coord(lat, 2.0),+ speed = 0f,+ acc = 5f,+ timestamp = Instant.fromEpochMilliseconds(measuredAtMs),+ battery = 50f,+ reportedAt = Instant.fromEpochMilliseconds(sentAtMs),+ source = source,+ )++ @Test+ fun sameSecondPartingReportSupersedesLiveFix() = runTest {+ val dao = db.locationValueDao()+ // Live heartbeat measured and sent in second 2000 (at .4 s), then a+ // shutdown parting report sent later in the same stored second+ // (at .6 s) carrying a fix measured back in second 1000. Both rows+ // persist reportedAt = 2000 s, so they tie on MAX(reportedAt).+ dao.upsert(fix(userid = 7L, sentAtMs = 2_000_400L, measuredAtMs = 2_000_400L, lat = 10.0))+ dao.upsert(+ fix(+ userid = 7L,+ sentAtMs = 2_000_600L,+ measuredAtMs = 1_000_000L,+ lat = 20.0,+ source = LocationSource.SHUTDOWN,+ ),+ )+ val latest = dao.getLatest().first()+ assertEquals(1, latest.size)+ assertEquals(LocationSource.SHUTDOWN, latest[0].source)+ assertEquals(20.0, latest[0].coord.lat)+ }++ @Test+ fun sameSecondTieIsBrokenPerUser() = runTest {+ val dao = db.locationValueDao()+ dao.upsert(fix(userid = 7L, sentAtMs = 2_000_400L, measuredAtMs = 2_000_400L, lat = 10.0))+ dao.upsert(fix(userid = 7L, sentAtMs = 2_000_600L, measuredAtMs = 2_000_600L, lat = 20.0))+ dao.upsert(fix(userid = 9L, sentAtMs = 1_500_000L, measuredAtMs = 1_500_000L, lat = 30.0))+ val latest = dao.getLatest().first().associateBy { it.userid }+ assertEquals(2, latest.size)+ assertEquals(20.0, latest[7L]?.coord?.lat)+ assertEquals(30.0, latest[9L]?.coord?.lat)+ }+}