fix(sync): translate positions only against the frames they name

Positions crossing copies could still land on other photos in three
windows:

- A push translated a row snapshot's settings against local frames
  listed later, after a manifest round trip: an import of earlier photos
  meanwhile moved the row's positions, and the server stored shifted
  exclusions, hero and keyframes. SyncPositions now reads this copy's
  row with its frames as one state (frames listed between two reads of
  the row at the same frames version) and translates that row's
  settings, at the settings version being pushed. The conflict
  comparison and ApplyLocallyAsync compare against that same row. When
  the copy was edited since, or its frames keep changing, nothing is
  sent or applied and the next comparison translates again (Keep mine
  reports a changed conflict; PushAll sends an edit saved meanwhile).
- The manifest read the sequence row, then paged its frames: an upload
  committed in between paired settings of one frames version with
  frames of the next. The server now lists them again while the frames
  version moved during the listing, then inside a transaction holding
  the row (on SQLite, on that transaction's connection, so no second
  pooled connection waits behind the write lock). The client also
  refuses to translate against a manifest whose frames are not each
  listed once at 0..n-1.
- Server capture times were fetched only when something was unhashed,
  so the same parked photo was written with or without capturedAt. They
  are now fetched whenever the two frame lists differ; an equal project
  then compares equal, and an entry can be placed on a photo that
  arrives unhashed.

CompareOriginals counted unhashed server frames one-to-one against this
PC's photos, so two copies of one photo in a folder the server adopted
left Download on offer forever while the download fetched nothing. A
photo on either side is now there when any photo of the other side
matches it, as downloads and uploads find them. Paged frame listings
keep each frame once (an insertion between pages lists one again).

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 17:09:30 +00:00
1 parent 98e1486f83
commit a56aaf8d65
12 files changed
+322 -95

No files matched your search

+32 -3
View File
@@ -27,6 +27,9 @@ public static class SequenceEndpoints
private const int MaxNotes = 2000;
private const int MaxTags = 50;
/// <summary>Listings of a sequence's frames read without a lock before one is read under its row lock (<see cref="SnapshotAsync"/>).</summary>
private const int SnapshotAttempts = 3;
public static void Map(RouteGroupBuilder api)
{
// Uploaders list sequences to pick an upload target.
@@ -220,14 +223,16 @@ public static class SequenceEndpoints
/// <summary>
/// Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5), with the proxies the server asks
/// for (<see cref="ServerProxies"/>); ETag over the version counters and the number of proxies asked for.
/// for (<see cref="ServerProxies"/>); ETag over the version counters and the number of proxies asked for. The frames,
/// the frames version and the settings whose positions name those frames are of one frames version (<see cref="SnapshotAsync"/>).
/// </summary>
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
{
var ct = context.RequestAborted;
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
var spec = ServerProxies.Spec(archive.Current);
var frames = await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, ct);
if (await SnapshotAsync(db, id, (on, s, c) => ServerProxies.FramesAsync(on, archive.Current, s, spec, c), ct) is not { } snapshot)
return ApiErrors.NotFound(context, "sequence");
var (sequence, frames) = snapshot;
var etag = string.Create(CultureInfo.InvariantCulture,
$"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}-{frames.Count(f => f.WantsProxy)}\"");
if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified);
@@ -238,6 +243,30 @@ public static class SequenceEndpoints
[.. renders.Select(Dto.Render)], spec), ProtocolJson.Default.SequenceManifest);
}
/// <summary>
/// The sequence and its frames as of one frames version: a client pairs the settings' frame positions with these
/// frames. <paramref name="listFrames"/> reads them in pages, and a commit of earlier photos renumbers them and moves
/// the settings' positions at once, so a listing read across one would mix two orders. It is read again while the
/// frames version moved during it, and then inside a transaction holding the sequence's row, which every commit of
/// frames locks first (on SQLite, the write lock: the listing runs on that transaction's own connection). Null when
/// the sequence is gone.
/// </summary>
internal static async Task<(Sequence Sequence, List<ManifestFrame> Frames)?> SnapshotAsync(TitanoDatabase db, Guid id,
Func<TitanoRepositories, Sequence, CancellationToken, Task<List<ManifestFrame>>> listFrames, CancellationToken cancellationToken)
{
for (var attempt = 0; attempt < SnapshotAttempts; attempt++)
{
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } sequence) return null;
var frames = await listFrames(db, sequence, cancellationToken);
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } after) return null;
if (after.FramesVersion == sequence.FramesVersion) return (sequence, frames);
}
// Commits keep landing (an upload of earlier photos): the frames are listed while none can.
await using var tx = await db.BeginAsync(cancellationToken);
if (await tx.Sequences.GetForUpdateAsync(id, cancellationToken) is not { } locked) return null;
return (locked, await listFrames(tx, locked, cancellationToken));
}
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, TitanoDatabase db)
{
var ct = context.RequestAborted;
+1 -1
View File
@@ -38,7 +38,7 @@ public static class ServerProxies
/// built them all or one was sent for the frame; it is wanted for a frame the server cannot decode that has none
/// (and is not missing), unless <paramref name="spec"/> is null.
/// </summary>
public static async Task<List<ManifestFrame>> FramesAsync(TitanoDatabase db, Archive archive, Sequence sequence, ProxySpec? spec,
public static async Task<List<ManifestFrame>> FramesAsync(TitanoRepositories db, Archive archive, Sequence sequence, ProxySpec? spec,
CancellationToken cancellationToken)
{
var proxies = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);