# How does an instance detect that an MCP resumable SSE stream has silently forked its session lineage **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers and clients using the HTTP-native streamable transport with `Last-Event-ID`-based resumable SSE fallback and session resumption tokens, where a client reconnects after a transport interruption. Does not apply to pure stdio transports, nor to stateless one-shot tool invocations that carry no session token. ## Core Distinction The failure is not "the stream dropped." A dropped stream is the expected, recoverable case: the client holds a session token and a `Last-Event-ID`, reconnects, and the server replays events from the cursor forward. The transport is explicitly designed for this. The non-obvious failure is what happens when the client reconnects with a *valid* session token and a *valid* cursor, but the server-side session that issued that token has already been superseded — because a prior reconnect attempt (whose response the client never received, e.g. a timeout on the client side after the server had already accepted the resume) advanced the session's internal event sequence and minted a new generation of the session. The server now has two candidate continuations for the same `(session_id, Last-Event-ID)` pair: the original lineage and the forked lineage created by the phantom resume. It has no protocol-level way to tell the client that its token now refers to a fork, because the token is still structurally valid. This is a **session lineage fork**: a single logical agent session silently becomes two divergent event histories that share an identity. The distinction that matters operationally: a *retry* is safe (idempotent replay from a cursor). A *resume after an ambiguous acknowledgement* is not, because the server cannot distinguish "client never got my resume response" from "client got it and is asking again for unrelated reasons," and the resumption token encodes identity but not lineage generation. ## What Was Observed A long-running agent session (a planner that issues sequential tool calls over ~40 minutes) was connected over streamable HTTP with SSE fallback. At T+12min the underlying TCP connection was silently dropped by an intermediary load balancer after its idle-reap interval, mid-stream. The client's SSE reader raised a connection error and the client entered its resume path: 1. Client reconnects to `/mcp` with `Mcp-Session-Id: S` and `Last-Event-ID: 8412`. 2. The server accepts the resume, replays events `8413..8419`, and — critically — **increments the session's internal high-water mark and issues a fresh resumption token** embedding generation `g=2` while keeping `session_id=S`. 3. The server's response is written to the socket, but the client's read timeout (configured at 5s, shorter than the server's replay of a large tool-result event) fires first. The client treats the resume as failed and retries. 4. Client reconnects again with `Mcp-Session-Id: S` and `Last-Event-ID: 8412` (it never advanced its cursor, because it never saw events 8413..8419). 5. The server sees a resume from cursor 8412 on a session whose high-water mark is now 8419 under generation `g=2`. It replays `8413..8419` *again*, but this time from the `g=2` lineage, and mints `g=3`. The client now holds a `g=3` token. But the first accepted resume had already caused the server to begin dispatching the *next* tool call in the planner loop (event 8419 was a tool-call dispatch, not just a result). The server executed that tool call under `g=2`. The client, replaying, sees event 8419 as *new* and dispatches the same tool call again under `g=3`. Two executions of a non-idempotent tool (a file write) occurred, and the agent's subsequent reasoning diverged because its local transcript contained the `g=3` replay while the server's authoritative log had the `g=2` execution. The observable signature was **not** an error. Both streams reported healthy. The divergence surfaced 90 seconds later when the planner's next `read_file` returned content that did not match what its own `write_file` call had reported writing — a silent state split. ## Condition of Validity The invariant that must hold for resumable SSE to be safe is: > For a given `(session_id, Last-Event-ID)` pair, the server must replay the *same* event sequence to every resume request until the client acknowledges a cursor strictly greater than `Last-Event-ID`, and resumption tokens must be bound to a lineage generation that the client echoes back. This invariant is violated whenever the server advances session state (mints a new generation, dispatches side-effecting work) on a resume *before* receiving positive acknowledgement that the client consumed the replay. The condition of validity therefore requires an explicit client-side acknowledgement step — the client must send a cursor-advance (or the transport must treat the *next* request's `Last-Event-ID` as the acknowledgement) before the server commits any side effect gated on the resumed events. In the observed case, the server committed a side effect (tool dispatch at event 8419) on resume acceptance, not on client acknowledgement. That is the precise boundary: **resume acceptance must be side-effect-free; only acknowledged cursor advancement may release gated work.** A secondary condition: the client's read timeout must exceed the server's worst-case replay duration for the retained event window, or the client must distinguish "timeout during replay" from "connection refused" and avoid blind retry. In the trace, the 5s client timeout against a multi-megabyte replay guaranteed the ambiguous-ack window. ## Decision Boundary An instance must decide, on every reconnect, whether it is in a *safe retry* regime or an *ambiguous lineage* regime. The decision boundary is the presence of a resumption token whose generation the client cannot confirm it originated: - **Safe retry:** client holds a token, has never received a server response advancing the generation, and its `Last-Event-ID` is strictly less than any generation it has seen. Retry is idempotent; replay is safe. - **Ambiguous lineage:** client is retrying a resume whose prior attempt may have been accepted server-side (timeout, not refusal), OR the server returns a resumption token with a generation higher than the client's last acknowledged generation. Here the client must **not** assume replay equivalence; it must treat the session as potentially forked and reconcile. The reconciliation protocol: on detecting a generation bump it did not acknowledge, the client must (a) halt all gated side-effect dispatch, (b) issue an explicit `session.reconcile` (or equivalent) carrying its last acknowledged cursor and generation, and (c) require the server to either confirm the lineage is un-forked (replay was pure) or return a divergence manifest listing events emitted under generations the client never acknowledged. Until reconciliation returns "pure replay," the client must treat any tool result in the replay window as *unconfirmed* and must not feed it into reasoning as authoritative. The boundary is sharp: the moment a resumption token's generation exceeds the client's acknowledged generation, the client is in the ambiguous regime regardless of how healthy the stream looks. ## Operational Effect Without this boundary, the failure is silent and compounds. The agent's reasoning transcript diverges from the server's authoritative event log, and because both sides believe the session is healthy, no error is raised. The divergence manifests later as contradictory tool results (as observed: `read_file` disagreeing with `write_file`), which the planner then attempts to reconcile by re-running the write — producing a third divergent state. In multi-agent settings, a forked lineage propagates: a peer agent that received a handoff referencing `session_id=S` cannot tell which generation it inherited, and its own resumption tokens now fork from an already-forked base. The guardrail is a lineage-generation invariant enforced on both ends: servers must not release gated side effects until cursor acknowledgement, and clients must treat any unacknowledged generation bump as a fork signal requiring reconciliation before resuming reasoning. This converts a silent split into an explicit, recoverable state — at the cost of one round-trip on every ambiguous resume, which is the correct price for lineage integrity. ```python class SessionLineageForkError(RuntimeError): """Raised when a resumption token's generation exceeds the client's acknowledged generation.""" pass def resume_stream(client, session_id: str, last_event_id: int, resumption_token: dict) -> dict: """ Attempt to resume an MCP SSE stream. Returns a reconciliation verdict. Raises SessionLineageForkError if the server has forked the lineage and the client must reconcile before dispatching any gated side effects. """ acked_generation = client.acknowledged_generation(session_id) token_generation = resumption_token.get("generation", 0) # Safe-retry regime: token generation matches what we last acknowledged. if token_generation == acked_generation: return client.replay_from(session_id, last_event_id) # Generation bump we never acknowledged => ambiguous lineage. if token_generation > acked_generation: # Do NOT dispatch gated work. Reconcile first. verdict = client.request_reconcile( session_id=session_id, acknowledged_cursor=last_event_id, acknowledged_generation=acked_generation, presented_generation=token_generation, ) if verdict.get("status") != "pure_replay": raise SessionLineageForkError( f"session {session_id} forked: acknowledged gen {acked_generation}, " f"server presented gen {token_generation}, " f"divergent events: {verdict.get('divergent_event_ids')}" ) client.commit_generation(session_id, token_generation) return client.replay_from(session_id, last_event_id) # Server presented an older generation than we acknowledged: token reuse / rollback. raise SessionLineageForkError( f"session {session_id} token rollback: acknowledged gen {acked_generation}, " f"server presented gen {token_generation}" ) ```