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

@@ -227,6 +227,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
Replaces = [.. plan.Photos.Where(p => p.Action == PhotoAction.TrashHere && p.FrameId is not null && winners.GetValueOrDefault(p.Key) is { Length: > 0 })
.Select(p => new MergeReplace(p.Key, p.FrameId!.Value, p.FileName, winners[p.Key]!))],
Absent = [.. plan.LastCopies.Where(l => l.Side == CopySide.Other).Select(l => l.Key.ToLowerInvariant())],
LastHere = [.. plan.LastCopies.Where(l => l.Side == CopySide.ThisPc).Select(l => l.Key.ToLowerInvariant())],
Renders = [.. plan.Renders],
Payload = plan.Payload,
Projects = [.. plan.Projects],
@@ -720,6 +721,13 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
Keep(replace, Loc.L("Archive.Log.KeptReason.NotInPlace"));
continue;
}
if (!await HeldOrListedAsync(loser).ConfigureAwait(false))
{
// Held by another sequence when planned, by nothing now: a last copy the plan did not list. Planned
// again, it is listed.
ForgetJournal();
throw new StaleCopyPlanException();
}
try
{
var plan = await owner.Archives.FrameTrash.PlanDeleteAsync(journal.KeepId, [replace.KeepFrameId], null, _ct, journal.JobId).ConfigureAwait(false);
@@ -747,6 +755,22 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
context.Log(Loc.L("Archive.Log.CopyKept", replace.FileName, why));
}
/// <summary>
/// Whether the losing version <paramref name="loser"/> may go to the trash: a last copy the plan listed, or held by
/// another usable frame of the kept sequence or by another sequence of this index now (the one that held it when
/// planned may have been flagged or deleted while the photos were copied). An unhashed loser was never compared by
/// content and is not asked.
/// </summary>
private async Task<bool> HeldOrListedAsync(Frame loser)
{
if (loser.Sha256 is not { Length: > 0 } recorded) return true;
var sha = recorded.ToLowerInvariant();
if (journal.LastHere.Contains(sha)) return true;
foreach (var frame in await _db.Frames.FindBySha256Async(sha, _ct).ConfigureAwait(false))
if (frame.SequenceId == journal.KeepId && frame.Id != loser.Id && (frame.Flags & CopySides.UnusableFlags) == 0) return true;
return (await new CopySides(_db).HeldElsewhereAsync([sha], [journal.KeepId, journal.DropId], _ct).ConfigureAwait(false)).Contains(sha);
}
/// <summary>Whether a usable frame of the kept sequence holds <paramref name="sha"/>, hashed again.</summary>
private async Task<bool> WinnerInPlaceAsync(string sha)
{
@@ -764,7 +788,17 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
{
if (journal.Payload is null || journal.DetailsDone) return;
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Details)).ConfigureAwait(false);
await new SequencePayloadWriter(_db, owner.Archives).ApplyAsync(journal.KeepId, journal.Payload, journal.KeepSettingsVersion, _ct).ConfigureAwait(false);
try
{
await new SequencePayloadWriter(_db, owner.Archives).ApplyAsync(journal.KeepId, journal.Payload, journal.KeepSettingsVersion, _ct).ConfigureAwait(false);
}
catch (SyncConflictChangedException)
{
// The kept sequence's settings version never goes back: retrying this journal would fail the same way, so it
// goes and the next run plans again.
ForgetJournal();
throw;
}
journal.DetailsDone = true;
Save();
}