fix(editor): server jobs, stale proxies and read-only accounts in the remote editor

- A server sequence whose cached frames are no longer the server's (a load, a 409
  merge or a frames change event finds other frame ids) turns read-only and says
  why; opening it, or a sequence without cached proxies, queues the proxy
  download and opens it when done. Signed out reads "Connect to the server".
- Server jobs show queued, running (with a bar), paused and analysing glyphs in
  the header with Cancel; a failed or cancelled one raises a toast with Retry.
  Progress alone no longer refreshes the whole editor.
- The server job list is read by state and kind to its last page; "Add to queue"
  sets the job's priority; the event listener survives any failure and always
  releases its frames and connection.
- An account that may render but not edit picks the preset to render without
  changing the project; preset cards and their batch boxes say why they wait.
- Export tab of a server sequence: no local encoder, notices or folder; no Jobs
  history link. Sync at 12 px beside a folder that keeps 96 px and trims.
- Restore's Undo puts back the project from before the restore, only while that
  restore is the editor's last change.
- Export "Sync" of a render that ends while the session is down is queued for the
  saved session's server; skips are logged; sync follows the engine before it
  starts.
- Capture: sequence-remote-export, -running and -readonly, and
  sequence-export-copies.

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-05 22:15:34 +00:00
1 parent 2813184b85
commit 757695f037
27 files changed
+932 -173

No files matched your search

