fix(workspace): phase 5a fix round 2, workspace sync and copy

A Save both, a recorded conflict and a damaged version file could each leave
a local version out of the chain or a copy half-made at the home. This round
makes every one of these end with nothing lost and nothing half done.

- Save both retry: a journaled copy somebody else sent a version to (or one
  deleted since) is left as it is, and a new copy takes every local version.
  A divergence met at the copy while sending also makes a new copy, once.
- Keep mine, Take server, or a pull that settles a conflict after a Save both
  was cut short first send the rest of the journaled versions to that copy,
  if it is still this workspace's. The journal is cleared at the commit point.
- A conflict of format met past local versions the home already stored
  (their answer lost) now moves the base onto them, like any other conflict.
  The conflict, the base and the chain then name one version. A save-both
  journal follows the base when it moves.
- A conflict recorded before stays while the home holds no version after
  the base. A base that has reached the conflict's head clears it.
- Take server no longer replays or sends the version it saves first when,
  replayed, that version holds only the server's values. It stays in
  versions/.replaced for the undo. The test checks the exact history again.
- Version files after the base that cannot be read, or that do not follow
  from it, are listed (WorkspaceProject.UnreadableVersions). The new state
  WorkspaceState.Damaged shows them. Commit, push, pull and resolve refuse to
  go past them. A damaged file whose version the home holds takes the home's
  copy at the next sync, and the damaged file is kept aside. Remove local
  copy counts these files as unsent.
- PushOutcome.TooLarge raises its own WorkspaceSyncException message. The
  desktop says Errors.Workspace.VersionTooLarge for it and for
  ProjectVersionTooLargeException when a local commit is too large.
- The editor maps LeaseUnavailableException from opening a busy project to
  Errors.Workspace.Busy.
- The copy job no longer deletes, during the pass, a proxy that the editor's
  current media name. RemoveStale removes it after the commit. A resumed run
  does not keep such a file as a frame whose photo changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 11:31:18 +01:00
1 parent 8256b72913
commit e23cba3ee1
18 files changed
+646 -88

No files matched your search

