jamjet-models 0.5.0

JamJet model adapter layer — unified interface for LLM providers
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
//! Sidecar model adapter — POSTs to the Python model-seam sidecar.
//!
//! Set `JAMJET_MODEL_SEAM_URL` (e.g. `http://127.0.0.1:4280`) to route
//! durable-path model calls through the governed Python seam (provider
//! allow-list, PII redaction, cost metering, middleware).
//!
//! Sidecar contract:
//! - `POST /v1/complete` — `{model, messages, temperature?, max_tokens?}`
//!   → `{message:{content,role}, input_tokens, output_tokens, cost_usd, model, finish_reason}`
//! - `GET /health` → `{ok:true}`

use crate::adapter::{
    ChatRole, ModelAdapter, ModelError, ModelRequest, ModelResponse, StructuredRequest, ToolCall,
};
use async_trait::async_trait;
use serde_json::{json, Value};

const DEFAULT_MODEL: &str = "anthropic/claude-sonnet-4-6";

/// Cap a provider-supplied Retry-After so an untrusted/huge value can't overflow
/// the timestamp math or push the backoff into the past. One hour is plenty.
const MAX_RETRY_AFTER_SECS: u64 = 3_600;

/// Routes durable-path model calls to the Python model-seam sidecar via HTTP.
///
/// The sidecar wraps `jamjet.model.Model` (Track-1 seam), so every call
/// inherits the same governed middleware as in-process `Agent.run()`.
pub struct SidecarModelAdapter {
    client: reqwest::Client,
    base_url: String,
}

impl SidecarModelAdapter {
    /// Create an adapter that POSTs to `base_url` (e.g. `http://127.0.0.1:4280`).
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            client: reqwest::Client::new(),
            base_url: base_url.into(),
        }
    }

    async fn call_complete(&self, body: Value) -> Result<Value, ModelError> {
        let url = format!("{}/v1/complete", self.base_url);
        let resp = self
            .client
            .post(&url)
            .json(&body)
            .send()
            .await
            .map_err(|e| ModelError::Network(e.to_string()))?;

        let status = resp.status().as_u16();
        let text = resp
            .text()
            .await
            .map_err(|e| ModelError::Network(e.to_string()))?;

        if status == 429 {
            // Prefer the retry_after the sidecar extracts from the provider response
            // (passed back as `{"retry_after": <secs>}` in the body).  Fall back to
            // 60 s when the body is absent or unparseable — keeps the existing safe
            // default for responses that predate this contract.
            let retry_after_secs = serde_json::from_str::<Value>(&text)
                .ok()
                .and_then(|v| v["retry_after"].as_u64())
                .unwrap_or(60)
                .min(MAX_RETRY_AFTER_SECS);
            return Err(ModelError::RateLimited { retry_after_secs });
        }
        if status != 200 {
            return Err(ModelError::Api { status, body: text });
        }
        serde_json::from_str(&text).map_err(|e| ModelError::Serialization(e.to_string()))
    }

    fn parse_response(&self, json: Value) -> Result<ModelResponse, ModelError> {
        let content = json["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string();
        let model = json["model"].as_str().unwrap_or(DEFAULT_MODEL).to_string();
        let finish_reason = json["finish_reason"].as_str().unwrap_or("stop").to_string();

        // I4: parse token counts strictly — a missing or wrong-type field means the
        // metering data is corrupt; fail closed rather than silently zeroing the count.
        // Cost is recorded by the Python MeteringMiddleware after C1; cost_usd is not
        // threaded to the Rust event here.
        // F-2e-cost: cost_usd from the Python MeteringMiddleware is not yet threaded to the Rust event.
        let input_tokens = json["input_tokens"].as_u64().ok_or_else(|| {
            ModelError::Serialization(
                "sidecar response missing or invalid 'input_tokens' field".to_string(),
            )
        })?;
        let output_tokens = json["output_tokens"].as_u64().ok_or_else(|| {
            ModelError::Serialization(
                "sidecar response missing or invalid 'output_tokens' field".to_string(),
            )
        })?;

        // Parse tool_calls when the sidecar surfaces them (finish_reason == "tool_calls").
        // The sidecar normalises arguments to a JSON object when possible; we store
        // whatever Value arrives (object or string) so no information is lost.
        let tool_calls: Vec<ToolCall> = json["tool_calls"]
            .as_array()
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|tc| ToolCall {
                id: tc["id"].as_str().unwrap_or("").to_string(),
                name: tc["name"].as_str().unwrap_or("").to_string(),
                arguments: tc["arguments"].clone(),
            })
            .collect();

        // 2j fail-closed: when the model is requesting tool calls, this data drives
        // tool dispatch and tool_call_id correlation downstream. A missing/empty
        // array, or a call with a blank id or name, would silently degrade to
        // "no tool executed" or mismatched correlation — a provider/sidecar bug
        // must surface as an error here rather than as a wrong answer.
        if finish_reason == "tool_calls" {
            if tool_calls.is_empty() {
                return Err(ModelError::Serialization(
                    "sidecar finish_reason is 'tool_calls' but tool_calls is missing or empty"
                        .to_string(),
                ));
            }
            if let Some(bad) = tool_calls
                .iter()
                .find(|tc| tc.id.is_empty() || tc.name.is_empty())
            {
                return Err(ModelError::Serialization(format!(
                    "sidecar tool_call missing id or name (id={:?}, name={:?})",
                    bad.id, bad.name
                )));
            }
        }

        Ok(ModelResponse {
            content,
            model,
            finish_reason,
            input_tokens,
            output_tokens,
            structured: None,
            tool_calls,
        })
    }

    fn build_messages(
        messages: &[crate::adapter::ChatMessage],
        system_prompt: Option<&str>,
    ) -> Vec<Value> {
        let mut out: Vec<Value> = Vec::new();
        // Inject system prompt as leading system message if configured.
        if let Some(sys) = system_prompt {
            if !sys.is_empty() {
                out.push(json!({ "role": "system", "content": sys }));
            }
        }
        for m in messages {
            let role = match m.role {
                ChatRole::System => "system",
                ChatRole::User | ChatRole::Tool => "user",
                ChatRole::Assistant => "assistant",
            };
            out.push(json!({ "role": role, "content": m.content }));
        }
        out
    }
}

