fix(workspace): every cut of a sync is recoverable, renders name only versions the home holds

A push now moves the base before its local file reads as sent; a replay
journals itself in sync.json, writes the replayed versions, then the base
(the commit point), and only then sets the old ones aside and adopts the
local commits the home already held; a conflict met past such commits moves
the base onto them first. A save-both journals its copy and the ids it sends
under, so a retry completes the same copy instead of making another. The
next push, pull or resolve finishes or undoes whatever a cut left, so no
version is orphaned and no false conflict appears.

The editor reads the working state and the media index under the project
lease; workspace.json is written whole and moved into place (an empty one is
made again); project files may hold 64 MiB and one that would be larger is
refused before it is written, so large projects check out. The workspace copy
checks the drive's free space before and during the copy, keeps the folder in
its checkpoint and stores a reset when a pass starts again, and runs at High
level in the priority queue. Renders record a version only when the index
holds it with the settings rendered; an unsent version renders as the project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 10:19:56 +01:00
1 parent 50e28fd63d
commit 833d7cf8e9
18 files changed
+950 -92

No files matched your search

@@ -11,6 +11,7 @@ using Chronos.Data.Archiving;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
namespace Chronos.Data.Workspaces;
@@ -47,7 +48,10 @@ public static class WorkspaceJobs
/// 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.
/// Before each pass and at each checkpoint the workspace's drive must hold what is still to copy plus a reserve, else the
/// job stops saying how much is missing (the workspace shares this PC's drive with the index and the logs).
/// Checkpoint: <c>copy:&lt;frame&gt;:&lt;frames version&gt;:&lt;folder&gt;</c>; a run resumed on other frames or in another folder
/// checks every file again, and a pass started again stores that before it reads anything.
/// Originals come in phase 5c: asked for, the job fails saying so.
/// </summary>
internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService archives) : IJobHandler
@@ -57,6 +61,17 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
private static readonly TimeSpan LockWait = TimeSpan.FromMinutes(2);
private static readonly TimeSpan CheckpointEvery = TimeSpan.FromSeconds(5);
/// <summary>Bytes left free on the workspace's drive besides the copy, as a download keeps (<see cref="SyncTransfers"/>).</summary>
private const long SpaceReserve = 512L << 20;
/// <summary>A proxy at <see cref="ProxyEstimateEdge"/> px, when the archive has none to measure: some 1.5 MB.</summary>
private const long ProxyEstimate = 3L << 19;
private const int ProxyEstimateEdge = 1920;
/// <summary>The long edge taken for a full-size proxy of a frame whose size is not known.</summary>
private const int UnknownFullSize = 6000;
public JobKind Kind => JobKind.WorkspaceCopy;
public async Task RunAsync(JobContext context)
@@ -80,20 +95,29 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
var paths = new ArchivePaths(archive.RootPath);
// 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);
var (copied, checkpointVersion, checkpointFolder) = 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 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;
// Another index moved the sequence away, maybe another one in: its files are not this sequence's photos. A retry
// checks every file again (the files of this run may be of that folder).
if (!SequenceFolders.Holds(folder, sequenceId))
{
await context.CheckpointAsync(Checkpoint(Stage.Copy, 0, row.FramesVersion, row.Folder), 0, frames.Count).ConfigureAwait(false);
throw new SequenceFolderMovedException();
}
// Files kept from an earlier run are this frames version's and this folder's only (a conversion keeps a frame's id,
// not its photo; a sequence moved meanwhile may have left its photos' names to another sequence's).
if ((checkpointVersion is { } version && version != row.FramesVersion) || (checkpointFolder is { } read && read != row.Folder)) copied = 0;
var total = (long)frames.Count;
var tally = new Tally();
var entries = new WorkspaceFrame[frames.Count];
Directory.CreateDirectory(project.ProxiesDirectory);
// The workspace is on this PC's own drive, with the index, the settings and the logs: the copy never fills it.
var remaining = Remaining(project, folder, frames, policy);
EnsureSpace(parameters.WorkspaceRoot, remaining[0]);
var clock = System.Diagnostics.Stopwatch.StartNew();
for (var i = 0; i < frames.Count; i++)
{
@@ -101,7 +125,9 @@ 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, row.FramesVersion), i + 1, total).ConfigureAwait(false);
await context.CheckpointAsync(Checkpoint(Stage.Copy, i + 1, row.FramesVersion, row.Folder), i + 1, total).ConfigureAwait(false);
// Another program may take the space meanwhile.
EnsureSpace(parameters.WorkspaceRoot, remaining[i + 1]);
clock.Restart();
}
else
@@ -114,15 +140,21 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
var moved = after.Folder != row.Folder || !SequenceFolders.Holds(paths.Sequence(after.Folder), sequenceId) || Renamed(frames, now, entries);
if (after.FramesVersion != row.FramesVersion || moved)
{
// The files read may be of other frames, or missing where a Reorganize took them: read them again.
// The files read may be of other frames, or missing where a Reorganize took them: read them again, and so
// does a run resumed after a crash in the next pass (the checkpoint stored says so).
if (pass < Passes)
{
context.Log(Loc.L("Archive.Log.WorkspaceFramesChanged", row.DisplayTitle));
(copied, checkpointVersion) = (0, null);
await context.CheckpointAsync(Checkpoint(Stage.Copy, 0, after.FramesVersion, after.Folder), 0, total).ConfigureAwait(false);
(copied, checkpointVersion, checkpointFolder) = (0, null, 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();
if (moved)
{
await context.CheckpointAsync(Checkpoint(Stage.Copy, 0, after.FramesVersion, after.Folder), 0, total).ConfigureAwait(false);
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,
@@ -134,7 +166,7 @@ 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, row.FramesVersion), total, total).ConfigureAwait(false);
await context.CheckpointAsync(Checkpoint(Stage.Done, frames.Count, row.FramesVersion, row.Folder), total, total).ConfigureAwait(false);
return;
}
}
@@ -205,6 +237,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
}
if (bytes is null)
{
// A file an earlier copy left for the frame (of other frames, or another folder's) is never kept as its photo by a
// resumed run: a regenerable copy, it goes.
if (File.Exists(target)) TryDelete(target);
tally.Missing++;
return WorkspaceFrame.Of(frame, null, null, 0);
}
@@ -292,15 +327,81 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
var listed = media.Frames.Where(f => f.File is not null).Select(f => Path.GetFileName(f.File!)).ToHashSet(StringComparer.OrdinalIgnoreCase);
foreach (var file in Directory.EnumerateFiles(project.ProxiesDirectory, "*.jpg"))
{
if (listed.Contains(Path.GetFileName(file))) continue;
try
{
File.Delete(file);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("workspace", $"{file} could not be removed", ex);
}
if (!listed.Contains(Path.GetFileName(file))) TryDelete(file);
}
}
private static void TryDelete(string file)
{
try
{
File.Delete(file);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("workspace", $"{file} could not be removed", ex);
}
}
/// <summary>
/// The bytes the copy still writes from each frame on (index <c>i</c>: frames <c>i</c> to the last; one more entry, 0):
/// nothing for a frame the workspace holds a file of already (kept, or replaced by one of about its size), the archive's
/// proxy's size where it has one, else an estimate at the proxy size the copy makes (<see cref="Estimate"/>).
/// </summary>
private static long[] Remaining(WorkspaceProject project, SequencePaths folder, List<Frame> frames, ProxyPolicy policy)
{
var held = Directory.Exists(project.ProxiesDirectory)
? Directory.EnumerateFiles(project.ProxiesDirectory).Select(Path.GetFileName).ToHashSet(StringComparer.OrdinalIgnoreCase)
: [];
var proxies = Directory.Exists(folder.Proxies)
? new DirectoryInfo(folder.Proxies).EnumerateFiles().ToDictionary(f => f.Name, f => f.Length, StringComparer.OrdinalIgnoreCase)
: [];
var remaining = new long[frames.Count + 1];
for (var i = frames.Count - 1; i >= 0; i--)
{
var frame = frames[i];
var bytes = held.Contains(ProxyCache.FileName(frame.Id)) ? 0
: proxies.TryGetValue(ArchiveLayout.ProxyFileName(frame.Index), out var length) ? length
: (frame.Flags & FrameFlags.Missing) != 0 ? 0
: Estimate(frame, policy);
remaining[i] = remaining[i + 1] + bytes;
}
return remaining;
}
/// <summary>A proxy's size when the archive has none to measure: some 1.5 MB at 1920 px, scaled by the area at the copy's long edge.</summary>
private static long Estimate(Frame frame, ProxyPolicy policy)
{
var edge = policy.LongEdge == ProxyPolicy.Disabled ? new ProxyPolicy().LongEdge : policy.LongEdge;
var longest = Math.Max(frame.Width, frame.Height);
var side = edge == ProxyPolicy.FullSize ? (longest > 0 ? longest : UnknownFullSize) : longest > 0 ? Math.Min(edge, longest) : edge;
return (long)(ProxyEstimate * Math.Pow(side / (double)ProxyEstimateEdge, 2));
}
/// <summary>
/// Refuses to go on while the workspace's drive lacks <paramref name="bytes"/> besides <see cref="SpaceReserve"/>, with the
/// gigabytes missing; a drive that cannot be measured (a network share) is not refused, the copy itself then says so.
/// </summary>
/// <exception cref="IOException">The drive lacks the space (<c>Errors.Workspace.DiskFull</c>).</exception>
private void EnsureSpace(string root, long bytes)
{
if (bytes <= 0 || FreeBytes(root) is not { } free || free >= bytes + SpaceReserve) return;
throw new IOException(Loc.L("Errors.Workspace.DiskFull", ((bytes + SpaceReserve - free) / 1e9).ToString("0.0", Loc.Culture)));
}
/// <summary>Free bytes on the volume of <paramref name="folder"/>, as the host measures them (<see cref="ArchiveService.FreeSpace"/>), else its drive's.</summary>
private long? FreeBytes(string folder)
{
if (archives.FreeSpace is { } measure) return measure(folder);
try
{
var full = Path.GetFullPath(folder);
// Windows wants a drive root; Unix accepts any directory and reports the volume it is mounted on.
return new DriveInfo(OperatingSystem.IsWindows() ? Path.GetPathRoot(full)! : full).AvailableFreeSpace;
}
catch (Exception ex) when (ex is ArgumentException or IOException or UnauthorizedAccessException)
{
return null;
}
}
@@ -361,22 +462,23 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
Done,
}
private static string Checkpoint(Stage stage, int frame, int framesVersion) =>
string.Create(CultureInfo.InvariantCulture, $"{stage.ToString().ToLowerInvariant()}:{frame}:{framesVersion}");
private static string Checkpoint(Stage stage, int frame, int framesVersion, string folder) =>
string.Create(CultureInfo.InvariantCulture, $"{stage.ToString().ToLowerInvariant()}:{frame}:{framesVersion}:{folder}");
/// <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.
/// The frames copied by an earlier run (<c>copy:&lt;frame&gt;[:&lt;frames version&gt;[:&lt;folder&gt;]]</c>, the folder
/// last as it may hold a colon), the frames version and the sequence's folder 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)
private static (int Copied, int? FramesVersion, string? Folder) Parse(string? checkpoint)
{
var parts = checkpoint?.Split(':');
if (parts is not ([_, _] or [_, _, _]) || !Enum.TryParse<Stage>(parts[0], ignoreCase: true, out var stage) || stage != Stage.Copy
var parts = checkpoint?.Split(':', 4);
if (parts is not ([_, _] or [_, _, _] 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);
return (0, null, null);
int? version = parts.Length >= 3 && int.TryParse(parts[2], NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var v) ? v : null;
return (next, version, parts.Length == 4 ? parts[3] : null);
}
private sealed class Tally