From 568ba4f21ff5d94db52a8ef72cf442ee687bb641 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 10:54:23 +0000 Subject: [PATCH] fix(archive): scans move project frame positions under the sequence lease Reconcile and a scan that finds a sidecar with other frames than the index now read the sidecar and index it while holding the sequence lease, the one an editor holds while it saves its project. The save then either lands before the move, which moves its positions, or reads the moved settings and merges onto them; a save made in between can no longer write positions of the old frame order back without a version conflict. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Archiving/ArchiveIndexer.cs | 49 ++++++++++++++++--- .../Archiving/ArchiveIngestTests.cs | 33 +++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/Titano.Data/Archiving/ArchiveIndexer.cs b/src/Titano.Data/Archiving/ArchiveIndexer.cs index 5561c3d6..a85265da 100644 --- a/src/Titano.Data/Archiving/ArchiveIndexer.cs +++ b/src/Titano.Data/Archiving/ArchiveIndexer.cs @@ -130,10 +130,30 @@ public sealed class ArchiveIndexer var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); var folder = paths.Sequence(sequence.Folder); - var manifest = await _writer.UpdateAsync(paths, sequence.Id, sequence.Folder, m => SequenceAdopter.Reconcile(folder, m, cancellationToken), - cancellationToken).ConfigureAwait(false); - await SyncAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false); - return manifest; + return await UpdateAndSyncAsync(archive, paths, folder, sequence.Id, m => SequenceAdopter.Reconcile(folder, m, cancellationToken), cancellationToken) + .ConfigureAwait(false); + } + + /// + /// Applies to the sidecar (null writes nothing) and indexes the result, both under the sequence + /// lease. Frames inserted before indexed ones move the project's positions () without a + /// new settings version; an editor saving its project holds the same lease, so it reads them moved and merges onto + /// them rather than writing its old positions back. + /// + private async Task UpdateAndSyncAsync(Archive archive, ArchivePaths paths, SequencePaths sequence, Guid sequenceId, + Func edit, CancellationToken cancellationToken) + { + await using (await _writer.LockAsync(paths, sequenceId, cancellationToken).ConfigureAwait(false)) + { + var manifest = SequenceWriter.ReadLocked(sequence, sequenceId); + if (edit(manifest) is { } updated) + { + ManifestStore.WriteSequence(sequence.ManifestPath, updated); + manifest = updated; + } + await SyncAsync(archive, sequence, manifest, cancellationToken).ConfigureAwait(false); + return manifest; + } } /// Indexes one folder; returns the sidecar bytes when they were read and not rewritten since (for the snapshot). @@ -169,20 +189,33 @@ public sealed class ArchiveIndexer } if (reconcile || File.Exists(sequence.IngestJournalPath)) { - var before = manifest; // Photos added, gone or back: the light analysis no longer describes the frames, so the scan's follow-up runs it again. - manifest = await _writer.UpdateAsync(paths, manifest.Id, folder.RelativeFolder, + manifest = await UpdateAndSyncAsync(archive, paths, sequence, manifest.Id, m => SequenceAdopter.Reconcile(sequence, m, cancellationToken) is { } reconciled ? reconciled with { Status = reconciled.Status & ~SequenceStatus.AnalyzedLight } : null, cancellationToken).ConfigureAwait(false); - if (!ReferenceEquals(before, manifest)) bytes = null; + bytes = null; + } + else if (await MayMovePositionsAsync(manifest, cancellationToken).ConfigureAwait(false)) + { + // Another process changed the frames: the sidecar is read again under the lease the project moves under. + manifest = await UpdateAndSyncAsync(archive, paths, sequence, manifest.Id, _ => null, cancellationToken).ConfigureAwait(false); + bytes = null; + } + else + { + await SyncAsync(archive, sequence, manifest, cancellationToken).ConfigureAwait(false); } - await SyncAsync(archive, sequence, manifest, cancellationToken).ConfigureAwait(false); locations.Set(manifest.Id, folder.RelativeFolder); return bytes; } + /// The index holds a project for this sequence and other frames than the sidecar: its positions may move. + private async Task MayMovePositionsAsync(SequenceManifest manifest, CancellationToken cancellationToken) => + await _db.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false) is { SettingsJson: not null } indexed + && indexed.FramesVersion != manifest.Versions.Frames; + /// 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/ArchiveIngestTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveIngestTests.cs index a944c0d9..548f799c 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveIngestTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveIngestTests.cs @@ -298,4 +298,37 @@ public sealed class ArchiveIngestTests(TestContext context) Assert.Contains("futureOption", settings); Assert.True(JsonNode.DeepEquals(JsonNode.Parse(settings), JsonNode.Parse(File.ReadAllText(folder.ProjectPath))), "project.json mirrors the index"); } + + [Test] + public async Task AScanMovesTheProjectPositionsOnlyBetweenProjectSaves() + { + await using var fx = await CreateAsync(context); + var late = await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(1), 1)), fx.Ct); + await fx.Service.Ingest.CommitAsync(Request(fx, fx.Stage(Start.AddMinutes(2), 2)), fx.Ct); + var id = late.SequenceId; + var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder); + var version = (await fx.Db.Sequences.EditAsync(id, new SequenceEdit { SettingsJson = "{ \"excludedFrames\": [1] }" }, null, fx.Ct)).Sequence!.SettingsVersion; + + // Another process files a photo captured before both into the sidecar; this index learns of it from a scan. + var photos = ArchiveScanner.PhotoDirectory(folder); + var stamp = Directory.GetLastWriteTimeUtc(photos); + File.WriteAllBytes(folder.File("originals/early.JPG"), Photos.Jpeg(Start, 3)); + ManifestStore.WriteSequence(folder.ManifestPath, SequenceAdopter.Reconcile(folder, ManifestStore.ReadSequence(folder.ManifestPath), fx.Ct)!); + Directory.SetLastWriteTimeUtc(photos, stamp); + + // An editor saving the project holds the sequence lease (LocalSequenceSource.SaveProjectAsync). + var save = await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{id:N}", "editor", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct); + var scan = Task.Run(() => fx.ScanAsync()); + await Task.Delay(500, fx.Ct); + Assert.False(scan.IsCompleted, "the scan waits for the save"); + // The save stores the positions its editor knew, as of the frames before the scan. + Assert.Equal(SequenceEditOutcome.Updated, + (await fx.Db.Sequences.EditAsync(id, new SequenceEdit { SettingsJson = "{ \"excludedFrames\": [0, 1] }" }, version, fx.Ct)).Outcome); + save.Dispose(); + Assert.Equal(1, (await scan).Indexed); + + var row = (await fx.Db.Sequences.GetAsync(id))!; + Assert.Equal(3, row.FrameCount); + Assert.SequenceEqual([1, 2], ProjectSettings.FromJson(row.SettingsJson!).ExcludedFrames); + } }