strands-harness 0.1.0

A preconfigured Strands agent in one call
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
//! The MCP cases run an in-process `rmcp` server over an in-memory duplex stream instead of a
//! stdio server.

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use serde_json::json;
use strands::agent::DirectToolCallOptions;
use strands::hooks::{AfterToolCallEvent, BeforeToolCallEvent};
use strands::interventions::{InterventionAction, InterventionHandler, InterventionResult, OnError};
use strands::testing::{MockModel, MockTurn, tool_context};
use strands::tools::{FunctionTool, ToolError};
use strands::types::{
    ContentBlock, JsonValue, ToolResultBlock, ToolResultContent, ToolResultStatus, ToolUse, ToolUseBlock,
};
use strands::{Agent, CancelHandle, CancelOptions, InvokeOptions, StopReason, Tool, ToolStreamItem};

use super::{
    DEFAULT_PROGRAMMATIC_TOOL_CALLER_DESCRIPTION, Isolation, LIMITS, Limits, MAX_CONCURRENT_TOOL_CALLS,
    MAX_OUTPUT_CHARS, MakeProgrammaticToolCallerOptions, Output, ProgrammaticToolCaller, ProgrammaticToolCallerConfig,
    make_programmatic_tool_caller, programmatic_tool_caller,
};

mod isolation;

/// The default caller, in-process, so the suite does not depend on a worker binary on `PATH`.
fn default_caller() -> ProgrammaticToolCaller {
    programmatic_tool_caller(ProgrammaticToolCallerConfig::default()).with_limits(LIMITS, Isolation::InProcess, true)
}

/// A caller whose steps run inline, so paused-clock tests see no thread time.
fn inline(caller: ProgrammaticToolCaller) -> ProgrammaticToolCaller {
    caller.with_limits(LIMITS, Isolation::InProcess, false)
}

fn evaluate(expression: &str) -> String {
    let parts: Vec<&str> = expression.split_whitespace().collect();
    let number = |text: &str| text.parse::<i64>().unwrap();
    match parts.as_slice() {
        [value] => number(value).to_string(),
        [a, "+", b] => (number(a) + number(b)).to_string(),
        [a, "*", b] => (number(a) * number(b)).to_string(),
        _ => panic!("unsupported expression {expression}"),
    }
}

fn calculator() -> Arc<dyn Tool> {
    Arc::new(
        FunctionTool::builder("calculator", "Evaluate a simple arithmetic expression.")
            .input_schema(json!({"type": "object", "properties": {"expression": {"type": "string"}}}))
            .callback(|input, _| async move { Ok(evaluate(input["expression"].as_str().unwrap_or_default()).into()) }),
    )
}

fn boom() -> Arc<dyn Tool> {
    Arc::new(FunctionTool::builder("boom", "Always fails.").callback(|_, _| async { Err(ToolError::msg("kaboom")) }))
}

fn info() -> Arc<dyn Tool> {
    Arc::new(
        FunctionTool::builder("info", "Returns structured data.")
            .callback(|_, _| async { Ok(json!({"name": "demo", "tags": ["a", "b"]})) }),
    )
}

fn fetch_url() -> Arc<dyn Tool> {
    Arc::new(
        FunctionTool::builder("fetch-url", "A tool whose name is not a valid identifier.").callback(
            |input, _| async move { Ok(format!("dash:{}", input["value"].as_str().unwrap_or_default()).into()) },
        ),
    )
}

fn slow_value(delay: Duration, active: Arc<AtomicUsize>, peak: Arc<AtomicUsize>) -> Arc<dyn Tool> {
    Arc::new(
        FunctionTool::builder("slow", "Sleep, then return the value.").callback(move |input, _| {
            let active = active.clone();
            let peak = peak.clone();
            async move {
                let now = active.fetch_add(1, Ordering::SeqCst) + 1;
                peak.fetch_max(now, Ordering::SeqCst);
                tokio::time::sleep(delay).await;
                active.fetch_sub(1, Ordering::SeqCst);
                Ok(input["value"].to_string().into())
            }
        }),
    )
}

fn agent_with(caller: impl Tool, tools: Vec<Arc<dyn Tool>>) -> Agent {
    let mut all: Vec<Arc<dyn Tool>> = vec![Arc::new(caller)];
    all.extend(tools);
    Agent::builder()
        .model(MockModel::new())
        .tools(all)
        .printer(false)
        .build()
        .unwrap()
}

async fn run_named(agent: &mut Agent, code: &str, name: &str) -> ToolResultBlock {
    let options = DirectToolCallOptions {
        record_direct_tool_call: Some(false),
    };
    agent
        .tool(name)
        .invoke_with(json!({"code": code}), options)
        .await
        .unwrap()
}

