fix(archive): scans see frames another writer changed; partial writers keep the row and the lease

A desktop sharing the archive that imports into an existing sequence lists the
new frames in the sidecar itself and keeps its ThumbsBuilt and ProxiesBuilt.
The server scan cleared the flags only when SequenceAdopter.Reconcile found
unindexed photos, so it queued no image pass: the new frames, and every frame
they renumbered, answered 404 on /thumb and /proxy until a pass was forced.
A scan now also clears AnalyzedLight, ThumbsBuilt and ProxiesBuilt when the
sidecar's frames version differs from the index row's, decided under the
sequence lease, and its follow-ups queue the light analysis and both passes.

Writers that change only part of a sequence wrote the whole index row back
from a sidecar read under a lease they had already released (decode tiers) or
without any (verify stamp): an ingest or a PATCH committed in between was
undone, and a row written from a sidecar the index had not synced claimed its
frames version, hiding the change from the scan. FrameTiers.RecordAsync and
ArchiveVerifier now write the sidecar and only their own columns
(SequenceRepository.RecordTiersAsync, RecordVerifiedAsync) under one lease.
They and the image passes record the folder's scan stamps only
while the index holds the sidecar's frames version
(ArchiveIndexer.RecordStampsInStepAsync), so the next scan still sees frames
another process added.

An image pass decided ThumbsBuilt/ProxiesBuilt from the sidecar it read at
its start and set the flag again after a scan had cleared it for photos added
meanwhile, so no pass was queued for them. MarkAsync now holds the lease for
the sidecar and the index, and sets the flag only while the sidecar lists the
photos the pass read at the same indexes; otherwise it clears it.

A rebuild deleted every row whose folder was not among those listed when it
began, so a sequence that got its first frame or was created by an upload
session while it ran lost its row (404 for the Uploader, a second '(2)'
sequence for the session). A row is now checked again under its lease, as it
and its folder are then, before it goes.

Tests: a desktop's ArchiveIngest commit followed by a server scan (Data and
Server, /thumb and /proxy answer 200 for every frame); a thumbnail pass with
a scan's reconcile in between; tier records and a verify stamp interleaved
with a PATCH and with another writer's frames, on SQLite and PostgreSQL; a
rebuild during which uploads place and create sequences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 19:21:44 +00:00
1 parent aa36768860
commit 48ae3efd22
11 files changed
+550 -75

No files matched your search

