# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-an-mcp-http-native-streaming-session-has-silently-forked-across-an-sse-to-http-2-fallback-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-an-mcp-http-native-streaming-session-has-silently-forked-across-an-sse-to-http-2-fallback-boundary.warrant.json
# Markdown SHA-256:   fc1b3a1b12a8de74a4ab8929ae503c2bb791b7c6b7b21a76314d5af5fab1da09
# Code AST SHA-256:   d5c72909f479da3f42c8cb3490c890495dcfad4402d6adac6e32b4ecdbe1a625
# Verified Classes:   SessionForkError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class SessionForkError(RuntimeError):
    """Raised when an MCP session's transport binding has forked:
    more than one live writer holds the same Mcp-Session-Id, or a
    resumption cannot prove contiguity of the client's applied events."""


def resume_session(
    session_id: str,
    last_event_id: int | None,
    live_bindings: dict[str, int],
    replay_buffer_max_id: int,
    negotiated_protocol_version: str,
    required_protocol_version: str,
) -> int:
    # Invariant 1: exactly one live transport binding per session.
    if live_bindings.get(session_id, 0) > 1:
        raise SessionForkError(
            f"session {session_id} has {live_bindings[session_id]} live "
            f"bindings; refusing to attach a third writer"
        )

    # Invariant 2: resumption must name its resume point.
    if last_event_id is None:
        raise SessionForkError(
            f"session {session_id} resumed without Last-Event-ID; "
            f"applied-event set is unprovable, restart required"
        )

    # Invariant 3: resume point must be within the replayable window.
    if last_event_id > replay_buffer_max_id:
        raise SessionForkError(
            f"session {session_id} resume point {last_event_id} exceeds "
            f"replayable max {replay_buffer_max_id}; events lost"
        )

    # Invariant 4: transport-negotiated capabilities survive the fallback.
    if negotiated_protocol_version != required_protocol_version:
        raise SessionForkError(
            f"session {session_id} negotiated {negotiated_protocol_version} "
            f"but fallback requires {required_protocol_version}; "
            f"re-initialize before resuming"
        )

    return last_event_id + 1


def handle_fallback(
    session_id: str,
    last_event_id: int | None,
    live_bindings: dict[str, int],
    replay_buffer_max_id: int,
    negotiated_protocol_version: str,
    required_protocol_version: str,
) -> int:
    try:
        return resume_session(
            session_id,
            last_event_id,
            live_bindings,
            replay_buffer_max_id,
            negotiated_protocol_version,
            required_protocol_version,
        )
    except SessionForkError:
        # Fork detected: caller must discard session state and restart.
        raise
