fix(archive): an equal frames version over other frames counts as a frames change

Each index of a shared archive bumps a sidecar put back behind its own row
to one version past that row, which can be the version another index holds
for other frames (a desktop rebuilding a damaged sidecar while the server is
one upload ahead). That index then took the sidecar's frames as unchanged:
it kept the project's positions and what covered the frames, and clients and
editors holding that version saw nothing. When the versions match, a scan
now also compares the row's frame ids and order with the sidecar's; other
frames are read again under the lease and written one version past the row.

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 23:29:55 +00:00
1 parent 06a1db7ae1
commit 92f4403c76
2 files changed
+77 -5

No files matched your search

+26 -5
View File
@@ -105,9 +105,10 @@ public sealed class ArchiveIndexer
/// <summary>
/// Writes a sequence's sidecar into the index (row, frames, session keys) and records the folder stamps. Frames
/// inserted before indexed ones take the project's frame positions along (<see cref="ProjectPositions"/>). The frames
/// version never goes back: a sidecar read under the lease that is behind the locked row (an older copy put back by
/// hand or by a file-sync tool) is written again one version past the row, so editors and clients holding the row's
/// version see the change.
/// version never goes back, nor names other frames than it did: a sidecar read under the lease that is behind the
/// locked row (an older copy put back by hand or by a file-sync tool), or at the row's version over other frames
/// (another index of the archive bumped a copy put back to it, <see cref="OtherFramesAsync"/>), is written again one
/// version past the row, so editors and clients holding the row's version see the change.
/// </summary>
/// <param name="listed">The folder as the scan listed it before reading <paramref name="manifest"/> without the sequence
/// lease; null when the caller holds the lease and read the sidecar under it.</param>
@@ -131,7 +132,11 @@ public sealed class ArchiveIndexer
if (listed is not null && (FileStamp.Of(sequence.ManifestPath) != listed.Sidecar
|| (existing is not null && existing.FramesVersion != manifest.Versions.Frames)))
return null;
if (existing is not null && existing.FramesVersion > manifest.Versions.Frames)
// Under the lease, the row's own version may also name other frames: another index of the archive bumped a copy
// put back to it (OtherFramesAsync). A caller without the lease compared them before its read (FramesMovedAsync).
if (existing is not null && (existing.FramesVersion > manifest.Versions.Frames
|| (listed is null && existing.FramesVersion == manifest.Versions.Frames
&& await OtherFramesAsync(tx, existing, manifest, cancellationToken).ConfigureAwait(false))))
{
manifest = manifest with
{
@@ -387,7 +392,23 @@ public sealed class ArchiveIndexer
/// </summary>
private async Task<bool> FramesMovedAsync(SequenceManifest manifest, CancellationToken cancellationToken) =>
await _db.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false) is { } indexed
&& indexed.FramesVersion != manifest.Versions.Frames;
&& await OtherFramesAsync(_db, indexed, manifest, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Whether the index row <paramref name="indexed"/> holds other frames than <paramref name="manifest"/>: another frames
/// version, or the same version over other frames (ids, or their order). Each index of a shared archive bumps a sidecar
/// put back behind its own row one version past that row (<see cref="SyncAsync"/>), and another index may hold that
/// very version for the frames as they were before: that index still sees the change, and bumps past it in turn.
/// </summary>
private static async Task<bool> OtherFramesAsync(TitanoRepositories db, Sequence indexed, SequenceManifest manifest, CancellationToken cancellationToken)
{
if (indexed.FramesVersion != manifest.Versions.Frames) return true;
var rows = await db.Frames.ListPositionsAsync(indexed.Id, cancellationToken).ConfigureAwait(false);
if (rows.Count != manifest.Frames.Count) return true;
var listed = new Dictionary<int, Guid>(manifest.Frames.Count);
foreach (var frame in manifest.Frames) listed[frame.I] = frame.Id;
return rows.Any(row => !listed.TryGetValue(row.Index, out var id) || id != row.FrameId);
}
/// <summary>A sidecar for a folder that has none; ids of a sequence the index already knew at this folder are kept.</summary>
private async Task<SequenceManifest> AdoptAsync(SequencePaths sequence, Locations locations, CancellationToken cancellationToken)
@@ -508,6 +508,57 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
Assert.Empty((await fx.ScanAsync()).Sequences);
}
[Test]
public async Task AFramesVersionAnotherIndexReachedForOtherFramesIsIndexedPastIt()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var folder = fx.Paths.Sequence("a");
async Task<Sequence> UploadAsync(string name, int minute)
{
var staged = fx.Stage(Start.AddMinutes(minute), seed: 90 + minute);
await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, sequence.Id, null, null, staged.Path, name, staged.Size, staged.Sha256, null, null),
fx.Ct);
return (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
}
await UploadAsync("IMG_0004.JPG", 1);
// A desktop sharing this server's archive, with its own index of the four photos.
await using var desktopDb = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(Context.CreateTempDirectory("desktop"), "titano.db")), fx.Ct);
var desktop = new ArchiveService(desktopDb, new ArchiveServiceOptions { Owner = "desktop", LeaseWait = TimeSpan.FromSeconds(20) });
var desktopArchive = await desktop.OpenAsync(fx.Archive.RootPath, fx.Ct);
Task<ScanSummary> DesktopScanAsync() => desktop.Indexer.ScanAsync(desktopArchive, ScanMode.Incremental, null, _ => ValueTask.CompletedTask, fx.Ct);
await DesktopScanAsync();
// The server takes a fifth photo before the desktop scans again.
var uploaded = await UploadAsync("IMG_0005.JPG", 2);
Assert.Equal(uploaded.FramesVersion - 1, (await desktopDb.Sequences.GetAsync(sequence.Id, fx.Ct))!.FramesVersion);
// The sidecar is damaged; the desktop's scan rebuilds it from the photos at the first version, the fifth photo under
// an id of its own, and writes it one version past its own row: the version the server holds for the photo as it
// filed it.
await File.WriteAllTextAsync(folder.ManifestPath, "{", fx.Ct);
Assert.SequenceEqual([sequence.Id], (await DesktopScanAsync()).Sequences);
var rebuilt = ManifestStore.ReadSequence(folder.ManifestPath);
Assert.Equal(uploaded.FramesVersion, rebuilt.Versions.Frames);
Assert.NotEqual((await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[^1].Id, rebuilt.Frames[^1].Id);
// The server's scan finds other frames at the version it holds: it indexes them one version past it, in both places,
// so an editor holding that version for the photo as it was is refused.
Assert.SequenceEqual([sequence.Id], (await fx.ScanAsync()).Sequences);
var indexed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal(uploaded.FramesVersion + 1, indexed.FramesVersion);
Assert.Equal(indexed.FramesVersion, ManifestStore.ReadSequence(folder.ManifestPath).Versions.Frames);
Assert.SequenceEqual(rebuilt.Frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).Select(f => f.Id));
var edit = await fx.Db.Sequences.EditAsync(sequence.Id, new SequenceEdit { Rating = 2 }, null, uploaded.FramesVersion, fx.Ct);
Assert.Equal(SequenceEditOutcome.Conflict, edit.Outcome);
// The desktop follows, and neither index finds anything more to do.
Assert.SequenceEqual([sequence.Id], (await DesktopScanAsync()).Sequences);
Assert.Equal(indexed.FramesVersion, (await desktopDb.Sequences.GetAsync(sequence.Id, fx.Ct))!.FramesVersion);
Assert.Empty((await fx.ScanAsync()).Sequences);
Assert.Empty((await DesktopScanAsync()).Sequences);
}
[Test]
public async Task AVerifyStampedWhileASettingsEditCommitsLeavesTheEdit()
{