causa-provider 0.0.4

Causa — a minimal context kernel for Rust agents (facts, ports, adapters)
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
//! Shared transport plumbing for kernel `ModelGateway` adapters.
//!
//! Owns the two adapter-wide concerns the per-protocol gateways must not
//! duplicate: control-plane wiring (`send_with_control`) and the error
//! mapping table (`ModelInvokeErrorKind` is the closed vocabulary the
//! caller can use when deciding whether to start another logical invocation):
//!
//! | Source                                  | Kind            |
//! |-----------------------------------------|-----------------|
//! | reqwest connect / request / body error  | `Transient`     |
//! | request timeout (incl. attempt deadline)| `TimedOut`      |
//! | cancellation token fired                | `Cancelled`     |
//! | HTTP 408 / 409 / 429 / 5xx              | `Transient`     |
//! | HTTP 400 / 422                          | `InvalidRequest`|
//! | HTTP 401 / 403 / 404                    | `Permanent`     |
//! | any other non-2xx status                | `Permanent`     |
//! | 2xx body that is an error envelope      | by code or type |
//! | 2xx body not parseable                  | `Undecodable`   |
//! | any body over the size cap              | `Undecodable`   |
//! | 2xx stream without an SSE content type  | `Undecodable`   |
//! | a request the client could not build    | `Permanent`     |
//!
//! A request error covers a connection the peer closed before answering.
//! The request may have reached the server, so a host that cannot tolerate
//! a repeated call must not retry `Transient`. An over-cap body is
//! `Undecodable` whatever its status, and carries no status.
//!
//! A non-2xx error carries its status in [`ModelInvokeError::status`] and
//! the provider's error code or type in [`ModelInvokeError::code`], so a
//! caller can tell a rejected credential (401, 403) from an unknown model or
//! path (404) without reading the message. The message keeps at most 200
//! characters of the provider's `error.message`, on one line.
//!
//! Two response headers can override the status rule: `x-should-retry: true`
//! classifies as `Transient` whatever the status, and `false` removes
//! retryability (400/422 stay `InvalidRequest`). A `retry-after-ms` or
//! `retry-after` value (seconds or an HTTP date) fills
//! [`ModelInvokeError::retry_delay`]; the transport itself never waits.
//!
//! The adapter is read-only on the control plane. The absolute deadline and
//! cancellation token are raced against every socket wait, independent of
//! host-client timeout configuration, and checked during local progression.

use std::{future::Future, time::Duration};

use causa_kernel::{
    CallControl, ControlError, ModelInvokeError, ModelInvokeErrorKind, ModelOutput,
};
use causa_protocol::translation::error_envelope::{ErrorDetail, reported_error};
use reqwest::header::HeaderMap;
use reqwest::{Client, StatusCode};
use serde_json::Value;

/// Hard cap on buffered provider response bodies. A runaway or hijacked
/// endpoint must not OOM the host process. Generous for any LLM response
/// including long tool observations.
const MAX_RESPONSE_BYTES: usize = 32 * 1024 * 1024;

/// Baseline transport timeouts for the default client (connect 10s,
/// total 120s). A per-request attempt deadline replaces the total timeout
/// when present; the connect baseline always applies.
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const DEFAULT_TOTAL_TIMEOUT: Duration = Duration::from_secs(120);

/// Endpoint + HTTP client bundle shared by the per-protocol gateways.
#[derive(Debug, Clone)]
pub(crate) struct GatewayCore {
    pub http: Client,
    pub endpoint: String,
}

impl GatewayCore {
    pub fn new(default_endpoint: &str) -> Self {
        Self {
            http: default_client(),
            endpoint: default_endpoint.into(),
        }
    }

    pub fn with_base_url(mut self, base_url: impl Into<String>, path: &str) -> Self {
        self.endpoint = format!("{}{path}", base_url.into().trim_end_matches('/'));
        self
    }

    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = endpoint.into();
        self
    }

    pub fn with_http_client(mut self, http: Client) -> Self {
        self.http = http;
        self
    }

    pub fn post(&self) -> reqwest::RequestBuilder {
        self.http.post(&self.endpoint)
    }
}

