From f7a1c158f4d7e04c907ef9a29223f097ebfe7b52 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 23:16:23 +0000 Subject: [PATCH] fix(analysis): follow-ups skip queued sequences; photos added by a scan are analysed again Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Archiving/ArchiveIndexer.cs | 6 +- src/Titano.Data/Archiving/ArchiveService.cs | 2 +- .../Archiving/Jobs/ImportJobHandler.cs | 4 +- .../Archiving/Jobs/ScanJobHandler.cs | 5 +- .../Processing/AnalysisFollowUp.cs | 13 +++-- .../Processing/AnalysisFollowUpTests.cs | 55 +++++++++++++++++++ 6 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/Titano.Data/Archiving/ArchiveIndexer.cs b/src/Titano.Data/Archiving/ArchiveIndexer.cs index 84cc6266..ff13cb34 100644 --- a/src/Titano.Data/Archiving/ArchiveIndexer.cs +++ b/src/Titano.Data/Archiving/ArchiveIndexer.cs @@ -160,7 +160,11 @@ public sealed class ArchiveIndexer if (reconcile || File.Exists(sequence.IngestJournalPath)) { var before = manifest; - manifest = await _writer.UpdateAsync(paths, manifest.Id, folder.RelativeFolder, m => SequenceAdopter.Reconcile(sequence, m, cancellationToken), + // Photos added, gone or back: the light analysis no longer describes the frames, so the scan's follow-up runs it again. + manifest = await _writer.UpdateAsync(paths, manifest.Id, folder.RelativeFolder, + m => SequenceAdopter.Reconcile(sequence, m, cancellationToken) is { } reconciled + ? reconciled with { Status = reconciled.Status & ~SequenceStatus.AnalyzedLight } + : null, cancellationToken).ConfigureAwait(false); if (!ReferenceEquals(before, manifest)) bytes = null; } diff --git a/src/Titano.Data/Archiving/ArchiveService.cs b/src/Titano.Data/Archiving/ArchiveService.cs index 96a62969..24c8a4d9 100644 --- a/src/Titano.Data/Archiving/ArchiveService.cs +++ b/src/Titano.Data/Archiving/ArchiveService.cs @@ -48,7 +48,7 @@ public sealed class ArchiveService : IJobHandlerProvider new ScanJobHandler(_db, Indexer, JobKind.Scan), new ScanJobHandler(_db, Indexer, JobKind.Rebuild), new VerifyJobHandler(_db, Verifier), - new ImportJobHandler(Imports), + new ImportJobHandler(_db, Imports), new ImageJobHandler(_db, Images, JobKind.Thumbs), new ImageJobHandler(_db, Images, JobKind.Proxies), new MaintenanceJobHandler(_db, Maintenance), diff --git a/src/Titano.Data/Archiving/Jobs/ImportJobHandler.cs b/src/Titano.Data/Archiving/Jobs/ImportJobHandler.cs index 3ad7cbec..bdd6ac58 100644 --- a/src/Titano.Data/Archiving/Jobs/ImportJobHandler.cs +++ b/src/Titano.Data/Archiving/Jobs/ImportJobHandler.cs @@ -13,7 +13,7 @@ namespace Titano.Data.Archiving.Jobs; /// number of plan items done. A removed card fails the job at a batch boundary; resuming it after re-insertion /// continues there. The sequences the import went into are then queued for light analysis. /// -internal sealed class ImportJobHandler(ImportService imports) : IJobHandler +internal sealed class ImportJobHandler(TitanoDatabase db, ImportService imports) : IJobHandler { public JobKind Kind => JobKind.Import; @@ -30,6 +30,6 @@ internal sealed class ImportJobHandler(ImportService imports) : IJobHandler .ConfigureAwait(false); }, p => context.ReportAsync(p.BytesDone, p.BytesTotal, p.Current), context.Cancellation).ConfigureAwait(false); context.Log(Loc.L("Archive.Log.ImportDone", final.Copied, final.Skipped, final.Failed)); - await AnalysisFollowUp.QueueAsync(context, await imports.SequencesOfAsync(parameters, context.Cancellation).ConfigureAwait(false)).ConfigureAwait(false); + await AnalysisFollowUp.QueueAsync(db, context, await imports.SequencesOfAsync(parameters, context.Cancellation).ConfigureAwait(false)).ConfigureAwait(false); } } diff --git a/src/Titano.Data/Archiving/Jobs/ScanJobHandler.cs b/src/Titano.Data/Archiving/Jobs/ScanJobHandler.cs index fc111423..7935157f 100644 --- a/src/Titano.Data/Archiving/Jobs/ScanJobHandler.cs +++ b/src/Titano.Data/Archiving/Jobs/ScanJobHandler.cs @@ -17,7 +17,8 @@ internal static class JobArchive /// /// (incremental, or full with ) and /// . Checkpoint: after:<folder>, the last folder indexed. When anything was -/// indexed, the sequences never light-analysed (adopted folders, new sidecars) are queued for it. +/// indexed, the sequences without a current light analysis (adopted folders, new sidecars, photos added to a folder) are +/// queued for it. /// internal sealed class ScanJobHandler(TitanoDatabase db, ArchiveIndexer indexer, JobKind kind) : IJobHandler { @@ -41,7 +42,7 @@ internal sealed class ScanJobHandler(TitanoDatabase db, ArchiveIndexer indexer, context.Log(Loc.L("Archive.Log.ScanDone", summary.Folders, summary.Indexed, summary.Adopted, summary.Removed)); // A resumed scan may have indexed its folders before the interruption. if (summary.Indexed > 0 || resume is not null) - await AnalysisFollowUp.QueueAsync(context, await AnalysisFollowUp.UnanalysedAsync(db, archive.Id, context.Cancellation).ConfigureAwait(false)) + await AnalysisFollowUp.QueueAsync(db, context, await AnalysisFollowUp.UnanalysedAsync(db, archive.Id, context.Cancellation).ConfigureAwait(false)) .ConfigureAwait(false); } } diff --git a/src/Titano.Data/Processing/AnalysisFollowUp.cs b/src/Titano.Data/Processing/AnalysisFollowUp.cs index dba09d89..7af05474 100644 --- a/src/Titano.Data/Processing/AnalysisFollowUp.cs +++ b/src/Titano.Data/Processing/AnalysisFollowUp.cs @@ -7,19 +7,24 @@ namespace Titano.Data.Processing; /// /// Light analysis queued after work that brings sequences or frames into the index (card import, adoption, scan), so the /// advisor's Auto values and recommended preset exist without any user action (BRIEF §1.1, §4.3). Nothing is queued -/// when the engine cannot run light analysis. +/// when the engine cannot run light analysis, and a sequence already waiting in a queued light analysis is not queued +/// again (that job runs after the changes that led here). /// internal static class AnalysisFollowUp { /// Queues one light analysis of (labelled like the job that found them). - public static async Task QueueAsync(JobContext context, IReadOnlyCollection sequenceIds) + public static async Task QueueAsync(TitanoDatabase db, JobContext context, IReadOnlyCollection sequenceIds) { if (sequenceIds.Count == 0) return; - var request = ProcessingJobs.LightAnalysis([.. sequenceIds], context.Job.TargetLabel); + var waiting = (await db.Jobs.ListQueuedAsync([JobKind.LightAnalyse], 1000, context.Cancellation).ConfigureAwait(false)) + .SelectMany(j => j.TargetIds).ToHashSet(); + var targets = sequenceIds.Where(id => !waiting.Contains(id)).Distinct().ToList(); + if (targets.Count == 0) return; + var request = ProcessingJobs.LightAnalysis(targets, context.Job.TargetLabel); await context.EnqueueFollowUpAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, request.Priority).ConfigureAwait(false); } - /// Sequences of that were never light-analysed (new or adopted ones). + /// Sequences of without a current light analysis (new, adopted, or with frames a scan added). public static async Task> UnanalysedAsync(TitanoDatabase db, Guid archiveId, CancellationToken cancellationToken) { var ids = new List(); diff --git a/tests/Titano.Data.Tests/Processing/AnalysisFollowUpTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisFollowUpTests.cs index 0f9cad44..0484c71d 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisFollowUpTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisFollowUpTests.cs @@ -60,6 +60,61 @@ public sealed class AnalysisFollowUpTests(TestContext context) Assert.True((await fx.Db.Sequences.GetAsync(sequenceId.Value, fx.Ct))!.Status.HasFlag(SequenceStatus.AnalyzedLight), "analysed"); } + [Test] + public async Task PhotosAddedToAnAnalysedFolderAreAnalysedAgain() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 12 }), "Roof"); + await fx.StartEngineAsync(withArchive: true); + var first = await fx.EnqueueAsync(Titano.Data.Processing.ProcessingJobs.LightAnalysis([sequence.Id])); + await fx.WaitForAsync(first.Id, JobState.Done); + + // The camera kept shooting into the folder: the next scan indexes the new photos and analyses the sequence again. + fx.Write(SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 18 }), "Roof", first: 12); + var scan = await fx.EnqueueAsync(ArchiveJobs.Scan(fx.Archive.Archive)); + await fx.WaitForAsync(scan.Id, JobState.Done); + var again = (await fx.Db.Jobs.ListAsync(null, JobKind.LightAnalyse, cancellationToken: fx.Ct)).Items.Single(j => j.Id != first.Id); + await fx.WaitForAsync(again.Id, JobState.Done); + + var analysed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.Equal(18, analysed.FrameCount); + Assert.True(analysed.Status.HasFlag(SequenceStatus.AnalyzedLight), "analysed again"); + Assert.True(ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status"); + } + + [Test] + public async Task ASequenceWaitingForLightAnalysisIsNotQueuedAgain() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 12 }), "Roof"); + // Queued before the scan, which runs first (user priority) and finds the sequence not analysed yet. + var waiting = await QueueAsync(fx, Titano.Data.Processing.ProcessingJobs.LightAnalysis([sequence.Id])); + var scan = await QueueAsync(fx, ArchiveJobs.Scan(fx.Archive.Archive, full: true)); + await fx.StartEngineAsync(withArchive: true); + + await fx.WaitForAsync(scan.Id, JobState.Done); + await fx.WaitForAsync(waiting.Id, JobState.Done); + Assert.Equal(waiting.Id, (await SingleLightJobAsync(fx))?.Id); + } + + /// Queues a job before the engine starts. + private static async Task QueueAsync(ProcessingFixture fx, Titano.Data.Archiving.Jobs.ArchiveJobRequest request) + { + var job = new Job + { + Id = Guid.CreateVersion7(), + Kind = request.Kind, + TargetIds = request.Targets, + TargetLabel = request.Label, + ParametersJson = request.ParametersJson, + Priority = request.Priority, + State = JobState.Queued, + Created = DateTimeOffset.UtcNow, + }; + await fx.Db.Jobs.EnqueueAsync(job, fx.Ct); + return job; + } + private static async Task SingleLightJobAsync(ProcessingFixture fx) => (await fx.Db.Jobs.ListAsync(null, JobKind.LightAnalyse, cancellationToken: fx.Ct)).Items.SingleOrDefault(); }