From 123ceba84578064906facf0709908a9453dab904 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Wed, 18 Nov 2026 10:45:11 +0100 Subject: [PATCH] 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 --- docs/CHANGELOG.md | 13 +++ docs/DECISIONS.md | 6 + .../Archiving/Jobs/MergeJobHandler.cs | 36 +++++- src/Chronos.Data/Archiving/MergeJournal.cs | 6 + src/Chronos.Data/Archiving/SequenceMerger.cs | 41 ++++++- src/Chronos.Data/Copies/FoundCopyStore.cs | 6 +- .../Copies/IntegrateJobHandler.cs | 46 +++++++- src/Chronos.Data/Sync/SyncService.cs | 11 +- src/Chronos.Data/Sync/SyncTransfers.cs | 18 ++- .../Archiving/SequenceMergerTests.cs | 103 ++++++++++++++++++ .../Copies/IntegrateTests.cs | 83 ++++++++++++++ .../Sync/SyncServiceCopyTests.cs | 33 ++++++ .../Sync/SyncTransferCopyTests.cs | 29 +++++ 13 files changed, 418 insertions(+), 13 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d4f94ccf..408531e4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -202,6 +202,19 @@ match. A 1.1 server works as before, without Integrate; a 1.2 server adds the ph no longer reads as a count of items. - The translation glossaries list the match terms (Match, Integrate, Merge sequences, Keep apart, Twin, Link only and Unlink, Union, Kept and Other) for every language. + - A merge whose kept sequence is not hashed yet hashes its photos that match the other's by size and time: the same + photo is no longer listed as a last copy, and a different one is copied in as a second version instead of going to + the trash with the other. + - A merge 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 it stops and plans again. + - A merge or an Integrate whose details changed on one side while it ran plans again on the next retry instead of + failing the same way each time. + - A server out of reach while an Integrate copies projects fails the job, and its retry finishes the same copy + instead of leaving a half copy on each side and copying it again under another name. + - A sequence read as deleted on the server is no longer so once the server lists it again or it is linked to it + again: Integrate is offered and new photos move again. + - Upload of a sequence kept apart from (or unlinked from) a server sequence makes a new server sequence instead of + adding the photos to the one kept apart and linking it again. ## [1.1.1] — 2026-11-16 diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md index c946b2d0..6d7d268b 100644 --- a/docs/DECISIONS.md +++ b/docs/DECISIONS.md @@ -1008,3 +1008,9 @@ One line per decision: date · decision · alternative · reason. | 2026-11-17 | Hindi: merging two sequences reads मर्ज (*सीक्वेंस मर्ज करें*, *मर्ज जारी*), as project merges already did, instead of मिलाएँ/मिलाना; *मिलान* stays the match term; merging import sessions keeps मिलाएँ (#73) | Keep मिलाएँ for both merges | *मिलाना · 40 %* (merging) and *मिलान · 3* (matches) sat side by side in one State column and read as one word | | 2026-11-17 | Croatian keeps *podudaranje* for a match (the Windows term of Find › Match), but the matching sequence itself is *podudarna sekvenca*: "s njoj podudarnom sekvencom", "odvojeno od podudarne sekvence" (#73) | *podudarnost*; *par* | "Njezino podudaranje" reads as "its matching", an act, where the tooltip means the other sequence | | 2026-11-17 | Finnish keeps *kohteen {0}* for a name in a placeholder (the usual way to avoid inflecting it), but never right after a count: "{0:N0} kuvaa kohteesta {1}" instead of "{0:N0} kohteen {1} kuvaa" (#73) | Inflect the placeholder | After a number *kohteen* reads as "of n items" | +| 2026-11-17 | A merge plan hashes, in memory, each unhashed frame of the kept sequence that matches a hashed photo of the other by size and time only: the same content counts as held by the kept one (no last copy); another content is copied in as a second version (*FetchVersion*) whatever the policy, left out of the projects as a Union keep-both is, unless the kept one's tombstones refuse it (#73) | File it at the verify, as for an unhashed photo of the other | The plan listed it as a last copy, so the verify settled it before it could be filed and it went to the trash; the plan now shows the copy it makes | +| 2026-11-17 | The merge's Replace stage trashes a losing version of the kept sequence only while it is a last copy the plan listed (`MergeJournal.LastHere`), or another usable frame of the kept one or another sequence still holds it; otherwise the journal goes and the job fails as a stale plan (#73) | Keep the loser and report it | The plan made again lists the new last copy, so the user decides on it; nothing is trashed that was never shown | +| 2026-11-17 | A details conflict at the merge's or the Integrate's Details stage (a settings version or a payload that moved since the plan) drops the journal before the job fails: the retry plans again and names what the new plan needs (#73) | Keep the journal for a retry | The change never goes back, so every retry failed the same way until Cancel | +| 2026-11-17 | The Integrate's Projects stage counts a project as kept for good only for a refusal a retry would meet again (`TransplantFormatException`, `InvalidDataException`, `ProjectException`, a non-transient server refusal, no project home on either side); any other error (a home out of reach, a network error) fails the job with the journal and the copy's step kept, so a retry finishes that copy (#73) | Count every error as a kept project | One transient error deleted the journal, and the next Integrate copied the half-made copy back as a new project on each side | +| 2026-11-17 | A pair's "deleted on server" mark is cleared whenever the server lists that sequence again (whatever the hit count) and whenever a link to that very sequence is made (upload, download, Integrate, Link, auto-link) (#73) | Clear only a single hit | A pair at two hits stayed "deleted on server" for good, even linked again, and its new photos never moved | +| 2026-11-17 | An upload without a target of a local sequence kept apart from a sequence of that server goes under a session key of the job (`chronos-sync::`, the same on a retry) instead of the sequence's own; a link made over a Kept apart pair moves it to Linked (#73) | Refuse the upload | The server files the sequence's own key into the sequence the user kept separate, and re-linked it without asking; the store and the link disagreed | diff --git a/src/Chronos.Data/Archiving/Jobs/MergeJobHandler.cs b/src/Chronos.Data/Archiving/Jobs/MergeJobHandler.cs index 01b7594e..ab284845 100644 --- a/src/Chronos.Data/Archiving/Jobs/MergeJobHandler.cs +++ b/src/Chronos.Data/Archiving/Jobs/MergeJobHandler.cs @@ -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)); } + /// + /// Whether the losing version 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. + /// + private async Task 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); + } + /// Whether a usable frame of the kept sequence holds , hashed again. private async Task 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(); } diff --git a/src/Chronos.Data/Archiving/MergeJournal.cs b/src/Chronos.Data/Archiving/MergeJournal.cs index c46c7068..d9a92299 100644 --- a/src/Chronos.Data/Archiving/MergeJournal.cs +++ b/src/Chronos.Data/Archiving/MergeJournal.cs @@ -77,6 +77,12 @@ internal sealed class MergeJournal /// Raw SHA-256 of drop originals the plan leaves out of the kept sequence (last copies, losers, tombstoned in keep, converted). public List Absent { get; set; } = []; + /// + /// Raw SHA-256 of the kept sequence's losing versions the plan listed as last copies (the user confirmed them): any other + /// loser goes to the trash only while another sequence still holds it. + /// + public List LastHere { get; set; } = []; + public List Renders { get; set; } = []; public SyncPayload? Payload { get; set; } diff --git a/src/Chronos.Data/Archiving/SequenceMerger.cs b/src/Chronos.Data/Archiving/SequenceMerger.cs index 07bebcd4..8a2f97d5 100644 --- a/src/Chronos.Data/Archiving/SequenceMerger.cs +++ b/src/Chronos.Data/Archiving/SequenceMerger.cs @@ -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(); + var keepTombstones = here.Removed.Select(r => r.Sha256).ToHashSet(StringComparer.OrdinalIgnoreCase); var steps = new List(); var excluded = new List(); 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; } + /// + /// The SHA-256 of each unhashed frame of the kept sequence that the comparison matched by size and time only + /// () 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. + /// + private async Task> HashUnhashedHereAsync(CopyComparisonResult comparison, ArchivePaths keepPaths, CancellationToken ct) + { + var result = new Dictionary(); + 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; + } + /// /// 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, } /// - /// The SHA-256 (lower-case) of the other's file of frame : the one read + /// The SHA-256 (lower-case) of the file of frame : the one read /// before while the file keeps its size and last write time (), else read now. /// private async Task HashAsync(Guid frameId, string path, CancellationToken ct) diff --git a/src/Chronos.Data/Copies/FoundCopyStore.cs b/src/Chronos.Data/Copies/FoundCopyStore.cs index 28e1fd7e..4087af1e 100644 --- a/src/Chronos.Data/Copies/FoundCopyStore.cs +++ b/src/Chronos.Data/Copies/FoundCopyStore.cs @@ -162,7 +162,7 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null /// /// The row of a link (evidence Link, state Linked) when it has none; the link's row otherwise kept (moved to Linked when it - /// was a Candidate). Any other Linked or Integrated row of the same server sequence, or of the same local sequence and + /// was a Candidate or Kept apart). Any other Linked or Integrated row of the same server sequence, or of the same local sequence and /// server, is demoted to Candidate (a link re-pointed to another copy: sync_links holds one link per side and server); the /// local copy's rows with another server sequence that answered 404 are deleted (Upload again). /// @@ -181,8 +181,10 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null row = LinkRow(server, remoteId, localId, now); await InsertAsync(inner, row, now, ct).ConfigureAwait(false); } - else if (row.State == CopyState.Candidate) + else if (row.State is CopyState.Candidate or CopyState.KeptApart) { + // A Kept apart answer never stays on a pair that is linked: the store and the link would disagree (Matches + // would offer no Unlink for a pair that syncs). await inner.ExecuteAsync("UPDATE found_copies SET state = 2, previous_state = NULL, job_id = NULL, auto_photos = $2, updated_at = $3 WHERE id = $1", [row.Id, false, now], ct).ConfigureAwait(false); } diff --git a/src/Chronos.Data/Copies/IntegrateJobHandler.cs b/src/Chronos.Data/Copies/IntegrateJobHandler.cs index 98d1bf3e..a414115c 100644 --- a/src/Chronos.Data/Copies/IntegrateJobHandler.cs +++ b/src/Chronos.Data/Copies/IntegrateJobHandler.cs @@ -261,6 +261,13 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla /// The journal no longer holds: it goes, and is thrown. [System.Diagnostics.CodeAnalysis.DoesNotReturn] private void Stale() + { + Forget(); + throw new StaleCopyPlanException(); + } + + /// The journal no longer holds: it goes, so the next run plans again. + private void Forget() { try { @@ -270,7 +277,6 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla { Log.Warning("sync", $"The stale journal of Integrate {journal.JobId} was not removed", ex); } - throw new StaleCopyPlanException(); } /// Before a stage that trashes or writes: the pair is still this job's and the link still joins its two sides. @@ -449,6 +455,14 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla { throw await GoneAsync(ex).ConfigureAwait(false); } + catch (SyncConflictChangedException) + { + // A detail changed on one side since the plan, and that edit never goes back: retrying this journal's + // payloads would fail the same way. It goes, and a retry plans again against both sides as they are now + // (the photos moved so far are found the same there). + Forget(); + throw; + } journal.PayloadRemote = true; journal.PayloadLocal = true; journal.FinalPayloadSha = IntegratePlanner.PayloadHash(journal.Payload); @@ -470,9 +484,14 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla _ct.ThrowIfCancellationRequested(); await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Projects, step.TargetName, n, journal.Projects.Count)).ConfigureAwait(false); var (from, to) = step.From == CopySide.ThisPc ? ((IProjectHome?)localHome, serverHome) : (serverHome, localHome); + if (from is null || to is null) + { + // The server keeps no projects for this PC (none planned there either): kept where it is, for good. + Failed(step, Loc.L("Errors.Copies.ProjectsUnreachable", journal.ServerName ?? journal.Server)); + continue; + } try { - if (from is null || to is null) throw new HomeUnavailableException(Loc.L("Errors.Copies.ProjectsUnreachable", journal.ServerName ?? journal.Server)); if ((await from.ListAsync(_ct).ConfigureAwait(false)).FirstOrDefault(p => p.Id == step.SourceProjectId) is not { } source) { // Deleted at its home since the plan: nothing to copy. @@ -490,15 +509,30 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla Save(); context.Log(Loc.L("Archive.Log.CopyProject", step.SourceName, step.TargetName)); } - catch (Exception ex) when (ex is not OperationCanceledException) + catch (Exception ex) when (IsRefusal(ex)) { - journal.ProjectsFailed.Add(step.SourceProjectId); - Save(); - context.Log(Loc.L("Archive.Log.CopyProjectKept", step.SourceName, ex.Message)); + Failed(step, ex.Message); } + // Anything else (the server out of reach for a moment, a busy database) fails the job with the journal and + // the copy's own step kept: a retry finishes that copy instead of planning it again as a foreign project. } } + /// + /// A refusal that a retry would meet again: a project or version the home refuses or cannot read, a record of another + /// format, a request the server refuses for good. A home out of reach, a network error or a server answer that may + /// change (busy, unavailable) is not. + /// + private static bool IsRefusal(Exception ex) => + ex is TransplantFormatException or InvalidDataException or ProjectException or ChronosApiException { IsTransient: false }; + + private void Failed(ProjectStep step, string why) + { + journal.ProjectsFailed.Add(step.SourceProjectId); + Save(); + context.Log(Loc.L("Archive.Log.CopyProjectKept", step.SourceName, why)); + } + // ---- done ---- private async Task FinishAsync() diff --git a/src/Chronos.Data/Sync/SyncService.cs b/src/Chronos.Data/Sync/SyncService.cs index f72645e1..cb69b5f2 100644 --- a/src/Chronos.Data/Sync/SyncService.cs +++ b/src/Chronos.Data/Sync/SyncService.cs @@ -469,6 +469,8 @@ public sealed class SyncService ?? new SyncLink { Server = server, RemoteId = remoteId, Title = local.DisplayTitle, RemoteUpdatedAt = DateTimeOffset.UtcNow }; if (link.LocalId != local.Id) link = Unlinked(link) with { LocalId = local.Id }; await _links.UpsertAsync(link, cancellationToken).ConfigureAwait(false); + // Linked to that very server sequence, which answered for it: no pair of it reads "deleted on server" any more. + await Copies.ClearRemoteGoneAsync(server, remoteId, cancellationToken).ConfigureAwait(false); await Copies.EnsureLinkedAsync(server, remoteId, local.Id, cancellationToken).ConfigureAwait(false); return link; } @@ -498,6 +500,7 @@ public sealed class SyncService { var linked = Unlinked(Observe(link, remote.Server, dto)) with { LocalId = local.Id }; await ReconcileSafelyAsync(remote, linked, dto, local, locals, policy, cancellationToken).ConfigureAwait(false); + await Copies.ClearRemoteGoneAsync(remote.Server, dto.Id, cancellationToken).ConfigureAwait(false); await Copies.EnsureLinkedAsync(remote.Server, dto.Id, local.Id, cancellationToken).ConfigureAwait(false); } catch @@ -793,7 +796,8 @@ public sealed class SyncService } /// - /// Pairs one 404 marked (design §6.3): a remote listed again is cleared at once and asked about again; one still unlisted + /// Pairs a 404 marked (design §6.3): a remote listed again is cleared at once and asked about again, also one that read + /// "deleted on server" (restored there, or served again); one marked once and still unlisted /// is asked for once more, no sooner than after it was marked, and a second 404 there makes the /// pair read "deleted on server". A refresh's first 404 forgot the link (the pair is asked about again); a job's 404 /// kept it (the pair stays Linked or Integrated). @@ -804,12 +808,15 @@ public sealed class SyncService var now = DateTimeOffset.UtcNow; foreach (var pair in await Copies.ListByServerAsync(remote.Server, cancellationToken).ConfigureAwait(false)) { - if (pair is not { RemoteGoneHits: 1, RemoteId: { } remoteId }) continue; + if (pair is not { RemoteGoneHits: >= 1, RemoteId: { } remoteId }) continue; if (listed.Contains(remoteId)) { + // Listed again (restored from the server's trash, or the server serves its archive again): whatever the + // count, the server answers for it, so the pair no longer reads "deleted on server". await BackAsync(pair, remoteId).ConfigureAwait(false); continue; } + if (pair.RemoteGoneHits > 1) continue; // A sweep of 404s (the server serves something else now) confirms no earlier hit either. if (!confirm || (pair.RemoteGoneAt is { } at && now - at < GoneRecheck)) continue; try diff --git a/src/Chronos.Data/Sync/SyncTransfers.cs b/src/Chronos.Data/Sync/SyncTransfers.cs index bb1de8aa..943fd703 100644 --- a/src/Chronos.Data/Sync/SyncTransfers.cs +++ b/src/Chronos.Data/Sync/SyncTransfers.cs @@ -80,9 +80,10 @@ internal sealed class SyncTransfers(SyncService sync) // server has filed one, which creates the sequence (an original it already holds elsewhere creates nothing, and // is filed into the new sequence once there is one). var heldElsewhere = new List(); + var sessionKey = remoteId is null ? await SessionKeyAsync(context, remote, local).ConfigureAwait(false) : null; while (remoteId is null && pending.TryDequeue(out var first)) { - var sent = await UploadFrameAsync(context, remote, null, SyncService.SessionKey(local.Id), paths.FromRelative(first.RelativePath), first, tombstones, + var sent = await UploadFrameAsync(context, remote, null, sessionKey, paths.FromRelative(first.RelativePath), first, tombstones, hashes, ct).ConfigureAwait(false); if (sent is null) skipped++; else if (sent.SequenceId == Guid.Empty && sent.FrameId != Guid.Empty) heldElsewhere.Add(first); @@ -962,6 +963,21 @@ internal sealed class SyncTransfers(SyncService sync) return (null, true); } + /// + /// The session key the first original of an upload without a target goes under: this sequence's own + /// (), unless the user kept it apart from (or unlinked it from) a sequence of this + /// server. The server files that key into the sequence an earlier upload made, which the user chose to keep separate; + /// such an upload makes a new sequence instead, under a key of this job (the same on a retry). + /// + private async Task SessionKeyAsync(JobContext context, ISyncRemote remote, Sequence local) + { + var own = SyncService.SessionKey(local.Id); + var apart = (await sync.Copies.ListByLocalAsync(local.Id, context.Cancellation).ConfigureAwait(false)) + .Any(c => c is { Kind: CopyKind.LocalServer, State: CopyState.KeptApart } && string.Equals(c.Server, remote.Server, StringComparison.Ordinal) + && c.LocalId == local.Id); + return apart ? string.Create(CultureInfo.InvariantCulture, $"{own}:{context.Job.Id:N}") : own; + } + private static async Task ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken) { try diff --git a/tests/Chronos.Data.Tests/Archiving/SequenceMergerTests.cs b/tests/Chronos.Data.Tests/Archiving/SequenceMergerTests.cs index d3cadf20..25a0cd09 100644 --- a/tests/Chronos.Data.Tests/Archiving/SequenceMergerTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/SequenceMergerTests.cs @@ -450,6 +450,109 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id)); } + [Test] + public async Task AnUnhashedPhotoOfTheKeptOneIsHashedByThePlanAndAnotherPhotoOfTheOtherIsKeptAsASecondVersion() + { + var fx = await FixtureAsync(); + // The kept one adopted and not verified, the other hashed: one photo of the same size and time, other bytes. + var keep = await SequenceAsync(fx, "keep", [(1, 0, "IMG_0001.JPG")]); + var drop = await SequenceAsync(fx, "drop", [(2, 0, "IMG_0001.JPG")]); + var access = new DbAccess(fx.Db.Database); + await access.ExecuteAsync("UPDATE frames SET size = 1000 WHERE sequence_id = $1 OR sequence_id = $2", [keep.Id, drop.Id], Ct); + await access.ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE sequence_id = $1", [keep.Id], Ct); + var copy = await PairAsync(fx, keep.Id, drop.Id); + var merger = Merger(fx); + var (engine, _) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace()); + await engine.StartAsync(Ct); + + var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct); + Assert.Empty(plan.LastCopies); + Assert.SequenceEqual([(PhotoAction.FetchVersion, Sha(2, 0))], plan.Photos.Select(p => (p.Action, p.Sha256!))); + + var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct); + await fx.WaitForAsync(job.Id, JobState.Done); + var frames = await fx.Db.Frames.ListBySequenceAsync(keep.Id); + Assert.Count(2, frames); + Assert.Contains(Sha(2, 0), frames.Select(f => f.Sha256)); + Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id)); + } + + [Test] + public async Task AnUnhashedPhotoOfTheKeptOneWithTheSameContentIsNoLastCopy() + { + var fx = await FixtureAsync(); + var keep = await SequenceAsync(fx, "keep", [(1, 0, "IMG_0001.JPG")]); + var drop = await SequenceAsync(fx, "drop", [(1, 0, "IMG_0001.JPG")]); + await new DbAccess(fx.Db.Database).ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE sequence_id = $1", [keep.Id], Ct); + var copy = await PairAsync(fx, keep.Id, drop.Id); + + var plan = await Merger(fx).PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct); + + Assert.Empty(plan.LastCopies); + Assert.Empty(plan.Photos); + } + + [Test] + public async Task ALosingVersionWhoseOtherHolderIsFlaggedMeanwhileIsNeverTrashedUnlisted() + { + var fx = await FixtureAsync(); + var keep = await SequenceAsync(fx, "keep", [(1, 0, "IMG_0001.JPG"), (4, 30_250, "IMG_0004.JPG")]); + var drop = await SequenceAsync(fx, "drop", [(1, 0, "IMG_0001.JPG"), (5, 30_250, "IMG_0004.JPG")]); + // A third sequence holds the kept one's losing version: not a last copy when planned. + var third = await SequenceAsync(fx, "third", [(4, 30_250, "IMG_0004.JPG")]); + var copy = await PairAsync(fx, keep.Id, drop.Id); + var merger = Merger(fx); + var (engine, handler) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace()); + await engine.StartAsync(Ct); + var choices = await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.OtherWins); + var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, choices, NoContext, Ct); + Assert.Empty(plan.LastCopies); + Assert.Contains(plan.Photos, p => p.Action == PhotoAction.TrashHere); + // While the photos are copied, a verify flags the third's copy Corrupt. + handler.BeforeStage = async stage => + { + if (stage != MergeStage.Replace) return; + var held = (await fx.Db.Frames.ListBySequenceAsync(third.Id)).Single(); + await fx.Db.Frames.UpdateFlagsAsync([held.Id], FrameFlags.Corrupt, FrameFlags.None, Ct); + }; + + var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct); + var failed = await fx.WaitForAsync(job.Id, JobState.Failed); + + // Its only usable copy now: the kept one still holds it (beside the other's version), nothing went to the trash, and + // the journal went so the next run plans again. + Assert.Equal(Chronos.Core.Localization.Loc.L("Errors.Copies.StalePlan"), failed.Error); + var kept = await fx.Db.Frames.ListBySequenceAsync(keep.Id); + Assert.Contains(kept, f => f.Sha256 == Sha(4, 30_250) && f.Flags == FrameFlags.None + && Sha(File.ReadAllBytes(fx.Paths.FromRelative(f.RelativePath))) == Sha(4, 30_250)); + Assert.Contains(Sha(5, 30_250), kept.Select(f => f.Sha256)); + Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id)); + Assert.False(File.Exists(MergeJournal.PathFor(fx.Paths, job.Id))); + } + + [Test] + public async Task ADetailsChangeOfTheKeptOneBeforeTheDetailsStageMakesTheRetryPlanAgain() + { + var fx = await FixtureAsync(); + var s = await ScenarioAsync(fx, 0); + var merger = Merger(fx); + var (engine, handler) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace()); + await engine.StartAsync(Ct); + handler.BeforeStage = stage => stage == MergeStage.Details ? fx.Db.Sequences.SetTagsAsync(s.Keep.Id, ["edited"], Ct) : Task.CompletedTask; + var plan = await merger.PlanAsync(s.Copy.Id, s.Keep.Id, s.Drop.Id, s.Choices, NoContext, Ct); + var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, s.Keep.DisplayTitle, Ct); + var failed = await fx.WaitForAsync(job.Id, JobState.Failed); + Assert.Equal(Chronos.Core.Localization.Loc.L("Errors.Copies.DetailsChanged"), failed.Error); + Assert.False(File.Exists(MergeJournal.PathFor(fx.Paths, job.Id)), "the next run plans again"); + + // The Retry plans again and says what the new plan needs (the new detail line) instead of failing the same way. + handler.BeforeStage = null; + await engine.ResumeAsync(job.Id, Ct); + failed = await fx.WaitForAsync(job.Id, JobState.Failed); + Assert.Equal(Chronos.Core.Localization.Loc.Plural("Errors.Copies.DetailsUnchosen", 1), failed.Error); + Assert.NotNull(await fx.Db.Sequences.GetAsync(s.Drop.Id)); + } + [Test] public async Task ADamagedPhotoOfTheKeptOneIsRepairedFromTheOther() { diff --git a/tests/Chronos.Data.Tests/Copies/IntegrateTests.cs b/tests/Chronos.Data.Tests/Copies/IntegrateTests.cs index f63b6c4f..1dd8097d 100644 --- a/tests/Chronos.Data.Tests/Copies/IntegrateTests.cs +++ b/tests/Chronos.Data.Tests/Copies/IntegrateTests.cs @@ -647,6 +647,89 @@ public sealed class IntegrateTests(TestContext context) Assert.Equal(CopyState.Linked, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State); } + [Test] + public async Task ARetryAfterTheDetailsChangedPlansAgainInsteadOfFailingTheSameWay() + { + await using var f = await FixtureAsync(context); + var (engine, handler) = Engine(f); + await using var _ = engine; + await engine.StartAsync(f.Ct); + var photos = f.NewPhotos(3); + var local = await f.LocalAsync("Comet", photos.Take(2).ToList(), SyncFixture.Window33); + var remote = f.Remote.Add("Comet NAS", [photos[0], photos[2]], SyncFixture.Window33); + var pair = await PairAsync(f, local.Id, remote.Id); + handler.BeforeStage = async stage => + { + if (stage == IntegrateStage.Details) await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41); + }; + var plan = await Planner(f).PlanAsync(f.Remote, pair, await ChoicesAsync(f, local.Id, remote.Id, CopyPhotoPolicy.Union), PlanContext(), f.Ct); + var job = await CopyJobs.EnqueueIntegrateAsync(engine, f.Sync.Copies, plan, local.DisplayTitle, f.Ct); + var failed = await f.Archive.WaitForAsync(job.Id, JobState.Failed); + Assert.Equal(Loc.L("Errors.Copies.DetailsChanged"), failed.Error); + Assert.False(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)), "the next run plans again"); + + // The edit never goes back: the Retry plans again against both sides as they are now and names the new difference. + handler.BeforeStage = null; + await engine.ResumeAsync(job.Id, f.Ct); + failed = await f.Archive.WaitForAsync(job.Id, JobState.Failed); + Assert.Equal(Loc.Plural("Errors.Copies.DetailsUnchosen", 1), failed.Error); + var after = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!; + Assert.Equal(CopyState.Linked, after.State); + // Compared again, the photos moved are the same on both sides and only the details differ. + var again = await Planner(f).PlanAsync(f.Remote, after, await ChoicesAsync(f, local.Id, remote.Id, CopyPhotoPolicy.Union), PlanContext(), f.Ct); + Assert.Empty(again.Photos); + } + + [Test] + public async Task AServerOutOfReachDuringTheProjectsFailsTheJobAndTheRetryFinishesTheSameCopy() + { + await using var f = await FixtureAsync(context); + f.Remote.Capabilities.Add(ServerCapabilities.Projects); + var (local, remote, pair) = await ScenarioAsync(f); + var standIn = await LocalAsync(f, "Stand-in", [(50, 0, "IMG_0050.JPG"), (51, 5_000, "IMG_0051.JPG")]); + var projects = f.Archive.Service.Projects; + var serverHome = new ArchiveProjectHome(f.Db, projects, standIn.Id); + var localFrames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct); + var project = await projects.CreateAsync(local.Id, "Vertical", cancellationToken: f.Ct); + await projects.PushAsync(project.Id, Projects.ProjectServiceTests.Draft(await projects.HeadAsync(project.Id, f.Ct), + new ProjectSettings { ExcludedFrames = [1] }, localFrames), f.Ct); + var planner = new IntegratePlanner(f.Sync, new CopySides(f.Db), projects, (_, _) => serverHome); + var plan = await planner.PlanAsync(f.Remote, pair, await ChoicesAsync(f, local.Id, remote.Id, CopyPhotoPolicy.Union), PlanContext(keepsProjects: true), f.Ct); + Assert.SequenceEqual(["ThisPc:Vertical"], plan.Projects.Select(p => $"{p.From}:{p.TargetName}")); + + var copier = new FlakyCopier(new ProjectTransplant()); + var connector = new SyncFixture.Connector(f.Remote); + var handler = new IntegrateJobHandler(f.Sync, planner, copier, connector, f.Sync.Copies, f.Archive.Service); + await using var engine = new JobEngine(f.Db, [.. f.Archive.Service.CreateJobHandlers(), .. f.Sync.CreateJobHandlers(connector), handler], + new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) }); + await engine.StartAsync(f.Ct); + var job = await CopyJobs.EnqueueIntegrateAsync(engine, f.Sync.Copies, plan, local.DisplayTitle, f.Ct); + await f.Archive.WaitForAsync(job.Id, JobState.Failed); + + // Not a project kept for good: the journal and the pair's job stay for the retry. + Assert.True(File.Exists(IntegrateJournal.PathFor(f.Archive.Paths, job.Id)), "a retry resumes the copy"); + Assert.Equal(job.Id, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.JobId); + + await engine.ResumeAsync(job.Id, f.Ct); + await f.Archive.WaitForAsync(job.Id, JobState.Done); + Assert.Equal(CopyState.Integrated, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State); + Assert.SequenceEqual(["Vertical"], (await projects.ListAsync(standIn.Id, f.Ct)).Where(p => !p.IsMain).Select(p => p.Name)); + Assert.Equal(2, copier.Calls); + } + + /// A project copier whose first copy meets a server out of reach (a network error, gone a moment later). + private sealed class FlakyCopier(IProjectCopier inner) : IProjectCopier + { + public int Calls { get; private set; } + + public async Task CopyAsync(IProjectHome from, SequenceProject source, IProjectHome to, string name, TransplantStep? journal, + Func save, CancellationToken ct) + { + if (++Calls == 1) throw new HomeUnavailableException("The server cannot be reached: connection reset"); + return await inner.CopyAsync(from, source, to, name, journal, save, ct); + } + } + [Test] public async Task AServerTitleEditMeanwhileOverAPlannedLineThisPcWinsStopsTheDetails() { diff --git a/tests/Chronos.Data.Tests/Sync/SyncServiceCopyTests.cs b/tests/Chronos.Data.Tests/Sync/SyncServiceCopyTests.cs index 499f4895..d12d0d44 100644 --- a/tests/Chronos.Data.Tests/Sync/SyncServiceCopyTests.cs +++ b/tests/Chronos.Data.Tests/Sync/SyncServiceCopyTests.cs @@ -177,6 +177,39 @@ public sealed class SyncServiceCopyTests(TestContext context) Assert.True(pair.NeedsAttention); } + [Test] + public async Task ASequenceReadAsDeletedOnTheServerIsClearedWhenItIsListedAgainOrLinkedAgain() + { + await using var f = await SyncFixture.CreateAsync(context); + var nas = new FakeSyncRemote { Server = HomeFolders.IdKey(Guid.NewGuid()) }; + var photos = f.NewPhotos(2); + var local = await f.LocalAsync("Comet", photos); + var remote = nas.Add("Comet", photos); + await f.Sync.LinkAsync(nas.Server, remote.Id, local.Id, f.Ct); + await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct); + nas.Sequences.Remove(remote.Id); + await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct); + var pair = (await f.Sync.Copies.ListByServerAsync(nas.Server, f.Ct)).Single(); + await using (var session = await f.Db.Database.OpenAsync(f.Ct)) + await session.ExecuteAsync("UPDATE found_copies SET remote_gone_at = $1 WHERE id = $2", [DateTimeOffset.UtcNow.AddMinutes(-11), pair.Id], f.Ct); + await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct); + Assert.True((await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.RemoteGone); + + // Restored from the server's trash: listed again, it no longer reads "deleted on server". + nas.Sequences[remote.Id] = remote; + await f.Sync.RefreshAsync(nas, new SyncPolicy(), f.Ct); + pair = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!; + Assert.Equal((0, (DateTimeOffset?)null, false), (pair.RemoteGoneHits, pair.RemoteGoneAt, pair.RemoteGone)); + Assert.Equal(CopyState.Candidate, pair.State); + + // A pair still marked (the listing missed it) is cleared by a link to that very sequence: automatic photos may move again. + await using (var session = await f.Db.Database.OpenAsync(f.Ct)) + await session.ExecuteAsync("UPDATE found_copies SET remote_gone_hits = 2, remote_gone_at = $1 WHERE id = $2", [DateTimeOffset.UtcNow, pair.Id], f.Ct); + await f.Sync.LinkAsync(nas.Server, remote.Id, local.Id, f.Ct); + pair = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!; + Assert.Equal((0, false, CopyState.Linked), (pair.RemoteGoneHits, pair.RemoteGone, pair.State)); + } + [Test] public async Task ASequenceBackOnTheServerIsAskedAboutAgain() { diff --git a/tests/Chronos.Data.Tests/Sync/SyncTransferCopyTests.cs b/tests/Chronos.Data.Tests/Sync/SyncTransferCopyTests.cs index 843eb2cd..c4646f25 100644 --- a/tests/Chronos.Data.Tests/Sync/SyncTransferCopyTests.cs +++ b/tests/Chronos.Data.Tests/Sync/SyncTransferCopyTests.cs @@ -143,6 +143,35 @@ public sealed class SyncTransferCopyTests(TestContext context) Assert.True(target.Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha))); } + [Test] + public async Task AnUploadOfASequenceKeptApartMakesANewServerSequenceAndLeavesTheOtherAlone() + { + await using var f = Capable(await SyncFixture.CreateAsync(context)); + var photos = f.NewPhotos(3); + var local = await f.LocalAsync("Comet", [photos[0], photos[1]]); + var engine = await f.StartEngineAsync(); + var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local); + await f.Archive.WaitForAsync(job.Id, JobState.Done); + var first = Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)).RemoteId; + // Unlinked ("Stop syncing these sequences"): the pair is kept apart. + await f.Sync.UnlinkAsync(f.Server, first, local.Id, f.Ct); + var pair = (await f.Sync.Copies.ListByServerAsync(f.Server, f.Ct)).Single(c => c.RemoteId == first); + Assert.Equal(CopyState.KeptApart, pair.State); + + // A new photo, then a plain Upload. + await f.AddLocalAsync(local.Id, photos[2]); + job = await f.Sync.EnqueueUploadAsync(engine, f.Server, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!); + await f.Archive.WaitForAsync(job.Id, JobState.Done); + + // The sequence kept apart gets nothing, and stays apart and unlinked; a new one holds every photo. + Assert.True(f.Remote.Sequences[first].Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals([Sha(photos[0]), Sha(photos[1])])); + Assert.Equal(CopyState.KeptApart, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State); + Assert.Null((await f.Sync.Links.GetAsync(f.Server, first, f.Ct))!.LocalId); + var link = Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)); + Assert.NotEqual(first, link.RemoteId); + Assert.True(f.Remote.Sequences[link.RemoteId].Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha))); + } + [Test] public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing() {