gadk-engine 0.5.3

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
// 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/runner_test.go.

use std::sync::Arc;
use std::time::Duration;

use futures::StreamExt;
use gadk_agent::{Agent, AgentConfig, AgentError, EventStream, InvocationContext, RunConfig};
use gadk_artifact::{ArtifactService, InMemoryArtifactService, ListRequest, LoadRequest};
use gadk_genai::{Blob, Content, FunctionCall, FunctionResponse, Part, ROLE_MODEL, ROLE_USER};
use gadk_model::LlmResponse;
use gadk_platform::Platform;
use gadk_session::{
    CreateRequest, DeleteRequest, Event, GetRequest, InMemorySessionService, Session, SessionError,
    SessionService,
};
use parking_lot::Mutex;
use rstest::rstest;

use crate::llm::{LlmAgentConfig, agent as llm_agent};
use crate::runner::run_node::build_runner_node;
use crate::runner::{PluginConfig, RunOptions, Runner, RunnerConfig};
use crate::testutil::MockModel;
use async_trait::async_trait;

fn noop_run() -> gadk_agent::AgentRunFn {
    Arc::new(|_ctx: InvocationContext| -> EventStream { Box::pin(futures::stream::empty()) })
}

fn custom_agent(name: &str) -> Arc<dyn Agent> {
    gadk_agent::new_agent(AgentConfig {
        name: name.into(),
        run: Some(noop_run()),
        ..Default::default()
    })
    .unwrap()
}

fn llm(cfg: LlmAgentConfig) -> Arc<dyn Agent> {
    llm_agent::new(cfg).unwrap()
}

fn runner_cfg(agent: Arc<dyn Agent>, svc: Arc<dyn SessionService>) -> RunnerConfig {
    RunnerConfig {
        app_name: "testApp".into(),
        agent,
        session_service: svc,
        artifact_service: None,
        memory_service: None,
        plugin_config: PluginConfig::default(),
        auto_create_session: false,
        platform: Platform::default(),
        compaction: None,
    }
}

struct AgentTree {
    root: Arc<dyn Agent>,
    no_transfer: Arc<dyn Agent>,
    allows_transfer: Arc<dyn Agent>,
}

fn agent_tree() -> AgentTree {
    let no_transfer = llm(LlmAgentConfig {
        name: "no_transfer_agent".into(),
        disallow_transfer_to_parent: true,
        ..Default::default()
    });
    let allows_transfer = llm(LlmAgentConfig {
        name: "allows_transfer_agent".into(),
        ..Default::default()
    });
    let root = llm(LlmAgentConfig {
        name: "root".into(),
        sub_agents: vec![no_transfer.clone(), allows_transfer.clone()],
        ..Default::default()
    });
    AgentTree {
        root,
        no_transfer,
        allows_transfer,
    }
}

/// Go createSession: a fresh in-memory service with `events` appended.
async fn create_session(events: Vec<Event>) -> Arc<dyn Session> {
    let svc = InMemorySessionService::new();
    let sess = svc
        .create(CreateRequest {
            app_name: "test".into(),
            user_id: "userID".into(),
            session_id: Some("sessionID".into()),
            state: None,
        })
        .await
        .unwrap();
    for mut ev in events {
        svc.append_event(&*sess, &mut ev).await.unwrap();
    }
    sess
}

fn authored(author: &str) -> Event {
    Event {
        author: author.into(),
        ..Default::default()
    }
}

async fn drain_errors(mut s: EventStream) -> Vec<AgentError> {
    let mut errs = Vec::new();
    while let Some(item) = s.next().await {
        if let Err(e) = item {
            errs.push(e);
        }
    }
    errs
}

/// (name, session events, user message, wanted agent).
type FindCase<'a> = (&'a str, Vec<Event>, Option<Content>, &'a Arc<dyn Agent>);