async fn run(agent: &mut Agent, code: &str) -> ToolResultBlock {
    run_named(agent, code, "programmatic_tool_caller").await
}

fn text(result: &ToolResultBlock) -> String {
    result
        .content
        .iter()
        .map(|block| match block {
            ToolResultContent::Text(text) => text.text.clone(),
            _ => String::new(),
        })
        .collect::<Vec<_>>()
        .join("\n")
}

async fn stream_result(tool: &dyn Tool, context: strands::ToolContext) -> ToolResultBlock {
    use futures::StreamExt;
    let mut stream = tool.stream(context);
    while let Some(item) = stream.next().await {
        if let ToolStreamItem::Outcome(outcome) = item {
            return outcome.result().unwrap().clone();
        }
    }
    panic!("no outcome")
}

#[tokio::test]
async fn runs_a_single_tool_call() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, r#"print(await calculator(expression="10 * 2"))"#).await;
    assert_eq!(result.status, ToolResultStatus::Success);
    assert_eq!(text(&result), "20");
}

#[tokio::test]
async fn loops_over_tool_calls() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(
        &mut agent,
        "for i in range(3):\n    print(await calculator(expression=f'{i} + 10'))",
    )
    .await;
    assert_eq!(text(&result), "10\n11\n12");
}

#[tokio::test(start_paused = true)]
async fn runs_tool_calls_concurrently_with_asyncio_gather() {
    let slow = slow_value(Duration::from_millis(300), Arc::default(), Arc::default());
    let mut agent = agent_with(inline(default_caller()), vec![slow]);
    let start = tokio::time::Instant::now();
    let result = run(
        &mut agent,
        "import asyncio\nprint(await asyncio.gather(*[slow(value=i) for i in range(4)]))",
    )
    .await;
    assert_eq!(text(&result), "['0', '1', '2', '3']");
    assert!(start.elapsed() < Duration::from_millis(1100));
}

#[tokio::test(start_paused = true)]
async fn bounds_how_many_inner_calls_run_at_once() {
    let peak = Arc::new(AtomicUsize::new(0));
    let slow = slow_value(Duration::from_millis(50), Arc::default(), peak.clone());
    let mut agent = agent_with(inline(default_caller()), vec![slow]);
    let count = MAX_CONCURRENT_TOOL_CALLS * 3;
    let code = format!("import asyncio\nprint(len(await asyncio.gather(*[slow(value=i) for i in range({count})])))");
    let result = run(&mut agent, &code).await;
    assert_eq!(text(&result), count.to_string());
    let peak = peak.load(Ordering::SeqCst);
    assert!(peak > 1);
    assert!(peak <= MAX_CONCURRENT_TOOL_CALLS);
}

#[tokio::test]
async fn returns_only_printed_output() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(
        &mut agent,
        "x = await calculator(expression=\"6 * 7\")\nprint(\"done\")",
    )
    .await;
    assert_eq!(text(&result), "done");
}

#[tokio::test]
async fn reports_no_output_for_code_that_prints_nothing() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "# nothing here").await;
    assert_eq!(result.status, ToolResultStatus::Success);
    assert_eq!(text(&result), "(no output)");
}

#[tokio::test]
async fn rejects_positional_arguments_with_a_hint() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "print(await calculator(\"1 + 1\"))").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("keyword arguments only"));
    assert!(text(&result).contains("calculator(key=value)"));
}

#[tokio::test]
async fn parses_a_json_text_result_and_leaves_other_text_alone() {
    let json_text: Arc<dyn Tool> = Arc::new(
        FunctionTool::builder("json_text", "Returns JSON as text.")
            .callback(|_, _| async { Ok(r#"{"a": [1, 2]}"#.into()) }),
    );
    let mut agent = agent_with(default_caller(), vec![json_text, calculator()]);
    let code = "r = await json_text()\nprint(type(r).__name__, r[\"a\"][1])\ns = await calculator(expression=\"1 + 1\")\nprint(type(s).__name__, s)";
    assert_eq!(text(&run(&mut agent, code).await), "dict 2\nstr 2");
}

#[tokio::test]
async fn hands_structured_results_to_the_code_as_data() {
    let mut agent = agent_with(default_caller(), vec![info()]);
    let result = run(&mut agent, "r = await info()\nprint(type(r).__name__, r[\"tags\"][1])").await;
    assert_eq!(text(&result), "dict b");
}

#[tokio::test]
async fn keeps_the_host_unreachable() {
    let cases = [
        ("import os\nprint(os.listdir('/'))", "PermissionError"),
        ("print(open('/etc/passwd').read())", "PermissionError"),
        ("import subprocess", "ModuleNotFoundError"),
        ("import socket", "ModuleNotFoundError"),
        ("print(().__class__.__bases__)", "AttributeError"),
        ("print(__builtins__)", "NameError"),
    ];
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    for (code, expected) in cases {
        let result = run(&mut agent, code).await;
        assert_eq!(result.status, ToolResultStatus::Error, "{code}");
        assert!(text(&result).contains(expected), "{code}: {}", text(&result));
    }
}

#[tokio::test]
async fn bounds_a_synchronous_busy_loop_via_the_vm_duration_limit() {
    let limits = Limits {
        max_duration: Duration::from_millis(300),
        ..LIMITS
    };
    let mut agent = agent_with(
        default_caller().with_limits(limits, Isolation::InProcess, true),
        vec![calculator()],
    );
    let start = std::time::Instant::now();
    let result = run(&mut agent, "while True:\n    pass").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("TimeoutError"), "{}", text(&result));
    assert!(start.elapsed() < Duration::from_secs(5));
}

#[tokio::test]
async fn enforces_the_memory_limit() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "x = 'a' * 10**9\nprint(len(x))").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("MemoryError"), "{}", text(&result));
}

