fix(projects): phase 5a fix round 2 of the projects engine

The sidecars stay the truth of a sequence's projects in the cases round 1's
checkers found still open, and no version or setting is lost on the way:

- An index-only Main meeting a Main another index made is written back as
  another project, and Main passes to theirs in one transaction through a
  free temporary name, so ux_projects_name and ux_projects_main never fail
  (the SqliteException used to escape ListAsync and leave the index and the
  sidecars disagreeing). A refusal is logged; the next reconcile settles it.
- The reconcile takes Main from the records, in step with the index, when
  another index passed it on: exactly one Main per sequence, names from the
  records where free.
- A project folder with versions and no record, of a project no index row
  names (a creation cut before its record), no longer holds the sequence
  back forever: it is left as it is and indexed once its record arrives.
- A record behind the index (a projects folder put back from an older
  backup) is made whole from the index: its missing versions and the record
  are written again, at every catch-up and before any append, rename or
  delete. Only a record ahead of the index or naming another version is
  refused, with a message that says so.
- An index meeting Main walks the chain down to the version the record names
  as mirrored (a bound, since that record write may lag project.json), so a
  lagging record never makes an External that reverts a newer push. When the
  record names a mirrored ancestor, settings written since merge with the
  unmirrored versions, as for an index that knows its mirror.
- The catch-up merge's External + Merge pair is resumable: an External head
  whose parent is a push no mirror wrote is finished into its Merge version
  next time, by this index or one rebuilt from the sidecars.
- A project deleted at the home has no head there, so a workspace copy of it
  reads Gone, never In sync; Remove local copy without discard says the
  project was deleted in the library.
- DatabaseExceptions are contained per sequence in the archive scan's project
  reconcile and in ProjectService reads.

Tests: SQLite (PostgreSQL-ready) for each scenario, including a cut between
the two catch-up versions, a 40,000-exclusion version through save, push and
rebuild, and the App's Remove local copy of a project deleted at home.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 11:24:15 +01:00
1 parent 8256b72913
commit a368dc8bf0
10 files changed
+635 -70

No files matched your search

@@ -2,9 +2,11 @@ using System.Globalization;
using System.Text.Json.Nodes;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Tests.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Projects;
@@ -162,6 +164,60 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
Assert.SequenceEqual([3], settings.ExcludedFrames);
}
[Test]
public async Task AMergeACrashStopsBetweenItsTwoVersionsIsFinishedNextTime()
{
foreach (var rebuild in new[] { false, true })
{
var fx = await FixtureAsync();
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
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
{
Stack = new StackSettings { Mode = StackSetting.Median },
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
}, frames), fx.Ct)).Version!;
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(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.
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] };
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, theirs);
File.WriteAllText(folder.ProjectPath, theirs.ToJson());
projects.Cut = step =>
{
if (step == "catchup.external") 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],
(await fx.Db.Projects.ListVersionsAsync(main.Id, null, 10, fx.Ct)).Select(v => v.Kind));
// The next catch-up (by this index, or by one rebuilt from the sidecars) makes the merge version: the push is in force.
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 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.SequenceEqual([3], settings.ExcludedFrames);
}
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
}
[Test]
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
{