lean-ctx-proxy 3.12.3

The LeanCTX model-API proxy: provider adapters, compression and accounting on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
use axum::{
    body::Body,
    extract::State,
    http::{HeaderName, Request, StatusCode},
    response::Response,
};

use super::admitted::{self, AdmittedBody};
use super::{ProxyState, forward, openai_responses};
use lean_ctx::core::context_admission::egress::{EgressTarget, TrustedDestinationBinding};

/// Codex subscription model turns hit ChatGPT's Responses-compatible rail:
/// `/backend-api/codex/responses`. Forward through the same compressor/metering
/// path as OpenAI Responses, but target `https://chatgpt.com`.
pub async fn codex_responses_handler(
    State(state): State<ProxyState>,
    mut req: Request<Body>,
) -> Result<Response, StatusCode> {
    // Drop the "responses-lite" marker before forwarding. Codex requests the
    // reduced lite transport on its HTTP path (`supports_websockets = false`),
    // but chatgpt.com rejects newer subscription models there
    // ("This model is not supported when using X-OpenAI-Internal-Codex-Responses-Lite",
    // seen with gpt-5.5). Stripping it makes chatgpt.com serve the full Responses
    // rail every model supports; Codex parses the full stream identically
    // (verified single- + multi-turn `previous_response_id` continuation). #623
    req.headers_mut().remove(CODEX_RESPONSES_LITE_HEADER);
    let upstream = state.chatgpt_upstream();
    forward::forward_request(
        State(state),
        req,
        &upstream,
        "/backend-api/codex/responses",
        openai_responses::compress_request_body,
        "ChatGPT",
        &[],
    )
    .await
}

/// Codex's HTTP fallback marks the reduced "responses-lite" transport with this
/// header. chatgpt.com gates newer subscription models behind the full rail, so
/// the Codex ChatGPT handler strips it (see [`codex_responses_handler`]).
const CODEX_RESPONSES_LITE_HEADER: &str = "x-openai-internal-codex-responses-lite";

/// ChatGPT's Codex rail rejects WS-only continuation fields such as
/// `previous_response_id`; ask Codex to retry through the HTTP/SSE path.
pub async fn codex_responses_ws_handler(
    State(_state): State<ProxyState>,
    _headers: axum::http::HeaderMap,
    _ws: axum::extract::ws::WebSocketUpgrade,
) -> Response {
    chatgpt_responses_ws_fallback_response()
}

fn chatgpt_responses_ws_fallback_response() -> Response {
    Response::builder()
        .status(StatusCode::UPGRADE_REQUIRED)
        .header("content-type", "application/json")
        .body(Body::from(
            r#"{"error":{"type":"unsupported_transport","message":"ChatGPT codex responses use HTTP/SSE; retry without WebSocket."}}"#,
        ))
        .expect("static response is valid")
}

/// Provider label of the ChatGPT backend rail in receipts and refusals.
pub(super) const BACKEND_PROVIDER: &str = "ChatGPT";

