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

@@ -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]