feat(projects): workspace projects with linear versions, the local archive as their home

Owner review 2, phase 5a (R8): editing moves out of the library into a workspace on a fast disk, and a
sequence gets several projects, each with a linear version history pushed and pulled like git.

- Core: ProjectSettings keeps members a newer Chronos wrote (Extra) and reads a newer settings format
  read-only; ProjectContent is the portable form of a version (every frame position parked by photo,
  canonical text, SHA-256 content hash); ProjectBundle format 2 adds the workspace section, and a file
  without it is still written as format 1. JobKind.WorkspaceCopy = 20 (19 kept for Convert).
- Data: migration 0009 (projects, project_versions, the render's project columns); ProjectService on
  ArchiveService.Projects keeps projects as sidecars first (.chronos/projects/<id>/), then the index,
  under the sequence lease: create, rename, delete into the projects' trash, history, push with
  fast-forward, idempotent repeat, divergence and hash checks. Main is made from the sequence's settings
  and mirrored both ways, so every older code path keeps working; outside writes become "external"
  versions, a mirror a crash cut short is completed or merged. Full scans and rebuilds take the projects
  back from their sidecars.
- ProjectMerge moves from the App to Data/Projects, merges pending frames photo by photo and replays
  unpushed commits on a moved head.
- Workspaces: WorkspaceStore, WorkspaceSync (checkout, save, commit, push, pull with replay and merge,
  conflicts settled by keep mine / take server / save both, restore, status) and the WorkspaceCopy job
  (proxies copied hash-verified and resumable; originals refused until phase 5c).
- Renders of a project version place its positions on the frames and record the project and version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c4c1802884e403db8759ed0501f770a67a047f20)
This commit is contained in:
Alby96 committed 2026-10-27 22:51:32 +01:00
1 parent e3cbe30b29
commit 03d32f18d3
51 files changed
+4917 -108

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Chronos.Core.Project;
/// <summary>
/// The portable form of a project (owner review 2, R8): the settings with every frame position (exclusions, hero, camera
/// keyframes) parked by photo as <see cref="ProjectSettings.PendingFrames"/>, so the same text means the same thing on
/// every copy of the sequence (the archive, a workspace's proxies, a server) whatever frames were inserted or removed
/// since. Project versions store this form; a copy places the positions on its own frames when it opens one
/// (<see cref="Place"/>). The text is canonical (<see cref="Canonical"/>), so equal settings give equal text and an
/// equal <see cref="Hash"/>.
/// </summary>
public static class ProjectContent
{
private static readonly FramePhotos NoFrames = new([]);
/// <summary>
/// The portable form of <paramref name="settingsJson"/>, whose positions name the frames <paramref name="frames"/>
/// (by index). A position naming no frame of <paramref name="frames"/> names nothing and goes. A project of a newer
/// Chronos is only made canonical: its positions are its own business.
/// </summary>
/// <exception cref="JsonException">The text is not a project object.</exception>
public static string ToPortable(string settingsJson, FramePhotos frames)
{
ArgumentNullException.ThrowIfNull(frames);
var canonical = Canonical(settingsJson);
if (FormatOf(canonical) > ProjectSettings.CurrentVersion) return canonical;
// With no frames there is nothing to name: a translation to no frames would keep the positions as they are.
var translated = frames.Count == 0 ? WithoutPositions(canonical) : ProjectSettings.TranslateFramePositions(canonical, frames, NoFrames);
return translated is null ? canonical : Canonical(translated);
}
/// <inheritdoc cref="ToPortable(string, FramePhotos)"/>
public static string ToPortable(ProjectSettings settings, FramePhotos frames)
{
ArgumentNullException.ThrowIfNull(settings);
return ToPortable(settings.ToJson(), frames);
}
/// <summary>
/// The settings of a portable project with its positions placed on <paramref name="frames"/>: each pending frame whose
/// photo a frame holds becomes a position again; those whose photo is not there keep waiting.
/// </summary>
/// <exception cref="JsonException">The text is not a project object.</exception>
public static string Place(string portableJson, FramePhotos frames)
{
ArgumentNullException.ThrowIfNull(frames);
ArgumentNullException.ThrowIfNull(portableJson);
if (FormatOf(portableJson) > ProjectSettings.CurrentVersion) return portableJson;
return ProjectSettings.TranslateFramePositions(portableJson, null, frames) ?? portableJson;
}
/// <summary>
/// The canonical text of a project: read (a newer Chronos's too, its unknown members kept) and written again, its
/// pending frames in a fixed order (by photo) and their capture times as wall-clock values.
/// </summary>
/// <exception cref="JsonException">The text is not a project object.</exception>
public static string Canonical(string json)
{
ArgumentNullException.ThrowIfNull(json);
ProjectSettings settings;
try
{
settings = ProjectSettings.FromJson(json, allowNewer: true);
}
catch (InvalidDataException ex)
{
throw new JsonException(ex.Message, ex);
}
if (settings.PendingFrames is { Count: > 0 } pending)
{
settings = settings with { PendingFrames = [.. pending.Select(Normalized).OrderBy(Key, StringComparer.Ordinal)] };
}
return settings.ToJson();
}
/// <summary>The SHA-256 (lower-case hex) of the UTF-8 bytes of <paramref name="text"/>: a project version's content hash.</summary>
public static string Hash(string text)
{
ArgumentNullException.ThrowIfNull(text);
return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text)));
}
/// <summary>Whether <paramref name="hash"/> is the content hash of <paramref name="text"/> (any case).</summary>
public static bool Verifies(string text, string? hash) => hash is { Length: 64 } && string.Equals(Hash(text), hash, StringComparison.OrdinalIgnoreCase);
/// <summary>The settings format (<see cref="ProjectSettings.Version"/>) a project's text declares; 1 when it says none or is not a project.</summary>
public static int FormatOf(string? json)
{
if (string.IsNullOrWhiteSpace(json)) return ProjectSettings.CurrentVersion;
try
{
return JsonNode.Parse(json, new JsonNodeOptions { PropertyNameCaseInsensitive = true }) is JsonObject project
&& project["version"] is JsonValue value && value.TryGetValue<int>(out var version) && version > 0
? version
: ProjectSettings.CurrentVersion;
}
catch (Exception ex) when (ex is JsonException or ArgumentException)
{
return ProjectSettings.CurrentVersion;
}
}
/// <summary>
/// A pending frame as the canonical text holds it: its capture time as a wall-clock value, and the frame its keyframes
/// were on left out (0: it is set again when the photo takes its place).
/// </summary>
private static PendingFrame Normalized(PendingFrame frame) => frame with
{
CapturedAt = frame.CapturedAt is { } at ? DateTime.SpecifyKind(at, DateTimeKind.Unspecified) : null,
// Read from a hand-written entry without keyframes, the list is null.
Keyframes = frame.Keyframes is { Count: > 0 } keys ? [.. keys.Select(k => k with { Frame = 0 })] : [],
};
/// <summary>Order of pending frames in the canonical text: by hash, then capture time, size and which copy of the photo.</summary>
private static string Key(PendingFrame frame) => string.Create(CultureInfo.InvariantCulture,
$"{frame.Sha256?.ToLowerInvariant()}|{frame.CapturedAt?.Ticks:D20}|{frame.Size:D20}|{frame.Occurrence:D10}");
/// <summary>A project of a sequence without frames: its positions name nothing.</summary>
private static string? WithoutPositions(string json)
{
if (JsonNode.Parse(json) is not JsonObject project) return null;
var changed = false;
if (project["excludedFrames"] is JsonArray { Count: > 0 } excluded)
{
excluded.Clear();
changed = true;
}
if (project["heroFrame"] is not null)
{
project.Remove("heroFrame");
changed = true;
}
if (project["camera"] is JsonObject camera && camera["keyframes"] is JsonArray { Count: > 0 } keys)
{
keys.Clear();
changed = true;
}
return changed ? project.ToJsonString() : null;
}
}