Add CultMesh Verse discovery contracts

This commit is contained in:
Meta Vens 2026-05-19 21:23:11 +01:00
commit b831a8f3bf
8 changed files with 349 additions and 1 deletions

View file

@ -196,6 +196,17 @@ namespace GameCult.Mesh
return new CultMeshVerseCatalog();
}
/// <summary>
/// Attaches Verse discovery responses to a CultMesh node.
/// </summary>
public static CultMeshVerseDiscoveryServer ServeVerseCatalog(
CultMeshNode node,
CultMeshVerseCatalog catalog)
{
if (node == null) throw new ArgumentNullException(nameof(node));
return new CultMeshVerseDiscoveryServer(node.Server, catalog);
}
/// <summary>
/// Creates a CultNet client configured for CultMesh use.
/// </summary>

View file

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GameCult.Networking;
using LiteNetLib;
namespace GameCult.Mesh
{
/// <summary>
/// Conversion helpers for CultMesh Verse wire messages.
/// </summary>
public static class CultMeshVerseMessages
{
/// <summary>
/// Converts a local Verse descriptor to its schema-v0 wire shape.
/// </summary>
public static CultMeshVerseDescriptorMessage ToMessage(this CultMeshVerseDescriptor verse)
{
if (verse == null) throw new ArgumentNullException(nameof(verse));
return new CultMeshVerseDescriptorMessage
{
VerseId = verse.VerseId,
DisplayName = verse.DisplayName,
AuthorityModel = verse.AuthorityModel.ToString(),
Compatibility = new CultMeshVerseCompatibilityMessage
{
TransportVersion = verse.Compatibility.TransportVersion,
RulesHash = verse.Compatibility.RulesHash,
CompatibleVerseIds = verse.Compatibility.CompatibleVerseIds.ToArray(),
RequiredPluginIds = verse.Compatibility.RequiredPluginIds.ToArray(),
OptionalPluginIds = verse.Compatibility.OptionalPluginIds.ToArray()
},
DiscoveryEndpoints = verse.DiscoveryEndpoints.ToArray(),
AuthorityRuntimeIds = verse.AuthorityRuntimeIds.ToArray(),
ParentVerseId = verse.ParentVerseId,
Description = verse.Description
};
}
/// <summary>
/// Converts a schema-v0 Verse descriptor to the local public API shape.
/// </summary>
public static CultMeshVerseDescriptor ToVerseDescriptor(this CultMeshVerseDescriptorMessage message)
{
if (message == null) throw new ArgumentNullException(nameof(message));
var authorityModel = Enum.TryParse<CultMeshVerseAuthorityModel>(
message.AuthorityModel,
ignoreCase: true,
out var parsed)
? parsed
: CultMeshVerseAuthorityModel.SubscribedOverlay;
return new CultMeshVerseDescriptor(
message.VerseId,
message.DisplayName,
authorityModel,
new CultMeshVerseCompatibility(
message.Compatibility.TransportVersion,
message.Compatibility.RulesHash,
message.Compatibility.CompatibleVerseIds,
message.Compatibility.RequiredPluginIds,
message.Compatibility.OptionalPluginIds),
message.DiscoveryEndpoints,
message.AuthorityRuntimeIds,
message.ParentVerseId,
message.Description);
}
}
/// <summary>
/// Answers CultMesh Verse discovery requests from a local Verse catalog.
/// </summary>
public sealed class CultMeshVerseDiscoveryServer : IDisposable
{
private readonly Server _server;
private readonly CultMeshVerseCatalog _catalog;
private readonly Func<CultMeshVerseCatalogRequestMessage, NetPeer, Task> _requestHandler;
private bool _disposed;
/// <summary>
/// Creates and attaches a Verse discovery bridge.
/// </summary>
public CultMeshVerseDiscoveryServer(Server server, CultMeshVerseCatalog catalog)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
_requestHandler = HandleRequestAsync;
_server.OnCultNet(_requestHandler);
}
/// <summary>
/// Creates a Verse catalog response for a request.
/// </summary>
public CultMeshVerseCatalogResponseMessage CreateResponse(CultMeshVerseCatalogRequestMessage request)
{
if (request == null) throw new ArgumentNullException(nameof(request));
var verseIds = request.VerseIds == null || request.VerseIds.Length == 0
? null
: new HashSet<string>(request.VerseIds, StringComparer.Ordinal);
var verses = _catalog.Verses
.Where(verse => verseIds == null || verseIds.Contains(verse.VerseId))
.Where(verse => string.IsNullOrWhiteSpace(request.TransportVersion) ||
string.Equals(verse.Compatibility.TransportVersion, request.TransportVersion, StringComparison.Ordinal))
.Select(verse => verse.ToMessage())
.ToArray();
return new CultMeshVerseCatalogResponseMessage
{
MessageId = string.IsNullOrWhiteSpace(request.MessageId)
? Guid.NewGuid().ToString("N")
: request.MessageId,
Verses = verses
};
}
/// <inheritdoc />
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_server.RemoveCultNetMessageListener<CultMeshVerseCatalogRequestMessage>(_requestHandler);
}
private Task HandleRequestAsync(CultMeshVerseCatalogRequestMessage request, NetPeer peer)
{
peer.SendCultNet(CreateResponse(request));
return Task.CompletedTask;
}
}
}

