gadk-engine 0.3.0

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
// 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 tool/tool_test.go.

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

use async_trait::async_trait;
use futures::StreamExt;
use futures::stream::BoxStream;
use gadk_agent::tool_util::append_instructions;
use gadk_agent::{
    AgentConfig, ConfirmationProvider, Context, ContextDelta, ReadonlyContext, RunnableTool,
    StreamingTool, Tool, ToolError, Toolset, new_agent, pack_tool, tool_from_request,
    with_confirmation,
};
use gadk_genai::FunctionDeclaration;
use gadk_model::LlmRequest;
use gadk_session::ToolConfirmation;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

use crate::tool::{AgentTool, FunctionTool, FunctionToolConfig, StreamingFunctionTool};

const FC_ID: &str = "test-function-call-id";

fn test_context(confirmation: Option<ToolConfirmation>) -> Context {
    Context::builder().build().with_delta(ContextDelta {
        function_call_id: Some(FC_ID.to_string()),
        tool_confirmation: Some(confirmation),
        ..Default::default()
    })
}

/// (name, require, provider, confirmation, want_requested, want_skip, want_err, want_ran)
type ConfirmationCase = (
    &'static str,
    bool,
    Option<ConfirmationProvider>,
    Option<ToolConfirmation>,
    bool,
    bool,
    Option<&'static str>,
    bool,
);

fn confirmation_requested(ctx: &Context) -> bool {
    ctx.actions()
        .requested_tool_confirmations
        .contains_key(FC_ID)
}

struct TestToolset {
    tools: Vec<Arc<dyn Tool>>,
}

