diff --git a/src/GameCult.Mesh/CultMesh.cs b/src/GameCult.Mesh/CultMesh.cs index 74afbe51..08ecfe36 100644 --- a/src/GameCult.Mesh/CultMesh.cs +++ b/src/GameCult.Mesh/CultMesh.cs @@ -426,6 +426,21 @@ namespace GameCult.Mesh return new CultMeshDocumentHandle(BindLiveFeed(context, feed), replace); } + /// + /// Binds a mutable predicted typed document live feed to a Verse context. + /// + public static CultMeshDocumentHandle BindDocument( + CultMeshVerseContext context, + CultMeshLiveFeed feed, + Func replace, + Func submitPrediction) + where TDocument : class + { + if (replace == null) throw new ArgumentNullException(nameof(replace)); + if (submitPrediction == null) throw new ArgumentNullException(nameof(submitPrediction)); + return new CultMeshDocumentHandle(BindLiveFeed(context, feed), replace, submitPrediction); + } + /// /// Binds a typed document live feed to a Verse. /// @@ -451,6 +466,20 @@ namespace GameCult.Mesh return BindDocument(verse.Context, feed, replace); } + /// + /// Binds a mutable predicted typed document live feed to a Verse. + /// + public static CultMeshDocumentHandle BindDocument( + CultMeshVerse verse, + CultMeshLiveFeed feed, + Func replace, + Func submitPrediction) + where TDocument : class + { + if (verse == null) throw new ArgumentNullException(nameof(verse)); + return BindDocument(verse.Context, feed, replace, submitPrediction); + } + /// /// Binds a typed state pointer to a Verse context. /// @@ -935,6 +964,32 @@ namespace GameCult.Mesh return BindDocument(context, feed, replace); } + /// + /// Creates a mutable predicted typed document handle from snapshot/watch/replace/prediction delegates and binds it to a Verse context. + /// + public static CultMeshDocumentHandle Document( + string documentId, + CultMeshVerseContext context, + Func> latest, + Func> watch, + Func replace, + Func submitPrediction, + IEnumerable? sources = null, + CultMeshRouteHint? routeHint = null) + where TDocument : class + { + if (replace == null) throw new ArgumentNullException(nameof(replace)); + if (submitPrediction == null) throw new ArgumentNullException(nameof(submitPrediction)); + + var feed = LiveFeed( + documentId, + (_parameters, queryContext) => latest(queryContext), + (_parameters, queryContext) => watch(queryContext), + sources, + routeHint); + return BindDocument(context, feed, replace, submitPrediction); + } + /// /// Creates a typed document handle from snapshot/watch delegates and binds it to a Verse. /// @@ -968,6 +1023,24 @@ namespace GameCult.Mesh return Document(documentId, verse.Context, latest, watch, replace, sources, routeHint); } + /// + /// Creates a mutable predicted typed document handle from snapshot/watch/replace/prediction delegates and binds it to a Verse. + /// + public static CultMeshDocumentHandle Document( + string documentId, + CultMeshVerse verse, + Func> latest, + Func> watch, + Func replace, + Func submitPrediction, + IEnumerable? sources = null, + CultMeshRouteHint? routeHint = null) + where TDocument : class + { + if (verse == null) throw new ArgumentNullException(nameof(verse)); + return Document(documentId, verse.Context, latest, watch, replace, submitPrediction, sources, routeHint); + } + /// /// Creates a typed document handle directly over one CultCache record. /// @@ -1068,6 +1141,10 @@ namespace GameCult.Mesh { await database.PutAsync(key, value).ConfigureAwait(false); }, + async value => + { + await database.PutPredictedAsync(key, value).ConfigureAwait(false); + }, sourceList, route); } diff --git a/src/GameCult.Mesh/CultMeshPrimitives.cs b/src/GameCult.Mesh/CultMeshPrimitives.cs index 67ce480f..35ffa740 100644 --- a/src/GameCult.Mesh/CultMeshPrimitives.cs +++ b/src/GameCult.Mesh/CultMeshPrimitives.cs @@ -900,6 +900,9 @@ namespace GameCult.Mesh /// Gets whether this handle can replace the underlying document value. bool CanReplace { get; } + /// Gets whether this handle can submit a client prediction for the underlying document value. + bool CanSubmitPrediction { get; } + /// Creates a same-schema alias presentation for another CLR document type. CultMeshDocumentHandle AsSchemaAlias() where TAlias : class; } @@ -915,14 +918,17 @@ namespace GameCult.Mesh private readonly CultMeshBoundLiveFeed _feed; private readonly Func? _replace; + private readonly Func? _submitPrediction; /// Creates a document handle from a Verse-bound live feed. public CultMeshDocumentHandle( CultMeshBoundLiveFeed feed, - Func? replace = null) + Func? replace = null, + Func? submitPrediction = null) { _feed = feed ?? throw new ArgumentNullException(nameof(feed)); _replace = replace; + _submitPrediction = submitPrediction; } /// Gets the semantic document id. @@ -955,6 +961,9 @@ namespace GameCult.Mesh /// Gets whether this handle can replace the underlying document value. public bool CanReplace => _replace != null; + /// Gets whether this handle can submit a client prediction for the underlying document value. + public bool CanSubmitPrediction => _submitPrediction != null; + /// Reads one coherent document snapshot. public Task LatestAsync() { @@ -992,6 +1001,18 @@ namespace GameCult.Mesh return _replace(value); } + /// Submits a locally predicted document value when this handle is backed by client-authoritative state. + public Task SubmitPredictionAsync(TDocument value) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + if (_submitPrediction == null) + { + throw new NotSupportedException($"Document handle '{DocumentId}' does not accept client predictions."); + } + + return _submitPrediction(value); + } + /// Creates a same-schema alias presentation for another CLR document type. public CultMeshDocumentHandle AsSchemaAlias() where TAlias : class { @@ -1018,8 +1039,11 @@ namespace GameCult.Mesh Func? replace = _replace == null ? null : value => _replace(ConvertDocument(value)); + Func? submitPrediction = _submitPrediction == null + ? null + : value => _submitPrediction(ConvertDocument(value)); - return new CultMeshDocumentHandle(aliasFeed.Bind(Context), replace); + return new CultMeshDocumentHandle(aliasFeed.Bind(Context), replace, submitPrediction); } private static TTarget ConvertDocument(TSource document) diff --git a/src/GameCult.Mesh/README.md b/src/GameCult.Mesh/README.md index 5bd3b1a0..a6699bde 100644 --- a/src/GameCult.Mesh/README.md +++ b/src/GameCult.Mesh/README.md @@ -155,13 +155,14 @@ the record. ```csharp var db = node.Database; +var input = CultMesh.Document(db, inputKey, verse); -await db.PutPredictedAsync(inputKey, inputState); +await input.SubmitPredictionAsync(inputState); -using var sub = db.WatchRecord(inputKey) - .Subscribe(change => +using var sub = input.Watch() + .Subscribe(latest => { - ReconcilePrediction(change); + ReconcilePrediction(latest); }); ``` diff --git a/src/GameCult.Mesh/docs/public-api.md b/src/GameCult.Mesh/docs/public-api.md index 3f46eef7..2fd2ae9d 100644 --- a/src/GameCult.Mesh/docs/public-api.md +++ b/src/GameCult.Mesh/docs/public-api.md @@ -163,13 +163,16 @@ portable machinery that makes it feel native. - `CultMeshDocumentHandle` is the typed reactive document edge for CultCache/CultNet state. It exposes document metadata, one coherent `LatestAsync()` read, a `Watch()` stream, and `ReplaceAsync(...)` when the - handle is backed by mutable state. Callers can bind a projected live feed, a - local `CultCache` record, a distributed `CultNetDatabase` record, or a + handle is backed by mutable state. Distributed `CultNetDatabase` handles also + expose `SubmitPredictionAsync(...)`, which applies a local prediction through + the database's configured client-authority scope and later reconciles through + the normal shard log. Callers can bind a projected live feed, a local + `CultCache` record, a distributed `CultNetDatabase` record, or a `CultMeshNode` record through the same surface instead of walking transport and projection layers themselves. Same-schema CLR aliases can be requested with `AsSchemaAlias()`; CultMesh verifies the shared `[CultDocument(schemaName, schemaVersion)]` identity and uses the shared - CultCache MessagePack codec for read/watch/replace conversion. + CultCache MessagePack codec for read/watch/replace/prediction conversion. - `CultMeshDocumentCatalog` is the schema-aware lookup edge for a set of document handles. It indexes handles by CLR document type, schema name, and schema version, and resolves same-schema CLR aliases by delegating to @@ -487,8 +490,10 @@ methods. Subscriptions receive domain changes rather than storage envelopes. ### Client Prediction `CultNetClientAuthorityScope` declares input documents a runtime may predict. -`PutPredictedAsync` writes local state and emits `Predicted`. When the -authoritative log arrives, the database emits `Reconciled`. +CultMesh document handles opened over a `CultNetDatabase` expose +`SubmitPredictionAsync(...)`, which writes local state and emits `Predicted` +through the configured database authority policy. When the authoritative log +arrives, the database emits `Reconciled`. ### Replica Catch-Up diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 8c73327d..29f675dd 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -754,6 +754,8 @@ public sealed class CultMeshStreamingTests handle.CanReplace.Should().BeTrue(); alias.CanReplace.Should().BeTrue(); + handle.CanSubmitPrediction.Should().BeFalse(); + alias.CanSubmitPrediction.Should().BeFalse(); handle.DocumentId.Should().Be(key.Value); handle.Sources.Should().ContainSingle().Which.SchemaId.Should().Be(handle.SchemaId); @@ -772,6 +774,66 @@ public sealed class CultMeshStreamingTests cache.Get(key)!.Text.Should().Be("cache-replaced"); observed.Text.Should().Be("cache-replaced"); observed.Revision.Should().Be(2); + + Func act = () => alias.SubmitPredictionAsync(new MeshNoteAliasDocument + { + Schema = "tests.mesh_note.v1", + Text = "cache-predicted", + Revision = 3 + }); + await act.Should().ThrowAsync(); + } + + [Test] + public async Task DocumentHandle_SubmitsPredictionsThroughCultNetDatabase() + { + var cache = new CultCache(); + var schemaId = CultDocumentRegistry.Shared.GetRequired().SchemaId; + var database = new CultNetDatabase(cache, new CultNetDatabaseOptions + { + RuntimeId = "pilot-a", + Shards = new[] + { + new CultNetShardDescriptor( + "pilot-inputs", + "server", + epoch: 1, + isPrimary: false, + schemaIds: new[] { schemaId }, + keyPrefix: "input:") + }, + ClientAuthorityScopes = new[] + { + new CultNetClientAuthorityScope( + "pilot-a", + schemaIds: new[] { schemaId }, + keyPrefix: "input:pilot-a") + } + }); + var key = new CultRecordKey("input:pilot-a:thermal"); + + var handle = CultMesh.Document( + database, + key, + CultMesh.Verse("starbridge", "pilot-a")); + var alias = handle.AsSchemaAlias(); + + handle.CanReplace.Should().BeTrue(); + alias.CanSubmitPrediction.Should().BeTrue(); + + MeshNoteDocument observed = null!; + using var subscription = handle.Watch(value => observed = value); + await alias.SubmitPredictionAsync(new MeshNoteAliasDocument + { + Schema = "tests.mesh_note.v1", + Text = "predicted-thermal", + Revision = 5 + }); + + cache.Get(key)!.Text.Should().Be("predicted-thermal"); + observed.Text.Should().Be("predicted-thermal"); + observed.Revision.Should().Be(5); + (await handle.LatestAsync()).Text.Should().Be("predicted-thermal"); } [Test]