merge: review2-p5a-fix-workspace into phase 5a fix round 1 integration

# Conflicts:
#	src/Chronos.Data/Workspaces/WorkspaceSync.cs
This commit is contained in:
Alby96 committed 2026-11-03 10:39:19 +01:00
commit 9772c2f934
18 files changed
+958 -93

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,
@@ -67,7 +94,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)
@@ -76,20 +107,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);
}
@@ -139,6 +158,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;
@@ -283,7 +366,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);
@@ -292,9 +378,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;
@@ -310,7 +410,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;