From 318577cd72072ae32df352b809199ef40d7d0756 Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Thu, 24 Sep 2026 16:52:16 +0800 Subject: [PATCH] fix(sync): avoid version ping-pong for identical providers --- .../cloudSync/syncAllStorageMethods.test.ts | 83 ++++++++++++++++++- .../cloudSync/syncAllStorageMethods.ts | 11 +-- 2 files changed, 83 insertions(+), 11 deletions(-) diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts index a2731e8c4..8d42e5ccd 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts @@ -993,7 +993,7 @@ test("syncAllProviders skips the upload when the payload already matches the pro } }); -test("syncAllProviders still uploads when the remote version is behind the local version", async () => { +test("syncAllProviders skips an identical remote even when its version is behind another provider", async () => { const originalDecryptPayload = EncryptionService.decryptPayload; const originalEncryptPayload = EncryptionService.encryptPayload; const checkedRemote = remoteFile("github", 3, 300); @@ -1045,10 +1045,85 @@ test("syncAllProviders still uploads when the remote version is behind the local const results = await syncAllProvidersImpl.call(manager, localPayload); - // Identical data but the remote version is behind: keep the monotonic - // upload so the version counter never regresses. + assert.equal(uploads, 0); + assert.equal(results.get("github")?.action, "none"); + assert.equal(results.get("github")?.version, 3); + assert.equal(manager.state.localVersion, 10); + } finally { + EncryptionService.decryptPayload = originalDecryptPayload; + EncryptionService.encryptPayload = originalEncryptPayload; + } +}); + +test("syncAllProviders leaves two converged providers idle across repeated cycles", async () => { + const originalDecryptPayload = EncryptionService.decryptPayload; + const originalEncryptPayload = EncryptionService.encryptPayload; + const localPayload = payload("local"); + const remotes = { + github: remoteFile("github", 10, 1000), + google: remoteFile("google", 7, 700), + }; + let uploads = 0; + + EncryptionService.decryptPayload = async (file: SyncedFile) => + file === remotes.github ? localPayload : payload("old-google"); + EncryptionService.encryptPayload = async (_outgoing: SyncPayload, _password: string, + _deviceId: string, _deviceName: string, _appVersion: string, baseVersion: number) => + remoteFile("google", baseVersion + 1, 1100); + + try { + const manager = { + masterPassword: "pw", + adapters: new Map(), + state: { + securityState: "UNLOCKED", + providers: { + github: { enabled: true, connected: true, status: "connected" }, + google: { enabled: true, connected: true, status: "connected" }, + }, + lastError: null, + syncState: "IDLE", + syncStrategy: "smartMerge", + localVersion: 10, + deviceId: "local-device", + deviceName: "Local", + }, + getConnectedAdapter: async (provider: CloudProvider) => ({ provider }), + updateProviderStatus: () => {}, + emit: () => {}, + checkProviderConflict: async (provider: "github" | "google") => ({ + conflict: false, + remoteFile: remotes[provider], + }), + loadSyncBase: async () => localPayload, + saveSyncBase: async () => {}, + saveSyncAnchor: async () => {}, + saveProviderConnection: async () => {}, + saveSyncConfig: () => {}, + uploadToProvider: async (provider: CloudProvider, _adapter: unknown, file: SyncedFile) => { + uploads += 1; + remotes[provider as "github" | "google"] = file; + manager.state.localVersion = file.meta.version; + return { success: true, provider, action: "upload" as const, version: file.meta.version }; + }, + exitBlockedState: () => {}, + notifyStateChange: () => {}, + }; + + const first = await syncAllProvidersImpl.call(manager, localPayload); + assert.equal(first.get("github")?.action, "none"); + assert.equal(first.get("google")?.action, "upload"); assert.equal(uploads, 1); - assert.equal(results.get("github")?.action, "upload"); + assert.equal(manager.state.localVersion, 11); + + EncryptionService.decryptPayload = async () => localPayload; + for (let cycle = 0; cycle < 3; cycle += 1) { + const result = await syncAllProvidersImpl.call(manager, localPayload); + assert.equal(result.get("github")?.action, "none"); + assert.equal(result.get("google")?.action, "none"); + } + assert.equal(uploads, 1); + assert.equal(manager.state.localVersion, 11); } finally { EncryptionService.decryptPayload = originalDecryptPayload; EncryptionService.encryptPayload = originalEncryptPayload; diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.ts index 6e731023f..3628f7240 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.ts @@ -582,13 +582,10 @@ export async function syncAllProvidersImpl(this: any, // a fresh cloud revision for unchanged data. This is exactly what the // periodic remote check's download-remote round-trip and a smart-merge // without a real diff produce, so the cloud version inflated every - // cycle while the app sat idle. Only skip when the remote version is - // not behind the local one, so keep-local resolutions keep their - // monotonic version bump. - if ( - checkedRemoteFile - && checkedRemoteFile.meta.version >= (Number(this.state.localVersion) || 0) - ) { + // cycle while the app sat idle. Providers may hold the same payload at + // different versions; requiring this remote version to match the global + // local version would make them upload in turns forever. + if (checkedRemoteFile) { try { assertSyncSecurityGeneration(this, syncSecurityGeneration); const checkedRemotePayload = await EncryptionService.decryptPayload(