gadk-engine 0.4.6

The gadk runtime: LLM agent and flow, workflow agents and graphs, and the runner
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Port of Go runner/live_runner_test.go.

use std::any::Any;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use async_trait::async_trait;
use chrono::{DateTime, TimeZone, Utc};
use futures::StreamExt;
use gadk_agent::{
    Agent, AgentConfig, AgentError, AgentState, AgentType, EventStream, InvocationContext,
    LiveAgent, LiveRequest, LiveRunConfig, LiveSession, Plugin, new_with_state,
};
use gadk_genai::{
    Blob, Content, FunctionCall, FunctionResponse, Part, ROLE_MODEL, ROLE_USER, Transcription,
};
use gadk_model::RealtimeInput;
use gadk_platform::Platform;
use gadk_session::{
    CreateRequest, DeleteRequest, Event, GetRequest, InMemorySessionService, ListRequest, Session,
    SessionError, SessionService,
};
use parking_lot::Mutex;
use serde_json::json;

use crate::runner::{PluginConfig, RunOptions, Runner, RunnerConfig};

const APP: &str = "testApp";
const USER: &str = "testUser";

/// The producer of a mock agent's live event stream (Go `runLiveFn`'s iterator).
type Sequence = Arc<dyn Fn(InvocationContext) -> EventStream + Send + Sync>;

/// Go `mockLiveAgent.RunLive`: the live hook of a plain custom agent.
struct MockLive {
    session: Arc<dyn LiveSession>,
    sequence: Sequence,
    called: Arc<AtomicBool>,
}

#[async_trait]
impl LiveAgent for MockLive {
    async fn run_live(
        &self,
        ctx: InvocationContext,
    ) -> Result<(Arc<dyn LiveSession>, EventStream), AgentError> {
        self.called.store(true, Ordering::SeqCst);
        Ok((self.session.clone(), (self.sequence)(ctx)))
    }
}

/// Go `&mockLiveAgent{Agent: agent.New(agent.Config{Name: name}), runLiveFn: …}`.
fn mock_live_agent(
    name: &str,
    session: Arc<dyn LiveSession>,
    sequence: Sequence,
    called: Arc<AtomicBool>,
) -> Arc<dyn Agent> {
    new_with_state(
        AgentConfig {
            name: name.into(),
            ..Default::default()
        },
        AgentState {
            agent_type: AgentType::CustomAgent,
            config: None,
            on_before_agent_callback_event: None,
            live: Some(Arc::new(MockLive {
                session,
                sequence,
                called,
            })),
        },
    )
    .unwrap()
}

/// Go `dummyLiveSession`.
struct DummyLiveSession;

