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

+121 -20
View File
@@ -13,14 +13,41 @@ namespace Chronos.Data.Workspaces;
/// <summary>The manifest of a workspace folder (<c>workspace.json</c>).</summary>
public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created);
/// <summary>A workspace's sync state of one project besides its versions (<c>sync.json</c>): a restore to record, a conflict to settle.</summary>
/// <summary>
/// A workspace's sync state of one project besides its versions (<c>sync.json</c>): a restore to record, a conflict to
/// settle, and the journal of a replay or a save-both under way, which the next sync finishes or undoes after a crash.
/// </summary>
/// <param name="RestoredFrom">The version a restore brought back; the next commit is a "restore" of it.</param>
/// <param name="Conflict">The conflict a pull or push stopped at, until the user settles it.</param>
public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? Conflict)
/// <param name="Replay">A replay (or a base moved onto local commits the home holds) written in steps; null when none is under way.</param>
/// <param name="SaveBoth">A save-both that made its copy at the home and has not finished; null when none is under way.</param>
public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? Conflict, WorkspaceReplay? Replay = null, WorkspaceSaveBoth? SaveBoth = null)
{
public static WorkspaceSyncState None { get; } = new(null, null);
/// <summary>Nothing to keep: the file can go.</summary>
[JsonIgnore]
public bool IsEmpty => RestoredFrom is null && Conflict is null && Replay is null && SaveBoth is null;
}
/// <summary>
/// The journal of a replay (<see cref="WorkspaceSync"/>): the base it moves the project onto, the local versions it
/// replaces, the replayed ones it writes and the local versions the home already holds (their answer lost), whose files
/// 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);
/// <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.
/// </summary>
public sealed record WorkspaceSaveBoth(Guid CopyId, Guid CopyBaseId, int CopyBaseNumber, Guid? FromBase, IReadOnlyList<WorkspaceMove> Moves);
/// <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);
/// <summary>
/// A pull or push that met real conflicts (owner review 2, R8): the home's head it met, the version the local commits were
/// made from, the local commits (oldest first) and the members both sides changed (mine, theirs). Settled by keeping mine,
@@ -58,7 +85,11 @@ public sealed class WorkspaceStore
/// <summary>The folder name of the workspace inside the program's data folder.</summary>
public const string DefaultFolderName = "workspace";
/// <summary>Opens the workspace at <paramref name="root"/>, making the folder and its manifest when there is none.</summary>
/// <summary>
/// Opens the workspace at <paramref name="root"/>, making the folder and its manifest when there is none. The manifest
/// is written whole to a temporary file and moved into place, so a crash never leaves half of one; a manifest left
/// empty, or unreadable in a folder that holds no project yet (an earlier build's interrupted first write), is made again.
/// </summary>
/// <exception cref="InvalidDataException">The folder's <c>workspace.json</c> is not a workspace manifest, or a newer one.</exception>
/// <exception cref="IOException">The folder cannot be made or read.</exception>
public WorkspaceStore(string root)
@@ -67,20 +98,8 @@ public sealed class WorkspaceStore
Root = LongPath.Normalize(Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)));
Directory.CreateDirectory(Root);
var path = Path.Combine(Root, ManifestFile);
if (!File.Exists(path))
{
var manifest = new WorkspaceManifest(FormatName, FormatVersion, Guid.CreateVersion7(), DateTimeOffset.UtcNow);
// Another program opening the same folder at the same moment: the first manifest wins.
try
{
using var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None);
JsonSerializer.Serialize(stream, manifest, WorkspaceJson.Default.WorkspaceManifest);
stream.Flush(flushToDisk: true);
}
catch (IOException) when (File.Exists(path))
{
}
}
if (!File.Exists(path)) Create(path, replace: false);
else if (Unfinished(path)) Create(path, replace: true);
Manifest = Read(path);
}
@@ -130,6 +149,70 @@ public sealed class WorkspaceStore
return bundles;
}
/// <summary>
/// Writes a new manifest. Without <paramref name="replace"/> another program opening the same folder at the same moment
/// may have made one first: that one wins (the move never replaces a file).
/// </summary>
private static void Create(string path, bool replace)
{
var manifest = new WorkspaceManifest(FormatName, FormatVersion, Guid.CreateVersion7(), DateTimeOffset.UtcNow);
var text = JsonSerializer.Serialize(manifest, WorkspaceJson.Default.WorkspaceManifest);
if (replace)
{
Log.Warning("workspace", $"{path} was left unfinished; a new manifest is written");
AtomicFile.WriteAllText(path, text);
return;
}
var temp = AtomicFile.TempPathFor(path);
try
{
using (var stream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
stream.Write(System.Text.Encoding.UTF8.GetBytes(text));
stream.Flush(flushToDisk: true);
}
try
{
File.Move(temp, path, overwrite: false);
}
catch (IOException) when (File.Exists(path))
{
// Another program's manifest came first.
}
}
finally
{
if (File.Exists(temp)) File.Delete(temp);
}
}
/// <summary>
/// A manifest an interrupted first write left (an earlier build wrote it in place): empty, or not JSON in a folder that
/// holds no project yet. A readable manifest, or any in a folder with projects, is never written over.
/// </summary>
private bool Unfinished(string path)
{
string text;
try
{
text = AtomicFile.ReadAllText(path);
}
catch (IOException)
{
return false;
}
if (string.IsNullOrWhiteSpace(text.Trim('\0'))) return true;
try
{
using var _ = JsonDocument.Parse(text);
return false;
}
catch (JsonException)
{
return List().Count == 0;
}
}
private static WorkspaceManifest Read(string path)
{
WorkspaceManifest? manifest;
@@ -274,7 +357,10 @@ public sealed class WorkspaceProject
internal void WriteVersion(ProjectVersion version, bool replace = false) => ProjectSidecars.WriteVersion(VersionPath(version), version, replace);
/// <summary>Moves a version file aside into <c>versions/.replaced</c> (a local commit replayed under another id).</summary>
/// <summary>
/// Moves a version file aside into <c>versions/.replaced</c> (a local commit replayed under another id), trying again
/// for a moment while another program (an indexer, an antivirus) holds it open.
/// </summary>
internal void SetAside(ProjectVersion version)
{
var path = VersionPath(version);
@@ -283,9 +369,23 @@ public sealed class WorkspaceProject
Directory.CreateDirectory(aside);
var target = Path.Combine(aside, Path.GetFileName(path));
if (File.Exists(target)) target += "." + DateTime.UtcNow.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture);
File.Move(path, target);
for (var attempt = 1; ; attempt++)
{
try
{
File.Move(path, target);
return;
}
catch (IOException) when (attempt < SetAsideAttempts && File.Exists(path) && !File.Exists(target))
{
Thread.Sleep(10 * attempt);
}
}
}
/// <summary>Moves tried, some 1 s in all, before a version file held open fails to move aside.</summary>
private const int SetAsideAttempts = 15;
public WorkspaceSyncState ReadSync()
{
if (!File.Exists(SyncPath)) return WorkspaceSyncState.None;
@@ -301,7 +401,8 @@ public sealed class WorkspaceProject
internal void WriteSync(WorkspaceSyncState state)
{
if (state == WorkspaceSyncState.None || state is { RestoredFrom: null, Conflict: null })
ArgumentNullException.ThrowIfNull(state);
if (state.IsEmpty)
{
if (File.Exists(SyncPath)) File.Delete(SyncPath);
return;