Skip to content

Stream wire (SSE frames, contract v2) ​

The SSE frame layer of the stream protocol (which documents the chunks these frames carry). Every Helix SSE surface that carries a run's stream — the CF Durable Object's /sse, the StreamServer DO's /sse, agent-server's /sse (the remote-agent protocol), core's ResumableStreamHandler — speaks one protocol. Core owns it (core/src/transport/stream-frame.ts): every writer encodes through encodeStreamFrame (or streamFrameToSSE, its unserialized fields) and every reader decodes through decodeStreamFrame and classifies the stream's end with classifyStreamTerminal. Introduced by WE MR C1; the user's rulings a–c (2026-10-09) and D-4 (2026-10-11).

No back-compat: deploy writers and readers together. A v1 reader misreads a v2 stream and a v2 reader reads a v1 failure as a drop (see the upgrade guide).

Frames ​

EventDataKindMeaning
chunk{ sequence, chunk }, SSE id: = the sequencecontentOne stream chunk. The chunk payload is passed through opaquely (only the envelope is checked).
end{ status, output?, state?, usage?, omitted? }terminalThe stream ended. status: completed, paused (suspended) or interrupted (stopped).
fail{ error, errorDetail?, recoverable }terminalThe stream failed. recoverable: true means re-attach from the cursor.
truncated{ floor, errorDetail } (code TRUNCATED)terminalThe cursor is below the retention floor: refetch the snapshot and resume from its sequence.
rewound{ atStep, atSequence?, minSequence?, scope? }markerA cleanupToStep removed chunks of steps after atStep: live while attached, or first on a re-open past a removed chunk.
caught-up{ head }markerThe replay reached head; live frames follow. The backlog boundary (catchUpHead).
evicted{ resumeFrom, reason }connection endThe writer evicted this connection (WE C2b); EOF follows. Not a stream terminal: the stream goes on.

