diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts index 219afbab8..1bfb167f3 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.test.ts @@ -1170,9 +1170,11 @@ test("no-op sync does not persist stale credentials after a concurrent provider EncryptionService.decryptPayload = async () => localPayload; try { - for (const raceAt of ["stored", "anchor", "connection"] as const) { + for (const raceAt of ["stored", "storage", "anchor", "connection"] as const) { let saveCalls = 0; + let anchorWrites = 0; let uploads = 0; + let storedToken = raceAt === "stored" ? "fresh" : "old"; const manager = { masterPassword: "pw", adapters: new Map(), @@ -1199,15 +1201,19 @@ test("no-op sync does not persist stale credentials after a concurrent provider checkProviderConflict: async () => ({ conflict: false, remoteFile: checkedRemote }), loadProviderConnection: () => ({ ...manager.state.providers.github, - tokens: { accessToken: raceAt === "stored" ? "fresh" : "old" }, + tokens: { accessToken: storedToken }, }), loadSyncBase: async () => localPayload, saveSyncBase: async () => {}, - saveSyncAnchor: async () => { + saveSyncAnchor: async (_provider: CloudProvider, _file: SyncedFile, + _resourceId: string | null, assertCanPersist?: () => void) => { + if (raceAt === "storage") storedToken = "fresh"; if (raceAt === "anchor") { manager.providerWriteSeq.github += 1; manager.providerDecryptSeq.github += 1; } + assertCanPersist?.(); + anchorWrites += 1; }, saveProviderConnection: async () => { saveCalls += 1; @@ -1231,10 +1237,11 @@ test("no-op sync does not persist stale credentials after a concurrent provider assert.equal(result?.success, false, raceAt); assert.match(result?.error ?? "", /Provider connection changed/, raceAt); assert.equal(saveCalls, raceAt === "connection" ? 1 : 0, raceAt); + assert.equal(anchorWrites, raceAt === "connection" ? 1 : 0, raceAt); assert.equal(uploads, 0, raceAt); assert.equal(manager.state.providers.github.status, "error", raceAt); assert.equal(manager.providerDecryptSeq.github, - raceAt === "stored" ? 0 : raceAt === "anchor" ? 1 : 2, raceAt); + raceAt === "connection" ? 2 : raceAt === "anchor" ? 1 : 0, raceAt); if (raceAt === "connection") { assert.equal(manager.state.providers.github.tokens.accessToken, "fresh"); } diff --git a/infrastructure/services/cloudSync/syncAllStorageMethods.ts b/infrastructure/services/cloudSync/syncAllStorageMethods.ts index e0fb2c6dc..c3fc783f2 100644 --- a/infrastructure/services/cloudSync/syncAllStorageMethods.ts +++ b/infrastructure/services/cloudSync/syncAllStorageMethods.ts @@ -600,6 +600,7 @@ export async function syncAllProvidersImpl(this: any, const uploadTasks = validUploads.map(async ({ provider, adapter }) => { try { const originalProviderWriteSeq = providerWriteSeqAtStart.get(provider); + let storedProviderFingerprint: string | null = null; const assertProviderConnectionUnchanged = () => { if ( originalProviderWriteSeq != null @@ -609,6 +610,17 @@ export async function syncAllProvidersImpl(this: any, 'Provider connection changed during sync; retry with its latest credentials', ); } + // Storage events arrive asynchronously in this window. Re-read the + // persisted connection so a peer-window write is visible even before + // its event has advanced providerWriteSeq here. + if ( + storedProviderFingerprint !== null + && JSON.stringify(this.loadProviderConnection(provider)) !== storedProviderFingerprint + ) { + throw new ProviderConnectionChangedDuringSyncError( + 'Provider connection changed during sync; retry with its latest credentials', + ); + } }; const entry = checkResults.find((result) => result.provider === provider); const checkedRemoteFile = entry?.check?.remoteFile; @@ -654,9 +666,9 @@ export async function syncAllProvidersImpl(this: any, // this sync starts, so the write sequence alone can look stable // while in-memory credentials still lag the stored connection. if (typeof this.loadProviderConnection === 'function') { - const storedConnection = await decryptProviderSecrets( - this.loadProviderConnection(provider), - ); + const rawStoredConnection = this.loadProviderConnection(provider); + storedProviderFingerprint = JSON.stringify(rawStoredConnection); + const storedConnection = await decryptProviderSecrets(rawStoredConnection); assertSyncSecurityGeneration(this, syncSecurityGeneration); assertProviderConnectionUnchanged(); const identityFields = (connection: ProviderConnection) => JSON.stringify({