fix(archive): weigh replaced originals against the archive, not this index

Owner review 2, phase 4b, fix round 2 (trash and sync).

- Emptying a Replaced entry decided "conversion deleted" from this host's index
  alone, so a conversion another host made, or one this index had not read yet,
  could be taken for deleted and its originals erased without re-hashing it. The
  survey now reads the archive: the sequence's sidecar where the index or the
  entry says it is, in the trash, or found by walking the archive's folders. Its
  Replaced tombstones are followed and every conversion it holds is re-hashed. A
  conversion counts as deleted only when the sidecar shows it (a Deleted
  tombstone, or the frame gone while every frame is hashed and no photo of the
  folder is unlisted), when the sequence is in the trash, or when no sidecar of it
  exists and this index has no row either. Whatever the archive cannot tell, or a
  converted frame whose file changed since, keeps the entry, and the job log says
  why (changed after the plan, sequence busy, conversion no longer matches,
  conversion not located).
- The erase weighs a Replaced entry under the entry's and the sequence's leases
  and settles the sequence first (an undo waiting for that entry's lease
  completes), so the entry it reads again and fingerprints is the one it erases.
  Read before the settle, a consumed entry's folder vanished under the erase.
- Each sidecar is indexed once per survey: one conversion's entry (every frame it
  swapped) is weighed in one pass, not one pass over the sidecar per photo.
- The desktop Empty confirmation names every entry whose conversion was deleted
  first, with why, wherever the trash lists it; the detail scrolls when long. The
  panel lists them first as well.
- Download originals queues the passes a frame swapped to another encoding
  cleared (light analysis, the deep analysis it had, thumbnails, proxies), as any
  replacement does.
- The job log lines of uploads and downloads are resources (en, it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 27d9a512183e64da9a3d7fa69a267470ca3ce744)
This commit is contained in:
Alby96 committed 2026-11-02 10:33:34 +01:00
1 parent c669d0504d
commit 48a0ce3e02
15 files changed
+834 -164

No files matched your search

@@ -5,6 +5,7 @@ using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Data.Archiving;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Archiving;
using Chronos.Protocol;
@@ -180,7 +181,7 @@ public sealed class SyncTransferTests(TestContext context)
var kept = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
Assert.True(kept.Select(k => k.Sha256).ToHashSet().SetEquals(photos[1..].Select(Sha)), "the photo deleted here stays deleted");
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[0])), "and keeps its tombstone");
Assert.Contains("deleted on this PC, not downloaded", done.LogTail);
Assert.Contains(Loc.L("Archive.Log.SyncDeletedHere", remote.Frames.Single(fr => fr.Sha256 == Sha(photos[0])).FileName), done.LogTail);
Assert.False(Directory.Exists(Path.Combine(f.Archive.Paths.StagingDirectory, "sync-" + remote.Id.ToString("N"))), "staging cleaned up");
}
@@ -207,7 +208,7 @@ public sealed class SyncTransferTests(TestContext context)
var done = await f.Archive.WaitForAsync(job.Id, JobState.Done);
Assert.Equal(3, remote.Frames.Count);
Assert.Contains("deleted on the server, not uploaded", done.LogTail);
Assert.Contains(Loc.L("Archive.Log.SyncDeletedThere", "DSC_0004.JPG"), done.LogTail);
// Its hash is in the index now: the comparison leaves it out, so the row settles instead of offering Upload for good.
Assert.True((await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).All(fr => fr.Sha256 is not null), "hashed by the verify");
var entry = await f.EntryAsync(local.DisplayTitle);
@@ -881,6 +882,36 @@ public sealed class SyncTransferTests(TestContext context)
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task AFrameThatTookBackItsPhotoInAnotherEncodingIsDrawnAgain()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Harbour", photos);
var engine = await f.StartEngineAsync();
var drawn = await engine.EnqueueAsync(ArchiveJobs.Thumbnails([local.Id], "Harbour"), cancellationToken: f.Ct);
await f.Archive.WaitForAsync(drawn.Id, JobState.Done);
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
Assert.True(((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "drawn before");
// The server converted that photo.
var converted = f.NewPhotos(1)[0];
var remote = f.Remote.Add("Harbour", [photos[0], converted, photos[2]]);
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[1]), Sha(converted), "replaced", DateTimeOffset.UtcNow));
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
await f.Archive.WaitForAsync(job.Id, JobState.Done);
// Nothing was filed, only a frame swapped, which cleared what covered the sequence: the thumbnails it had are drawn again.
Assert.Equal(lost.Id, (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1].Id);
var again = Assert.Contains((await f.Db.Jobs.ListAsync(null, JobKind.Thumbs, 50, null, f.Ct)).Items, j => j.Id != drawn.Id && j.TargetIds.Contains(local.Id));
await f.Archive.WaitForAsync(again.Id, JobState.Done);
Assert.True(((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "drawn again");
}
[Test]
public async Task AFrameTakesBackItsPhotoInAnotherEncodingOnlyWhereTheSequenceIsNow()
{