Modern Apps git

← Modern-Apps.git

passwords: fix KDBX tombstone merge and read-error handling

Commit
787fdb6fb8ba52bb89d5b6dbe2b7d4d6af28d20f
Author
Vayun Mathur <[email protected]>
When
2026-11-07 02:52 UTC (29 days ago)
Parents
08f5789
The zero-data-loss hardening in 94e0783d4 swapped the two
single-sided merge cases. reconcileLocalOnly (local present,
remote live absent) treated a baselined row as a local delete
and counted deletedRemote, so a remote tombstone never
deleted locally; reconcileRemoteOnly (remote present, local
absent) quarantined, so a local delete was pulled back
instead of being replaced by a tombstone. Its tests could
not run at all because of the syntax error fixed in the
previous commit, so this went unnoticed.

Restore the intended semantics: a tombstoned id meeting a
local row deletes it locally (deletedLocal, tombstone
retained); a baselined live remote entry with no local row
is a local delete and is replaced by a tombstone
(deletedRemote); a baselined local row absent remotely with
no tombstone fails closed into quarantine. Also catch
IOException on document read as Error instead of throwing
(transportExceptionAbortsWithoutMutating), and strip a
foreign same-named _Tombstone key from live entries on pull
and on the content-equal path so it can never arm deletion
or survive a sync (foreignTombstoneValuedKeyDoesNotDelete).

Verified: gslot ./gradlew :passwords:testDevUnitTest passes
(109 tests, previously 7 failures in KdbxSyncEngineTest).

(cherry picked from commit 124dfa1e096c5440ff8f903cd4e201c22bbec180)

Diff

passwords/src/main/java/com/vayunmathur/passwords/sync/KdbxSyncEngine.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/sync/KdbxSyncEngine.kt b/passwords/src/main/java/com/vayunmathur/passwords/sync/KdbxSyncEngine.ktindex c3016db801..8c894ade89 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/sync/KdbxSyncEngine.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/sync/KdbxSyncEngine.kt@@ -174,6 +174,8 @@ class KdbxSyncEngine(             document.read()         } catch (_: FileNotFoundException) {             return KdbxSyncResult.FileMissing+        } catch (expected: IOException) {+            return KdbxSyncResult.Error(expected.message ?: "Failed to read KDBX file")         }         // A brand new SAF document is zero bytes; that is an empty vault, not a failure.         val remoteEntries = if (remoteBytes.isEmpty()) {@@ -453,7 +455,11 @@ class KdbxSyncEngine(         val remoteHash = EntryMapper.contentHash(remoteFields)          if (localHash == remoteHash) {-            accumulator.remoteOut += remoteEntry+            // Strip a foreign same-named tombstone key: only an exact+            // own-client tombstone entry (split out before merge) deletes.+            val cleaned = remoteEntry - EntryMapper.FIELD_TOMBSTONE+            if (cleaned.size != remoteEntry.size) accumulator.remoteChanged = true+            accumulator.remoteOut += cleaned             if (id in accumulator.freshlyIdentified) accumulator.localUpserts += localEntity             accumulator.snapshots += kind.snapshot(                 id,@@ -527,7 +533,7 @@ class KdbxSyncEngine(         // Pulled rows keep the remote timestamp, so the next cycle sees them         // as clean rather than bouncing the change back.         accumulator.localUpserts += kind.rebind(remoteEntity, kind.rowId(localEntity), id)-        accumulator.remoteOut += remoteEntry+        accumulator.remoteOut += remoteEntry - EntryMapper.FIELD_TOMBSTONE         accumulator.pulled++         val stamp = kind.updatedAt(remoteEntity)         accumulator.snapshots += kind.snapshot(id, remoteHash, stamp, stamp, now)@@ -542,14 +548,19 @@ class KdbxSyncEngine(         accumulator: MergeAccumulator<T>,         tombstonedIds: Set<String>,     ) {-        if (base != null) {-            // Own-client deleted locally: write a tombstone entry (retained-            // until acked) instead of dropping the row from the file.-            accumulator.tombstones += id-            accumulator.remoteChanged = true-            accumulator.deletedRemote+++        if (id in tombstonedIds) {+            // Remote tombstone met the local row: the delete wins.             accumulator.localDeletes += localEntity             accumulator.removed += id+            accumulator.retainedTombstones += id+            accumulator.remoteChanged = true+            accumulator.deletedLocal+++            return+        }+        if (base != null) {+            // Baselined id absent remotely with no tombstone is ambiguous;+            // sync() quarantines before merge, so fail closed here too.+            accumulator.quarantineIds += id             return         }         val localFields = kind.fields(localEntity)@@ -585,15 +596,18 @@ class KdbxSyncEngine(             return         }         if (base != null) {-            // Unreachable: ambiguous foreign absences quarantine in sync()-            // before merge() runs, so a baselined id always has a live or-            // tombstoned remote entry. Retained as a hard fail-closed guard.-            accumulator.quarantineIds += id+            // Baselined id is present remotely but gone locally: this+            // client deleted it, so replace the live entry with a+            // tombstone (retained until acked) instead of pulling it back.+            accumulator.tombstones += id+            accumulator.remoteChanged = true+            accumulator.deletedRemote+++            accumulator.removed += id             return         }         val remoteEntity = kind.entity(remoteEntry)         accumulator.localUpserts += kind.rebind(remoteEntity, 0, id)-        accumulator.remoteOut += remoteEntry+        accumulator.remoteOut += remoteEntry - EntryMapper.FIELD_TOMBSTONE         accumulator.pulled++         val stamp = kind.updatedAt(remoteEntity)         accumulator.snapshots += kind.snapshot(