fix(projects): one atomic catch-up merge, Main settled from the records, lost records rebuilt

The catch-up merge of a push whose mirror a crash cut short with settings
written meanwhile was two appends (External, then Merge), resumed by a purely
structural test that also fired on ordinary histories (two outside edits in
a row, a second index with an older mirror) and reverted the latest edit.
It is now one Merge version on the head in which the settings written
meanwhile (the settings in force, which older clients wrote) win where both
changed; the head's values stay in its version. A merge that adds nothing to
the head makes no version, so a cut before its mirror is finished next time
by the same index or a rebuilt one without any marker. The structural resume
is gone. An index meeting Main with a lagging record merges project.json the
same way, so a newer outside edit is no longer reverted by an older push.
Versions the catch-up records as mirrored are named in Main's record too
when project.json holds them, so records no longer lag behind externals.

Duplicate Main claimants (an index that met the sequence before Main's
record arrived) are settled from the records: the oldest claimant with a
version is Main in every index, the others are ordinary projects, and their
records say so when the archive takes writes.

A live project folder whose record is missing while the index holds the
project gets its record (and missing version files) written again from the
index instead of being held back forever. A record behind the index no
longer gives the index its old name. MirrorAsync reads the version leniently
and skips a newer one, as the other branches do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 12:56:51 +01:00
1 parent 488b588ad9
commit bc87a57830
5 files changed
+398 -130

No files matched your search

