fix(workspace): settle only the conflict shown, keep what a choice replaces, desktop gaps of phase 5a

Review 2, phase 5a, fix round 1 (desktop and the engine side of Resolve and Restore).

- Resolve applies Keep mine / Take the library's only to the conflict the dialog listed:
  when the head or what differs moved since, the new conflict is recorded and asked again;
  Sync re-works a pending conflict instead of re-showing a stale list. Take the library's
  first saves uncommitted changes as a version, and the editor takes it as one undo step
  (toast Undo, Ctrl+Z) that brings back only the settings both changed.
- Restore saves unsaved changes as a version also while a conflict waits; a restore that
  changed nothing, or was undone, no longer marks the next version as a Restore.
- The editor's saves and merges of a workspace project merge positions photo by photo
  (portable form) and ask where both sides changed a setting, never deciding for ours.
- Read-only after a pull is signalled, and no save that can never be stored is left behind.
- The chip is read again after edits; a background conflict waits for the open dialog;
  Send first and Delete show the conflict they met; Discard changes needs the typed name.
- Open project... reaches the library's other projects (Save both's sibling, also from
  its toast); copies whose project or sequence is gone read Not in library and can be
  removed from the picker's Workspace rows; a delete whose local copy stays says so.
- A workspace folder inside a library is refused before anything is written.
- Conflict and Compare lines name heroes, exclusions and keyframes; History shows the PC
  name alone, never the program's prefixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 10:22:17 +01:00
1 parent 50e28fd63d
commit 8027d84f7e
28 files changed
+1321 -134

No files matched your search

