vtcode-llm 0.181.2

LLM provider abstraction, client implementations, and streaming for VT Code
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
use super::*;

#[test]
fn non_streaming_capability_is_pinned_for_stream_timeout_fallback() {
    // The harness's stream-timeout retry falls back to non-streaming only
    // when this capability is advertised; losing it silently re-streams
    // every retry into the first-token timeout watchdog. Streaming-only
    // routes (`zai/`) are the intentional exception: they reject
    // non-streaming with `streaming_only`, so advertising the fallback would
    // re-hit the known-failing mode.
    let provider = test_provider("http://127.0.0.1:1");
    for model in models::merge_gateway::SUPPORTED_MODELS {
        if is_streaming_only_model(model) {
            assert!(
                !LLMProvider::supports_non_streaming(&provider, model),
                "streaming-only route {model} must not advertise non-streaming"
            );
        } else {
            assert!(
                LLMProvider::supports_non_streaming(&provider, model),
                "route {model} must advertise non-streaming fallback capability"
            );
        }
    }
}

#[test]
fn streaming_only_detection_matches_gateway_body() {
    let body = r#"{"error":{"type":"provider_error","message":"Only streaming requests are supported. Set stream to true.","code":"streaming_only","param":"stream","source":"provider","provider":"zai"}}"#;
    assert!(is_streaming_only_rejection(StatusCode::BAD_REQUEST, body));
    assert!(is_streaming_only_rejection(StatusCode::UNPROCESSABLE_ENTITY, body));
    assert!(!is_streaming_only_rejection(StatusCode::BAD_REQUEST, r#"{"error":{"code":"invalid_parameter"}}"#));
    assert!(!is_streaming_only_rejection(StatusCode::INTERNAL_SERVER_ERROR, body));
    assert!(!is_streaming_only_rejection(StatusCode::BAD_REQUEST, ""));
    // Disjoint from the other classifiers.
    assert!(!is_capability_unavailable(StatusCode::BAD_REQUEST, body));
    assert!(!is_merge_tier_pricing_rejection(StatusCode::BAD_REQUEST, body));
}

#[test]
fn streaming_only_model_matches_zai_prefix() {
    assert!(is_streaming_only_model(models::merge_gateway::ZAI_GLM_5_3_FLASH));
    assert!(is_streaming_only_model(models::merge_gateway::ZAI_GLM_5_3_FLASHX));
    assert!(is_streaming_only_model("zai/glm-future"));
    assert!(!is_streaming_only_model(models::merge_gateway::OPENAI_GPT_6_1_SOL));
    assert!(!is_streaming_only_model(models::merge_gateway::DEFAULT_ROUTING));
}

#[test]
fn native_payload_maps_openai_service_tiers_and_omits_ultrafast() {
    // Merge Gateway only accepts `standard`/`flex`/`priority`; OpenAI's
    // `ultrafast` has no equivalent and must be omitted (gateway default
    // routing) rather than 422ing on `literal_error`.
    assert_eq!(map_openai_service_tier_for_merge("flex"), Some("flex"));
    assert_eq!(map_openai_service_tier_for_merge("priority"), Some("priority"));
    assert_eq!(map_openai_service_tier_for_merge("standard"), Some("standard"));
    assert_eq!(map_openai_service_tier_for_merge("Flex"), Some("flex"));
    assert_eq!(map_openai_service_tier_for_merge("ultrafast"), None);

    let provider =
        MergeGatewayProvider::with_model("test-key".to_string(), models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string());
    let mut ultrafast = LLMRequest {
        messages: vec![Message::user("hello".to_string())].into(),
        model: models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string(),
        service_tier: Some("ultrafast".to_string()),
        ..Default::default()
    };
    let payload = provider.build_native_payload(&ultrafast, false).expect("payload");
    assert!(payload.get("service_tier").is_none(), "ultrafast must be omitted for Merge Gateway routes");

    ultrafast.service_tier = Some("priority".to_string());
    let payload = provider.build_native_payload(&ultrafast, false).expect("payload");
    assert_eq!(payload.get("service_tier").and_then(Value::as_str), Some("priority"));
}

#[test]
fn tier_pricing_rejection_matches_unpriced_route_body() {
    // Merge fail-closed 400 for valid-but-unpriced tiers (service-tiers docs).
    let body = r#"{"error":{"type":"invalid_request_error","message":"Model 'openai/gpt-6.1-sol' does not support service tier 'flex'.","source":"gateway"}}"#;
    assert!(is_merge_tier_pricing_rejection(StatusCode::BAD_REQUEST, body));
    assert!(is_merge_tier_pricing_rejection(
        StatusCode::BAD_REQUEST,
        r#"{"error":{"message":"Model 'openai/gpt-6-sol' does not support service tier 'priority'.","source":"gateway"}}"#
    ));
    // Disjoint from capability routing: capability bodies never name the tier field.
    assert!(!is_merge_tier_pricing_rejection(
        StatusCode::BAD_REQUEST,
        "has no vendor that supports the requested capabilities (['tools'])"
    ));
    assert!(!is_merge_tier_pricing_rejection(StatusCode::UNPROCESSABLE_ENTITY, body));
    assert!(!is_merge_tier_pricing_rejection(StatusCode::BAD_REQUEST, ""));
}

#[tokio::test]
async fn native_generate_retries_without_tier_on_pricing_rejection() {
    use std::sync::Mutex;

    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());
    let seen = Arc::new(Mutex::new(Vec::new()));
    let seen_for_mock = Arc::clone(&seen);

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(move |req: &wiremock::Request| {
            let payload: Value = serde_json::from_slice(&req.body).expect("valid json body");
            let tier = payload.get("service_tier").and_then(Value::as_str).map(ToOwned::to_owned);
            seen_for_mock.lock().expect("mutex not poisoned").push(tier.clone());
            match tier.as_deref() {
                Some("flex") => ResponseTemplate::new(400).set_body_json(json!({
                    "error": {
                        "type": "invalid_request_error",
                        "message": "Model 'openai/gpt-6.1-sol' does not support service tier 'flex'.",
                        "source": "gateway"
                    }
                })),
                None => ResponseTemplate::new(200).set_body_json(json!({
                    "id": "resp_retry",
                    "object": "response",
                    "created_at": "2026-03-23T12:03:00Z",
                    "model": "openai/gpt-6.1-sol",
                    "output": [{
                        "type": "message",
                        "id": "msg_1",
                        "role": "assistant",
                        "content": [{"type": "text", "text": "Hello"}],
                        "finish_reason": "stop"
                    }],
                    "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15, "cost": 0.01}
                })),
                other => ResponseTemplate::new(500).set_body_string(format!("unexpected tier: {other:?}")),
            }
        })
        .expect(2)
        .mount(&server)
        .await;

    let response = provider
        .generate(LLMRequest {
            messages: vec![Message::user("hello".to_string())].into(),
            model: models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string(),
            service_tier: Some("flex".to_string()),
            ..Default::default()
        })
        .await
        .expect("pricing retry without tier should succeed");
    assert_eq!(response.content.as_deref(), Some("Hello"));
    assert_eq!(seen.lock().expect("mutex not poisoned").as_slice(), &[Some("flex".to_string()), None]);
}

#[test]
fn capability_unavailable_detection_matches_gateway_body() {
    let body = r#"{"error":{"type":"invalid_request_error","message":"Model 'openai/gpt-6.1-sol' has no vendor that supports the requested capabilities (['streaming_tools', 'tools']).","source":"gateway","code":"capability_unavailable","param":"model"}}"#;
    assert!(is_capability_unavailable(StatusCode::BAD_REQUEST, body));
    assert!(is_capability_unavailable(StatusCode::UNPROCESSABLE_ENTITY, body));
    assert!(!is_capability_unavailable(StatusCode::BAD_REQUEST, r#"{"error":{"code":"invalid_parameter"}}"#));
    assert!(!is_capability_unavailable(StatusCode::INTERNAL_SERVER_ERROR, body));
    assert!(!is_capability_unavailable(StatusCode::BAD_REQUEST, ""));
}

#[test]
fn reasoning_capability_rejection_attribution_matches_gateway_body() {
    let reasoning_body = r#"{"error":{"type":"invalid_request_error","message":"Model 'xiaomimimo/mimo-v2.6-flash' has no vendor that supports the requested capabilities (['reasoning', 'tools']).","source":"gateway","code":"capability_unavailable","param":"model"}}"#;
    assert!(is_reasoning_capability_rejection(reasoning_body));
    let tools_body = r#"{"error":{"type":"invalid_request_error","message":"Model 'openai/gpt-6.1-sol' has no vendor that supports the requested capabilities (['streaming_tools', 'tools']).","source":"gateway","code":"capability_unavailable","param":"model"}}"#;
    assert!(!is_reasoning_capability_rejection(tools_body));
    assert!(!is_reasoning_capability_rejection(r#"{"error":{"code":"invalid_parameter"}}"#));
    assert!(!is_reasoning_capability_rejection(""));
}

#[test]
fn native_payload_omits_reasoning_for_haiku_5_5_route() {
    // Like the `xiaomimimo/` routes, the gateway has no vendor serving
    // reasoning jointly with tools for `anthropic/claude-haiku-5-5`
    // (`capability_unavailable` for `['reasoning', 'tools']`), so the route
    // stays unclassified and reasoning controls are omitted.
    let provider = test_provider("http://127.0.0.1:1");
    let model = models::merge_gateway::ANTHROPIC_CLAUDE_HAIKU_5_5;
    assert!(!provider.supports_reasoning(model), "{model} must not advertise reasoning");
    assert!(!provider.supports_reasoning_effort(model), "{model} must not advertise reasoning effort");
    let payload = provider
        .build_native_payload(
            &LLMRequest {
                messages: vec![Message::user("hello".to_string())].into(),
                model: model.to_string(),
                reasoning_effort: Some(vtcode_config::types::ReasoningEffortLevel::High),
                max_tokens: Some(4096),
                ..Default::default()
            },
            false,
        )
        .expect("payload builds");
    assert!(payload.get("reasoning_effort").is_none(), "{model} must not forward reasoning_effort");
    assert!(payload.get("thinking").is_none(), "{model} must not forward thinking");
}

#[test]
fn haiku_routes_advertise_vision_matching_catalog() {
    // Both gateway Haiku entries declare image input in `docs/models.json`;
    // the provider must agree so image payloads are not dropped client-side.
    let provider = test_provider("http://127.0.0.1:1");
    for model in [
        models::merge_gateway::ANTHROPIC_CLAUDE_HAIKU_4_5_20251001,
        models::merge_gateway::ANTHROPIC_CLAUDE_HAIKU_5_5,
    ] {
        assert!(provider.supports_vision(model), "{model} must advertise vision");
    }
}

#[test]
fn native_payload_omits_reasoning_for_xiaomimimo_routes() {
    // The gateway has no vendor serving reasoning jointly with tools for
    // `xiaomimimo/` routes: forwarding `thinking` turns every agentic request
    // into a `capability_unavailable` rejection, so the routes stay
    // unclassified and reasoning controls are omitted.
    let provider = test_provider("http://127.0.0.1:1");
    for model in [
        models::merge_gateway::XIAOMIMIMO_MIMO_V2_6_PRO,
        models::merge_gateway::XIAOMIMIMO_MIMO_V2_6_FLASH,
    ] {
        assert!(!provider.supports_reasoning(model), "{model} must not advertise reasoning");
        assert!(!provider.supports_reasoning_effort(model), "{model} must not advertise reasoning effort");
        let payload = provider
            .build_native_payload(
                &LLMRequest {
                    messages: vec![Message::user("hello".to_string())].into(),
                    model: model.to_string(),
                    reasoning_effort: Some(vtcode_config::types::ReasoningEffortLevel::High),
                    max_tokens: Some(4096),
                    ..Default::default()
                },
                false,
            )
            .expect("payload builds");
        assert!(payload.get("reasoning_effort").is_none(), "{model} must not forward reasoning_effort");
        assert!(payload.get("thinking").is_none(), "{model} must not forward thinking");
    }
}

#[tokio::test]
async fn reasoning_capability_rejection_does_not_poison_tool_vendor_cache() {
    use std::sync::Mutex;

    // A rejection naming `reasoning` (e.g. `(['reasoning', 'tools'])`)
    // blames the combination, not tools alone: later turns must re-probe the
    // network instead of failing fast on a cached no-tool-vendor verdict.
    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());
    let seen_thinking = Arc::new(Mutex::new(Vec::new()));
    let seen_for_mock = Arc::clone(&seen_thinking);

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(move |req: &wiremock::Request| {
            let payload: Value = serde_json::from_slice(&req.body).expect("valid json body");
            seen_for_mock.lock().expect("mutex not poisoned").push(payload.get("thinking").is_some());
            ResponseTemplate::new(400).set_body_json(json!({
                "error": {
                    "type": "invalid_request_error",
                    "message": "Model 'deepseek/deepseek-v4.1-flash-1017' has no vendor that supports the requested capabilities (['reasoning', 'tools']).",
                    "source": "gateway",
                    "code": "capability_unavailable",
                    "param": "model"
                }
            }))
        })
        .expect(2)
        .mount(&server)
        .await;

    let tool_request = || LLMRequest {
        messages: vec![Message::user("hello".to_string())].into(),
        model: models::merge_gateway::DEEPSEEK_FLASH.to_string(),
        tools: Some(Arc::new(vec![ToolDefinition::function(
            "get_weather".to_string(),
            "Get weather".to_string(),
            json!({"type": "object", "properties": {}}),
        )])),
        reasoning_effort: Some(vtcode_config::types::ReasoningEffortLevel::Medium),
        ..Default::default()
    };
    let first = provider
        .generate(tool_request())
        .await
        .expect_err("reasoning+tools rejection must fail");
    assert!(first.to_string().contains("capability"), "got: {first}");
    assert!(!first.to_string().contains("cached"), "first failure must not claim a cached verdict, got: {first}");

    let second = provider
        .generate(tool_request())
        .await
        .expect_err("second turn must re-probe, not fail fast");
    assert!(
        !second.to_string().contains("cached"),
        "reasoning-caused rejection must not poison the tool-vendor cache, got: {second}"
    );

    // Both turns sent the thinking block that triggered the joint rejection.
    assert_eq!(seen_thinking.lock().expect("mutex not poisoned").as_slice(), &[true, true]);
}

#[tokio::test]
async fn native_stream_retries_without_streaming_on_capability_unavailable() {
    use std::sync::Mutex;

    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());
    let seen = Arc::new(Mutex::new(Vec::new()));
    let seen_for_mock = Arc::clone(&seen);

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(move |req: &wiremock::Request| {
            let payload: Value = serde_json::from_slice(&req.body).expect("valid json body");
            let streaming = payload.get("stream").and_then(Value::as_bool).unwrap_or(false);
            let has_tools = payload.get("tools").is_some();
            seen_for_mock.lock().expect("mutex not poisoned").push((streaming, has_tools));
            if streaming {
                ResponseTemplate::new(400).set_body_json(json!({
                    "error": {
                        "type": "invalid_request_error",
                        "message": "Model 'openai/gpt-6.1-sol' has no vendor that supports the requested capabilities (['streaming_tools', 'tools']).",
                        "source": "gateway",
                        "code": "capability_unavailable",
                        "param": "model"
                    }
                }))
            } else {
                ResponseTemplate::new(200).set_body_json(json!({
                    "id": "resp_retry",
                    "object": "response",
                    "created_at": "2026-03-23T12:03:00Z",
                    "model": "openai/gpt-6.1-sol",
                    "output": [{
                        "type": "message",
                        "id": "msg_1",
                        "role": "assistant",
                        "content": [{"type": "text", "text": "Hello"}],
                        "finish_reason": "stop"
                    }],
                    "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15, "cost": 0.01}
                }))
            }
        })
        .expect(2)
        .mount(&server)
        .await;

    let request = LLMRequest {
        messages: vec![Message::user("hello".to_string())].into(),
        model: models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string(),
        tools: Some(Arc::new(vec![ToolDefinition::function(
            "get_weather".to_string(),
            "Get weather".to_string(),
            json!({"type": "object", "properties": {}}),
        )])),
        ..Default::default()
    };
    let stream = provider.stream(request).await.expect("stream with fallback");
    let events = stream.collect::<Vec<_>>().await;
    let completed = events
        .into_iter()
        .find_map(|event| match event.expect("stream event") {
            LLMStreamEvent::Completed { response } => Some(response),
            _ => None,
        })
        .expect("fallback stream must complete");
    assert_eq!(completed.content.as_deref(), Some("Hello"));

    // First attempt streams with tools; the retry keeps tools but drops streaming.
    assert_eq!(seen.lock().expect("mutex not poisoned").as_slice(), &[(true, true), (false, true)]);
}

#[tokio::test]
async fn native_generate_retries_with_streaming_on_streaming_only() {
    use std::sync::Mutex;

    // `zai/` routes reject non-streaming `/responses` with 400
    // `streaming_only`. `generate` must retry once via streaming and return
    // the collected response instead of surfacing the transport mismatch.
    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());
    let seen = Arc::new(Mutex::new(Vec::new()));
    let seen_for_mock = Arc::clone(&seen);

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(move |req: &wiremock::Request| {
            let payload: Value = serde_json::from_slice(&req.body).expect("valid json body");
            let streaming = payload.get("stream").and_then(Value::as_bool).unwrap_or(false);
            seen_for_mock.lock().expect("mutex not poisoned").push(streaming);
            if streaming {
                let body = [sse(
                    "response.completed",
                    json!({
                        "response": {
                            "id": "resp_stream_fallback",
                            "object": "response",
                            "created_at": "2026-03-23T12:03:00Z",
                            "model": "zai/glm-5.3-flash",
                            "output": [{
                                "type": "message",
                                "id": "msg_1",
                                "role": "assistant",
                                "content": [{"type": "text", "text": "Hello"}],
                                "finish_reason": "stop"
                            }],
                            "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15}
                        }
                    }),
                )]
                .concat();
                ResponseTemplate::new(200)
                    .insert_header("content-type", "text/event-stream")
                    .set_body_string(body)
            } else {
                ResponseTemplate::new(400).set_body_json(json!({
                    "error": {
                        "type": "provider_error",
                        "message": "Only streaming requests are supported. Set stream to true.",
                        "code": "streaming_only",
                        "param": "stream",
                        "source": "provider",
                        "provider": "zai"
                    }
                }))
            }
        })
        .expect(2)
        .mount(&server)
        .await;

    let response = provider
        .generate(LLMRequest {
            messages: vec![Message::user("hello".to_string())].into(),
            model: models::merge_gateway::ZAI_GLM_5_3_FLASH.to_string(),
            ..Default::default()
        })
        .await
        .expect("streaming-only retry should succeed");
    assert_eq!(response.content.as_deref(), Some("Hello"));
    assert_eq!(seen.lock().expect("mutex not poisoned").as_slice(), &[false, true]);
}

#[tokio::test]
async fn native_stream_does_not_downgrade_streaming_only_routes() {
    // A `capability_unavailable` streaming failure on a streaming-only route
    // must surface directly: downgrading to non-streaming is known to fail
    // with `streaming_only`, so the transport must not ping-pong.
    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({
            "error": {
                "type": "invalid_request_error",
                "message": "Model 'zai/glm-5.3-flash' has no vendor that supports the requested capabilities (['streaming_tools', 'tools']).",
                "source": "gateway",
                "code": "capability_unavailable",
                "param": "model"
            }
        })))
        .expect(1)
        .mount(&server)
        .await;

    let result = provider
        .stream_normalized(LLMRequest {
            messages: vec![Message::user("hello".to_string())].into(),
            model: models::merge_gateway::ZAI_GLM_5_3_FLASH.to_string(),
            tools: Some(Arc::new(vec![ToolDefinition::function(
                "get_weather".to_string(),
                "Get weather".to_string(),
                json!({"type": "object", "properties": {}}),
            )])),
            ..Default::default()
        })
        .await;
    let err = match result {
        Ok(_) => panic!("streaming-only capability failure must not downgrade"),
        Err(err) => err,
    };
    assert!(err.to_string().contains("capability"), "got: {err}");
}