@@ -134,7 +134,7 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
private async Task<ArchiveFixture> FixtureAsync() => Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
[Test]
public async Task TheValuesTheMergeLeavesOutStayInMainsHistory()
public async Task WhereBothChangedTheSettingsWrittenMeanwhileWinAndThePushStaysInMainsHistory()
{
var fx = await FixtureAsync();
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
@@ -152,20 +152,19 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] });
// The pushed version wins where both changed; the other writer's settings are the version before the merge.
// One merge version on the push: the settings written meanwhile win where both changed, the push's offset stays in its version.
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
var external = ProjectSettings.FromJson(ProjectContent.Place(history[1].SettingsJson, ProjectServiceTests.Photos(frames)));
Assert.Equal((1.25, StackSetting.None), (external.Exposure.ExposureOffsetEv, external.Stack.Mode));
Assert.SequenceEqual([3], external.ExcludedFrames);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
Assert.Equal((pushed.Id, pushed.Id), (history[0].ParentId, history[1].Id));
Assert.Equal(0.5, ProjectSettings.FromJson(history[1].SettingsJson).Exposure.ExposureOffsetEv);
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
Assert.Equal((1.25, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
Assert.SequenceEqual([3], settings.ExcludedFrames);
Assert.SequenceEqual([3], ProjectSettings.FromJson(ProjectContent.Place(history[0].SettingsJson, ProjectServiceTests.Photos(frames))).ExcludedFrames);
}
[Test]
public async Task AMergeACrashStopsBetweenItsTwoVersionsIsFinishedNextTime()
public async Task AMergeACrashCutBeforeItsMirrorIsFinishedNextTimeWithNoOtherVersion()
{
foreach (var rebuild in new[] { false, true })
{
@@ -183,7 +182,7 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(first.Id, 1) });
// The push's mirror cut short, another path's settings meanwhile, and the catch-up's merge cut after its external version.
// The push's mirror cut short, another path's settings meanwhile, and the catch-up's merge cut before its mirror.
var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
var theirs = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] };
@@ -191,33 +190,118 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
File.WriteAllText(folder.ProjectPath, theirs.ToJson());
projects.Cut = step =>
{
if (step == "catchup.external") throw new CutException(step);
if (step == "catchup.merge") throw new CutException(step);
};
await Assert.ThrowsAsync<CutException>(() => projects.ListAsync(sequence.Id, fx.Ct));
projects.Cut = null;
Assert.SequenceEqual([ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
(await fx.Db.Projects.ListVersionsAsync(main.Id, null, 10, fx.Ct)).Select(v => v.Kind));
Assert.Equal(StackSetting.None, ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).Stack.Mode);
// The next catch-up (by this index, or by one rebuilt from the sidecars) makes the merge version: the push is in force.
// The next catch-up (by this index, or by one rebuilt from the sidecars) mirrors that merge version and adds none.
if (rebuild)
{
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
}
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
history.Select(v => v.Kind));
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
foreach (var _ in new[] { 1, 2 })
{
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
Assert.Equal(pushed.Id, history[0].ParentId);
}
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
{
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
Assert.Equal((1.25, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
Assert.SequenceEqual([3], settings.ExcludedFrames);
}
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
}
[Test]
public async Task TwoOutsideEditsInARowAreNoMergeForAnyIndex()
{
foreach (var rebuild in new[] { true, false })
{
var fx = await FixtureAsync();
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var first = await projects.HeadAsync(main.Id, fx.Ct);
var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Version!;
var folder = fx.Paths.Sequence(sequence.Folder);
// Another PC's index that met Main mirrored at version 2.
var (other, _) = await fx.OtherWriterAsync();
await other.Projects.ListAsync(sequence.Id, fx.Ct);
// An older path edits the settings twice, the second time back to what version 2 holds: two external versions.
foreach (var excluded in new[] { new[] { 0, 1 }, new[] { 0 } })
{
var settings = new ProjectSettings { ExcludedFrames = [.. excluded] };
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, settings);
File.WriteAllText(folder.ProjectPath, settings.ToJson());
await projects.ListAsync(sequence.Id, fx.Ct);
}
var latest = (await projects.HeadAsync(main.Id, fx.Ct)).Id;
Assert.Equal(latest, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)).Mirrored?.Id);
// As an older build leaves the record (naming the push as mirrored): the shape of the history decides nothing.
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(pushed.Id, pushed.Number) });
var service = other;
if (rebuild)
{
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
service = fx.Service;
}
await service.Projects.ListAsync(sequence.Id, fx.Ct);
var history = await service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.External, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
history.Select(v => v.Kind));
Assert.Equal(latest, history[0].Id);
Assert.SequenceEqual([0], ProjectSettings.FromJson(ProjectContent.Place(history[0].SettingsJson, ProjectServiceTests.Photos(frames))).ExcludedFrames);
Assert.SequenceEqual([0], ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).ExcludedFrames);
}
}
[Test]
public async Task AnOutsideEditAfterAPushWhoseRecordLagsStaysInForce()
{
var fx = await FixtureAsync();
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var first = await projects.HeadAsync(main.Id, fx.Ct);
var second = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Version!;
var third = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(second, new ProjectSettings
{
ExcludedFrames = [0],
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
}, frames), fx.Ct)).Version!;
// Version 3's mirror wrote project.json but not the record (it still names version 2); then an older path edits project.json.
var folder = fx.Paths.Sequence(sequence.Folder);
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(second.Id, 2) });
File.WriteAllText(folder.ProjectPath, new ProjectSettings { ExcludedFrames = [0], Exposure = new ExposureSettings { ExposureOffsetEv = 1.0 } }.ToJson());
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
// The outside edit stays in force; version 3's offset stays in the history.
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
Assert.Equal((third.Id, 0.5), (history[1].Id, ProjectSettings.FromJson(history[1].SettingsJson).Exposure.ExposureOffsetEv));
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
{
Assert.Equal(1.0, settings.Exposure.ExposureOffsetEv);
Assert.SequenceEqual([0], settings.ExcludedFrames);
}
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
[Test]
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
{
@@ -579,9 +579,111 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
Assert.FileExists(folder.VersionPath(id, 1, cut.Id));
Assert.Null(await fx.Db.Projects.GetAsync(id));
// Its record there at last (a sync tool bringing another PC's project): indexed as another project, Main stays.
// Its record there at last (a sync tool bringing another PC's Main, made before this one): Main, as in every index; the
// one made here meanwhile is another project, its version kept.
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(id), new ProjectRecord(id, sequence.Id, ProjectService.MainName, true, new ProjectHead(cut.Id, 1), now, null, now));
Assert.SequenceEqual([(main.Id, true), (id, false)], (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain)));
Assert.SequenceEqual([(id, "Main", true), (main.Id, "Main (2)", false)],
(await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain)));
Assert.Equal(1, MainRecords(folder));
Assert.Count(1, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
[Test]
public async Task AMainMadeWhileAnotherMainsRecordWasNotThereYetGivesWayInEveryIndex()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var pushed = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct))
.Version!;
var folder = fx.Paths.Sequence(sequence.Folder);
// Another PC's index meets the sequence while a sync tool is still bringing Main's record: it makes a Main of its own.
var record = folder.ProjectRecordPath(main.Id);
File.Move(record, record + ".syncing");
var (other, _) = await fx.OtherWriterAsync();
var theirs = await other.Projects.MainAsync(sequence.Id, fx.Ct);
Assert.NotEqual(main.Id, theirs.Id);
File.Move(record + ".syncing", record);
// The record there, both indexes take the older Main, in either order, and the other is an ordinary project with its version.
foreach (var service in new[] { other, fx.Service, other })
{
Assert.SequenceEqual([(main.Id, "Main", true), (theirs.Id, "Main (2)", false)],
(await service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain)));
}
Assert.Equal(1, MainRecords(folder));
Assert.Equal(("Main (2)", false), (ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).Name, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).IsMain));
Assert.Count(1, await other.Projects.HistoryAsync(theirs.Id, cancellationToken: fx.Ct));
Assert.Equal(pushed.Id, (await other.Projects.HeadAsync(main.Id, fx.Ct)).Id);
Assert.Equal(PushOutcome.Stored, (await other.Projects.PushAsync(main.Id, Draft(pushed, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Outcome);
Assert.SequenceEqual([2], Settings(fx, sequence).ExcludedFrames);
Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id);
}
[Test]
public async Task AProjectWhoseRecordIsLostIsWrittenAgainFromTheIndex()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var first = await projects.HeadAsync(vertical.Id, fx.Ct);
var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!;
var mainSecond = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct))
.Version!;
var folder = fx.Paths.Sequence(sequence.Folder);
// Version 3 of Vertical is in its folder (another PC's push), then both records and version 1's file are lost.
var third = Draft(second, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 3, third.Id), third);
File.Delete(folder.ProjectRecordPath(vertical.Id));
File.Delete(folder.ProjectRecordPath(main.Id));
File.Delete(folder.VersionPath(vertical.Id, 1, first.Id));
// Nothing is held back: the records are written again from the index, the version on disk past its head adopted.
Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 3)],
(await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber)));
var verticalRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
Assert.Equal(("Vertical", third.Id), (verticalRecord.Name, verticalRecord.Head!.Id));
var mainRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
Assert.Equal((true, mainSecond.Id, mainSecond.Id), (mainRecord.IsMain, mainRecord.Head!.Id, mainRecord.Mirrored?.Id));
Assert.FileExists(folder.VersionPath(vertical.Id, 1, first.Id));
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(mainSecond, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Outcome);
Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames);
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(third, new ProjectSettings(), frames), fx.Ct)).Outcome);
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
Assert.SequenceEqual([4, 3, 2, 1], (await fx.Service.Projects.HistoryAsync(vertical.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
Assert.SequenceEqual([3, 2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
}
[Test]
public async Task ARenameMadeAfterABackupStaysWhenTheBackupIsPutBack()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
var backup = folder.ProjectsDirectory + "-backup";
CopyDirectory(folder.ProjectsDirectory, backup);
await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct);
await projects.RenameAsync(vertical.Id, "Square", fx.Ct);
// The backup made before is put back: its record is behind the index and names the old name.
Directory.Delete(folder.ProjectsDirectory, recursive: true);
CopyDirectory(backup, folder.ProjectsDirectory);
Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
Assert.Equal(("Square", 2), (record.Name, record.Head!.Number));
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
}
[Test]