Files
Chronos/src/Chronos.Data/Archiving/MergeJournal.cs
T
Alby96andClaude Opus 5.5 bf7701d279 fix(copies): close the fifth verification round of owner review 6
The metered hold now records the jobs it paused in the index and forgets
any job the user pauses, resumes, retries or cancels, so it never resumes
a pause of the user's and still resumes its own after a restart.

Drop... reports why its dry run is blocked instead of asking first, and
dropping the version Main uses lets Main use the version that stays,
with its hero and keyframes. That version reads "In Main".

Merges:
- A re-plan no longer hashes the other's photos again.
- A resumed merge verifies photos flagged damaged while it waited.
- A merge is blocked while the other's folder holds photos the index
  has not read; this is checked again under the lease before the trash.
- Choose each offers Keep both, Kept and Other, and blocks until every
  differing photo is chosen.
- Classes are named by role.

The Compare dialog:
- Its headings stay over their columns.
- The Projects part scrolls.
- Every confirmation line carries its own prefix, and a merge's facts
  carry none.
- A server card in a match offers Compare... alone.

The Italian strings follow the glossary.

Finding 9 (a local drop counting 0 photos) is not reproduced; a test now
keeps the count at 2.

Refs #73

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

186 lines
8.3 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Chronos.Core.Archiving;
using Chronos.Core.IO;
using Chronos.Data.Copies;
using Chronos.Data.Sync;
namespace Chronos.Data.Archiving;
/// <summary>The stages of a merge, in the order they run (design §5d).</summary>
internal enum MergeStage
{
Copy,
Renders,
Verify,
Replace,
Details,
Projects,
Links,
Trash,
Done,
}
/// <summary>A photo of the dropped sequence a merge copies into the kept one: its key, the drop's frame, name, size and recorded SHA-256.</summary>
internal sealed record MergeCopy(string Key, PhotoAction Action, Guid DropFrameId, string FileName, long Bytes, string? Sha256);
/// <summary>A losing version of the kept sequence that goes to its photo trash once the winner (the drop's version) is in place.</summary>
internal sealed record MergeReplace(string Key, Guid KeepFrameId, string FileName, string WinnerSha256);
/// <summary>A drop original the verify found in the kept sequence: its SHA-256 now and the keep frame that holds it.</summary>
internal sealed record MergeVerified(string Sha256, Guid KeepFrameId);
/// <summary>
/// What a merge's links stage changes, read before it: the session keys each manifest held, the other's archive, and the
/// rows <see cref="FoundCopyStore.MoveToKeptAsync"/> moves (<see cref="MoveUndo"/>).
/// </summary>
internal sealed record MergeLinksUndo(List<string> KeepKeysBefore, List<string> DropKeysBefore, Guid DropArchiveId, MoveUndo Moved);
/// <summary>
/// The journal of a merge (owner review 6, #73; design §5d): <c>&lt;keep archive&gt;/.chronos/journal/merge-&lt;job:N&gt;.json</c>,
/// written atomically (temp + rename) after each step. It keeps what the confirmed plan decided (a resumed run never plans
/// again: its own steps moved the versions) and how far each stage came, so every stage resumes and nothing is done twice.
/// </summary>
internal sealed class MergeJournal
{
public Guid JobId { get; set; }
public Guid CopyId { get; set; }
public Guid KeepId { get; set; }
public Guid DropId { get; set; }
public string Fingerprint { get; set; } = string.Empty;
public MergeStage Stage { get; set; }
/// <summary>The kept sequence's settings version the plan read (the details are written only at it).</summary>
public int KeepSettingsVersion { get; set; }
/// <summary>The dropped sequence's frames version the plan read (checked before each photo and at the verify).</summary>
public int DropFramesVersion { get; set; }
/// <summary>
/// The dropped sequence's settings version the plan read: the trash refuses another (work sent home to the other since
/// the plan, its Main settings or details, would go to the trash unmerged). Null in a journal written before it was kept.
/// </summary>
public int? DropSettingsVersion { get; set; }
/// <summary>The kept sequence's statuses before the merge (what its follow-ups make again).</summary>
public int KeepStatus { get; set; }
public List<MergeCopy> Copies { get; set; } = [];
public List<MergeReplace> Replaces { get; set; } = [];
/// <summary>Raw SHA-256 of drop originals the plan leaves out of the kept sequence (last copies, losers, tombstoned in keep, converted).</summary>
public List<string> Absent { get; set; } = [];
public List<RenderStep> Renders { get; set; } = [];
public SyncPayload? Payload { get; set; }
public List<ProjectStep> Projects { get; set; } = [];
public List<string> SessionKeys { get; set; } = [];
/// <summary>Drop frame → the keep frame it was filed as (or that already held it).</summary>
public Dictionary<Guid, Guid> Committed { get; set; } = [];
/// <summary>Render → its archive-relative path in the kept sequence, written before the copy.</summary>
public Dictionary<Guid, string> RenderTargets { get; set; } = [];
public List<Guid> RendersMoved { get; set; } = [];
/// <summary>Drop frame → its SHA-256 as hashed by the verify and the keep frame that holds it.</summary>
public Dictionary<Guid, MergeVerified> Verified { get; set; } = [];
/// <summary>Drop frames the verify found to be another photo than keep's (unhashed, same size and time) and filed as a second version.</summary>
public List<Guid> SecondVersions { get; set; } = [];
/// <summary>Drop frames the verify found nothing of to lose: last copies the plan listed, or photos a third sequence holds.</summary>
public List<Guid> Settled { get; set; } = [];
/// <summary>
/// Raw SHA-256 of the drop photos the verify settled because a third sequence held them: asked again under the kept
/// sequence's lease before the trash (that sequence may have gone since). Those settled through <see cref="Absent"/> the
/// user confirmed as last copies and are not asked again.
/// </summary>
public List<string> SettledElsewhere { get; set; } = [];
/// <summary>The dropped sequence's frames version the verify read: the trash refuses another.</summary>
public int? VerifiedDropFramesVersion { get; set; }
/// <summary>
/// The dropped sequence's frames flagged Missing or Corrupt whose file was there at the verify, each hashed then and
/// settled (held by the kept sequence, a last copy, or held elsewhere): one flagged since keeps the trash from running.
/// </summary>
public List<Guid> FlaggedVerified { get; set; } = [];
public List<string> Replaced { get; set; } = [];
/// <summary>Keys of losing versions a refused trash step kept (reported at the end).</summary>
public List<string> KeptLosers { get; set; } = [];
public bool DetailsDone { get; set; }
public Dictionary<Guid, TransplantStep> ProjectSteps { get; set; } = [];
public List<Guid> ProjectsDone { get; set; } = [];
public bool KeepKeysWritten { get; set; }
public bool DropKeysRemoved { get; set; }
public bool LinksMoved { get; set; }
/// <summary>
/// What the links stage takes from the other sequence, read before its first change: a merge abandoned after it (a check
/// before the trash fails, a cancel) gives it back, so the other keeps its keys, links and pairs. Null before the stage.
/// </summary>
public MergeLinksUndo? LinksUndo { get; set; }
/// <summary>The journal of job <paramref name="jobId"/> in the archive at <paramref name="paths"/>.</summary>
public static string PathFor(ArchivePaths paths, Guid jobId) => Path.Combine(paths.JournalDirectory, $"merge-{jobId:N}.json");
/// <summary>Where the job stages the files it copies.</summary>
public static string StagingFor(ArchivePaths paths, Guid jobId) => Path.Combine(paths.StagingDirectory, $"merge-{jobId:N}");
/// <summary>The journal of job <paramref name="jobId"/>; null when there is none (a first run), or it cannot be read.</summary>
/// <exception cref="InvalidDataException">The journal is damaged: the merge stops rather than start again over half a merge.</exception>
public static MergeJournal? Read(ArchivePaths paths, Guid jobId)
{
var path = PathFor(paths, jobId);
if (!File.Exists(path)) return null;
try
{
return JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), MergeJournalJson.Default.MergeJournal);
}
catch (JsonException ex)
{
throw new InvalidDataException($"The merge journal {path} is damaged.", ex);
}
}
/// <summary>Writes the journal atomically.</summary>
public void Save(ArchivePaths paths)
{
Directory.CreateDirectory(paths.JournalDirectory);
AtomicFile.WriteAllText(PathFor(paths, JobId), JsonSerializer.Serialize(this, MergeJournalJson.Default.MergeJournal));
}
/// <summary>Deletes the journal and the staging folder of job <paramref name="jobId"/>.</summary>
public static void Delete(ArchivePaths paths, Guid jobId)
{
var staging = StagingFor(paths, jobId);
if (Directory.Exists(staging)) Directory.Delete(staging, recursive: true);
File.Delete(PathFor(paths, jobId));
}
}
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(MergeJournal))]
internal sealed partial class MergeJournalJson : JsonSerializerContext;