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

+41 -8
View File
@@ -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);
}
/// <summary>
/// Applies <paramref name="edit"/> 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 (<see cref="ProjectPositions"/>) 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.
/// </summary>
private async Task<SequenceManifest> UpdateAndSyncAsync(Archive archive, ArchivePaths paths, SequencePaths sequence, Guid sequenceId,
Func<SequenceManifest, SequenceManifest?> 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;
}
}
/// <summary>Indexes one folder; returns the sidecar bytes when they were read and not rewritten since (for the snapshot).</summary>
@@ -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;
}
/// <summary>The index holds a project for this sequence and other frames than the sidecar: its positions may move.</summary>
private async Task<bool> MayMovePositionsAsync(SequenceManifest manifest, CancellationToken cancellationToken) =>
await _db.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false) is { SettingsJson: not null } indexed
&& indexed.FramesVersion != manifest.Versions.Frames;
/// <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)
{
@@ -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);
}
}