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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 10:54:23 +00:00
1 parent 886132b5bb
commit 568ba4f21f
2 files changed
+74 -8

No files matched your search

@@ -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);
}
}