rig-agent 0.46.0

Rig's classic agent runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
use crate::agent::ResponseIdentity;
use crate::agent::typed::{TypedPromptResponse, deserialize_structured_output};
use crate::run::response::{CompletionCall, PromptResponse};
use crate::{
    agent::{
        AgentBuilder,
        hook::{
            AgentHook, DispatchAction, DispatchEvent, HookContext, InvalidToolCallAction,
            InvalidToolCallContext, ModelTurnAction, ModelTurnFinished,
        },
    },
    completion::{
        AssistantContent, CompletionRequest, FinishReason, Message, PromptError,
        StructuredOutputError, Usage,
    },
    test_utils::{
        MockAddTool, MockCompletionModel, MockContextProbeTool, MockSubtractTool, MockTurn,
        SessionId,
    },
    tool::ToolContext,
};
use rig_core::error::ProviderError;
use rig_core::message::{ToolChoice, UserContent};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;

/// rig#2322 — the **blocking** surface enforces the same truncation
/// contract as the streamed one.
///
/// The premise of the whole fix is that the two surfaces disagreeing is
/// what let truncation surface as a blank answer, yet every other guard
/// test drives the streamed surface. Until `MockTurn::with_finish_reason`
/// existed the blocking mock could not report a reason at all, so
/// `runner.rs`'s `.with_finish_reason(resp.finish_reason())` — and its
/// propagation through `model_response` → `record_completion_call` → this
/// guard — was never exercised. Deleting that one line failed nothing.
#[tokio::test]
async fn blocking_prompt_rejects_an_empty_truncated_turn() {
    let agent = AgentBuilder::new(MockCompletionModel::from_turns([MockTurn::from_contents(
        [],
    )
    .with_finish_reason(FinishReason::Length)]))
    .build();

    let err = agent
        .prompt("write a long essay")
        .await
        .expect_err("a content-less truncated turn must not return an empty answer");

    let rendered = format!("{err:?}");
    assert!(
        rendered.contains("Length") && rendered.contains("max_tokens"),
        "the blocking error must name the reason and the remedy: {rendered}"
    );
}

/// rig#2322 — blocking counterpart of the reasoning-only case: the shape
/// that motivated the predicate fix must be caught on both surfaces.
#[tokio::test]
async fn blocking_prompt_rejects_a_reasoning_only_truncated_turn() {
    let agent = AgentBuilder::new(MockCompletionModel::from_turns([MockTurn::from_content(
        AssistantContent::Reasoning(rig_core::message::Reasoning::new(
            "thinking, never answering",
        )),
    )
    .with_finish_reason(FinishReason::Length)]))
    .build();

    let err = agent
        .prompt("solve this")
        .await
        .expect_err("a reasoning-only truncated turn must not return an empty answer");

    assert!(format!("{err:?}").contains("Length"));
}

#[derive(Serialize)]
struct SerializeOnly {
    value: &'static str,
}

#[derive(Deserialize)]
struct DeserializeOnly {
    value: String,
}

#[derive(Debug, Deserialize, JsonSchema, PartialEq)]
struct TypedAnswer {
    value: String,
}

#[test]
fn deserialize_structured_output_tolerates_fences_and_prose() {
    // Clean JSON (native / output-tool path).
    assert_eq!(
        deserialize_structured_output::<TypedAnswer>(r#"{"value":"x"}"#).unwrap(),
        TypedAnswer { value: "x".into() }
    );
    // Markdown-fenced JSON (weak Prompted-mode models).
    assert_eq!(
        deserialize_structured_output::<TypedAnswer>("```json\n{\"value\":\"y\"}\n```").unwrap(),
        TypedAnswer { value: "y".into() }
    );
    // Prose around the JSON object.
    assert_eq!(
        deserialize_structured_output::<TypedAnswer>(
            "Here you go: {\"value\":\"z\"} — hope that helps!"
        )
        .unwrap(),
        TypedAnswer { value: "z".into() }
    );
    // No JSON at all still errors.
    assert!(deserialize_structured_output::<TypedAnswer>("no json here").is_err());
}

#[derive(Clone)]
struct PanicOnUnknownToolHook;

impl AgentHook for PanicOnUnknownToolHook {
    // The rejected attempt's completion outcome still fires, observe-only;
    // the accepted-turn hook must not.
    async fn on_model_turn_finished(
        &self,
        _ctx: &HookContext,
        _event: ModelTurnFinished<'_>,
    ) -> ModelTurnAction {
        panic!("unknown tool response should fail before response hooks run")
    }
    async fn on_dispatch(&self, _ctx: &HookContext, event: DispatchEvent<'_>) -> DispatchAction {
        if event.tool_name().is_some() {
            panic!("unknown tool call should fail before tool hooks run")
        }
        DispatchAction::proceed()
    }
}

#[derive(Clone)]
struct RepairDefaultApiHook;

impl AgentHook for RepairDefaultApiHook {
    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        event: &InvalidToolCallContext,
    ) -> Option<InvalidToolCallAction> {
        assert_eq!(event.tool_name, "default_api");
        Some(InvalidToolCallAction::repair("add"))
    }
}

