fix(server): keep Reorganize dry runs for the confirm, test the protocol's key paths

An all:true dry run reads every sidecar and photo folder of the archive inside
the request, and the confirm planned the whole archive again before answering
202. The server now keeps each dry run's plan for five minutes (eight plans at
most) by archive, selection, templates and a stamp of the archive's index rows:
the same dry run again and its confirmation take the kept plan, and any change
the index records (an upload, an edit, a move, a scan, a verify) gives another
stamp and a new plan. A change made on disk alone is not in the stamp; the move
job still plans again and refuses a plan that no longer matches.

Server tests now cover an all:true apply around a sequence already in place, a
real plan_changed after an upload, a move job paused between two sequences and
resumed, archive_offline, job_unavailable, more than 1,000 ids and the kept
plans themselves; a Data test covers the index stamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 71bb53fbf8eb7d0327ea692d046634b12f8d37e9)
This commit is contained in:
Alby96 committed 2026-10-27 16:30:00 +01:00
1 parent 9ce201cd70
commit 9ae773aa5a
7 files changed
+468 -9

No files matched your search

+8 -3
View File
@@ -138,10 +138,11 @@ public static class ArchiveEndpoints
/// <c>not_found</c> (<c>sequence</c>: not in the archive); 409 <c>nested_target</c> (details: the folder that would lie
/// inside a sequence's folder), <c>plan_changed</c> (details: the fingerprint now), <c>job_unavailable</c>; 503
/// <c>archive_offline</c>. Audited; the job tells every sequence it changes as <c>sequenceChanged</c>
/// (<c>reorganized</c>).
/// (<c>reorganized</c>). A dry run's plan is kept a few minutes while the index stays as it was
/// (<see cref="ReorganizePlanCache"/>): the same dry run again, and its confirmation, take it instead of planning again.
/// </summary>
private static async Task<IResult> ReorganizeAsync(HttpContext context, Caller caller, ReorganizeRequest request, ChronosDatabase db, ServerArchive archive,
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit)
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, ReorganizePlanCache plans)
{
var ct = context.RequestAborted;
if (request.All == (request.SequenceIds is not null) || request.SequenceIds is { Count: 0 or > LibraryEndpoints.MaxBulkIds })
@@ -152,10 +153,13 @@ public static class ArchiveEndpoints
if (!request.DryRun && string.IsNullOrWhiteSpace(request.Fingerprint)) return ApiErrors.BadRequest(context, "bad_request", "fingerprint");
if (ArchiveService.GetAvailability(archive.Current) != ArchiveAvailability.Online)
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
var sequences = request.All ? null : request.SequenceIds;
// Stamped before planning: a change committed while the plan is made gives the next request another key.
var key = ReorganizePlanCache.Key.Of(archive.Id, await db.Sequences.GetStampAsync(archive.Id, ct), sequences, templates);
RenamePlan plan;
try
{
plan = await LibraryEndpoints.Engine(archives, db).PlanReorganizeAsync(archive.Id, request.All ? null : request.SequenceIds, templates, ct);
plan = await plans.GetOrPlanAsync(key, () => LibraryEndpoints.Engine(archives, db).PlanReorganizeAsync(archive.Id, sequences, templates, ct));
}
catch (NestedRenameException ex)
{
@@ -170,6 +174,7 @@ public static class ArchiveEndpoints
if (!string.Equals(fingerprint, request.Fingerprint, StringComparison.Ordinal)) return ApiErrors.Conflict(context, "plan_changed", fingerprint);
if (!host.CanRun(JobKind.Move)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.Move));
var job = await host.Engine.EnqueueAsync(ArchiveJobs.Reorganize(archive.Current, plan, templates), caller.Id, ct);
plans.Forget(key);
await audit.WriteAsync(caller, context, "archive_reorganized", "job", job.Id.ToString(),
$"{plan.Steps.Count(s => s.Changes)} · {plan.Steps.Sum(s => s.FileChanges)} · {templates.Folder} · {templates.File} · {templates.Title}");
return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);