fix(proxies): use only a frame's own proxy and let tiers, reports and built flags follow it

- Renders, analyses, the server's manifests and proxy downloads take a proxy only
  when its Exif capture time is the frame's (ArchiveImages.IsProxyOf); one left at
  the index by a frame renumbered since counts as missing and is asked for again
  (V-24).
- A frame whose original the host reads through its proxy (HEIF on the server) is
  preview-only only while it has none: the light analysis and the proxy pass keep
  the flag, the preview-only count, the light finding and the advisor's output cap
  in step with the proxies, and an arriving proxy queues a light analysis on the
  server (V-1, V-25).
- The deep checkpoint's content key includes whether each frame is read from its
  original or its proxy, and analysis.json lists the frames measured from proxies;
  an unreadable verdict on a frame now read from its proxy makes the report stale
  (V-26).
- Thumbnails drawn from proxies are compared with the archive's thumbnail size
  instead of 320 px (V-27).
- A resumed thumbnail or proxy pass sets ThumbsBuilt/ProxiesBuilt only when the
  frames an earlier run did still have theirs, and the proxy pass logs frames
  waiting for a client's proxy apart from unreadable ones (V-28).

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 16:07:31 +00:00
1 parent 32a0e9bbc1
commit b4f1fa32d5
22 files changed
+601 -59

No files matched your search

@@ -6,15 +6,18 @@ using Titano.Core.Codecs.Dng;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving;
using Titano.Data.Archiving.Jobs;
using Titano.Data.Processing;
using Titano.Data.Processing.Jobs;
using Titano.Data.Repositories;
using Titano.Data.Tests.Archiving;
using Titano.Testing;
namespace Titano.Data.Tests.Processing;
@@ -115,6 +118,101 @@ public sealed class AnalysisJobTests(TestContext context)
Assert.Equal(written, File.GetLastWriteTimeUtc(folder.ManifestPath));
}
[Test]
public async Task AHeifFrameTheServerReadsFromItsProxyCapsNothing()
{
// The server's decoders (BRIEF §4.2, §8.1): a HEIF gives them only the 160 × 120 JPEG thumbnail of its Exif.
await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Archive = o.Archive with { Decoder = DecoderFactory.CreateServer() } });
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();
// Without proxies the server has nothing better than those thumbnails: preview-only, and the advisor caps the output.
await LightAnalysisAsync(fx, sequence);
var probed = ManifestStore.ReadSequence(folder.ManifestPath).Frames;
Assert.True(probed.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 160, PreviewH: 120 } && f.Flags.HasFlag(FrameFlags.PreviewOnly)),
"preview-only at the thumbnail's size");
Assert.Equal(4, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
var capped = Advisor.Recommend(LightAnalyzer.Analyze(rows), null, SequenceContext.FromFrames(rows));
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'.
Directory.CreateDirectory(folder.Proxies);
foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value), 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");
Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
Assert.True(rows.All(f => !f.Flags.HasFlag(FrameFlags.PreviewOnly)), "index flags");
var advice = Advisor.Recommend(LightAnalyzer.Analyze(rows), null, SequenceContext.FromFrames(rows));
Assert.Equal((AdvisorKeys.ResolutionSource, "4k-hevc"), (advice.Find(AdvisorParameters.Resolution)!.ReasonKey, advice.Find(AdvisorParameters.ExportPreset)!.Value as string));
Assert.False(advice.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "no preview-only finding");
}
[Test]
public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering());
var folder = fx.Folder(sequence);
// Frame 4 is a HEIF this host reads only from its proxy (BRIEF §2.3); the proxy a client sends shows the photo.
var original = Path.Combine(folder.Folder, ProcessingFixture.FileName(3));
var photo = await File.ReadAllBytesAsync(original, fx.Ct);
await File.WriteAllBytesAsync(original, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8], fx.Ct);
var frame = (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[3];
await using (var session = await fx.Db.Database.OpenAsync(fx.Ct))
await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE id = $2", [(int)ImageFileFormat.Heif, frame.Id], fx.Ct);
var sidecar = ManifestStore.ReadSequence(folder.ManifestPath);
ManifestStore.WriteSequence(folder.ManifestPath, sidecar with
{
Frames = [.. sidecar.Frames.Select(f => f.Id == frame.Id ? f with { Format = ImageFileFormat.Heif } : f)],
});
fx.Decoder.HoldFrame(25);
await fx.StartEngineAsync();
// A deep analysis reads the original, finds no pixels, and is interrupted once its checkpoint holds that verdict.
var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await fx.Engine.CancelAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Cancelled);
// The proxy arrives meanwhile: the frame is no longer read as the checkpoint read it, so the resumed run starts over.
Directory.CreateDirectory(folder.Proxies);
await File.WriteAllBytesAsync(folder.Proxy(3), photo, fx.Ct);
fx.Decoder.Hold = null;
await fx.Engine.ResumeAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Done);
var report = SequenceAnalyser.ReadReport(folder, sequence.FrameCount)!;
Assert.Empty(report.Issues.Undecodable);
Assert.SequenceEqual([3], report.ProxyFrames!);
// Without its proxy, a deep analysis lists the frame as unreadable, a sequence warning...
File.Delete(folder.Proxy(3));
var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.WaitForAsync(deep.Id, JobState.Done);
Assert.SequenceEqual([3], SequenceAnalyser.ReadReport(folder, sequence.FrameCount)!.Issues.Undecodable);
var unreadable = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.True(unreadable.Status.HasFlag(SequenceStatus.AnalyzedDeep | SequenceStatus.HasWarnings), unreadable.Status.ToString());
// ...a verdict that no longer holds once the proxy is there: the next light analysis drops the report and its warning.
await File.WriteAllBytesAsync(folder.Proxy(3), photo, fx.Ct);
await LightAnalysisAsync(fx, sequence);
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.False(row.Status.HasFlag(SequenceStatus.AnalyzedDeep), "the report is stale");
Assert.Less(row.WarningCount, unreadable.WarningCount);
}
[Test]
public async Task DeepAnalysisFlagsFramesItCannotDecodeAndListsThemOnce()
{
@@ -386,6 +484,21 @@ public sealed class AnalysisJobTests(TestContext context)
foreach (var text in new[] { null, "6", "2;3", "2;x", "a" }) Assert.Equal(0, AnalyseJobHandler.Parse(text, 5).Next);
}
private static async Task LightAnalysisAsync(ProcessingFixture fx, Sequence sequence)
{
var job = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
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)
{
using var image = new ImageBuffer(320, 240);
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..]] });
}
private static async Task<List<bool>> AnalysedAsync(ProcessingFixture fx, params Sequence[] sequences)
{
var analysed = new List<bool>();