feat(events): opt-in stream-attached acknowledgement on StreamEvents (#227)
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m28s
ci / java (push) Failing after 2m21s
ci / portable (push) Failing after 3m33s

A fresh StreamEvents is never replayed, so an initial value published before
the gateway registers the stream's subscriber is lost (lmxopcua#865 —
slow-changing ScanState). Clients had nothing deterministic to wait on.

Contract (additive, proto3):
- StreamEventsRequest.emit_stream_attached = 3 (bool, default false)
- MxEvent.stream_attached = 15 (optional StreamAttached)
- message StreamAttached { uint64 live_after_sequence = 1; }
- OpenSessionReply capability "stream-events-attached-ack"
  (GatewayCapabilities.StreamEventsAttachedAck)

When requested, EventStreamService yields the sentinel as the FIRST message,
after AttachEventSubscriber/AttachEventSubscriberWithReplay has returned, so
receipt implies registration; refused attaches never see it. It precedes the
ReplayGap sentinel and replay on a resume. Shape mirrors ReplayGap: family
UNSPECIFIED, no body, worker_sequence 0.

Opt-in rather than always-on: ScadaBridge's MxGateway adapter stores every
streamed event's worker_sequence as its resume cursor, so an unrequested
sentinel would reset it to 0. Default wire behaviour is unchanged.

.NET client: StreamEventsAsync/StreamEventItemsAsync(after, emitStreamAttached),
MxEventStreamItem.IsStreamAttached, MxGatewaySession.SupportsStreamAttachedAck,
and OpenEventStreamAsync -> MxGatewayEventStream (awaits the sentinel, with
attach timeout; refuses older gateways).

Regenerated Contracts/Generated, the client protoset, Go and Python bindings
(also picks up comment-only drift already on main), synced the Rust vendored
proto and set emit_stream_attached in the Rust StreamEventsRequest literals.
No package versions bumped.

Tests: ordering (sentinel first, then events), race proof (event fanned right
after the sentinel is delivered — mutation-checked: fails when the sentinel is
yielded before registration), resume-with-gap order, refused attach emits no
sentinel, default emits none, contract field numbers, capability, and .NET
client wire tests (await, no-capability, older gateway, timeout).
This commit is contained in:
Joseph Doherty
2026-11-12 07:26:49 -05:00
parent 892bdc1a19
commit afd504d5ae
28 changed files with 2329 additions and 1005 deletions
@@ -135,6 +135,19 @@ public sealed class EventStreamService(
try
{
// Opt-in attach acknowledgement (#227). Emitted FIRST, and only here: by this point
// AttachEventSubscriber / AttachEventSubscriberWithReplay has returned, so this
// subscriber's channel is registered on the session distributor and every event
// the pump fans from now on lands in it (the replay variant also covered the ring
// under the same lock). The client sees the sentinel only after the server reached
// this line, so "sentinel received" implies "registered" — no publish can fall in
// between. Default off so the pre-#227 wire shape is unchanged for existing
// consumers (they treat every streamed worker_sequence as a resume cursor).
if (request.EmitStreamAttached)
{
yield return CreateStreamAttachedSentinel(request.SessionId, afterWorkerSequence);
}
// Emit order for a resume: the ReplayGap sentinel FIRST (only when events were
// evicted), then the still-retained replay batch, then live. The sentinel is an
// explicit documented control signal (not a synthesized MXAccess event) and is
@@ -249,6 +262,16 @@ public sealed class EventStreamService(
}
}
// Builds the opt-in StreamAttached control sentinel (#227): session id plus the live
// watermark, family UNSPECIFIED, no body, worker_sequence 0. Like the ReplayGap sentinel it
// is a documented control signal, not a synthesized MXAccess event.
private static MxEvent CreateStreamAttachedSentinel(string sessionId, ulong liveAfterSequence)
=> new()
{
SessionId = sessionId,
StreamAttached = new StreamAttached { LiveAfterSequence = liveAfterSequence },
};
// Builds the single ReplayGap control sentinel emitted at the head of a resumed
// StreamEvents stream when the requested AfterWorkerSequence predates the oldest event
// still retained (events were evicted). Per the proto contract (MxEvent.replay_gap),
@@ -60,6 +60,7 @@ public sealed class MxAccessGatewayService(
reply.Capabilities.Add("unary-close-session");
reply.Capabilities.Add("unary-invoke");
reply.Capabilities.Add("server-stream-events");
reply.Capabilities.Add(GatewayCapabilities.StreamEventsAttachedAck);
reply.Capabilities.Add("bulk-subscribe-commands");
reply.Capabilities.Add("bulk-read-commands");
reply.Capabilities.Add("bulk-write-commands");