#[tokio::test]
async fn repeated_tool_turns_fail_fast_after_first_capability_rejection() {
    use std::sync::Mutex;

    // The first turn burns one call proving the route has no tool vendor;
    // later turns must fail fast without touching the network.
    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({
            "error": {
                "type": "invalid_request_error",
                "message": "Model 'openai/gpt-6.1-sol' has no vendor that supports the requested capabilities (['tools']).",
                "source": "gateway",
                "code": "capability_unavailable",
                "param": "model"
            }
        })))
        .expect(1)
        .mount(&server)
        .await;

    let tool_request = || LLMRequest {
        messages: vec![Message::user("hello".to_string())].into(),
        model: models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string(),
        tools: Some(Arc::new(vec![ToolDefinition::function(
            "get_weather".to_string(),
            "Get weather".to_string(),
            json!({"type": "object", "properties": {}}),
        )])),
        ..Default::default()
    };
    let first = provider.generate(tool_request()).await.expect_err("no tool vendor must fail");
    assert!(first.to_string().contains("default_routing"));

    let second = provider.generate(tool_request()).await.expect_err("second turn must fail fast");
    assert!(second.to_string().contains("cached"), "fast-fail must say the verdict is cached, got: {second}");
}

#[tokio::test]
async fn terminal_capability_error_names_a_working_route() {
    // When even the non-streaming retry finds no tool vendor, the surfaced
    // error must tell the user the route (not the request) is at fault.
    let server = MockServer::start().await;
    let provider = test_provider(&server.uri());

    Mock::given(method("POST"))
        .and(path("/responses"))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({
            "error": {
                "type": "invalid_request_error",
                "message": "Model 'openai/gpt-6.1-sol' has no vendor that supports the requested capabilities (['tools']).",
                "source": "gateway",
                "code": "capability_unavailable",
                "param": "model"
            }
        })))
        .expect(1)
        .mount(&server)
        .await;

    let err = provider
        .generate(LLMRequest {
            messages: vec![Message::user("hello".to_string())].into(),
            model: models::merge_gateway::OPENAI_GPT_6_1_SOL.to_string(),
            tools: Some(Arc::new(vec![ToolDefinition::function(
                "get_weather".to_string(),
                "Get weather".to_string(),
                json!({"type": "object", "properties": {}}),
            )])),
            ..Default::default()
        })
        .await
        .expect_err("route with no tool vendor must fail closed");
    let text = err.to_string();
    assert!(text.contains("capability"), "error must preserve the gateway diagnostic, got: {text}");
    assert!(text.contains("default_routing"), "error must name a working route, got: {text}");
}