#[tokio::test]
async fn surfaces_a_tool_error_as_a_catchable_runtime_error() {
    let mut agent = agent_with(default_caller(), vec![boom()]);
    let result = run(
        &mut agent,
        "try:\n    await boom()\nexcept RuntimeError as e:\n    print('caught:', e)",
    )
    .await;
    assert_eq!(result.status, ToolResultStatus::Success);
    let text = text(&result);
    assert!(text.starts_with("caught: Tool 'boom' error: "), "{text}");
    assert!(text.contains("kaboom"));
}

#[tokio::test]
async fn keeps_output_printed_before_an_error() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "print('step 1')\nraise ValueError('late')").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    let text = text(&result);
    assert!(
        text.starts_with("Error: step 1\n\nExecution error:\nTraceback"),
        "{text}"
    );
    assert!(text.contains("ValueError: late"));
}

#[tokio::test]
async fn reports_a_syntax_error() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "def broken(:\n    pass").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("Syntax error:"));
    assert!(text(&result).contains("SyntaxError"), "{}", text(&result));
}

#[tokio::test]
async fn reports_a_runtime_error_with_the_user_line_number() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "x = 1\ny = 2\nz = {}['missing']").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("line 3"));
    assert!(text(&result).contains("KeyError"));
}

#[tokio::test]
async fn does_not_expose_itself_to_the_code() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "programmatic_tool_caller").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("NameError"));
}

#[tokio::test]
async fn filters_exposed_tools_with_allowed_tools() {
    let restricted = make_programmatic_tool_caller(
        MakeProgrammaticToolCallerOptions::builder()
            .allowed_tools(vec!["calculator".to_owned()])
            .name("run_code")
            .isolation(Isolation::InProcess)
            .build(),
    );
    let mut agent = agent_with(restricted, vec![calculator(), boom()]);
    let result = run_named(&mut agent, "print(await calculator(expression=\"1 + 1\"))", "run_code").await;
    assert_eq!(text(&result), "2");
    let result = run_named(&mut agent, "await boom()", "run_code").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("NameError"));
}

#[test]
fn states_the_contract_in_the_description() {
    for phrase in ["await", "print()", "RuntimeError", "keyword arguments", "fetch_url"] {
        assert!(
            DEFAULT_PROGRAMMATIC_TOOL_CALLER_DESCRIPTION.contains(phrase),
            "{phrase}"
        );
    }
}

#[tokio::test]
async fn never_exposes_another_programmatic_tool_caller() {
    let other = make_programmatic_tool_caller(
        MakeProgrammaticToolCallerOptions::builder()
            .name("run_code")
            .allowed_tools(vec!["programmatic_tool_caller".to_owned(), "calculator".to_owned()])
            .isolation(Isolation::InProcess)
            .build(),
    );
    let mut agent = agent_with(default_caller(), vec![Arc::new(other), calculator()]);
    for (caller, target) in [
        ("run_code", "programmatic_tool_caller"),
        ("programmatic_tool_caller", "run_code"),
    ] {
        let result = run_named(&mut agent, &format!("await {target}(code='print(1)')"), caller).await;
        assert_eq!(result.status, ToolResultStatus::Error);
        assert!(text(&result).contains("NameError"), "{}", text(&result));
    }
    let result = run_named(&mut agent, "print(await calculator(expression=\"1 + 1\"))", "run_code").await;
    assert_eq!(text(&result), "2");
}

#[tokio::test]
async fn calls_a_hyphenated_tool_via_its_identifier_alias() {
    let mut agent = agent_with(default_caller(), vec![fetch_url()]);
    assert_eq!(
        text(&run(&mut agent, "print(await fetch_url(value=\"x\"))").await),
        "dash:x"
    );
}

