From 9758940b35827b0ff0f1a93964390e22a71c26e7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 22 Jul 2026 04:01:20 +0100 Subject: [PATCH] Integrate geometry worker through CultMesh --- ...th-gamecult-geometry-migration-progress.md | 39 +++- .../CultGeometryWorkerDocuments.cs | 87 +++++++++ .../CultGeometryWorkerProvider.cs | 124 ++++++++++++ .../GameCult.Geometry.csproj | 2 + .../CultGeometryWorkerProviderTests.cs | 182 ++++++++++++++++++ 5 files changed, 424 insertions(+), 10 deletions(-) create mode 100644 src/GameCult.Geometry/CultGeometryWorkerDocuments.cs create mode 100644 src/GameCult.Geometry/CultGeometryWorkerProvider.cs create mode 100644 tests/GameCult.Geometry.Tests/CultGeometryWorkerProviderTests.cs diff --git a/docs/cultmath-gamecult-geometry-migration-progress.md b/docs/cultmath-gamecult-geometry-migration-progress.md index ace7653e..79dce55e 100644 --- a/docs/cultmath-gamecult-geometry-migration-progress.md +++ b/docs/cultmath-gamecult-geometry-migration-progress.md @@ -4,7 +4,7 @@ Plan: `docs/cultmath-gamecult-geometry-migration-plan.md` (locked) Started: 2026-07-22 -Status: Stages 0-4 complete; Stage 5 in progress +Status: Stages 0-5 complete; Stage 6 in progress ## Objective @@ -59,8 +59,8 @@ Demotions: | 2. Primitive deletion and v2 schemas | Complete | C# and Geometry-owned Rust emit identical v2 bytes and record keys for all four roots | | 3. Planetary transfer | Complete | Managed, HLSL, CMPT, Unity, web, tool, docs, tests, and package authority moved; CultMath owners deleted | | 4. Mine `vg-csg` | Complete | Geometry-owned Rust kernel passes correctness, parity, performance, formatting, and warning-denied lint gates | -| 5. CultCache/CultNet/CultMesh integration | In progress | Provider/dataflow mapped; typed commit-path smoke remains | -| 6. Consumer cutover/release | Pending | Requires local candidate feed and downstream passes | +| 5. CultCache/CultNet/CultMesh integration | Complete | One provider commit path persists, watches, replicates, and probes typed geometry state | +| 6. Consumer cutover/release | In progress | Fensalir/Aetheria exact cutover surfaces mapped; consumer edits and release receipts remain | | 7. Delete obsolete authorities/audit | Pending | Requires every prior stage gate | ## Verification Receipts @@ -244,13 +244,32 @@ a broader stage gate. passes 67/67 plus doc tests; rustfmt and warning-denied Clippy pass. - The authority transfer commits are CultLib `756f5a33`, CultMath `6a16260`, and the post-transfer label cleanup `99c893bd`. The Rust absorption commit is - recorded after this progress update is committed. + CultLib `6b44f969`. + +### Stage 5 receipts + +- `CultGeometryWorkerProvider` owns the build invocation and output commit + sequence. Direct calls and the `gamecult.geometry.worker.build` CultMesh + operation share `BuildAsync`; compute is injected through the persistence-free + `ICultGeometryBuildPipeline` port. +- Domain/request reads and selected-cut, artifact, and observational worker-state + writes use `CultNetDatabase` typed APIs. Watches and raw MessagePack envelopes + are consequences of that commit path rather than parallel publishers. +- The development probe derives owner, v2 schema, request/cut keys, selected + nodes, artifact identities, content hashes, and served assembly version from + persisted documents. Mutating worker display state cannot overwrite geometry + outputs or forge the served version. +- Worker state registers through the generated CultDocument registry. Focused + provider tests pass 4/4 and the full GameCult.Geometry suite passes 31/31. +- Rust raw-envelope duplication was deliberately omitted: CultNet owns that + protocol. Existing C#/Rust exact v2 document bytes and record keys remain the + cross-runtime geometry witness. ## Next Actions -1. Add the Geometry-owned worker provider with `CultNetDatabase.PutAsync` as - the single commit/publication primitive for direct and operation-handle use. -2. Prove domain/request to selected-cut/artifact persistence, raw envelope - replication, worker-state observation, and typed dev-probe agreement. -3. Add exact C#/Rust raw-envelope parity without growing a second networking - stack or a load-bearing JSON projection. +1. Cut Fensalir's C# and HLSL planetary surfaces atomically to + GameCult.Geometry, including shader resolver and parity harnesses. +2. Replace Aetheria Freeze's remaining `CultVec*` fixtures with CultMath values + under an exact serialization witness, then regenerate Unity project files. +3. Rebuild coordinated CultMath and Geometry candidates and run the named + downstream parity corpus before publication. diff --git a/src/GameCult.Geometry/CultGeometryWorkerDocuments.cs b/src/GameCult.Geometry/CultGeometryWorkerDocuments.cs new file mode 100644 index 00000000..280f7c4f --- /dev/null +++ b/src/GameCult.Geometry/CultGeometryWorkerDocuments.cs @@ -0,0 +1,87 @@ +using System; +using GameCult.Caching; +using MessagePack; + +namespace GameCult.Geometry +{ + public static class CultGeometryWorkerSchemaVersions + { + public const string State = "gamecult.geometry.worker_state.v1"; + } + + [MessagePackObject] + [CultDocument("gamecult.geometry.worker_state", CultGeometryWorkerSchemaVersions.State)] + public sealed class CultGeometryWorkerState + { + [Key(0), CultName] + public string WorkerId { get; set; } = string.Empty; + + [Key(1)] + public string Phase { get; set; } = string.Empty; + + [Key(2), CultReference(typeof(CultGeometryBuildRequest))] + public string ActiveRequestKey { get; set; } = string.Empty; + + [Key(3), CultReference(typeof(CultGeometrySelectedCutManifest))] + public string LastSelectedCutKey { get; set; } = string.Empty; + + [Key(4)] + public string[] LastArtifactKeys { get; set; } = Array.Empty(); + + [Key(5)] + public string LastError { get; set; } = string.Empty; + + [Key(6)] + public string UpdatedAt { get; set; } = string.Empty; + + [Key(7)] + public string ServedPackageVersion { get; set; } = string.Empty; + + public static CultRecordKey CreateRecordKey(string workerId) + { + if (string.IsNullOrWhiteSpace(workerId)) throw new ArgumentException("Value must be non-empty.", nameof(workerId)); + return new CultRecordKey("geometry:worker:" + workerId); + } + } + + [MessagePackObject] + public sealed class CultGeometryBuildCommand + { + [Key(0)] + public string RequestKey { get; set; } = string.Empty; + } + + [MessagePackObject] + public sealed class CultGeometryBuildReceipt + { + [Key(0)] + public string RequestKey { get; set; } = string.Empty; + + [Key(1)] + public string SelectedCutKey { get; set; } = string.Empty; + + [Key(2)] + public string[] ArtifactKeys { get; set; } = Array.Empty(); + + [Key(3)] + public string[] ContentHashes { get; set; } = Array.Empty(); + } + + public sealed class CultGeometryBuildOutput + { + public CultGeometrySelectedCutManifest SelectedCut { get; init; } = new(); + public CultGeometryChunkArtifact[] Artifacts { get; init; } = Array.Empty(); + } + + public sealed class CultGeometryDevelopmentProbe + { + public string Owner { get; init; } = string.Empty; + public string SchemaVersion { get; init; } = string.Empty; + public string SourceRecordKey { get; init; } = string.Empty; + public string SelectedCutKey { get; init; } = string.Empty; + public string[] SelectedNodes { get; init; } = Array.Empty(); + public string[] ArtifactKeys { get; init; } = Array.Empty(); + public string[] ContentHashes { get; init; } = Array.Empty(); + public string ServedPackageVersion { get; init; } = string.Empty; + } +} diff --git a/src/GameCult.Geometry/CultGeometryWorkerProvider.cs b/src/GameCult.Geometry/CultGeometryWorkerProvider.cs new file mode 100644 index 00000000..745da5a1 --- /dev/null +++ b/src/GameCult.Geometry/CultGeometryWorkerProvider.cs @@ -0,0 +1,124 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using GameCult.Caching; +using GameCult.Mesh; +using GameCult.Networking; + +namespace GameCult.Geometry +{ + public interface ICultGeometryBuildPipeline + { + Task BuildAsync( + CultGeometryDomainDocument domain, + CultGeometryBuildRequest request); + } + + /// Owns geometry worker commands and the authoritative output commit path. + public sealed class CultGeometryWorkerProvider + { + public const string Owner = "GameCult.Geometry"; + public const string BuildOperationId = "gamecult.geometry.worker.build"; + + private readonly string _workerId; + private readonly CultNetDatabase _database; + private readonly ICultGeometryBuildPipeline _pipeline; + + public CultGeometryWorkerProvider( + string workerId, + CultNetDatabase database, + ICultGeometryBuildPipeline pipeline) + { + _workerId = string.IsNullOrWhiteSpace(workerId) + ? throw new ArgumentException("Value must be non-empty.", nameof(workerId)) + : workerId; + _database = database ?? throw new ArgumentNullException(nameof(database)); + _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline)); + BuildOperation = new CultMeshOperationHandle( + BuildOperationId, + (command, _) => BuildAsync(command)); + } + + public CultMeshOperationHandle BuildOperation { get; } + + public CultRecordKey WorkerStateKey => CultGeometryWorkerState.CreateRecordKey(_workerId); + + public async Task BuildAsync(CultGeometryBuildCommand command) + { + if (command == null) throw new ArgumentNullException(nameof(command)); + if (string.IsNullOrWhiteSpace(command.RequestKey)) + throw new ArgumentException("Request key must be non-empty.", nameof(command)); + + var requestKey = new CultRecordKey(command.RequestKey); + var request = await RequireAsync(requestKey).ConfigureAwait(false); + var domain = await RequireAsync(new CultRecordKey(request.DomainKey)).ConfigureAwait(false); + var output = await _pipeline.BuildAsync(domain, request).ConfigureAwait(false) + ?? throw new InvalidOperationException("Geometry build pipeline returned no output."); + + output.SelectedCut.RequestKey = requestKey.Value; + var cutKey = CultGeometrySelectedCutManifest.CreateRecordKey(output.SelectedCut); + await _database.PutAsync(cutKey, output.SelectedCut).ConfigureAwait(false); + + var artifactKeys = new string[output.Artifacts.Length]; + for (var index = 0; index < output.Artifacts.Length; index++) + { + var artifact = output.Artifacts[index]; + artifact.CutKey = cutKey.Value; + artifact.SelectedCutId = output.SelectedCut.CutId; + var artifactKey = CultGeometryChunkArtifact.CreateRecordKey(artifact); + await _database.PutAsync(artifactKey, artifact).ConfigureAwait(false); + artifactKeys[index] = artifactKey.Value; + } + + var receipt = new CultGeometryBuildReceipt + { + RequestKey = requestKey.Value, + SelectedCutKey = cutKey.Value, + ArtifactKeys = artifactKeys, + ContentHashes = artifactKeys.Select(ContentHashFromKey).ToArray() + }; + await _database.PutAsync(WorkerStateKey, new CultGeometryWorkerState + { + WorkerId = _workerId, + Phase = "completed", + ActiveRequestKey = requestKey.Value, + LastSelectedCutKey = cutKey.Value, + LastArtifactKeys = artifactKeys, + UpdatedAt = DateTimeOffset.UtcNow.ToString("O"), + ServedPackageVersion = PackageVersion + }).ConfigureAwait(false); + return receipt; + } + + public async Task ProbeAsync() + { + var state = await RequireAsync(WorkerStateKey).ConfigureAwait(false); + var cut = await RequireAsync(new CultRecordKey(state.LastSelectedCutKey)).ConfigureAwait(false); + return new CultGeometryDevelopmentProbe + { + Owner = Owner, + SchemaVersion = CultGeometrySchemaVersions.SelectedCut, + SourceRecordKey = state.ActiveRequestKey, + SelectedCutKey = state.LastSelectedCutKey, + SelectedNodes = cut.SelectedNodes, + ArtifactKeys = state.LastArtifactKeys, + ContentHashes = state.LastArtifactKeys.Select(ContentHashFromKey).ToArray(), + ServedPackageVersion = PackageVersion + }; + } + + private async Task RequireAsync(CultRecordKey key) where T : class => + await _database.GetAsync(key).ConfigureAwait(false) + ?? throw new InvalidOperationException($"Required geometry document '{key.Value}' was not found."); + + private static string ContentHashFromKey(string key) => + key[(key.LastIndexOf(':') + 1)..]; + + private static string PackageVersion => + typeof(CultGeometryWorkerProvider).Assembly + .GetCustomAttribute()?.InformationalVersion + ?? typeof(CultGeometryWorkerProvider).Assembly.GetName().Version?.ToString() + ?? "unknown"; + } +} diff --git a/src/GameCult.Geometry/GameCult.Geometry.csproj b/src/GameCult.Geometry/GameCult.Geometry.csproj index 32d72d0a..5f45e89b 100644 --- a/src/GameCult.Geometry/GameCult.Geometry.csproj +++ b/src/GameCult.Geometry/GameCult.Geometry.csproj @@ -13,6 +13,8 @@ + + diff --git a/tests/GameCult.Geometry.Tests/CultGeometryWorkerProviderTests.cs b/tests/GameCult.Geometry.Tests/CultGeometryWorkerProviderTests.cs new file mode 100644 index 00000000..0941f0f4 --- /dev/null +++ b/tests/GameCult.Geometry.Tests/CultGeometryWorkerProviderTests.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using CultMath; +using FluentAssertions; +using GameCult.Caching; +using GameCult.Mesh; +using GameCult.Networking; +using NUnit.Framework; +using R3; + +namespace GameCult.Geometry.Tests +{ + public sealed class CultGeometryWorkerProviderTests + { + [Test] + public async Task BuildOperation_UsesTheDirectCommitPath_AndPublishesTypedState() + { + using var database = CreateDatabase("geometry-worker"); + var (domain, request) = await SeedAsync(database); + var provider = new CultGeometryWorkerProvider("worker-1", database, new FakePipeline()); + var artifactChanges = new List>(); + using var subscription = database.Watch() + .Subscribe(change => artifactChanges.Add(change)); + + var receipt = await provider.BuildOperation.InvokeAsync( + new CultGeometryBuildCommand { RequestKey = CultGeometryBuildRequest.CreateRecordKey(request).Value }, + "test-runtime"); + + receipt.RequestKey.Should().Be(CultGeometryBuildRequest.CreateRecordKey(request).Value); + receipt.ArtifactKeys.Should().ContainSingle(); + receipt.ContentHashes.Should().ContainSingle(); + artifactChanges.Should().ContainSingle(change => change.Key.Value == receipt.ArtifactKeys[0]); + (await database.GetAsync(new CultRecordKey(receipt.SelectedCutKey))) + .Should().NotBeNull(); + (await database.GetAsync(new CultRecordKey(receipt.ArtifactKeys[0]))) + .Should().NotBeNull(); + + var state = await database.GetAsync(provider.WorkerStateKey); + state.Should().NotBeNull(); + state!.ActiveRequestKey.Should().Be(receipt.RequestKey); + state.LastSelectedCutKey.Should().Be(receipt.SelectedCutKey); + domain.DomainId.Should().Be("domain-1"); + } + + [Test] + public async Task DirectAndOperationBuilds_DeriveTheSameCanonicalOutputKeys() + { + using var database = CreateDatabase("geometry-direct-operation"); + var (_, request) = await SeedAsync(database); + var provider = new CultGeometryWorkerProvider("worker-1", database, new FakePipeline()); + var command = new CultGeometryBuildCommand + { + RequestKey = CultGeometryBuildRequest.CreateRecordKey(request).Value + }; + + var direct = await provider.BuildAsync(command); + var operation = await provider.BuildOperation.InvokeAsync(command, "test-runtime"); + + operation.SelectedCutKey.Should().Be(direct.SelectedCutKey); + operation.ArtifactKeys.Should().Equal(direct.ArtifactKeys); + operation.ContentHashes.Should().Equal(direct.ContentHashes); + } + + [Test] + public async Task Artifact_RawEnvelopeReplicates_AndWorkerStateCannotOverrideProbeTruth() + { + using var source = CreateDatabase("geometry-source"); + var (_, request) = await SeedAsync(source); + var provider = new CultGeometryWorkerProvider("worker-1", source, new FakePipeline()); + var receipt = await provider.BuildAsync(new CultGeometryBuildCommand + { + RequestKey = CultGeometryBuildRequest.CreateRecordKey(request).Value + }); + var artifactKey = new CultRecordKey(receipt.ArtifactKeys[0]); + var artifact = await source.GetAsync(artifactKey); + artifact.Should().NotBeNull(); + + var put = source.Documents.CreateRawDocumentPutMessage( + "geometry-artifact", + new CultRecordHandle(artifactKey), + artifact!, + new CultNetDocumentMessageOptions { SourceRuntimeId = "geometry-source", SourceRole = "geometry-worker" }); + put.Document.PayloadEncoding.Should().Be("messagepack"); + + var targetCache = new CultCache(); + var replicated = await source.Documents.ApplyRawDocumentPutMessageAsync(targetCache, put); + CultGeometryChunkArtifact.CreateRecordKey(replicated).Should().Be(artifactKey); + + await source.PutAsync(provider.WorkerStateKey, new CultGeometryWorkerState + { + WorkerId = "worker-1", + Phase = "lying-status", + ActiveRequestKey = receipt.RequestKey, + LastSelectedCutKey = receipt.SelectedCutKey, + LastArtifactKeys = receipt.ArtifactKeys, + ServedPackageVersion = "false-version" + }); + var probe = await provider.ProbeAsync(); + + probe.Owner.Should().Be(CultGeometryWorkerProvider.Owner); + probe.SchemaVersion.Should().Be(CultGeometrySchemaVersions.SelectedCut); + probe.SelectedCutKey.Should().Be(receipt.SelectedCutKey); + probe.ContentHashes.Should().Equal(receipt.ContentHashes); + probe.ServedPackageVersion.Should().NotBe("false-version"); + (await source.GetAsync(artifactKey))!.ChunkId.Should().Be("chunk-1"); + } + + [Test] + public void WorkerState_RegistersThroughTheGeneratedDocumentRegistry() + { + var descriptor = new CultCache().Registry.GetRequired(); + descriptor.SchemaVersion.Should().Be(CultGeometryWorkerSchemaVersions.State); + descriptor.SchemaName.Should().Be("gamecult.geometry.worker_state"); + } + + private static CultNetDatabase CreateDatabase(string runtimeId) + { + var cache = new CultCache(); + return new CultNetDatabase(cache, new CultNetDatabaseOptions + { + RuntimeId = runtimeId, + DocumentRegistry = new CultNetDocumentRegistry(cache.Registry) + }); + } + + private static async Task<(CultGeometryDomainDocument Domain, CultGeometryBuildRequest Request)> SeedAsync( + CultNetDatabase database) + { + var domain = new CultGeometryDomainDocument + { + DomainId = "domain-1", + RootKey = "root", + SourceRuntime = "test", + Root = new CultGeometryDomainNode { Name = "root", Kind = "root" } + }; + var domainKey = CultGeometryDomainDocument.CreateRecordKey(domain); + var request = new CultGeometryBuildRequest + { + RequestId = "request-1", + DomainKey = domainKey.Value, + WorkerGroup = "test", + ViewportHeightPixels = 1080, + VerticalFovRadians = 1, + TargetError = 0.01f, + TriangleBudget = 100, + ColliderBudget = 100 + }; + await database.PutAsync(domainKey, domain); + await database.PutAsync(CultGeometryBuildRequest.CreateRecordKey(request), request); + return (domain, request); + } + + private sealed class FakePipeline : ICultGeometryBuildPipeline + { + public Task BuildAsync( + CultGeometryDomainDocument domain, + CultGeometryBuildRequest request) + { + return Task.FromResult(new CultGeometryBuildOutput + { + SelectedCut = new CultGeometrySelectedCutManifest + { + CutId = "cut-1", + SelectedNodes = [domain.Root.Name] + }, + Artifacts = + [ + new CultGeometryChunkArtifact + { + ChunkId = "chunk-1", + BoundsMin = float3.zero, + BoundsMax = new float3(1, 1, 1), + SourceDomainKeys = [request.DomainKey], + RenderMesh = new CultGeometryTriangleMesh() + } + ] + }); + } + } + } +}