notes: a watch edit can no longer silently truncate a long note
- Commit
cf5b40685f03a66bb2f375143b830dbbb7aa2eb3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:53 UTC (5 days ago)
- Parents
58e61f1
buildWearSnapshot caps titles at 120 chars and bodies at 4000 (flagging bodyTruncated), but applyWearOp only refused UPSERTs for non-text notes: a text note over the cap passed last-writer-wins and was rebuilt as a single Text block from the incoming truncated body, permanently discarding the tail the watch never saw (and any title tail past 120). A note of 4000 chars plus 'TAIL' lost its TAIL to a snapshot-based edit. The existing ink/image refusal already guarded the same 'watch never saw the full body' hazard; text had no such guard. applyWearOp now refuses a UPSERT when the op is marked truncated or the existing note exceeds the snapshot caps, leaving the op unreflected/pending on the watch (honest state, no false 'synced'); DELETE is unchanged since a tombstone loses no text. Three regression tests failed pre-fix on exactly this loss. Search, tag rewriting, relay and backup mapping were audited and cleared. Verified: :notes:testDevUnitTest 251 tests pass; :notes:lint 0 errors with only pre-existing warnings.
Diff
notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt b/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.ktindex 5b3ea0e8c2..71cc3a3c8f 100644--- a/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt+++ b/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt@@ -53,8 +53,13 @@ data class WearNotesSyncState( * compare the same stamps. An UPSERT for a note whose phone body has * image/ink blocks is refused (the watch never offers it; this is the * belt-and-braces check) — a text replacement would destroy those blocks.- * DELETE tombstones any note, matching [NotesRepository.delete] semantics- * so the deletion also propagates over the relay sync.+ * An UPSERT is likewise refused when the watch could only have seen a+ * truncated copy of the note: the snapshot caps title/body (see+ * [com.vayunmathur.notes.domain.buildWearSnapshot]), so an edit built+ * from it carries none of the text past the cap, and applying it would+ * silently delete that unseen tail. DELETE tombstones any note, matching+ * [NotesRepository.delete] semantics so the deletion also propagates over+ * the relay sync. */ class WearNotesSync private constructor(context: Context) { @@ -191,6 +196,19 @@ class WearNotesSync private constructor(context: Context) { Log.status(TAG, "wear edit refused for non-text note ${incoming.syncId}") return false }+ // The watch edits only what the snapshot showed it. When+ // that snapshot truncated this note (or the op itself says+ // its body is truncated), the incoming title/body cannot+ // contain the full phone text: replacing the body would+ // delete everything past the cap that the watch never saw.+ if (incoming.bodyTruncated ||+ (existing != null &&+ (existing.title.length > WearNotesProtocol.MAX_TITLE_CHARS ||+ existing.content.length > WearNotesProtocol.MAX_BODY_CHARS))+ ) {+ Log.status(TAG, "wear edit refused for truncated note ${incoming.syncId}")+ return false+ } val base = existing ?: Note( title = incoming.title, content = incoming.body,
notes/src/test/java/com/vayunmathur/notes/sync/WearNotesApplyTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/sync/WearNotesApplyTest.kt b/notes/src/test/java/com/vayunmathur/notes/sync/WearNotesApplyTest.ktindex 569a380808..73611d435d 100644--- a/notes/src/test/java/com/vayunmathur/notes/sync/WearNotesApplyTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/sync/WearNotesApplyTest.kt@@ -9,6 +9,7 @@ import com.vayunmathur.notes.data.NotesRepository import com.vayunmathur.notes.data.withBody import com.vayunmathur.notes.domain.WearNote import com.vayunmathur.notes.domain.WearNoteOp+import com.vayunmathur.notes.domain.WearNotesProtocol import com.vayunmathur.notes.domain.WearOpKind import com.vayunmathur.notes.seedNotesDb import kotlinx.coroutines.test.runTest@@ -64,6 +65,58 @@ class WearNotesApplyTest { assertFalse(sync.applyWearOp(op(WearOpKind.DELETE, "missing", 3_000L))) } + @Test+ fun watchEditRefusedWhenSnapshotTruncatedTheBody() = runTest {+ // The phone publishes at most MAX_BODY_CHARS of this note, so a+ // watch edit is built from the truncated copy alone. Applying it+ // would replace the full body and silently lose the unseen tail.+ val fullBody = "a".repeat(WearNotesProtocol.MAX_BODY_CHARS) + "TAIL"+ repo.upsertRaw(Note(title = "Long", content = fullBody, syncId = "long1", updatedAt = 2_000L))+ val truncatedEdit = op(+ WearOpKind.UPSERT,+ "long1",+ 3_000L,+ title = "Long",+ body = "edited " + "a".repeat(100),+ )+ assertFalse(sync.applyWearOp(truncatedEdit))+ val note = repo.getBySyncId("long1")!!+ assertEquals(fullBody, note.content)+ assertEquals(2_000L, note.updatedAt)+ // Delete remains allowed: a tombstone loses no body text.+ assertTrue(sync.applyWearOp(op(WearOpKind.DELETE, "long1", 4_000L)))+ }++ @Test+ fun watchEditRefusedWhenSnapshotTruncatedTheTitle() = runTest {+ val fullTitle = "T".repeat(WearNotesProtocol.MAX_TITLE_CHARS) + "X"+ repo.upsertRaw(Note(title = fullTitle, content = "short", syncId = "longt", updatedAt = 2_000L))+ assertFalse(+ sync.applyWearOp(+ op(WearOpKind.UPSERT, "longt", 3_000L, title = fullTitle.take(WearNotesProtocol.MAX_TITLE_CHARS), body = "edited"),+ ),+ )+ assertEquals(fullTitle, repo.getBySyncId("longt")!!.title)+ assertEquals("short", repo.getBySyncId("longt")!!.content)+ }++ @Test+ fun watchEditRefusedForExplicitlyTruncatedIncomingBody() = runTest {+ repo.upsertRaw(Note(title = "Phone", content = "phone", syncId = "trunc1", updatedAt = 2_000L))+ val op = WearNoteOp(+ WearOpKind.UPSERT,+ WearNote(+ syncId = "trunc1",+ title = "Phone",+ body = "partial",+ updatedAt = 3_000L,+ bodyTruncated = true,+ ),+ )+ assertFalse(sync.applyWearOp(op))+ assertEquals("phone", repo.getBySyncId("trunc1")!!.content)+ }+ @Test fun watchEditRefusedForInkNoteButDeleteAllowed() = runTest { val ink = Note(title = "Sketch", content = "", syncId = "ink1", updatedAt = 2_000L)