merge: third-pass sync and editor fixes (W-2, W-3, W-4, W-16)

Merge branch fix3/sync-editor into the verification branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 20:02:15 +00:00
commit 8897363e60
21 files changed
+1038 -79

No files matched your search

+36 -8
View File
@@ -26,12 +26,13 @@ public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt)
/// <summary>
/// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among
/// them. Immutable once built, so parallel workers may share it.
/// them. A copy may hold one photo more than once (a folder adopted with 'IMG_0001.JPG' and 'IMG_0001 (1).JPG'); those
/// frames are told apart by their order (<see cref="Occurrence"/>). Immutable once built, so parallel workers may share it.
/// </summary>
public sealed class FramePhotos
{
private readonly FramePhoto?[] _byIndex;
private readonly Dictionary<string, int> _bySha = new(StringComparer.Ordinal);
private readonly Dictionary<string, List<int>> _bySha = new(StringComparer.Ordinal);
private readonly Dictionary<(long Size, long Time), List<int>> _bySizeAndTime = [];
/// <param name="frames">Each frame's index and photo, in any order.</param>
@@ -44,7 +45,12 @@ public sealed class FramePhotos
for (var i = 0; i < _byIndex.Length; i++)
{
if (_byIndex[i] is not { } photo) continue;
if (photo.IsHashed) _bySha.TryAdd(photo.Sha256!.ToLowerInvariant(), i);
if (photo.IsHashed)
{
var sha = photo.Sha256!.ToLowerInvariant();
if (!_bySha.TryGetValue(sha, out var copies)) _bySha[sha] = copies = [];
copies.Add(i);
}
if (photo.CapturedAt is not { } time) continue;
var key = (photo.Size, FramePhoto.Millisecond(time));
if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = [];
@@ -60,17 +66,39 @@ public sealed class FramePhotos
/// <summary>Some frame is not hashed yet: matching it needs capture times.</summary>
public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false });
/// <summary>The index of the first frame holding <paramref name="photo"/> (<see cref="FramePhoto.Matches"/>); null when none does.</summary>
public int? IndexOf(FramePhoto photo)
/// <summary>
/// The index of the first frame holding <paramref name="photo"/> (<see cref="FramePhoto.Matches"/>), or with
/// <paramref name="occurrence"/> n of the (n+1)-th in index order; null when fewer frames hold it.
/// </summary>
public int? IndexOf(FramePhoto photo, int occurrence = 0)
{
ArgumentNullException.ThrowIfNull(photo);
ArgumentOutOfRangeException.ThrowIfNegative(occurrence);
var holding = Holding(photo);
return occurrence < holding.Count ? holding[occurrence] : null;
}
/// <summary>
/// Which of the frames holding the photo at <paramref name="index"/> it is, in index order: 0 for the first (and for
/// a photo held once), 1 for the second, … A position translated to another copy names the same one there.
/// </summary>
public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0;
/// <summary>
/// The indices of the frames holding <paramref name="photo"/>, ascending. A hashed photo is the frames with its SHA-256
/// when there are any: a frame not hashed yet with the same size and capture time may be another photo (a RAW of
/// constant size in a burst without sub-second times), so it counts only when no frame has the hash.
/// </summary>
private List<int> Holding(FramePhoto photo)
{
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed;
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return null;
var holding = new List<int>(1);
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding;
foreach (var index in candidates)
{
if (_byIndex[index]!.Matches(photo)) return index;
if (_byIndex[index]!.Matches(photo)) holding.Add(index);
}
return null;
return holding;
}
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
+63 -15
View File
@@ -1,6 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Titano.Core.Model;
namespace Titano.Core.Project;
@@ -59,6 +60,13 @@ public sealed record PendingFrame
public long Size { get; init; }
public DateTime? CapturedAt { get; init; }
/// <summary>
/// Which of the frames holding the photo it names, in capture order, when a copy holds the photo more than once
/// (<see cref="FramePhotos.Occurrence"/>): 0, the first, is not written.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int Occurrence { get; init; }
/// <summary>The photo is excluded from renders.</summary>
public bool Excluded { get; init; }
@@ -317,6 +325,39 @@ public sealed record ProjectSettings
}
}
/// <summary>
/// A project written against the frames <paramref name="before"/> (frame ids, by index) moved to the frames
/// <paramref name="after"/> (in index order) the way the archive index moves its own copy when it takes them
/// (ProjectPositions in Titano.Data): while every frame of <paramref name="before"/> is still there, its positions
/// move with their frames (<see cref="MoveFramePositions"/>), otherwise they stay; either way pending frames whose
/// photos are now there take their place (<see cref="TranslateFramePositions"/>). An editor that read the frames
/// before frames were inserted saves its edit in the new order this way. Null when nothing changes.
/// </summary>
public static string? FollowFrames(string json, IReadOnlyList<Guid> before, IReadOnlyList<Frame> after)
{
ArgumentNullException.ThrowIfNull(before);
ArgumentNullException.ThrowIfNull(after);
var now = new Dictionary<Guid, int>(after.Count);
for (var i = 0; i < after.Count; i++) now[after[i].Id] = i;
Dictionary<int, int>? moves = [];
for (var i = 0; i < before.Count; i++)
{
if (!now.TryGetValue(before[i], out var index))
{
moves = null;
break;
}
if (index != i) moves[i] = index;
}
var moved = moves is null ? null : MoveFramePositions(json, moves);
var current = moved ?? json;
// Most projects wait for no photo: the frames' photos are gathered only for those that do.
var placed = current.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase)
? TranslateFramePositions(current, null, new FramePhotos(after.Select((f, i) => (i, new FramePhoto(f.Sha256, f.Size, f.CapturedAt)))))
: null;
return placed ?? moved;
}
private static bool Move(JsonNode? position, Action<JsonNode> replace, IReadOnlyDictionary<int, int> moves)
{
if (position is not JsonValue value || !value.TryGetValue<int>(out var old) || !moves.TryGetValue(old, out var now) || now == old) return false;
@@ -347,7 +388,9 @@ public sealed record ProjectSettings
/// Translates the frame positions of a project written against the frames of another copy of the sequence
/// (<paramref name="from"/>; a server sequence and a PC's copy hold the same photos, though not always all of them,
/// BRIEF §14.5) to the frames of <paramref name="to"/>, by photo: each exclusion, the hero and each camera keyframe
/// moves to the frame of <paramref name="to"/> holding its photo. One whose photo <paramref name="to"/> lacks becomes
/// moves to the frame of <paramref name="to"/> holding its photo, and a position on the n-th frame holding a photo
/// <paramref name="from"/> holds more than once to the n-th such frame (<see cref="FramePhotos.Occurrence"/>), so
/// duplicates stay apart. One whose photo (or that copy of it) <paramref name="to"/> lacks becomes
/// a <see cref="PendingFrame"/>, and pending frames whose photo <paramref name="to"/> holds become positions again; a
/// pending hero only while no other hero is chosen (a hero chosen since one was left pending is newer, so that one no
/// longer waits even when the chosen one must wait in turn), a pending keyframe only where no keyframe is. With
@@ -370,7 +413,7 @@ public sealed record ProjectSettings
foreach (var node in stored)
{
// An entry naming no photo is not this Titano's to place: it stays as it is.
if (node is JsonObject entry && PhotoOf(entry) is { } photo) pending.Add(new Parked(photo, entry.DeepClone().AsObject()));
if (node is JsonObject entry && PhotoOf(entry) is { } photo) pending.Add(new Parked(photo, OccurrenceOf(entry), entry.DeepClone().AsObject()));
else unreadable.Add(node?.DeepClone());
}
}
@@ -409,14 +452,14 @@ public sealed record ProjectSettings
{
if (!IsPosition(node, out var at)) kept.Add(node?.DeepClone());
else if (from[at] is not { } photo) changed = true;
else if (to.IndexOf(photo) is { } now)
else if (to.IndexOf(photo, from.Occurrence(at)) is { } now)
{
kept.Add(JsonValue.Create(now));
changed |= now != at;
}
else
{
Park(pending, photo).Entry["excluded"] = true;
Park(pending, photo, from.Occurrence(at)).Entry["excluded"] = true;
changed = true;
}
}
@@ -425,8 +468,8 @@ public sealed record ProjectSettings
if (project["heroFrame"] is JsonValue hero && hero.TryGetValue<int>(out var heroAt))
{
var photo = from[heroAt];
var now = photo is null ? null : to.IndexOf(photo);
if (photo is not null && now is null) Park(pending, photo).Entry["hero"] = true;
var now = photo is null ? null : to.IndexOf(photo, from.Occurrence(heroAt));
if (photo is not null && now is null) Park(pending, photo, from.Occurrence(heroAt)).Entry["hero"] = true;
if (now != heroAt)
{
project["heroFrame"] = now;
@@ -448,7 +491,7 @@ public sealed record ProjectSettings
{
changed = true;
}
else if (to.IndexOf(photo) is { } now)
else if (to.IndexOf(photo, from.Occurrence(at)) is { } now)
{
copy["frame"] = now;
kept.Add(copy);
@@ -456,7 +499,7 @@ public sealed record ProjectSettings
}
else
{
var parked = Park(pending, photo).Entry;
var parked = Park(pending, photo, from.Occurrence(at)).Entry;
if (parked["keyframes"] is not JsonArray keys) parked["keyframes"] = keys = new JsonArray();
keys.Add(copy);
changed = true;
@@ -473,7 +516,7 @@ public sealed record ProjectSettings
var changed = false;
foreach (var parked in pending)
{
if (to.IndexOf(parked.Photo) is not { } at) continue;
if (to.IndexOf(parked.Photo, parked.Occurrence) is not { } at) continue;
parked.Placed = changed = true;
if (Flag(parked.Entry, "excluded"))
{
@@ -523,18 +566,19 @@ public sealed record ProjectSettings
}
}
/// <summary>The pending entry of <paramref name="photo"/>, added when there is none.</summary>
private static Parked Park(List<Parked> pending, FramePhoto photo)
/// <summary>The pending entry of <paramref name="photo"/> (the copy of it <paramref name="occurrence"/> names), added when there is none.</summary>
private static Parked Park(List<Parked> pending, FramePhoto photo, int occurrence)
{
if (pending.FirstOrDefault(p => p.Photo.Matches(photo)) is { } known) return known;
if (pending.FirstOrDefault(p => p.Occurrence == occurrence && p.Photo.Matches(photo)) is { } known) return known;
var entry = new JsonObject();
if (photo.IsHashed) entry["sha256"] = photo.Sha256!.ToLowerInvariant();
entry["size"] = photo.Size;
if (photo.CapturedAt is { } captured) entry["capturedAt"] = captured;
if (occurrence > 0) entry["occurrence"] = occurrence;
entry["excluded"] = false;
entry["hero"] = false;
entry["keyframes"] = new JsonArray();
var parked = new Parked(photo, entry);
var parked = new Parked(photo, occurrence, entry);
pending.Add(parked);
return parked;
}
@@ -548,6 +592,9 @@ public sealed record ProjectSettings
return string.IsNullOrEmpty(sha) && (captured is null || size < 0) ? null : new FramePhoto(sha, size, captured);
}
/// <summary>Which copy of its photo a pending entry names; the first when it says nothing (or nothing readable).</summary>
private static int OccurrenceOf(JsonObject entry) => entry["occurrence"] is JsonValue o && o.TryGetValue<int>(out var occurrence) && occurrence > 0 ? occurrence : 0;
private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue<bool>(out var set) && set;
/// <summary>A pending entry still has a position to give its photo.</summary>
@@ -566,10 +613,11 @@ public sealed record ProjectSettings
foreach (var node in copy) array.Add(node);
}
/// <summary>A pending entry being edited: its photo, its JSON, and whether its photo took its place.</summary>
private sealed class Parked(FramePhoto photo, JsonObject entry)
/// <summary>A pending entry being edited: its photo (and which copy of it), its JSON, and whether its photo took its place.</summary>
private sealed class Parked(FramePhoto photo, int occurrence, JsonObject entry)
{
public FramePhoto Photo { get; } = photo;
public int Occurrence { get; } = occurrence;
public JsonObject Entry { get; } = entry;
public bool Placed { get; set; }
}