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:
1 parent
e3cbe30b29
commit
03d32f18d3
51 files changed
+4917
-108
No files matched your search
@@ -0,0 +1,312 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Sync;
|
||||
|
||||
namespace Chronos.Data.Workspaces;
|
||||
|
||||
/// <summary>The manifest of a workspace folder (<c>workspace.json</c>).</summary>
|
||||
public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created);
|
||||
|
||||
/// <summary>A workspace's sync state of one project besides its versions (<c>sync.json</c>): a restore to record, a conflict to settle.</summary>
|
||||
/// <param name="RestoredFrom">The version a restore brought back; the next commit is a "restore" of it.</param>
|
||||
/// <param name="Conflict">The conflict a pull or push stopped at, until the user settles it.</param>
|
||||
public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? Conflict)
|
||||
{
|
||||
public static WorkspaceSyncState None { get; } = new(null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A pull or push that met real conflicts (owner review 2, R8): the home's head it met, the version the local commits were
|
||||
/// made from, the local commits (oldest first) and the members both sides changed (mine, theirs). Settled by keeping mine,
|
||||
/// taking the server's (each member or all), or saving both (<see cref="WorkspaceSync.ResolveAsync"/>).
|
||||
/// </summary>
|
||||
public sealed record WorkspaceConflict(Guid TheirsVersionId, int TheirsNumber, Guid BaseVersionId, IReadOnlyList<Guid> Mine,
|
||||
IReadOnlyList<SettingsDifference> Differences, DateTimeOffset Found);
|
||||
|
||||
/// <summary>
|
||||
/// Parameters of a <see cref="JobKind.WorkspaceCopy"/> job: target = the home's sequence; the workspace folder, the
|
||||
/// project whose media to copy and what to copy (<see cref="BundleMedia.Proxies"/>; originals in phase 5c).
|
||||
/// </summary>
|
||||
public sealed record WorkspaceCopyParameters(string WorkspaceRoot, Guid ProjectId, string Media);
|
||||
|
||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
UseStringEnumConverter = true, WriteIndented = true)]
|
||||
[JsonSerializable(typeof(WorkspaceManifest))]
|
||||
[JsonSerializable(typeof(WorkspaceMedia))]
|
||||
[JsonSerializable(typeof(WorkspaceSyncState))]
|
||||
[JsonSerializable(typeof(WorkspaceCopyParameters))]
|
||||
internal sealed partial class WorkspaceJson : JsonSerializerContext;
|
||||
|
||||
/// <summary>
|
||||
/// The workspace of this PC (owner review 2, R8): a folder on a fast disk where projects are edited, never inside the
|
||||
/// library. <c><root>/workspace.json</c> names it; each project has a folder named by its id
|
||||
/// (<see cref="WorkspaceProject"/>). There is no index: the store lists the folders holding a <c>project.chronos</c>. The
|
||||
/// desktop puts it in <c>%LOCALAPPDATA%\Chronos\workspace</c> unless set otherwise; the store takes any folder.
|
||||
/// </summary>
|
||||
public sealed class WorkspaceStore
|
||||
{
|
||||
public const string ManifestFile = "workspace.json";
|
||||
public const string FormatName = "chronos-workspace";
|
||||
public const int FormatVersion = 1;
|
||||
|
||||
/// <summary>The folder name of the workspace inside the program's data folder.</summary>
|
||||
public const string DefaultFolderName = "workspace";
|
||||
|
||||
/// <summary>Opens the workspace at <paramref name="root"/>, making the folder and its manifest when there is none.</summary>
|
||||
/// <exception cref="InvalidDataException">The folder's <c>workspace.json</c> is not a workspace manifest, or a newer one.</exception>
|
||||
/// <exception cref="IOException">The folder cannot be made or read.</exception>
|
||||
public WorkspaceStore(string root)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(root);
|
||||
Root = LongPath.Normalize(Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)));
|
||||
Directory.CreateDirectory(Root);
|
||||
var path = Path.Combine(Root, ManifestFile);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
var manifest = new WorkspaceManifest(FormatName, FormatVersion, Guid.CreateVersion7(), DateTimeOffset.UtcNow);
|
||||
// Another program opening the same folder at the same moment: the first manifest wins.
|
||||
try
|
||||
{
|
||||
using var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None);
|
||||
JsonSerializer.Serialize(stream, manifest, WorkspaceJson.Default.WorkspaceManifest);
|
||||
stream.Flush(flushToDisk: true);
|
||||
}
|
||||
catch (IOException) when (File.Exists(path))
|
||||
{
|
||||
}
|
||||
}
|
||||
Manifest = Read(path);
|
||||
}
|
||||
|
||||
public string Root { get; }
|
||||
|
||||
public WorkspaceManifest Manifest { get; }
|
||||
|
||||
public Guid Id => Manifest.Id;
|
||||
|
||||
/// <summary>The folder of a project, whether it exists or not.</summary>
|
||||
public WorkspaceProject Project(Guid projectId) => new(this, projectId);
|
||||
|
||||
/// <summary>The project's folder when it holds a working state; null otherwise.</summary>
|
||||
public WorkspaceProject? Find(Guid projectId)
|
||||
{
|
||||
var project = Project(projectId);
|
||||
return project.Exists ? project : null;
|
||||
}
|
||||
|
||||
/// <summary>The projects of the workspace: every folder named by a project id that holds a <c>project.chronos</c>.</summary>
|
||||
public IReadOnlyList<WorkspaceProject> List()
|
||||
{
|
||||
var found = new List<WorkspaceProject>();
|
||||
foreach (var directory in Directory.EnumerateDirectories(Root).Order(StringComparer.Ordinal))
|
||||
{
|
||||
if (Guid.TryParseExact(Path.GetFileName(directory), "N", out var id) && File.Exists(Path.Combine(directory, WorkspaceProject.BundleFile)))
|
||||
found.Add(Project(id));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/// <summary>The working states of the workspace's projects (one unreadable is left out, and logged).</summary>
|
||||
public IReadOnlyList<(WorkspaceProject Project, ProjectBundle Bundle)> ListBundles()
|
||||
{
|
||||
var bundles = new List<(WorkspaceProject, ProjectBundle)>();
|
||||
foreach (var project in List())
|
||||
{
|
||||
try
|
||||
{
|
||||
bundles.Add((project, project.ReadBundle()));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"The project in {project.Folder} cannot be read", ex);
|
||||
}
|
||||
}
|
||||
return bundles;
|
||||
}
|
||||
|
||||
private static WorkspaceManifest Read(string path)
|
||||
{
|
||||
WorkspaceManifest? manifest;
|
||||
try
|
||||
{
|
||||
manifest = JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), WorkspaceJson.Default.WorkspaceManifest);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"{path} is not a workspace manifest.", ex);
|
||||
}
|
||||
if (manifest is null || manifest.Format != FormatName || manifest.Id == Guid.Empty) throw new InvalidDataException($"{path} is not a workspace manifest.");
|
||||
if (manifest.FormatVersion > FormatVersion)
|
||||
throw new InvalidDataException($"{path} uses format {manifest.FormatVersion}, newer than this Chronos ({FormatVersion}).");
|
||||
return manifest;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One project in the workspace (owner review 2, R8): <c><root>/<project id>/</c> with
|
||||
/// <c>project.chronos</c> (the working state: a project file of format 2, its settings placed on the media's frames),
|
||||
/// <c>media/media.json</c> with <c>media/proxies/<frame id>.jpg</c> (originals later) and <c>media/analysis.json</c>
|
||||
/// (the home's deep report), <c>versions/<number>-<id>.json</c> (commits not pushed yet, and the versions pulled),
|
||||
/// <c>sync.json</c>, <c>exports/</c> and the lease <c>.lock.lease</c> that one writer at a time holds.
|
||||
/// </summary>
|
||||
public sealed class WorkspaceProject
|
||||
{
|
||||
public const string BundleFile = "project.chronos";
|
||||
public const string MediaFolder = "media";
|
||||
public const string MediaIndexFile = "media.json";
|
||||
public const string ProxiesFolder = "proxies";
|
||||
public const string OriginalsFolder = "originals";
|
||||
public const string VersionsFolder = "versions";
|
||||
public const string ExportsFolder = "exports";
|
||||
public const string SyncFile = "sync.json";
|
||||
|
||||
/// <summary>Old local commits replaced by their replay on top of the home's head: kept, never erased by Chronos.</summary>
|
||||
public const string ReplacedFolder = ".replaced";
|
||||
|
||||
private const string LeaseName = ".lock";
|
||||
|
||||
internal WorkspaceProject(WorkspaceStore store, Guid projectId)
|
||||
{
|
||||
Store = store;
|
||||
ProjectId = projectId;
|
||||
Folder = Path.Combine(store.Root, projectId.ToString("N"));
|
||||
}
|
||||
|
||||
public WorkspaceStore Store { get; }
|
||||
public Guid ProjectId { get; }
|
||||
public string Folder { get; }
|
||||
public string BundlePath => Path.Combine(Folder, BundleFile);
|
||||
public string MediaDirectory => Path.Combine(Folder, MediaFolder);
|
||||
public string MediaIndexPath => Path.Combine(MediaDirectory, MediaIndexFile);
|
||||
public string ProxiesDirectory => Path.Combine(MediaDirectory, ProxiesFolder);
|
||||
public string OriginalsDirectory => Path.Combine(MediaDirectory, OriginalsFolder);
|
||||
public string AnalysisPath => Path.Combine(MediaDirectory, ArchiveLayout.AnalysisFile);
|
||||
public string VersionsDirectory => Path.Combine(Folder, VersionsFolder);
|
||||
public string ExportsDirectory => Path.Combine(Folder, ExportsFolder);
|
||||
public string SyncPath => Path.Combine(Folder, SyncFile);
|
||||
|
||||
public bool Exists => File.Exists(BundlePath);
|
||||
|
||||
/// <summary>The file a frame's proxy is copied to, relative to <see cref="MediaDirectory"/>.</summary>
|
||||
public static string ProxyName(Guid frameId) => ProxiesFolder + "/" + ProxyCache.FileName(frameId);
|
||||
|
||||
/// <summary>The absolute path of a file named relative to <see cref="MediaDirectory"/>.</summary>
|
||||
public string MediaFile(string relative) => LongPath.FromPortableRelative(MediaDirectory, relative);
|
||||
|
||||
/// <summary>Holds the project for one writer (this program's sync, a copy job) until disposed.</summary>
|
||||
/// <exception cref="LeaseUnavailableException">Another writer kept it for longer than <paramref name="wait"/>.</exception>
|
||||
public async Task<IDisposable> LockAsync(TimeSpan wait, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Directory.CreateDirectory(Folder);
|
||||
return await LeaseFile.AcquireAsync(Folder, LeaseName, "workspace@" + Environment.MachineName, wait, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <exception cref="InvalidDataException">Not a project file of a workspace, damaged or newer.</exception>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
public ProjectBundle ReadBundle()
|
||||
{
|
||||
var bundle = ProjectBundle.Load(BundlePath);
|
||||
if (bundle.Workspace is not { } workspace || workspace.ProjectId != ProjectId)
|
||||
throw new InvalidDataException($"{BundlePath} is not the working state of project {ProjectId}.");
|
||||
return bundle;
|
||||
}
|
||||
|
||||
public void WriteBundle(ProjectBundle bundle)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(bundle);
|
||||
if (bundle.Workspace?.ProjectId != ProjectId) throw new ArgumentException("The project file is another project's.", nameof(bundle));
|
||||
Directory.CreateDirectory(Folder);
|
||||
bundle.Save(BundlePath);
|
||||
}
|
||||
|
||||
/// <summary>The media index; null while the media were never copied, or were removed.</summary>
|
||||
/// <exception cref="InvalidDataException">The index is damaged or newer.</exception>
|
||||
public WorkspaceMedia? ReadMedia()
|
||||
{
|
||||
if (!File.Exists(MediaIndexPath)) return null;
|
||||
WorkspaceMedia? media;
|
||||
try
|
||||
{
|
||||
media = JsonSerializer.Deserialize(AtomicFile.ReadAllText(MediaIndexPath), WorkspaceJson.Default.WorkspaceMedia);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"{MediaIndexPath} cannot be read.", ex);
|
||||
}
|
||||
if (media is null || media.Format != WorkspaceMedia.FormatName) throw new InvalidDataException($"{MediaIndexPath} is not a media index.");
|
||||
if (media.FormatVersion > WorkspaceMedia.CurrentFormatVersion) throw new InvalidDataException($"{MediaIndexPath} is newer than this Chronos.");
|
||||
return media;
|
||||
}
|
||||
|
||||
internal void WriteMedia(WorkspaceMedia media)
|
||||
{
|
||||
Directory.CreateDirectory(MediaDirectory);
|
||||
AtomicFile.WriteAllText(MediaIndexPath, JsonSerializer.Serialize(media, WorkspaceJson.Default.WorkspaceMedia));
|
||||
}
|
||||
|
||||
/// <summary>The versions this workspace holds, oldest first; one that cannot be read is left out (and logged).</summary>
|
||||
public IReadOnlyList<ProjectVersion> Versions()
|
||||
{
|
||||
var versions = new List<ProjectVersion>();
|
||||
foreach (var (_, _, path) in ProjectSidecars.ListVersions(VersionsDirectory))
|
||||
{
|
||||
try
|
||||
{
|
||||
var version = ProjectSidecars.ReadVersion(path);
|
||||
if (version.ProjectId == ProjectId) versions.Add(version);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"Version file {path} cannot be read", ex);
|
||||
}
|
||||
}
|
||||
return versions;
|
||||
}
|
||||
|
||||
/// <summary>The path of a version in <see cref="VersionsDirectory"/>.</summary>
|
||||
public string VersionPath(ProjectVersion version) => Path.Combine(VersionsDirectory, ArchiveLayout.ProjectVersionFileName(version.Number, version.Id));
|
||||
|
||||
internal void WriteVersion(ProjectVersion version, bool replace = false) => ProjectSidecars.WriteVersion(VersionPath(version), version, replace);
|
||||
|
||||
/// <summary>Moves a version file aside into <c>versions/.replaced</c> (a local commit replayed under another id).</summary>
|
||||
internal void SetAside(ProjectVersion version)
|
||||
{
|
||||
var path = VersionPath(version);
|
||||
if (!File.Exists(path)) return;
|
||||
var aside = Path.Combine(VersionsDirectory, ReplacedFolder);
|
||||
Directory.CreateDirectory(aside);
|
||||
var target = Path.Combine(aside, Path.GetFileName(path));
|
||||
if (File.Exists(target)) target += "." + DateTime.UtcNow.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
File.Move(path, target);
|
||||
}
|
||||
|
||||
public WorkspaceSyncState ReadSync()
|
||||
{
|
||||
if (!File.Exists(SyncPath)) return WorkspaceSyncState.None;
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize(AtomicFile.ReadAllText(SyncPath), WorkspaceJson.Default.WorkspaceSyncState) ?? WorkspaceSyncState.None;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"{SyncPath} cannot be read.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal void WriteSync(WorkspaceSyncState state)
|
||||
{
|
||||
if (state == WorkspaceSyncState.None || state is { RestoredFrom: null, Conflict: null })
|
||||
{
|
||||
if (File.Exists(SyncPath)) File.Delete(SyncPath);
|
||||
return;
|
||||
}
|
||||
Directory.CreateDirectory(Folder);
|
||||
AtomicFile.WriteAllText(SyncPath, JsonSerializer.Serialize(state, WorkspaceJson.Default.WorkspaceSyncState));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user