fix(sync): translate positions only against the frames they name

Positions crossing copies could still land on other photos in three
windows:

- A push translated a row snapshot's settings against local frames
  listed later, after a manifest round trip: an import of earlier photos
  meanwhile moved the row's positions, and the server stored shifted
  exclusions, hero and keyframes. SyncPositions now reads this copy's
  row with its frames as one state (frames listed between two reads of
  the row at the same frames version) and translates that row's
  settings, at the settings version being pushed. The conflict
  comparison and ApplyLocallyAsync compare against that same row. When
  the copy was edited since, or its frames keep changing, nothing is
  sent or applied and the next comparison translates again (Keep mine
  reports a changed conflict; PushAll sends an edit saved meanwhile).
- The manifest read the sequence row, then paged its frames: an upload
  committed in between paired settings of one frames version with
  frames of the next. The server now lists them again while the frames
  version moved during the listing, then inside a transaction holding
  the row (on SQLite, on that transaction's connection, so no second
  pooled connection waits behind the write lock). The client also
  refuses to translate against a manifest whose frames are not each
  listed once at 0..n-1.
- Server capture times were fetched only when something was unhashed,
  so the same parked photo was written with or without capturedAt. They
  are now fetched whenever the two frame lists differ; an equal project
  then compares equal, and an entry can be placed on a photo that
  arrives unhashed.

CompareOriginals counted unhashed server frames one-to-one against this
PC's photos, so two copies of one photo in a folder the server adopted
left Download on offer forever while the download fetched nothing. A
photo on either side is now there when any photo of the other side
matches it, as downloads and uploads find them. Paged frame listings
keep each frame once (an insertion between pages lists one again).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 17:09:30 +00:00
1 parent 98e1486f83
commit a56aaf8d65
12 files changed
+322 -95

No files matched your search

@@ -74,6 +74,18 @@ internal sealed class SyncFixture : IAsyncDisposable
return sequence;
}
/// <summary>Files one more original into a local sequence through the archive ingest (an earlier photo lands before the others).</summary>
public async Task AddLocalAsync(Guid sequenceId, byte[] original)
{
var staging = Path.Combine(Archive.Paths.StagingDirectory, "test");
Directory.CreateDirectory(staging);
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
await File.WriteAllBytesAsync(path, original, Ct);
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(original));
await Archive.Service.Ingest.CommitAsync(new IngestRequest(Archive.Archive.Id, sequenceId, null, null, path, $"IMG_{sha[..8]}.JPG", original.Length, sha,
null, null), Ct);
}
/// <summary>An edit on this PC, as the editor makes it (project file and index, settings version + 1).</summary>
public async Task<Sequence> EditLocalSettingsAsync(Guid id, string settingsJson)
{
@@ -79,6 +79,11 @@ public sealed class SyncRulesTests
Assert.Equal((0, 3), SyncRules.CompareOriginals(Array.Empty<string?>(), ["", "", ""]));
// Hashed on both sides, the hash decides even when size and time agree.
Assert.Equal((1, 1), SyncRules.CompareOriginals([Photo("aa", 100, 0)], [Photo("cc", 100, 0)]));
// A photo the adopted folder holds twice ('IMG_0001.JPG' and 'IMG_0001 (1).JPG') is here in this PC's one copy,
// as the download finds it: nothing is left to fetch.
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo("aa", 100, 0)], [Photo("", 100, 0), Photo("", 100, 0)]));
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo(null, 100, 0)], [Photo("", 100, 0), Photo("", 100, 0)]));
Assert.Equal((0, 1), SyncRules.CompareOriginals([Photo("aa", 100, 0)], [Photo("", 100, 0), Photo("", 100, 0), Photo("", 100, 10)]));
}
[Test]
@@ -144,6 +144,53 @@ public sealed class SyncServiceTests(TestContext context)
Assert.Equal((SyncStatus.ServerAhead, 1), ((await f.EntryAsync("Harbour")).Status, (await f.EntryAsync("Harbour")).Link!.MissingLocal));
}
[Test]
public async Task SettingsSentWhileAnImportHereMovesTheirPositionsStayOnTheirPhotos()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Harbour", photos[1..]);
var remote = f.Remote.Add("Harbour", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [1], HeroFrame = 2 }.ToJson());
// The earliest photo is imported here after the comparison read this copy, before its settings are translated.
var racing = new RacingRemote(f.Remote, () => f.AddLocalAsync(local.Id, photos[0]).GetAwaiter().GetResult(), beforeManifest: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
var mine = ProjectSettings.FromJson((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson!);
Assert.SequenceEqual([2], mine.ExcludedFrames);
Assert.Equal(3, mine.HeroFrame);
var sent = ProjectSettings.FromJson(remote.SettingsJson!);
Assert.SequenceEqual([Sha(photos[2])], sent.ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal(Sha(photos[3]), remote.Frames[sent.HeroFrame!.Value].Sha256);
Assert.Null(sent.PendingFrames);
}
[Test]
public async Task SettingsComparedWhileAnImportHereMovesTheirPositionsAreAgreed()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Harbour", photos[1..]);
var remote = f.Remote.Add("Harbour", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
// Both sides exclude the same photo, each by its own frames.
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [1] }.ToJson());
f.Remote.EditSettings(remote.Id, new ProjectSettings { ExcludedFrames = [2] }.ToJson());
var racing = new RacingRemote(f.Remote, () => f.AddLocalAsync(local.Id, photos[0]).GetAwaiter().GetResult(), beforeManifest: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
var entry = await f.EntryAsync("Harbour");
Assert.Null(entry.Link!.ConflictRemote);
Assert.Equal(entry.Local!.SettingsVersion, entry.Link.BaseLocalSettings);
Assert.Equal(remote.SettingsVersion, entry.Link.BaseRemoteSettings);
Assert.SequenceEqual([2], ProjectSettings.FromJson(entry.Local.SettingsJson!).ExcludedFrames);
}
[Test]
public async Task TheSameSettingsFromTheServerAreAgreedWhileTheArchiveIsOffline()
{
@@ -366,6 +366,9 @@ public sealed class SyncTransferTests(TestContext context)
Assert.Null(taken.Hero);
Assert.SequenceEqual([Sha(photos[2])], taken.Keyframes);
Assert.True(taken.Pending.ToHashSet().SetEquals([Sha(photos[0]), Sha(photos[5])]), "the photos this copy lacks wait for them");
// Each waits with its capture time too, whichever side hashed what: the same project reads the same either way.
var waiting = ProjectSettings.FromJson((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson!).PendingFrames!;
Assert.True(waiting.All(p => p.CapturedAt is not null), "a photo waits with its capture time");
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);