fix: full verification against the brief — 137 confirmed findings fixed over six passes #4

Merged
Alby96 merged 169 commits from claude/keen-knuth-uhxv8w into main 2026-10-13 12:10:04 +02:00
3 changed files with 128 additions and 24 deletions
Showing only changes of commit f2ce806d56 - Show all commits

No files matched your search

+36 -9
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,38 @@ 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);
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;
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.</summary>
private List<int> Holding(FramePhoto photo)
{
var holding = new List<int>(1);
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) holding.AddRange(hashed);
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding;
// Frames not hashed on one side match by size and capture time; a hashed photo's hashed frames are listed already.
foreach (var index in candidates)
{
if (_byIndex[index]!.Matches(photo)) return index;
var frame = _byIndex[index]!;
if (!(photo.IsHashed && frame.IsHashed) && frame.Matches(photo)) holding.Add(index);
}
return null;
if (holding.Count > 1) holding.Sort();
return holding;
}
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
+29 -15
View File
@@ -59,6 +59,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; }
@@ -347,7 +354,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 +379,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 +418,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 +434,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 +457,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 +465,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 +482,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 +532,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 +558,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 +579,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; }
}
@@ -283,6 +283,69 @@ public sealed class ProjectSettingsTests
Assert.Null(cleared.PendingFrames);
}
[Test]
public void APhotoHeldTwiceKeepsEachCopysPositions()
{
// The server adopted a folder holding photo 1 twice ('IMG_0001.JPG', 'IMG_0001 (1).JPG'); this PC keeps it once.
var photos = Enumerable.Range(0, 3).Select(Photo).ToList();
var server = Frames([photos[0], photos[1], photos[1], photos[2]]);
var here = Frames(photos);
var project = new ProjectSettings
{
ExcludedFrames = [2],
HeroFrame = 2,
Camera = new CameraSettings { Keyframes = [new CameraKey(1, 0.1, 0.5, 1, 0), new CameraKey(2, 0.2, 0.5, 1, 0)] },
};
// The second copy, which the deep analysis flags as a duplicate, is excluded there: this PC still renders its only copy.
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
Assert.Empty(taken.ExcludedFrames);
Assert.Null(taken.HeroFrame);
Assert.SequenceEqual([1], taken.Camera.Keyframes.Select(k => k.Frame));
Assert.Equal(0.1, taken.Camera.Keyframes[0].CenterX);
var waiting = Assert.NotNull(taken.PendingFrames);
Assert.Count(1, waiting);
Assert.True(waiting[0] is { Excluded: true, Hero: true, Occurrence: 1 } && waiting[0].Sha256 == photos[1].Sha256);
Assert.Count(1, waiting[0].Keyframes);
Assert.Equal(0.2, waiting[0].Keyframes[0].CenterX);
Assert.Contains("\"occurrence\": 1", taken.ToJson());
// Sent back after an edit of something else, every position lands on the copy it named.
var edited = taken with { Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 } };
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(edited.ToJson(), here, server)));
Assert.SequenceEqual([2], back.ExcludedFrames);
Assert.Equal(2, back.HeroFrame);
Assert.SequenceEqual([1, 2], back.Camera.Keyframes.Select(k => k.Frame));
Assert.SequenceEqual([0.1, 0.2], back.Camera.Keyframes.Select(k => k.CenterX));
Assert.Null(back.PendingFrames);
// Both copies excluded there: this PC excludes its copy, and the server gets both back.
var both = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(new ProjectSettings { ExcludedFrames = [1, 2] }.ToJson(), server, here)));
Assert.SequenceEqual([1], both.ExcludedFrames);
var excluded = Assert.NotNull(both.PendingFrames);
Assert.Count(1, excluded);
Assert.True(excluded[0] is { Excluded: true, Hero: false, Occurrence: 1 });
var returned = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(both.ToJson(), here, server)));
Assert.SequenceEqual([1, 2], returned.ExcludedFrames);
Assert.Null(returned.PendingFrames);
}
[Test]
public void TheCopiesOfAPhotoAreToldApartByTheirOrder()
{
var time = new DateTime(2026, 9, 28, 18, 0, 0);
var hashed = new FramePhoto(new string('a', 64), 100, time);
var other = new FramePhoto(new string('b', 64), 200, time);
// The copy at 3 has not been hashed yet: it is the same photo by size and capture time.
var frames = Frames([other, hashed, other, new FramePhoto(null, 100, time), hashed]);
Assert.SequenceEqual([1, 3, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(hashed, n)).OfType<int>());
Assert.Null(frames.IndexOf(hashed, 3));
Assert.SequenceEqual([0, 0, 1, 1, 2], Enumerable.Range(0, 5).Select(frames.Occurrence));
Assert.Equal(0, frames.Occurrence(9));
Assert.Equal(2, frames.IndexOf(new FramePhoto(new string('B', 64), 0, null), 1));
}
[Test]
public void AProjectNamingNoFrameCrossesAsItIs()
{