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 pub upstreams: tokio::sync::watch::Receiver<Arc<Upstreams>>,
42}
43
44impl ProxyState {
45 pub fn upstream_snapshot(&self) -> Arc<Upstreams> {
47 self.upstreams.borrow().clone()
48 }
49
50 pub fn anthropic_upstream(&self) -> String {
52 self.upstreams.borrow().anthropic.clone()
53 }
54
55 pub fn openai_upstream(&self) -> String {
57 self.upstreams.borrow().openai.clone()
58 }
59
60 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
111fn 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
120fn 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
132fn 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
142fn 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
170fn 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
189fn 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 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 usage_meter::resume_from_disk();
216
217 let cfg = Config::load();
218 let require_token = cfg.proxy_require_token;
220 let initial = cfg.proxy.resolve_all();
221
222 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 .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 .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 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 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 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 let spend = usage_meter::snapshot();
379 let spend_total: f64 = spend.iter().map(|m| m.cost_usd).sum();
380
381 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 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 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 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 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 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
518fn 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
532fn canonical_provider_path(path: &str) -> Option<String> {
543 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
565fn 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
577async 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 #[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 for key in [
741 "Bearer or-v1-9f8e7d6c", "Bearer gsk_live_1234", "Bearer abc.def.ghi", "Bearer 0123456789", ] {
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 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 #[test]
770 fn provider_key_fallback_allowed_in_default_mode() {
771 assert!(provider_key_fallback_allowed(false, true, true));
775 }
776
777 #[test]
778 fn provider_key_fallback_denied_in_strict_mode() {
779 assert!(!provider_key_fallback_allowed(true, true, true));
783 }
784
785 #[test]
786 fn provider_key_fallback_requires_key_and_provider_route() {
787 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 assert!(!crate::core::config::Config::default().proxy_require_token);
800 }
801
802 #[test]
805 fn is_provider_route_bare_responses_and_messages() {
806 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 assert_eq!(canonical_provider_path("/v1/responses"), None);
845 assert_eq!(canonical_provider_path("/v1/chat/completions"), None);
846 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 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 #[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 #[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 crate::test_env::remove_var("LEAN_CTX_OPENAI_UPSTREAM");
947 crate::test_env::set_var("LEAN_CTX_PROXY_RELOAD_SECS", "1");
948
949 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 Config::update_global(|c| {
962 c.proxy.openai_upstream = Some("http://127.0.0.1:19102".into());
963 })
964 .unwrap();
965
966 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}