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

+103 -5
View File
@@ -36,14 +36,35 @@ public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? C
/// take the home's copy. The bundle's base is the commit point: once it is <see cref="OntoBase"/> the replay is finished
/// (the replaced set aside, the held ones adopted), before it the replayed ones are set aside and nothing else moved.
/// </summary>
public sealed record WorkspaceReplay(Guid OntoBase, IReadOnlyList<Guid> Replaced, IReadOnlyList<Guid> Replayed, IReadOnlyList<Guid> Adopted);
/// <param name="Settles">
/// The replay settles the conflict recorded beside it (a pull, a push or a choice that merged): once its base is
/// <see cref="OntoBase"/> that conflict is over, the choice made applied. False for a base moved onto versions the home
/// held, which leaves the conflict to settle (and for a journal an earlier build wrote).
/// </param>
public sealed record WorkspaceReplay(Guid OntoBase, IReadOnlyList<Guid> Replaced, IReadOnlyList<Guid> Replayed, IReadOnlyList<Guid> Adopted,
bool Settles = false);
/// <summary>
/// A save-both under way (<see cref="WorkspaceSync"/>): the copy made at the home and its first version (the base the
/// local versions are sent on top of), the base this project had when it started, and the id each local version is sent
/// under, so a retry sends to the same copy what it lacks and the home answers "already stored" for the rest.
/// under, so a retry sends to the same copy what it lacks and the home answers "already stored" for the rest. Before the
/// copy is asked of the home its name and the version it is made from are journaled (<see cref="Intent"/>, the copy's ids
/// still empty): a retry after a cut finds the copy the home made by them instead of making another.
/// </summary>
public sealed record WorkspaceSaveBoth(Guid CopyId, Guid CopyBaseId, int CopyBaseNumber, Guid? FromBase, IReadOnlyList<WorkspaceMove> Moves);
public sealed record WorkspaceSaveBoth(Guid CopyId, Guid CopyBaseId, int CopyBaseNumber, Guid? FromBase, IReadOnlyList<WorkspaceMove> Moves,
WorkspaceCopyIntent? Intent = null)
{
/// <summary>The copy is asked of the home but not known yet: only its <see cref="Intent"/> is.</summary>
[JsonIgnore]
public bool Intended => CopyId == Guid.Empty;
}
/// <summary>
/// The copy a save-both asks the home for, journaled before it asks: its name, the version it is made from (null: Main's
/// head) with that version's content hash, and when it was asked. A project of that name made since, whose only version
/// holds that content, is that copy.
/// </summary>
public sealed record WorkspaceCopyIntent(string Name, Guid? From, string? FromHash, DateTimeOffset Asked);
/// <summary>A local version (<paramref name="LocalId"/>) and the id it is sent under to a save-both's copy.</summary>
public sealed record WorkspaceMove(Guid LocalId, Guid CopyVersionId);
@@ -261,7 +282,10 @@ public sealed class WorkspaceProject
public const string ExportsFolder = "exports";
public const string SyncFile = "sync.json";
/// <summary>Old local commits replaced by their replay on top of the home's head: kept, never erased by Chronos.</summary>
/// <summary>
/// Old local commits replaced by their replay on top of the home's head, files the home's copy replaced, and the working
/// states a take of the server's values replaced: kept up to a month, the newest 20 at most (<see cref="PruneReplaced"/>).
/// </summary>
public const string ReplacedFolder = ".replaced";
private const string LeaseName = ".lock";
@@ -291,6 +315,16 @@ public sealed class WorkspaceProject
/// <summary>The file a frame's proxy is copied to, relative to <see cref="MediaDirectory"/>.</summary>
public static string ProxyName(Guid frameId) => ProxiesFolder + "/" + ProxyCache.FileName(frameId);
/// <summary>
/// The file a frame's proxy of other bytes is copied to while the media name <see cref="ProxyName(Guid)"/> for the frame
/// (a conversion since): the editor keeps reading that one until the copy commits.
/// </summary>
public static string ProxyName(Guid frameId, string sha256)
{
ArgumentException.ThrowIfNullOrEmpty(sha256);
return ProxiesFolder + "/" + frameId.ToString("N") + "-" + sha256[..Math.Min(16, sha256.Length)].ToLowerInvariant() + ".jpg";
}
/// <summary>The absolute path of a file named relative to <see cref="MediaDirectory"/>.</summary>
public string MediaFile(string relative) => LongPath.FromPortableRelative(MediaDirectory, relative);
@@ -358,6 +392,12 @@ public sealed class WorkspaceProject
/// </summary>
public IReadOnlyList<UnreadableVersion> UnreadableVersions() => ReadVersions(log: false).Unreadable;
/// <summary>
/// <see cref="Versions"/> and <see cref="UnreadableVersions"/> from one read of the folder: each file (up to
/// <see cref="ProjectSidecars.MaxVersionBytes"/>) is parsed once.
/// </summary>
internal (IReadOnlyList<ProjectVersion> Versions, IReadOnlyList<UnreadableVersion> Unreadable) ReadVersionFiles() => ReadVersions(log: true);
private (List<ProjectVersion> Versions, List<UnreadableVersion> Unreadable) ReadVersions(bool log)
{
var versions = new List<ProjectVersion>();
@@ -410,13 +450,15 @@ public sealed class WorkspaceProject
try
{
File.Move(path, target);
return;
break;
}
catch (IOException) when (attempt < SetAsideAttempts && File.Exists(path) && !File.Exists(target))
{
Thread.Sleep(10 * attempt);
}
}
// Dated as set aside, which the pruning goes by (a move keeps the time the version was written).
TryTouch(target);
}
/// <summary>
@@ -429,6 +471,62 @@ public sealed class WorkspaceProject
ProjectSidecars.WriteVersion(Path.Combine(VersionsDirectory, ReplacedFolder, Path.GetFileName(VersionPath(version))), version);
}
/// <summary>Files <see cref="PruneReplaced"/> keeps in <c>versions/.replaced</c> whatever their age.</summary>
public const int ReplacedKept = 20;
/// <summary>The age past which <see cref="PruneReplaced"/> removes a file of <c>versions/.replaced</c>.</summary>
public static readonly TimeSpan ReplacedMaxAge = TimeSpan.FromDays(30);
/// <summary>
/// Removes from <c>versions/.replaced</c> the files set aside more than <see cref="ReplacedMaxAge"/> ago and those past
/// the newest <see cref="ReplacedKept"/>, never one of a version in <paramref name="referenced"/> (a journal's). Every
/// file there is a copy of what the chain or the home holds, or what a take of the server's values replaced (its undo):
/// called only after a sync that ended well. How many went.
/// </summary>
internal int PruneReplaced(IReadOnlySet<Guid> referenced, DateTime nowUtc)
{
ArgumentNullException.ThrowIfNull(referenced);
var aside = Path.Combine(VersionsDirectory, ReplacedFolder);
if (!Directory.Exists(aside)) return 0;
var files = new DirectoryInfo(aside).EnumerateFiles().OrderByDescending(f => f.LastWriteTimeUtc).ThenByDescending(f => f.Name, StringComparer.Ordinal).ToList();
var removed = 0;
for (var i = 0; i < files.Count; i++)
{
var file = files[i];
if (i < ReplacedKept && nowUtc - file.LastWriteTimeUtc <= ReplacedMaxAge) continue;
if (VersionIdOf(file.Name) is { } id && referenced.Contains(id)) continue;
try
{
file.Delete();
removed++;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("workspace", $"{file.FullName} could not be removed", ex);
}
}
return removed;
// "<number>-<id>.json", maybe with a suffix a second file of the same version took.
static Guid? VersionIdOf(string name)
{
var end = name.IndexOf(".json", StringComparison.OrdinalIgnoreCase);
return end > 0 && ProjectSidecars.TryParseName(name[..(end + 5)], out _, out var id) ? id : null;
}
}
private static void TryTouch(string path)
{
try
{
if (File.Exists(path)) File.SetLastWriteTimeUtc(path, DateTime.UtcNow);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Debug("workspace", $"{path} keeps the time it was written: {ex.Message}");
}
}
/// <summary>Moves tried, some 1 s in all, before a version file held open fails to move aside.</summary>
private const int SetAsideAttempts = 15;