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

@@ -1,4 +1,5 @@
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Sync;
using Titano.Protocol;
using Titano.Protocol.Client;
@@ -94,6 +95,85 @@ public sealed class SyncServiceTests(TestContext context)
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task PositionsSentToTheServerNameItsFramesAndWaitForPhotosItLacks()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(5);
var local = await f.LocalAsync("Harbour", photos);
// The server lacks the earliest photo (not uploaded yet).
var remote = f.Remote.Add("Harbour", photos[1..]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [0, 2], HeroFrame = 4 }.ToJson());
await f.RefreshAsync();
var sent = ProjectSettings.FromJson(remote.SettingsJson!);
Assert.SequenceEqual([Sha(photos[2])], sent.ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal(Sha(photos[4]), remote.Frames[sent.HeroFrame!.Value].Sha256);
Assert.Count(1, sent.PendingFrames!);
var waiting = sent.PendingFrames![0];
Assert.True(waiting.Excluded && waiting.Sha256 == Sha(photos[0]), "the exclusion of the photo the server lacks waits for it");
Assert.Equal(remote.FramesVersion, f.Remote.Updates[^1].ExpectedFramesVersion);
// This PC's project keeps its own positions.
Assert.SequenceEqual([0, 2], ProjectSettings.FromJson((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson!).ExcludedFrames);
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
Assert.Equal(1, (await f.EntryAsync("Harbour")).Link!.MissingRemote);
}
[Test]
public async Task SettingsSentWhileTheServersFramesChangeAreTranslatedAgain()
{
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[1..]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [1] }.ToJson());
// An earlier photo is filed on the server after the manifest was read, before the settings arrive.
var racing = new RacingRemote(f.Remote, () => remote.InsertFrame(0, photos[0]), beforeUpdate: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
Assert.Empty(f.Remote.Updates);
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
await f.RefreshAsync();
Assert.SequenceEqual([Sha(photos[2])], ProjectSettings.FromJson(remote.SettingsJson!).ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal((SyncStatus.ServerAhead, 1), ((await f.EntryAsync("Harbour")).Status, (await f.EntryAsync("Harbour")).Link!.MissingLocal));
}
[Test]
public async Task TheSameSettingsFromTheServerAreAgreedWhileTheArchiveIsOffline()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Harbour", photos, SyncFixture.Window41);
var remote = f.Remote.Add("Harbour", photos, SyncFixture.Window41);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
// The server's version moves with the same content (another PC sent the same values); the archive's drive is unplugged.
f.Remote.EditSettings(remote.Id, SyncFixture.Window41);
var aside = f.Archive.Paths.ManifestPath + ".unplugged";
File.Move(f.Archive.Paths.ManifestPath, aside);
try
{
await f.RefreshAsync();
}
finally
{
File.Move(aside, f.Archive.Paths.ManifestPath);
}
var entry = await f.EntryAsync("Harbour");
Assert.Null(entry.Link!.Error);
Assert.Equal(SyncStatus.InSync, entry.Status);
Assert.Equal(local.SettingsVersion, entry.Local!.SettingsVersion);
}
[Test]
public async Task AnEditSavedHereDuringTheComparisonIsNotReplacedByTheServers()
{
@@ -441,6 +521,8 @@ public sealed class SyncServiceTests(TestContext context)
Assert.Equal(originals.Id, (await f.EntryAsync("Construction cam")).Transfer!.Id);
}
private static string Sha(byte[] bytes) => Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes));
/// <summary>A linked sequence whose settings changed differently on both sides since they last agreed (41 here, 33 on the server).</summary>
private static async Task<(Sequence Local, FakeSequence Remote)> ConflictAsync(SyncFixture f)
{