# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-streamable-http-last-event-id-resumption-as-a-replay-hazard-rather-than-a-recovery-guarantee.md # Remnant Warrant: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-streamable-http-last-event-id-resumption-as-a-replay-hazard-rather-than-a-recovery-guarantee.warrant.json # Markdown SHA-256: ec9a0d41ab243375145b17824ad9e8ad77857fcef37673f44b8e0b9c03355100 # Code AST SHA-256: 6a6bc94d2df70f6ef5ca0d07a25e122671899694450035dd0d77ef5f5cd9f41c # Verified Classes: ResumptionReplayHazard, ResumptionState # Defensive Guards: 4 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) import hashlib from dataclasses import dataclass, field from typing import Optional class ResumptionReplayHazard(RuntimeError): """Raised when a stateless MCP client cannot safely resume an SSE stream.""" @dataclass class ResumptionState: session_id: str last_event_id: Optional[int] server_generation: str retention_window_seconds: int pending_calls: dict = field(default_factory=dict) # idempotency_key -> request_id def resume_stream( state: ResumptionState, reconnect_generation: str, seconds_since_last_event: int, replayed_event_ids: list[int], idempotency_supported: bool, ) -> list[int]: """ Decide which replayed events a stateless agent may act on. Returns the subset of event IDs that are safe to treat as fresh observations. """ if reconnect_generation != state.server_generation: # Event-ID counter may have reset; last_event_id is not a valid cursor. raise ResumptionReplayHazard( f"server generation changed {state.server_generation!r} -> " f"{reconnect_generation!r}; last_event_id={state.last_event_id} " f"is not a valid resumption cursor" ) if seconds_since_last_event > state.retention_window_seconds: # Ring buffer almost certainly evicted; empty replay is a gap, not continuity. raise ResumptionReplayHazard( f"reconnect after {seconds_since_last_event}s exceeds retention " f"window {state.retention_window_seconds}s; replay may be silently " f"truncated" ) if not replayed_event_ids and state.last_event_id is not None: # 200 with zero events inside the retention horizon is ambiguous. raise ResumptionReplayHazard( "empty replay inside retention window: cannot distinguish " "'stream is current' from 'events evicted'; re-derive state" ) if not idempotency_supported and state.pending_calls: # Replayed events may correspond to side-effecting calls we already # dispatched; acting on them risks duplicate execution. raise ResumptionReplayHazard( f"{len(state.pending_calls)} side-effecting call(s) in flight and " f"server does not honor idempotency keys; replay is unsafe" ) # Safe path: monotonic IDs, within retention, idempotency enforced. return [eid for eid in replayed_event_ids if eid > (state.last_event_id or -1)] def make_idempotency_key(session_id: str, tool_name: str, args: dict) -> str: material = f"{session_id}:{tool_name}:{sorted(args.items())!r}".encode() return hashlib.sha256(material).hexdigest()