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

+1 -17
View File
@@ -703,6 +703,7 @@ public sealed class SequenceViewModel : SectionViewModel
{
if (!ReferenceEquals(fetch, _fetch) || job.Id != fetch.JobId || job.State is not (JobState.Done or JobState.Failed or JobState.Cancelled)) return;
StopFetch();
// A first copy of a project's media the user cancelled removes what it wrote itself, once it has stopped (the job).
switch (job.State)
{
// Opened only when it is still the sequence asked for; otherwise the next visit opens it (from the cache, from the workspace).
@@ -714,23 +715,6 @@ public sealed class SequenceViewModel : SectionViewModel
Shell.Notify(Loc.L(fetch.ProjectId is null ? "Editor.Proxies.Failed" : "Editor.Workspace.CopyFailed"), job.Error ?? fetch.Title, Loc.L("Shell.Nav.Jobs"),
Loc.L("Tooltips.Nav.Jobs"), () => Shell.Navigate(SectionKey.Jobs), isError: true);
break;
// The user cancelled the first copy of a project's media: the photos it wrote go (a failed one keeps them for its retry).
case JobState.Cancelled when fetch.ProjectId is { } cancelled:
_ = RemovePartialMediaAsync(cancelled);
break;
}
}
private async Task RemovePartialMediaAsync(Guid projectId)
{
try
{
await Shell.Services.Workspace.RemovePartialMediaAsync(projectId);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Copies the library can make again: they wait for the next copy, which checks each.
Log.Warning("workspace", $"The media of a cancelled copy of project {projectId} could not be removed", ex);
}
}
@@ -126,8 +126,12 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
/// <summary>The button's accessible name: the project and its state.</summary>
public string MenuName => string.Join(Loc.Separator, Name, StateText);
/// <summary>The home's frames moved past those the workspace holds: the banner offers Update media.</summary>
public bool HasMediaUpdate => _homeFramesChanged;
/// <summary>
/// The home's frames moved past those the workspace holds, or the library names photos otherwise since the copy (hashed,
/// converted) and the media could not learn it yet (the last read of the library says so; a later one that taught them
/// clears it): the banner offers Update media.
/// </summary>
public bool HasMediaUpdate => _homeFramesChanged || _status is { MediaOutdated: true };
/// <summary>A copy of the media runs (Update media): the banner says so.</summary>
public bool IsUpdatingMedia => _copyJob != Guid.Empty;
@@ -170,8 +174,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
var status = await _host.StatusAsync(ProjectId);
var moved = await _source.HomeFramesChangedAsync(CancellationToken.None);
if (_disposed) return;
// Also photos the library named otherwise since the copy (hashed, converted) while the media could not learn it.
_homeFramesChanged = moved || status.MediaOutdated;
_homeFramesChanged = moved;
Apply(status);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
@@ -236,7 +239,8 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
if (_disposed) return;
Apply(result.Status);
await TakeAsync(result);
if (result.Status.Conflict is { } conflict) AskConflict(conflict, background: quiet);
if (result.Status.State == WorkspaceState.Gone) _ui.Notify(Loc.L("Editor.Workspace.State.Gone"), Loc.L("Errors.Workspace.DeletedAtHome"));
else if (result.Status.Conflict is { } conflict) AskConflict(conflict, background: quiet);
else if (!quiet) _ui.Notify(Loc.L(result.Status.State == WorkspaceState.InSync ? "Editor.Workspace.InSync" : "Editor.Workspace.Synced"), Name);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
@@ -615,7 +619,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
var gone = last.State == WorkspaceState.Gone;
// A base that moved since (a sync) leaves how far the library is ahead unknown until it is asked again.
var behind = last.Base?.VersionId == local.Base?.VersionId ? last.Behind : 0;
var status = local with { Behind = behind, Offline = last.Offline, HomeHead = last.HomeHead };
var status = local with { Behind = behind, Offline = last.Offline, HomeHead = last.HomeHead, MediaOutdated = last.MediaOutdated };
var state = gone ? WorkspaceState.Gone
: status.MediaRemoved ? WorkspaceState.MediaRemoved
: status.Damaged > 0 ? WorkspaceState.Damaged
+3 -19
View File
@@ -241,23 +241,6 @@ public sealed class WorkspaceHost
}, cancellationToken);
}
/// <summary>
/// A first copy of a project's media cancelled before it ended: the photos it wrote (copies the home can make again)
/// are removed, so the workspace holds no half media. A project whose copy ended once keeps its media (Update media
/// cancelled leaves the earlier ones in use). False when there was nothing to remove.
/// </summary>
public Task<bool> RemovePartialMediaAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
{
if (Store.Find(projectId) is not { } project) return false;
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
{
if (File.Exists(project.MediaIndexPath) || !Directory.Exists(project.MediaDirectory)) return false;
Directory.Delete(project.MediaDirectory, recursive: true);
}
Log.Info("workspace", $"The media a cancelled first copy of project {projectId} wrote were removed");
return true;
}, cancellationToken);
/// <summary>The project of <paramref name="sequenceId"/> opened last here, else the one saved last; null when the workspace has none.</summary>
public async Task<WorkspaceEntry?> LastUsedAsync(Guid sequenceId, CancellationToken cancellationToken = default)
{
@@ -404,7 +387,8 @@ public sealed class WorkspaceHost
{
var home = HomeOfProject(projectId);
var pulled = await Sync.PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
if (pulled.Status.State == WorkspaceState.Conflict) return pulled;
// Deleted at the library: nothing to send, nothing to prune; the copy stays as it is.
if (pulled.Status.State is WorkspaceState.Conflict or WorkspaceState.Gone) return pulled;
var pushed = await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
await PruneQuietlyAsync(projectId, home, cancellationToken).ConfigureAwait(false);
Log.Info("workspace", $"Project {projectId} synced: {pushed.Pushed.Count} sent, {pulled.Pulled.Count + pushed.Pulled.Count} taken, {pushed.Status.State}");
@@ -682,7 +666,7 @@ public sealed class WorkspaceHost
private async Task<WorkspaceSyncResult> SendAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken)
{
var pulled = await Sync.PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
return pulled.Status.State == WorkspaceState.Conflict ? pulled : await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
return pulled.Status.State is WorkspaceState.Conflict or WorkspaceState.Gone ? pulled : await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
}
/// <summary>The home holds this copy's work: its head and the base this copy was last in step with read back, each matching its hash.</summary>
+19
View File
@@ -59,6 +59,13 @@ public interface IProjectHome
/// <summary>The sequence's frames as the home holds them now.</summary>
Task<HomeFrames> FramesAsync(CancellationToken cancellationToken);
/// <summary>
/// A cheap stamp of what <see cref="FramesAsync"/> answers: another value whenever the frames, how their photos are
/// named (a hash learnt) or the encodings conversions joined may have changed, so a workspace reads the frames again only
/// then. Null from a home that keeps none: the frames are read every time.
/// </summary>
Task<string?> FramesStampAsync(CancellationToken cancellationToken) => Task.FromResult<string?>(null);
}
/// <summary>A sequence of this PC's archive as its projects' home: the archive's <see cref="ProjectService"/>.</summary>
@@ -139,6 +146,18 @@ public sealed class ArchiveProjectHome(ChronosDatabase db, ProjectService projec
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => projects.FramesAsync(sequenceId, cancellationToken);
/// <summary>
/// The sequence's frames version (frames added, removed, converted or renamed), with how many of its photos the index has
/// hashed (a Verify hashes them without another frames version) and how many removals it keeps (a conversion's links).
/// </summary>
public async Task<string?> FramesStampAsync(CancellationToken cancellationToken)
{
var row = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
var (hashed, removals) = await db.Frames.CountNamedAsync(sequenceId, cancellationToken).ConfigureAwait(false);
return string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{row.FramesVersion}:{hashed}:{removals}");
}
/// <summary>
/// The project is one of this sequence's. One the index does not know yet (an index taken back from an older backup, a
/// sequence back from the trash, a project another program sharing the archive made) is looked for in the sequence's
@@ -238,6 +238,19 @@ public sealed class FrameRepository
return await session.ExecuteAsync("DELETE FROM frames WHERE sequence_id = $1 AND idx < 0", [sequenceId], cancellationToken).ConfigureAwait(false);
}, cancellationToken);
/// <summary>
/// How many photos of a sequence the index has hashed, and how many removals (tombstones) it keeps for it: with the
/// frames version, what decides how project positions name its photos (<see cref="Projects.IProjectHome.FramesStampAsync"/>).
/// </summary>
public Task<(long Hashed, long Removals)> CountNamedAsync(Guid sequenceId, CancellationToken cancellationToken = default) =>
_db.RunAsync(async session =>
{
var row = await session.QuerySingleAsync(
"SELECT (SELECT count(sha256) FROM frames WHERE sequence_id = $1), (SELECT count(*) FROM frame_removals WHERE sequence_id = $1)",
[sequenceId], cancellationToken).ConfigureAwait(false);
return (row.GetInt64(0), row.GetInt64(1));
}, cancellationToken);
/// <summary>Number of frames of a sequence.</summary>
public async Task<int> CountBySequenceAsync(Guid sequenceId, CancellationToken cancellationToken = default) =>
(int)await _db.ScalarAsync<long>("SELECT count(*) FROM frames WHERE sequence_id = $1", [sequenceId], cancellationToken).ConfigureAwait(false);
@@ -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)
{
+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;
+406 -143
View File
@@ -35,7 +35,8 @@ public enum WorkspaceState
/// <summary>
/// The home answered that it no longer has the project or its sequence (the sequence left its index): nothing more can be
/// sent, and the local copy can only be removed, its work kept aside. Told by the desktop, which asks the home.
/// sent, and the local copy can only be removed, its work kept aside. Told by a status asked of the home, and by a sync
/// or a choice that met it (nothing here is changed by it).
/// </summary>
Gone,
@@ -258,7 +259,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// <summary>
/// Pushes the local versions in order. When the home moved meanwhile, its versions are pulled and the local ones
/// replayed on top of them first; a real conflict stops the push (<see cref="WorkspaceState.Conflict"/>).
/// replayed on top of them first; a real conflict stops the push (<see cref="WorkspaceState.Conflict"/>). A project the
/// home no longer has reads as <see cref="WorkspaceState.Gone"/>, its versions kept here. A push that ended well prunes
/// <c>versions/.replaced</c> (<see cref="WorkspaceProject.PruneReplaced"/>).
/// </summary>
/// <exception cref="HomeUnavailableException">The home cannot be reached; nothing was lost, push again later.</exception>
/// <exception cref="WorkspaceSyncException">The home refused a version for good.</exception>
@@ -268,45 +271,55 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var project = Existing(projectId);
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
{
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
var pushed = new List<ProjectVersion>();
var pulled = new List<ProjectVersion>();
for (var attempt = 0; attempt < PushAttempts; attempt++)
try
{
var bundle = project.ReadBundle();
if (project.ReadSync().Conflict is not null) break;
var diverged = false;
var locals = Unpushed(project, bundle);
EnsureChained(project, bundle, locals);
foreach (var local in locals)
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
for (var attempt = 0; attempt < PushAttempts; attempt++)
{
var result = await home.PushAsync(projectId, local, cancellationToken).ConfigureAwait(false);
switch (result.Outcome)
var bundle = project.ReadBundle();
if (project.ReadSync().Conflict is not null) break;
var diverged = false;
var locals = Unpushed(project, bundle);
EnsureChained(project, bundle, locals);
foreach (var local in locals)
{
case PushOutcome.Stored or PushOutcome.AlreadyStored when result.Version is { } stored:
Verify(stored);
// The base moves first: cut after it, the local file still reads as unsent and the next sync
// adopts it; cut before it, the home answers "already stored" next time. Marked sent first, it
// would leave the chain from the old base and take the versions after it along.
bundle = WithBase(bundle, stored);
project.WriteBundle(bundle);
Step("push.base");
Adopt(project, local, stored);
pushed.Add(stored);
continue;
case PushOutcome.Diverged:
diverged = true;
break;
default:
throw Refused(result.Outcome, local, $"project {projectId}");
var result = await home.PushAsync(projectId, local, cancellationToken).ConfigureAwait(false);
switch (result.Outcome)
{
case PushOutcome.Stored or PushOutcome.AlreadyStored when result.Version is { } stored:
Verify(stored);
// The base moves first: cut after it, the local file still reads as unsent and the next sync
// adopts it; cut before it, the home answers "already stored" next time. Marked sent first, it
// would leave the chain from the old base and take the versions after it along.
bundle = WithBase(bundle, stored);
project.WriteBundle(bundle);
Step("push.base");
Adopt(project, local, stored);
pushed.Add(stored);
continue;
case PushOutcome.Diverged:
diverged = true;
break;
default:
throw Refused(result.Outcome, local, $"project {projectId}");
}
break;
}
break;
if (!diverged) break;
var (_, conflict, taken) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
pulled.AddRange(taken);
if (conflict is not null) break;
}
if (!diverged) break;
var (_, conflict, taken) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
pulled.AddRange(taken);
if (conflict is not null) break;
}
catch (ProjectException ex) when (NotFoundAtHome(ex))
{
// Deleted at the home: no failure to retry but the state the project is in (Gone); nothing here moves.
if (!await GoneAsync(project, home, ex, cancellationToken).ConfigureAwait(false)) throw;
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), pushed, pulled);
}
PruneReplaced(project);
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), pushed, pulled);
}
}
@@ -315,7 +328,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// Takes the home's versions newer than the base: the working state (and any local versions, replayed) moves onto the
/// home's head; a real conflict stops it (<see cref="WorkspaceState.Conflict"/>). A conflict recorded before is worked
/// out again (the home may have moved on, this side may have been edited since), so what is asked is what differs now;
/// one that no longer conflicts is settled by the merge.
/// one that no longer conflicts is settled by the merge. A project the home no longer has reads as
/// <see cref="WorkspaceState.Gone"/>, its copy here left as it is.
/// </summary>
/// <exception cref="HomeUnavailableException">The home cannot be reached.</exception>
public async Task<WorkspaceSyncResult> PullAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken = default)
@@ -324,20 +338,29 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var project = Existing(projectId);
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
{
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
IReadOnlyList<ProjectVersion> pulled = [];
var bundle = project.ReadBundle();
EnsureChained(project, bundle, Unpushed(project, bundle));
if (project.ReadSync().Conflict is not null)
try
{
(_, var conflict, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
var bundle = project.ReadBundle();
EnsureChained(project, bundle, Unpushed(project, bundle));
if (project.ReadSync().Conflict is not null)
{
(_, var conflict, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
}
else
{
var head = await home.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
if (head.Id != bundle.Workspace!.Base?.VersionId) (_, _, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
}
}
else
catch (ProjectException ex) when (NotFoundAtHome(ex))
{
var head = await home.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
if (head.Id != bundle.Workspace!.Base?.VersionId) (_, _, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
if (!await GoneAsync(project, home, ex, cancellationToken).ConfigureAwait(false)) throw;
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled);
}
if (project.ReadSync().Conflict is null) PruneReplaced(project);
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled);
}
}
@@ -346,16 +369,19 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// Settles the conflict a pull or push stopped at, as the user was shown it (<paramref name="shown"/>, else the one
/// recorded). <see cref="ConflictChoice.KeepMine"/> keeps this workspace's values (but for the members in
/// <paramref name="takeServer"/>), <see cref="ConflictChoice.TakeServer"/> takes the server's on the settings listed;
/// either way the local versions are replayed on the home's head and wait to be pushed. A choice applies only to the
/// conflicts listed: when what differs now is not that (the home moved on, or this side was edited meanwhile), nothing
/// is settled, and the conflict found now is recorded and returned to be asked again; so is one the home holds no
/// versions for now (nothing to take them from). Taking the server's keeps the working state it replaces as a version
/// file in <c>versions/.replaced</c>, outside the chain: nothing is committed, replayed or sent for it, so a take that
/// leaves only the server's values sends nothing. After a Save both cut short, either choice first sends the rest of
/// this workspace's versions to the copy it made (one somebody else pushed to since is left as it is).
/// either way the local versions are replayed on the home's head and wait to be pushed (one the choice leaves changing
/// nothing is not: its file is kept in <c>versions/.replaced</c>). A choice applies only to the conflicts listed: when
/// what differs now is not that (the home moved on, or this side was edited meanwhile), nothing is settled, and the
/// conflict found now is recorded and returned to be asked again; so is one the home holds no versions for now (nothing
/// to take them from). A choice cut short past its commit point was applied: the conflict is over, and a later choice
/// finds nothing to settle. Taking the server's keeps the working state it replaces as a version file in
/// <c>versions/.replaced</c>, outside the chain: nothing is committed, replayed or sent for it, so a take that leaves
/// only the server's values sends nothing. After a Save both cut short, either choice first sends the rest of this
/// workspace's versions to the copy it made (one somebody else pushed to since is left as it is).
/// <see cref="ConflictChoice.SaveBoth"/> saves the working state as a version, sends this workspace's versions to a new
/// project at the home (<paramref name="saveBothName"/>, else this project's name with a number) made from the base,
/// and moves this project onto the home's head.
/// and moves this project onto the home's head; a project deleted at the home meanwhile keeps its versions here, the
/// copy holding them too (<see cref="WorkspaceState.Gone"/>).
/// </summary>
/// <exception cref="HomeUnavailableException">The home cannot be reached; the conflict stays.</exception>
public async Task<WorkspaceSyncResult> ResolveAsync(Guid projectId, IProjectHome home, ConflictChoice choice, IReadOnlySet<string>? takeServer = null,
@@ -365,23 +391,31 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var project = Existing(projectId);
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
{
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
var bundle = project.ReadBundle();
var sync = project.ReadSync();
if (sync.Conflict is not { } recorded) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
EnsureChained(project, bundle, Unpushed(project, bundle));
if (choice == ConflictChoice.SaveBoth)
return await SaveBothAsync(project, bundle, home, saveBothName, cancellationToken).ConfigureAwait(false);
// Nothing is committed first: a version of the working state would be replayed into one that only repeats the
// head, and sent. What the take replaces of the working state is kept aside by the replay instead.
IReadOnlySet<string> takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet<string>();
ProjectSettings? kept = null;
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled)
try
{
Kept = conflict is null ? kept : null,
};
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
var bundle = project.ReadBundle();
var sync = project.ReadSync();
if (sync.Conflict is not { } recorded) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
EnsureChained(project, bundle, Unpushed(project, bundle));
if (choice == ConflictChoice.SaveBoth)
return await SaveBothAsync(project, bundle, home, saveBothName, cancellationToken).ConfigureAwait(false);
// Nothing is committed first: a version of the working state would be replayed into one that only repeats the
// head, and sent. What the take replaces of the working state is kept aside by the replay instead.
IReadOnlySet<string> takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet<string>();
ProjectSettings? kept = null;
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k).ConfigureAwait(false);
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled)
{
Kept = conflict is null ? kept : null,
};
}
catch (ProjectException ex) when (NotFoundAtHome(ex))
{
if (!await GoneAsync(project, home, ex, cancellationToken).ConfigureAwait(false)) throw;
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
}
}
}
@@ -522,6 +556,13 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
{
var local = locals[i];
var portable = ProjectContent.Canonical(replay.Commits[i].ToJson());
// A commit the replay leaves changing nothing (the server's values taken over its own, or the same change made
// there) is not replayed into an empty version to send: its file goes to versions/.replaced with the others.
if (ProjectContent.SameChoices(parent.SettingsJson, portable, photos))
{
Log.Info("workspace", $"Version {local.Number} of project {project.ProjectId} changes nothing once replayed on version {head.Number}: it is not sent");
continue;
}
parent = local with
{
Id = Guid.CreateVersion7(),
@@ -542,7 +583,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
if (takeTheirs is EveryPath && differences.Count > 0 && Portable(bundle.Project, media) is { } unsaved
&& (tipVersion is null || !ProjectContent.SameChoices(tipVersion.SettingsJson, unsaved, photos)))
project.WriteAside(VersionOf(project, unsaved, tipVersion, ProjectVersionKind.Commit, null, null, null, media?.FramesVersion ?? bundle.Workspace!.Media.FramesVersion));
var journal = new WorkspaceReplay(head.Id, [.. locals.Select(v => v.Id)], [.. rebased.Select(v => v.Id)], [.. held.Select(v => v.Id)]);
// Past its commit point the replay has settled the conflict recorded beside it, the choice made applied (SettleAsync).
var journal = new WorkspaceReplay(head.Id, [.. locals.Select(v => v.Id)], [.. rebased.Select(v => v.Id)], [.. held.Select(v => v.Id)], Settles: true);
project.WriteSync(project.ReadSync() with { Replay = journal });
Step("replay.journal");
foreach (var version in rebased)
@@ -697,11 +739,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var basis = project.ReadBundle().Workspace!.Base;
if (sync.Replay is { } replay)
{
if (basis?.VersionId == replay.OntoBase) await FinishReplayAsync(project, replay, [], home, cancellationToken).ConfigureAwait(false);
var finished = basis?.VersionId == replay.OntoBase;
if (finished) await FinishReplayAsync(project, replay, [], home, cancellationToken).ConfigureAwait(false);
else foreach (var id in replay.Replayed) SetAsideUnsent(project, id);
sync = sync with { Replay = null };
// Past its commit point a replay that settles a conflict has applied the choice made: the conflict is over. Cut
// before it, nothing moved and the conflict stays to be asked again.
sync = sync with { Replay = null, Conflict = finished && replay.Settles ? null : sync.Conflict };
project.WriteSync(sync);
Log.Info("workspace", $"A sync of project {project.ProjectId} cut short was {(basis?.VersionId == replay.OntoBase ? "finished" : "undone")}");
Log.Info("workspace", $"A sync of project {project.ProjectId} cut short was {(finished ? "finished" : "undone")}");
}
if (sync.SaveBoth is { } both && basis?.VersionId != both.FromBase)
{
@@ -744,12 +789,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
/// <summary>
/// Both sides kept: this workspace's versions to a new project at the home, this project onto the home's head. The copy
/// and the ids its versions are sent under are journaled before the first is sent (<see cref="WorkspaceSaveBoth"/>): a
/// retry after a cut sends to the same copy what it lacks (the home answers "already stored" for the rest), whatever name
/// it is asked for, and the base moving onto the home's head is the commit point. A copy deleted at the home since, or
/// one somebody else sent versions to (it is theirs now, left as it is), gives way to a new copy that takes them all.
/// Every local version after the base is sent: one the chain does not reach stops it first (<see cref="EnsureChained"/>).
/// Both sides kept: this workspace's versions to a new project at the home, this project onto the home's head. The
/// copy's name and the version it is made from are journaled before the home is asked for it, then the copy and the ids
/// its versions are sent under, before the first is sent (<see cref="WorkspaceSaveBoth"/>): a retry after a cut finds the
/// copy the home made by its name (<see cref="WorkspaceCopyIntent"/>) and sends it what it lacks (the home answers
/// "already stored" for the rest), whatever name it is asked for, and the base moving onto the home's head is the
/// commit point. A copy deleted at the home since, or one somebody else sent versions to (it is theirs now, left as it
/// is), gives way to a new copy that takes them all. Every local version after the base is sent: one the chain does not
/// reach stops it first (<see cref="EnsureChained"/>). A project the home deleted meanwhile keeps its versions and its
/// working state here as they are, the copy holding them too (<see cref="WorkspaceState.Gone"/>); a retry sends nothing new.
/// </summary>
private async Task<WorkspaceSyncResult> SaveBothAsync(WorkspaceProject project, ProjectBundle bundle, IProjectHome home, string? name, CancellationToken cancellationToken)
{
@@ -759,8 +807,11 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var locals = Unpushed(project, bundle);
EnsureChained(project, bundle, locals);
var pending = project.ReadSync().SaveBoth;
var copy = locals.Count > 0 && pending is not null ? await OurCopyAsync(project, home, pending, cancellationToken).ConfigureAwait(false) : null;
if (copy is null) pending = null;
SequenceProject? copy = null;
if (locals.Count > 0 && pending is not null && await OurCopyAsync(project, home, pending, cancellationToken).ConfigureAwait(false) is { } ours)
(copy, pending) = ours;
else
pending = null;
var pushed = new List<ProjectVersion>();
for (var attempt = 1; locals.Count > 0; attempt++)
{
@@ -768,9 +819,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
if (pending is null)
{
var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false);
copy = await CreateNamedAsync(home, name, workspace.Name, basis?.Id, cancellationToken).ConfigureAwait(false);
var first = await home.HeadAsync(copy.Id, cancellationToken).ConfigureAwait(false);
pending = new WorkspaceSaveBoth(copy.Id, first.Id, first.Number, workspace.Base?.VersionId, []);
(copy, pending) = await CreateCopyAsync(project, home, name, workspace, basis, locals, cancellationToken).ConfigureAwait(false);
}
var known = pending.Moves.Select(m => m.LocalId).ToHashSet();
if (!journaled || locals.Any(l => !known.Contains(l.Id)))
@@ -794,7 +843,18 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
}
// This project takes the home's head: the local versions now live in the copy (and their files in versions/.replaced).
var head = await home.HeadAsync(project.ProjectId, cancellationToken).ConfigureAwait(false);
ProjectVersion head;
try
{
head = await home.HeadAsync(project.ProjectId, cancellationToken).ConfigureAwait(false);
}
catch (ProjectException ex) when (NotFoundAtHome(ex))
{
// Deleted at the home meanwhile: nothing to move onto. The versions are in the copy, and stay here too, unsent,
// with the journal, so a retry finds that copy and sends nothing again.
ReportGone(project, ex);
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), pushed, [], copy);
}
Verify(head);
project.WriteVersion(head, replace: true);
var media = project.ReadMedia();
@@ -803,28 +863,107 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
Step("saveboth.base");
foreach (var local in locals) project.SetAside(local);
foreach (var move in pending?.Moves ?? []) SetAsideUnsent(project, move.LocalId);
project.WriteSync(WorkspaceSyncState.None);
// Settled and finished; a restore recorded stays, the next version saved tells whether it still restores.
project.WriteSync(project.ReadSync() with { Conflict = null, Replay = null, SaveBoth = null });
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), pushed, [head], copy);
}
/// <summary>
/// The copy a save-both cut short made (<paramref name="pending"/>) while it is still this workspace's to send to: at the
/// home, its head the version it was made with or one this workspace sent it. Null when it was deleted since, or somebody
/// else sent it a version (it is theirs: never sent to, never deleted by this workspace).
/// home, its head the version it was made with or one this workspace sent it; with the journal as it now names it. One
/// only asked for (<see cref="WorkspaceSaveBoth.Intended"/>: a cut came before its answer) is found by the name it was
/// asked under: a project of that name made since, whose only version is the one it was made from (its ids journaled
/// then). Null when it was deleted since, never made, or somebody else sent it a version (it is theirs: never sent to,
/// never deleted by this workspace).
/// </summary>
private static async Task<SequenceProject?> OurCopyAsync(WorkspaceProject project, IProjectHome home, WorkspaceSaveBoth pending, CancellationToken cancellationToken)
private static async Task<(SequenceProject Copy, WorkspaceSaveBoth Pending)?> OurCopyAsync(WorkspaceProject project, IProjectHome home,
WorkspaceSaveBoth pending, CancellationToken cancellationToken)
{
if ((await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == pending.CopyId) is not { } copy)
var listed = await home.ListAsync(cancellationToken).ConfigureAwait(false);
if (pending.Intended)
{
if (pending.Intent is not { } intent
|| listed.FirstOrDefault(p => string.Equals(p.Name, intent.Name.Trim(), StringComparison.OrdinalIgnoreCase)) is not { } named
|| named.CreatedAt < intent.Asked - IntentSkew)
{
Log.Info("workspace", $"The copy a save-both of project {project.ProjectId} asked for was not made at the home");
return null;
}
var first = await home.HeadAsync(named.Id, cancellationToken).ConfigureAwait(false);
if (first.Number != 1 || (intent.FromHash is { } hash && !string.Equals(first.ContentHash, hash, StringComparison.OrdinalIgnoreCase)))
{
Log.Info("workspace", $"Project '{named.Name}' at the home is not the copy a save-both of project {project.ProjectId} asked for: it is left as it is");
return null;
}
var found = pending with { CopyId = named.Id, CopyBaseId = first.Id, CopyBaseNumber = first.Number };
project.WriteSync(project.ReadSync() with { SaveBoth = found });
Log.Info("workspace", $"Project '{named.Name}' at the home is the copy a save-both of project {project.ProjectId} cut short asked for: it is used");
return (named, found);
}
if (listed.FirstOrDefault(p => p.Id == pending.CopyId) is not { } copy)
{
Log.Info("workspace", $"The copy a save-both of project {project.ProjectId} made is no longer at the home");
return null;
}
var head = await home.HeadAsync(copy.Id, cancellationToken).ConfigureAwait(false);
if (head.Id == pending.CopyBaseId || pending.Moves.Any(m => m.CopyVersionId == head.Id)) return copy;
if (head.Id == pending.CopyBaseId || pending.Moves.Any(m => m.CopyVersionId == head.Id)) return (copy, pending);
Log.Info("workspace", $"Project '{copy.Name}', the copy a save-both of project {project.ProjectId} made, took another's version since: it is left as it is");
return null;
}
/// <summary>How much earlier than asked a home's clock may date the copy a save-both asked for (<see cref="WorkspaceCopyIntent.Asked"/>).</summary>
private static readonly TimeSpan IntentSkew = TimeSpan.FromMinutes(5);
/// <summary>
/// A new project at the home for a save-both, named <paramref name="name"/>, else "<paramref name="workspace"/>'s name
/// (2)", "(3)"… whichever is free, made from <paramref name="basis"/>. Each name is journaled before the home is asked
/// (<see cref="WorkspaceCopyIntent"/>), then the copy with the ids <paramref name="locals"/> are sent under. A name the
/// home finds taken leaves no journal when it was asked for (the caller asks again without one).
/// </summary>
/// <exception cref="ProjectException">The name asked for is taken or invalid.</exception>
private async Task<(SequenceProject Copy, WorkspaceSaveBoth Pending)> CreateCopyAsync(WorkspaceProject project, IProjectHome home, string? name,
BundleWorkspace workspace, ProjectVersion? basis, IReadOnlyList<ProjectVersion> locals, CancellationToken cancellationToken)
{
var moves = locals.Select(l => new WorkspaceMove(l.Id, Guid.CreateVersion7())).ToList();
var named = !string.IsNullOrWhiteSpace(name);
HashSet<string> taken = named ? [] : (await home.ListAsync(cancellationToken).ConfigureAwait(false)).Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
for (var n = 2; ; n++)
{
var candidate = named ? name!.Trim() : Numbered(workspace.Name, n);
if (!named && n < 100 && taken.Contains(candidate)) continue;
var intended = new WorkspaceSaveBoth(Guid.Empty, Guid.Empty, 0, workspace.Base?.VersionId, moves,
new WorkspaceCopyIntent(candidate, basis?.Id, basis?.ContentHash, DateTimeOffset.UtcNow));
project.WriteSync(project.ReadSync() with { SaveBoth = intended });
Step("saveboth.intent");
SequenceProject copy;
try
{
copy = await home.CreateAsync(candidate, basis?.Id, null, cancellationToken).ConfigureAwait(false);
}
catch (ProjectException ex) when (ex.Reason == ProjectError.NameTaken && !named && n < 100)
{
continue;
}
catch (ProjectException ex) when (ex.Reason is ProjectError.NameTaken or ProjectError.InvalidName)
{
// Not made: no copy of that name is this workspace's.
project.WriteSync(project.ReadSync() with { SaveBoth = null });
throw;
}
Step("saveboth.created");
var first = await home.HeadAsync(copy.Id, cancellationToken).ConfigureAwait(false);
var pending = intended with { CopyId = copy.Id, CopyBaseId = first.Id, CopyBaseNumber = first.Number };
project.WriteSync(project.ReadSync() with { SaveBoth = pending });
return (copy, pending);
}
static string Numbered(string source, int n)
{
var candidate = $"{source} ({n})";
return candidate.Length > ProjectService.MaxNameLength ? $"{source[..(ProjectService.MaxNameLength - 6)]} ({n})" : candidate;
}
}
/// <summary>
/// Sends <paramref name="locals"/> to the save-both's copy under the ids its journal gives them, in order on the version
/// it was made with (the home answers "already stored" for those a cut sent before).
@@ -854,18 +993,20 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// <summary>
/// Keep mine or take the server's after a Save both cut short (its journal still there): the rest of this workspace's
/// versions the journal lists go to the copy it made, so the home never keeps half of one; a copy deleted or taken over
/// since is left as it is. The journal itself goes once the project moved (the caller's commit point).
/// versions the journal lists go to the copy it made (or asked for, found by its name), so the home never keeps half of
/// one; a copy deleted, never made or taken over since is left as it is. The journal itself goes once the project moved
/// (the caller's commit point).
/// </summary>
private async Task FinishCopyAsync(WorkspaceProject project, IProjectHome home, IReadOnlyList<ProjectVersion> chain, CancellationToken cancellationToken)
{
if (project.ReadSync().SaveBoth is not { } pending) return;
var listed = pending.Moves.Select(m => m.LocalId).ToHashSet();
var sent = chain.TakeWhile(v => listed.Contains(v.Id)).ToList();
if (sent.Count == 0 || await OurCopyAsync(project, home, pending, cancellationToken).ConfigureAwait(false) is not { } copy) return;
if (sent.Count == 0 || await OurCopyAsync(project, home, pending, cancellationToken).ConfigureAwait(false) is not { } ours) return;
var (copy, known) = ours;
try
{
await SendToCopyAsync(project, home, copy, pending, sent, cancellationToken).ConfigureAwait(false);
await SendToCopyAsync(project, home, copy, known, sent, cancellationToken).ConfigureAwait(false);
}
catch (CopyMovedException)
{
@@ -884,24 +1025,6 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
_ => new WorkspaceSyncException(outcome, $"The home refused version {local.Number} of {target}: {outcome}."),
};
/// <summary>A new project at the home named <paramref name="name"/>, else "<paramref name="source"/> (2)", "(3)"… whichever is free.</summary>
private static async Task<SequenceProject> CreateNamedAsync(IProjectHome home, string? name, string source, Guid? from, CancellationToken cancellationToken)
{
if (!string.IsNullOrWhiteSpace(name)) return await home.CreateAsync(name, from, null, cancellationToken).ConfigureAwait(false);
for (var n = 2; ; n++)
{
var candidate = $"{source} ({n})";
if (candidate.Length > ProjectService.MaxNameLength) candidate = $"{source[..(ProjectService.MaxNameLength - 6)]} ({n})";
try
{
return await home.CreateAsync(candidate, from, null, cancellationToken).ConfigureAwait(false);
}
catch (ProjectException ex) when (ex.Reason == ProjectError.NameTaken && n < 100)
{
}
}
}
private ProjectVersion? CommitLocked(WorkspaceProject project, ProjectBundle bundle, string? note, string? author)
{
if (bundle.IsReadOnly) throw ReadOnly(project.ProjectId);
@@ -909,13 +1032,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var media = project.ReadMedia();
var portable = Portable(bundle.Project, media) ?? throw MediaMissing(project.ProjectId);
var hash = ProjectContent.Hash(portable);
var locals = Unpushed(project, bundle);
var versions = project.Versions();
var locals = Unpushed(versions, bundle);
// Saved after a gap, the new version would hang from the versions before it and the ones after it would be left out.
EnsureChained(project, bundle, locals);
var (tipId, tipNumber, tipHash) = locals.Count > 0
? (locals[^1].Id, locals[^1].Number, locals[^1].ContentHash)
: (workspace.Base?.VersionId, workspace.Base?.Number ?? 0, workspace.Base?.ContentHash);
if (hash == tipHash || SameAsTip(project, bundle, locals, portable, media)) return null;
if (hash == tipHash || SameAsTip(versions, bundle, locals, portable, media)) return null;
var sync = project.ReadSync();
// A restore taken back since (Undo, Ctrl+Z) no longer shows in the working state: that version is no restore.
var restoredFrom = sync.RestoredFrom is { } restored && StillRestores(project, restored, Tip(project, bundle, locals), portable, media?.Photos ?? NoFrames)
@@ -956,40 +1080,41 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// Where the project stands. With a <paramref name="home"/>, the media first learn how it names its photos now (a photo
/// hashed or converted there since the copy, <see cref="WorkspaceMedia.NamedAsAt"/>), so a version the home made since
/// and the working state name each photo alike: written at once while the project's lease is held
/// (<paramref name="leased"/>), else when it can be taken without waiting (<see cref="WorkspaceStatus.MediaOutdated"/>
/// when it cannot).
/// (<paramref name="leased"/>), else when it can be taken now or after a moment (<see cref="WorkspaceStatus.MediaOutdated"/>
/// when it cannot). The home's frames are read again only when its stamp of them moved (<see cref="IProjectHome.FramesStampAsync"/>).
/// Without the lease, a version file that reads as damaged is read again under it before the state says so: another
/// operation between two of its writes is no damage; while that one holds the project, it is not reported (that
/// operation stops at it itself). A home that answers it no longer has the project reads as <see cref="WorkspaceState.Gone"/>.
/// </summary>
private async Task<WorkspaceStatus> StatusLockedAsync(WorkspaceProject project, IProjectHome? home, CancellationToken cancellationToken, bool leased = true)
{
var bundle = project.ReadBundle();
var local = ReadLocal(project);
if (local.Damaged > 0 && !leased) local = await ConfirmDamagedAsync(project, local, cancellationToken).ConfigureAwait(false);
var (bundle, media, sync, versions, locals, damaged) = local;
var workspace = bundle.Workspace!;
WorkspaceMedia? media;
try
{
media = project.ReadMedia();
}
catch (InvalidDataException)
{
media = null;
}
var sync = project.ReadSync();
var conflict = sync.Conflict;
var locals = Unpushed(project, bundle);
var damaged = Gaps(project, bundle, locals, sync).Count;
// A take of the server's values (or any replay that settles) cut past its commit point applied the choice: the
// conflict is over, as the next sync writes.
var conflict = sync.Replay is { Settles: true } replay && workspace.Base?.VersionId == replay.OntoBase ? null : sync.Conflict;
ProjectVersion? head = null;
HomeFrames? frames = null;
var offline = false;
var gone = false;
if (home is not null)
{
try
{
head = await home.HeadAsync(project.ProjectId, cancellationToken).ConfigureAwait(false);
if (media is not null) frames = await home.FramesAsync(cancellationToken).ConfigureAwait(false);
if (media is not null) frames = await FramesOfAsync(home, workspace.Home, cancellationToken).ConfigureAwait(false);
}
catch (HomeUnavailableException)
{
offline = true;
}
catch (ProjectException ex) when (ex.Reason is ProjectError.NotFound or ProjectError.SequenceNotFound)
{
ReportGone(project, ex);
(gone, offline) = (true, true);
}
}
var outdated = false;
if (media is not null && frames is not null)
@@ -1001,11 +1126,12 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
var tipHash = locals.Count > 0 ? locals[^1].ContentHash : workspace.Base?.ContentHash;
var modified = !bundle.IsReadOnly && (Portable(bundle.Project, media) is not { } portable
|| (ProjectContent.Hash(portable) != tipHash && !SameAsTip(project, bundle, locals, portable, media)));
|| (ProjectContent.Hash(portable) != tipHash && !SameAsTip(versions, bundle, locals, portable, media)));
var behind = head is not null && head.Id != workspace.Base?.VersionId ? Math.Max(1, head.Number - (workspace.Base?.Number ?? 0)) : 0;
// A local commit the home stored although its answer was lost reads as behind by the commits it holds: they are ours.
if (head is not null && locals.Any(v => v.Id == head.Id)) behind = 0;
var state = media is null ? WorkspaceState.MediaRemoved
var state = gone ? WorkspaceState.Gone
: media is null ? WorkspaceState.MediaRemoved
: damaged > 0 ? WorkspaceState.Damaged
: conflict is not null ? WorkspaceState.Conflict
: offline ? WorkspaceState.Offline
@@ -1020,17 +1146,137 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
};
}
/// <summary>What the workspace holds of a project, read once: its working state, media, sync state and versions.</summary>
private sealed record LocalState(ProjectBundle Bundle, WorkspaceMedia? Media, WorkspaceSyncState Sync, IReadOnlyList<ProjectVersion> Versions,
List<ProjectVersion> Locals, int Damaged);
private static LocalState ReadLocal(WorkspaceProject project)
{
var bundle = project.ReadBundle();
WorkspaceMedia? media;
try
{
media = project.ReadMedia();
}
catch (InvalidDataException)
{
media = null;
}
var sync = project.ReadSync();
var (versions, unreadable) = project.ReadVersionFiles();
var locals = Unpushed(versions, bundle);
return new LocalState(bundle, media, sync, versions, locals, Gaps(project, bundle, locals, sync, versions, unreadable).Count);
}
/// <summary>
/// A state read without the lease that found damaged versions, read again under it (taken without waiting): what that
/// read finds. While another operation holds the project its writes may be half done, so nothing is reported damaged.
/// </summary>
private static async Task<LocalState> ConfirmDamagedAsync(WorkspaceProject project, LocalState local, CancellationToken cancellationToken)
{
IDisposable lease;
try
{
lease = await project.LockAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
}
catch (LeaseUnavailableException)
{
Log.Debug("workspace", $"Project {project.ProjectId} reads as damaged while another operation holds it: not reported yet");
return local with { Damaged = 0 };
}
using (lease) return ReadLocal(project);
}
/// <summary>
/// The home's frames: read again only when the home's stamp of them moved since they were last read for that home
/// (<see cref="IProjectHome.FramesStampAsync"/>), else those read then; always read from a home that keeps no stamp.
/// </summary>
private async Task<HomeFrames> FramesOfAsync(IProjectHome home, BundleHome key, CancellationToken cancellationToken)
{
var stamp = await home.FramesStampAsync(cancellationToken).ConfigureAwait(false);
if (stamp is not null && _frames.TryGetValue(key, out var known) && known.Stamp == stamp) return known.Frames;
var frames = await home.FramesAsync(cancellationToken).ConfigureAwait(false);
// Read after the stamp: these frames are at least as new as it, so a stamp moved meanwhile reads them again next time.
if (stamp is not null) _frames[key] = (stamp, frames);
return frames;
}
/// <summary>The homes' frames last read by a status, by home, with the stamp they were read at.</summary>
private readonly System.Collections.Concurrent.ConcurrentDictionary<BundleHome, (string Stamp, HomeFrames Frames)> _frames = new();
/// <summary>The projects whose home answered it no longer has them, each logged once.</summary>
private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, byte> _gone = new();
private void ReportGone(WorkspaceProject project, ProjectException ex)
{
if (_gone.TryAdd(project.ProjectId, 0))
Log.Info("workspace", $"The home no longer has project {project.ProjectId}: its copy here is kept as it is, with its versions ({ex.Message})");
}
/// <summary>
/// After <paramref name="ex"/> (a project or version the home does not know) stopped a sync: whether the home no longer
/// has the project itself (deleted there, or its sequence gone), which is no failure to retry but the state the project is
/// in. False when the home still has it (the version alone was not found) or cannot be asked now.
/// </summary>
private async Task<bool> GoneAsync(WorkspaceProject project, IProjectHome home, ProjectException ex, CancellationToken cancellationToken)
{
try
{
await home.HeadAsync(project.ProjectId, cancellationToken).ConfigureAwait(false);
return false;
}
catch (ProjectException gone) when (gone.Reason is ProjectError.NotFound or ProjectError.SequenceNotFound)
{
ReportGone(project, ex);
return true;
}
catch (HomeUnavailableException)
{
return false;
}
}
/// <summary>
/// <c>versions/.replaced</c> pruned after a sync that ended well (<see cref="WorkspaceProject.PruneReplaced"/>): never a
/// version the sync state still names (a journal, a conflict, a restore). A file that cannot go waits for the next one.
/// </summary>
private static void PruneReplaced(WorkspaceProject project)
{
var sync = project.ReadSync();
var referenced = new HashSet<Guid>();
if (sync.Replay is { } replay) referenced.UnionWith([.. replay.Replaced, .. replay.Replayed, .. replay.Adopted, replay.OntoBase]);
if (sync.SaveBoth is { } both) referenced.UnionWith([.. both.Moves.Select(m => m.LocalId), .. both.Moves.Select(m => m.CopyVersionId)]);
if (sync.Conflict is { } conflict) referenced.UnionWith([.. conflict.Mine, conflict.TheirsVersionId, conflict.BaseVersionId]);
if (sync.RestoredFrom is { } restored) referenced.Add(restored);
var removed = project.PruneReplaced(referenced, DateTime.UtcNow);
if (removed > 0) Log.Info("workspace", $"{removed} old file(s) of project {project.ProjectId} left versions/.replaced");
}
/// <summary>Whether a sync stopped because the home does not know a project or a version (<see cref="GoneAsync"/> tells which).</summary>
private static bool NotFoundAtHome(ProjectException ex) => ex.Reason is ProjectError.NotFound or ProjectError.SequenceNotFound;
/// <summary>
/// Writes the media with the photos named as <paramref name="frames"/> name them (read again under the lease: a copy
/// may have written new media meanwhile); without <paramref name="leased"/>, only when the lease is free now. Whether
/// the media know the home's names.
/// may have written new media meanwhile); without <paramref name="leased"/>, only when the lease can be taken now or,
/// tried once more, after a moment (<see cref="LearnWait"/>). Whether the media know the home's names.
/// </summary>
private async Task<bool> LearnNamesAsync(WorkspaceProject project, HomeFrames frames, bool leased, CancellationToken cancellationToken)
{
IDisposable? lease = null;
try
{
if (!leased) lease = await project.LockAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
if (!leased)
{
try
{
lease = await project.LockAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
}
catch (LeaseUnavailableException)
{
// Two reads of the state at once (the picker and the menu), or a copy's commit: held only for a moment.
lease = await project.LockAsync(LearnWait, cancellationToken).ConfigureAwait(false);
}
}
if (project.ReadMedia() is not { } media) return false;
var named = media.NamedAsAt(frames);
if (!ReferenceEquals(named, media)) project.WriteMedia(named);
@@ -1046,20 +1292,30 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
}
/// <summary>How long a status read waits, the second time, for the lease to teach the media the home's names.</summary>
private static readonly TimeSpan LearnWait = TimeSpan.FromMilliseconds(500);
/// <summary>
/// The version files after the base the chain of versions to send (<paramref name="chain"/>) does not reach: files that
/// cannot be read, and unsent versions that do not follow from the base. Those a replay or a save-both cut short left
/// (their journals, which the next sync settles) are not counted.
/// (their journals, which the next sync settles) are not counted. The files are read once.
/// </summary>
private static List<string> Gaps(WorkspaceProject project, ProjectBundle bundle, IReadOnlyList<ProjectVersion> chain, WorkspaceSyncState sync)
{
var (versions, unreadable) = project.ReadVersionFiles();
return Gaps(project, bundle, chain, sync, versions, unreadable);
}
private static List<string> Gaps(WorkspaceProject project, ProjectBundle bundle, IReadOnlyList<ProjectVersion> chain, WorkspaceSyncState sync,
IReadOnlyList<ProjectVersion> versions, IReadOnlyList<UnreadableVersion> unreadable)
{
var basis = bundle.Workspace!.Base;
var after = basis?.Number ?? 0;
var reached = chain.Select(v => v.Id).ToHashSet();
if (sync.Replay is { } replay) reached.UnionWith([.. replay.Replayed, .. replay.Replaced]);
if (sync.SaveBoth is { } both) reached.UnionWith(both.Moves.Select(m => m.LocalId));
var gaps = project.UnreadableVersions().Where(u => u.Number > after && !reached.Contains(u.Id)).Select(u => u.Path).ToList();
gaps.AddRange(project.Versions().Where(v => v.StoredAt is null && v.Number > after && v.Id != basis?.VersionId && !reached.Contains(v.Id))
var gaps = unreadable.Where(u => u.Number > after && !reached.Contains(u.Id)).Select(u => u.Path).ToList();
gaps.AddRange(versions.Where(v => v.StoredAt is null && v.Number > after && v.Id != basis?.VersionId && !reached.Contains(v.Id))
.Select(project.VersionPath));
return gaps;
}
@@ -1082,15 +1338,19 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// once each names its photos as the media hold them now: a version saved before the media were copied again after a
/// conversion or a hash at the home parks the same photos under their former names.
/// </summary>
private static bool SameAsTip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals, string portable, WorkspaceMedia? media)
private static bool SameAsTip(IReadOnlyList<ProjectVersion> versions, ProjectBundle bundle, List<ProjectVersion> locals, string portable, WorkspaceMedia? media)
{
if (media is null) return false;
return Tip(project, bundle, locals) is { } tip && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
return Tip(versions, bundle, locals) is { } tip && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
}
/// <summary>The last version: the newest local one, else the base (null when this workspace keeps neither).</summary>
private static ProjectVersion? Tip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals) =>
locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) : null;
locals.Count > 0 ? locals[^1] : Tip(project.Versions(), bundle, locals);
/// <summary>The last version among <paramref name="versions"/> (the workspace's, read once).</summary>
private static ProjectVersion? Tip(IReadOnlyList<ProjectVersion> versions, ProjectBundle bundle, List<ProjectVersion> locals) =>
locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? versions.FirstOrDefault(v => v.Id == basis.VersionId) : null;
/// <summary>
/// Whether the working state <paramref name="portable"/>, saved as a version on <paramref name="parent"/>, still restores
@@ -1125,10 +1385,13 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
shown.Count == found.Count && shown.ToHashSet().SetEquals(found);
/// <summary>The local versions after the base, oldest first, each the child of the one before (the base's for the first).</summary>
private static List<ProjectVersion> Unpushed(WorkspaceProject project, ProjectBundle bundle)
private static List<ProjectVersion> Unpushed(WorkspaceProject project, ProjectBundle bundle) => Unpushed(project.Versions(), bundle);
/// <summary>The local versions after the base among <paramref name="versions"/> (the workspace's, read once).</summary>
private static List<ProjectVersion> Unpushed(IReadOnlyList<ProjectVersion> versions, ProjectBundle bundle)
{
var basis = bundle.Workspace!.Base;
var candidates = project.Versions().Where(v => v.StoredAt is null && v.Number > (basis?.Number ?? 0)).ToList();
var candidates = versions.Where(v => v.StoredAt is null && v.Number > (basis?.Number ?? 0)).ToList();
var chain = new List<ProjectVersion>();
var parent = basis?.VersionId;
while (candidates.FirstOrDefault(v => v.ParentId == parent) is { } next)
@@ -189,26 +189,6 @@ public sealed class WorkspaceFallbackTests(TestContext context)
|| f.Contains(unsent.Id.ToString("N"), StringComparison.OrdinalIgnoreCase)), "the version never sent is kept");
}
[Test]
public async Task ACancelledFirstCopyLeavesNoHalfMedia()
{
await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 4);
var host = bed.Host();
var mainId = await bed.CheckoutAsync(host);
var project = host.Store.Find(mainId)!;
Assert.False(await host.RemovePartialMediaAsync(mainId), "a copy that ended keeps its media");
Assert.FileExists(project.MediaIndexPath);
// A first copy stopped before its index was written: what it wrote goes, the working state stays.
File.Delete(project.MediaIndexPath);
Assert.NotEmpty(Directory.EnumerateFiles(project.MediaDirectory, "*", SearchOption.AllDirectories));
Assert.True(await host.RemovePartialMediaAsync(mainId));
Assert.False(Directory.Exists(project.MediaDirectory));
Assert.FileExists(project.BundlePath);
Assert.False(await host.RemovePartialMediaAsync(mainId), "nothing left to remove");
Assert.False(await host.RemovePartialMediaAsync(Guid.NewGuid()));
}
[Test]
public async Task OpenInWorkspaceShowsTheCopysSizeAndTheFreeSpace()
{
@@ -126,7 +126,10 @@ public abstract class WorkspaceConflictTests(TestContext context) : DatabaseTest
Assert.Null(settled.Status.Conflict, "one answer settles what was shown");
Assert.Equal(0.5, (await two.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv);
Assert.Equal(1.0, Assert.NotNull(settled.Kept).Exposure.ExposureOffsetEv);
Assert.Equal("warmer", two.Unpushed(id).Single().Note, "the saved version is replayed, nothing more");
// The saved version changes nothing once the library's offset is taken: it is not sent, its file kept aside.
Assert.Empty(two.Unpushed(id));
var replaced = Path.Combine(two.Store.Find(id)!.VersionsDirectory, WorkspaceProject.ReplacedFolder);
Assert.Contains(Directory.GetFiles(replaced).Select(ProjectSidecars.ReadVersion), v => v.Note == "warmer");
}
[Test]
@@ -83,6 +83,19 @@ internal sealed class WorkspaceFixture : IAsyncDisposable
return await Archive.WaitForAsync(job.Id, expected, (int)TestTiming.Budget(60));
}
/// <summary>Queues a workspace copy job and returns at once, while it runs.</summary>
public async Task<Job> StartCopyAsync(WorkspaceStore store, Guid projectId)
{
var engine = await EngineAsync();
return await engine.EnqueueAsync(WorkspaceJobs.Copy(Sequence, store.Root, projectId), cancellationToken: Ct);
}
/// <summary>Cancels a job, as the user does from the jobs list.</summary>
public async Task CancelAsync(Guid jobId) => await (await EngineAsync()).CancelAsync(jobId, Ct);
/// <summary>Pauses a job, as the user does from the jobs list.</summary>
public async Task PauseAsync(Guid jobId) => await (await EngineAsync()).PauseAsync(jobId, Ct);
private async Task<JobEngine> EngineAsync()
{
if (_engine is not null) return _engine;
@@ -151,6 +164,39 @@ internal sealed class CuttingHome(IProjectHome home) : IProjectHome
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => home.FramesAsync(cancellationToken);
}
/// <summary>A home that counts its reads of the frames, its stamp of them passed through.</summary>
internal sealed class CountingHome(IProjectHome home) : IProjectHome
{
public int FramesReads { get; private set; }
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken)
{
FramesReads++;
return home.FramesAsync(cancellationToken);
}
public Task<string?> FramesStampAsync(CancellationToken cancellationToken) => home.FramesStampAsync(cancellationToken);
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken);
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => home.ListAsync(cancellationToken);
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
home.CreateAsync(name, fromVersionId, author, cancellationToken);
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken);
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
home.GetVersionAsync(projectId, versionId, cancellationToken);
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => home.PushAsync(projectId, version, cancellationToken);
}
/// <summary>A home that cannot be reached, standing for a server out of reach or an archive offline.</summary>
internal sealed class UnreachableHome(IProjectHome home) : IProjectHome
{
@@ -188,6 +188,30 @@ public abstract class WorkspaceMergeTests(TestContext context) : DatabaseTest(co
Assert.SequenceEqual([2], (await sync.OpenAsync(id, fx.Ct)).Working.ExcludedFrames);
}
[Test]
public async Task AProjectHeldForAMomentStillLetsTheMediaLearnTheHomesNames()
{
var fx = await FixtureAsync();
await UnhashAsync(fx, 2);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
await VerifyAsync(fx);
var hashed = (await fx.Db.Frames.ListBySequenceAsync(fx.Sequence.Id))[2].Sha256!;
// Another read of the state holds the project for a moment as this one asks: it waits for it, once.
var lease = await project.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(10)), fx.Ct);
var release = Task.Run(async () =>
{
await Task.Delay(150, fx.Ct);
lease.Dispose();
}, fx.Ct);
var status = await sync.StatusAsync(id, fx.Home, fx.Ct);
await release;
Assert.False(status.MediaOutdated);
Assert.Equal(hashed, project.ReadMedia()!.Frames[2].Sha256);
}
[Test]
public async Task APhotoConvertedAtTheHomeIsOutdatedMediaAndTheMediaLearnItsEncodings()
{
@@ -534,7 +534,10 @@ public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest
// One read in another folder (the sequence moved since, another may hold its old one) checks every file again.
job = await fx.CopyAsync(store, main.Id, checkpoint: $"copy:{copied.Frames.Count}:{fx.Sequence.FramesVersion}:elsewhere/folder");
Assert.Contains(Loc.L("Archive.Log.WorkspaceCopied", fx.Sequence.DisplayTitle, 1, 0, 4, 0), job.LogTail!);
Assert.Equal(copied.Frames[0].FileSha256, Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(damaged))));
// Under a name of its own: the media named the file kept before, which the editor reads until the commit.
var mended = project.MediaFile(project.ReadMedia()!.Frames[0].File!);
Assert.Equal(copied.Frames[0].FileSha256, Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(mended))));
Assert.False(File.Exists(damaged));
}
[Test]
@@ -0,0 +1,480 @@
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Tests.Projects;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>
/// What the workspace's sync and copy never lose or leave behind (owner review 2, R8, fix round 3): a choice cut short is
/// applied or asked again, a version a take leaves changing nothing is not sent, a save-both cut short makes one copy, a
/// project deleted at the home stops the sync once, the copy never writes over the editor's photo nor leaves half media.
/// </summary>
[Category("workspace")]
public abstract class WorkspaceSafetyTests(TestContext context) : DatabaseTest(context)
{
private async Task<WorkspaceFixture> FixtureAsync(int frames = 5)
{
var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
return Own(await WorkspaceFixture.CreateAsync(Context, archive, frames));
}
private static async Task EditAsync(WorkspaceSync sync, Guid projectId, Func<ProjectSettings, ProjectSettings> edit, CancellationToken ct)
{
var session = await sync.OpenAsync(projectId, ct);
await sync.SaveAsync(projectId, edit(session.Working), ct);
}
private static ProjectSettings Offset(ProjectSettings s, double ev) => s with { Exposure = s.Exposure with { ExposureOffsetEv = ev } };
private static ProjectSettings Settings(ProjectVersion version) => ProjectSettings.FromJson(version.SettingsJson);
private static async Task<double> WorkingOffsetAsync(WorkspaceSync sync, Guid projectId, CancellationToken ct) =>
(await sync.OpenAsync(projectId, ct)).Working.Exposure.ExposureOffsetEv;
private static List<ProjectVersion> Replaced(WorkspaceProject project)
{
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
return Directory.Exists(aside) ? [.. Directory.GetFiles(aside).Select(ProjectSidecars.ReadVersion)] : [];
}
private static TimeSpan Wait => TimeSpan.FromSeconds(TestTiming.Budget(10));
/// <summary>Polls <paramref name="done"/> until it holds, failing after the budget.</summary>
private static async Task UntilAsync(Func<bool> done, string what, CancellationToken ct)
{
var until = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(30));
while (!done())
{
if (DateTime.UtcNow > until) Assert.Fail($"Timed out waiting: {what}");
await Task.Delay(20, ct);
}
}
/// <summary>
/// A project of its own checked out into two workspaces: the first sent an offset of 0.5, the second changed it to 1
/// without saving and was asked. The second's store, sync and project, and the conflict shown.
/// </summary>
private static async Task<(WorkspaceStore Store, WorkspaceSync Two, WorkspaceProject Project, WorkspaceConflict Shown)> AskedAsync(WorkspaceFixture fx, Guid id)
{
var (_, one) = fx.Workspace("one");
var (store, two) = fx.Workspace("two");
await fx.CheckoutAsync(one, id);
var project = await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct);
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
return (store, two, project, shown);
}
private static async Task<Guid> ProjectAsync(WorkspaceFixture fx, string name) =>
(await fx.Archive.Service.Projects.CreateAsync(fx.Sequence.Id, name, cancellationToken: fx.Ct)).Id;
[Test]
public async Task ATakeCutPastItsCommitPointIsAppliedAndALaterChoiceFindsNothingToSettle()
{
var fx = await FixtureAsync();
var id = await ProjectAsync(fx, "Cut late");
var (store, two, project, shown) = await AskedAsync(fx, id);
await Assert.ThrowsAsync<CutException>(() =>
WorkspaceFixture.Cutting(store, "replay.base").ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct));
// The working state is on the library's head with its value: the choice was made, nothing asks it again.
var status = await two.StatusAsync(id, fx.Home, fx.Ct);
Assert.Null(status.Conflict, "a take past its commit point was applied");
Assert.Equal(WorkspaceState.InSync, status.State);
Assert.Equal(0.5, await WorkingOffsetAsync(two, id, fx.Ct));
Assert.Contains(Replaced(project), v => Settings(v).Exposure.ExposureOffsetEv == 1.0);
// The dialog still open answers Keep mine: there is nothing left to settle, and nothing is cleared without applying it.
var later = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: shown, cancellationToken: fx.Ct);
Assert.Null(later.Status.Conflict);
Assert.Equal(0.5, await WorkingOffsetAsync(two, id, fx.Ct));
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
}
[Test]
public async Task ATakeCutBeforeItsCommitPointKeepsTheConflictAndALaterChoiceIsApplied()
{
var fx = await FixtureAsync();
var id = await ProjectAsync(fx, "Cut early");
var (store, two, project, shown) = await AskedAsync(fx, id);
await Assert.ThrowsAsync<CutException>(() =>
WorkspaceFixture.Cutting(store, "replay.journal").ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct));
var status = await two.StatusAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.Conflict, status.State);
Assert.Equal(1.0, await WorkingOffsetAsync(two, id, fx.Ct));
// Keep mine, asked again: applied, and sent.
var kept = await two.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, shown: Assert.NotNull(status.Conflict), cancellationToken: fx.Ct);
Assert.Null(kept.Status.Conflict);
Assert.Equal(1.0, await WorkingOffsetAsync(two, id, fx.Ct));
Assert.Null(project.ReadSync().Conflict);
await two.CommitAsync(id, cancellationToken: fx.Ct);
await two.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(1.0, Settings(await fx.Home.HeadAsync(id, fx.Ct)).Exposure.ExposureOffsetEv);
}
[Test]
public async Task AVersionTheTakeLeavesChangingNothingIsNotSentAndStaysAside()
{
var fx = await FixtureAsync();
var (_, one) = fx.Workspace("one");
var (_, two) = fx.Workspace("two");
var id = (await fx.CheckoutAsync(one)).ProjectId;
var project = await fx.CheckoutAsync(two, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
// Saved here: an offset and a stack, then the offset alone again.
await EditAsync(two, id, s => Offset(s, 0.8) with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
await two.CommitAsync(id, "stacked", cancellationToken: fx.Ct);
await EditAsync(two, id, s => Offset(s, 0.9), fx.Ct);
await two.CommitAsync(id, "again", cancellationToken: fx.Ct);
var shown = Assert.NotNull((await two.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
// The library's offset taken: "again" changes nothing on "stacked" replayed, so only that one waits to be sent.
var settled = await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
Assert.Null(settled.Status.Conflict);
Assert.Equal("stacked", two.Unpushed(id).Single().Note);
Assert.Contains(Replaced(project), v => v.Note == "again");
var history = (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count;
Assert.Count(1, (await two.PushAsync(id, fx.Home, fx.Ct)).Pushed);
Assert.Equal(history + 1, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count);
var head = Settings(await fx.Home.HeadAsync(id, fx.Ct));
Assert.Equal((0.5, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode));
// A take whose every version changes nothing sends none, merge or not.
var (_, three) = fx.Workspace("three");
await fx.CheckoutAsync(three, id);
await EditAsync(one, id, s => Offset(s, 0.6), fx.Ct);
await one.PullAsync(id, fx.Home, fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(three, id, s => Offset(s, 2.0), fx.Ct);
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
var asked = Assert.NotNull((await three.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict);
var taken = await three.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: asked, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.InSync, taken.Status.State);
Assert.Empty(three.Unpushed(id));
Assert.Contains(Replaced(three.Store.Project(id)), v => v.Note == "bright");
}
/// <summary>A conflict where this side saved "bright" (offset 2) and has nothing unsaved: the working state is that version.</summary>
private static async Task<(WorkspaceStore Store, WorkspaceSync Three, WorkspaceProject Project, Guid Id)> SavedConflictAsync(WorkspaceFixture fx, Guid? projectId = null)
{
var (_, one) = fx.Workspace("one");
var (store, three) = fx.Workspace("three");
var id = (await fx.CheckoutAsync(one, projectId)).ProjectId;
var project = await fx.CheckoutAsync(three, id);
await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct);
await one.CommitAsync(id, cancellationToken: fx.Ct);
await one.PushAsync(id, fx.Home, fx.Ct);
await EditAsync(three, id, s => Offset(s, 2), fx.Ct);
await three.CommitAsync(id, "bright", cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
return (store, three, project, id);
}
[Test]
public async Task ASaveBothCutOnceTheHomeMadeTheCopyUsesThatCopyOnRetry()
{
var fx = await FixtureAsync();
var (store, three, project, id) = await SavedConflictAsync(fx);
await Assert.ThrowsAsync<CutException>(() =>
WorkspaceFixture.Cutting(store, "saveboth.created").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
var made = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)");
Assert.True(Assert.NotNull(project.ReadSync().SaveBoth).Intended, "the copy was asked for under a journal");
// The retry, asked another name, finds the copy it asked for and makes no other.
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct);
Assert.Equal(made.Id, Assert.NotNull(both.SavedAs).Id);
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
Assert.Equal(2.0, Settings(await fx.Home.HeadAsync(made.Id, fx.Ct)).Exposure.ExposureOffsetEv);
Assert.Equal(2, (await fx.Home.HistoryAsync(made.Id, null, 10, fx.Ct)).Count);
Assert.Equal(WorkspaceState.InSync, both.Status.State);
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
}
[Test]
public async Task ASaveBothCutBeforeTheHomeWasAskedMakesOneCopyOnRetry()
{
var fx = await FixtureAsync();
var (store, three, project, id) = await SavedConflictAsync(fx);
await Assert.ThrowsAsync<CutException>(() =>
WorkspaceFixture.Cutting(store, "saveboth.intent").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct));
Assert.Count(1, await fx.Home.ListAsync(fx.Ct));
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct);
Assert.Equal("Main (PC)", Assert.NotNull(both.SavedAs).Name);
Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count);
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
}
[Test]
public async Task ASaveBothKeepsTheRestoreRecorded()
{
var fx = await FixtureAsync();
var (_, three, project, id) = await SavedConflictAsync(fx);
var first = (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Single(v => v.Number == 1);
project.WriteSync(project.ReadSync() with { RestoredFrom = first.Id });
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
Assert.NotNull(both.SavedAs);
Assert.Equal(new WorkspaceSyncState(first.Id, null), project.ReadSync());
}
[Test]
public async Task AProjectDeletedAtTheHomeIsGoneAndKeepsEverythingHere()
{
var fx = await FixtureAsync();
var id = await ProjectAsync(fx, "Side");
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync, id);
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
await sync.CommitAsync(id, "unsent", cancellationToken: fx.Ct);
await EditAsync(sync, id, s => Offset(s, 0.9), fx.Ct);
await fx.Archive.Service.Projects.DeleteAsync(id, fx.Ct);
// Every sync answers Gone, without throwing, and moves nothing.
var pulled = await sync.PullAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.Gone, pulled.Status.State);
Assert.Empty(pulled.Pulled);
var pushed = await sync.PushAsync(id, fx.Home, fx.Ct);
Assert.Equal(WorkspaceState.Gone, pushed.Status.State);
Assert.Empty(pushed.Pushed);
Assert.Equal(WorkspaceState.Gone, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State);
Assert.Equal("unsent", sync.Unpushed(id).Single().Note);
Assert.Equal(0.9, await WorkingOffsetAsync(sync, id, fx.Ct));
Assert.Equal(WorkspaceSyncState.None, project.ReadSync());
}
[Test]
public async Task ASaveBothOfAProjectDeletedAtTheHomeIsGoneAndARetryMakesNoOtherCopy()
{
var fx = await FixtureAsync();
var side = await ProjectAsync(fx, "Side");
var (_, three, project, id) = await SavedConflictAsync(fx, side);
await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct);
await fx.Archive.Service.Projects.DeleteAsync(id, fx.Ct);
var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Gone, both.Status.State);
var listed = (await fx.Home.ListAsync(fx.Ct)).Count;
var again = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct);
Assert.Equal(WorkspaceState.Gone, again.Status.State);
Assert.Equal(listed, (await fx.Home.ListAsync(fx.Ct)).Count);
Assert.Equal(WorkspaceState.Gone, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State);
// Nothing of this side's is lost: its versions wait here, the working state as it was.
var working = (await three.OpenAsync(id, fx.Ct)).Working;
Assert.Equal((2.0, StackSetting.Mean), (working.Exposure.ExposureOffsetEv, working.Stack.Mode));
Assert.Contains(project.Versions(), v => v.Note == "bright");
}
[Test]
public async Task ACopyWritesAConvertedPhotoUnderANameOfItsOwnAndSwitchesAtItsCommit()
{
var fx = await FixtureAsync(4);
var (store, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
var before = project.ReadMedia()!.Frames[1];
var editors = project.MediaFile(before.File!);
var bytes = File.ReadAllBytes(editors);
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
// The archive's proxy of the frame is made again from the converted photo.
var row = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
var proxy = fx.Archive.Paths.Sequence(row.Folder).Proxy(1);
if (File.Exists(proxy)) File.Delete(proxy);
Job job;
string? written = null;
using (await project.LockAsync(Wait, fx.Ct))
{
// The copy waits for the lease at its commit, its files written.
job = await fx.StartCopyAsync(store, id);
await UntilAsync(() => (written = Directory.GetFiles(project.ProxiesDirectory, $"{before.Id:N}-*.jpg").FirstOrDefault()) is not null,
"the converted photo's proxy", fx.Ct);
await fx.Archive.WaitForAsync(job.Id, JobState.Running, (int)TestTiming.Budget(30));
Assert.SequenceEqual(bytes, File.ReadAllBytes(editors), "the editor's file is never written over before the commit");
Assert.Equal(before.File, project.ReadMedia()!.Frames[1].File);
}
await fx.Archive.WaitForAsync(job.Id, JobState.Done, (int)TestTiming.Budget(60));
var after = project.ReadMedia()!.Frames[1];
Assert.Equal(Path.GetFileName(written), Path.GetFileName(after.File));
Assert.NotEqual(before.FileSha256, after.FileSha256);
Assert.False(File.Exists(editors), "the file the media no longer name goes at the commit");
}
[Test]
public async Task ACancelledFirstCopyRemovesTheMediaItWroteOnceItStopped()
{
var fx = await FixtureAsync();
var (store, sync) = fx.Workspace();
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
var lease = await project.LockAsync(Wait, fx.Ct);
Job job;
try
{
job = await fx.StartCopyAsync(store, id);
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
"the copy's files", fx.Ct);
await fx.CancelAsync(job.Id);
await fx.Archive.WaitForAsync(job.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
Assert.True(Directory.Exists(project.MediaDirectory), "nothing is removed while another holds the project");
}
finally
{
lease.Dispose();
}
await UntilAsync(() => !Directory.Exists(project.MediaDirectory), "the cancelled copy's media removed", fx.Ct);
Assert.Equal(id, project.ReadBundle().Workspace!.ProjectId);
// Copied again, then a second copy cancelled: media a copy finished stay.
await fx.CopyAsync(store, id);
var media = project.ReadMedia()!;
using (await project.LockAsync(Wait, fx.Ct))
{
job = await fx.StartCopyAsync(store, id);
await fx.Archive.WaitForAsync(job.Id, JobState.Running, (int)TestTiming.Budget(30));
await fx.CancelAsync(job.Id);
await fx.Archive.WaitForAsync(job.Id, JobState.Cancelled, (int)TestTiming.Budget(30));
}
using (await project.LockAsync(Wait, fx.Ct))
Assert.Equal(media.Frames.Count, project.ReadMedia()!.Frames.Count);
foreach (var frame in media.Frames) Assert.FileExists(project.MediaFile(frame.File!));
}
[Test]
public async Task APausedFirstCopyKeepsWhatItWrote()
{
var fx = await FixtureAsync();
var (store, sync) = fx.Workspace();
var id = (await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct)).Id;
var project = await sync.CheckoutAsync(fx.Home, id, cancellationToken: fx.Ct);
using (await project.LockAsync(Wait, fx.Ct))
{
var job = await fx.StartCopyAsync(store, id);
await UntilAsync(() => Directory.Exists(project.ProxiesDirectory) && Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length == fx.Frames.Count,
"the copy's files", fx.Ct);
await fx.PauseAsync(job.Id);
await fx.Archive.WaitForAsync(job.Id, JobState.Paused, (int)TestTiming.Budget(30));
}
// Taken again by this test: nothing waited for it to remove the files.
using (await project.LockAsync(Wait, fx.Ct))
Assert.Equal(fx.Frames.Count, Directory.GetFiles(project.ProxiesDirectory, "*.jpg").Length);
}
[Test]
public async Task AVersionReadAsDamagedWhileAnotherHoldsTheProjectIsReportedOnlyOnceConfirmed()
{
var fx = await FixtureAsync();
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
await EditAsync(sync, id, s => Offset(s, 0.5), fx.Ct);
var version = Assert.NotNull(await sync.CommitAsync(id, "first", cancellationToken: fx.Ct));
File.WriteAllText(project.VersionPath(version), "{ half written");
WorkspaceStatus busy;
using (await project.LockAsync(Wait, fx.Ct))
busy = await sync.StatusAsync(id, fx.Home, fx.Ct);
Assert.Equal(0, busy.Damaged);
Assert.NotEqual(WorkspaceState.Damaged, busy.State);
var free = await sync.StatusAsync(id, fx.Home, fx.Ct);
Assert.Equal((WorkspaceState.Damaged, 1), (free.State, free.Damaged));
}
[Test]
public void TheReplacedVersionsArePrunedByAgeAndCountButNeverOneStillNamed()
{
var store = new WorkspaceStore(Path.Combine(Context.CreateTempDirectory("prune"), "workspace"));
var project = store.Project(Guid.CreateVersion7());
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
Directory.CreateDirectory(aside);
var now = DateTime.UtcNow;
var files = new List<(string Path, Guid Id)>();
for (var i = 0; i < 25; i++)
{
var id = Guid.CreateVersion7();
var path = Path.Combine(aside, $"{i + 1}-{id:N}.json" + (i == 24 ? ".638000000000000000" : ""));
File.WriteAllText(path, "{}");
File.SetLastWriteTimeUtc(path, now - (i < 22 ? TimeSpan.FromHours(i + 1) : TimeSpan.FromDays(31 + i)));
files.Add((path, id));
}
// The newest 20 stay; of the rest, only those a journal still names.
var removed = project.PruneReplaced(new HashSet<Guid> { files[21].Id, files[24].Id }, now);
Assert.Equal(3, removed);
var left = Directory.GetFiles(aside).ToHashSet(StringComparer.OrdinalIgnoreCase);
foreach (var (path, _) in files.Take(20)) Assert.Contains(path, left);
Assert.Contains(files[21].Path, left);
Assert.Contains(files[24].Path, left);
Assert.DoesNotContain(files[20].Path, left);
Assert.DoesNotContain(files[22].Path, left);
Assert.DoesNotContain(files[23].Path, left);
}
[Test]
public async Task ASyncThatEndsWellPrunesTheReplacedVersions()
{
var fx = await FixtureAsync();
var id = await ProjectAsync(fx, "Pruned");
var (_, two, project, shown) = await AskedAsync(fx, id);
await two.ResolveAsync(id, fx.Home, ConflictChoice.TakeServer, shown: shown, cancellationToken: fx.Ct);
var aside = Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder);
var files = Directory.GetFiles(aside);
Assert.Count(1, files);
var taken = files[0];
Assert.True(DateTime.UtcNow - File.GetLastWriteTimeUtc(taken) < TimeSpan.FromHours(1), "a file set aside is dated then");
var old = Path.Combine(aside, $"1-{Guid.CreateVersion7():N}.json");
File.WriteAllText(old, "{}");
File.SetLastWriteTimeUtc(old, DateTime.UtcNow - TimeSpan.FromDays(40));
await two.PushAsync(id, fx.Home, fx.Ct);
Assert.False(File.Exists(old));
Assert.True(File.Exists(taken), "what the take replaced stays a month");
}
[Test]
public async Task TheHomesFramesAreReadAgainOnlyWhenItsStampMoved()
{
var fx = await FixtureAsync(4);
var (_, sync) = fx.Workspace();
var project = await fx.CheckoutAsync(sync);
var id = project.ProjectId;
var home = new CountingHome(fx.Home);
Assert.False((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
var read = home.FramesReads;
Assert.False((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
Assert.Equal(read, home.FramesReads, "the same stamp: the frames read before");
await ProjectServiceTests.ConvertAsync(fx.Archive, fx.Sequence.Id, [1], 700);
Assert.True((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
Assert.Equal(read + 1, home.FramesReads);
Assert.True((await sync.StatusAsync(id, home, fx.Ct)).MediaOutdated);
Assert.Equal(read + 1, home.FramesReads);
}
}
public sealed class SqliteWorkspaceSafetyTests(TestContext context) : WorkspaceSafetyTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresWorkspaceSafetyTests(TestContext context) : WorkspaceSafetyTests(context)
{
protected override Backend Backend => Backend.Postgres;
}