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

The fourth verification round of the matches work found 18 defects in
the merge, Integrate and Matches surfaces. This commit fixes them:

- An Integrate job now carries the planning account's id, so its re-plan
  decides ownership the way the dialog did.
- A merge skips only a project the kept sequence holds a real copy of
  (name, offset head and history content) and re-points its renders to
  that copy.
- A merge runs its trash checks before the Links stage and records an
  undo, so an abandoned merge gives the other sequence back its link,
  session keys and pairs.
- The metered hold adopts automatic transfers that were paused before a
  restart.
- Apply in the Compare dialog re-plans when the plan is pending or stale.
- The Library's Two versions... lists the second versions a Union merge
  leaves, each with Drop... (dry run, confirmation, verified partner).
  A merge job's Open shows that list.
- The merge's typed confirmation is the invariant count, and the prompt
  names it in all 38 languages.
- Italian text uses Principale and Gemello. The Main project is named
  through WorkspaceText in the Projects part and in the twin tooltip.
- The session keys tooltip is split into two keys, and To trash... and
  Delete on server... say why they are disabled.
- The capture restores the editor account before the Matches scenes.
- Upload again follows SyncHost.CanSendSync (Upload with Edit).
- Matches rows trim archive names in the middle on a rebalanced grid;
  the Projects part rows trim, with the full text in the tooltip.
- The sample project targets use ProjectTransplant.FreeName.

DECISIONS, CHANGELOG, STATUS 8.7, the request doc and the 16 changed
capture baselines are updated.

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 19:20:30 +01:00
1 parent 3c99badcfe
commit 2c06e7ac2b
171 files changed
+1591 -218

No files matched your search

