fix(archive): keep project frame positions on their frames after inserts

The project names frames by position (exclusions, hero, virtual-camera
keyframes), and an upload, import, scan or sync download of earlier photos
merges them by capture time and renumbers the frames after them; nothing
moved the project, so a render excluded the wrong photo and the project's
hero no longer matched the Library's.

project.json keeps its format: when the index takes a sidecar in which frames
were inserted before indexed ones, the index's copy of the project has its
positions moved from the indexed order to the new one in the same transaction
as the frame rows, and project.json mirrors it unless another writer changed
it meanwhile. The JSON is edited in place, so members a newer Titano wrote
survive; only an insertion is followed (if an indexed frame is gone, the
positions stay). The settings version stays, so a synced copy receiving the
same frames makes the same move (CD-2).

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 09:40:40 +00:00
1 parent 94b13cc064
commit 01e17c9961
7 files changed
+252 -3

No files matched your search

+11 -1
View File
@@ -91,18 +91,28 @@ public sealed class ArchiveIndexer
return new ScanSummary(folders.Count, indexed, adopted, removed, failed);
}
/// <summary>Writes a sequence's sidecar into the index (row, frames, session keys) and records the folder stamps.</summary>
/// <summary>
/// Writes a sequence's sidecar into the index (row, frames, session keys) and records the folder stamps. Frames
/// inserted before indexed ones take the project's frame positions along (<see cref="ProjectPositions"/>).
/// </summary>
internal async Task SyncAsync(Archive archive, SequencePaths sequence, SequenceManifest manifest, CancellationToken cancellationToken)
{
(string Before, string Moved)? positions = null;
await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
var existing = await tx.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false);
if (existing is not null)
{
positions = await ProjectPositions.MoveAsync(tx, existing, manifest, cancellationToken).ConfigureAwait(false);
if (positions is { } project) existing = existing with { SettingsJson = project.Moved };
}
await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, existing), cancellationToken).ConfigureAwait(false);
await tx.Frames.SyncSequenceAsync(manifest.Id, SequenceMapping.ToFrames(manifest, sequence), cancellationToken).ConfigureAwait(false);
foreach (var key in manifest.SessionKeys)
await tx.Sequences.MapSessionKeyAsync(archive.Id, key, manifest.Id, cancellationToken).ConfigureAwait(false);
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
}
if (positions is { } moved) ProjectPositions.WriteProject(sequence, moved.Before, moved.Moved);
await RecordStampsAsync(archive, sequence, manifest.Id, cancellationToken).ConfigureAwait(false);
}
+1 -1
View File
@@ -456,7 +456,7 @@ public sealed class ArchiveIngest : IArchiveIngest
/// <summary>
/// Appends the new rows when the index was in step with the sidecar; otherwise (or when frames were repaired, whose
/// flags change) resyncs the whole sequence.
/// flags change) resyncs the whole sequence, which also moves the project's frame positions after an insertion.
/// </summary>
private async Task IndexAsync(Archive archive, SequencePaths sequence, SequenceManifest manifest, int before, List<SidecarFrame> appended,
bool repaired, CancellationToken cancellationToken)
@@ -0,0 +1,75 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.IO;
using Titano.Core.Model;
using Titano.Core.Project;
namespace Titano.Data.Archiving;
/// <summary>
/// Keeps a project's frame positions (exclusions, hero, virtual-camera keyframes; BRIEF §5, §7) on their frames when the
/// index takes a sidecar in which frames were inserted before others: an upload or import of earlier photos, photos a
/// scan found, a sync download, a journalled batch completed after a crash. Positions name the frames in the indexed
/// order (what editors, renders and the API read), so the old order comes from the index rows. Only an insertion is
/// followed: when an indexed frame is no longer in the sidecar, positions cannot be mapped and stay as they are. The move
/// is part of the frames change, not a settings edit, so the settings version stays and a synced copy receiving the
/// same frames makes the same move.
/// </summary>
internal static class ProjectPositions
{
/// <summary>
/// The index's copy of the project (the one in force) and the same project with its positions moved to the order of
/// <paramref name="manifest"/>; null when nothing moves. A <c>project.json</c> the index has no copy of was written by
/// another process against its own frames, so it is left to that writer.
/// </summary>
public static async Task<(string Before, string Moved)?> MoveAsync(TitanoRepositories db, Sequence indexed, SequenceManifest manifest,
CancellationToken cancellationToken)
{
// The sidecar's frames are the ones indexed: nothing was inserted.
if (indexed.FramesVersion == manifest.Versions.Frames) return null;
if (indexed.SettingsJson is not { } project) return null;
var rows = await db.Frames.ListPositionsAsync(indexed.Id, cancellationToken).ConfigureAwait(false);
var now = new Dictionary<Guid, int>(manifest.Frames.Count);
foreach (var frame in manifest.Frames) now[frame.Id] = frame.I;
var moves = new Dictionary<int, int>();
foreach (var row in rows)
{
if (!now.TryGetValue(row.FrameId, out var index)) return null;
if (index != row.Index) moves[row.Index] = index;
}
return ProjectSettings.MoveFramePositions(project, moves) is { } moved ? (project, moved) : null;
}
/// <summary>
/// Mirrors moved positions into <c>project.json</c> once the index holds them, unless the file was written since
/// (it then belongs to that writer, whose next save mirrors the index again). A failed write leaves the index right.
/// </summary>
public static void WriteProject(SequencePaths sequence, string before, string moved)
{
try
{
if (File.Exists(sequence.ProjectPath) && !SameJson(File.ReadAllText(sequence.ProjectPath), before)) return;
Directory.CreateDirectory(sequence.MetaDirectory);
AtomicFile.WriteAllText(sequence.ProjectPath, moved);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("archive", $"Project of {sequence.RelativeFolder} keeps its old frame positions on disk", ex);
}
}
/// <summary>Same JSON content whatever the layout (an index may store it normalised).</summary>
private static bool SameJson(string a, string b)
{
try
{
return JsonNode.DeepEquals(JsonNode.Parse(a), JsonNode.Parse(b));
}
catch (JsonException)
{
return false;
}
}
}