View file

@ -62,8 +62,14 @@ var aetheria = new CultMeshVerseDescriptor(
using var catalog = CultMesh.CreateVerseCatalog();
catalog.Upsert(aetheria);
using var discovery = CultMesh.ServeVerseCatalog(node, catalog);
```
Peers can request Verse catalogs over schema-v0 with
`cultmesh.verse_catalog_request.v0` and receive transport-filtered
`cultmesh.verse_catalog_response.v0` descriptors.
## Client-Side Prediction
CultMesh lets a runtime declare which input documents it may author

View file

@ -42,6 +42,10 @@ known authority runtimes, optional parent Verse id, and plugin requirements.
`CultMeshVerseCatalog` is the local reactive catalog for discovered Verses.
It can publish discovery updates and find compatible transfer targets.
`CultMesh.ServeVerseCatalog(node, catalog)` attaches schema-v0 Verse discovery
responses to a node. The wire contracts are
`cultmesh.verse_catalog_request.v0` and `cultmesh.verse_catalog_response.v0`.
### Shard Authority
Each shard has one primary writer for now. Non-primary writes are rejected or

View file

@ -69,6 +69,8 @@ Implemented:
replacement and cursor advancement
- CultMesh node option for file-backed authoritative shard logs with a default
`.cultmesh/shard-logs` path beside the cache file
- schema-v0 Verse catalog request/response messages
- CultMesh Verse discovery server bridge over the local Verse catalog
- client-authority scopes for locally predicted input documents
- predicted and reconciled database change kinds for client-side prediction
- simulation witness observations for mesh-side opinions about frame facts
@ -121,7 +123,7 @@ Not implemented yet:
Add discovery fanout and operational polish:
- add Verse discovery over the schema-v0 mesh lane
- add peer-to-peer Verse discovery fetchers and gossip fanout
- add snapshot throttling and size limits for public mesh edges
- after that, add simulation-frame rollback/resimulation helpers around
predicted input streams

View file

@ -68,6 +68,8 @@ namespace GameCult.Networking
CultNetSchemaVersions.ShardLogResponse => Deserialize<CultNetShardLogResponseMessage>(payload),
CultNetSchemaVersions.SimulationObservation => Deserialize<CultNetSimulationObservationMessage>(payload),
CultNetSchemaVersions.SimulationConsensusCandidate => Deserialize<CultNetSimulationConsensusCandidateMessage>(payload),
CultNetSchemaVersions.VerseCatalogRequest => Deserialize<CultMeshVerseCatalogRequestMessage>(payload),
CultNetSchemaVersions.VerseCatalogResponse => Deserialize<CultMeshVerseCatalogResponseMessage>(payload),
CultNetSchemaVersions.SchemaCatalogRequest => Deserialize<CultNetSchemaCatalogRequestMessage>(payload),
CultNetSchemaVersions.SchemaCatalogResponse => Deserialize<CultNetSchemaCatalogResponseMessage>(payload),
_ => throw new MessagePackSerializationException(

View file

@ -115,6 +115,14 @@ namespace GameCult.Networking
/// simulation consensus candidate gossip contract identifier.
/// </summary>
public const string SimulationConsensusCandidate = "cultnet.simulation_consensus_candidate.v0";
/// <summary>
/// CultMesh Verse catalog request contract identifier.
/// </summary>
public const string VerseCatalogRequest = "cultmesh.verse_catalog_request.v0";
/// <summary>
/// CultMesh Verse catalog response contract identifier.
/// </summary>
public const string VerseCatalogResponse = "cultmesh.verse_catalog_response.v0";
}
/// <summary>
@ -957,6 +965,118 @@ namespace GameCult.Networking
}
}
/// <summary>
/// Requests known CultMesh Verses from a peer.
/// </summary>
[MessagePackObject]
public class CultMeshVerseCatalogRequestMessage : ICultNetSchemaMessage
{
/// <summary>
/// Gets or sets the schema version.
/// </summary>
[Key("schemaVersion")] public string SchemaVersion { get; set; } = CultNetSchemaVersions.VerseCatalogRequest;
/// <summary>
/// Gets or sets the request id.
/// </summary>
[Key("messageId")] public string MessageId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets optional Verse ids to request.
/// </summary>
[Key("verseIds")] public string[]? VerseIds { get; set; }
/// <summary>
/// Gets or sets an optional transport compatibility filter.
/// </summary>
[Key("transportVersion")] public string? TransportVersion { get; set; }
}
/// <summary>
/// Returns known CultMesh Verses.
/// </summary>
[MessagePackObject]
public class CultMeshVerseCatalogResponseMessage : ICultNetSchemaMessage
{
/// <summary>
/// Gets or sets the schema version.
/// </summary>
[Key("schemaVersion")] public string SchemaVersion { get; set; } = CultNetSchemaVersions.VerseCatalogResponse;
/// <summary>
/// Gets or sets the request id.
/// </summary>
[Key("messageId")] public string MessageId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets discovered Verses.
/// </summary>
[Key("verses")] public CultMeshVerseDescriptorMessage[] Verses { get; set; } = Array.Empty<CultMeshVerseDescriptorMessage>();
}
/// <summary>
/// Wire representation of a CultMesh Verse.
/// </summary>
[MessagePackObject]
public class CultMeshVerseDescriptorMessage
{
/// <summary>
/// Gets or sets the stable Verse id.
/// </summary>
[Key("verseId")] public string VerseId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the display name.
/// </summary>
[Key("displayName")] public string DisplayName { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the authority model name.
/// </summary>
[Key("authorityModel")] public string AuthorityModel { get; set; } = string.Empty;
/// <summary>
/// Gets or sets compatibility metadata.
/// </summary>
[Key("compatibility")] public CultMeshVerseCompatibilityMessage Compatibility { get; set; } = new CultMeshVerseCompatibilityMessage();
/// <summary>
/// Gets or sets endpoints that can answer discovery for this Verse.
/// </summary>
[Key("discoveryEndpoints")] public string[] DiscoveryEndpoints { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets known authoritative runtime ids.
/// </summary>
[Key("authorityRuntimeIds")] public string[] AuthorityRuntimeIds { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets the parent Verse id for overlays or branches.
/// </summary>
[Key("parentVerseId")] public string? ParentVerseId { get; set; }
/// <summary>
/// Gets or sets an optional public description.
/// </summary>
[Key("description")] public string? Description { get; set; }
}
/// <summary>
/// Wire representation of CultMesh Verse compatibility.
/// </summary>
[MessagePackObject]
public class CultMeshVerseCompatibilityMessage
{
/// <summary>
/// Gets or sets the CultMesh transport compatibility version.
/// </summary>
[Key("transportVersion")] public string TransportVersion { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the stable rules hash.
/// </summary>
[Key("rulesHash")] public string RulesHash { get; set; } = string.Empty;
/// <summary>
/// Gets or sets Verse ids this Verse can transfer from or subscribe to.
/// </summary>
[Key("compatibleVerseIds")] public string[] CompatibleVerseIds { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets required plugin ids.
/// </summary>
[Key("requiredPluginIds")] public string[] RequiredPluginIds { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets optional plugin ids.
/// </summary>
[Key("optionalPluginIds")] public string[] OptionalPluginIds { get; set; } = Array.Empty<string>();
}
/// <summary>
/// Describes one schema exposed through the CultNet schema catalog.
/// </summary>

View file

@ -328,6 +328,43 @@ namespace GameCult.Networking.Tests
Assert.That(roundTrip.Entries[0].Put!.Document.RecordKey, Is.EqualTo("player:42"));
}
[Test]
public void CultNetSchemaMessageSerialization_RoundTrips_VerseCatalogResponse()
{
var message = new CultMeshVerseCatalogResponseMessage
{
MessageId = "verses-1",
Verses =
[
new CultMeshVerseDescriptorMessage
{
VerseId = "aetheria-main",
DisplayName = "Aetheria",
AuthorityModel = "OperatorCluster",
Compatibility = new CultMeshVerseCompatibilityMessage
{
TransportVersion = "cultmesh.v0",
RulesHash = "rules",
CompatibleVerseIds = ["aetheria-modded"],
RequiredPluginIds = ["core"],
OptionalPluginIds = ["skylands"]
},
DiscoveryEndpoints = ["cultmesh://aetheria.example.test:3075"],
AuthorityRuntimeIds = ["runtime-a"],
Description = "main branch"
}
]
};
var payload = CultNetSchemaMessageSerialization.Serialize(message);
var roundTrip = (CultMeshVerseCatalogResponseMessage)CultNetSchemaMessageSerialization.Deserialize(payload);
Assert.That(roundTrip.MessageId, Is.EqualTo("verses-1"));
Assert.That(roundTrip.Verses, Has.Length.EqualTo(1));
Assert.That(roundTrip.Verses[0].VerseId, Is.EqualTo("aetheria-main"));
Assert.That(roundTrip.Verses[0].Compatibility.RequiredPluginIds, Is.EqualTo(["core"]));
}
[Test]
public void CultNetSchemaMessageSerialization_RoundTrips_SimulationObservation()
{
@ -792,6 +829,38 @@ namespace GameCult.Networking.Tests
Assert.That(incompatible.CanTransferFrom(aetheria), Is.False);
}
[Test]
public void CultMeshVerseDiscoveryServer_Creates_FilteredCatalogResponse()
{
using var catalog = CultMesh.CreateVerseCatalog();
var rulesHash = CultMeshVerseDescriptor.ComputeRulesHash("aetheria", "vanilla");
catalog.Upsert(new CultMeshVerseDescriptor(
"aetheria-main",
"Aetheria",
CultMeshVerseAuthorityModel.OperatorCluster,
new CultMeshVerseCompatibility("cultmesh.v0", rulesHash),
discoveryEndpoints: ["cultmesh://aetheria.example.test:3075"],
authorityRuntimeIds: ["runtime-a"]));
catalog.Upsert(new CultMeshVerseDescriptor(
"old-branch",
"Old Branch",
CultMeshVerseAuthorityModel.PeerToPeer,
new CultMeshVerseCompatibility("cultmesh.legacy", rulesHash)));
using var server = new Server(new CultCache(), DevelopmentServerSecurity);
using var discovery = new CultMeshVerseDiscoveryServer(server, catalog);
var response = discovery.CreateResponse(new CultMeshVerseCatalogRequestMessage
{
MessageId = "discover-1",
TransportVersion = "cultmesh.v0"
});
Assert.That(response.MessageId, Is.EqualTo("discover-1"));
Assert.That(response.Verses, Has.Length.EqualTo(1));
Assert.That(response.Verses[0].VerseId, Is.EqualTo("aetheria-main"));
Assert.That(response.Verses[0].DiscoveryEndpoints, Is.EqualTo(["cultmesh://aetheria.example.test:3075"]));
}
[Test]
public async Task CultMeshNode_CanEnable_DurableShardLogs_WithDefaultPath()
{