Modern Apps git

← Modern-Apps.git

todo: the wear protocol's op-reflection rule is shared, and a truncated page can't prove a delete

Commit
6d1c80260a633cb327a09c1262b7818ac89d2f5e
Author
Vayun Mathur <[email protected]>
When
2026-12-01 16:50 UTC (4 days ago)
Parents
a10b333
The two WearTodoProtocol copies differed by exactly one
function: pendingOpsNotReflectedBy existed only on the
wear side. Call-site evidence says the semantics are
shared contract (the watch queues/resends ops and drops
them only via this predicate; the phone applies ops as
source of truth and republishes the pages that carry the
reflection proof), so the function is ported to the phone
copy rather than recorded as intentional asymmetry โ€” and
structure_check has no asymmetry mechanism anyway.

One hardening came with it, in both copies: DELETE was
deemed reflected whenever its task was absent from the
page, including truncated pages, where absence may mean
the byte cap dropped a still-live task (notes' protocol
already guards exactly this). Presence-based reflection
(SET_DONE/EDIT/QUICK_ADD) still drops on truncated pages โ€”
carrying the task is positive proof โ€” and SNOOZE never
drops. Both golden test suites now pin identical JSON
including the truncated page and reflection cases.
Verified: both full suites pass, structure_check wear is
clean (12 pairs), todo-wear lint passes; todo lint errors
are pre-existing on untouched UI files. This unblocks the
todo pair's wearlink migration.

Diff

