# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-resumes-an-mcp-sse-session-after-http-native-migration-and-the-oauth-2-1-resource-indicator-no-longer-matches-the-sessio.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-resumes-an-mcp-sse-session-after-http-native-migration-and-the-oauth-2-1-resource-indicator-no-longer-matches-the-sessio.warrant.json
# Markdown SHA-256:   c518dd2b743a06ec0dfc1da00fa6727a68d632d2c53eac9d9d8603890401069f
# Code AST SHA-256:   8b8be9593352fe161b7574fe90d9b85db9841b83a859f8a14bef136e228aede1
# Verified Classes:   AudienceScopeMismatch
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class AudienceScopeMismatch(RuntimeError):
    """Raised when a resumption cursor is presented under a token whose
    RFC 8707 resource indicator does not match the receiving endpoint."""
    pass


def resume_session(
    token_resource: str,
    endpoint_canonical_uri: str,
    last_event_id: int,
    event_log_by_audience: dict,
) -> list:
    # Invariant: a cursor minted under audience A must never resolve under B.
    if token_resource != endpoint_canonical_uri:
        raise AudienceScopeMismatch(
            f"token scoped to {token_resource!r} cannot resume against "
            f"{endpoint_canonical_uri!r}; cursor {last_event_id} is unaddressable "
            f"under this audience — force re-auth bound to the new resource"
        )

    audience_log = event_log_by_audience.get(token_resource, [])
    # Cursor must exist within the audience-partitioned log, not a global one.
    if last_event_id not in audience_log:
        raise AudienceScopeMismatch(
            f"cursor {last_event_id} not present in event log for audience "
            f"{token_resource!r}; refusing replay"
        )

    return audience_log[audience_log.index(last_event_id) + 1:]