#[async_trait]
impl LiveSession for DummyLiveSession {
    async fn send(&self, _req: LiveRequest) -> Result<(), AgentError> {
        Ok(())
    }
    async fn close(&self) -> Result<(), AgentError> {
        Ok(())
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Go `recordingLiveSession`.
#[derive(Default)]
struct RecordingLiveSession {
    requests: Mutex<Vec<LiveRequest>>,
}

#[async_trait]
impl LiveSession for RecordingLiveSession {
    async fn send(&self, req: LiveRequest) -> Result<(), AgentError> {
        self.requests.lock().push(req);
        Ok(())
    }
    async fn close(&self) -> Result<(), AgentError> {
        Ok(())
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Go `failingLiveAppendService`: fails the append of the event whose ID is `fail_id`.
struct FailingLiveAppendService {
    inner: Arc<dyn SessionService>,
    fail_id: Option<String>,
}

#[async_trait]
impl SessionService for FailingLiveAppendService {
    async fn create(&self, req: CreateRequest) -> Result<Arc<dyn Session>, SessionError> {
        self.inner.create(req).await
    }
    async fn get(&self, req: GetRequest) -> Result<Arc<dyn Session>, SessionError> {
        self.inner.get(req).await
    }
    async fn list(&self, req: ListRequest) -> Result<Vec<Arc<dyn Session>>, SessionError> {
        self.inner.list(req).await
    }
    async fn delete(&self, req: DeleteRequest) -> Result<(), SessionError> {
        self.inner.delete(req).await
    }
    async fn append_event(
        &self,
        session: &dyn Session,
        event: &mut Event,
    ) -> Result<(), SessionError> {
        if self.fail_id.as_deref() == Some(event.id.as_str()) {
            return Err(SessionError::Backend("session storage unavailable".into()));
        }
        self.inner.append_event(session, event).await
    }
}

async fn create_session(svc: &dyn SessionService, session_id: &str) {
    svc.create(CreateRequest {
        app_name: APP.into(),
        user_id: USER.into(),
        session_id: Some(session_id.into()),
        state: None,
    })
    .await
    .unwrap();
}

fn new_runner(agent: Arc<dyn Agent>, svc: Arc<dyn SessionService>, plugins: Vec<Plugin>) -> Runner {
    Runner::new(RunnerConfig {
        app_name: APP.into(),
        agent,
        session_service: svc,
        artifact_service: None,
        memory_service: None,
        plugin_config: PluginConfig {
            plugins,
            ..Default::default()
        },
        auto_create_session: false,
        platform: Platform::default(),
        compaction: None,
    })
    .unwrap()
}

/// Go `newRunLiveTestRunner`: a created session and a runner over a `test_agent` mock live agent.
async fn new_run_live_test_runner(
    session_id: &str,
    sequence: Sequence,
) -> (Runner, Arc<dyn SessionService>) {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, session_id).await;
    let agent = mock_live_agent(
        "test_agent",
        Arc::new(DummyLiveSession),
        sequence,
        Arc::new(AtomicBool::new(false)),
    );
    (new_runner(agent, svc.clone(), vec![]), svc)
}

/// Go `getRunLiveTestEvents`.
async fn get_run_live_test_events(svc: &dyn SessionService, session_id: &str) -> Vec<Event> {
    svc.get(GetRequest {
        app_name: APP.into(),
        user_id: USER.into(),
        session_id: session_id.into(),
        ..Default::default()
    })
    .await
    .unwrap()
    .events()
}

fn new_event(ctx: &InvocationContext) -> Event {
    Event::new(ctx.platform(), ctx.invocation_id())
}

fn transcription(text: &str) -> Option<Transcription> {
    Some(Transcription {
        text: text.into(),
        ..Default::default()
    })
}

fn function_call_content(name: &str) -> Option<Content> {
    Some(Content {
        role: String::new(),
        parts: vec![Part {
            function_call: Some(FunctionCall {
                name: name.into(),
                ..Default::default()
            }),
            ..Default::default()
        }],
    })
}

fn has_function_call(ev: &Event) -> bool {
    ev.response
        .content
        .as_ref()
        .is_some_and(|c| c.parts.iter().any(|p| p.function_call.is_some()))
}

/// Sets its flag when dropped: detects that the upstream stream was dropped mid-way.
struct SetOnDrop(Arc<AtomicBool>);

impl Drop for SetOnDrop {
    fn drop(&mut self) {
        self.0.store(true, Ordering::SeqCst);
    }
}

// parity: skipped TestRunner_RunLive_NilEventYieldedTerminatesRun: an EventStream item is Result<Event, AgentError>, so a live agent cannot yield a nil event (as 1b skipped TestRunner_NilEventYieldedTerminatesRun)

#[tokio::test]
async fn test_runner_run_live_callbacks() {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, "testSession").await;

    let before_run_called = Arc::new(AtomicBool::new(false));
    let after_run_called = Arc::new(AtomicBool::new(false));
    let (b, a) = (before_run_called.clone(), after_run_called.clone());
    let plugin = Plugin::new(gadk_agent::PluginConfig {
        name: "test_plugin".into(),
        before_run_callback: Some(Arc::new(move |_ctx| {
            b.store(true, Ordering::SeqCst);
            Box::pin(async { Ok::<Option<Content>, AgentError>(None) })
        })),
        after_run_callback: Some(Arc::new(move |_ctx| {
            a.store(true, Ordering::SeqCst);
            Box::pin(async {})
        })),
        ..Default::default()
    });

    let sequence: Sequence =
        Arc::new(|ctx: InvocationContext| futures::stream::iter(vec![Ok(new_event(&ctx))]).boxed());
    let agent = mock_live_agent(
        "test_agent",
        Arc::new(DummyLiveSession),
        sequence,
        Arc::new(AtomicBool::new(false)),
    );
    let r = new_runner(agent, svc, vec![plugin]);

    let (_sess, mut stream) = r
        .run_live(
            USER,
            "testSession",
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");

    assert!(
        before_run_called.load(Ordering::SeqCst),
        "BeforeRunCallback was not called before starting RunLive"
    );
    assert!(
        !after_run_called.load(Ordering::SeqCst),
        "AfterRunCallback should not be called until iterator is consumed"
    );
    while let Some(item) = stream.next().await {
        item.unwrap();
    }
    assert!(
        after_run_called.load(Ordering::SeqCst),
        "AfterRunCallback was not called after iterator was consumed"
    );
}

#[tokio::test]
async fn test_runner_run_live_early_exit() {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, "testSession2").await;

    let expected_content = Content::from_text("early exit content", "");
    let reply = expected_content.clone();
    let plugin = Plugin::new(gadk_agent::PluginConfig {
        name: "test_plugin".into(),
        before_run_callback: Some(Arc::new(move |_ctx| {
            let out = Ok(Some(reply.clone()));
            Box::pin(async move { out })
        })),
        ..Default::default()
    });

    let run_live_called = Arc::new(AtomicBool::new(false));
    let sequence: Sequence = Arc::new(|_ctx: InvocationContext| futures::stream::empty().boxed());
    let agent = mock_live_agent(
        "test_agent",
        Arc::new(DummyLiveSession),
        sequence,
        run_live_called.clone(),
    );
    let r = new_runner(agent, svc.clone(), vec![plugin]);

    let (sess, stream) = r
        .run_live(
            USER,
            "testSession2",
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");
    assert!(
        !run_live_called.load(Ordering::SeqCst),
        "RunLive should not have been called on the agent due to early exit"
    );

    let events: Vec<Event> = stream.map(|r| r.unwrap()).collect().await;
    assert_eq!(events.len(), 1, "expected 1 event");
    assert_eq!(events[0].response.content.as_ref(), Some(&expected_content));
    assert_eq!(events[0].author, "test_agent");
    // The early-exit event is persisted.
    assert_eq!(
        get_run_live_test_events(&*svc, "testSession2").await.len(),
        1
    );

    let err = sess
        .send(LiveRequest::default())
        .await
        .expect_err("send to an early-exited session must fail");
    assert!(
        err.to_string().contains("session is closed"),
        "expected error 'session is closed' when sending to early exited session, got {err}"
    );
    sess.close().await.expect("Close() failed");
}

#[tokio::test]
async fn test_runner_run_live_chronological_buffering() {
    let sequence: Sequence = Arc::new(|ctx: InvocationContext| {
        Box::pin(async_stream::stream! {
            // 1. Partial transcription.
            let mut ev1 = new_event(&ctx);
            ev1.response.partial = true;
            ev1.response.output_transcription = transcription("Hello");
            yield Ok(ev1);
            // 2. Function call (happening during transcription).
            let mut ev2 = new_event(&ctx);
            ev2.response.content = function_call_content("test_func");
            yield Ok(ev2);
            // 3. Final transcription.
            let mut ev3 = new_event(&ctx);
            ev3.response.output_transcription = transcription("Hello there.");
            yield Ok(ev3);
        })
    });
    let (r, svc) = new_run_live_test_runner("testSession3", sequence).await;

    let (_sess, mut stream) = r
        .run_live(
            USER,
            "testSession3",
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");
    while let Some(item) = stream.next().await {
        item.unwrap();
    }

    let events = get_run_live_test_events(&*svc, "testSession3").await;
    // Final transcription first, function call second; the partial transcription is not saved.
    assert_eq!(events.len(), 2, "expected 2 saved events in session");
    assert_eq!(
        events[0]
            .response
            .output_transcription
            .as_ref()
            .map(|t| t.text.as_str()),
        Some("Hello there."),
        "first saved event should be the final transcription"
    );
    assert!(
        has_function_call(&events[1]),
        "second saved event should be the function call, got {:?}",
        events[1]
    );
}

#[tokio::test]
async fn test_runner_run_live_flushes_buffered_events_at_eof() {
    const SESSION_ID: &str = "testSessionBufferedEOF";
    let sequence: Sequence = Arc::new(|ctx: InvocationContext| {
        Box::pin(async_stream::stream! {
            let mut partial = new_event(&ctx);
            partial.response.partial = true;
            partial.response.input_transcription = transcription("book me a ");
            yield Ok(partial);
            let mut buffered_call = new_event(&ctx);
            buffered_call.response.content = function_call_content("book_flight");
            yield Ok(buffered_call);
        })
    });
    let (r, svc) = new_run_live_test_runner(SESSION_ID, sequence).await;

    let (_sess, mut stream) = r
        .run_live(
            USER,
            SESSION_ID,
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");
    let mut received = Vec::new();
    while let Some(item) = stream.next().await {
        let event = item.unwrap();
        if has_function_call(&event) {
            let got = get_run_live_test_events(&*svc, SESSION_ID).await.len();
            assert_eq!(got, 1, "persisted events while yielding buffered call");
        }
        received.push(event);
    }

    assert_eq!(
        received.len(),
        2,
        "RunLive() yielded {} events, want partial transcription and buffered call",
        received.len()
    );
    assert!(
        has_function_call(&received[1]),
        "RunLive() second event is not the buffered function call"
    );
    let events = get_run_live_test_events(&*svc, SESSION_ID).await;
    assert_eq!(events.len(), 1, "persisted events after EOF");
    let content = events[0].response.content.as_ref().expect("content");
    assert_eq!(content.parts.len(), 1);
    assert_eq!(
        content.parts[0]
            .function_call
            .as_ref()
            .map(|fc| fc.name.as_str()),
        Some("book_flight"),
        "persisted event does not contain the expected function call"
    );
}

// parity: adapted: Go observes the downstream stop through yield returning false; a Rust upstream observes it by being dropped before it finishes (a drop flag set while the completion flag is unset)
#[tokio::test]
async fn test_runner_run_live_does_not_flush_buffered_events_after_consumer_stops() {
    const SESSION_ID: &str = "testSessionBufferedStop";
    let dropped = Arc::new(AtomicBool::new(false));
    let completed = Arc::new(AtomicBool::new(false));
    let (d, c) = (dropped.clone(), completed.clone());
    let sequence: Sequence = Arc::new(move |ctx: InvocationContext| {
        let (d, c) = (d.clone(), c.clone());
        Box::pin(async_stream::stream! {
            let _on_drop = SetOnDrop(d);
            let mut partial = new_event(&ctx);
            partial.response.partial = true;
            partial.response.input_transcription = transcription("book me a ");
            yield Ok(partial);
            let mut call = new_event(&ctx);
            call.response.content = function_call_content("book_flight");
            yield Ok(call);
            let mut continued_partial = new_event(&ctx);
            continued_partial.response.partial = true;
            continued_partial.response.input_transcription = transcription("book me a flight");
            yield Ok(continued_partial);
            c.store(true, Ordering::SeqCst);
        })
    });
    let (r, svc) = new_run_live_test_runner(SESSION_ID, sequence).await;

    let (_sess, mut stream) = r
        .run_live(
            USER,
            SESSION_ID,
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");
    let mut yield_calls = 0;
    while yield_calls < 2 {
        let Some(item) = stream.next().await else {
            break;
        };
        item.unwrap();
        yield_calls += 1;
    }
    drop(stream);

    assert_eq!(yield_calls, 2, "downstream yield calls");
    assert!(
        dropped.load(Ordering::SeqCst) && !completed.load(Ordering::SeqCst),
        "upstream iterator did not observe the downstream stop"
    );
    assert_eq!(
        get_run_live_test_events(&*svc, SESSION_ID).await.len(),
        0,
        "persisted events after consumer stop"
    );
}

// parity: adapted: Rust event streams cannot carry nil events, so the two nil-event cases are dropped; SessionService::append_event takes no context, so Go's contextBoundLiveAppendService has no counterpart and the cancel cases cancel the invocation token and yield AgentError::Cancelled
#[tokio::test]
async fn test_runner_run_live_buffered_event_exits() {
    struct Case {
        name: &'static str,
        cancel_run: bool,
        fail_append: bool,
        stop_on_error: bool,
        stop_on_call: bool,
        want_ids: &'static [&'static str],
        want_saved: &'static [&'static str],
        want_errors: usize,
    }
    let cases = [
        Case {
            name: "cancelled context preserves buffered events",
            cancel_run: true,
            fail_append: false,
            stop_on_error: false,
            stop_on_call: false,
            want_ids: &["partial", "later", "call", "response"],
            want_saved: &["later", "call", "response"],
            want_errors: 1,
        },
        Case {
            name: "cancelled context consumer stops on error",
            cancel_run: true,
            fail_append: false,
            stop_on_error: true,
            stop_on_call: false,
            want_ids: &["partial", "later"],
            want_saved: &["later"],
            want_errors: 1,
        },
        Case {
            name: "cancelled context consumer stops during flush",
            cancel_run: true,
            fail_append: false,
            stop_on_error: false,
            stop_on_call: true,
            want_ids: &["partial", "later", "call"],
            want_saved: &["later", "call"],
            want_errors: 1,
        },
        Case {
            name: "EOF preserves order and timestamps",
            cancel_run: false,
            fail_append: false,
            stop_on_error: false,
            stop_on_call: false,
            want_ids: &["partial", "later", "call", "response"],
            want_saved: &["later", "call", "response"],
            want_errors: 0,
        },
        Case {
            name: "append failure continues flush",
            cancel_run: false,
            fail_append: true,
            stop_on_error: false,
            stop_on_call: false,
            want_ids: &["partial", "later", "response"],
            want_saved: &["later", "response"],
            want_errors: 1,
        },
        Case {
            name: "append failure consumer stops",
            cancel_run: false,
            fail_append: true,
            stop_on_error: true,
            stop_on_call: false,
            want_ids: &["partial", "later"],
            want_saved: &["later"],
            want_errors: 1,
        },
        Case {
            name: "consumer stops during flush",
            cancel_run: false,
            fail_append: false,
            stop_on_error: false,
            stop_on_call: true,
            want_ids: &["partial", "later", "call"],
            want_saved: &["later", "call"],
            want_errors: 0,
        },
    ];
    for tc in cases {
        const SESSION_ID: &str = "bufferedExits";
        let start: DateTime<Utc> = Utc.with_ymd_and_hms(2026, 9, 1, 0, 0, 0).unwrap();
        let timestamp = move |id: &str| -> DateTime<Utc> {
            let secs = match id {
                "partial" => 0,
                "call" => 1,
                "response" => 2,
                "later" => 3,
                other => panic!("unexpected event id {other}"),
            };
            start + chrono::Duration::seconds(secs)
        };
        let cancel_run = tc.cancel_run;
        let sequence: Sequence = Arc::new(move |ctx: InvocationContext| {
            Box::pin(async_stream::stream! {
                let mut partial = new_event(&ctx);
                partial.id = "partial".into();
                partial.response.partial = true;
                partial.response.input_transcription = transcription("book a ");
                let mut call = new_event(&ctx);
                call.id = "call".into();
                call.response.content = function_call_content("book_flight");
                let mut response = new_event(&ctx);
                response.id = "response".into();
                response.response.content = Some(Content {
                    role: String::new(),
                    parts: vec![Part {
                        function_response: Some(FunctionResponse {
                            name: "book_flight".into(),
                            response: json!({"ok": true}).as_object().unwrap().clone(),
                            ..Default::default()
                        }),
                        ..Default::default()
                    }],
                });
                let mut later = new_event(&ctx);
                later.id = "later".into();
                later.response.content = Some(Content::from_text("unrelated message", ROLE_MODEL));
                for mut event in [partial, call, response, later] {
                    event.timestamp = timestamp(&event.id);
                    yield Ok(event);
                }
                if cancel_run {
                    // The live producer reports cancellation before closing its stream.
                    ctx.cancellation_token().cancel();
                    yield Err(AgentError::Cancelled);
                }
            })
        });
        let base: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
        create_session(&*base, SESSION_ID).await;
        let svc: Arc<dyn SessionService> = Arc::new(FailingLiveAppendService {
            inner: base.clone(),
            fail_id: tc.fail_append.then(|| "call".to_string()),
        });
        let agent = mock_live_agent(
            "test_agent",
            Arc::new(DummyLiveSession),
            sequence,
            Arc::new(AtomicBool::new(false)),
        );
        let r = new_runner(agent, svc, vec![]);
        let (_sess, mut stream) = r
            .run_live(
                USER,
                SESSION_ID,
                LiveRunConfig::default(),
                RunOptions::default(),
            )
            .await
            .unwrap();

        let mut got_ids: Vec<String> = Vec::new();
        let mut error_count = 0;
        while let Some(item) = stream.next().await {
            match item {
                Err(err) => {
                    error_count += 1;
                    if tc.cancel_run {
                        assert!(
                            matches!(err, AgentError::Cancelled),
                            "{}: error = {err}, want context cancellation",
                            tc.name
                        );
                    } else {
                        assert_eq!(
                            err.to_string(),
                            "failed to add event to session: session storage unavailable",
                            "{}: want wrapped append error",
                            tc.name
                        );
                    }
                    if tc.stop_on_error {
                        break;
                    }
                }
                Ok(event) => {
                    got_ids.push(event.id.clone());
                    assert_eq!(
                        event.timestamp,
                        timestamp(&event.id),
                        "{}: timestamp changed for yielded event {}",
                        tc.name,
                        event.id
                    );
                    if !event.response.partial {
                        let saved = get_run_live_test_events(&*base, SESSION_ID).await;
                        assert_eq!(
                            saved.last().map(|e| e.id.as_str()),
                            Some(event.id.as_str()),
                            "{}: event {} was yielded before it was saved",
                            tc.name,
                            event.id
                        );
                    }
                    if tc.stop_on_call && event.id == "call" {
                        break;
                    }
                }
            }
        }
        drop(stream);

        assert_eq!(got_ids, tc.want_ids, "{}: yielded IDs", tc.name);
        assert_eq!(error_count, tc.want_errors, "{}: errors", tc.name);
        let saved = get_run_live_test_events(&*base, SESSION_ID).await;
        for event in &saved {
            assert_eq!(
                event.timestamp,
                timestamp(&event.id),
                "{}: timestamp changed for saved event {}",
                tc.name,
                event.id
            );
        }
        let saved_ids: Vec<&str> = saved.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(saved_ids, tc.want_saved, "{}: saved IDs", tc.name);
    }
}

// parity: adapted: a Rust Part cannot be nil, so there is nothing to drop; the cases keep Go's persistence rule (text saved, function response and empty content not saved, realtime input forwarded) without the nil entries
#[tokio::test]
async fn test_runner_run_live_send_drops_nil_parts() {
    let text = Part::from_text("hi");
    let text2 = Part::from_text("there");
    let func_resp = Part {
        function_response: Some(FunctionResponse {
            name: "book_flight".into(),
            ..Default::default()
        }),
        ..Default::default()
    };
    let blob = RealtimeInput::Blob(Blob {
        mime_type: "audio/pcm".into(),
        data: bytes::Bytes::from_static(&[0x00, 0x01]),
        ..Default::default()
    });
    struct Case {
        name: &'static str,
        parts: Vec<Part>,
        realtime_input: Option<RealtimeInput>,
        want_saved: bool,
    }
    let cases = [
        Case {
            name: "text",
            parts: vec![text.clone()],
            realtime_input: None,
            want_saved: true,
        },
        Case {
            name: "two texts",
            parts: vec![text.clone(), text2.clone()],
            realtime_input: None,
            want_saved: true,
        },
        Case {
            name: "function response",
            parts: vec![func_resp.clone()],
            realtime_input: None,
            want_saved: false,
        },
        Case {
            name: "no parts",
            parts: vec![],
            realtime_input: None,
            want_saved: false,
        },
        Case {
            name: "text with realtime input",
            parts: vec![text.clone()],
            realtime_input: Some(blob.clone()),
            want_saved: true,
        },
    ];
    for tc in cases {
        const SESSION_ID: &str = "testSessionNilParts";
        let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
        create_session(&*svc, SESSION_ID).await;
        let agent_sess = Arc::new(RecordingLiveSession::default());
        let sequence: Sequence =
            Arc::new(|_ctx: InvocationContext| futures::stream::empty().boxed());
        let agent = mock_live_agent(
            "test_agent",
            agent_sess.clone(),
            sequence,
            Arc::new(AtomicBool::new(false)),
        );
        let r = new_runner(agent, svc.clone(), vec![]);
        let (sess, _stream) = r
            .run_live(
                USER,
                SESSION_ID,
                LiveRunConfig::default(),
                RunOptions::default(),
            )
            .await
            .expect("RunLive failed");

        let content = Content {
            role: ROLE_USER.into(),
            parts: tc.parts.clone(),
        };
        sess.send(LiveRequest {
            content: Some(content.clone()),
            realtime_input: tc.realtime_input.clone(),
        })
        .await
        .unwrap_or_else(|e| panic!("{}: Send() error = {e}", tc.name));

        let requests = agent_sess.requests.lock().clone();
        assert_eq!(requests.len(), 1, "{}: forwarded requests", tc.name);
        assert_eq!(
            requests[0].content.as_ref(),
            Some(&content),
            "{}: forwarded content",
            tc.name
        );
        assert_eq!(
            requests[0].realtime_input, tc.realtime_input,
            "{}: forwarded RealtimeInput",
            tc.name
        );

        let events = get_run_live_test_events(&*svc, SESSION_ID).await;
        if !tc.want_saved {
            assert_eq!(events.len(), 0, "{}: saved events", tc.name);
            continue;
        }
        assert_eq!(events.len(), 1, "{}: saved events", tc.name);
        assert_eq!(events[0].author, "user", "{}: author", tc.name);
        assert_eq!(
            events[0].response.content.as_ref(),
            Some(&content),
            "{}: saved content",
            tc.name
        );
    }
}

/// Go `time.Date(2026, time.March, d, h, 0, 0, 0, time.UTC)`.
fn march(day: u32, hour: u32) -> DateTime<Utc> {
    Utc.with_ymd_and_hms(2026, 3, day, hour, 0, 0).unwrap()
}

/// A runner over `agent` whose platform clock always reads `run_time` (Go
/// `platform.WithTimeProvider(ctx, func() time.Time { return runTime })`).
fn new_timed_runner(
    agent: Arc<dyn Agent>,
    svc: Arc<dyn SessionService>,
    plugins: Vec<Plugin>,
    run_time: DateTime<Utc>,
) -> Runner {
    Runner::new(RunnerConfig {
        app_name: APP.into(),
        agent,
        session_service: svc,
        artifact_service: None,
        memory_service: None,
        plugin_config: PluginConfig {
            plugins,
            ..Default::default()
        },
        auto_create_session: false,
        platform: Platform::default().with_time(move || run_time),
        compaction: None,
    })
    .unwrap()
}

// parity: adapted: on_event receives the event by value, so Go's in-place rewrite (`*ev = …; return nil`) only changes the plugin's own copy; the "rewrite" case then stores the agent's original event, which carries no timestamp either
#[tokio::test]
async fn test_runner_run_live_stamps_events_yielded_without_timestamp() {
    let run_time = march(2, 10);
    let own_time = march(1, 9);
    struct Case {
        name: &'static str,
        own: DateTime<Utc>,
        replace: bool,
        rewrite: bool,
        want: DateTime<Utc>,
    }
    let zero = gadk_session::zero_time();
    let cases = [
        Case {
            name: "agent event",
            own: zero,
            replace: false,
            rewrite: false,
            want: run_time,
        },
        Case {
            name: "plugin replacement",
            own: zero,
            replace: true,
            rewrite: false,
            want: run_time,
        },
        Case {
            name: "plugin rewrites in place",
            own: zero,
            replace: false,
            rewrite: true,
            want: run_time,
        },
        Case {
            name: "own timestamp",
            own: own_time,
            replace: false,
            rewrite: false,
            want: own_time,
        },
    ];
    for tc in cases {
        const SESSION_ID: &str = "testSession";
        let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
        create_session(&*svc, SESSION_ID).await;
        let saw_unstamped = Arc::new(AtomicBool::new(false));
        let saw = saw_unstamped.clone();
        let (replace, rewrite) = (tc.replace, tc.rewrite);
        let plugin = Plugin::new(gadk_agent::PluginConfig {
            name: "observe_and_replace".into(),
            on_event_callback: Some(Arc::new(move |_ctx, mut ev: Event| {
                if ev.timestamp == gadk_session::zero_time() {
                    saw.store(true, Ordering::SeqCst);
                }
                let out = if rewrite {
                    ev = Event {
                        author: ev.author.clone(),
                        response: ev.response.clone(),
                        ..Default::default()
                    };
                    let _ = ev;
                    None
                } else if replace {
                    Some(Event {
                        author: ev.author.clone(),
                        response: ev.response.clone(),
                        ..Default::default()
                    })
                } else {
                    None
                };
                Box::pin(async move { Ok(out) })
            })),
            ..Default::default()
        });
        let own = tc.own;
        let sequence: Sequence = Arc::new(move |_ctx: InvocationContext| {
            let mut ev = Event {
                author: "test_agent".into(),
                timestamp: own,
                ..Default::default()
            };
            ev.response.content = Some(Content::from_text("reply", ROLE_MODEL));
            futures::stream::iter(vec![Ok(ev)]).boxed()
        });
        let agent = mock_live_agent(
            "test_agent",
            Arc::new(DummyLiveSession),
            sequence,
            Arc::new(AtomicBool::new(false)),
        );
        let r = new_timed_runner(agent, svc.clone(), vec![plugin], run_time);
        let (_sess, mut stream) = r
            .run_live(
                USER,
                SESSION_ID,
                LiveRunConfig::default(),
                RunOptions::default(),
            )
            .await
            .unwrap_or_else(|e| panic!("{}: RunLive() error = {e}", tc.name));
        while let Some(item) = stream.next().await {
            item.unwrap_or_else(|e| panic!("{}: {e}", tc.name));
        }

        let mut saw_reply = false;
        for ev in get_run_live_test_events(&*svc, SESSION_ID).await {
            if ev.author != "test_agent" {
                continue;
            }
            saw_reply = true;
            assert_eq!(ev.timestamp, tc.want, "{}: stored event timestamp", tc.name);
        }
        assert!(
            saw_reply,
            "{}: no event from the live agent was stored",
            tc.name
        );
        assert!(
            !saw_unstamped.load(Ordering::SeqCst),
            "{}: OnEventCallback saw the live agent's event with a zero timestamp",
            tc.name
        );
    }
}

#[tokio::test]
async fn test_runner_run_live_plugin_replacement_keeps_event_identity() {
    const SESSION_ID: &str = "testSession";
    let run_time = march(2, 10);
    let agent_time = march(1, 9);
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, SESSION_ID).await;
    let original_invocation_id: Arc<Mutex<String>> = Arc::default();
    let seen = original_invocation_id.clone();
    let plugin = Plugin::new(gadk_agent::PluginConfig {
        name: "replace".into(),
        on_event_callback: Some(Arc::new(move |_ctx, ev: Event| {
            *seen.lock() = ev.invocation_id.clone();
            let mut out = Event {
                id: "plugin-id".into(),
                invocation_id: "plugin-invocation".into(),
                timestamp: march(3, 11),
                ..Default::default()
            };
            out.response.content = Some(Content::from_text("replaced", ROLE_MODEL));
            out.response
                .custom_metadata
                .insert("modified".into(), json!(true));
            Box::pin(async move { Ok(Some(out)) })
        })),
        ..Default::default()
    });
    let sequence: Sequence = Arc::new(move |ctx: InvocationContext| {
        let mut ev = Event {
            id: "agent-event".into(),
            invocation_id: ctx.invocation_id().to_string(),
            timestamp: agent_time,
            author: "test_agent".into(),
            ..Default::default()
        };
        ev.response.content = Some(Content::from_text("reply", ROLE_MODEL));
        futures::stream::iter(vec![Ok(ev)]).boxed()
    });
    let agent = mock_live_agent(
        "test_agent",
        Arc::new(DummyLiveSession),
        sequence,
        Arc::new(AtomicBool::new(false)),
    );
    let r = new_timed_runner(agent, svc.clone(), vec![plugin], run_time);
    let (_sess, stream) = r
        .run_live(
            USER,
            SESSION_ID,
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive() error");
    let streamed: Vec<Event> = stream.map(|r| r.unwrap()).collect().await;
    let stored = get_run_live_test_events(&*svc, SESSION_ID).await;
    let original_invocation_id = original_invocation_id.lock().clone();
    assert!(
        !original_invocation_id.is_empty(),
        "the plugin never saw the live agent's event"
    );

    // Go cmp.Diff ignoring Actions: identity from the original, content and metadata from the
    // replacement.
    let check = |where_: &str, evs: &[Event]| {
        assert_eq!(evs.len(), 1, "{where_} events");
        let ev = &evs[0];
        assert_eq!(ev.id, "agent-event", "{where_} ID");
        assert_eq!(
            ev.invocation_id, original_invocation_id,
            "{where_} InvocationID"
        );
        assert_eq!(ev.timestamp, agent_time, "{where_} Timestamp");
        assert_eq!(ev.author, "test_agent", "{where_} Author");
        assert_eq!(
            ev.response.content,
            Some(Content::from_text("replaced", ROLE_MODEL)),
            "{where_} Content"
        );
        assert_eq!(
            ev.response.custom_metadata.get("modified"),
            Some(&json!(true)),
            "{where_} CustomMetadata"
        );
        assert_eq!(
            ev.response.custom_metadata.len(),
            1,
            "{where_} CustomMetadata"
        );
    };
    check("streamed", &streamed);
    check("stored", &stored);
}

#[tokio::test]
async fn test_runner_run_live_early_exit_runs_on_event_and_after_run() {
    const SESSION_ID: &str = "testSession";
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, SESSION_ID).await;

    let cached_reply = Content::from_text("cached answer", ROLE_MODEL);
    let redacted_reply = Content::from_text("redacted answer", ROLE_MODEL);
    let on_event_calls = Arc::new(Mutex::new(0usize));
    let after_run_calls = Arc::new(Mutex::new(0usize));
    let (oe, ar, cached, redacted) = (
        on_event_calls.clone(),
        after_run_calls.clone(),
        cached_reply.clone(),
        redacted_reply.clone(),
    );
    let plugin = Plugin::new(gadk_agent::PluginConfig {
        name: "cache".into(),
        before_run_callback: Some(Arc::new(move |_ctx| {
            let out = Ok(Some(cached.clone()));
            Box::pin(async move { out })
        })),
        on_event_callback: Some(Arc::new(move |_ctx, mut ev: Event| {
            *oe.lock() += 1;
            ev.response.content = Some(redacted.clone());
            Box::pin(async move { Ok(Some(ev)) })
        })),
        after_run_callback: Some(Arc::new(move |_ctx| {
            *ar.lock() += 1;
            Box::pin(async {})
        })),
        ..Default::default()
    });

    let run_live_called = Arc::new(AtomicBool::new(false));
    let sequence: Sequence = Arc::new(|_ctx: InvocationContext| futures::stream::empty().boxed());
    let agent = mock_live_agent(
        "test_agent",
        Arc::new(DummyLiveSession),
        sequence,
        run_live_called.clone(),
    );
    let r = new_runner(agent, svc.clone(), vec![plugin]);
    let (_sess, stream) = r
        .run_live(
            USER,
            SESSION_ID,
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");
    let got: Vec<Event> = stream.map(|r| r.unwrap()).collect().await;

    assert!(
        !run_live_called.load(Ordering::SeqCst),
        "the agent ran although BeforeRun replied"
    );
    assert_eq!(*on_event_calls.lock(), 1, "OnEvent calls");
    assert_eq!(*after_run_calls.lock(), 1, "AfterRun calls");
    assert_eq!(got.len(), 1, "yielded events");
    assert_eq!(
        got[0].response.content.as_ref(),
        Some(&redacted_reply),
        "yielded reply"
    );
    let stored = get_run_live_test_events(&*svc, SESSION_ID).await;
    assert_eq!(stored.len(), 1, "stored events");
    assert_eq!(
        stored[0].response.content.as_ref(),
        Some(&redacted_reply),
        "stored reply"
    );
}

#[tokio::test]
async fn test_runner_run_live_send_rejects_user_function_call() {
    const SESSION_ID: &str = "testSessionUserFunctionCall";
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    create_session(&*svc, SESSION_ID).await;
    let agent_sess = Arc::new(RecordingLiveSession::default());
    let sequence: Sequence = Arc::new(|_ctx: InvocationContext| futures::stream::empty().boxed());
    let agent = mock_live_agent(
        "test_agent",
        agent_sess.clone(),
        sequence,
        Arc::new(AtomicBool::new(false)),
    );
    let r = new_runner(agent, svc.clone(), vec![]);
    let (sess, _stream) = r
        .run_live(
            USER,
            SESSION_ID,
            LiveRunConfig::default(),
            RunOptions::default(),
        )
        .await
        .expect("RunLive failed");

    let content = Content {
        role: ROLE_USER.into(),
        parts: vec![
            Part::from_text("hi"),
            Part {
                function_call: Some(FunctionCall {
                    name: "some_tool".into(),
                    ..Default::default()
                }),
                ..Default::default()
            },
        ],
    };
    let err = sess
        .send(LiveRequest {
            content: Some(content),
            realtime_input: None,
        })
        .await;
    match err {
        Err(e) => assert_eq!(e.to_string(), "user message cannot contain function calls"),
        Ok(()) => panic!("Send() error = nil, want \"user message cannot contain function calls\""),
    }
    assert_eq!(agent_sess.requests.lock().len(), 0, "forwarded requests");
    assert_eq!(
        get_run_live_test_events(&*svc, SESSION_ID).await.len(),
        0,
        "saved events"
    );
}