flashcards: future last-review timestamps no longer crash or poison FSRS cards
- Commit
1f271e4072e9010343ca0d6682560cd79c1aa249- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 23:05 UTC (5 days ago)
- Parents
d7f1fce
When a card's lastReview is in the future (clock moved backwards, or restored state with a future timestamp), elapsed days went negative: retrievability computed (negative)^-0.5 = NaN, which either threw IllegalArgumentException in nextIntervalDays (crashing the review) or, on AGAIN, persisted a NaN stability — coerceIn does not remove NaN — poisoning the card for every later review and preview. Smaller skews returned a retrievability above 1.0, an impossible probability. The scheduler now guards at its own layer: non-positive elapsed reads as 1.0 (nothing decayed yet), non-finite or non-positive stability reads as 0, and clampStability/ clampDifficulty fall back to their floors for non-finite values so already-poisoned persisted cards recover on the next grade instead of propagating NaN forever. Normal-path behaviour is unchanged; the new regression tests failed pre-fix on exactly the two failure modes. Review sessions, stats, deck I/O, the optimizer and APKG import were audited and cleared. Verified: :flashcards:testDevUnitTest 173 tests pass; lint errors are pre-existing on untouched files.
Diff
flashcards/src/main/java/com/vayunmathur/flashcards/domain/Scheduler.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/domain/Scheduler.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/domain/Scheduler.ktindex 2995b9a845..153aabec33 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/domain/Scheduler.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/domain/Scheduler.kt@@ -77,7 +77,12 @@ object Scheduler { /** Probability of recall after [elapsedDays] given [stability] (in days). */ fun retrievability(elapsedDays: Double, stability: Double): Double {- if (stability <= 0.0) return 0.0+ if (!stability.isFinite() || stability <= 0.0) return 0.0+ // A future lastReview (clock moved backwards, restored state) gives a+ // negative elapsed time: nothing has decayed yet, so recall is 1.+ // Without this the base of the power goes negative for large skews and+ // the result is NaN, which poisons stability and crashes interval math.+ if (elapsedDays <= 0.0) return 1.0 return (1.0 + FACTOR * elapsedDays / stability).pow(DECAY) } @@ -87,8 +92,14 @@ object Scheduler { return (stability / FACTOR) * (r.pow(1.0 / DECAY) - 1.0) } - private fun clampStability(s: Double) = s.coerceIn(MIN_STABILITY, MAX_STABILITY)- private fun clampDifficulty(d: Double) = d.coerceIn(MIN_DIFFICULTY, MAX_DIFFICULTY)+ // NaN fails every coerceIn comparison and would survive as NaN, so+ // non-finite memory state (e.g. persisted by the negative-elapsed bug)+ // falls back to the clamp floor instead of propagating forever.+ private fun clampStability(s: Double) =+ (if (s.isFinite()) s else MIN_STABILITY).coerceIn(MIN_STABILITY, MAX_STABILITY)++ private fun clampDifficulty(d: Double) =+ (if (d.isFinite()) d else MIN_DIFFICULTY).coerceIn(MIN_DIFFICULTY, MAX_DIFFICULTY) private fun initialStability(w: DoubleArray, grade: Grade) = clampStability(w[grade.value - 1])@@ -149,7 +160,11 @@ object Scheduler { val w = if (weights.size == DEFAULT_W.size) weights else DEFAULT_W val firstReview = card.reps == 0 || card.state == CardState.NEW val elapsedDays =- if (card.lastReview > 0) (now - card.lastReview).toDouble() / DAY_MS else 0.0+ if (card.lastReview > 0) {+ ((now - card.lastReview).toDouble() / DAY_MS).coerceAtLeast(0.0)+ } else {+ 0.0+ } val (newDifficulty, newStability) = updateMemory(w, card, grade, firstReview, elapsedDays) val (newState, due) = resolveDue(card, grade, now, desiredRetention, newStability, firstReview)
flashcards/src/test/java/com/vayunmathur/flashcards/util/SchedulerTest.kt
diff --git a/flashcards/src/test/java/com/vayunmathur/flashcards/util/SchedulerTest.kt b/flashcards/src/test/java/com/vayunmathur/flashcards/util/SchedulerTest.ktindex 567d62ac35..2e53ba304b 100644--- a/flashcards/src/test/java/com/vayunmathur/flashcards/util/SchedulerTest.kt+++ b/flashcards/src/test/java/com/vayunmathur/flashcards/util/SchedulerTest.kt@@ -124,6 +124,31 @@ class SchedulerTest { assertEquals(CardState.LEARNING, graded.state) } + @Test+ fun retrievabilityNeverExceedsOneOrNaNForFutureLastReview() {+ // A lastReview in the future (device clock moved backwards, restored+ // state) yields a negative elapsed time: recall cannot exceed 100%+ // and must never be NaN, or it poisons the stability update below.+ val r = Scheduler.retrievability(-50.0, 10.0)+ assertTrue(r.isFinite(), "retrievability must be finite for negative elapsed, was $r")+ assertTrue(r in 0.0..1.0, "retrievability must be a probability, was $r")+ assertEquals(1.0, r, 1e-9)+ }++ @Test+ fun schedulingCardWithFutureLastReviewDoesNotCrashOrPoisonState() {+ val card = reviewCard(stability = 10.0, difficulty = 5.0, daysAgo = 10)+ .copy(lastReview = now + 50 * Scheduler.DAY_MS)+ val graded = Scheduler.schedule(card, Grade.GOOD, now)+ assertTrue(graded.stability.isFinite() && graded.stability > 0.0, "stability was ${graded.stability}")+ assertTrue(graded.difficulty in 1.0..10.0, "difficulty was ${graded.difficulty}")+ assertEquals(CardState.REVIEW, graded.state)+ assertTrue(graded.dueDate >= now + Scheduler.DAY_MS, "dueDate was ${graded.dueDate}")+ val lapsed = Scheduler.schedule(card, Grade.AGAIN, now)+ assertTrue(lapsed.stability.isFinite() && lapsed.stability > 0.0, "lapse stability was ${lapsed.stability}")+ assertEquals(CardState.RELEARNING, lapsed.state)+ }+ @Test fun goodGraduatesToReviewWithDayScaleInterval() { val graded = Scheduler.schedule(newCard(), Grade.EASY, now)