A Save both, a recorded conflict and a damaged version file could each leave a local version out of the chain or a copy half-made at the home. This round makes every one of these end with nothing lost and nothing half done. - Save both retry: a journaled copy somebody else sent a version to (or one deleted since) is left as it is, and a new copy takes every local version. A divergence met at the copy while sending also makes a new copy, once. - Keep mine, Take server, or a pull that settles a conflict after a Save both was cut short first send the rest of the journaled versions to that copy, if it is still this workspace's. The journal is cleared at the commit point. - A conflict of format met past local versions the home already stored (their answer lost) now moves the base onto them, like any other conflict. The conflict, the base and the chain then name one version. A save-both journal follows the base when it moves. - A conflict recorded before stays while the home holds no version after the base. A base that has reached the conflict's head clears it. - Take server no longer replays or sends the version it saves first when, replayed, that version holds only the server's values. It stays in versions/.replaced for the undo. The test checks the exact history again. - Version files after the base that cannot be read, or that do not follow from it, are listed (WorkspaceProject.UnreadableVersions). The new state WorkspaceState.Damaged shows them. Commit, push, pull and resolve refuse to go past them. A damaged file whose version the home holds takes the home's copy at the next sync, and the damaged file is kept aside. Remove local copy counts these files as unsent. - PushOutcome.TooLarge raises its own WorkspaceSyncException message. The desktop says Errors.Workspace.VersionTooLarge for it and for ProjectVersionTooLargeException when a local commit is too large. - The editor maps LeaseUnavailableException from opening a busy project to Errors.Workspace.Busy. - The copy job no longer deletes, during the pass, a proxy that the editor's current media name. RemoveStale removes it after the commit. A resumed run does not keep such a file as a frame whose photo changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
399 lines
21 KiB
C#
399 lines
21 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using Chronos.Core.Advice;
|
|
using Chronos.Core.Analysis;
|
|
using Chronos.Core.Analysis.Deep;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Diagnostics;
|
|
using Chronos.Core.Export;
|
|
using Chronos.Core.Imaging;
|
|
using Chronos.Core.Localization;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Core.Render;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Archiving.Jobs;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Data.Processing;
|
|
using Chronos.Data.Projects;
|
|
using Chronos.Data.Workspaces;
|
|
using Chronos.Protocol;
|
|
|
|
namespace Chronos.App.Editor;
|
|
|
|
/// <summary>A source whose renders are made otherwise than from the project as the editor holds it (a workspace project: at its home).</summary>
|
|
public interface IProjectRenders
|
|
{
|
|
/// <summary>The jobs that render <paramref name="export"/> of <paramref name="project"/> (the editor's settings, positions on the source's frames).</summary>
|
|
IReadOnlyList<ArchiveJobRequest> RenderRequests(Sequence sequence, ProjectSettings project, ExportSettings export, int sourceWidth, int sourceHeight);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A workspace project open in the editor (owner review 2, R8: editing happens in the workspace, never inside the
|
|
/// library): the frames and pixels of the media the workspace holds (<c>media/media.json</c>, the proxies), the project
|
|
/// from its working state (<c>project.chronos</c>, positions placed on those frames), saved there atomically under the
|
|
/// project's lease and merged against the basis like <see cref="LocalSequenceSource"/> (a pull or a copy job writes it
|
|
/// too): frame positions photo by photo as the sync merges them (<see cref="Merge"/>), and settings the editor changed to
|
|
/// other values than those stored meanwhile asked about (<see cref="ProjectConflictException"/>), never decided for the
|
|
/// editor. A pull that brings a newer Chronos's settings turns it read-only (<see cref="ReadOnlyChanged"/>). Jobs run on
|
|
/// this PC's engine against the home's sequence (same id): an analysis reads the home's originals, a
|
|
/// render renders the home's originals with the project's portable settings and records the project and the version
|
|
/// (<see cref="RenderRequests"/>). The deep report is the home's while its frames are the media's, else the copy the
|
|
/// workspace took. A copy job that changed the media (Update media) makes saves made against the frames read before refused
|
|
/// (<see cref="FramesChangedException"/>) until the editor has read them again and moved its positions by frame.
|
|
/// </summary>
|
|
public sealed class WorkspaceSequenceSource : ISequenceSource, IProjectRenders
|
|
{
|
|
private const int JobsListed = 200;
|
|
|
|
private readonly WorkspaceSync _sync;
|
|
private readonly ChronosDatabase _db;
|
|
private readonly ImageDecoder _decoder;
|
|
private readonly JobEngine? _engine;
|
|
private readonly Lock _gate = new();
|
|
private readonly List<IRenderFrameSource> _retired = [];
|
|
private IRenderFrameSource? _frames;
|
|
private IFrameSource? _pixels;
|
|
private int _frameCount;
|
|
private int _framesVersion = -1;
|
|
private Guid[] _frameIds = [];
|
|
private FramePhotos _photos = new([]);
|
|
private BundleWorkspace? _workspace;
|
|
private bool _readOnly;
|
|
private int _disposed;
|
|
|
|
public WorkspaceSequenceSource(WorkspaceSync sync, Guid projectId, ChronosDatabase db, ImageDecoder decoder, JobEngine? engine)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(sync);
|
|
ArgumentNullException.ThrowIfNull(db);
|
|
ArgumentNullException.ThrowIfNull(decoder);
|
|
_sync = sync;
|
|
_db = db;
|
|
_decoder = decoder;
|
|
_engine = engine;
|
|
ProjectId = projectId;
|
|
if (engine is not null) engine.JobChanged += OnJobChanged;
|
|
}
|
|
|
|
public Guid ProjectId { get; }
|
|
|
|
/// <summary>The project's folder in the workspace.</summary>
|
|
public WorkspaceProject Project => _sync.Store.Project(ProjectId);
|
|
|
|
/// <summary>The workspace section of the project file as read last (name, Main, home, base, media); null before the first read.</summary>
|
|
public BundleWorkspace? Workspace => Volatile.Read(ref _workspace);
|
|
|
|
/// <summary>The frames version of the home the media read last were copied at; -1 before the first read.</summary>
|
|
public int MediaFramesVersion => Volatile.Read(ref _framesVersion);
|
|
|
|
/// <summary>The photos of the media read last, in frame order (what positions name).</summary>
|
|
public FramePhotos Photos => Volatile.Read(ref _photos);
|
|
|
|
public bool IsRemote => false;
|
|
|
|
/// <summary>Settings of a newer Chronos: shown, never saved.</summary>
|
|
public bool IsReadOnly => Volatile.Read(ref _readOnly);
|
|
|
|
public bool IsOutdated => false;
|
|
|
|
/// <summary>The library's values and the editor's are asked about where both changed a setting, as a sync asks.</summary>
|
|
public bool AsksConflicts => true;
|
|
|
|
/// <summary>The working state read again holds a newer Chronos's settings (a pull took them), or no longer does.</summary>
|
|
public event Action? ReadOnlyChanged;
|
|
|
|
public event Action<Job>? JobChanged;
|
|
|
|
/// <exception cref="EditorException">
|
|
/// The workspace holds no media for the project (copy them again), the project is gone, or a sync or a copy kept it for
|
|
/// longer than the sync waits (<see cref="WorkspaceSync.LockWait"/>).
|
|
/// </exception>
|
|
public async Task<SequenceContent> LoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
WorkspaceSession session;
|
|
try
|
|
{
|
|
session = await _sync.OpenAsync(ProjectId, cancellationToken);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
throw new EditorException(Loc.L("Errors.Workspace.NotCheckedOut"), ex);
|
|
}
|
|
catch (InvalidDataException ex)
|
|
{
|
|
throw new EditorException(Loc.L("Editor.Error.Project"), ex);
|
|
}
|
|
catch (LeaseUnavailableException ex)
|
|
{
|
|
// A long sync or copy holds the project: nothing was read, opening it again later reads it whole.
|
|
Log.Info("editor", $"Workspace project {ProjectId} is busy: {ex.Message}");
|
|
throw new EditorException(Loc.L("Errors.Workspace.Busy"), ex);
|
|
}
|
|
if (session is not { Sequence: { } media, Media: { } index }) throw new EditorException(Loc.L("Errors.Workspace.MediaMissing"));
|
|
var home = session.Bundle.Workspace!.Home;
|
|
var row = await _db.Sequences.GetAsync(home.SequenceId, cancellationToken);
|
|
var archive = row is null ? null : await _db.Archives.GetAsync(row.ArchiveId, cancellationToken);
|
|
IReadOnlyList<Render> renders = row is null ? [] : await _db.Renders.ListBySequenceAsync(home.SequenceId, cancellationToken);
|
|
return await Task.Run(() =>
|
|
{
|
|
var frames = media.Frames;
|
|
Swap(media, index, session.Bundle.Workspace!, session.IsReadOnly);
|
|
var times = frames.Where(f => f.CapturedAt is not null).Select(f => f.CapturedAt!.Value).Order().ToList();
|
|
// The home's row (camera, interval, tags) with the frames this workspace holds.
|
|
var sequence = (row ?? media.Sequence) with
|
|
{
|
|
FrameCount = frames.Count,
|
|
FramesVersion = index.FramesVersion,
|
|
FirstCapture = times.Count > 0 ? times[0] : null,
|
|
LastCapture = times.Count > 0 ? times[^1] : null,
|
|
SettingsJson = session.Working.ToJson(),
|
|
};
|
|
var folder = archive is null ? null : new ArchivePaths(archive.RootPath).Sequence(row!.Folder);
|
|
var (deep, otherFrames) = ReadReport(folder, row?.FramesVersion == index.FramesVersion, session.Project.AnalysisPath, frames.Count);
|
|
var context = SequenceContext.FromFrames(frames);
|
|
return new SequenceContent(sequence, frames, session.Working, LightAnalyzer.Analyze(frames), deep, context.SourceWidth, context.SourceHeight,
|
|
folder?.Exports, [.. renders.OrderByDescending(r => r.Created)], [])
|
|
{
|
|
DeepOfOtherFrames = otherFrames,
|
|
};
|
|
}, cancellationToken);
|
|
}
|
|
|
|
/// <remarks>Only a copy job changes the workspace's frames (Update media): its media index is compared with the one read last.</remarks>
|
|
public Task<bool> FramesCurrentAsync(CancellationToken cancellationToken) => Task.Run(() =>
|
|
{
|
|
var (framesVersion, ids) = (Volatile.Read(ref _framesVersion), Volatile.Read(ref _frameIds));
|
|
try
|
|
{
|
|
return Project.ReadMedia() is { } media && media.FramesVersion == framesVersion && media.Frames.Select(f => f.Id).SequenceEqual(ids);
|
|
}
|
|
catch (InvalidDataException ex)
|
|
{
|
|
Log.Warning("editor", $"The media index of workspace project {ProjectId} cannot be read", ex);
|
|
return false;
|
|
}
|
|
}, cancellationToken);
|
|
|
|
/// <summary>Whether the home's frames moved past those the workspace holds (frames added or removed there): "Update media" copies them again.</summary>
|
|
public async Task<bool> HomeFramesChangedAsync(CancellationToken cancellationToken)
|
|
{
|
|
if (Workspace is not { } workspace || MediaFramesVersion < 0) return false;
|
|
return await _db.Sequences.GetAsync(workspace.Home.SequenceId, cancellationToken) is { } row && row.FramesVersion != MediaFramesVersion;
|
|
}
|
|
|
|
public async ValueTask<ImageBuffer?> DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
|
{
|
|
IFrameSource? pixels;
|
|
int count;
|
|
lock (_gate) (pixels, count) = (_pixels, _frameCount);
|
|
if (pixels is null || (uint)index >= (uint)count) return null;
|
|
// The loader decodes synchronously: an async delegate keeps a superseded decode's cancellation in user code (R4).
|
|
return await Task.Run(async () => await pixels.LoadAsync(index, maxLongEdge, cancellationToken).ConfigureAwait(false), cancellationToken);
|
|
}
|
|
|
|
/// <exception cref="EditorException">The project is read-only, or its media are gone.</exception>
|
|
/// <exception cref="FramesChangedException">The media changed since they were read (a copy job): read them again first.</exception>
|
|
/// <exception cref="ProjectConflictException">
|
|
/// The working state stored meanwhile (a sync took the library's versions) holds other values of settings this edit
|
|
/// changed too: nothing is stored until the user keeps theirs or takes the library's.
|
|
/// </exception>
|
|
public Task<ProjectSettings> SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(project);
|
|
ArgumentNullException.ThrowIfNull(basis);
|
|
var framesVersion = Volatile.Read(ref _framesVersion);
|
|
if (framesVersion < 0) throw new InvalidOperationException("LoadAsync first.");
|
|
var folder = Project;
|
|
return Task.Run(async () =>
|
|
{
|
|
using (await folder.LockAsync(_sync.LockWait, cancellationToken))
|
|
{
|
|
var bundle = folder.ReadBundle();
|
|
if (bundle.IsReadOnly || project.IsNewerFormat) throw new EditorException(Loc.L("Errors.Workspace.ReadOnly"));
|
|
var media = folder.ReadMedia() ?? throw new EditorException(Loc.L("Errors.Workspace.MediaMissing"));
|
|
// A copy job moved the working state onto other frames: the editor reads them and moves its positions first.
|
|
if (media.FramesVersion != framesVersion) throw new FramesChangedException(Loc.L("Errors.Workspace.MediaChanged"));
|
|
var stored = ProjectSettings.FromJson(ProjectContent.Place(bundle.Project.ToJson(), media.Photos), allowNewer: true);
|
|
var merged = project;
|
|
if (!ProjectMerge.SameValues(stored, basis))
|
|
{
|
|
var merge = Merge(basis, project, stored, media.Photos);
|
|
if (merge.HasConflicts) throw new ProjectConflictException(Loc.L("Editor.Error.SaveConflict"), stored, merge.Conflicts);
|
|
merged = merge.Merged;
|
|
}
|
|
if (ProjectMerge.SameValues(merged, stored)) return merged;
|
|
folder.WriteBundle(bundle with { Project = merged, Saved = DateTimeOffset.UtcNow });
|
|
return merged;
|
|
}
|
|
}, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The editor's merges as the sync merges (owner review 2, R8): member by member, and frame positions photo by photo and
|
|
/// flag by flag in the portable form, so an exclusion or a keyframe a sync wrote meanwhile stays beside the editor's own
|
|
/// (positional arrays would take one side whole). All three name the frames of the media read last.
|
|
/// </summary>
|
|
public ProjectMergeResult Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs) => Merge(basis, ours, theirs, Photos);
|
|
|
|
private static ProjectMergeResult Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs, FramePhotos photos)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(basis);
|
|
ArgumentNullException.ThrowIfNull(ours);
|
|
ArgumentNullException.ThrowIfNull(theirs);
|
|
var merge = ProjectMerge.MergeWithConflicts(basis, ours, theirs);
|
|
// Positions only one side moved, or none, merge alike either way; the portable form is needed where both did.
|
|
if (photos.Count == 0 || !merge.Conflicts.Any(c => NamesPosition(c.Path)) || basis.IsNewerFormat || ours.IsNewerFormat || theirs.IsNewerFormat) return merge;
|
|
var portable = ProjectMerge.MergeWithConflicts(Portable(basis, photos), Portable(ours, photos), Portable(theirs, photos));
|
|
var placed = ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(portable.Merged.ToJson()), photos), allowNewer: true);
|
|
return portable with { Merged = placed };
|
|
|
|
static ProjectSettings Portable(ProjectSettings settings, FramePhotos photos) =>
|
|
ProjectSettings.FromJson(ProjectContent.ToPortable(settings, photos), allowNewer: true);
|
|
}
|
|
|
|
/// <summary>A merge path of a frame position (an exclusion, the hero, a camera keyframe, a frame waiting for its photo).</summary>
|
|
private static bool NamesPosition(string path) =>
|
|
path is "excludedFrames" or "heroFrame" || path.StartsWith("camera.keyframes", StringComparison.Ordinal) || path.StartsWith("pendingFrames", StringComparison.Ordinal);
|
|
|
|
public async Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
|
|
{
|
|
if (_engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"));
|
|
var job = await _engine.EnqueueAsync(request, cancellationToken: cancellationToken);
|
|
return job.Id;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<Job>> ListJobsAsync(CancellationToken cancellationToken) =>
|
|
(await _db.Jobs.ListAsync(null, JobsListed, null, cancellationToken)).Items;
|
|
|
|
public Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken) =>
|
|
(_engine ?? throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"))).CancelAsync(jobId, cancellationToken);
|
|
|
|
public Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken) =>
|
|
(_engine ?? throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"))).ResumeAsync(jobId, cancellationToken);
|
|
|
|
/// <summary>
|
|
/// The render at the home (owner review 2, R8: edit with proxies, render where the originals are): one job of this PC's
|
|
/// engine per preset, with the project's portable settings (positions parked by photo, placed on the home's frames by
|
|
/// the render) and the project recorded on the render; the version too when the settings are those of the last
|
|
/// version the home holds (an unsaved working state, or one saved here and not sent yet, renders as it is, without a version).
|
|
/// </summary>
|
|
public IReadOnlyList<ArchiveJobRequest> RenderRequests(Sequence sequence, ProjectSettings project, ExportSettings export, int sourceWidth, int sourceHeight)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(sequence);
|
|
ArgumentNullException.ThrowIfNull(project);
|
|
ArgumentNullException.ThrowIfNull(export);
|
|
var portable = ProjectContent.ToPortable(project with { Export = export }, Photos);
|
|
var hash = ProjectContent.Hash(portable);
|
|
var (tipId, tipHash) = Tip();
|
|
var version = new ProjectVersion
|
|
{
|
|
Id = tipId ?? Guid.Empty,
|
|
ProjectId = ProjectId,
|
|
Number = 0,
|
|
SettingsJson = portable,
|
|
ContentHash = hash,
|
|
};
|
|
var requests = ProcessingJobs.RenderVersion(sequence, version, export, sourceWidth, sourceHeight);
|
|
if (tipId is not null && string.Equals(tipHash, hash, StringComparison.OrdinalIgnoreCase)) return requests;
|
|
return [.. requests.Select(r => r with { ParametersJson = WithoutVersion(r.ParametersJson) })];
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) == 1) return;
|
|
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
|
|
List<IRenderFrameSource> sources;
|
|
lock (_gate)
|
|
{
|
|
sources = [.. _retired];
|
|
if (_frames is not null) sources.Add(_frames);
|
|
_retired.Clear();
|
|
(_frames, _pixels) = (null, null);
|
|
}
|
|
foreach (var source in sources)
|
|
{
|
|
if (source is IAsyncDisposable disposable) await disposable.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Takes the media read now: a frame source over them when they differ from those read before (the old one is disposed
|
|
/// with this source, as a decode may still read it), their frames and photos, the project's workspace section, and
|
|
/// whether the settings are a newer Chronos's (said by <see cref="ReadOnlyChanged"/> when that changed).
|
|
/// </summary>
|
|
private void Swap(WorkspaceSequence media, WorkspaceMedia index, BundleWorkspace workspace, bool readOnly)
|
|
{
|
|
var ids = media.Frames.Select(f => f.Id).ToArray();
|
|
bool changed;
|
|
lock (_gate)
|
|
{
|
|
if (_frames is null || index.FramesVersion != _framesVersion || !ids.AsSpan().SequenceEqual(_frameIds))
|
|
{
|
|
if (_frames is not null) _retired.Add(_frames);
|
|
_frames = media.OpenFrames(_decoder);
|
|
_pixels = (IFrameSource)_frames;
|
|
_frameCount = media.Frames.Count;
|
|
}
|
|
_frameIds = ids;
|
|
_framesVersion = index.FramesVersion;
|
|
_photos = index.Photos;
|
|
_workspace = workspace;
|
|
changed = _readOnly != readOnly;
|
|
_readOnly = readOnly;
|
|
}
|
|
if (changed) ReadOnlyChanged?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The version a render may name, with its content hash: the base, which the home holds, while no version waits to be
|
|
/// sent; none otherwise. A version not sent yet may never reach the home under its id (a replay or a save-both sends it
|
|
/// under another), so a render of it names the project alone.
|
|
/// </summary>
|
|
private (Guid? Id, string? Hash) Tip()
|
|
{
|
|
try
|
|
{
|
|
if (_sync.Unpushed(ProjectId).Count > 0) return (null, null);
|
|
return Project.ReadBundle().Workspace?.Base is { } basis ? (basis.VersionId, basis.ContentHash) : (null, null);
|
|
}
|
|
catch (Exception ex) when (ex is IOException or InvalidDataException or InvalidOperationException)
|
|
{
|
|
Log.Warning("editor", $"The versions of workspace project {ProjectId} cannot be read; the render names none", ex);
|
|
return (null, null);
|
|
}
|
|
}
|
|
|
|
private static string? WithoutVersion(string? parametersJson)
|
|
{
|
|
if (parametersJson is null || JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters) is not { } parameters) return parametersJson;
|
|
return JsonSerializer.Serialize(parameters with { ProjectVersionId = null }, ProtocolJson.Default.RenderJobParameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The deep report the editor shows: the home's while its frames are the workspace's, else the copy the workspace took;
|
|
/// whether one of another number of frames was read instead.
|
|
/// </summary>
|
|
private static (DeepAnalysisReport? Report, bool OtherFrames) ReadReport(SequencePaths? home, bool sameFrames, string copied, int frameCount)
|
|
{
|
|
var otherFrames = false;
|
|
foreach (var path in new[] { sameFrames ? home?.AnalysisPath : null, copied })
|
|
{
|
|
if (path is null || !File.Exists(path)) continue;
|
|
try
|
|
{
|
|
var report = DeepAnalysisReport.Load(path);
|
|
if (report.FrameCount == frameCount) return (report, false);
|
|
otherFrames = true;
|
|
}
|
|
catch (Exception ex) when (ex is IOException or InvalidDataException or JsonException or UnauthorizedAccessException)
|
|
{
|
|
Log.Warning("editor", $"{path} cannot be read", ex);
|
|
}
|
|
}
|
|
return (null, otherFrames);
|
|
}
|
|
|
|
private void OnJobChanged(Job job) => JobChanged?.Invoke(job);
|
|
}
|