@@ -239,22 +239,26 @@ public async Task<SyncJobResult> ExecuteSync(CancellationToken stoppingToken, bo
239239 }
240240 }
241241
242- //We defer regenerating the snapshot until we know the changes applied to fwdata were successfully pushed.
243- //Otherwise, the changes might have been rolled back. In that case, the next sync could overwrite
244- //CRDT data with old/rolled back FW data.
245- if ( result . CrdtChanges > 0 )
246- {
247- logger . LogInformation ( "Regenerating project snapshot to include {CrdtChangeCount} crdt changes" , result . CrdtChanges ) ;
248- //note we are using the crdt API, this avoids issues where some data isn't synced yet
249- //later when we add the ability to sync that data we need the snapshot to reflect the synced state, not what was in the FW project
250- //related to https://github.com/sillsdev/languageforge-lexbox/issues/1912
251- await projectSnapshotService . RegenerateProjectSnapshot ( miniLcmApi , fwdataApi . Project , keepBackup : false ) ;
252- }
253- else
254- {
255- logger . LogInformation ( "Skipping regenerating project snapshot, because there were no crdt changes" ) ;
256- }
257-
242+ /*
243+ Notes:
244+ 1) We defer regenerating the snapshot until we know the changes applied to fwdata were successfully pushed.
245+ Otherwise, the changes might have been rolled back. In that case, the next sync could overwrite CRDT data with old/rolled back FW data.
246+ 2) We are intentionaly using the crdt API. This avoids issues when new data/fields don't yet get synced.
247+ When we start syncing that data/those fields we need the snapshot to reflect the CRDT state, rather than the FW project,
248+ otherwise existing FW data will never be synced to CRDT. Related to https://github.com/sillsdev/languageforge-lexbox/issues/1912
249+ 3) We should always regenerate the snapshot even if no changes were detected.
250+ If the same change was made in both fwdata and crdt, no changes would be detected,
251+ but we still need that change to get into the snapshot
252+ */
253+ logger . LogInformation ( "Regenerating project snapshot" ) ;
254+ await projectSnapshotService . RegenerateProjectSnapshot ( miniLcmApi , fwdataApi . Project , keepBackup : false ) ;
255+
256+ /*
257+ Push new changes to Harmony (changes that came from FW)
258+ Important: we only sync the Harmony project AFTER regenerating the snapshot.
259+ Otherwise, the sync could pull changes into the snapshot that were not respected during the sync.
260+ Could presumably be skipped if 0 CrdtChanges, but it's cheap.
261+ */
258262 await crdtSyncService . SyncHarmonyProject ( ) ;
259263
260264 activity ? . SetStatus ( ActivityStatusCode . Ok , "Sync finished" ) ;
0 commit comments