1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! 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`](crate::client::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());
//! # Ok::<(), ag_ui::client::Error>(())
//! ```
//!
//! # The rules
//!
//! 1. `RUN_STARTED` opens the stream — or `RUN_ERROR` does, for a run that
//! failed before it began. Nothing else may come first, `RAW` and `CUSTOM`
//! included.
//! 2. `RUN_FINISHED` and `RUN_ERROR` end the run. Two things may follow: one
//! `RUN_ERROR` after `RUN_FINISHED`, a failure that surfaced after the run
//! reported success, which fails the run; and after either, a
//! `RUN_STARTED`, which begins the next run on the same stream. A
//! `RUN_STARTED` inside a run that has not ended is rejected.
//! 3. `TEXT_MESSAGE_CONTENT` and `TEXT_MESSAGE_END` require an open message
//! with the same id, and `TEXT_MESSAGE_START` may not re-open an id that is
//! already open.
//! 4. The same, for `TOOL_CALL_*`, for `REASONING_MESSAGE_*`, and for the
//! block `REASONING_START` and `REASONING_END` bracket. 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.
//! 5. `TOOL_CALL_RESULT` may not answer a call that has not ended.
//! 6. `STEP_FINISHED` requires a matching `STEP_STARTED`, and step names do not
//! nest with themselves.
//! 7. Everything open must be closed before `RUN_FINISHED`.
//! 8. An `interrupt` outcome must carry at least one interrupt — the one rule
//! the type system cannot express, checked by
//! [`RunOutcome::validate`](crate::outcome::RunOutcome::validate).
//! 9. 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_VALUE` attaches 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.
//! 10. A tool call belongs to the message its `parentMessageId` names, so a
//! `TOOL_CALL_START` tagged with one subagent while that message belongs
//! to another is rejected; an untagged one inherits the message's owner.
//! 11. 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.
//! 12. `SUBAGENT_STARTED` names an invocation that is neither active nor
//! already finished in this run, and a `parentSubagentRunId` that was
//! started. `SUBAGENT_FINISHED` and `SUBAGENT_ERROR` name an active one.
//! 13. Every started subagent is closed before `RUN_FINISHED` — not before
//! `RUN_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"),
//! ])?;
//! # Ok::<(), ag_ui::client::Error>(())
//! ```
//!
//! # Chunks
//!
//! A `*_CHUNK` event is judged as the events it stands for.
//! [`crate::client::chunks`] expands chunks before this stage, so a
//! [`Thread`](crate::client::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::finish`] reports a stream that stopped before a terminal
//! event. Only a reader can see that.
//! - A `TOOL_CALL_RESULT` for 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.
use crate;
use crate::;
use crate;
/// An ordering state machine for one event stream.
///
/// One verifier per stream: it is stateful, and its state is that stream's
/// progress, through however many runs the stream carries.
/// A violation as one sentence, for whoever is reading the stream: the event
/// that arrived, then what was wrong with it.
/// The rejection of a `RUN_STARTED` inside a run that has not ended, for a
/// reader that pairs runs without a [`Verifier`].
pub
/// Verifies a whole stream in one call: one run, or several in sequence.
///
/// The streaming form is [`Verifier`]; this is the convenience for recorded
/// streams and tests.