fix(projects): one photo merges as one, newer settings are never merged

A workspace copied before the home hashed or converted a photo named it
differently from the versions the home made since, so a pull merged the
same photo as two: frame edits were silently undone and Keep mine placed
the library's value. Before replaying, the workspace media now learn the
home's hashes (same frame id, size and capture time) and conversion
aliases, so every side names each photo alike. Rebind also gives a
renamed entry the occurrence a placement looks for, so positions on
burst photos keep applying once hashed, and folds entries of one photo
(in Rebind and in the merge's keyed form) instead of keeping one.

Settings of a newer Chronos are no longer merged member by member with
this one's: ProjectMerge refuses when both sides changed. A workspace
pull keeps its local versions and working state untouched and records a
conflict of format that only Save both settles (into a new project of
this format); with nothing local it takes the newer head read-only.
Main's catch-up leaves a newer head and settings written meanwhile as
they are.

When Main's catch-up merge overrides values another writer stored, that
writer's settings are kept as an External version before the Merge one,
so nothing is lost from the history.

Unknown members inside Auto values, camera keyframes and curve points,
and on pending entries once placed, are kept like ProjectSettings' Extra.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 10:05:27 +01:00
1 parent 50e28fd63d
commit eac4a36a7b
12 files changed
+905 -38

No files matched your search

+39 -18
View File
@@ -22,6 +22,12 @@ public sealed record ProjectReplay(IReadOnlyList<ProjectSettings> Commits, IRead
public bool HasConflicts => Conflicts.Count > 0;
}
/// <summary>
/// A merge both sides of which changed met settings of a newer Chronos (<see cref="ProjectSettings.IsNewerFormat"/>): this
/// Chronos never merges them, as their known members may mean something else there (owner review 2, R8). Nothing was merged.
/// </summary>
public sealed class NewerSettingsException(string message) : InvalidOperationException(message);
/// <summary>
/// Three-way merge of projects: the editor's edit (<c>ours</c>) and a change stored meanwhile by someone else
/// (<c>theirs</c>: an analysis filling Auto values, another client through the server, another workspace through a
@@ -31,15 +37,24 @@ public sealed record ProjectReplay(IReadOnlyList<ProjectSettings> Commits, IRead
/// user instead (a server sequence, BRIEF §14.5; a workspace project, owner review 2, R8). Pending frames (frame
/// positions parked by photo, the portable form of <see cref="ProjectContent"/>) merge photo by photo and flag by flag, so
/// two people excluding different photos never conflict; two heroes chosen on both sides do (path <see cref="HeroPath"/>).
/// Members this Chronos does not know merge like the others and are kept.
/// Entries of one photo in one project (saved under two names, alike since) merge as one, keeping the positions of both.
/// Members this Chronos does not know merge like the others and are kept. Settings of a newer Chronos are never merged:
/// a side that did not change takes the other's as they are, anything else is refused (<see cref="NewerSettingsException"/>).
/// </summary>
public static class ProjectMerge
{
/// <summary>The path of the conflict of two heroes chosen among pending frames on both sides.</summary>
public const string HeroPath = "hero";
/// <summary>
/// The path of the settings format (<see cref="ProjectSettings.Version"/>): a difference there is settings of a newer
/// Chronos that this one cannot merge with its own (a workspace's conflict of format).
/// </summary>
public const string FormatPath = "version";
private const string PendingPath = "pendingFrames";
/// <exception cref="NewerSettingsException">Both sides changed, and one of the three is of a newer format.</exception>
public static ProjectSettings Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs) =>
MergeWithConflicts(basis, ours, theirs).Merged;
@@ -48,6 +63,7 @@ public static class ProjectMerge
/// conflicts. A conflict whose path is in <paramref name="takeTheirs"/> takes theirs (the user chose "take server"
/// there), every other one ours.
/// </summary>
/// <exception cref="NewerSettingsException">Both sides changed, and one of the three is of a newer format.</exception>
public static ProjectMergeResult MergeWithConflicts(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs, IReadOnlySet<string>? takeTheirs = null)
{
ArgumentNullException.ThrowIfNull(basis);
@@ -56,6 +72,14 @@ public static class ProjectMerge
var conflicts = new List<SettingsDifference>();
var choice = takeTheirs ?? EmptySet;
var (b, o, t) = (Keyed(basis), Keyed(ours), Keyed(theirs));
if (basis.IsNewerFormat || ours.IsNewerFormat || theirs.IsNewerFormat)
{
// Their known members may mean something else there: only a side that did not change gives way.
if (JsonNode.DeepEquals(o, b) || JsonNode.DeepEquals(t, o)) return new ProjectMergeResult(Read(theirs.ToJson()), []);
if (JsonNode.DeepEquals(t, b)) return new ProjectMergeResult(Read(ours.ToJson()), []);
throw new NewerSettingsException(string.Create(CultureInfo.InvariantCulture,
$"Settings of format {Math.Max(basis.Version, Math.Max(ours.Version, theirs.Version))} are not merged by this Chronos (format {ProjectSettings.CurrentVersion})."));
}
var merged = MergeNode(b, o, t, string.Empty, conflicts, choice) as JsonObject ?? new JsonObject();
OneHero(merged, o, t, conflicts, choice);
return new ProjectMergeResult(FromKeyed(merged), conflicts);
@@ -75,6 +99,7 @@ public static class ProjectMerge
/// history stays linear and the last one holds the net change. Conflicts are those of the last commit; their paths in
/// <paramref name="takeTheirs"/> take theirs in every commit.
/// </summary>
/// <exception cref="NewerSettingsException">A commit and theirs both changed, and one of them (or the basis) is of a newer format.</exception>
public static ProjectReplay Replay(ProjectSettings basis, IReadOnlyList<ProjectSettings> mine, ProjectSettings theirs, IReadOnlySet<string>? takeTheirs = null)
{
ArgumentNullException.ThrowIfNull(mine);
@@ -157,14 +182,22 @@ public static class ProjectMerge
private static string? HeroOf(JsonObject project) =>
project[PendingPath] is JsonObject pending ? pending.FirstOrDefault(p => p.Value is JsonObject entry && Flag(entry, "hero")).Key : null;
/// <summary>The project as JSON with its pending frames as an object keyed by photo (<see cref="KeyOf"/>).</summary>
/// <summary>
/// The project as JSON with its pending frames as an object keyed by photo (<see cref="KeyOf"/>); entries of one photo
/// (saved under two names, alike since) are one, with the positions of both (<see cref="ProjectContent.Fold"/>).
/// </summary>
private static JsonObject Keyed(ProjectSettings settings)
{
var project = JsonNode.Parse(settings.ToJson())!.AsObject();
if (project[PendingPath] is JsonArray pending)
{
var keyed = new JsonObject();
foreach (var entry in pending) keyed[KeyOf(entry)] = entry?.DeepClone();
foreach (var entry in pending)
{
var key = KeyOf(entry);
if (keyed[key] is JsonObject known && entry is JsonObject other) ProjectContent.Fold(known, other);
else keyed[key] = entry?.DeepClone();
}
project[PendingPath] = keyed;
}
return project;
@@ -188,22 +221,10 @@ public static class ProjectMerge
}
/// <summary>
/// The key of a pending entry: its photo and which copy of it ("sha:…#0", or "photo:size@ticks#0" for a photo not hashed),
/// or its JSON for an entry naming no photo.
/// The key of a pending entry: its photo and which copy of it (<see cref="ProjectContent.PhotoKey"/>: "sha:…#0", or
/// "photo:size@ticks#0" for a photo not hashed), or its JSON for an entry naming no photo.
/// </summary>
private static string KeyOf(JsonNode? entry)
{
if (entry is JsonObject e)
{
var occurrence = e["occurrence"] is JsonValue o && o.TryGetValue<int>(out var n) ? n : 0;
if (e["sha256"] is JsonValue s && s.TryGetValue<string>(out var sha) && sha.Length > 0)
return string.Create(CultureInfo.InvariantCulture, $"sha:{sha.ToLowerInvariant()}#{occurrence}");
var size = e["size"] is JsonValue z && z.TryGetValue<long>(out var bytes) ? bytes : 0;
if (e["capturedAt"] is JsonValue c && c.TryGetValue<DateTime>(out var at))
return string.Create(CultureInfo.InvariantCulture, $"photo:{size}@{at.Ticks}#{occurrence}");
}
return "raw:" + (entry?.ToJsonString() ?? "null");
}
private static string KeyOf(JsonNode? entry) => ProjectContent.PhotoKey(entry) ?? "raw:" + (entry?.ToJsonString() ?? "null");
/// <summary>The entry of the photo of <paramref name="template"/> holding no position; null for an entry naming no photo.</summary>
private static JsonNode? Unplaced(JsonNode? template)