/// ChatGPT backend calls outside the model rail are not model JSON and must not be
/// compressed or cost-metered. They are credential-preserving passthroughs —
/// of headers. Their bodies are context leaving this machine like any other:
/// each one is admitted by the gateway, bound to the configured ChatGPT
/// upstream in its receipt, and refused with the typed reason when it may not
/// leave. An admitted body is forwarded byte for byte.
///
/// A WebSocket upgrade (Codex Desktop remote-control pairing, #597) is tunnelled
/// to chatgpt.com by [`super::chatgpt_ws`], frame by admitted frame; plain
/// HTTP/SSE falls through to the forward below.
pub async fn backend_api_handler(
    State(state): State<ProxyState>,
    req: Request<Body>,
) -> Result<Response, StatusCode> {
    if super::chatgpt_ws::is_websocket_upgrade(req.headers()) {
        return Ok(super::chatgpt_ws::passthrough(state, req).await);
    }

    let (parts, body) = req.into_parts();
    let body_bytes = axum::body::to_bytes(body, forward::max_body_bytes())
        .await
        .map_err(|_| StatusCode::PAYLOAD_TOO_LARGE)?;
    let upstream = state.chatgpt_upstream();
    let path = parts
        .uri
        .path_and_query()
        .map_or("/backend-api", axum::http::uri::PathAndQuery::as_str);
    let url = format!("{upstream}{path}");

    // The destination is the configured upstream; no caller header selects it.
    let target = EgressTarget {
        provider: BACKEND_PROVIDER,
        model: None,
        upstream_base: &upstream,
    };
    // The path, query and relayed header values leave as surely as the body
    // (AP10-C2).
    if let Err(refusal) = admitted::admit_request_target(
        path,
        parts
            .headers
            .iter()
            .filter(|(key, _)| is_backend_passthrough_request_header(key)),
        &target,
        TrustedDestinationBinding::default(),
        forward::sealed_principal(&parts).as_ref(),
    )
    .await
    {
        return Ok(refusal.response(BACKEND_PROVIDER));
    }
    let admission = admitted::admit_passthrough(
        &parts,
        &body_bytes,
        forward::max_body_bytes(),
        &target,
        TrustedDestinationBinding::default(),
    );
    // The relayed header set is bound with the body; the relay rule itself
    // (everything but hop-by-hop and this proxy's own namespace) is kept.
    let head = admitted::RequestHead::new(
        parts.method.clone(),
        parts
            .headers
            .iter()
            .filter(|(key, _)| is_backend_passthrough_request_header(key))
            .map(|(key, value)| (key.clone(), value.clone())),
    );
    let admitted_body = match admission {
        Ok((outcome, bytes)) => {
            AdmittedBody::bind(
                outcome,
                bytes,
                &upstream,
                &url,
                head,
                body_bytes.len(),
                None,
            )
            .await
        }
        Err(refusal) => Err(refusal),
    };
    let admitted_body = match admitted_body {
        Ok(admitted_body) => admitted_body,
        Err(refusal) => return Ok(refusal.response(BACKEND_PROVIDER)),
    };

    let attempt = admitted_body
        .send(&state.client, 1, |request| request)
        .await;
    let attempt = match attempt {
        Ok(attempt) => attempt,
        Err(refusal) => return Ok(refusal.response(BACKEND_PROVIDER)),
    };
    let response = attempt.response.map_err(|e| {
        tracing::error!(
            "lean-ctx proxy: ChatGPT backend upstream error: {}",
            admitted::transport_failure(&e)
        );
        StatusCode::BAD_GATEWAY
    })?;

    let status = StatusCode::from_u16(response.status().as_u16()).unwrap_or(StatusCode::OK);
    let headers = response.headers().clone();
    let is_stream = headers
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .is_some_and(|ct| ct.contains("text/event-stream"));

    let mut out = Response::builder().status(status);
    for (key, value) in &headers {
        if is_backend_passthrough_response_header(key) {
            out = out.header(key, value);
        }
    }

    if is_stream {
        return out
            .body(Body::from_stream(super::sse_keepalive::keepalive_stream(
                Box::pin(response.bytes_stream()),
            )))
            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
    }

    let bytes = response
        .bytes()
        .await
        .map_err(|_| StatusCode::BAD_GATEWAY)?;

    out.body(Body::from(bytes))
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}

fn is_backend_passthrough_request_header(name: &HeaderName) -> bool {
    let lower = name.as_str().to_ascii_lowercase();
    // `x-leanctx-*` is this proxy's own namespace (project path, agent and
    // session ids, the presenter capability). This relay passes everything it
    // does not name, so the whole prefix is named: a header added to the
    // namespace later is never relayed either.
    !lower.starts_with(LEANCTX_INTERNAL_HEADER_PREFIX)
        && !matches!(
            lower.as_str(),
            "host"
                | "connection"
                | "content-length"
                | "transfer-encoding"
                | "upgrade"
                | "keep-alive"
                | "proxy-authenticate"
                | "proxy-authorization"
                | "te"
                | "trailer"
                | "accept-encoding"
        )
}