fn assert_truncated(text: &str) {
    let marker = text.rsplit_once("\n[output truncated at ").map(|(_, rest)| rest);
    let digits = marker.and_then(|rest| rest.strip_suffix(" characters]"));
    assert!(
        digits.is_some_and(|digits| digits.parse::<usize>().is_ok()),
        "{}",
        &text[text.len().saturating_sub(80)..]
    );
}

#[tokio::test]
async fn truncates_an_error_message_that_is_too_long() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "print('before')\nraise ValueError('x' * 600000)").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    let text = text(&result);
    assert!(text.starts_with("Error: before\n\nExecution error:"));
    assert_truncated(&text);
    assert!(text.chars().count() < 200_100);
}

#[tokio::test]
async fn shares_the_cap_between_printed_output_and_the_error() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, "print('p' * 210000)\nraise ValueError('e' * 210000)").await;
    let text = text(&result);
    assert!(text.starts_with("Error: ppp"));
    assert!(text.contains("ValueError: eee"));
    assert!(text.chars().count() < 200_100);
}

#[tokio::test]
async fn truncates_a_syntax_error_that_is_too_long() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let result = run(&mut agent, &format!("x = '{}' +", "A".repeat(400_000))).await;
    let text = text(&result);
    assert!(
        text.starts_with("Error: Syntax error:"),
        "{}",
        &text[..80.min(text.len())]
    );
    assert!(text.chars().count() < 200_100);
}

#[test]
fn does_not_buffer_an_oversized_print_in_full() {
    let output = Output::default();
    output.write(&"a".repeat(MAX_OUTPUT_CHARS * 5));
    output.write(&"b".repeat(100));
    assert!(output.raw().chars().count() <= 200_001);
    assert!(output.text().ends_with("[output truncated at 200000 characters]"));
}

#[tokio::test]
async fn truncates_output_that_is_too_long() {
    let mut agent = agent_with(default_caller(), vec![calculator()]);
    let text = text(&run(&mut agent, "print('x' * 200500)").await);
    assert!(text.ends_with("[output truncated at 200000 characters]"));
    assert!(text.chars().count() < 200_100);
}

#[tokio::test(start_paused = true)]
async fn enforces_the_timeout() {
    let slow: Arc<dyn Tool> = Arc::new(
        FunctionTool::builder("slow", "Block longer than the timeout.").callback(|_, _| async {
            tokio::time::sleep(Duration::from_secs(5)).await;
            Ok("late".into())
        }),
    );
    let quick = make_programmatic_tool_caller(
        MakeProgrammaticToolCallerOptions::builder()
            .name("quick")
            .timeout(Some(Duration::from_millis(300)))
            .isolation(Isolation::InProcess)
            .build(),
    );
    let mut agent = agent_with(inline(quick), vec![slow]);
    let result = run_named(&mut agent, "print('start')\nawait slow()", "quick").await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert_eq!(
        text(&result),
        "Error: start\n\nExecution error: timed out after 0.3 seconds."
    );
}

#[tokio::test]
async fn honors_the_parent_cancel_signal() {
    let context = tool_context(ToolUse::new(
        "programmatic_tool_caller",
        "x",
        json!({"code": "await calculator(expression=\"1\")"}),
    ));
    context.agent().cancel(CancelOptions::default());
    let result = stream_result(&default_caller(), context).await;
    assert_eq!(result.status, ToolResultStatus::Error);
    assert!(text(&result).contains("Execution cancelled."));
}

#[tokio::test(start_paused = true)]
async fn cancels_an_in_flight_run_and_makes_no_further_inner_tool_calls() {
    let calls = Arc::new(AtomicUsize::new(0));
    let cancel = CancelHandle::new();
    let counter = calls.clone();
    let trigger = cancel.clone();
    let bump: Arc<dyn Tool> = Arc::new(FunctionTool::builder("bump", "Records a call.").callback(move |_, _| {
        let counter = counter.clone();
        let trigger = trigger.clone();
        async move {
            if counter.fetch_add(1, Ordering::SeqCst) + 1 == 3 {
                trigger.cancel(CancelOptions::default());
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
            Ok("ok".into())
        }
    }));
    let model = MockModel::new()
        .with_turn(MockTurn::content([ContentBlock::ToolUse(ToolUseBlock::new(
            "programmatic_tool_caller",
            "ptc-1",
            json!({"code": "while True:\n    await bump()"}),
        ))]))
        .with_turn(MockTurn::text("done"));
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(inline(default_caller())), bump];
    let mut agent = Agent::builder()
        .model(model)
        .tools(tools)
        .printer(false)
        .build()
        .unwrap();
    let seen = Arc::new(Calls::default());
    let record = seen.clone();
    agent.add_hook(move |event: &mut AfterToolCallEvent| {
        if event.tool_use().name == "programmatic_tool_caller" {
            record.0.lock().unwrap().push(text(event.result()));
        }
    });
    let start = tokio::time::Instant::now();
    let options = InvokeOptions {
        cancel_signal: Some(cancel.signal()),
        ..InvokeOptions::default()
    };
    let _ = agent.invoke_with("go", options).await;
    assert!(start.elapsed() < Duration::from_secs(2));
    let results = seen.take();
    assert_eq!(results.len(), 1);
    assert!(results[0].contains("Execution cancelled."), "{results:?}");
    tokio::time::sleep(Duration::from_millis(300)).await;
    assert_eq!(calls.load(Ordering::SeqCst), 3);
}

