taxi: a transient status failure cannot discard a pending booking
- Commit
9bcaa5ce830fbba6a62856d20282773bdb8bfb0e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 09:21 UTC (5 days ago)
- Parents
7059c40
The pending offer exists precisely because a transport failure may still have created the ride server-side (it is kept on BookingResult.Failed as the double-booking/ double-charge guard). The launch reconciliation collapsed activeRide() with 'as? Active': an HTTP 500 (Failed — unknown) became null, indistinguishable from None (known- absent), and an expired pending offer was Discard- ed/cleared. Next launch the user could re-book a second ride while the first was live and untracked. Same collapse class as the landed by-id fix, different site (untouched). pendingRideAction gains a result-aware overload: Active -> RestoreTracking, Failed -> KeepPending, None/Unsupported -> the existing expiry decision; RideScreen passes the raw result. Four new tests pin the matrix — Failed+expired is the regression. Fare math, parsing and tracking polls were audited and cleared; a display-only instance of the same collapse in CurrentRideScreen clears nothing and is noted as a follow-up, not folded in. Verified: :taxi: testDevUnitTest 268 tests pass; lint errors are pre-existing on untouched files.
Diff
taxi/src/main/java/com/vayunmathur/taxi/data/lyft/PendingRideOffer.kt
diff --git a/taxi/src/main/java/com/vayunmathur/taxi/data/lyft/PendingRideOffer.kt b/taxi/src/main/java/com/vayunmathur/taxi/data/lyft/PendingRideOffer.ktindex 7334b8581a..57d67a40b1 100644--- a/taxi/src/main/java/com/vayunmathur/taxi/data/lyft/PendingRideOffer.kt+++ b/taxi/src/main/java/com/vayunmathur/taxi/data/lyft/PendingRideOffer.kt@@ -4,6 +4,7 @@ import com.vayunmathur.taxi.data.LatLng import com.vayunmathur.taxi.data.Place import com.vayunmathur.taxi.data.Provider import com.vayunmathur.taxi.data.RideQuote+import com.vayunmathur.taxi.data.RideStatusResult import kotlinx.serialization.Serializable /**@@ -123,3 +124,22 @@ fun pendingRideAction(pending: PendingRideOffer, hasActiveRide: Boolean, nowMs: pending.isExpired(nowMs) -> PendingRideAction.Discard else -> PendingRideAction.KeepPending }++/**+ * Reconcile decision from the raw active-ride read. Only a known-absent result+ * ([RideStatusResult.None] / [RideStatusResult.Unsupported]) may discard an expired+ * offer: a [RideStatusResult.Failed] read is transient (5xx, transport blip) and says+ * nothing about whether the server created the ride, so the pending offer is kept —+ * discarding it would remove the guard against booking (and charging) a second time.+ */+fun pendingRideAction(+ result: RideStatusResult,+ pending: PendingRideOffer,+ nowMs: Long,+): PendingRideAction = when (result) {+ is RideStatusResult.Active -> PendingRideAction.RestoreTracking+ is RideStatusResult.Failed -> PendingRideAction.KeepPending+ RideStatusResult.None,+ RideStatusResult.Unsupported,+ -> pendingRideAction(pending, hasActiveRide = false, nowMs = nowMs)+}
taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt
diff --git a/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt b/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.ktindex 16c6ecad24..831000fdd7 100644--- a/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt+++ b/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt@@ -110,8 +110,11 @@ fun RideScreen(bookingTrip: MutableState<BookingTrip?>? = null) { val store = LyftTokenStore(context.applicationContext) val pending = store.pendingRide() ?: return@LaunchedEffect val provider = LyftProvider(context.applicationContext)- val active = provider.activeRide() as? RideStatusResult.Active- when (pendingRideAction(pending, active != null, System.currentTimeMillis())) {+ // Keep the raw result: collapsing Failed into "no ride" here would discard an+ // expired pending offer even when the server already created the ride.+ val activeResult = provider.activeRide()+ val active = activeResult as? RideStatusResult.Active+ when (pendingRideAction(activeResult, pending, System.currentTimeMillis())) { PendingRideAction.RestoreTracking -> { store.clearPendingRide() pickup = pending.pickup()
taxi/src/test/java/com/vayunmathur/taxi/data/lyft/PendingReconcileTransientTest.kt
diff --git a/taxi/src/test/java/com/vayunmathur/taxi/data/lyft/PendingReconcileTransientTest.kt b/taxi/src/test/java/com/vayunmathur/taxi/data/lyft/PendingReconcileTransientTest.ktnew file mode 100644index 0000000000..d77f6beb5e--- /dev/null+++ b/taxi/src/test/java/com/vayunmathur/taxi/data/lyft/PendingReconcileTransientTest.kt@@ -0,0 +1,98 @@+package com.vayunmathur.taxi.data.lyft++import com.vayunmathur.taxi.data.ActiveRide+import com.vayunmathur.taxi.data.LatLng+import com.vayunmathur.taxi.data.Place+import com.vayunmathur.taxi.data.Provider+import com.vayunmathur.taxi.data.RideQuote+import com.vayunmathur.taxi.data.RideStatus+import com.vayunmathur.taxi.data.RideStatusResult+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: launch reconciliation collapsed a transient [RideStatusResult.Failed]+ * active-ride read into "no ride" (`as? Active`), so an expired pending offer was+ * discarded even when the server had already created the ride — removing the guard+ * against booking (and charging) a second time. Only a known-absent read may discard.+ */+class PendingReconcileTransientTest {++ private fun pending(expiry: Long?) = PendingRideOffer.from(+ quote = RideQuote(+ provider = Provider.LYFT, productId = "lyft", displayName = "Lyft",+ fareLowMinor = 1200, fareHighMinor = 1500, currency = "USD",+ pickupEtaMinutes = 4, tripDurationMinutes = 18, surgeMultiplier = null, capacity = 4,+ offerId = "offer-1", offerToken = "otok", costToken = "ctok", rideType = "lyft",+ costTokenExpiryMs = expiry,+ ),+ pickup = Place("Home", "1 Main St", LatLng(37.1, -122.1)),+ dropoff = Place("Office", "2 Oak Ave", LatLng(37.2, -122.2)),+ chargeAccountId = "acct-1",+ purchaseSessionId = "sess-1",+ savedAtMs = 1000L,+ )++ private fun activeRide() = ActiveRide(+ rideId = "r1",+ status = RideStatus.APPROACHING,+ statusRaw = RideStatus.APPROACHING.wire,+ driver = null,+ vehicle = null,+ driverLocation = null,+ stops = emptyList(),+ raw = "",+ )++ @Test+ fun transientFailureKeepsExpiredPendingOffer() {+ // Exact failing state: offer cost token already expired at relaunch, and the+ // active-ride read fails transiently (HTTP 500). The ride may exist server-side,+ // so the pending offer must survive for the next launch to reconcile again.+ val action = pendingRideAction(+ RideStatusResult.Failed("Lyft returned HTTP 500: kaput"),+ pending(expiry = NOW_MS),+ NOW_MS,+ )+ assertEquals(PendingRideAction.KeepPending, action)+ }++ @Test+ fun transientFailureKeepsValidPendingOffer() {+ val action = pendingRideAction(+ RideStatusResult.Failed("timeout"),+ pending(expiry = FAR_FUTURE_MS),+ NOW_MS,+ )+ assertEquals(PendingRideAction.KeepPending, action)+ }++ @Test+ fun knownAbsentStillDiscardsExpiredOffer() {+ assertEquals(+ PendingRideAction.Discard,+ pendingRideAction(RideStatusResult.None, pending(expiry = NOW_MS), NOW_MS),+ )+ assertEquals(+ PendingRideAction.Discard,+ pendingRideAction(RideStatusResult.Unsupported, pending(expiry = NOW_MS), NOW_MS),+ )+ }++ @Test+ fun knownAbsentKeepsValidOfferAndActiveRestoresTracking() {+ assertEquals(+ PendingRideAction.KeepPending,+ pendingRideAction(RideStatusResult.None, pending(expiry = FAR_FUTURE_MS), NOW_MS),+ )+ assertEquals(+ PendingRideAction.RestoreTracking,+ pendingRideAction(RideStatusResult.Active(activeRide()), pending(expiry = NOW_MS), NOW_MS),+ )+ }++ private companion object {+ private const val NOW_MS = 5_000_000L+ private const val FAR_FUTURE_MS = 9_999_999_999_999L+ }+}