llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
//! Native OAuth forwarding on the daemon's TLS listener. These routes have
//! mandatory daemon-key authentication and loopback peer admission, and bypass
//! prompt history, provider credential fallback, and peer routing.

use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::{Arc, Mutex};

use anyhow::{Context, Result};
use axum::body::{Body, Bytes};
use axum::extract::{ConnectInfo, DefaultBodyLimit, Request, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use futures::StreamExt;
use serde::Deserialize;
use serde_json::Value;

use super::{auth, AppError, GENERATION_BODY_LIMIT_BYTES};

const CONNECTION_KEY: &str = "x-api-key";
const UPSTREAM_IP: &str = "x-llmman-upstream-ip";
const CAPABILITY: &str = "codex-oauth-forwarding-v2";
const CLAUDE_CAPABILITY: &str = "claude-oauth-forwarding-v2";
const CLAUDE_OAUTH_BETA: &str = "oauth-2025-04-20";
const ERROR_LIMIT: usize = 1024 * 1024;
/// Upstream transports kept warm, one per (profile, pinned IP). The
/// supervisor pins a handful of resolved addresses, so this is a leak
/// guard rather than an eviction policy: past it the map starts over.
const CLIENT_CACHE_LIMIT: usize = 64;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Profile {
    Codex,
    Claude,
}

impl Profile {
    fn host(self) -> &'static str {
        match self {
            Self::Codex => "chatgpt.com",
            Self::Claude => "api.anthropic.com",
        }
    }

    fn base(self) -> &'static str {
        match self {
            Self::Codex => "https://chatgpt.com/backend-api/codex",
            Self::Claude => "https://api.anthropic.com/v1",
        }
    }

    fn model_prefix(self) -> &'static str {
        match self {
            Self::Codex => "llmman.provider/openai/",
            Self::Claude => "llmman.provider/anthropic/",
        }
    }

    #[cfg(test)]
    fn route_prefix(self) -> &'static str {
        match self {
            Self::Codex => "/api/codex",
            Self::Claude => "/api/anthropic",
        }
    }
}

/// Validates opt-in before any socket binds. Ordinary daemon auth may be
/// optional on loopback, but delegated OAuth forwarding never is.
pub(super) fn routes(
    config: crate::config::Managed,
    policy: auth::Policy,
    tls: bool,
) -> Result<Router> {
    if !config.enabled {
        return Ok(Router::new());
    }
    anyhow::ensure!(
        cfg!(unix),
        "managed forwarding is unavailable on this platform until owner-only key ACLs can be verified"
    );
    anyhow::ensure!(
        tls,
        "managed forwarding requires LLMMAN_TLS_CERT/LLMMAN_TLS_KEY"
    );
    anyhow::ensure!(
        policy.enforced(),
        "managed forwarding requires daemon API keys and LLMMAN_AUTH must not be off"
    );
    Ok(router(ManagedState(Arc::new(ManagedInner {
        connection_auth: policy,
        read_timeout: config.read_timeout,
        clients: Mutex::default(),
        #[cfg(test)]
        test_base: None,
    }))))
}

#[derive(Clone)]
struct ManagedState(Arc<ManagedInner>);

struct ManagedInner {
    connection_auth: auth::Policy,
    read_timeout: Option<std::time::Duration>,
    clients: Mutex<HashMap<(Profile, IpAddr), reqwest::Client>>,
    // No production configuration or request may replace the fixed endpoint.
    #[cfg(test)]
    test_base: Option<String>,
}

impl ManagedInner {
    /// The transport for `profile` pinned to `ip`, reused across requests
    /// so each forwarded call does not pay a fresh TCP + TLS handshake.
    fn upstream_client(&self, profile: Profile, ip: IpAddr) -> Result<reqwest::Client> {
        let mut clients = self.clients.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(client) = clients.get(&(profile, ip)) {
            return Ok(client.clone());
        }
        let client = upstream_client_builder(profile, ip, self.read_timeout)
            .build()
            .context("build managed provider transport")?;
        if clients.len() >= CLIENT_CACHE_LIMIT {
            clients.clear();
        }
        clients.insert((profile, ip), client.clone());
        Ok(client)
    }
}