#[derive(Clone)]
struct RetryDefaultApiHook;

impl AgentHook for RetryDefaultApiHook {
    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        event: &InvalidToolCallContext,
    ) -> Option<InvalidToolCallAction> {
        Some(InvalidToolCallAction::retry(format!(
            "Use one of these tools instead: {:?}",
            event.allowed_tools
        )))
    }
}

fn usage(input_tokens: u64, output_tokens: u64) -> Usage {
    Usage::new()
        .input_tokens(input_tokens)
        .output_tokens(output_tokens)
        .total_tokens(input_tokens + output_tokens)
}

#[test]
fn typed_prompt_response_serializes_with_serialize_only_output() {
    let response = TypedPromptResponse::new(
        SerializeOnly { value: "ok" },
        Usage::new()
            .input_tokens(1)
            .output_tokens(2)
            .total_tokens(3),
    );

    let json = serde_json::to_string(&response).expect("serialize typed prompt response");
    assert!(json.contains("\"value\":\"ok\""));
}

#[test]
fn typed_prompt_response_deserializes_with_deserialize_only_output() {
    let response: TypedPromptResponse<DeserializeOnly> = serde_json::from_str(
        r#"{"output":{"value":"ok"},"usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3,"cached_input_tokens":0,"cache_creation_input_tokens":0,"reasoning_tokens":0}}"#,
    )
    .expect("deserialize typed prompt response");

    assert_eq!(response.requests(), 0);
    assert_eq!(response.output.value, "ok");
    assert_eq!(response.usage.input_tokens, Some(1));
    assert_eq!(response.usage.output_tokens, Some(2));
    assert_eq!(response.usage.total_tokens, Some(3));
}

#[test]
fn prompt_response_serializes_completion_calls_with_missing_usage() {
    let reported_usage = usage(3, 4);
    let response = PromptResponse::new("ok", reported_usage).with_completion_calls(vec![
        CompletionCall::new(0, Usage::default(), json!({"id": "resp_0"})),
        CompletionCall::new(1, reported_usage, json!({"id": "resp_1"})),
    ]);

    let value = serde_json::to_value(&response).expect("serialize prompt response");

    // Unreported usage serializes as an empty object: every counter is
    // `None`, and `None` counters are omitted rather than encoded as zero.
    assert_eq!(
        value.get("completion_calls"),
        Some(&json!([
            {
                "call_index": 0,
                "usage": {},
                "raw": {"id": "resp_0"}
            },
            {
                "call_index": 1,
                "usage": {
                    "input_tokens": 3,
                    "output_tokens": 4,
                    "total_tokens": 7,
                },
                "raw": {"id": "resp_1"}
            }
        ]))
    );

    let response: PromptResponse =
        serde_json::from_value(value).expect("deserialize prompt response");
    assert_eq!(
        response.completion_calls(),
        &[
            CompletionCall::new(0, Usage::default(), json!({"id": "resp_0"})),
            CompletionCall::new(1, reported_usage, json!({"id": "resp_1"}))
        ]
    );
    assert_eq!(response.requests(), 2);
}

#[test]
fn prompt_response_output_tool_marker_is_never_serialized() {
    let response = PromptResponse::new("ok", usage(1, 2)).with_output_tool_calls(3);

    let value = serde_json::to_value(&response).expect("serialize prompt response");
    assert!(value.get("output_tool_calls").is_none());

    let decoded: PromptResponse =
        serde_json::from_value(value).expect("deserialize prompt response");
    assert_eq!(decoded.output_tool_calls(), 0);
}

