diff --git a/i18n/en/Editor.resx b/i18n/en/Editor.resx index b371aa79..54114912 100644 --- a/i18n/en/Editor.resx +++ b/i18n/en/Editor.resx @@ -402,6 +402,7 @@ Not sentversions saved here wait to be sent to the library Library newerthe library has versions the workspace has not taken Conflict + Damageda version saved in the workspace cannot be read Not in library In sync No media diff --git a/i18n/en/Errors.resx b/i18n/en/Errors.resx index e6c1f139..84d1585e 100644 --- a/i18n/en/Errors.resx +++ b/i18n/en/Errors.resx @@ -49,6 +49,7 @@ Unknown token “{0}” at position {1}. The closing brace at position {1} has no opening one; double it for a literal brace. Something went wrong. + A sync or a copy is using this project; open it again in a moment. Settle the conflict first: Sync shows which settings differ. Its local copy could not be removed now: Remove local copy takes it later.long {0} did not match its source after it was copied.{0} file path @@ -74,4 +75,5 @@ The library refused a version of this project; the log says why. This project's sequence is no longer in the library: only its local copy can be removed, from the picker's menu.long Some work is not in the library yet: send it first, or discard it. + This version is larger than the {0} MB a version can be, so the library cannot store it; your work stays in the workspace.{0} megabytes diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx index 0189e145..867d9ea0 100644 --- a/i18n/en/Tooltips.resx +++ b/i18n/en/Tooltips.resx @@ -333,6 +333,7 @@ Versions saved here that wait to be sent to the library: {0}. Sync sends them{0} number of versions Versions in the library this workspace has not taken yet: {0}. Sync takes them{0} number of versions You and the library changed the same settings: Sync to choose which to keep + A version saved here cannot be read (the log names its file): nothing is saved, sent or taken until it is repairedlong The library no longer has this project or its sequence: nothing more can be sent; remove the local copy (its work is kept aside)long Every version of this project is in the library, and nothing changed since the last one The workspace holds no photos of this project: opening it copies them again diff --git a/i18n/it/Editor.it.resx b/i18n/it/Editor.it.resx index dcc5a09f..0964fa74 100644 --- a/i18n/it/Editor.it.resx +++ b/i18n/it/Editor.it.resx @@ -377,6 +377,7 @@ Da inviare Libreria più nuova Conflitto + Danneggiato Non in libreria Sincronizzato Senza media diff --git a/i18n/it/Errors.it.resx b/i18n/it/Errors.it.resx index 82039775..17566d93 100644 --- a/i18n/it/Errors.it.resx +++ b/i18n/it/Errors.it.resx @@ -49,6 +49,7 @@ Segnaposto sconosciuto «{0}» alla posizione {1}. La parentesi graffa chiusa alla posizione {1} non ha un'apertura; raddoppiala per una graffa letterale. Si è verificato un problema. + Una sincronizzazione o una copia sta usando questo progetto; riaprilo tra un momento. Risolvi prima il conflitto: Sincronizza mostra quali impostazioni sono diverse. La sua copia locale ora non è stata rimossa: Rimuovi copia locale la toglie più tardi. {0} non corrispondeva all'originale dopo la copia. @@ -74,4 +75,5 @@ La libreria ha rifiutato una versione di questo progetto; il log spiega il motivo. La sequenza di questo progetto non è più nella libreria: si può solo rimuoverne la copia locale, dal menu del selettore. Parte del lavoro non è ancora nella libreria: prima inviala oppure scartala. + Questa versione supera i {0} MB che una versione può occupare, quindi la libreria non può conservarla; il tuo lavoro resta nello spazio di lavoro. diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx index 46054e19..3d5fc5a8 100644 --- a/i18n/it/Tooltips.it.resx +++ b/i18n/it/Tooltips.it.resx @@ -332,6 +332,7 @@ Versioni salvate qui in attesa di essere inviate alla libreria: {0}. Sincronizza le invia Versioni nella libreria non ancora prese da questo spazio di lavoro: {0}. Sincronizza le prende Tu e la libreria avete modificato le stesse impostazioni: sincronizza per scegliere quali tenere + Una versione salvata qui non è leggibile (il log indica il file): nulla viene salvato, inviato o ricevuto finché non è riparata La libreria non ha più questo progetto o la sua sequenza: non si può più inviare nulla; rimuovi la copia locale (il suo lavoro resta da parte) Ogni versione di questo progetto è nella libreria e nulla è cambiato dall'ultima Lo spazio di lavoro non ha foto di questo progetto: aprirlo le copia di nuovo diff --git a/src/Chronos.App/Editor/WorkspaceSequenceSource.cs b/src/Chronos.App/Editor/WorkspaceSequenceSource.cs index 492c573d..6549ce15 100644 --- a/src/Chronos.App/Editor/WorkspaceSequenceSource.cs +++ b/src/Chronos.App/Editor/WorkspaceSequenceSource.cs @@ -105,7 +105,10 @@ public sealed class WorkspaceSequenceSource : ISequenceSource, IProjectRenders public event Action? JobChanged; - /// The workspace holds no media for the project (copy them again), or the project is gone. + /// + /// The workspace holds no media for the project (copy them again), the project is gone, or a sync or a copy kept it for + /// longer than the sync waits (). + /// public async Task LoadAsync(CancellationToken cancellationToken) { WorkspaceSession session; @@ -121,6 +124,12 @@ public sealed class WorkspaceSequenceSource : ISequenceSource, IProjectRenders { throw new EditorException(Loc.L("Editor.Error.Project"), ex); } + catch (LeaseUnavailableException ex) + { + // A long sync or copy holds the project: nothing was read, opening it again later reads it whole. + Log.Info("editor", $"Workspace project {ProjectId} is busy: {ex.Message}"); + throw new EditorException(Loc.L("Errors.Workspace.Busy"), ex); + } if (session is not { Sequence: { } media, Media: { } index }) throw new EditorException(Loc.L("Errors.Workspace.MediaMissing")); var home = session.Bundle.Workspace!.Home; var row = await _db.Sequences.GetAsync(home.SequenceId, cancellationToken); diff --git a/src/Chronos.App/Shell/ErrorText.cs b/src/Chronos.App/Shell/ErrorText.cs index 02e3dcad..ac81e4e0 100644 --- a/src/Chronos.App/Shell/ErrorText.cs +++ b/src/Chronos.App/Shell/ErrorText.cs @@ -47,7 +47,8 @@ public static class ErrorText // A template an archive refused (Settings › Archive, or this PC's templates for a new archive): the rule it breaks. TemplateRuleException rule => Loc.L("Errors.Template." + rule.Rule), // The workspace (owner review 2, R8): its own sentences, a home out of reach, a project the home refused. - Workspace.WorkspaceException or HomeUnavailableException or ProjectException or WorkspaceSyncException => Workspace.WorkspaceText.Failure(ex), + Workspace.WorkspaceException or HomeUnavailableException or ProjectException or WorkspaceSyncException or ProjectVersionTooLargeException => + Workspace.WorkspaceText.Failure(ex), // The server words its errors in the client's language (Accept-Language). ChronosApiException api when FromServer(api.Error) => api.Error.Message, // Any other reply (a reverse proxy's 502 page while the server restarts, its 413 for a large body, a bare 500) holds diff --git a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs index 4c44b3d6..91549116 100644 --- a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs +++ b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs @@ -618,6 +618,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel var status = local with { Behind = behind, Offline = last.Offline, HomeHead = last.HomeHead }; var state = gone ? WorkspaceState.Gone : status.MediaRemoved ? WorkspaceState.MediaRemoved + : status.Damaged > 0 ? WorkspaceState.Damaged : status.Conflict is not null ? WorkspaceState.Conflict : status.Offline ? WorkspaceState.Offline : status.Behind > 0 ? WorkspaceState.Behind diff --git a/src/Chronos.App/Workspace/WorkspaceHost.cs b/src/Chronos.App/Workspace/WorkspaceHost.cs index c1e18d3f..695173e6 100644 --- a/src/Chronos.App/Workspace/WorkspaceHost.cs +++ b/src/Chronos.App/Workspace/WorkspaceHost.cs @@ -267,7 +267,7 @@ public sealed class WorkspaceHost return local with { Offline = true, - State = local.State is WorkspaceState.MediaRemoved or WorkspaceState.Conflict && !gone ? local.State + State = local.State is WorkspaceState.MediaRemoved or WorkspaceState.Damaged or WorkspaceState.Conflict && !gone ? local.State : gone ? WorkspaceState.Gone : WorkspaceState.Offline, }; @@ -508,7 +508,7 @@ public sealed class WorkspaceHost var name = project.ReadBundle().Workspace!.Name; var status = await StatusAsync(projectId, cancellationToken: cancellationToken).ConfigureAwait(false); var (files, bytes) = Measure(project.Folder); - return new WorkspaceRemovalPlan(projectId, name, files, bytes, status.Ahead, status.Modified, status.Conflict is not null, status.Offline, + return new WorkspaceRemovalPlan(projectId, name, files, bytes, status.Ahead + status.Damaged, status.Modified, status.Conflict is not null, status.Offline, status.State == WorkspaceState.Gone); }, cancellationToken); @@ -528,7 +528,9 @@ public sealed class WorkspaceHost if (!discard) { var status = await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false); - if (status.Ahead > 0 || (status.Modified && !status.ReadOnly) || status.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent")); + // A version saved here that cannot be read may be one the home never had: only discarding keeps it aside. + if (status.Ahead > 0 || status.Damaged > 0 || (status.Modified && !status.ReadOnly) || status.Conflict is not null) + throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent")); await VerifyHomeAsync(projectId, workspace, cancellationToken).ConfigureAwait(false); } await RemoveFolderAsync(project, cancellationToken).ConfigureAwait(false); diff --git a/src/Chronos.App/Workspace/WorkspaceText.cs b/src/Chronos.App/Workspace/WorkspaceText.cs index c2c9ccdc..38939a10 100644 --- a/src/Chronos.App/Workspace/WorkspaceText.cs +++ b/src/Chronos.App/Workspace/WorkspaceText.cs @@ -32,6 +32,7 @@ public static class WorkspaceText WorkspaceState.Offline => StatusKind.Offline, WorkspaceState.MediaRemoved => StatusKind.NoProxies, WorkspaceState.Gone => StatusKind.Warning, + WorkspaceState.Damaged => StatusKind.Error, _ => StatusKind.Queued, }; @@ -88,6 +89,9 @@ public static class WorkspaceText ProjectException { Reason: ProjectError.InvalidName } => Loc.L("Errors.Workspace.InvalidName"), ProjectException { Reason: ProjectError.MainCannotBeDeleted } => Loc.L("Errors.Workspace.MainKept"), ProjectException => Loc.L("Errors.Workspace.ProjectGone"), + // A version over the size one may be: saved here (refused before anything was written) or sent (the home stored nothing). + ProjectVersionTooLargeException or WorkspaceSyncException { Outcome: PushOutcome.TooLarge } => + Loc.L("Errors.Workspace.VersionTooLarge", ProjectSidecars.MaxVersionBytes >> 20), WorkspaceSyncException => Loc.L("Errors.Workspace.Refused"), InvalidDataException => Loc.L("Errors.Workspace.Damaged"), Editor.EditorException => ex.Message, diff --git a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs index 945be6b3..6b9179ec 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs @@ -114,6 +114,8 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService var total = (long)frames.Count; var tally = new Tally(); var entries = new WorkspaceFrame[frames.Count]; + // The media the editor reads until this copy commits: their files stay until then (RemoveStale). + var current = TryReadMedia(project)?.Frames.Where(f => f.File is not null).ToDictionary(f => f.Id) ?? []; Directory.CreateDirectory(project.ProxiesDirectory); // The workspace is on this PC's own drive, with the index, the settings and the logs: the copy never fills it. var remaining = Remaining(project, folder, frames, policy); @@ -122,7 +124,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService for (var i = 0; i < frames.Count; i++) { ct.ThrowIfCancellationRequested(); - entries[i] = Copy(project, folder, paths, frames[i], policy, done: i < copied, tally, ct); + entries[i] = Copy(project, folder, paths, frames[i], policy, done: i < copied, current.GetValueOrDefault(frames[i].Id), tally, ct); if (clock.Elapsed >= CheckpointEvery) { await context.CheckpointAsync(Checkpoint(Stage.Copy, i + 1, row.FramesVersion, row.Folder), i + 1, total).ConfigureAwait(false); @@ -205,17 +207,26 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService /// /// 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 () 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 + /// (). Nor is it taken as this run's copy of a frame whose photo changed since. /// - private WorkspaceFrame Copy(WorkspaceProject project, SequencePaths folder, ArchivePaths paths, Frame frame, ProxyPolicy policy, bool done, Tally tally, - CancellationToken cancellationToken) + private WorkspaceFrame Copy(WorkspaceProject project, SequencePaths folder, ArchivePaths paths, Frame frame, ProxyPolicy policy, bool done, + WorkspaceFrame? current, Tally tally, CancellationToken cancellationToken) { var name = WorkspaceProject.ProxyName(frame.Id); var target = project.MediaFile(name); + var listed = current is not null && string.Equals(current.File, name, StringComparison.OrdinalIgnoreCase); if (done && File.Exists(target)) { - tally.Kept++; var kept = AtomicFile.ReadAllBytes(target); - return WorkspaceFrame.Of(frame, name, Sha(kept), kept.Length); + var keptSha = Sha(kept); + // The editor's file of another photo of the frame (a conversion since) was left by an earlier copy, not this run. + if (!listed || keptSha != current!.FileSha256 || SamePhoto(current, frame)) + { + tally.Kept++; + return WorkspaceFrame.Of(frame, name, keptSha, kept.Length); + } } var source = folder.Proxy(frame.Index); byte[]? bytes = null; @@ -238,8 +249,8 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService if (bytes is null) { // A file an earlier copy left for the frame (of other frames, or another folder's) is never kept as its photo by a - // resumed run: a regenerable copy, it goes. - if (File.Exists(target)) TryDelete(target); + // resumed run: a regenerable copy, it goes, unless the editor's media name it (it goes once this copy commits). + if (!listed && File.Exists(target)) TryDelete(target); tally.Missing++; return WorkspaceFrame.Of(frame, null, null, 0); } @@ -321,6 +332,11 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService private static FramePhoto Photo(WorkspaceFrame frame) => new(frame.Sha256, frame.Size, frame.CapturedAt); + /// Whether the media's entry of a frame is of the photo the frame holds now (a conversion gives it another file). + private static bool SamePhoto(WorkspaceFrame held, Frame frame) => + held.Size == frame.Size && held.CapturedAt == frame.CapturedAt + && (string.IsNullOrEmpty(held.Sha256) || string.IsNullOrEmpty(frame.Sha256) || string.Equals(held.Sha256, frame.Sha256, StringComparison.OrdinalIgnoreCase)); + /// Proxies of frames the sequence no longer has: regenerable copies, removed so the media folder holds the frames listed. private static void RemoveStale(WorkspaceProject project, WorkspaceMedia media) { diff --git a/src/Chronos.Data/Workspaces/WorkspaceStore.cs b/src/Chronos.Data/Workspaces/WorkspaceStore.cs index 40843c6c..2d5facaa 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceStore.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceStore.cs @@ -48,6 +48,9 @@ public sealed record WorkspaceSaveBoth(Guid CopyId, Guid CopyBaseId, int CopyBas /// A local version () and the id it is sent under to a save-both's copy. public sealed record WorkspaceMove(Guid LocalId, Guid CopyVersionId); +/// A version file of a workspace that cannot be read (): the number and id its name carries, and why. +public sealed record UnreadableVersion(int Number, Guid Id, string Path, string Reason); + /// /// A pull or push that met real conflicts (owner review 2, R8): the home's head it met, the version the local commits were /// made from, the local commits (oldest first) and the members both sides changed (mine, theirs). Settled by keeping mine, @@ -342,11 +345,24 @@ public sealed class WorkspaceProject AtomicFile.WriteAllText(MediaIndexPath, JsonSerializer.Serialize(media, WorkspaceJson.Default.WorkspaceMedia)); } - /// The versions this workspace holds, oldest first; one that cannot be read is left out (and logged). - public IReadOnlyList Versions() + /// + /// The versions this workspace holds, oldest first. One that cannot be read is left out here, logged, and listed by + /// : the sync treats one above the base as a gap in the chain of versions to send, never + /// as a version that is not there (). + /// + public IReadOnlyList Versions() => ReadVersions(log: true).Versions; + + /// + /// The version files of the workspace that cannot be read now (damaged, held open, over the size a version may be, of + /// a newer Chronos), by the number and id their names carry, oldest first. + /// + public IReadOnlyList UnreadableVersions() => ReadVersions(log: false).Unreadable; + + private (List Versions, List Unreadable) ReadVersions(bool log) { var versions = new List(); - foreach (var (_, _, path) in ProjectSidecars.ListVersions(VersionsDirectory)) + var unreadable = new List(); + foreach (var (number, id, path) in ProjectSidecars.ListVersions(VersionsDirectory)) { try { @@ -355,12 +371,21 @@ public sealed class WorkspaceProject } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { - Log.Warning("workspace", $"Version file {path} cannot be read", ex); + // Said once per file as it is now: every sync step lists the versions again. + if (log && (!Reported.TryGetValue(path, out var said) || said != ex.Message)) + { + Reported[path] = ex.Message; + Log.Warning("workspace", $"Version file {path} cannot be read", ex); + } + unreadable.Add(new UnreadableVersion(number, id, path, ex.Message)); } } - return versions; + return (versions, unreadable); } + /// The version files whose read failure was logged, with why (logged again when the reason changes). + private static readonly System.Collections.Concurrent.ConcurrentDictionary Reported = new(StringComparer.Ordinal); + /// The path of a version in . public string VersionPath(ProjectVersion version) => Path.Combine(VersionsDirectory, ArchiveLayout.ProjectVersionFileName(version.Number, version.Id)); @@ -370,9 +395,11 @@ public sealed class WorkspaceProject /// Moves a version file aside into versions/.replaced (a local commit replayed under another id), trying again /// for a moment while another program (an indexer, an antivirus) holds it open. /// - internal void SetAside(ProjectVersion version) + internal void SetAside(ProjectVersion version) => SetAside(VersionPath(version)); + + /// Moves the version file at aside into versions/.replaced (see ). + internal void SetAside(string path) { - var path = VersionPath(version); if (!File.Exists(path)) return; var aside = Path.Combine(VersionsDirectory, ReplacedFolder); Directory.CreateDirectory(aside); diff --git a/src/Chronos.Data/Workspaces/WorkspaceSync.cs b/src/Chronos.Data/Workspaces/WorkspaceSync.cs index 1bcbf590..0931ac31 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceSync.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceSync.cs @@ -38,6 +38,13 @@ public enum WorkspaceState /// sent, and the local copy can only be removed, its work kept aside. Told by the desktop, which asks the home. /// Gone, + + /// + /// A version saved here cannot be read, or no longer follows from the base (): + /// nothing is saved, sent or taken while it is so, which would leave it and the versions after it out of the chain. + /// One the home holds takes the home's copy at the next sync. + /// + Damaged, } /// @@ -56,6 +63,9 @@ public sealed record WorkspaceStatus(WorkspaceState State, bool Modified, int Ah /// teaches the media the home's names; false without a home asked. /// public bool MediaOutdated { get; init; } + + /// Version files after the base that cannot be read or do not follow from it (). + public int Damaged { get; init; } } /// What a push, pull or resolve did: the versions it stored at the home and those it took from there, and where it left the project. @@ -93,7 +103,10 @@ public sealed record WorkspaceSession(WorkspaceProject Project, ProjectBundle Bu public bool IsReadOnly => Bundle.IsReadOnly; } -/// A push the home refused for good (not a divergence: settings damaged on the way, of a newer format, or not this project's). +/// +/// A push the home refused for good (not a divergence: settings damaged on the way, of a newer format, larger than a +/// version may be (), or not this project's). +/// public sealed class WorkspaceSyncException(PushOutcome outcome, string message) : IOException(message) { public PushOutcome Outcome { get; } = outcome; @@ -118,7 +131,9 @@ public sealed class WorkspaceSyncException(PushOutcome outcome, string message) /// before its local file reads as sent; a replay journals itself in sync.json (), writes /// the replayed versions, then the base, and only then sets the old ones aside; a save-both journals its copy and the ids /// it sends under (). The next push, pull or resolve finishes or undoes what a cut left -/// (). +/// (). A local version after the base that cannot be read, or does not follow from it, is never +/// taken for one that is not there: nothing is saved, sent or taken past it () until it +/// reads again or takes the home's copy. /// public sealed class WorkspaceSync(WorkspaceStore store, string clientName) { @@ -260,7 +275,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) var bundle = project.ReadBundle(); if (project.ReadSync().Conflict is not null) break; var diverged = false; - foreach (var local in Unpushed(project, bundle)) + var locals = Unpushed(project, bundle); + EnsureChained(project, bundle, locals); + foreach (var local in locals) { var result = await home.PushAsync(projectId, local, cancellationToken).ConfigureAwait(false); switch (result.Outcome) @@ -280,7 +297,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) diverged = true; break; default: - throw new WorkspaceSyncException(result.Outcome, $"The home refused version {local.Number} of project {projectId}: {result.Outcome}."); + throw Refused(result.Outcome, local, $"project {projectId}"); } break; } @@ -309,6 +326,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) await SettleAsync(project, home, cancellationToken).ConfigureAwait(false); IReadOnlyList pulled = []; 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); @@ -329,8 +347,11 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) /// ), 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. Taking the server's first saves - /// uncommitted changes as a version, so the values it replaces are kept (its file in versions/.replaced). + /// 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 first saves uncommitted changes as a version, so + /// the values it replaces are kept (its file in versions/.replaced); replayed, that version holds the server's + /// values only and is not sent. 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). /// saves the working state as a version, sends this workspace's versions to a new /// project at the home (, else this project's name with a number) made from the base, /// and moves this project onto the home's head. @@ -347,13 +368,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) 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); // A conflict of format refuses the choice (nothing is merged with a newer Chronos's settings): nothing is saved first. - if (choice == ConflictChoice.TakeServer && !bundle.IsReadOnly && !recorded.NewerFormat) CommitLocked(project, bundle, null, null); + var saved = choice == ConflictChoice.TakeServer && !bundle.IsReadOnly && !recorded.NewerFormat ? CommitLocked(project, bundle, null, null) : null; IReadOnlySet takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet(); ProjectSettings? kept = null; - var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k).ConfigureAwait(false); + var (_, conflict, pulled) = await ReplayAsync(project, project.ReadBundle(), home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k, saved?.Id) + .ConfigureAwait(false); if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null }); return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled) { @@ -422,19 +445,23 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) /// files; the base only moves onto local commits the home already holds); with a choice the conflicts are settled by it, /// only when they are those lists (others are recorded and asked about first, as without a /// choice). Taking the server's everywhere tells the working state as keeping this side's values - /// would have left it (placed). Written in steps a cut can stop at any point (): the journal, - /// the replayed versions, the base with the merged working state (the commit point), then the old versions set aside and - /// the held ones adopted. + /// would have left it (placed); names the version taking them saved first, which is not replayed + /// when it then holds nothing its parent does not (its file is set aside with the others, for the undo). A conflict + /// recorded before stays when the home holds no version after the base (it lost them: nothing to settle it against). + /// Written in steps a cut can stop at any point (): the journal, the replayed versions, the + /// base with the merged working state (the commit point), then the old versions set aside and the held ones adopted. + /// Before the commit point the rest of a Save both cut short goes to its copy (). /// private async Task<(ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList Pulled)> ReplayAsync(WorkspaceProject project, ProjectBundle bundle, IProjectHome home, IReadOnlySet? takeTheirs, CancellationToken cancellationToken, WorkspaceConflict? shown = null, - Action? kept = null) + Action? kept = null, Guid? saved = null) { var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false); // A local commit the home stored although its answer was lost comes back among the home's versions, and the commits // after it were made on it. Its file keeps reading as unsent until the base has moved onto it or past it: written // over now, it would leave the chain from the base, and the commits after it with it. var locals = Unpushed(project, bundle); + var chain = locals; var unsent = project.Versions().Where(v => v.StoredAt is null).Select(v => v.Id).ToHashSet(); var theirs = new List(); for (var after = basis?.Number ?? 0; ;) @@ -449,7 +476,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) if (page.Count < PullPage) break; after = page[^1].Number; } - if (theirs.Count == 0) return (bundle, null, theirs); + if (theirs.Count == 0) return (bundle, StillRecorded(project, bundle), theirs); var held = theirs.Where(v => unsent.Contains(v.Id)).ToList(); var head = theirs[^1]; var atHome = theirs.Select(v => v.Id).ToHashSet(); @@ -461,13 +488,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) var media = await MediaNamedAsAtHomeAsync(project, home, cancellationToken).ConfigureAwait(false); var photos = media?.Photos ?? NoFrames; if (IsNewer(head) || IsNewer(basis)) - { - var newer = NewerAtHome(project, bundle, media, tipVersion, locals, basis, head, theirs, takeTheirs); - // Moved onto the head: the local files of versions the home held take its copies, as after a replay. - if (newer.Conflict is null && held.Count > 0) - await FinishReplayAsync(project, new WorkspaceReplay(head.Id, [], [], [.. held.Select(v => v.Id)]), held, home, cancellationToken).ConfigureAwait(false); - return newer; - } + return await NewerAtHomeAsync(project, bundle, home, media, tipVersion, chain, locals, basis, ours.Count > 0, held, head, theirs, takeTheirs, + cancellationToken).ConfigureAwait(false); // Each side names a photo as the frames held it when it was saved: named as the media hold it now (a conversion or a // hash since), the same photo merges as one. var basisSettings = basis is null ? new ProjectSettings() : ProjectMerge.Read(ProjectContent.Rebind(basis.SettingsJson, photos)); @@ -489,42 +511,36 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) if (differences.Count > 0 && (takeTheirs is null || (shown is not null && !SameDifferences(shown.Differences, differences)))) { var conflict = new WorkspaceConflict(head.Id, head.Number, basis?.Id ?? Guid.Empty, [.. locals.Select(v => v.Id)], differences, DateTimeOffset.UtcNow); - if (ours.Count > 0) - { - // The conflict is met past local commits the home holds: the base moves onto the last of them (their files - // adopted once it has), so the commits after them stay in the chain and the conflict is settled against it. - var onto = new WorkspaceReplay(basis!.Id, [], [], [.. held.Select(v => v.Id)]); - project.WriteSync(project.ReadSync() with { Replay = onto }); - Step("replay.journal"); - bundle = WithBase(bundle, basis); - project.WriteBundle(bundle); - Step("replay.base"); - await FinishReplayAsync(project, onto, held, home, cancellationToken).ConfigureAwait(false); - } + if (ours.Count > 0) bundle = await MoveOntoHeldAsync(project, bundle, basis!, held, home, cancellationToken).ConfigureAwait(false); project.WriteSync(project.ReadSync() with { Conflict = conflict, Replay = null }); return (bundle, conflict, theirs); } var rebased = new List(locals.Count); ProjectVersion parent = head; + var previous = headSettings; for (var i = 0; i < locals.Count; i++) { var local = locals[i]; + // The version taking the server's saved first, replayed onto their values, holds nothing more: not sent (kept aside for the undo). + if (local.Id == saved && ProjectMerge.SameValues(replay.Commits[i], previous)) continue; var portable = ProjectContent.Canonical(replay.Commits[i].ToJson()); parent = local with { Id = Guid.CreateVersion7(), ParentId = parent.Id, Number = parent.Number + 1, - Kind = i == locals.Count - 1 && differences.Count > 0 ? ProjectVersionKind.Merge : local.Kind, SettingsJson = portable, ContentHash = ProjectContent.Hash(portable), SettingsFormat = ProjectContent.FormatOf(portable), StoredAt = null, }; rebased.Add(parent); + previous = replay.Commits[i]; } + if (differences.Count > 0 && rebased.Count > 0) rebased[^1] = rebased[^1] with { Kind = ProjectVersionKind.Merge }; var placed = ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(merge.Merged.ToJson()), photos), allowNewer: true); var moved = WithBase(bundle, head) with { Project = placed, Saved = DateTimeOffset.UtcNow }; + await FinishCopyAsync(project, home, chain, cancellationToken).ConfigureAwait(false); var journal = new WorkspaceReplay(head.Id, [.. locals.Select(v => v.Id)], [.. rebased.Select(v => v.Id)], [.. held.Select(v => v.Id)]); project.WriteSync(project.ReadSync() with { Replay = journal }); Step("replay.journal"); @@ -537,12 +553,47 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) project.WriteBundle(moved); Step("replay.base"); await FinishReplayAsync(project, journal, held, home, cancellationToken).ConfigureAwait(false); - project.WriteSync(project.ReadSync() with { Replay = null }); + // A save-both cut short whose copy got the rest above is over with the conflict it was for. + project.WriteSync(project.ReadSync() with { Replay = null, SaveBoth = null }); if (takeTheirs is EveryPath && kept is not null) kept(ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(keptMine.ToJson()), photos), allowNewer: true)); return (moved, null, theirs); } + /// + /// A conflict met past local commits the home holds (their answer lost): the base moves onto the last of them, + /// (their files adopted once it has), so the commits after them stay in the chain from the base + /// and the conflict is settled against it. Journaled like a replay. + /// + private async Task MoveOntoHeldAsync(WorkspaceProject project, ProjectBundle bundle, ProjectVersion onto, IReadOnlyList held, + IProjectHome home, CancellationToken cancellationToken) + { + var journal = new WorkspaceReplay(onto.Id, [], [], [.. held.Select(v => v.Id)]); + var sync = project.ReadSync(); + // A save-both cut short goes on from the new base: the move is not its commit point (SettleAsync). + project.WriteSync(sync with { Replay = journal, SaveBoth = sync.SaveBoth is { } both ? both with { FromBase = onto.Id } : null }); + Step("replay.journal"); + bundle = WithBase(bundle, onto); + project.WriteBundle(bundle); + Step("replay.base"); + await FinishReplayAsync(project, journal, held, home, cancellationToken).ConfigureAwait(false); + return bundle; + } + + /// + /// The conflict recorded, while the home holds no version after the base: it lost them (an index taken back from an + /// older backup, a home answering late), so there is nothing to settle it against and it stays. One the base has reached + /// since (a replay that settled it, cut before it said so) is over: null. + /// + private static WorkspaceConflict? StillRecorded(WorkspaceProject project, ProjectBundle bundle) + { + if (project.ReadSync().Conflict is not { } recorded) return null; + var basis = bundle.Workspace!.Base; + if (basis is not null && (basis.VersionId == recorded.TheirsVersionId || basis.Number >= recorded.TheirsNumber)) return null; + Log.Info("workspace", $"The home holds no version of project {project.ProjectId} after the base: its conflict stays"); + return recorded; + } + /// /// The workspace's media with their photos named as the home names them now (), /// written back when the home taught them anything, so the versions, the working state and every comparison after this @@ -564,28 +615,37 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) /// last version's), the project moves onto the head as it is, read-only when that is the newer one. Otherwise the local /// versions and the working state stay as they are and a conflict of format waits (): /// Save both sends them to a new project made from the base, in this Chronos's format. A choice to merge them - /// ( set: keep mine, take the server's) is refused, the conflict left recorded. + /// ( set: keep mine, take the server's) is refused, the conflict left recorded. As for any + /// conflict, one met past local commits the home holds (: is the last + /// of them) first moves the base onto them, so the conflict, the chain and the base name one version. /// /// A choice to merge was made. - private static (ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList Pulled) NewerAtHome(WorkspaceProject project, - ProjectBundle bundle, WorkspaceMedia? media, ProjectVersion? tipVersion, IReadOnlyList locals, ProjectVersion? basis, ProjectVersion head, - IReadOnlyList theirs, IReadOnlySet? takeTheirs) + private async Task<(ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList Pulled)> NewerAtHomeAsync(WorkspaceProject project, + ProjectBundle bundle, IProjectHome home, WorkspaceMedia? media, ProjectVersion? tipVersion, IReadOnlyList chain, + IReadOnlyList locals, ProjectVersion? basis, bool pastHeld, IReadOnlyList held, ProjectVersion head, + IReadOnlyList theirs, IReadOnlySet? takeTheirs, CancellationToken cancellationToken) { var photos = media?.Photos ?? NoFrames; var tip = tipVersion?.SettingsJson ?? ProjectContent.ToPortable(new ProjectSettings(), photos); if (locals.Count == 0 && (bundle.IsReadOnly || ProjectContent.SameChoices(tip, Portable(bundle.Project, media) ?? throw MediaMissing(project.ProjectId), photos))) { + await FinishCopyAsync(project, home, chain, cancellationToken).ConfigureAwait(false); var placed = ProjectSettings.FromJson(ProjectContent.Place(head.SettingsJson, photos), allowNewer: true); var moved = WithBase(bundle, head) with { Project = placed, Saved = DateTimeOffset.UtcNow }; project.WriteBundle(moved); + // Moved onto the head: the local files of versions the home held take its copies, as after a replay. + if (held.Count > 0) + await FinishReplayAsync(project, new WorkspaceReplay(head.Id, [], [], [.. held.Select(v => v.Id)]), held, home, cancellationToken).ConfigureAwait(false); + if (project.ReadSync().SaveBoth is not null) project.WriteSync(project.ReadSync() with { SaveBoth = null }); return (moved, null, theirs); } + if (pastHeld) bundle = await MoveOntoHeldAsync(project, bundle, basis!, held, home, cancellationToken).ConfigureAwait(false); var newer = Math.Max(ProjectContent.FormatOf(head.SettingsJson), basis is null ? 0 : ProjectContent.FormatOf(basis.SettingsJson)); var format = new SettingsDifference(ProjectMerge.FormatPath, ProjectContent.FormatOf(tip).ToString(CultureInfo.InvariantCulture), newer.ToString(CultureInfo.InvariantCulture)); var conflict = new WorkspaceConflict(head.Id, head.Number, basis?.Id ?? Guid.Empty, [.. locals.Select(v => v.Id)], [format], DateTimeOffset.UtcNow); - project.WriteSync(project.ReadSync() with { Conflict = conflict }); + project.WriteSync(project.ReadSync() with { Conflict = conflict, Replay = null }); if (takeTheirs is not null) throw new WorkspaceSyncException(PushOutcome.NewerFormat, $"Version {head.Number} of project {project.ProjectId} holds settings of a newer Chronos: nothing of this workspace's is merged into it (save both, or update Chronos)."); @@ -613,10 +673,25 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) /// Finishes or undoes what a cut left (see the class remarks), before anything reads the chain: a replay's journal is /// finished once the base is its target, else its replayed versions are set aside (the old chain still hangs from the /// base); a save-both whose project moved onto the home's head sets its sent versions aside and clears its conflict; a - /// local file of the version the base names (a push cut after it moved the base) takes the home's copy. + /// local file of the version the base names (a push cut after it moved the base) takes the home's copy. A version file + /// that cannot be read takes the home's copy when the home holds that version (the damaged file is kept aside, in + /// versions/.replaced); one the home never had stays, a gap every sync then stops at (). /// private static async Task SettleAsync(WorkspaceProject project, IProjectHome home, CancellationToken cancellationToken) { + foreach (var unreadable in project.UnreadableVersions()) + { + if (await home.GetVersionAsync(project.ProjectId, unreadable.Id, cancellationToken).ConfigureAwait(false) is not { StoredAt: not null } copy + || copy.Id != unreadable.Id) + continue; + Verify(copy); + project.SetAside(unreadable.Path); + // A local version the home stored (its answer lost) with versions saved after it stays in their chain, unsent, + // until the next push hears "already stored"; any other is the home's version it is. + var parentOfUnsent = project.Versions().Any(v => v.StoredAt is null && v.ParentId == copy.Id); + project.WriteVersion(parentOfUnsent ? copy with { StoredAt = null } : copy, replace: true); + Log.Warning("workspace", $"Version file {unreadable.Path} could not be read ({unreadable.Reason}); it took the home's copy and was kept aside"); + } var sync = project.ReadSync(); var basis = project.ReadBundle().Workspace!.Base; if (sync.Replay is { } replay) @@ -671,7 +746,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) /// 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 (): 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. + /// 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 (). /// private async Task SaveBothAsync(WorkspaceProject project, ProjectBundle bundle, IProjectHome home, string? name, CancellationToken cancellationToken) { @@ -679,18 +756,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) if (!bundle.IsReadOnly) CommitLocked(project, bundle, null, null); bundle = project.ReadBundle(); var locals = Unpushed(project, bundle); - var sync = project.ReadSync(); - var pending = sync.SaveBoth; - SequenceProject? copy = null; - if (pending is not null) - { - copy = (await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == pending.CopyId); - // A copy deleted at the home meanwhile: a new one is made. - if (copy is null) pending = null; - } + 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; var pushed = new List(); - if (locals.Count > 0) + for (var attempt = 1; locals.Count > 0; attempt++) { + var journaled = pending is not null; if (pending is null) { var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false); @@ -699,23 +772,24 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) pending = new WorkspaceSaveBoth(copy.Id, first.Id, first.Number, workspace.Base?.VersionId, []); } var known = pending.Moves.Select(m => m.LocalId).ToHashSet(); - if (locals.Any(l => !known.Contains(l.Id)) || !ReferenceEquals(pending, sync.SaveBoth)) + if (!journaled || locals.Any(l => !known.Contains(l.Id))) { pending = pending with { Moves = [.. pending.Moves, .. locals.Where(l => !known.Contains(l.Id)).Select(l => new WorkspaceMove(l.Id, Guid.CreateVersion7()))] }; - project.WriteSync(sync with { SaveBoth = pending }); + project.WriteSync(project.ReadSync() with { SaveBoth = pending }); } Step("saveboth.journal"); - var ids = pending.Moves.ToDictionary(m => m.LocalId, m => m.CopyVersionId); - var (parentId, parentNumber) = (pending.CopyBaseId, pending.CopyBaseNumber); - foreach (var local in locals) + try { - var moved = local with { Id = ids[local.Id], ProjectId = copy!.Id, ParentId = parentId, Number = parentNumber + 1, StoredAt = null }; - var result = await home.PushAsync(copy.Id, moved, cancellationToken).ConfigureAwait(false); - if (result is not { Outcome: PushOutcome.Stored or PushOutcome.AlreadyStored, Version: { } stored }) - throw new WorkspaceSyncException(result.Outcome, $"The home refused version {local.Number} saved as project '{copy.Name}': {result.Outcome}."); - pushed.Add(stored); - (parentId, parentNumber) = (stored.Id, stored.Number); - Step("saveboth.pushed"); + pushed = await SendToCopyAsync(project, home, copy!, pending, locals, cancellationToken).ConfigureAwait(false); + break; + } + catch (CopyMovedException) + { + if (attempt >= 2) + throw new WorkspaceSyncException(PushOutcome.Diverged, $"Project '{copy!.Name}' at the home kept moving while project {project.ProjectId} was saved into it."); + // Somebody sent a version to the copy between our check and our push: it is theirs; a new one takes ours. + Log.Info("workspace", $"Save both of project {project.ProjectId}: project '{copy!.Name}' at the home moved meanwhile; a new copy is made"); + pending = null; } } // This project takes the home's head: the local versions now live in the copy (and their files in versions/.replaced). @@ -732,6 +806,83 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), pushed, [head], copy); } + /// + /// The copy a save-both cut short made () 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). + /// + private static async Task 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) + { + 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; + 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; + } + + /// + /// Sends 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). + /// + /// The copy's head is not the parent sent: somebody else sent it a version. + /// The home refused a version for good. + private async Task> SendToCopyAsync(WorkspaceProject project, IProjectHome home, SequenceProject copy, WorkspaceSaveBoth pending, + IReadOnlyList locals, CancellationToken cancellationToken) + { + var ids = pending.Moves.ToDictionary(m => m.LocalId, m => m.CopyVersionId); + var (parentId, parentNumber) = (pending.CopyBaseId, pending.CopyBaseNumber); + var pushed = new List(locals.Count); + foreach (var local in locals) + { + var moved = local with { Id = ids[local.Id], ProjectId = copy.Id, ParentId = parentId, Number = parentNumber + 1, StoredAt = null }; + var result = await home.PushAsync(copy.Id, moved, cancellationToken).ConfigureAwait(false); + if (result.Outcome == PushOutcome.Diverged) throw new CopyMovedException(); + if (result is not { Outcome: PushOutcome.Stored or PushOutcome.AlreadyStored, Version: { } stored }) throw Refused(result.Outcome, local, $"project '{copy.Name}'"); + Verify(stored); + pushed.Add(stored); + (parentId, parentNumber) = (stored.Id, stored.Number); + Step("saveboth.pushed"); + } + Log.Info("workspace", $"{pushed.Count} version(s) of project {project.ProjectId} are in its copy '{copy.Name}' at the home"); + return pushed; + } + + /// + /// 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). + /// + private async Task FinishCopyAsync(WorkspaceProject project, IProjectHome home, IReadOnlyList 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; + try + { + await SendToCopyAsync(project, home, copy, pending, sent, cancellationToken).ConfigureAwait(false); + } + catch (CopyMovedException) + { + Log.Info("workspace", $"Project '{copy.Name}' took another's version while a save-both of project {project.ProjectId} was finished: it is left as it is"); + } + } + + /// The home answered a save-both's copy moved under it (). + private sealed class CopyMovedException() : Exception("The copy moved at the home."); + + /// A push the home refused for good: too large for a version, or refused otherwise ( says why). + private static WorkspaceSyncException Refused(PushOutcome outcome, ProjectVersion local, string target) => outcome switch + { + PushOutcome.TooLarge => new WorkspaceSyncException(outcome, + $"Version {local.Number} of {target} is larger than the {ProjectSidecars.MaxVersionBytes >> 20} MB a version may be: the home did not store it."), + _ => new WorkspaceSyncException(outcome, $"The home refused version {local.Number} of {target}: {outcome}."), + }; + /// A new project at the home named , else " (2)", "(3)"… whichever is free. private static async Task CreateNamedAsync(IProjectHome home, string? name, string source, Guid? from, CancellationToken cancellationToken) { @@ -758,6 +909,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) var portable = Portable(bundle.Project, media) ?? throw MediaMissing(project.ProjectId); var hash = ProjectContent.Hash(portable); var locals = Unpushed(project, 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); @@ -809,8 +962,10 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) { media = null; } - var conflict = project.ReadSync().Conflict; + var sync = project.ReadSync(); + var conflict = sync.Conflict; var locals = Unpushed(project, bundle); + var damaged = Gaps(project, bundle, locals, sync).Count; ProjectVersion? head = null; HomeFrames? frames = null; var offline = false; @@ -841,6 +996,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) // 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 + : damaged > 0 ? WorkspaceState.Damaged : conflict is not null ? WorkspaceState.Conflict : offline ? WorkspaceState.Offline : behind > 0 ? WorkspaceState.Behind @@ -850,6 +1006,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) return new WorkspaceStatus(state, modified, locals.Count, behind, offline, media is null, bundle.IsReadOnly, conflict, workspace.Base, head) { MediaOutdated = outdated, + Damaged = damaged, }; } @@ -879,6 +1036,37 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) } } + /// + /// The version files after the base the chain of versions to send () 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. + /// + private static List Gaps(WorkspaceProject project, ProjectBundle bundle, IReadOnlyList chain, WorkspaceSyncState sync) + { + 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)) + .Select(project.VersionPath)); + return gaps; + } + + /// + /// Refuses to save, send or take anything while a version saved here after the base cannot be read or does not follow + /// from it (): the chain would go on without it and the versions after it. + /// + /// There is such a version; the log names its file. + private static void EnsureChained(WorkspaceProject project, ProjectBundle bundle, IReadOnlyList chain) + { + var gaps = Gaps(project, bundle, chain, project.ReadSync()); + if (gaps.Count == 0) return; + Log.Warning("workspace", $"Project {project.ProjectId} has versions saved here the chain from its base does not reach: {string.Join(", ", gaps)}"); + throw new InvalidDataException($"Project {project.ProjectId} has {gaps.Count} version file(s) after its base that cannot be read or do not follow from it: {gaps[0]}."); + } + /// /// Whether the working state's portable form holds what the last version does (the last local one, else the base), /// once each names its photos as the media hold them now: a version saved before the media were copied again after a diff --git a/tests/Chronos.App.Tests/Workspace/WorkspaceEditorTests.cs b/tests/Chronos.App.Tests/Workspace/WorkspaceEditorTests.cs index 27d9d530..12ff6d73 100644 --- a/tests/Chronos.App.Tests/Workspace/WorkspaceEditorTests.cs +++ b/tests/Chronos.App.Tests/Workspace/WorkspaceEditorTests.cs @@ -1,7 +1,9 @@ using System.IO; +using Chronos.App.Editor; using Chronos.App.Infrastructure; using Chronos.App.Workspace; using Chronos.Core.Imaging; +using Chronos.Core.Imaging.Decoders; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; @@ -64,6 +66,26 @@ public sealed class WorkspaceEditorTests(TestContext context) } } + [Test] + public async Task AProjectASyncKeepsBusyOpensOnceItIsFree() + { + await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 3); + var host = bed.Host(); + var projectId = await bed.CheckoutAsync(host); + var impatient = new WorkspaceSync(host.Store, "Chronos test") { LockWait = TimeSpan.FromMilliseconds(200) }; + var source = new WorkspaceSequenceSource(impatient, projectId, bed.Db, DecoderFactory.CreateClient(null), bed.Engine); + await using (source) + { + // A long sync or copy holds the project: the editor says so in words, and nothing was read. + using (await host.Store.Find(projectId)!.LockAsync(TimeSpan.FromSeconds(TestTiming.Budget(5)), bed.Ct)) + { + var busy = await Assert.ThrowsAsync(() => source.LoadAsync(bed.Ct)); + Assert.Equal(Loc.L("Errors.Workspace.Busy"), busy.Message); + } + Assert.Equal(3, (await source.LoadAsync(bed.Ct)).Frames.Count); + } + } + [Test] public async Task SaveVersionSendsItAndTheLibraryTakesMain() { diff --git a/tests/Chronos.App.Tests/Workspace/WorkspaceMenuTests.cs b/tests/Chronos.App.Tests/Workspace/WorkspaceMenuTests.cs index 7303f4e3..a823fc53 100644 --- a/tests/Chronos.App.Tests/Workspace/WorkspaceMenuTests.cs +++ b/tests/Chronos.App.Tests/Workspace/WorkspaceMenuTests.cs @@ -5,7 +5,9 @@ using Chronos.App.Workspace; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; +using Chronos.Data.Projects; using Chronos.Data.Sync; +using Chronos.Ui.Controls; using Chronos.Data.Workspaces; using Chronos.Testing; @@ -258,6 +260,25 @@ public sealed class WorkspaceMenuTests(TestContext context) Assert.Equal((WorkspaceState.Behind, 2), (ProjectMenuViewModel.WithLocal(behind, edited).State, ProjectMenuViewModel.WithLocal(behind, edited).Behind)); Assert.Equal(WorkspaceState.Modified, ProjectMenuViewModel.WithLocal(behind, edited with { Base = basis with { VersionId = Guid.NewGuid() } }).State, "a base that moved since says nothing of how far the library is ahead"); + // A version saved here that cannot be read outranks what the library said. + Assert.Equal(WorkspaceState.Damaged, ProjectMenuViewModel.WithLocal(behind, edited with { Damaged = 1 }).State); + } + + [Test] + public void ADamagedVersionAndOneTooLargeReadAsSuch() + { + var damaged = new WorkspaceStatus(WorkspaceState.Damaged, false, 1, 0, false, false, false, null, null, null) { Damaged = 1 }; + Assert.Equal((Loc.L("Editor.Workspace.State.Damaged"), StatusKind.Error), (WorkspaceText.State(damaged), WorkspaceText.Kind(damaged))); + Assert.Equal(Loc.L("Tooltips.Editor.Workspace.State.Damaged"), WorkspaceText.StateToolTip(damaged)); + Assert.Equal(Loc.L("Errors.Workspace.Damaged"), WorkspaceText.Failure(new InvalidDataException("a version file cannot be read"))); + + // Too large: a version saved here (refused before it was written) and one the library refused say the same, with the limit. + var tooLarge = Loc.L("Errors.Workspace.VersionTooLarge", ProjectSidecars.MaxVersionBytes >> 20); + Assert.Contains("64", tooLarge); + Assert.Equal(tooLarge, WorkspaceText.Failure(new ProjectVersionTooLargeException("over the limit"))); + Assert.Equal(tooLarge, WorkspaceText.Failure(new WorkspaceSyncException(PushOutcome.TooLarge, "over the limit"))); + Assert.Equal(tooLarge, Chronos.App.Shell.ErrorText.For(new ProjectVersionTooLargeException("over the limit"))); + Assert.Equal(Loc.L("Errors.Workspace.Refused"), WorkspaceText.Failure(new WorkspaceSyncException(PushOutcome.Invalid, "not this project's"))); } [Test] diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs index 487d1332..9c8cde30 100644 --- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs @@ -115,10 +115,17 @@ internal sealed class CuttingHome(IProjectHome home) : IProjectHome /// The push (counted from 1) the home stores and whose answer is lost. public int? LoseAnswer { get; set; } + /// What the home answers every push with, storing nothing (a home with a smaller limit, say); null to store. + public PushOutcome? Answer { get; set; } + + /// The home answers it holds no versions after any number (an index taken back from an older backup). + public bool Forgets { get; set; } + public async Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) { var push = ++Pushes; if (push == Refuse) throw new HomeUnavailableException("The home cannot be reached."); + if (Answer is { } answer) return new PushResult(answer, null, await home.HeadAsync(projectId, cancellationToken)); var result = await home.PushAsync(projectId, version, cancellationToken); if (push == LoseAnswer) throw new HomeUnavailableException("The answer was lost."); return result; @@ -136,7 +143,7 @@ internal sealed class CuttingHome(IProjectHome home) : IProjectHome home.GetVersionAsync(projectId, versionId, cancellationToken); public Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) => - home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken); + Forgets ? Task.FromResult>([]) : home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken); public Task> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) => home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken); diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceRecoveryTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceRecoveryTests.cs index 9a96501b..70d8500b 100644 --- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceRecoveryTests.cs +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceRecoveryTests.cs @@ -159,8 +159,257 @@ public abstract class WorkspaceRecoveryTests(TestContext context) : DatabaseTest Assert.Equal(WorkspaceState.InSync, (await one.PushAsync(id, fx.Home, fx.Ct)).Status.State); var head = Settings(await fx.Home.HeadAsync(id, fx.Ct)); Assert.Equal((1.0, StackSetting.Mean), (head.Exposure.ExposureOffsetEv, head.Stack.Mode)); - // Taking the server's first saves the unsaved offset as a version (kept for its undo); the versions before it reach the home in order. - Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1).Take(3)); + // Taking the server's first saves the unsaved offset as a version, kept aside for its undo: replayed it holds the + // server's values only, so it is not sent. + Assert.SequenceEqual(["first", "other", "second"], (await NotesAsync(fx, id)).Skip(1)); + Assert.Contains(Directory.GetFiles(Path.Combine(one.Store.Project(id).VersionsDirectory, WorkspaceProject.ReplacedFolder)), + f => ProjectSidecars.ReadVersion(f) is { Note: null } saved && Settings(saved).Exposure.ExposureOffsetEv == 2.0); + } + + /// + /// A conflict this workspace stopped at: Main moved at the home (offset 0.5), and three saved "bright" (offset 2) + /// and then changed the stack without saving a version. + /// + private static async Task<(WorkspaceSync Three, WorkspaceStore Store, WorkspaceProject Project, Guid Id)> ConflictAsync(WorkspaceFixture fx) + { + var (_, one) = fx.Workspace("one"); + var (store, three) = fx.Workspace("three"); + var id = (await fx.CheckoutAsync(one)).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); + await EditAsync(three, id, s => s with { Stack = new StackSettings { Mode = StackSetting.StarTrails } }, fx.Ct); + Assert.Equal(WorkspaceState.Conflict, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State); + return (three, store, project, id); + } + + [Test] + public async Task ASaveBothRetryMakesANewCopyWhenSomebodySentAVersionToTheHalfMadeOne() + { + var fx = await FixtureAsync(); + var (three, _, project, id) = await ConflictAsync(fx); + await Assert.ThrowsAsync(() => + three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct)); + var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)"); + + // Another PC opens the half-made copy and sends a version to it: it is theirs now. + var (_, two) = fx.Workspace("two"); + await fx.CheckoutAsync(two, partial.Id); + await EditAsync(two, partial.Id, s => Offset(s, 4), fx.Ct); + await two.CommitAsync(partial.Id, "theirs", cancellationToken: fx.Ct); + await two.PushAsync(partial.Id, fx.Home, fx.Ct); + var taken = await fx.Home.HeadAsync(partial.Id, fx.Ct); + + // The retry leaves it as it is and sends every version of this workspace to a new copy. + var both = await three.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, saveBothName: "Main (other)", cancellationToken: fx.Ct); + var copy = Assert.NotNull(both.SavedAs); + Assert.NotEqual(partial.Id, copy.Id); + Assert.Equal("Main (other)", copy.Name); + var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct)); + Assert.Equal((2, StackSetting.StarTrails), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode)); + Assert.SequenceEqual(["bright", null], (await NotesAsync(fx, copy.Id)).Skip(1)); + Assert.Equal(taken.Id, (await fx.Home.HeadAsync(partial.Id, fx.Ct)).Id); + Assert.Equal(WorkspaceState.InSync, both.Status.State); + Assert.Equal(WorkspaceSyncState.None, project.ReadSync()); + Assert.Empty(project.Versions().Where(v => v.StoredAt is null)); + } + + [Test] + public async Task KeepMineAfterASaveBothCutShortFinishesItsCopy() + { + var fx = await FixtureAsync(); + var (three, _, project, id) = await ConflictAsync(fx); + await Assert.ThrowsAsync(() => + three.ResolveAsync(id, new CuttingHome(fx.Home) { Refuse = 2 }, ConflictChoice.SaveBoth, saveBothName: "Main (PC)", cancellationToken: fx.Ct)); + var partial = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (PC)"); + Assert.Equal(2, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count); + + // The user keeps theirs instead: the copy the cut left half-made gets the rest, and this project goes on. + var resolved = await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct); + Assert.Null(resolved.Status.Conflict); + Assert.Null(project.ReadSync().SaveBoth); + var copied = Settings(await fx.Home.HeadAsync(partial.Id, fx.Ct)); + Assert.Equal((2, StackSetting.StarTrails), (copied.Exposure.ExposureOffsetEv, copied.Stack.Mode)); + Assert.Equal(3, (await fx.Home.HistoryAsync(partial.Id, null, 10, fx.Ct)).Count); + Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State); + var main = Settings(await fx.Home.HeadAsync(id, fx.Ct)); + Assert.Equal((2, StackSetting.StarTrails), (main.Exposure.ExposureOffsetEv, main.Stack.Mode)); + Assert.Equal(2, (await fx.Home.ListAsync(fx.Ct)).Count); + } + + [Test] + public async Task AConflictStaysWhileTheHomeHoldsNoVersionAfterTheBase() + { + var fx = await FixtureAsync(); + var (three, _, project, id) = await ConflictAsync(fx); + var recorded = project.ReadSync().Conflict!; + + // The home answers it has nothing after the base (its index taken back from an older backup): nothing settles the conflict. + var forgetful = new CuttingHome(fx.Home) { Forgets = true }; + Assert.Equal(WorkspaceState.Conflict, (await three.PullAsync(id, forgetful, fx.Ct)).Status.State); + Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.KeepMine, cancellationToken: fx.Ct)).Status.State); + Assert.Equal(WorkspaceState.Conflict, (await three.ResolveAsync(id, forgetful, ConflictChoice.TakeServer, cancellationToken: fx.Ct)).Status.State); + Assert.Equal(recorded.TheirsVersionId, project.ReadSync().Conflict!.TheirsVersionId); + Assert.Equal("bright", three.Unpushed(id)[0].Note); + + // Back as it was, the home settles it as the user chooses. + await three.ResolveAsync(id, fx.Home, ConflictChoice.KeepMine, cancellationToken: fx.Ct); + Assert.Equal(WorkspaceState.InSync, (await three.PushAsync(id, fx.Home, fx.Ct)).Status.State); + var head = Settings(await fx.Home.HeadAsync(id, fx.Ct)); + Assert.Equal((2, StackSetting.StarTrails), (head.Exposure.ExposureOffsetEv, head.Stack.Mode)); + } + + [Test] + public async Task ASaveBothAfterAConflictMetPastAVersionTheHomeHeldSendsEveryVersion() + { + var fx = await FixtureAsync(); + var (store, one) = fx.Workspace("one"); + var (_, two) = fx.Workspace("two"); + var project = await fx.CheckoutAsync(one); + var id = project.ProjectId; + await fx.CheckoutAsync(two, id); + await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct); + var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!; + await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Mean } }, fx.Ct); + await one.CommitAsync(id, "second", cancellationToken: fx.Ct); + await Assert.ThrowsAsync(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct)); + await two.PullAsync(id, fx.Home, fx.Ct); + await EditAsync(two, id, s => Offset(s, 1.0), fx.Ct); + await two.CommitAsync(id, "other", cancellationToken: fx.Ct); + await two.PushAsync(id, fx.Home, fx.Ct); + await EditAsync(one, id, s => Offset(s, 2.0), fx.Ct); + var stopped = await one.PullAsync(id, fx.Home, fx.Ct); + Assert.Equal((WorkspaceState.Conflict, first.Id), (stopped.Status.State, stopped.Status.Conflict!.BaseVersionId)); + Assert.Equal(first.Id, stopped.Status.Base!.VersionId); + + // Save both, cut once this project moved onto the head: the next sync finishes it and nothing of this workspace's is lost. + await Assert.ThrowsAsync(() => WorkspaceFixture.Cutting(store, "saveboth.base").ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, + cancellationToken: fx.Ct)); + var settled = await one.PushAsync(id, fx.Home, fx.Ct); + Assert.Equal((WorkspaceState.InSync, 0), (settled.Status.State, settled.Status.Ahead)); + var copy = (await fx.Home.ListAsync(fx.Ct)).Single(p => p.Name == "Main (2)"); + Assert.SequenceEqual(["second", null], (await NotesAsync(fx, copy.Id)).Skip(1)); + var kept = Settings(await fx.Home.HeadAsync(copy.Id, fx.Ct)); + Assert.Equal((2.0, StackSetting.Mean), (kept.Exposure.ExposureOffsetEv, kept.Stack.Mode)); + Assert.Equal(1.0, (await one.OpenAsync(id, fx.Ct)).Working.Exposure.ExposureOffsetEv); + Assert.Equal(WorkspaceSyncState.None, project.ReadSync()); + Assert.Empty(project.Versions().Where(v => v.StoredAt is null)); + } + + [Test] + public async Task AConflictOfFormatMetPastAVersionTheHomeHeldMovesTheBaseOntoIt() + { + var fx = await FixtureAsync(3); + var (_, one) = fx.Workspace("one"); + var project = await fx.CheckoutAsync(one); + var id = project.ProjectId; + await EditAsync(one, id, s => Offset(s, 0.5), fx.Ct); + var first = (await one.CommitAsync(id, "first", cancellationToken: fx.Ct))!; + await EditAsync(one, id, s => s with { ExcludedFrames = [1] }, fx.Ct); + await one.CommitAsync(id, "second", cancellationToken: fx.Ct); + await Assert.ThrowsAsync(() => one.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct)); + var newer = NewerChronos.Push(fx.Archive, fx.Sequence, await fx.Home.HeadAsync(id, fx.Ct)); + + // The conflict, the base and the chain name one version: the one the home held. + var stopped = await one.PullAsync(id, fx.Home, fx.Ct); + Assert.True(stopped.Status.Conflict!.NewerFormat); + Assert.Equal((first.Id, first.Id, 1), (stopped.Status.Base!.VersionId, stopped.Status.Conflict.BaseVersionId, stopped.Status.Ahead)); + Assert.True(project.Versions().Single(v => v.Id == first.Id).StoredAt is not null, "its file took the home's copy"); + + var both = await one.ResolveAsync(id, fx.Home, ConflictChoice.SaveBoth, cancellationToken: fx.Ct); + var copy = Assert.NotNull(both.SavedAs); + Assert.SequenceEqual(["second"], (await NotesAsync(fx, copy.Id)).Skip(1)); + var kept = ProjectSettings.FromJson(ProjectContent.Place((await fx.Home.HeadAsync(copy.Id, fx.Ct)).SettingsJson, ProjectServiceTests.Photos(fx.Frames))); + Assert.Equal(0.5, kept.Exposure.ExposureOffsetEv); + Assert.SequenceEqual([1], kept.ExcludedFrames); + Assert.Equal((newer.Id, true), (both.Status.Base!.VersionId, both.Status.ReadOnly)); + Assert.Empty(project.Versions().Where(v => v.StoredAt is null)); + } + + [Test] + public async Task AVersionFileThatCannotBeReadStopsTheSyncAndTakesTheHomesCopyWhenItHasOne() + { + 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 first = (await sync.CommitAsync(id, "first", cancellationToken: fx.Ct))!; + await EditAsync(sync, id, s => Offset(s, 0.8), fx.Ct); + var second = (await sync.CommitAsync(id, "second", cancellationToken: fx.Ct))!; + await EditAsync(sync, id, s => Offset(s, 0.9), fx.Ct); + + // A version saved here and never sent cannot be read: shown, and nothing is saved, sent or taken past it. + var path = project.VersionPath(second); + var bytes = File.ReadAllBytes(path); + File.WriteAllText(path, "{ damaged"); + var status = await sync.StatusAsync(id, fx.Home, fx.Ct); + Assert.Equal((WorkspaceState.Damaged, 1), (status.State, status.Damaged)); + await Assert.ThrowsAsync(() => sync.CommitAsync(id, cancellationToken: fx.Ct)); + await Assert.ThrowsAsync(() => sync.PushAsync(id, fx.Home, fx.Ct)); + await Assert.ThrowsAsync(() => sync.PullAsync(id, fx.Home, fx.Ct)); + Assert.Equal(1, (await fx.Home.HistoryAsync(id, null, 10, fx.Ct)).Count); + Assert.True(File.Exists(path)); + + // Readable again (it was held open, say), it goes on as if nothing happened. + File.WriteAllBytes(path, bytes); + Assert.Equal(WorkspaceState.Ahead, (await sync.StatusAsync(id, fx.Home, fx.Ct)).State); + + // One the home stored (its answer lost) takes the home's copy at the next sync; the damaged file is kept aside. + await Assert.ThrowsAsync(() => sync.PushAsync(id, new CuttingHome(fx.Home) { LoseAnswer = 1 }, fx.Ct)); + File.WriteAllText(project.VersionPath(first), "{ damaged"); + Assert.Equal((WorkspaceState.Damaged, 2), ((await sync.StatusAsync(id, fx.Home, fx.Ct)).State, (await sync.StatusAsync(id, null, fx.Ct)).Damaged)); + Assert.Null((await sync.PullAsync(id, fx.Home, fx.Ct)).Status.Conflict); + await sync.CommitAsync(id, "third", cancellationToken: fx.Ct); + Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State); + Assert.SequenceEqual(["first", "second", "third"], (await NotesAsync(fx, id)).Skip(1)); + Assert.Contains(Directory.GetFiles(Path.Combine(project.VersionsDirectory, WorkspaceProject.ReplacedFolder)), + f => File.ReadAllText(f) == "{ damaged"); + } + + [Test] + public async Task AVersionTheHomeFindsTooLargeIsRefusedAsSuchAndWaits() + { + var fx = await FixtureAsync(3); + 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); + await sync.CommitAsync(id, "large", cancellationToken: fx.Ct); + var refused = await Assert.ThrowsAsync(() => sync.PushAsync(id, new CuttingHome(fx.Home) { Answer = PushOutcome.TooLarge }, fx.Ct)); + Assert.Equal(PushOutcome.TooLarge, refused.Outcome); + Assert.Equal("large", sync.Unpushed(id).Single().Note); + Assert.Equal(WorkspaceState.InSync, (await sync.PushAsync(id, fx.Home, fx.Ct)).Status.State); + } + + [Test] + public async Task ACopyThatFailsLeavesTheEditorsPhotosInPlace() + { + var fx = await FixtureAsync(); + var (store, sync) = fx.Workspace(); + var project = await fx.CheckoutAsync(sync); + var before = project.ReadMedia()!; + var frames = before.Frames.OrderBy(f => f.Index).ToList(); + var editors = project.MediaFile(frames[0].File!); + // The first frame's original is gone at the home, and the copy then fails on the second. + File.Delete(fx.Archive.Paths.FromRelative(fx.Frames.Single(f => f.Id == frames[0].Id).RelativePath)); + var blocked = project.MediaFile(frames[1].File!); + File.Delete(blocked); + Directory.CreateDirectory(blocked); + + await fx.CopyAsync(store, project.ProjectId, JobState.Failed); + Assert.True(File.Exists(editors), "the editor's photo of the first frame stays while the copy has not committed"); + Assert.Equal(frames[0].File, project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File); + + // A copy that commits without it removes it then. + Directory.Delete(blocked); + await fx.CopyAsync(store, project.ProjectId); + Assert.Null(project.ReadMedia()!.Frames.Single(f => f.Id == frames[0].Id).File); + Assert.False(File.Exists(editors)); + Assert.True(File.Exists(blocked)); } [Test]