diff --git a/src/Titano.Core/Archiving/SequenceAdopter.cs b/src/Titano.Core/Archiving/SequenceAdopter.cs index 77d38d90..72069d3c 100644 --- a/src/Titano.Core/Archiving/SequenceAdopter.cs +++ b/src/Titano.Core/Archiving/SequenceAdopter.cs @@ -62,6 +62,9 @@ public static class SequenceAdopter if (flagChanges.Count == 0 && orphans.UnindexedFiles.Count == 0) return null; var updated = flagChanges.Count == 0 ? manifest : SequenceFrames.Update(manifest, flagChanges); var (added, first) = ReadFrames(sequence, orphans.UnindexedFiles, cancellationToken); + // Photos that cannot be read yet (held open by another program, no read permission, a placeholder not downloaded) + // stay unindexed: with nothing else changed, nothing changed. + if (added.Count == 0 && flagChanges.Count == 0) return null; if (updated.Camera is null && first is not null) updated = updated with { Camera = new SidecarCamera { Make = first.Make, Model = first.Model, Serial = first.BodySerial }, Lens = updated.Lens ?? first.LensModel }; return SequenceFrames.Insert(updated, added); diff --git a/src/Titano.Data/Processing/SequenceAnalyser.cs b/src/Titano.Data/Processing/SequenceAnalyser.cs index 47593854..8b9c048a 100644 --- a/src/Titano.Data/Processing/SequenceAnalyser.cs +++ b/src/Titano.Data/Processing/SequenceAnalyser.cs @@ -100,17 +100,22 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti { var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); var change = await FramesChangeAsync(sequence, manifest, cancellationToken).ConfigureAwait(false); - var saved = deep && SaveReport(folder, report!, change == FramesChange.None && manifest.Versions.Frames == sequence.FramesVersion, - manifest.Frames.Count); + // The frames as this host reads them now: a proxy that arrived while the pass ran moves no frames version but + // makes the pass's verdict on that frame's original stale, so only a report that describes them is current. + var now = deep && change == FramesChange.None + ? await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, options.Archive.Decoder, cancellationToken).ConfigureAwait(false) + : null; + var saved = deep && SaveReport(folder, report!, + change == FramesChange.None && manifest.Versions.Frames == sequence.FramesVersion && Describes(report!, now!), + now, manifest.Frames.Count); if (change != FramesChange.None) return new AnalysisOutcome(sequence, frames.Count, 0, SettingsChanged: false, SettingsUnreadable: false, [], change); if (deep) { // A report left in place is read as a light analysis reads it, so the deep status, summary, warnings and // Auto values recorded here describe the file that readers and the report endpoint open. - var now = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, options.Archive.Decoder, cancellationToken).ConfigureAwait(false); - if (!saved) report = ReadReport(folder, now); - else if (!Describes(report!, now)) report = null; + if (!saved) report = ReadReport(folder, now!); + else if (!Describes(report!, now!)) report = null; } else { @@ -255,15 +260,16 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti /// /// Saves a deep pass's report as analysis.json, under the sequence lock, then drops its checkpoint; returns - /// whether it saved it. The report replaces the saved one when it measured the frames as they are - /// (), or when the saved one describes them no better: none, unreadable, or of another frame - /// count than , the sidecar's. A pass overtaken by an upload, or by another process - /// sharing the archive, would otherwise replace the report a later pass made of the frames as they are, and the next - /// light analysis would drop that one's deep status. + /// whether it saved it. The report replaces the saved one when it describes the frames as they are + /// (), or when the saved one describes them no better: none, unreadable, or not describing + /// (the frames as this host reads them, proxies included), or, when they were not read, of + /// another frame count than , the sidecar's. A pass overtaken by an upload, by a proxy, + /// or by another process sharing the archive, would otherwise replace the report a later pass made of the frames as + /// they are, and the next light analysis would drop that one's deep status. /// - private static bool SaveReport(SequencePaths folder, DeepAnalysisReport report, bool current, int frameCount) + private static bool SaveReport(SequencePaths folder, DeepAnalysisReport report, bool current, SequenceFrames? now, int frameCount) { - var save = current || ReadReport(folder, frameCount) is null; + var save = current || (now is not null ? ReadReport(folder, now) : ReadReport(folder, frameCount)) is null; if (save) report.Save(folder.AnalysisPath); Checkpoint(folder).Delete(); return save; diff --git a/tests/Titano.Core.Tests/Archiving/ArchiveFolderTests.cs b/tests/Titano.Core.Tests/Archiving/ArchiveFolderTests.cs index 78748418..b1e13402 100644 --- a/tests/Titano.Core.Tests/Archiving/ArchiveFolderTests.cs +++ b/tests/Titano.Core.Tests/Archiving/ArchiveFolderTests.cs @@ -78,6 +78,25 @@ public sealed class ArchiveFolderTests(TestContext context) Assert.Equal(FrameFlags.None, restored.Frames[2].Flags); } + [Test] + public void APhotoThatCannotBeReadYetChangesNothing() + { + var archive = NewArchive(); + var sequence = archive.Sequence("s"); + TestPhotos.WriteSeries(sequence.Originals, Start, 3, TimeSpan.FromSeconds(10)); + var manifest = SequenceAdopter.Build(Guid.CreateVersion7(), sequence, null); + var added = Path.Combine(sequence.Originals, "IMG_0009.JPG"); + TestPhotos.Write(added, Start.AddSeconds(60), 9); + + // Held open by another program (or without read permission): it stays unindexed, and the sidecar is as it was. + using (new FileStream(added, FileMode.Open, FileAccess.Read, FileShare.None)) + { + Assert.SequenceEqual(["originals/IMG_0009.JPG"], SequenceAdopter.FindOrphans(sequence, manifest).UnindexedFiles); + Assert.Null(SequenceAdopter.Reconcile(sequence, manifest)); + } + Assert.Equal(4, SequenceAdopter.Reconcile(sequence, manifest)!.Frames.Count); + } + [Test] public async Task VerifyDetectsAFlippedByteAMissingFileAndAChangedFile() { diff --git a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs index fc26bb1e..05f0416c 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs @@ -255,6 +255,50 @@ public sealed class AnalysisJobTests(TestContext context) Assert.Equal(advice, await AutoValuesAsync()); } + [Test] + public async Task ADeepAnalysisMadeStaleByProxiesLeavesTheReportALaterPassMadeFromThem() + { + 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, thumbnailWidth: 0), + 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); + + // A deep analysis starts on the originals, in which the server finds no pixels; while it runs, the proxies arrive + // and a second deep analysis, on the other worker, measures every frame from them and records its report. + fx.Decoder.Hold = "IMG_0001.HEIC"; + var stale = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct); + fx.Decoder.Hold = null; + 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); + var current = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + await fx.WaitForAsync(current.Id, JobState.Done); + var report = await File.ReadAllTextAsync(folder.AnalysisPath, fx.Ct); + var analysed = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.True(analysed.Status.HasFlag(SequenceStatus.AnalyzedDeep), analysed.Status.ToString()); + fx.Decoder.Release(); + await fx.WaitForAsync(stale.Id, JobState.Done); + + // The first pass's verdict on the originals no longer describes the frames: it leaves the report made from the + // proxies, and with it the deep status, in place. + Assert.Equal(report, await File.ReadAllTextAsync(folder.AnalysisPath, fx.Ct)); + var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedDeep), row.Status.ToString()); + Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedDeep), "sidecar status"); + Assert.Equal(analysed.AnalysisJson, row.AnalysisJson); + } + [Test] public async Task FramesAddedDuringADeepAnalysisKeepTheLightAnalysisOfTheFramesAsTheyAre() {