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

@@ -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);
}
/// <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]
public async Task AServerTitleEditMeanwhileOverAPlannedLineThisPcWinsStopsTheDetails()
{