# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-streamable-http-last-event-id-resumption-as-unsafe-when-sse-backpressure-has-already-forced-the-server-to.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-streamable-http-last-event-id-resumption-as-unsafe-when-sse-backpressure-has-already-forced-the-server-to.warrant.json
# Markdown SHA-256:   e19045320d54e07e43be3ac94334ab7d451cec72b4e71fb2e384bcc68f05c0be
# Code AST SHA-256:   d37e2c73cdff71c157efa5e75926612ca734060f8379a16f266a33c0270f523b
# Verified Classes:   StreamResumptionGapError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StreamResumptionGapError(RuntimeError):
    """Raised when a resumed SSE stream cannot prove continuity with Last-Event-ID."""
    pass


def validate_resumption(
    last_event_id: int,
    first_replayed_id: int,
    buffer_floor: int,
    replay_log_is_generation_time: bool,
) -> None:
    if not replay_log_is_generation_time:
        # Drop policy touched the replay ring; the floor is not a bound on client misses.
        raise StreamResumptionGapError(
            f"replay log is not a faithful generation-time record; "
            f"cannot trust floor={buffer_floor} as continuity bound"
        )
    expected = last_event_id + 1
    if first_replayed_id == expected:
        return  # contiguous; safe to treat stream as continuous
    if first_replayed_id > expected:
        raise StreamResumptionGapError(
            f"continuity gap: expected event {expected}, stream resumed at "
            f"{first_replayed_id} (missing {first_replayed_id - expected} events); "
            f"treat as new session and re-issue original request"
        )
    raise StreamResumptionGapError(
        f"non-monotonic resumption: first_replayed_id={first_replayed_id} "
        f"<= last_event_id={last_event_id}; possible ID reuse after server restart"
    )
