fix(android): bind duplicate-content rows to one server identity (sync shadow) #1359
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/sync-binder-duplicates"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Root cause
A device can hold several MediaStore rows with identical bytes (one image in multiple albums/folders).
ChangeBinderrule 2 (hash binding, spec 065 PR-B) required a unique unbound row per content hash:With N copies of one hash,
candidates.sizewas never exactly 1, so the change bound nothing — for every copy. Server-side trash/purge verdicts therefore never reached any copy: rows stayed plainUPLOADED, theReconcilerclassified themskipped(user saw "18 übersprungen/skipped"), and the spec 065 S8 library-removed card ("Bewusst nicht in Memories") never appeared. The server side was already correct:SyncChangeQueryService.PAGE_SQLcarriescontentHash+byteSizeeven forpermanently_deletedtombstones, and 0.10.0 devices already cachecontentHashper row.Behavior changes (client-only)
UPLOADEDrows whosecontentHashmatches (byteSize corroborating on both sides when both known — existing degrade-to-hash-only tolerance kept). Rationale: content identity is the server identity rule (spec 001 FR-068e/FR-068b1: identical bytes never create a second active server item), so same-hash device copies are copies of one server identity — binding them all is exact, not ambiguous.claimedHashesone-claim guard kept; rule 1 (directserverMediaItemIdrefresh) untouched and final (an already-bound row is never rebound to another item).media_itemsrows with one hash (the upload dedupe excludespermanently_deletedtwins), so one page can carry several changes per hash. The binder elects per hash: lifecycleactive>trashed>permanently_deleted, tiebreak higherchangeSeq; only the winner binds that hash's rows, losers bind nothing for the hash. Unknown lifecycle values rank below known ones (consistent withServerLifecycle.SHADOWINGdegrading unknowns). The purge+reupload shape now correctly binds the rows to the active twin — they do not shadow, and the purged twin cannot steal the binding.RoomSyncPullStateRepository.applyPagenow reportsappliedChanges = changes.sizeinstead ofshadowUpdates.size, matching the interface's documented contract ("consumed change entries — the honest pull-progress metric, independent of how many rows happened to bind", already what the in-memory test double does). With fan-out, shadow writes can exceed the change count (6 changes → 18 writes), which would have overstated the pull metric.ISSUE-192(high/resolved/area:android) documenting the bug and fix.Propagation
No engine changes needed — once the binder shadows rows, the existing chain surfaces them:
Reconciler.shadowed→BackupRunResult.shadowedCount→BackupWorker.KEY_SHADOWED→BackupStatusViewModel.shadowedCount→BackupScreencard (shadowedCount > 0⇒ "Bewusst nicht in Memories"). The user's exact shape (18 rows, 6 hashes, all purged) now yieldsshadowedCount = 18,skippedCount = 0; no existing engine test pinnedskippedfor a duplicate-row shape, so none had to change.Tests
ChangeBinderTestreworked to 19 cases: the bug scenario (1 purged change + 3 copies ⇒ all 3 shadowed), the exact AVD shape (6 hashes × 3 rows ⇒ 18 updates over 6 server ids), size-mismatch excludes only that copy, different hashes no cross-talk, purge+active twin (active wins, rows not shadowed), trashed-vs-purged (trashed wins), changeSeq tiebreak, unknown-lifecycle ranking, already-bound row keeps its own item while the unbound copy binds the elected winner, plus all prior single-row regressions.SyncDaoTest+2: fan-out through the real Room DAO (3 rows, one change,appliedChanges = 1) and twin election throughRoomSyncPullStateRepository.BackupEngineTest+1 end-to-end: 3 seeded copies + one purged change page ⇒shadowedCount = 3,skippedCount = 0, all rows bound to the purged item.Verification
rtk ./gradlew :core:test→ 140 tests, 0 failures.rtk ./gradlew :app:testDebugUnitTest→ 649 tests, 0 failures. INV-1 untouched (write surface stays the three shadow columns); no server changes, no spec edits, no UI changes.ChangeBinder rule 2 demanded a UNIQUE unbound row per content hash, so a device holding several copies of one image (same SHA-256) never bound: server trash/purge verdicts never reached any copy, rows stayed plain UPLOADED and the reconciler reported them as skipped — the spec 065 S8 library-removed card ('Bewusst nicht in Memories') never appeared and the deletion was invisible on the device. - Rule 2 now fans out: ALL unbound UPLOADED rows matching (contentHash, byteSize-corroborated) bind to the change — content identity is the server's identity rule (spec 001 FR-068e/FR-068b1 dedupe), so same-hash device copies are one server identity, not an ambiguity. claimedHashes one-claim guard kept; rule 1 (direct serverMediaItemId refresh) untouched and final (no rebinding). - Same-hash election per page (spec 065 C5 purge+reupload twins): lifecycle active > trashed > permanently_deleted, tiebreak higher changeSeq; only the winning change binds the hash's rows. Unknown lifecycle values rank below known ones. - Room applyPage now reports appliedChanges = changes.size per the interface contract (binding is fan-out; shadow writes overstate the pull metric — 6 changes could count as 18). - KNOWN_ISSUES.md: ISSUE-192 (resolved). Tests: ChangeBinderTest reworked (19 cases: AVD 18-rows/6-hashes shape, purge+active twin, trashed-vs-purged, size-mismatch exclusion, bound-row no-rebind, regressions); SyncDaoTest +2 (Room fan-out + twin election through the real DAO); BackupEngineTest +1 (3 copies shadow, 0 skipped).