fix(sessions): reject a StreamEvents resume cursor ahead of the stream with InvalidArgument (SES-006, #256)

This commit is contained in:
Joseph Doherty
2026-12-03 09:48:27 -05:00
parent 9756ab843e
commit 09da8063a9
6 changed files with 108 additions and 0 deletions
+2
View File
@@ -297,6 +297,8 @@ Emit order on a resumed stream:
When `after_worker_sequence` is inside the retained window (nothing was evicted), step 1 is skipped: the stream replays the retained tail then resumes live with no sentinel.
**A cursor ahead of the stream is rejected.** If `after_worker_sequence` is greater than every `worker_sequence` the session has produced (a cursor carried over from an earlier session, a corrupted one, or `2^64 - 1` from an unsigned `0 - 1`), `StreamEvents` fails the attach with gRPC `InvalidArgument` and registers nothing. Accepting it would attach a stream whose live filter drops every event until the session catches up to the bogus value, with nothing replayed and no gap signalled — a silently dead stream. The cursor equal to the newest sequence is valid (fully caught up). A client that gets this error should discard the cursor and attach fresh (`after_worker_sequence = 0`) after re-snapshotting.
**`oldest_available_sequence` when the ring is empty.** Age eviction (`ReplayRetentionSeconds`, default 300) and a disabled ring (`ReplayBufferCapacity = 0`) both leave nothing retained, so there is no oldest-retained sequence to report. In that case the sentinel carries the **next sequence that can possibly be delivered** — the highest sequence the distributor has observed plus one — rather than `0`. That keeps `after_worker_sequence = oldest_available_sequence - 1` the single universal resume formula: the follow-up resume lands exactly on the highest observed sequence, replays nothing, reports no gap, and receives every subsequent live event. Reporting `0` here would make an unsigned client compute `2^64 - 1` and then silently receive nothing, because the live filter drops every event at or below that watermark. Nothing is lost relative to reporting `0` — the evicted interval is unrecoverable either way, and the sentinel's job is to tell the client to re-snapshot. `0` remains the value when there is no gap, where the field is meaningless and never emitted.
The ReplayGap sentinel is emitted **only** on the `StreamEvents` server stream and only to the resuming subscriber — it is never fanned to other subscribers and never appears in `DrainEventsReply` (the diagnostic drain path is untouched). Replay retention itself is bounded by `MxGateway:Events:ReplayBufferCapacity` (count), `ReplayRetentionSeconds` (age) and `ReplayBufferMaxBytes` (summed serialized size, so a burst of large-array events cannot pin memory the count cap would allow); whichever binds first evicts oldest-first. Evicted slots are cleared at once, so an evicted event is released immediately rather than when its slot is next reused. See [Configuration](GatewayConfiguration.md).