everruns-contracts 0.44.0

Shared runtime, provider, capability, and model profile contracts for Everruns
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
use super::*;
use serde_json::{Value, json};

// ========================================================================
// Request-too-large detection tests
// ========================================================================

// ========================================================================
// Model-not-found detection tests
// ========================================================================

// ========================================================================
// Reasoning effort guard tests
// ========================================================================

// ------------------------------------------------------------------
// EVE-522: streaming chunk handling (process_stream_choice)
// ------------------------------------------------------------------

fn choice(json_str: &str) -> OpenAiStreamChoice {
    serde_json::from_str(json_str).unwrap()
}

/// `process_stream_choice` for chunks that emit at most one event.
fn single_event(
    choice: &OpenAiStreamChoice,
    total_tokens: &mut u32,
    acc: &mut StreamToolCallAccumulator,
    finish_reason: &mut Option<String>,
) -> Option<LlmStreamEvent> {
    let mut events = process_stream_choice(choice, total_tokens, acc, finish_reason);
    assert!(events.len() <= 1, "expected one event, got {events:?}");
    events.pop()
}

/// Mistral streams reasoning as typed content chunks, and its last chunk can
/// carry an (empty) thinking chunk beside the answer text and the finish
/// reason. Verbatim shapes from `mistral-large-4` with `reasoning_effort:
/// "high"`; before these parsed, every such chunk failed to deserialize and
/// the answer was dropped.
#[test]
fn mistral_typed_content_chunks_split_into_reasoning_and_text() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    let thinking = choice(
        r#"{"index":0,"delta":{"content":[{"type":"thinking","thinking":[{"type":"text","text":"17 x 23"}],"closed":true}]},"finish_reason":null}"#,
    );
    let events = process_stream_choice(&thinking, &mut total_tokens, &mut acc, &mut finish_reason);
    assert!(
        matches!(events.as_slice(), [LlmStreamEvent::ReasoningDelta { delta, summary: false }] if delta == "17 x 23"),
        "{events:?}"
    );

    let last = choice(
        r#"{"index":0,"delta":{"content":[{"type":"thinking","thinking":[]},{"type":"text","text":"391"}]},"finish_reason":"stop"}"#,
    );
    let events = process_stream_choice(&last, &mut total_tokens, &mut acc, &mut finish_reason);
    assert!(
        matches!(events.as_slice(), [LlmStreamEvent::TextDelta(text)] if text == "391"),
        "{events:?}"
    );
    assert_eq!(finish_reason.as_deref(), Some("stop"));

    let both = choice(
        r#"{"delta":{"content":[{"type":"thinking","thinking":[{"type":"text","text":"done"}]},{"type":"text","text":"answer"},{"type":"reference","reference_ids":[1]}]}}"#,
    );
    let events = process_stream_choice(&both, &mut total_tokens, &mut acc, &mut finish_reason);
    assert!(
        matches!(
            events.as_slice(),
            [LlmStreamEvent::ReasoningDelta { delta, .. }, LlmStreamEvent::TextDelta(text)]
                if delta == "done" && text == "answer"
        ),
        "{events:?}"
    );
}