/// Pins the supervisor-authorized address while retaining the provider's TLS
/// name. Never inherit proxy/CA overrides or redirect delegated credentials.
fn upstream_client_builder(
    profile: Profile,
    ip: IpAddr,
    read_timeout: Option<std::time::Duration>,
) -> reqwest::ClientBuilder {
    let builder = reqwest::Client::builder()
        .no_proxy()
        .resolve(profile.host(), SocketAddr::new(ip, 443))
        .redirect(reqwest::redirect::Policy::none())
        .connect_timeout(std::time::Duration::from_secs(30));
    match read_timeout {
        Some(timeout) => builder.read_timeout(timeout),
        None => builder,
    }
}

fn router(state: ManagedState) -> Router {
    Router::new()
        .route("/api/managed/capabilities", get(capabilities))
        .route("/api/codex/responses", post(responses))
        .route("/api/codex/responses/compact", post(compact))
        .route("/api/anthropic/messages", post(messages))
        .route("/api/anthropic/messages/count_tokens", post(count_tokens))
        .layer(DefaultBodyLimit::max(GENERATION_BODY_LIMIT_BYTES))
        .layer(middleware::from_fn_with_state(
            state.clone(),
            require_connection,
        ))
        .with_state(state)
}

async fn require_connection(
    State(state): State<ManagedState>,
    mut req: Request,
    next: Next,
) -> Response {
    // Only connection metadata is authoritative; forwarded IP headers are not.
    let local = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .is_some_and(|peer| peer.0.ip().to_canonical().is_loopback());
    if !local {
        return error(
            StatusCode::FORBIDDEN,
            "managed forwarding requires a loopback peer",
        )
        .into_response();
    }
    let accepted = state.0.connection_auth.enforced()
        && single_header(req.headers(), CONNECTION_KEY)
            .ok()
            .flatten()
            .is_some_and(|key| state.0.connection_auth.accepts(key));
    req.headers_mut().remove(CONNECTION_KEY);
    if !accepted {
        return error(
            StatusCode::UNAUTHORIZED,
            "managed daemon authentication required",
        )
        .into_response();
    }
    next.run(req).await
}

async fn capabilities() -> Json<Value> {
    Json(serde_json::json!({"capabilities": [CAPABILITY, CLAUDE_CAPABILITY]}))
}

/// Deliberately has no derived Debug/Serialize and never enters shared state.
struct Credentials {
    authorization: HeaderValue,
    account: Option<HeaderValue>,
    upstream_ip: IpAddr,
}

impl std::fmt::Debug for Credentials {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Credentials(<redacted>)")
    }
}

fn single_header<'a>(headers: &'a HeaderMap, name: &str) -> Result<Option<&'a str>, AppError> {
    let mut values = headers.get_all(name).iter();
    let Some(value) = values.next() else {
        return Ok(None);
    };
    if values.next().is_some() {
        return Err(error(
            StatusCode::BAD_REQUEST,
            "duplicate managed request header",
        ));
    }
    value
        .to_str()
        .map(Some)
        .map_err(|_| error(StatusCode::BAD_REQUEST, "invalid managed request header"))
}

