From f2ce806d56d2f94f40cb090957f022e9544e0613 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:13:38 +0000 Subject: [PATCH 1/8] fix(sync): keep the copies of a photo apart when positions cross copies A server folder adopted with the same photo twice ('IMG_0001.JPG' and 'IMG_0001 (1).JPG') holds two frames where the PC keeps one. Translation by photo sent every position of that photo to its first copy, so the server's exclusion of the duplicate excluded the PC's only copy, a push gave the server back one exclusion instead of two, and keyframes of both copies piled on one frame. FramePhotos now lists every frame holding a photo and tells them apart by their order (Occurrence). A position on the n-th copy goes to the n-th copy of the receiving copy, or waits as a pending frame with its occurrence when that copy holds fewer; occurrence 0 is not written, so older projects read the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Core/Project/FramePhotos.cs | 45 ++++++++++--- src/Titano.Core/Project/ProjectSettings.cs | 44 ++++++++----- .../Project/ProjectSettingsTests.cs | 63 +++++++++++++++++++ 3 files changed, 128 insertions(+), 24 deletions(-) diff --git a/src/Titano.Core/Project/FramePhotos.cs b/src/Titano.Core/Project/FramePhotos.cs index 880b2169..7eca054d 100644 --- a/src/Titano.Core/Project/FramePhotos.cs +++ b/src/Titano.Core/Project/FramePhotos.cs @@ -26,12 +26,13 @@ public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt) /// /// 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 (). Immutable once built, so parallel workers may share it. /// public sealed class FramePhotos { private readonly FramePhoto?[] _byIndex; - private readonly Dictionary _bySha = new(StringComparer.Ordinal); + private readonly Dictionary> _bySha = new(StringComparer.Ordinal); private readonly Dictionary<(long Size, long Time), List> _bySizeAndTime = []; /// Each frame's index and photo, in any order. @@ -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 /// Some frame is not hashed yet: matching it needs capture times. public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false }); - /// The index of the first frame holding (); null when none does. - public int? IndexOf(FramePhoto photo) + /// + /// The index of the first frame holding (), or with + /// n of the (n+1)-th in index order; null when fewer frames hold it. + /// + 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; + } + + /// + /// Which of the frames holding the photo at 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. + /// + public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0; + + /// The indices of the frames holding , ascending. + private List Holding(FramePhoto photo) + { + 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) { - 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; } /// Both lists hold the same photos at the same indices: a position names the same photo on both. diff --git a/src/Titano.Core/Project/ProjectSettings.cs b/src/Titano.Core/Project/ProjectSettings.cs index 6e5402e2..1494e670 100644 --- a/src/Titano.Core/Project/ProjectSettings.cs +++ b/src/Titano.Core/Project/ProjectSettings.cs @@ -59,6 +59,13 @@ public sealed record PendingFrame public long Size { get; init; } public DateTime? CapturedAt { get; init; } + /// + /// Which of the frames holding the photo it names, in capture order, when a copy holds the photo more than once + /// (): 0, the first, is not written. + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int Occurrence { get; init; } + /// The photo is excluded from renders. 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 /// (; 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 , by photo: each exclusion, the hero and each camera keyframe - /// moves to the frame of holding its photo. One whose photo lacks becomes + /// moves to the frame of holding its photo, and a position on the n-th frame holding a photo + /// holds more than once to the n-th such frame (), so + /// duplicates stay apart. One whose photo (or that copy of it) lacks becomes /// a , and pending frames whose photo 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(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 } } - /// The pending entry of , added when there is none. - private static Parked Park(List pending, FramePhoto photo) + /// The pending entry of (the copy of it names), added when there is none. + private static Parked Park(List 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); } + /// Which copy of its photo a pending entry names; the first when it says nothing (or nothing readable). + private static int OccurrenceOf(JsonObject entry) => entry["occurrence"] is JsonValue o && o.TryGetValue(out var occurrence) && occurrence > 0 ? occurrence : 0; + private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue(out var set) && set; /// A pending entry still has a position to give its photo. @@ -566,10 +579,11 @@ public sealed record ProjectSettings foreach (var node in copy) array.Add(node); } - /// A pending entry being edited: its photo, its JSON, and whether its photo took its place. - private sealed class Parked(FramePhoto photo, JsonObject entry) + /// A pending entry being edited: its photo (and which copy of it), its JSON, and whether its photo took its place. + 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; } } diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 08943aa5..33e3c525 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -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()); + 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() { From 31dea66270988f03e345b1e0c3cb20eef2577a26 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:13:55 +0000 Subject: [PATCH 2/8] fix(editor): move a local editor's positions with frames inserted under it An editor open on a sequence of this PC while earlier photos arrived (a sync download, an import, a scan) kept its frames and positions in the old order, while the index moved the stored positions with their frames. The next edit of exclusions, the hero or keyframes was merged as the editor's own value and stored in the old order, so other photos were excluded and the Library's hero and the project's named different frames. LocalSequenceSource records the frames version it read the frames at (row and frames read as one state) and refuses a save under the sequence lease when the row's frames version moved (FramesChangedException); the index edit also names that version. EditorSession then reads the sequence again and moves its project, its basis and every undo and redo step the way the index moved the stored copy (ProjectSettings.FollowFrames: by frame id when frames were only inserted, otherwise unchanged; waiting photos placed), and saves in the new order. A read after a job follows the frames the same way, and saves wait for a read under way. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- i18n/en/Editor.resx | 1 + i18n/it/Editor.it.resx | 1 + src/Titano.App/Editor/EditorSession.cs | 103 ++++++++++++++---- src/Titano.App/Editor/LocalSequenceSource.cs | 63 ++++++++--- src/Titano.App/Editor/ProjectHistory.cs | 19 ++++ src/Titano.Core/Project/ProjectSettings.cs | 34 ++++++ .../Editor/EditorSessionTests.cs | 71 ++++++++++++ .../Editor/ProjectStoreTests.cs | 57 ++++++++++ .../Project/ProjectSettingsTests.cs | 41 +++++++ 9 files changed, 357 insertions(+), 33 deletions(-) diff --git a/i18n/en/Editor.resx b/i18n/en/Editor.resx index f6a59fbb..402b1f0c 100644 --- a/i18n/en/Editor.resx +++ b/i18n/en/Editor.resx @@ -52,6 +52,7 @@ Nothing to restore Undo Pick a sequence in the Library + Photos are being added to this sequence; wait a moment and try again.long The frames of this sequence changed on the server; get its proxies again.long This sequence is no longer in the archive.long Jobs cannot run until the archive index is open.long diff --git a/i18n/it/Editor.it.resx b/i18n/it/Editor.it.resx index 1e8570ad..bfc1a2d8 100644 --- a/i18n/it/Editor.it.resx +++ b/i18n/it/Editor.it.resx @@ -51,6 +51,7 @@ Niente da ripristinare Annulla Scegli una sequenza in Libreria + Si stanno aggiungendo foto a questa sequenza; attendi un momento e riprova. I fotogrammi di questa sequenza sono cambiati sul server; scarica di nuovo i proxy. Questa sequenza non è più nell’archivio. I lavori non possono partire finché l’indice dell’archivio non è aperto. diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs index 9b7f63a6..56b9f2f1 100644 --- a/src/Titano.App/Editor/EditorSession.cs +++ b/src/Titano.App/Editor/EditorSession.cs @@ -13,12 +13,17 @@ namespace Titano.App.Editor; /// and redo, and the actions of every section — edit, Optimise (§10.4), Reset, exclusions and hero, analysis and render /// jobs. Every change is saved shortly after it is made (); a change stored meanwhile by /// someone else (an analysis job, another client) is merged in. On a server sequence, settings both sides changed to -/// different values wait for the user's choice (). Call from one thread (the UI thread). +/// different values wait for the user's choice (). Frames inserted under the editor (a sync +/// download, an import of earlier photos, a scan) are read again before the next save, and the project's frame positions, +/// its undo steps included, move with their frames as the stored ones did. Call from one thread (the UI thread). /// public sealed class EditorSession : IAsyncDisposable { public static readonly TimeSpan DefaultSaveDelay = TimeSpan.FromMilliseconds(400); + /// Times a save reads the frames again when frames keep changing under it. + private const int FollowAttempts = 3; + /// Sections "Optimise all" walks through, in panel order. public static IReadOnlyList OptimisedSections { get; } = [AdvisorSection.Frames, AdvisorSection.Timing, AdvisorSection.Exposure, AdvisorSection.Motion, AdvisorSection.Camera, AdvisorSection.Export]; @@ -27,6 +32,9 @@ public sealed class EditorSession : IAsyncDisposable private readonly ProjectSaver _saver; private ProjectSettings _basis; + /// The sequence being read again () until its content is taken: saves wait for it. + private Task _reading = Task.CompletedTask; + public EditorSession(ISequenceSource source, SequenceContent content, IReadOnlyList codecs, TimeSpan? saveDelay = null) { ArgumentNullException.ThrowIfNull(source); @@ -201,19 +209,31 @@ public sealed class EditorSession : IAsyncDisposable public Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken = default) => Source.SubmitAsync(request, cancellationToken); - /// Reads the sequence again (an analysis finished): new reports and advice, the stored project merged into the open one. + /// + /// Reads the sequence again (an analysis finished): new reports and advice, the frames with the project's positions + /// following them (), the stored project merged into the open one. Saves wait until it is taken. + /// public async Task ReloadAsync(CancellationToken cancellationToken = default) { await _saver.FlushAsync(); - var content = await Source.LoadAsync(cancellationToken); - (Content, Context, Advice, Presets) = Analyse(content, _codecs); - // A server's settings that conflict with the editor's waiting change are not merged over it: that change's - // retry meets them and asks (Conflict). - if (Conflict is null && (!IsRemote || ProjectMerge.Conflicts(_basis, Project, content.Project).Count == 0)) + var taken = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _reading = taken.Task; + try { - var merged = ProjectMerge.Merge(_basis, Project, content.Project); - _basis = content.Project; - History.Replace(merged); + var content = await Source.LoadAsync(cancellationToken); + Follow(content); + // A server's settings that conflict with the editor's waiting change are not merged over it: that change's + // retry meets them and asks (Conflict). + if (Conflict is null && (!IsRemote || ProjectMerge.Conflicts(_basis, Project, content.Project).Count == 0)) + { + var merged = ProjectMerge.Merge(_basis, Project, content.Project); + _basis = content.Project; + History.Replace(merged); + } + } + finally + { + taken.SetResult(); } ContentChanged?.Invoke(); } @@ -233,6 +253,31 @@ public sealed class EditorSession : IAsyncDisposable await Source.DisposeAsync(); } + /// + /// Takes as the sequence. Frames inserted or removed since the last read (a sync download, + /// an import of earlier photos, a scan) moved the stored project's frame positions with their frames: the editor's + /// project, its undo and redo steps and its basis move the same way (), so + /// they keep naming the frames the user chose. Returns that move (none when the frames are the same). + /// + private Func Follow(SequenceContent content) + { + var before = Content.Frames.Select(f => f.Id).ToList(); + (Content, Context, Advice, Presets) = Analyse(content, _codecs); + if (before.SequenceEqual(content.Frames.Select(f => f.Id))) return static project => project; + var after = content.Frames; + Log.Info("editor", $"The frames of {content.Sequence.DisplayTitle} changed while it was open; its frame positions follow them"); + _basis = Move(_basis); + History.Remap(Move); + return Move; + + ProjectSettings Move(ProjectSettings project) => + NamesFrames(project) && ProjectSettings.FollowFrames(project.ToJson(), before, after) is { } moved ? ProjectSettings.FromJson(moved) : project; + } + + /// Exclusions, a hero, keyframes or frames waiting for their photos: positions that follow frames. + private static bool NamesFrames(ProjectSettings project) => + project.ExcludedFrames.Count > 0 || project.HeroFrame is not null || project.Camera.Keyframes.Count > 0 || project.PendingFrames is { Count: > 0 }; + private static (SequenceContent, SequenceContext, AdvisorResult, IReadOnlyList) Analyse(SequenceContent content, IReadOnlyList codecs) { @@ -271,21 +316,39 @@ public sealed class EditorSession : IAsyncDisposable private async Task SaveAsync(ProjectSettings project) { - ProjectSettings stored; - try + if (!_reading.IsCompleted) { - stored = await Source.SaveProjectAsync(project, _basis, CancellationToken.None); + // A read under way decides the frames the project's positions name. Once it is taken, the project as it is + // then contains this change (projects are whole snapshots), with its positions on those frames. + while (!_reading.IsCompleted) await _reading; + project = Project; } - catch (ProjectConflictException ex) + ProjectSettings stored; + for (var attempt = 1; ; attempt++) { - var found = Conflict is null; - Conflict = ex; - if (found) + try { - Log.Info("editor", $"Settings of {Content.Sequence.DisplayTitle} changed on the server too: {string.Join(", ", ex.Differences.Select(d => d.Path))}"); - ConflictFound?.Invoke(ex); + stored = await Source.SaveProjectAsync(project, _basis, CancellationToken.None); + break; + } + catch (FramesChangedException) when (attempt < FollowAttempts) + { + // Frames were inserted under the editor and the stored positions moved with them: the frames are read + // again, this edit's positions follow them too, and it is saved in the new order. + project = Follow(await Source.LoadAsync(CancellationToken.None))(project); + ContentChanged?.Invoke(); + } + catch (ProjectConflictException ex) + { + var found = Conflict is null; + Conflict = ex; + if (found) + { + Log.Info("editor", $"Settings of {Content.Sequence.DisplayTitle} changed on the server too: {string.Join(", ", ex.Differences.Select(d => d.Path))}"); + ConflictFound?.Invoke(ex); + } + throw; } - throw; } Conflict = null; _basis = stored; diff --git a/src/Titano.App/Editor/LocalSequenceSource.cs b/src/Titano.App/Editor/LocalSequenceSource.cs index 2105978f..a7d54027 100644 --- a/src/Titano.App/Editor/LocalSequenceSource.cs +++ b/src/Titano.App/Editor/LocalSequenceSource.cs @@ -23,7 +23,10 @@ namespace Titano.App.Editor; /// project in .titano/project.json mirrored in the index's settings column (with its hero frame, which the /// Library shows), jobs on this PC's engine. Project writes follow the archive engine's protocol: under the sequence's /// lease file (so the server and the job engine wait for it), file first (atomically), then the index with its settings -/// version as an optimistic check. +/// version as an optimistic check. Project settings name frames by position, and frames inserted while the sequence is +/// open (a sync download, an import of earlier photos, a scan) move the stored positions with their frames: a save made +/// against the frames read earlier is refused () until the editor has read them again +/// and moved its own positions the same way (). /// public sealed class LocalSequenceSource : ISequenceSource { @@ -31,6 +34,9 @@ public sealed class LocalSequenceSource : ISequenceSource private const int SaveAttempts = 3; private const int JobsListed = 200; + /// Times the frames are listed again when an insertion lands while they are read. + private const int ReadAttempts = 3; + private readonly TitanoDatabase _db; private readonly Guid _sequenceId; private readonly ImageDecoder _decoder; @@ -41,6 +47,10 @@ public sealed class LocalSequenceSource : ISequenceSource /// The JPEG of each GPR frame that an import keeps in another sequence (), by position. private string?[] _companions = []; private Guid[] _frameIds = []; + + /// The frames version of the frames last read (), whose order the project's positions follow. + private int _framesVersion = -1; + private ArchivePaths? _paths; private SequencePaths? _folder; @@ -74,15 +84,8 @@ public sealed class LocalSequenceSource : ISequenceSource public async Task LoadAsync(CancellationToken cancellationToken) { - var sequence = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); + var (sequence, frames) = await ReadFramesAsync(cancellationToken); var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken) ?? throw Missing(); - var frames = new List(); - while (true) - { - var page = await _db.Frames.ListBySequenceAsync(_sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken); - frames.AddRange(page); - if (page.Count < PageSize) break; - } var renders = await _db.Renders.ListBySequenceAsync(_sequenceId, cancellationToken); var companions = await GprCompanions.FindAsync(_db, archive, frames, cancellationToken); return await Task.Run(() => @@ -95,7 +98,7 @@ public sealed class LocalSequenceSource : ISequenceSource if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath); } _companions = [.. frames.Select(f => companions.GetValueOrDefault(f.Id))]; - (_paths, _folder, _files, _frameIds) = (paths, folder, files, [.. frames.Select(f => f.Id)]); + (_paths, _folder, _files, _frameIds, _framesVersion) = (paths, folder, files, [.. frames.Select(f => f.Id)], sequence.FramesVersion); var context = SequenceContext.FromFrames(frames); return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count), context.SourceWidth, context.SourceHeight, folder.Exports, [.. renders.OrderByDescending(r => r.Created)], SavedCopies.List(folder.MetaDirectory)); @@ -127,7 +130,7 @@ public sealed class LocalSequenceSource : ISequenceSource { ArgumentNullException.ThrowIfNull(project); ArgumentNullException.ThrowIfNull(basis); - var (paths, folder, frameIds) = (_paths, _folder, _frameIds); + var (paths, folder, frameIds, framesVersion) = (_paths, _folder, _frameIds, _framesVersion); if (paths is null || folder is null) throw new InvalidOperationException("LoadAsync first."); return Task.Run(async () => { @@ -137,13 +140,16 @@ public sealed class LocalSequenceSource : ISequenceSource for (var attempt = 1; ; attempt++) { var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); + // Frames were inserted or removed since the editor read them, so its positions name the old order. Insertions + // hold this lease, so the frames do not change again before this save ends. + if (row.FramesVersion != framesVersion) throw new FramesChangedException(Loc.L("Editor.Error.FramesArriving")); var stored = ReadProject(row, folder); var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored); var json = merged.ToJson(); if (json == stored.ToJson()) return merged; AtomicFile.WriteAllText(folder.ProjectPath, json); var edit = new SequenceEdit { SettingsJson = json, HeroFrameId = merged.HeroFrame == stored.HeroFrame ? null : HeroFrameId(merged, frameIds) }; - var result = await _db.Sequences.EditAsync(_sequenceId, edit, row.SettingsVersion, cancellationToken); + var result = await _db.Sequences.EditAsync(_sequenceId, edit, row.SettingsVersion, framesVersion, cancellationToken); switch (result.Outcome) { case SequenceEditOutcome.Updated: @@ -151,7 +157,8 @@ public sealed class LocalSequenceSource : ISequenceSource return merged; case SequenceEditOutcome.NotFound: throw Missing(); - // The settings moved in the index meanwhile (server API): merge again on top of them; the file follows the next write. + // The settings moved in the index meanwhile (server API): merge again on top of them, or find the frames that + // moved; the file follows the next write. case SequenceEditOutcome.Conflict when attempt == SaveAttempts: throw new EditorException(Loc.L("Editor.Error.SaveConflict")); } @@ -181,6 +188,30 @@ public sealed class LocalSequenceSource : ISequenceSource return ValueTask.CompletedTask; } + /// + /// The sequence row with its frames as one state: the frames are listed between two reads of the row that find the + /// same frames version (an insertion renumbers the frames and moves the row's positions in one transaction, and + /// bumps the version), so the project's positions name these frames. + /// + /// The sequence is gone, or an insertion landed during every attempt. + private async Task<(Sequence Row, List Frames)> ReadFramesAsync(CancellationToken cancellationToken) + { + for (var attempt = 0; attempt < ReadAttempts; attempt++) + { + var before = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); + var frames = new List(); + while (true) + { + var page = await _db.Frames.ListBySequenceAsync(_sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken); + frames.AddRange(page); + if (page.Count < PageSize) break; + } + var after = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); + if (after.FramesVersion == before.FramesVersion) return (after, frames); + } + throw new EditorException(Loc.L("Editor.Error.FramesArriving")); + } + /// The settings in force: the index copy, else project.json, else defaults. /// The settings are not a project or come from a newer Titano. internal static ProjectSettings ReadProject(Sequence sequence, SequencePaths folder) @@ -221,3 +252,9 @@ public sealed class LocalSequenceSource : ISequenceSource private static EditorException Missing() => new(Loc.L("Editor.Error.Missing")); } + +/// +/// The frames of a sequence of this PC changed since the editor read them (frames inserted or removed): nothing was +/// saved. The editor reads them again, moves its positions as the index moved the stored ones, and saves again. +/// +public sealed class FramesChangedException(string message) : EditorException(message); diff --git a/src/Titano.App/Editor/ProjectHistory.cs b/src/Titano.App/Editor/ProjectHistory.cs index 3622b17c..c90b1a95 100644 --- a/src/Titano.App/Editor/ProjectHistory.cs +++ b/src/Titano.App/Editor/ProjectHistory.cs @@ -78,6 +78,25 @@ public sealed class ProjectHistory(ProjectSettings initial, int capacity = 200) Changed?.Invoke(); } + /// + /// Rewrites the project and every undo and redo snapshot with , without an undo step: a change + /// of what the values mean rather than of the values (frame positions following frames inserted under the editor), so + /// undo and redo keep naming the same frames. + /// + public void Remap(Func remap) + { + ArgumentNullException.ThrowIfNull(remap); + foreach (var steps in new[] { _undo, _redo }) + { + for (var node = steps.First; node is not null; node = node.Next) node.Value = remap(node.Value); + } + var current = remap(Current); + if (ProjectMerge.SameValues(current, Current)) return; + _runKey = null; + Current = current; + Changed?.Invoke(); + } + private static void Rebase(LinkedList steps, ProjectMerge.Rebase rebase) { for (var node = steps.First; node is not null; node = node.Next) node.Value = rebase.Apply(node.Value); diff --git a/src/Titano.Core/Project/ProjectSettings.cs b/src/Titano.Core/Project/ProjectSettings.cs index 1494e670..60fc2eae 100644 --- a/src/Titano.Core/Project/ProjectSettings.cs +++ b/src/Titano.Core/Project/ProjectSettings.cs @@ -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; @@ -324,6 +325,39 @@ public sealed record ProjectSettings } } + /// + /// A project written against the frames (frame ids, by index) moved to the frames + /// (in index order) the way the archive index moves its own copy when it takes them + /// (ProjectPositions in Titano.Data): while every frame of is still there, its positions + /// move with their frames (), otherwise they stay; either way pending frames whose + /// photos are now there take their place (). An editor that read the frames + /// before frames were inserted saves its edit in the new order this way. Null when nothing changes. + /// + public static string? FollowFrames(string json, IReadOnlyList before, IReadOnlyList after) + { + ArgumentNullException.ThrowIfNull(before); + ArgumentNullException.ThrowIfNull(after); + var now = new Dictionary(after.Count); + for (var i = 0; i < after.Count; i++) now[after[i].Id] = i; + Dictionary? 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 replace, IReadOnlyDictionary moves) { if (position is not JsonValue value || !value.TryGetValue(out var old) || !moves.TryGetValue(old, out var now) || now == old) return false; diff --git a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs index 7015300d..9b9a5b44 100644 --- a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs @@ -98,6 +98,77 @@ public sealed class EditorSessionTests } } + [Test] + public async Task AnEditMadeBeforeInsertedFramesWereReadIsSavedOnTheFramesItNamed() + { + var (session, source) = OpenShort(); + await using (session) + { + Assert.True(session.SetExcluded([3], true)); + Assert.True(await session.FlushAsync()); + // Two earlier photos are inserted (a download, an import, a scan): the stored exclusion moved with its frame. + var frames = session.Content.Frames; + InsertEarlier(source, 2); + source.Stored = source.Stored with { ExcludedFrames = [5] }; + source.FailNextSave = new FramesChangedException("frames changed"); + + // The editor still shows the frames it read when the user excludes another one. + Assert.True(session.SetExcluded([6], true)); + Assert.True(await session.FlushAsync()); + + Assert.Equal(frames.Count + 2, session.Content.Count, "the frames were read again"); + Assert.SequenceEqual([5, 8], source.Stored.ExcludedFrames); + Assert.SequenceEqual([frames[3].Id, frames[6].Id], session.Project.ExcludedFrames.Select(i => session.Content.Frames[i].Id)); + Assert.True(session.Undo()); + Assert.SequenceEqual([5], session.Project.ExcludedFrames, "undo steps name the same frames"); + Assert.True(session.Undo()); + Assert.Empty(session.Project.ExcludedFrames); + } + } + + [Test] + public async Task AReadAfterFramesWereInsertedMovesTheOpenProjectWithThem() + { + var (session, source) = OpenShort(); + await using (session) + { + Assert.True(session.SetExcluded([3], true)); + Assert.True(session.SetHero(4)); + Assert.True(await session.FlushAsync()); + InsertEarlier(source, 2); + source.Stored = source.Stored with { ExcludedFrames = [5], HeroFrame = 6 }; + + await session.ReloadAsync(); + + Assert.SequenceEqual([5], session.Project.ExcludedFrames); + Assert.Equal(6, session.Project.HeroFrame); + Assert.True(session.Undo()); + Assert.Null(session.Project.HeroFrame); + Assert.SequenceEqual([5], session.Project.ExcludedFrames, "the step before Set hero names the same frame"); + Assert.True(await session.FlushAsync()); + Assert.SequenceEqual([5], source.Stored.ExcludedFrames); + Assert.Null(source.Stored.HeroFrame); + } + } + + /// A short sequence without its analysis, whose frame ids are all different (the synthetic ids repeat every 64 frames). + private static (EditorSession Session, FakeSequenceSource Source) OpenShort() + { + var sequence = EditorFixture.Sequence(12); + var content = EditorFixture.Content(sequence, deep: false); + var source = new FakeSequenceSource(sequence, content); + return (new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)), source); + } + + /// Photos captured before the first frame join the source's sequence, as an insertion renumbers it. + private static void InsertEarlier(FakeSequenceSource source, int count) + { + var first = source.Content.Frames[0]; + var early = Enumerable.Range(0, count).Select(i => first with { Id = Guid.CreateVersion7(), CapturedAt = first.CapturedAt!.Value.AddMinutes(i - count) }); + IReadOnlyList frames = [.. early.Concat(source.Content.Frames).Select((f, i) => f with { Index = i })]; + source.Content = source.Content with { Frames = frames, Light = LightAnalyzer.Analyze(frames) }; + } + [Test] public async Task AFailedSaveStaysPendingUntilItIsStored() { diff --git a/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs index ab55c4a9..4a8d4de5 100644 --- a/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs +++ b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs @@ -138,6 +138,63 @@ public sealed class ProjectStoreTests(TestContext context) } } + [Test] + public async Task AnEditorOpenWhileEarlierPhotosArriveSavesOnTheFramesTheUserChose() + { + var (db, sequenceId, root) = await ArchiveAsync(10); + await using (db) + { + var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }; + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null, options); + var content = await source.LoadAsync(context.Cancellation); + var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)); + await using (session) + { + Assert.True(session.SetExcluded([5], true)); + Assert.True(session.SetHero(6)); + Assert.True(await session.FlushAsync()); + + // Three photos taken before the others are found in the folder (a sync download, an import or a scan + // inserts them the same way): the index moves the stored positions with their frames. + var sequence = EditorFixture.Sequence(3); + var first = content.Frames[0].CapturedAt!.Value; + for (var i = 0; i < 3; i++) + { + using var image = sequence.Render(i); + var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i] with { CapturedAt = first.AddMinutes(i - 3) }); + File.WriteAllBytes(Path.Combine(root, "Synthetic", $"EARLY_{i + 1}.JPG"), + JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] })); + } + var archive = (await db.Archives.GetAsync(content.Sequence.ArchiveId, context.Cancellation))!; + await new ArchiveService(db, options).Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation); + var moved = ProjectSettings.FromJson((await db.Sequences.GetAsync(sequenceId))!.SettingsJson!); + Assert.SequenceEqual([8], moved.ExcludedFrames); + Assert.Equal(9, moved.HeroFrame); + + // The editor still shows the ten frames it read; the user excludes the eighth of them. + Assert.Equal(10, session.Content.Count); + Assert.True(session.SetExcluded([7], true)); + Assert.True(await session.FlushAsync()); + + Assert.Equal(13, session.Content.Count, "the frames were read again before the save"); + Assert.SequenceEqual([8, 10], session.Project.ExcludedFrames); + Assert.Equal(9, session.Project.HeroFrame); + var row = (await db.Sequences.GetAsync(sequenceId))!; + var stored = ProjectSettings.FromJson(row.SettingsJson!); + var frames = session.Content.Frames; + Assert.SequenceEqual([content.Frames[5].Id, content.Frames[7].Id], stored.ExcludedFrames.Select(i => frames[i].Id), "the photos the user chose"); + Assert.Equal(content.Frames[6].Id, frames[stored.HeroFrame!.Value].Id); + Assert.Equal(content.Frames[6].Id, row.HeroFrameId); + Assert.Equal(row.SettingsJson, File.ReadAllText(Path.Combine(root, "Synthetic", ".titano", "project.json"))); + + // Undo takes back the user's last step on the new frames too. + Assert.True(session.Undo()); + Assert.True(await session.FlushAsync()); + Assert.SequenceEqual([8], ProjectSettings.FromJson((await db.Sequences.GetAsync(sequenceId))!.SettingsJson!).ExcludedFrames); + } + } + } + [Test] public async Task ASaveThatDoesNotReachTheIndexThrows() { diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 33e3c525..60978fa2 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -167,6 +167,47 @@ public sealed class ProjectSettingsTests Assert.Null(ProjectSettings.MoveFramePositions("""{ "heroFrame": 1, "HeroFrame": 1 }""", new Dictionary { [1] = 2 })); } + [Test] + public void AnEditorsPositionsFollowFramesInsertedUnderItAsTheIndexMovedTheStoredOnes() + { + // An editor read four frames; a download then inserted two earlier photos, one of which an exclusion waited for. + var photos = Enumerable.Range(0, 6).Select(Photo).ToList(); + var ids = Enumerable.Range(0, 6).Select(_ => Guid.CreateVersion7()).ToList(); + IReadOnlyList before = ids[2..]; + var after = ids.Select((id, i) => new Titano.Core.Model.Frame + { + Id = id, + SequenceId = Guid.Empty, + Index = i, + RelativePath = $"s/IMG_{i}.JPG", + FileName = $"IMG_{i}.JPG", + Sha256 = photos[i].Sha256, + Size = photos[i].Size, + CapturedAt = photos[i].CapturedAt, + }).ToList(); + var project = new ProjectSettings + { + ExcludedFrames = [1, 3], + HeroFrame = 0, + Camera = new CameraSettings { Keyframes = [new CameraKey(2, 0.5, 0.5, 1, 0)] }, + Exposure = new ExposureSettings { DeflickerWindow = new Auto().WithManual(41) }, + PendingFrames = [new PendingFrame { Sha256 = photos[1].Sha256, Size = photos[1].Size, Excluded = true }], + }; + + var followed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.FollowFrames(project.ToJson(), before, after))); + + Assert.SequenceEqual([1, 3, 5], followed.ExcludedFrames); + Assert.Equal(2, followed.HeroFrame); + Assert.SequenceEqual([4], followed.Camera.Keyframes.Select(k => k.Frame)); + Assert.Equal(41, followed.Exposure.DeflickerWindow.Manual); + Assert.Null(followed.PendingFrames); + + // The same frames: nothing moves. A frame the editor read that is gone: positions stay, as the index leaves them. + Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), ids, after)); + var removed = after.Where(f => f.Id != ids[3]).Select((f, i) => f with { Index = i }).ToList(); + Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1, 3] }.ToJson(), before, removed)); + } + [Test] public void PositionsCrossToAnotherCopyByPhotoAndWaitForPhotosItLacks() { From 678a827b423b5d207e3bfaf57dfbfc4ec31deedc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:14:04 +0000 Subject: [PATCH 3/8] fix(editor): name the frames version in the remote editor's saves The desktop editor of a server sequence relied on the server's sequenceChanged event to go read-only when frames changed. Scans publish none, and an event missed while the socket reconnected was never noticed, so a save made meanwhile passed the settings-version check and wrote positions in the old order over the ones the server had moved. The PATCH now carries expectedFramesVersion, the frames version of the proxies the editor holds. On 409 the editor reads the manifest: other frames make it outdated (read-only, Get proxies; the edit stays pending), the same frames at a new version retry at that version. After a lost event socket the frames are compared again. Servers that predate the field ignore it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Editor/RemoteSequenceSource.cs | 20 ++++++--- .../Editor/FakeRemoteSequenceApi.cs | 18 ++++++-- .../Editor/RemoteSequenceSourceTests.cs | 45 +++++++++++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/Titano.App/Editor/RemoteSequenceSource.cs b/src/Titano.App/Editor/RemoteSequenceSource.cs index c581489b..97993e95 100644 --- a/src/Titano.App/Editor/RemoteSequenceSource.cs +++ b/src/Titano.App/Editor/RemoteSequenceSource.cs @@ -31,7 +31,8 @@ namespace Titano.App.Editor; /// local archive. Analyses and renders are server jobs; their state and progress come from the server's events. /// Project settings name frames by position, so the cached frames must be the server's: a load that finds other frames /// throws , and frames that change on the server while the sequence is open make -/// it (read-only, nothing saved or rendered). +/// it (read-only, nothing saved or rendered). Saves name the frames version the editor's frames +/// are at, so the server refuses one made against frames it no longer has even before its event arrives. /// public sealed class RemoteSequenceSource : ISequenceSource { @@ -150,19 +151,24 @@ public sealed class RemoteSequenceSource : ISequenceSource if (changedThere && ProjectMerge.Conflicts(basis, project, stored) is { Count: > 0 } conflicts) throw new ProjectConflictException(Loc.L("Editor.Error.SaveConflict"), stored, conflicts); var hero = merged.HeroFrame == stored.HeroFrame ? (Guid?)null : HeroFrameId(merged); + // The positions name the frames this editor holds: the server refuses them (409 frames_conflict) once its + // frames changed, also before its event says so. A server that predates the check ignores it. + var request = new UpdateSequenceRequest(null, null, null, null, merged.ToJson(), version, hero, Volatile.Read(ref _framesVersion)); try { - var updated = await Call(() => _api.UpdateSequenceAsync(Id, new UpdateSequenceRequest(null, null, null, null, merged.ToJson(), version, hero), - cancellationToken)); + var updated = await Call(() => _api.UpdateSequenceAsync(Id, request, cancellationToken)); Volatile.Write(ref _stored, new Stored(merged, updated.SettingsVersion)); return merged; } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict) { - // Changed on the server since this editor read it: merge again on top of the server's settings. - if (attempt == SaveAttempts) throw new EditorException(Loc.L("Editor.Error.SaveConflict"), ex); + // Changed on the server since this editor read it, its settings or its frames: other frames make the + // editor outdated (its proxies are not the server's), otherwise the edit merges again on top of the + // server's settings, at the frames version they are at. var manifest = await Call(() => _api.GetManifestAsync(Id, cancellationToken)); if (!SameFrames(manifest)) throw Outdated(); + if (attempt == SaveAttempts) throw new EditorException(Loc.L("Editor.Error.SaveConflict"), ex); + Volatile.Write(ref _framesVersion, manifest.FramesVersion); Volatile.Write(ref _stored, new Stored(Parse(manifest.SettingsJson), manifest.SettingsVersion)); } } @@ -219,7 +225,7 @@ public sealed class RemoteSequenceSource : ISequenceSource /// /// Follows the server's events until the editor closes, reconnecting with a growing delay (job states lost meanwhile - /// are read back from the jobs). Nothing that fails on the way ends it. + /// are read back from the jobs, and the frames are compared again). Nothing that fails on the way ends it. /// private async Task ListenAsync(CancellationToken cancellationToken) { @@ -244,6 +250,8 @@ public sealed class RemoteSequenceSource : ISequenceSource var delay = Math.Min(MaxBackoff.Ticks, MinBackoff.Ticks << Math.Min(failures++, 10)); await Task.Delay(TimeSpan.FromTicks(delay), cancellationToken).ConfigureAwait(false); await RefreshTrackedAsync(cancellationToken).ConfigureAwait(false); + // A frames change announced while the socket was down is found now rather than at the next save. + if (Volatile.Read(ref _framesVersion) >= 0) await CheckFramesAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) when (ex is not OutOfMemoryException) { diff --git a/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs b/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs index 495f7cfe..05a9c11c 100644 --- a/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs +++ b/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.IO; using System.Net; using System.Runtime.CompilerServices; using System.Threading.Channels; @@ -17,7 +18,7 @@ namespace Titano.App.Tests.Editor; internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyList frames) : IRemoteSequenceApi { private readonly Lock _gate = new(); - private readonly Channel _events = Channel.CreateUnbounded(); + private Channel _events = Channel.CreateUnbounded(); /// The server's frames in order (another list when frames are added or removed on the server). public IReadOnlyList Frames { get; set; } = frames; @@ -59,7 +60,10 @@ internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyList _events.Writer.TryWrite(e); + public void Publish(ServerEvent e) => Volatile.Read(ref _events).Writer.TryWrite(e); + + /// The event socket drops: the stream open now fails, and the next one starts empty. + public void DropEvents() => Interlocked.Exchange(ref _events, Channel.CreateUnbounded()).Writer.TryComplete(new IOException("connection lost")); public Task GetSequenceAsync(Guid id, CancellationToken cancellationToken) { @@ -98,6 +102,11 @@ internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyList EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken) { - while (await _events.Reader.WaitToReadAsync(cancellationToken)) + var events = Volatile.Read(ref _events).Reader; + while (await events.WaitToReadAsync(cancellationToken)) { - while (_events.Reader.TryRead(out var e)) yield return e; + while (events.TryRead(out var e)) yield return e; } } diff --git a/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs b/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs index 1ef3ec8a..9a6c9043 100644 --- a/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs +++ b/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs @@ -433,6 +433,51 @@ public sealed class RemoteSequenceSourceTests(TestContext context) } } + [Test] + public async Task SavesNameTheirFramesAndTheServerRefusesThemOnceItsFramesChanged() + { + var (source, api, remote) = Open(); + await using (source) + { + var content = await source.LoadAsync(context.Cancellation); + var stored = await source.SaveProjectAsync(content.Project with { ExcludedFrames = [1] }, content.Project, context.Cancellation); + + // A new frames version over the same frames: the save goes through at that version. + api.FramesVersion = 2; + stored = await source.SaveProjectAsync(stored with { HeroFrame = 2 }, stored, context.Cancellation); + Assert.SequenceEqual([1, 1, 2], api.Edits.Select(e => e.Request.ExpectedFramesVersion ?? -1)); + Assert.SequenceEqual([true, false, true], api.Edits.Select(e => e.Accepted)); + + // An earlier photo reaches the server (a scan, an upload) and its positions move with their frames, before + // any event reaches this editor: the server refuses the edit made on the old frames, which stays unsaved. + api.Frames = [Guid.CreateVersion7(), .. remote.Frames.Select(f => f.Id)]; + api.FramesVersion = 3; + api.SettingsJson = (ProjectSettings.FromJson(api.SettingsJson!) with { ExcludedFrames = [2], HeroFrame = 3 }).ToJson(); + var refused = await Assert.ThrowsAsync(() => + source.SaveProjectAsync(stored with { ExcludedFrames = [1, 4] }, stored, context.Cancellation)); + Assert.Equal(Loc.L("Editor.Error.FramesChanged"), refused.Message); + Assert.True(source.IsOutdated); + Assert.False(api.Edits[^1].Accepted); + Assert.SequenceEqual([2], ProjectSettings.FromJson(api.SettingsJson!).ExcludedFrames, "the server keeps the positions it moved"); + } + } + + [Test] + public async Task FramesChangedWhileTheEventsWereLostAreFoundOnReconnecting() + { + var (source, api, remote) = Open(); + await using (source) + { + await source.LoadAsync(context.Cancellation); + api.DropEvents(); + api.Frames = [.. remote.Frames.Skip(1).Select(f => f.Id)]; + api.FramesVersion = 2; + // The listener reconnects after its first delay (2 s) and compares the frames again. + await WaitAsync(() => source.IsOutdated); + Assert.True(source.IsReadOnly); + } + } + [Test] public async Task AConflictFoundOnOtherFramesIsNotMergedOntoThem() { From 99a0f37973b81924986785f5e780b359fd8ba1cc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:14:13 +0000 Subject: [PATCH 4/8] fix(sync): replace cached thumbnails once the server offers their proxies With Keep proxies offline, the agent refreshed a proxy cache only when the server's sequence had ProxiesBuilt. That flag waits for every frame, and a missing or damaged frame never gets a proxy, so a HEIF sequence with one such frame kept the 320-px thumbnails cached in place of proxies forever, although the server offered the other frames' proxies sent by clients. For such a cache the agent now reads the manifest once per change of the server's update time and queues the background download when a frame is offered as a proxy that the cache holds only a stand-in for (or nothing). The test server's fake serves proxies sent to it, as the real one does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Sync/SyncAgent.cs | 40 ++++++++++++++++++- src/Titano.Data/Sync/SyncService.cs | 2 +- .../Titano.Data.Tests/Sync/FakeSyncRemote.cs | 2 + .../Titano.Data.Tests/Sync/SyncAgentTests.cs | 33 +++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/Titano.Data/Sync/SyncAgent.cs b/src/Titano.Data/Sync/SyncAgent.cs index 1d47748d..9b3594ec 100644 --- a/src/Titano.Data/Sync/SyncAgent.cs +++ b/src/Titano.Data/Sync/SyncAgent.cs @@ -1,9 +1,11 @@ using System.Collections.Concurrent; +using System.Text.Json; using System.Threading.Channels; using Titano.Core.Diagnostics; using Titano.Core.Model; using Titano.Data.Jobs; using Titano.Protocol; +using Titano.Protocol.Client; namespace Titano.Data.Sync; @@ -48,6 +50,10 @@ public sealed class SyncAgent : IAsyncDisposable private readonly Dictionary _proxyJobs = []; private readonly Dictionary _proxyFailures = []; + + /// The server's update time at which a cache holding thumbnails in place of proxies was last compared with its manifest. + private readonly Dictionary _standInsCompared = []; + private readonly SemaphoreSlim _refreshing = new(1, 1); private Task _running = Task.CompletedTask; private SyncSnapshot? _snapshot; @@ -220,7 +226,9 @@ public sealed class SyncAgent : IAsyncDisposable /// /// Queues (background priority) the proxies of server sequences this PC keeps proxies of, without local originals, - /// whose cache is stale; a sequence whose last download failed waits for its backoff. + /// whose cache is stale: frames changed on a server that has the sequence's proxies, or the server now offers the + /// proxy of a frame the cache holds a thumbnail for (). A sequence whose last + /// download failed waits for its backoff. /// private async Task QueueProxiesAsync(SyncSnapshot snapshot, CancellationToken cancellationToken) { @@ -241,14 +249,42 @@ public sealed class SyncAgent : IAsyncDisposable } foreach (var entry in snapshot.Entries) { - if (entry is not { Local: null, Transfer: null, Link: { ProxiesCached: > 0, ServerHasProxies: true, ProxiesStale: true } link }) continue; + if (entry is not { Local: null, Transfer: null, Link: { ProxiesCached: > 0, ProxiesStale: true } link }) continue; if (_proxyFailures.TryGetValue(link.RemoteId, out var failed) && DateTime.UtcNow < failed.NotBefore) continue; + if (!link.ServerHasProxies && !await OffersProxiesAsync(link, cancellationToken).ConfigureAwait(false)) continue; var job = await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Proxies, null, JobPriority.Background, cancellationToken).ConfigureAwait(false); _proxyJobs[link.RemoteId] = job.Id; } } + /// + /// Whether the server now offers the proxy of a frame whose cached file only stands in for one (a thumbnail, a file + /// of a cache that did not record its kind) or that has no cached file. The sequence's proxy flag cannot say: it waits + /// for every frame, a missing or damaged one never gets a proxy, and clients and render nodes send the other frames' + /// proxies one by one. The manifest is read once per change of the server's sequence, since a proxy that arrives + /// leaves a new update time (the proxy pass it queues records the sequence's state). + /// + private async Task OffersProxiesAsync(SyncLink link, CancellationToken cancellationToken) + { + if (_standInsCompared.TryGetValue(link.RemoteId, out var compared) && SyncService.SameMoment(compared, link.RemoteUpdatedAt)) return false; + SequenceManifest manifest; + try + { + manifest = await _remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (ex is TitanoApiException or HttpRequestException or JsonException + || (ex is OperationCanceledException && !cancellationToken.IsCancellationRequested)) + { + // Compared again at the next refresh; the other sequences go on. + Log.Info(LogCategory, $"The proxies of {link.Title} on {Server} could not be compared: {ex.Message}"); + return false; + } + _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; + var cached = _sync.Proxies.Kinds(Server, link.RemoteId); + return manifest.Frames.Any(f => f.HasProxy && cached.GetValueOrDefault(f.Id) != ProxyCache.Proxy); + } + /// Waits for a wake-up or the delay; false when stopping. private static async Task WaitAsync(ChannelReader wake, TimeSpan delay, CancellationToken cancellationToken) { diff --git a/src/Titano.Data/Sync/SyncService.cs b/src/Titano.Data/Sync/SyncService.cs index 647d8085..35d4ff1c 100644 --- a/src/Titano.Data/Sync/SyncService.cs +++ b/src/Titano.Data/Sync/SyncService.cs @@ -710,7 +710,7 @@ public sealed class SyncService private static DateTime Second(DateTime time) => new(time.Ticks - time.Ticks % TimeSpan.TicksPerSecond, DateTimeKind.Unspecified); /// The same instant as databases keep it (to the microsecond). - private static bool SameMoment(DateTimeOffset a, DateTimeOffset b) => a.UtcTicks / 10 == b.UtcTicks / 10; + internal static bool SameMoment(DateTimeOffset a, DateTimeOffset b) => a.UtcTicks / 10 == b.UtcTicks / 10; /// /// Writes the server's payload into the local index, project and sidecar in one step (settings version + 1) under the diff --git a/tests/Titano.Data.Tests/Sync/FakeSyncRemote.cs b/tests/Titano.Data.Tests/Sync/FakeSyncRemote.cs index c795358e..dbcc567b 100644 --- a/tests/Titano.Data.Tests/Sync/FakeSyncRemote.cs +++ b/tests/Titano.Data.Tests/Sync/FakeSyncRemote.cs @@ -174,6 +174,8 @@ internal sealed class FakeSyncRemote : ISyncRemote "original" when CorruptOnce == frameId => [.. frame.Bytes.Select(b => (byte)~b)], "original" => frame.Bytes, "proxy" or "thumb" when FailAssets => throw new TitanoApiException(HttpStatusCode.InternalServerError, new ApiError("internal", "failed")), + // A proxy a client or render node sent is served as soon as it is filed, like the manifest says. + "proxy" when SentProxies.TryGetValue(frameId, out var sent) => sent, "proxy" when (sequence.Status & SequenceStatus.ProxiesBuilt) != 0 => frame.Proxy, "thumb" when (sequence.Status & SequenceStatus.ThumbsBuilt) != 0 => frame.Proxy[..Math.Min(frame.Proxy.Length, 64)], _ => throw NotFound(), diff --git a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs index cfdc577a..6188f1ae 100644 --- a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs +++ b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs @@ -159,6 +159,39 @@ public sealed class SyncAgentTests(TestContext context) Assert.Equal(2, (await DownloadsAsync(f)).Count); } + [Test] + public async Task ThumbnailsStandingInAreReplacedOnceTheServerOffersTheirProxies() + { + await using var f = await SyncFixture.CreateAsync(context); + // The server cannot decode these originals and waits for their proxies; one frame (missing or damaged) never gets + // one, so the sequence's proxy flag never comes. + var remote = f.Remote.Add("Fog", f.NewPhotos(3), status: SequenceStatus.ThumbsBuilt); + remote.Undecodable = true; + var engine = await f.StartEngineAsync(); + await f.RefreshAsync(); + var first = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Fog", SyncDownloadMode.Proxies, null); + await f.Archive.WaitForAsync(first.Id, JobState.Done); + Assert.True(Assert.NotNull(f.Sync.OpenRemote(f.Server, remote.Id)).Files.All(file => new FileInfo(file!).Length == 64), "thumbnails stand in"); + await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(KeepProxiesOffline: true), engine, EventsOnly); + agent.Start(); + await WaitAsync(() => agent.Snapshot is not null, "first comparison"); + await agent.RefreshAsync(f.Ct); + Assert.Count(1, await DownloadsAsync(f)); + + // Another PC sends the proxies of two frames; the server's proxy pass then records the sequence's state. + foreach (var frame in remote.Frames.Take(2)) f.Remote.SentProxies[frame.Id] = frame.Proxy; + remote.UpdatedAt = DateTimeOffset.UtcNow.AddSeconds(1); + await agent.RefreshAsync(f.Ct); + var refresh = Assert.Contains(await DownloadsAsync(f), j => j.Id != first.Id); + await f.Archive.WaitForAsync(refresh.Id, JobState.Done); + + var opened = Assert.NotNull(f.Sync.OpenRemote(f.Server, remote.Id)); + Assert.SequenceEqual([remote.Frames[0].Proxy.Length, remote.Frames[1].Proxy.Length, 64], opened.Files.Select(file => (int)new FileInfo(file!).Length)); + // The frame still waiting keeps its thumbnail; nothing is queued again until the server's sequence changes. + await agent.RefreshAsync(f.Ct); + Assert.Count(2, await DownloadsAsync(f)); + } + private static async Task> DownloadsAsync(SyncFixture f) => (await f.Db.Jobs.ListAsync(null, JobKind.Download, 100, null, f.Ct)).Items; From 827b224ee322e04a5bb4bda7c5e90bf76aabb8b0 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:59:45 +0000 Subject: [PATCH 5/8] fix(editor): read inserted frames before a render embeds the project A render queued before the editor had seen frames inserted under it (a sync download, an import of earlier photos, a scan without an event) embedded the project's positions in the old frame order. Render now checks first that the frames read are still the sequence's (ISequenceSource.FramesCurrentAsync: the row's frames version on this PC, the manifest on a server). On this PC the sequence is read again and the positions follow the frames; a server sequence whose frames changed turns outdated and the render is not queued. Reads of the sequence no longer overlap: a read waits for the one under way, saves that meet new frames read them through the same slot, and they save the project as it is once its positions followed. The frame on screen stays on its photo when frames are inserted or removed before it. Tests: the event-loss test waits for the listener's subscription before dropping the stream, and the conflict tests flush once more after the choice when a retry started before it was still failing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Editor/EditorSession.cs | 59 ++++++++++--- src/Titano.App/Editor/EditorViewModel.cs | 28 ++++++- src/Titano.App/Editor/ISequenceSource.cs | 8 ++ src/Titano.App/Editor/LocalSequenceSource.cs | 8 ++ src/Titano.App/Editor/RemoteSequenceSource.cs | 26 +++++- .../Titano.App.Tests/Editor/EditorFixture.cs | 36 +++++++- .../Editor/EditorSessionTests.cs | 83 +++++++++++++++++++ .../Editor/EditorSmokeTests.cs | 31 +++++++ .../Editor/FakeRemoteSequenceApi.cs | 5 ++ .../Editor/ProjectStoreTests.cs | 61 +++++++++++--- .../Editor/RemoteSequenceSourceTests.cs | 37 ++++++++- 11 files changed, 351 insertions(+), 31 deletions(-) diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs index 56b9f2f1..6ee1beea 100644 --- a/src/Titano.App/Editor/EditorSession.cs +++ b/src/Titano.App/Editor/EditorSession.cs @@ -32,7 +32,10 @@ public sealed class EditorSession : IAsyncDisposable private readonly ProjectSaver _saver; private ProjectSettings _basis; - /// The sequence being read again () until its content is taken: saves wait for it. + /// + /// The sequence being read again () until its content is taken: saves and other reads wait + /// for it. + /// private Task _reading = Task.CompletedTask; public EditorSession(ISequenceSource source, SequenceContent content, IReadOnlyList codecs, TimeSpan? saveDelay = null) @@ -209,15 +212,30 @@ public sealed class EditorSession : IAsyncDisposable public Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken = default) => Source.SubmitAsync(request, cancellationToken); + /// + /// Makes the project's frame positions name the sequence's frames as they are now, before the project is taken along + /// (a render embeds it): frames inserted since the last read (a sync download, an import of earlier photos, a scan) + /// are read again and the positions follow them (). A server sequence whose frames changed + /// becomes outdated instead (: read-only, its renders refused). + /// + public async Task EnsureFramesAsync(CancellationToken cancellationToken = default) + { + while (!_reading.IsCompleted) await _reading; + var current = await Source.FramesCurrentAsync(cancellationToken); + // A read that began meanwhile (a save that met the new frames) may have taken them already: it ends first. + while (!_reading.IsCompleted) await _reading; + if (!current && !IsRemote) await ReloadAsync(cancellationToken); + } + /// /// Reads the sequence again (an analysis finished): new reports and advice, the frames with the project's positions - /// following them (), the stored project merged into the open one. Saves wait until it is taken. + /// following them (), the stored project merged into the open one. Saves wait until it is taken, + /// and a read waits for the one under way, so each follows the frames the one before it took. /// public async Task ReloadAsync(CancellationToken cancellationToken = default) { await _saver.FlushAsync(); - var taken = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - _reading = taken.Task; + var taken = await BeginReadAsync(); try { var content = await Source.LoadAsync(cancellationToken); @@ -257,23 +275,34 @@ public sealed class EditorSession : IAsyncDisposable /// Takes as the sequence. Frames inserted or removed since the last read (a sync download, /// an import of earlier photos, a scan) moved the stored project's frame positions with their frames: the editor's /// project, its undo and redo steps and its basis move the same way (), so - /// they keep naming the frames the user chose. Returns that move (none when the frames are the same). + /// they keep naming the frames the user chose. Call while the read is under way (). /// - private Func Follow(SequenceContent content) + private void Follow(SequenceContent content) { var before = Content.Frames.Select(f => f.Id).ToList(); (Content, Context, Advice, Presets) = Analyse(content, _codecs); - if (before.SequenceEqual(content.Frames.Select(f => f.Id))) return static project => project; + if (before.SequenceEqual(content.Frames.Select(f => f.Id))) return; var after = content.Frames; Log.Info("editor", $"The frames of {content.Sequence.DisplayTitle} changed while it was open; its frame positions follow them"); _basis = Move(_basis); History.Remap(Move); - return Move; ProjectSettings Move(ProjectSettings project) => NamesFrames(project) && ProjectSettings.FollowFrames(project.ToJson(), before, after) is { } moved ? ProjectSettings.FromJson(moved) : project; } + /// + /// Waits for the read under way (two jobs that end together read the sequence one after the other), then marks a new + /// one under way until the returned source is completed. + /// + private async Task BeginReadAsync() + { + while (!_reading.IsCompleted) await _reading; + var taken = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _reading = taken.Task; + return taken; + } + /// Exclusions, a hero, keyframes or frames waiting for their photos: positions that follow frames. private static bool NamesFrames(ProjectSettings project) => project.ExcludedFrames.Count > 0 || project.HeroFrame is not null || project.Camera.Keyframes.Count > 0 || project.PendingFrames is { Count: > 0 }; @@ -334,8 +363,18 @@ public sealed class EditorSession : IAsyncDisposable catch (FramesChangedException) when (attempt < FollowAttempts) { // Frames were inserted under the editor and the stored positions moved with them: the frames are read - // again, this edit's positions follow them too, and it is saved in the new order. - project = Follow(await Source.LoadAsync(CancellationToken.None))(project); + // again (after a read under way, whose move this edit's snapshot missed), the project's positions follow + // them, and the project as it is then, which contains this edit, is saved in the new order. + var taken = await BeginReadAsync(); + try + { + Follow(await Source.LoadAsync(CancellationToken.None)); + } + finally + { + taken.SetResult(); + } + project = Project; ContentChanged?.Invoke(); } catch (ProjectConflictException ex) diff --git a/src/Titano.App/Editor/EditorViewModel.cs b/src/Titano.App/Editor/EditorViewModel.cs index 2e4ca35a..a3f11ca8 100644 --- a/src/Titano.App/Editor/EditorViewModel.cs +++ b/src/Titano.App/Editor/EditorViewModel.cs @@ -48,6 +48,9 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable private readonly AsyncRelayCommand _cancelServerJob; private PreviewRenderer _renderer; private int _currentIndex; + + /// The frame on screen: it stays on screen when frames are inserted or removed before it. + private Guid _currentFrameId; private EditorTab _selectedTab; private ImageSource? _previewImage; private ImageSource? _originalImage; @@ -111,6 +114,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable Tabs = [Frames, Exposure, new MotionTab(this), new CameraTab(this), new TimeTab(this), new StackTab(this), new ExportTab(this)]; _selectedTab = Exposure; _currentIndex = session.Project.HeroFrame is { } hero && (uint)hero < (uint)FrameCount ? hero : 0; + _currentFrameId = FrameId(_currentIndex); // Until the link is read (AttachJobsAsync), a sequence of this PC cannot send its renders. _syncGate = session.IsRemote ? ExportSyncGate.Remote : ExportSyncGate.SignedOut; RenderChoices = [.. ExportPresets.BuiltIn.Select(p => new Choice(p.Id, p.NameKey))]; @@ -233,6 +237,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable set { var clamped = FrameCount == 0 ? 0 : int.Clamp(value, 0, FrameCount - 1); + _currentFrameId = FrameId(clamped); if (!Set(ref _currentIndex, clamped)) return; foreach (var tab in Tabs) tab.OnCurrentFrameChanged(); UpdateInfo(); @@ -515,6 +520,10 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable /// public async Task RenderAsync(JobPriority priority, bool batch) { + // The job embeds the project: its frame positions follow frames inserted since the editor read them first. A server + // sequence whose frames changed is outdated instead, and its toast says so and offers fresh proxies. + await Session.EnsureFramesAsync(); + if (IsOutdated) return; var export = batch ? RenderExport : RenderExport with { Batch = [] }; var requests = Session.RenderRequests(export); foreach (var request in requests) await Session.SubmitAsync(request with { Priority = priority }); @@ -801,6 +810,9 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable private void OnContentChanged() { + // The frame on screen stays on screen, wherever frames inserted or removed before it put it (the tabs that reload + // below may seek meanwhile). + var shown = IndexOfFrame(_currentFrameId) ?? int.Min(CurrentIndex, int.Max(0, FrameCount - 1)); _renderer = new PreviewRenderer(Session.Content, Session.Source); Planner = new ExportPlanner(Session.Content, _encoders, _host); if (Thumbnails.Count != FrameCount) Thumbnails = new EditorThumbnails(Session.Source, FrameCount); @@ -808,13 +820,27 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable foreach (var tab in Tabs) tab.OnContentChanged(); OnPropertyChanged(nameof(Summary)); OnPropertyChanged(nameof(FrameCount)); - CurrentIndex = int.Min(CurrentIndex, int.Max(0, FrameCount - 1)); + CurrentIndex = shown; UpdateMarkers(); RequestCorrections(); RequestPlan(); RequestPreview(immediate: true); } + /// The id of frame ; empty when there is none. + private Guid FrameId(int index) => (uint)index < (uint)FrameCount ? Session.Content.Frames[index].Id : Guid.Empty; + + /// Where frame is now; null when the sequence no longer has it. + private int? IndexOfFrame(Guid id) + { + var frames = Session.Content.Frames; + for (var i = 0; i < frames.Count; i++) + { + if (frames[i].Id == id) return i; + } + return null; + } + /// /// The UI language changed: what the tabs keep in objects (chart legends and summaries, the Auto chips of the /// settings), the timeline markers and the preview's info line are made again in the new language. diff --git a/src/Titano.App/Editor/ISequenceSource.cs b/src/Titano.App/Editor/ISequenceSource.cs index 00e089a2..bafab671 100644 --- a/src/Titano.App/Editor/ISequenceSource.cs +++ b/src/Titano.App/Editor/ISequenceSource.cs @@ -44,6 +44,14 @@ public interface ISequenceSource : IAsyncDisposable /// The sequence is gone, or its project cannot be read (corrupt, or written by a newer Titano). Task LoadAsync(CancellationToken cancellationToken); + /// + /// Whether the frames read last are still the sequence's, in the same order: frames inserted + /// or removed since (a sync download, an import of earlier photos, a scan) moved the stored project's positions. A + /// server sequence whose frames changed becomes . + /// + /// The sequence is gone. + Task FramesCurrentAsync(CancellationToken cancellationToken); + /// /// Frame in linear light, oriented, long edge at most ; null /// when the frame cannot be read. The caller owns the buffer. diff --git a/src/Titano.App/Editor/LocalSequenceSource.cs b/src/Titano.App/Editor/LocalSequenceSource.cs index a7d54027..300491ee 100644 --- a/src/Titano.App/Editor/LocalSequenceSource.cs +++ b/src/Titano.App/Editor/LocalSequenceSource.cs @@ -105,6 +105,14 @@ public sealed class LocalSequenceSource : ISequenceSource }, cancellationToken); } + /// An insertion bumps the row's frames version in the transaction that renumbers the frames. + public async Task FramesCurrentAsync(CancellationToken cancellationToken) + { + var framesVersion = _framesVersion; + var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); + return row.FramesVersion == framesVersion; + } + public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { if ((uint)index >= (uint)_files.Length || _files[index] is not { } file) return null; diff --git a/src/Titano.App/Editor/RemoteSequenceSource.cs b/src/Titano.App/Editor/RemoteSequenceSource.cs index 97993e95..1a4bddf8 100644 --- a/src/Titano.App/Editor/RemoteSequenceSource.cs +++ b/src/Titano.App/Editor/RemoteSequenceSource.cs @@ -130,6 +130,13 @@ public sealed class RemoteSequenceSource : ISequenceSource }, cancellationToken); } + /// Read from the server's manifest: a sequence whose frames are not this PC's becomes outdated. + public async Task FramesCurrentAsync(CancellationToken cancellationToken) + { + if (IsOutdated) return false; + return Compare(await Call(() => _api.GetManifestAsync(Id, cancellationToken))); + } + public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { if ((uint)index >= (uint)_remote.Frames.Count) return null; @@ -305,9 +312,7 @@ public sealed class RemoteSequenceSource : ISequenceSource { try { - var manifest = await _api.GetManifestAsync(Id, cancellationToken).ConfigureAwait(false); - if (SameFrames(manifest)) Volatile.Write(ref _framesVersion, manifest.FramesVersion); - else MarkOutdated(); + Compare(await _api.GetManifestAsync(Id, cancellationToken).ConfigureAwait(false)); } catch (Exception ex) when (ex is TitanoApiException or HttpRequestException or JsonException || (ex is OperationCanceledException && !cancellationToken.IsCancellationRequested)) @@ -344,6 +349,21 @@ public sealed class RemoteSequenceSource : ISequenceSource JobChanged?.Invoke(job); } + /// + /// Takes the server's frames as lists them: their frames version when they are this PC's + /// (saves name it), outdated otherwise. Returns whether they are. + /// + private bool Compare(SequenceManifest manifest) + { + if (!SameFrames(manifest)) + { + MarkOutdated(); + return false; + } + Volatile.Write(ref _framesVersion, manifest.FramesVersion); + return true; + } + /// The server's frames are the cached ones, in the same order (frame positions in the project mean the same frames). private bool SameFrames(SequenceManifest manifest) => manifest.Frames.OrderBy(f => f.Index).Select(f => f.Id).SequenceEqual(_remote.Frames.Select(f => f.Id)); diff --git a/tests/Titano.App.Tests/Editor/EditorFixture.cs b/tests/Titano.App.Tests/Editor/EditorFixture.cs index 2099ebd1..3a950da9 100644 --- a/tests/Titano.App.Tests/Editor/EditorFixture.cs +++ b/tests/Titano.App.Tests/Editor/EditorFixture.cs @@ -77,6 +77,9 @@ internal sealed class FakeSequenceSource(SyntheticSequence sequence, SequenceCon { private readonly SyntheticFrameSource _pixels = new(sequence); + /// The frames the session read last (opened with, or loaded since). + private IReadOnlyList _read = content.Frames; + public ProjectSettings Stored { get; set; } = content.Project; public SequenceContent Content { get; set; } = content; @@ -114,11 +117,36 @@ internal sealed class FakeSequenceSource(SyntheticSequence sequence, SequenceCon /// Thrown by the next load (the sequence read again after a job). public Exception? FailNextLoad { get; set; } - public Task LoadAsync(CancellationToken cancellationToken) + /// Awaited by the next load once it has read the sequence (a read that is slow to complete). + public Task? NextLoadGate { get; set; } + + /// Times the sequence was read (). + public int Loads { get; private set; } + + public async Task LoadAsync(CancellationToken cancellationToken) { - if (FailNextLoad is not { } failure) return Task.FromResult(Content with { Project = Stored }); - FailNextLoad = null; - return Task.FromException(failure); + if (FailNextLoad is { } failure) + { + FailNextLoad = null; + throw failure; + } + Loads++; + var content = Content with { Project = Stored }; + if (NextLoadGate is { } gate) + { + NextLoadGate = null; + await gate; + } + _read = content.Frames; + return content; + } + + /// Whether holds the frames read last; a server sequence becomes outdated when not. + public Task FramesCurrentAsync(CancellationToken cancellationToken) + { + var current = _read.Select(f => f.Id).SequenceEqual(Content.Frames.Select(f => f.Id)); + if (!current && IsRemote) MarkOutdated(); + return Task.FromResult(current); } public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) diff --git a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs index 9b9a5b44..c4f8e0c2 100644 --- a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs @@ -151,6 +151,89 @@ public sealed class EditorSessionTests } } + [Test] + public async Task ARenderAfterFramesWereInsertedEmbedsThePositionsOfTheFramesTheUserChose() + { + var (session, source) = OpenShort(); + await using (session) + { + Assert.True(session.SetExcluded([3], true)); + Assert.True(session.SetHero(4)); + Assert.True(await session.FlushAsync()); + var frames = session.Content.Frames; + // Two earlier photos are inserted and the index moves the stored positions; the user renders without an edit. + InsertEarlier(source, 2); + source.Stored = source.Stored with { ExcludedFrames = [5], HeroFrame = 6 }; + + await session.EnsureFramesAsync(); + var requests = session.RenderRequests(session.Project.Export); + + Assert.Equal(frames.Count + 2, session.Content.Count, "the frames were read again"); + var parameters = JsonSerializer.Deserialize(requests[0].ParametersJson!, ProtocolJson.Default.RenderJobParameters)!; + var embedded = ProjectSettings.FromJson(parameters.SettingsJson!); + Assert.SequenceEqual([5], embedded.ExcludedFrames); + Assert.Equal(6, embedded.HeroFrame); + Assert.Equal(frames[3].Id, session.Content.Frames[embedded.ExcludedFrames[0]].Id); + Assert.Equal(frames[4].Id, session.Content.Frames[embedded.HeroFrame!.Value].Id); + Assert.True(session.Undo()); + Assert.Null(session.Project.HeroFrame); + Assert.SequenceEqual([5], session.Project.ExcludedFrames, "undo steps name the same frames"); + + // Frames that did not change are not read again. + var loads = source.Loads; + await session.EnsureFramesAsync(); + Assert.Equal(loads, source.Loads); + } + } + + [Test] + public async Task AServerSequenceWhoseFramesChangedIsOutdatedBeforeItsRender() + { + var sequence = EditorFixture.Sequence(12); + var content = EditorFixture.Content(sequence, deep: false); + var source = new FakeSequenceSource(sequence, content) { IsRemote = true }; + var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)); + await using (session) + { + InsertEarlier(source, 1); + + await session.EnsureFramesAsync(); + + Assert.True(source.IsOutdated); + Assert.True(session.IsReadOnly); + Assert.Equal(0, source.Loads, "its proxies are not the server's frames: nothing is read"); + Assert.Equal(12, session.Content.Count); + } + } + + [Test] + public async Task ReadsThatOverlapFollowTheFramesInTheOrderTheyWereRead() + { + var (session, source) = OpenShort(); + await using (session) + { + Assert.True(session.SetExcluded([3], true)); + Assert.True(await session.FlushAsync()); + var frames = session.Content.Frames; + // A job ends after an earlier photo was inserted, and its read of the sequence is slow to complete. + InsertEarlier(source, 1); + source.Stored = source.Stored with { ExcludedFrames = [4] }; + var slow = new TaskCompletionSource(); + source.NextLoadGate = slow.Task; + var first = session.ReloadAsync(); + // Another job ends after two more were inserted: its read waits for the first one and follows its frames. + InsertEarlier(source, 2); + source.Stored = source.Stored with { ExcludedFrames = [6] }; + var second = session.ReloadAsync(); + slow.SetResult(); + await Task.WhenAll(first, second); + + Assert.Equal(frames.Count + 3, session.Content.Count); + Assert.SequenceEqual([6], session.Project.ExcludedFrames); + Assert.Equal(frames[3].Id, session.Content.Frames[6].Id); + } + } + /// A short sequence without its analysis, whose frame ids are all different (the synthetic ids repeat every 64 frames). private static (EditorSession Session, FakeSequenceSource Source) OpenShort() { diff --git a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs index 70ccf97b..6d7289b6 100644 --- a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs @@ -14,6 +14,7 @@ using Titano.App.Sections; using Titano.App.Shell; using Titano.App.Tests.Windows; using Titano.Core.Advice; +using Titano.Core.Analysis; using Titano.Core.Export; using Titano.Core.Localization; using Titano.Core.Model; @@ -299,6 +300,36 @@ public sealed class EditorSmokeTests } }); + /// + /// Frames inserted under the editor (a sync download, an import of earlier photos) move the frame on screen with its + /// photo: what the frame actions (Exclude, Set hero, a keyframe) apply to stays the photo the user looks at. + /// + [Test] + public Task TheFrameOnScreenStaysWhenFramesAreInsertedBeforeIt() => ScreenHost.RunAsync(async () => + { + await using var app = await ScreenSmokeTests.SampleApp.StartAsync(); + var content = EditorFixture.Content(EditorFixture.Sunset, deep: false); + var source = new FakeSequenceSource(EditorFixture.Sunset, content); + var editor = new EditorViewModel(app.Shell, new EditorSession(source, content, []), [], EncoderHost.Client); + await using (editor) + { + editor.CurrentIndex = 5; + var shown = content.Frames[5].Id; + // An earlier photo arrives and the last frame goes: as many frames as before, each one place later. + var first = content.Frames[0]; + IReadOnlyList frames = + [.. new[] { first with { Id = Guid.CreateVersion7(), CapturedAt = first.CapturedAt!.Value.AddMinutes(-1) } }.Concat(content.Frames.SkipLast(1)) + .Select((f, i) => f with { Index = i })]; + source.Content = content with { Frames = frames, Light = LightAnalyzer.Analyze(frames) }; + + await editor.Session.ReloadAsync(); + + Assert.Equal(6, editor.CurrentIndex); + Assert.Equal(shown, editor.Session.Content.Frames[editor.CurrentIndex].Id); + Assert.Equal(6, editor.Frames.Selected?.Index); + } + }); + /// The measuring tool of each section has content. private static void ExpectMeasure(SequenceView view, EditorTab tab) { diff --git a/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs b/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs index 05a9c11c..7b1a6e51 100644 --- a/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs +++ b/tests/Titano.App.Tests/Editor/FakeRemoteSequenceApi.cs @@ -18,6 +18,7 @@ namespace Titano.App.Tests.Editor; internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyList frames) : IRemoteSequenceApi { private readonly Lock _gate = new(); + private readonly TaskCompletionSource _subscribed = new(TaskCreationOptions.RunContinuationsAsynchronously); private Channel _events = Channel.CreateUnbounded(); /// The server's frames in order (another list when frames are added or removed on the server). @@ -55,6 +56,9 @@ internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyListCompletes once the editor's listener has opened an event stream (the stream open then is the one it reads). + public Task Subscribed => _subscribed.Task; + /// A job of the server, as GET /jobs/{id} returns it. public JobDto Job(Guid id, string kind, string state, Guid? target = null, long done = 0, long total = 0) => new(id, kind, [target ?? sequence.Id], sequence.DisplayTitle, state, "user", done, total, null, null, null, null, 0, null, DateTimeOffset.UtcNow, @@ -168,6 +172,7 @@ internal sealed class FakeRemoteSequenceApi(Sequence sequence, IReadOnlyList EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken) { var events = Volatile.Read(ref _events).Reader; + _subscribed.TrySetResult(); while (await events.WaitToReadAsync(cancellationToken)) { while (events.TryRead(out var e)) yield return e; diff --git a/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs index 4a8d4de5..6fd23079 100644 --- a/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs +++ b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs @@ -156,17 +156,7 @@ public sealed class ProjectStoreTests(TestContext context) // Three photos taken before the others are found in the folder (a sync download, an import or a scan // inserts them the same way): the index moves the stored positions with their frames. - var sequence = EditorFixture.Sequence(3); - var first = content.Frames[0].CapturedAt!.Value; - for (var i = 0; i < 3; i++) - { - using var image = sequence.Render(i); - var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i] with { CapturedAt = first.AddMinutes(i - 3) }); - File.WriteAllBytes(Path.Combine(root, "Synthetic", $"EARLY_{i + 1}.JPG"), - JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] })); - } - var archive = (await db.Archives.GetAsync(content.Sequence.ArchiveId, context.Cancellation))!; - await new ArchiveService(db, options).Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation); + await InsertEarlierAsync(db, root, content, options, 3); var moved = ProjectSettings.FromJson((await db.Sequences.GetAsync(sequenceId))!.SettingsJson!); Assert.SequenceEqual([8], moved.ExcludedFrames); Assert.Equal(9, moved.HeroFrame); @@ -195,6 +185,36 @@ public sealed class ProjectStoreTests(TestContext context) } } + [Test] + public async Task ARenderAfterEarlierPhotosArriveEmbedsTheFramesTheUserChose() + { + var (db, sequenceId, root) = await ArchiveAsync(10); + await using (db) + { + var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }; + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null, options); + var content = await source.LoadAsync(context.Cancellation); + var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)); + await using (session) + { + Assert.True(session.SetExcluded([5], true)); + Assert.True(await session.FlushAsync()); + Assert.True(await source.FramesCurrentAsync(context.Cancellation)); + + // Earlier photos arrive while the editor is open; the user renders without another edit. + await InsertEarlierAsync(db, root, content, options, 3); + Assert.False(await source.FramesCurrentAsync(context.Cancellation)); + await session.EnsureFramesAsync(context.Cancellation); + var request = session.RenderRequests(session.Project.Export)[0]; + + Assert.True(await source.FramesCurrentAsync(context.Cancellation)); + Assert.Equal(13, session.Content.Count); + var embedded = ProjectSettings.FromJson(ProcessingJobs.ReadRender(request.ParametersJson).Parameters.SettingsJson!); + Assert.SequenceEqual([content.Frames[5].Id], embedded.ExcludedFrames.Select(i => session.Content.Frames[i].Id), "the photo the user excluded"); + } + } + } + [Test] public async Task ASaveThatDoesNotReachTheIndexThrows() { @@ -249,6 +269,25 @@ public sealed class ProjectStoreTests(TestContext context) } } + /// + /// Writes photos taken before the sequence's first frame into its folder and scans the + /// archive: they are inserted in front, and the index moves the stored positions with their frames. + /// + private async Task InsertEarlierAsync(TitanoDatabase db, string root, SequenceContent content, ArchiveServiceOptions options, int count) + { + var sequence = EditorFixture.Sequence(count); + var first = content.Frames[0].CapturedAt!.Value; + for (var i = 0; i < count; i++) + { + using var image = sequence.Render(i); + var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i] with { CapturedAt = first.AddMinutes(i - count) }); + File.WriteAllBytes(Path.Combine(root, "Synthetic", $"EARLY_{i + 1}.JPG"), + JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] })); + } + var archive = (await db.Archives.GetAsync(content.Sequence.ArchiveId, context.Cancellation))!; + await new ArchiveService(db, options).Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation); + } + /// A temporary archive with one adopted folder of synthetic JPEGs, indexed. private async Task<(TitanoDatabase Db, Guid SequenceId, string Root)> ArchiveAsync(int frames) { diff --git a/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs b/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs index 9a6c9043..fee4899b 100644 --- a/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs +++ b/tests/Titano.App.Tests/Editor/RemoteSequenceSourceTests.cs @@ -159,7 +159,7 @@ public sealed class RemoteSequenceSourceTests(TestContext context) Assert.Same(conflict, session.Conflict); Assert.False(await session.FlushAsync(), "the change waits for the choice"); Assert.True(session.ResolveConflict(keepMine: true)); - Assert.True(await session.FlushAsync()); + Assert.True(await FlushAfterChoiceAsync(session)); Assert.Null(session.Conflict); var stored = ProjectSettings.FromJson(api.SettingsJson!); @@ -181,7 +181,7 @@ public sealed class RemoteSequenceSourceTests(TestContext context) await found.Task.WaitAsync(TimeSpan.FromSeconds(10)); Assert.True(session.ResolveConflict(keepMine: false)); - Assert.True(await session.FlushAsync()); + Assert.True(await FlushAfterChoiceAsync(session)); var stored = ProjectSettings.FromJson(api.SettingsJson!); Assert.Equal((0.3, -0.2, (int?)2), (stored.Exposure.ExposureOffsetEv, stored.Camera.OffsetY, stored.HeroFrame), "the server's values where both changed; the editor's other change is saved"); @@ -469,6 +469,8 @@ public sealed class RemoteSequenceSourceTests(TestContext context) await using (source) { await source.LoadAsync(context.Cancellation); + // The listener reads the stream open when it subscribes: it must have subscribed for the drop to reach it. + await api.Subscribed.WaitAsync(TimeSpan.FromSeconds(10), context.Cancellation); api.DropEvents(); api.Frames = [.. remote.Frames.Skip(1).Select(f => f.Id)]; api.FramesVersion = 2; @@ -478,6 +480,30 @@ public sealed class RemoteSequenceSourceTests(TestContext context) } } + [Test] + public async Task ARenderIsRefusedOnceTheServersFramesChangedWithoutAnEvent() + { + var (source, api, remote) = Open(); + await using (source) + { + await source.LoadAsync(context.Cancellation); + // A new frames version over the same frames: they are still this PC's. + api.FramesVersion = 2; + Assert.True(await source.FramesCurrentAsync(context.Cancellation)); + Assert.False(source.IsOutdated); + + // A server scan inserts an earlier photo and sends no event: the check before a render finds it. + api.Frames = [Guid.CreateVersion7(), .. remote.Frames.Select(f => f.Id)]; + api.FramesVersion = 3; + Assert.False(await source.FramesCurrentAsync(context.Cancellation)); + Assert.True(source.IsOutdated); + var refused = await Assert.ThrowsAsync(() => + source.SubmitAsync(ProcessingJobs.Render(remote.Sequence, new ExportSettings(), 1920, 1080), context.Cancellation)); + Assert.Equal(Loc.L("Editor.Error.FramesChanged"), refused.Message); + Assert.Empty(api.Posted); + } + } + [Test] public async Task AConflictFoundOnOtherFramesIsNotMergedOntoThem() { @@ -591,6 +617,13 @@ public sealed class RemoteSequenceSourceTests(TestContext context) lock (seen) return seen[^1]; } + /// + /// Stores the change after the user settled a conflict. The test's thread is not the saver's: a retry that started + /// before the choice may still be failing on the old basis, and a flush that arrives meanwhile reports it, so one more + /// flush (on the UI thread the choice cannot land inside a retry) is answered by the attempt made after the choice. + /// + private static async Task FlushAfterChoiceAsync(EditorSession session) => await session.FlushAsync() || await session.FlushAsync(); + private static async Task WaitAsync(Func condition) { for (var i = 0; i < 200; i++) From 9ca10d96f42cd2b17a5dc560fa708d4f841498aa Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:59:53 +0000 Subject: [PATCH 6/8] fix(sync): compare stand-ins again after a proxy download that did not finish The agent recorded the server's update time for a cache of stand-in thumbnails before the proxy download it queued, so a download that failed (server restart, network drop) was never queued again once its backoff ended: the server's sequence may never change again. A comparison that offers proxies is now recorded only once their download is queued, and dropped when that download ends other than done. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Sync/SyncAgent.cs | 14 ++++++-- .../Titano.Data.Tests/Sync/SyncAgentTests.cs | 35 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Titano.Data/Sync/SyncAgent.cs b/src/Titano.Data/Sync/SyncAgent.cs index 9b3594ec..caa6e992 100644 --- a/src/Titano.Data/Sync/SyncAgent.cs +++ b/src/Titano.Data/Sync/SyncAgent.cs @@ -237,6 +237,9 @@ public sealed class SyncAgent : IAsyncDisposable var job = await _sync.Database.Jobs.GetAsync(jobId, cancellationToken).ConfigureAwait(false); if (job?.State is JobState.Queued or JobState.Running or JobState.Paused) continue; _proxyJobs.Remove(remoteId); + // A download of stand-ins' proxies that did not finish compares the cache with the manifest again (after its + // backoff when it failed): the server's sequence may never change again to ask for it. + if (job?.State != JobState.Done) _standInsCompared.Remove(remoteId); if (job?.State == JobState.Failed) { var failures = _proxyFailures.GetValueOrDefault(remoteId).Failures + 1; @@ -255,6 +258,8 @@ public sealed class SyncAgent : IAsyncDisposable var job = await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Proxies, null, JobPriority.Background, cancellationToken).ConfigureAwait(false); _proxyJobs[link.RemoteId] = job.Id; + // Compared at this update time once the download that brings what the server offers is queued. + if (!link.ServerHasProxies) _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; } } @@ -263,7 +268,9 @@ public sealed class SyncAgent : IAsyncDisposable /// of a cache that did not record its kind) or that has no cached file. The sequence's proxy flag cannot say: it waits /// for every frame, a missing or damaged one never gets a proxy, and clients and render nodes send the other frames' /// proxies one by one. The manifest is read once per change of the server's sequence, since a proxy that arrives - /// leaves a new update time (the proxy pass it queues records the sequence's state). + /// leaves a new update time (the proxy pass it queues records the sequence's state). A comparison that offers nothing + /// is recorded here; one that offers proxies is recorded when their download is queued, and dropped when that + /// download does not finish (). /// private async Task OffersProxiesAsync(SyncLink link, CancellationToken cancellationToken) { @@ -280,9 +287,10 @@ public sealed class SyncAgent : IAsyncDisposable Log.Info(LogCategory, $"The proxies of {link.Title} on {Server} could not be compared: {ex.Message}"); return false; } - _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; var cached = _sync.Proxies.Kinds(Server, link.RemoteId); - return manifest.Frames.Any(f => f.HasProxy && cached.GetValueOrDefault(f.Id) != ProxyCache.Proxy); + if (manifest.Frames.Any(f => f.HasProxy && cached.GetValueOrDefault(f.Id) != ProxyCache.Proxy)) return true; + _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; + return false; } /// Waits for a wake-up or the delay; false when stopping. diff --git a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs index 6188f1ae..5c12e86d 100644 --- a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs +++ b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs @@ -192,6 +192,41 @@ public sealed class SyncAgentTests(TestContext context) Assert.Count(2, await DownloadsAsync(f)); } + [Test] + public async Task AStandInDownloadThatFailedIsQueuedAgainAfterItsBackoff() + { + await using var f = await SyncFixture.CreateAsync(context); + var remote = f.Remote.Add("Fog", f.NewPhotos(3), status: SequenceStatus.ThumbsBuilt); + remote.Undecodable = true; + var engine = await f.StartEngineAsync(); + await f.RefreshAsync(); + var first = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Fog", SyncDownloadMode.Proxies, null); + await f.Archive.WaitForAsync(first.Id, JobState.Done); + await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(KeepProxiesOffline: true), engine, EventsOnly); + agent.Start(); + await WaitAsync(() => agent.Snapshot is not null, "first comparison"); + + // Two frames' proxies reach the server, but their download fails (the server restarts meanwhile). + foreach (var frame in remote.Frames.Take(2)) f.Remote.SentProxies[frame.Id] = frame.Proxy; + remote.UpdatedAt = DateTimeOffset.UtcNow.AddSeconds(1); + f.Remote.FailAssets = true; + await agent.RefreshAsync(f.Ct); + var failed = Assert.Contains(await DownloadsAsync(f), j => j.Id != first.Id); + await f.Archive.WaitForAsync(failed.Id, JobState.Failed); + + // The server's sequence does not change again: once the backoff has passed, the download is queued anyway. + f.Remote.FailAssets = false; + await WaitAsync(async () => + { + await agent.RefreshAsync(f.Ct); + return (await DownloadsAsync(f)).Count == 3; + }, "the download queued again"); + var retry = Assert.Contains(await DownloadsAsync(f), j => j.Id != first.Id && j.Id != failed.Id); + await f.Archive.WaitForAsync(retry.Id, JobState.Done); + var opened = Assert.NotNull(f.Sync.OpenRemote(f.Server, remote.Id)); + Assert.SequenceEqual([remote.Frames[0].Proxy.Length, remote.Frames[1].Proxy.Length, 64], opened.Files.Select(file => (int)new FileInfo(file!).Length)); + } + private static async Task> DownloadsAsync(SyncFixture f) => (await f.Db.Jobs.ListAsync(null, JobKind.Download, 100, null, f.Ct)).Items; From 991742462bfb0b754c4a4ba6c9d7e7fc76ddebb1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 19:59:54 +0000 Subject: [PATCH 7/8] 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] From 3eff486ef8cbd8db9402853c2e215c03e2074887 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 20:00:48 +0000 Subject: [PATCH 8/8] docs(editor): say the session reads inserted frames before a render too Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Editor/EditorSession.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs index 6ee1beea..3905649b 100644 --- a/src/Titano.App/Editor/EditorSession.cs +++ b/src/Titano.App/Editor/EditorSession.cs @@ -14,8 +14,8 @@ namespace Titano.App.Editor; /// jobs. Every change is saved shortly after it is made (); a change stored meanwhile by /// someone else (an analysis job, another client) is merged in. On a server sequence, settings both sides changed to /// different values wait for the user's choice (). Frames inserted under the editor (a sync -/// download, an import of earlier photos, a scan) are read again before the next save, and the project's frame positions, -/// its undo steps included, move with their frames as the stored ones did. Call from one thread (the UI thread). +/// download, an import of earlier photos, a scan) are read again before the next save or render, and the project's frame +/// positions, its undo steps included, move with their frames as the stored ones did. Call from one thread (the UI thread). /// public sealed class EditorSession : IAsyncDisposable {