/// Prefix of the headers that are addressed to this proxy and never leave it.
pub(super) const LEANCTX_INTERNAL_HEADER_PREFIX: &str = "x-leanctx-";

fn is_backend_passthrough_response_header(name: &HeaderName) -> bool {
    let lower = name.as_str().to_ascii_lowercase();
    !matches!(
        lower.as_str(),
        "connection"
            | "content-length"
            | "transfer-encoding"
            | "upgrade"
            | "keep-alive"
            | "proxy-authenticate"
            | "proxy-authorization"
            | "te"
            | "trailer"
    )
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::time::Duration;

    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    use super::*;
    use lean_ctx::core::config::Upstreams;

    fn proxy_state(chatgpt_upstream: String) -> ProxyState {
        let (_tx, rx) = tokio::sync::watch::channel(Arc::new(Upstreams {
            anthropic: "https://api.anthropic.com".into(),
            openai: "https://api.openai.com".into(),
            chatgpt: chatgpt_upstream,
            gemini: "https://generativelanguage.googleapis.com".into(),
            providers: Vec::new(),
        }));
        ProxyState {
            client: reqwest::Client::new(),
            port: 0,
            stats: Arc::new(crate::ProxyStats::default()),
            break_even: Arc::new(crate::break_even::BreakEvenCalculator::new(1500)),
            introspect: Arc::new(crate::introspect::IntrospectState::default()),
            ocla_cache: None,
            upstreams: rx,
            chatgpt_cookies: crate::chatgpt_cookies::shared_chatgpt_cloudflare_cookie_store(),
            mcp_servers: Arc::new(Vec::new()),
        }
    }

    #[test]
    fn codex_responses_ws_requests_trigger_http_fallback() {
        let response = chatgpt_responses_ws_fallback_response();
        assert_eq!(response.status(), StatusCode::UPGRADE_REQUIRED);
        assert_eq!(
            response
                .headers()
                .get(axum::http::header::CONTENT_TYPE)
                .unwrap(),
            "application/json"
        );
    }

    async fn spawn_streaming_upstream() -> (String, tokio::sync::oneshot::Receiver<String>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (tx, rx) = tokio::sync::oneshot::channel();
        tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.unwrap();
            let mut buf = Vec::new();
            loop {
                let mut chunk = [0_u8; 1024];
                let n = socket.read(&mut chunk).await.unwrap();
                if n == 0 {
                    break;
                }
                buf.extend_from_slice(&chunk[..n]);
                if buf.windows(4).any(|w| w == b"\r\n\r\n") {
                    break;
                }
            }
            let _ = tx.send(String::from_utf8_lossy(&buf).into_owned());
            socket
                .write_all(
                    b"HTTP/1.1 200 OK\r\n\
                      content-type: text/event-stream\r\n\
                      mcp-session-id: server-session\r\n\
                      cache-control: no-cache\r\n\
                      x-custom-backend-state: passthrough\r\n\
                      \r\n\
                      event: message\n\
                      data: {\"jsonrpc\":\"2.0\"}\n\n",
                )
                .await
                .unwrap();
            tokio::time::sleep(Duration::from_secs(2)).await;
        });
        (format!("http://{addr}"), rx)
    }

    /// The backend rail passes headers through, not unexamined content: a
    /// protected value in the body — also in a member called `name` — is
    /// masked before the body reaches the upstream.
    #[tokio::test]
    async fn backend_api_body_is_admitted_before_it_is_forwarded() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        let key = concat!("AK", "IAIOSFODNN7EXAMPLE");
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let upstream = format!("http://{}", listener.local_addr().unwrap());
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.unwrap();
            let mut request = Vec::new();
            let mut chunk = [0_u8; 1024];
            let (header_end, length) = loop {
                let read = socket.read(&mut chunk).await.unwrap();
                assert!(read > 0, "upstream closed before the request headers");
                request.extend_from_slice(&chunk[..read]);
                if let Some(end) = request.windows(4).position(|w| w == b"\r\n\r\n") {
                    let head = String::from_utf8_lossy(&request[..end]).to_ascii_lowercase();
                    let length = head
                        .lines()
                        .find_map(|line| line.strip_prefix("content-length:"))
                        .map_or(0, |value| value.trim().parse::<usize>().unwrap());
                    break (end + 4, length);
                }
            };
            while request.len() < header_end + length {
                let read = socket.read(&mut chunk).await.unwrap();
                assert!(read > 0, "upstream closed before the request body");
                request.extend_from_slice(&chunk[..read]);
            }
            socket
                .write_all(
                    b"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: 2\r\nconnection: close\r\n\r\n{}",
                )
                .await
                .unwrap();
            String::from_utf8_lossy(&request[header_end..]).into_owned()
        });
        let body = serde_json::json!({
            "note": format!("deploy with {key} today"),
            "name": key,
        });
        let req = Request::builder()
            .method("POST")
            .uri("/backend-api/wham/tasks")
            .header("content-type", "application/json")
            .body(Body::from(body.to_string()))
            .unwrap();

        let response = tokio::time::timeout(
            Duration::from_secs(5),
            backend_api_handler(State(proxy_state(upstream)), req),
        )
        .await
        .expect("bounded backend request")
        .expect("backend request should succeed");
        assert_eq!(response.status(), StatusCode::OK);

        let received = server.await.unwrap();
        assert!(
            !received.contains(key),
            "the upstream received the raw value: {received}"
        );
        assert!(received.contains("deploy with"), "{received}");
    }

    #[tokio::test]
    async fn backend_api_streams_mcp_sse_and_preserves_session_headers() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        let (upstream, seen_request) = spawn_streaming_upstream().await;
        let state = proxy_state(upstream);
        let req = Request::builder()
            .method("POST")
            .uri("/backend-api/ps/mcp?transport=streamable")
            .header("Authorization", "Bearer codex-token")
            .header("Mcp-Session-Id", "client-session")
            .header("Last-Event-ID", "event-7")
            .header("X-OpenAI-Product-Sku", "codex")
            .header("X-OpenAI-Internal-Codex-Residency", "us")
            .header("Originator", "codex_cli_rs")
            .header("Accept", "application/json, text/event-stream")
            // The proxy's own namespace, under a name no list knows.
            .header("X-LeanCTX-Later-Id", "internal")
            .body(Body::empty())
            .unwrap();

        let response = tokio::time::timeout(
            Duration::from_millis(500),
            backend_api_handler(State(state), req),
        )
        .await
        .expect("SSE passthrough must return after upstream headers")
        .expect("backend request should succeed");

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get("mcp-session-id").unwrap(),
            "server-session"
        );
        assert_eq!(
            response.headers().get("x-custom-backend-state").unwrap(),
            "passthrough"
        );

        let request = seen_request.await.unwrap().to_ascii_lowercase();
        assert!(request.contains("post /backend-api/ps/mcp?transport=streamable http/1.1"));
        assert!(request.contains("authorization: bearer codex-token"));
        assert!(request.contains("mcp-session-id: client-session"));
        assert!(request.contains("last-event-id: event-7"));
        assert!(request.contains("x-openai-product-sku: codex"));
        assert!(request.contains("x-openai-internal-codex-residency: us"));
        assert!(request.contains("originator: codex_cli_rs"));
        assert!(
            !request.contains(LEANCTX_INTERNAL_HEADER_PREFIX),
            "no x-leanctx-* header is relayed"
        );
    }
}