impl Credentials {
    fn parse(headers: &HeaderMap, profile: Profile) -> Result<Self, AppError> {
        let upstream_ip = single_header(headers, UPSTREAM_IP)?
            .and_then(|ip| ip.parse::<IpAddr>().ok())
            .filter(public_upstream_ip)
            .ok_or_else(|| {
                error(
                    StatusCode::BAD_REQUEST,
                    "managed inference requires a public upstream IP",
                )
            })?;
        // One Authorization header, parsed by the same rule as the public
        // listener (`auth::bearer`), then held to this listener's stricter
        // shape: a single printable token that is not the local placeholder.
        single_header(headers, "authorization")?
            .ok_or_else(|| error(StatusCode::UNAUTHORIZED, "provider bearer required"))?;
        let token = auth::bearer(headers)
            .ok_or_else(|| error(StatusCode::UNAUTHORIZED, "provider bearer required"))?;
        if token.is_empty()
            || token.bytes().any(|b| !b.is_ascii_graphic() || b == b',')
            || token == crate::providers::PLACEHOLDER_API_KEY
        {
            return Err(error(
                StatusCode::UNAUTHORIZED,
                "explicit provider bearer required",
            ));
        }
        let mut authorization = HeaderValue::from_str(&format!("Bearer {token}"))
            .map_err(|_| error(StatusCode::UNAUTHORIZED, "invalid provider bearer"))?;
        authorization.set_sensitive(true);
        let account = if profile == Profile::Codex {
            single_header(headers, "chatgpt-account-id")?
        } else {
            None
        }
        .map(|account| {
            if account.trim().is_empty() {
                return Err(error(StatusCode::BAD_REQUEST, "invalid account header"));
            }
            let mut value = HeaderValue::from_str(account)
                .map_err(|_| error(StatusCode::BAD_REQUEST, "invalid account header"))?;
            value.set_sensitive(true);
            Ok(value)
        })
        .transpose()?;
        Ok(Self {
            authorization,
            account,
            upstream_ip,
        })
    }

    fn redact(&self, text: String) -> String {
        let token = self
            .authorization
            .to_str()
            .expect("validated bearer")
            .trim_start_matches("Bearer ");
        let text = text.replace(token, "<redacted>");
        self.account
            .as_ref()
            .and_then(|v| v.to_str().ok())
            .map_or_else(
                || text.clone(),
                |account| text.replace(account, "<redacted>"),
            )
    }

    /// Decode JSON strings before redaction so escaped quotes, backslashes,
    /// slashes, and Unicode escapes cannot conceal credentials on the wire.
    fn redact_error_body(&self, bytes: &[u8]) -> String {
        fn redact_value(credentials: &Credentials, value: &mut Value) {
            match value {
                Value::String(text) => *text = credentials.redact(std::mem::take(text)),
                Value::Array(values) => {
                    for value in values {
                        redact_value(credentials, value);
                    }
                }
                Value::Object(values) => {
                    *values = std::mem::take(values)
                        .into_iter()
                        .map(|(key, mut value)| {
                            redact_value(credentials, &mut value);
                            (credentials.redact(key), value)
                        })
                        .collect();
                }
                _ => {}
            }
        }
        match serde_json::from_slice::<Value>(bytes) {
            Ok(mut value) => {
                redact_value(self, &mut value);
                value.to_string()
            }
            Err(_) => self.redact(String::from_utf8_lossy(bytes).into_owned()),
        }
    }
}

// The caller authorizes this address against its network policy.
// Refuse special-use space as defense in depth; never re-resolve the host and
// silently select an address the supervisor did not authorize.
fn public_upstream_ip(ip: &IpAddr) -> bool {
    match ip {
        IpAddr::V4(ip) => {
            let [a, b, c, _] = ip.octets();
            !(a == 0
                || a == 10
                || a == 127
                || a >= 224
                || (a == 100 && (64..=127).contains(&b))
                || (a == 169 && b == 254)
                || (a == 172 && (16..=31).contains(&b))
                || (a == 192 && ((b == 0 && (c == 0 || c == 2)) || b == 168))
                || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100)))
                || (a == 203 && b == 0 && c == 113))
        }
        IpAddr::V6(ip) => {
            let words = ip.segments();
            words[0] & 0xe000 == 0x2000
                && words[0] != 0x2002
                && !(words[0] == 0x2001 && (words[1] < 0x0200 || words[1] == 0x0db8))
                && !(words[0] == 0x3fff && words[1] & 0xf000 == 0) // RFC 9637: 3fff::/20
        }
    }
}

fn error(status: StatusCode, message: &'static str) -> AppError {
    AppError::status(status, message)
}

