fix(archive): verify writes only the integrity flags of files still as checked

A verify's flush replaced every flag of a pending frame, in the sidecar
and the index, with the flags it computed from the frame as read without
the lease, possibly hours earlier. A preview-only or undecodable tier, a
duplicate flag or a repair recorded meanwhile was reverted, and a photo
an upload repaired during the run was flagged missing again, so the next
copy moved the good file aside.

Under the lease the flush now merges only Missing and Corrupt into the
frame's current flags, and the index sets and clears only those bits with
the hash, after locking the sequence row. A check whose sidecar record or
file (size and modification time taken before it was read) changed since
is left for the next verify.

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:34:47 +00:00
1 parent e369d1d20a
commit 1b84d74486
3 files changed
+121 -16

No files matched your search

@@ -273,6 +273,63 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
Assert.Equal(sequence.FrameCount + 1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.FrameCount);
}
[Test]
public async Task AVerifyKeepsTheTiersRecordedWhileItRan()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
// The first verify of photos a scan adopted hashes them all; a thumbnail pass records a preview-only frame meanwhile.
var recorded = false;
var report = await fx.Service.Verifier.VerifyAsync(fx.Archive, [sequence.Id], default, (_, _, _) => ValueTask.CompletedTask, async (_, _) =>
{
if (recorded) return;
recorded = true;
await Tiers(fx).RecordAsync(fx.Archive, sequence, Preview(frames[0].Id), proxies: false, fx.Ct);
}, fx.Ct);
Assert.Equal(3L, report.Hashed);
// The sidecar and the index keep the tier's flag and count beside the hashes.
var indexed = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence("a").ManifestPath);
Assert.SequenceEqual([FrameFlags.PreviewOnly, FrameFlags.None, FrameFlags.None], indexed.Select(f => f.Flags));
Assert.SequenceEqual([FrameFlags.PreviewOnly, FrameFlags.None, FrameFlags.None], sidecar.Frames.Select(f => f.Flags));
Assert.True(indexed.All(f => f.Sha256 is not null), "hashed");
Assert.SequenceEqual(sidecar.Frames.Select(f => f.Sha256), indexed.Select(f => f.Sha256));
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
}
[Test]
public async Task AVerifyLeavesAFrameAnUploadRepairedWhileItRanUnflagged()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
// Hashed by a first verify, so an upload of the same photo repairs the frame once its file is gone.
await fx.Service.Verifier.VerifyAsync(fx.Archive, [sequence.Id], default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct);
var folder = fx.Paths.Sequence("a");
var lost = ManifestStore.ReadSequence(folder.ManifestPath).Frames[1];
File.Delete(folder.File(lost.File));
IngestRequest Upload((string Path, long Size, string Sha256) staged) =>
new(fx.Archive.Id, sequence.Id, null, null, staged.Path, lost.File, staged.Size, staged.Sha256, null, null);
// The verify finds the file missing, and an upload puts it back before the verify writes what it found.
var checkedFrames = 0;
var report = await fx.Service.Verifier.VerifyAsync(fx.Archive, [sequence.Id], default, (_, _, _) => ValueTask.CompletedTask, async (_, _) =>
{
if (++checkedFrames != 2) return;
Assert.True((await fx.Service.Ingest.CommitAsync(Upload(fx.Stage(Start.AddSeconds(10), seed: 2)), fx.Ct)).Repaired, "repaired");
}, fx.Ct);
Assert.SequenceEqual([(lost.Id, FrameHealth.Missing)], report.Problems.Select(p => (p.FrameId, p.Health)));
// The frame stays unflagged and the sequence verified, so the next copy is a duplicate and the file stays in place.
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.GetAsync(lost.Id, fx.Ct))!.Flags);
Assert.Equal(FrameFlags.None, ManifestStore.ReadSequence(folder.ManifestPath).Frames[1].Flags);
Assert.True((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.Verified), "verified");
Assert.True((await fx.Service.Ingest.CommitAsync(Upload(fx.Stage(Start.AddSeconds(10), seed: 2)), fx.Ct)).Duplicate, "duplicate");
Assert.Empty(Directory.GetFiles(folder.Folder, "*.damaged-*"));
}
[Test]
public async Task AVerifyStampedWhileASettingsEditCommitsLeavesTheEdit()
{