fix(analysis): record nothing for frames that changed while an analysis ran

A light or deep analysis read its frames at the start and, under the
sequence lock, recorded AnalyzedLight, the warning count and the advisor's
Auto values for them whatever happened meanwhile. Frames added by an upload,
an import or a scan's reconcile then kept advice and warnings counted on the
old frame count, over a newer light analysis, and scans never queued another
one because AnalyzedLight was set again.

Under the lock the analysis now compares the row's frames version with the
one it read. When it moved and no light analysis is queued for the
sequence, it records nothing (a deep pass keeps only analysis.json and its
per-frame measurements), and the job logs it and queues a light analysis of
the frames as they are. A queued light analysis replaces what the pass
records, as for image passes.

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 21:37:00 +00:00
1 parent bb2f40c29f
commit d064719c11
7 files changed
+143 -7

No files matched your search

@@ -254,6 +254,78 @@ public sealed class AnalysisJobTests(TestContext context)
Assert.Equal(advice, await AutoValuesAsync());
}
[Test]
public async Task FramesAddedDuringADeepAnalysisKeepTheLightAnalysisOfTheFramesAsTheyAre()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering(12));
var folder = fx.Folder(sequence);
await fx.StartEngineAsync(workers: 2);
// A deep analysis starts on twelve frames; while it runs, a thirteenth photo this host cannot decode is indexed,
// and a light analysis on the other worker analyses all thirteen.
fx.Decoder.HoldFrame(0);
var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
fx.Decoder.Tiers[ProcessingFixture.FileName(12)] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.Unavailable, null, "test", 0, 0);
await fx.AddAsync(Flickering(13), first: 12);
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.WaitForAsync(light.Id, JobState.Done);
var analysed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
var advice = await ProjectJsonAsync(fx, folder);
Assert.Equal(13, analysed.FrameCount);
Assert.Contains("13", ProjectSettings.FromJson(advice).Timing.FrameRate.ReasonArgs!);
Assert.True(analysed.Status.HasFlag(SequenceStatus.HasWarnings), "the photo this host cannot decode is a warning");
fx.Decoder.Release();
var done = await fx.WaitForAsync(deep.Id, JobState.Done);
// The deep pass measured twelve of them: it saved its report but applied none of it (neither the Auto values nor
// the status and warnings of twelve frames), and queued a light analysis of the frames as they are.
Assert.Contains(Loc.L("Advisor.Log.FramesChanged", sequence.DisplayTitle), done.LogTail);
Assert.Equal(12, SequenceAnalyser.ReadReport(folder, 12)!.FrameCount);
var again = Assert.Contains(await LightJobsAsync(fx), j => j.Id != light.Id);
await fx.WaitForAsync(again.Id, JobState.Done);
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal((analysed.WarningCount, analysed.Status), (row.WarningCount, row.Status));
Assert.Null(row.AnalysisJson);
Assert.Equal(advice, await ProjectJsonAsync(fx, folder));
}
[Test]
public async Task ALightAnalysisOvertakenByAScanMarksNothingAnalysedAndRunsAgain()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering(12));
var folder = fx.Folder(sequence);
await fx.StartEngineAsync();
// A light analysis reads twelve frames; before it records, a scan indexes a thirteenth photo copied into the folder.
fx.Decoder.HoldProbeOf(0);
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await fx.AddAsync(Flickering(13), first: 12);
fx.Decoder.Release();
// The light analysis that follows waits on the new photo's probe, so what the first one left can be seen.
fx.Decoder.HoldProbeOf(12);
var done = await fx.WaitForAsync(light.Id, JobState.Done);
Assert.Contains(Loc.L("Advisor.Log.FramesChanged", sequence.DisplayTitle), done.LogTail);
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal(13, row.FrameCount);
Assert.False(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status");
Assert.False(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status");
Assert.False(ProjectSettings.FromJson(await ProjectJsonAsync(fx, folder)).Timing.FrameRate.HasAdvice, "no advice for twelve frames");
var again = Assert.Contains(await LightJobsAsync(fx), j => j.Id != light.Id);
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
fx.Decoder.Release();
await fx.WaitForAsync(again.Id, JobState.Done);
row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status");
Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status");
Assert.Contains("13", ProjectSettings.FromJson(await ProjectJsonAsync(fx, folder)).Timing.FrameRate.ReasonArgs!);
}
[Test]
public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives()
{
@@ -612,6 +684,13 @@ public sealed class AnalysisJobTests(TestContext context)
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
}
private static async Task<IReadOnlyList<Job>> LightJobsAsync(ProcessingFixture fx) =>
(await fx.Db.Jobs.ListAsync(null, JobKind.LightAnalyse, 100, null, fx.Ct)).Items;
/// <summary>The project as <c>project.json</c> holds it (the defaults before any analysis wrote it).</summary>
private static async Task<string> ProjectJsonAsync(ProcessingFixture fx, SequencePaths folder) =>
(File.Exists(folder.ProjectPath) ? ProjectSettings.FromJson(await File.ReadAllTextAsync(folder.ProjectPath, fx.Ct)) : new ProjectSettings()).ToJson();
private static async Task<List<bool>> AnalysedAsync(ProcessingFixture fx, params Sequence[] sequences)
{
var analysed = new List<bool>();