# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-tool-call-state-exactly-once-after-an-sse-to-streamable-http-migration.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-tool-call-state-exactly-once-after-an-sse-to-streamable-http-migration.warrant.json
# Markdown SHA-256:   180e19993ae73e85684ee1a8c3e6ae3818415fa74311085b3b580555aaaddedc
# Code AST SHA-256:   106087499d055b25a96a7a613756c0b383e6650b210f12da74e1566e3c95c630
# Verified Classes:   ResumeCursorRaceError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ResumeCursorRaceError(RuntimeError):
    """Raised when a streamable-HTTP resume cursor points at an event the
    client has not drained, causing silent loss of exactly one event."""
    pass


def resume_stream(session, last_event_id, transport_of_cursor, transport_of_session):
    if transport_of_cursor != transport_of_session:
        # Cross-transport cursor: the event-ID namespaces are disjoint by
        # construction, so this cursor cannot refer to a consumable event.
        raise ResumeCursorRaceError(
            f"cursor {last_event_id!r} minted by {transport_of_cursor!r} "
            f"cannot resume {transport_of_session!r} session {session.id!r}; "
            f"force re-initialization instead of replay"
        )

    last_sent = session.last_sent_event_id
    last_acked = session.last_acked_event_id

    if last_event_id == last_sent and last_event_id != last_acked:
        # The client is resuming from an event the server sent but never saw
        # acknowledged. Replaying with "> cursor" would skip it.
        raise ResumeCursorRaceError(
            f"resume cursor {last_event_id!r} equals last-sent-but-unacked "
            f"event for session {session.id!r}; at-most-once replay would "
            f"drop this event. Use '>= cursor' with client dedup, or "
            f"re-initialize."
        )

    cursor = session.event_index(last_event_id)
    if cursor is None:
        raise ResumeCursorRaceError(
            f"cursor {last_event_id!r} not found in session {session.id!r} "
            f"event log; refusing to replay from an unknown position"
        )

    return session.replay_from(cursor, inclusive=True)
