Skip to main content

lean_ctx/proxy/
mod.rs

1pub mod anthropic;
2pub mod cache_safety;
3pub mod compress;
4pub mod cost;
5pub mod forward;
6pub mod google;
7pub mod history_prune;
8pub mod introspect;
9pub mod metrics;
10pub mod openai;
11pub mod openai_responses;
12pub mod openai_responses_ws;
13pub mod prose;
14pub mod tool_kind;
15pub mod usage;
16pub mod usage_meter;
17
18use std::net::SocketAddr;
19use std::sync::Arc;
20use std::sync::atomic::{AtomicU64, Ordering};
21
22use crate::core::config::Upstreams;
23
24use axum::{
25    Router,
26    body::Body,
27    extract::State,
28    http::{Request, StatusCode},
29    response::{IntoResponse, Response},
30    routing::{any, get, post},
31};
32
33#[derive(Clone)]
34pub struct ProxyState {
35    pub client: reqwest::Client,
36    pub port: u16,
37    pub stats: Arc<ProxyStats>,
38    pub introspect: Arc<introspect::IntrospectState>,
39    /// Live provider upstreams, refreshed from config.toml without a proxy
40    /// restart (#449). Read per request via [`ProxyState::openai_upstream`] etc.
41    pub upstreams: tokio::sync::watch::Receiver<Arc<Upstreams>>,
42}
43
44impl ProxyState {
45    /// Consistent snapshot of all upstreams for the current request/response.
46    pub fn upstream_snapshot(&self) -> Arc<Upstreams> {
47        self.upstreams.borrow().clone()
48    }
49
50    /// Current Anthropic upstream (live).
51    pub fn anthropic_upstream(&self) -> String {
52        self.upstreams.borrow().anthropic.clone()
53    }
54
55    /// Current OpenAI upstream (live).
56    pub fn openai_upstream(&self) -> String {
57        self.upstreams.borrow().openai.clone()
58    }
59
60    /// Current Gemini upstream (live).
61    pub fn gemini_upstream(&self) -> String {
62        self.upstreams.borrow().gemini.clone()
63    }
64}
65
66pub struct ProxyStats {
67    pub requests_total: AtomicU64,
68    pub requests_compressed: AtomicU64,
69    pub tokens_saved: AtomicU64,
70    pub bytes_original: AtomicU64,
71    pub bytes_compressed: AtomicU64,
72}
73
74impl Default for ProxyStats {
75    fn default() -> Self {
76        Self {
77            requests_total: AtomicU64::new(0),
78            requests_compressed: AtomicU64::new(0),
79            tokens_saved: AtomicU64::new(0),
80            bytes_original: AtomicU64::new(0),
81            bytes_compressed: AtomicU64::new(0),
82        }
83    }
84}
85
86impl ProxyStats {
87    pub fn record_request(&self) {
88        self.requests_total.fetch_add(1, Ordering::Relaxed);
89    }
90
91    pub fn record_compression(&self, original: usize, compressed: usize) {
92        self.requests_compressed.fetch_add(1, Ordering::Relaxed);
93        self.bytes_original
94            .fetch_add(original as u64, Ordering::Relaxed);
95        self.bytes_compressed
96            .fetch_add(compressed as u64, Ordering::Relaxed);
97        let saved_tokens = (original.saturating_sub(compressed) / 4) as u64;
98        self.tokens_saved.fetch_add(saved_tokens, Ordering::Relaxed);
99    }
100
101    pub fn compression_ratio(&self) -> f64 {
102        let original = self.bytes_original.load(Ordering::Relaxed);
103        if original == 0 {
104            return 0.0;
105        }
106        let compressed = self.bytes_compressed.load(Ordering::Relaxed);
107        (1.0 - compressed as f64 / original as f64) * 100.0
108    }
109}
110
111/// TCP connect timeout (seconds). Configurable via `LEAN_CTX_PROXY_CONNECT_TIMEOUT_SECS`.
112fn connect_timeout_secs() -> u64 {
113    std::env::var("LEAN_CTX_PROXY_CONNECT_TIMEOUT_SECS")
114        .ok()
115        .and_then(|v| v.trim().parse::<u64>().ok())
116        .filter(|s| *s > 0)
117        .unwrap_or(15)
118}
119
120/// Idle read timeout (seconds) between bytes from upstream. Generous by default
121/// so long extended-thinking phases (which still emit SSE keepalives) are never
122/// cut, while a truly dead connection eventually fails. Configurable via
123/// `LEAN_CTX_PROXY_READ_TIMEOUT_SECS`.
124fn read_idle_timeout_secs() -> u64 {
125    std::env::var("LEAN_CTX_PROXY_READ_TIMEOUT_SECS")
126        .ok()
127        .and_then(|v| v.trim().parse::<u64>().ok())
128        .filter(|s| *s > 0)
129        .unwrap_or(300)
130}
131
132/// How often (seconds) a running proxy re-reads config.toml for upstream
133/// changes. `LEAN_CTX_PROXY_RELOAD_SECS` overrides; default 5s.
134fn upstream_reload_secs() -> u64 {
135    std::env::var("LEAN_CTX_PROXY_RELOAD_SECS")
136        .ok()
137        .and_then(|v| v.trim().parse::<u64>().ok())
138        .filter(|s| *s > 0)
139        .unwrap_or(5)
140}
141
142/// Background task: re-resolves the provider upstreams from config.toml on an
143/// interval and publishes any change to the live request handlers (#449). Ends
144/// once every receiver (the proxy itself) has been dropped.
145///
146/// `Config::load()` already keeps an internal content-hash cache, so re-reading
147/// an unchanged `config.toml` skips the TOML parse + merge and costs only a small
148/// file read; combined with the relaxed default interval (#453) the idle steady
149/// state is negligible without needing a separate stat pre-check.
150fn spawn_upstream_refresh(tx: tokio::sync::watch::Sender<Arc<Upstreams>>, initial: Upstreams) {
151    let interval = std::time::Duration::from_secs(upstream_reload_secs());
152    tokio::spawn(async move {
153        let mut last = initial;
154        loop {
155            tokio::time::sleep(interval).await;
156            let next = crate::core::config::Config::load()
157                .proxy
158                .refresh_upstreams(&last);
159            if next != last {
160                log_upstream_change(&last, &next);
161                last = next.clone();
162                if tx.send(Arc::new(next)).is_err() {
163                    break;
164                }
165            }
166        }
167    });
168}
169
170/// One stdout line per changed provider, matching the startup banner style so a
171/// running proxy's log shows when (and to what) an upstream switched.
172fn log_upstream_change(old: &Upstreams, new: &Upstreams) {
173    if old.anthropic != new.anthropic {
174        println!("  ↻ Anthropic upstream → {}", new.anthropic);
175    }
176    if old.openai != new.openai {
177        println!("  ↻ OpenAI upstream → {}", new.openai);
178    }
179    if old.gemini != new.gemini {
180        println!("  ↻ Gemini upstream → {}", new.gemini);
181    }
182}
183
184pub async fn start_proxy(port: u16) -> anyhow::Result<()> {
185    let token = crate::core::session_token::resolve_proxy_token("LEAN_CTX_PROXY_TOKEN");
186    start_proxy_with_token(port, Some(token)).await
187}
188
189/// Security invariant: the proxy NEVER runs unauthenticated. `None` does not
190/// mean "no auth" — it means "resolve the session token for me". Provider
191/// routes additionally accept provider API keys (see `proxy_auth_guard`), so
192/// IDE clients keep working without any setup.
193fn effective_auth_token(auth_token: Option<String>) -> String {
194    auth_token
195        .filter(|t| !t.trim().is_empty())
196        .unwrap_or_else(|| crate::core::session_token::resolve_proxy_token("LEAN_CTX_PROXY_TOKEN"))
197}
198
199pub async fn start_proxy_with_token(port: u16, auth_token: Option<String>) -> anyhow::Result<()> {
200    use crate::core::config::{Config, is_local_proxy_url};
201
202    let auth_token = effective_auth_token(auth_token);
203
204    // A single total timeout aborts long streaming generations (e.g. Opus doing
205    // a big refactor) mid-response. Use a connect timeout plus a read (idle)
206    // timeout instead: a genuinely hung upstream still fails, but a slow-but-
207    // alive stream is never cut off. Both are configurable for edge networks.
208    let client = reqwest::Client::builder()
209        .connect_timeout(std::time::Duration::from_secs(connect_timeout_secs()))
210        .read_timeout(std::time::Duration::from_secs(read_idle_timeout_secs()))
211        .build()?;
212
213    // Seed the measured-spend meter from disk so a proxy restart never zeroes
214    // the user's cumulative real provider bill.
215    usage_meter::resume_from_disk();
216
217    let cfg = Config::load();
218    // Read once at startup — avoids a Config::load() on every proxied request.
219    let require_token = cfg.proxy_require_token;
220    let initial = cfg.proxy.resolve_all();
221
222    // The proxy reads its upstreams live from a watch channel: a background task
223    // re-resolves them from config.toml on an interval and publishes any change,
224    // so `lean-ctx config set proxy.*_upstream` (or any config.toml edit) takes
225    // effect on the running proxy within seconds, without a restart (#449).
226    let (upstream_tx, upstream_rx) = tokio::sync::watch::channel(Arc::new(initial.clone()));
227    spawn_upstream_refresh(upstream_tx, initial.clone());
228
229    let Upstreams {
230        anthropic: anthropic_upstream,
231        openai: openai_upstream,
232        gemini: gemini_upstream,
233    } = initial;
234
235    let state = ProxyState {
236        client,
237        port,
238        stats: Arc::new(ProxyStats::default()),
239        introspect: Arc::new(introspect::IntrospectState::default()),
240        upstreams: upstream_rx,
241    };
242
243    let mut app = Router::new()
244        .route("/health", get(health))
245        .route("/status", get(status_handler))
246        .route("/v1/messages", any(anthropic::handler))
247        .route("/v1/messages/{*rest}", any(anthropic::handler))
248        .route("/v1/chat/completions", any(openai::handler))
249        // POST → HTTP/SSE forwarder; GET → Codex/OpenAI WebSocket bridge (#440).
250        .route(
251            "/v1/responses",
252            post(openai_responses::handler).get(openai_responses::ws_handler),
253        )
254        .route("/v1/responses/{*rest}", any(openai_responses::handler))
255        // Bare provider endpoints (no `/v1` prefix). Clients whose base URL points
256        // at the proxy root — notably OpenCode via `@ai-sdk/openai`, whose
257        // Responses-API requests hit `/responses` — dispatch here. The
258        // `normalize_provider_path` layer rewrites the URI to its canonical
259        // `/v1/...` form before the handler forwards upstream (#353).
260        .route("/messages", any(anthropic::handler))
261        .route("/messages/{*rest}", any(anthropic::handler))
262        .route("/chat/completions", any(openai::handler))
263        .route(
264            "/responses",
265            post(openai_responses::handler).get(openai_responses::ws_handler),
266        )
267        .route("/responses/{*rest}", any(openai_responses::handler))
268        .route("/v1/references/{id}", get(v1_resolve_reference))
269        .fallback(fallback_router)
270        .layer(axum::middleware::from_fn(host_guard))
271        .with_state(state);
272
273    {
274        let expected = auth_token.clone();
275        app = app.layer(axum::middleware::from_fn(move |req, next| {
276            let expected = expected.clone();
277            proxy_auth_guard(req, next, expected, require_token)
278        }));
279    }
280
281    // Outermost layer (runs first): normalize bare provider endpoints to their
282    // canonical `/v1/...` form so auth, routing and upstream forwarding all agree,
283    // regardless of whether the client's base URL includes `/v1` (#353).
284    app = app.layer(axum::middleware::from_fn(normalize_provider_path));
285
286    let addr = SocketAddr::from(([127, 0, 0, 1], port));
287    println!("lean-ctx proxy listening on http://{addr} (token auth enabled)");
288    println!("  Anthropic: POST /v1/messages → {anthropic_upstream}");
289    println!("  OpenAI:    POST /v1/chat/completions → {openai_upstream}");
290    println!(
291        "  OpenAI:    POST /v1/responses → {openai_upstream}  (bare /responses also accepted)"
292    );
293    println!("  Gemini:    POST /v1beta/models/... → {gemini_upstream}");
294    // Codex defaults to a WebSocket Responses transport (ws://…/responses). The
295    // proxy now bridges it to the HTTP/SSE upstream (#440), so Codex works as a
296    // drop-in without a `supports_websockets = false` workaround.
297    println!(
298        "  Codex:     WS  ws://{addr}/responses → bridged to {openai_upstream} (HTTP/SSE, #440)"
299    );
300    if openai_upstream.starts_with("http://") && !is_local_proxy_url(&openai_upstream) {
301        println!(
302            "  ⚠ OpenAI upstream is plaintext HTTP to a non-loopback host \
303             (allow_insecure_http_upstream) — use only on a trusted local network"
304        );
305    }
306
307    let listener = tokio::net::TcpListener::bind(addr).await?;
308    axum::serve(listener, app)
309        .with_graceful_shutdown(shutdown_signal())
310        .await?;
311
312    println!("lean-ctx proxy shut down cleanly.");
313    Ok(())
314}
315
316async fn shutdown_signal() {
317    let ctrl_c = tokio::signal::ctrl_c();
318
319    #[cfg(unix)]
320    {
321        // Fall back to Ctrl-C only if the SIGTERM handler cannot be installed,
322        // rather than panicking the proxy on startup.
323        match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
324            Ok(mut sigterm) => {
325                tokio::select! {
326                    _ = ctrl_c => {},
327                    _ = sigterm.recv() => {},
328                }
329            }
330            Err(e) => {
331                tracing::warn!("lean-ctx proxy: SIGTERM handler unavailable ({e}); Ctrl-C only");
332                ctrl_c.await.ok();
333            }
334        }
335    }
336
337    #[cfg(not(unix))]
338    {
339        ctrl_c.await.ok();
340    }
341
342    println!("lean-ctx proxy: received shutdown signal, draining…");
343}
344
345async fn health() -> impl IntoResponse {
346    let body = serde_json::json!({
347        "status": "ok",
348        "pid": std::process::id(),
349    });
350    (StatusCode::OK, axum::Json(body))
351}
352
353async fn v1_resolve_reference(
354    axum::extract::Path(id): axum::extract::Path<String>,
355) -> impl IntoResponse {
356    match crate::server::reference_store::resolve(&id) {
357        Some(content) => (StatusCode::OK, content),
358        None => (
359            StatusCode::NOT_FOUND,
360            "Reference expired or not found".to_string(),
361        ),
362    }
363}
364
365async fn status_handler(State(state): State<ProxyState>) -> impl IntoResponse {
366    use std::sync::atomic::Ordering::Relaxed;
367    let s = &state.stats;
368    let i = &state.introspect;
369
370    let last_breakdown = i
371        .last_breakdown
372        .lock()
373        .ok()
374        .and_then(|guard| guard.as_ref().map(|b| serde_json::to_value(b).ok()))
375        .flatten();
376
377    // Measured spend: real model + billed tokens read from provider responses.
378    let spend = usage_meter::snapshot();
379    let spend_total: f64 = spend.iter().map(|m| m.cost_usd).sum();
380
381    // Live upstreams the proxy is forwarding to right now (#449). This is the
382    // single source of truth for "where is my traffic actually going" — it
383    // reflects config.toml hot-reloads and any start-time env override.
384    let up = state.upstream_snapshot();
385
386    let body = serde_json::json!({
387        "status": "running",
388        "port": state.port,
389        "upstreams": {
390            "anthropic": up.anthropic.clone(),
391            "openai": up.openai.clone(),
392            "gemini": up.gemini.clone(),
393        },
394        "requests_total": s.requests_total.load(Relaxed),
395        "requests_compressed": s.requests_compressed.load(Relaxed),
396        "tokens_saved": s.tokens_saved.load(Relaxed),
397        "tokens_saved_estimated": true,
398        "bytes_original": s.bytes_original.load(Relaxed),
399        "bytes_compressed": s.bytes_compressed.load(Relaxed),
400        "compression_ratio_pct": format!("{:.1}", s.compression_ratio()),
401        "cache_safety": cache_safety::snapshot(),
402        "per_model": cost::snapshot(),
403        "spend": {
404            "source": "measured",
405            "total_usd": spend_total,
406            "per_model": spend,
407            "note": "Actual provider bill: real model + billed tokens (incl. cache reads/writes & reasoning) read from upstream responses for proxy-routed clients."
408        },
409        "note": "Savings are request-side (tokens removed before forwarding); they do not subtract any re-reads the agent performs. Token figures are estimates; USD uses the shared model price table.",
410        "introspect": {
411            "total_requests_analyzed": i.total_requests.load(Relaxed),
412            "total_system_prompt_tokens": i.total_system_prompt_tokens.load(Relaxed),
413            "last_breakdown": last_breakdown,
414        }
415    });
416    (StatusCode::OK, axum::Json(body))
417}
418
419async fn proxy_auth_guard(
420    req: axum::extract::Request,
421    next: axum::middleware::Next,
422    expected_token: String,
423    require_token: bool,
424) -> Result<Response, Response> {
425    let path = req.uri().path();
426    if path == "/health" {
427        return Ok(next.run(req).await);
428    }
429
430    // Accept Bearer token (lean-ctx session token)
431    if let Some(auth) = req
432        .headers()
433        .get("authorization")
434        .and_then(|v| v.to_str().ok())
435        && let Some(token) = auth.strip_prefix("Bearer ")
436        && constant_time_eq(token.as_bytes(), expected_token.as_bytes())
437    {
438        return Ok(next.run(req).await);
439    }
440
441    // Accept provider API keys on provider routes (loopback-only, host_guard runs first).
442    // AI tools like Claude Code send x-api-key, not Bearer tokens. Since the proxy
443    // only binds to 127.0.0.1, the presence of a valid API key header is sufficient
444    // to authenticate the request as coming from a local AI tool. Disabled when
445    // `proxy_require_token` is set — strict hosts then require the Bearer token.
446    if provider_key_fallback_allowed(
447        require_token,
448        has_provider_api_key(&req),
449        is_provider_route(path),
450    ) {
451        return Ok(next.run(req).await);
452    }
453
454    let cfg = crate::core::config::Config::load();
455    let hint = match cfg.proxy_enabled {
456        Some(true) => {
457            "lean-ctx proxy requires authentication. Use a Bearer token (LEAN_CTX_PROXY_TOKEN) or configure your AI tool's API key."
458        }
459        Some(false) => "lean-ctx proxy is disabled but still running. Run: lean-ctx proxy cleanup",
460        None => {
461            "lean-ctx proxy is not configured. Your AI tool's ANTHROPIC_BASE_URL may be pointing here by mistake. Fix: lean-ctx proxy cleanup  OR  lean-ctx proxy enable"
462        }
463    };
464
465    let body = serde_json::json!({
466        "type": "error",
467        "error": {
468            "type": "authentication_error",
469            "message": format!("401 Unauthorized — {hint}")
470        }
471    });
472
473    Err((StatusCode::UNAUTHORIZED, axum::Json(body)).into_response())
474}
475
476fn has_provider_api_key(req: &axum::extract::Request) -> bool {
477    let headers = req.headers();
478    // Provider-specific key headers: Anthropic `x-api-key`, Google
479    // `x-goog-api-key`, Azure `api-key`. Any non-empty value authenticates.
480    for key in ["x-api-key", "x-goog-api-key", "api-key"] {
481        if headers
482            .get(key)
483            .and_then(|v| v.to_str().ok())
484            .is_some_and(|v| !v.trim().is_empty())
485        {
486            return true;
487        }
488    }
489    // OpenAI-style `Authorization` auth. Accept ANY non-empty credential, not
490    // just `Bearer sk-`/`gsk_`: OpenAI-*compatible* providers driven through
491    // OpenCode/Codex (Azure, OpenRouter, Groq, vLLM/Ollama gateways, project &
492    // service-account keys) issue keys that don't carry those prefixes. The proxy
493    // binds to loopback only and never injects upstream credentials — it forwards
494    // this header verbatim, so an invalid key is rejected by the real upstream,
495    // never silently honoured. Gating provider routes on key *shape* only ever
496    // produced false 401s for those clients (#362).
497    if let Some(auth) = headers.get("authorization").and_then(|v| v.to_str().ok()) {
498        let auth = auth.trim();
499        let credential = auth
500            .strip_prefix("Bearer ")
501            .or_else(|| auth.strip_prefix("bearer "))
502            .unwrap_or(auth)
503            .trim();
504        // Reject an empty value or a bare scheme keyword carrying no token.
505        return !credential.is_empty() && !credential.eq_ignore_ascii_case("bearer");
506    }
507    false
508}
509
510fn is_provider_route(path: &str) -> bool {
511    path.starts_with("/v1/")
512        || path.starts_with("/v1beta/")
513        || path.starts_with("/chat/completions")
514        || path.starts_with("/responses")
515        || path.starts_with("/messages")
516}
517
518/// Decides whether a request authenticates via a provider API key alone, without
519/// the lean-ctx Bearer token. True only in the default, loopback-friendly mode
520/// where a local AI tool's own provider key is accepted on a provider route. When
521/// `require_token` is set (strict, shared-host mode) the fallback is disabled and
522/// the Bearer token becomes mandatory. Pure, so the policy is unit-testable
523/// without axum middleware plumbing.
524fn provider_key_fallback_allowed(
525    require_token: bool,
526    has_provider_key: bool,
527    is_provider_route: bool,
528) -> bool {
529    !require_token && has_provider_key && is_provider_route
530}
531
532/// Maps a bare provider endpoint to its canonical `/v1/...` form, preserving any
533/// sub-path. Returns `None` when the path is already canonical or not a known
534/// provider endpoint.
535///
536/// Some OpenAI-compatible clients treat the configured base URL as the API root
537/// and append the bare endpoint, so they send `POST /responses` or
538/// `/chat/completions` instead of `/v1/responses` — notably OpenCode via
539/// `@ai-sdk/openai`, whose Responses-API requests land on `/responses`. The proxy
540/// and every upstream only know the `/v1/...` paths, so an un-prefixed request
541/// would 401 (not a provider route) and then 404 (no handler). (#353)
542fn canonical_provider_path(path: &str) -> Option<String> {
543    // Inverse case of the bare-endpoint rewrite below: the advertised
544    // OPENAI_BASE_URL includes `/v1` (#366), so a client that treats the base URL
545    // as an origin and appends `/v1/...` itself produces `/v1/v1/...`.
546    if let Some(rest) = path.strip_prefix("/v1/v1/") {
547        return Some(format!("/v1/{rest}"));
548    }
549    const BARE_TO_CANONICAL: &[(&str, &str)] = &[
550        ("/responses", "/v1/responses"),
551        ("/chat/completions", "/v1/chat/completions"),
552        ("/messages", "/v1/messages"),
553    ];
554    for (bare, canonical) in BARE_TO_CANONICAL {
555        if path == *bare {
556            return Some((*canonical).to_string());
557        }
558        if let Some(rest) = path.strip_prefix(&format!("{bare}/")) {
559            return Some(format!("{canonical}/{rest}"));
560        }
561    }
562    None
563}
564
565/// Returns the canonicalized URI for a bare provider endpoint (query preserved),
566/// or `None` when no rewrite is needed. Pure, so the rewrite is unit-testable
567/// without constructing axum middleware plumbing.
568fn normalized_provider_uri(uri: &axum::http::Uri) -> Option<axum::http::Uri> {
569    let canonical = canonical_provider_path(uri.path())?;
570    let new_path_and_query = match uri.query() {
571        Some(q) => format!("{canonical}?{q}"),
572        None => canonical,
573    };
574    new_path_and_query.parse::<axum::http::Uri>().ok()
575}
576
577/// Rewrites the request URI in place when it targets a bare provider endpoint, so
578/// downstream auth (`is_provider_route`), routing and upstream forwarding all see
579/// the canonical `/v1/...` path. (#353)
580async fn normalize_provider_path(
581    mut req: axum::extract::Request,
582    next: axum::middleware::Next,
583) -> Response {
584    if let Some(uri) = normalized_provider_uri(req.uri()) {
585        *req.uri_mut() = uri;
586    }
587    next.run(req).await
588}
589
590fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
591    use subtle::ConstantTimeEq;
592    if a.len() != b.len() {
593        return false;
594    }
595    bool::from(a.ct_eq(b))
596}
597
598async fn host_guard(
599    req: axum::extract::Request,
600    next: axum::middleware::Next,
601) -> Result<Response, StatusCode> {
602    if let Some(host) = req.headers().get("host").and_then(|v| v.to_str().ok()) {
603        let h = host.split(':').next().unwrap_or(host);
604        if matches!(h, "127.0.0.1" | "localhost" | "[::1]") {
605            return Ok(next.run(req).await);
606        }
607    }
608    Err(StatusCode::FORBIDDEN)
609}
610
611async fn fallback_router(State(state): State<ProxyState>, req: Request<Body>) -> Response {
612    let path = req.uri().path().to_string();
613
614    if path.starts_with("/v1beta/models/") || path.starts_with("/v1/models/") {
615        match google::handler(State(state), req).await {
616            Ok(resp) => resp,
617            Err(status) => Response::builder()
618                .status(status)
619                .body(Body::from("proxy error"))
620                .expect("BUG: building error response with valid status should never fail"),
621        }
622    } else {
623        let method = req.method().to_string();
624        eprintln!("lean-ctx proxy: unmatched {method} {path}");
625        Response::builder()
626            .status(StatusCode::NOT_FOUND)
627            .body(Body::from(format!(
628                "lean-ctx proxy: no handler for {method} {path}"
629            )))
630            .expect("BUG: building 404 response should never fail")
631    }
632}
633
634#[cfg(test)]
635mod auth_tests {
636    use super::*;
637
638    // P0-4 (#416): the proxy must never run unauthenticated — `None` means
639    // "resolve the session token", not "no auth".
640    #[test]
641    fn effective_auth_token_never_yields_empty() {
642        let _env = crate::core::data_dir::test_env_lock();
643        let tmp = tempfile::tempdir().unwrap();
644        crate::test_env::set_var("LEAN_CTX_DATA_DIR", tmp.path());
645
646        assert_eq!(effective_auth_token(Some("tok".into())), "tok");
647        let auto = effective_auth_token(None);
648        assert!(!auto.trim().is_empty(), "None must auto-resolve a token");
649        let blank = effective_auth_token(Some("   ".into()));
650        assert!(!blank.trim().is_empty(), "blank tokens must be replaced");
651
652        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
653    }
654
655    #[test]
656    fn is_provider_route_v1() {
657        assert!(is_provider_route("/v1/chat/completions"));
658        assert!(is_provider_route("/v1/messages"));
659        assert!(is_provider_route("/v1/completions"));
660    }
661
662    #[test]
663    fn is_provider_route_anthropic_subpaths() {
664        assert!(is_provider_route("/v1/messages/count_tokens"));
665        assert!(is_provider_route("/v1/messages/batches"));
666        assert!(is_provider_route("/v1/messages/batches/batch_123"));
667    }
668
669    #[test]
670    fn is_provider_route_v1beta() {
671        assert!(is_provider_route("/v1beta/models"));
672    }
673
674    #[test]
675    fn is_provider_route_chat() {
676        assert!(is_provider_route("/chat/completions"));
677    }
678
679    #[test]
680    fn is_provider_route_rejects_non_provider() {
681        assert!(!is_provider_route("/health"));
682        assert!(!is_provider_route("/api/v2/test"));
683        assert!(!is_provider_route("/"));
684    }
685
686    fn build_request(headers: &[(&str, &str)], path: &str) -> axum::extract::Request {
687        let mut builder = axum::http::Request::builder().uri(path);
688        for (k, v) in headers {
689            builder = builder.header(*k, *v);
690        }
691        builder.body(axum::body::Body::empty()).unwrap()
692    }
693
694    #[test]
695    fn has_provider_api_key_x_api_key() {
696        let req = build_request(&[("x-api-key", "sk-ant-abc123")], "/v1/messages");
697        assert!(has_provider_api_key(&req));
698    }
699
700    #[test]
701    fn has_provider_api_key_x_goog() {
702        let req = build_request(&[("x-goog-api-key", "AIzaSyAbc")], "/v1beta/models");
703        assert!(has_provider_api_key(&req));
704    }
705
706    #[test]
707    fn has_provider_api_key_azure() {
708        let req = build_request(&[("api-key", "deadbeef")], "/v1/completions");
709        assert!(has_provider_api_key(&req));
710    }
711
712    #[test]
713    fn has_provider_api_key_bearer_sk() {
714        let req = build_request(
715            &[("authorization", "Bearer sk-proj-abc123")],
716            "/v1/chat/completions",
717        );
718        assert!(has_provider_api_key(&req));
719    }
720
721    #[test]
722    fn has_provider_api_key_empty_rejected() {
723        let req = build_request(&[("x-api-key", "  ")], "/v1/messages");
724        assert!(!has_provider_api_key(&req));
725    }
726
727    #[test]
728    fn has_provider_api_key_no_headers() {
729        let req = build_request(&[], "/v1/messages");
730        assert!(!has_provider_api_key(&req));
731    }
732
733    #[test]
734    fn has_provider_api_key_accepts_non_sk_bearer() {
735        // #362: OpenAI-*compatible* providers (Azure, OpenRouter, Groq, vLLM/
736        // Ollama gateways, project/service keys) issue keys without the sk-/gsk_
737        // prefix. OpenCode (@ai-sdk/openai) forwards them as `Bearer <key>`; they
738        // must authenticate on a loopback provider route. The upstream validates
739        // the real key — the proxy never injects one.
740        for key in [
741            "Bearer or-v1-9f8e7d6c", // OpenRouter
742            "Bearer gsk_live_1234",  // (still works)
743            "Bearer abc.def.ghi",    // gateway/service token
744            "Bearer 0123456789",     // opaque
745        ] {
746            let req = build_request(&[("authorization", key)], "/v1/responses");
747            assert!(
748                has_provider_api_key(&req),
749                "non-sk Bearer must count as a provider credential: {key}"
750            );
751        }
752    }
753
754    #[test]
755    fn has_provider_api_key_empty_bearer_rejected() {
756        // A blank credential — or a bare scheme word with no token (some HTTP
757        // stacks trim trailing whitespace down to just "Bearer") — is not auth.
758        for bad in ["Bearer    ", "", "Bearer", "bearer", "   "] {
759            let req = build_request(&[("authorization", bad)], "/responses");
760            assert!(
761                !has_provider_api_key(&req),
762                "blank/scheme-only Authorization must not authenticate: {bad:?}"
763            );
764        }
765    }
766
767    // --- #334: opt-in strict proxy auth (proxy_require_token) ---
768
769    #[test]
770    fn provider_key_fallback_allowed_in_default_mode() {
771        // Default (require_token = false): a provider key on a provider route is
772        // sufficient. This is what lets a local AI tool authenticate with its own
773        // key and no lean-ctx Bearer token (the loopback-friendly behavior).
774        assert!(provider_key_fallback_allowed(false, true, true));
775    }
776
777    #[test]
778    fn provider_key_fallback_denied_in_strict_mode() {
779        // Strict (require_token = true, e.g. shared/multi-user host): the
780        // provider-key fallback is disabled, so even a valid provider key on a
781        // provider route is not enough — the Bearer token becomes mandatory.
782        assert!(!provider_key_fallback_allowed(true, true, true));
783    }
784
785    #[test]
786    fn provider_key_fallback_requires_key_and_provider_route() {
787        // The fallback never fires without a provider key, nor off a provider
788        // route — regardless of mode.
789        assert!(!provider_key_fallback_allowed(false, false, true));
790        assert!(!provider_key_fallback_allowed(false, true, false));
791        assert!(!provider_key_fallback_allowed(true, false, true));
792    }
793
794    #[test]
795    fn proxy_require_token_defaults_off() {
796        // The strict mode must be opt-in: a fresh config keeps the loopback
797        // behavior so existing local setups (Claude Code, OpenCode, Codex) keep
798        // working without a token.
799        assert!(!crate::core::config::Config::default().proxy_require_token);
800    }
801
802    // --- #353: bare provider endpoints (OpenCode / @ai-sdk/openai) ---
803
804    #[test]
805    fn is_provider_route_bare_responses_and_messages() {
806        // Clients that point their base URL at the proxy root (no `/v1`) send the
807        // bare endpoint; auth must still recognise it as a provider route.
808        assert!(is_provider_route("/responses"));
809        assert!(is_provider_route("/responses/resp_123/input_items"));
810        assert!(is_provider_route("/messages"));
811    }
812
813    #[test]
814    fn canonical_provider_path_rewrites_bare_endpoints() {
815        assert_eq!(
816            canonical_provider_path("/responses").as_deref(),
817            Some("/v1/responses")
818        );
819        assert_eq!(
820            canonical_provider_path("/chat/completions").as_deref(),
821            Some("/v1/chat/completions")
822        );
823        assert_eq!(
824            canonical_provider_path("/messages").as_deref(),
825            Some("/v1/messages")
826        );
827    }
828
829    #[test]
830    fn canonical_provider_path_preserves_subpaths() {
831        assert_eq!(
832            canonical_provider_path("/responses/resp_abc/cancel").as_deref(),
833            Some("/v1/responses/resp_abc/cancel")
834        );
835        assert_eq!(
836            canonical_provider_path("/messages/batches/batch_1").as_deref(),
837            Some("/v1/messages/batches/batch_1")
838        );
839    }
840
841    #[test]
842    fn canonical_provider_path_ignores_already_canonical_and_unknown() {
843        // Already canonical → no rewrite (avoids `/v1/v1/...`).
844        assert_eq!(canonical_provider_path("/v1/responses"), None);
845        assert_eq!(canonical_provider_path("/v1/chat/completions"), None);
846        // Unrelated paths are untouched.
847        assert_eq!(canonical_provider_path("/health"), None);
848        assert_eq!(canonical_provider_path("/responsesx"), None);
849        assert_eq!(canonical_provider_path("/"), None);
850    }
851
852    #[test]
853    fn canonical_provider_path_collapses_double_v1_prefix() {
854        // OPENAI_BASE_URL now advertises `/v1` (#366); a client treating it as an
855        // origin and appending `/v1/...` itself produces a double prefix.
856        assert_eq!(
857            canonical_provider_path("/v1/v1/responses").as_deref(),
858            Some("/v1/responses")
859        );
860        assert_eq!(
861            canonical_provider_path("/v1/v1/chat/completions").as_deref(),
862            Some("/v1/chat/completions")
863        );
864    }
865
866    #[test]
867    fn normalized_provider_uri_rewrites_path_and_preserves_query() {
868        use axum::http::Uri;
869        let uri: Uri = "/responses?stream=true".parse().unwrap();
870        let rewritten = normalized_provider_uri(&uri).expect("bare /responses must rewrite");
871        assert_eq!(rewritten.path(), "/v1/responses");
872        assert_eq!(rewritten.query(), Some("stream=true"));
873        assert_eq!(
874            rewritten
875                .path_and_query()
876                .map(axum::http::uri::PathAndQuery::as_str),
877            Some("/v1/responses?stream=true")
878        );
879    }
880
881    #[test]
882    fn normalized_provider_uri_noop_for_canonical() {
883        use axum::http::Uri;
884        let uri: Uri = "/v1/responses".parse().unwrap();
885        assert!(normalized_provider_uri(&uri).is_none());
886    }
887}
888
889#[cfg(test)]
890mod upstream_tests {
891    use super::*;
892
893    fn upstreams_with_openai(openai: &str) -> Upstreams {
894        Upstreams {
895            anthropic: "https://api.anthropic.com".into(),
896            openai: openai.into(),
897            gemini: "https://generativelanguage.googleapis.com".into(),
898        }
899    }
900
901    /// The #449 core wiring: provider handlers read the upstream per request from
902    /// the watch channel, so a published change is served immediately, without
903    /// rebuilding the `ProxyState`.
904    #[tokio::test]
905    async fn proxy_state_reads_upstream_live_from_watch() {
906        let (tx, rx) =
907            tokio::sync::watch::channel(Arc::new(upstreams_with_openai("https://old.example")));
908        let state = ProxyState {
909            client: reqwest::Client::new(),
910            port: 0,
911            stats: Arc::new(ProxyStats::default()),
912            introspect: Arc::new(introspect::IntrospectState::default()),
913            upstreams: rx,
914        };
915        assert_eq!(state.openai_upstream(), "https://old.example");
916
917        tx.send(Arc::new(upstreams_with_openai("https://new.example")))
918            .unwrap();
919        assert_eq!(
920            state.openai_upstream(),
921            "https://new.example",
922            "a live handler read must reflect the published change"
923        );
924        assert_eq!(state.upstream_snapshot().openai, "https://new.example");
925    }
926
927    /// End-to-end #449 repro (in-process, no network): a `config set`-style edit
928    /// to config.toml is picked up by a *running* proxy's refresh task within the
929    /// reload interval — without any restart. Before the fix this value stayed
930    /// frozen at the start-time upstream forever.
931    ///
932    /// The process-global env lock is intentionally held across the polling
933    /// `.await`s to keep `LEAN_CTX_*` isolated for the whole test; safe because
934    /// each `#[tokio::test]` owns its current-thread runtime, so this std guard
935    /// only makes *other* test threads wait — it can never deadlock this one.
936    #[tokio::test]
937    #[allow(clippy::await_holding_lock)]
938    async fn config_change_is_picked_up_live_without_restart() {
939        use crate::core::config::Config;
940
941        let _lock = crate::core::data_dir::test_env_lock();
942        let tmp = tempfile::tempdir().unwrap();
943        crate::test_env::set_var("LEAN_CTX_DATA_DIR", tmp.path());
944        // Isolate from a developer shell that exports the env override (#449),
945        // and make the reload fast + deterministic.
946        crate::test_env::remove_var("LEAN_CTX_OPENAI_UPSTREAM");
947        crate::test_env::set_var("LEAN_CTX_PROXY_RELOAD_SECS", "1");
948
949        // Start state: config.toml points OpenAI at a loopback upstream.
950        Config::update_global(|c| {
951            c.proxy.openai_upstream = Some("http://127.0.0.1:19101".into());
952        })
953        .unwrap();
954        let initial = Config::load().proxy.resolve_all();
955        assert_eq!(initial.openai, "http://127.0.0.1:19101");
956
957        let (tx, rx) = tokio::sync::watch::channel(Arc::new(initial.clone()));
958        spawn_upstream_refresh(tx, initial);
959
960        // `lean-ctx config set proxy.openai_upstream …` (same safe write path).
961        Config::update_global(|c| {
962            c.proxy.openai_upstream = Some("http://127.0.0.1:19102".into());
963        })
964        .unwrap();
965
966        // Poll the live value the handlers would read — no restart in between.
967        let mut live = rx.borrow().openai.clone();
968        for _ in 0..80 {
969            if live == "http://127.0.0.1:19102" {
970                break;
971            }
972            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
973            live = rx.borrow().openai.clone();
974        }
975        assert_eq!(
976            live, "http://127.0.0.1:19102",
977            "running proxy must serve the new config.toml upstream without a restart"
978        );
979
980        crate::test_env::remove_var("LEAN_CTX_PROXY_RELOAD_SECS");
981        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
982    }
983}