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

+43 -27
View File
@@ -197,9 +197,16 @@ public sealed class SyncService
try
{
var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false);
var request = await RequestAsync(remote, local, manifest, seenRemote, cancellationToken).ConfigureAwait(false);
var updated = await remote.UpdateSequenceAsync(remoteId, request, cancellationToken).ConfigureAwait(false);
link = Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion);
// Edited here since it was shown, or frames that kept changing while they were read: compared again.
if (await RequestAsync(remote, local, manifest, seenRemote, cancellationToken).ConfigureAwait(false) is not { } request)
{
changed = true;
}
else
{
var updated = await remote.UpdateSequenceAsync(remoteId, request, cancellationToken).ConfigureAwait(false);
link = Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion);
}
}
catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict)
{
@@ -349,7 +356,13 @@ public sealed class SyncService
var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false);
try
{
var request = await RequestAsync(remote, local, manifest, manifest.SettingsVersion, cancellationToken).ConfigureAwait(false);
if (await RequestAsync(remote, local, manifest, manifest.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } request)
{
// An edit saved here meanwhile goes instead; frames that keep changing here leave it to the next comparison.
if (attempt >= 3 || await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false) is not { } fresh) return;
local = fresh;
continue;
}
var updated = await remote.UpdateSequenceAsync(remoteId, request, cancellationToken).ConfigureAwait(false);
await _links.UpsertAsync(Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion), cancellationToken)
.ConfigureAwait(false);
@@ -569,9 +582,13 @@ public sealed class SyncService
manifest ??= await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false);
// The listing and the manifest disagree: the server changed during the comparison; settle it next time.
if (manifest.SettingsVersion != dto.SettingsVersion) return (link, local);
// Compared (and shown) with its positions on this copy's frames, against this copy as it is at those frames: the
// same photos are the same settings. Edited here meanwhile, or frames that kept changing: compared next time.
if (await Positions(remote, local, manifest).ToLocalAsync(manifest.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false)
is not { } translated) return (link, local);
local = translated.Local;
var mine = SyncPayload.Of(local);
// Compared (and shown) with its positions on this copy's frames: the same photos are the same settings.
var theirs = SyncPayload.Of(dto, await Positions(remote, local, manifest).ToLocalAsync(manifest.SettingsJson, cancellationToken).ConfigureAwait(false));
var theirs = SyncPayload.Of(dto, translated.SettingsJson);
if (mine.SameAs(theirs)) return (Agreed(link, local.SettingsVersion, manifest.SettingsVersion), local);
// Last writer wins only when one side changed; both changed differently is the user's choice (BRIEF §14.5).
return (link with
@@ -602,14 +619,14 @@ public sealed class SyncService
/// <summary>
/// Sends this PC's payload unless the server moved past <paramref name="expected"/>, which shows up as a conflict next,
/// or its frames changed since <paramref name="manifest"/> (the positions are translated again next time).
/// or either copy's frames changed since they were read (the positions are translated again next time).
/// </summary>
private async Task<SyncLink> PushAsync(ISyncRemote remote, SyncLink link, Sequence local, SequenceManifest manifest, int? expected,
CancellationToken cancellationToken)
{
try
{
var request = await RequestAsync(remote, local, manifest, expected, cancellationToken).ConfigureAwait(false);
if (await RequestAsync(remote, local, manifest, expected, cancellationToken).ConfigureAwait(false) is not { } request) return link;
var updated = await remote.UpdateSequenceAsync(link.RemoteId, request, cancellationToken).ConfigureAwait(false);
return Agreed(Observe(link, link.Server, updated), local.SettingsVersion, updated.SettingsVersion);
}
@@ -620,16 +637,18 @@ public sealed class SyncService
}
/// <summary>
/// The PATCH that makes the server's copy equal to <paramref name="local"/>, its frame positions moved to the server's
/// frames as <paramref name="manifest"/> lists them; the server refuses it when its frames changed since.
/// The PATCH that makes the server's copy equal to this copy at <paramref name="local"/>'s settings version (read again
/// with its frames, which may have moved its positions since), its frame positions moved to the server's frames as
/// <paramref name="manifest"/> lists them; the server refuses it when its frames changed since. Null when this copy
/// was edited since <paramref name="local"/> was read, or either copy's frames cannot be read as one state now.
/// </summary>
private async Task<UpdateSequenceRequest> RequestAsync(ISyncRemote remote, Sequence local, SequenceManifest manifest, int? expected,
private async Task<UpdateSequenceRequest?> RequestAsync(ISyncRemote remote, Sequence local, SequenceManifest manifest, int? expected,
CancellationToken cancellationToken)
{
var positions = Positions(remote, local, manifest);
var settings = await positions.ToServerAsync(local.SettingsJson, cancellationToken).ConfigureAwait(false);
var frames = ProjectSettings.NamesFrames(local.SettingsJson) ? positions.ServerFrames : (int?)null;
return (SyncPayload.Of(local) with { SettingsJson = settings }).ToRequest(expected, frames);
if (await positions.ToServerAsync(local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } translated) return null;
var frames = ProjectSettings.NamesFrames(translated.Local.SettingsJson) ? positions.ServerFrames : (int?)null;
return (SyncPayload.Of(translated.Local) with { SettingsJson = translated.SettingsJson }).ToRequest(expected, frames);
}
private SyncPositions Positions(ISyncRemote remote, Sequence local, SequenceManifest manifest) => new(_db, remote, local.Id, manifest);
@@ -695,19 +714,17 @@ public sealed class SyncService
/// <summary>
/// Writes the server's payload into the local index, project and sidecar in one step (settings version + 1) under the
/// sequence's lease, which the editor's saves take too, and only while the local copy is still at the settings version
/// <paramref name="local"/> was read at. The settings' frame positions are moved to this copy's frames as they are
/// under the lease (<paramref name="positions"/>). Null when an edit was saved on this PC since: nothing is written
/// then. A payload the local copy already holds writes nothing, so it needs neither a writable archive nor the lease.
/// sequence's lease, which the editor's saves and the archive's frame commits take too, and only while the local copy
/// is still at the settings version <paramref name="local"/> was read at. The settings' frame positions are moved to
/// this copy's frames as they are under the lease (<paramref name="positions"/>). Null when an edit was saved on this
/// PC since, or the frames of either copy could not be read as one state: nothing is written then. A payload the local
/// copy already holds writes nothing, so it needs neither a writable archive nor the lease.
/// </summary>
private async Task<Sequence?> ApplyLocallyAsync(Sequence local, SyncPayload payload, SyncPositions positions, CancellationToken cancellationToken)
{
var stored = await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false)
?? throw new InvalidDataException($"Sequence {local.Id} is not indexed.");
if (stored.SettingsVersion != local.SettingsVersion) return null;
if (await positions.ToLocalAsync(payload.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } stored) return null;
// Settled as it is: an archive that is offline or read-only does not keep the link from being agreed.
var mapped = payload with { SettingsJson = await positions.ToLocalAsync(payload.SettingsJson, cancellationToken).ConfigureAwait(false) };
if (mapped.SameAs(SyncPayload.Of(stored))) return stored;
if ((payload with { SettingsJson = stored.SettingsJson }).SameAs(SyncPayload.Of(stored.Local))) return stored.Local;
var archive = await _db.Archives.GetAsync(local.ArchiveId, cancellationToken).ConfigureAwait(false)
?? throw new InvalidDataException($"Archive {local.ArchiveId} is not indexed.");
@@ -716,11 +733,10 @@ public sealed class SyncService
var folder = paths.Sequence(local.Folder);
await using (await _writer.LockAsync(paths, local.Id, cancellationToken).ConfigureAwait(false))
{
var row = await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false)
?? throw new InvalidDataException($"Sequence {local.Id} is not indexed.");
if (row.SettingsVersion != local.SettingsVersion) return null;
// The frames cannot change under the lease: the positions are moved to them as they are now.
payload = payload with { SettingsJson = await positions.ToLocalAsync(payload.SettingsJson, cancellationToken).ConfigureAwait(false) };
if (await positions.ToLocalAsync(payload.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } current) return null;
var row = current.Local;
payload = payload with { SettingsJson = current.SettingsJson };
var metadata = !payload.SameMetadata(SyncPayload.Of(row));
var settings = !SyncRules.SameSettings(payload.SettingsJson, row.SettingsJson);
if (!metadata && !settings) return row;