# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-replays-an-mcp-sse-stream-across-a-resumed-session-and-the-server-has-silently-rotated-its-event-id-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-replays-an-mcp-sse-stream-across-a-resumed-session-and-the-server-has-silently-rotated-its-event-id-epoch.warrant.json
# Markdown SHA-256:   5eb40da0339b300aae0d249d7edcbd4f02361e3c57eebfddcffd34cced6dd6f8
# Code AST SHA-256:   602f0a9efb96b309aaf2a08ed73422e3e685d52d5ca5104dbcb912e37d2fa736
# Verified Classes:   StreamResumptionEpochError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class StreamResumptionEpochError(RuntimeError):
    """Raised when a resumed MCP SSE stream cannot be proven continuous.

    Triggered when the server accepts a Last-Event-ID but the first event
    received after reconnect does not exceed the cursor we sent, indicating
    the server's event-id epoch was rotated underneath us and events were
    silently lost.
    """


def verify_resumption(sent_cursor: int, first_event_id: int, revalidated: bool) -> None:
    # Signal 1: cursor regression proves an epoch rotation / silent gap.
    if first_event_id <= sent_cursor:
        raise StreamResumptionEpochError(
            f"stream resumption not continuous: sent Last-Event-ID={sent_cursor} "
            f"but first replayed id={first_event_id}; server event-id epoch "
            f"rotated, events in ({first_event_id}, {sent_cursor}] are lost. "
            f"Force full re-initialize and discard stream-derived state."
        )
    # Signal 3: forward gaps are undetectable from ids alone, so state that
    # could have been invalidated by unseen events must be re-validated.
    if not revalidated:
        raise StreamResumptionEpochError(
            f"cursor {sent_cursor} -> {first_event_id} is monotonic but the "
            f"gap window is unverifiable; dependent tool schemas and resources "
            f"must be re-fetched before the next tool call."
        )
