# Under what conditions must an instance treat an MCP `Last-Event-ID` resume as a fresh subscription rather than a gap-fill, and why silent replay loss follows from getting it wrong **Date of Record:** 2026 December 2 **Scope:** Applies to MCP servers implementing HTTP-native transport with stateless streaming (SSE over POST responses or a dedicated GET stream) and resumable session semantics keyed on `Last-Event-ID`. Does not apply to stdio transport, nor to servers that buffer the full event log in durable storage with monotonic, unbounded sequence numbers. ## Core Distinction The failure is not "resume drops events." It is a conflation between two semantically different operations that share one wire primitive: - **Gap-fill resume:** the client has a valid cursor `Last-Event-ID = N`, the server still holds events `N+1..M` in its replay buffer, and the correct response is to emit the missing tail before switching to live mode. - **Fresh subscription:** the client presents `Last-Event-ID = N`, but the server's replay buffer has been evicted, rotated, or was never durable (it lived in the process that just got rescheduled). There is no `N+1..M` to emit. The correct response is to **declare the gap** and re-establish subscription state from scratch. A stateless streaming server cannot distinguish these two cases from the header alone. The header says "I have seen up to N." It does not say "N is still within your retention window." When the server treats an out-of-window cursor as a gap-fill that happens to be empty, it returns a clean `200` SSE stream that begins at the current live head. The client sees a valid stream, updates its cursor to the new head, and **the events between N and the new head are gone with no error on either side.** This is the silent replay loss. The invariant is: *a resume whose cursor precedes the replay floor MUST be surfaced as a gap, never silently satisfied as an empty gap-fill.* ## What Was Observed A tool-calling agent loop over MCP HTTP transport. The server emitted SSE events per tool-call progress tick, each tagged `id: `. Replay buffer: an in-memory ring of the last 1,000 events per session, with session state pinned to a single process behind a load balancer. No sticky routing. Trace: 1. Agent issues `tools/call`, opens the POST-response SSE stream, receives events `id: 4001..4012` (progress ticks), then the connection is severed by an LB idle timeout mid-call. Client has `Last-Event-ID: 4012`. 2. Client reconnects with `Last-Event-ID: 4012`. The LB routes to a **different process instance**. That instance has never seen this session; its replay ring is empty for the session ID. It creates a fresh session record, sees the cursor `4012` is greater than its (empty) floor, and — because the cursor is "ahead" of everything it knows — treats the resume as a no-op gap-fill and immediately attaches the client to the live stream head, which is now at `id: 5001` (the new process's counter starts fresh or is offset by wall-clock). 3. The client observes `id: 5001` next. Its resume logic checks "is 5001 > 4012? yes → contiguous, advance cursor." **Events 4013..5000 never existed from the client's perspective** — they were the completion of the tool call, the final result payload, and the `tools/call` response envelope. The agent, having lost the terminal result, either hangs waiting for a completion event that already fired on the dead process, or — worse — re-issues the tool call, causing a duplicate side effect. The tell in logs: two processes both report "session resumed cleanly, cursor advanced 4012 → 5001." No error, no gap flag, no metric. The loss is only visible by diffing cursor deltas against per-process event counters. A second variant with the same root cause: server does use a shared durable log, but the ring evicts under load. Cursor `4012` is below the floor `4200`. Server clamps to floor, emits from `4200`, client advances. Events `4013..4199` silently dropped. Same silent-loss signature, different eviction mechanism. ## Condition of Validity The "treat resume as gap-fill" behavior is only correct under the conjunction: - The replay store is **durable across process boundaries** (shared log, not per-process ring), AND - The cursor namespace is **globally monotonic** across all processes that can serve the session (not per-process counters), AND - The server can **prove** `Last-Event-ID N` is `≥ floor − 1` for that session at the moment of resume. If any of the three fails, the resume is a *fresh subscription* and must be labeled as such. The most common violation is the second: per-process monotonic counters that reset or diverge on reschedule. Two processes can both emit `id: 5001` for different events. A client cursor is then ambiguous — `5001` matches two distinct events — and no amount of replay buffering fixes it because the identifier is not a total order. The stateless-streaming variant makes this worse: there is no server-side session object to compare the cursor against, so the only floor check available is against whatever transient buffer the current process holds. "No buffer" reads as "nothing to replay," which reads as "gap-fill complete." ## Decision Boundary On receiving a resume with `Last-Event-ID: N`, the server must resolve exactly one of three states before emitting any live event: 1. **In-window:** `N ≥ floor − 1` and `N ≤ head`. Emit `N+1..head` from the buffer, then live. This is a true gap-fill. 2. **Below-floor (evicted):** `N < floor − 1`. The buffer cannot satisfy the gap. **Do not clamp.** Emit an explicit gap marker (e.g., a control event `event: replay-gap` carrying `resume_from: N, floor: `), then either (a) refuse with `409`/`416`-style semantics so the client re-subscribes and re-derives state, or (b) attach to live head **only after** the client acknowledges it has abandoned `N..floor−1`. The client must be told the gap exists. 3. **Ahead-of-head / unknown-namespace:** `N > head`, or `N` cannot be ordered against the current process's counter (per-process counter detected). This is the dangerous case. Treat as **fresh subscription**, not gap-fill. The client's cursor is meaningless here; the server must not silently accept it as "already caught up." The boundary the article is about: **case 3 masquerading as case 1.** A per-process counter makes `N > head` look like "client is ahead, nothing to send," which is indistinguishable on the wire from "client is caught up." The guardrail is a **cursor provenance check**: the server must be able to verify that `N` belongs to the same monotonic namespace it is currently serving. Concretely, embed a namespace/epoch token in the event ID (e.g., `id: :`) so a resume from a stale epoch is *detectably* stale rather than *plausibly current*. If the epoch does not match the current serving epoch, the resume is a fresh subscription by definition. ## Operational Effect Without the epoch token and the explicit gap marker, the system exhibits **silent continuity loss**: the agent's perceived event history is a non-contiguous splice of two process lifetimes, and every downstream reasoning step that depends on "I saw the tool call complete" is now operating on a false premise. The agent does not error; it confabulates a completion or re-fires the call. Duplicate side effects and phantom completions both trace to this single unguarded transition. With the guardrail, the failure becomes loud and bounded: a `replay-gap` on the wire, a client-side decision to re-derive state (re-issue the tool call idempotently, or reconcile against a `tools/status` query), and a metric counting gap events per session. The invariant to enforce in code: **a resume whose cursor cannot be proven to lie within the current replay window of the current epoch must raise, not attach.** Do not let "empty gap-fill" be a valid terminal state for an unverifiable cursor. ```python class ReplayCursorUnverifiable(RuntimeError): """Raised when a resume cursor cannot be proven to belong to the current serving epoch's replay window. Must NOT be swallowed into a silent gap-fill; the client must be told the gap exists.""" pass def resolve_resume(cursor: str, current_epoch: str, floor_seq: int, head_seq: int) -> str: """ cursor format: ":" Returns one of: "gap-fill", "fresh-subscription". Raises ReplayCursorUnverifiable when the cursor is ahead of head or from a different epoch -- i.e. when it *looks* caught up but isn't. """ try: epoch, seq_s = cursor.split(":", 1) seq = int(seq_s) except (ValueError, AttributeError) as exc: raise ReplayCursorUnverifiable(f"malformed Last-Event-ID: {cursor!r}") from exc if epoch != current_epoch: # Different process lifetime / counter namespace. The cursor is not # orderable against our head; treating it as "ahead" would silently # drop every event between the two epochs. raise ReplayCursorUnverifiable( f"cursor epoch {epoch!r} != serving epoch {current_epoch!r}; " f"resume is a fresh subscription, not a gap-fill" ) if seq > head_seq: # Same epoch but ahead of head: impossible under a monotonic counter # unless the client is lying or state was lost. Refuse to attach. raise ReplayCursorUnverifiable( f"cursor seq {seq} > head {head_seq} in epoch {current_epoch!r}" ) if seq < floor_seq - 1: # Evicted below the replay floor: a real gap exists. Caller must # emit an explicit replay-gap marker, not clamp silently. raise ReplayCursorUnverifiable( f"cursor seq {seq} below replay floor {floor_seq - 1}; " f"gap of {floor_seq - 1 - seq} events must be surfaced" ) return "gap-fill" def handle_resume(cursor: str, session: dict) -> dict: try: mode = resolve_resume( cursor, current_epoch=session["epoch"], floor_seq=session["floor"], head_seq=session["head"], ) except ReplayCursorUnverifiable as exc: # Loud, bounded failure. Client re-derives state; nothing silently lost. return {"action": "replay-gap", "reason": str(exc), "resume_from": cursor} return {"action": mode, "replay": session["events"][int(cursor.split(":")[1]) + 1:]} ```