fix(archive): settle trash journals under their leases and say why restored photos stayed

Second fix round of owner review 2, phase 3 (engine, server, desktop).

- A photo never hashed whose original is missing can be deleted again: it goes
  into its trash record without a file and without a tombstone (nothing is left
  to name), stays listed until Empty erases it, and a restore keeps it with the
  reason "no file". FrameDeleteBlock.Unhashable now blocks only a present
  original that cannot be read; the API answers it as 409 photo_unreadable
  naming the photo, and every block has its own code and message.
- Deleting a whole sequence first settles an interrupted photo deletion,
  restore or ingest of it (ReadLocked under its lease), so no journal or half
  entry travels into the sequence's trash entry; it refuses while a restore
  into it waits for its busy entry.
- FrameTrash.Settle takes the photo entry's lease and leaves the journal for
  later while the entry is busy (an emptying), so it never races EraseAsync;
  meanwhile no other restore writes its journal over it and scans do not adopt
  its files.
- The hashes computed for frames that stay (to find a twin of a deleted photo)
  go into the sidecar and index, and only a frame whose first and last 64 KiB
  match a deleted photo's is read whole.
- A restore into a new sequence rewrites its record after each photo filed, so
  a crash or a database failure never leaves filed photos listed.
- An upload completion no longer has a window before the commit: the ingest
  checks the sidecar's tombstones under the sequence lease a deletion takes
  (IngestRequest.RefuseRemoved), unless the upload asked to bring it back.
- A restore reports each kept photo with its reason (no longer matches its
  record, could not be moved back, already in the sequence, had no file); the
  desktop line and the API message say so in en and it, and the tooltip holds
  the whole sentence. The desktop tells TrashMismatchException by its type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 1858f1f450a35fb8383ede8d80ce5f8de9b7ceac)
This commit is contained in:
Alby96 committed 2026-10-27 13:53:41 +01:00
1 parent a133306fb0
commit 4518ffe655
29 files changed
+856 -162

No files matched your search

@@ -171,7 +171,9 @@ public sealed class TrashDialogTests(TestContext context)
await RestoreAsync(Only(dialog.Rows));
Assert.Equal(Loc.Plural("Archive.Trash.KeptFiles", 1), dialog.ErrorText, "the sentence itself, not a generic file error");
Assert.Equal(Loc.Plural("Archive.Trash.Kept.Mismatch", 1), dialog.ErrorText, "the sentence itself, not a generic file error");
// The line may cut the sentence: the tooltip holds it whole, then the photo's name.
Assert.True(dialog.ErrorToolTip!.StartsWith(dialog.ErrorText + Environment.NewLine, StringComparison.Ordinal), dialog.ErrorToolTip);
Assert.Contains(frames[2].OriginalFileName ?? Path.GetFileName(frames[2].RelativePath), dialog.ErrorToolTip);
Assert.Equal(1, restored, "the other photo came back");
var left = Only(dialog.Rows);
@@ -200,13 +202,47 @@ public sealed class TrashDialogTests(TestContext context)
await RestoreAsync(Only(dialog.Rows));
Assert.Equal(Loc.L("Archive.Error.TrashMismatch"), dialog.ErrorText, "the sentence itself, not a generic file error");
Assert.Equal(dialog.ErrorText, dialog.ErrorToolTip, "the whole sentence, once");
Assert.Equal(0, restored);
Assert.Equal(2, Only(dialog.Rows).Entry.Frames);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FrameCount);
// Any other invalid data (a sidecar of another sequence, say) still reads as a file error.
// Told by its type: an IOException that merely carries the same words, or any other invalid data (a sidecar of
// another sequence, say), still reads as a file error.
Assert.Equal(Loc.L("Archive.Error.TrashMismatch"), ArchiveFailures.Cause(new TrashMismatchException()));
Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new IOException(Loc.L("Archive.Error.TrashMismatch"))));
Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new InvalidDataException("sequence.json belongs to sequence 42, not 43.")));
}
[Test]
public void WhatARestoreKeptSaysWhyForEachReasonAndNamesThePhotos()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
// One reason: its sentence on the line, repeated whole in the tooltip above the names.
var (one, oneTip) = TrashDialogViewModel.KeptText([new KeptPhoto("IMG_0001.CR3", KeptReason.NotMoved), new KeptPhoto("IMG_0002.CR3", KeptReason.NotMoved)]);
Assert.Equal(Loc.Plural("Archive.Trash.Kept.NotMoved", 2), one);
Assert.Equal(string.Join(Environment.NewLine, one, Formatting.LeftToRight("IMG_0001.CR3"), Formatting.LeftToRight("IMG_0002.CR3")), oneTip);
// Several reasons: how many stayed on the line; each reason's sentence in the tooltip, names counted past the list.
var kept = Enumerable.Range(0, DeletePlans.ListedLines + 3)
.Select(i => new KeptPhoto($"IMG_{i:0000}.JPG", i % 3 == 0 ? KeptReason.InSequence : i % 3 == 1 ? KeptReason.Mismatch : KeptReason.NoFile)).ToList();
var (several, tip) = TrashDialogViewModel.KeptText(kept);
Assert.Equal(Loc.Plural("Archive.Trash.KeptPhotos", kept.Count), several);
var lines = tip.Split(Environment.NewLine);
Assert.Equal(several, lines[0]);
foreach (var reason in new[] { KeptReason.Mismatch, KeptReason.InSequence, KeptReason.NoFile })
Assert.Contains(KeptPhoto.Sentence(reason, kept.Count(k => k.Reason == reason), Loc.Culture), lines);
Assert.DoesNotContain(Loc.Plural("Archive.Trash.Kept.NotMoved", 1), lines);
Assert.Equal(Loc.Plural("Archive.Trash.KeptMore", 3), lines[^1]);
Assert.Equal(DeletePlans.ListedLines, lines.Count(l => l.Contains("IMG_", StringComparison.Ordinal)));
// Italian counts photos, which are feminine.
var italian = CultureInfo.GetCultureInfo("it");
Assert.Equal("1 foto è rimasta nel cestino: non corrisponde più al suo registro.", KeptPhoto.Sentence(KeptReason.Mismatch, 1, italian));
Assert.Equal("3 foto sono rimaste nel cestino: la sequenza le contiene già.", KeptPhoto.Sentence(KeptReason.InSequence, 3, italian));
Assert.Equal("+ altre 3", Loc.Text("Archive.Trash.KeptMore", [3], italian));
Assert.Equal("+ un'altra", Loc.Text("Archive.Trash.KeptMore", [1], italian));
}
private static TrashRowViewModel Only(IReadOnlyList<TrashRowViewModel> rows)
{
Assert.Count(1, rows);