#[test]
fn parses_the_portable_config() {
    let config: ProgrammaticToolCallerConfig =
        serde_json::from_value(json!({"allowedTools": ["read"], "timeout": 30})).unwrap();
    assert_eq!(config.allowed_tools, Some(vec!["read".to_owned()]));
    assert_eq!(config.timeout, Some(Some(30.0)));
    let unbounded: ProgrammaticToolCallerConfig = serde_json::from_value(json!({"timeout": null})).unwrap();
    assert_eq!(unbounded.timeout, Some(None));
    assert_eq!(serde_json::to_value(&unbounded).unwrap(), json!({"timeout": null}));
    assert_eq!(
        serde_json::to_value(ProgrammaticToolCallerConfig::default()).unwrap(),
        json!({})
    );
    assert!(serde_json::from_value::<ProgrammaticToolCallerConfig>(json!({"bogus": 1})).is_err());
    assert_eq!(
        programmatic_tool_caller(unbounded).inner.timeout,
        None,
        "null timeout disables the bound"
    );
    assert_eq!(default_caller().inner.timeout, Some(Duration::from_secs(900)));
    assert_eq!(
        programmatic_tool_caller(config).inner.timeout,
        Some(Duration::from_secs(30))
    );
}

#[test]
fn matches_the_ts_tool_spec() {
    let spec = serde_json::to_value(default_caller().tool_spec()).unwrap();
    let expected = json!({
        "name": "programmatic_tool_caller",
        "description": DEFAULT_PROGRAMMATIC_TOOL_CALLER_DESCRIPTION,
        "inputSchema": {
            "type": "object",
            "properties": {"code": {"type": "string", "description": "Python code to execute. Use `await tool_name(...)` to call tools."}},
            "required": ["code"],
            "additionalProperties": false
        }
    });
    assert_eq!(spec, expected);
    assert_eq!(
        serde_json::to_string(&spec["inputSchema"]).unwrap(),
        r#"{"type":"object","properties":{"code":{"type":"string","description":"Python code to execute. Use `await tool_name(...)` to call tools."}},"required":["code"],"additionalProperties":false}"#
    );
}

// --- inner calls run through the agent hooks ------------------------------------------------

#[derive(Default)]
struct Calls(std::sync::Mutex<Vec<String>>);

impl Calls {
    fn take(&self) -> Vec<String> {
        std::mem::take(&mut *self.0.lock().unwrap())
    }
}

fn shell_like(calls: Arc<Calls>) -> Arc<dyn Tool> {
    Arc::new(
        FunctionTool::builder("shell_like", "Records that it ran.").callback(move |input, _| {
            let calls = calls.clone();
            async move {
                let command = input["command"].as_str().unwrap_or_default().to_owned();
                calls.0.lock().unwrap().push(command.clone());
                Ok(format!("ran:{command}").into())
            }
        }),
    )
}

struct Deny;

#[strands::async_trait]
impl InterventionHandler for Deny {
    fn name(&self) -> &str {
        "deny-shell"
    }

    async fn before_tool_call(&self, event: &mut BeforeToolCallEvent) -> InterventionResult<BeforeToolCallEvent> {
        Ok(if event.tool_use().name == "shell_like" {
            InterventionAction::deny("shell is off")
        } else {
            InterventionAction::proceed()
        })
    }
}

struct Confirm {
    response: Option<JsonValue>,
}

#[strands::async_trait]
impl InterventionHandler for Confirm {
    fn name(&self) -> &str {
        "ask"
    }

    fn on_error(&self) -> OnError {
        OnError::Proceed
    }

    async fn before_tool_call(&self, event: &mut BeforeToolCallEvent) -> InterventionResult<BeforeToolCallEvent> {
        if event.tool_use().name != "shell_like" {
            return Ok(InterventionAction::proceed());
        }
        let mut confirm = strands::interventions::Confirm::new("Run shell?");
        if let Some(response) = &self.response {
            confirm = confirm.with_response(response.clone());
        }
        Ok(InterventionAction::Confirm(confirm))
    }
}

