feat(archive): merge two local sequences into the kept one (owner review 6, P4)

SequenceMerger plans the merge of a local pair (kept sequence, photos
copied by policy, last copies, renders, details, projects, session keys,
links, blocks and a fingerprint) and queues the MergeSequences job.
MergeJobHandler applies the confirmed plan through a journal, so every
stage resumes: copy and file the other's photos, move renders, hash-verify
every original of the other in the kept one (or list it as a last copy),
trash the kept one's losing versions, write the details
(SequencePayloadWriter), copy projects, move keys and links, then move the
other to its archive's trash only at the verified frames version. A cancel
drops the journal and returns the pair to its previous state.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-17 10:24:32 +01:00
1 parent 9912baf78a
commit b4070ddfd4
6 files changed
+2062

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
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>
/// 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 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>The dropped sequence's frames version the verify read: the trash refuses another.</summary>
public int? VerifiedDropFramesVersion { 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>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;