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

+13
View File
@@ -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. no longer reads as a count of items.
- The translation glossaries list the match terms (Match, Integrate, Merge sequences, Keep apart, Twin, Link only - 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. 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 ## [1.1.1] — 2026-11-16
+6
View File
@@ -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 | 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 | 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 | 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:<local:N>:<job:N>`, 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 |
@@ -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 }) 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]!))], .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())], 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], Renders = [.. plan.Renders],
Payload = plan.Payload, Payload = plan.Payload,
Projects = [.. plan.Projects], Projects = [.. plan.Projects],
@@ -720,6 +721,13 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
Keep(replace, Loc.L("Archive.Log.KeptReason.NotInPlace")); Keep(replace, Loc.L("Archive.Log.KeptReason.NotInPlace"));
continue; 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 try
{ {
var plan = await owner.Archives.FrameTrash.PlanDeleteAsync(journal.KeepId, [replace.KeepFrameId], null, _ct, journal.JobId).ConfigureAwait(false); 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)); 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> /// <summary>Whether a usable frame of the kept sequence holds <paramref name="sha"/>, hashed again.</summary>
private async Task<bool> WinnerInPlaceAsync(string sha) 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; if (journal.Payload is null || journal.DetailsDone) return;
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Details)).ConfigureAwait(false); 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; journal.DetailsDone = true;
Save(); Save();
} }
@@ -77,6 +77,12 @@ internal sealed class MergeJournal
/// <summary>Raw SHA-256 of drop originals the plan leaves out of the kept sequence (last copies, losers, tombstoned in keep, converted).</summary> /// <summary>Raw SHA-256 of drop originals the plan leaves out of the kept sequence (last copies, losers, tombstoned in keep, converted).</summary>
public List<string> Absent { get; set; } = []; public List<string> Absent { get; set; } = [];
/// <summary>
/// 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.
/// </summary>
public List<string> LastHere { get; set; } = [];
public List<RenderStep> Renders { get; set; } = []; public List<RenderStep> Renders { get; set; } = [];
public SyncPayload? Payload { get; set; } public SyncPayload? Payload { get; set; }
+40 -1
View File
@@ -131,6 +131,10 @@ public sealed class SequenceMerger(ChronosDatabase db, ArchiveService archives,
there = hashed; there = hashed;
comparison = CopyComparer.Compare(here, there); 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 steps = new List<PhotoStep>();
var excluded = new List<string>(); var excluded = new List<string>();
var unchosen = 0; 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. // 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!)); steps.Add(Step(photo.Key, PhotoAction.Fetch, photo.There!));
break; 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: case PhotoClass.Differs:
// Under Choose each a photo nobody chose blocks: its other version would go to the trash unasked (Leave, // 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). // 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 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)) 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); .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 // 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 // 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. // 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; 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> /// <summary>
/// Whether the other sequence holds a frame flagged unusable (Corrupt, or Missing by an earlier verify although its file /// 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 /// 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> /// <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. /// before while the file keeps its size and last write time (<see cref="_hashes"/>), else read now.
/// </summary> /// </summary>
private async Task<string> HashAsync(Guid frameId, string path, CancellationToken ct) private async Task<string> HashAsync(Guid frameId, string path, CancellationToken ct)
+4 -2
View File
@@ -162,7 +162,7 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
/// <summary> /// <summary>
/// The row of a link (evidence Link, state Linked) when it has none; the link's row otherwise kept (moved to Linked when it /// 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 /// 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). /// local copy's rows with another server sequence that answered 404 are deleted (Upload again).
/// </summary> /// </summary>
@@ -181,8 +181,10 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
row = LinkRow(server, remoteId, localId, now); row = LinkRow(server, remoteId, localId, now);
await InsertAsync(inner, row, now, ct).ConfigureAwait(false); 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", 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); [row.Id, false, now], ct).ConfigureAwait(false);
} }
+40 -6
View File
@@ -261,6 +261,13 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
/// <summary>The journal no longer holds: it goes, and <see cref="StaleCopyPlanException"/> is thrown.</summary> /// <summary>The journal no longer holds: it goes, and <see cref="StaleCopyPlanException"/> is thrown.</summary>
[System.Diagnostics.CodeAnalysis.DoesNotReturn] [System.Diagnostics.CodeAnalysis.DoesNotReturn]
private void Stale() private void Stale()
{
Forget();
throw new StaleCopyPlanException();
}
/// <summary>The journal no longer holds: it goes, so the next run plans again.</summary>
private void Forget()
{ {
try 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); Log.Warning("sync", $"The stale journal of Integrate {journal.JobId} was not removed", ex);
} }
throw new StaleCopyPlanException();
} }
/// <summary>Before a stage that trashes or writes: the pair is still this job's and the link still joins its two sides.</summary> /// <summary>Before a stage that trashes or writes: the pair is still this job's and the link still joins its two sides.</summary>
@@ -449,6 +455,14 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
{ {
throw await GoneAsync(ex).ConfigureAwait(false); 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.PayloadRemote = true;
journal.PayloadLocal = true; journal.PayloadLocal = true;
journal.FinalPayloadSha = IntegratePlanner.PayloadHash(journal.Payload); journal.FinalPayloadSha = IntegratePlanner.PayloadHash(journal.Payload);
@@ -470,9 +484,14 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
_ct.ThrowIfCancellationRequested(); _ct.ThrowIfCancellationRequested();
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Projects, step.TargetName, n, journal.Projects.Count)).ConfigureAwait(false); 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); 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 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) 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. // Deleted at its home since the plan: nothing to copy.
@@ -490,15 +509,30 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
Save(); Save();
context.Log(Loc.L("Archive.Log.CopyProject", step.SourceName, step.TargetName)); 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); Failed(step, ex.Message);
Save();
context.Log(Loc.L("Archive.Log.CopyProjectKept", step.SourceName, 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.
} }
} }
/// <summary>
/// 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.
/// </summary>
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 ---- // ---- done ----
private async Task FinishAsync() private async Task FinishAsync()
+9 -2
View File
@@ -469,6 +469,8 @@ public sealed class SyncService
?? new SyncLink { Server = server, RemoteId = remoteId, Title = local.DisplayTitle, RemoteUpdatedAt = DateTimeOffset.UtcNow }; ?? new SyncLink { Server = server, RemoteId = remoteId, Title = local.DisplayTitle, RemoteUpdatedAt = DateTimeOffset.UtcNow };
if (link.LocalId != local.Id) link = Unlinked(link) with { LocalId = local.Id }; if (link.LocalId != local.Id) link = Unlinked(link) with { LocalId = local.Id };
await _links.UpsertAsync(link, cancellationToken).ConfigureAwait(false); 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); await Copies.EnsureLinkedAsync(server, remoteId, local.Id, cancellationToken).ConfigureAwait(false);
return link; return link;
} }
@@ -498,6 +500,7 @@ public sealed class SyncService
{ {
var linked = Unlinked(Observe(link, remote.Server, dto)) with { LocalId = local.Id }; var linked = Unlinked(Observe(link, remote.Server, dto)) with { LocalId = local.Id };
await ReconcileSafelyAsync(remote, linked, dto, local, locals, policy, cancellationToken).ConfigureAwait(false); 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); await Copies.EnsureLinkedAsync(remote.Server, dto.Id, local.Id, cancellationToken).ConfigureAwait(false);
} }
catch catch
@@ -793,7 +796,8 @@ public sealed class SyncService
} }
/// <summary> /// <summary>
/// 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 <see cref="GoneRecheck"/> after it was marked, and a second 404 there makes the /// is asked for once more, no sooner than <see cref="GoneRecheck"/> 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 /// 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). /// kept it (the pair stays Linked or Integrated).
@@ -804,12 +808,15 @@ public sealed class SyncService
var now = DateTimeOffset.UtcNow; var now = DateTimeOffset.UtcNow;
foreach (var pair in await Copies.ListByServerAsync(remote.Server, cancellationToken).ConfigureAwait(false)) 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)) 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); await BackAsync(pair, remoteId).ConfigureAwait(false);
continue; continue;
} }
if (pair.RemoteGoneHits > 1) continue;
// A sweep of 404s (the server serves something else now) confirms no earlier hit either. // 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; if (!confirm || (pair.RemoteGoneAt is { } at && now - at < GoneRecheck)) continue;
try try
+17 -1
View File
@@ -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 // 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). // is filed into the new sequence once there is one).
var heldElsewhere = new List<Frame>(); var heldElsewhere = new List<Frame>();
var sessionKey = remoteId is null ? await SessionKeyAsync(context, remote, local).ConfigureAwait(false) : null;
while (remoteId is null && pending.TryDequeue(out var first)) 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); hashes, ct).ConfigureAwait(false);
if (sent is null) skipped++; if (sent is null) skipped++;
else if (sent.SequenceId == Guid.Empty && sent.FrameId != Guid.Empty) heldElsewhere.Add(first); 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); return (null, true);
} }
/// <summary>
/// The session key the first original of an upload without a target goes under: this sequence's own
/// (<see cref="SyncService.SessionKey"/>), 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).
/// </summary>
private async Task<string> 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<bool> ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken) private static async Task<bool> ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken)
{ {
try try
@@ -450,6 +450,109 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co
Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id)); 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] [Test]
public async Task ADamagedPhotoOfTheKeptOneIsRepairedFromTheOther() public async Task ADamagedPhotoOfTheKeptOneIsRepairedFromTheOther()
{ {
@@ -647,6 +647,89 @@ public sealed class IntegrateTests(TestContext context)
Assert.Equal(CopyState.Linked, (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!.State); 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);
}
/// <summary>A project copier whose first copy meets a server out of reach (a network error, gone a moment later).</summary>
private sealed class FlakyCopier(IProjectCopier inner) : IProjectCopier
{
public int Calls { get; private set; }
public async Task<SequenceProject> CopyAsync(IProjectHome from, SequenceProject source, IProjectHome to, string name, TransplantStep? journal,
Func<TransplantStep, ValueTask> 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] [Test]
public async Task AServerTitleEditMeanwhileOverAPlannedLineThisPcWinsStopsTheDetails() public async Task AServerTitleEditMeanwhileOverAPlannedLineThisPcWinsStopsTheDetails()
{ {
@@ -177,6 +177,39 @@ public sealed class SyncServiceCopyTests(TestContext context)
Assert.True(pair.NeedsAttention); 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] [Test]
public async Task ASequenceBackOnTheServerIsAskedAboutAgain() public async Task ASequenceBackOnTheServerIsAskedAboutAgain()
{ {
@@ -143,6 +143,35 @@ public sealed class SyncTransferCopyTests(TestContext context)
Assert.True(target.Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha))); 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] [Test]
public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing() public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing()
{ {