Files
Chronos/src/Chronos.Data/Copies/CopyPlans.cs
T
Alby96andClaude Opus 5.5 2c06e7ac2b fix(copies): close the fourth verification round of owner review 6
The fourth verification round of the matches work found 18 defects in
the merge, Integrate and Matches surfaces. This commit fixes them:

- An Integrate job now carries the planning account's id, so its re-plan
  decides ownership the way the dialog did.
- A merge skips only a project the kept sequence holds a real copy of
  (name, offset head and history content) and re-points its renders to
  that copy.
- A merge runs its trash checks before the Links stage and records an
  undo, so an abandoned merge gives the other sequence back its link,
  session keys and pairs.
- The metered hold adopts automatic transfers that were paused before a
  restart.
- Apply in the Compare dialog re-plans when the plan is pending or stale.
- The Library's Two versions... lists the second versions a Union merge
  leaves, each with Drop... (dry run, confirmation, verified partner).
  A merge job's Open shows that list.
- The merge's typed confirmation is the invariant count, and the prompt
  names it in all 38 languages.
- Italian text uses Principale and Gemello. The Main project is named
  through WorkspaceText in the Projects part and in the twin tooltip.
- The session keys tooltip is split into two keys, and To trash... and
  Delete on server... say why they are disabled.
- The capture restores the editor account before the Matches scenes.
- Upload again follows SyncHost.CanSendSync (Upload with Edit).
- Matches rows trim archive names in the middle on a rebalanced grid;
  the Projects part rows trim, with the full text in the tooltip.
- The sample project targets use ProjectTransplant.FreeName.

DECISIONS, CHANGELOG, STATUS 8.7, the request doc and the 16 changed
capture baselines are updated.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 19:20:30 +01:00

243 lines
12 KiB
C#