#[test]
fn empty_turn_classification_survives_a_serde_round_trip() {
    let is_empty_turn = |choice: &[AssistantContent]| {
        rig_core::message::AssistantMessage::new(choice.to_vec()).is_empty_turn()
    };
    // A suspended run restored from JSON must classify its empty-text
    // turn exactly like the live run did, whatever spelling of "no
    // extras" the JSON carries, and an *annotated* empty block must
    // still read as content either way. The serde canonicalization
    // mechanics behind this (`{}`/`null` decode to `None`, empty params
    // never serialize) are pinned where they live, by rig-core's
    // `empty_params_canonicalize_to_none_in_both_serde_directions` —
    // this test asserts classification only.
    let live = vec![AssistantContent::text("")];
    assert!(is_empty_turn(&live));

    let round: Vec<AssistantContent> =
        serde_json::from_str(&serde_json::to_string(&live).expect("serialize"))
            .expect("deserialize");
    assert!(
        is_empty_turn(&round),
        "restored turn must classify like the live one: {round:?}"
    );

    // An explicit `{}` or `null` in the JSON — the shape a mechanical
    // migration script writes — classifies exactly like an absent field.
    for empty_spelling in [serde_json::json!({}), serde_json::Value::Null] {
        let migrated: Vec<AssistantContent> = serde_json::from_value(serde_json::json!([
            {"type": "text", "text": "", "additional_params": empty_spelling}
        ]))
        .expect("deserialize migrated");
        assert!(is_empty_turn(&migrated));
    }

    // Text with no provider item is empty, live and restored alike.
    let canonical_absent = vec![AssistantContent::Text(rig_core::message::Text::new(""))];
    assert!(is_empty_turn(&canonical_absent));
    let restored: Vec<AssistantContent> =
        serde_json::from_value(serde_json::to_value(&canonical_absent).expect("serialize"))
            .expect("deserialize");
    assert!(is_empty_turn(&restored));

    // An empty block holding a current provider item carries data; one whose
    // item no longer matches it (an edit) replays as nothing, so it is empty.
    let annotated =
        vec![AssistantContent::text("").with_native(serde_json::json!({"signature": "sig"}))];
    let restored: Vec<AssistantContent> =
        serde_json::from_value(serde_json::to_value(&annotated).expect("serialize"))
            .expect("deserialize annotated");
    assert!(
        !is_empty_turn(&restored),
        "an annotated empty block carries data: {restored:?}"
    );
    let stale: Vec<AssistantContent> = serde_json::from_value(serde_json::json!([
        {"type": "text", "text": "", "native": {"item": {"signature": "sig"}, "fingerprint": "0000000000000000"}}
    ]))
    .expect("deserialize stale");
    assert!(is_empty_turn(&stale), "{stale:?}");
}

#[test]
fn the_type_key_is_the_tag_and_the_untagged_shape_does_not_load() {
    // Assistant content is tagged like user content: `"type"` is consumed
    // as the discriminant, never captured into `additional_params`. And
    // there is deliberately no untagged fallback — the bare shape 0.41
    // serialized fails to deserialize (MIGRATING carries the recipe),
    // pinned here so removing the tag requirement is a visible decision,
    // not an accident.
    let tagged: Vec<AssistantContent> =
        serde_json::from_value(serde_json::json!([{"type": "text", "text": "ok"}]))
            .expect("deserialize");
    let [AssistantContent::Text(text)] = tagged.as_slice() else {
        panic!("expected one text block, got {tagged:?}");
    };
    assert_eq!(text.text, "ok");
    assert_eq!(text.native, None, "the tag is not data");

    serde_json::from_value::<Vec<AssistantContent>>(serde_json::json!([{"text": "ok"}]))
        .expect_err("the untagged shape must not deserialize");
}

#[test]
fn prompt_response_roundtrip_preserves_structured_content() {
    let response = PromptResponse::from_content(
        vec![
            AssistantContent::Reasoning(rig_core::message::Reasoning::new("thinking")),
            AssistantContent::text("visible "),
            AssistantContent::text("text"),
        ],
        Usage::default(),
    );
    assert_eq!(response.output(), "visible text");

    let value = serde_json::to_value(&response).expect("serialize prompt response");
    assert!(
        value.get("content").is_some(),
        "content is part of the serialized shape"
    );
    assert!(
        value.get("output").is_none(),
        "output is derived from content, not stored"
    );

    let round: PromptResponse = serde_json::from_value(value).expect("deserialize prompt response");
    assert_eq!(round.output(), "visible text");
    // Compare parts directly to sidestep the `Text::additional_params` serde
    // round-trip asymmetry.
    assert!(matches!(
        round.content(),
        [AssistantContent::Reasoning(_), AssistantContent::Text(a), AssistantContent::Text(b)]
            if a.text == "visible " && b.text == "text"
    ));
}

#[test]
fn prompt_response_serialize_and_deserialize_agree_on_wire_shape() {
    // `content` is a required, bare list in both serde directions — the
    // pre-`content` reconstruction (and the shadow repr that carried it)
    // is gone, so serialize and deserialize agree by construction. Pin
    // the shape: `content` present, `completion_calls` omitted only when
    // empty, and the value round-trips.
    let response = PromptResponse::new("hi", usage(1, 2))
        .with_completion_calls(vec![CompletionCall::new(0, usage(1, 2), json!({}))]);

    let from_response = serde_json::to_value(&response).expect("serialize response");
    assert!(from_response.get("content").is_some());
    assert!(from_response.get("completion_calls").is_some());

    let round: PromptResponse =
        serde_json::from_value(from_response).expect("deserialize response");
    assert_eq!(round.output(), "hi");
    assert_eq!(round.usage(), usage(1, 2));
    assert_eq!(
        round.completion_calls(),
        &[CompletionCall::new(0, usage(1, 2), json!({}))]
    );

    // The omission direction of `completion_calls`' skip-when-empty:
    // an empty list serializes without the key (the shadow-era wire
    // shape), and the keyless JSON still deserializes.
    let bare = serde_json::to_value(PromptResponse::new("hi", usage(1, 2)))
        .expect("serialize bare response");
    assert!(bare.get("completion_calls").is_none());
    let round: PromptResponse = serde_json::from_value(bare).expect("deserialize keyless response");
    assert!(round.completion_calls().is_empty());
}

