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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 16:45:44 +00:00
1 parent 9cbfaae10d
commit 3f3ca31a3f
2 files changed
+11 -9

No files matched your search

+2 -2
View File
@@ -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);
}
}
@@ -42,8 +42,7 @@ public sealed class RemoteSequenceSource : ISequenceSource
private readonly ConcurrentDictionary<Guid, byte> _foreign = new();
private readonly CancellationTokenSource _stopping = new();
private readonly Task _listening;
private ProjectSettings? _stored;
private int _version;
private Stored? _stored;
private int _disposed;
/// <param name="readOnly">The account may look at the sequence but not change it (no Edit right on the server).</param>
@@ -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);
}
}
/// <summary>The server's settings as last read or written, with the version a save must name.</summary>
private sealed record Stored(ProjectSettings Project, int Version);
}