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

- A merge plan hashes the kept sequence's unhashed frames that match a hashed
  photo of the other by size and time: the same content is no longer a false
  last copy, and another content is copied in as a second version instead of
  going to the trash with the other sequence.
- The Replace stage trashes a losing version of the kept sequence only while
  another sequence still holds it or the plan listed it as a last copy;
  otherwise the journal goes and the job fails as a stale plan.
- A details conflict at the merge's or the Integrate's Details stage drops
  the journal, so a retry plans again instead of failing the same way.
- The Integrate's Projects stage keeps a project for good only for a
  permanent refusal; a server out of reach fails the job with the journal
  kept, and the retry finishes the same copy.
- A pair read as deleted on the server is cleared when the server lists it
  again or a link to that sequence is made.
- An upload of a sequence kept apart from a server sequence goes under a
  session key of the job, making a new server sequence; a link made over a
  kept-apart pair moves it to Linked.

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 10:45:11 +01:00
1 parent c3273b7da5
commit 123ceba845
13 files changed
+418 -13

No files matched your search

+40 -1
View File
@@ -131,6 +131,10 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
there = hashed;
comparison = CopyComparer.Compare(here, there);
}
// The kept sequence's unhashed frames matched by size and time with a hashed photo of the other, hashed now (in
// memory): the same content is held (never a last copy); another content is a second photo the merge copies in.
var freshHere = online ? await HashUnhashedHereAsync(comparison, keepPaths, ct).ConfigureAwait(false) : new Dictionary<Guid, string>();
var keepTombstones = here.Removed.Select(r => r.Sha256).ToHashSet(StringComparer.OrdinalIgnoreCase);
var steps = new List<PhotoStep>();
var excluded = new List<string>();
var unchosen = 0;
@@ -142,6 +146,15 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
// Every photo only the other holds is copied: it goes to the trash, nothing it alone holds stays behind.
steps.Add(Step(photo.Key, PhotoAction.Fetch, photo.There!));
break;
case PhotoClass.SameUnhashed when photo is { Here: { } mine, There.Sha256: { Length: > 0 } theirs }
&& freshHere.TryGetValue(mine.FrameId, out var fresh)
&& !string.Equals(fresh, theirs, StringComparison.OrdinalIgnoreCase)
&& !keepTombstones.Contains(theirs):
// Another photo after all (design §5d step 4): the other's version is copied into the kept one as a
// second version, whatever the policy (nothing chose between the two), and left out of its projects.
steps.Add(Step(photo.Key, PhotoAction.FetchVersion, photo.There!));
excluded.Add(theirs.ToLowerInvariant());
break;
case PhotoClass.Differs:
// Under Choose each a photo nobody chose blocks: its other version would go to the trash unasked (Leave,
// which a merge does not offer, keeps the kept sequence's version as Kept does).
@@ -168,6 +181,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
var losers = steps.Where(s => s.Action == PhotoAction.TrashHere).Select(s => s.FrameId!.Value).ToHashSet();
var keepHolds = here.Photos.Where(p => !p.Unusable && p.Sha256 is { Length: > 0 } && !losers.Contains(p.FrameId))
.Select(p => p.Sha256!.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
keepHolds.UnionWith(freshHere.Values);
// Photos of the other a verify flagged Corrupt (changed since import, or damaged) or Missing whose file is there: the
// comparison leaves them out, so nothing would copy or hash-verify them before their folder goes to the trash. One
// whose content now nothing else holds (an edited photo's only copy) blocks the merge until it is resolved.
@@ -305,6 +319,31 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
return changed ? there with { Photos = photos } : null;
}
/// <summary>
/// The SHA-256 of each unhashed frame of the kept sequence that the comparison matched by size and time only
/// (<see cref="PhotoClass.SameUnhashed"/>) with a hashed photo of the other, read now (in memory), by frame. Without it a
/// photo of the other read as held by the kept sequence would be listed as a last copy when the content is the same,
/// and go to the trash with the other when it is not. A file that cannot be read is left out.
/// </summary>
private async Task<Dictionary<Guid, string>> HashUnhashedHereAsync(CopyComparisonResult comparison, ArchivePaths keepPaths, CancellationToken ct)
{
var result = new Dictionary<Guid, string>();
foreach (var photo in comparison.Photos)
{
if (photo is not { Class: PhotoClass.SameUnhashed, Here: { Sha256: null or { Length: 0 } } mine, There.Sha256: { Length: > 0 } }) continue;
if (await _db.Frames.GetAsync(mine.FrameId, ct).ConfigureAwait(false) is not { } frame) continue;
try
{
result[frame.Id] = await HashAsync(frame.Id, keepPaths.FromRelative(frame.RelativePath), ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// Unreadable now: compared as before (the other's photo is listed as a last copy).
}
}
return result;
}
/// <summary>
/// Whether the other sequence holds a frame flagged unusable (Corrupt, or Missing by an earlier verify although its file
/// is back) whose file is there and whose content, hashed now (in memory), neither the kept sequence
@@ -336,7 +375,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
}
/// <summary>
/// The SHA-256 (lower-case) of the other's file <paramref name="path"/> of frame <paramref name="frameId"/>: the one read
/// The SHA-256 (lower-case) of the file <paramref name="path"/> of frame <paramref name="frameId"/>: the one read
/// before while the file keeps its size and last write time (<see cref="_hashes"/>), else read now.
/// </summary>
private async Task<string> HashAsync(Guid frameId, string path, CancellationToken ct)