+124 -25
View File
@@ -27,6 +27,9 @@ namespace Titano.App.Editor;
/// version they were made from; a 409 (someone changed them meanwhile) reads the server's settings and merges again as
/// the local archive does (<see cref="ProjectMerge"/>: the editor wins on members both changed). Nothing is written to a
/// 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 <see cref="ProxiesOutdatedException"/>, and frames that change on the server while the sequence is open make
/// it <see cref="IsOutdated"/> (read-only, nothing saved or rendered).
/// </summary>
public sealed class RemoteSequenceSource : ISequenceSource
{
@@ -42,7 +45,10 @@ public sealed class RemoteSequenceSource : ISequenceSource
private readonly ConcurrentDictionary<Guid, byte> _foreign = new();
private readonly CancellationTokenSource _stopping = new();
private readonly Task _listening;
private readonly bool _noEditRight;
private Stored? _stored;
private int _framesVersion = -1;
private int _outdated;
private int _disposed;
/// <param name="readOnly">The account may look at the sequence but not change it (no Edit right on the server).</param>
@@ -53,7 +59,7 @@ public sealed class RemoteSequenceSource : ISequenceSource
ArgumentNullException.ThrowIfNull(decoder);
_remote = remote;
_api = api;
IsReadOnly = readOnly;
_noEditRight = readOnly;
_frames = remote.OpenFrames(decoder);
_pixels = (IFrameSource)_frames;
var stopping = _stopping.Token;
@@ -62,7 +68,11 @@ public sealed class RemoteSequenceSource : ISequenceSource
public bool IsRemote => true;
public bool IsReadOnly { get; }
public bool IsReadOnly => _noEditRight || IsOutdated;
public bool IsOutdated => Volatile.Read(ref _outdated) == 1;
public event Action? ReadOnlyChanged;
public event Action<Job>? JobChanged;
@@ -73,6 +83,8 @@ public sealed class RemoteSequenceSource : ISequenceSource
{
var dto = await Call(() => _api.GetSequenceAsync(Id, cancellationToken));
var manifest = await Call(() => _api.GetManifestAsync(Id, cancellationToken));
if (!SameFrames(manifest)) throw Outdated();
Volatile.Write(ref _framesVersion, manifest.FramesVersion);
var report = await Call(() => _api.GetAnalysisReportAsync(Id, cancellationToken));
return await Task.Run(() =>
{
@@ -129,7 +141,8 @@ public sealed class RemoteSequenceSource : ISequenceSource
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"));
if (IsOutdated) throw new ProxiesOutdatedException(Loc.L("Editor.Error.FramesChanged"));
if (_noEditRight) throw new InvalidOperationException(Loc.L("Editor.Error.RemoteReadOnly"));
var hero = merged.HeroFrame == stored.HeroFrame ? (Guid?)null : HeroFrameId(merged);
try
{
@@ -143,15 +156,22 @@ 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));
if (!SameFrames(manifest)) throw Outdated();
Volatile.Write(ref _stored, new Stored(Parse(manifest.SettingsJson), manifest.SettingsVersion));
}
}
}
/// <remarks>
/// The server queues every job at the user's priority; another priority ("Add to queue") is set right after. A
/// render embeds the project, whose frame positions mean nothing once the frames changed: refused while outdated.
/// </remarks>
public async Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
if (request.Kind == JobKind.Render && IsOutdated) throw new ProxiesOutdatedException(Loc.L("Editor.Error.FramesChanged"));
var dto = await Call(() => _api.CreateJobAsync(new CreateJobRequest(JobWire.Kind(request.Kind), request.Targets, request.ParametersJson), cancellationToken));
if (request.Priority != JobPriority.User) dto = await PrioritiseAsync(dto, request.Priority, cancellationToken);
// The server's events may have reported the job already; its first state never replaces a later one.
if (JobWire.FromDto(dto) is { } job && _jobs.TryAdd(job.Id, job)) JobChanged?.Invoke(job);
return dto.Id;
@@ -165,44 +185,65 @@ public sealed class RemoteSequenceSource : ISequenceSource
return mine;
}
public async Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken)
{
if (JobWire.FromDto(await _api.CancelJobAsync(jobId, cancellationToken)) is { } job) Publish(job);
}
public async Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken)
{
if (JobWire.FromDto(await _api.RetryJobAsync(jobId, cancellationToken)) is { } job) Publish(job);
}
public async ValueTask DisposeAsync()
{
if (Interlocked.Exchange(ref _disposed, 1) == 1) return;
await _stopping.CancelAsync();
await _listening;
_stopping.Dispose();
if (_frames is IAsyncDisposable frames) await frames.DisposeAsync();
_api.Dispose();
try
{
await _stopping.CancelAsync();
await _listening;
}
finally
{
_stopping.Dispose();
if (_frames is IAsyncDisposable frames) await frames.DisposeAsync();
_api.Dispose();
}
}
/// <summary>Follows the server's job events, reconnecting with a growing delay (events lost meanwhile are read back from the job).</summary>
/// <summary>
/// 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.
/// </summary>
private async Task ListenAsync(CancellationToken cancellationToken)
{
var failures = 0;
try
while (true)
{
while (true)
try
{
try
await foreach (var e in _api.EventsAsync(cancellationToken).ConfigureAwait(false))
{
await foreach (var e in _api.EventsAsync(cancellationToken).ConfigureAwait(false))
{
failures = 0;
await OnEventAsync(e, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex) when (ex is not OutOfMemoryException && !cancellationToken.IsCancellationRequested)
{
Log.Info("editor", $"Events of the server lost for {_remote.Sequence.DisplayTitle}: {ex.Message}");
failures = 0;
await OnEventAsync(e, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
if (cancellationToken.IsCancellationRequested) return;
Log.Info("editor", $"Events of the server lost for {_remote.Sequence.DisplayTitle}: {ex.Message}");
}
try
{
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);
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// The editor closed.
catch (Exception ex) when (ex is not OutOfMemoryException)
{
if (cancellationToken.IsCancellationRequested) return;
Log.Info("editor", $"Jobs of {_remote.Sequence.DisplayTitle} could not be read again: {ex.Message}");
}
}
}
@@ -210,6 +251,9 @@ public sealed class RemoteSequenceSource : ISequenceSource
{
switch (e)
{
case SequenceChangedEvent changed when changed.SequenceId == Id && changed.FramesVersion != Volatile.Read(ref _framesVersion):
await CheckFramesAsync(cancellationToken).ConfigureAwait(false);
break;
// A late progress event never brings a finished job back to running.
case JobProgressEvent progress when _jobs.TryGetValue(progress.JobId, out var job) && job.State is JobState.Queued or JobState.Running:
Publish(job with
@@ -242,6 +286,37 @@ public sealed class RemoteSequenceSource : ISequenceSource
}
}
/// <summary>Frames added or removed on the server: whether this PC's frames are still the sequence's.</summary>
private async Task CheckFramesAsync(CancellationToken cancellationToken)
{
try
{
var manifest = await _api.GetManifestAsync(Id, cancellationToken).ConfigureAwait(false);
if (SameFrames(manifest)) Volatile.Write(ref _framesVersion, manifest.FramesVersion);
else MarkOutdated();
}
catch (Exception ex) when (ex is TitanoApiException or HttpRequestException or JsonException
|| (ex is OperationCanceledException && !cancellationToken.IsCancellationRequested))
{
Log.Info("editor", $"The frames of {_remote.Sequence.DisplayTitle} could not be compared: {ex.Message}");
}
}
/// <summary>Sets a priority other than the user's on a job the server just queued; the job stays queued if that fails.</summary>
private async Task<JobDto> PrioritiseAsync(JobDto dto, JobPriority priority, CancellationToken cancellationToken)
{
try
{
return await _api.SetJobPriorityAsync(dto.Id, JobWire.Priority(priority), cancellationToken);
}
catch (Exception ex) when (ex is TitanoApiException or HttpRequestException)
{
// Already claimed (409) or refused: the job runs at the user's priority, which is never later than asked.
Log.Info("editor", $"Job {dto.Id} keeps the user's priority on the server: {ex.Message}");
return dto;
}
}
/// <summary>After a lost event socket: the tracked jobs as the server has them now.</summary>
private async Task RefreshTrackedAsync(CancellationToken cancellationToken)
{
@@ -255,6 +330,24 @@ public sealed class RemoteSequenceSource : ISequenceSource
JobChanged?.Invoke(job);
}
/// <summary>The server's frames are the cached ones, in the same order (frame positions in the project mean the same frames).</summary>
private bool SameFrames(SequenceManifest manifest) =>
manifest.Frames.OrderBy(f => f.Index).Select(f => f.Id).SequenceEqual(_remote.Frames.Select(f => f.Id));
/// <summary>The frames changed on the server: from now on the project is only shown.</summary>
private ProxiesOutdatedException Outdated()
{
MarkOutdated();
return new ProxiesOutdatedException(Loc.L("Editor.Error.FramesChanged"));
}
private void MarkOutdated()
{
if (Interlocked.Exchange(ref _outdated, 1) == 1) return;
Log.Info("editor", $"The frames of {_remote.Sequence.DisplayTitle} changed on the server; its proxies are out of date");
ReadOnlyChanged?.Invoke();
}
private bool Concerns(Job job) => job.TargetIds.Contains(Id) && job.Kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render;
/// <summary>The frame of the project's hero, or <see cref="Guid.Empty"/> (the first frame shows) without one.</summary>
@@ -307,3 +400,9 @@ public sealed class RemoteSequenceSource : ISequenceSource
/// <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);
}
/// <summary>
/// The proxies of a server sequence are for frames the server no longer has (added or removed since they were fetched):
/// they must be fetched again before the sequence can be edited.
/// </summary>
public sealed class ProxiesOutdatedException(string message) : IOException(message);