// parity: adapted: the Runner is built with Runner::new (Go assigns rootAgent on a bare struct)
#[tokio::test]
async fn test_runner_find_agent_to_run() {
    let tree = agent_tree();
    let call_event = Event {
        author: tree.no_transfer.name().into(),
        response: LlmResponse {
            content: Some(Content::from_parts(
                vec![Part {
                    function_call: Some(FunctionCall {
                        name: "fn_name".into(),
                        id: "fn_id".into(),
                        ..Default::default()
                    }),
                    ..Default::default()
                }],
                "",
            )),
            ..Default::default()
        },
        ..Default::default()
    };
    let fr_msg = Content::from_parts(
        vec![Part {
            function_response: Some(FunctionResponse {
                name: "fn_name".into(),
                id: "fn_id".into(),
                ..Default::default()
            }),
            ..Default::default()
        }],
        ROLE_USER,
    );
    let cases: Vec<FindCase<'_>> = vec![
        (
            "last event from agent allowing transfer",
            vec![authored("allows_transfer_agent")],
            None,
            &tree.allows_transfer,
        ),
        (
            "last event from agent not allowing transfer",
            vec![authored("no_transfer_agent")],
            None,
            &tree.root,
        ),
        ("no events from agents, call root", vec![], None, &tree.root),
        (
            "last event from user with function response",
            vec![call_event, authored("root")],
            Some(fr_msg),
            &tree.no_transfer,
        ),
    ];
    let runner = Runner::new(runner_cfg(
        tree.root.clone(),
        Arc::new(InMemorySessionService::new()),
    ))
    .unwrap();
    for (name, events, msg, want) in cases {
        let sess = create_session(events).await;
        let got = runner.find_agent_to_run(&*sess, msg.as_ref()).unwrap();
        assert!(
            Arc::ptr_eq(&got, want),
            "{name}: got {}, want {}",
            got.name(),
            want.name()
        );
    }
}

#[rstest]
#[case::disallow_for_agent_with_disallow_transfer_to_parent(
    llm(LlmAgentConfig { name: "test".into(), disallow_transfer_to_parent: true, ..Default::default() }),
    false
)]
#[case::disallow_for_non_llm_agent(
    gadk_agent::new_agent(AgentConfig { name: "test".into(), ..Default::default() }).unwrap(),
    false
)]
#[case::allow_for_the_default_llm_agent(
    llm(LlmAgentConfig { name: "test".into(), ..Default::default() }),
    true
)]
fn test_is_transferrable_across_agent_tree(#[case] agent: Arc<dyn Agent>, #[case] want: bool) {
    let runner = Runner::new(runner_cfg(
        agent.clone(),
        Arc::new(InMemorySessionService::new()),
    ))
    .unwrap();
    assert_eq!(runner.is_transferable_across_agent_tree(&agent), want);
}

// parity: adapted: the artifact service is passed in RunnerConfig instead of assigned to the private field after New
#[tokio::test]
async fn test_runner_save_input_blobs_as_artifacts() {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let store = Arc::new(InMemoryArtifactService::new());
    let runner = Runner::new(RunnerConfig {
        artifact_service: Some(store.clone()),
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    svc.create(CreateRequest {
        app_name: "testApp".into(),
        user_id: "testUser".into(),
        session_id: Some("testSession".into()),
        state: None,
    })
    .await
    .unwrap();

    // The message is saved only when inline data is present.
    let blob_data = "this is not blob data - René Magritte".as_bytes().to_vec();
    let msg = Content {
        role: ROLE_USER.into(),
        parts: vec![
            Part::from_text("here is a file"),
            Part {
                inline_data: Some(Blob {
                    mime_type: "application/octet-stream".into(),
                    data: blob_data.clone().into(),
                    ..Default::default()
                }),
                ..Default::default()
            },
        ],
    };
    let cfg = RunConfig {
        save_input_blobs_as_artifacts: true,
        ..Default::default()
    };
    let errs = drain_errors(runner.run(
        "testUser",
        "testSession",
        Some(msg),
        cfg,
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "run errors: {errs:?}");

    let list = store
        .list(ListRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: "testSession".into(),
        })
        .await
        .unwrap();
    assert_eq!(list.file_names.len(), 1, "expected 1 artifact");
    let saved = list.file_names[0].clone();
    assert!(saved.starts_with("artifact_"), "saved file name {saved:?}");

    let loaded = store
        .load(LoadRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: "testSession".into(),
            file_name: saved.clone(),
            version: 0,
        })
        .await
        .unwrap();
    let data = loaded.part.unwrap().inline_data.unwrap().data;
    assert_eq!(&data[..], &blob_data[..]);

    let stored = svc
        .get(GetRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: "testSession".into(),
            ..Default::default()
        })
        .await
        .unwrap();
    let events = stored.events();
    assert!(!events.is_empty(), "no events in session");
    let user_event = &events[0];
    assert_eq!(user_event.author, "user");
    let parts = &user_event.response.content.as_ref().unwrap().parts;
    assert_eq!(parts.len(), 2);
    assert!(parts[1].inline_data.is_none(), "InlineData was not removed");
    assert_eq!(
        parts[1].text,
        format!("Uploaded file: {saved}. It has been saved to the artifacts")
    );
}