using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
namespace Chronos.Data.Copies;
/// <summary>The user's choice for one photo under Choose each (nothing preselected: <see cref="Leave"/>).</summary>
public enum PhotoChoice
{
Leave = 0,
Bring = 1,
KeepBoth = 2,
TakeHere = 3,
TakeThere = 4,
}
/// <summary>What a plan does with one photo.</summary>
public enum PhotoAction
{
Send = 0,
Fetch = 1,
SendVersion = 2,
FetchVersion = 3,
TrashHere = 4,
TrashThere = 5,
}
/// <summary>Why a plan cannot be applied (each maps to one <c>Errors.Copies.*</c> key).</summary>
public enum CopyBlock
{
NeedsNewerServer,
NoUploadRight,
NoFetchRight,
NoEditRight,
NoDeleteRight,
LinkTaken,
NoSpace,
Busy,
ArchiveOffline,
DetailsUnchosen,
FolderMoved,
UnsentWorkspace,
BothLinked,
TrashBlocked,
KeptUnchosen,
ChangedFile,
/// <summary>Photos sent into a server sequence the account neither owns nor may edit (the server refuses that, 403).</summary>
NoAddRight,
/// <summary>The other sequence of a merge holds photos a verify flagged changed or damaged: they would go to the trash unchecked.</summary>
DamagedPhotos,
}
/// <summary>The choices of an Integrate: photo policy, per-photo choices (by photo key), per-detail sides (by path), projects.</summary>
public sealed record IntegrateChoices(CopyPhotoPolicy Photos, IReadOnlyDictionary<string, PhotoChoice> PerPhoto,
IReadOnlyDictionary<string, CopySide> Details, CopyProjects Projects);
/// <summary>One photo step of a plan: the photo's key, what is done, the frame it starts from, its name, size and SHA-256.</summary>
public sealed record PhotoStep(string Key, PhotoAction Action, Guid? FrameId, string FileName, long Bytes, string? Sha256);
/// <summary>A photo a trash step takes whose raw SHA-256 nothing else holds (no other frame of this index, nor the other side).</summary>
public sealed record LastCopy(string Key, CopySide Side, string FileName, long Bytes);
/// <summary>A project copied across: from which side, the source, the name it gets at the other home, its versions and head.</summary>
public sealed record ProjectStep(CopySide From, Guid SourceProjectId, string SourceName, string TargetName, int Versions, Guid SourceHeadId);
/// <summary>
/// The dry run of an Integrate of a local sequence with a server sequence (design §5c). Payload is the one portable payload
/// both sides get; LocalPayload and RemotePayload are each side's as planned (the apply-time content check).
/// </summary>
public sealed record IntegratePlan(Guid CopyId, string Server, Guid LocalId, Guid RemoteId, IntegrateChoices Choices,
IReadOnlyList<PhotoStep> Photos, IReadOnlyList<LastCopy> LastCopies, int LeftOut, SyncPayload? Payload,
SyncPayload? LocalPayload, SyncPayload? RemotePayload, int DetailChanges, IReadOnlyList<ProjectStep> Projects,
long FetchBytes, long SendBytes, long FreeBytes, long? QuotaLeftBytes, IReadOnlyList<CopyBlock> Blocks,
int LocalFramesVersion, int LocalSettingsVersion, int RemoteFramesVersion, int RemoteSettingsVersion, string Fingerprint)
{
public bool CanApply => Blocks.Count == 0;
public bool Destructive => Photos.Any(p => p.Action is PhotoAction.TrashHere or PhotoAction.TrashThere);
/// <summary>New photos move by themselves afterwards only when the plan leaves nothing on one side only (design §6.1).</summary>
public bool AutoPhotos => LeftOut == 0;
/// <summary>The context the plan was made with (rights, names): the job plans again with it (<see cref="IntegratePlanner"/>).</summary>
public CopyPlanContext? Context { get; init; }
/// <summary>
/// Photos whose server version stays for want of the right to trash it there (Delete, and owner or admin of the server
/// sequence): counted in <see cref="LeftOut"/>; the server's Main uses this PC's version of them.
/// </summary>
public int StaysForDelete { get; init; }
/// <summary>Photos whose server version the plan trashes there or excludes from the server's Main: the server uses this PC's version.</summary>
public int ServerUsesOurs { get; init; }
}
/// <summary>The choices of a merge of two local copies (projects are always copied across: the other's home goes to the trash).</summary>
public sealed record MergeChoices(CopyPhotoPolicy Photos, IReadOnlyDictionary<string, PhotoChoice> PerPhoto,
IReadOnlyDictionary<string, CopySide> Details);
/// <summary>A render of the dropped sequence that moves to the kept one (its row keeps its id).</summary>
public sealed record RenderStep(Guid RenderId, string FileName, long Bytes, string? Sha256);
/// <summary>The dry run of a merge of two local sequences into the kept one (design §5d).</summary>
public sealed record MergePlan(Guid CopyId, Guid KeepId, Guid DropId, MergeChoices Choices, IReadOnlyList<PhotoStep> Photos,
IReadOnlyList<LastCopy> LastCopies, IReadOnlyList<RenderStep> Renders, SyncPayload? Payload, int DetailChanges,
IReadOnlyList<ProjectStep> Projects, IReadOnlyList<string> SessionKeys, IReadOnlyList<string> SkippedSessionKeys,
IReadOnlyList<string> Links, DeletePlan Trash, long CopyBytes, long FreeBytes, IReadOnlyList<CopyBlock> Blocks,
int KeepFramesVersion, int KeepSettingsVersion, int DropFramesVersion, int DropSettingsVersion, string Fingerprint)
{
public bool CanApply => Blocks.Count == 0;
/// <summary><see cref="Links"/> by the names their servers are shown by.</summary>
public IReadOnlyList<string> LinkNames { get; init; } = [];
/// <summary>The name of the server both sequences are linked to (<see cref="CopyBlock.BothLinked"/>); null otherwise.</summary>
public string? BothLinkedServer { get; init; }
/// <summary>Photos the kept sequence holds that the other deleted: they stay in the kept one ("Deleted in the other").</summary>
public int DeletedInOther { get; init; }
}
/// <summary>
/// What the planners need to know of this PC and the signed-in account: names, rights (OwnsOrAdmin: the account is an admin
/// of the server, which owns every sequence there; owning one is decided per sequence from its owner and <see cref="UserId"/>),
/// whether the server keeps projects, the quota left, sequences with unsent workspace work, and every server's originals archive.
/// </summary>
public sealed record CopyPlanContext(string PcName, string? ServerName, bool CanUpload, bool CanFetch, bool CanEdit, bool CanDelete,
bool OwnsOrAdmin, bool KeepsProjects, long? QuotaLeft, IReadOnlySet<Guid> UnsentWorkspaceSequences,
IReadOnlySet<Guid> ServerOriginalsArchives)
{
/// <summary>The signed-in account's id: it owns a server sequence whose owner it is (null: unknown, signed out).</summary>
public Guid? UserId { get; init; }
}
/// <summary>The server-side dry run of moving photos of a pair's server sequence to the server's trash (design §6.2).</summary>
public sealed record TrashTherePlan(Guid CopyId, string Server, Guid RemoteId, IReadOnlyList<Guid> FrameIds, IReadOnlyList<string> Keys, int Photos,
long Bytes, int FramesVersion, IReadOnlyList<LastCopy> LastCopies);
/// <summary>The dry run of moving photos of a pair's local copy to its archive trash, with the ones no other sequence of this index holds.</summary>
public sealed record TrashHerePlan(FrameDeletePlan Plan, IReadOnlyList<LastCopy> LastCopies);
/// <summary>
/// The dry run of dropping one of the two versions one sequence keeps of a photo (owner Q3; a Union merge leaves them, and
/// its pair is gone): the version's frame goes to the sequence's archive trash, the other version (<see cref="PartnerFrameId"/>)
/// stays. Applied only while that other version is still there, usable and verified by content. <see cref="LastCopies"/>:
/// the version itself when no other sequence of this index holds its content.
/// </summary>
public sealed record OwnVersionDropPlan(FrameDeletePlan Plan, Guid PartnerFrameId, string PartnerFileName, string PartnerSha256,
IReadOnlyList<LastCopy> LastCopies);
/// <summary>
/// The dry run of dropping one version of a photo kept in two versions (design §4.1, Versions): the version
/// <see cref="Key"/> goes to the trash of both sides (this PC's copy and the server's, or both local copies of a merge),
/// the other version <see cref="PartnerKey"/> stays on both. Applied only while that other version is still held,
/// verified by content, on both sides.
/// </summary>
/// <param name="Other">The other local copy's dry run (a pair of two local copies); null for a server pair.</param>
/// <param name="RemoteFrameIds">The server's frames of the version (a server pair), at <see cref="RemoteFramesVersion"/>.</param>
public sealed record DropVersionPlan(Guid CopyId, string Key, string PartnerKey, FrameDeletePlan Here, FrameDeletePlan? Other, Guid? RemoteId,
IReadOnlyList<Guid> RemoteFrameIds, int RemotePhotos, long RemoteBytes, int RemoteFramesVersion, IReadOnlyList<LastCopy> LastCopies)
{
/// <summary>Every frame the drop moves, on both sides.</summary>
public int Photos => Here.Frames.Count + (Other?.Frames.Count ?? 0) + RemotePhotos;
}
/// <summary>
/// The journal of one project copied to another home: the intended name, when it was asked, the copy once made, the version
/// the copy was made from, and the versions pushed so far (source version id to the copy's).
/// </summary>
public sealed record TransplantStep(string Intent, DateTimeOffset Asked, Guid? TargetId, Guid? BaseId, int BaseNumber,
IReadOnlyList<TransplantMove> Moves);
/// <summary>A version of the source project and its copy at the other home.</summary>
public sealed record TransplantMove(Guid Source, Guid Copy);
/// <summary>Copies a project with its whole history from one home to another (ProjectTransplant).</summary>
public interface IProjectCopier
{
Task<SequenceProject> CopyAsync(IProjectHome from, SequenceProject source, IProjectHome to, string name,
TransplantStep? journal, Func<TransplantStep, ValueTask> save, CancellationToken ct);
}
/// <summary>Whether a sequence has workspace projects with unsent versions or unsaved work (a merge waits for them).</summary>
public interface IWorkspaceGuard
{
Task<bool> HasUnsentAsync(Guid sequenceId, CancellationToken ct);
}
/// <summary>The sequences changed since the plan was made: nothing was applied; the pair goes back to its previous state.</summary>
public sealed class StaleCopyPlanException : InvalidOperationException
{
public StaleCopyPlanException()
: base(Loc.L("Errors.Copies.StalePlan"))
{
}
public StaleCopyPlanException(string message)
: base(message)
{
}
public StaleCopyPlanException(string message, Exception inner)
: base(message, inner)
{
}
}
/// <summary>A sequence of the pair is already linked to another sequence of that server: nothing was changed.</summary>
public sealed class CopyLinkTakenException : InvalidOperationException
{
public CopyLinkTakenException()
: base(Loc.L("Errors.Copies.LinkTaken", string.Empty, string.Empty))
{
}
public CopyLinkTakenException(string message)
: base(message)
{
}
public CopyLinkTakenException(string message, Exception inner)
: base(message, inner)
{
}
/// <summary>The server key both sequences of a merge are linked to (<see cref="BothLinked"/>); null otherwise.</summary>
public string? Server { get; private init; }
/// <summary>The refusal for <paramref name="title"/> already linked to another sequence of <paramref name="server"/>.</summary>
public static CopyLinkTakenException For(string title, string server) => new(Loc.L("Errors.Copies.LinkTaken", title, server));
/// <summary>
/// The refusal of a merge whose two sequences are both linked to sequences of the server <paramref name="key"/>, worded
/// with <paramref name="name"/> (the caller resolves the key to the name the server is shown by; the key by default).
/// </summary>
public static CopyLinkTakenException BothLinked(string key, string? name = null) =>
new(Loc.L("Errors.Copies.BothLinked", name ?? key)) { Server = key };
}