todo-wear/src/main/java/com/vayunmathur/todo/wear/domain/WearTodoProtocol.kt
diff --git a/todo-wear/src/main/java/com/vayunmathur/todo/wear/domain/WearTodoProtocol.kt b/todo-wear/src/main/java/com/vayunmathur/todo/wear/domain/WearTodoProtocol.ktindex 06ee039724..a739a9341c 100644--- a/todo-wear/src/main/java/com/vayunmathur/todo/wear/domain/WearTodoProtocol.kt+++ b/todo-wear/src/main/java/com/vayunmathur/todo/wear/domain/WearTodoProtocol.kt@@ -202,9 +202,12 @@ data class WearTodoOp(  *  * A page reflects SET_DONE/EDIT/QUICK_ADD by carrying the target task (for  * QUICK_ADD, the task the phone created carries the op's [WearTodoOp.syncId]);- * it reflects DELETE by no longer carrying the task. SNOOZE ops carry no- * page-visible target value, so a page can never prove them reflected and- * they stay queued.+ * carrying the task is positive proof, so it holds even on a truncated page.+ * It reflects DELETE only by no longer carrying the task on a non-truncated+ * page: a truncated page may have byte-cap-dropped the still-live task, so+ * its absence proves nothing and the delete stays queued. SNOOZE ops carry+ * no page-visible target value, so a page can never prove them reflected+ * and they stay queued.  */ fun pendingOpsNotReflectedBy(     pendingOps: List<WearTodoOp>,@@ -216,7 +219,7 @@ fun pendingOpsNotReflectedBy(         when (op.kind) {             WearTodoOpKind.SET_DONE, WearTodoOpKind.EDIT,             WearTodoOpKind.QUICK_ADD -> op.syncId !in ids-            WearTodoOpKind.DELETE -> op.syncId in ids+            WearTodoOpKind.DELETE -> op.syncId in ids || page.truncated             WearTodoOpKind.SNOOZE_HOUR, WearTodoOpKind.SNOOZE_TOMORROW -> true         }     }
todo-wear/src/test/java/com/vayunmathur/todo/wear/domain/WearTodoProtocolTest.kt
diff --git a/todo-wear/src/test/java/com/vayunmathur/todo/wear/domain/WearTodoProtocolTest.kt b/todo-wear/src/test/java/com/vayunmathur/todo/wear/domain/WearTodoProtocolTest.ktindex 9ef92d3c2a..04ff706fc7 100644--- a/todo-wear/src/test/java/com/vayunmathur/todo/wear/domain/WearTodoProtocolTest.kt+++ b/todo-wear/src/test/java/com/vayunmathur/todo/wear/domain/WearTodoProtocolTest.kt@@ -151,6 +151,57 @@ class WearTodoProtocolTest {         )     } +    @Test+    fun goldenTruncatedPageJson() {+        val page = WearTodoPage(+            pageKey = "today",+            title = "Today",+            generatedAtMillis = 1_000L,+            totalCount = 1,+            tasks = listOf(task("a", "Buy milk")),+            truncated = true,+        )+        assertEquals(+            """{"pageKey":"today","title":"Today","generatedAtMillis":1000,""" ++                """"totalCount":1,"tasks":[{"syncId":"a","title":"Buy milk"}],"truncated":true}""",+            WearTodoProtocol.json.encodeToString(page),+        )+    }++    @Test+    fun goldenWatchStateJson() {+        val state = com.vayunmathur.todo.wear.domain.WearTodoWatchState(+            appliedCatalogAtMillis = 1_000L,+            cachedPages = 2,+            pendingOps = 3,+        )+        assertEquals(+            """{"appliedCatalogAtMillis":1000,"cachedPages":2,"pendingOps":3}""",+            WearTodoProtocol.json.encodeToString(state),+        )+    }++    @Test+    fun truncatedPageDoesNotProveDeleteReflected() {+        val setDone = WearTodoOp(WearTodoOpKind.SET_DONE, syncId = "a", done = true)+        val delete = WearTodoOp(WearTodoOpKind.DELETE, syncId = "c")+        val quickAdd = WearTodoOp(+            WearTodoOpKind.QUICK_ADD, opId = "o2", syncId = "n1",+            text = "Buy milk tomorrow", pageKey = "today",+        )+        val page = WearTodoPage(+            pageKey = "today", generatedAtMillis = 2_000L,+            tasks = listOf(task("a")),+            truncated = true,+        )+        // Presence is positive proof (setDone drops), but absence on a+        // truncated page proves nothing: delete and quick-add stay queued.+        assertEquals(+            listOf(delete, quickAdd),+            pendingOpsNotReflectedBy(listOf(setDone, delete, quickAdd), page),+        )+    }+     @Test     fun overlayAppliesDoneEditAndDeleteButNotSnoozeOrQuickAdd() {         val tasks = listOf(task("a"), task("b"), task("c"))
todo/src/main/java/com/vayunmathur/todo/domain/WearTodoProtocol.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/WearTodoProtocol.kt b/todo/src/main/java/com/vayunmathur/todo/domain/WearTodoProtocol.ktindex de8d5fd9a6..1df736226f 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/WearTodoProtocol.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/WearTodoProtocol.kt@@ -197,6 +197,34 @@ data class WearTodoOp(     val priority: Int? = null, ) +/**+ * Pending ops that a republished [page] does NOT yet reflect, in queue order.+ *+ * A page reflects SET_DONE/EDIT/QUICK_ADD by carrying the target task (for+ * QUICK_ADD, the task the phone created carries the op's [WearTodoOp.syncId]);+ * carrying the task is positive proof, so it holds even on a truncated page.+ * It reflects DELETE only by no longer carrying the task on a non-truncated+ * page: a truncated page may have byte-cap-dropped the still-live task, so+ * its absence proves nothing and the delete stays queued. SNOOZE ops carry+ * no page-visible target value, so a page can never prove them reflected+ * and they stay queued.+ */+fun pendingOpsNotReflectedBy(+    pendingOps: List<WearTodoOp>,+    page: WearTodoPage,+): List<WearTodoOp> {+    if (pendingOps.isEmpty()) return pendingOps+    val ids = page.tasks.flatMap { listOf(it.syncId) + it.subtasks.map { s -> s.syncId } }.toSet()+    return pendingOps.filter { op ->+        when (op.kind) {+            WearTodoOpKind.SET_DONE, WearTodoOpKind.EDIT,+            WearTodoOpKind.QUICK_ADD -> op.syncId !in ids+            WearTodoOpKind.DELETE -> op.syncId in ids || page.truncated+            WearTodoOpKind.SNOOZE_HOUR, WearTodoOpKind.SNOOZE_TOMORROW -> true+        }+    }+}+ /** Builds a bounded catalog: view caps then the byte cap (last entries drop first). */ fun buildWearCatalog(     views: List<WearTodoViewEntry>,
todo/src/test/java/com/vayunmathur/todo/sync/WearTodoProtocolTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/sync/WearTodoProtocolTest.kt b/todo/src/test/java/com/vayunmathur/todo/sync/WearTodoProtocolTest.ktindex b1ff552fd8..5b8ba90862 100644--- a/todo/src/test/java/com/vayunmathur/todo/sync/WearTodoProtocolTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/sync/WearTodoProtocolTest.kt@@ -7,8 +7,10 @@ import com.vayunmathur.todo.domain.WearTodoPage import com.vayunmathur.todo.domain.WearTodoProtocol import com.vayunmathur.todo.domain.WearTodoTask import com.vayunmathur.todo.domain.WearTodoViewEntry+import com.vayunmathur.todo.domain.WearTodoWatchState import com.vayunmathur.todo.domain.buildWearPage import com.vayunmathur.todo.domain.overlayPendingTasks+import com.vayunmathur.todo.domain.pendingOpsNotReflectedBy import kotlinx.serialization.encodeToString import kotlin.test.Test import kotlin.test.assertEquals@@ -103,6 +105,99 @@ class WearTodoProtocolTest {         assertTrue(bytes <= WearTodoProtocol.MAX_JSON_BYTES, "page $bytes bytes over cap")     } +    @Test+    fun goldenTruncatedPageJson() {+        val page = WearTodoPage(+            pageKey = "today",+            title = "Today",+            generatedAtMillis = 1_000L,+            totalCount = 1,+            tasks = listOf(task("a", "Buy milk")),+            truncated = true,+        )+        assertEquals(+            """{"pageKey":"today","title":"Today","generatedAtMillis":1000,""" ++                """"totalCount":1,"tasks":[{"syncId":"a","title":"Buy milk"}],"truncated":true}""",+            WearTodoProtocol.json.encodeToString(page),+        )+    }++    @Test+    fun goldenWatchStateJson() {+        val state = WearTodoWatchState(+            appliedCatalogAtMillis = 1_000L,+            cachedPages = 2,+            pendingOps = 3,+        )+        assertEquals(+            """{"appliedCatalogAtMillis":1000,"cachedPages":2,"pendingOps":3}""",+            WearTodoProtocol.json.encodeToString(state),+        )+    }++    @Test+    fun quickAddDroppedOncePageCarriesTheCreatedTask() {+        val quickAdd = WearTodoOp(+            WearTodoOpKind.QUICK_ADD, opId = "o2", syncId = "n1",+            text = "Buy milk tomorrow", pageKey = "today",+        )+        val reflectedPage = WearTodoPage(+            pageKey = "today", generatedAtMillis = 2_000L,+            tasks = listOf(task("n1", "Buy milk tomorrow")),+        )+        assertEquals(emptyList(), pendingOpsNotReflectedBy(listOf(quickAdd), reflectedPage))+    }++    @Test+    fun quickAddKeptWhilePageDoesNotCarryTheCreatedTask() {+        val quickAdd = WearTodoOp(+            WearTodoOpKind.QUICK_ADD, opId = "o2", syncId = "n1",+            text = "Buy milk tomorrow", pageKey = "today",+        )+        val otherPage = WearTodoPage(+            pageKey = "today", generatedAtMillis = 2_000L,+            tasks = listOf(task("a")),+        )+        assertEquals(listOf(quickAdd), pendingOpsNotReflectedBy(listOf(quickAdd), otherPage))+    }++    @Test+    fun reflectedPageKeepsExistingDoneEditDeleteAndSnoozeSemantics() {+        val setDone = WearTodoOp(WearTodoOpKind.SET_DONE, syncId = "a", done = true)+        val edit = WearTodoOp(WearTodoOpKind.EDIT, syncId = "b", title = "Edited")+        val delete = WearTodoOp(WearTodoOpKind.DELETE, syncId = "c")+        val snooze = WearTodoOp(WearTodoOpKind.SNOOZE_HOUR, syncId = "a")+        val page = WearTodoPage(+            pageKey = "today", generatedAtMillis = 2_000L,+            tasks = listOf(task("a"), task("b")),+        )+        assertEquals(+            listOf(snooze),+            pendingOpsNotReflectedBy(listOf(setDone, edit, delete, snooze), page),+        )+    }++    @Test+    fun truncatedPageDoesNotProveDeleteReflected() {+        val setDone = WearTodoOp(WearTodoOpKind.SET_DONE, syncId = "a", done = true)+        val delete = WearTodoOp(WearTodoOpKind.DELETE, syncId = "c")+        val quickAdd = WearTodoOp(+            WearTodoOpKind.QUICK_ADD, opId = "o2", syncId = "n1",+            text = "Buy milk tomorrow", pageKey = "today",+        )+        val page = WearTodoPage(+            pageKey = "today", generatedAtMillis = 2_000L,+            tasks = listOf(task("a")),+            truncated = true,+        )+        // Presence is positive proof (setDone drops), but absence on a+        // truncated page proves nothing: delete and quick-add stay queued.+        assertEquals(+            listOf(delete, quickAdd),+            pendingOpsNotReflectedBy(listOf(setDone, delete, quickAdd), page),+        )+    }+     @Test     fun overlayAppliesDoneEditAndDeleteButNotSnoozeOrQuickAdd() {         val tasks = listOf(task("a"), task("b"), task("c"))