#[tokio::test]
async fn test_runner_plugin_modifies_user_message() {
    let got_message: Arc<Mutex<Option<Content>>> = Arc::default();
    let seen = got_message.clone();
    let run: gadk_agent::AgentRunFn = Arc::new(move |ctx: InvocationContext| -> EventStream {
        // Accessors that would fail on a callback-only context.
        let _ = ctx.agent().name().to_string();
        let _ = ctx.session().id().to_string();
        *seen.lock() = ctx.user_content().cloned();
        Box::pin(futures::stream::empty())
    });
    let agent = gadk_agent::new_agent(AgentConfig {
        name: "test_agent".into(),
        run: Some(run),
        ..Default::default()
    })
    .unwrap();
    let plugin = gadk_agent::Plugin::new(gadk_agent::PluginConfig {
        name: "message_modifier".into(),
        on_user_message_callback: Some(Arc::new(|_ctx, _msg| {
            Box::pin(async {
                Ok(Some(Content {
                    role: ROLE_USER.into(),
                    parts: vec![Part::from_text("modified")],
                }))
            })
        })),
        ..Default::default()
    });
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let runner = Runner::new(RunnerConfig {
        plugin_config: PluginConfig {
            plugins: vec![plugin],
            close_timeout: Duration::from_secs(1),
        },
        ..runner_cfg(agent, svc.clone())
    })
    .unwrap();
    svc.create(CreateRequest {
        app_name: "testApp".into(),
        user_id: "testUser".into(),
        session_id: Some("testSession".into()),
        state: None,
    })
    .await
    .unwrap();
    let msg = Content {
        role: ROLE_USER.into(),
        parts: vec![Part::from_text("original")],
    };
    let errs = drain_errors(runner.run(
        "testUser",
        "testSession",
        Some(msg),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "run errors: {errs:?}");
    let got = got_message
        .lock()
        .clone()
        .expect("agent did not observe a user message");
    assert_eq!(got.parts.len(), 1);
    assert_eq!(got.parts[0].text, "modified");
}

#[test]
fn test_build_runner_node_all_agent_kinds() {
    let cases: Vec<(&str, Arc<dyn Agent>)> = vec![
        (
            "llm_agent",
            llm(LlmAgentConfig {
                name: "llm_agent".into(),
                model: Some(MockModel::new(vec![])),
                ..Default::default()
            }),
        ),
        ("custom_agent", custom_agent("custom_agent")),
        (
            "agent_with_subagents",
            gadk_agent::new_agent(AgentConfig {
                name: "agent_with_subagents".into(),
                run: Some(noop_run()),
                sub_agents: vec![custom_agent("child")],
                ..Default::default()
            })
            .unwrap(),
        ),
    ];
    for (name, agent) in cases {
        let node = build_runner_node(&agent).unwrap_or_else(|e| panic!("{name}: {e}"));
        assert_eq!(node.name(), agent.name(), "{name}");
    }
}

// parity: skipped TestBuildRunnerNode_NilAgent: Go nil-interface check; build_runner_node takes &Arc<dyn Agent>, which cannot be nil

#[tokio::test]
async fn test_new_in_memory() {
    let runner = Runner::in_memory("testApp", custom_agent("test_agent")).unwrap();
    assert!(runner.has_artifact_service());
    assert!(runner.has_memory_service());
    assert!(runner.auto_create_session());

    // A run must succeed without pre-creating the session (auto-create on).
    let msg = Content::from_parts(vec![Part::from_text("hello")], "");
    let errs = drain_errors(runner.run(
        "testUser",
        "testSession",
        Some(msg),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "run errors: {errs:?}");
    runner
        .session_service()
        .get(GetRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: "testSession".into(),
            ..Default::default()
        })
        .await
        .expect("auto-created session exists");
}

#[rstest]
#[case::auto_create_true_session_missing(true, false, false)]
#[case::auto_create_false_session_missing(false, false, true)]
#[case::auto_create_false_session_exists(false, true, false)]
#[case::auto_create_true_session_exists(true, true, false)]
#[tokio::test]
async fn test_runner_auto_create_session(
    #[case] auto_create: bool,
    #[case] setup_session: bool,
    #[case] want_err: bool,
) {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    if setup_session {
        svc.create(CreateRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: Some("testSession".into()),
            state: None,
        })
        .await
        .unwrap();
    }
    let runner = Runner::new(RunnerConfig {
        auto_create_session: auto_create,
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    let errs = drain_errors(runner.run(
        "testUser",
        "testSession",
        Some(Content::from_text("hello", ROLE_USER)),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert_eq!(!errs.is_empty(), want_err, "errors: {errs:?}");
    if !want_err {
        svc.get(GetRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
            session_id: "testSession".into(),
            ..Default::default()
        })
        .await
        .expect("session exists");
    }
}

// parity: skipped TestRunner_NilEventYieldedTerminatesRun: an EventStream item is Result<Event, AgentError>; an agent cannot yield a nil event

async fn create_test_session(svc: &Arc<dyn SessionService>, app: &str, user: &str, id: &str) {
    svc.create(CreateRequest {
        app_name: app.into(),
        user_id: user.into(),
        session_id: Some(id.into()),
        state: None,
    })
    .await
    .expect("sessionService.Create()");
}

async fn get_test_session(
    svc: &Arc<dyn SessionService>,
    app: &str,
    user: &str,
    id: &str,
) -> Arc<dyn Session> {
    svc.get(GetRequest {
        app_name: app.into(),
        user_id: user.into(),
        session_id: id.into(),
        ..Default::default()
    })
    .await
    .expect("sessionService.Get()")
}

/// Go `recordingModel`: records each request and replies "ok".
fn recording_model() -> Arc<MockModel> {
    MockModel::from_contents(vec![
        Content::from_text("ok", ROLE_MODEL),
        Content::from_text("ok", ROLE_MODEL),
    ])
}

/// A custom agent whose run sets `ran` (Go `func(...) { ran = true }`).
fn flagging_agent(name: &str, ran: Arc<Mutex<bool>>) -> Arc<dyn Agent> {
    let run: gadk_agent::AgentRunFn = Arc::new(move |_ctx: InvocationContext| -> EventStream {
        *ran.lock() = true;
        Box::pin(futures::stream::empty())
    });
    gadk_agent::new_agent(AgentConfig {
        name: name.into(),
        run: Some(run),
        ..Default::default()
    })
    .unwrap()
}

fn replacing_plugin(name: &str, returned: Content) -> gadk_agent::Plugin {
    gadk_agent::Plugin::new(gadk_agent::PluginConfig {
        name: name.into(),
        on_user_message_callback: Some(Arc::new(move |_ctx, _msg| {
            let out = returned.clone();
            Box::pin(async move { Ok(Some(out)) })
        })),
        ..Default::default()
    })
}

#[rstest]
#[case::empty_role("")]
#[case::model_role(ROLE_MODEL)]
#[case::user_role(ROLE_USER)]
#[tokio::test]
async fn test_runner_inbound_message_is_user_turn(#[case] role: &str) {
    let m = recording_model();
    let a = llm(LlmAgentConfig {
        name: "a".into(),
        model: Some(m.clone()),
        ..Default::default()
    });
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let r = Runner::new(RunnerConfig {
        app_name: "app".into(),
        ..runner_cfg(a, svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s").await;

    let msg = Content {
        role: role.into(),
        parts: vec![Part::from_text("hello model")],
    };
    let errs = drain_errors(r.run(
        "u",
        "s",
        Some(msg.clone()),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "r.Run() returned an error: {errs:?}");

    // The caller's message is left alone.
    assert_eq!(msg.role, role, "caller's msg.Role");

    // The model sees the message as a user turn.
    let reqs = m.requests();
    assert_eq!(reqs.len(), 1, "model requests");
    let sent: Vec<String> = reqs[0]
        .contents
        .iter()
        .filter(|c| c.parts.iter().any(|p| p.text == "hello model"))
        .map(|c| c.role.clone())
        .collect();
    assert_eq!(sent, vec![ROLE_USER.to_string()], "roles the model saw");

    // The stored event is a user turn too.
    let got = get_test_session(&svc, "app", "u", "s").await;
    let first = &got.events()[0];
    assert_eq!(first.author, "user");
    assert_eq!(
        first.response.content.as_ref().map(|c| c.role.as_str()),
        Some(ROLE_USER),
        "stored inbound event role"
    );
}

// parity: adapted: Rust's Runner::run takes the message by value, so it can never mutate the caller's struct; the guard checks the caller's copy keeps its blob part
#[tokio::test]
async fn test_runner_save_input_blobs_does_not_mutate_caller_message() {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let r = Runner::new(RunnerConfig {
        app_name: "app".into(),
        artifact_service: Some(Arc::new(InMemoryArtifactService::new())),
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s").await;

    let blob = Part {
        inline_data: Some(Blob {
            mime_type: "application/octet-stream".into(),
            data: b"data".to_vec().into(),
            ..Default::default()
        }),
        ..Default::default()
    };
    let msg = Content {
        role: ROLE_USER.into(),
        parts: vec![Part::from_text("file"), blob.clone()],
    };
    let cfg = RunConfig {
        save_input_blobs_as_artifacts: true,
        ..Default::default()
    };
    let errs = drain_errors(r.run("u", "s", Some(msg.clone()), cfg, RunOptions::default())).await;
    assert!(errs.is_empty(), "r.Run() returned an error: {errs:?}");
    assert_eq!(msg.parts[1], blob, "caller's message was modified");
}

#[tokio::test]
async fn test_runner_inbound_role_normalized_for_context_and_plugin() {
    let got_user_content: Arc<Mutex<Option<Content>>> = Arc::default();
    let seen = got_user_content.clone();
    let run: gadk_agent::AgentRunFn = Arc::new(move |ctx: InvocationContext| -> EventStream {
        *seen.lock() = ctx.user_content().cloned();
        Box::pin(futures::stream::empty())
    });
    let test_agent = gadk_agent::new_agent(AgentConfig {
        name: "test_agent".into(),
        run: Some(run),
        ..Default::default()
    })
    .unwrap();
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let r = Runner::new(RunnerConfig {
        app_name: "app".into(),
        ..runner_cfg(test_agent.clone(), svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s").await;
    let msg = Content {
        role: String::new(),
        parts: vec![Part::from_text("hi")],
    };
    let errs = drain_errors(r.run(
        "u",
        "s",
        Some(msg.clone()),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "r.Run() returned an error: {errs:?}");
    let got = got_user_content.lock().clone();
    assert_eq!(
        got.as_ref().map(|c| c.role.as_str()),
        Some(ROLE_USER),
        "ctx.UserContent() = {got:?}"
    );

    // A plugin that returns a message with no Role still yields a user turn.
    let roleless = replacing_plugin(
        "roleless",
        Content {
            role: String::new(),
            parts: vec![Part::from_text("from plugin")],
        },
    );
    let r2 = Runner::new(RunnerConfig {
        app_name: "app".into(),
        plugin_config: PluginConfig {
            plugins: vec![roleless],
            close_timeout: Duration::from_secs(1),
        },
        ..runner_cfg(test_agent, svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s2").await;
    *got_user_content.lock() = None;
    let errs = drain_errors(r2.run(
        "u",
        "s2",
        Some(msg),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(errs.is_empty(), "r2.Run() returned an error: {errs:?}");
    let got = got_user_content.lock().clone();
    assert!(
        got.as_ref()
            .is_some_and(|c| c.role == ROLE_USER && c.parts[0].text == "from plugin"),
        "ctx.UserContent() with plugin = {got:?}, want the plugin message with Role user"
    );
    let stored = get_test_session(&svc, "app", "u", "s2").await;
    let first = stored.events()[0].response.content.clone();
    assert!(
        first
            .as_ref()
            .is_some_and(|c| c.role == ROLE_USER && c.parts[0].text == "from plugin"),
        "stored event content = {first:?}, want the plugin message with Role user"
    );
}

fn function_call_part(name: &str, args: serde_json::Value) -> Part {
    Part {
        function_call: Some(FunctionCall {
            name: name.into(),
            args: args.as_object().cloned().unwrap_or_default(),
            ..Default::default()
        }),
        ..Default::default()
    }
}

#[rstest]
#[case::legacy_path(false, vec![function_call_part("some_tool", serde_json::json!({"key": "value"}))])]
#[case::node_path(true, vec![function_call_part("some_tool", serde_json::json!({"key": "value"}))])]
#[case::node_path_call_after_text(true, vec![Part::from_text("hi"), function_call_part("some_tool", serde_json::json!({"key": "value"}))])]
#[case::legacy_path_call_after_inline_data(false, vec![png_part(), function_call_part("some_tool", serde_json::json!({"key": "value"}))])]
#[case::node_path_call_after_inline_data(true, vec![png_part(), function_call_part("some_tool", serde_json::json!({"key": "value"}))])]
#[tokio::test]
async fn test_runner_rejects_user_function_call(#[case] llm_root: bool, #[case] parts: Vec<Part>) {
    let ran = Arc::new(Mutex::new(false));
    let m = recording_model();
    let a = if llm_root {
        llm(LlmAgentConfig {
            name: "test_agent".into(),
            model: Some(m.clone()),
            ..Default::default()
        })
    } else {
        flagging_agent("test_agent", ran.clone())
    };
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let store = Arc::new(InMemoryArtifactService::new());
    let r = Runner::new(RunnerConfig {
        app_name: "app".into(),
        artifact_service: Some(store.clone()),
        ..runner_cfg(a, svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s").await;

    let msg = Content {
        role: ROLE_USER.into(),
        parts,
    };
    let cfg = RunConfig {
        save_input_blobs_as_artifacts: true,
        ..Default::default()
    };
    let errs = drain_errors(r.run("u", "s", Some(msg), cfg, RunOptions::default())).await;
    assert_eq!(
        errs.last().map(ToString::to_string).as_deref(),
        Some("user message cannot contain function calls"),
        "r.Run() errors: {errs:?}"
    );
    assert!(
        !*ran.lock() && m.requests().is_empty(),
        "agent ran = {}, model requests = {}, want no agent run and no model request",
        *ran.lock(),
        m.requests().len()
    );
    let got = get_test_session(&svc, "app", "u", "s").await;
    assert_eq!(got.events().len(), 0, "session events");
    let saved = store
        .list(ListRequest {
            app_name: "app".into(),
            user_id: "u".into(),
            session_id: "s".into(),
        })
        .await
        .unwrap();
    assert!(
        saved.file_names.is_empty(),
        "saved artifacts = {:?}",
        saved.file_names
    );
}

fn png_part() -> Part {
    Part {
        inline_data: Some(Blob {
            mime_type: "image/png".into(),
            data: vec![0x89, 0x50].into(),
            ..Default::default()
        }),
        ..Default::default()
    }
}

#[rstest]
#[case::plugin_removes_the_call(vec![function_call_part("some_tool", serde_json::json!({}))], vec![Part::from_text("from plugin")], false)]
#[case::plugin_adds_a_call(vec![Part::from_text("hi")], vec![function_call_part("some_tool", serde_json::json!({}))], true)]
#[tokio::test]
async fn test_runner_user_function_call_checked_after_plugin(
    #[case] sent: Vec<Part>,
    #[case] returned: Vec<Part>,
    #[case] want_err: bool,
) {
    let replacer = replacing_plugin(
        "replacer",
        Content {
            role: ROLE_USER.into(),
            parts: returned,
        },
    );
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let r = Runner::new(RunnerConfig {
        app_name: "app".into(),
        plugin_config: PluginConfig {
            plugins: vec![replacer],
            close_timeout: Duration::from_secs(1),
        },
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    create_test_session(&svc, "app", "u", "s").await;
    let errs = drain_errors(r.run(
        "u",
        "s",
        Some(Content {
            role: ROLE_USER.into(),
            parts: sent,
        }),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert_eq!(!errs.is_empty(), want_err, "r.Run() errors: {errs:?}");
}

// parity: skipped TestRunner_UserMessageWithNilPartRuns: Content.parts is a Vec<Part>, so a user message cannot carry a nil part

/// Go `getErrSessionService`: fails `get` with a chosen error and counts `create` calls.
struct GetErrSessionService {
    inner: Arc<dyn SessionService>,
    get_err: fn() -> SessionError,
    creates: Mutex<usize>,
}

#[async_trait]
impl SessionService for GetErrSessionService {
    async fn create(&self, req: CreateRequest) -> Result<Arc<dyn Session>, SessionError> {
        *self.creates.lock() += 1;
        self.inner.create(req).await
    }
    async fn get(&self, _req: GetRequest) -> Result<Arc<dyn Session>, SessionError> {
        Err((self.get_err)())
    }
    async fn list(
        &self,
        req: gadk_session::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> {
        self.inner.append_event(session, event).await
    }
}

/// Go `errors.Is(err, target)` over the source chain: the [`SessionError`] in `err`'s chain,
/// rendered.
fn session_error_in(err: &AgentError) -> Option<String> {
    let mut cur: Option<&(dyn std::error::Error + 'static)> = Some(err);
    while let Some(e) = cur {
        if let Some(se) = e.downcast_ref::<SessionError>() {
            return Some(se.to_string());
        }
        if let Some(AgentError::Session(se)) = e.downcast_ref::<AgentError>() {
            return Some(se.to_string());
        }
        cur = e.source();
    }
    None
}

fn transient_err() -> SessionError {
    SessionError::Backend("connection refused".into())
}
fn plain_not_found_err() -> SessionError {
    SessionError::Backend("not found".into())
}
fn not_found_err() -> SessionError {
    SessionError::NotFound
}

#[rstest]
#[case::transient_error_is_returned_without_create(true, transient_err as fn() -> SessionError, 0, Some("connection refused"))]
#[case::unwrapped_not_found_is_returned_without_create(true, plain_not_found_err as fn() -> SessionError, 0, Some("not found"))]
#[case::wrapped_not_found_creates_once(true, not_found_err as fn() -> SessionError, 1, None)]
#[case::auto_create_false_returns_not_found(false, not_found_err as fn() -> SessionError, 0, Some("session not found"))]
#[tokio::test]
// parity: adapted: Go's wrapped `fmt.Errorf("%w: %q", session.ErrNotFound, …)` is SessionError::NotFound; a third-party "not found" and a transient failure are SessionError::Backend; errors.Is is a source-chain search for the same SessionError
async fn test_runner_auto_create_session_get_error(
    #[case] auto_create: bool,
    #[case] get_err: fn() -> SessionError,
    #[case] want_creates: usize,
    #[case] want_err: Option<&str>,
) {
    let svc = Arc::new(GetErrSessionService {
        inner: Arc::new(InMemorySessionService::new()),
        get_err,
        creates: Mutex::new(0),
    });
    let r = Runner::new(RunnerConfig {
        auto_create_session: auto_create,
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    let errs = drain_errors(r.run(
        "testUser",
        "testSession",
        Some(Content::from_parts(vec![Part::from_text("hello")], "")),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    let got = errs.last().and_then(session_error_in);
    assert_eq!(got.as_deref(), want_err, "Runner.Run() errors = {errs:?}");
    assert_eq!(*svc.creates.lock(), want_creates, "Create calls");
}

#[tokio::test]
async fn test_runner_auto_create_session_empty_session_id() {
    let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
    let r = Runner::new(RunnerConfig {
        auto_create_session: true,
        ..runner_cfg(custom_agent("test_agent"), svc.clone())
    })
    .unwrap();
    let errs = drain_errors(r.run(
        "testUser",
        "",
        Some(Content::from_parts(vec![Part::from_text("hello")], "")),
        RunConfig::default(),
        RunOptions::default(),
    ))
    .await;
    assert!(
        errs.is_empty(),
        "Runner.Run() with an empty session ID error = {errs:?}"
    );
    let sessions = svc
        .list(gadk_session::ListRequest {
            app_name: "testApp".into(),
            user_id: "testUser".into(),
        })
        .await
        .expect("List()");
    assert_eq!(sessions.len(), 1, "List() sessions");
    assert!(
        !sessions[0].id().is_empty(),
        "created session has an empty ID"
    );
}