fix(workspace): finish or keep every choice, copy and sync a cut or a deleted project leaves

- A take of the library's values cut past its commit point settles its conflict
  (replay journal marks Settles); a later stale choice finds nothing to settle,
  and one cut before it keeps the conflict so the next choice is applied.
- A local version that changes nothing once replayed on the head is not sent
  nor written as a Merge; its file stays in versions/.replaced.
- Save both journals its intent (name, base, time) before the home is asked,
  so a cut after the copy was made reuses that copy on retry.
- A project deleted at the home reads as Gone from pull, push, resolve and
  save both, logged once, without retry loops; versions and working state stay.
- Save both keeps the restore recorded in sync.json.
- The copy job writes a photo's new bytes under a name of its own while the
  editor's media still name the old file, switching at the commit; a cancelled
  first copy removes its own half media under the lease (VM hook removed).
- Status reads the version files once, re-reads the home's frames only when
  its stamp moved, reports damage only when confirmed under the lease, and
  waits once for a busy lease before calling the media outdated.
- versions/.replaced is pruned after a sync that ended well (30 days, newest
  20 kept, never a file a journal names).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 13:06:19 +01:00
1 parent 488b588ad9
commit 5eeb64b680
14 files changed
+1183 -216

No files matched your search

@@ -65,7 +65,8 @@ public static class WorkspaceJobs
/// 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.
/// checks every file again, and a pass started again stores that before it reads anything. A first copy the user cancels
/// removes the media it wrote itself, once it has stopped writing and under the project's lease (<see cref="RemovePartialMediaAsync"/>).
/// Originals come in phase 5c: asked for, the job fails saying so.
/// </summary>
internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService archives) : IJobHandler
@@ -106,6 +107,46 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
if (project.ReadBundle().Workspace is not { Home: { IsArchive: true } home } || home.SequenceId != sequenceId)
throw new InvalidOperationException(Loc.L("Errors.Workspace.OtherHome"));
try
{
await CopyAsync(context, project, sequenceId, archive, parameters, ct).ConfigureAwait(false);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
// Stopped writing: what a first copy the user cancelled wrote goes (a pause or a shutdown resumes it).
await RemovePartialMediaAsync(context.Job, project).ConfigureAwait(false);
throw;
}
}
/// <summary>
/// A first copy of the project's media the user cancelled (the job reads <see cref="JobState.Cancelled"/>), once it has
/// stopped writing: the photos it wrote (copies the home can make again) are removed under the project's lease, so the
/// workspace holds no half media. Media that a copy ever finished (<c>media.json</c>) stay, as do those of a copy paused,
/// yielded or stopped with the program, which goes on from its checkpoint. A failure is logged: the next copy checks
/// every file it finds.
/// </summary>
private async Task RemovePartialMediaAsync(Job job, WorkspaceProject project)
{
try
{
if (await db.Jobs.GetAsync(job.Id, CancellationToken.None).ConfigureAwait(false) is not { State: JobState.Cancelled }) return;
using (await project.LockAsync(LockWait, CancellationToken.None).ConfigureAwait(false))
{
if (File.Exists(project.MediaIndexPath) || !Directory.Exists(project.MediaDirectory)) return;
Directory.Delete(project.MediaDirectory, recursive: true);
}
Log.Info("workspace", $"The media a cancelled first copy of project {project.ProjectId} wrote were removed");
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException)
{
Log.Warning("workspace", $"The media of a cancelled copy of project {project.ProjectId} could not be removed", ex);
}
}
private async Task CopyAsync(JobContext context, WorkspaceProject project, Guid sequenceId, Archive archive, WorkspaceCopyParameters parameters,
CancellationToken ct)
{
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);
@@ -221,8 +262,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
/// <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.
/// The file the media the editor reads now name for the frame (<paramref name="current"/>) is never removed here: the
/// editor keeps it while the copy runs and if it fails, and a copy that commits without it removes it then
/// The file the media the editor reads now name for the frame (<paramref name="current"/>) is never removed nor written
/// over here: the editor keeps it while the copy runs and if it fails, other bytes for it go under a name of their own
/// (<see cref="WorkspaceProject.ProxyName(Guid, string)"/>), and a copy that commits without it removes it then
/// (<see cref="RemoveStale"/>). Nor is it taken as this run's copy of a frame whose photo changed since.
/// </summary>
private WorkspaceFrame Copy(WorkspaceProject project, SequencePaths folder, ArchivePaths paths, Frame frame, ProxyPolicy policy, bool done,
@@ -269,17 +311,41 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
return WorkspaceFrame.Of(frame, null, null, 0);
}
var sha = Sha(bytes);
if (File.Exists(target) && Sha(AtomicFile.ReadAllBytes(target)) == sha)
// The file the editor's media name for the frame holds these bytes already (under its own name, maybe): kept.
if (current?.File is { } held && !string.Equals(held, name, StringComparison.OrdinalIgnoreCase)
&& string.Equals(current.FileSha256, sha, StringComparison.OrdinalIgnoreCase) && Holds(project.MediaFile(held), sha))
{
tally.Kept++;
return WorkspaceFrame.Of(frame, held, sha, bytes.Length);
}
if (Holds(target, sha))
{
tally.Kept++;
return WorkspaceFrame.Of(frame, name, sha, bytes.Length);
}
// Other bytes for the file the editor's media name (a conversion since): written under a name of their own, never over
// the editor's file, which the commit switches the media from (RemoveStale then removes it). A copy that fails before
// its commit leaves the editor reading the bytes its entry names. A file that no longer holds them (damaged, gone)
// is mended where it is.
if (listed && current!.FileSha256 is { } editors && Holds(target, editors.ToLowerInvariant()))
{
name = WorkspaceProject.ProxyName(frame.Id, sha);
target = project.MediaFile(name);
if (Holds(target, sha))
{
tally.Kept++;
return WorkspaceFrame.Of(frame, name, sha, bytes.Length);
}
}
WriteVerified(target, bytes, sha);
if (made) tally.Made++;
else tally.Copied++;
return WorkspaceFrame.Of(frame, name, sha, bytes.Length);
}
/// <summary>Whether the file at <paramref name="path"/> is there and hashes as <paramref name="sha"/>.</summary>
private static bool Holds(string path, string sha) => File.Exists(path) && Sha(AtomicFile.ReadAllBytes(path)) == sha;
/// <summary>Writes atomically and reads back: a file that does not hash as its source is written again once, then refused.</summary>
private static void WriteVerified(string path, byte[] bytes, string sha)
{