fix: projects and the workspace copy follow phase 4's folder and conversion rules

Phase 5a was written on an early phase 4a state. On top of phase 4b it now
keeps the rules every writer of a sequence's folder follows, and keeps
project versions intact through Reorganize and conversions:

- ProjectService writes project sidecars, project.json and the settings
  only under the sequence's lease and only in a folder that holds the
  sequence (SequenceFolders.Holds). A folder another index moved away, or
  one another sequence was moved into, is refused with
  SequenceFolderMovedException: reads answer from the index, so no
  project is taken for gone, nothing is written into the other sequence's
  folder and the old folder is never created again. The home reports it
  as out of reach. Full scans and rebuilds reconcile projects the same
  way, and a failure there is logged instead of failing the scan.
- Stored versions park positions by photo and now follow the encodings a
  Replace conversion (or its undo) joined through the sequence's Replaced
  tombstones (PhotoAlias, HomePhotos): Main's mirror, renders of a
  version and workspace media place them on the converted frames.
  ProjectContent.Rebind/SameChoices name parked photos as the frames hold
  them now, so a conversion or a hash since is no "external" Main version
  and no spurious "Modified" in a workspace, and merges see one photo.
- The workspace copy job only reads the archive: it no longer runs the
  archive's proxy pass, makes missing proxies in the workspace, reads
  only a folder that holds the sequence, starts again when the folder
  moved or files were renamed during the copy, resumes from a checkpoint
  of its frames version, and refuses a workspace folder inside any
  library before it writes anything (Errors.Workspace.InsideLibrary).
  The working state keeps its positions by frame where frames stay.
- ArchiveProjectHome looks for a project or version the index does not
  know yet in the sequence's sidecars first (an index taken back from a
  backup, a sequence back from the trash), instead of calling it gone.

Tests cover the moved and taken folder, conversions in the archive and in
the workspace, the copy refusing a workspace in a library, resuming on
other frames, the alias lookup and the rebinding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-02 10:14:39 +01:00
1 parent b9d1c396a1
commit 50e28fd63d
18 files changed
+596 -91

No files matched your search

