# What happens when an instance resumes an MCP stream with a stale `Last-Event-ID` after server-side replay-window eviction **Date of Record:** 2026 November 21 **Scope:** Applies to MCP clients using HTTP-native transport with Server-Sent Events and `Last-Event-ID`-based stream resumption, where the server maintains a bounded per-session replay buffer. Does not apply to stdio transport or to SSE servers that persist the full event log indefinitely. ## Core Distinction There are two distinct resumption failure classes, and conflating them produces the wrong recovery primitive: 1. **Session-token invalidation** — the `Mcp-Session-Id` is rejected (404/410), meaning the *session identity* is gone. Recovery requires re-initialization: a fresh `initialize` handshake, capability re-negotiation, and loss of all in-flight request correlation. 2. **Replay-window eviction** — the `Mcp-Session-Id` is *still valid*, but the `Last-Event-ID` cursor points to an event ordinal the server has already dropped from its bounded buffer. The server can honor the session but cannot replay the gap. The non-obvious boundary is that case 2 returns a *successful* HTTP 200 with an open SSE stream. The client sees a healthy connection, resumes reading events with monotonically increasing IDs, and has no transport-level signal that events `N+1..M` were silently skipped. The gap is invisible unless the client independently tracks event-ID continuity. This is the failure mode this record addresses: **silent event-sequence discontinuity across a nominally successful resumption.** ## What Was Observed A tool-orchestrating agent issued a long-running `tools/call` (a batch filesystem scan) over streamable HTTP. Mid-call, the client's network path reset; the SSE connection dropped after the server had emitted event `id: 412`. The client reconnected per spec, sending: ``` GET /mcp HTTP/1.1 Mcp-Session-Id: sess_7f3a... Last-Event-ID: 412 Accept: text/event-stream ``` The server's replay buffer held a ring of the most recent 128 events (IDs 390–517 at that moment). By the time the reconnect landed, the buffer had advanced to 620–747 because the server kept producing progress notifications during the disconnect. Event 412 was outside the window. The server responded `200 OK` and began streaming from its buffer head (620), emitting no gap marker. It did *not* emit a `retry:` hint or a synthetic discontinuity event. The client's SSE parser accepted the stream, resumed dispatch at `id: 620`, and — critically — the agent's event handler assumed ordinal continuity to reconstruct partial tool output. Events 413–619 contained the `notifications/progress` deltas and the terminal `result` of the `tools/call`. The agent never received the result; it waited on a request-ID future that would never resolve, until an unrelated timeout fired. The tool call's side effects (files written) had *already committed* server-side. The agent's model, lacking the result, re-planned and re-issued the identical `tools/call`, producing a double-write. The failure was not the disconnect. The failure was that resumption *succeeded at the transport layer while failing at the semantic layer*, and the agent had no invariant asserting that `first_resumed_event_id == last_seen_event_id + 1`. ## Condition of Validity The silent-gap hazard holds only under the conjunction of: - **Bounded replay buffer** on the server (ring buffer, TTL-evicted, or size-capped). Unbounded logs make eviction impossible and the gap cannot occur. - **Progress/notification traffic during disconnect**, so the buffer advances past the client's cursor while the client is offline. A quiescent server that only emits on request would leave the cursor in-window. - **Ordinal-continuity assumption** in the client's event handler — i.e., the client uses `Last-Event-ID` arithmetic to reconstruct state rather than treating each event as self-describing. - **200-on-eviction** server policy. Servers that return `409 Conflict` or a structured gap event on out-of-window resumption convert the silent failure into a loud one and exit this hazard class. If any conjunct is absent, the failure degrades to a clean detectable error or does not occur. ## Decision Boundary The architectural invariant required to bound this hazard: > A resumed stream MUST be treated as *unverified* until the client confirms event-ID continuity, and any tool-call whose terminal event falls in an unconfirmed gap MUST be treated as **indeterminate**, never as failed-and-retryable. Concretely, the client must, on every resumption: 1. Compare the first `id:` of the resumed stream against `last_seen_id + 1`. 2. If they differ, emit a `StreamGapDetected` condition carrying `[last_seen_id, first_resumed_id)`. 3. For every in-flight request whose terminal event lies in that gap, transition its state to `INDETERMINATE`, not `FAILED`. Indeterminate calls MUST NOT be auto-retried; they require an idempotency key or a server-side `tools/call` status query before re-issue. 4. If the server offers no status-query capability, the agent must escalate to a human or a reconciliation step — the side effects may have committed. The boundary between "retry" and "reconcile" is exactly the gap interval. Retrying across an indeterminate gap is the double-write bug. ## Operational Effect Without the continuity invariant, the agent exhibits **phantom-completion divergence**: its internal plan believes a tool call is still pending while the world has already advanced, and its recovery action (re-issue) is non-idempotent. The observable signature is a duplicate side effect with a single logical intent, correlated with a reconnect whose `Last-Event-ID` predates the server's buffer floor. With the invariant enforced, the same reconnect produces a bounded, loud `StreamGapDetected`, the affected call is quarantined as `INDETERMINATE`, and recovery routes through reconciliation rather than blind retry. The transport still "succeeds"; the difference is that success is no longer trusted to imply semantic continuity. The guardrail converts an invisible correctness bug into a visible, bounded state. ```python from dataclasses import dataclass from typing import Optional class StreamGapDetected(RuntimeError): """Raised when a resumed SSE stream skips event IDs, indicating replay-window eviction.""" def __init__(self, last_seen: int, first_resumed: int, session_id: str): self.last_seen = last_seen self.first_resumed = first_resumed self.session_id = session_id self.gap = (last_seen + 1, first_resumed - 1) super().__init__( f"session {session_id}: event gap {self.gap} " f"(last_seen={last_seen}, first_resumed={first_resumed})" ) @dataclass class InFlightCall: request_id: str idempotency_key: Optional[str] state: str = "PENDING" # PENDING -> INDETERMINATE | COMPLETED | FAILED def resume_stream( session_id: str, last_seen_id: int, first_resumed_id: int, in_flight: dict[str, InFlightCall], ) -> None: """Validate event-ID continuity on resumption and quarantine calls in the gap.""" if first_resumed_id != last_seen_id + 1: gap_start, gap_end = last_seen_id + 1, first_resumed_id - 1 for call in in_flight.values(): if call.state == "PENDING": # Terminal event may have been inside the evicted window. call.state = "INDETERMINATE" raise StreamGapDetected(last_seen_id, first_resumed_id, session_id) def maybe_retry(call: InFlightCall) -> str: """Only retry calls that are provably safe; indeterminate calls must reconcile.""" if call.state == "INDETERMINATE": if call.idempotency_key is None: return "RECONCILE" # side effects may have committed; do not re-issue return "RETRY_SAFE" # idempotency key lets server dedupe if call.state == "FAILED": return "RETRY" raise StreamGapDetected(-1, -1, call.request_id) # retry on non-failed state is a bug ```