Skip to content

The agent that grows with you

fix(tui-gateway): keep replay seq monotonic across ring eviction

Brooklyn Nicholson12c3d5c9686b
diff --git a/tests/tui_gateway/test_tui_gateway_event_replay.py b/tests/tui_gateway/test_tui_gateway_event_replay.py
index 734d4768625..d0c01f7b29b 100644
--- a/tests/tui_gateway/test_tui_gateway_event_replay.py
+++ b/tests/tui_gateway/test_tui_gateway_event_replay.py
@@ -110,6 +110,58 @@ def test_session_count_bounded_with_fifo_eviction():
assert latest_seq(f"s{event_replay._REPLAY_SESSIONS_MAX + 9}") == 1
+def test_fifo_eviction_keeps_seq_monotonic_within_epoch():
+ """#100122: FIFO eviction must not reset a revisited session's seq under
+ the same process-wide replay epoch — clients hold their old watermark, and
+ both the replay response and live parked frames with a reset (lower) seq
+ are silently dropped by the client's dispatchIfNewer gate."""
+ first = _frame("s0")
+ second = _frame("s0")
+ event_replay._stamp_event(first)
+ event_replay._stamp_event(second)
+ assert second["params"]["seq"] == 2
+
+ # Evict s0's ring by pushing _REPLAY_SESSIONS_MAX newer sessions through.
+ for index in range(1, event_replay._REPLAY_SESSIONS_MAX + 1):
+ event_replay._stamp_event(_frame(f"s{index}"))
+
+ assert "s0" not in event_replay._replay_buffers
+
+ revisited = _frame("s0")
+ event_replay._stamp_event(revisited)
+ # Same epoch (no restart happened): the revisited session CONTINUES its
+ # sequence instead of restarting at 1.
+ assert revisited["params"]["seq"] == 3
+
+ # A client holding the pre-eviction watermark still sees the new event…
+ assert [event["seq"] for event in events_since("s0", 2)] == [3]
+ # …while a client that saw only seq 1 is told the ring dropped what it
+ # missed (seq 2 went out live but is no longer replayable): truncated,
+ # refetch history.
+ assert event_replay.is_truncated("s0", 1)
+ # A client that saw seq 2 lost nothing: the ring resumes at 3 with no hole.
+ assert not event_replay.is_truncated("s0", 2)
+ assert not event_replay.is_truncated("s0", 3)
+
+
+def test_fifo_eviction_marks_truncation_for_old_watermarks():
+ """The whole retained ring is gone once a session is FIFO-evicted, so any
+ client watermark below its latest stamped seq reports truncation —
+ without the raise, a revisited session answers truncated=false and the
+ client trusts a tail with a hole in it (#100122)."""
+ frames = [_frame("s0") for _ in range(3)]
+ for f in frames:
+ event_replay._stamp_event(f)
+ assert latest_seq("s0") == 3
+
+ for index in range(1, event_replay._REPLAY_SESSIONS_MAX + 1):
+ event_replay._stamp_event(_frame(f"s{index}"))
+
+ assert event_replay.is_truncated("s0", 0)
+ assert event_replay.is_truncated("s0", 2)
+ assert not event_replay.is_truncated("s0", 3) # saw everything before eviction
+
+
def test_concurrent_stamping_never_drops_or_duplicates_seq():
errors = []
diff --git a/tui_gateway/event_replay.py b/tui_gateway/event_replay.py
index b603a67120b..160a9a399bd 100644
--- a/tui_gateway/event_replay.py
+++ b/tui_gateway/event_replay.py
@@ -7,7 +7,8 @@ event frames; Ink ignores unknown keys); one lock guards counters + buffers, and
serializes per-transport writes so stamping cannot reorder frames; memory bound =
_REPLAY_BUFFER_MAX events AND _REPLAY_BUFFER_BYTES_MAX serialized bytes per session,
_REPLAY_PROCESS_BYTES_MAX bytes across at most _REPLAY_SESSIONS_MAX sessions, oldest evicted
-FIFO. Evicted or never-retained (oversized) frames leave a truncation watermark so a
+FIFO (their seq/truncation counters are retained so a revisited session stays monotonic within
+the epoch, #100122). Evicted or never-retained (oversized) frames leave a truncation watermark so a
reconnecting client refetches instead of trusting a replay with holes.
"""
@@ -72,10 +73,21 @@ def _stamp_event(obj: dict) -> None:
buf = _replay_buffers[sid] = deque()
_replay_buffer_bytes[sid] = 0
while len(_replay_buffers) > _REPLAY_SESSIONS_MAX:
- oldest_sid, oldest_buf = _replay_buffers.popitem(last=False)
+ oldest_sid, _oldest_buf = _replay_buffers.popitem(last=False)
_replay_total_bytes -= _replay_buffer_bytes.pop(oldest_sid, 0)
- _replay_next_seq.pop(oldest_sid, None)
- _replay_evicted_through.pop(oldest_sid, None)
+ # FIFO eviction drops the ring, not the session's seq numbering
+ # (#100122). Keep the counter so a revisited session continues
+ # from its high seq instead of restarting at 1 under clients'
+ # still-held watermarks (a reset seq is invisible to
+ # dispatchIfNewer — replay AND live frames silently vanish).
+ # Retain the truncation watermark too, raised to the latest
+ # stamped seq: the whole retained ring is gone, so a client
+ # holding any older watermark has a gap and must refetch
+ # history instead of trusting the new tail. Both counters are
+ # one int per session id seen this process (bounded by distinct
+ # sessions, not by traffic).
+ _replay_evicted_through[oldest_sid] = max(
+ _replay_evicted_through.get(oldest_sid, 0), _replay_next_seq.get(oldest_sid, 0))
if size > _REPLAY_BUFFER_BYTES_MAX or size > _REPLAY_PROCESS_BYTES_MAX:
_replay_evicted_through[sid] = seq
return