#[tokio::test]
async fn typed_prompt_response_preserves_completion_calls() {
    let call_usage = Usage::new()
        .input_tokens(4)
        .output_tokens(6)
        .total_tokens(10);
    let turn = MockTurn::text(r#"{"value":"ok"}"#).with_usage(call_usage);
    let raw = turn.raw().expect("a scripted turn has a document");
    let model = MockCompletionModel::from_turns([turn]);
    let agent = AgentBuilder::new(model).build();

    let response = agent
        .prompt_typed::<TypedAnswer>("return typed json")
        .await
        .expect("typed prompt should succeed");

    assert_eq!(
        response.output,
        TypedAnswer {
            value: "ok".to_string()
        }
    );
    assert_eq!(response.usage, call_usage);
    assert_eq!(
        response.completion_calls(),
        &[CompletionCall::new(0, call_usage, raw)]
    );
    assert_eq!(
        response.messages.len(),
        2,
        "the accepted attempt's prompt and answer: {:?}",
        response.messages
    );
}

#[tokio::test]
async fn typed_prompt_deserialization_error_keeps_the_model_output() {
    let model = MockCompletionModel::from_turns([MockTurn::text("the answer is ok")]);
    let agent = AgentBuilder::new(model).build();

    let error = agent
        .prompt_typed::<TypedAnswer>("return typed json")
        .await
        .expect_err("prose is not a TypedAnswer");

    match error {
        StructuredOutputError::Deserialization { output, .. } => {
            assert_eq!(output, "the answer is ok");
        }
        other => panic!("expected a deserialization error, got {other:?}"),
    }
}

fn validate_follow_up_tool_history(request: &CompletionRequest) {
    let history = request.chat_history.clone();
    assert_eq!(
        history.len(),
        3,
        "follow-up request should contain the prompt, assistant tool call, and user tool result: {history:?}"
    );

    assert!(matches!(
        history.first().map(|message| &**message),
        Some(Message::User { content })
            if matches!(
                content.first(),
                Some(UserContent::Text(text)) if text.text == "do tool work"
            )
    ));

    // The provider's correlator was overridden to "call_1"; the call carries
    // that one id and the result answers it.
    assert!(matches!(
        history.get(1).map(|message| &**message),
        Some(Message::Assistant(rig_core::message::AssistantMessage { content, .. }))
            if matches!(
                content.first(),
                Some(AssistantContent::ToolCall(tool_call))
                    if tool_call.id.provider().map(|provider| provider.as_str()) == Some("call_1")
            )
    ));

    assert!(matches!(
        history.get(2).map(|message| &**message),
        Some(Message::User { content })
            if matches!(
                content.first(),
                Some(UserContent::ToolResult(tool_result))
                    if tool_result.call.provider().map(|provider| provider.as_str()) == Some("call_1")
            )
    ));
}

fn history_contains_tool_call(history: &[Message], tool_name: &str) -> bool {
    history.iter().any(|message| {
        matches!(
            message,
            Message::Assistant(rig_core::message::AssistantMessage { content, .. })
                if content.iter().any(|item| matches!(
                    item,
                    AssistantContent::ToolCall(tool_call)
                        if tool_call.function.name == tool_name
                ))
        )
    })
}

#[tokio::test]
async fn unknown_tool_call_fails_before_non_streaming_second_request() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "default_api", json!({"x": 1, "y": 2})),
        MockTurn::text("should not be requested"),
    ]);
    let recorded = model.clone();
    let agent = AgentBuilder::new(model).tool(MockAddTool).build();

    let err = agent
        .prompt("use the tool")
        .add_hook(PanicOnUnknownToolHook)
        .max_turns(3)
        .await
        .expect_err("unknown model-emitted tool should fail");

    match err {
        PromptError::UnknownToolCall {
            tool_name,
            available_tools,
            allowed_tools,
            chat_history,
        } => {
            assert_eq!(tool_name, "default_api");
            assert_eq!(available_tools, vec!["add".to_string()]);
            assert_eq!(allowed_tools, vec!["add".to_string()]);
            assert!(history_contains_tool_call(&chat_history, "default_api"));
        }
        other => panic!("expected UnknownToolCall, got {other:?}"),
    }
    assert_eq!(recorded.request_count(), 1);
}

