fix(ingest): file uploads into API-made sequences and repair damaged frames

A sequence created with POST /api/v1/sequences had no folder, and the ingest
looked for its sidecar at the archive root: every upload into it failed with
503 commit_failed, so the Uploader's "New sequence" target never worked. Its
first frame now claims a folder by the rules of a sequence created by an
upload (folder template with its title, unique variant, sidecar with the
row's id, title, tags, rating, notes and project), and the row takes the
folder in one transaction (DOCKER-1).

A frame flagged missing or corrupt, or whose file is gone or has another
size, no longer stands for an upload: the start short-circuit and crash
recovery match only intact frames still on disk, and the ingest repairs such
a frame with the verified copy (same id and position, flags cleared, a damaged
file kept beside it as name.damaged-<time>) instead of calling it a
duplicate. The Uploader deletes a source only after the server received and
verified its bytes in that upload, never on "already on the server" (SRV-1).

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:27 +00:00
1 parent 23308c77dd
commit 94b13cc064
12 files changed
+441 -41

No files matched your search

@@ -62,10 +62,18 @@ public sealed class FrameRepository
_db.ListAsync($"SELECT {Columns} FROM frames WHERE sequence_id = $1 AND idx > $2 ORDER BY idx LIMIT $3",
[sequenceId, afterIndex, Cursor.ClampLimit(limit)], Map, cancellationToken);
/// <summary>Every frame with this content hash (duplicate detection, upload short-circuit).</summary>
/// <summary>Every frame with this content hash (duplicate detection).</summary>
public Task<IReadOnlyList<Frame>> FindBySha256Async(string sha256, CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM frames WHERE sha256 = $1 ORDER BY sequence_id, idx", [sha256.ToLowerInvariant()], Map, cancellationToken);
/// <summary>
/// Frames with this content hash that are not flagged missing or corrupt: only such a copy may stand for an upload
/// (the upload short-circuit, a commit settled after a crash), so an upload of the same content repairs a damaged one.
/// </summary>
public Task<IReadOnlyList<Frame>> FindIntactBySha256Async(string sha256, CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM frames WHERE sha256 = $1 AND (flags & $2) = 0 ORDER BY sequence_id, idx",
[sha256.ToLowerInvariant(), (int)(FrameFlags.Missing | FrameFlags.Corrupt)], Map, cancellationToken);
/// <summary>Writes analysis results for many frames in one transaction; returns the number of frames updated.</summary>
public Task<int> UpdateLightStatsAsync(IReadOnlyList<FrameLightStats> stats, CancellationToken cancellationToken = default) =>
_db.InTransactionAsync(async session =>