feat: owner review 2 phase 5b: the server as a project home (#17)
CI / Build and test (push) Successful in 9m58s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 14m8s
CI / PostgreSQL 16 integration (push) Successful in 8m55s
CI / PostgreSQL 17 integration (push) Successful in 8m50s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m22s

A Chronos Server keeps its sequences' projects, as a PC's archive does: Main and named projects with versions under /api/v1/sequences/{id}/projects, the projects capability and the projectChanged event. The desktop opens a server's sequences in the workspace (proxies downloaded, hash-checked, resumable) with Save version, Sync, conflicts, History, Restore and the project actions against the server. Renders can be of a project version. The admin panel lists a sequence's projects and versions. Strings in all 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #17.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-11-04 14:58:11 +01:00
1 parent 255e149087
commit 81c303f484
325 files changed
+11477 -516

No files matched your search

+82 -6
View File
@@ -3,9 +3,12 @@ using Chronos.App.Editor;
using Chronos.App.Infrastructure;
using Chronos.Core.Diagnostics;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.App.Server;
@@ -15,23 +18,28 @@ public delegate ISyncRemote SyncRemoteFactory(Uri address, string token, IReadOn
/// <summary>
/// This PC's sync with the signed-in server (BRIEF §4.4, §14.5): the <see cref="SyncService"/> of the index, the
/// connection of upload and download jobs (<see cref="ISyncConnector"/>), a <see cref="SyncAgent"/> that keeps the
/// table current while a session is open, started and stopped in order as the session changes, the
/// <see cref="UploadHold"/> for metered connections, the connections of editors of server sequences
/// (<see cref="ConnectEditor"/>) and the Export "Sync" of finished renders (<see cref="ExportSync"/>). Options come from
/// <see cref="AppSettings.SyncSettingsAutomatically"/>, <see cref="AppSettings.KeepProxiesOffline"/> and
/// <see cref="AppSettings.PauseUploadsOnMetered"/>.
/// connection of upload and download jobs (<see cref="ISyncConnector"/>, also the workspace copies of server projects), a
/// <see cref="SyncAgent"/> that keeps the table current while a session is open, started and stopped in order as the
/// session changes, the <see cref="UploadHold"/> for metered connections, the connections of editors of server sequences
/// (<see cref="ConnectEditor"/>), the server as the home of workspace projects when it keeps projects (owner review 2, R8,
/// phase 5b: <see cref="ProjectHome"/>, <see cref="ProjectChanged"/>) and the Export "Sync" of finished renders
/// (<see cref="ExportSync"/>). Options come from <see cref="AppSettings.SyncSettingsAutomatically"/>,
/// <see cref="AppSettings.KeepProxiesOffline"/> and <see cref="AppSettings.PauseUploadsOnMetered"/>.
/// </summary>
public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDisposable
{
/// <summary>How long a job resumed at start waits for the saved session to be restored.</summary>
private static readonly TimeSpan RestoreWait = TimeSpan.FromSeconds(30);
private readonly Lock _gate = new();
private Task _switch = Task.CompletedTask;
private Credentials? _credentials;
private Running? _running;
private JobEngine? _renders;
/// <summary>The client project homes of the session share, made at first use for the credentials it holds.</summary>
private SharedClient? _projects;
/// <summary>Connections over <see cref="ChronosClient"/>; screenshots substitute a fake server.</summary>
public SyncRemoteFactory RemoteFactory { get; set; } = (address, token, trusted) =>
new ChronosSyncRemote(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token }, ownsClient: true);
@@ -40,6 +48,10 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
public RemoteSequenceApiFactory EditorFactory { get; set; } = (address, token, trusted) =>
new ChronosRemoteSequenceApi(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token });
/// <summary>The client the session's project homes share (<see cref="ProjectHome"/>); tests substitute one of their own.</summary>
public Func<Uri, string, IReadOnlyCollection<string>, ChronosClient> ProjectClientFactory { get; set; } = (address, token, trusted) =>
new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token };
/// <summary>The engine; null until the index is open.</summary>
public SyncService? Service { get; private set; }
@@ -61,6 +73,38 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
/// <summary>The signed-in account may queue renders on the server.</summary>
public bool CanRender => Allows(Permissions.Render);
/// <summary>
/// The signed-in server keeps its sequences' projects (owner review 2, R8: the <c>projects</c> capability), so Edit opens
/// its sequences as workspace projects; false while signed out and for an older server, whose sequences Edit opens with
/// proxies as before.
/// </summary>
public bool HasProjects =>
services.Server.IsConnected && services.Server.Flow.Info?.Capabilities is { } capabilities && capabilities.Contains(ServerCapabilities.Projects);
/// <summary>The home of a workspace project of server sequence <paramref name="remoteId"/>; null while signed out or when the server keeps no projects.</summary>
public BundleHome? ServerHome(Guid remoteId) => Server is { } server && HasProjects ? new BundleHome(BundleHome.ServerKind, remoteId, null, server) : null;
/// <summary>
/// The project home of a workspace project whose home is a server (<see cref="ServerProjectHome"/> over the session's
/// client); null while signed out, signed in to another server, or when the server keeps no projects: the workspace then
/// shows the project offline and its work waits.
/// </summary>
public IProjectHome? ProjectHome(BundleHome home)
{
ArgumentNullException.ThrowIfNull(home);
if (home.IsArchive || !HasProjects || Volatile.Read(ref _credentials) is not { } credentials
|| !string.Equals(home.Server, SyncService.ServerKey(credentials.Address), StringComparison.OrdinalIgnoreCase))
return null;
return new ServerProjectHome(SessionClient(credentials), home.SequenceId, services.Server.ServerName);
}
/// <summary>
/// A project of a sequence of the signed-in server changed there (the server's key, and its <c>projectChanged</c>), or,
/// with null, changes may have been missed while its events were lost, or the session with that server started or
/// stopped (its projects become reachable, or offline) (any thread).
/// </summary>
public event Action<string, ProjectChangedEvent?>? ProjectChanged;
/// <summary>Pauses uploads while the connection is metered and the option is on.</summary>
public UploadHold Uploads { get; } = new(services);
@@ -96,6 +140,9 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
engine.JobChanged += OnJobChanged;
}
/// <summary>The sign-in <see cref="ConnectEditor"/> uses now (another object once signed in anew); null while signed out.</summary>
public object? Session => Volatile.Read(ref _credentials);
/// <summary>A connection for the editor of a sequence of the signed-in server; null while signed out.</summary>
public IRemoteSequenceApi? ConnectEditor() =>
Volatile.Read(ref _credentials) is { } c ? EditorFactory(c.Address, c.Token, c.Fingerprints) : null;
@@ -146,10 +193,31 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
await _switch.ConfigureAwait(false);
await StopAsync().ConfigureAwait(false);
lock (_gate)
{
_projects?.Client.Dispose();
_projects = null;
}
}
private bool Allows(Permissions permission) => services.Server.User is { } user && (user.Permissions & (long)permission) != 0;
/// <summary>
/// The client of <paramref name="credentials"/>' session, made once for its address and token: another sign-in replaces
/// it (a call still on the old one fails as a server out of reach, and is tried again on the new one).
/// </summary>
private ChronosClient SessionClient(Credentials credentials)
{
lock (_gate)
{
if (_projects is { } current && current.Address == credentials.Address && current.Token == credentials.Token) return current.Client;
_projects?.Client.Dispose();
var client = ProjectClientFactory(credentials.Address, credentials.Token, credentials.Fingerprints);
_projects = new SharedClient(credentials.Address, credentials.Token, client);
return client;
}
}
/// <summary>
/// Export "Sync": a render of this PC that asked for it is sent to the server once done (<see cref="ExportSync"/>),
/// also when it ends while the session is down: the send is queued for the saved session's server and waits for it.
@@ -196,7 +264,10 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
await previous.ConfigureAwait(false);
if (Volatile.Read(ref _running) is { } running && wanted is { } same && running.Credentials.Address == same.Address && running.Credentials.Token == same.Token)
return;
var stopped = Volatile.Read(ref _running)?.Remote.Server;
await StopAsync().ConfigureAwait(false);
// The projects of the server left are offline now: their state is read again.
if (stopped is not null) ProjectChanged?.Invoke(stopped, null);
if (wanted is not { } start || Service is not { } service)
{
Changed?.Invoke();
@@ -205,10 +276,12 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
var remote = RemoteFactory(start.Address, start.Token, start.Fingerprints);
var agent = new SyncAgent(service, remote, () => Policy, services.Engine);
agent.Changed += () => Changed?.Invoke();
agent.ProjectChanged += e => ProjectChanged?.Invoke(remote.Server, e);
agent.Start();
Volatile.Write(ref _running, new Running(agent, remote, start));
Log.Info("sync", $"Comparing with {remote.Server}");
Changed?.Invoke();
ProjectChanged?.Invoke(remote.Server, null);
}
private async Task StopAsync()
@@ -221,6 +294,9 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
private sealed record Credentials(Uri Address, string Token, IReadOnlyCollection<string> Fingerprints);
/// <summary>The client project homes share, and the session it was made for.</summary>
private sealed record SharedClient(Uri Address, string Token, ChronosClient Client);
/// <summary>The agent of the signed-in session: one reference, so the UI thread never reads half of a switch.</summary>
private sealed record Running(SyncAgent Agent, ISyncRemote Remote, Credentials Credentials);
}