struct Rewrite;

#[strands::async_trait]
impl InterventionHandler for Rewrite {
    fn name(&self) -> &str {
        "rewrite"
    }

    async fn before_tool_call(&self, _event: &mut BeforeToolCallEvent) -> InterventionResult<BeforeToolCallEvent> {
        Ok(InterventionAction::transform(|event: &mut BeforeToolCallEvent| {
            event.tool_use_mut().input = json!({"command": "safe"});
        }))
    }
}

const GATED_CODE: &str =
    "try:\n    print(await shell_like(command='rm -rf /'))\nexcept RuntimeError as e:\n    print('ERR', e)";

fn gated_agent(calls: &Arc<Calls>, handler: impl InterventionHandler) -> Agent {
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(default_caller()), shell_like(calls.clone())];
    Agent::builder()
        .model(MockModel::new())
        .tools(tools)
        .interventions([handler])
        .printer(false)
        .build()
        .unwrap()
}

#[tokio::test]
async fn deny_blocks_the_inner_call_and_the_tool_never_runs() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(&calls, Deny);
    let result = run(&mut agent, GATED_CODE).await;
    assert_eq!(result.status, ToolResultStatus::Success);
    assert!(text(&result).contains("shell is off"), "{}", text(&result));
    assert!(calls.take().is_empty());
}

#[tokio::test]
async fn confirm_refuses_the_inner_call_no_prompt_possible_even_with_on_error_proceed() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(&calls, Confirm { response: None });
    let result = run(&mut agent, GATED_CODE).await;
    assert!(text(&result).contains("approval"), "{}", text(&result));
    assert!(calls.take().is_empty());
}

#[tokio::test]
async fn transform_rewrites_the_inner_call_input() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(&calls, Rewrite);
    let result = run(&mut agent, GATED_CODE).await;
    assert_eq!(text(&result), "ran:safe");
    assert_eq!(calls.take(), vec!["safe"]);
}

#[tokio::test]
async fn accepts_an_approval_the_handler_already_collected() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(
        &calls,
        Confirm {
            response: Some(json!(true)),
        },
    );
    let result = run(&mut agent, GATED_CODE).await;
    assert_eq!(text(&result), "ran:rm -rf /");
    assert_eq!(calls.take(), vec!["rm -rf /"]);
}

#[tokio::test]
async fn fires_after_tool_call_for_a_denied_inner_call_with_the_error_result() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(&calls, Deny);
    let seen = Arc::new(Calls::default());
    let record = seen.clone();
    agent.add_hook(move |event: &mut AfterToolCallEvent| {
        let status = match event.result().status {
            ToolResultStatus::Error => "error",
            _ => "success",
        };
        record
            .0
            .lock()
            .unwrap()
            .push(format!("{}:{status}", event.tool_use().name));
    });
    run(&mut agent, GATED_CODE).await;
    assert_eq!(seen.take(), vec!["shell_like:error"]);
}

#[tokio::test]
async fn fires_after_tool_call_for_a_refused_inner_approval_with_the_refusal() {
    let calls = Arc::new(Calls::default());
    let mut agent = gated_agent(&calls, Confirm { response: None });
    let seen = Arc::new(Calls::default());
    let record = seen.clone();
    agent.add_hook(move |event: &mut AfterToolCallEvent| {
        let refusal = event
            .result()
            .content
            .iter()
            .filter_map(|block| match block {
                ToolResultContent::Text(text) => Some(text.text.clone()),
                _ => None,
            })
            .collect::<String>();
        record.0.lock().unwrap().push(refusal);
        let mut result = event.result().clone();
        result.content = vec![ToolResultContent::text("not without a human")];
        event.set_result(result);
    });
    let result = run(&mut agent, GATED_CODE).await;
    assert_eq!(
        seen.take(),
        vec![
            "Tool 'shell_like' needs approval (Run shell?), which programmatic_tool_caller cannot prompt for; call it as a normal tool call instead."
        ]
    );
    assert_eq!(
        text(&result),
        "ERR Failed to execute tool 'shell_like': not without a human"
    );
    assert!(calls.take().is_empty());
}