@@ -159,8 +159,257 @@ public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest
Assert.Equal(WorkspaceState.InSync, (await one.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
Assert.Equal((1.0, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
// Taking the server's first saves the unsaved offset as a version (kept for its undo); the versions before it reach the home in order.
Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1).Take(3));
// Taking the server's first saves the unsaved offset as a version, kept aside for its undo: replayed it holds the
// server's values only, so it is not sent.
Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1));
Assert.Contains(Directory.GetFiles(Path.Combine(one.Store.Project(id).VersionsDirectory, WorkspaceProject.ReplacedFolder)),
f => ProjectSidecars.ReadVersion(f) is { Note: null } saved && Settings(saved).Exposure.ExposureOffsetEv == 2.0);
}
/// <summary>
/// A conflict this workspace stopped at: Main moved at the home (offset 0.5), and <c>three</c> saved "bright" (offset 2)
/// and then changed the stack without saving a version.
/// </summary>
private static async Task<(WorkspaceSync Three, WorkspaceStore Store, WorkspaceProject Project, Guid Id)> ConflictAsync(WorkspaceFixture fx)
{
var (_, one) = fx.Workspace("one");
var (store, three) = fx.Workspace("three");
var id = (await fx.CheckoutAsync(one)).ProjectId;
var project = await fx.CheckoutAsync(three, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(three, id, s => Offset(s, 2), fx.Ct);
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct);
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
return (three, store, project, id);
}
[Test]
public async Task ASaveBothRetryMakesANewCopyWhenSomebodySentAVersionToTheHalfMadeOne()
{
var fx = await FixtureAsync();
var (three, _, project, id) = await ConflictAsync(fx);
await Assert.ThrowsAsync<HomeUnavailableException>(() =>
three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
// Another PC opens the half-made copy and sends a version to it: it is theirs now.
var (_, two) = fx.Workspace("two");
await fx.CheckoutAsync(two, partial.Id);
await EditAsync(two, partial.Id, s => Offset(s, 4), fx.Ct);
await two.CommitAsync(partial.Id, "theirs", cancellationToken: fx.Ct);
await two.PushAsync(partial.Id, fx.Home, fx.Ct);
var taken = await fx.Home.HeadAsync(partial.Id, fx.Ct);
// The retry leaves it as it is and sends every version of this workspace to a new copy.
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct);
var copy = Assert.NotNull(both.SavedAs);
Assert.NotEqual(partial.Id, copy.Id);
Assert.Equal("Main (other)", copy.Name);
var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct));
Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
Assert.SequenceEqual(["bright", null], (await NotesAsync(fx, copy.Id)).Skip(1));
Assert.Equal(taken.Id, (await fx.Home.HeadAsync(partial.Id, fx.Ct)).Id);
Assert.Equal(WorkspaceState.InSync, both.Status.State);
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
}
[Test]
public async Task KeepMineAfterASaveBothCutShortFinishesItsCopy()
{
var fx = await FixtureAsync();
var (three, _, project, id) = await ConflictAsync(fx);
await Assert.ThrowsAsync<HomeUnavailableException>(() =>
three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
Assert.Equal(2, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count);
// The user keeps theirs instead: the copy the cut left half-made gets the rest, and this project goes on.
var resolved = await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
Assert.Null(resolved.Status.Conflict);
Assert.Null(project.ReadSync().SaveBoth);
var copied = Settings(await fx.Home.HeadAsync(partial.Id, fx.Ct));
Assert.Equal((2, StackSetting.StarTrails), (copied.Exposure.ExposureOffsetEv, copied.Stack.Mode));
Assert.Equal(3, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count);
Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var main = Settings(await fx.Home.HeadAsync(id, fx.Ct));
Assert.Equal((2, StackSetting.StarTrails), (main.Exposure.ExposureOffsetEv, main.Stack.Mode));
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
}
[Test]
public async Task AConflictStaysWhileTheHomeHoldsNoVersionAfterTheBase()
{
var fx = await FixtureAsync();
var (three, _, project, id) = await ConflictAsync(fx);
var recorded = project.ReadSync().Conflict!;
// The home answers it has nothing after the base (its index taken back from an older backup): nothing settles the conflict.
var forgetful = new CuttingHome(fx.Home) { Forgets = true };
Assert.Equal(WorkspaceState.Conflict, (await three.PullAsync(id, forgetful, fx.Ct)).Status.State);
Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.KeepMine, cancellationToken: fx.Ct)).Status.State);
Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.TakeServer, cancellationToken: fx.Ct)).Status.State);
Assert.Equal(recorded.TheirsVersionId, project.ReadSync().Conflict!.TheirsVersionId);
Assert.Equal("bright", three.Unpushed(id)[0].Note);
// Back as it was, the home settles it as the user chooses.
await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
Assert.Equal((2, StackSetting.StarTrails), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
}
[Test]
public async Task ASaveBothAfterAConflictMetPastAVersionTheHomeHeldSendsEveryVersion()
{
var fx = await FixtureAsync();
var (store, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var project = await fx.CheckoutAsync(one);
var id = project.ProjectId;
await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
await one.CommitAsync(id, "second", cancellationToken: fx.Ct);
await Assert.ThrowsAsync<HomeUnavailableException>(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
await two.PullAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
await two.CommitAsync(id, "other", cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(one, id, s => Offset(s, 2.0), fx.Ct);
var stopped = await one.PullAsync(id, fx.Home, fx.Ct);
Assert.Equal((WorkspaceState.Conflict, first.Id), (stopped.Status.State, stopped.Status.Conflict!.BaseVersionId));
Assert.Equal(first.Id, stopped.Status.Base!.VersionId);
// Save both, cut once this project moved onto the head: the next sync finishes it and nothing of this workspace's is lost.
await Assert.ThrowsAsync<CutException>(() => WorkspaceFixture.Cutting(store, "saveboth.base").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth,
cancellationToken: fx.Ct));
var settled = await one.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal((WorkspaceState.InSync, 0), (settled.Status.State, settled.Status.Ahead));
var copy = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (2)");
Assert.SequenceEqual(["second", null], (await NotesAsync(fx, copy.Id)).Skip(1));
var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct));
Assert.Equal((2.0, StackSetting.Mean), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode));
Assert.Equal(1.0, (await one.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
}
[Test]
public async Task AConflictOfFormatMetPastAVersionTheHomeHeldMovesTheBaseOntoIt()
{
var fx = await FixtureAsync(3);
var (_, one) = fx.Workspace("one");
var project = await fx.CheckoutAsync(one);
var id = project.ProjectId;
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
await EditAsync(one, id, s => s with { ExcludedFrames = [1] }, fx.Ct);
await one.CommitAsync(id, "second", cancellationToken: fx.Ct);
await Assert.ThrowsAsync<HomeUnavailableException>(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
var newer = NewerChronos.Push(fx.Archive, fx.Sequence, await fx.Home.HeadAsync(id, fx.Ct));
// The conflict, the base and the chain name one version: the one the home held.
var stopped = await one.PullAsync(id, fx.Home, fx.Ct);
Assert.True(stopped.Status.Conflict!.NewerFormat);
Assert.Equal((first.Id, first.Id, 1), (stopped.Status.Base!.VersionId, stopped.Status.Conflict.BaseVersionId, stopped.Status.Ahead));
Assert.True(project.Versions().Single(v => v.Id == first.Id).StoredAt is not null, "its file took the home's copy");
var both = await one.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
var copy = Assert.NotNull(both.SavedAs);
Assert.SequenceEqual(["second"], (await NotesAsync(fx, copy.Id)).Skip(1));
var kept = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames)));
Assert.Equal(0.5, kept.Exposure.ExposureOffsetEv);
Assert.SequenceEqual([1], kept.ExcludedFrames);
Assert.Equal((newer.Id, true), (both.Status.Base!.VersionId, both.Status.ReadOnly));
Assert.Empty(project.Versions().Where(v => v.StoredAt is null));
}
[Test]
public async Task AVersionFileThatCannotBeReadStopsTheSyncAndTakesTheHomesCopyWhenItHasOne()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
var first = (await sync.CommitAsync(id, "first", cancellationToken: fx.Ct))!;
await EditAsync(sync, id, s => Offset(s, 0.8), fx.Ct);
var second = (await sync.CommitAsync(id, "second", cancellationToken: fx.Ct))!;
await EditAsync(sync, id, s => Offset(s, 0.9), fx.Ct);
// A version saved here and never sent cannot be read: shown, and nothing is saved, sent or taken past it.
var path = project.VersionPath(second);
var bytes = File.ReadAllBytes(path);
File.WriteAllText(path, "{ damaged");
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
Assert.Equal((WorkspaceState.Damaged, 1), (status.State, status.Damaged));
await Assert.ThrowsAsync<InvalidDataException>(() => sync.CommitAsync(id, cancellationToken: fx.Ct));
await Assert.ThrowsAsync<InvalidDataException>(() => sync.PushAsync(id, fx.Home, fx.Ct));
await Assert.ThrowsAsync<InvalidDataException>(() => sync.PullAsync(id, fx.Home, fx.Ct));
Assert.Equal(1, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count);
Assert.True(File.Exists(path));
// Readable again (it was held open, say), it goes on as if nothing happened.
File.WriteAllBytes(path, bytes);
Assert.Equal(WorkspaceState.Ahead, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
// One the home stored (its answer lost) takes the home's copy at the next sync; the damaged file is kept aside.
await Assert.ThrowsAsync<HomeUnavailableException>(() => sync.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct));
File.WriteAllText(project.VersionPath(first), "{ damaged");
Assert.Equal((WorkspaceState.Damaged, 2), ((await sync.StatusAsync(id, fx.Home, fx.Ct)).State, (await sync.StatusAsync(id, null, fx.Ct)).Damaged));
Assert.Null((await sync.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
await sync.CommitAsync(id, "third", cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
Assert.SequenceEqual(["first", "second", "third"], (await NotesAsync(fx, id)).Skip(1));
Assert.Contains(Directory.GetFiles(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder)),
f => File.ReadAllText(f) == "{ damaged");
}
[Test]
public async Task AVersionTheHomeFindsTooLargeIsRefusedAsSuchAndWaits()
{
var fx = await FixtureAsync(3);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
await sync.CommitAsync(id, "large", cancellationToken: fx.Ct);
var refused = await Assert.ThrowsAsync<WorkspaceSyncException>(() => sync.PushAsync(id, new CuttingHome(fx.Home) { Answer = PushOutcome.TooLarge }, fx.Ct));
Assert.Equal(PushOutcome.TooLarge, refused.Outcome);
Assert.Equal("large", sync.Unpushed(id).Single().Note);
Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State);
}
[Test]
public async Task ACopyThatFailsLeavesTheEditorsPhotosInPlace()
{
var fx = await FixtureAsync();
var (store, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var before = project.ReadMedia()!;
var frames = before.Frames.OrderBy(f => f.Index).ToList();
var editors = project.MediaFile(frames[0].File!);
// The first frame's original is gone at the home, and the copy then fails on the second.
File.Delete(fx.Archive.Paths.FromRelative(fx.Frames.Single(f => f.Id == frames[0].Id).RelativePath));
var blocked = project.MediaFile(frames[1].File!);
File.Delete(blocked);
Directory.CreateDirectory(blocked);
await fx.CopyAsync(store, project.ProjectId, JobState.Failed);
Assert.True(File.Exists(editors), "the editor's photo of the first frame stays while the copy has not committed");
Assert.Equal(frames[0].File, project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File);
// A copy that commits without it removes it then.
Directory.Delete(blocked);
await fx.CopyAsync(store, project.ProjectId);
Assert.Null(project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File);
Assert.False(File.Exists(editors));
Assert.True(File.Exists(blocked));
}
[Test]