#[async_trait]
impl Toolset for TestToolset {
    fn name(&self) -> &str {
        "testToolset"
    }
    async fn tools(&self, _ctx: &ReadonlyContext) -> Result<Vec<Arc<dyn Tool>>, ToolError> {
        Ok(self.tools.clone())
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[derive(Deserialize, Serialize, JsonSchema)]
struct Empty {}

// parity: adapted: geminitool and loadartifactstool are out of scope (spec § Non-Goals); Go's FunctionTool/RequestProcessor interface checks map to as_runnable() and declaration() (packing is generic via pack_tool)
#[tokio::test]
async fn test_types() {
    #[derive(Deserialize, Serialize, JsonSchema)]
    struct IntIo {
        value: i64,
    }
    let function_tool =
        FunctionTool::new(
            FunctionToolConfig::new("", ""),
            |_ctx: Context, input: IntIo| async move {
                Ok::<_, ToolError>(IntIo { value: input.value })
            },
        )
        .unwrap();
    let agent_tool = AgentTool::new(
        new_agent(AgentConfig {
            name: "agent".into(),
            ..Default::default()
        })
        .unwrap(),
        None,
    );
    for (name, tool) in [("FunctionTool", function_tool), ("AgentTool", agent_tool)] {
        assert!(
            tool.as_runnable().is_some(),
            "expected {name} to be runnable (Go toolinternal.FunctionTool)"
        );
        assert!(
            tool.declaration().is_some(),
            "expected {name} to be packable (Go toolinternal.RequestProcessor)"
        );
    }
}

#[tokio::test]
async fn test_with_confirmation() {
    let tool_ran = Arc::new(AtomicBool::new(false));
    let tr = tool_ran.clone();
    let noop = FunctionTool::new(
        FunctionToolConfig::new("noOpTool", ""),
        move |_ctx: Context, _in: Empty| {
            tr.store(true, SeqCst);
            async { Ok::<_, ToolError>(Empty {}) }
        },
    )
    .unwrap();
    let ts: Arc<dyn Toolset> = Arc::new(TestToolset { tools: vec![noop] });
    let yes: ConfirmationProvider = Arc::new(|_name: &str, _input: &Value| true);
    let no: ConfirmationProvider = Arc::new(|_name: &str, _input: &Value| false);
    let confirmed = |c: bool| {
        Some(ToolConfirmation {
            confirmed: c,
            ..Default::default()
        })
    };

    let cases: Vec<ConfirmationCase> = vec![
        (
            "confirmation required, no confirmation in context",
            true,
            None,
            None,
            true,
            true,
            Some("requires confirmation"),
            false,
        ),
        (
            "confirmation required, confirmed in context",
            true,
            None,
            confirmed(true),
            false,
            false,
            None,
            true,
        ),
        (
            "confirmation required, rejected in context",
            true,
            None,
            confirmed(false),
            false,
            false,
            Some("call is rejected"),
            false,
        ),
        (
            "confirmation not required",
            false,
            None,
            None,
            false,
            false,
            None,
            true,
        ),
        (
            "confirmation provider requires confirmation",
            false,
            Some(yes.clone()),
            None,
            true,
            true,
            Some("requires confirmation"),
            false,
        ),
        (
            "confirmation provider does not require confirmation",
            false,
            Some(no.clone()),
            None,
            false,
            false,
            None,
            true,
        ),
        (
            "requireConfirmation=true but provider returns false",
            true,
            Some(no.clone()),
            None,
            false,
            false,
            None,
            true,
        ),
    ];
    for (name, require, provider, confirmation, want_requested, want_skip, want_err, want_ran) in
        cases
    {
        tool_ran.store(false, SeqCst);
        let ctx = test_context(confirmation);
        let cts = with_confirmation(ts.clone(), require, provider);
        let tools = cts.tools(ctx.as_readonly()).await.unwrap();
        assert_eq!(tools.len(), 1, "{name}");
        let runnable = tools[0]
            .as_runnable()
            .unwrap_or_else(|| panic!("{name}: tools[0] is not runnable"));
        let res = runnable.run(ctx.clone(), json!({})).await;
        match want_err {
            Some(msg) => {
                let e = res.expect_err(name);
                assert!(
                    e.to_string().contains(msg),
                    "{name}: Run() error msg = {e}, want it to contain {msg:?}"
                );
            }
            None => assert!(res.is_ok(), "{name}: Run() error = {:?}", res.err()),
        }
        assert_eq!(
            ctx.actions().skip_summarization,
            want_skip,
            "{name}: skip_summarization"
        );
        assert_eq!(
            confirmation_requested(&ctx),
            want_requested,
            "{name}: confirmation requested"
        );
        assert_eq!(tool_ran.load(SeqCst), want_ran, "{name}: tool ran");
    }
}

/// Go `mockCustomRequestTool` / `mockCustomRequestPackingTool`: name and description come from the
/// embedded function tool; the declaration is overridden; `process_request` records the call and,
/// for the request tool, appends a system instruction.
struct CustomRequestTool {
    base: Arc<dyn Tool>,
    decl_name: &'static str,
    append_instruction: bool,
    called: Arc<AtomicBool>,
}

#[async_trait]
impl Tool for CustomRequestTool {
    fn name(&self) -> &str {
        self.base.name()
    }
    fn description(&self) -> &str {
        self.base.description()
    }
    fn declaration(&self) -> Option<FunctionDeclaration> {
        Some(FunctionDeclaration {
            name: self.decl_name.into(),
            ..Default::default()
        })
    }
    async fn process_request(&self, _ctx: &Context, req: &mut LlmRequest) -> Result<(), ToolError> {
        self.called.store(true, SeqCst);
        if self.append_instruction {
            append_instructions(req, &["custom tool system instruction"]);
        }
        Ok(())
    }
    fn as_runnable(&self) -> Option<&dyn RunnableTool> {
        Some(self)
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[async_trait]
impl RunnableTool for CustomRequestTool {
    async fn run(&self, _ctx: Context, _args: Value) -> Result<Map<String, Value>, ToolError> {
        Ok(Map::new())
    }
}

fn custom_request_tool(
    name: &'static str,
    append_instruction: bool,
    called: &Arc<AtomicBool>,
) -> Arc<dyn Tool> {
    let base = FunctionTool::new(
        FunctionToolConfig::new(name, ""),
        |_ctx: Context, _in: Empty| async { Ok::<_, ToolError>(Empty {}) },
    )
    .unwrap_or_else(|e| panic!("FunctionTool::new() failed: {e}"));
    Arc::new(CustomRequestTool {
        base,
        decl_name: name,
        append_instruction,
        called: called.clone(),
    })
}

async fn wrapped_tools(inner: Arc<dyn Tool>) -> Vec<Arc<dyn Tool>> {
    let ts: Arc<dyn Toolset> = Arc::new(TestToolset { tools: vec![inner] });
    let tools = with_confirmation(ts, true, None)
        .tools(Context::builder().build().as_readonly())
        .await
        .unwrap_or_else(|e| panic!("cts.Tools() failed: {e}"));
    assert_eq!(
        tools.len(),
        1,
        "cts.Tools() returned {} tools, want 1",
        tools.len()
    );
    tools
}

// parity: adapted: Rust Tool::process_request does extra edits only (packing is pack_tool), so the port asserts the wrapper forwards process_request to the inner tool
#[tokio::test]
async fn test_with_confirmation_process_request() {
    let called = Arc::new(AtomicBool::new(false));
    let tools = wrapped_tools(custom_request_tool("customReqTool", true, &called)).await;

    let mut req = LlmRequest::default();
    pack_tool(&mut req, &tools[0]).unwrap_or_else(|e| panic!("pack_tool() failed: {e}"));
    tools[0]
        .process_request(&test_context(None), &mut req)
        .await
        .unwrap_or_else(|e| panic!("ProcessRequest() failed: {e}"));

    assert!(
        called.load(SeqCst),
        "expected wrapped tool's ProcessRequest to be called, but it was not"
    );
    let si = req
        .config
        .as_ref()
        .and_then(|c| c.system_instruction.as_ref())
        .filter(|si| !si.parts.is_empty())
        .expect("expected SystemInstruction to be set on req.Config, got nil/empty");
    assert_eq!(
        si.parts[0].text, "custom tool system instruction",
        "SystemInstruction text"
    );
}

// parity: adapted: Go's inner tool packs itself from ProcessRequest; in Rust the flow packs with pack_tool, so the port asserts the packed entry is the gated wrapper
#[tokio::test]
async fn test_with_confirmation_process_request_packing() {
    let called = Arc::new(AtomicBool::new(false));
    let tools = wrapped_tools(custom_request_tool("customPackingTool", false, &called)).await;

    let mut req = LlmRequest::default();
    pack_tool(&mut req, &tools[0]).unwrap_or_else(|e| panic!("pack_tool() failed: {e}"));
    tools[0]
        .process_request(&test_context(None), &mut req)
        .await
        .unwrap_or_else(|e| panic!("ProcessRequest() failed: {e}"));

    assert!(
        called.load(SeqCst),
        "expected wrapped tool's ProcessRequest to be called, but it was not"
    );
    let packed =
        tool_from_request(&req, "customPackingTool").expect("req.tools has no customPackingTool");
    assert!(
        Arc::ptr_eq(&packed, &tools[0]),
        "expected req.tools[\"customPackingTool\"] to be the confirmation wrapper"
    );
    let runnable = packed
        .as_runnable()
        .expect("expected packed tool to be runnable");

    let ctx = test_context(None);
    let res = runnable.run(ctx.clone(), json!({})).await;
    assert!(
        matches!(res, Err(ToolError::ConfirmationRequired)),
        "expected Run() to return ErrConfirmationRequired, got {res:?}"
    );
    assert!(
        confirmation_requested(&ctx),
        "expected RequestConfirmation to be called on ctx, but it was not"
    );
}

#[tokio::test]
async fn test_with_confirmation_streaming_tool() {
    let yes: ConfirmationProvider = Arc::new(|_name: &str, _input: &Value| true);
    let no: ConfirmationProvider = Arc::new(|_name: &str, _input: &Value| false);
    let confirmed = |c: bool| {
        Some(ToolConfirmation {
            confirmed: c,
            ..Default::default()
        })
    };

    let cases: Vec<ConfirmationCase> = vec![
        (
            "confirmation required, no confirmation in context",
            true,
            None,
            None,
            true,
            true,
            Some("requires confirmation"),
            false,
        ),
        (
            "confirmation required, confirmed in context",
            true,
            None,
            confirmed(true),
            false,
            false,
            None,
            true,
        ),
        (
            "confirmation required, rejected in context",
            true,
            None,
            confirmed(false),
            false,
            false,
            Some("call is rejected"),
            false,
        ),
        (
            "confirmation not required",
            false,
            None,
            None,
            false,
            false,
            None,
            true,
        ),
        (
            "provider requires confirmation",
            false,
            Some(yes),
            None,
            true,
            true,
            Some("requires confirmation"),
            false,
        ),
        (
            "requireConfirmation=true but provider returns false",
            true,
            Some(no),
            None,
            false,
            false,
            None,
            true,
        ),
    ];
    for (name, require, provider, confirmation, want_requested, want_skip, want_err, want_ran) in
        cases
    {
        let handler_ran = Arc::new(AtomicBool::new(false));
        let ran = handler_ran.clone();
        // The tool itself asks for no confirmation. Only the toolset does, which is the case the
        // wrapper exists to serve.
        let stream_tool = StreamingFunctionTool::new(
            FunctionToolConfig::new("streamTool", ""),
            move |_ctx: Context, _in: Empty| {
                let ran = ran.clone();
                futures::stream::once(async move {
                    ran.store(true, SeqCst);
                    Ok::<_, ToolError>("did the thing".to_string())
                })
            },
        )
        .unwrap_or_else(|e| panic!("{name}: StreamingFunctionTool::new() failed: {e}"));

        let ts: Arc<dyn Toolset> = Arc::new(TestToolset {
            tools: vec![stream_tool],
        });
        let ctx = test_context(confirmation);
        let tools = with_confirmation(ts, require, provider)
            .tools(ctx.as_readonly())
            .await
            .unwrap_or_else(|e| panic!("{name}: cts.Tools() failed: {e}"));
        assert_eq!(
            tools.len(),
            1,
            "{name}: cts.Tools() returned {} tools, want 1",
            tools.len()
        );
        let wrapped = tools[0].as_streaming().unwrap_or_else(|| {
            panic!("{name}: tools[0] is not a StreamingFunctionTool, the flow could not execute it")
        });

        let mut got_err = None;
        let mut s = wrapped.run_stream(ctx.clone(), json!({}));
        while let Some(item) = s.next().await {
            if let Err(e) = item {
                got_err = Some(e);
            }
        }

        match want_err {
            None => assert!(
                got_err.is_none(),
                "{name}: RunStream() error = {got_err:?}, want none"
            ),
            Some(msg) => {
                let e = got_err.unwrap_or_else(|| {
                    panic!("{name}: RunStream() error = nil, want it to contain {msg:?}")
                });
                assert!(
                    e.to_string().contains(msg),
                    "{name}: RunStream() error msg = {e}, want it to contain {msg:?}"
                );
            }
        }
        assert_eq!(handler_ran.load(SeqCst), want_ran, "{name}: handlerRan");
        assert_eq!(
            confirmation_requested(&ctx),
            want_requested,
            "{name}: requestConfirmationCalled"
        );
        assert_eq!(
            ctx.actions().skip_summarization,
            want_skip,
            "{name}: SkipSummarization"
        );
    }
}

/// Go `dualTool`: both the runnable and the streaming shapes. The flow dispatches on the streaming
/// shape first, so the confirmation wrapper has to do the same.
struct DualTool {
    stream_ran: Arc<AtomicBool>,
    run_ran: Arc<AtomicBool>,
}

impl Tool for DualTool {
    fn name(&self) -> &str {
        "dualTool"
    }
    fn description(&self) -> &str {
        ""
    }
    fn declaration(&self) -> Option<FunctionDeclaration> {
        Some(FunctionDeclaration {
            name: self.name().into(),
            ..Default::default()
        })
    }
    fn as_runnable(&self) -> Option<&dyn RunnableTool> {
        Some(self)
    }
    fn as_streaming(&self) -> Option<&dyn StreamingTool> {
        Some(self)
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[async_trait]
impl RunnableTool for DualTool {
    async fn run(&self, _ctx: Context, _args: Value) -> Result<Map<String, Value>, ToolError> {
        self.run_ran.store(true, SeqCst);
        Ok(Map::new())
    }
}

impl StreamingTool for DualTool {
    fn run_stream(
        &self,
        _ctx: Context,
        _args: Value,
    ) -> BoxStream<'static, Result<String, ToolError>> {
        let ran = self.stream_ran.clone();
        futures::stream::once(async move {
            ran.store(true, SeqCst);
            Ok("chunk".to_string())
        })
        .boxed()
    }
}

#[tokio::test]
async fn test_with_confirmation_tool_with_both_run_and_run_stream() {
    let stream_ran = Arc::new(AtomicBool::new(false));
    let run_ran = Arc::new(AtomicBool::new(false));
    let dt: Arc<dyn Tool> = Arc::new(DualTool {
        stream_ran: stream_ran.clone(),
        run_ran: run_ran.clone(),
    });
    let tools = wrapped_tools(dt).await;

    // The flow checks the streaming shape before the runnable one, so the wrapper must present
    // the streaming shape and must not leak an unguarded Run.
    let streaming = tools[0]
        .as_streaming()
        .expect("wrapped tool is not a StreamingFunctionTool");
    assert!(
        tools[0].as_runnable().is_none(),
        "wrapped tool also satisfies FunctionTool, so an unguarded Run is reachable"
    );

    let ctx = test_context(None);
    let mut s = streaming.run_stream(ctx.clone(), json!({}));
    while let Some(item) = s.next().await {
        assert!(
            matches!(item, Err(ToolError::ConfirmationRequired)),
            "RunStream() item = {item:?}, want ErrConfirmationRequired"
        );
    }
    assert!(
        !stream_ran.load(SeqCst) && !run_ran.load(SeqCst),
        "tool executed without confirmation: streamRan={} runRan={}",
        stream_ran.load(SeqCst),
        run_ran.load(SeqCst)
    );
    assert!(
        confirmation_requested(&ctx),
        "requestConfirmationCalled = false, want true"
    );
}

const STREAM_PACKING_TOOL: &str = "streamPackingTool";

/// Go `bareStreamingTool` (no `process_request` of its own) and, with `called` set, Go
/// `selfPackingStreamingTool` (its `process_request` records the call; packing is `pack_tool`'s).
struct PackingStreamingTool {
    stream_ran: Arc<AtomicBool>,
    called: Option<Arc<AtomicBool>>,
}

#[async_trait]
impl Tool for PackingStreamingTool {
    fn name(&self) -> &str {
        STREAM_PACKING_TOOL
    }
    fn description(&self) -> &str {
        ""
    }
    fn declaration(&self) -> Option<FunctionDeclaration> {
        Some(FunctionDeclaration {
            name: STREAM_PACKING_TOOL.into(),
            ..Default::default()
        })
    }
    async fn process_request(
        &self,
        _ctx: &Context,
        _req: &mut LlmRequest,
    ) -> Result<(), ToolError> {
        if let Some(called) = &self.called {
            called.store(true, SeqCst);
        }
        Ok(())
    }
    fn as_streaming(&self) -> Option<&dyn StreamingTool> {
        Some(self)
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl StreamingTool for PackingStreamingTool {
    fn run_stream(
        &self,
        _ctx: Context,
        _args: Value,
    ) -> BoxStream<'static, Result<String, ToolError>> {
        let ran = self.stream_ran.clone();
        futures::stream::once(async move {
            ran.store(true, SeqCst);
            Ok("did the thing".to_string())
        })
        .boxed()
    }
}

// parity: adapted: packing is pack_tool in Rust; the inner tool's process_request is still forwarded
#[tokio::test]
async fn test_with_confirmation_streaming_process_request_packing() {
    for (name, self_packing, want_called) in [
        ("inner tool packs itself", true, true),
        ("inner tool has no ProcessRequest", false, false),
    ] {
        let stream_ran = Arc::new(AtomicBool::new(false));
        let called = Arc::new(AtomicBool::new(false));
        let inner: Arc<dyn Tool> = Arc::new(PackingStreamingTool {
            stream_ran: stream_ran.clone(),
            called: self_packing.then(|| called.clone()),
        });
        let tools = wrapped_tools(inner).await;

        let mut req = LlmRequest::default();
        pack_tool(&mut req, &tools[0])
            .unwrap_or_else(|e| panic!("{name}: pack_tool() failed: {e}"));
        tools[0]
            .process_request(&test_context(None), &mut req)
            .await
            .unwrap_or_else(|e| panic!("{name}: ProcessRequest() failed: {e}"));
        assert_eq!(
            called.load(SeqCst),
            want_called,
            "{name}: inner ProcessRequest called"
        );

        // The flow executes whatever req.tools holds under this name, so an inner tool left there
        // is an ungated tool.
        let packed = tool_from_request(&req, STREAM_PACKING_TOOL)
            .unwrap_or_else(|| panic!("{name}: req.tools has no {STREAM_PACKING_TOOL}"));
        assert!(
            Arc::ptr_eq(&packed, &tools[0]),
            "{name}: req.tools[{STREAM_PACKING_TOOL:?}] is not the confirmation wrapper"
        );
        let streaming = packed
            .as_streaming()
            .unwrap_or_else(|| panic!("{name}: packed tool is not a StreamingFunctionTool, so the flow could not execute it"));
        assert!(
            packed.as_runnable().is_none(),
            "{name}: packed tool also satisfies FunctionTool, so an unguarded Run is reachable"
        );

        let ctx = test_context(None);
        let mut s = streaming.run_stream(ctx.clone(), json!({}));
        while let Some(item) = s.next().await {
            assert!(
                matches!(item, Err(ToolError::ConfirmationRequired)),
                "{name}: RunStream() item = {item:?}, want ErrConfirmationRequired"
            );
        }
        assert!(
            !stream_ran.load(SeqCst),
            "{name}: packed tool ran without confirmation"
        );
        assert!(
            confirmation_requested(&ctx),
            "{name}: requestConfirmationCalled = false, want true"
        );
    }
}