@@ -20,8 +20,10 @@ namespace Chronos.Data.Archiving.Jobs;
/// tombstones honoured), move the renders, verify every original of the other is held by the kept one (hashed again on
/// both sides) or listed as a last copy, trash the kept sequence's losing versions, write the details, copy the projects,
/// move session keys, links and pairs, then move the other to its archive's trash, only while it is still at the frames
/// version the verify read and the kept photos verified are still there. A cancel leaves the photos copied so far in the
/// kept sequence (nothing of the other is touched before the trash stage) and drops the journal.
/// version the verify read and the kept photos verified are still there. Every check that can still abandon the merge runs
/// before the links stage changes anything, and again at the trash; a merge abandoned after the links stage (a check that
/// fails at the trash, a cancel) gives the other back its keys, links and pairs first (<see cref="MergeLinksUndo"/>). A
/// cancel leaves the photos copied so far in the kept sequence and drops the journal.
/// </summary>
internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger, ArchiveService archives, IProjectCopier projects,
IWorkspaceGuard workspace) : ICancelledJobHandler
@@ -93,6 +95,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
/// <summary>A cancelled merge: its journal and staging go, and its pair returns to the state it had (when it is still this job's).</summary>
private async Task ForgetAsync(Job job, MergeJobParameters parameters, ArchivePaths keepPaths)
{
await MoveBackAsync(keepPaths, job.Id).ConfigureAwait(false);
try
{
MergeJournal.Delete(keepPaths, job.Id);
@@ -105,6 +108,70 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
await new FoundCopyStore(db).ReleaseAsync(parameters.CopyId, job.Id, null, keepJob: false, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>Gives back what the links stage of job <paramref name="jobId"/>'s journal moved (when it has one and the stage began).</summary>
private async Task MoveBackAsync(ArchivePaths keepPaths, Guid jobId)
{
MergeJournal? journal;
try
{
journal = MergeJournal.Read(keepPaths, jobId);
}
catch (InvalidDataException ex)
{
Log.Warning("archive", $"The journal of merge {jobId} could not be read: what its links stage moved stays with the kept sequence", ex);
return;
}
if (journal is not null) await MoveBackAsync(journal).ConfigureAwait(false);
}
/// <summary>
/// Gives the other sequence back what the links stage took (<see cref="MergeJournal.LinksUndo"/>): its rows (session keys,
/// links, pairs), then the session keys of both manifests; the journal is set back to before the links stage (not saved).
/// Nothing when the stage had not begun, or the other is gone (the trash ran). False when it could not be done.
/// </summary>
internal async Task<bool> MoveBackAsync(MergeJournal journal)
{
if (journal.LinksUndo is not { } undo) return true;
var ct = CancellationToken.None;
try
{
var drop = await db.Sequences.GetAsync(journal.DropId, ct).ConfigureAwait(false);
var keep = await db.Sequences.GetAsync(journal.KeepId, ct).ConfigureAwait(false);
if (drop is null || keep is null) return true;
await new FoundCopyStore(db).MoveBackAsync(journal.DropId, journal.KeepId, keep.ArchiveId, undo.DropArchiveId, undo.Moved, ct).ConfigureAwait(false);
var keys = journal.SessionKeys.ToHashSet(StringComparer.Ordinal);
var added = keys.Where(k => !undo.KeepKeysBefore.Contains(k, StringComparer.Ordinal)).ToHashSet(StringComparer.Ordinal);
var removed = keys.Where(k => undo.DropKeysBefore.Contains(k, StringComparer.Ordinal)).ToList();
if (added.Count > 0 && await db.Archives.GetAsync(keep.ArchiveId, ct).ConfigureAwait(false) is { } keepArchive)
{
var paths = new ArchivePaths(keepArchive.RootPath);
if (File.Exists(paths.Sequence(keep.Folder).ManifestPath))
await archives.Writer.UpdateAsync(paths, keep.Id, keep.Folder, manifest =>
!manifest.SessionKeys.Any(added.Contains) ? null : manifest with { SessionKeys = [.. manifest.SessionKeys.Where(k => !added.Contains(k))] },
ct).ConfigureAwait(false);
}
if (removed.Count > 0 && await db.Archives.GetAsync(drop.ArchiveId, ct).ConfigureAwait(false) is { } dropArchive)
{
var paths = new ArchivePaths(dropArchive.RootPath);
if (File.Exists(paths.Sequence(drop.Folder).ManifestPath))
await archives.Writer.UpdateAsync(paths, drop.Id, drop.Folder, manifest =>
removed.All(manifest.SessionKeys.Contains) ? null : manifest with { SessionKeys = [.. manifest.SessionKeys.Union(removed, StringComparer.Ordinal)] },
ct).ConfigureAwait(false);
}
journal.LinksUndo = null;
journal.KeepKeysWritten = false;
journal.DropKeysRemoved = false;
journal.LinksMoved = false;
if (journal.Stage > MergeStage.Links) journal.Stage = MergeStage.Links;
return true;
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
Log.Warning("archive", $"Merge {journal.JobId}: what its links stage moved could not be given back to the other sequence", ex);
return false;
}
}
/// <summary>
/// A resumed run (a pause, a crash, or a retry after a failure) goes on only while the pair is still this job's: it reads
/// Integrating with this job (a failure keeps both; a Keep apart, an Ask again or another merge since clears them), or
@@ -116,6 +183,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
var copy = await new FoundCopyStore(db).GetAsync(parameters.CopyId, ct).ConfigureAwait(false);
if (copy is { State: CopyState.Integrating } && copy.JobId == job.Id) return;
if (copy is null && await db.Sequences.GetAsync(parameters.DropId, ct).ConfigureAwait(false) is null) return;
await MoveBackAsync(keepPaths, job.Id).ConfigureAwait(false);
try
{
MergeJournal.Delete(keepPaths, job.Id);
@@ -178,6 +246,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
private ArchivePaths _dropPaths = null!;
private string _keepTitle = string.Empty;
private string _dropTitle = string.Empty;
private bool _forget;
private long Total => journal.Copies.Count + journal.Renders.Count + journal.Replaces.Count + journal.Projects.Count + 4;
@@ -198,16 +267,24 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
{
_ct.ThrowIfCancellationRequested();
if (owner.BeforeStage is { } hook) await hook(journal.Stage).ConfigureAwait(false);
switch (journal.Stage)
try
{
case MergeStage.Copy: await CopyAsync().ConfigureAwait(false); break;
case MergeStage.Renders: await RendersAsync().ConfigureAwait(false); break;
case MergeStage.Verify: await VerifyAsync().ConfigureAwait(false); break;
case MergeStage.Replace: await ReplaceAsync().ConfigureAwait(false); break;
case MergeStage.Details: await DetailsAsync().ConfigureAwait(false); break;
case MergeStage.Projects: await ProjectsAsync().ConfigureAwait(false); break;
case MergeStage.Links: await LinksAsync().ConfigureAwait(false); break;
case MergeStage.Trash: await TrashAsync().ConfigureAwait(false); break;
switch (journal.Stage)
{
case MergeStage.Copy: await CopyAsync().ConfigureAwait(false); break;
case MergeStage.Renders: await RendersAsync().ConfigureAwait(false); break;
case MergeStage.Verify: await VerifyAsync().ConfigureAwait(false); break;
case MergeStage.Replace: await ReplaceAsync().ConfigureAwait(false); break;
case MergeStage.Details: await DetailsAsync().ConfigureAwait(false); break;
case MergeStage.Projects: await ProjectsAsync().ConfigureAwait(false); break;
case MergeStage.Links: await LinksAsync().ConfigureAwait(false); break;
case MergeStage.Trash: await TrashAsync().ConfigureAwait(false); break;
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
await AbandonAsync().ConfigureAwait(false);
throw;
}
journal.Stage++;
Save();
@@ -223,16 +300,32 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
private InvalidOperationException Changed(string title) => new(Loc.L("Errors.Copies.Changed", title));
/// <summary>Drops this run's journal and staging: what it planned no longer holds.</summary>
private void ForgetJournal()
/// <summary>Asks for this run's journal and staging to go as it fails (<see cref="AbandonAsync"/>): what it planned no longer holds.</summary>
private void ForgetJournal() => _forget = true;
/// <summary>
/// A stage failed: what the links stage took from the other goes back to it first (a retry runs that stage again, after
/// its checks), then the journal goes when <see cref="ForgetJournal"/> asked for it, else it is saved as set back. When
/// nothing could be given back, the journal stays (at the stage it reached) so a retry or a cancel tries again.
/// </summary>
private async Task AbandonAsync()
{
try
var linksBegun = journal.LinksUndo is not null;
if (!await owner.MoveBackAsync(journal).ConfigureAwait(false)) return;
if (_forget)
{
MergeJournal.Delete(keepPaths, journal.JobId);
try
{
MergeJournal.Delete(keepPaths, journal.JobId);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("archive", $"The stale journal of merge {journal.JobId} was not removed", ex);
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
else if (linksBegun)
{
Log.Warning("archive", $"The stale journal of merge {journal.JobId} was not removed", ex);
Save();
}
}
@@ -582,6 +675,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
private async Task ProjectsAsync()
{
await RenamesHeldRendersAsync().ConfigureAwait(false);
if (journal.Projects.Count == 0) return;
var from = new ArchiveProjectHome(_db, owner.Archives.Projects, journal.DropId);
var to = new ArchiveProjectHome(_db, owner.Archives.Projects, journal.KeepId);
@@ -624,16 +718,33 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
if (failed > 0) throw new InvalidOperationException(Loc.Plural("Errors.Copies.ProjectsKept", failed));
}
/// <summary>
/// The renders moved to the kept sequence that name a project of the other the plan left because the kept sequence
/// holds its copy already name that copy (and its versions), as those of a copied project do.
/// </summary>
private async Task RenamesHeldRendersAsync()
{
var planned = journal.Projects.Select(p => p.SourceProjectId).ToHashSet();
IReadOnlyList<SequenceProject>? keepProjects = null;
foreach (var project in await _db.Projects.ListBySequenceAsync(journal.DropId, includeDeleted: false, _ct).ConfigureAwait(false))
{
if (project.IsMain || planned.Contains(project.Id)) continue;
keepProjects ??= await _db.Projects.ListBySequenceAsync(journal.KeepId, includeDeleted: false, _ct).ConfigureAwait(false);
if (await owner.Merger.HeldAsync(project, keepProjects, _dropArchive.Name, _ct).ConfigureAwait(false) is { } held)
await RenamesRendersAsync(project.Id, held.KeptId, held.Versions.Select(v => new TransplantMove(v.Key, v.Value))).ConfigureAwait(false);
}
}
/// <summary>The renders moved to the kept sequence name the project copy (and its versions) instead of the other's.</summary>
private async Task RenamesRendersAsync(Guid source, Guid copy)
private Task RenamesRendersAsync(Guid source, Guid copy) =>
RenamesRendersAsync(source, copy, journal.ProjectSteps.GetValueOrDefault(source)?.Moves ?? []);
private async Task RenamesRendersAsync(Guid source, Guid copy, IEnumerable<TransplantMove> moves)
{
var access = new DbAccess(_db.Database);
if (journal.ProjectSteps.GetValueOrDefault(source) is { } step)
{
foreach (var move in step.Moves)
await access.ExecuteAsync("UPDATE renders SET project_id = $2, project_version_id = $3 WHERE sequence_id = $1 AND project_version_id = $4",
[journal.KeepId, copy, move.Copy, move.Source], _ct).ConfigureAwait(false);
}
foreach (var move in moves)
await access.ExecuteAsync("UPDATE renders SET project_id = $2, project_version_id = $3 WHERE sequence_id = $1 AND project_version_id = $4",
[journal.KeepId, copy, move.Copy, move.Source], _ct).ConfigureAwait(false);
await access.ExecuteAsync("UPDATE renders SET project_id = $2 WHERE sequence_id = $1 AND project_id = $3", [journal.KeepId, copy, source], _ct)
.ConfigureAwait(false);
}
@@ -645,6 +756,17 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Process)).ConfigureAwait(false);
var keep = await _db.Sequences.GetAsync(journal.KeepId, _ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
var drop = await _db.Sequences.GetAsync(journal.DropId, _ct).ConfigureAwait(false) ?? throw Changed(_dropTitle);
if (journal.LinksUndo is null)
{
// Nothing of the other is touched while a check before the trash can still abandon the merge.
await CheckTrashAsync(drop).ConfigureAwait(false);
await using (await owner.Archives.Writer.LockAsync(keepPaths, journal.KeepId, _ct).ConfigureAwait(false))
await PlanTrashAsync().ConfigureAwait(false);
journal.LinksUndo = new MergeLinksUndo(KeysOf(keepPaths, keep), KeysOf(_dropPaths, drop), _dropArchive.Id,
await new FoundCopyStore(_db).UndoOfMoveAsync(journal.DropId, journal.KeepId, keepArchive.Id, journal.SessionKeys, journal.CopyId, _ct)
.ConfigureAwait(false));
Save();
}
var keys = journal.SessionKeys.ToHashSet(StringComparer.Ordinal);
if (!journal.KeepKeysWritten)
{
@@ -686,13 +808,22 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
}
}
/// <summary>The session keys of the manifest of <paramref name="sequence"/> (none when it has no manifest).</summary>
private static List<string> KeysOf(ArchivePaths paths, Sequence sequence)
{
var manifest = paths.Sequence(sequence.Folder).ManifestPath;
return sequence.Folder.Length > 0 && File.Exists(manifest) ? [.. ManifestStore.ReadSequence(manifest).SessionKeys] : [];
}
// ---- trash ----
private async Task TrashAsync()
/// <summary>
/// The checks before the trash that may abandon the merge, run before the links stage and again at the trash: no
/// unsent workspace work, no work saved into the other since the plan, the other at the frames version the verify read
/// and readable. Returns that frames version.
/// </summary>
private async Task<int> CheckTrashAsync(Sequence drop)
{
var drop = await _db.Sequences.GetAsync(journal.DropId, _ct).ConfigureAwait(false);
if (drop is null) return; // a run that moved it before its journal said so
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Trash, _dropTitle)).ConfigureAwait(false);
if (await owner.Workspace.HasUnsentAsync(journal.DropId, _ct).ConfigureAwait(false))
throw new InvalidOperationException(Loc.L("Errors.Copies.Unsent", _dropTitle));
if (await WorkSinceThePlanAsync(drop).ConfigureAwait(false))
@@ -704,12 +835,28 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
var expected = journal.VerifiedDropFramesVersion ?? throw Changed(_dropTitle);
if (drop.FramesVersion != expected) throw Changed(_dropTitle);
EnsureDropReadable(drop);
return expected;
}
/// <summary>Under the kept sequence's lease: every verified photo is still held, and the trash's dry run of the other is not blocked.</summary>
private async Task<DeletePlan> PlanTrashAsync()
{
await EnsureVerifiedHeldAsync().ConfigureAwait(false);
var plan = await owner.Archives.Trash.PlanAsync([journal.DropId], null, _ct, journal.JobId).ConfigureAwait(false);
if (plan.Blocked.Count > 0)
throw new InvalidOperationException(Loc.L("Errors.Copies.TrashBlocked", _dropTitle, Loc.L("Archive.Delete.Reason." + plan.Blocked[0].Reason)));
return plan;
}
private async Task TrashAsync()
{
var drop = await _db.Sequences.GetAsync(journal.DropId, _ct).ConfigureAwait(false);
if (drop is null) return; // a run that moved it before its journal said so
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Trash, _dropTitle)).ConfigureAwait(false);
var expected = await CheckTrashAsync(drop).ConfigureAwait(false);
await using (await owner.Archives.Writer.LockAsync(keepPaths, journal.KeepId, _ct).ConfigureAwait(false))
{
await EnsureVerifiedHeldAsync().ConfigureAwait(false);
var plan = await owner.Archives.Trash.PlanAsync([journal.DropId], null, _ct, journal.JobId).ConfigureAwait(false);
if (plan.Blocked.Count > 0)
throw new InvalidOperationException(Loc.L("Errors.Copies.TrashBlocked", _dropTitle, Loc.L("Archive.Delete.Reason." + plan.Blocked[0].Reason)));
var plan = await PlanTrashAsync().ConfigureAwait(false);
try
{
await owner.Archives.Trash.DeleteAsync(plan, null, null, _ct, journal.JobId, new Dictionary<Guid, int> { [journal.DropId] = expected })
@@ -732,15 +879,14 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
{
if (journal.DropSettingsVersion is { } settings && drop.SettingsVersion != settings) return true;
var planned = journal.Projects.ToDictionary(p => p.SourceProjectId);
HashSet<string>? keepHeads = null;
IReadOnlyList<SequenceProject>? keepProjects = null;
foreach (var project in await _db.Projects.ListBySequenceAsync(journal.DropId, includeDeleted: false, _ct).ConfigureAwait(false))
{
if (project.IsMain) continue;
if (planned.TryGetValue(project.Id, out var step) && (project.HeadVersionId ?? Guid.Empty) == step.SourceHeadId) continue;
// Not planned (or moved on since) is fine only when the kept sequence holds that head already, as the plan saw it.
keepHeads ??= await owner.Merger.HeadHashesAsync(
await _db.Projects.ListBySequenceAsync(journal.KeepId, includeDeleted: false, _ct).ConfigureAwait(false), _ct).ConfigureAwait(false);
if (!await owner.Merger.HeldAlreadyAsync(project, keepHeads, _ct).ConfigureAwait(false)) return true;
// Not planned (or moved on since) is fine only when the kept sequence holds a copy of it already, as the plan saw it.
keepProjects ??= await _db.Projects.ListBySequenceAsync(journal.KeepId, includeDeleted: false, _ct).ConfigureAwait(false);
if (await owner.Merger.HeldAsync(project, keepProjects, _dropArchive.Name, _ct).ConfigureAwait(false) is null) return true;
}
var renders = await _db.Renders.ListBySequenceAsync(journal.DropId, _ct).ConfigureAwait(false);
if (renders.Count == 0) return false;