feat(server): the server keeps its sequences' projects, pushed and pulled like a PC's archive

Owner review 2, phase 5b (R8): a server is a project home. A workspace can
list a sequence's projects, make, rename and delete them, read their
versions and push new ones through /api/v1/sequences/{sid}/projects. The
endpoints run on the archive's ProjectService exactly as a desktop's archive
does (sidecars first, then the index, under the sequence's lease), so no
version is stored apart and a desktop sharing the archive sees the same
projects.

- Protocol: project, version, create, rename, version page, push answer
  (outcome, stored version, head, plus code and message on 409/422) and
  home frames DTOs, the projectChanged event, the "projects" capability and
  the ChronosClient calls; a push returns its outcome without throwing on
  409 or 422. ProjectWire maps them to and from the model for both sides.
- Server: Read lists and reads, Edit makes, renames and pushes, Delete (the
  sequence's owner, the project's maker or an admin) deletes; refusals with
  stable codes, 503 while the sequence is busy, offline or moved; the
  pushing account becomes the version's author; creations, renames and
  deletions audited, pushes logged.
- ProjectService tells the host what each call changed, compared under the
  lease, so projectChanged also follows Main's external versions; a Main
  version mirrored into the settings sends sequenceChanged too, for older
  clients. ProjectCatchUp takes settings a PATCH, an analysis or a
  conversion wrote into Main at once, for sequences that have a Main.
- Renders of a project version: POST /jobs checks the project and version
  against the sequence and pins a project alone to its head; render nodes
  get the version's settings placed on the frames they claim; the render
  records the project and version.
- Admin panel: the sequence page lists its projects (Main marked, head,
  last change, versions on demand) with Rename and Delete for the others,
  live on projectChanged. It edits no settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-04 11:15:19 +01:00
1 parent 917cddde41
commit e2ecc409a3
30 files changed
+2261 -35

No files matched your search

+58 -5
View File
@@ -1,8 +1,11 @@
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Processing;
using Chronos.Data.Projects;
using Chronos.Protocol;
using Chronos.Server.Jobs;
using Chronos.Server.Security;
@@ -11,8 +14,9 @@ using Chronos.Server.Storage;
namespace Chronos.Server.Api;
/// <summary>
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset), cancel,
/// pause, resume, retry and priority. Changes reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset; a render of a
/// project version is checked against the sequence's projects, owner review 2, R8), cancel, pause, resume, retry and
/// priority. Changes reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
/// </summary>
public static class JobEndpoints
{
@@ -62,7 +66,7 @@ public static class JobEndpoints
}
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, ChronosDatabase db, JobEngineHost host,
ServerArchive archive, AuditLog audit)
ServerArchive archive, AuditLog audit, [FromServices] ArchiveService? archives)
{
var ct = context.RequestAborted;
if (!JobWire.TryParseKind(request.Kind, out var kind) || !(kind == JobKind.Render || SequenceKinds.Contains(kind) || ArchiveKinds.Contains(kind)))
@@ -94,7 +98,11 @@ public static class JobEndpoints
List<(string Label, string? Parameters)> queue = [(label, request.ParametersJson)];
if (kind == JobKind.Render)
{
if (ParseRender(request.ParametersJson) is not { } render) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
if (ParseRender(request.ParametersJson) is not { } parsed) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
// A render of a project version (owner review 2, R8): of the sequence's projects, a project alone pinned to its head.
var (render, missing) = await ProjectRenderAsync(db, archives, targets[0], parsed, ct);
if (missing is not null) return ApiErrors.NotFound(context, missing);
var parametersJson = render == parsed ? request.ParametersJson : JsonSerializer.Serialize(render, ProtocolJson.Default.RenderJobParameters);
// A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7).
IReadOnlyList<RenderJobParameters> each;
try
@@ -107,7 +115,7 @@ public static class JobEndpoints
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
}
queue = each.Count == 1
? [(ProcessingJobs.RenderLabel(label, render.PresetName), request.ParametersJson)]
? [(ProcessingJobs.RenderLabel(label, render.PresetName), parametersJson)]
: [.. each.Select(p => (ProcessingJobs.RenderLabel(label, p.PresetName), (string?)JsonSerializer.Serialize(p, ProtocolJson.Default.RenderJobParameters)))];
}
else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson))
@@ -133,6 +141,51 @@ public static class JobEndpoints
return Results.Json(Dto.Job(first!), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
}
/// <summary>
/// The render of a project version (owner review 2, R8), checked against the sequence: <see cref="RenderJobParameters.ProjectVersionId"/>
/// must be a version of one of its projects (that project, when <see cref="RenderJobParameters.ProjectId"/> names one too) and
/// fills in the project; a live project alone, without settings of its own, renders its head as it is now, which the job then
/// names, so the render records the version it rendered; embedded settings with a project render as they are, recording
/// the project. A deleted project's versions still render (they are kept). Without either, the parameters as they are.
/// Returns what is missing (<c>project</c>, <c>version</c>) instead of the parameters when they do not hold.
/// </summary>
internal static async Task<(RenderJobParameters Parameters, string? Missing)> ProjectRenderAsync(ChronosDatabase db, ArchiveService? archives, Guid sequenceId,
RenderJobParameters render, CancellationToken ct)
{
if (render.ProjectId is null && render.ProjectVersionId is null) return (render, null);
var projects = LibraryEndpoints.Engine(archives, db).Projects;
ProjectVersion? version = null;
if (render.ProjectVersionId is { } versionId && (version = await KnownVersionAsync(versionId)) is null) return (render, "version");
var projectId = render.ProjectId ?? version!.ProjectId;
if (await db.Projects.GetAsync(projectId, ct) is not { } project)
{
// A project the index does not know yet may be in the sequence's sidecars: listing reads them first.
await projects.ListAsync(sequenceId, ct);
project = await db.Projects.GetAsync(projectId, ct);
}
if (project is null || project.SequenceId != sequenceId) return (render, "project");
if (version is not null) return version.ProjectId == project.Id ? (render with { ProjectId = project.Id }, null) : (render, "version");
if (render.SettingsJson is not null) return (render with { ProjectId = project.Id }, null);
try
{
// Caught up first: Main's head is the sequence's settings as they are now.
var head = await projects.HeadAsync(project.Id, ct);
return (render with { ProjectId = project.Id, ProjectVersionId = head.Id }, null);
}
catch (ProjectException)
{
// A deleted project has no head to render; its versions, named by id, still render (they are kept).
return (render, project.DeletedAt is null ? "version" : "project");
}
async Task<ProjectVersion?> KnownVersionAsync(Guid id)
{
if (await db.Projects.GetVersionAsync(id, ct) is { } known) return known;
await projects.ListAsync(sequenceId, ct);
return await db.Projects.GetVersionAsync(id, ct);
}
}
/// <summary>The oriented size of the sequence's first frame (what a preset without a fixed size renders at), else the job's size.</summary>
private static async Task<(int Width, int Height)> SourceSizeAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) =>
(await db.Frames.ListBySequenceAsync(sequenceId, limit: 1, cancellationToken: ct)).FirstOrDefault() is { Width: > 0, Height: > 0 } frame