fix(proxies): judge proxies by one rule and record tiers and reports as they stand
- ArchiveImages.IsProxyOf compares capture times to the microsecond the index
keeps (sidecars and Exif keep 100 ns ticks), and for frames read through
proxies also applies the policy's size rule, so the proxy pass, uploads,
thumbnails, renders, analyses, tiers, manifests and downloads agree.
- Proxy headers are cached per file while length and write time are unchanged
(and the file has settled), so manifests cost a stat per proxy.
- FrameTiers.RecordAsync checks proxy presence when it writes the sidecar and
returns the sequence's frames as they are, so a deep analysis or render that
ran while proxies arrived no longer flags the frames preview-only again, and
the analysis advises from the current flags.
- Server manifests ignore ProxiesBuilt for frames the server cannot decode, so
frames added since the last proxy pass are asked for their proxies.
- GET /sequences/{id}/analysis/report answers 404 for a report that found
frames unreadable before their proxies arrived (DeepReports.ReadCurrentAsync).
- The deep checkpoint key marks only frames read from proxies, so checkpoints
written by earlier versions still resume.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
16 files changed
+514
-99
No files matched your search
@@ -1,3 +1,5 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Titano.Core.Advice;
|
||||
using Titano.Core.Analysis;
|
||||
using Titano.Core.Analysis.Deep;
|
||||
@@ -5,6 +7,7 @@ using Titano.Core.Archiving;
|
||||
using Titano.Core.Codecs.Dng;
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Diagnostics.Synthetic;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Import;
|
||||
@@ -145,10 +148,10 @@ public sealed class AnalysisJobTests(TestContext context)
|
||||
Assert.Equal((AdvisorKeys.ResolutionPreview, "720p-preview"), (capped.Find(AdvisorParameters.Resolution)!.ReasonKey, capped.Find(AdvisorParameters.ExportPreset)!.Value as string));
|
||||
Assert.True(capped.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "preview-only finding");
|
||||
|
||||
// A client sends their proxies (BRIEF §2.3): the server renders the frames from them, so none is preview-only and
|
||||
// the advice follows. The tier and preview size stay the originals'.
|
||||
// A client sends their proxies (BRIEF §2.3) at the policy's 1920 px: the server renders the frames from them, so none
|
||||
// is preview-only and the advice follows. The tier and preview size stay the originals'.
|
||||
Directory.CreateDirectory(folder.Proxies);
|
||||
foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value), fx.Ct);
|
||||
foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value, 1920, 1440), fx.Ct);
|
||||
await LightAnalysisAsync(fx, sequence);
|
||||
var proxied = ManifestStore.ReadSequence(folder.ManifestPath).Frames;
|
||||
Assert.True(proxied.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 160 } && !f.Flags.HasFlag(FrameFlags.PreviewOnly)), "not preview-only");
|
||||
@@ -160,6 +163,49 @@ public sealed class AnalysisJobTests(TestContext context)
|
||||
Assert.False(advice.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "no preview-only finding");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProxiesThatArriveDuringADeepAnalysisStillCount()
|
||||
{
|
||||
// A server, whose advisor rules no codec out: it reads these HEIF files only through their 160 × 120 Exif
|
||||
// thumbnails until proxies come (BRIEF §2.3).
|
||||
await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Host = EncoderHost.Server });
|
||||
var directory = Path.Combine(fx.Archive.Paths.Root, "Heif");
|
||||
Directory.CreateDirectory(directory);
|
||||
var start = new DateTime(2026, 9, 28, 18, 0, 0);
|
||||
for (var i = 0; i < 4; i++)
|
||||
await File.WriteAllBytesAsync(Path.Combine(directory, $"IMG_{i + 1:0000}.HEIC"), HeifFile.Create(start.AddSeconds(10 * i), 4032, 3024), fx.Ct);
|
||||
await fx.Archive.ScanAsync(ScanMode.Full);
|
||||
var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Archive.Id, fx.Ct)).Single(l => l.Folder == "Heif");
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(location.Id, fx.Ct))!;
|
||||
var folder = fx.Folder(sequence);
|
||||
await fx.StartEngineAsync(workers: 2);
|
||||
async Task<string?> PresetAsync() => ProjectSettings.FromJson(await File.ReadAllTextAsync(folder.ProjectPath, fx.Ct)).Export.Preset.AutoValue;
|
||||
await LightAnalysisAsync(fx, sequence);
|
||||
Assert.Equal(4, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
|
||||
Assert.Equal("720p-preview", await PresetAsync());
|
||||
|
||||
// A deep analysis starts on the originals; while it runs, the proxies arrive and a light analysis on the other
|
||||
// worker counts them.
|
||||
fx.Decoder.Hold = "IMG_0001.HEIC";
|
||||
var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
|
||||
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
|
||||
Directory.CreateDirectory(folder.Proxies);
|
||||
foreach (var frame in await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))
|
||||
await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value, 1920, 1440), fx.Ct);
|
||||
await LightAnalysisAsync(fx, sequence);
|
||||
Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
|
||||
Assert.Equal("4k-hevc", await PresetAsync());
|
||||
fx.Decoder.Release();
|
||||
await fx.WaitForAsync(deep.Id, JobState.Done);
|
||||
|
||||
// The deep pass decoded the originals from their thumbnails, yet the frames are read from their proxies by now:
|
||||
// it flags none preview-only again, and its advice caps nothing.
|
||||
Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Frames.All(f => f.Tier == DecodeTier.EmbeddedPreview && !f.Flags.HasFlag(FrameFlags.PreviewOnly)),
|
||||
"not preview-only");
|
||||
Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
|
||||
Assert.Equal("4k-hevc", await PresetAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives()
|
||||
{
|
||||
@@ -213,6 +259,25 @@ public sealed class AnalysisJobTests(TestContext context)
|
||||
Assert.Less(row.WarningCount, unreadable.WarningCount);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACheckpointOfASequenceReadFromItsOriginalsKeepsTheKeyEarlierVersionsWrote()
|
||||
{
|
||||
await using var fx = await ProcessingFixture.CreateAsync(context);
|
||||
var sequence = await fx.AddAsync(Flickering(4));
|
||||
var frames = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive.Archive, sequence, useProxies: false, new ImageDecoder([fx.Decoder]), fx.Ct);
|
||||
Assert.SequenceEqual([false, false, false, false], frames.Proxied!);
|
||||
|
||||
// A deep analysis interrupted before an upgrade resumes after it (a server restarted mid-run does not start over).
|
||||
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
foreach (var f in frames.Frames)
|
||||
hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & Titano.Data.Processing.SequenceFrames.NotAnalysed};"));
|
||||
var earlier = Convert.ToHexStringLower(hash.GetHashAndReset())[..16];
|
||||
Assert.Equal(earlier, SequenceAnalyser.ContentKey(frames));
|
||||
|
||||
// A frame read from its proxy since makes the checkpoint stale.
|
||||
Assert.NotEqual(earlier, SequenceAnalyser.ContentKey(frames with { Proxied = [false, true, false, false] }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeepAnalysisFlagsFramesItCannotDecodeAndListsThemOnce()
|
||||
{
|
||||
@@ -490,10 +555,10 @@ public sealed class AnalysisJobTests(TestContext context)
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
}
|
||||
|
||||
/// <summary>A 320 × 240 JPEG with <paramref name="captured"/> in its Exif, as a client makes a proxy.</summary>
|
||||
private static byte[] Proxy(DateTime captured)
|
||||
/// <summary>A JPEG (320 × 240 unless sized) with <paramref name="captured"/> in its Exif, as a client makes a proxy.</summary>
|
||||
private static byte[] Proxy(DateTime captured, int width = 320, int height = 240)
|
||||
{
|
||||
using var image = new ImageBuffer(320, 240);
|
||||
using var image = new ImageBuffer(width, height);
|
||||
for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.3f + c * 0.1f);
|
||||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" });
|
||||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
|
||||
|
||||
@@ -95,7 +95,7 @@ internal sealed class ProcessingFixture : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Managed decoding that records every request and can hold one file's decode until the job is cancelled.</summary>
|
||||
/// <summary>Managed decoding that records every request and can hold one file's decode until the job is cancelled or the decode is released.</summary>
|
||||
internal sealed class GateDecoder : IImageDecoderProvider
|
||||
{
|
||||
private readonly ManagedDecoderProvider _managed = new();
|
||||
@@ -116,13 +116,19 @@ internal sealed class GateDecoder : IImageDecoderProvider
|
||||
/// <summary>Completed when a decode of <see cref="Hold"/> started waiting.</summary>
|
||||
public TaskCompletionSource Holding { get; private set; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
private TaskCompletionSource _released = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
public void HoldFrame(int index, DecodePurpose? purpose = null, int? edge = null)
|
||||
{
|
||||
Holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
_released = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
(HoldPurpose, HoldEdge) = (purpose, edge);
|
||||
Hold = ProcessingFixture.FileName(index);
|
||||
}
|
||||
|
||||
/// <summary>Lets the held decode go on, as a slow read would.</summary>
|
||||
public void Release() => _released.TrySetResult();
|
||||
|
||||
/// <summary>Tiers <see cref="Probe(string, ImageFileFormat)"/> reports per file name instead of the managed provider's (a
|
||||
/// host that reads them only as previews, or not at all).</summary>
|
||||
public ConcurrentDictionary<string, FileTier> Tiers { get; } = new();
|
||||
@@ -152,7 +158,13 @@ internal sealed class GateDecoder : IImageDecoderProvider
|
||||
&& (HoldEdge ?? request.MaxLongEdge) == request.MaxLongEdge)
|
||||
{
|
||||
Holding.TrySetResult();
|
||||
cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));
|
||||
try
|
||||
{
|
||||
_released.Task.Wait(TimeSpan.FromSeconds(30), cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
if (Unreadable.ContainsKey(name)) throw new IOException($"The process cannot access the file '{request.Path}' because it is being used by another process.");
|
||||
|
||||
Reference in new issue
Block a user