/// Default shared client: baseline connect + total timeouts so a gateway
/// without an attempt deadline cannot hang forever on a stalled
/// connection. Hosts with different baselines inject their own client
/// through `GatewayCore::with_http_client`.
fn default_client() -> Client {
    Client::builder()
        .connect_timeout(DEFAULT_CONNECT_TIMEOUT)
        .timeout(DEFAULT_TOTAL_TIMEOUT)
        .retry(reqwest::retry::never())
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .expect("static client configuration is valid")
}

/// Send a prepared POST under the attempt's control plane: pre-cancel
/// short-circuit, deadline-as-timeout, token raced against the in-flight
/// work. Returns the raw status + bounded body for protocol-specific
/// parsing.
pub(crate) async fn send_with_control(
    req: reqwest::RequestBuilder,
    control: &CallControl,
) -> Result<BufferedResponse, ModelInvokeError> {
    let response = send_response(req, control).await?;
    read_bounded(response, control).await
}

/// A fully buffered response: the status and headers classification reads,
/// plus the bounded body the protocol parser consumes.
pub(crate) struct BufferedResponse {
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub body: String,
}

/// Buffer the response body under [`MAX_RESPONSE_BYTES`]. The
/// Content-Length header is rejected up front when present; the chunk
/// loop covers chunked bodies with no declared length. Mid-body network
/// failures surface through the transport table (body error →
/// `Transient`); an over-cap body or non-UTF-8 payload is `Undecodable`
/// (the provider produced an unusable response).
pub(crate) async fn read_bounded(
    mut response: reqwest::Response,
    control: &CallControl,
) -> Result<BufferedResponse, ModelInvokeError> {
    let status = response.status();
    let headers = response.headers().clone();
    if let Some(len) = response.content_length()
        && len as usize > MAX_RESPONSE_BYTES
    {
        return Err(oversize_error(len as usize));
    }
    let mut body: Vec<u8> = Vec::new();
    let mut chunks = 0;
    while let Some(chunk) = controlled(control, response.chunk())
        .await?
        .map_err(map_transport_error)?
    {
        check_control(control)?;
        chunks += 1;
        if chunks >= 32 {
            chunks = 0;
            tokio::task::yield_now().await;
            check_control(control)?;
        }
        if body.len().saturating_add(chunk.len()) > MAX_RESPONSE_BYTES {
            return Err(oversize_error(MAX_RESPONSE_BYTES + 1));
        }
        body.extend_from_slice(&chunk);
    }
    let text = match String::from_utf8(body) {
        Ok(t) => t,
        Err(_) if !status.is_success() => String::new(),
        Err(_) => {
            return Err(ModelInvokeError::new(
                ModelInvokeErrorKind::Undecodable,
                "provider response body is not valid UTF-8",
            ));
        }
    };
    Ok(BufferedResponse {
        status,
        headers,
        body: text,
    })
}

fn oversize_error(len: usize) -> ModelInvokeError {
    ModelInvokeError::new(
        ModelInvokeErrorKind::Undecodable,
        format!(
            "provider response too large: {len} bytes exceeds the {MAX_RESPONSE_BYTES}-byte cap"
        ),
    )
}

/// Turn a raw response into a [`ModelOutput`]: non-2xx through the error
/// table, 2xx JSON-syntax failures as `Undecodable`, a 2xx error envelope as
/// the error it reports, then the protocol-specific body parser (whose own
/// schema failures also surface as `Undecodable`).
pub(crate) fn finish_response<F>(
    response: BufferedResponse,
    provider: &str,
    parse: F,
) -> Result<ModelOutput, ModelInvokeError>
where
    F: FnOnce(&Value) -> Result<ModelOutput, ModelInvokeError>,
{
    if !response.status.is_success() {
        return Err(map_status_error(
            response.status,
            &response.headers,
            &response.body,
            provider,
        ));
    }
    let value: Value = serde_json::from_str(&response.body).map_err(|e| {
        ModelInvokeError::new(
            ModelInvokeErrorKind::Undecodable,
            format!("{provider} 2xx response body is not valid JSON: {e}"),
        )
    })?;
    // Some servers report a failure under a success status.
    if let Some(error) = reported_error(&value) {
        return Err(error);
    }
    parse(&value)
}