#[async_trait]
impl ModelAdapter for SidecarModelAdapter {
    fn system_name(&self) -> &'static str {
        "sidecar"
    }

    fn default_model(&self) -> &str {
        DEFAULT_MODEL
    }

    async fn chat(&self, request: ModelRequest) -> Result<ModelResponse, ModelError> {
        let model = request
            .config
            .model
            .clone()
            .unwrap_or_else(|| DEFAULT_MODEL.into());

        let messages =
            Self::build_messages(&request.messages, request.config.system_prompt.as_deref());

        let mut body = json!({
            "model": model,
            "messages": messages,
        });
        if let Some(temp) = request.config.temperature {
            body["temperature"] = json!(temp);
        }
        if let Some(max) = request.config.max_tokens {
            body["max_tokens"] = json!(max);
        }
        // Forward tool schemas to the sidecar when tools are offered; the governed
        // seam (allowlist + PII + metering middleware) still runs on the sidecar side.
        if !request.tools.is_empty() {
            body["tools"] = json!(request.tools);
        }

        let resp_json = self.call_complete(body).await?;
        self.parse_response(resp_json)
    }

    async fn structured_output(
        &self,
        request: StructuredRequest,
    ) -> Result<ModelResponse, ModelError> {
        // Append schema instruction to system prompt (mirrors AnthropicAdapter).
        let schema_str = serde_json::to_string_pretty(&request.output_schema)
            .map_err(|e| ModelError::Serialization(e.to_string()))?;
        let mut config = request.config.clone();
        let system = config.system_prompt.get_or_insert_with(String::new);
        system.push_str(&format!(
            "\n\nRespond ONLY with a valid JSON object matching this schema:\n{schema_str}\nDo not include any other text."
        ));

        let chat_req = ModelRequest {
            messages: request.messages,
            config,
            tools: vec![],
        };
        let mut response = self.chat(chat_req).await?;

        // Parse structured output from the response content.
        let structured = serde_json::from_str::<Value>(&response.content)
            .or_else(|_| {
                let trimmed = response.content.trim();
                let inner = trimmed
                    .trim_start_matches("```json")
                    .trim_start_matches("```")
                    .trim_end_matches("```")
                    .trim();
                serde_json::from_str::<Value>(inner)
            })
            .map_err(|e| {
                ModelError::Serialization(format!("failed to parse structured output: {e}"))
            })?;

        response.structured = Some(structured);
        Ok(response)
    }
}

