fix(archive): a scan never writes an older sidecar over a newer index commit

The scan's plain branch indexed a sidecar read without the lease. When an
upload or a partial writer (decode tiers, a verify) committed between that
read and the scan's transaction, the scan wrote its older copy over the
newer row and frames (dropping the uploaded frame rows), then recorded
the folder's current stamps, so incremental scans skipped the regression.

Under the row lock, SyncAsync now refuses an unleased read when the
sidecar no longer has the stamp the scan listed or the row holds another
frames version, and the scan reads the sidecar again under the lease. An
unleased sync records the stamps it was checked against, and the index
snapshot files each sidecar under the stamp it was read at. Under the
lease, a sidecar behind the locked row (an older copy put back) is
written again one frames version past the row, so the version never goes
back. Statuses cleared in this host's row only (Uncovered) now pass as a
parameter, so that rewrite never clears them in the shared sidecar.

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 21:35:24 +00:00
1 parent 1b84d74486
commit b187e6ce02
3 files changed
+154 -14

No files matched your search

@@ -1,5 +1,6 @@
using Titano.Core.Archiving;
using Titano.Core.Imaging;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
@@ -40,6 +41,17 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
private static Dictionary<Guid, FileTier> Preview(Guid frameId) =>
new() { [frameId] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", null, 48, 32) };
/// <summary>
/// Retitles the sequence in <paramref name="folder"/> in its sidecar alone, as another PC does: the next scan reads the
/// sidecar and indexes it without the lease. Returns the sidecar as written.
/// </summary>
private static SequenceManifest Retitle(SequencePaths folder)
{
var retitled = ManifestStore.ReadSequence(folder.ManifestPath) with { Title = "Ridge" };
ManifestStore.WriteSequence(folder.ManifestPath, retitled);
return retitled;
}
/// <summary>Commits <paramref name="tx"/> once the writer under test had time to read the row and wait for it.</summary>
private static async Task CommitLaterAsync(TitanoTransaction tx, CancellationToken cancellationToken)
{
@@ -330,6 +342,100 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
Assert.Empty(Directory.GetFiles(folder.Folder, "*.damaged-*"));
}
[Test]
public async Task AScanThatReadTheSidecarBeforeAnUploadCommittedReadsItAgainUnderTheLease()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var folder = fx.Paths.Sequence("a");
var read = Retitle(folder);
// The upload's transaction holds the row (and SQLite's write lock) when the scan, which read the sidecar, reaches it.
Task<ScanSummary> scan;
SequenceManifest uploaded;
await using (var tx = await fx.Db.BeginAsync(fx.Ct))
{
var row = await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct);
scan = fx.ScanAsync();
await Task.WhenAny(scan, Task.Delay(500, fx.Ct));
Assert.False(scan.IsCompleted, "the scan waits for the row");
// Under the lease, the upload filed its photo and wrote the sidecar; this transaction indexes them.
var photo = folder.File("IMG_0004.JPG");
await File.WriteAllBytesAsync(photo, Photos.Jpeg(Start.AddMinutes(1), 90), fx.Ct);
var frame = SequenceFrames.FromMetadata(Guid.CreateVersion7(), "IMG_0004.JPG", "IMG_0004.JPG", new FileInfo(photo).Length, null,
MetadataReader.Read(photo, fx.Ct));
uploaded = SequenceFrames.Insert(read, [frame]);
ManifestStore.WriteSequence(folder.ManifestPath, uploaded);
await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(uploaded, fx.Archive.Id, folder, row), fx.Ct);
await tx.Frames.BulkInsertAsync([SequenceMapping.ToFrame(uploaded.Frames[^1], sequence.Id, folder)], fx.Ct);
await tx.CommitAsync(fx.Ct);
}
Assert.SequenceEqual([sequence.Id], (await scan).Sequences);
// The scan wrote nothing from its older read: the photo, its frames version and the title are indexed.
var indexed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal((4, uploaded.Versions.Frames, "Ridge"), (indexed.FrameCount, indexed.FramesVersion, indexed.Title));
Assert.SequenceEqual(uploaded.Frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).Select(f => f.Id));
Assert.Empty((await fx.ScanAsync()).Sequences);
}
[Test]
public async Task AScanThatReadTheSidecarBeforeATierWasRecordedReadsItAgainUnderTheLease()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var folder = fx.Paths.Sequence("a");
var read = Retitle(folder);
// The tier writer's transaction holds the row when the scan, which read the sidecar, reaches it.
Task<ScanSummary> scan;
await using (var tx = await fx.Db.BeginAsync(fx.Ct))
{
await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct);
scan = fx.ScanAsync();
await Task.WhenAny(scan, Task.Delay(500, fx.Ct));
Assert.False(scan.IsCompleted, "the scan waits for the row");
// Under the lease, the writer gave the first frame its preview tier in the sidecar; this transaction indexes it.
var tier = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", null, 48, 32);
var tiered = SequenceFrames.Update(read, new Dictionary<Guid, Func<SidecarFrame, SidecarFrame>> { [read.Frames[0].Id] = f => SequenceFrames.WithTier(f, tier) });
ManifestStore.WriteSequence(folder.ManifestPath, tiered);
var first = tiered.Frames[0];
await tx.Sequences.RecordTiersAsync(sequence.Id, SequenceStatus.None, 1, fx.Ct);
await tx.Frames.UpdateTiersAsync([new FrameTierFields(first.Id, first.Flags, first.PreviewW ?? 0, first.PreviewH ?? 0)], fx.Ct);
await tx.CommitAsync(fx.Ct);
}
Assert.SequenceEqual([sequence.Id], (await scan).Sequences);
// The scan wrote nothing from its older read: the tier stays, and the title is indexed.
var indexed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal(("Ridge", 1), (indexed.Title, indexed.PreviewOnlyCount));
Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[0].Flags);
Assert.Empty((await fx.ScanAsync()).Sequences);
}
[Test]
public async Task AnOlderSidecarPutBackIsIndexedPastTheFramesVersionTheIndexHolds()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var folder = fx.Paths.Sequence("a");
var older = await File.ReadAllBytesAsync(folder.ManifestPath, fx.Ct);
var staged = fx.Stage(Start.AddMinutes(1), seed: 90);
await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, sequence.Id, null, null, staged.Path, "IMG_0004.JPG", staged.Size, staged.Sha256, null, null),
fx.Ct);
var uploaded = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
// A file-sync tool puts the sidecar back as it was before the upload; the photo stays.
await File.WriteAllBytesAsync(folder.ManifestPath, older, fx.Ct);
Assert.SequenceEqual([sequence.Id], (await fx.ScanAsync()).Sequences);
// Indexed as the sidecar lists it, one frames version past the index's, in both places.
var indexed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal((3, uploaded.FramesVersion + 1), (indexed.FrameCount, indexed.FramesVersion));
Assert.Equal(indexed.FramesVersion, ManifestStore.ReadSequence(folder.ManifestPath).Versions.Frames);
Assert.Empty((await fx.ScanAsync()).Sequences);
}
[Test]
public async Task AVerifyStampedWhileASettingsEditCommitsLeavesTheEdit()
{