Merge branch 'review2-p5a-fix2-desktop' into review2-p5a-int2

# Conflicts:
#	i18n/en/Editor.resx
#	i18n/en/Tooltips.resx
#	i18n/it/Tooltips.it.resx
#	src/Chronos.Data/Workspaces/WorkspaceSync.cs
#	tests/Chronos.Data.Tests/Workspaces/WorkspaceRecoveryTests.cs
This commit is contained in:
Alby96 committed 2026-11-03 12:15:27 +01:00
commit 47c660735f
30 files changed
+1021 -110

No files matched your search

+57 -36
View File
@@ -125,8 +125,9 @@ public sealed class WorkspaceSyncException(PushOutcome outcome, string message)
/// (hashed or converted there since the copy), so every side names one photo alike. Settings of a newer Chronos are never
/// merged with this one's: a project with nothing of its own moves onto them read-only, any other waits on a conflict of
/// format that only Save both settles (<see cref="WorkspaceConflict.NewerFormat"/>). 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.
/// lease; nothing is ever deleted: a restore or a save-both first saves uncommitted changes as a version, and taking the
/// server's values keeps the working state they replace aside (<c>versions/.replaced</c>). A conflict lists this side's
/// values as the working state holds them, so it is asked and settled against what the user sees.
/// Every write sequence survives a cut at any step: the working state's base is the commit point. A push moves the base
/// before its local file reads as sent; a replay journals itself in <c>sync.json</c> (<see cref="WorkspaceReplay"/>), writes
/// the replayed versions, then the base, and only then sets the old ones aside; a save-both journals its copy and the ids
@@ -348,10 +349,10 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// 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; so is one the home holds no
/// versions for now (nothing to take them from). 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>); replayed, that version holds the server's
/// values only and is not sent. After a Save both cut short, either choice first sends the rest of this workspace's
/// versions to the copy it made (one somebody else pushed to since is left as it is).
/// versions for now (nothing to take them from). Taking the server's keeps the working state it replaces as a version
/// file in <c>versions/.replaced</c>, outside the chain: nothing is committed, replayed or sent for it, so a take that
/// leaves only the server's values sends nothing. After a Save both cut short, either choice first sends the rest of
/// this workspace's versions to the copy it made (one somebody else pushed to since is left as it is).
/// <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.
@@ -371,12 +372,11 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
EnsureChained(project, bundle, Unpushed(project, bundle));
if (choice == ConflictChoice.SaveBoth)
return await SaveBothAsync(project, bundle, home, saveBothName, cancellationToken).ConfigureAwait(false);
// A conflict of format refuses the choice (nothing is merged with a newer Chronos's settings): nothing is saved first.
var saved = choice == ConflictChoice.TakeServer && !bundle.IsReadOnly && !recorded.NewerFormat ? CommitLocked(project, bundle, null, null) : null;
// Nothing is committed first: a version of the working state would be replayed into one that only repeats the
// head, and sent. What the take replaces of the working state is kept aside by the replay instead.
IReadOnlySet<string> takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet<string>();
ProjectSettings? kept = null;
var (_, conflict, pulled) = await ReplayAsync(project, project.ReadBundle(), home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k, saved?.Id)
.ConfigureAwait(false);
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)
{
@@ -445,16 +445,16 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// files; the base only moves onto local commits the home already holds); 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); <paramref name="saved"/> names the version taking them saved first, which is not replayed
/// when it then holds nothing its parent does not (its file is set aside with the others, for the undo). A conflict
/// recorded before stays when the home holds no version after the base (it lost them: nothing to settle it against).
/// would have left it (placed), and the working state it replaces is kept aside (<c>versions/.replaced</c>), never in the
/// chain. A conflict recorded before stays when the home holds no version after the base (it lost them: nothing to
/// settle it against).
/// Written in steps a cut can stop at any point (<see cref="WorkspaceReplay"/>): the journal, the replayed versions, the
/// base with the merged working state (the commit point), then the old versions set aside and the held ones adopted.
/// Before the commit point the rest of a Save both cut short goes to its copy (<see cref="FinishCopyAsync"/>).
/// </summary>
private async Task<(ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList<ProjectVersion> Pulled)> ReplayAsync(WorkspaceProject project,
ProjectBundle bundle, IProjectHome home, IReadOnlySet<string>? takeTheirs, CancellationToken cancellationToken, WorkspaceConflict? shown = null,
Action<ProjectSettings>? kept = null, Guid? saved = null)
Action<ProjectSettings>? kept = null)
{
var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false);
// A local commit the home stored although its answer was lost comes back among the home's versions, and the commits
@@ -507,7 +507,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
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();
var differences = AsInTheWorkingState(replay.Conflicts.Concat(merge.Conflicts).DistinctBy(d => d.Path),
ProjectMerge.Conflicts(basisSettings, working, headSettings));
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);
@@ -517,12 +518,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
var rebased = new List<ProjectVersion>(locals.Count);
ProjectVersion parent = head;
var previous = headSettings;
for (var i = 0; i < locals.Count; i++)
{
var local = locals[i];
// The version taking the server's saved first, replayed onto their values, holds nothing more: not sent (kept aside for the undo).
if (local.Id == saved && ProjectMerge.SameValues(replay.Commits[i], previous)) continue;
var portable = ProjectContent.Canonical(replay.Commits[i].ToJson());
parent = local with
{
@@ -535,12 +533,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
StoredAt = null,
};
rebased.Add(parent);
previous = replay.Commits[i];
}
if (differences.Count > 0 && rebased.Count > 0) rebased[^1] = rebased[^1] with { Kind = ProjectVersionKind.Merge };
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 };
await FinishCopyAsync(project, home, chain, cancellationToken).ConfigureAwait(false);
// Taking the server's values over changes no version holds yet: the working state is kept aside first, never in the chain.
if (takeTheirs is EveryPath && differences.Count > 0 && Portable(bundle.Project, media) is { } unsaved
&& (tipVersion is null || !ProjectContent.SameChoices(tipVersion.SettingsJson, unsaved, photos)))
project.WriteAside(VersionOf(project, unsaved, tipVersion, ProjectVersionKind.Commit, null, null, null, media?.FramesVersion ?? bundle.Workspace!.Media.FramesVersion));
var journal = new WorkspaceReplay(head.Id, [.. locals.Select(v => v.Id)], [.. rebased.Select(v => v.Id)], [.. held.Select(v => v.Id)]);
project.WriteSync(project.ReadSync() with { Replay = journal });
Step("replay.journal");
@@ -920,28 +921,37 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
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 = restoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore,
Author = author,
ClientName = clientName,
Note = string.IsNullOrWhiteSpace(note) ? null : note.Trim(),
SettingsJson = portable,
ContentHash = hash,
SettingsFormat = ProjectContent.FormatOf(portable),
FramesVersion = media?.FramesVersion ?? workspace.Media.FramesVersion,
RestoredFrom = restoredFrom,
CreatedAt = DateTimeOffset.UtcNow,
};
var version = VersionOf(project, portable, (tipId, tipNumber), restoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore, note, author,
restoredFrom, media?.FramesVersion ?? workspace.Media.FramesVersion);
project.WriteVersion(version);
if (sync.RestoredFrom is not null) project.WriteSync(sync with { RestoredFrom = null });
return version;
}
/// <summary>A version of this workspace holding <paramref name="portable"/>, the child of <paramref name="parent"/> (null: the first).</summary>
private ProjectVersion VersionOf(WorkspaceProject project, string portable, ProjectVersion? parent, ProjectVersionKind kind, string? note, string? author,
Guid? restoredFrom, int framesVersion) =>
VersionOf(project, portable, (parent?.Id, parent?.Number ?? 0), kind, note, author, restoredFrom, framesVersion);
private ProjectVersion VersionOf(WorkspaceProject project, string portable, (Guid? Id, int Number) parent, ProjectVersionKind kind, string? note, string? author,
Guid? restoredFrom, int framesVersion) => new()
{
Id = Guid.CreateVersion7(),
ProjectId = project.ProjectId,
ParentId = parent.Id,
Number = parent.Number + 1,
Kind = kind,
Author = author,
ClientName = clientName,
Note = string.IsNullOrWhiteSpace(note) ? null : note.Trim(),
SettingsJson = portable,
ContentHash = ProjectContent.Hash(portable),
SettingsFormat = ProjectContent.FormatOf(portable),
FramesVersion = framesVersion,
RestoredFrom = restoredFrom,
CreatedAt = DateTimeOffset.UtcNow,
};
/// <summary>
/// Where the project stands. With a <paramref name="home"/>, the media first learn how it names its photos now (a photo
/// hashed or converted there since the copy, <see cref="WorkspaceMedia.NamedAsAt"/>), so a version the home made since
@@ -1099,6 +1109,17 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
return merge.Conflicts.Count == 0 && ProjectContent.Canonical(merge.Merged.ToJson()) == ProjectContent.Canonical(now);
}
/// <summary>
/// The conflicts found as the user sees them: where the working state holds a value of its own against the home's
/// (<paramref name="working"/>: edited since the last local version), that value is this side's, not the version's.
/// Keeping mine keeps it, and taking the server's replaces it, so one answer settles what was listed.
/// </summary>
private static List<SettingsDifference> AsInTheWorkingState(IEnumerable<SettingsDifference> found, IReadOnlyList<SettingsDifference> working)
{
var now = working.DistinctBy(d => d.Path).ToDictionary(d => d.Path, StringComparer.Ordinal);
return [.. found.Select(d => now.TryGetValue(d.Path, out var held) ? held : d)];
}
/// <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);