fix(sync): keep project positions on their photos when settings cross copies

Settings synced between a PC and its server sequence were stored as they
came, in the other copy's frame order. When the inserted photos arrived
later (a download, an upload), the index moved those already-correct
positions a second time, so exclusions, the hero and camera keyframes
landed on other photos (V-21, CD-2).

- Settings are translated by photo (SHA-256, else size and capture time)
  to the frames of the copy they go to: the server's before they are
  compared or applied here, this PC's to the server's frames as its
  manifest lists them before a push (SyncPositions).
- A position on a photo the receiving copy lacks waits in project.json as
  a pending frame and takes its place when the photo arrives, on an
  insertion or an append in step with the index; the Library's hero
  frame follows. A hero chosen meanwhile wins, and advice does not fill
  a hero while one waits. Old projects without the member are unchanged.
- Pushes send expectedFramesVersion; PATCH /api/v1/sequences/{id} answers
  409 frames_conflict when the frames changed since, and the next
  comparison translates again.
- A pulled payload equal to the local copy is settled without the
  archive: an offline or read-only archive no longer fails the
  comparison (V-10).

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 16:20:11 +00:00
1 parent 32a0e9bbc1
commit ea6e1a69e0
25 files changed
+1091 -51

No files matched your search

@@ -249,6 +249,117 @@ public sealed class SyncServerTests(TestContext context)
Assert.Equal(0, (await laptop.EntryAsync("Star trails")).Link!.ExportsToGet);
}
[Test]
public async Task PositionsEditedOnTheServerStayOnTheirPhotosWhenAnotherPcDownloadsTheInsertedFrames()
{
await using var server = await StartServerAsync();
var (http, _, token) = await server.UserAsync("alberto", "editor");
await using var studio = await SyncClient.StartAsync(context, server, token, "studio");
await using var laptop = await SyncClient.StartAsync(context, server, token, "laptop");
var photos = Enumerable.Range(60, 5).Select(SyncClient.Photo).ToList();
// Both PCs hold the last four photos, the third of them excluded.
var local = await studio.LocalAsync("Harbour at dawn", photos[1..], new ProjectSettings { ExcludedFrames = [2] }.ToJson());
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
var remoteId = (await studio.EntryAsync("Harbour at dawn")).Link!.RemoteId;
await ServerIdleAsync(server);
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
await laptop.WaitAsync((await laptop.Sync.EnqueueDownloadAsync(laptop.Engine, laptop.Server, remoteId, "Harbour at dawn", SyncDownloadMode.Originals,
laptop.Archive.Id)).Id);
var copy = (await laptop.EntryAsync("Harbour at dawn")).Local!;
// The studio files an earlier photo and uploads it: the server inserts it first and moves its own positions.
await studio.AddPhotoAsync(local.Id, photos[0], new DateTime(2026, 9, 28, 17, 59, 50));
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
await ServerIdleAsync(server);
Assert.SequenceEqual([SyncClient.Sha(photos[3])], (await ServerNamedAsync(server, remoteId)).Excluded);
// An editor on the server excludes the new photo too, by the server's frames, and changes the deflicker window.
var row = (await server.Database.Sequences.GetAsync(remoteId))!;
var current = ProjectSettings.FromJson(row.SettingsJson!);
var edit = current with
{
ExcludedFrames = [0, .. current.ExcludedFrames],
Exposure = current.Exposure with { DeflickerWindow = new Auto<int>().WithManual(33) },
};
using (var patch = await http.PatchAsJsonAsync($"/api/v1/sequences/{remoteId}",
new UpdateSequenceRequest(null, null, null, null, edit.ToJson(), row.SettingsVersion), ProtocolJson.Default.UpdateSequenceRequest))
await patch.ExpectAsync(HttpStatusCode.OK);
// The laptop takes the edit while it still lacks the new photo, then downloads it.
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
var taken = await LocalNamedAsync(laptop, copy.Id);
Assert.Equal(33, taken.Project.Exposure.DeflickerWindow.Manual);
Assert.SequenceEqual([SyncClient.Sha(photos[3])], taken.Excluded);
Assert.True(taken.Project.PendingFrames is [{ Excluded: true } waiting] && waiting.Sha256 == SyncClient.Sha(photos[0]), "the new photo's exclusion waits for it");
await laptop.WaitAsync((await laptop.Sync.EnqueueDownloadAsync(laptop.Engine, laptop.Server, remoteId, "Harbour at dawn", SyncDownloadMode.Originals,
null)).Id);
var onServer = await ServerNamedAsync(server, remoteId);
var here = await LocalNamedAsync(laptop, copy.Id);
Assert.SequenceEqual([SyncClient.Sha(photos[0]), SyncClient.Sha(photos[3])], here.Excluded);
Assert.SequenceEqual(onServer.Excluded, here.Excluded);
Assert.Equal(onServer.Hero, here.Hero);
Assert.Null(here.Project.PendingFrames);
await laptop.Sync.RefreshAsync(laptop.Remote, new SyncPolicy(), context.Cancellation);
Assert.Equal(SyncStatus.InSync, (await laptop.EntryAsync("Harbour at dawn")).Status);
}
[Test]
public async Task PositionsSentBeforeTheirPhotosStayOnThemWhenTheUploadArrives()
{
await using var server = await StartServerAsync();
var (_, _, token) = await server.UserAsync("alberto", "editor");
await using var studio = await SyncClient.StartAsync(context, server, token, "studio");
var photos = Enumerable.Range(70, 5).Select(SyncClient.Photo).ToList();
var local = await studio.LocalAsync("Pier", photos[1..]);
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
var remoteId = (await studio.EntryAsync("Pier")).Link!.RemoteId;
await ServerIdleAsync(server);
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
// This PC files an earlier photo and, before uploading it, excludes it and a later one and picks the hero.
await studio.AddPhotoAsync(local.Id, photos[0], new DateTime(2026, 9, 28, 17, 59, 50));
var project = ProjectSettings.FromJson((await studio.Db.Sequences.GetAsync(local.Id))!.SettingsJson ?? "{}");
await studio.EditSettingsAsync(local.Id, (project with { ExcludedFrames = [0, 3], HeroFrame = 1 }).ToJson());
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
var sent = await ServerNamedAsync(server, remoteId);
Assert.SequenceEqual([SyncClient.Sha(photos[3])], sent.Excluded);
Assert.Equal(SyncClient.Sha(photos[1]), sent.Hero);
Assert.True(sent.Project.PendingFrames is [{ Excluded: true } waiting] && waiting.Sha256 == SyncClient.Sha(photos[0]), "the exclusion waits for its photo");
await studio.WaitAsync((await studio.Sync.EnqueueUploadAsync(studio.Engine, studio.Server, local)).Id);
await ServerIdleAsync(server);
var arrived = await ServerNamedAsync(server, remoteId);
Assert.SequenceEqual([SyncClient.Sha(photos[0]), SyncClient.Sha(photos[3])], arrived.Excluded);
Assert.Equal(SyncClient.Sha(photos[1]), arrived.Hero);
Assert.Null(arrived.Project.PendingFrames);
var heroFrame = (await server.Database.Sequences.GetAsync(remoteId))!.HeroFrameId;
Assert.Equal(SyncClient.Sha(photos[1]), (await server.Database.Frames.ListBySequenceAsync(remoteId)).Single(f => f.Id == heroFrame).Sha256);
var here = await LocalNamedAsync(studio, local.Id);
Assert.SequenceEqual(arrived.Excluded, here.Excluded);
Assert.Equal(arrived.Hero, here.Hero);
await studio.Sync.RefreshAsync(studio.Remote, new SyncPolicy(), context.Cancellation);
Assert.Equal(SyncStatus.InSync, (await studio.EntryAsync("Pier")).Status);
}
/// <summary>The server's project and the photos (SHA-256) it excludes and shows as hero.</summary>
private static async Task<(ProjectSettings Project, string?[] Excluded, string? Hero)> ServerNamedAsync(TestServer server, Guid remoteId)
{
var project = ProjectSettings.FromJson((await server.Database.Sequences.GetAsync(remoteId))!.SettingsJson ?? "{}");
var frames = await server.Database.Frames.ListBySequenceAsync(remoteId);
return (project, [.. project.ExcludedFrames.Select(i => frames[i].Sha256)], project.HeroFrame is { } hero ? frames[hero].Sha256 : null);
}
/// <summary>A PC's project and the photos (SHA-256) it excludes and shows as hero.</summary>
private static async Task<(ProjectSettings Project, string?[] Excluded, string? Hero)> LocalNamedAsync(SyncClient client, Guid localId)
{
var project = ProjectSettings.FromJson((await client.Db.Sequences.GetAsync(localId))!.SettingsJson ?? "{}");
var frames = await client.Db.Frames.ListBySequenceAsync(localId);
return (project, [.. project.ExcludedFrames.Select(i => frames[i].Sha256)], project.HeroFrame is { } hero ? frames[hero].Sha256 : null);
}
private Task<TestServer> StartServerAsync() => TestServer.StartAsync(context, o => o.ChunkSizeBytes = 64 * 1024, ArchiveModule.Register);
/// <summary>Waits until the server's engines finished what the commits queued (thumbnails, proxies, analysis, which may edit settings).</summary>