// ── Coverage guard ────────────────────────────────────────────────────────────

/// Probe the sidecar `/health` endpoint at startup.
///
/// Returns `Err` with a descriptive message if the sidecar is unreachable or
/// responds with a non-2xx status — so a misconfigured deployment fails loud
/// rather than silently falling through to the native (ungoverned) adapters.
pub async fn check_sidecar_health(
    base_url: &str,
    client: &reqwest::Client,
) -> Result<(), ModelError> {
    let url = format!("{base_url}/health");
    let resp = client.get(&url).send().await.map_err(|e| {
        ModelError::Network(format!(
            "JAMJET_MODEL_SEAM_URL set but sidecar unreachable at {url}\
             refusing to start so model calls never silently bypass the governed seam. \
             Cause: {e}"
        ))
    })?;

    let status = resp.status();
    if !status.is_success() {
        let code = status.as_u16();
        let body = resp.text().await.unwrap_or_default();
        return Err(ModelError::Api {
            status: code,
            body: format!(
                "JAMJET_MODEL_SEAM_URL set but sidecar /health returned {code}\
                 refusing to start so model calls never silently bypass the governed seam. \
                 Body: {body}"
            ),
        });
    }

    // I3: also validate the JSON body — a wrong service returning 200 must not pass.
    // The sidecar contract guarantees {"ok": true}; anything else is treated as a failure.
    let body = resp
        .text()
        .await
        .map_err(|e| ModelError::Network(e.to_string()))?;
    let json: serde_json::Value = serde_json::from_str(&body).map_err(|_| {
        ModelError::Serialization(format!(
            "sidecar /health returned a non-JSON body — \
             refusing to start. Body: {body}"
        ))
    })?;
    if json.get("ok").and_then(|v| v.as_bool()) != Some(true) {
        return Err(ModelError::Api {
            status: status.as_u16(),
            body: format!(
                "sidecar /health did not return {{\"ok\":true}}\
                 refusing to start. Body: {body}"
            ),
        });
    }

    Ok(())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::ChatMessage;

    #[tokio::test]
    async fn chat_maps_response_fields() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": "Hello, world!", "role": "assistant"},
                "input_tokens": 10,
                "output_tokens": 5,
                "cost_usd": 0.001,
                "model": "anthropic/claude-sonnet-4-6",
                "finish_reason": "stop"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let resp = adapter.chat(req).await.expect("chat should succeed");

        assert_eq!(resp.content, "Hello, world!");
        assert_eq!(resp.input_tokens, 10);
        assert_eq!(resp.output_tokens, 5);
        assert_eq!(resp.model, "anthropic/claude-sonnet-4-6");
        assert_eq!(resp.finish_reason, "stop");
        assert!(resp.structured.is_none());
    }

    #[tokio::test]
    async fn chat_sends_temperature_and_max_tokens() {
        use crate::adapter::ModelConfig;

        let mut server = mockito::Server::new_async().await;
        let _mock = server
            .mock("POST", "/v1/complete")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"temperature":0.5,"max_tokens":256}"#.into(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message":{"content":"ok","role":"assistant"},
                "input_tokens":1,"output_tokens":1,
                "model":"anthropic/claude-sonnet-4-6","finish_reason":"stop"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]).with_config(ModelConfig {
            temperature: Some(0.5),
            max_tokens: Some(256),
            ..Default::default()
        });
        adapter.chat(req).await.expect("should succeed");
    }

    #[tokio::test]
    async fn chat_errors_on_non_200() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(500)
            .with_body("internal server error")
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::Api { status: 500, .. })),
            "expected Api error with status 500, got {result:?}"
        );
    }

    #[tokio::test]
    async fn chat_errors_on_rate_limit() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(429)
            .with_body("rate limited")
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::RateLimited { .. })),
            "expected RateLimited, got {result:?}"
        );
    }

    // 2f-5: 429 body with retry_after must be propagated into ModelError::RateLimited.
    #[tokio::test]
    async fn chat_rate_limit_uses_body_retry_after() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(429)
            .with_header("content-type", "application/json")
            .with_body(r#"{"error":"rate limit","retry_after":12}"#)
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(
                result,
                Err(ModelError::RateLimited {
                    retry_after_secs: 12
                })
            ),
            "expected RateLimited{{retry_after_secs:12}}, got {result:?}"
        );
    }

    // 2f-5: 429 with no parseable body falls back to 60 s default.
    #[tokio::test]
    async fn chat_rate_limit_falls_back_when_no_retry_after() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(429)
            .with_body("too many requests")
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(
                result,
                Err(ModelError::RateLimited {
                    retry_after_secs: 60
                })
            ),
            "expected RateLimited{{retry_after_secs:60}}, got {result:?}"
        );
    }

    #[tokio::test]
    async fn health_check_passes_on_200() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("GET", "/health")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"ok":true}"#)
            .create_async()
            .await;

        let client = reqwest::Client::new();
        check_sidecar_health(&server.url(), &client)
            .await
            .expect("health check should pass");
    }

    #[tokio::test]
    async fn health_check_errors_on_non_200() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("GET", "/health")
            .with_status(503)
            .with_body("unavailable")
            .create_async()
            .await;

        let client = reqwest::Client::new();
        let result = check_sidecar_health(&server.url(), &client).await;
        assert!(
            matches!(result, Err(ModelError::Api { status: 503, .. })),
            "expected Api error with status 503, got {result:?}"
        );
    }

    #[tokio::test]
    async fn health_check_errors_on_unreachable() {
        // Port 1 is never listening.
        let client = reqwest::Client::new();
        let result = check_sidecar_health("http://127.0.0.1:1", &client).await;
        assert!(
            matches!(result, Err(ModelError::Network(_))),
            "expected Network error, got {result:?}"
        );
    }

    // I3: health guard must reject ok=false and non-JSON 200 bodies.

    #[tokio::test]
    async fn health_check_errors_on_ok_false() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("GET", "/health")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"ok":false}"#)
            .create_async()
            .await;

        let client = reqwest::Client::new();
        let result = check_sidecar_health(&server.url(), &client).await;
        assert!(
            matches!(result, Err(ModelError::Api { .. })),
            "health guard must reject {{\"ok\":false}}, got {result:?}"
        );
    }

    #[tokio::test]
    async fn health_check_errors_on_non_json_200() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("GET", "/health")
            .with_status(200)
            .with_header("content-type", "text/plain")
            .with_body("OK")
            .create_async()
            .await;

        let client = reqwest::Client::new();
        let result = check_sidecar_health(&server.url(), &client).await;
        assert!(
            matches!(result, Err(ModelError::Serialization(_))),
            "health guard must reject a non-JSON 200 body, got {result:?}"
        );
    }

    // 2j-1: tool_calls round-trip — sidecar returns tool_calls -> ModelResponse.tool_calls populated.
    #[tokio::test]
    async fn chat_returns_tool_calls_from_sidecar() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": null, "role": "assistant"},
                "tool_calls": [{"id": "c1", "name": "get_weather", "arguments": {"city": "SF"}}],
                "finish_reason": "tool_calls",
                "input_tokens": 5,
                "output_tokens": 3,
                "model": "anthropic/claude-sonnet-4-6"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("what's the weather?")]);
        let resp = adapter.chat(req).await.expect("chat should succeed");

        assert_eq!(resp.finish_reason, "tool_calls");
        assert_eq!(resp.tool_calls.len(), 1);
        let tc = &resp.tool_calls[0];
        assert_eq!(tc.id, "c1");
        assert_eq!(tc.name, "get_weather");
        assert_eq!(tc.arguments, serde_json::json!({"city": "SF"}));
    }

    // 2j fail-closed: finish_reason "tool_calls" but no tool_calls array must Err,
    // not silently degrade to an empty dispatch.
    #[tokio::test]
    async fn chat_errors_when_tool_calls_missing_for_tool_finish() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": null, "role": "assistant"},
                "finish_reason": "tool_calls",
                "input_tokens": 5,
                "output_tokens": 3,
                "model": "anthropic/claude-sonnet-4-6"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::Serialization(_))),
            "finish_reason tool_calls with no tool_calls must Err, got {result:?}"
        );
    }

    // 2j fail-closed: a tool_call missing its id (or name) must Err — a blank id
    // would break tool_call_id correlation downstream.
    #[tokio::test]
    async fn chat_errors_when_tool_call_missing_id() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": null, "role": "assistant"},
                "tool_calls": [{"name": "get_weather", "arguments": {"city": "SF"}}],
                "finish_reason": "tool_calls",
                "input_tokens": 5,
                "output_tokens": 3,
                "model": "anthropic/claude-sonnet-4-6"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("what's the weather?")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::Serialization(_))),
            "tool_call with a blank id must Err, got {result:?}"
        );
    }

    // 2j-1: tools forwarded — a request with non-empty tools sends them in the POST body.
    #[tokio::test]
    async fn chat_sends_tools_in_post_body() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .match_body(mockito::Matcher::PartialJsonString(
                r#"{"tools":[{"type":"function","function":{"name":"get_weather"}}]}"#.into(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": "ok", "role": "assistant"},
                "finish_reason": "stop",
                "input_tokens": 5,
                "output_tokens": 3,
                "model": "anthropic/claude-sonnet-4-6"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]).with_tools(vec![
            serde_json::json!({"type": "function", "function": {"name": "get_weather"}}),
        ]);
        adapter.chat(req).await.expect("chat should succeed");
    }

    // I4: missing output_tokens must cause chat() to return Err, not a zero-metered response.

    #[tokio::test]
    async fn chat_errors_when_output_tokens_missing() {
        let mut server = mockito::Server::new_async().await;

        // Response omits output_tokens — the old unwrap_or(0) would silently zero it.
        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": "hi", "role": "assistant"},
                "input_tokens": 5,
                "model": "anthropic/claude-sonnet-4-6",
                "finish_reason": "stop"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::Serialization(_))),
            "missing output_tokens must produce Serialization error, got {result:?}"
        );
    }

    // 2f-security: a huge (malicious or buggy) provider Retry-After must be clamped
    // to MAX_RETRY_AFTER_SECS so it cannot overflow the timestamp math in the worker.
    #[tokio::test]
    async fn chat_rate_limit_clamps_huge_retry_after() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(429)
            .with_header("content-type", "application/json")
            .with_body(r#"{"error":"rate limit","retry_after":99999999999999}"#)
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        match result {
            Err(ModelError::RateLimited { retry_after_secs }) => {
                assert!(
                    retry_after_secs <= MAX_RETRY_AFTER_SECS,
                    "retry_after_secs {retry_after_secs} must be <= MAX_RETRY_AFTER_SECS ({MAX_RETRY_AFTER_SECS})"
                );
            }
            other => panic!("expected RateLimited, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn chat_errors_when_input_tokens_missing() {
        let mut server = mockito::Server::new_async().await;

        let _mock = server
            .mock("POST", "/v1/complete")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{
                "message": {"content": "hi", "role": "assistant"},
                "output_tokens": 3,
                "model": "anthropic/claude-sonnet-4-6",
                "finish_reason": "stop"
            }"#,
            )
            .create_async()
            .await;

        let adapter = SidecarModelAdapter::new(server.url());
        let req = ModelRequest::new(vec![ChatMessage::user("hi")]);
        let result = adapter.chat(req).await;

        assert!(
            matches!(result, Err(ModelError::Serialization(_))),
            "missing input_tokens must produce Serialization error, got {result:?}"
        );
    }
}