@@ -10,6 +10,7 @@ using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
namespace Chronos.Data.Workspaces;
@@ -37,12 +38,17 @@ public static class WorkspaceJobs
/// <summary>
/// <see cref="JobKind.WorkspaceCopy"/> for a sequence of this PC's archive (owner review 2, R8): its proxies into the
/// workspace project's <c>media/proxies/&lt;frame id&gt;.jpg</c>, the archive's own when current, the missing ones made
/// first by the archive's proxy pass (made here from the original when the archive makes none), each written atomically
/// and read back to check its SHA-256; then the deep report (<c>media/analysis.json</c>) and, last, <c>media/media.json</c>.
/// Frames changed at the home during the copy make it start again (three times at most). The working state's positions
/// follow the new frames by photo. Checkpoint: <c>proxies:&lt;frame&gt;</c> during the archive's pass, then
/// <c>copy:&lt;frame&gt;</c>. Originals come in phase 5c: asked for, the job fails saying so.
/// workspace project's <c>media/proxies/&lt;frame id&gt;.jpg</c>, the archive's own when current, else made here from the
/// original (at the archive's proxy size, or the default one when the archive makes none), each written atomically and read
/// back to check its SHA-256; then the deep report (<c>media/analysis.json</c>) and, last, <c>media/media.json</c> with the
/// encodings conversions joined at the home. The job only reads the archive: it never writes there, not even proxies (the
/// archive's own pass makes those), refuses a workspace folder inside any library of the index before it writes anything,
/// and reads only a folder that holds the sequence (<see cref="SequenceFolders.Holds"/>).
/// Frames changed at the home during the copy, or the folder moved (a Reorganize), make it start again (three times at
/// most; a folder still moving fails it, to be retried). The working state's positions follow the new frames: by frame
/// where a frame stays (one whose file a conversion replaced keeps its positions), by photo where frames came or went.
/// Checkpoint: <c>copy:&lt;frame&gt;:&lt;frames version&gt;</c>; a run resumed on other frames checks every file again.
/// Originals come in phase 5c: asked for, the job fails saying so.
/// </summary>
internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService archives) : IJobHandler
{
@@ -62,35 +68,29 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
if (context.Job.TargetIds is not [var sequenceId] || await db.Sequences.GetAsync(sequenceId, ct).ConfigureAwait(false) is not { } sequence
|| await db.Archives.GetAsync(sequence.ArchiveId, ct).ConfigureAwait(false) is not { } archive)
throw new InvalidOperationException(Loc.L("Archive.Log.SequenceMissing", context.Job.TargetIds.FirstOrDefault()));
// Asked before the store is opened, which makes its folder: editing happens outside every library (owner review 2,
// R8), and a workspace inside one would put its copies among that library's own files.
if (await InsideALibraryAsync(parameters.WorkspaceRoot, ct).ConfigureAwait(false))
throw new InvalidOperationException(Loc.L("Errors.Workspace.InsideLibrary"));
var store = new WorkspaceStore(parameters.WorkspaceRoot);
var project = store.Find(parameters.ProjectId) ?? throw new InvalidOperationException(Loc.L("Errors.Workspace.NotCheckedOut"));
if (project.ReadBundle().Workspace is not { Home: { IsArchive: true } home } || home.SequenceId != sequenceId)
throw new InvalidOperationException(Loc.L("Errors.Workspace.OtherHome"));
var paths = new ArchivePaths(archive.RootPath);
var (stage, next) = Parse(context.Checkpoint);
// Read only: a read-only archive is copied from like any other.
if (ArchiveVolume.Probe(paths) == ArchiveAvailability.Offline) throw new ArchiveOfflineException(archive.RootPath, ArchiveAvailability.Offline);
var (copied, checkpointVersion) = Parse(context.Checkpoint);
for (var pass = 1; ; pass++)
{
var (row, frames) = await FramesAsync(sequenceId, ct).ConfigureAwait(false);
var policy = ManifestStore.ReadArchive(paths.ManifestPath).ProxyPolicy;
var makesProxies = policy.LongEdge != ProxyPolicy.Disabled && ArchiveVolume.Probe(paths) == ArchiveAvailability.Online;
var total = (long)frames.Count * (makesProxies ? 2 : 1);
long offset = 0;
if (makesProxies)
{
if (stage == Stage.Proxies && (row.Status & SequenceStatus.ProxiesBuilt) == 0)
{
var result = await archives.Images.BuildProxiesAsync(archive, row, force: false, next,
(frame, _) => context.CheckpointAsync(Checkpoint(Stage.Proxies, frame), frame, total), ct).ConfigureAwait(false);
context.Log(Loc.L("Archive.Log.ProxiesDone", row.DisplayTitle, result.Built, result.Reused, result.Undecodable, result.Waiting));
next = 0;
}
offset = frames.Count;
}
// Frames before a copy checkpoint were copied by an earlier run.
var copied = stage == Stage.Copy ? next : 0;
(stage, next) = (Stage.Copy, 0);
var folder = paths.Sequence(row.Folder);
// Another index moved the sequence away, maybe another one in: its files are not this sequence's photos.
if (!SequenceFolders.Holds(folder, sequenceId)) throw new SequenceFolderMovedException();
// Files kept from an earlier run are this frames version's only (a conversion keeps a frame's id, not its photo).
if (checkpointVersion is { } version && version != row.FramesVersion) copied = 0;
var total = (long)frames.Count;
var tally = new Tally();
var entries = new WorkspaceFrame[frames.Count];
Directory.CreateDirectory(project.ProxiesDirectory);
@@ -101,25 +101,32 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
entries[i] = Copy(project, folder, paths, frames[i], policy, done: i < copied, tally, ct);
if (clock.Elapsed >= CheckpointEvery)
{
await context.CheckpointAsync(Checkpoint(Stage.Copy, i + 1), offset + i + 1, total).ConfigureAwait(false);
await context.CheckpointAsync(Checkpoint(Stage.Copy, i + 1, row.FramesVersion), i + 1, total).ConfigureAwait(false);
clock.Restart();
}
else
{
await context.ReportAsync(offset + i + 1, total).ConfigureAwait(false);
await context.ReportAsync(i + 1, total).ConfigureAwait(false);
}
}
var analysis = CopyAnalysis(folder, project, frames.Count);
var after = await db.Sequences.GetAsync(sequenceId, ct).ConfigureAwait(false)
?? throw new InvalidOperationException(Loc.L("Archive.Log.SequenceMissing", sequenceId));
if (after.FramesVersion != row.FramesVersion && pass < Passes)
var (after, now) = await FramesAsync(sequenceId, ct).ConfigureAwait(false);
var moved = after.Folder != row.Folder || !SequenceFolders.Holds(paths.Sequence(after.Folder), sequenceId) || Renamed(frames, now, entries);
if (after.FramesVersion != row.FramesVersion || moved)
{
context.Log(Loc.L("Archive.Log.WorkspaceFramesChanged", row.DisplayTitle));
(stage, next) = (Stage.Proxies, 0);
continue;
// The files read may be of other frames, or missing where a Reorganize took them: read them again.
if (pass < Passes)
{
context.Log(Loc.L("Archive.Log.WorkspaceFramesChanged", row.DisplayTitle));
(copied, checkpointVersion) = (0, null);
continue;
}
// Frames still arriving are copied as they were read (Update media takes the rest); a folder still moving is not.
if (moved) throw new SequenceFolderMovedException();
}
var aliases = await HomePhotos.AliasesAsync(db, sequenceId, ct).ConfigureAwait(false);
var media = new WorkspaceMedia(WorkspaceMedia.FormatName, WorkspaceMedia.CurrentFormatVersion, BundleMedia.Proxies, sequenceId, row.FramesVersion, entries,
DateTimeOffset.UtcNow, analysis);
DateTimeOffset.UtcNow, analysis, aliases.Count > 0 ? aliases : null);
// The working state is the editor's meanwhile: it is held only while the media index and the positions move.
using (await project.LockAsync(LockWait, ct).ConfigureAwait(false))
{
@@ -127,11 +134,42 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
RemoveStale(project, media);
}
context.Log(Loc.L("Archive.Log.WorkspaceCopied", row.DisplayTitle, tally.Copied, tally.Made, tally.Kept, tally.Missing));
await context.CheckpointAsync(Checkpoint(Stage.Done, frames.Count), total, total).ConfigureAwait(false);
await context.CheckpointAsync(Checkpoint(Stage.Done, frames.Count, row.FramesVersion), total, total).ConfigureAwait(false);
return;
}
}
/// <summary>Whether <paramref name="folder"/> is an archive's root of this index or lies inside one.</summary>
private async Task<bool> InsideALibraryAsync(string folder, CancellationToken cancellationToken)
{
var path = Plain(Path.GetFullPath(folder));
foreach (var archive in await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false))
{
if (DataLocation.IsUnder(path, Plain(Path.GetFullPath(archive.RootPath)))) return true;
}
return false;
// Without the long-path prefix, which only one of the two may carry.
static string Plain(string path) =>
path.StartsWith(@"\\?\UNC\", StringComparison.Ordinal) ? @"\\" + path[8..]
: path.StartsWith(@"\\?\", StringComparison.Ordinal) ? path[4..]
: path;
}
/// <summary>
/// A frame the copy found no file for whose original the index has under another name now (a Reorganize renamed the
/// files during the copy, keeping the frames version): the copy runs again.
/// </summary>
private static bool Renamed(List<Frame> read, List<Frame> now, WorkspaceFrame[] entries)
{
var paths = now.ToDictionary(f => f.Id, f => f.RelativePath);
for (var i = 0; i < read.Count; i++)
{
if (entries[i].File is null && paths.TryGetValue(read[i].Id, out var path) && path != read[i].RelativePath) return true;
}
return false;
}
/// <summary>
/// The frame's file in the workspace: kept when an earlier run copied it (before the checkpoint, or the same bytes as the
/// archive's proxy), else the archive's current proxy, else one made from the original; none for a frame without either.
@@ -219,7 +257,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
/// <summary>
/// The media index and the working state moved onto it: first the working state with every position parked by photo
/// (valid whatever the frames), then the index, then the working state placed on the new frames. A crash in between
/// leaves positions that wait for their photos, which the next open places.
/// leaves positions that wait for their photos, which the next open places. A frame the new media still hold is parked
/// as the photo they hold for it, so one whose file a conversion replaced (the same frame, another SHA-256) keeps its
/// positions, as the archive's own settings do (<see cref="ProjectPositions"/>).
/// </summary>
private static void Commit(WorkspaceProject project, ProjectBundle bundle, WorkspaceMedia media, Sequence row)
{
@@ -230,7 +270,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
// Without the media index the positions were made against, they are taken as naming these frames.
if (!working.IsNewerFormat && old is not null)
{
var portable = ProjectSettings.FromJson(ProjectContent.ToPortable(working, old.Photos), allowNewer: true);
var held = media.Frames.ToDictionary(f => f.Id);
var before = new FramePhotos(old.Frames.Select(f => (f.Index, Photo(held.TryGetValue(f.Id, out var now) ? now : f))), old.Aliases);
var portable = ProjectSettings.FromJson(ProjectContent.ToPortable(working, before), allowNewer: true);
project.WriteBundle(bundle with { Project = portable, Workspace = workspace, Sequence = sequence });
project.WriteMedia(media);
working = ProjectSettings.FromJson(ProjectContent.Place(portable.ToJson(), media.Photos), allowNewer: true);
@@ -242,6 +284,8 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
project.WriteBundle(bundle with { Project = working, Workspace = workspace, Sequence = sequence, Saved = DateTimeOffset.UtcNow });
}
private static FramePhoto Photo(WorkspaceFrame frame) => new(frame.Sha256, frame.Size, frame.CapturedAt);
/// <summary>Proxies of frames the sequence no longer has: regenerable copies, removed so the media folder holds the frames listed.</summary>
private static void RemoveStale(WorkspaceProject project, WorkspaceMedia media)
{
@@ -313,21 +357,26 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
private enum Stage
{
Proxies,
Copy,
Done,
}
private static string Checkpoint(Stage stage, int frame) =>
string.Create(CultureInfo.InvariantCulture, $"{stage.ToString().ToLowerInvariant()}:{frame}");
private static string Checkpoint(Stage stage, int frame, int framesVersion) =>
string.Create(CultureInfo.InvariantCulture, $"{stage.ToString().ToLowerInvariant()}:{frame}:{framesVersion}");
private static (Stage Stage, int Next) Parse(string? checkpoint)
/// <summary>
/// The frames copied by an earlier run (<c>copy:&lt;frame&gt;[:&lt;frames version&gt;]</c>) and the frames version they
/// were of (null when the checkpoint does not say); nothing for a finished run, or another stage (an earlier build's
/// archive proxy pass), whose files the copy checks again.
/// </summary>
private static (int Copied, int? FramesVersion) Parse(string? checkpoint)
{
var parts = checkpoint?.Split(':');
return parts is [var name, var number] && Enum.TryParse<Stage>(name, ignoreCase: true, out var stage)
&& int.TryParse(number, NumberStyles.None, CultureInfo.InvariantCulture, out var next)
? (stage == Stage.Done ? Stage.Copy : stage, stage == Stage.Done ? 0 : next)
: (Stage.Proxies, 0);
if (parts is not ([_, _] or [_, _, _]) || !Enum.TryParse<Stage>(parts[0], ignoreCase: true, out var stage) || stage != Stage.Copy
|| !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var next))
return (0, null);
int? version = parts.Length == 3 && int.TryParse(parts[2], NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var v) ? v : null;
return (next, version);
}
private sealed class Tally