async fn responses(
    State(state): State<ManagedState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, AppError> {
    forward(state, headers, body, Profile::Codex, "/responses").await
}

async fn compact(
    State(state): State<ManagedState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, AppError> {
    forward(state, headers, body, Profile::Codex, "/responses/compact").await
}

async fn messages(
    State(state): State<ManagedState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, AppError> {
    forward(state, headers, body, Profile::Claude, "/messages").await
}

async fn count_tokens(
    State(state): State<ManagedState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, AppError> {
    forward(
        state,
        headers,
        body,
        Profile::Claude,
        "/messages/count_tokens",
    )
    .await
}

// Reject duplicate top-level keys instead of letting serde's last-value-wins
// turn two conflicting model fields into one admitted routing decision.
#[derive(Deserialize)]
struct NativeRequest(#[serde(deserialize_with = "unique_object")] serde_json::Map<String, Value>);

fn unique_object<'de, D: serde::Deserializer<'de>>(
    deserializer: D,
) -> Result<serde_json::Map<String, Value>, D::Error> {
    struct ObjectVisitor;
    impl<'de> serde::de::Visitor<'de> for ObjectVisitor {
        type Value = serde_json::Map<String, Value>;
        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("an object with unique keys")
        }
        fn visit_map<A: serde::de::MapAccess<'de>>(
            self,
            mut access: A,
        ) -> Result<Self::Value, A::Error> {
            let mut map = serde_json::Map::new();
            while let Some((key, value)) = access.next_entry::<String, Value>()? {
                if map.insert(key, value).is_some() {
                    return Err(serde::de::Error::custom("duplicate request field"));
                }
            }
            Ok(map)
        }
    }
    deserializer.deserialize_map(ObjectVisitor)
}

/// Forward extensions without maintaining a provider-specific allowlist.
/// Strip connection-nominated fields too, and regenerate framing for JSON
/// rewriting/error redaction. Authentication is set explicitly afterward.
fn forward_headers(headers: &HeaderMap) -> HeaderMap {
    let connection_fields: Vec<_> = headers
        .get_all("connection")
        .iter()
        .filter_map(|value| value.to_str().ok())
        .flat_map(|value| value.split(','))
        .map(str::trim)
        .collect();
    let mut forwarded = HeaderMap::new();
    for (name, value) in headers {
        let name_str = name.as_str();
        if matches!(
            name_str,
            "connection"
                | "keep-alive"
                | "transfer-encoding"
                | "te"
                | "trailer"
                | "upgrade"
                | "host"
                | "content-length"
                | "authorization"
                | "x-api-key"
                | "x-goog-api-key"
                | "cookie"
                | "set-cookie"
                | "chatgpt-account-id"
        ) || name_str.starts_with("proxy-")
            || name_str.starts_with("x-llmman-")
            || connection_fields
                .iter()
                .any(|field| name_str.eq_ignore_ascii_case(field))
        {
            continue;
        }
        forwarded.append(name.clone(), value.clone());
    }
    forwarded
}

/// Never put request URLs, bearer credentials, or account IDs in diagnostics.
fn log_upstream_error(credentials: &Credentials, err: reqwest::Error) {
    crate::debug_log!(
        "managed provider transport: {}",
        credentials.redact(err.without_url().to_string())
    );
}

/// Content-coding tokens are case-insensitive, including in combined fields.
fn identity_encoding(headers: &HeaderMap) -> bool {
    headers.get_all("content-encoding").iter().all(|value| {
        value.to_str().is_ok_and(|value| {
            value
                .split(',')
                .all(|coding| coding.trim().eq_ignore_ascii_case("identity"))
        })
    })
}

async fn forward(
    state: ManagedState,
    headers: HeaderMap,
    body: Bytes,
    profile: Profile,
    operation: &'static str,
) -> Result<Response, AppError> {
    if !identity_encoding(&headers) {
        return Err(error(
            StatusCode::UNSUPPORTED_MEDIA_TYPE,
            "encoded managed request bodies are not supported",
        ));
    }
    let credentials = Credentials::parse(&headers, profile)?;
    let NativeRequest(mut body) = serde_json::from_slice(&body)
        .map_err(|_| error(StatusCode::BAD_REQUEST, "invalid native inference request"))?;
    let model = body
        .get("model")
        .and_then(Value::as_str)
        .and_then(|model| model.strip_prefix(profile.model_prefix()))
        .filter(|model| {
            !model.is_empty()
                && model.bytes().all(|b| b.is_ascii_graphic())
                && !model.contains(['?', '#'])
        })
        .ok_or_else(|| {
            error(
                StatusCode::BAD_REQUEST,
                "managed model must match the route provider and include a model ID",
            )
        })?
        .to_owned();
    body.insert("model".into(), Value::String(model));
    let base = profile.base();
    #[cfg(test)]
    let base = state.0.test_base.as_deref().unwrap_or(base);
    let client = state
        .0
        .upstream_client(profile, credentials.upstream_ip)
        .map_err(|err| {
            crate::debug_log!(
                "managed provider transport setup: {}",
                credentials.redact(err.to_string())
            );
            error(
                StatusCode::BAD_GATEWAY,
                "provider upstream transport unavailable",
            )
        })?;
    let mut forwarded = forward_headers(&headers);
    forwarded.remove("content-type");
    forwarded.remove("content-encoding");
    forwarded.remove("accept-encoding");
    if profile == Profile::Claude {
        // These are singular protocol fields, unlike general extension headers.
        single_header(&headers, "anthropic-version")?;
        forwarded.remove("anthropic-beta");
    }
    forwarded.insert("authorization", credentials.authorization.clone());
    if let Some(account) = &credentials.account {
        forwarded.insert("chatgpt-account-id", account.clone());
    }
    let mut request = client
        .post(format!("{base}{operation}"))
        .headers(forwarded)
        .header("accept-encoding", "identity")
        .json(&body);
    match profile {
        Profile::Codex => {}
        Profile::Claude => {
            if !headers.contains_key("anthropic-version") {
                request = request.header("anthropic-version", "2023-06-01");
            }
            let beta = single_header(&headers, "anthropic-beta")?.unwrap_or("");
            let beta = if beta
                .split(',')
                .any(|value| value.trim() == CLAUDE_OAUTH_BETA)
            {
                beta.to_owned()
            } else if beta.trim().is_empty() {
                CLAUDE_OAUTH_BETA.to_owned()
            } else {
                format!("{beta},{CLAUDE_OAUTH_BETA}")
            };
            request = request.header("anthropic-beta", beta);
        }
    }
    let mut upstream = request.send().await.map_err(|err| {
        log_upstream_error(&credentials, err);
        error(
            StatusCode::BAD_GATEWAY,
            "provider upstream connection failed",
        )
    })?;
    let status = upstream.status();
    if status.is_redirection() {
        return Err(error(
            StatusCode::BAD_GATEWAY,
            "provider upstream redirect refused",
        ));
    }
    let mut response = Response::builder().status(status);
    for (name, value) in &forward_headers(upstream.headers()) {
        if let Ok(value) = value.to_str() {
            response = response.header(name, credentials.redact(value.to_owned()));
        }
    }
    let body = if status.is_client_error() || status.is_server_error() {
        if !identity_encoding(upstream.headers()) {
            return Err(error(
                StatusCode::BAD_GATEWAY,
                "encoded provider error body refused",
            ));
        }
        let mut bytes = Vec::new();
        while let Some(chunk) = upstream.chunk().await.map_err(|err| {
            log_upstream_error(&credentials, err);
            error(
                StatusCode::BAD_GATEWAY,
                "provider upstream error body interrupted",
            )
        })? {
            if bytes.len() + chunk.len() > ERROR_LIMIT {
                return Err(error(
                    StatusCode::BAD_GATEWAY,
                    "provider upstream error body too large",
                ));
            }
            bytes.extend_from_slice(&chunk);
        }
        Body::from(credentials.redact_error_body(&bytes))
    } else {
        // No detached pump: dropping the downstream body drops the reqwest
        // stream and cancels the upstream. Never retry or buffer generation.
        Body::from_stream(upstream.bytes_stream().map(move |chunk| {
            chunk.map_err(|err| {
                log_upstream_error(&credentials, err);
                std::io::Error::other("provider upstream stream interrupted")
            })
        }))
    };
    response.body(body).map_err(|_| {
        error(
            StatusCode::BAD_GATEWAY,
            "invalid provider upstream response",
        )
    })
}

#[cfg(test)]
mod tests;