/// Context values persist for the whole run, across *multiple* tool-call rounds
/// (the headline value prop). The model calls the probe in two consecutive
/// rounds; both must observe the same injected value, not just the first.
#[tokio::test]
async fn tool_context_persists_across_multiple_rounds() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("c1", "context_probe", json!({})),
        MockTurn::tool_call("c2", "context_probe", json!({})),
        MockTurn::text("done"),
    ]);
    let probe = MockContextProbeTool::default();
    let agent = AgentBuilder::new(model).tool(probe.clone()).build();

    let mut context = ToolContext::new();
    context.insert(SessionId("abc-123".to_string())).unwrap();

    let out = agent
        .prompt("use the tool twice")
        .tool_context(context)
        .max_turns(5)
        .await
        .expect("run succeeds");

    assert_eq!(out.output(), "done");
    assert_eq!(
        probe.observations(),
        vec!["session:abc-123".to_string(), "session:abc-123".to_string()],
    );
}

#[tokio::test]
async fn disallowed_specific_tool_call_fails_before_non_streaming_second_request() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "subtract", json!({"x": 3, "y": 1})),
        MockTurn::text("should not be requested"),
    ]);
    let recorded = model.clone();
    let agent = AgentBuilder::new(model)
        .tool(MockAddTool)
        .tool(MockSubtractTool)
        .tool_choice(ToolChoice::Specific {
            function_names: vec![rig_core::message::ToolName::new("add").expect("tool name")],
        })
        .build();

    let err = agent
        .prompt("use the allowed tool")
        .add_hook(PanicOnUnknownToolHook)
        .max_turns(3)
        .await
        .expect_err("disallowed model-emitted tool should fail");

    match err {
        PromptError::UnknownToolCall {
            tool_name,
            available_tools,
            allowed_tools,
            chat_history,
        } => {
            assert_eq!(tool_name, "subtract");
            assert_eq!(
                available_tools,
                vec!["add".to_string(), "subtract".to_string()]
            );
            assert_eq!(allowed_tools, vec!["add".to_string()]);
            assert!(history_contains_tool_call(&chat_history, "subtract"));
        }
        other => panic!("expected UnknownToolCall, got {other:?}"),
    }
    assert_eq!(recorded.request_count(), 1);
}

#[tokio::test]
async fn tool_choice_none_rejects_non_streaming_tool_call() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "add", json!({"x": 1, "y": 2})),
        MockTurn::text("should not be requested"),
    ]);
    let recorded = model.clone();
    let agent = AgentBuilder::new(model)
        .tool(MockAddTool)
        .tool_choice(ToolChoice::None)
        .build();

    let err = agent
        .prompt("do not use tools")
        .add_hook(PanicOnUnknownToolHook)
        .max_turns(3)
        .await
        .expect_err("ToolChoice::None should reject returned tool calls");

    match err {
        PromptError::UnknownToolCall {
            tool_name,
            available_tools,
            allowed_tools,
            chat_history,
        } => {
            assert_eq!(tool_name, "add");
            assert_eq!(available_tools, vec!["add".to_string()]);
            assert!(allowed_tools.is_empty());
            assert!(history_contains_tool_call(&chat_history, "add"));
        }
        other => panic!("expected UnknownToolCall, got {other:?}"),
    }
    assert_eq!(recorded.request_count(), 1);
}

#[tokio::test]
async fn typed_prompt_invalid_tool_call_hook_can_repair_tool_name() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "default_api", json!({"x": 2, "y": 3})),
        MockTurn::text(r#"{"value":"repaired"}"#),
    ]);
    let agent = AgentBuilder::new(model).tool(MockAddTool).build();

    let response = agent
        .prompt_typed::<TypedAnswer>("return typed json")
        .add_hook(RepairDefaultApiHook)
        .max_turns(3)
        .await
        .expect("typed prompt should repair invalid tool call");

    assert_eq!(
        response.output,
        TypedAnswer {
            value: "repaired".to_string()
        }
    );
}

#[tokio::test]
async fn typed_prompt_invalid_tool_call_hook_can_retry_and_parse_response() {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "default_api", json!({"x": 2, "y": 3})),
        MockTurn::text(r#"{"value":"retried"}"#),
    ]);
    let recorded = model.clone();
    let agent = AgentBuilder::new(model).tool(MockAddTool).build();

    let response = agent
        .prompt_typed::<TypedAnswer>("return typed json")
        .add_hook(RetryDefaultApiHook)
        .max_invalid_tool_call_retries(1)
        .max_turns(3)
        .await
        .expect("typed prompt should retry invalid tool call");

    assert_eq!(
        response.output,
        TypedAnswer {
            value: "retried".to_string()
        }
    );
    assert_eq!(recorded.request_count(), 2);
}

#[tokio::test]
async fn invalid_specific_tool_choice_fails_before_non_streaming_provider_request() {
    let model = MockCompletionModel::text("should not be requested");
    let recorded = model.clone();
    let agent = AgentBuilder::new(model)
        .tool(MockAddTool)
        .tool_choice(ToolChoice::Specific {
            function_names: vec![rig_core::message::ToolName::new("missing").expect("tool name")],
        })
        .build();

    let err = agent
        .prompt("use the missing tool")
        .await
        .expect_err("invalid ToolChoice::Specific should fail before provider request");

    match err {
        PromptError::Provider(ProviderError::Request(err)) => {
            let msg = err.to_string();
            assert!(msg.contains("missing"), "got: {msg}");
            assert!(msg.contains("add"), "got: {msg}");
        }
        other => panic!("expected ProviderError::Request, got {other:?}"),
    }
    assert_eq!(recorded.request_count(), 0);
}

