fix(sync): let a photo's hash decide which frames hold it

Telling copies of a photo apart counted a frame not hashed yet with the
same size and capture time as a hashed photo's copy, although it may be
another photo (a RAW of constant size in a burst without sub-second
times). Positions then shifted between copies: an exclusion waited forever
as a pending frame, or landed on the other photo. A hashed photo is now the
frames holding its hash; size and capture time count only when no frame
holds it.

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 19:59:54 +00:00
1 parent 9ca10d96f4
commit 991742462b
2 files changed
+45 -11

No files matched your search

+7 -6
View File
@@ -84,19 +84,20 @@ public sealed class FramePhotos
/// </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>
/// <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;
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)
{
var frame = _byIndex[index]!;
if (!(photo.IsHashed && frame.IsHashed) && frame.Matches(photo)) holding.Add(index);
if (_byIndex[index]!.Matches(photo)) holding.Add(index);
}
if (holding.Count > 1) holding.Sort();
return holding;
}
@@ -377,14 +377,47 @@ public sealed class ProjectSettingsTests
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]);
var unhashed = new FramePhoto(null, 100, time);
// The frame at 3 has not been hashed yet: it may be a copy of the photo, or another photo of the same size taken in
// the same second. The frames with the photo's hash are its copies; size and capture time match the unhashed one.
var frames = Frames([other, hashed, other, unhashed, 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.SequenceEqual([1, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(hashed, n)).OfType<int>());
Assert.Null(frames.IndexOf(hashed, 2));
Assert.SequenceEqual([1, 3, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(unhashed, n)).OfType<int>());
Assert.SequenceEqual([0, 0, 1, 1, 1], 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));
// Without a frame holding its hash, a hashed photo is the frames not hashed yet with its size and capture time.
Assert.Equal(3, frames.IndexOf(new FramePhoto(new string('c', 64), 100, time)));
}
[Test]
public void APhotoNotHashedYetDoesNotTakeThePositionsOfAnotherWithItsSizeAndTime()
{
// Two photos of a burst share size and capture time (a RAW of constant size, no sub-second times). The server has
// not hashed the first yet; this PC has hashed both and does not have the third photo yet.
var time = new DateTime(2026, 9, 28, 18, 0, 0);
var (first, second) = (new FramePhoto(new string('a', 64), 100, time), new FramePhoto(new string('b', 64), 100, time));
var server = Frames([first with { Sha256 = null }, second, Photo(3)]);
var here = Frames([first, second]);
// The second photo excluded on the server is the second here, and back; the first stays the hero on both.
var project = new ProjectSettings { ExcludedFrames = [1, 2], HeroFrame = 0 };
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
Assert.SequenceEqual([1], taken.ExcludedFrames);
Assert.Equal(0, taken.HeroFrame);
var waiting = Assert.NotNull(taken.PendingFrames);
Assert.True(waiting is [{ Excluded: true, Occurrence: 0 }] && waiting[0].Sha256 == Photo(3).Sha256, "only the third photo waits");
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(taken.ToJson(), here, server)));
Assert.SequenceEqual([1, 2], back.ExcludedFrames);
Assert.Equal(0, back.HeroFrame);
Assert.Null(back.PendingFrames);
// A copy that has not hashed its first frame still finds the second photo by its hash, not on that frame.
Assert.Equal(1, server.IndexOf(second));
Assert.Equal(0, server.Occurrence(1));
Assert.Equal(0, server.IndexOf(first));
}
[Test]