Merge the TypeScript QUIC realtime line (Cuts 3-6 and retention)

What lands:
- Cut 3: the native MsQuic bridge, with the Windows release leg and F5
  closed on both platforms, and the closerace fixture fix.
- Cut 4: the koffi Node binding, consumer, session manager and trust.
- Cut 5: the TS provider. It reaches the C# native connector, which is
  the Unity overlay's path, on Windows.
- Fix batches 1-4: use-after-free, handler isolation, the
  handshake-timer eviction, the koffi allocation leak, one C#-shaped
  inbox, encode-once, advertisedHost, publish-only
  (acceptClientFrames: false), and maxConnections.
- Retention: the provider seeds late joiners with the newest
  latest-only frame per (channel, body), in TS and C#.
- Cut 6, narrowed: native binaries committed under
  packages/cultmesh-ts/native, built by Actions. A second from-scratch
  build reproduced them byte for byte.

Gates: four Opus Soul passes on the runtime and one Sonnet pass on the
packaging. The last runtime pass found that the connection death under
load was the old handshake eviction, which fix 3a had removed, and it
confirmed StreamPixels' load (60 Hz latest-only for 10 minutes) with
flat memory.

Also carried, per the operator's ruling: CultMath erf/erfinv (Cut 12.0
and its fix batch) from the branch base.

Map conflicts were resolved in favour of main, which owns the maps. The
only branch-side lines main lacked were a Cut 3 "still outstanding"
block that main's closure supersedes.

Open follow-ups, recorded on the map: IPv6-literal endpoints cannot be
dialled; consumer-side native queues are unbounded while Unity is not
polling; a client can see connect() resolve past maxConnections before
the refusal arrives; subtraction (about -130, mostly comments) is
deferred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-23 23:51:19 +02:00
commit d110d9d6ff
45 changed files with 6959 additions and 47 deletions

View file

@ -158,6 +158,16 @@ public sealed class CultMeshQuicRealtimeServer : IAsyncDisposable
private readonly CancellationTokenSource _shutdown = new();
private readonly ConcurrentDictionary<CultMeshQuicRealtimeTransport, byte> _clients = new();
private readonly CultMeshRealtimeInbox _received = new();
/// <summary>Guards <see cref="_retained"/> together with client registration, so a newly
/// accepted client's seed and a concurrent broadcast's retained update are totally ordered:
/// a client only ever becomes visible to <see cref="BroadcastAsync"/> already carrying the
/// newest retained generation, and can never be seeded with a frame older than one it already
/// received via broadcast.</summary>
private readonly object _retainedGate = new();
/// <summary>The newest `latest-only` frame broadcast so far per `(channel, body)` key, seeded
/// into every client accepted after that broadcast. Reliable-ordered frames are events, not
/// state, and are never retained.</summary>
private readonly Dictionary<string, CultMeshRealtimeFrame> _retained = new(StringComparer.Ordinal);
private readonly TaskCompletionSource<Exception> _backgroundFailure = new(
TaskCreationOptions.RunContinuationsAsynchronously);
private readonly Task _acceptLoop;
@ -222,10 +232,22 @@ public sealed class CultMeshQuicRealtimeServer : IAsyncDisposable
if (frame.Delivery == CultMeshRealtimeDelivery.LatestOnly)
{
foreach (var client in _clients.Keys)
var key = frame.ChannelId + "\u001f" + frame.BodyId;
var candidate = new CultMeshQuicRealtimeTransport.CultMeshGeneration(frame.ProducerEpoch, frame.Sequence);
lock (_retainedGate)
{
if (client.TryPublishLatest(frame)) continue;
if (_clients.TryRemove(client, out _)) client.Dispose();
if (!_retained.TryGetValue(key, out var current) ||
candidate.CompareTo(new CultMeshQuicRealtimeTransport.CultMeshGeneration(
current.ProducerEpoch, current.Sequence)) > 0)
{
_retained[key] = frame;
}
foreach (var client in _clients.Keys)
{
if (client.TryPublishLatest(frame)) continue;
if (_clients.TryRemove(client, out _)) client.Dispose();
}
}
return;
}
@ -284,7 +306,11 @@ public sealed class CultMeshQuicRealtimeServer : IAsyncDisposable
connection.RemoteEndPoint?.ToString() ?? "quic-peer",
connection,
frame => _received.Publish(frame));
_clients.TryAdd(transport, 0);
lock (_retainedGate)
{
_clients.TryAdd(transport, 0);
foreach (var retainedFrame in _retained.Values) transport.TryPublishLatest(retainedFrame);
}
_ = transport.Completion.ContinueWith(
completed =>
{
@ -559,7 +585,7 @@ internal sealed class CultMeshQuicRealtimeTransport : ICultMeshRealtimeTransport
_received.Publish(frame);
}
private readonly record struct CultMeshGeneration(long ProducerEpoch, long Sequence) : IComparable<CultMeshGeneration>
internal readonly record struct CultMeshGeneration(long ProducerEpoch, long Sequence) : IComparable<CultMeshGeneration>
{
public int CompareTo(CultMeshGeneration other)
{

View file

@ -66,7 +66,8 @@ var frame = await session.ReceiveAsync(cancellationToken);
- `ReliableOrdered` uses one ordered QUIC stream.
- `LatestOnly` uses independent streams and keeps at most one pending frame per
channel and body. A newer `(producer epoch, sequence)` generation replaces
queued state.
queued state. The provider also retains the newest `LatestOnly` frame per
`(channel, body)` and delivers it to each client as soon as it attaches.
- `Unreliable` fails closed in this adapter because `System.Net.Quic` does not
expose QUIC datagrams. Use a native MsQuic connector when datagram semantics
are required.

View file

@ -55,7 +55,11 @@ broadcast enqueues into a keyed, coalescing outbox owned by each physical peer
and returns without waiting for network delivery. The peer's transport worker
owns stream writes and eviction after disconnect or send failure. Thus a slow,
stalled, or departed client cannot backpressure a simulation tick, while a
healthy peer still converges on the newest generation. Reliable-ordered
healthy peer still converges on the newest generation. The provider also
retains the newest latest-only frame per `(channel, body)` and seeds it into
each newly attached client, bounded by the number of distinct keys, so a peer
that connects or reconnects after a broadcast still converges on current
state without a service-side replay loop. Reliable-ordered
broadcast remains completion-bearing because dropping or replacing it would
violate its declared delivery contract; caller cancellation can stop that
operation.