Comments (: heartbeat, the DO's : waiting park marker) are not frames. An event this version does not know is unknown (ignored) whatever its data; a known event whose JSON or envelope does not validate is malformed (each reader turns it into a retryable typed error, never a skip and never an end). The decoder reads LF and CRLF lines, and flush() delivers a last frame that has no closing blank line.

An end's omitted parts (RM-90). end.omitted (optional, a non-empty list of output / state / usage) names the parts of the run's terminal payload its stream manager did not store because each was over the manager's limits.maxChunkBytes. An omitted part is absent from the frame although the run produced it. Never read an absent output as null when omitted names it: recover it from the run's own record (the output from /status, the state and usage from /snapshot and /usage). Absent means nothing was omitted. The field is additive: a reader that does not know it sees an end without those parts, as before. The stock AI SDK chat transport reads no end payload.

The v1 names are gone: error decodes to unknown; fail is always a failure (the v1 fail + code: 'TRUNCATED' overload is the truncated frame); the v1 non-terminal truncated cleanup marker is rewound; the DO's : connected comment is the caught-up frame.

The terminal state machine ​

Each connection ends with exactly one terminal frame, then the body closes. A reader maps the last frame — or the body's end — through classifyStreamTerminal(terminal | 'eof'):

Reader sawActionWhat the reader does
enddoneEnds (a non-completed status is not success: a remote dispatch recovers through /status).
failfailSurfaces the failure with its errorDetail (a stream reader: one error chunk, then done).
truncatedtruncatedThrows StreamTruncatedError.
EOF with no terminal framedropThrows the typed, retryable StreamDropError — never a clean end (ruling a).

A reader whose own caller aborted the read (CP-102: signal, close(), the chat transport's stop) ends quietly instead: an abort is not a drop. A close by the server with no terminal frame is an eviction (since C2b announced by an evicted frame, below; a writer older than C2b closes bare) or a crash, so every reader treats it as a drop and re-attaches.

Evictions (WE C2b) ​

Both stream DOs evict a connection (core SSEConnectionManager) and say why: the connection's last frame is evicted{resumeFrom, reason}, then EOF. The eviction aborts the connection's writer, so every frame still queued for it is dropped and every pending write is released at once (CP-26); the abort carries the evicted frame, which core createSSEStream delivers as the client's NEXT read, whenever it reads again, instead of a body error.

reasonWho evictsRe-open counts toward MAX_STREAM_REOPENS
slow_consumerthe connection passed its unread-byte budget (or the pending-write cap)no (the reader's own pace); progress-gated
stalenothing read within the inactivity bound (the heartbeat)no; progress-gated
max_connectionsthe agent DO is at its connection cap (it never registered the connection)yes
server_errorthe route failed to serve the connectionyes

resumeFrom is the highest chunk sequence the writer knows the client READ (readThrough: resolved broadcasts and the route's replay, never the snapshot head it registered at). Readers re-open from their own cursor; every chunk frame the client read precedes the evicted frame, so a cursor below resumeFrom means a frame was lost on the way and the read ends StreamTruncatedError (re-anchor), never a re-open over a gap. A second eviction with no new chunk since the last re-open, or a counted re-open past the cap, ends the read with StreamEvictedError (transport_stream_evicted, retryable, cause = the reason). After a re-open, a chunk of a newer run of the stream's own session ends it with StreamRunChangedError (transport_stream_run_changed, retryable: re-anchor from the snapshot). Both are StreamDropErrors, so a reader that only knows drops (the remote-agent transports) keeps its recovery. A reader older than C2b decodes evicted as unknown and reads the EOF as a drop: any deploy order works.

Writers guarantee the terminal on every exit: the DO writes end with the persisted end status (a stopped run's interrupted included), end{paused} or fail for every non-active status; agent-server writes a recoverable transport_stream_drop fail when its source ends without one; ResumableStreamHandler writes the stream's own outcome from getStreamInfo (a reader that ended while the stream is still active is a drop).

The retention floor (decision 1) ​

truncated{floor} is answered only while a cleanupToStep marker (truncated_at_step) is set — a cleanup removed chunks inside the current run — and the cursor is below the floor (the highest sequence that is not replayable: MIN(sequence) - 1, or the counter when the log is empty). resetStream and reactivateStream clear the marker: a reset is a deliberate new run whose old chunks no reader expects, so a reader from 0 is served the next run, as the in-DO reader and the StreamManager contract do. The cross-service guarantee holds: a reconnect never silently replays only a cleanup's survivors. The DO and the StreamServer DO apply the same rule; StreamServer's /info reports the floor (oldestRetainedSequence).

rewound while attached, and on a re-open ​

cleanupToStep broadcasts rewound{atStep, atSequence: head, minSequence?} on every cleanup (a no-op one included). A reader prunes the buffered chunks the cleanup removed (step after atStep, sequence above minSequence); once its buffer is drained, a reader still behind atSequence on a live stream cannot make progress and throws StreamTruncatedError (in-DO reader parity, contract G4.truncation-throws). A terminal frame pre-empts it. A connection that already closed gets no rewound, so a push-SSE reader of an ended stream delivers the chunks it was pushed and then ends (FU-G4-BINDING-AFTER-ENDSTREAM-RETURNS-DONE).

What a cleanup removes. One core rule (isRemovedByCleanup), applied by every manager and by every reader's prune. Without a scope, every chunk of a step above atStep past minSequence. With one ({ sessionId, committedThrough, openFrom? }), the chunk must also be that session's own (its agentId, not forwarded from a remote child) and uncommitted: past committedThrough, or at / after openFrom, the session's first step_start of a step above atStep. A stream is shared, and a child's chunks carry the child's own step numbers, so a step number alone says nothing about them. A manager declares supportsCleanupScope; one that does not ignores the scope.

A reader judges a rewound only when its connection's replay is complete. A re-open, and a parked reader (: waiting) once live, are written the rewound before the replay: a reader holds it until caught-up, so a chunk that survived the cleanup and is still on the way is read, and one whose unread chunks were removed throws at caught-up.

A reader that was away re-opens to the same frame (CP-200). A stop discards the interrupted step's chunks this way, so a reader that was not attached when it ran (evicted for reading slowly, or its connection dropped) would re-open from a cursor past a removed chunk and skip to what is left with no signal, still holding the chunks it read. Both stream DOs (the CF DO's DOStreamManager and store-cloudflare's StreamDurableObject) keep the latest cleanup that removed chunks (its { atStep, atSequence, minSequence? } frame and a baseline, the oldest retained sequence just before the cleanup's delete; cleared with the marker), and an /sse open writes that rewound frame FIRST, before its replay, when the reader held a removed chunk (core rewoundForReopen): its cursor is below atSequence and above the floor (the higher of minSequence, 0 without one, and baseline, so a reset's deletions are never read as the cleanup's), and fewer chunks are stored in (floor, cursor] than that range spans. The reader then acts on it as an attached one does, pruning only chunks at or below atSequence (a chunk written after the cleanup survives). A reader that held nothing the cleanup removed, one at or past its head, or one after a reset gets no frame: it reads a consistent log.

The remote protocol ​

A remote transport (HttpRemoteAgentTransport, the CF DO's DOStubTransport) decodes a /sse body with core decodeTransportEvents into TransportEvents — chunk, end, fail, truncated — and throws StreamDropError for a body with no terminal frame. Every runtime's remote dispatch maps a terminal event through core remoteTerminalOutcome (completed / stopped / retry / failed), so a remote failure keeps its errorDetail; a transport drop falls through to the dispatch's post-drop /status recovery. A completed end whose omitted names output carries it through remoteTerminalOutcome to core processRemoteSubAgentCompletion, which reads the output from /status (a completed status with an output); a failed read, or a status without one, fails the call typed (reason: 'output-unavailable'), never a null output. An omitted state / usage takes the existing /snapshot / /usage fallback. Entries (/start, /resume) are sent exactly once (CP-95): see the remote agents guide.

The chat reply (D-4) ​

@helix-agents/ai-sdk's transformer ends a reply that forwarded a run error with { type: 'finish', finishReason: 'error' } after the typed error chunk; the chat transport reads a fail frame that way, and errors its UI stream with StreamDropError when the body ends with no terminal frame.

Step boundaries (CP-340). A chat answer on which nothing continues (a submit whose batch still awaits another call, an answer already consumed, a re-anchor) is start → start-step → data-helix-step-ended { runId } → its rejections and triggers → finish-step → finish. data-helix-step-ended is non-transient (it stays in the message's parts, so the boundary reaches the chat's state) and is a framework part: apps may ignore it or filter it from rendering. The transformer also emits start-step at the first root step_start after an answered client-executed call's output, even with step events off, and the snapshot converter puts a step-start before the next assistant row by the same rule, so ai's stock auto-send predicates, which read only the last step, never read an answered call twice.

Where it is tested ​

  • Core: core/src/__tests__/transport/stream-frame-codec.test.ts (round-trips, no back-compat), stream-terminal.test.ts, sse-frame-decoder.test.ts (flush, CRLF).
  • The StreamManager contract (core/src/testing/stream-manager-contract.ts): W.eof-is-a-drop (SSE readers, via the cutConnection hook), the G4 and RF cases on every manager.
  • Writers: runtime-cloudflare/.../integration/do-sse-terminals.integ.test.ts, store-cloudflare/.../integration/stream-server-real.integ.test.ts (wire v2 block), agent-server/src/__tests__/agent-server-wire-v2.test.ts, core/src/__tests__/stream/endpoint.test.ts.

Released under the MIT License.