fix(copies): close the eleventh verification round of owner review 6
- A merge's Verify stage journals a second version, with the hash it is filed as, before it files it. A run that stops between filing it and leaving it out of Main (a drive dropping out, project.json held, the app closing) now leaves it out on resume, instead of reading it as the kept sequence's own photo and keeping both versions in Main for good. - Upload all local ignores a Kept apart pair whose server sequence is confirmed deleted only when the answer came before the first 404. A Keep here, answered after the deletion was shown, skips the sequence again instead of uploading it and erasing the answer. - An upload's checkpoint records whether the job joined a server sequence this PC already knew. A resumed or retried upload no longer takes such a sequence for one it made and overwrites its details. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e002c583d6
commit
6db4c2e80c
10 files changed
+168
-10
No files matched your search
@@ -52,6 +52,9 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
/// <summary>Called before each stage runs (tests: a crash, a change made between stages).</summary>
|
||||
internal Func<MergeStage, Task>? BeforeStage { get; set; }
|
||||
|
||||
/// <summary>Called before a second version is left out of Main (tests: a failure between filing it and saving the journal).</summary>
|
||||
internal Func<string, Task>? BeforeLeaveOutOfMain { get; set; }
|
||||
|
||||
public async Task RunAsync(JobContext context)
|
||||
{
|
||||
var job = context.Job;
|
||||
@@ -395,6 +398,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
private async Task LeaveOutOfMainAsync(string sha)
|
||||
{
|
||||
var ct = CancellationToken.None;
|
||||
if (owner.BeforeLeaveOutOfMain is { } hook) await hook(sha).ConfigureAwait(false);
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
var keep = await _db.Sequences.GetAsync(journal.KeepId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
@@ -655,9 +659,17 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Copies.ChangedFile", frame.FileName));
|
||||
if (frame.Sha256 is { Length: > 0 } recorded && !string.Equals(recorded, sha, StringComparison.OrdinalIgnoreCase))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Copies.ChangedFile", frame.FileName));
|
||||
var version = journal.SecondVersions.Contains(frame.Id);
|
||||
if (version && !string.Equals(journal.VersionShas.GetValueOrDefault(frame.Id), sha, StringComparison.OrdinalIgnoreCase))
|
||||
// A run that stopped while filing it as a second version read other bytes: changed since.
|
||||
throw new InvalidOperationException(Loc.L("Errors.Copies.ChangedFile", frame.FileName));
|
||||
if (held.TryGetValue(sha, out var keepFrame))
|
||||
{
|
||||
// Filed as a second version by a run that stopped before the journal said so: left out of Main again
|
||||
// (nothing changes when it already is), and still left out at the Details stage.
|
||||
if (version) await LeaveOutOfMainAsync(sha).ConfigureAwait(false);
|
||||
journal.Verified[frame.Id] = new MergeVerified(sha, keepFrame);
|
||||
if (version) Save();
|
||||
continue;
|
||||
}
|
||||
if (journal.Absent.Contains(sha))
|
||||
@@ -669,9 +681,13 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
if (frame.Sha256 is not { Length: > 0 } && !keepRemoved.Contains(sha))
|
||||
{
|
||||
// Compared by size and time only, and another photo after all: filed in the kept sequence as a second version.
|
||||
// The journal says so before it is filed: a run that stops before it is left out of Main (the exclusion
|
||||
// fails, the app ends) finds it in the kept sequence on resume and leaves it out then.
|
||||
if (!version) journal.SecondVersions.Add(frame.Id);
|
||||
journal.VersionShas[frame.Id] = sha.ToLowerInvariant();
|
||||
Save();
|
||||
var filed = await FileAsync(frame, sha, planned: false).ConfigureAwait(false);
|
||||
await LeaveOutOfMainAsync(sha).ConfigureAwait(false);
|
||||
journal.SecondVersions.Add(frame.Id);
|
||||
journal.Verified[frame.Id] = new MergeVerified(sha, filed.FrameId);
|
||||
held.TryAdd(sha, filed.FrameId);
|
||||
Save();
|
||||
|
||||
@@ -105,6 +105,12 @@ internal sealed class MergeJournal
|
||||
/// <summary>Drop frames the verify found to be another photo than keep's (unhashed, same size and time) and filed as a second version.</summary>
|
||||
public List<Guid> SecondVersions { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Drop frame of <see cref="SecondVersions"/> → the SHA-256 it is filed as, written before it is filed: a resumed verify
|
||||
/// that finds it in the kept sequence leaves it out of Main again, and one that reads other bytes stops.
|
||||
/// </summary>
|
||||
public Dictionary<Guid, string> VersionShas { get; set; } = [];
|
||||
|
||||
/// <summary>Drop frames the verify found nothing of to lose: last copies the plan listed, or photos a third sequence holds.</summary>
|
||||
public List<Guid> Settled { get; set; } = [];
|
||||
|
||||
|
||||
@@ -55,10 +55,10 @@ public static class UploadAllRule
|
||||
foreach (var entry in localOnly)
|
||||
{
|
||||
if (entry.Local is not { } local) continue;
|
||||
// A Kept apart answer about a server sequence confirmed deleted there (two 404s apart) holds nothing back: that
|
||||
// sequence is no copy of anything any more, and the local one is this PC's only.
|
||||
var pairs = (bySequence.GetValueOrDefault(local.Id) ?? []).Where(p => p is not { Kind: CopyKind.LocalServer, State: CopyState.KeptApart, RemoteGone: true })
|
||||
.ToList();
|
||||
// A Kept apart answer given before a server sequence was confirmed deleted there (two 404s apart) holds nothing
|
||||
// back: that sequence is no copy of anything any more, and the local one is this PC's only. A Keep here, answered
|
||||
// once the deletion was shown, is the user's answer about the deletion itself: it still skips.
|
||||
var pairs = (bySequence.GetValueOrDefault(local.Id) ?? []).Where(p => !ApartBeforeDeletion(p)).ToList();
|
||||
UploadSkip? why =
|
||||
busy.Contains(local.Id) || pairs.Any(p => p.State == CopyState.Integrating) ? UploadSkip.Busy
|
||||
: pairs.Any(p => p.Kind == CopyKind.LocalServer && p.RemoteGoneHits >= 1) ? UploadSkip.DeletedOnServer
|
||||
@@ -71,6 +71,10 @@ public static class UploadAllRule
|
||||
}
|
||||
return (upload, skipped);
|
||||
|
||||
static bool ApartBeforeDeletion(FoundCopy p) =>
|
||||
p is { Kind: CopyKind.LocalServer, State: CopyState.KeptApart, RemoteGone: true, AnsweredAt: { } answered, RemoteGoneAt: { } gone }
|
||||
&& answered < gone;
|
||||
|
||||
void Add(Guid id, FoundCopy copy)
|
||||
{
|
||||
if (!bySequence.TryGetValue(id, out var list)) bySequence[id] = list = [];
|
||||
|
||||
@@ -37,6 +37,9 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
{
|
||||
private const long SpaceReserve = 512L << 20;
|
||||
|
||||
/// <summary>The upload checkpoint's suffix for a server sequence the job joined (one this PC already knew), not made.</summary>
|
||||
private const string JoinedMark = ":joined";
|
||||
|
||||
/// <summary>Space a cached proxy is assumed to take before it is downloaded (1920 px JPEG, BRIEF §8.5).</summary>
|
||||
private const long ProxyEstimate = 3L << 19;
|
||||
|
||||
@@ -94,7 +97,9 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
{
|
||||
remoteId = sent.SequenceId;
|
||||
if (knownRemotes.Contains(sent.SequenceId)) created = false;
|
||||
await context.CheckpointAsync(sent.SequenceId.ToString(), progress.Done + 1, frames.Count).ConfigureAwait(false);
|
||||
// Saved with the target: a resumed or retried attempt reads whether this job made it, never infers it.
|
||||
await context.CheckpointAsync(sent.SequenceId.ToString() + (created ? string.Empty : JoinedMark), progress.Done + 1, frames.Count)
|
||||
.ConfigureAwait(false);
|
||||
await sync.LinkAsync(remote.Server, sent.SequenceId, local, ct).ConfigureAwait(false);
|
||||
}
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
@@ -962,11 +967,15 @@ 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 (unless the user kept it apart or unlinked it since), or none yet (the first original creates it).
|
||||
/// The checkpoint names the server sequence an earlier attempt reached, with <see cref="JoinedMark"/> when that attempt
|
||||
/// joined one this PC already knew (its details are compared, never sent all over the server's, on every attempt).
|
||||
/// </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;
|
||||
var checkpoint = context.Checkpoint ?? string.Empty;
|
||||
var joined = checkpoint.EndsWith(JoinedMark, StringComparison.Ordinal);
|
||||
Guid? started = Guid.TryParse(joined ? checkpoint[..^JoinedMark.Length] : checkpoint, out var saved) ? saved : null;
|
||||
var link = await sync.Links.FindByLocalAsync(remote.Server, local.Id, ct).ConfigureAwait(false);
|
||||
foreach (var candidate in new[] { link?.RemoteId, started })
|
||||
{
|
||||
@@ -978,7 +987,7 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
if (await ExistsAsync(remote, id, ct).ConfigureAwait(false))
|
||||
{
|
||||
if (link?.RemoteId != id) await sync.LinkAsync(remote.Server, id, local, ct).ConfigureAwait(false);
|
||||
return (id, id == started);
|
||||
return (id, id == started && !joined);
|
||||
}
|
||||
// Deleted on the server meanwhile: the originals go into a new sequence.
|
||||
if (id == link?.RemoteId) await sync.ForgetGoneAsync(remote.Server, id, ct).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user