fix(copies): a changed photo no longer wedges a merge's resumed verify

A merge's Verify stage journals a second version before filing it. When
the copy then failed or read other bytes (the file rewritten by an
editor or a sync tool, a drive dropping out before an edit), every retry
read new bytes for that journalled frame and stopped with changed,
keeping the journal, so the merge failed the same way until cancelled.

- When the kept sequence does not hold the journalled bytes, nothing was
  filed: the frame leaves SecondVersions and VersionShas and is verified
  as on a first visit, which files the photo as it is now.
- When it holds them (filed, then the exclusion failed), they are left
  out of Main and the merge stops and drops its journal, so the next run
  plans again instead of a cancel leaving them in Main for good.

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 14:30:27 +01:00
1 parent 6db4c2e80c
commit 65b7cdf91b
5 files changed
+115 -3

No files matched your search

@@ -55,6 +55,9 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
/// <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; }
/// <summary>Called before the verify files a second version, once the journal names it (tests: its file rewritten meanwhile).</summary>
internal Func<Guid, Task>? BeforeFileVersion { get; set; }
public async Task RunAsync(JobContext context)
{
var job = context.Job;
@@ -660,9 +663,27 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
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))
if (version && journal.VersionShas.GetValueOrDefault(frame.Id) is var filedAs
&& !string.Equals(filedAs, 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 (filedAs is null || !held.ContainsKey(filedAs))
{
// Nothing was filed (the copy failed or read other bytes): visited as for the first time.
journal.SecondVersions.Remove(frame.Id);
journal.VersionShas.Remove(frame.Id);
Save();
version = false;
}
else
{
// The old bytes were filed: left out of Main now (a new plan reads them as the kept sequence's
// own photo), then the merge stops and is planned again, which compares the new bytes.
await LeaveOutOfMainAsync(filedAs).ConfigureAwait(false);
ForgetJournal();
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
@@ -686,6 +707,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
if (!version) journal.SecondVersions.Add(frame.Id);
journal.VersionShas[frame.Id] = sha.ToLowerInvariant();
Save();
if (owner.BeforeFileVersion is { } fileHook) await fileHook(frame.Id).ConfigureAwait(false);
var filed = await FileAsync(frame, sha, planned: false).ConfigureAwait(false);
await LeaveOutOfMainAsync(sha).ConfigureAwait(false);
journal.Verified[frame.Id] = new MergeVerified(sha, filed.FrameId);
+3 -1
View File
@@ -107,7 +107,9 @@ internal sealed class MergeJournal
/// <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.
/// that finds it in the kept sequence leaves it out of Main again. One that reads other bytes starts over for that frame
/// when the kept sequence does not hold the old ones (nothing was filed), else leaves them out of Main and stops, and the
/// merge is planned again.
/// </summary>
public Dictionary<Guid, string> VersionShas { get; set; } = [];