pub(crate) fn map_transport_error(e: reqwest::Error) -> ModelInvokeError {
    let kind = if e.is_timeout() {
        ModelInvokeErrorKind::TimedOut
    } else if e.is_connect() || e.is_request() || e.is_body() || e.is_decode() {
        // A request error is the exchange failing before a response arrived,
        // for example the peer closing the connection; a refused connection
        // is the same failure one step earlier. Response::chunk wraps failed
        // HTTP body frames as decode errors. Wire JSON/UTF-8 failures are
        // classified by our own pure parsers.
        ModelInvokeErrorKind::Transient
    } else {
        // What is left is a request the client could not build.
        ModelInvokeErrorKind::Permanent
    };
    ModelInvokeError::new(kind, format!("provider transport: {}", e.without_url()))
}

/// Classify a non-2xx response. `x-should-retry` overrides the status rule
/// when it names a boolean; a `retry-after-ms` / `retry-after` header adds
/// the server's requested delay to the error.
pub(crate) fn map_status_error(
    status: StatusCode,
    headers: &HeaderMap,
    body: &str,
    provider: &str,
) -> ModelInvokeError {
    let kind = match should_retry(headers) {
        Some(true) => ModelInvokeErrorKind::Transient,
        // A refused retry never stays retryable; a rejected request keeps
        // its own kind so callers can still tell it from auth/not-found.
        Some(false) => match status.as_u16() {
            400 | 422 => ModelInvokeErrorKind::InvalidRequest,
            _ => ModelInvokeErrorKind::Permanent,
        },
        None => status_kind(status),
    };
    let detail = ErrorDetail::read_body(body);
    let message = match &detail.message {
        Some(msg) => format!("{provider} error {}: {msg}", status.as_u16()),
        None => format!("{provider} error {}", status.as_u16()),
    };
    let mut error = ModelInvokeError::new(kind, message).with_status(status.as_u16());
    if let Some(code) = detail.code {
        error = error.with_code(code);
    }
    match retry_after(headers) {
        Some(delay) => error.with_retry_delay(delay),
        None => error,
    }
}

fn status_kind(status: StatusCode) -> ModelInvokeErrorKind {
    match status.as_u16() {
        408 | 409 | 429 | 500..=599 => ModelInvokeErrorKind::Transient,
        400 | 422 => ModelInvokeErrorKind::InvalidRequest,
        // 401 / 403 / 404 and everything else.
        _ => ModelInvokeErrorKind::Permanent,
    }
}

/// The `x-should-retry` override: `Some(true)` means retry, `Some(false)`
/// means never, and `None` (absent, unreadable or any other value) leaves
/// the status rule in force.
fn should_retry(headers: &HeaderMap) -> Option<bool> {
    match header_str(headers, "x-should-retry")?.trim() {
        value if value.eq_ignore_ascii_case("true") => Some(true),
        value if value.eq_ignore_ascii_case("false") => Some(false),
        _ => None,
    }
}

/// A server-requested retry delay: `retry-after-ms` as fractional
/// milliseconds first, then `retry-after` as seconds or an HTTP date. A
/// missing, unparseable or already-past value yields `None`.
fn retry_after(headers: &HeaderMap) -> Option<Duration> {
    if let Some(value) = header_str(headers, "retry-after-ms")
        && let Some(delay) = seconds_duration(value, 1_000.0)
    {
        return Some(delay);
    }
    let value = header_str(headers, "retry-after")?;
    seconds_duration(value, 1.0).or_else(|| http_date_duration(value))
}

/// Parse a non-negative decimal number of the given unit into a duration.
fn seconds_duration(value: &str, units_per_second: f64) -> Option<Duration> {
    let amount: f64 = value.trim().parse().ok()?;
    if !amount.is_finite() || amount < 0.0 {
        return None;
    }
    Duration::try_from_secs_f64(amount / units_per_second).ok()
}

/// Parse an HTTP-date as a delay from now; a past date yields `None`.
fn http_date_duration(value: &str) -> Option<Duration> {
    let date = httpdate::parse_http_date(value.trim()).ok()?;
    date.duration_since(std::time::SystemTime::now()).ok()
}