#[tokio::test]
async fn prompt_request_stops_cleanly_on_empty_terminal_turn() {
    let first_call_usage = Usage::new()
        .input_tokens(1)
        .output_tokens(1)
        .total_tokens(2);
    let second_call_usage = Usage::new()
        .input_tokens(1)
        .output_tokens(1)
        .total_tokens(2);
    let first_turn = MockTurn::tool_call("tool_call_1", "add", json!({"x": 1, "y": 2}))
        .with_call_id("call_1")
        .with_usage(first_call_usage);
    let second_turn = MockTurn::text("").with_usage(second_call_usage);
    let first_raw = first_turn.raw().expect("a scripted turn has a document");
    let second_raw = second_turn.raw().expect("a scripted turn has a document");
    let model = MockCompletionModel::from_turns([first_turn, second_turn]);
    let agent = AgentBuilder::new(model.clone()).tool(MockAddTool).build();

    let response = agent
        .prompt("do tool work")
        .max_turns(3)
        .await
        .expect("empty terminal turn should not error");

    assert!(response.output().is_empty());
    assert_eq!(
        response.usage,
        Usage::new()
            .input_tokens(2)
            .output_tokens(2)
            .total_tokens(4)
    );
    assert_eq!(
        response.completion_calls(),
        &[
            CompletionCall::new(0, first_call_usage, first_raw),
            CompletionCall::new(1, second_call_usage, second_raw)
        ]
    );

    let history = response.messages;
    assert_eq!(history.len(), 3);
    assert!(matches!(
        history.first(),
        Some(Message::User { content })
            if matches!(
                content.first(),
                Some(UserContent::Text(text)) if text.text == "do tool work"
            )
    ));
    assert!(history.iter().any(|message| matches!(
        message,
        Message::Assistant(rig_core::message::AssistantMessage { content, .. })
            if matches!(
                content.first(),
                Some(AssistantContent::ToolCall(tool_call))
                    if tool_call.id.provider().map(|provider| provider.as_str()) == Some("call_1")
                        && tool_call.id.provider().as_ref().is_some_and(
                            |provider| provider.as_str() == "call_1"
                        )
            )
    )));
    assert!(history.iter().any(|message| matches!(
        message,
        Message::User { content }
            if matches!(
                content.first(),
                Some(UserContent::ToolResult(tool_result))
                    if tool_result.call.provider().map(|provider| provider.as_str()) == Some("call_1")
                        && tool_result.call.provider().as_ref().is_some_and(
                            |provider| provider.as_str() == "call_1"
                        )
            )
    )));
    assert!(!history.iter().any(|message| matches!(
        message,
        Message::Assistant(rig_core::message::AssistantMessage { content, .. })
            if content.iter().any(|item| matches!(
                item,
                AssistantContent::Text(text) if text.text.is_empty()
            ))
    )));
    let requests = model.requests();
    assert_eq!(requests.len(), 2);
    validate_follow_up_tool_history(&requests[1]);
}

/// The identity fields are skipped when `None` (rig#2265), so a record
/// written without them loads with every identity field `None`.
#[test]
fn completion_call_without_identity_fields_deserializes() {
    let call: CompletionCall = serde_json::from_str(
        r#"{"call_index": 3, "usage": {"input_tokens": 1, "output_tokens": 2,
                "total_tokens": 3, "cached_input_tokens": 0,
                "cache_creation_input_tokens": 0, "reasoning_tokens": 0},
                "raw": {}}"#,
    )
    .expect("a CompletionCall without identity fields should load");
    assert_eq!(call.call_index, 3);
    assert_eq!(call.identity(), ResponseIdentity::default());
}

/// And a populated record round-trips the identity losslessly.
#[test]
fn completion_call_identity_round_trips() {
    let call = CompletionCall::new(0, crate::completion::Usage::default(), json!({}))
        .with_identity(ResponseIdentity {
            response_id: Some("resp_1".into()),
            provider_request_id: Some("req_1".into()),
        });
    let json = serde_json::to_string(&call).expect("serialize");
    let restored: CompletionCall = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(restored, call);
}

/// A turn the provider failed with nothing to show never ends a run as a
/// success (an empty answer would pass for one).
#[tokio::test]
async fn a_failed_turn_without_an_answer_fails_the_run() {
    let model = MockCompletionModel::from_turns([MockTurn::from_contents(Vec::new())
        .with_finish_reason(rig_core::completion::FinishReason::Other(
            "MALFORMED_FUNCTION_CALL".to_owned(),
        ))]);
    let agent = AgentBuilder::new(model).build();
    let error = agent
        .prompt("hello")
        .await
        .expect_err("a failed, answerless turn fails the run");
    assert!(
        error.to_string().contains("MALFORMED_FUNCTION_CALL"),
        "the run names why the provider failed the turn: {error}"
    );
}

