ag-ui 0.4.5

A Rust SDK for the AG-UI protocol: protocol types and wire encoding, a server runtime for hosting an agent, and a client runtime for consuming one. Working toward becoming the official AG-UI Rust SDK; not affiliated with the AG-UI protocol organisation today.
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Regression tests for public conversation contracts from an SDK consumer.
#![cfg(feature = "client")]

use std::sync::{
    Arc, Mutex,
    atomic::{AtomicUsize, Ordering},
};
use std::task::Poll;

use ag_ui::client::transport::{ReplayTransport, Transport, TransportFuture, boxed_stream};
use ag_ui::client::{Error, RunEnd, SubagentStatus, SubmissionStatus, Thread, Update};
use ag_ui::{Event, Interrupt, Message, ResumeEntry, RunAgentInput, TextMessageRole};
use futures_util::{
    StreamExt,
    task::{ArcWake, waker},
};
use serde::{Deserialize, Serialize};
use serde_json::json;

fn script(events: impl IntoIterator<Item = Event>) -> ReplayTransport {
    ReplayTransport::new(events).matching_requests()
}
fn success() -> Vec<Event> {
    vec![
        Event::run_started("t", "r"),
        Event::run_finished_success("t", "r"),
    ]
}
fn questions() -> Vec<Interrupt> {
    vec![
        Interrupt::new("first", "approval"),
        Interrupt::new("second", "approval"),
    ]
}
fn pause() -> Vec<Event> {
    vec![
        Event::run_started("t", "r"),
        Event::run_finished_interrupt("t", "r", questions()),
    ]
}
fn answers() -> Vec<ResumeEntry> {
    vec![
        ResumeEntry::resolved("first", json!({"approved":true})),
        ResumeEntry::cancelled("second"),
    ]
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Counter {
    count: u32,
}

#[tokio::test]
async fn local_state_is_validated_atomically_and_remote_mismatch_invalidates_cache() {
    let transport = script([
        Event::run_started("t", "r"),
        Event::state_snapshot(json!({"count":"invalid"})),
        Event::state_snapshot(json!({"count":3})),
        Event::run_finished_success("t", "r"),
    ]);
    let mut thread = Thread::<_, Counter>::builder(transport, "t")
        .state(json!({"count":1}))
        .build()
        .unwrap();
    assert_eq!(thread.state().unwrap().count, 1);
    thread.set_state(json!({"count":2})).unwrap();
    assert!(thread.set_state(json!({"count":"invalid"})).is_err());
    assert_eq!(thread.state().unwrap().count, 2);
    assert_eq!(thread.raw_state(), &json!({"count":2}));
    let mut run = thread.send("go").unwrap();
    assert!(matches!(
        run.next().await,
        Some(Update::Error(Error::State(_)))
    ));
    assert!(
        run.thread().state().is_err(),
        "old typed values must be unavailable"
    );
    assert_eq!(run.thread().raw_state(), &json!({"count":"invalid"}));
    let report = run.collect_report().await;
    assert_eq!(
        report.diagnostics.len(),
        1,
        "partial consumption still retains errors"
    );
    assert_eq!(thread.state().unwrap().count, 3);
    assert!(
        Thread::<_, Counter>::builder(script([]), "t")
            .build()
            .is_err()
    );
}

#[tokio::test]
async fn preparing_dropping_and_aborting_before_first_poll_do_not_dispatch() {
    let transport = script(success());
    let mut thread = Thread::new(transport.clone(), "t");
    thread.set_next_run_id("reserved");
    let before = thread.snapshot();
    drop(thread.send("not sent").unwrap());
    assert_eq!(before, thread.snapshot());
    let run = thread.send("also not sent").unwrap();
    run.abort_handle().abort();
    assert_eq!(run.collect_report().await.end, RunEnd::Aborted);
    assert_eq!(before, thread.snapshot());
    assert!(transport.requests().is_empty());
    thread.send("actually sent").unwrap().collect_report().await;
    assert_eq!(transport.last_request().unwrap().run_id, "reserved");
    assert_eq!(thread.messages().len(), 1);
}

#[tokio::test]
async fn resume_preflight_requires_each_stored_pending_id_once() {
    let transport = ReplayTransport::with_runs([pause(), success()]).matching_requests();
    let mut thread = Thread::new(transport.clone(), "t");
    thread.send("review").unwrap().collect_report().await;
    let before = thread.snapshot();
    assert!(thread.send("ignore approvals").is_err());
    assert!(
        thread
            .resume_many([ResumeEntry::cancelled("first")])
            .is_err()
    );
    assert!(
        thread
            .resume_many([
                ResumeEntry::cancelled("first"),
                ResumeEntry::cancelled("first")
            ])
            .is_err()
    );
    assert!(
        thread
            .resume_many([
                ResumeEntry::cancelled("first"),
                ResumeEntry::cancelled("unknown")
            ])
            .is_err()
    );
    assert_eq!(before, thread.snapshot());
    assert_eq!(transport.requests().len(), 1);
    drop(thread.resume_many(answers()).unwrap());
    assert_eq!(before, thread.snapshot());
    let report = thread
        .resume_many(answers())
        .unwrap()
        .collect_report()
        .await;
    assert!(matches!(report.end, RunEnd::Success { .. }));
    assert!(thread.interrupts().is_empty());
    assert!(thread.submission().is_none());
}

#[tokio::test]
async fn expiry_comes_from_stored_interrupt_not_a_forged_argument() {
    let mut interrupt = Interrupt::new("first", "approval");
    interrupt.expires_at = Some("2000-01-01T00:00:00Z".into());
    let transport = script([
        Event::run_started("t", "r"),
        Event::run_finished_interrupt("t", "r", vec![interrupt]),
    ]);
    let mut thread = Thread::new(transport, "t");
    thread.send("go").unwrap().collect_report().await;
    let forged = Interrupt::new("first", "approval");
    assert!(
        thread
            .resume(&forged, true)
            .unwrap_err()
            .to_string()
            .contains("expired")
    );
    assert_eq!(thread.interrupts().len(), 1);
}

#[tokio::test]
async fn expiry_preflight_handles_offsets_fractional_seconds_and_invalid_dates() {
    for (timestamp, expected_error) in [
        ("9999-12-31T23:59:59.999999999+09:00", None),
        ("2999-01-01t00:00:00.123456789z", None),
        ("2000-01-01T09:00:00+09:00", Some("expired")),
        ("1969-12-31T23:59:59.999999999Z", Some("expired")),
        ("2016-12-31T23:59:60Z", Some("expired")),
        ("2017-01-01T08:59:60+09:00", Some("expired")),
        ("2999-01-01T12:00:60Z", Some("invalid expiry timestamp")),
        ("2999-02-30T00:00:00Z", Some("invalid expiry timestamp")),
        ("2999-01-01T00:00:00", Some("invalid expiry timestamp")),
        (
            "Fri, 01 Jan 2999 00:00:00 GMT",
            Some("invalid expiry timestamp"),
        ),
        (
            "2999-01-01T00:00:00Z trailing",
            Some("invalid expiry timestamp"),
        ),
    ] {
        let mut interrupt = Interrupt::new("first", "approval");
        interrupt.expires_at = Some(timestamp.into());
        let transport = script([
            Event::run_started("t", "r"),
            Event::run_finished_interrupt("t", "r", vec![interrupt.clone()]),
        ]);
        let mut thread = Thread::new(transport.clone(), "t");
        thread.send("go").unwrap().collect_report().await;
        let before = thread.snapshot();
        match expected_error {
            Some(message) => assert!(
                thread
                    .resume(&interrupt, true)
                    .unwrap_err()
                    .to_string()
                    .contains(message),
                "{timestamp}"
            ),
            None => drop(thread.resume(&interrupt, true).unwrap()),
        }
        assert_eq!(thread.snapshot(), before, "{timestamp}");
        assert_eq!(transport.requests().len(), 1, "{timestamp}");
    }
}

#[tokio::test]
async fn expiry_rejects_invalid_and_offset_timestamps_before_dispatch() {
    for (timestamp, expected) in [
        ("2000-01-01T09:00:00.123456789+09:00", "expired"),
        ("not-a-timestamp", "invalid expiry"),
        ("2000-02-30T00:00:00Z", "invalid expiry"),
    ] {
        let mut interrupt = Interrupt::new("first", "approval");
        interrupt.expires_at = Some(timestamp.into());
        let transport = script([
            Event::run_started("t", "r"),
            Event::run_finished_interrupt("t", "r", vec![interrupt]),
        ]);
        let mut thread = Thread::new(transport.clone(), "t");
        thread.send("go").unwrap().collect_report().await;
        let before = thread.snapshot();
        let pending = thread.interrupts()[0].clone();
        assert!(
            thread
                .resume(&pending, true)
                .unwrap_err()
                .to_string()
                .contains(expected)
        );
        assert_eq!(thread.snapshot(), before);
        assert_eq!(transport.requests().len(), 1);
    }
}

#[tokio::test]
async fn lost_response_run_error_and_invalid_terminal_keep_unconfirmed_submission() {
    let error_cases = [
        vec![Event::run_started("t", "r")],
        vec![Event::run_started("t", "r"), Event::run_error("failed")],
        vec![
            Event::run_started("t", "r"),
            Event::step_started("open"),
            Event::run_finished_success("t", "r"),
        ],
    ];
    for reply in error_cases {
        let transport = ReplayTransport::with_runs([pause(), reply]).matching_requests();
        let mut thread = Thread::new(transport.clone(), "t");
        thread.send("review").unwrap().collect_report().await;
        let report = thread
            .resume_many(answers())
            .unwrap()
            .collect_report()
            .await;
        assert!(matches!(report.end, RunEnd::Failed { .. }));
        assert_eq!(thread.interrupts(), questions());
        let submission = thread.submission().unwrap();
        assert_eq!(submission.status, SubmissionStatus::Unconfirmed);
        assert_eq!(submission.entries, answers());
        assert_eq!(submission.run_id, transport.last_request().unwrap().run_id);
        assert!(thread.resume_many(answers()).is_err());
        assert!(thread.send("retry").is_err());
        let saved = serde_json::to_string(&thread.snapshot()).unwrap();
        let mut restored =
            Thread::<_>::restore(script(success()), serde_json::from_str(&saved).unwrap()).unwrap();
        assert_eq!(restored.snapshot(), thread.snapshot());
        assert!(restored.resume_many(answers()).is_err());
    }
}

#[tokio::test]
async fn valid_interrupt_terminal_replaces_previous_pending() {
    let next = vec![Interrupt::new("next", "question")];
    let transport = ReplayTransport::with_runs([
        pause(),
        vec![
            Event::run_started("t", "r"),
            Event::run_finished_interrupt("t", "r", next.clone()),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::new(transport, "t");
    thread.run().unwrap().collect_report().await;
    thread
        .resume_many(answers())
        .unwrap()
        .collect_report()
        .await;
    assert_eq!(thread.interrupts(), next);
    assert!(thread.submission().is_none());
}

#[tokio::test]
async fn a_terminal_for_another_request_never_confirms_submission_even_without_verifier() {
    let transport = ReplayTransport::with_runs([
        pause(),
        vec![
            Event::run_started("t", "r"),
            Event::run_finished_success("t", "wrong"),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::new(transport, "t");
    thread.set_verify(false);
    thread.run().unwrap().collect_report().await;
    let report = thread
        .resume_many(answers())
        .unwrap()
        .collect_report()
        .await;
    assert!(matches!(report.end, RunEnd::Failed { .. }));
    assert_eq!(
        thread.submission().unwrap().status,
        SubmissionStatus::Unconfirmed
    );
    let mut wrong = Thread::new(ReplayTransport::new(success()), "t");
    wrong.set_next_run_id("actual-request");
    let report = wrong.send("wrong response").unwrap().collect_report().await;
    assert!(
        report.diagnostics[0]
            .message
            .contains("requested thread and run IDs")
    );
}

#[tokio::test]
async fn observer_sees_raw_events_once_and_report_keeps_bounded_diagnostics_after_partial_read() {
    let transport = script([
        Event::run_started("t", "r"),
        Event::custom("progress", json!(50)),
        Event::step_started("work"),
        Event::step_finished("work"),
        Event::text_message_chunk(Some("reply".into()), Some("Hello".into())),
        Event::state_delta(vec![ag_ui::PatchOperation::replace("/missing", json!(1))]),
        Event::state_delta(vec![ag_ui::PatchOperation::replace("/missing", json!(2))]),
        Event::state_delta(vec![ag_ui::PatchOperation::replace("/missing", json!(3))]),
        Event::run_finished_success("t", "r"),
    ]);
    let mut thread = Thread::new(transport, "t");
    thread.set_diagnostic_limit(2);
    let observed = Arc::new(Mutex::new(Vec::new()));
    let events = observed.clone();
    thread.on_event(move |event| events.lock().unwrap().push(event.event_type()));
    let mut run = thread.send("go").unwrap();
    assert!(matches!(run.next().await, Some(Update::Message(_))));
    let report = run.collect_report().await;
    assert!(matches!(report.end, RunEnd::Success { .. }));
    assert_eq!(report.diagnostics.len(), 2);
    assert_eq!(report.diagnostics_omitted, 1);
    assert_eq!(report.new_messages.len(), 2);
    let events = observed.lock().unwrap();
    assert_eq!(events.len(), 9);
    assert!(events.contains(&ag_ui::EventType::Custom));
    assert!(events.contains(&ag_ui::EventType::TextMessageChunk));
    assert!(!events.contains(&ag_ui::EventType::TextMessageStart));
}

#[derive(Clone)]
struct PendingTransport {
    dropped: Arc<AtomicUsize>,
    connecting: bool,
}
struct DropCounter(Arc<AtomicUsize>);
impl Drop for DropCounter {
    fn drop(&mut self) {
        self.0.fetch_add(1, Ordering::SeqCst);
    }
}
impl Transport for PendingTransport {
    fn run(&self, input: RunAgentInput) -> TransportFuture {
        let guard = DropCounter(self.dropped.clone());
        if self.connecting {
            Box::pin(async move {
                let _guard = guard;
                std::future::pending().await
            })
        } else {
            Box::pin(async move {
                let start = Event::run_started(input.thread_id, input.run_id);
                Ok(boxed_stream(futures_util::stream::iter([Ok(start)]).chain(
                    futures_util::stream::poll_fn(move |_| {
                        let _ = &guard;
                        Poll::Pending
                    }),
                )))
            })
        }
    }
}
#[derive(Default)]
struct WakeCount(AtomicUsize);
impl ArcWake for WakeCount {
    fn wake_by_ref(this: &Arc<Self>) {
        this.0.fetch_add(1, Ordering::SeqCst);
    }
}

#[tokio::test]
async fn abort_wakes_pending_connect_and_stream_drops_resources_and_yields_one_terminal() {
    use futures_util::Stream;
    for connecting in [true, false] {
        let dropped = Arc::new(AtomicUsize::new(0));
        let transport = PendingTransport {
            dropped: dropped.clone(),
            connecting,
        };
        let mut thread = Thread::new(transport, "t");
        let mut run = thread.send("go").unwrap();
        let abort = run.abort_handle();
        let wake = Arc::new(WakeCount::default());
        let waker = waker(wake.clone());
        let mut cx = std::task::Context::from_waker(&waker);
        assert!(std::pin::Pin::new(&mut run).poll_next(&mut cx).is_pending());
        abort.abort();
        assert!(wake.0.load(Ordering::SeqCst) > 0);
        let updates: Vec<_> = run.collect().await;
        assert_eq!(
            updates
                .iter()
                .filter(|u| matches!(u, Update::Done(_)))
                .count(),
            1
        );
        assert!(matches!(
            updates.last(),
            Some(Update::Done(RunEnd::Aborted))
        ));
        assert_eq!(dropped.load(Ordering::SeqCst), 1);
        assert_eq!(thread.last_run_end(), Some(&RunEnd::Aborted));
    }
}

#[tokio::test]
async fn terminal_application_wins_late_abort_and_old_handle_cannot_cancel_next_run() {
    let transport = ReplayTransport::with_runs([success(), success()]).matching_requests();
    let mut thread = Thread::new(transport, "t");
    let mut run = thread.send("first").unwrap();
    let abort = run.abort_handle();
    assert!(matches!(
        run.next().await,
        Some(Update::Done(RunEnd::Success { .. }))
    ));
    abort.abort();
    assert!(matches!(
        run.collect_report().await.end,
        RunEnd::Success { .. }
    ));
    abort.abort();
    assert!(matches!(
        thread.send("second").unwrap().collect_report().await.end,
        RunEnd::Success { .. }
    ));
}

#[tokio::test]
async fn dropping_dispatched_resume_and_restoring_inflight_snapshot_remain_unconfirmed() {
    let transport = ReplayTransport::with_runs([
        pause(),
        vec![
            Event::run_started("t", "r"),
            Event::custom("received", json!(true)),
            Event::text_message_start("partial", TextMessageRole::Assistant),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::new(transport, "t");
    thread.run().unwrap().collect_report().await;
    let mut run = thread.resume_many(answers()).unwrap();
    assert!(matches!(run.next().await, Some(Update::Message(_))));
    assert_eq!(
        run.thread().submission().unwrap().status,
        SubmissionStatus::InFlight
    );
    let snapshot = run.thread().snapshot();
    drop(run);
    assert_eq!(
        thread.submission().unwrap().status,
        SubmissionStatus::Unconfirmed
    );
    assert_eq!(thread.last_run_end(), Some(&RunEnd::Aborted));
    let restored = Thread::<_>::restore(script(success()), snapshot).unwrap();
    assert_eq!(
        restored.submission().unwrap().status,
        SubmissionStatus::Unconfirmed
    );
}

#[tokio::test]
async fn restored_ids_are_never_reused_and_malformed_snapshots_are_rejected() {
    let transport = script(success());
    let mut ids = ["old-message", "new-message", "new-run"].into_iter();
    let mut thread = Thread::<_>::builder(transport.clone(), "t")
        .messages(vec![Message::user("old-message", "saved")])
        .id_generator(move || ids.next().unwrap().into())
        .build()
        .unwrap();
    thread.send("next").unwrap().collect_report().await;
    assert_eq!(
        transport.last_request().unwrap().messages[1].id(),
        "new-message"
    );
    let snapshot = thread.snapshot();
    let mut restored = Thread::<_>::restore(script(success()), snapshot.clone()).unwrap();
    restored.set_next_run_id("new-run");
    assert!(restored.send("collision").is_err());
    assert_eq!(restored.messages(), thread.messages());
    let mut bad = snapshot.clone();
    bad.version = 2;
    assert!(Thread::<_>::restore(script([]), bad).is_err());
    let mut bad = snapshot.clone();
    bad.messages.push(bad.messages[0].clone());
    assert!(Thread::<_>::restore(script([]), bad).is_err());
    let mut bad = snapshot;
    bad.run_ids.push(bad.run_ids[0].clone());
    assert!(Thread::<_>::restore(script([]), bad).is_err());
}

#[tokio::test]
async fn run_error_marks_open_subagents_aborted_without_inventing_business_failure() {
    let transport = script([
        Event::run_started("t", "r"),
        Event::subagent_started("child", "research"),
        Event::run_error("parent failed"),
    ]);
    let mut thread = Thread::new(transport, "t");
    thread.run().unwrap().collect_report().await;
    assert_eq!(thread.subagents()[0].status, SubagentStatus::Aborted);
    let restored = Thread::<_>::restore(script([]), thread.snapshot()).unwrap();
    assert_eq!(restored.subagents(), thread.subagents());
}

#[cfg(feature = "http")]
#[test]
fn http_agent_threads_own_connections_and_keep_conversations_independent() {
    let agent = ag_ui::client::HttpAgent::new("https://example.com/agent").unwrap();
    let mut first = agent.thread("first");
    let second = agent.thread("second");
    let typed = agent
        .thread_with_state("typed", Counter { count: 4 })
        .unwrap();
    drop(agent);
    first.set_state(json!({"independent":true})).unwrap();
    first.push_message(Message::user("m", "hello")).unwrap();
    assert_eq!(second.raw_state(), &json!({}));
    assert!(second.messages().is_empty());
    assert_eq!(typed.state().unwrap().count, 4);
    fn assert_send<T: Send>(_: T) {}
    assert_send(first.send("owned").unwrap());
}

#[tokio::test]
async fn restoring_a_live_non_resume_run_records_local_abortion() {
    let transport = script([
        Event::run_started("t", "r"),
        Event::text_message_start("partial", TextMessageRole::Assistant),
    ]);
    let mut thread = Thread::new(transport, "t");
    let mut run = thread.send("go").unwrap();
    run.next().await.unwrap();
    let snapshot = run.thread().snapshot();
    assert!(snapshot.active_run_id.is_some());
    let restored = Thread::<_>::restore(script([]), snapshot).unwrap();
    assert_eq!(restored.last_run_end(), Some(&RunEnd::Aborted));
    assert!(restored.snapshot().active_run_id.is_none());
}

#[tokio::test]
#[allow(deprecated)]
async fn restored_legacy_reasoning_ids_do_not_overwrite_saved_reasoning() {
    let first = script([
        Event::run_started("t", "r"),
        Event::thinking_start(None),
        Event::thinking_text_message_content("original"),
        Event::thinking_end(),
        Event::run_finished_success("t", "r"),
    ]);
    let mut thread = Thread::new(first, "t");
    thread.run().unwrap().collect_report().await;
    let transport = script([
        Event::run_started("t", "r"),
        Event::thinking_start(None),
        Event::thinking_text_message_content("next"),
        Event::thinking_end(),
        Event::run_finished_success("t", "r"),
    ]);
    let mut restored = Thread::<_>::restore(transport, thread.snapshot()).unwrap();
    restored.run().unwrap().collect_report().await;
    assert_eq!(restored.reasoning().len(), 2);
    assert_ne!(restored.reasoning()[0].id, restored.reasoning()[1].id);
    assert_eq!(restored.reasoning()[0].content, "original");
    assert_eq!(restored.reasoning()[1].content, "next");
}

#[tokio::test]
async fn activity_is_local_ui_state_while_raw_requests_remain_literal() {
    // Reference: official TS AbstractAgent.prepareRunAgentInput filters
    // message.role !== "activity" (commit 747933694b05676203da7d5bb8d8e50432e59b75).
    let activity = Message::Activity(ag_ui::ActivityMessage {
        id: "progress".into(),
        activity_type: "progress".into(),
        content: json!({"percent": 50}).as_object().unwrap().clone(),
        ..Default::default()
    });
    let transport = ReplayTransport::with_runs([success(), success()]).matching_requests();
    let mut thread = Thread::new(transport.clone(), "t");
    thread.push_message(activity.clone()).unwrap();
    thread.send("next turn").unwrap().collect_report().await;
    assert_eq!(transport.requests()[0].messages.len(), 1);
    assert!(matches!(
        transport.requests()[0].messages[0],
        Message::User(_)
    ));
    assert_eq!(thread.messages()[0], activity);
    let restored = Thread::<_>::restore(script([]), thread.snapshot()).unwrap();
    assert_eq!(restored.messages()[0], activity);

    // Raw transport callers choose their complete request payload themselves.
    let raw = ag_ui::client::RemoteAgent::new(transport.clone());
    let input = ag_ui::client::RunParams::new("t", "raw").message(activity.clone());
    raw.run_events(input).collect::<Vec<_>>().await;
    assert_eq!(transport.requests()[1].messages, vec![activity]);
}

#[tokio::test]
async fn dropping_a_run_clears_open_text_and_reasoning_even_without_subagents() {
    let transport = script([
        Event::run_started("t", "r"),
        Event::text_message_start("partial-text", TextMessageRole::Assistant),
        Event::reasoning_start("partial-reason"),
    ]);
    let mut thread = Thread::new(transport, "t");
    let mut run = thread.send("go").unwrap();
    assert!(matches!(run.next().await, Some(Update::Message(_))));
    assert!(matches!(run.next().await, Some(Update::Reasoning(_))));
    drop(run);
    assert!(thread.subagents().is_empty());
    let mut observed = thread.applier().clone();
    assert!(
        observed
            .apply(&Event::text_message_chunk(
                None,
                Some("must not append".into())
            ))
            .is_err()
    );
    let chunk = ag_ui::ReasoningMessageChunkEvent {
        delta: Some("must not append".into()),
        ..Default::default()
    };
    assert!(
        observed
            .apply(&Event::ReasoningMessageChunk(chunk))
            .is_err()
    );
    assert_eq!(thread.last_run_end(), Some(&RunEnd::Aborted));
    assert!(thread.snapshot().active_run_id.is_none());
}