Modern Apps git

← Modern-Apps.git

todo: monthly tasks keep their anchor day after a short month

Commit
eb708ae72e88f40a0fee8cbd0056a9aa31fad44f
Author
Vayun Mathur <[email protected]>
When
2026-11-30 19:08 UTC (5 days ago)
Parents
38fb82f
Completing a monthly task due on the 31st rolled it to Feb 28
(correct clamp), but the rolled row kept an implicit Monthly(null)
rule, so the next completion anchored on the 28th — Mar 28, Apr 28,
forever. The original anchor day was discarded at the first roll and
unrecoverable from the stored rule, contradicting the documented
clamp semantics (Jan 31 -> Feb 28 -> Mar 31) that the existing test
only checked from the original date, never sequentially.

Completion now pins the anchor into the stored rule on the first
roll when the implicit day can clamp (29-31): FREQ=MONTHLY becomes
FREQ=MONTHLY;BYMONTHDAY=31, using the existing explicit-day
representation so parsing, sync and labels already handle it. Days
<= 28 keep the implicit form. The custom recurrence sheet, which
re-saved Monthly(n, null) whenever 'last day' was off, similarly
preserves an explicit anchor day it cannot edit. New regression
tests walk Jan 31 -> Feb 28 -> Mar 31 -> Apr 30 -> May 31
sequentially and assert the pinned rule; a day-15 control stays
implicit. Verified: :todo:testDevUnitTest 405 tests pass; lint
errors are pre-existing on untouched files.

Diff

todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt b/todo/src/main/java/com/vayunmathur/todo/domain/Completion.ktindex b6befd4308..037174d868 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt@@ -5,6 +5,8 @@ import kotlinx.datetime.TimeZone  /** Checking a task off. A repeating task rolls forward to its next occurrence instead. */ object Completion {+    /** Last day of February in a non-leap year: month days up to this never clamp. */+    private const val LAST_UNCLAMPED_DAY = 28      fun complete(todo: Todo, now: Long, zone: TimeZone): Todo {         val recurrence = Recurrence.parse(todo.recurrence)@@ -21,10 +23,24 @@ object Completion {             DueDates.at(nextDate, DueDates.localTime(due, zone), zone)         }         val shift = nextDue - (due ?: DueDates.startOfDay(today, zone))+        // A monthly schedule with no explicit day anchors to the due date's+        // day. Rolling a clamped occurrence forward (Jan 31 -> Feb 28) would+        // otherwise re-anchor it to the clamped day and drift every later+        // occurrence (Feb 28 -> Mar 28, not Mar 31), so pin the original+        // anchor day into the stored rule on the first such roll. Days that+        // exist in every month never clamp, so they keep the implicit form.+        val rolledRecurrence = if (+            recurrence is Recurrence.Monthly && recurrence.dayOfMonth == null && dueDate.day > LAST_UNCLAMPED_DAY+        ) {+            recurrence.copy(dayOfMonth = dueDate.day).serialize()+        } else {+            todo.recurrence+        }         return todo.copy(             done = false,             completedAt = now,             dueAt = nextDue,+            recurrence = rolledRecurrence,             startAt = todo.startAt?.plus(shift),             reminderAt = todo.reminderAt?.plus(shift),         )
todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt b/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.ktindex e1e0afd5fa..852838466a 100644--- a/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt+++ b/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt@@ -136,7 +136,13 @@ private fun CustomRecurrence(current: Recurrence?, onSet: (Recurrence) -> Unit)                     fromCompletion && unit == Unit4.WEEKS -> Recurrence.AfterCompletion(n * DAYS_PER_WEEK)                     unit == Unit4.DAYS -> Recurrence.Daily(n)                     unit == Unit4.WEEKS -> Recurrence.Weekly(n, days)-                    unit == Unit4.MONTHS -> Recurrence.Monthly(n, if (lastDay) Recurrence.LAST_DAY else null)+                    // Preserve an explicit anchor day (e.g. 31, pinned when a+                    // clamped monthly task rolled forward): this form has no+                    // day picker, so re-saving must not drop it back to null.+                    unit == Unit4.MONTHS -> Recurrence.Monthly(+                        n,+                        if (lastDay) Recurrence.LAST_DAY else (current as? Recurrence.Monthly)?.dayOfMonth,+                    )                     else -> Recurrence.Yearly(n)                 },             )
todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt b/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.ktindex 865d478829..fe5b7d7b9c 100644--- a/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt@@ -54,6 +54,31 @@ class CompletionTest {         assertEquals(next.dueAt!! - 86_400_000L, next.startAt)     } +    @Test+    fun `monthly task on the 31st keeps its anchor day across sequential completions`() {+        // Regression: rolling Jan 31 forward clamps to Feb 28; completing that+        // occurrence must return to Mar 31, not drift to Mar 28 (and Apr 30+        // must be followed by May 31, not May 30).+        var todo = Todo(title = "x", dueAt = day(2026, 1, 31), recurrence = "FREQ=MONTHLY")+        todo = Completion.complete(todo, now = day(2026, 1, 31), zone = zone)+        assertEquals(day(2026, 2, 28), todo.dueAt)+        assertEquals("FREQ=MONTHLY;BYMONTHDAY=31", todo.recurrence)+        todo = Completion.complete(todo, now = day(2026, 2, 28), zone = zone)+        assertEquals(day(2026, 3, 31), todo.dueAt)+        todo = Completion.complete(todo, now = day(2026, 3, 31), zone = zone)+        assertEquals(day(2026, 4, 30), todo.dueAt)+        todo = Completion.complete(todo, now = day(2026, 4, 30), zone = zone)+        assertEquals(day(2026, 5, 31), todo.dueAt)+    }++    @Test+    fun `monthly task on a day present in every month keeps its implicit recurrence`() {+        val todo = Todo(title = "x", dueAt = day(2026, 1, 15), recurrence = "FREQ=MONTHLY")+        val next = Completion.complete(todo, now = day(2026, 1, 15), zone = zone)+        assertEquals(day(2026, 2, 15), next.dueAt)+        assertEquals("FREQ=MONTHLY", next.recurrence)+    }+     @Test     fun `after-completion repeats work without a due date`() {         val todo = Todo(recurrence = "FREQ=DAILY;INTERVAL=3;X-FROM=COMPLETION")