fn unknown_finish(turn: MockTurn) -> MockTurn {
    turn.with_finish_reason(FinishReason::Other("weird".to_owned()))
}

fn replays_assistant_text(request: &CompletionRequest, text: &str) -> bool {
    request.chat_history.iter().any(|message| {
        matches!(
            &**message,
            Message::Assistant(turn) if turn.content.iter().any(|block| matches!(
                block,
                AssistantContent::Text(t) if t.text == text
            ))
        )
    })
}

/// By default a text answer that ends in an unknown finish reason fails the
/// run, naming the reason, as replay would leave the turn out.
#[tokio::test]
async fn an_answer_with_an_unknown_finish_reason_fails_the_run_by_default() {
    let model = MockCompletionModel::from_turns([unknown_finish(MockTurn::text("answer"))]);
    let agent = AgentBuilder::new(model).build();
    let error = agent
        .prompt("hello")
        .await
        .expect_err("a failed turn is no answer");
    assert!(error.to_string().contains("weird"), "{error}");
}

/// Accepted, the answer succeeds and the next run replays it.
#[tokio::test]
async fn an_accepted_unknown_finish_reason_answers_and_replays() {
    let model = MockCompletionModel::from_turns([
        unknown_finish(MockTurn::text("answer")),
        MockTurn::text("again"),
    ]);
    let agent = AgentBuilder::new(model.clone())
        .accept_unknown_finish_reasons(true)
        .build();
    let first = agent
        .prompt("hello")
        .await
        .expect("the accepted turn answers");
    assert_eq!(first.output(), "answer");
    agent
        .prompt("and now")
        .history(first.messages)
        .await
        .expect("the follow-up answers");
    let requests = model.requests();
    assert!(
        requests
            .iter()
            .all(|request| request.accept_unknown_finish_reasons)
    );
    assert!(replays_assistant_text(&requests[1], "answer"));
}

/// A run can accept unknown reasons where its agent does not.
#[tokio::test]
async fn a_run_accepts_unknown_finish_reasons_on_its_own() {
    let model = MockCompletionModel::from_turns([unknown_finish(MockTurn::text("answer"))]);
    let agent = AgentBuilder::new(model).build();
    let answer = agent
        .prompt("hello")
        .accept_unknown_finish_reasons(true)
        .await
        .expect("the run accepts the reason");
    assert_eq!(answer.output(), "answer");
}

/// A host driving the run itself from the agent's spec keeps the setting:
/// the prepared request accepts the reason and the run answers.
#[tokio::test]
async fn a_sans_io_run_from_the_agent_spec_accepts_unknown_finish_reasons() {
    use crate::run::{AgentRun, AgentRunStep, ModelTurn, prepare_request};

    let model = MockCompletionModel::from_turns([unknown_finish(MockTurn::text("answer"))]);
    let agent = AgentBuilder::new(model.clone())
        .accept_unknown_finish_reasons(true)
        .build();
    let spec = agent.run_spec();
    assert!(spec.accept_unknown_finish_reasons);
    let mut run = AgentRun::from_spec(&spec, "hello", None);
    let output = loop {
        match run.next_step().expect("a step") {
            AgentRunStep::CallModel {
                prompt, history, ..
            } => {
                let prepared =
                    prepare_request(&spec, &Default::default(), &history, Vec::new(), None, None)
                        .expect("prepared");
                let policy = prepared.policy.clone();
                let request = prepared.apply(CompletionRequest::new(prompt));
                assert!(request.accept_unknown_finish_reasons);
                let response = model.call(request).await.expect("the reply folds");
                run.model_response(ModelTurn::new(
                    response.head(),
                    response.choice,
                    response.usage,
                    policy,
                    response.raw,
                ))
                .expect("the accepted turn is no failure");
            }
            AgentRunStep::CallTools { .. } => panic!("no tools were called"),
            AgentRunStep::Done(response) => break response.output().to_owned(),
        }
    };
    assert_eq!(output, "answer");
}

/// The tool calls of a turn with an unknown finish reason run only when the
/// agent accepts the reason.
#[tokio::test]
async fn calls_with_an_unknown_finish_reason_run_only_when_accepted() {
    for accept in [false, true] {
        let model = MockCompletionModel::from_turns([
            unknown_finish(MockTurn::tool_call(
                "call_1",
                "add",
                json!({"x": 1, "y": 2}),
            )),
            MockTurn::text("3"),
        ]);
        let agent = AgentBuilder::new(model.clone())
            .tool(MockAddTool)
            .accept_unknown_finish_reasons(accept)
            .build();
        let outcome = agent.prompt("add").max_turns(3).await;
        if accept {
            assert_eq!(outcome.expect("the call runs").output(), "3");
            assert_eq!(model.request_count(), 2);
        } else {
            let error = outcome.expect_err("the failed turn runs no call");
            assert!(
                error.to_string().contains("none of its tool calls ran"),
                "{error}"
            );
            assert_eq!(model.request_count(), 1);
        }
    }
}

