fix(analysis): record nothing over photos another Titano changed in a shared sidecar

An analysis compared only this host's index row with the frames version it
read at its start. On a desktop sharing a server's archive, the server's
upload bumps only the shared sidecar until the desktop's next scan, and a
desktop scan clears nothing. A desktop pass that recorded in that window
wrote Auto values and statuses for the old frame count over the server's
analysis in the shared project.json and sidecar, and nothing analysed the
photos again.

Under the sequence lock the analyser now reads the sidecar first. When its
frames version is not the row's, the photos changed in another process and
this index does not hold them yet: the pass records nothing, logs
Advisor.Log.FramesChangedElsewhere and queues nothing, since a light
analysis here would read the same index. The process that changed them
analyses them, and this host's next scan indexes them (a server's scan
then queues their analysis). A moved row still queues a light analysis
unless one is queued, in which case the pass records as before.

Tests cover the shared-archive case with a second index and engine, and
the queued-light escape on a one-worker engine.

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 22:08:20 +00:00
1 parent 098e5b27f6
commit 79125b547a
5 files changed
+158 -26

No files matched your search

@@ -17,6 +17,7 @@ using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving;
using Titano.Data.Archiving.Jobs;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Data.Processing.Jobs;
using Titano.Data.Repositories;
@@ -291,6 +292,42 @@ public sealed class AnalysisJobTests(TestContext context)
Assert.Equal(advice, await ProjectJsonAsync(fx, folder));
}
[Test]
public async Task AnAnalysisOvertakenWhileALightAnalysisWaitsRecordsAndThatOneReplacesIt()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering(12));
var folder = fx.Folder(sequence);
await fx.StartEngineAsync();
// A deep analysis starts on twelve frames on the only worker; while it runs a thirteenth photo is indexed, and its
// light analysis waits in the queue, as during an upload burst.
fx.Decoder.HoldFrame(0);
var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await fx.AddAsync(Flickering(13), first: 12);
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
fx.Decoder.Hold = null;
fx.Decoder.Release();
// The light analysis waits on the new photo's probe, so what the deep pass left can be seen.
fx.Decoder.HoldProbeOf(12);
var done = await fx.WaitForAsync(deep.Id, JobState.Done);
// The deep pass records the analysis of the frames it read meanwhile, and queues nothing more.
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Contains(Loc.L("Advisor.Log.DeepDone", sequence.DisplayTitle, 12, row.WarningCount), done.LogTail);
Assert.False(done.LogTail!.Contains(Loc.L("Advisor.Log.FramesChanged", sequence.DisplayTitle), StringComparison.Ordinal), done.LogTail);
Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status");
Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status");
Assert.SequenceEqual([light.Id], (await LightJobsAsync(fx)).Select(j => j.Id));
// The light analysis that waited replaces it with the analysis of all thirteen.
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
fx.Decoder.Release();
await fx.WaitForAsync(light.Id, JobState.Done);
Assert.Contains("13", ProjectSettings.FromJson(await ProjectJsonAsync(fx, folder)).Timing.FrameRate.ReasonArgs!);
}
[Test]
public async Task ALightAnalysisOvertakenByAScanMarksNothingAnalysedAndRunsAgain()
{
@@ -326,6 +363,64 @@ public sealed class AnalysisJobTests(TestContext context)
Assert.Contains("13", ProjectSettings.FromJson(await ProjectJsonAsync(fx, folder)).Timing.FrameRate.ReasonArgs!);
}
[Test]
public async Task AnAnalysisOfPhotosAnotherTitanoChangedLeavesWhatThatTitanoRecorded()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering(12));
var folder = fx.Folder(sequence);
await fx.StartEngineAsync();
// A server sharing this desktop's archive, with its own index and analysis.
await using var serverDb = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("server"), "titano.db")), fx.Ct);
var serverOptions = fx.Processing.Options with
{
Archive = new ArchiveServiceOptions { Decoder = new ImageDecoder([new ManagedDecoderProvider()]), Owner = "server", LeaseWait = TimeSpan.FromSeconds(20) },
};
var server = new ArchiveService(serverDb, serverOptions.Archive);
var archive = await server.OpenAsync(fx.Archive.Archive.RootPath, fx.Ct);
await server.Indexer.ScanAsync(archive, ScanMode.Incremental, null, _ => ValueTask.CompletedTask, fx.Ct);
await using var serverEngine = new JobEngine(serverDb, new ProcessingService(serverDb, serverOptions).CreateJobHandlers(),
new JobEngineOptions { Workers = 1, WorkerId = "server", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await serverEngine.StartAsync(fx.Ct);
// A light analysis here reads twelve frames; before it records, the server takes a thirteenth photo and analyses
// the sequence as it is now, in the shared sidecar and project.
fx.Decoder.HoldProbeOf(0);
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
var staged = fx.Archive.Stage(new DateTime(2024, 6, 21, 19, 2, 0), seed: 90);
await server.Ingest.CommitAsync(new IngestRequest(archive.Id, sequence.Id, null, null, staged.Path, "IMG_0013.JPG", staged.Size, staged.Sha256, null, null),
fx.Ct);
var analysed = await serverEngine.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]), cancellationToken: fx.Ct);
for (var until = DateTime.UtcNow.AddSeconds(60); (await serverDb.Jobs.GetAsync(analysed.Id, fx.Ct))!.State != JobState.Done;)
{
Assert.True(DateTime.UtcNow < until, "the server's light analysis ends");
await Task.Delay(20, fx.Ct);
}
var advice = await ProjectJsonAsync(fx, folder);
Assert.Contains("13", ProjectSettings.FromJson(advice).Timing.FrameRate.ReasonArgs!);
var status = ManifestStore.ReadSequence(folder.ManifestPath).Status;
Assert.True(status.HasFlag(SequenceStatus.AnalyzedLight), "the server's status");
fx.Decoder.Release();
var done = await fx.WaitForAsync(light.Id, JobState.Done);
// This index still holds twelve frames: the analysis here records nothing over the server's, and queues nothing.
Assert.Contains(Loc.L("Advisor.Log.FramesChangedElsewhere", sequence.DisplayTitle), done.LogTail);
Assert.Equal(advice, await ProjectJsonAsync(fx, folder));
Assert.Equal(status, ManifestStore.ReadSequence(folder.ManifestPath).Status);
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal(12, row.FrameCount);
Assert.False(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status");
Assert.SequenceEqual([light.Id], (await LightJobsAsync(fx)).Select(j => j.Id));
// The next scan here indexes the photo with the server's analysis, which a desktop keeps.
Assert.Contains(sequence.Id, (await fx.Archive.ScanAsync()).Sequences);
row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
Assert.Equal(13, row.FrameCount);
Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status");
Assert.Equal(advice, await ProjectJsonAsync(fx, folder));
}
[Test]
public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives()
{