fix(copies): close the eighth verification round of owner review 6

- A merge's Renders stage never deletes what sits at a journaled render
  target: a file or folder it did not copy there (a render of the kept
  sequence made after a failed run), or a path another render of the kept
  sequence names, makes the copy take a new free name instead.
- A merge's Verify stage records the hash it read for each kept frame the
  index had none for, the Details stage leaves the versions the verify filed
  out of Main, and Two versions also lists a whole-second instant holding two
  frames of one size and other content (owner Q3).
- The Integrate journal keeps the losers the plan listed as last copies; a
  loser that became the only copy while the job ran is never trashed: the
  journal goes and the job fails as a stale plan.
- Download originals of a server sequence whose earlier download was
  unlinked or kept apart makes a new local sequence under a key of the job,
  instead of filling the old one, re-linking it and overwriting its details.
- An upload never links again from its checkpoint a server sequence the user
  kept apart since the attempt that made it.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 11:38:18 +01:00
1 parent 123ceba845
commit aad8ae46dd
11 files changed
+536 -36

No files matched your search

+32 -6
View File
@@ -538,8 +538,9 @@ internal sealed class SyncTransfers(SyncService sync)
if (local is null && wanted.Count > 0)
{
// The first original creates the local sequence (grouped by the server id); the others join it.
var key = await DownloadKeyAsync(context, remote, remoteId, archive.Id).ConfigureAwait(false);
var (staged, sha) = await FetchOriginalAsync(remote, wanted[0], staging, ct).ConfigureAwait(false);
var first = await CommitAsync(remoteId, archive.Id, null, wanted[0], staged, sha, captured, ct).ConfigureAwait(false);
var first = await CommitAsync(key, archive.Id, null, wanted[0], staged, sha, captured, ct).ConfigureAwait(false);
local = await db.Sequences.GetAsync(first.SequenceId, ct).ConfigureAwait(false)
?? throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
await sync.LinkAsync(remote.Server, remoteId, local, ct).ConfigureAwait(false);
@@ -574,7 +575,7 @@ internal sealed class SyncTransfers(SyncService sync)
}
else
{
await CommitAsync(remoteId, archive.Id, sequenceId, frame, staged, sha, captured, token).ConfigureAwait(false);
await CommitAsync(null, archive.Id, sequenceId, frame, staged, sha, captured, token).ConfigureAwait(false);
}
await progress.StepAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
@@ -942,7 +943,7 @@ internal sealed class SyncTransfers(SyncService sync)
/// <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).
/// attempt created (unless the user kept it apart or unlinked it since), or none yet (the first original creates it).
/// </summary>
private async Task<(Guid? RemoteId, bool Created)> TargetAsync(JobContext context, ISyncRemote remote, Sequence local)
{
@@ -952,6 +953,10 @@ internal sealed class SyncTransfers(SyncService sync)
foreach (var candidate in new[] { link?.RemoteId, started })
{
if (candidate is not { } id) continue;
// The sequence an interrupted attempt created, unlinked or kept apart by the user since: never linked again from
// the checkpoint. The upload makes a new sequence (SessionKeyAsync), as one of a kept-apart sequence does.
if (id != link?.RemoteId && await sync.Copies.FindRemoteAsync(local.Id, remote.Server, id, ct).ConfigureAwait(false) is { State: CopyState.KeptApart })
continue;
if (await ExistsAsync(remote, id, ct).ConfigureAwait(false))
{
if (link?.RemoteId != id) await sync.LinkAsync(remote.Server, id, local, ct).ConfigureAwait(false);
@@ -978,6 +983,24 @@ internal sealed class SyncTransfers(SyncService sync)
return apart ? string.Create(CultureInfo.InvariantCulture, $"{own}:{context.Job.Id:N}") : own;
}
/// <summary>
/// The session key the first original of a download without a local copy goes under: the server sequence's own
/// (<see cref="SyncService.SessionKey"/>), which groups the download in the local sequence an earlier download of it made,
/// unless that sequence has been unlinked or kept apart from it, or linked to another sequence of this server since (it
/// has a pair or a link for this server). The user chose to keep such a sequence separate: the download makes a new
/// local sequence instead, under a key of this job (the same on a retry), and never fills or overwrites the other.
/// </summary>
private async Task<string> DownloadKeyAsync(JobContext context, ISyncRemote remote, Guid remoteId, Guid archiveId)
{
var ct = context.Cancellation;
var own = SyncService.SessionKey(remoteId);
if (await sync.Database.Sequences.FindBySessionKeyAsync(archiveId, own, ct).ConfigureAwait(false) is not { } holder) return own;
var apart = (await sync.Copies.ListByLocalAsync(holder, ct).ConfigureAwait(false))
.Any(c => c.Kind == CopyKind.LocalServer && string.Equals(c.Server, remote.Server, StringComparison.Ordinal) && c.LocalId == holder)
|| await sync.Links.FindByLocalAsync(remote.Server, holder, ct).ConfigureAwait(false) is not null;
return apart ? string.Create(CultureInfo.InvariantCulture, $"{own}:{context.Job.Id:N}") : own;
}
private static async Task<bool> ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken)
{
try
@@ -1060,11 +1083,14 @@ internal sealed class SyncTransfers(SyncService sync)
public bool Refuse() => Interlocked.Exchange(ref _refused, 1) == 0;
}
/// <summary>Commits one original from staging into the local sequence (created from the server id when null).</summary>
private async Task<IngestResult> CommitAsync(Guid remoteId, Guid archiveId, Guid? sequenceId, ManifestFrame frame, string staged, string sha,
/// <summary>
/// Commits one original from staging into the local sequence <paramref name="sequenceId"/>, or (when null) the one of
/// <paramref name="sessionKey"/>, created by the first original (<see cref="DownloadKeyAsync"/>).
/// </summary>
private async Task<IngestResult> CommitAsync(string? sessionKey, Guid archiveId, Guid? sequenceId, ManifestFrame frame, string staged, string sha,
IReadOnlyDictionary<Guid, DateTime?> captured, CancellationToken cancellationToken)
{
var request = new IngestRequest(archiveId, sequenceId, null, sequenceId is null ? SyncService.SessionKey(remoteId) : null, staged, frame.FileName,
var request = new IngestRequest(archiveId, sequenceId, null, sequenceId is null ? sessionKey : null, staged, frame.FileName,
frame.Size, sha, captured.GetValueOrDefault(frame.Id), null);
var result = await sync.Archives.Ingest.CommitAsync(request, cancellationToken).ConfigureAwait(false);
// A duplicate stays in staging (the sequence already holds that content).