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

+24 -14
View File
@@ -61,6 +61,12 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi
/// </summary>
/// <param name="proxies">Whether every such frame follows its proxy even where no tier is recorded (analyses and the
/// proxy pass); otherwise only the frames in <paramref name="tiers"/> do.</param>
/// <remarks>
/// The sidecar and the index are written under one lease, the index from the sidecar read under it, and the index
/// row gets only what tiers change (<see cref="SequenceRepository.RecordTiersAsync"/>): a whole row written back
/// would undo what another writer committed meanwhile, an ingest's frames or a settings edit (the server's PATCH
/// takes no lease).
/// </remarks>
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
public async Task<IReadOnlyList<SidecarFrame>> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary<Guid, FileTier> tiers,
bool proxies, CancellationToken cancellationToken)
@@ -69,12 +75,12 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
var folder = paths.Sequence(sequence.Folder);
var changed = new List<SidecarFrame>();
ProxyPolicy? policy = null;
var manifest = await writer.UpdateAsync(paths, sequence.Id, sequence.Folder, m =>
await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false))
{
changed.Clear();
var m = SequenceWriter.ReadLocked(folder, sequence.Id);
var changed = new List<SidecarFrame>();
var gained = false;
ProxyPolicy? policy = null;
var frames = new SidecarFrame[m.Frames.Count];
for (var i = 0; i < frames.Length; i++)
{
@@ -96,21 +102,25 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi
frames[i] = updated;
changed.Add(updated);
}
if (changed.Count == 0) return null;
return m with
if (changed.Count == 0) return m.Frames;
var manifest = m with
{
Frames = frames,
Status = gained ? m.Status | SequenceStatus.HasWarnings : m.Status,
UpdatedAt = DateTimeOffset.UtcNow,
};
}, cancellationToken).ConfigureAwait(false);
if (changed.Count == 0) return manifest.Frames;
await db.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken)
.ConfigureAwait(false);
if (await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) is { } existing)
await db.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, folder, existing), cancellationToken).ConfigureAwait(false);
await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false);
return manifest.Frames;
ManifestStore.WriteSequence(folder.ManifestPath, manifest);
await using (var tx = await db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
await tx.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken)
.ConfigureAwait(false);
await tx.Sequences.RecordTiersAsync(sequence.Id, gained ? SequenceStatus.HasWarnings : SequenceStatus.None,
frames.Count(f => (f.Flags & FrameFlags.PreviewOnly) != 0), cancellationToken).ConfigureAwait(false);
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
}
await indexer.RecordStampsInStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false);
return manifest.Frames;
}
}
/// <summary>