/// Patches every turn's tool choice and answers invalid calls with a Skip,
/// recording the tool choice each invalid-call context reports.
#[derive(Clone)]
pub(crate) struct PatchChoiceThenSkip {
    choice: ToolChoice,
    seen: std::sync::Arc<std::sync::Mutex<Vec<Option<ToolChoice>>>>,
}

impl PatchChoiceThenSkip {
    pub(crate) fn new(choice: ToolChoice) -> Self {
        Self {
            choice,
            seen: std::sync::Arc::default(),
        }
    }

    pub(crate) fn seen(&self) -> Vec<Option<ToolChoice>> {
        self.seen
            .lock()
            .map(|seen| seen.clone())
            .unwrap_or_default()
    }
}

impl AgentHook for PatchChoiceThenSkip {
    async fn on_completion_call(
        &self,
        _ctx: &HookContext,
        _event: crate::agent::CompletionCallEvent<'_>,
    ) -> crate::agent::CompletionCallAction {
        crate::agent::CompletionCallAction::patch(
            crate::agent::RequestPatch::new().tool_choice(self.choice.clone()),
        )
    }

    async fn on_invalid_tool_call(
        &self,
        _ctx: &HookContext,
        event: &InvalidToolCallContext,
    ) -> Option<InvalidToolCallAction> {
        if let Ok(mut seen) = self.seen.lock() {
            seen.push(event.tool_choice.clone());
        }
        Some(InvalidToolCallAction::skip("skipped"))
    }
}

/// A turn patched to `ToolChoice::None` forbids every tool call, so a Skip is
/// rejected exactly as it is under an agent-level `ToolChoice::None`
/// (`tool_choice_none_rejects_non_streaming_tool_call`).
#[tokio::test]
async fn a_turn_patched_to_tool_choice_none_rejects_a_skip() -> anyhow::Result<()> {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "add", json!({"x": 1, "y": 2})),
        MockTurn::text("the skip was accepted"),
    ]);
    let recorded = model.clone();
    let agent = AgentBuilder::new(model).tool(MockAddTool).build();

    let result = agent
        .prompt("do not use tools")
        .add_hook(PatchChoiceThenSkip::new(ToolChoice::None))
        .max_turns(3)
        .await;

    anyhow::ensure!(
        matches!(result, Err(PromptError::UnknownToolCall { ref tool_name, .. }) if tool_name == "add"),
        "a Skip under a turn patched to ToolChoice::None must fail with UnknownToolCall, got {result:?}"
    );
    anyhow::ensure!(recorded.request_count() == 1);
    Ok(())
}

/// An agent-level `ToolChoice::None` patched to `Auto` for the turn permits
/// tools, so a Skip of an unknown call is valid and the run continues.
#[tokio::test]
async fn a_turn_patched_away_from_tool_choice_none_accepts_a_skip() -> anyhow::Result<()> {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "default_api", json!({"x": 1, "y": 2})),
        MockTurn::text("done"),
    ]);
    let agent = AgentBuilder::new(model)
        .tool(MockAddTool)
        .tool_choice(ToolChoice::None)
        .build();

    let result = agent
        .prompt("use tools")
        .add_hook(PatchChoiceThenSkip::new(ToolChoice::Auto))
        .max_turns(3)
        .await;

    anyhow::ensure!(
        matches!(result, Ok(ref answer) if answer.output() == "done"),
        "a Skip under a turn patched to ToolChoice::Auto must be accepted, got {result:?}"
    );
    Ok(())
}

/// The invalid-call context reports the tool choice the turn was sent with,
/// not the run-level default it was patched from.
#[tokio::test]
async fn invalid_tool_call_context_reports_the_patched_tool_choice() -> anyhow::Result<()> {
    let model = MockCompletionModel::from_turns([
        MockTurn::tool_call("tool_call_1", "default_api", json!({"x": 1, "y": 2})),
        MockTurn::text("done"),
    ]);
    let agent = AgentBuilder::new(model).tool(MockAddTool).build();
    let hook = PatchChoiceThenSkip::new(ToolChoice::Required);

    let result = agent
        .prompt("use tools")
        .add_hook(hook.clone())
        .max_turns(3)
        .await;

    anyhow::ensure!(result.is_ok(), "run should succeed, got {result:?}");
    let seen = hook.seen();
    anyhow::ensure!(
        seen == vec![Some(ToolChoice::Required)],
        "invalid-call context must report the patched ToolChoice::Required, got {seen:?}"
    );
    Ok(())
}