fn header_str<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
    headers.get(name)?.to_str().ok()
}

/// Check local progress as well as socket waits.
pub(crate) fn check_control(control: &CallControl) -> Result<(), ModelInvokeError> {
    control.check().map_err(|error| match error {
        ControlError::Cancelled => ModelInvokeError::new(
            ModelInvokeErrorKind::Cancelled,
            "model invocation cancelled",
        ),
        ControlError::TimedOut => ModelInvokeError::new(
            ModelInvokeErrorKind::TimedOut,
            "model invocation deadline exceeded",
        ),
    })
}

/// Race a transport operation against the absolute control deadline. This
/// remains effective with host clients that have no reqwest timeout.
///
/// Note: This stop-wait race is kept separate from `causa-runtime` because edge crates
/// (like `causa-provider`) cannot depend on the runtime per repo layering rules,
/// and `causa-kernel` holds contracts only without timers or async runtime dependencies.
pub(crate) async fn controlled<T>(
    control: &CallControl,
    work: impl Future<Output = T>,
) -> Result<T, ModelInvokeError> {
    check_control(control)?;
    let deadline = async {
        match control.deadline() {
            Some(deadline) => tokio::time::sleep_until(deadline.into()).await,
            None => std::future::pending::<()>().await,
        }
    };
    tokio::select! {
        biased;
        _ = control.cancellation_token().cancelled() => Err(ModelInvokeError::new(ModelInvokeErrorKind::Cancelled, "model invocation cancelled")),
        _ = deadline => Err(ModelInvokeError::new(ModelInvokeErrorKind::TimedOut, "model invocation deadline exceeded")),
        result = work => { check_control(control)?; Ok(result) },
    }
}

/// Send one request without buffering its body.
pub(crate) async fn send_response(
    mut req: reqwest::RequestBuilder,
    control: &CallControl,
) -> Result<reqwest::Response, ModelInvokeError> {
    if let Some(deadline) = control.deadline() {
        req = req.timeout(deadline.saturating_duration_since(std::time::Instant::now()));
    }
    controlled(control, req.send())
        .await?
        .map_err(map_transport_error)
}

#[cfg(test)]
mod tests {
    use super::*;
    use reqwest::header::{HeaderName, HeaderValue};
    use std::time::SystemTime;

    fn headers(pairs: &[(&str, &str)]) -> HeaderMap {
        let mut map = HeaderMap::new();
        for (name, value) in pairs {
            map.insert(
                HeaderName::from_bytes(name.as_bytes()).expect("valid header name"),
                HeaderValue::from_str(value).expect("valid header value"),
            );
        }
        map
    }

    fn kind(status: u16, pairs: &[(&str, &str)]) -> ModelInvokeErrorKind {
        map_status_error(
            StatusCode::from_u16(status).expect("valid status"),
            &headers(pairs),
            "{\"error\":{\"message\":\"busy\"}}",
            "test",
        )
        .kind
    }

    #[test]
    fn status_table_classifies_retryable_and_rejected_statuses() {
        for status in [408, 409, 429, 500, 503, 599] {
            assert_eq!(
                kind(status, &[]),
                ModelInvokeErrorKind::Transient,
                "{status}"
            );
        }
        for status in [400, 422] {
            assert_eq!(
                kind(status, &[]),
                ModelInvokeErrorKind::InvalidRequest,
                "{status}"
            );
        }
        for status in [401, 403, 404, 418] {
            assert_eq!(
                kind(status, &[]),
                ModelInvokeErrorKind::Permanent,
                "{status}"
            );
        }
    }

