fix(archive): exports follow a folder any index moved; proxies, exports and copies no longer recreate it

Second review of owner review 2, phase 4a (Reorganize), cross-cutting findings.

- A second index of the archive (a desktop sharing a server's archive, or a folder moved by
  hand) moved the sequence row at its next scan but left renders.output_path at the old
  folder: the export answered 404 there. The scan now rebases the render rows in the same
  transaction when it finds a sequence in another folder.
- The rebase covers every render of the archive under the old folder: the sequence's own
  exports always, and another sequence's export saved inside it (through the Export folder
  picker) when its file moved with the folder. That check keeps the exports of a sequence
  that took the old folder since (a later Reorganize on the other index) where they are. A
  change of case only counts as a move.
- The desktop now tells an open editor after each Reorganize step
  (ArchiveServiceOptions.SequenceChanged -> AppServices.SequenceReorganized), and the editor
  reads its sequence again where it is now, so frames no longer decode blank until the next
  save. LocalSequenceSource.LoadAsync also requires the same folder on both row reads, so a
  move that commits during the frame listing is never taken as current.
- Writers that wrote without the lease, or trusted a row another index had moved, could
  create the old folder again. A proxy sent by a client is now filed under the sequence's
  lease into the folder the index has then, and a gone sequence or folder answers 404
  (sequence_busy 503 after the lease wait). The server files a render a client or render
  node sends under the lease (ArchiveService.UnderLeaseAsync). "Save both" keeps its copy
  under the lease. The proxy pass holds the thumbnail lease, as the thumbnail pass does. The
  deep analysis opens its checkpoint under the sequence lock only while the folder is still
  there, and a pass whose folder another process moved records nothing (the job logs
  Advisor.Log.FramesChangedElsewhere). Editor saves and sync applies refuse when the folder
  is gone.
- ARCHITECTURE.md no longer claims more than this: a render of another process whose folder
  moves while it runs is still not stopped.
- ArchiveVerifier.FlushAsync: corrected the comment. A move keeps each file's stamp, so only a
  check that found the file missing no longer applies.

Tests cover the scan path with a later swap into the vacated folder, another sequence's
export inside a moving folder, a photo deletion refused when the folder moved while it
waited for the lease, proxies sent and made after a move, Save both during a move, a deep
analysis whose folder another index moved, a render filed after a move on the server, and
an open editor reloaded by a Move job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c857b98f795b22956c060f1c5acf4d94b6d1c994)
This commit is contained in:
Alby96 committed 2026-10-27 18:09:02 +01:00
1 parent 765f0ad36b
commit 6e99a4b269
20 files changed
+561 -160

No files matched your search

@@ -46,20 +46,35 @@ public sealed class RenderRepository
}, cancellationToken);
/// <summary>
/// Rewrites the output paths of a sequence's renders after its folder moved (Reorganize): an export inside the folder
/// moved with it. Outputs elsewhere (absolute paths, other folders of the archive) stay as they are.
/// Rewrites the archive-relative output paths of an archive's renders after the folder of sequence
/// <paramref name="sequenceId"/> moved (a Reorganize, or a move another index of the archive or the user made, which a
/// scan finds): an export inside the folder moved with it. The sequence's own exports there follow it, and so does an
/// export of another sequence saved inside it (through the Export folder picker) when <paramref name="moved"/> says its
/// file moved with it: a sequence that took the old folder since (a later Reorganize another index made) keeps its own.
/// Outputs elsewhere (absolute paths, other folders) stay as they are.
/// </summary>
/// <param name="moved">Whether the file at the first archive-relative path is at the second one now.</param>
/// <remarks>Only paths still under <paramref name="oldFolder"/> change, so repeating it after a crash is harmless.</remarks>
public Task<int> RebaseFolderAsync(Guid sequenceId, string oldFolder, string newFolder, CancellationToken cancellationToken = default)
public async Task<int> RebaseFolderAsync(Guid archiveId, Guid sequenceId, string oldFolder, string newFolder, Func<string, string, bool> moved,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(moved);
// The archive root never moves, and its prefix ("/") would match absolute paths outside the archive.
if (oldFolder.Length == 0) return Task.FromResult(0);
// substr counts characters (code points) on both dialects; the tail starts with the '/' after the folder.
if (oldFolder.Length == 0) return 0;
// substr counts characters (code points) on both dialects.
var prefix = oldFolder + "/";
var length = prefix.EnumerateRunes().Count();
return _db.ExecuteAsync(
"UPDATE renders SET output_path = $2 || substr(output_path, $3) WHERE sequence_id = $1 AND substr(output_path, 1, $4) = $5",
[sequenceId, newFolder, length, length, prefix], cancellationToken);
var under = await _db.ListAsync(
"SELECT r.id, r.sequence_id, r.output_path FROM renders r JOIN sequences s ON s.id = r.sequence_id WHERE s.archive_id = $1 AND substr(r.output_path, 1, $2) = $3",
[archiveId, prefix.EnumerateRunes().Count(), prefix], r => (Id: r.GetGuid(0), SequenceId: r.GetGuid(1), Path: r.GetString(2)), cancellationToken)
.ConfigureAwait(false);
var rebased = 0;
foreach (var (id, owner, path) in under)
{
var to = newFolder.Length == 0 ? path[prefix.Length..] : newFolder + path[oldFolder.Length..];
if (owner != sequenceId && !moved(path, to)) continue;
rebased += await _db.ExecuteAsync("UPDATE renders SET output_path = $2 WHERE id = $1", [id, to], cancellationToken).ConfigureAwait(false);
}
return rebased;
}
/// <summary>