From 92f4403c761131928abd1697c2178982dcef53a4 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 23:29:55 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Archiving/ArchiveIndexer.cs | 31 +++++++++-- .../Archiving/PartialRowWriterTests.cs | 51 +++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/Titano.Data/Archiving/ArchiveIndexer.cs b/src/Titano.Data/Archiving/ArchiveIndexer.cs index 1ea5a444..d0619248 100644 --- a/src/Titano.Data/Archiving/ArchiveIndexer.cs +++ b/src/Titano.Data/Archiving/ArchiveIndexer.cs @@ -105,9 +105,10 @@ public sealed class ArchiveIndexer /// /// 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 (). 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, ), is written again one + /// version past the row, so editors and clients holding the row's version see the change. /// /// The folder as the scan listed it before reading without the sequence /// lease; null when the caller holds the lease and read the sidecar under it. @@ -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 /// private async Task 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); + + /// + /// Whether the index row holds other frames than : 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 (), 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. + /// + private static async Task 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(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); + } /// A sidecar for a folder that has none; ids of a sequence the index already knew at this folder are kept. private async Task AdoptAsync(SequencePaths sequence, Locations locations, CancellationToken cancellationToken) diff --git a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs index 0d0dee07..5014e921 100644 --- a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs +++ b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs @@ -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 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 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() {