fix(workspace): newer-format editor stays usable; media learn the home's photo names

A pull onto a newer Chronos's settings made the editor throw: ProjectHistory.Replace
rebased every undo and redo snapshot through ProjectMerge, which refuses to merge a
newer format when both sides changed, so any non-empty undo stack broke the reload
after a sync that had succeeded. Replace now drops the steps instead when either side
(or a step) is of a newer format: the project is shown read-only and undo never brings
back values from before the newer settings.

The media learnt the home's hashes and conversion aliases only inside a replay, so a
status or Save version before that compared a SHA-named base with photo-named media
and read a false Modified or saved a version that only re-keyed photos. Now:
- ProjectContent.SameChoices also compares the texts as placed on the frames, hashed
  or not (Bound), so status and commit without a home agree with the base;
- a status asked of the home teaches the media its names (written under the lease, or
  when the lease is free), and so does a checkout of a project already here;
- WorkspaceStatus.MediaOutdated reports frames moved at the home, or names the media
  could not learn yet, and the project menu offers Update media for it (tooltip text
  updated in en and it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 11:19:21 +01:00
1 parent 8256b72913
commit 43b092bd5d
10 files changed
+321 -19

No files matched your search

+77 -9
View File
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Model;
using Chronos.Core.Project;
@@ -46,7 +47,16 @@ public enum WorkspaceState
/// <param name="Base">The home's version the project was last in step with.</param>
/// <param name="HomeHead">The home's newest version, when it was asked.</param>
public sealed record WorkspaceStatus(WorkspaceState State, bool Modified, int Ahead, int Behind, bool Offline, bool MediaRemoved, bool ReadOnly,
WorkspaceConflict? Conflict, BundleBase? Base, ProjectVersion? HomeHead);
WorkspaceConflict? Conflict, BundleBase? Base, ProjectVersion? HomeHead)
{
/// <summary>
/// The media are behind the home's frames, as the home was asked: frames added, removed or converted there since the copy
/// (Update media copies them again), or photos the home names otherwise now (hashed, or joined to another encoding,
/// there since) whose names the media could not learn yet because another operation held the project. Every sync
/// teaches the media the home's names; false without a home asked.
/// </summary>
public bool MediaOutdated { get; init; }
}
/// <summary>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.</summary>
/// <param name="SavedAs">After <see cref="ConflictChoice.SaveBoth"/>: the new project at the home holding this workspace's versions.</param>
@@ -134,7 +144,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
/// The project <paramref name="projectId"/> of <paramref name="home"/> brought into the workspace: its folder, the home's
/// head as the base and the working state (positions waiting for the media, which a
/// <see cref="JobKind.WorkspaceCopy"/> job copies: <see cref="WorkspaceJobs.Copy"/>). A project already here is left as
/// it is.
/// it is, but for its media's names of the photos (<see cref="WorkspaceMedia.NamedAsAt"/>).
/// </summary>
/// <exception cref="ProjectException">The home has no such project.</exception>
/// <exception cref="InvalidOperationException">A project of that id from another home is in the workspace.</exception>
@@ -150,6 +160,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
var existing = project.ReadBundle();
if (existing.Workspace!.Home.SequenceId != sequence.Home.SequenceId)
throw new InvalidOperationException($"Project {projectId} in the workspace belongs to another sequence.");
// Opened again: its media learn how the home names its photos now, as a sync's do (damaged ones are copied again).
try
{
await MediaNamedAsAtHomeAsync(project, home, cancellationToken).ConfigureAwait(false);
}
catch (InvalidDataException ex)
{
Log.Warning("workspace", $"The media index of workspace project {projectId} cannot be read", ex);
}
return project;
}
var listed = (await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == projectId)
@@ -378,11 +397,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
}
/// <summary>Where the project stands; with a <paramref name="home"/>, against the home's head too (offline when it cannot be reached).</summary>
/// <summary>
/// Where the project stands; with a <paramref name="home"/>, against the home's head too (offline when it cannot be
/// reached), the media naming the photos as the home does now (written when the project's lease is free).
/// </summary>
public async Task<WorkspaceStatus> StatusAsync(Guid projectId, IProjectHome? home = null, CancellationToken cancellationToken = default)
{
var project = Existing(projectId);
return await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false);
return await StatusLockedAsync(project, home, cancellationToken, leased: false).ConfigureAwait(false);
}
/// <summary>The versions this workspace saved and has not pushed yet, oldest first.</summary>
@@ -767,7 +789,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
return version;
}
private async Task<WorkspaceStatus> StatusLockedAsync(WorkspaceProject project, IProjectHome? home, CancellationToken cancellationToken)
/// <summary>
/// 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).
/// </summary>
private async Task<WorkspaceStatus> StatusLockedAsync(WorkspaceProject project, IProjectHome? home, CancellationToken cancellationToken, bool leased = true)
{
var bundle = project.ReadBundle();
var workspace = bundle.Workspace!;
@@ -782,22 +811,32 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
}
var conflict = project.ReadSync().Conflict;
var locals = Unpushed(project, bundle);
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)));
ProjectVersion? head = null;
HomeFrames? frames = null;
var offline = 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);
}
catch (HomeUnavailableException)
{
offline = true;
}
}
var outdated = false;
if (media is not null && frames is not null)
{
var named = media.NamedAsAt(frames);
if (!ReferenceEquals(named, media)) outdated = !await LearnNamesAsync(project, frames, leased, cancellationToken).ConfigureAwait(false);
outdated |= frames.FramesVersion != media.FramesVersion;
media = named;
}
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)));
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;
@@ -808,7 +847,36 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
: locals.Count > 0 ? WorkspaceState.Ahead
: modified ? WorkspaceState.Modified
: WorkspaceState.InSync;
return new WorkspaceStatus(state, modified, locals.Count, behind, offline, media is null, bundle.IsReadOnly, conflict, workspace.Base, head);
return new WorkspaceStatus(state, modified, locals.Count, behind, offline, media is null, bundle.IsReadOnly, conflict, workspace.Base, head)
{
MediaOutdated = outdated,
};
}
/// <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.
/// </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 (project.ReadMedia() is not { } media) return false;
var named = media.NamedAsAt(frames);
if (!ReferenceEquals(named, media)) project.WriteMedia(named);
return true;
}
catch (Exception ex) when (ex is LeaseUnavailableException or InvalidDataException)
{
return false;
}
finally
{
lease?.Dispose();
}
}
/// <summary>