fix(projects): never undo a deletion by a record rebuild; keep outside project.json edits
Phase 5a fix round 4, projects engine. - The reconcile reads the projects' trash before the live folders, so a deletion wins over what a sync tool has left of the live folder. A lost record is never written again while the trash holds a folder of that project; a trashed record that cannot be read holds the project back. - A lost record is written again from the index only once the folder has looked the same, file by file, on two reconciles two minutes apart, so a record a sync tool is still bringing (a rename, a newer head) wins. - IndexAsync's "behind" is the rule the heal uses: the record's head must be a version the index holds below its head. A record on another chain keeps its name. - A record written before what the index knows (its Updated time is older, e.g. a backup made before a rename) never gives its name, and the heal writes the index's name back. A record's Updated time never goes back, so a newer rename from a PC whose clock is behind is still taken. - A mirror never writes project.json over settings written there since this index last knew what it held (another PC, an older client): they are merged first (theirs win where both changed) or kept as an external version, then the result is mirrored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c5ed31bced
commit
56b8ba3c1d
4 files changed
+449
-45
No files matched your search
@@ -643,7 +643,14 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
|
||||
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.
|
||||
// The first look holds them back and writes nothing (a sync tool may still be bringing the records); once the folders
|
||||
// looked the same twice, nothing is held back: the records are written again from the index, the version on disk past
|
||||
// its head adopted.
|
||||
projects.RecordSettle = TimeSpan.Zero;
|
||||
Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 2)],
|
||||
(await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber)));
|
||||
Assert.False(File.Exists(folder.ProjectRecordPath(vertical.Id)));
|
||||
Assert.False(File.Exists(folder.ProjectRecordPath(main.Id)));
|
||||
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));
|
||||
@@ -686,6 +693,160 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
|
||||
Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARenameOnlyMadeAfterABackupStaysWhenTheBackupIsPutBack()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, _) = 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.RenameAsync(vertical.Id, "Square", fx.Ct);
|
||||
|
||||
// The backup made before the rename (the same head) is put back: its record was written before the index's 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", 1), (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);
|
||||
|
||||
// A rename another PC makes is newer than what this index knows, even from a clock behind the one this index wrote
|
||||
// with (ten minutes ahead here): it is taken.
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
Assert.Equal("Square", (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
var ahead = DateTimeOffset.UtcNow.AddMinutes(10);
|
||||
var written = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(vertical.Id), written with { Updated = ahead });
|
||||
await fx.Db.Projects.UpdateAsync((await fx.Db.Projects.GetAsync(vertical.Id))! with { UpdatedAt = ahead }, fx.Ct);
|
||||
await other.Projects.RenameAsync(vertical.Id, "Wide", fx.Ct);
|
||||
Assert.True(ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Updated > ahead);
|
||||
Assert.Equal("Wide", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
Assert.Equal("Wide", ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARecordOnAnotherChainBelowTheHeadKeepsItsName()
|
||||
{
|
||||
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 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!;
|
||||
await projects.PushAsync(vertical.Id, Draft(second, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
|
||||
// Another PC's record, on a chain of its own from version 1, renamed there: its head number is below the index's, but
|
||||
// it is not a version the index holds, so the record is not behind and its name is taken.
|
||||
var theirs = Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
|
||||
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, theirs.Id), theirs);
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
|
||||
var apart = record with { Name = "Square", Head = new ProjectHead(theirs.Id, 2), Updated = DateTimeOffset.UtcNow.AddSeconds(1) };
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(vertical.Id), apart);
|
||||
var listed = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
|
||||
Assert.Equal(("Square", 3), (listed.Name, listed.HeadNumber));
|
||||
// Index and record agree on the name, and the record is never written over.
|
||||
Assert.Equal(apart, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)));
|
||||
Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ADeletionArrivingHalfSyncedIsTakenAndNeverUndone()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, _) = 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 square = await projects.CreateAsync(sequence.Id, "Square", cancellationToken: fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
Assert.SequenceEqual([main.Id, vertical.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
projects.RecordSettle = TimeSpan.Zero;
|
||||
other.Projects.RecordSettle = TimeSpan.Zero;
|
||||
|
||||
// This PC deletes Vertical; on the other PC a sync tool moving it file by file into the trash has left its live folder
|
||||
// with a version file and no record. However often the other index looks, it takes the deletion and writes nothing.
|
||||
await projects.DeleteAsync(vertical.Id, fx.Ct);
|
||||
var trashed = Directory.GetDirectories(folder.ProjectsTrashDirectory).Single();
|
||||
Directory.CreateDirectory(folder.ProjectVersionsDirectory(vertical.Id));
|
||||
foreach (var file in Directory.GetFiles(Path.Combine(trashed, ArchiveLayout.ProjectVersionsFolder)))
|
||||
File.Copy(file, Path.Combine(folder.ProjectVersionsDirectory(vertical.Id), Path.GetFileName(file)));
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
Assert.SequenceEqual([main.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
Assert.SequenceEqual([main.Id, square.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
}
|
||||
Assert.NotNull((await other.Projects.GetAsync(vertical.Id, fx.Ct))!.DeletedAt);
|
||||
Assert.False(File.Exists(folder.ProjectRecordPath(vertical.Id)));
|
||||
|
||||
// Square's deletion arrives with its versions in the trash before its record, and its live record gone already: it is
|
||||
// held back (listed, nothing written), never written again as live, and deleted once the trash's record is there.
|
||||
var squareTrash = Path.Combine(folder.ProjectsTrashDirectory, square.Id.ToString("N") + "-" + DateTimeOffset.UtcNow.UtcTicks);
|
||||
CopyDirectory(folder.ProjectDirectory(square.Id), squareTrash);
|
||||
var squareRecord = ProjectSidecars.ReadRecord(Path.Combine(squareTrash, ArchiveLayout.ProjectFile));
|
||||
File.Delete(Path.Combine(squareTrash, ArchiveLayout.ProjectFile));
|
||||
File.Delete(folder.ProjectRecordPath(square.Id));
|
||||
for (var i = 0; i < 2; i++)
|
||||
Assert.SequenceEqual([main.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
Assert.False(File.Exists(folder.ProjectRecordPath(square.Id)));
|
||||
ProjectSidecars.WriteRecord(Path.Combine(squareTrash, ArchiveLayout.ProjectFile), squareRecord with { Deleted = DateTimeOffset.UtcNow });
|
||||
Assert.SequenceEqual([main.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
Assert.SequenceEqual([main.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
Assert.False(File.Exists(folder.ProjectRecordPath(square.Id)));
|
||||
|
||||
// A rebuilt index has both deleted too.
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
Assert.SequenceEqual([main.Id], (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ALostRecordIsWrittenAgainOnlyOnceItsFolderHasSettledSoAnIncomingOneWins()
|
||||
{
|
||||
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 first = await projects.HeadAsync(vertical.Id, fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var path = folder.ProjectRecordPath(vertical.Id);
|
||||
|
||||
// A sync tool is bringing the record another PC wrote (a rename, a push): meanwhile the folder holds none. However often
|
||||
// the index looks within the settling time, it writes none of its own and takes nothing for the project.
|
||||
var incoming = ProjectSidecars.ReadRecord(path);
|
||||
File.Delete(path);
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
var held = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
|
||||
Assert.Equal(("Vertical", 1), (held.Name, held.HeadNumber));
|
||||
}
|
||||
Assert.False(File.Exists(path));
|
||||
await Assert.ThrowsAsync<ProjectsUnreadableException>(() => projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct));
|
||||
|
||||
// The record arrives, renamed and moved on there: it wins.
|
||||
var theirs = Draft(first, new ProjectSettings { ExcludedFrames = [2] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
|
||||
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, theirs.Id), theirs);
|
||||
ProjectSidecars.WriteRecord(path, incoming with { Name = "Square", Head = new ProjectHead(theirs.Id, 2), Updated = DateTimeOffset.UtcNow });
|
||||
var listed = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
|
||||
Assert.Equal(("Square", theirs.Id), (listed.Name, listed.HeadVersionId));
|
||||
|
||||
// A record really lost is written again from the index once the folder looked the same on two reconciles a settling time apart.
|
||||
File.Delete(path);
|
||||
projects.RecordSettle = TimeSpan.Zero;
|
||||
await projects.ListAsync(sequence.Id, fx.Ct);
|
||||
Assert.False(File.Exists(path));
|
||||
await projects.ListAsync(sequence.Id, fx.Ct);
|
||||
var written = ProjectSidecars.ReadRecord(path);
|
||||
Assert.Equal(("Square", theirs.Id), (written.Name, written.Head!.Id));
|
||||
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(theirs, new ProjectSettings(), frames), fx.Ct)).Outcome);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectsFolderPutBackFromAnOlderBackupIsMadeWholeFromTheIndex()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user