# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-an-mcp-streamable-http-session-was-silently-forked-by-a-load-balancer-mid-resumption.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-an-mcp-streamable-http-session-was-silently-forked-by-a-load-balancer-mid-resumption.warrant.json
# Markdown SHA-256:   a1ef6f518d25b91ed5e8f34b8e736d0570644bbe9d8aab78cace0644e34d987f
# Code AST SHA-256:   47b5249d94ac55b62ff7de55125f654f532fb4d9820e251acbfc79b6e08ba622
# Verified Classes:   SessionForkError, ReplayMonotonicityGuard
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class SessionForkError(RuntimeError):
    """Raised when a resumption stream replays an event ID at or below the
    client's last consumed cursor, indicating the backend does not own the
    event log the cursor indexes into (load-balancer session fork)."""
    pass


class ReplayMonotonicityGuard:
    def __init__(self, max_consumed_id: int):
        self.max_consumed_id = max_consumed_id

    def accept(self, event_id: int) -> None:
        # Strictly-greater is the invariant. '>=' would admit the fork where
        # the second backend's log happens to begin exactly at the cursor.
        if event_id <= self.max_consumed_id:
            raise SessionForkError(
                f"replayed event id {event_id} <= last consumed "
                f"{self.max_consumed_id}; backend is replaying from a "
                f"cursor it does not honor (session forked by LB)"
            )
        self.max_consumed_id = event_id


def resume_stream(guard: ReplayMonotonicityGuard, replayed_ids: list[int]) -> None:
    # First replayed event is the discriminator: legitimate resumption from
    # cursor C emits C+1; a forked backend replaying from head emits <= C.
    if not replayed_ids:
        return
    guard.accept(replayed_ids[0])
    for eid in replayed_ids[1:]:
        guard.accept(eid)


# Usage: client consumed up to 102, resubscribes, backend B replays from head.
guard = ReplayMonotonicityGuard(max_consumed_id=102)
resume_stream(guard, [100, 101, 102, 103])  # raises SessionForkError on 100
