fix(sync): resolve conflicts against the versions shown, isolate failures, renders on request

- Conflicts are settled against the versions the user was shown; a later change on
  either side refuses the choice and compares again. Title, tags, rating and notes
  changed differently on both sides are a conflict too (no silent tag union).
- Links are written under the engine's gate and forgotten only after a 404; linked
  local copies are read by id; unlinked copies with the same originals are linked
  by content.
- A failure on one sequence is recorded on its link; the others are still compared.
- Downloads hash unverified local copies first and restore originals missing here.
- Background proxy downloads give way to the user's requests; "Keep proxies offline"
  refreshes the caches of sequences opened with proxies, checks free space and backs
  off after a failure.
- Renders travel on request: Send exports (chunked, hash-verified upload keeping the
  render id; new POST /sequences/{id}/renders endpoints) and Get exports.
- Server screen: stable row buttons, mockup column grid, Tab through rows, conflict
  rows first on the card colour, localised conflict detail, Save both confirmation,
  Pause metered uploads, culture switch, Library one-word sync glyphs.

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-03 10:26:56 +00:00
1 parent 3601e0a47f
commit 2d40a613b9
48 files changed
+2498 -465

No files matched your search

+217 -25
View File
@@ -109,13 +109,13 @@ public sealed class SyncServiceTests(TestContext context)
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
Assert.Null(remote.SettingsJson);
await f.Sync.SyncSettingsAsync(f.Remote, remote.Id, f.Ct);
await f.Sync.SyncSettingsAsync(f.Remote, remote.Id, new SyncPolicy(AutoSyncSettings: false), f.Ct);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task OnlyMetadataChangedOnBothSidesIsMergedWithoutAConflict()
public async Task TagsChangedDifferentlyOnBothSidesAreAConflictToo()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(1);
@@ -124,17 +124,35 @@ public sealed class SyncServiceTests(TestContext context)
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
// The server is edited first, so this PC's later title and rating win; tags are united.
await f.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["boats"], 2, null, null, null), f.Ct);
await Task.Delay(20, f.Ct);
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["sunset"], 4, null, f.Ct);
await f.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["boats"], null, null, null, null), f.Ct);
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["sunset"], 0, null, f.Ct);
await f.RefreshAsync();
// No side wins silently: a tag removed on one side must not come back, and an edit must not be lost.
var entry = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.SequenceEqual(["boats"], entry.Link!.ConflictRemote!.Tags);
Assert.Equal("sequence.tags", SyncRules.Differences(SyncPayload.Of(entry.Local!), entry.Link.ConflictRemote)[0].Path);
Assert.SequenceEqual(["boats"], remote.Tags);
Assert.SequenceEqual(["sunset"], (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Tags);
}
[Test]
public async Task TheSameEditOnBothSidesIsAgreedWithoutAConflict()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(1);
var local = await f.LocalAsync("Harbour", photos);
var remote = f.Remote.Add("Harbour", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["sunset"], null, null, SyncFixture.Window41, null), f.Ct);
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["SUNSET"], 0, null, f.Ct);
await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
await f.RefreshAsync();
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
var merged = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.True(merged.Tags.ToHashSet().SetEquals(["sunset", "boats"]), string.Join(",", merged.Tags));
Assert.True(remote.Tags.ToHashSet().SetEquals(["sunset", "boats"]), string.Join(",", remote.Tags));
Assert.Equal(4, remote.Rating);
}
[Test]
@@ -146,8 +164,8 @@ public sealed class SyncServiceTests(TestContext context)
var entry = await f.EntryAsync("Holy grail");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.Equal(SyncState.Conflict, entry.Local!.SyncState);
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, entry.Link!.ConflictSettingsJson), "the server's side is kept for display");
Assert.Equal("exposure.deflickerWindow.manual", SyncRules.Differences(entry.Local.SettingsJson, entry.Link.ConflictSettingsJson)[0].Path);
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, entry.Link!.ConflictRemote!.SettingsJson), "the server's side is kept for display");
Assert.Equal("exposure.deflickerWindow.manual", SyncRules.Differences(entry.Local.SettingsJson, entry.Link.ConflictRemote.SettingsJson)[0].Path);
// Neither side is overwritten while the conflict stands, and the comparison does not fetch it again.
var manifests = f.Remote.ManifestCalls;
@@ -184,7 +202,7 @@ public sealed class SyncServiceTests(TestContext context)
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson));
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, await File.ReadAllTextAsync(f.Archive.Paths.Sequence(taken.Folder).ProjectPath, f.Ct)));
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
Assert.Empty(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.local-*.json"));
Assert.Empty(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.this-pc-*.json"));
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
}
@@ -194,25 +212,74 @@ public sealed class SyncServiceTests(TestContext context)
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct);
var copy = await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct);
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson));
var copy = Assert.Contains(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.local-*.json"), _ => true);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(copy, f.Ct)), "this PC's version is kept");
Assert.Equal(copy, Assert.Contains(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.this-pc-*.json"), _ => true));
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(copy!, f.Ct)), "this PC's version is kept");
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
}
[Test]
public async Task KeepMineRefusesWhenTheServerChangedAgainMeanwhile()
public async Task KeepMineRefusesWhenTheServerChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (_, remote) = await ConflictAsync(f);
var racing = new RacingRemote(f.Remote, () => f.Remote.EditSettings(remote.Id, SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal)));
var later = SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal);
f.Remote.EditSettings(remote.Id, later);
var error = await Assert.ThrowsAsync<TitanoApiException>(() => f.Sync.ResolveAsync(racing, remote.Id, SyncResolution.KeepMine, f.Ct));
Assert.Equal(System.Net.HttpStatusCode.Conflict, error.Status);
Assert.True(SyncRules.SameSettings(SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal), remote.SettingsJson), "nothing overwritten");
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.KeepMine, f.Ct));
Assert.True(SyncRules.SameSettings(later, remote.SettingsJson), "the edit the user never saw is not overwritten");
var entry = await f.EntryAsync("Holy grail");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.True(SyncRules.SameSettings(later, entry.Link!.ConflictRemote!.SettingsJson), "the conflict now shows the server's latest edit");
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.KeepMine, f.Ct);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
}
[Test]
public async Task KeepMineRefusesWhenTheServerChangesWhileItIsSent()
{
await using var f = await SyncFixture.CreateAsync(context);
var (_, remote) = await ConflictAsync(f);
var later = SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal);
var racing = new RacingRemote(f.Remote, () => f.Remote.EditSettings(remote.Id, later), beforeUpdate: true);
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(racing, remote.Id, SyncResolution.KeepMine, f.Ct));
Assert.True(SyncRules.SameSettings(later, remote.SettingsJson), "nothing overwritten");
}
[Test]
public async Task TakeServerRefusesWhenThisPcChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
var later = SyncFixture.Window41.Replace("41", "45", StringComparison.Ordinal);
await f.EditLocalSettingsAsync(local.Id, later);
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct));
Assert.True(SyncRules.SameSettings(later, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson), "this PC's later edit is kept");
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
var entry = await f.EntryAsync("Holy grail");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.Equal(entry.Local!.SettingsVersion, entry.Link!.ConflictLocalSettings);
}
[Test]
public async Task TakeServerRefusesWhenTheServerChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
f.Remote.EditSettings(remote.Id, SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal));
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct));
var kept = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, kept.SettingsJson), "nothing taken that the user did not see");
Assert.Empty(Directory.GetFiles(f.Archive.Paths.Sequence(kept.Folder).MetaDirectory, "project.this-pc-*.json"));
}
[Test]
@@ -234,6 +301,121 @@ public sealed class SyncServiceTests(TestContext context)
Assert.Equal(SyncState.LocalOnly, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SyncState);
}
[Test]
public async Task ALinkIsForgottenOnlyWhenTheServerSaysTheSequenceIsGone()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Harbour", photos);
var remote = f.Remote.Add("Harbour", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
// A listing whose pages moved while they were read misses the sequence: it is asked for by id instead.
f.Remote.Unlisted.Add(remote.Id);
await f.RefreshAsync();
var kept = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.InSync, kept.Status);
Assert.Equal(remote.Id, kept.Link!.RemoteId);
f.Remote.Sequences.Remove(remote.Id);
await f.RefreshAsync();
Assert.Equal(SyncStatus.LocalOnly, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task AServerRefusalOnOneSequenceIsRecordedAndTheOthersAreStillCompared()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var harbour = await f.LocalAsync("Harbour", photos[..2]);
var aurora = await f.LocalAsync("Aurora", photos[2..]);
var refused = f.Remote.Add("Harbour", photos[..2]);
var fine = f.Remote.Add("Aurora", photos[2..]);
await f.Sync.LinkAsync(f.Server, refused.Id, harbour.Id, f.Ct);
await f.Sync.LinkAsync(f.Server, fine.Id, aurora.Id, f.Ct);
f.Remote.Refused.Add(refused.Id);
await f.RefreshAsync();
Assert.NotNull((await f.EntryAsync("Harbour")).Link!.Error);
var compared = await f.EntryAsync("Aurora");
Assert.Equal(SyncStatus.InSync, compared.Status);
Assert.Null(compared.Link!.Error);
f.Remote.Refused.Clear();
await f.RefreshAsync();
var recovered = await f.EntryAsync("Harbour");
Assert.Null(recovered.Link!.Error);
Assert.Equal(SyncStatus.InSync, recovered.Status);
}
[Test]
public async Task WithoutTheEditRightThisPcsEditsWaitInsteadOfFailing()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(1);
var local = await f.LocalAsync("Harbour", photos);
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, SyncFixture.Window41);
await f.RefreshAsync(new SyncPolicy(CanEdit: false));
Assert.Null(remote.SettingsJson);
var entry = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.LocalAhead, entry.Status);
Assert.Null(entry.Link!.Error);
}
[Test]
public async Task CopiesHoldingTheSameOriginalsAreLinkedByContent()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
// The Uploader sent the same card to the server; nothing links the two yet.
var remote = f.Remote.Add("2026-09-28 Canon", photos);
remote.FirstCapture = local.FirstCapture;
remote.CameraModel = local.CameraModel;
var other = f.Remote.Add("Other card", f.NewPhotos(2));
other.FirstCapture = local.FirstCapture;
other.CameraModel = local.CameraModel;
await f.RefreshAsync();
var entry = await f.EntryAsync("Comet");
Assert.Equal(remote.Id, entry.Link?.RemoteId);
Assert.Equal((0, 0), (entry.Link!.MissingRemote, entry.Link.MissingLocal));
Assert.Null((await f.EntryAsync("Other card")).Local);
var manifests = f.Remote.ManifestCalls;
await f.RefreshAsync();
Assert.Equal(manifests, f.Remote.ManifestCalls);
}
[Test]
public async Task ABackgroundProxyDownloadGivesWayToTheUsersRequest()
{
await using var f = await SyncFixture.CreateAsync(context);
var remote = f.Remote.Add("Construction cam", f.NewPhotos(1));
await f.RefreshAsync();
await using var engine = new Titano.Data.Jobs.JobEngine(f.Db, f.Sync.CreateJobHandlers(new SyncFixture.Connector(f.Remote)));
var background = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null, JobPriority.Background);
Assert.Equal(background.Id, (await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null,
JobPriority.Background)).Id);
var opened = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null);
Assert.NotEqual(background.Id, opened.Id);
Assert.Equal(JobPriority.User, opened.Priority);
Assert.Equal(JobState.Cancelled, (await f.Db.Jobs.GetAsync(background.Id, f.Ct))!.State);
var originals = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Originals, f.Archive.Archive.Id);
Assert.NotEqual(opened.Id, originals.Id);
Assert.Equal(SyncTransferKind.DownloadOriginals, SyncService.TransferKind(originals));
Assert.Equal(originals.Id, (await f.EntryAsync("Construction cam")).Transfer!.Id);
}
/// <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)
{
@@ -250,8 +432,8 @@ public sealed class SyncServiceTests(TestContext context)
return (local, remote);
}
/// <summary>A remote where someone else edits the settings right after the first read of a sequence.</summary>
private sealed class RacingRemote(FakeSyncRemote inner, Action race) : ISyncRemote
/// <summary>A remote where someone else edits the settings right after the first read of a sequence, or right before the first edit.</summary>
private sealed class RacingRemote(FakeSyncRemote inner, Action race, bool beforeUpdate = false) : ISyncRemote
{
private Action? _race = race;
@@ -260,14 +442,18 @@ public sealed class SyncServiceTests(TestContext context)
public async Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
{
var dto = await inner.GetSequenceAsync(id, cancellationToken);
Interlocked.Exchange(ref _race, null)?.Invoke();
if (!beforeUpdate) Interlocked.Exchange(ref _race, null)?.Invoke();
return dto;
}
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken) => inner.ListSequencesAsync(cancellationToken);
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken) => inner.GetManifestAsync(id, cancellationToken);
public Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => inner.ListFramesAsync(sequenceId, cancellationToken);
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) => inner.UpdateSequenceAsync(id, request, cancellationToken);
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
{
if (beforeUpdate) Interlocked.Exchange(ref _race, null)?.Invoke();
return inner.UpdateSequenceAsync(id, request, cancellationToken);
}
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken) =>
@@ -276,6 +462,12 @@ public sealed class SyncServiceTests(TestContext context)
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
inner.DownloadFrameAsync(frameId, kind, destination, cancellationToken);
public Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
inner.DownloadRenderAsync(renderId, destination, cancellationToken);
public Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
inner.UploadRenderAsync(sequenceId, path, request, cancellationToken);
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => inner.EventsAsync(cancellationToken);
public void Dispose()