# Why does an instance lose tool-call durability when SSE backpressure outruns its resumable session cursor **Date of Record:** 2026 November 24 **Scope:** Applies to agents that stream reasoning and tool-call deltas to a client over HTTP Server-Sent Events (SSE) or chunked transfer, where tool calls are dispatched to side-effecting backends and the session is advertised as resumable via a cursor (e.g., `Last-Event-ID`). Does not apply to fully buffered request/response agents, nor to agents whose tool calls are pure/idempotent reads. ## Core Distinction There are two independent notions of "progress" in a streaming agent, and conflating them is the entire failure. The **transport cursor** advances when a frame is written to the socket (or acknowledged by the client). The **durable session cursor** advances when a tool-call effect is committed to the session log and made replayable. Under healthy conditions these advance together. The boundary condition is when client-side backpressure (a slow or paused SSE consumer, a full TCP window, a proxy that stops draining) stalls the transport cursor while the agent's reasoning loop keeps producing tool-call intents. The non-obvious part: the agent does not stop. The reasoning loop and the tool executor are typically decoupled from the socket writer by an async queue. When the writer blocks, the queue absorbs frames, the executor keeps firing side-effecting tool calls, and the durable session cursor *does* advance — but the frames announcing those calls are still stuck in the unsent buffer. A resume from the last *delivered* `Last-Event-ID` therefore replays a window in which the agent has already committed effects it cannot prove it committed, or re-issues tool calls whose effects exist but whose announcement never left the process. ## What Was Observed A file-mutation agent streamed `tool_call` deltas for `write_file` and `apply_patch` operations. The client paused reading for ~40 seconds (a UI tab throttled by the browser). During the pause: 1. The SSE writer blocked on `await response.write(frame)` after the client's receive buffer filled. 2. The agent's planner, unaware of the writer state, emitted 6 more `tool_call` frames into an unbounded `asyncio.Queue`. 3. The executor drained that queue and committed 6 file mutations to the workspace. The durable session log recorded `committed` entries with sequence numbers 41–46. 4. The client reconnected with `Last-Event-ID: 40`. The server resumed the stream from the *durable* log at 41, re-emitting the tool-call frames. 5. The agent, treating a resumed stream as an instruction to reconcile, re-executed the tool calls at 41–46. `apply_patch` is not idempotent against an already-patched file; it failed on 4 of 6 with "context not found," and on 2 it applied a second time, producing duplicated blocks. The observable signature is a **resume divergence**: the set of effects committed before the disconnect is not equal to the set of effects the resumed client believes were committed. The gap is exactly the frames that were queued but never delivered, and it is invisible to both sides because each holds a consistent but different view of "what happened up to sequence N." ## Condition of Validity The invariant that must hold for resumability to be sound is: > For every tool-call frame with sequence number `s`, the effect of that call is committed to durable state **if and only if** frame `s` has been delivered to the client (or the client has explicitly acknowledged `s`). This invariant holds only when the executor's commit is *gated* on transport delivery of the corresponding frame. It is violated whenever the executor commits ahead of the writer. The violation is bounded by the queue depth: the maximum divergence equals the number of frames enqueued but not yet flushed at the moment of disconnect. An unbounded queue makes the divergence unbounded; a bounded queue makes it finite but still non-zero unless the bound is zero (fully synchronous commit-on-delivery). The condition of validity is therefore not "use a resumable cursor." It is "the cursor must be a *delivery* cursor, and the executor must not run ahead of it." A cursor derived from the durable log alone is a *commit* cursor, and a commit cursor is not resumable in the transport sense — it will over-replay. ## Decision Boundary The decision is where to place the commit gate relative to the write. Three placements, with distinct failure profiles: - **Commit-on-write (executor gated on `write()` returning):** Divergence bounded to one frame. `write()` returning means the frame entered the kernel buffer, not that the client received it — so a hard disconnect can still lose the last frame. Safe only if the client's resume logic treats a re-delivered frame as idempotent (dedupe on sequence number) and the tool is idempotent or carries an idempotency key. - **Commit-on-ack (client sends periodic `ack` events, executor gated on ack):** Divergence zero for acknowledged frames, but introduces a new stall: a client that never acks blocks the executor forever. Requires an ack timeout that, on expiry, *aborts* rather than *commits*. The abort must roll back uncommitted intents, not leave them pending. - **Commit-ahead (current failure):** Divergence equals queue depth. Unbounded queue → unbounded replay. This is the default when the writer and executor are decoupled by an async queue with no backpressure signal, which is the common case because decoupling is the obvious way to keep the planner responsive. The boundary is crossed the moment the executor's commit is not causally downstream of the writer's flush. Once crossed, no amount of correct resume logic on the client can recover, because the client's cursor is a *delivery* cursor and the server's log is a *commit* cursor, and the two have silently diverged. ## Operational Effect The practical consequence is that a resumable agent session must carry **two** sequence spaces, not one: a delivery sequence (what the client has seen) and a commit sequence (what the workspace reflects), with an explicit reconciliation record mapping between them. On resume, the server must replay from the *delivery* cursor and must *not* re-execute any tool call whose commit sequence is already past the delivery cursor — instead it emits a `replayed_commit` frame describing the effect that already happened, so the client's view converges without re-firing the side effect. Without this, the agent exhibits the classic symptom: resume "works" (the stream reconnects, frames flow) but the workspace is corrupted by double-application, and the corruption is non-deterministic because it depends on exactly how many frames were in flight when the client paused. The bug is invisible in tests that never pause the consumer, which is most tests. The guardrail is a single rule: **the executor must not commit a tool call whose announcing frame has not been flushed, and the resume path must treat the delivery cursor as authoritative for replay while treating the commit cursor as authoritative for effect-existence.** Any agent that lets the two cursors drift, or that derives resume position from the commit log, will double-apply under backpressure. ```python class ResumeDivergenceError(RuntimeError): """Raised when the delivery cursor and commit cursor disagree on resume.""" def reconcile_resume(delivery_cursor: int, commit_log: list[dict]) -> list[dict]: """ Given the client's Last-Event-ID (delivery cursor) and the durable commit log, decide what to replay. A frame with seq <= delivery_cursor was already seen by the client. A commit with seq <= delivery_cursor but whose frame was NOT delivered is a divergence: the effect exists but the client never learned of it. """ replay = [] for entry in commit_log: seq = entry["seq"] if seq <= delivery_cursor: if not entry.get("delivered", False): # Effect committed ahead of delivery: do NOT re-execute. # Emit a reconciliation frame instead of a tool_call. replay.append({"type": "replayed_commit", "seq": seq, "effect": entry["effect"]}) continue # seq > delivery_cursor: not yet seen. Re-issue only if uncommitted. if entry.get("committed", False): raise ResumeDivergenceError( f"seq {seq} committed but beyond delivery cursor " f"{delivery_cursor}; commit ran ahead of transport flush. " f"Refusing to replay a side effect that already occurred." ) replay.append({"type": "tool_call", "seq": seq, "call": entry["call"]}) return replay def flush_gated_commit(frame_seq: int, write_result: bool, commit_fn) -> None: """ The commit gate: a tool call may only be committed after its announcing frame has been flushed to the transport. """ if not write_result: raise ResumeDivergenceError( f"frame {frame_seq} not flushed; refusing to commit. " f"Executor would run ahead of the delivery cursor." ) commit_fn() ```