From 8d6ef7200638d5da09f8a33cadd3b7dbcf2a4f83 Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Sun, 27 Sep 2026 14:30:07 +0800 Subject: [PATCH] fix(sync): stop upload when no-op state persistence fails --- .../cloudSync/syncAllStorageMethods.test.ts | 65 ++++++++++++++++++- .../cloudSync/syncAllStorageMethods.ts | 15 +++-- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts index 7236ee96c..4e9007037 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts @@ -926,7 +926,9 @@ test("syncAllProviders skips the upload when the payload already matches the pro assert.equal(file, checkedRemote); return checkedRemotePayload; }; - EncryptionService.encryptPayload = async (outgoing: SyncPayload) => { + EncryptionService.encryptPayload = async (outgoing: SyncPayload, _password: string, + _deviceId: string, _deviceName: string, _appVersion: string, baseVersion: number) => { + assert.equal(baseVersion, 7); encryptCalls += 1; encryptedPayloads.push(outgoing); return remoteFile("github", 8, 800); @@ -949,8 +951,8 @@ test("syncAllProviders skips the upload when the payload already matches the pro lastError: null, syncState: "IDLE", syncStrategy: "smartMerge", - localVersion: 7, - remoteVersion: 7, + localVersion: 3, + remoteVersion: 3, remoteUpdatedAt: 700, deviceId: "local-device", deviceName: "Local", @@ -995,6 +997,7 @@ test("syncAllProviders skips the upload when the payload already matches the pro assert.equal(Reflect.get(manager.state.providers.github, "lastSyncVersion"), 7); assert.equal(Reflect.get(manager.state.providers.github, "resourceId"), "resource-7"); assert.equal(manager.providerDecryptSeq.github, 5); + assert.equal(manager.state.localVersion, 7); // A remote learned a deletion elsewhere while this device's base still // has the same materialized data. Its newer base must be kept locally. @@ -1110,6 +1113,62 @@ test("no-op sync stops when the vault locks during persistence", async () => { } }); +test("no-op sync does not upload when saving a newer remote base fails", async () => { + const originalDecryptPayload = EncryptionService.decryptPayload; + const originalEncryptPayload = EncryptionService.encryptPayload; + const localPayload = payload("local"); + const checkedRemotePayload = withSyncReliabilityMeta( + localPayload, + payloadWithHosts(["local", "deleted"]), + { deviceId: "remote-device", now: 700 }, + ); + const checkedRemote = remoteFile("github", 7, 700); + let uploads = 0; + EncryptionService.decryptPayload = async () => checkedRemotePayload; + EncryptionService.encryptPayload = async () => remoteFile("github", 8, 800); + + try { + const manager = { + masterPassword: "pw", + adapters: new Map(), + providerDecryptSeq: { github: 0 }, + state: { + securityState: "UNLOCKED", + providers: { github: { enabled: true, connected: true, status: "connected" } }, + lastError: null, + syncState: "IDLE", + syncStrategy: "smartMerge", + localVersion: 7, + deviceId: "local-device", + deviceName: "Local", + }, + getConnectedAdapter: async () => ({ provider: "github" }), + updateProviderStatus: () => {}, + emit: () => {}, + checkProviderConflict: async () => ({ conflict: false, remoteFile: checkedRemote }), + loadSyncBase: async () => localPayload, + saveSyncBase: async () => { throw new Error("storage full"); }, + saveSyncAnchor: async () => {}, + saveProviderConnection: async () => {}, + saveSyncConfig: () => {}, + uploadToProvider: async () => { + uploads += 1; + return { success: true, provider: "github" as const, action: "upload" as const }; + }, + exitBlockedState: () => {}, + notifyStateChange: () => {}, + }; + + const results = await syncAllProvidersImpl.call(manager, localPayload); + assert.equal(uploads, 0); + assert.equal(results.get("github")?.success, false); + assert.match(results.get("github")?.error ?? "", /storage full/); + } finally { + EncryptionService.decryptPayload = originalDecryptPayload; + EncryptionService.encryptPayload = originalEncryptPayload; + } +}); + test("syncAllProviders uploads missing deletion records, then skips once the remote has them", async () => { const originalDecryptPayload = EncryptionService.decryptPayload; const originalEncryptPayload = EncryptionService.encryptPayload; diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.ts index 56dd1752e..68308ee2d 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.ts @@ -595,13 +595,21 @@ export async function syncAllProvidersImpl(this: any, // different versions; requiring this remote version to match the global // local version would make them upload in turns forever. if (checkedRemoteFile) { + let checkedRemotePayload: SyncPayload | null = null; try { assertSyncSecurityGeneration(this, syncSecurityGeneration); - const checkedRemotePayload = await EncryptionService.decryptPayload( + checkedRemotePayload = await EncryptionService.decryptPayload( checkedRemoteFile, this.masterPassword, ); assertSyncSecurityGeneration(this, syncSecurityGeneration); + } catch { + assertSyncSecurityGeneration(this, syncSecurityGeneration); + // A decrypt failure cannot prove equality. The normal upload path + // still handles a real local edit, but persistence failures below + // must stop instead of replacing a newer remote revision. + } + if (checkedRemotePayload) { const payloadMatches = cloudSyncPayloadsEqual(payload, checkedRemotePayload); const providerBase = payloadMatches ? await this.loadSyncBase(provider) : null; const deletionsCovered = payloadMatches && remoteCoversSyncDeletions( @@ -691,11 +699,6 @@ export async function syncAllProvidersImpl(this: any, results.set(provider, noOpResult); return; } - } catch { - assertSyncSecurityGeneration(this, syncSecurityGeneration); - // Could not prove the payloads identical (decrypt failure, storage - // failure). Fall through to the normal upload path — a real data - // change must never be dropped because this guard failed. } } assertConvergentSyncWriteCompatible(entry?.check?.remoteFile?.meta, payload);