test(sync): upload, download, resume, proxies and live events against the real server

The first original of an upload now creates the server sequence through its
session key (the archive ingest files API-created empty sequences nowhere), and
settings sent after an upload are retried when the server's own analysis edits
the new sequence meanwhile.

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-02 22:44:20 +00:00
1 parent 7f542646b4
commit c02d1867d0
9 files changed
+420 -49

No files matched your search

+9 -10
View File
@@ -23,16 +23,17 @@ public interface ISyncRemote : IDisposable
/// <summary>Every frame row of a sequence, in index order.</summary>
Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken);
Task<SequenceDto> CreateSequenceAsync(CreateSequenceRequest request, CancellationToken cancellationToken);
/// <summary>A 409 <see cref="TitanoApiException"/> means the settings version moved on the server.</summary>
Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken);
/// <summary>
/// Chunked, resumable upload of one original that the server verifies against <paramref name="sha256"/> before
/// committing it (BRIEF §14.4); see <see cref="TitanoClient.UploadFileAsync"/>.
/// Chunked, resumable upload of one original into <paramref name="sequenceId"/>, or into the sequence the server
/// files <paramref name="sessionKey"/> under (created by the first one); the server verifies <paramref name="sha256"/>
/// before committing it (BRIEF §14.4). An original the server already holds is not sent again: the response then
/// names <paramref name="sequenceId"/> (<see cref="Guid.Empty"/> without one). See <see cref="TitanoClient.UploadFileAsync"/>.
/// </summary>
Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt, CancellationToken cancellationToken);
Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken);
/// <summary>Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.</summary>
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
@@ -88,14 +89,12 @@ public sealed class TitanoSyncRemote(TitanoClient client, bool ownsClient) : ISy
return all;
}
public Task<SequenceDto> CreateSequenceAsync(CreateSequenceRequest request, CancellationToken cancellationToken) =>
client.CreateSequenceAsync(request, cancellationToken);
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) =>
client.UpdateSequenceAsync(id, request, cancellationToken);
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt, CancellationToken cancellationToken) =>
client.UploadFileAsync(path, sequenceId, capturedAt, null, sha256, null, cancellationToken);
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken) =>
client.UploadFileAsync(path, sequenceId, capturedAt, sessionKey, sha256, null, cancellationToken);
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
client.DownloadFrameAsync(frameId, kind, destination, null, cancellationToken);
+20 -5
View File
@@ -260,17 +260,32 @@ public sealed class SyncService
return link;
}
/// <summary>Makes the server's copy equal to this PC's (an upload sends the settings with the originals).</summary>
/// <summary>
/// Makes the server's copy equal to this PC's (an upload sends the settings with the originals of the sequence it
/// created); the server's own processing may edit the new sequence meanwhile, so a refused edit is sent again.
/// </summary>
internal async Task PushAllAsync(ISyncRemote remote, Guid remoteId, CancellationToken cancellationToken)
{
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
var (link, local) = await LinkedAsync(remote.Server, remoteId, cancellationToken).ConfigureAwait(false);
var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
var updated = await remote.UpdateSequenceAsync(remoteId, SyncPayload.Of(local).ToRequest(dto.SettingsVersion), cancellationToken).ConfigureAwait(false);
await _links.UpsertAsync(Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion), cancellationToken)
.ConfigureAwait(false);
for (var attempt = 1; ; attempt++)
{
var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
try
{
var updated = await remote.UpdateSequenceAsync(remoteId, SyncPayload.Of(local).ToRequest(dto.SettingsVersion), cancellationToken)
.ConfigureAwait(false);
await _links.UpsertAsync(Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion), cancellationToken)
.ConfigureAwait(false);
return;
}
catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict && attempt < 3)
{
Log.Info(LogCategory, $"{local.DisplayTitle} changed on the server while its settings were sent; sending again");
}
}
}
finally
{
+44 -19
View File
@@ -38,22 +38,41 @@ internal sealed class SyncTransfers(SyncService sync)
var paths = new ArchivePaths(archive.RootPath);
var (remoteId, created) = await TargetAsync(context, remote, local).ConfigureAwait(false);
var manifest = await remote.GetManifestAsync(remoteId, ct).ConfigureAwait(false);
var present = manifest.Frames.Select(f => f.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
var frames = (await SequenceFrames.ListAsync(db, local.Id, ct).ConfigureAwait(false)).Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
var pending = frames.Where(f => f.Sha256 is null || !present.Contains(f.Sha256.ToLowerInvariant())).ToList();
var present = remoteId is { } known
? (await remote.GetManifestAsync(known, ct).ConfigureAwait(false)).Frames.Select(f => f.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal)
: [];
var pending = new Queue<Frame>(frames.Where(f => f.Sha256 is null || !present.Contains(f.Sha256.ToLowerInvariant())));
var progress = new TransferProgress(context, frames.Count, frames.Count - pending.Count);
await progress.ReportAsync().ConfigureAwait(false);
var skipped = 0;
// Without a server sequence the originals go out one by one under this sequence's session key until the
// server has filed one, which creates the sequence (an original it already holds elsewhere creates nothing).
while (remoteId is null && pending.TryDequeue(out var first))
{
var sent = await UploadFrameAsync(context, remote, null, SyncService.SessionKey(local.Id), paths.FromRelative(first.RelativePath), first, ct)
.ConfigureAwait(false);
if (sent is null) skipped++;
else if (sent.SequenceId != Guid.Empty)
{
remoteId = sent.SequenceId;
await context.CheckpointAsync(sent.SequenceId.ToString(), progress.Done + 1, frames.Count).ConfigureAwait(false);
await sync.LinkAsync(remote.Server, sent.SequenceId, local, ct).ConfigureAwait(false);
}
await progress.StepAsync().ConfigureAwait(false);
}
if (remoteId is not { } target) throw new InvalidOperationException(Loc.L("Errors.Sync.NothingSent"));
await Parallel.ForEachAsync(pending, Workers(sync.Options.Uploads, ct), async (frame, token) =>
{
if (!await UploadFrameAsync(context, remote, remoteId, paths.FromRelative(frame.RelativePath), frame, token).ConfigureAwait(false))
if (await UploadFrameAsync(context, remote, target, null, paths.FromRelative(frame.RelativePath), frame, token).ConfigureAwait(false) is null)
Interlocked.Increment(ref skipped);
await progress.StepAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
if (created) await sync.PushAllAsync(remote, remoteId, ct).ConfigureAwait(false);
await sync.RefreshSequencesAsync(remote, [remoteId], CompareOnly, ct).ConfigureAwait(false);
if (created) await sync.PushAllAsync(remote, target, ct).ConfigureAwait(false);
await sync.RefreshSequencesAsync(remote, [target], CompareOnly, ct).ConfigureAwait(false);
if (skipped > 0) throw new InvalidDataException(Loc.Plural("Errors.Sync.Unreadable", skipped));
}
@@ -139,8 +158,11 @@ internal sealed class SyncTransfers(SyncService sync)
if (cached < frames.Count) context.Log($"{frames.Count - cached} frame(s) have neither proxy nor thumbnail on the server");
}
/// <summary>The server sequence an upload fills and whether this job created it: the linked one, one an interrupted attempt created, or a new one.</summary>
private async Task<(Guid RemoteId, bool Created)> TargetAsync(JobContext context, ISyncRemote remote, Sequence local)
/// <summary>
/// The server sequence an upload fills and whether this job creates it: the linked one, the one an interrupted
/// attempt created, or none yet (the first original creates it).
/// </summary>
private async Task<(Guid? RemoteId, bool Created)> TargetAsync(JobContext context, ISyncRemote remote, Sequence local)
{
var ct = context.Cancellation;
Guid? started = Guid.TryParse(context.Checkpoint, out var saved) ? saved : null;
@@ -153,13 +175,10 @@ internal sealed class SyncTransfers(SyncService sync)
if (link?.RemoteId != id) await sync.LinkAsync(remote.Server, id, local, ct).ConfigureAwait(false);
return (id, id == started);
}
// Deleted on the server meanwhile: upload into a new sequence.
// Deleted on the server meanwhile: the originals go into a new sequence.
if (id == link?.RemoteId) await sync.Links.DeleteAsync(remote.Server, id, ct).ConfigureAwait(false);
}
var dto = await remote.CreateSequenceAsync(new CreateSequenceRequest(local.Title, local.CameraModel), ct).ConfigureAwait(false);
await context.CheckpointAsync(dto.Id.ToString(), 0, local.FrameCount).ConfigureAwait(false);
await sync.LinkAsync(remote.Server, dto.Id, local, ct).ConfigureAwait(false);
return (dto.Id, true);
return (null, true);
}
private static async Task<bool> ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken)
@@ -175,22 +194,26 @@ internal sealed class SyncTransfers(SyncService sync)
}
}
/// <summary>Hashes the original again and sends it; false (logged) when it is gone or no longer the archived file.</summary>
private static async Task<bool> UploadFrameAsync(JobContext context, ISyncRemote remote, Guid remoteId, string path, Frame frame, CancellationToken cancellationToken)
/// <summary>
/// Hashes the original again and sends it into <paramref name="sequenceId"/> (or the sequence of
/// <paramref name="sessionKey"/>); null (logged) when it is gone or no longer the archived file.
/// </summary>
private static async Task<CompleteUploadResponse?> UploadFrameAsync(JobContext context, ISyncRemote remote, Guid? sequenceId, string? sessionKey, string path,
Frame frame, CancellationToken cancellationToken)
{
if (!File.Exists(path))
{
context.Log($"{frame.FileName}: missing, not uploaded");
return false;
return null;
}
var sha = await TitanoClient.HashFileAsync(path, cancellationToken).ConfigureAwait(false);
if (frame.Sha256 is { } archived && !archived.Equals(sha, StringComparison.OrdinalIgnoreCase))
{
context.Log($"{frame.FileName}: changed since it was archived, not uploaded");
return false;
return null;
}
await remote.UploadFileAsync(path, remoteId, sha, frame.CapturedAt, cancellationToken).ConfigureAwait(false);
return true;
return await remote.UploadFileAsync(path, sequenceId, sessionKey, sha, frame.CapturedAt, cancellationToken).ConfigureAwait(false)
?? new CompleteUploadResponse(Guid.Empty, sequenceId ?? Guid.Empty, -1);
}
/// <summary>Downloads one original into staging and commits it into the local sequence (created from the server id when null).</summary>
@@ -325,6 +348,8 @@ internal sealed class SyncTransfers(SyncService sync)
}
}
public long Done => done;
public ValueTask ReportAsync() => context.ReportAsync(done, total);
}
}