Expand description
The ordering state machine.
TEXT_MESSAGE_CONTENT without a preceding TEXT_MESSAGE_START is a bug
that currently surfaces as a confused frontend, three network hops from
where it was caused. Neither the TypeScript SDK (which verifies on the
client) nor the .NET one (which does not verify) catches it on the server.
This crate does, on by default.
§What it rejects
| Rule | Rejected |
|---|---|
RunEnded | anything after RUN_ERROR except the RUN_STARTED of the next run; anything after RUN_FINISHED except that or one RUN_ERROR |
DuplicateRunStarted | a RUN_STARTED inside a run that has not ended |
DuplicateStart | opening a message, reasoning block, tool call, step or subagent whose id is already open — or a subagent id that already finished in this run |
NotOpen | content or a terminator for something that was never opened, including SUBAGENT_FINISHED / SUBAGENT_ERROR for a subagent that is not active |
UnknownId | TOOL_CALL_RESULT for a call id neither this run nor the history it continues introduced, or a parentSubagentRunId that was never started |
OutOfOrder | TOOL_CALL_RESULT before the call’s TOOL_CALL_END; a first event that is neither RUN_STARTED nor RUN_ERROR |
OpenAtFinish | RUN_FINISHED while a message, reasoning block, tool call, step or subagent is open |
OwnerMismatch | a tagged continuation, terminator or re-open whose subagentRunId is not the one that opened the entity — a message, reasoning block, tool call, activity, or the entity a REASONING_ENCRYPTED_VALUE names; a tool call tagged with one subagent whose parent message belongs to another |
MalformedChunk | a *_CHUNK event that names no id with no chunk stream to continue, opens a tool call without naming the tool, or repeats role, name, toolCallName or parentMessageId with a value other than the one its stream was opened with |
InvalidOutcome | RUN_FINISHED with an empty interrupt list or repeated interrupt IDs |
RUN_ERROR is exempt from OpenAtFinish: a run that blew up mid-message
could not have closed it. When several things are open, the error names
one, and the same stream always names the same one: a message before a
tool call, before a reasoning message, a reasoning block, a step and a
subagent, and of several of a kind the one that was opened first.
§After the run ended
AG-UI 1.0 lets two things follow a run that ended: one RUN_ERROR after
RUN_FINISHED, a failure that surfaced after the run reported success,
and a RUN_STARTED after either terminal event, which begins the next
run on the same stream. A standalone EventVerifier accepts both, so a
recorded stream that carries a late failure or several runs in sequence
verifies. The next run starts with nothing open, and keeps who owns each
id. A hosted run sends neither: its event sink refuses every emit after a
terminal event, with RunEnded, whether or not this verifier is
compiled in. That is the sink’s own rule about what it sends, not the
protocol’s about what a stream may carry.
§Chunks
A *_CHUNK event is judged as the events it stands for: the end of the
chunk stream it closes, the start it implies, its content. So a chunk for
an id that is explicitly open is a second start (DuplicateStart), and
an explicit event for an id a chunk opened finds it closed (NotOpen),
because the chunk stream ended when that event arrived. A hosted run’s
RUN_FINISHED closes whatever chunk stream is still open, as a consumer
reading it does.
§Subagents
Every entity is opened by someone — a subagent, or the parent agent when
the opener carries no subagentRunId — and the verifier remembers who.
A later event that names a different owner is rejected; one that names
none is accepted, because attribution is optional on every event and a
bare continuation is what a pre-subagent producer sends — and it does not
hand the entity to the parent either: the first writer stays the owner, as
upstream records it. Steps are keyed by owner as well as name, so a
subagent cannot close the parent’s step, or a sibling’s, and two agents may
run a step of the same name at once. A MESSAGES_SNAPSHOT seeds ownership
from the messages it carries and is authoritative; the RUN_STARTED input
echo seeds it too, for ids not yet recorded — and in a hosted run so do the
request’s messages, before the first event, whether or not they are
echoed; a TOOL_CALL_RESULT mints the tool message it names under its own
attribution.
What is deliberately not checked, because the protocol does not require
it: that an attributing subagentRunId was announced by SUBAGENT_STARTED
(attribution without lifecycle events is a supported mode), that a
subagent’s own messages are closed before its SUBAGENT_FINISHED, or that
events stop after it — a parentSubagentRunId may even name a subagent
that already finished, since a parent legitimately finishes before its
child. What is required is that every started subagent is closed before
RUN_FINISHED.
§What it lets through
The deprecated THINKING_* family is not tracked at all. State, activity,
raw and custom events are unordered — though an activity delta, like any
continuation, may not name an owner other than its activity’s, and a
state, custom or step event ends the chunk stream of whoever sent it.
§Cost
A handful of maps and one lookup per event. Turning the verify feature
off replaces the whole state machine with a zero-sized type whose
observe is an inlined Ok(()). In debug builds a rejection additionally
carries a dump of everything still open, which is the expensive part and is
why it is debug-only.
§One set of rules
The rules are not written here. They are the crate’s shared ordering
rules, the ones client::verify reads an incoming stream by; this module
names the Rule a rejection broke and words it for
whoever emitted the event.
Structs§
- Event
Verifier - Standalone protocol ordering and attribution checks, without an event queue. Disabling verification does not expose a permissive no-op version of this API. Tracks what is open, and who opened it, so misordered events can be named precisely.