+99 -28
View File
@@ -29,6 +29,12 @@ public enum WorkspaceState
/// <summary>The workspace holds no media for the project (never copied, or removed): copy them again to edit.</summary>
MediaRemoved,
/// <summary>
/// The home answered that it no longer has the project or its sequence (the sequence left its index): nothing more can be
/// sent, and the local copy can only be removed, its work kept aside. Told by the desktop, which asks the home.
/// </summary>
Gone,
}
/// <summary>
@@ -43,7 +49,15 @@ public sealed record WorkspaceStatus(WorkspaceState State, bool Modified, int Ah
/// <summary>What a push, pull or resolve did: the versions it stored at the home and those it took from there, and where it left the project.</summary>
/// <param name="SavedAs">After <see cref="ConflictChoice.SaveBoth"/>: the new project at the home holding this workspace's versions.</param>
public sealed record WorkspaceSyncResult(WorkspaceStatus Status, IReadOnlyList<ProjectVersion> Pushed, IReadOnlyList<ProjectVersion> Pulled,
SequenceProject? SavedAs = null);
SequenceProject? SavedAs = null)
{
/// <summary>
/// After <see cref="ConflictChoice.TakeServer"/> settled a conflict: the working state as keeping this side's values on
/// the settings both changed would have left it (positions on the media's frames), so the editor takes the server's
/// values as one step its undo takes back, and only them; null otherwise.
/// </summary>
public ProjectSettings? Kept { get; init; }
}
/// <summary>How a conflict is settled (owner review 2, R8: only on real conflicts).</summary>
public enum ConflictChoice
@@ -80,10 +94,10 @@ public sealed class WorkspaceSyncException(PushOutcome outcome, string message)
/// When the home moved meanwhile, its new versions are pulled and each local commit is replayed on top of them
/// (<see cref="ProjectMerge.Replay"/>, new ids, the old files kept in <c>versions/.replaced</c>), and the working state is
/// merged too (<see cref="ProjectMerge.MergeWithConflicts"/>); settings both sides changed to different values stop
/// there as a conflict (<see cref="WorkspaceConflict"/>) for the user to settle (<see cref="ResolveAsync"/>). Versions
/// travel in the portable form (<see cref="ProjectContent"/>), so frames inserted at the home never move a position. Every
/// operation holds the project's lease; nothing is ever deleted: a restore or save-both first saves uncommitted changes
/// as a version.
/// there as a conflict (<see cref="WorkspaceConflict"/>) for the user to settle (<see cref="ResolveAsync"/>), a choice
/// applying only to the conflicts the user was shown. Versions travel in the portable form (<see cref="ProjectContent"/>),
/// so frames inserted at the home never move a position. Every operation holds the project's lease; nothing is ever
/// deleted: a restore, a save-both or taking the server's values first saves uncommitted changes as a version.
/// </summary>
public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
{
@@ -231,7 +245,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// <summary>
/// Takes the home's versions newer than the base: the working state (and any local versions, replayed) moves onto the
/// home's head; a real conflict stops it (<see cref="WorkspaceState.Conflict"/>).
/// home's head; a real conflict stops it (<see cref="WorkspaceState.Conflict"/>). A conflict recorded before is worked
/// out again (the home may have moved on, this side may have been edited since), so what is asked is what differs now;
/// one that no longer conflicts is settled by the merge.
/// </summary>
/// <exception cref="HomeUnavailableException">The home cannot be reached.</exception>
public async Task<WorkspaceSyncResult> PullAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken = default)
@@ -241,9 +257,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
{
IReadOnlyList<ProjectVersion> pulled = [];
if (project.ReadSync().Conflict is null)
var bundle = project.ReadBundle();
if (project.ReadSync().Conflict is not null)
{
(_, var conflict, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
}
else
{
var bundle = project.ReadBundle();
var head = await home.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
if (head.Id != bundle.Workspace!.Base?.VersionId) (_, _, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
}
@@ -252,16 +273,20 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
/// <summary>
/// Settles the conflict a pull or push stopped at. <see cref="ConflictChoice.KeepMine"/> keeps this workspace's values
/// (but for the members in <paramref name="takeServer"/>), <see cref="ConflictChoice.TakeServer"/> takes the server's
/// wherever both changed; either way the local versions are replayed on the home's head and wait to be pushed.
/// Settles the conflict a pull or push stopped at, as the user was shown it (<paramref name="shown"/>, else the one
/// recorded). <see cref="ConflictChoice.KeepMine"/> keeps this workspace's values (but for the members in
/// <paramref name="takeServer"/>), <see cref="ConflictChoice.TakeServer"/> takes the server's on the settings listed;
/// either way the local versions are replayed on the home's head and wait to be pushed. A choice applies only to the
/// conflicts listed: when what differs now is not that (the home moved on, or this side was edited meanwhile), nothing
/// is settled, and the conflict found now is recorded and returned to be asked again. Taking the server's first saves
/// uncommitted changes as a version, so the values it replaces are kept (its file in <c>versions/.replaced</c>).
/// <see cref="ConflictChoice.SaveBoth"/> saves the working state as a version, sends this workspace's versions to a new
/// project at the home (<paramref name="saveBothName"/>, else this project's name with a number) made from the base,
/// and moves this project onto the home's head.
/// </summary>
/// <exception cref="HomeUnavailableException">The home cannot be reached; the conflict stays.</exception>
public async Task<WorkspaceSyncResult> ResolveAsync(Guid projectId, IProjectHome home, ConflictChoice choice, IReadOnlySet<string>? takeServer = null,
string? saveBothName = null, CancellationToken cancellationToken = default)
string? saveBothName = null, WorkspaceConflict? shown = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(home);
var project = Existing(projectId);
@@ -269,20 +294,26 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
{
var bundle = project.ReadBundle();
var sync = project.ReadSync();
if (sync.Conflict is null) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
if (sync.Conflict is not { } recorded) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
if (choice == ConflictChoice.SaveBoth)
return await SaveBothAsync(project, bundle, home, saveBothName, cancellationToken).ConfigureAwait(false);
if (choice == ConflictChoice.TakeServer && !bundle.IsReadOnly) CommitLocked(project, bundle, null, null);
IReadOnlySet<string> takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet<string>();
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken).ConfigureAwait(false);
ProjectSettings? kept = null;
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled);
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled)
{
Kept = conflict is null ? kept : null,
};
}
}
/// <summary>
/// Brings version <paramref name="versionId"/> back as the working state (from this workspace, else from
/// <paramref name="home"/>); the next version saved is a "restore" of it. Uncommitted changes are saved as a version
/// first, so a restore loses nothing.
/// <paramref name="home"/>); the next version saved is a "restore" of it while the working state still holds it.
/// Uncommitted changes are saved as a version first, also while a conflict waits (settling it replays that version
/// too), so a restore loses nothing.
/// </summary>
/// <exception cref="ProjectException">Neither this workspace nor the home has the version.</exception>
public async Task<ProjectSettings> RestoreAsync(Guid projectId, Guid versionId, IProjectHome? home = null, CancellationToken cancellationToken = default)
@@ -300,11 +331,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
if (version is null) throw new ProjectException(ProjectError.NotFound, $"Version {versionId} of project {projectId} is not known here.");
var bundle = project.ReadBundle();
if (bundle.IsReadOnly) throw ReadOnly(projectId);
if (project.ReadSync().Conflict is null) CommitLocked(project, bundle, null, null);
CommitLocked(project, bundle, null, null);
var media = project.ReadMedia();
var working = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, media?.Photos ?? NoFrames), allowNewer: true);
project.WriteBundle(project.ReadBundle() with { Project = working, Saved = DateTimeOffset.UtcNow });
project.WriteSync(project.ReadSync() with { RestoredFrom = versionId });
var photos = media?.Photos ?? NoFrames;
var working = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, photos), allowNewer: true);
bundle = project.ReadBundle();
project.WriteBundle(bundle with { Project = working, Saved = DateTimeOffset.UtcNow });
// Restoring what the project already is changes nothing: the next version is no restore.
if (Tip(project, bundle, Unpushed(project, bundle)) is not { } tip || !ProjectContent.SameChoices(tip.SettingsJson, version.SettingsJson, photos))
project.WriteSync(project.ReadSync() with { RestoredFrom = versionId });
return working;
}
}
@@ -328,10 +363,13 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// <summary>
/// Pulls the home's versions after the base and replays the local ones and the working state on its head. With
/// <paramref name="takeTheirs"/> null a conflict is recorded and nothing else moves (the versions pulled are kept as
/// files); with a choice the conflicts are settled by it.
/// files); with a choice the conflicts are settled by it, only when they are those <paramref name="shown"/> lists
/// (others are recorded and asked about first, as without a choice). Taking the server's everywhere tells
/// <paramref name="kept"/> the working state as keeping this side's values would have left it (placed).
/// </summary>
private async Task<(ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList<ProjectVersion> Pulled)> ReplayAsync(WorkspaceProject project,
ProjectBundle bundle, IProjectHome home, IReadOnlySet<string>? takeTheirs, CancellationToken cancellationToken)
ProjectBundle bundle, IProjectHome home, IReadOnlySet<string>? takeTheirs, CancellationToken cancellationToken, WorkspaceConflict? shown = null,
Action<ProjectSettings>? kept = null)
{
var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false);
// Read before the home's versions are written over them: a local commit the home stored although its answer was lost
@@ -369,12 +407,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var choice = takeTheirs is EveryPath ? null : takeTheirs;
List<ProjectSettings> mine = [.. locals.Select(v => ProjectMerge.Read(ProjectContent.Rebind(v.SettingsJson, photos)))];
var replay = ProjectMerge.Replay(basisSettings, mine, headSettings, choice);
// This side's values wherever both changed: what taking the server's everywhere replaces (the editor's undo of it).
var keptTip = replay.Commits.Count > 0 ? replay.Commits[^1] : headSettings;
if (takeTheirs is EveryPath) replay = ProjectMerge.Replay(basisSettings, mine, headSettings, Paths(replay.Conflicts));
var newTip = replay.Commits.Count > 0 ? replay.Commits[^1] : headSettings;
var merge = ProjectMerge.MergeWithConflicts(tip, working, newTip, choice);
var keptMine = takeTheirs is EveryPath ? ProjectMerge.MergeWithConflicts(tip, working, keptTip).Merged : merge.Merged;
if (takeTheirs is EveryPath) merge = ProjectMerge.MergeWithConflicts(tip, working, newTip, Paths(merge.Conflicts));
var differences = replay.Conflicts.Concat(merge.Conflicts).DistinctBy(d => d.Path).ToList();
if (differences.Count > 0 && takeTheirs is null)
if (differences.Count > 0 && (takeTheirs is null || (shown is not null && !SameDifferences(shown.Differences, differences))))
{
var conflict = new WorkspaceConflict(head.Id, head.Number, basis?.Id ?? Guid.Empty, [.. locals.Select(v => v.Id)], differences, DateTimeOffset.UtcNow);
project.WriteSync(project.ReadSync() with { Conflict = conflict });
@@ -403,6 +444,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var placed = ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(merge.Merged.ToJson()), photos), allowNewer: true);
var moved = WithBase(bundle, head) with { Project = placed, Saved = DateTimeOffset.UtcNow };
project.WriteBundle(moved);
if (takeTheirs is EveryPath && kept is not null)
kept(ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(keptMine.ToJson()), photos), allowNewer: true));
return (moved, null, theirs);
}
@@ -473,13 +516,17 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
: (workspace.Base?.VersionId, workspace.Base?.Number ?? 0, workspace.Base?.ContentHash);
if (hash == tipHash || SameAsTip(project, bundle, locals, portable, media)) return null;
var sync = project.ReadSync();
// A restore taken back since (Undo, Ctrl+Z) no longer shows in the working state: that version is no restore.
var restoredFrom = sync.RestoredFrom is { } restored && StillRestores(project, restored, Tip(project, bundle, locals), portable, media?.Photos ?? NoFrames)
? sync.RestoredFrom
: null;
var version = new ProjectVersion
{
Id = Guid.CreateVersion7(),
ProjectId = project.ProjectId,
ParentId = tipId,
Number = tipNumber + 1,
Kind = sync.RestoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore,
Kind = restoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore,
Author = author,
ClientName = clientName,
Note = string.IsNullOrWhiteSpace(note) ? null : note.Trim(),
@@ -487,7 +534,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
ContentHash = hash,
SettingsFormat = ProjectContent.FormatOf(portable),
FramesVersion = media?.FramesVersion ?? workspace.Media.FramesVersion,
RestoredFrom = sync.RestoredFrom,
RestoredFrom = restoredFrom,
CreatedAt = DateTimeOffset.UtcNow,
};
project.WriteVersion(version);
@@ -547,10 +594,34 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
private static bool SameAsTip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals, string portable, WorkspaceMedia? media)
{
if (media is null) return false;
var tip = locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) : null;
return tip is not null && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
return Tip(project, bundle, locals) is { } tip && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
}
/// <summary>The last version: the newest local one, else the base (null when this workspace keeps neither).</summary>
private static ProjectVersion? Tip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals) =>
locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) : null;
/// <summary>
/// Whether the working state <paramref name="portable"/>, saved as a version on <paramref name="parent"/>, still restores
/// version <paramref name="restoredId"/>: that version differs from the parent, and the working state holds its values
/// wherever it does. A restore taken back since (the toast's Undo, Ctrl+Z) is no restore; one whose version this
/// workspace no longer keeps counts as recorded.
/// </summary>
private static bool StillRestores(WorkspaceProject project, Guid restoredId, ProjectVersion? parent, string portable, FramePhotos photos)
{
if (project.Versions().FirstOrDefault(v => v.Id == restoredId) is not { } restored) return true;
var then = ProjectContent.Rebind(restored.SettingsJson, photos);
var before = parent is null ? ProjectContent.ToPortable(new ProjectSettings(), photos) : ProjectContent.Rebind(parent.SettingsJson, photos);
if (then == before) return false;
var now = ProjectContent.Rebind(portable, photos);
var merge = ProjectMerge.MergeWithConflicts(ProjectMerge.Read(before), ProjectMerge.Read(now), ProjectMerge.Read(then));
return merge.Conflicts.Count == 0 && ProjectContent.Canonical(merge.Merged.ToJson()) == ProjectContent.Canonical(now);
}
/// <summary>Whether two conflicts list the same settings with the same values on each side (in any order).</summary>
private static bool SameDifferences(IReadOnlyList<SettingsDifference> shown, IReadOnlyList<SettingsDifference> found) =>
shown.Count == found.Count && shown.ToHashSet().SetEquals(found);
/// <summary>The local versions after the base, oldest first, each the child of the one before (the base's for the first).</summary>
private static List<ProjectVersion> Unpushed(WorkspaceProject project, ProjectBundle bundle)
{