findfamily: heartbeats no longer re-stamp stale fixes as fresh
- Commit
b883c9691677ddfcf3e5ba010440f9d148f69dad- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:30 UTC (5 days ago)
- Parents
dfcf9e8
Every heartbeat (and every pre-unlock direct-boot publish) re-stamped the held fix's measured time with the publish wall-clock time. Indoors, where all new fixes fail the 100 m intake gate, the held fix can be hours old โ yet every peer stored it with timestamp = now, so receivers showed it as 'just updated', and NoShowPolicy trusted the fake freshness (a stale fix at a waypoint suppressed a real no-show alert). The heartbeat conflated send time with measurement time; freshness already has a correct channel (reportedAt). Both paths now stamp the fix's real measured time via fixMeasuredAt (the same elapsed-realtime derivation FinalLocationReporter uses, immune to wall-clock steps), matching OwnFixStamps' documented contract. New regression test: a 2-hour-old held fix heartbeats with a ~2-hour-old timestamp and a fresh reportedAt. Location intake, geofences, movement detection and ingest sequencing were audited in the same pass. Verified: :findfamily:testDevUnitTest 585 tests, 0 failures; lint errors are pre-existing on untouched files.
Diff
findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingDirectBoot.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingDirectBoot.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingDirectBoot.ktindex fd46da878e..6d0de53e64 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingDirectBoot.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingDirectBoot.kt@@ -162,7 +162,8 @@ internal suspend fun LocationTrackingService.publishDirectBoot(targets: List<Dir Coord(location.latitude, location.longitude), if (location.hasSpeed()) location.speed else 0f, location.accuracy,- Clock.System.now(),+ // Measured time of the held fix, not publish time โ see fixMeasuredAt.+ fixMeasuredAt(location, Clock.System.now()), battery, // Pre-unlock fixes are own fixes too: hybrid reportedAt from the same // device-protected high-water the unlocked path uses (OwnFixStamps).
findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingHeartbeat.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingHeartbeat.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingHeartbeat.ktindex 64918561dd..dc0764f245 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingHeartbeat.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/LocationTrackingHeartbeat.kt@@ -3,6 +3,7 @@ import com.vayunmathur.findfamily.network.Networking import android.location.Location import android.os.BatteryManager+import android.os.SystemClock import com.vayunmathur.library.log.Log import com.vayunmathur.findfamily.data.Coord import com.vayunmathur.findfamily.data.DirectBootStore@@ -15,6 +16,7 @@ import com.vayunmathur.findfamily.data.TemporaryLink import com.vayunmathur.findfamily.domain.NoShowPolicy import com.vayunmathur.findfamily.R import kotlin.time.Clock+import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.minutes import kotlin.time.Instant @@ -57,6 +59,21 @@ private fun Location.hasUsableAccuracy(): Boolean { return accuracy <= MAX_FIX_ACCURACY_METERS } +/**+ * When [location] was actually measured, derived from the monotonic clock rather+ * than the wall clock alone. Same derivation as the parting-report path+ * (`FinalLocationReporter`): a held fix is re-published on every heartbeat, so+ * stamping it with the heartbeat's wall time would claim the phone is at that+ * position *now* however old the fix is โ receivers render `timestamp` as the+ * "last updated" time and the no-show freshness gate trusts it, so the lie+ * propagates. Wall time can also be stepped between the fix and now; the+ * elapsed-realtime age cannot.+ */+internal fun fixMeasuredAt(location: Location, now: Instant): Instant {+ val ageMs = (SystemClock.elapsedRealtimeNanos() - location.elapsedRealtimeNanos) / 1_000_000+ return now - ageMs.coerceAtLeast(0).milliseconds+}+ internal fun LocationTrackingService.recordFix(location: Location) { if (!location.hasUsableAccuracy()) { return@@ -95,10 +112,13 @@ private suspend fun LocationTrackingService.upsertSelfLocation( // card read 0 forever, however fast they were actually moving. if (location.hasSpeed()) location.speed else 0f, location.accuracy,- // timestamp (measuredAt) unchanged; only reportedAt is hybrid-stamped- // (see OwnFixStamps) so a backwards clock correction cannot make this+ // timestamp is the fix's own measured time (see fixMeasuredAt): the+ // held fix may be minutes or hours old when the heartbeat re-publishes+ // it, and stamping it "now" would show every receiver a stale position+ // as if it were current. Only reportedAt is hybrid-stamped (see+ // OwnFixStamps) so a backwards clock correction cannot make this // heartbeat arrive stale against our own high-water at receivers.- now,+ fixMeasuredAt(location, now), bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY).toFloat(), reportedAt = OwnFixStamps.next(this@upsertSelfLocation), )
findfamily/src/test/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeatTest.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeatTest.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeatTest.ktindex 0bf8762cf3..610982b2a4 100644--- a/findfamily/src/test/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeatTest.kt+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeatTest.kt@@ -1,4 +1,6 @@ package com.vayunmathur.findfamily.util++// FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch. import com.vayunmathur.findfamily.platform.LocationTrackingService import com.vayunmathur.findfamily.network.Networking import com.vayunmathur.findfamily.platform.recordFix@@ -115,6 +117,32 @@ class LocationTrackingHeartbeatTest { assertEquals(13.5f, stored[0].speed) } + @Test+ fun syncHeartbeatKeepsFixMeasuredTime() = runBlocking {+ // Hold a fix measured two hours ago (elapsed-realtime age, not wall time):+ // the heartbeat re-publishes the held fix, and it must keep its real+ // measured time. Stamping it "now" made every receiver render the stale+ // position as current and let it satisfy the no-show freshness gate.+ val twoHoursNanos = 2L * 60 * 60 * 1_000_000_000L+ val held = location().apply {+ elapsedRealtimeNanos =+ android.os.SystemClock.elapsedRealtimeNanos() - twoHoursNanos+ }+ service.lastKnownLocation = held+ service.syncHeartbeat()+ val stored = repo.locationHistory(Networking.userid).first()+ assertEquals(1, stored.size)+ val ageMs = Clock.System.now().toEpochMilliseconds() -+ stored[0].timestamp.toEpochMilliseconds()+ assertTrue(+ ageMs > 90 * 60_000L,+ "stored fix must be ~2h old, was ${ageMs}ms"+ )+ // The send stamp stays fresh: freshness lives in reportedAt, not in+ // rewriting when the position was measured.+ assertTrue(stored[0].reportedAt > stored[0].timestamp)+ }+ @Test fun syncHeartbeatWithoutLocationIsNoOp() = runBlocking { val before = repo.getAllUsers().size