#[tokio::test]
async fn plain_before_after_hooks_observe_inner_calls_and_can_replace_the_result() {
    let calls = Arc::new(Calls::default());
    let mut agent = agent_with(default_caller(), vec![shell_like(calls)]);
    let seen = Arc::new(Calls::default());
    let before = seen.clone();
    agent.add_hook(move |event: &mut BeforeToolCallEvent| {
        before
            .0
            .lock()
            .unwrap()
            .push(format!("before:{}", event.tool_use().name));
    });
    let after = seen.clone();
    agent.add_hook(move |event: &mut AfterToolCallEvent| {
        after.0.lock().unwrap().push(format!("after:{}", event.tool_use().name));
        let id = event.tool_use().tool_use_id.clone();
        event.set_result(ToolResultBlock::success(id, vec![ToolResultContent::text("replaced")]));
    });
    let result = run(&mut agent, "print(await shell_like(command='ls'))").await;
    assert_eq!(text(&result), "replaced");
    assert_eq!(seen.take(), vec!["before:shell_like", "after:shell_like"]);
}

/// Inside the agent loop an inner approval is refused like a direct call's: the outer call
/// completes without an interrupt, so nothing runs again.
#[tokio::test]
async fn an_inner_approval_completes_the_outer_call_in_the_agent_loop() {
    let calls = Arc::new(Calls::default());
    let notes = Arc::new(AtomicUsize::new(0));
    let counter = notes.clone();
    let note: Arc<dyn Tool> = Arc::new(
        FunctionTool::builder("write_note", "Side effect.").callback(move |_, _| {
            let counter = counter.clone();
            async move {
                counter.fetch_add(1, Ordering::SeqCst);
                Ok("noted".into())
            }
        }),
    );
    let code = format!("print(await write_note(x=1))\n{GATED_CODE}");
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(default_caller()), shell_like(calls.clone()), note];
    let model = MockModel::new()
        .with_turn(MockTurn::content([ContentBlock::ToolUse(ToolUseBlock::new(
            "programmatic_tool_caller",
            "ptc-1",
            json!({"code": code}),
        ))]))
        .with_turn(MockTurn::text("done"));
    let mut agent = Agent::builder()
        .model(model)
        .tools(tools)
        .interventions([Confirm { response: None }])
        .printer(false)
        .build()
        .unwrap();
    let seen = Arc::new(Calls::default());
    let record = seen.clone();
    agent.add_hook(move |event: &mut AfterToolCallEvent| {
        if event.tool_use().name == "programmatic_tool_caller" {
            record.0.lock().unwrap().push(text(event.result()));
        }
    });
    let result = agent.invoke("go").await.unwrap();
    assert_eq!(result.stop_reason(), StopReason::EndTurn);
    assert!(result.interrupts().is_empty());
    assert_eq!(
        seen.take(),
        vec![
            "noted\nERR Failed to execute tool 'shell_like': Tool 'shell_like' needs approval (Run shell?), which programmatic_tool_caller cannot prompt for; call it as a normal tool call instead."
        ]
    );
    assert_eq!(notes.load(Ordering::SeqCst), 1);
    assert!(calls.take().is_empty());
}

#[tokio::test]
async fn refuses_an_interrupt_raised_by_the_inner_tool_itself() {
    let asks: Arc<dyn Tool> = Arc::new(FunctionTool::builder("asks", "Asks a human.").callback(
        |_, context| async move {
            context.interrupt(strands::types::InterruptParams::new("approve"))?;
            Ok("approved".into())
        },
    ));
    let mut agent = agent_with(default_caller(), vec![asks]);
    let result = run(
        &mut agent,
        "try:\n    await asks()\nexcept RuntimeError as e:\n    print('ERR', e)",
    )
    .await;
    assert_eq!(
        text(&result),
        "ERR Failed to execute tool 'asks': Interrupts are not supported from programmatic_tool_caller."
    );
}

// --- programmatic_tool_caller over MCP ------------------------------------------------------

#[cfg(feature = "mcp")]
mod mcp {
    use std::sync::Arc;

    use rmcp::model::{
        CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, ErrorData, ListToolsResult,
        PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool as McpServerTool,
    };
    use rmcp::service::{RequestContext, RoleServer};
    use rmcp::{ServerHandler, ServiceExt};
    use serde_json::{Value, json};
    use strands::error::BoxError;
    use strands::mcp::{McpClient, McpClientConfig, McpTransport};
    use strands::{Agent, Tool};
    use tokio::io::DuplexStream;

    use super::{default_caller, run, text};
    use strands::types::ToolResultStatus;

    #[derive(Clone)]
    struct PtcServer;

    fn schema(properties: Value) -> Arc<rmcp::model::JsonObject> {
        let mut map = rmcp::model::JsonObject::new();
        map.insert("type".to_owned(), json!("object"));
        map.insert("properties".to_owned(), properties);
        Arc::new(map)
    }

    fn reply(text: String) -> CallToolResult {
        CallToolResult::success(vec![ContentBlock::text(text)])
    }

    impl ServerHandler for PtcServer {
        fn get_info(&self) -> ServerConfig {
            ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
        }