/// EVE-522 regression: providers such as OpenRouter/DeepInfra send an empty
/// `content: ""` in the same chunk that carries `finish_reason: "tool_calls"`.
/// The accumulated tool calls must still be emitted exactly once.
#[test]
fn test_empty_content_finish_chunk_still_emits_tool_calls() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    // Chunk 2: tool_calls delta opens the call (id + name).
    let e = single_event(
        &choice(
            r#"{"delta":{"content":null,"tool_calls":[{"index":0,"id":"call_1","function":{"name":"read_file","arguments":""}}]},"finish_reason":null}"#,
        ),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    assert!(e.is_none());

    // Chunk 3: tool_calls delta streams the arguments.
    let e = single_event(
        &choice(
            r#"{"delta":{"content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"path\":\"Cargo.toml\"}"}}]},"finish_reason":null}"#,
        ),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    assert!(e.is_none());

    // Chunk 4: content:"" alongside finish_reason:"tool_calls" — must NOT
    // short-circuit; emits the accumulated call with parsed JSON arguments.
    let e = single_event(
        &choice(r#"{"delta":{"content":""},"finish_reason":"tool_calls"}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    match e {
        Some(LlmStreamEvent::ToolCalls(calls)) => {
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0].id, "call_1");
            assert_eq!(calls[0].name, "read_file");
            assert_eq!(calls[0].arguments, json!({"path": "Cargo.toml"}));
        }
        other => panic!("expected ToolCalls, got {:?}", other),
    }
    assert_eq!(finish_reason.as_deref(), Some("tool_calls"));

    // Chunk 5: second finish chunk with content:"" — the accumulator was
    // drained, so the same call must not be emitted again.
    let e = single_event(
        &choice(r#"{"delta":{"content":""},"finish_reason":"tool_calls"}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    assert!(e.is_none(), "tool calls must only be emitted once");
}

/// Non-empty content deltas are still emitted and counted as output tokens.
#[test]
fn test_non_empty_content_is_emitted() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    let e = single_event(
        &choice(r#"{"delta":{"content":"hello"},"finish_reason":null}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    assert!(matches!(e, Some(LlmStreamEvent::TextDelta(s)) if s == "hello"));
    assert_eq!(total_tokens, 1);
}

/// EVE-636: streamed tool-call arguments must concatenate exactly across
/// many small chunks (accumulated as a raw string, parsed zero times
/// mid-stream) and be parsed exactly once at the `tool_calls` finish chunk.
#[test]
fn test_tool_call_arguments_accumulate_across_many_chunks() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    // Open the call (id + name, empty initial arguments).
    single_event(
        &choice(
            r#"{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"write_file","arguments":""}}]},"finish_reason":null}"#,
        ),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );

    let payload = r#"{"path":"a.rs","contents":"a fairly long contents value streamed one character at a time to exceed one hundred chunks","n":987654321}"#;

    // Stream the arguments one character per chunk.
    for ch in payload.chars() {
        let frag = ch.to_string();
        let chunk = json!({
            "delta": {"tool_calls": [{"index": 0, "function": {"arguments": frag}}]},
            "finish_reason": null
        })
        .to_string();
        single_event(
            &choice(&chunk),
            &mut total_tokens,
            &mut acc,
            &mut finish_reason,
        );
    }

    // Mid-stream the shared accumulator holds the fragments as a raw string
    // (parsed once at finalize); its own unit tests cover that internal, so
    // here we assert the observable finish-chunk result concatenates exactly.

    // Finish chunk: parsed exactly once into the structured value.
    let e = single_event(
        &choice(r#"{"delta":{},"finish_reason":"tool_calls"}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    match e {
        Some(LlmStreamEvent::ToolCalls(calls)) => {
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0].id, "call_1");
            assert_eq!(calls[0].name, "write_file");
            assert_eq!(
                calls[0].arguments,
                serde_json::from_str::<serde_json::Value>(payload).unwrap()
            );
        }
        other => panic!("expected ToolCalls, got {:?}", other),
    }
}

/// OpenAI's native path sends `delta: {}` (no content key) in the finish
/// chunk; the existing behavior of emitting tool calls there is preserved.
#[test]
fn test_finish_chunk_without_content_emits_tool_calls() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    single_event(
        &choice(
            r#"{"delta":{"tool_calls":[{"index":0,"id":"call_9","function":{"name":"list_dir","arguments":"{}"}}]},"finish_reason":null}"#,
        ),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );

    let e = single_event(
        &choice(r#"{"delta":{},"finish_reason":"tool_calls"}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );
    match e {
        Some(LlmStreamEvent::ToolCalls(calls)) => {
            assert_eq!(calls.len(), 1);
            assert_eq!(
                serde_json::to_value(&calls).unwrap(),
                json!([{"id":"call_9","name":"list_dir","arguments":{}}])
            );
        }
        other => panic!("expected ToolCalls, got {:?}", other),
    }
}

/// Seed a single tool-call slot into an accumulator the way the streamed
/// chunks would (id + name + raw argument buffer), so the fallback-flush
/// tests exercise the real accumulation path.
fn seeded_acc(id: &str, name: &str, arguments: &str) -> StreamToolCallAccumulator {
    let mut acc = StreamToolCallAccumulator::new();
    acc.apply_indexed_delta(0, Some(id), Some(name), Some(arguments));
    acc
}

/// The [DONE] fallback flushes accumulated-but-unemitted tool calls when no
/// finish reason was reported and drains the accumulator; once drained it
/// returns None.
#[test]
fn test_take_pending_tool_calls_flushes_then_drains_without_finish_reason() {
    let mut acc = seeded_acc("call_1", "read_file", r#"{"path":"Cargo.toml"}"#);

    match take_pending_tool_calls(&mut acc, None) {
        Some(LlmStreamEvent::ToolCalls(calls)) => {
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0].id, "call_1");
            assert_eq!(calls[0].name, "read_file");
            assert_eq!(calls[0].arguments, json!({"path": "Cargo.toml"}));
        }
        other => panic!("expected ToolCalls, got {:?}", other),
    }
    assert!(acc.is_empty(), "accumulator must be drained after flush");
    assert!(take_pending_tool_calls(&mut acc, None).is_none());
}

#[test]
fn test_take_pending_tool_calls_discards_non_tool_finish_reason() {
    let mut acc = seeded_acc("call_cut", "read_file", r#"{"path":"#);

    assert!(take_pending_tool_calls(&mut acc, Some("length")).is_none());
    assert!(
        acc.is_empty(),
        "discarded unsafe fallback calls must still drain the accumulator"
    );
}

#[test]
fn test_take_pending_tool_calls_rejects_malformed_fallback_arguments() {
    let mut acc = seeded_acc("call_cut", "read_file", r#"{"path":"#);

    assert!(take_pending_tool_calls(&mut acc, None).is_none());
    assert!(
        acc.is_empty(),
        "malformed fallback calls must be drained instead of re-emitted"
    );
}

#[test]
fn test_non_tool_finish_reason_leaves_pending_calls_for_done_discard() {
    let mut total_tokens = 0u32;
    let mut acc = StreamToolCallAccumulator::new();
    let mut finish_reason: Option<String> = None;

    single_event(
        &choice(
            r#"{"delta":{"tool_calls":[{"index":0,"id":"call_cut","function":{"name":"read_file","arguments":"{\"path\":"}}]},"finish_reason":null}"#,
        ),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );

    let e = single_event(
        &choice(r#"{"delta":{},"finish_reason":"length"}"#),
        &mut total_tokens,
        &mut acc,
        &mut finish_reason,
    );

    assert!(e.is_none());
    assert_eq!(finish_reason.as_deref(), Some("length"));
    assert!(take_pending_tool_calls(&mut acc, finish_reason.as_deref()).is_none());
    assert!(acc.is_empty());
}

#[test]
fn function_tools_serialize_strict_only_for_compatible_schemas() {
    use crate::tool_types::{BuiltinTool, DeferrablePolicy, ToolHints, ToolPolicy};
    let make_tool = |parameters| {
        ToolDefinition::Builtin(BuiltinTool {
            name: "lookup".into(),
            display_name: None,
            description: "Lookup".into(),
            parameters,
            policy: ToolPolicy::Auto,
            category: None,
            deferrable: DeferrablePolicy::Never,
            hints: ToolHints::default(),
            full_parameters: None,
        })
    };
    let compatible = OpenAIProtocolChatDriver::convert_tools(&[make_tool(json!({
        "type":"object","properties":{"query":{"type":"string"}}
    }))]);
    assert_eq!(
        serde_json::to_value(&compatible).unwrap(),
        json!([{"type":"function","function":{"name":"lookup","description":"Lookup","strict":true,"parameters":{"type":"object","properties":{"query":{"type":["string","null"]}},"required":["query"],"additionalProperties":false}}}])
    );
    let incompatible = OpenAIProtocolChatDriver::convert_tools(&[make_tool(json!({
        "type":"object","allOf":[{"type":"object"}]
    }))]);
    assert_eq!(
        serde_json::to_value(&incompatible).unwrap(),
        json!([{"type":"function","function":{"name":"lookup","description":"Lookup","parameters":{"type":"object","allOf":[{"type":"object"}]}}}])
    );
}
#[test]
fn the_request_capture_is_opt_in_and_is_what_goes_on_the_wire() {
    let request = OpenAiRequest {
        model: "gpt-5-mini".into(),
        messages: vec![],
        temperature: Some(0.5),
        max_tokens: Some(64),
        max_completion_tokens: None,
        stream: true,
        stream_options: None,
        tools: None,
        parallel_tool_calls: None,
        reasoning_effort: None,
        service_tier: None,
        verbosity: None,
        metadata: None,
        response_format: None,
    };

    let mut config = call_config();
    assert_eq!(
        capture_request_body(&config, &request),
        None,
        "the prompt is not recorded unless the caller asked"
    );

    config.capture_request = true;
    let captured = capture_request_body(&config, &request).expect("the body is captured");
    // The same value the wire gets, so the capture cannot drift from it.
    assert_eq!(captured, serde_json::to_value(&request).unwrap());
    assert_eq!(captured["model"], json!("gpt-5-mini"));
    assert_eq!(captured["temperature"], json!(0.5));
    // Authentication travels in headers; nothing credential-shaped is here.
    assert!(captured.get("api_key").is_none());
}

fn call_config() -> LlmCallConfig {
    LlmCallConfig::new("model")
}
async fn mock_provider(sse: &str) -> (wiremock::MockServer, crate::Provider) {
    use wiremock::matchers::{header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};
    // A pooled server can reuse connections owned by an earlier test runtime.
    let server = MockServer::builder().start().await;
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .and(header("authorization", "Bearer synthetic-key"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("content-type", "text/event-stream")
                .set_body_string(sse),
        )
        .expect(1)
        .mount(&server)
        .await;
    let provider = crate::Provider::new(
        "test",
        OpenAIProtocolChatDriver::new().with_retry_config(LlmRetryConfig {
            max_retries: 0,
            ..Default::default()
        }),
    )
    .base_url(format!("{}/v1", server.uri()))
    .auth(crate::BearerAuth::new("synthetic-key"));
    (server, provider)
}

async fn mock_json_provider(body: serde_json::Value) -> (wiremock::MockServer, crate::Provider) {
    use wiremock::matchers::{header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};
    let server = MockServer::builder().start().await;
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .and(header("authorization", "Bearer synthetic-key"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("content-type", "application/json")
                .set_body_json(body),
        )
        .expect(1)
        .mount(&server)
        .await;
    let provider = crate::Provider::new(
        "test",
        OpenAIProtocolChatDriver::new().with_retry_config(LlmRetryConfig {
            max_retries: 0,
            ..Default::default()
        }),
    )
    .base_url(format!("{}/v1", server.uri()))
    .auth(crate::BearerAuth::new("synthetic-key"));
    (server, provider)
}

#[tokio::test]
async fn non_streaming_completion_waits_for_full_json_response() {
    let (server, provider) = mock_json_provider(json!({
        "id": "chatcmpl-123",
        "model": "gpt-5.2-2026-09-01",
        "choices": [{
            "message": {"role": "assistant", "content": "done"},
            "finish_reason": "stop"
        }],
        "usage": {"prompt_tokens": 10, "completion_tokens": 3}
    }))
    .await;
    assert!(provider.supports_native_non_streaming());
    let messages = vec![Message::text(MessageRole::User, "Hello")];
    let response = provider
        .chat_completion_non_streaming(messages, &call_config())
        .await
        .unwrap();
    assert_eq!(response.text, "done");
    assert_eq!(response.metadata.finish_reason.as_deref(), Some("stop"));
    assert_eq!(
        response.metadata.response_id.as_deref(),
        Some("chatcmpl-123")
    );
    assert_eq!(
        response.metadata.response_model.as_deref(),
        Some("gpt-5.2-2026-09-01")
    );
    assert_eq!(response.metadata.prompt_tokens, Some(10));
    assert_eq!(response.metadata.completion_tokens, Some(3));
    assert!(response.tool_calls.is_none());
    let requests = server.received_requests().await.unwrap();
    assert_eq!(requests.len(), 1);
    let sent = requests[0].body_json::<Value>().unwrap();
    assert_eq!(sent["stream"], json!(false));
    assert!(sent.get("stream_options").is_none());
}

/// Mistral's non-streamed reasoning answer: `content` is a chunk array with
/// the thinking first. Verbatim shape from `mistral-large-4`.
#[tokio::test]
async fn non_streaming_completion_reads_mistral_thinking_chunks() {
    let (_server, provider) = mock_json_provider(json!({
        "id": "3ffd67b5",
        "model": "mistral-large-4",
        "choices": [{
            "index": 0,
            "finish_reason": "stop",
            "message": {"role": "assistant", "tool_calls": null, "content": [
                {"type": "thinking", "thinking": [{"type": "text", "text": "17 x 23 = 391"}], "closed": true},
                {"type": "text", "text": "391"}
            ]}
        }],
        "usage": {"prompt_tokens": 24, "completion_tokens": 136}
    }))
    .await;
    let messages = vec![Message::text(MessageRole::User, "17*23?")];
    let response = provider
        .chat_completion_non_streaming(messages, &call_config())
        .await
        .unwrap();
    assert_eq!(response.text, "391");
    assert_eq!(response.reasoning.len(), 1);
    assert_eq!(
        response.reasoning[0].display_text().as_deref(),
        Some("17 x 23 = 391")
    );
}

#[tokio::test]
async fn non_streaming_completion_maps_tool_calls_and_drops_malformed_arguments() {
    let (server, provider) = mock_json_provider(json!({
        "id": "chatcmpl-456",
        "choices": [{
            "message": {
                "role": "assistant",
                "content": null,
                "tool_calls": [
                    {"id": "call_1", "type": "function",
                     "function": {"name": "get_weather", "arguments": "{\"city\":\"Oslo\"}"}},
                    {"id": "call_2", "type": "function",
                     "function": {"name": "broken", "arguments": "{oops"}}
                ]
            },
            "finish_reason": "tool_calls"
        }],
        "usage": {"prompt_tokens": 20, "completion_tokens": 15}
    }))
    .await;
    let messages = vec![Message::text(MessageRole::User, "Weather?")];
    let response = provider
        .chat_completion_non_streaming(messages, &call_config())
        .await
        .unwrap();
    let tool_calls = response.tool_calls.expect("tool calls survive");
    assert_eq!(tool_calls.len(), 1);
    assert_eq!(tool_calls[0].name, "get_weather");
    assert_eq!(tool_calls[0].arguments, json!({"city": "Oslo"}));
    assert_eq!(
        response.metadata.finish_reason.as_deref(),
        Some("tool_calls")
    );
    let requests = server.received_requests().await.unwrap();
    assert_eq!(
        requests[0].body_json::<Value>().unwrap()["stream"],
        json!(false)
    );
}

#[tokio::test]
async fn request_options_reach_complete_http_payload_without_reimplementing_filters() {
    for (reasoning, parallel, tier, verbosity) in [
        (Some(crate::model::ReasoningEffort::None), None, None, None),
        (
            Some(crate::model::ReasoningEffort::High),
            Some(true),
            Some("priority"),
            Some("high"),
        ),
        (None, Some(false), Some("flex"), Some("low")),
    ] {
        let (server, provider) = mock_provider("data: [DONE]\n\n").await;
        let mut config = call_config();
        config.reasoning_effort = reasoning;
        config.parallel_tool_calls = parallel;
        config.speed = tier.map(str::to_string);
        config.verbosity = verbosity.map(str::to_string);
        config.temperature = Some(0.5);
        config.max_tokens = Some(128);
        if tier.is_some() {
            config
                .metadata
                .insert("session_id".into(), "session_abc123".into());
            config
                .metadata
                .insert("agent_id".into(), "agent_xyz789".into());
        }
        let messages = vec![
            Message::text(MessageRole::System, "A"),
            Message::text(MessageRole::User, "Hello"),
            Message::text(MessageRole::System, "B"),
        ];
        provider.chat_completion(messages, &config).await.unwrap();
        let requests = server.received_requests().await.unwrap();
        assert_eq!(requests.len(), 1);
        let mut expected = json!({"model":"model","messages":[{"role":"system","content":"A"},{"role":"user","content":"Hello"},{"role":"system","content":"B"}],"temperature":0.5,"max_tokens":128,"stream":true,"stream_options":{"include_usage":true}});
        if tier == Some("priority") {
            expected["reasoning_effort"] = json!("high");
        }
        if let Some(value) = parallel {
            expected["parallel_tool_calls"] = json!(value);
        }
        if let Some(value) = tier {
            expected["service_tier"] = json!(value);
            expected["metadata"] = json!({"session_id":"session_abc123","agent_id":"agent_xyz789"});
        }
        if let Some(value) = verbosity {
            expected["verbosity"] = json!(value);
        }
        assert_eq!(requests[0].body_json::<Value>().unwrap(), expected);
    }
}

#[tokio::test]
async fn streamed_usage_and_first_id_reach_completion_metadata() {
    for (reported, usage_index) in [(0, 0), (2, 1), (3, 2)] {
        let mut chunks = [
            json!({"choices":[{"delta":{"content":"Hello"}}]}),
            json!({"choices":[{"delta":{"content":"!"},"finish_reason":"stop"}]}),
            json!({"choices":[]}),
        ];
        chunks[usage_index]["usage"] = json!({"prompt_tokens":10,"completion_tokens":reported});
        let sse = chunks
            .iter()
            .map(|chunk| format!("data: {chunk}\n\n"))
            .collect::<String>()
            + "data: [DONE]\n\n";
        let (_server, provider) = mock_provider(&sse).await;
        let response = provider
            .chat_completion(vec![], &call_config())
            .await
            .unwrap();
        assert_eq!(response.text, "Hello!");
        assert_eq!(response.metadata.completion_tokens, Some(reported));
        assert_eq!(response.metadata.total_tokens, Some(10 + reported));
    }
    for (usage, expected) in [
        (
            json!({"prompt_tokens":150,"completion_tokens":42}),
            (150, 42, None, None, 192),
        ),
        (
            json!({"prompt_tokens":150,"completion_tokens":42,"prompt_tokens_details":{"cached_tokens":100}}),
            (50, 42, Some(100), None, 192),
        ),
        (
            json!({"prompt_tokens":194,"completion_tokens":2,"cost":0.00095}),
            (194, 2, None, Some(0.00095), 196),
        ),
        (
            json!({"prompt_tokens":10,"completion_tokens":5,"cost":0.0}),
            (10, 5, None, Some(0.0), 15),
        ),
    ] {
        let sse = format!(
            "data: {}\n\ndata: {}\n\ndata: {}\n\ndata: [DONE]\n\n",
            json!({"choices":[{"delta":{"content":"Hello"}}]}),
            json!({"id":"first-id","choices":[{"delta":{},"finish_reason":"stop"}]}),
            json!({"id":"later-id","choices":[],"usage":usage})
        );
        let (_server, provider) = mock_provider(&sse).await;
        let response = provider
            .chat_completion(vec![], &call_config())
            .await
            .unwrap();
        assert_eq!(response.text, "Hello");
        assert!(response.tool_calls.is_none());
        assert!(response.reasoning.is_empty());
        let meta = response.metadata;
        assert_eq!(
            (
                meta.prompt_tokens,
                meta.completion_tokens,
                meta.cache_read_tokens,
                meta.provider_cost_usd
            ),
            (Some(expected.0), Some(expected.1), expected.2, expected.3)
        );
        assert_eq!(meta.total_tokens, Some(expected.4));
        assert_eq!(meta.response_id.as_deref(), Some("first-id"));
        assert_eq!(meta.finish_reason.as_deref(), Some("stop"));
        assert_eq!(meta.model.as_deref(), Some("model"));
        assert!(meta.cache_creation_tokens.is_none());
        assert!(meta.retry_metadata.is_none());
    }
    let (_server, provider) = mock_provider(
        "data: {\"choices\":[{\"delta\":{\"content\":\"text\"}}]}\n\ndata: [DONE]\n\n",
    )
    .await;
    let response = provider
        .chat_completion(vec![], &call_config())
        .await
        .unwrap();
    assert_eq!(response.metadata.response_id, None);
    assert_eq!(response.metadata.response_model, None);
    assert_eq!(response.metadata.completion_tokens, Some(1));
    assert_eq!(response.metadata.provider_cost_usd, None);
}

#[tokio::test]
async fn terminal_reasons_survive_final_deltas_and_block_truncated_tool_execution() {
    for reason in ["length", "content_filter"] {
        for delta in [
            json!({"content":"partial"}),
            json!({"tool_calls":[{"index":0,"id":"call","function":{"name":"run","arguments":"{}"}}]}),
        ] {
            let sse = format!(
                "data: {}\n\ndata: [DONE]\n\n",
                json!({"choices":[{"delta":delta,"finish_reason":reason}]})
            );
            let (_server, provider) = mock_provider(&sse).await;
            let response = provider
                .chat_completion(vec![], &call_config())
                .await
                .unwrap();
            assert_eq!(response.metadata.finish_reason.as_deref(), Some(reason));
            assert!(
                response.tool_calls.is_none(),
                "truncated/rejected calls must not execute"
            );
            // ...but the discard is reported, not silent.
            let dropped = u32::from(delta.get("tool_calls").is_some());
            assert_eq!(response.metadata.tool_calls_dropped, dropped);
            assert_eq!(
                response.metadata.provider_finish_reason.as_deref(),
                Some(reason)
            );
            assert_eq!(
                response.text,
                if delta.get("content").is_some() {
                    "partial"
                } else {
                    ""
                }
            );
        }
    }
}

#[test]
fn azure_host_detection_rejects_lookalikes_and_ignores_url_components() {
    for (url, azure) in [
        (
            "https://example.openai.azure.com/openai/v1/chat/completions",
            true,
        ),
        (
            "https://example.services.ai.azure.com/openai/v1/responses",
            true,
        ),
        ("https://EXAMPLE.OPENAI.AZURE.COM:8443/path?x=1", true),
        ("https://api.openai.com/v1/chat/completions", false),
        ("https://example.openai.azure.com.evil.test/path", false),
        ("https://example.openai.azure.com@evil.test/", false),
        ("https://evil.test/?host=example.openai.azure.com", false),
        ("not a URL", false),
    ] {
        assert_eq!(is_azure_openai_api_url(url), azure, "{url}");
    }
}

#[test]
fn orphan_filter_preserves_complete_matched_transcript_and_rejects_missing_ids() {
    use crate::tool_types::ToolCall;
    let mut assistant = Message::text(MessageRole::Assistant, "");
    assistant.tool_calls = Some(vec![ToolCall {
        id: "call".into(),
        name: "read_file".into(),
        arguments: json!({"path":"a"}),
    }]);
    let tool = |id: Option<&str>, text: &str| {
        let mut m = Message::text(MessageRole::Tool, text);
        m.tool_call_id = id.map(str::to_string);
        m
    };
    let messages = vec![
        Message::text(MessageRole::User, "hello"),
        assistant,
        tool(Some("call"), "file content"),
        tool(Some("trimmed"), "orphan"),
        tool(None, "missing id"),
    ];
    let filtered = drop_orphaned_tool_messages(&messages);
    let wire: Vec<_> = filtered
        .iter()
        .map(crate::openai_message_convert::convert_message)
        .collect();
    assert_eq!(
        serde_json::to_value(wire).unwrap(),
        json!([
            {"role":"user","content":"hello"},
            {"role":"assistant","content":"","tool_calls":[{"id":"call","type":"function","function":{"name":"read_file","arguments":"{\"path\":\"a\"}"}}]},
            {"role":"tool","content":"file content","tool_call_id":"call"}
        ])
    );
    let only_user = drop_orphaned_tool_messages(&[
        messages[0].clone(),
        messages[3].clone(),
        messages[4].clone(),
    ]);
    assert_eq!(
        serde_json::to_value(
            only_user
                .iter()
                .map(crate::openai_message_convert::convert_message)
                .collect::<Vec<_>>()
        )
        .unwrap(),
        json!([{"role":"user","content":"hello"}])
    );
}
#[test]
fn discovery_urls_preserve_origin_queries_and_custom_paths() {
    for (input, expected) in [
        (
            "https://api.openai.com/v1/responses",
            "https://api.openai.com/v1/models",
        ),
        (
            "https://openrouter.ai/api/v1/responses?route=a%20b#section",
            "https://openrouter.ai/api/v1/models?route=a%20b#section",
        ),
        (
            "https://api.fireworks.ai/inference/v1/chat/completions/",
            "https://api.fireworks.ai/inference/v1/models",
        ),
        (
            "https://api.meta.ai/v1/models?x=1",
            "https://api.meta.ai/v1/models?x=1",
        ),
        (
            "https://resource.openai.azure.com/custom?api-version=preview",
            "https://resource.openai.azure.com/custom/models?api-version=preview",
        ),
        (
            "https://resource.services.ai.azure.com/openai/v1/?api-version=preview",
            "https://resource.services.ai.azure.com/openai/v1/models?api-version=preview",
        ),
        (
            "https://proxy.example:8443/tenant%20one",
            "https://proxy.example:8443/tenant%20one/models",
        ),
        ("https://proxy.example", "https://proxy.example/models"),
        ("not a URL", "not a URL"),
    ] {
        assert_eq!(models_url_for_api_url(input), expected, "{input}");
    }
}

#[test]
fn models_status_error_classifies_auth_separately_from_outage() {
    // Credential checks branch on the kind, so a rejected key and a dead
    // provider must not collapse into the same classification.
    assert_eq!(
        models_api_status_error(reqwest::StatusCode::UNAUTHORIZED).llm_error_kind(),
        Some(LlmErrorKind::Authentication)
    );
    assert_eq!(
        models_api_status_error(reqwest::StatusCode::FORBIDDEN).llm_error_kind(),
        Some(LlmErrorKind::Authentication)
    );
    assert_eq!(
        models_api_status_error(reqwest::StatusCode::SERVICE_UNAVAILABLE).llm_error_kind(),
        Some(LlmErrorKind::Unavailable)
    );
}

#[test]
fn file_part_serializes_to_openai_file() {
    let part = OpenAiContentPart::File {
        r#type: "file".to_string(),
        file: OpenAiFile {
            filename: Some("report.pdf".to_string()),
            file_data: "data:application/pdf;base64,JVBERi0=".to_string(),
        },
    };
    let v = serde_json::to_value(&part).unwrap();
    assert_eq!(v["type"], serde_json::json!("file"));
    assert_eq!(
        v["file"]["file_data"],
        serde_json::json!("data:application/pdf;base64,JVBERi0=")
    );
    assert_eq!(v["file"]["filename"], serde_json::json!("report.pdf"));
}