    #[test]
    fn should_retry_header_overrides_the_status_rule() {
        assert_eq!(
            kind(400, &[("x-should-retry", "true")]),
            ModelInvokeErrorKind::Transient
        );
        assert_eq!(
            kind(401, &[("x-should-retry", "true")]),
            ModelInvokeErrorKind::Transient
        );
        assert_eq!(
            kind(429, &[("x-should-retry", "false")]),
            ModelInvokeErrorKind::Permanent
        );
        assert_eq!(
            kind(503, &[("x-should-retry", "false")]),
            ModelInvokeErrorKind::Permanent
        );
        assert_eq!(
            kind(400, &[("x-should-retry", "false")]),
            ModelInvokeErrorKind::InvalidRequest
        );
        // An unrecognized value leaves the status rule in force.
        assert_eq!(
            kind(429, &[("x-should-retry", "maybe")]),
            ModelInvokeErrorKind::Transient
        );
        assert_eq!(
            kind(404, &[("x-should-retry", "maybe")]),
            ModelInvokeErrorKind::Permanent
        );
        // So does a missing or unreadable one.
        assert_eq!(
            kind(429, &[("x-should-retry", "1")]),
            ModelInvokeErrorKind::Transient
        );
    }

    #[test]
    fn retry_after_ms_takes_precedence_over_seconds() {
        let error = map_status_error(
            StatusCode::TOO_MANY_REQUESTS,
            &headers(&[("retry-after-ms", "1500"), ("retry-after", "9")]),
            "",
            "test",
        );
        assert_eq!(error.retry_delay, Some(Duration::from_millis(1500)));

        let error = map_status_error(
            StatusCode::TOO_MANY_REQUESTS,
            &headers(&[("retry-after-ms", "250.5"), ("retry-after", "9")]),
            "",
            "test",
        );
        let delay = error.retry_delay.expect("fractional milliseconds parse");
        assert!(
            delay >= Duration::from_nanos(250_499_999)
                && delay <= Duration::from_nanos(250_500_001),
            "{delay:?}"
        );
    }

    #[test]
    fn retry_after_seconds_and_http_dates_are_parsed() {
        assert_eq!(
            retry_after(&headers(&[("retry-after", "3")])),
            Some(Duration::from_secs(3))
        );
        let future = SystemTime::now() + Duration::from_secs(30);
        let value = httpdate::fmt_http_date(future);
        let parsed = retry_after(&headers(&[("retry-after", &value)])).expect("future HTTP date");
        assert!(
            parsed >= Duration::from_secs(25) && parsed <= Duration::from_secs(31),
            "{parsed:?}"
        );
    }

    #[test]
    fn unusable_retry_after_values_leave_the_delay_unset() {
        for value in ["", "abc", "-1", "NaN", "inf"] {
            assert_eq!(
                retry_after(&headers(&[("retry-after", value)])),
                None,
                "retry-after {value:?}"
            );
            assert_eq!(
                retry_after(&headers(&[("retry-after-ms", value)])),
                None,
                "retry-after-ms {value:?}"
            );
        }
        // An unusable retry-after-ms falls back to a usable retry-after.
        assert_eq!(
            retry_after(&headers(&[
                ("retry-after-ms", "junk"),
                ("retry-after", "4")
            ])),
            Some(Duration::from_secs(4))
        );
        let past = httpdate::fmt_http_date(SystemTime::now() - Duration::from_secs(60));
        assert_eq!(retry_after(&headers(&[("retry-after", &past)])), None);
        let error = map_status_error(
            StatusCode::TOO_MANY_REQUESTS,
            &headers(&[("retry-after", "junk")]),
            "",
            "test",
        );
        assert_eq!(error.retry_delay, None);
        assert_eq!(
            ModelInvokeError::new(ModelInvokeErrorKind::Transient, "x").retry_delay,
            None
        );
    }

    #[test]
    fn documented_error_message_comes_from_the_envelope() {
        let error = map_status_error(
            StatusCode::TOO_MANY_REQUESTS,
            &HeaderMap::new(),
            "{\"error\":{\"message\":\"slow down\"}}",
            "anthropic",
        );
        assert_eq!(error.message, "anthropic error 429: slow down");
    }

    #[test]
    fn non_envelope_error_omits_body_text() {
        let error = map_status_error(
            StatusCode::BAD_GATEWAY,
            &HeaderMap::new(),
            "<html>502 Bad Gateway</html>",
            "anthropic",
        );
        assert_eq!(error.message, "anthropic error 502");

        let error2 = map_status_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            &HeaderMap::new(),
            "{\"status\": \"fail\"}",
            "chat",
        );
        assert_eq!(error2.message, "chat error 500");
    }
}