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

@@ -66,7 +66,10 @@ public abstract class ArchiveDialogViewModel : LocalizedViewModel
/// <summary>Why the last dry run or apply stopped (<see cref="ArchiveFailures.Cause"/>), or what it left undone; null when nothing failed.</summary>
public string? ErrorText { get => _errorText; private set => Set(ref _errorText, value); }
/// <summary>The failure as the engine reported it (or what a <see cref="ShowMessage"/> names), for the tooltip.</summary>
/// <summary>
/// The tooltip of <see cref="ErrorText"/>: the whole sentence (the line may cut it), then the failure as the engine
/// reported it, or what a <see cref="ShowMessage"/> names.
/// </summary>
public string? ErrorToolTip { get => _errorToolTip; private set => Set(ref _errorToolTip, value); }
protected void Close() => _close(this);
@@ -74,14 +77,17 @@ public abstract class ArchiveDialogViewModel : LocalizedViewModel
protected void ShowError(Exception ex)
{
ErrorText = ArchiveFailures.Cause(ex);
ErrorToolTip = ex.Message;
ErrorToolTip = ex.Message == ErrorText ? ErrorText : ErrorText + Environment.NewLine + ex.Message;
}
/// <summary>Shows a sentence localized already, such as what an action that half succeeded left undone; <paramref name="tip"/> details it.</summary>
/// <summary>
/// Shows a sentence localized already, such as what an action that half succeeded left undone; <paramref name="tip"/>
/// details it, and repeats the sentence first (the line may cut it).
/// </summary>
protected void ShowMessage(string text, string? tip = null)
{
ErrorText = text;
ErrorToolTip = tip ?? text;
ErrorToolTip = tip is null || tip.StartsWith(text, StringComparison.Ordinal) ? tip ?? text : text + Environment.NewLine + tip;
}
protected void ClearError()
+4 -6
View File
@@ -1,4 +1,3 @@
using System.IO;
using Chronos.App.Infrastructure;
using Chronos.Core.Archiving;
using Chronos.Core.Localization;
@@ -17,12 +16,11 @@ public static class ArchiveFailures
{
ArchiveOfflineException offline => Loc.L(offline.Availability == ArchiveAvailability.ReadOnly ? "Archive.Error.ReadOnly" : "Archive.Error.Offline",
Formatting.LeftToRight(offline.Root)),
// Already one localized sentence each.
// Already one localized sentence each; TrashMismatchException is a trash entry none of whose files matches its record
// any more (FrameTrash.RestoreAsync). Other IOExceptions and InvalidDataExceptions (a sidecar of another sequence)
// carry technical text.
LeaseUnavailableException or NewerFormatException or StaleRenamePlanException or NestedRenameException or StaleDeletePlanException
or SequenceInLibraryException => ex.Message,
// A trash entry none of whose files matches its record any more (FrameTrash.RestoreAsync), told by its sentence: other
// InvalidDataExceptions, such as a sidecar of another sequence, carry technical text.
InvalidDataException when ex.Message == Loc.L("Archive.Error.TrashMismatch") => ex.Message,
or SequenceInLibraryException or TrashMismatchException => ex.Message,
UnauthorizedAccessException => Loc.L("Archive.Failed.Access"),
DatabaseException => Loc.L("Archive.Failed.Index"),
ArgumentException => Loc.L("Archive.Failed.Gone"),
@@ -171,10 +171,11 @@ public sealed class TrashDialogViewModel : ArchiveDialogViewModel
BusyText = null;
if (result.Kept.Count > 0)
{
// Files that no longer match their record stay in the trash: the entry stays, with what is left, and the line
// says how many (the tooltip names them).
// Photos that stay in the trash keep their entry, with what is left; the line says how many stayed and why,
// and the tooltip repeats it with their names.
await LoadAsync();
ShowMessage(Loc.Plural("Archive.Trash.KeptFiles", result.Kept.Count), KeptList(result.Kept));
var (text, tip) = KeptText(result.Kept);
ShowMessage(text, tip);
}
else
{
@@ -195,12 +196,28 @@ public sealed class TrashDialogViewModel : ArchiveDialogViewModel
}
}
/// <summary>The files a restore kept in the trash, one per line; past <see cref="DeletePlans.ListedLines"/> the rest are counted.</summary>
private static string KeptList(IReadOnlyList<string> kept)
/// <summary>
/// The line of what a restore kept in the trash, and its tooltip: one reason's sentence ("2 photos stayed in the trash:
/// they could not be moved back."), or how many stayed when they stayed for different reasons. The tooltip holds that
/// line, then each reason's sentence (when there are several) with its photos' names, one per line, the names counted
/// past <see cref="DeletePlans.ListedLines"/> in all.
/// </summary>
internal static (string Text, string ToolTip) KeptText(IReadOnlyList<KeptPhoto> kept)
{
var lines = kept.Take(DeletePlans.ListedLines).Select(Formatting.LeftToRight).ToList();
if (kept.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", kept.Count - DeletePlans.ListedLines));
return string.Join(Environment.NewLine, lines);
var groups = kept.GroupBy(k => k.Reason).OrderBy(g => g.Key).ToList();
var text = groups.Count == 1 ? KeptPhoto.Sentence(groups[0].Key, groups[0].Count(), Loc.Culture) : Loc.Plural("Archive.Trash.KeptPhotos", kept.Count);
var lines = new List<string> { text };
var listed = 0;
foreach (var group in groups)
{
if (groups.Count > 1) lines.Add(KeptPhoto.Sentence(group.Key, group.Count(), Loc.Culture));
foreach (var photo in group)
{
if (listed++ < DeletePlans.ListedLines) lines.Add(Formatting.LeftToRight(photo.Name));
}
}
if (kept.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Trash.KeptMore", kept.Count - DeletePlans.ListedLines));
return (text, string.Join(Environment.NewLine, lines));
}
/// <summary>