From 3f3ca31a3f2e41218fb44e5eec28112696661d37 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 16:45:44 +0000 Subject: [PATCH] fix(editor): a server sequence's settings and their version are read and replaced together Saves run on the saver's thread while a reload reads the server: one immutable pair keeps a save from naming the version of other settings. The sample server keeps its settings when an edit sends none. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/DesignTime/SampleData.cs | 4 ++-- src/Titano.App/Editor/RemoteSequenceSource.cs | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Titano.App/DesignTime/SampleData.cs b/src/Titano.App/DesignTime/SampleData.cs index 9adecab5..7f0e1551 100644 --- a/src/Titano.App/DesignTime/SampleData.cs +++ b/src/Titano.App/DesignTime/SampleData.cs @@ -682,9 +682,9 @@ public sealed class SampleData { lock (_sequences) { - var (dto, _, frames) = _sequences[id]; + var (dto, settings, frames) = _sequences[id]; var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow }; - _sequences[id] = (updated, request.SettingsJson, frames); + _sequences[id] = (updated, request.SettingsJson ?? settings, frames); return Task.FromResult(updated); } } diff --git a/src/Titano.App/Editor/RemoteSequenceSource.cs b/src/Titano.App/Editor/RemoteSequenceSource.cs index 4a151dd5..1a5b4421 100644 --- a/src/Titano.App/Editor/RemoteSequenceSource.cs +++ b/src/Titano.App/Editor/RemoteSequenceSource.cs @@ -42,8 +42,7 @@ public sealed class RemoteSequenceSource : ISequenceSource private readonly ConcurrentDictionary _foreign = new(); private readonly CancellationTokenSource _stopping = new(); private readonly Task _listening; - private ProjectSettings? _stored; - private int _version; + private Stored? _stored; private int _disposed; /// The account may look at the sequence but not change it (no Edit right on the server). @@ -78,7 +77,7 @@ public sealed class RemoteSequenceSource : ISequenceSource return await Task.Run(() => { var project = Parse(manifest.SettingsJson); - (_stored, _version) = (project, manifest.SettingsVersion); + Volatile.Write(ref _stored, new Stored(project, manifest.SettingsVersion)); var frames = _remote.Frames; var sequence = _remote.Sequence with { @@ -127,16 +126,16 @@ public sealed class RemoteSequenceSource : ISequenceSource ArgumentNullException.ThrowIfNull(basis); for (var attempt = 1; ; attempt++) { - var stored = _stored ?? throw new InvalidOperationException("LoadAsync first."); + var (stored, version) = Volatile.Read(ref _stored) ?? throw new InvalidOperationException("LoadAsync first."); var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored); if (ProjectMerge.SameValues(merged, stored)) return merged; if (IsReadOnly) throw new InvalidOperationException(Loc.L("Editor.Error.RemoteReadOnly")); var hero = merged.HeroFrame == stored.HeroFrame ? (Guid?)null : HeroFrameId(merged); try { - var updated = await Call(() => _api.UpdateSequenceAsync(Id, new UpdateSequenceRequest(null, null, null, null, merged.ToJson(), _version, hero), + var updated = await Call(() => _api.UpdateSequenceAsync(Id, new UpdateSequenceRequest(null, null, null, null, merged.ToJson(), version, hero), cancellationToken)); - (_stored, _version) = (merged, updated.SettingsVersion); + Volatile.Write(ref _stored, new Stored(merged, updated.SettingsVersion)); return merged; } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict) @@ -144,7 +143,7 @@ public sealed class RemoteSequenceSource : ISequenceSource // Changed on the server since this editor read it: merge again on top of the server's settings. if (attempt == SaveAttempts) throw new IOException(Loc.L("Editor.Error.SaveConflict"), ex); var manifest = await Call(() => _api.GetManifestAsync(Id, cancellationToken)); - (_stored, _version) = (Parse(manifest.SettingsJson), manifest.SettingsVersion); + Volatile.Write(ref _stored, new Stored(Parse(manifest.SettingsJson), manifest.SettingsVersion)); } } } @@ -304,4 +303,7 @@ public sealed class RemoteSequenceSource : ISequenceSource throw new InvalidOperationException(Loc.L("Editor.Error.Missing"), ex); } } + + /// The server's settings as last read or written, with the version a save must name. + private sealed record Stored(ProjectSettings Project, int Version); }