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

+112 -45
View File
@@ -9,6 +9,7 @@ using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Core.Video;
using Titano.Data;
using Titano.Data.Jobs;
using Titano.Data.Sync;
using Titano.Protocol;
using Titano.Protocol.Client;
@@ -31,6 +32,8 @@ public sealed class SampleData
private readonly AppServices _services;
private readonly SampleSyncRemote _remote = new();
private TitanoDatabase? _db;
private Guid _sunrise;
private Guid _lakeComo;
private SampleData(AppServices services) => _services = services;
@@ -87,55 +90,84 @@ public sealed class SampleData
settings.FirstRunCompleted = true;
}
/// <summary>An upload in progress for the sync table (Comet over the ridge, 46 %).</summary>
/// <summary>
/// Transfers for the sync table: an upload at 46 % (Comet over the ridge), an upload waiting (Fog lifting, Asti), a
/// download of originals running (Sunrise over Superga) and one paused (Lake Como storm).
/// </summary>
public async Task FillSyncAsync()
{
var db = _db ?? throw new InvalidOperationException("FillAsync first.");
var comet = (await db.Sequences.ListAsync(new Data.Repositories.SequenceQuery { Text = "Comet", Limit = 1 })).Items[0];
async Task<Sequence> LocalAsync(string text) => (await db.Sequences.ListAsync(new Data.Repositories.SequenceQuery { Text = text, Limit = 1 })).Items[0];
var now = DateTimeOffset.UtcNow;
await db.Jobs.EnqueueAsync(new Job
Job Transfer(JobKind kind, Guid target, string label, string parameters, JobState state, long done, long total) => new()
{
Id = Guid.CreateVersion7(),
Kind = JobKind.UploadCommit,
TargetIds = [comet.Id],
TargetLabel = comet.DisplayTitle,
ParametersJson = $$"""{"server":"{{ServerAddress}}"}""",
State = JobState.Running,
Kind = kind,
TargetIds = [target],
TargetLabel = label,
ParametersJson = parameters,
State = state,
Priority = JobPriority.User,
ProgressDone = 462,
ProgressTotal = 1_005,
ItemsPerSecond = 1.6,
Eta = TimeSpan.FromMinutes(4),
NodeId = _services.WorkerId,
Attempts = 1,
ProgressDone = done,
ProgressTotal = total,
ItemsPerSecond = state == JobState.Running ? 1.6 : null,
Eta = state == JobState.Running ? TimeSpan.FromMinutes(4) : null,
NodeId = state == JobState.Queued ? null : _services.WorkerId,
Attempts = state == JobState.Queued ? 0 : 1,
Created = now.AddMinutes(-5),
Started = now.AddMinutes(-5),
});
Started = state == JobState.Queued ? null : now.AddMinutes(-5),
};
var upload = $$"""{"server":"{{ServerAddress}}"}""";
var originals = $$"""{"server":"{{ServerAddress}}","mode":"Originals"}""";
var comet = await LocalAsync("Comet");
var fog = await LocalAsync("Fog lifting");
await db.Jobs.EnqueueAsync(Transfer(JobKind.UploadCommit, comet.Id, comet.DisplayTitle, upload, JobState.Running, 462, 1_005));
await db.Jobs.EnqueueAsync(Transfer(JobKind.UploadCommit, fog.Id, fog.DisplayTitle, upload, JobState.Queued, 0, 0));
await db.Jobs.EnqueueAsync(Transfer(JobKind.Download, _sunrise, "Sunrise over Superga", originals, JobState.Running, 1_210, 2_880));
await db.Jobs.EnqueueAsync(Transfer(JobKind.Download, _lakeComo, "Lake Como storm", originals, JobState.Paused, 300, 1_450));
}
/// <summary>
/// The server's side: four sequences this PC also holds (two the same, one with settings changed on both sides, one
/// with more originals on the server) and two only on the server; the others are only here.
/// Fetches the proxies of Sunrise over Superga into the proxy cache through a download job on a one-off engine (the
/// capture's engine does not run jobs), so the sync table shows a sequence kept with proxies only.
/// </summary>
public async Task CacheProxiesAsync()
{
var db = _db ?? throw new InvalidOperationException("FillAsync first.");
var service = _services.Sync.Service ?? throw new InvalidOperationException("The index is not open.");
await using var engine = new JobEngine(db, service.CreateJobHandlers(_services.Sync),
new JobEngineOptions { Workers = 1, WorkerId = "sample-proxies", Kinds = [JobKind.Download], PollInterval = TimeSpan.FromMilliseconds(50) });
await engine.StartAsync();
var job = await service.EnqueueDownloadAsync(engine, ServerAddress, _sunrise, "Sunrise over Superga", SyncDownloadMode.Proxies, null);
for (var i = 0; i < 200 && await db.Jobs.GetAsync(job.Id) is { State: JobState.Queued or JobState.Running }; i++) await Task.Delay(50);
}
/// <summary>
/// The server's side: six sequences this PC also holds (two the same, one with settings changed on both sides, one
/// with more originals on the server, one with more here, one with renders this PC lacks) and two only on the server;
/// the others are only here.
/// </summary>
private async Task FillServerAsync(TitanoDatabase db, IReadOnlyList<Sequence> local)
{
Guid Id(string title) => local.First(s => s.Title == title).Id;
await db.Sequences.EditAsync(Id("Holy grail · Monviso"), new Data.Repositories.SequenceEdit { SettingsJson = DeflickerWindow(41) }, null);
var linked = new (string Title, int Frames, string? Settings)[]
var linked = new (string Title, int Frames, string? Settings, int Renders)[]
{
("Harbour sunset", 3, null),
("Aurora, Tromsø", 3, null),
("Holy grail · Monviso", 3, DeflickerWindow(33)),
("Construction cam · week 38", 8, null),
("Harbour sunset", 3, null, 0),
("Aurora, Tromsø", 3, null, 0),
("Holy grail · Monviso", 3, DeflickerWindow(33), 0),
("Construction cam · week 38", 8, null, 0),
("Milan skyline dusk", 1, null, 0),
("Star trails, Colle del Nivolet", 3, null, 3),
};
foreach (var (title, frames, settings) in linked)
foreach (var (title, frames, settings, renders) in linked)
{
// The server's copy carries the same title, tags and rating as this PC's.
var sequence = (await db.Sequences.GetAsync(Id(title)))!;
var remote = _remote.Add(SampleSyncRemote.Dto(sequence), settings, frames);
var remote = _remote.Add(SampleSyncRemote.Dto(sequence) with { RenderedCount = renders }, settings, frames);
await _services.Sync.Service!.LinkAsync(ServerAddress, remote, sequence.Id);
}
_remote.Add(SampleSyncRemote.Dto(new Sequence
_sunrise = _remote.Add(SampleSyncRemote.Dto(new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.Empty,
@@ -147,7 +179,7 @@ public sealed class SampleData
RenderedCount = 1,
UpdatedAt = DateTimeOffset.UtcNow,
}), null, 3);
_remote.Add(SampleSyncRemote.Dto(new Sequence
_lakeComo = _remote.Add(SampleSyncRemote.Dto(new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.Empty,
@@ -395,11 +427,12 @@ public sealed class SampleData
/// <summary>
/// The sample server's sequences for the sync table: listings, manifests (frames hashed from their index, so
/// unhashed sample frames match them by count) and settings edits; transfers are not part of the screenshots.
/// unhashed sample frames match them by count; renders as many as the listing says), settings edits and proxies
/// (generated JPEGs); uploads and originals are not part of the screenshots.
/// </summary>
private sealed class SampleSyncRemote : ISyncRemote
{
private readonly Dictionary<Guid, (SequenceDto Dto, string? Settings, int Frames)> _sequences = [];
private readonly Dictionary<Guid, (SequenceDto Dto, string? Settings, Guid[] Frames)> _sequences = [];
public string Server => ServerAddress;
@@ -409,39 +442,73 @@ public sealed class SampleData
public Guid Add(SequenceDto dto, string? settings, int frames)
{
_sequences[dto.Id] = (dto, settings, frames);
lock (_sequences) _sequences[dto.Id] = (dto, settings, [.. Enumerable.Range(0, frames).Select(_ => Guid.CreateVersion7())]);
return dto.Id;
}
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken) =>
Task.FromResult<IReadOnlyList<SequenceDto>>([.. _sequences.Values.Select(s => s.Dto)]);
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
{
lock (_sequences) return Task.FromResult<IReadOnlyList<SequenceDto>>([.. _sequences.Values.Select(s => s.Dto)]);
}
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken) => Task.FromResult(_sequences[id].Dto);
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
{
lock (_sequences) return Task.FromResult(_sequences[id].Dto);
}
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken)
{
var (dto, settings, frames) = _sequences[id];
var hasProxy = (dto.Status & (long)SequenceStatus.ProxiesBuilt) != 0;
return Task.FromResult(new SequenceManifest(id, dto.FramesVersion, dto.SettingsVersion, settings, dto.Tags,
[.. Enumerable.Range(0, frames).Select(i => new ManifestFrame(Guid.CreateVersion7(), i, $"IMG_{i + 1:0000}.JPG", 24_000_000,
Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(BitConverter.GetBytes(i))), hasProxy, true))], []));
lock (_sequences)
{
var (dto, settings, frames) = _sequences[id];
var hasProxy = (dto.Status & (long)SequenceStatus.ProxiesBuilt) != 0;
var created = new DateTimeOffset(2026, 9, 20, 21, 0, 0, TimeSpan.Zero);
return Task.FromResult(new SequenceManifest(id, dto.FramesVersion, dto.SettingsVersion, settings, dto.Tags,
[.. frames.Select((frame, i) => new ManifestFrame(frame, i, $"IMG_{i + 1:0000}.JPG", 24_000_000,
Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(BitConverter.GetBytes(i))), hasProxy, true))],
[.. Enumerable.Range(0, dto.RenderedCount).Select(i => new RenderDto(Guid.CreateVersion7(), i == 0 ? "4K HEVC" : "1080p H.264", "hevc",
3840, 2160, 30, 21.3, 640_000_000, created.AddHours(i), "Studio NAS"))]));
}
}
public Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<FrameDto>>([]);
public Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
lock (_sequences)
{
return Task.FromResult<IReadOnlyList<FrameDto>>([.. _sequences[sequenceId].Frames.Select((frame, i) => new FrameDto(frame, sequenceId, i,
$"IMG_{i + 1:0000}.JPG", 24_000_000, null, new DateTime(2026, 7, 2, 5, 40, 0).AddSeconds(30 * i), null, 1.0 / 250, 8, 100, 24, 1, 320, 180,
"jpeg", 0))]);
}
}
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
{
var (dto, _, frames) = _sequences[id];
var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow };
_sequences[id] = (updated, request.SettingsJson, frames);
return Task.FromResult(updated);
lock (_sequences)
{
var (dto, _, frames) = _sequences[id];
var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow };
_sequences[id] = (updated, request.SettingsJson, frames);
return Task.FromResult(updated);
}
}
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken) => throw new NotSupportedException("The sample server takes no uploads.");
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
throw new NotSupportedException("The sample server serves no files.");
/// <summary>Proxies and thumbnails: a dawn gradient whose sun rises with the frame.</summary>
public async Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken)
{
if (kind == "original") throw new NotSupportedException("The sample server serves no originals.");
int index;
lock (_sequences) index = _sequences.Values.Select(s => Array.IndexOf(s.Frames, frameId)).Max();
await destination.WriteAsync(Gradient(["#1B2A4A", "#E8925A", "#F6D28B"], 320, 180, index / 3.0), cancellationToken);
}
public Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
throw new NotSupportedException("The sample server serves no renders.");
public Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
throw new NotSupportedException("The sample server takes no renders.");
public async IAsyncEnumerable<ServerEvent> EventsAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{