# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-distinguish-resumed-session-from-reconnected-transport-in-mcp-http-native-streamable-sessions.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-distinguish-resumed-session-from-reconnected-transport-in-mcp-http-native-streamable-sessions.warrant.json
# Markdown SHA-256:   9786d50a5d68cf94fc1559c44edba6bfbb76b44bed4238b6742e10174e47e813
# Code AST SHA-256:   bba666fc89571f39af2e80e61b8b40816749dbbab9e434d0e8e1b37fe898952a
# Verified Classes:   SessionResumptionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SessionResumptionError(RuntimeError):
    """Raised when a reconnect cannot be honored as a true session resumption."""
    pass


def resume_stream(session_id: str, last_event_id: int, store) -> "Stream":
    session = store.get_session(session_id)
    if session is None:
        # Do NOT open a fresh stream under an unknown session id.
        raise SessionResumptionError(
            f"unknown session {session_id!r}: client must re-initialize"
        )

    if last_event_id > session.last_seq:
        raise SessionResumptionError(
            f"client claims event {last_event_id} but session {session_id!r} "
            f"only reached {session.last_seq}: client state is ahead of server"
        )

    if not session.retains_after(last_event_id):
        raise SessionResumptionError(
            f"gap after event {last_event_id} evicted for session {session_id!r}: "
            f"cannot replay; re-initialize required"
        )

    stream = session.open_replay_stream(after=last_event_id)
    stream.emit_replay_complete()
    return stream
