fix(desktop): stop pushing a snapshot that changed nothing

Measured on the phone, opening a cached vault: 121 ms to read the sealed file,
14 ms to display it - and then 5718 ms decrypting a snapshot it already had.

The cause is upstream. A sync pushes unconditionally at the end, and the push
is never a no-op: the container is re-encrypted with a fresh salt and IV, so an
identical vault produces completely different bytes. On Gitea that is one more
commit and a new blob sha, so every other device sees a "new" snapshot and pays
a full key derivation to arrive back where it started. It also grew the
repository on every sync that changed nothing, which is the history cost the
plan warns about.

The merged state is now compared against what was pulled, as plaintext -
the only place equality is observable - ignoring snapshot_at, which changes by
construction. Equal means the push is skipped and the sync still counts as done,
timestamp included: having nothing to write is a success, not an absence.

The comparison is deliberately strict. A false negative costs one commit; a
false positive would silence a push that carried a change and lose it for the
other devices. Four tests fix where that line sits, including a modified
updated_at (which arbitrates the merge elsewhere) and a tombstone (a deletion
that has to propagate).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
r-zakarya
2026-08-17 15:41:44 +01:00
parent aa87580443
commit f97e44768a
4 changed files with 121 additions and 1 deletions
+40
View File
@@ -67,6 +67,26 @@ function giteaUnwrap(bodyText) {
} catch (e) { return null; }
}
// Do these two snapshots carry the same vault? Compared as plaintext, because
// the ciphertext never repeats — a fresh salt and IV per encryption make two
// containers of an identical vault differ completely.
//
// `snapshot_at` is stripped: it is the time of the build, not part of what the
// vault contains, and keeping it would make every comparison fail. Everything
// else is compared verbatim, which is deliberately strict — a difference in key
// order or in a field we have not thought about falls on the side of pushing,
// which costs a commit rather than losing a change.
function syncSnapshotsEqual(a, b) {
if (!a || !b) return false;
const strip = s => {
const c = Object.assign({}, s);
delete c.snapshot_at;
delete c.exported_at;
return JSON.stringify(c);
};
try { return strip(a) === strip(b); } catch (e) { return false; }
}
// method: 'get'|'put'|'test'. opts.ifMatch → sent as If-Match on a put so
// the server rejects (412) a write when the remote changed since our pull.
// On Gitea the sha travels inside the PUT body instead, and the bridge sends
@@ -668,6 +688,26 @@ async function runSyncNow(_attempt) {
await loadEntries(); // pull latest after applying remote changes
const snap = await buildSyncSnapshot();
pushedCount = Array.isArray(snap.entries) ? snap.entries.length : 0;
// RIEN A DIRE, RIEN A ECRIRE. Un push a lieu meme quand la fusion n'a
// rien change, et il ne passe pas inapercu : le conteneur est rechiffre
// avec un sel et un IV neufs, donc son contenu differe a chaque fois
// meme a coffre identique. Sur Gitea cela vaut un commit de plus et un
// nouveau sha ; les autres appareils voient alors un instantane
// "nouveau" et paient une derivation de cle complete - mesuree a 5,7 s
// sur le telephone - pour retrouver exactement ce qu'ils avaient deja.
//
// La comparaison porte sur le CLAIR, seul endroit ou l'egalite est
// observable, et ignore `snapshot_at`, qui change par construction.
if (remoteSnap && syncSnapshotsEqual(remoteSnap, snap)) {
syncStatus('');
// La synchronisation a bien eu lieu, et l'horodatage le dit : ne
// rien avoir a ecrire est une reussite, pas une absence.
Bridge.setPref(SYNC_PREFS.last, new Date().toISOString());
render();
return toast('Already up to date · ' + pushedCount + ' entries');
}
syncStatus('Encrypting…');
const container = await encryptExportPayload(snap, cfg.encPwd);
const containerBytes = new TextEncoder().encode(JSON.stringify(container, null, 2));