merge: sync and project position fixes (V-9, V-10, V-21, V-29, CD-2)

Merge branch fix2/sync-positions into the verification branch.

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:10:58 +00:00
commit 51acc7f91b
32 files changed
+1843 -153

No files matched your search

+38 -5
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.
@@ -140,14 +143,18 @@ public static class SequenceEndpoints
HeroFrameId = request.HeroFrameId
?? (request.SettingsJson is { } settings ? await SettingsHeroAsync(db, id, settings, context.RequestAborted) : null),
};
var result = await db.Sequences.EditAsync(id, edit, expected, context.RequestAborted);
var result = await db.Sequences.EditAsync(id, edit, expected, request.ExpectedFramesVersion, context.RequestAborted);
switch (result.Outcome)
{
case SequenceEditOutcome.NotFound:
return ApiErrors.NotFound(context, "sequence");
case SequenceEditOutcome.Conflict when expected is { } settingsVersion && settingsVersion != result.Sequence!.SettingsVersion:
context.Response.Headers.ETag = ETag(result.Sequence);
return ApiErrors.Conflict(context, "settings_conflict", result.Sequence.SettingsVersion.ToString(CultureInfo.InvariantCulture));
case SequenceEditOutcome.Conflict:
// The settings name frames by position and were written against frames that changed since.
context.Response.Headers.ETag = ETag(result.Sequence!);
return ApiErrors.Conflict(context, "settings_conflict", result.Sequence!.SettingsVersion.ToString(CultureInfo.InvariantCulture));
return ApiErrors.Conflict(context, "frames_conflict", result.Sequence!.FramesVersion.ToString(CultureInfo.InvariantCulture));
}
var sequence = result.Sequence!;
await audit.WriteAsync(caller, context, "sequence_updated", "sequence", id.ToString(), Changes(request));
@@ -216,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);
@@ -234,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
@@ -49,7 +49,7 @@ public static class ServerProxies
/// added or renumbered since keep it until the next proxy pass), and wants one otherwise (unless it is missing or
/// damaged, or <paramref name="spec"/> is null); any other frame has one when the server built them all.
/// </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);