From 991742462bfb0b754c4a4ba6c9d7e7fc76ddebb1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:59:54 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Core/Project/FramePhotos.cs | 13 +++--- .../Project/ProjectSettingsTests.cs | 43 ++++++++++++++++--- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/Titano.Core/Project/FramePhotos.cs b/src/Titano.Core/Project/FramePhotos.cs index 7eca054d..5a674717 100644 --- a/src/Titano.Core/Project/FramePhotos.cs +++ b/src/Titano.Core/Project/FramePhotos.cs @@ -84,19 +84,20 @@ public sealed class FramePhotos /// public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0; - /// The indices of the frames holding , ascending. + /// + /// The indices of the frames holding , 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. + /// private List Holding(FramePhoto photo) { + if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed; var holding = new List(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; } diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 60978fa2..b005c4f8 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -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()); - 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()); + Assert.Null(frames.IndexOf(hashed, 2)); + Assert.SequenceEqual([1, 3, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(unhashed, n)).OfType()); + 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]