# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-resumable-mcp-stream-sessions-from-silently-replaying-tool-side-effects-after-a-backpressure-induced-disconnect.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-resumable-mcp-stream-sessions-from-silently-replaying-tool-side-effects-after-a-backpressure-induced-disconnect.warrant.json
# Markdown SHA-256:   f057db339cb7a9d427245c87786873c9ccd8ee3cf0de7db81b3a050b844189ba
# Code AST SHA-256:   7fc3b790f7ff506c8b8f5049ede26689a95b87fded36c70b8385ef549912d27f
# Verified Classes:   ResumptionEffectMismatch, StreamFrame
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ResumptionEffectMismatch(RuntimeError):
    """Raised when a resumable MCP stream frame claims an effect that is not
    yet committed, or when a resume references an effect outside the retained
    window. This is the guardrail against silent double-execution."""


class StreamFrame:
    def __init__(self, invocation_id, effect_seq, committed_effect_seq,
                 is_terminal, idempotency_token):
        self.invocation_id = invocation_id
        self.effect_seq = effect_seq
        self.committed_effect_seq = committed_effect_seq
        self.is_terminal = is_terminal
        self.idempotency_token = idempotency_token


def emit_frame(frame: StreamFrame, retained_effect_floor: int) -> str:
    # Invariant 1: a frame may not describe an effect that is not committed.
    if frame.effect_seq > frame.committed_effect_seq:
        raise ResumptionEffectMismatch(
            f"frame for invocation {frame.invocation_id} declares effect "
            f"{frame.effect_seq} but only {frame.committed_effect_seq} committed"
        )
    # Invariant 2: the resume window must not outlive the effect log.
    if frame.effect_seq < retained_effect_floor:
        raise ResumptionEffectMismatch(
            f"frame effect {frame.effect_seq} predates retained floor "
            f"{retained_effect_floor}; resume would reference a pruned effect"
        )
    # Invariant 3: terminal frames must carry an idempotency token so the
    # client can suppress re-invocation on resume.
    if frame.is_terminal and not frame.idempotency_token:
        raise ResumptionEffectMismatch(
            f"terminal frame for {frame.invocation_id} lacks idempotency token"
        )
    # Effect-anchored event ID: derived from commit sequence, not frame sequence.
    return f"id: {frame.committed_effect_seq}\n"


def resume(last_event_id: int, retained_effect_floor: int,
           committed_effect_seq: int) -> int:
    if last_event_id < retained_effect_floor:
        # Force the client to reconcile via the effect log by token.
        raise ResumptionEffectMismatch(
            f"resume at {last_event_id} below retained floor "
            f"{retained_effect_floor}; client must query effect log"
        )
    if last_event_id > committed_effect_seq:
        raise ResumptionEffectMismatch(
            f"resume at {last_event_id} ahead of committed effect "
            f"{committed_effect_seq}; stream state is inconsistent"
        )
    return last_event_id