@@ -175,6 +175,101 @@ public sealed class ArchiveJobTests(TestContext context)
Assert.Count(7, Directory.GetFiles(fx.Paths.Sequence("a").Proxies));
}
[Test]
public async Task AServerScanQueuesTheImagesOfFramesAnotherWriterListedInTheSidecarItself()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await using var engine = ServerEngine(fx);
await engine.StartAsync(fx.Ct);
var scan = await engine.EnqueueAsync(ArchiveJobs.Scan(fx.Archive), cancellationToken: fx.Ct);
await fx.WaitForAsync(scan.Id, JobState.Done);
var first = new HashSet<Guid>();
foreach (var kind in new[] { JobKind.Thumbs, JobKind.Proxies })
{
foreach (var pass in (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items)
{
await fx.WaitForAsync(pass.Id, JobState.Done);
first.Add(pass.Id);
}
}
var a = (await LocationsAsync(fx)).Single(l => l.Folder == "a");
Assert.True(Built((await fx.Db.Sequences.GetAsync(a.Id))!.Status), "built");
// A desktop sharing the archive imports a photo taken before the others into the sequence: it files the photo and
// lists it in the sidecar itself, whose statuses it keeps, so the scan finds no photo to reconcile.
var (desktop, archive) = await fx.OtherWriterAsync();
var staged = fx.Stage(Start.AddMinutes(-5), seed: 90);
await desktop.Ingest.CommitAsync(new IngestRequest(archive.Id, a.Id, null, null, staged.Path, "IMG_0000.JPG", staged.Size, staged.Sha256, null, null),
fx.Ct);
var sidecar = fx.Paths.Sequence("a").ManifestPath;
Assert.True(Built(ManifestStore.ReadSequence(sidecar).Status), "the other writer keeps the statuses");
var again = await engine.EnqueueAsync(ArchiveJobs.Scan(fx.Archive), cancellationToken: fx.Ct);
await fx.WaitForAsync(again.Id, JobState.Done);
// The frames changed behind the index: the statuses no longer hold, and the sequence gets a pass of each kind.
foreach (var kind in new[] { JobKind.Thumbs, JobKind.Proxies })
{
var pass = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items.Single(j => !first.Contains(j.Id));
Assert.SequenceEqual([a.Id], pass.TargetIds);
await fx.WaitForAsync(pass.Id, JobState.Done);
}
var row = (await fx.Db.Sequences.GetAsync(a.Id))!;
Assert.Equal(7, row.FrameCount);
Assert.True(Built(row.Status), "built again");
// Every frame has its own thumbnail and proxy: the new first one, and the others it renumbered.
var folder = fx.Paths.Sequence("a");
using var pack = ThumbPackReader.Open(folder.ThumbsPackPath);
foreach (var frame in ManifestStore.ReadSequence(sidecar).Frames)
{
Assert.True(pack.Entries.GetValueOrDefault(frame.I) is { } entry
&& entry.SourceHash.AsSpan().SequenceEqual(ThumbPack.Fingerprint(frame.Sha256, frame.File, frame.Size)), $"thumbnail of {frame.File}");
Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(frame.I), frame.CapturedAt, frame.W, frame.H, null), $"proxy of {frame.File}");
}
}
[Test]
public async Task AnImagePassMarksTheSequenceBuiltOnlyWhenItsSidecarStillListsTheFramesThePassRead()
{
await using var fx = await CreateAsync(context);
// The pass commits its pack and checkpoints after 200 frames.
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 201);
await fx.ScanAsync();
var a = (await LocationsAsync(fx)).Single();
var sequence = (await fx.Db.Sequences.GetAsync(a.Id))!;
// While it runs, another writer copies in a photo taken before the others, and a scan reconciles it.
var scanned = false;
var result = await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, sequence, false, 0, async (_, _) =>
{
if (scanned) return;
scanned = true;
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start.AddMinutes(-5), 1, firstSeed: 900, firstNumber: 900);
Assert.SequenceEqual([a.Id], (await fx.ScanAsync()).Sequences);
}, fx.Ct);
// The pass drew every frame it read, but not the new photo: the sequence is not built.
Assert.True(scanned, "scanned during the pass");
Assert.Equal((201, 0), (result.Built, result.Undecodable));
var row = (await fx.Db.Sequences.GetAsync(a.Id))!;
Assert.Equal(202, row.FrameCount);
Assert.False(row.Status.HasFlag(SequenceStatus.ThumbsBuilt), row.Status.ToString());
Assert.False(ManifestStore.ReadSequence(fx.Paths.Sequence("a").ManifestPath).Status.HasFlag(SequenceStatus.ThumbsBuilt), "sidecar flag");
// So a scan's follow-up (here a resumed one, which lists the whole archive) queues the next pass, which makes them.
await using var engine = ServerEngine(fx);
var resumed = await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.Scan(fx.Archive), "after:a");
await engine.StartAsync(fx.Ct);
await fx.WaitForAsync(resumed.Id, JobState.Done);
var pass = (await fx.Db.Jobs.ListAsync(null, JobKind.Thumbs, cancellationToken: fx.Ct)).Items.Single();
Assert.SequenceEqual([a.Id], pass.TargetIds);
await fx.WaitForAsync(pass.Id, JobState.Done);
Assert.True((await fx.Db.Sequences.GetAsync(a.Id))!.Status.HasFlag(SequenceStatus.ThumbsBuilt), "built");
using var pack = ThumbPackReader.Open(fx.Paths.Sequence("a").ThumbsPackPath);
Assert.Count(202, pack.Entries);
}
[Test]
public async Task AFolderGoneBeforeItsImagePassesFailsThemAloneAndTheOtherSequencesGetTheirImages()
{