Expand description
Client-side protocol verification.
The TypeScript SDK puts its verifier on the client, and that is the right instinct for a consumer: the events arrive from someone else’s process, and a stream that breaks the rules should produce one clear error rather than a confused UI. This module is that check, as an ordering state machine.
crate::client::Thread runs it by default. Turn it off with
ThreadBuilder::verify when talking to a
producer whose quirks you have decided to live with.
use ag_ui::client::verify::Verifier;
use ag_ui::Event;
let mut verifier = Verifier::new();
verifier.verify(&Event::run_started("thread-1", "run-1"))?;
// Content for a message that was never opened.
let orphan = Event::text_message_content("msg-1", "Hello");
assert!(verifier.verify(&orphan).is_err());§The rules
RUN_STARTEDopens the stream — orRUN_ERRORdoes, for a run that failed before it began. Nothing else may come first,RAWandCUSTOMincluded.RUN_FINISHEDandRUN_ERRORend the run. Two things may follow: oneRUN_ERRORafterRUN_FINISHED, a failure that surfaced after the run reported success, which fails the run; and after either, aRUN_STARTED, which begins the next run on the same stream. ARUN_STARTEDinside a run that has not ended is rejected.TEXT_MESSAGE_CONTENTandTEXT_MESSAGE_ENDrequire an open message with the same id, andTEXT_MESSAGE_STARTmay not re-open an id that is already open.- The same, for
TOOL_CALL_*, forREASONING_MESSAGE_*, and for the blockREASONING_STARTandREASONING_ENDbracket. A block and the message inside it may share an id, so each has its own open set: content naming a block’s id has no open message to land in. TOOL_CALL_RESULTmay not answer a call that has not ended.STEP_FINISHEDrequires a matchingSTEP_STARTED, and step names do not nest with themselves.- Everything open must be closed before
RUN_FINISHED. - An
interruptoutcome must carry at least one interrupt — the one rule the type system cannot express, checked byRunOutcome::validate. - A continuation, terminator or re-open that names a subagent must name
the one that opened the entity — a message, a reasoning block or the
message inside it, a tool call, an activity, or whatever a
REASONING_ENCRYPTED_VALUEattaches to. One that names none is accepted: attribution is optional per event, and a bare continuation is what a pre-subagent producer sends. It does not hand the entity to the parent either: the first writer stays the owner. - A tool call belongs to the message its
parentMessageIdnames, so aTOOL_CALL_STARTtagged with one subagent while that message belongs to another is rejected; an untagged one inherits the message’s owner. - Steps are scoped to the agent that opened them: a subagent cannot close the parent’s step, or a sibling’s, and the same name may be open under two owners at once.
SUBAGENT_STARTEDnames an invocation that is neither active nor already finished in this run, and aparentSubagentRunIdthat was started.SUBAGENT_FINISHEDandSUBAGENT_ERRORname an active one.- Every started subagent is closed before
RUN_FINISHED— not beforeRUN_ERROR, where an unclosed subagent is the expected shape.
What is deliberately not a rule: that one stream must close before the
next opens. Everything here is keyed by id, exactly as the TypeScript
verifier keys its activeMessages / activeToolCalls maps. Two messages
may stream at once, two tool calls may stream at once, and a tool call may
open inside the message that narrates it — which is what every provider
doing parallel tool calls actually sends. Nor must an attributing
subagentRunId have been announced: attribution without lifecycle events
is a supported mode. Events outside these families (state, activity, raw,
custom) are unordered and close nothing that was opened explicitly. 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 a
TOOL_CALL_RESULT mints the tool message it names under its own
attribution.
§Several runs
AG-UI 1.0 lets one stream carry several runs in sequence: the run a request asked for, preceded by runs the producer replays. Every rule above that says “open” is about one run. A new run starts with nothing open, whatever a failed run left behind, and may announce again a subagent the run before it suspended. Who owns an id is about the stream: messages accumulate across runs, and a message keeps its first writer.
use ag_ui::client::verify_all;
use ag_ui::Event;
verify_all(&[
Event::run_started("thread-1", "run-1"),
Event::run_error("the model provider went away"),
Event::run_started("thread-1", "run-2"),
Event::run_finished_success("thread-1", "run-2"),
])?;§Chunks
A *_CHUNK event is judged as the events it stands for.
crate::client::chunks expands chunks before this stage, so a
Thread never shows it one; a verifier handed a
raw stream expands each chunk itself, by the same model, and rejects one
that cannot be expanded.
§One set of rules
The rules are not written here. They are the crate’s shared ordering rules, the ones a hosted run checks what it emits by. This module words a rejection for whoever is reading the stream, and differs from the producer’s reading in two places, both about what only one end knows:
Verifier::finishreports a stream that stopped before a terminal event. Only a reader can see that.- A
TOOL_CALL_RESULTfor a call this stream never introduced is accepted. A producer rejects one, since it knows every call it answers; a reader may hold less of the conversation than the producer does.
Structs§
- Verifier
- An ordering state machine for one event stream.
Functions§
- verify_
all - Verifies a whole stream in one call: one run, or several in sequence.