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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 19:14:13 +00:00
1 parent 678a827b42
commit 99a0f37973
4 files changed
+74 -3

No files matched your search

+38 -2
View File
@@ -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<Guid, Guid> _proxyJobs = [];
private readonly Dictionary<Guid, (int Failures, DateTime NotBefore)> _proxyFailures = [];
/// <summary>The server's update time at which a cache holding thumbnails in place of proxies was last compared with its manifest.</summary>
private readonly Dictionary<Guid, DateTimeOffset> _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
/// <summary>
/// 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 (<see cref="OffersProxiesAsync"/>). A sequence whose last
/// download failed waits for its backoff.
/// </summary>
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;
}
}
/// <summary>
/// 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).
/// </summary>
private async Task<bool> 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);
}
/// <summary>Waits for a wake-up or the delay; false when stopping.</summary>
private static async Task<bool> WaitAsync(ChannelReader<bool> wake, TimeSpan delay, CancellationToken cancellationToken)
{
+1 -1
View File
@@ -710,7 +710,7 @@ public sealed class SyncService
private static DateTime Second(DateTime time) => new(time.Ticks - time.Ticks % TimeSpan.TicksPerSecond, DateTimeKind.Unspecified);
/// <summary>The same instant as databases keep it (to the microsecond).</summary>
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;
/// <summary>
/// Writes the server's payload into the local index, project and sidecar in one step (settings version + 1) under the