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

The metered hold now records the jobs it paused in the index and forgets
any job the user pauses, resumes, retries or cancels, so it never resumes
a pause of the user's and still resumes its own after a restart.

Drop... reports why its dry run is blocked instead of asking first, and
dropping the version Main uses lets Main use the version that stays,
with its hero and keyframes. That version reads "In Main".

Merges:
- A re-plan no longer hashes the other's photos again.
- A resumed merge verifies photos flagged damaged while it waited.
- A merge is blocked while the other's folder holds photos the index
  has not read; this is checked again under the lease before the trash.
- Choose each offers Keep both, Kept and Other, and blocks until every
  differing photo is chosen.
- Classes are named by role.

The Compare dialog:
- Its headings stay over their columns.
- The Projects part scrolls.
- Every confirmation line carries its own prefix, and a merge's facts
  carry none.
- A server card in a match offers Compare... alone.

The Italian strings follow the glossary.

Finding 9 (a local drop counting 0 photos) is not reproduced; a test now
keeps the count at 2.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-17 20:51:23 +01:00
1 parent 2c06e7ac2b
commit bf7701d279
159 files changed
+1759 -126

No files matched your search

+73 -4
View File
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
@@ -32,6 +33,21 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
private readonly CopySides _sides = sides ?? throw new ArgumentNullException(nameof(sides));
private readonly DbAccess _access = new(db.Database);
/// <summary>
/// The hashes the dry runs read of the other sequence's files, by frame, with the size and last write time they were read
/// at: a plan made again (every change of choice in the dialog) reads only the files that are new or changed. Kept in
/// memory only, as a dry run writes nothing to the index; the job's Verify stage hashes every file again.
/// </summary>
private readonly ConcurrentDictionary<Guid, (long Length, DateTime Written, string Sha256)> _hashes = new();
/// <summary>The most hashes <see cref="_hashes"/> keeps before it starts again.</summary>
private const int HashesKept = 200_000;
private int _filesHashed;
/// <summary>Files the dry runs read and hashed so far (tests: a plan made again reads only what changed).</summary>
internal int FilesHashed => Volatile.Read(ref _filesHashed);
/// <summary>The name a server key is shown by (default: the key): the links a merge moves and the server both are linked to.</summary>
public Func<string, string?>? ServerNames { get; init; }
@@ -98,6 +114,9 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
var online = ArchiveVolume.Probe(keepPaths) == ArchiveAvailability.Online && ArchiveVolume.Probe(dropPaths) == ArchiveAvailability.Online;
if (!online) blocks.Add(CopyBlock.ArchiveOffline);
else if (Moved(keepPaths, keepRow) || Moved(dropPaths, dropRow)) blocks.Add(CopyBlock.FolderMoved);
// Photos of the other this index has not read (copied in by hand, filed by another index sharing the archive) would
// go to the trash with its folder, never copied nor listed: a scan reads them first.
else if (await FolderChangedAsync(dropArchive, dropPaths, dropRow, ct).ConfigureAwait(false)) blocks.Add(CopyBlock.FolderChanged);
if (await BusyAsync([keep, drop, keepRow.ArchiveId, dropRow.ArchiveId], exceptJob, ct).ConfigureAwait(false)) blocks.Add(CopyBlock.Busy);
if (context.UnsentWorkspaceSequences.Contains(drop)) blocks.Add(CopyBlock.UnsentWorkspace);
@@ -114,6 +133,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
}
var steps = new List<PhotoStep>();
var excluded = new List<string>();
var unchosen = 0;
foreach (var photo in comparison.Photos)
{
switch (photo.Class)
@@ -123,6 +143,9 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
steps.Add(Step(photo.Key, PhotoAction.Fetch, photo.There!));
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).
if (choices.Photos == CopyPhotoPolicy.ChoosePerPhoto && !choices.PerPhoto.ContainsKey(photo.Key)) unchosen++;
var choice = choices.Photos switch
{
CopyPhotoPolicy.Union => PhotoChoice.KeepBoth,
@@ -136,6 +159,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
break;
}
}
if (unchosen > 0) blocks.Add(CopyBlock.PhotosUnchosen);
// Last copies: drop originals nothing will hold once the copies are made (losers, tombstoned in keep, converted), and
// keep's losing versions nothing else holds.
@@ -243,6 +267,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
LinkNames = [.. moved.Select(ServerName)],
BothLinkedServer = bothLinked is null ? null : ServerName(bothLinked),
DeletedInOther = comparison.Photos.Count(p => p.Class == PhotoClass.DeletedThere),
PhotosUnchosen = unchosen,
};
}
@@ -267,8 +292,8 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
}
try
{
var sha = await ContentHash.Sha256Async(dropPaths.FromRelative(frame.RelativePath), null, ct).ConfigureAwait(false);
photos.Add(photo with { Sha256 = sha.ToLowerInvariant() });
var sha = await HashAsync(frame.Id, dropPaths.FromRelative(frame.RelativePath), ct).ConfigureAwait(false);
photos.Add(photo with { Sha256 = sha });
changed = true;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
@@ -297,7 +322,7 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
if (!File.Exists(path)) continue;
try
{
var sha = (await ContentHash.Sha256Async(path, null, ct).ConfigureAwait(false)).ToLowerInvariant();
var sha = await HashAsync(frame.Id, path, ct).ConfigureAwait(false);
if (!keepHolds.Contains(sha)) hashes.Add(sha);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
@@ -310,6 +335,48 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
return hashes.Any(h => !held.Contains(h));
}
/// <summary>
/// The SHA-256 (lower-case) of the other's 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)
{
var file = new FileInfo(path);
if (!file.Exists) throw new FileNotFoundException("The photo is not there.", path);
var (length, written) = (file.Length, file.LastWriteTimeUtc);
if (_hashes.TryGetValue(frameId, out var known) && known.Length == length && known.Written == written) return known.Sha256;
var sha = (await ContentHash.Sha256Async(path, null, ct).ConfigureAwait(false)).ToLowerInvariant();
Interlocked.Increment(ref _filesHashed);
if (_hashes.Count >= HashesKept) _hashes.Clear();
_hashes[frameId] = (length, written, sha);
return sha;
}
/// <summary>
/// Whether the other sequence's folder holds photos this index has not read: photos copied in or removed since the
/// photo directory's recorded stamp (<see cref="ArchiveIndexer.PhotosChangedAsync"/>), or a sidecar whose frames are not
/// the row's (another index of the archive filed or dropped photos). A sidecar that cannot be read, or names another
/// sequence, counts. A sidecar rewritten without new frames does not (the merge's own session-key moves rewrite it).
/// </summary>
internal async Task<bool> FolderChangedAsync(Archive archive, ArchivePaths paths, Sequence row, CancellationToken ct)
{
var folder = paths.Sequence(row.Folder);
if (!File.Exists(folder.ManifestPath)) return false;
SequenceManifest manifest;
try
{
manifest = ManifestStore.ReadSequence(folder.ManifestPath);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or JsonException)
{
return true;
}
if (manifest.Id != row.Id) return true;
var indexed = await _db.Sequences.GetAsync(row.Id, ct).ConfigureAwait(false);
return indexed is null || indexed.FramesVersion != manifest.Versions.Frames
|| await _archives.Indexer.PhotosChangedAsync(archive, folder, ct).ConfigureAwait(false);
}
/// <summary>
/// Queues the job that applies <paramref name="plan"/>: the pair goes to <see cref="CopyState.Integrating"/> (its state
/// before kept as the previous one), the job targets the kept sequence, then the other (both held from other jobs).
@@ -360,7 +427,9 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
return block switch
{
CopyBlock.Busy => Loc.L("Errors.Copies.Busy"),
CopyBlock.ArchiveOffline or CopyBlock.FolderMoved or CopyBlock.ChangedFile or CopyBlock.DamagedPhotos => Loc.L("Errors.Copies." + block, dropTitle),
CopyBlock.ArchiveOffline or CopyBlock.FolderMoved or CopyBlock.ChangedFile or CopyBlock.DamagedPhotos or CopyBlock.FolderChanged
=> Loc.L("Errors.Copies." + block, dropTitle),
CopyBlock.PhotosUnchosen => Loc.L("Errors.Copies.PhotosUnchosen"),
CopyBlock.NoSpace => Loc.L("Errors.Copies.NoSpace", keepTitle, Size(plan.CopyBytes + SpaceReserve)),
CopyBlock.BothLinked => Loc.L("Errors.Copies.BothLinked", plan.BothLinkedServer ?? string.Empty),
CopyBlock.UnsentWorkspace => Loc.L("Errors.Copies.Unsent", dropTitle),