# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-its-mcp-session-resumption-state-when-the-http-native-transport-replays-a-last-event-id-across-a-re-established-single-strea.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-its-mcp-session-resumption-state-when-the-http-native-transport-replays-a-last-event-id-across-a-re-established-single-strea.warrant.json
# Markdown SHA-256:   b2c145888d36b258c3a0257ef04a8842918af293e7582a3ba44aa2fb96cb0388
# Code AST SHA-256:   4e87502079a39266a2a56fe49912d6178b5ea3cf9a50e2a4445bb5ff0a3308b5
# Verified Classes:   ResumptionConformanceError, ResumeProbe
# Defensive Guards:   3 fail-closed invariants (zero external deps)

"""Conformance check for MCP streamable-session resumption semantics.

Detects the connection-scoped-buffer, ID-collision, and ghost-session
failure traces described above by testing the *composition* of
Mcp-Session-Id and Last-Event-ID across a forced mid-stream disconnect.
"""

from dataclasses import dataclass


class ResumptionConformanceError(RuntimeError):
    """Raised when a server violates the session-scoped resumption invariant."""


@dataclass
class ResumeProbe:
    session_id: str
    last_event_id: int          # the ID the client last consumed
    first_event_id_on_resume: int
    terminal_event_replayed: bool
    status_code: int


def assert_resumption_sound(probe: ResumeProbe) -> None:
    # Invariant 4: unknown/expired session must be 404, never silently re-created.
    if probe.status_code == 404:
        # Correct: client must re-initialize. Not a resumption failure.
        return
    if probe.status_code != 200:
        raise ResumptionConformanceError(
            f"unexpected status {probe.status_code} on resume for session "
            f"{probe.session_id}"
        )

    # Invariant 1 + 3: resumed stream must continue a strictly greater,
    # session-scoped monotonic ID space. A restart at 1 (Trace A) or a
    # collision (Trace B) both violate this.
    if probe.first_event_id_on_resume <= probe.last_event_id:
        raise ResumptionConformanceError(
            f"non-monotonic resume: last_event_id={probe.last_event_id} "
            f"but resumed at {probe.first_event_id_on_resume} "
            f"(connection-scoped buffer or ID-space collision)"
        )

    # Invariant 5: the terminal event for the in-flight call must be replayable.
    if not probe.terminal_event_replayed:
        raise ResumptionConformanceError(
            f"terminal event missing on resume for session {probe.session_id}; "
            f"client would hang or duplicate side effects"
        )


def run_conformance(probes: list[ResumeProbe]) -> None:
    for probe in probes:
        assert_resumption_sound(probe)


if __name__ == "__main__":
    # Trace A: buffer restarted at 1 after last_event_id=47.
    run_conformance([
        ResumeProbe("sess-A", 47, 1, False, 200),   # raises
    ])
