feat(copies): engine composition, automatic photos, metered hold and App wiring (owner review 6, P5)

CopyEngine puts the planners together behind ICopyEngine so the App can find,
compare, integrate, merge, link, unlink and keep pairs apart. It also moves
photos that one side deleted to the other side's trash. Each trash step
re-reads both sides when applied, and nothing moves if any photo was restored
since the dry run. The sync agent now queues the new photos of pairs that were
integrated with automatic photos, both ways, at background priority. Failures
back off, and three in a row are reported so the pair can show "not sent". On
a metered connection, integrations are held like other server transfers.
AppServices builds the engine and registers the Integrate and Merge handlers.
A CopyWatch looks for local copies after start and after jobs that change
photos. Merges wait for workspace work that has not been sent home.

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 12:03:56 +01:00
1 parent 461dfcf4a3
commit 96b5b4078b
11 files changed
+1377 -8

No files matched your search

+78 -1
View File
@@ -22,10 +22,13 @@ using Chronos.Core.Windows.MediaFoundation;
using Chronos.Core.Windows.Wic;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Copies;
using Chronos.Data.Homes;
using Chronos.Data.Jobs;
using Chronos.Data.Processing;
using Chronos.Data.Projects;
using Chronos.Data.Scheduling;
using Chronos.Data.Sync;
using Chronos.Data.Workspaces;
using Chronos.Protocol.Client;
@@ -64,6 +67,8 @@ public sealed class AppServices : IAsyncDisposable
private WicDecoderProvider? _wic;
private readonly RenderNodeHost _renderNode;
private ServerSession _server = null!;
private CopyEngine? _copies;
private CopyWatch? _copyWatch;
public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs)
{
@@ -205,8 +210,68 @@ public sealed class AppServices : IAsyncDisposable
.. services.Database is { } db && services.Archives is { } archives
? WorkspaceJobs.CreateHandlers(db, archives, services.Sync, WorkspaceOptions(services, archives))
: [],
.. CopyHandlers(services),
];
/// <summary>
/// The matches' jobs (owner review 6, #73): the integration of a pair with a server sequence, over the signed-in session,
/// and the merge of two local sequences, which waits for workspace work not sent home.
/// </summary>
private static IReadOnlyList<IJobHandler> CopyHandlers(AppServices services)
{
if (services.Database is not { } db || services.Archives is not { } archives || services.Sync.Service is not { } sync) return [];
return
[
CopyJobs.CreateIntegrateHandler(sync, archives, services.Sync, sync.Copies),
SequenceMerger.CreateHandler(db, new SequenceMerger(db, archives, new CopySides(db)), archives, new ProjectTransplant(),
new WorkspaceCopyGuard(services.Workspace)),
];
}
/// <summary>
/// The matches engine over the open index (owner review 6, #73): the planners, the workspace guard of merges, the
/// signed-in server's comparisons through its agent, and the names the servers are known by here.
/// </summary>
private CopyEngine CreateCopyEngine(ChronosDatabase database, ArchiveService archives, SyncService sync)
{
var sides = new CopySides(database);
var homes = CopyJobs.ServerHomes(Sync);
return new CopyEngine(sync, archives, new IntegratePlanner(sync, sides, archives.Projects, homes), new SequenceMerger(database, archives, sides),
new CopyReviewBuilder(database, archives.Projects, sides, homes), CopyContext, new WorkspaceCopyGuard(Workspace),
refresh: async (remote, ct) =>
{
// The signed-in server compares through its agent, one comparison at a time; another (a job's session) directly.
if (Sync.Agent is { } agent && string.Equals(agent.Server, remote.Server, StringComparison.Ordinal)) await agent.RefreshAsync(ct).ConfigureAwait(false);
else await sync.RefreshAsync(remote, Sync.Policy, ct).ConfigureAwait(false);
},
serverNames: ServerNameOf,
followUps: () => Engine,
workspaceProjects: async ct => Workspace.IsAvailable
? [.. (await Workspace.ListAsync(ct).ConfigureAwait(false)).Where(e => e.IsReadable).Select(e => (e.Home, e.Name))]
: []);
}
/// <summary>
/// What the planners know of this PC and the signed-in account. The account's rights stand for its rights over the
/// server's sequence: the server enforces ownership itself, so OwnsOrAdmin reads Delete or an admin right.
/// </summary>
private CopyPlanContext CopyContext(ISyncRemote? remote)
{
var user = remote is not null ? Server.User : null;
var permissions = user?.Permissions ?? 0;
var rights = SyncRights.Of(permissions);
bool Has(Permissions p) => (permissions & (long)p) != 0;
var originals = Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType<Guid>().ToHashSet();
return new CopyPlanContext(System.Environment.MachineName, remote is null ? null : Server.ServerName, rights.CanUpload, rights.CanFetchOriginals,
rights.CanEdit, Has(Permissions.Delete), Has(Permissions.Delete) || Has(Permissions.ManageArchive) || Has(Permissions.ManageUsers),
remote is not null && Sync.HasProjects, user is { QuotaBytes: > 0 } ? Math.Max(0, user.QuotaBytes - user.UsedBytes) : null, new HashSet<Guid>(),
originals);
}
/// <summary>The name a server key is shown by: the known server's name (by its id), else none (the key is shown).</summary>
private string? ServerNameOf(string key) =>
ServerIdentity.IdOf(Sync.Identity.Canonical(key)) is { } id && Settings.FindKnownServer(id) is { Name: { Length: > 0 } name } ? name : null;
/// <summary>
/// The workspace jobs' share of the desktop (phase 5c): its processing options for local renders, the metered hold of
/// Settings › Server over transfers with a server, the workspace that follows a moved folder, and the folder in use
@@ -342,7 +407,9 @@ public sealed class AppServices : IAsyncDisposable
_stopping.Dispose();
_server.PropertyChanged -= OnServerChanged;
await _renderNode.DisposeAsync().ConfigureAwait(false);
if (_copyWatch is not null) await _copyWatch.DisposeAsync().ConfigureAwait(false);
await Sync.DisposeAsync().ConfigureAwait(false);
_copies?.Dispose();
(Jobs as IDisposable)?.Dispose();
if (Engine is not null) await Engine.DisposeAsync().ConfigureAwait(false);
Thumbnails?.Dispose();
@@ -589,7 +656,10 @@ public sealed class AppServices : IAsyncDisposable
Archives.Videos.ProtectedFolders = () => [Workspace.Root, Share.Folder];
Archives.Videos.Servers = Sync;
Processing = new ProcessingService(database, ProcessingOptions(archive));
Sync.Open(database, Archives, archive);
var sync = Sync.Open(database, Archives, archive);
_copies = CreateCopyEngine(database, Archives, sync);
// Sample data puts its own engine in place in capture.
Sync.Copies = _copies;
var handlers = CreateJobHandlers(this);
var kinds = handlers.Select(h => h.Kind).Distinct().ToArray();
// One job at a time (owner review 1): the parallelism works inside it, the priority queue decides which runs.
@@ -603,6 +673,13 @@ public sealed class AppServices : IAsyncDisposable
var started = startEngine && handlers.Count > 0;
if (started) await Engine.StartAsync(ct);
Jobs = new EngineJobQueue(database, Engine, started);
// Two copies of a sequence among this PC's archives are looked for by themselves (owner review 6, #73).
if (!Environment.IsCapture)
{
var copies = _copies;
_copyWatch = new CopyWatch(ct => copies.FindLocalAsync(ct));
_copyWatch.Start(Engine);
}
Log.Info("jobs", started ? $"Job engine started for {string.Join(", ", kinds)}" : "No job handlers registered; jobs stay queued");
UpdateRenderNode();
}