fix(sync): tell missed project changes only once the event stream is open again

The agent told "changes may have been missed" as soon as the stream dropped,
so a workspace re-read before the server's events could reach it again and
missed changes made in between. The signal now waits for the reconnected
stream and the refresh that follows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-04 12:31:51 +01:00
1 parent 353024fc71
commit ed7452e739
4 files changed
+102 -9

No files matched your search

+26 -5
View File
@@ -60,8 +60,16 @@ public sealed class SyncAgent : IAsyncDisposable
private readonly SemaphoreSlim _refreshing = new(1, 1);
private Task _running = Task.CompletedTask;
/// <summary>1 while project changes may have been missed (the event stream was lost), until a comparison reached the server.</summary>
/// <summary>
/// Whether project changes may have been missed: <see cref="Missed"/> from the moment the event stream was lost,
/// <see cref="Reopened"/> once it is open again (no change is missed from then on), back to <see cref="InStep"/> when a
/// comparison that started after that reached the server and <see cref="ProjectChanged"/> said so.
/// </summary>
private int _projectsMissed;
private const int InStep = 0;
private const int Missed = 1;
private const int Reopened = 2;
private SyncSnapshot? _snapshot;
private Exception? _lastError;
@@ -114,13 +122,16 @@ public sealed class SyncAgent : IAsyncDisposable
await _refreshing.WaitAsync(linked.Token).ConfigureAwait(false);
try
{
// Only a comparison that started once the stream was open again covers every change missed meanwhile: one that
// started before may end while the stream is still down, and a change said then would be missed for good.
var reopened = Volatile.Read(ref _projectsMissed) == Reopened;
var policy = _policy();
var snapshot = await _sync.RefreshAsync(_remote, policy, linked.Token).ConfigureAwait(false);
Volatile.Write(ref _snapshot, snapshot);
Volatile.Write(ref _lastError, null);
if (policy.KeepProxiesOffline && _engine is not null) await QueueProxiesAsync(snapshot, linked.Token).ConfigureAwait(false);
Changed?.Invoke();
if (Interlocked.Exchange(ref _projectsMissed, 0) == 1) TellProjects(null);
if (reopened && Interlocked.CompareExchange(ref _projectsMissed, InStep, Reopened) == Reopened) TellProjects(null);
return snapshot;
}
finally
@@ -180,7 +191,7 @@ public sealed class SyncAgent : IAsyncDisposable
{
try
{
await foreach (var e in _remote.EventsAsync(cancellationToken).ConfigureAwait(false))
await foreach (var e in _remote.EventsAsync(Reconnected, cancellationToken).ConfigureAwait(false))
{
failures = 0;
if (e is ProjectChangedEvent project)
@@ -201,8 +212,9 @@ public sealed class SyncAgent : IAsyncDisposable
{
Log.Info(LogCategory, $"Event stream of {Server} lost: {ex.Message}");
}
// Project changes said meanwhile are missed: the next comparison that reaches the server says so.
Volatile.Write(ref _projectsMissed, 1);
// Project changes said meanwhile are missed: the first comparison that reaches the server once the stream is open
// again says so (Reconnected).
Volatile.Write(ref _projectsMissed, Missed);
try
{
await Task.Delay(Backoff(failures++), cancellationToken).ConfigureAwait(false);
@@ -215,6 +227,15 @@ public sealed class SyncAgent : IAsyncDisposable
}
}
/// <summary>
/// The event stream is open (again): after a loss, a comparison is asked for now, and the one that starts from here says
/// that project changes may have been missed (<see cref="ProjectChanged"/> with null).
/// </summary>
private void Reconnected()
{
if (Interlocked.CompareExchange(ref _projectsMissed, Reopened, Missed) == Missed) RequestRefresh();
}
/// <summary>Tells <see cref="ProjectChanged"/>; what a listener throws is logged, never ends the event loop.</summary>
private void TellProjects(ProjectChangedEvent? change)
{
+14
View File
@@ -64,6 +64,18 @@ public interface ISyncRemote : IDisposable
/// <summary>Server events until cancelled or the socket closes.</summary>
IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken);
/// <summary>
/// <see cref="EventsAsync(CancellationToken)"/>, calling <paramref name="connected"/> once the stream is open, before its
/// first event: from then on no event the server sends is missed. A remote that cannot say calls it when the stream is
/// asked for.
/// </summary>
IAsyncEnumerable<ServerEvent> EventsAsync(Action connected, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(connected);
connected();
return EventsAsync(cancellationToken);
}
/// <summary>
/// Every frame of a sequence as of one frames version, with the photo aliases of its conversions (owner review 2, R8:
/// what a workspace copy of a server project holds; servers with the <c>projects</c> capability). A remote without the
@@ -148,6 +160,8 @@ public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient) : I
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
public IAsyncEnumerable<ServerEvent> EventsAsync(Action connected, CancellationToken cancellationToken) => client.EventsAsync(connected, cancellationToken);
public Task<ProjectFramesDto> GetProjectFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => client.GetProjectFramesAsync(sequenceId, cancellationToken);
public Task<string?> GetAnalysisReportAsync(Guid sequenceId, CancellationToken cancellationToken) => client.GetAnalysisReportAsync(sequenceId, cancellationToken);