        async fn list_tools(
            &self,
            _request: Option<PaginatedRequestParams>,
            _context: RequestContext<RoleServer>,
        ) -> Result<ListToolsResult, ErrorData> {
            Ok(ListToolsResult::with_all_items(vec![
                McpServerTool::new(
                    "ptc_echo",
                    "Echoes the given text back",
                    schema(json!({"text": {"type": "string"}})),
                ),
                McpServerTool::new(
                    "ptc_add",
                    "Adds two integers",
                    schema(json!({"a": {"type": "number"}, "b": {"type": "number"}})),
                ),
                McpServerTool::new(
                    "ptc-dash",
                    "Tool whose name contains a hyphen",
                    schema(json!({"value": {"type": "string"}})),
                ),
                McpServerTool::new("ptc_boom", "Always raises", schema(json!({}))),
            ]))
        }

        async fn call_tool(
            &self,
            request: CallToolRequestParams,
            _context: RequestContext<RoleServer>,
        ) -> Result<CallToolResponse, ErrorData> {
            let args = request.arguments.unwrap_or_default();
            let string = |key: &str| args.get(key).and_then(Value::as_str).unwrap_or_default().to_owned();
            let number = |key: &str| args.get(key).and_then(Value::as_i64).unwrap_or_default();
            let result = match &*request.name {
                "ptc_echo" => reply(format!("echo:{}", string("text"))),
                "ptc_add" => reply((number("a") + number("b")).to_string()),
                "ptc-dash" => reply(format!("dash:{}", string("value"))),
                _ => CallToolResult::error(vec![ContentBlock::text("mcp tool exploded")]),
            };
            Ok(result.into())
        }
    }

    async fn mcp_agent() -> (Agent, McpClient) {
        let transport = McpTransport::from_fn(|| async {
            let (client, server) = tokio::io::duplex(1 << 16);
            tokio::spawn(async move {
                if let Ok(running) = PtcServer.serve(server).await {
                    let _ = running.waiting().await;
                }
            });
            Ok::<DuplexStream, BoxError>(client)
        });
        let client = McpClient::new(McpClientConfig {
            transport: Some(transport),
            ..McpClientConfig::default()
        })
        .unwrap();
        let mut tools: Vec<Arc<dyn Tool>> = vec![Arc::new(default_caller())];
        for tool in client.list_tools().await.unwrap() {
            tools.push(Arc::new(tool));
        }
        let agent = Agent::builder()
            .model(strands::testing::MockModel::new())
            .tools(tools)
            .printer(false)
            .build()
            .unwrap();
        (agent, client)
    }

    #[tokio::test]
    async fn calls_an_mcp_tool() {
        let (mut agent, client) = mcp_agent().await;
        assert_eq!(
            text(&run(&mut agent, "print(await ptc_echo(text=\"hello\"))").await),
            "echo:hello"
        );
        client.disconnect().await.unwrap();
    }

    #[tokio::test]
    async fn loops_over_mcp_tool_calls() {
        let (mut agent, client) = mcp_agent().await;
        let result = run(&mut agent, "for i in range(3):\n    print(await ptc_add(a=i, b=10))").await;
        assert_eq!(text(&result), "10\n11\n12");
        client.disconnect().await.unwrap();
    }

    #[tokio::test]
    async fn runs_mcp_tools_concurrently() {
        let (mut agent, client) = mcp_agent().await;
        let code = "import asyncio\nprint(await asyncio.gather(*[ptc_echo(text=str(i)) for i in range(3)]))";
        assert_eq!(text(&run(&mut agent, code).await), "['echo:0', 'echo:1', 'echo:2']");
        client.disconnect().await.unwrap();
    }

    #[tokio::test]
    async fn surfaces_an_mcp_tool_error_as_a_catchable_exception() {
        let (mut agent, client) = mcp_agent().await;
        let code = "try:\n    await ptc_boom()\nexcept RuntimeError as e:\n    print('caught:', e)";
        let result = run(&mut agent, code).await;
        assert_eq!(result.status, ToolResultStatus::Success);
        assert!(text(&result).contains("mcp tool exploded"));
        client.disconnect().await.unwrap();
    }

    #[tokio::test]
    async fn calls_a_hyphenated_mcp_tool_via_its_identifier_alias() {
        let (mut agent, client) = mcp_agent().await;
        assert_eq!(
            text(&run(&mut agent, "print(await ptc_dash(value=\"x\"))").await),
            "dash:x"
        );
        client.disconnect().await.unwrap();
    }

    #[tokio::test]
    async fn returns_only_printed_output() {
        let (mut agent, client) = mcp_agent().await;
        let result = run(&mut agent, "x = await ptc_echo(text=\"secret\")\nprint(\"done\")").await;
        assert_eq!(text(&result), "done");
        client.disconnect().await.unwrap();
    }
}