#[allow(clippy::wildcard_imports)]
use crate::doctor::common::*;
use crate::doctor::{Advice, BOLD, DIM, FixAction, GREEN, Outcome, RED, RST, Severity, YELLOW};
pub(crate) fn proxy_health_outcome() -> Outcome {
use lean_ctx::core::config::Config;
let cfg = Config::load();
let port = crate::proxy_setup::default_port();
match cfg.proxy_enabled {
Some(true) => {
let reachable = {
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
TcpStream::connect_timeout(&addr, crate::proxy_setup::proxy_timeout()).is_ok()
};
let supported = lean_ctx::proxy_autostart::is_supported();
if reachable {
if !crate::proxy_setup::lean_ctx_proxy_responding(port) {
return Outcome {
ok: false,
line: format!(
"{BOLD}Proxy{RST} {RED}port {port} is answered by another process, not the lean-ctx proxy{RST} {DIM}fix: lean-ctx proxy restart (or set proxy_port){RST}"
),
};
}
if supported && !lean_ctx::proxy_autostart::is_installed() {
Outcome {
ok: true,
line: format!(
"{BOLD}Proxy{RST} {GREEN}running on port {port}{RST} {YELLOW}autostart not installed — persist: lean-ctx proxy enable{RST}"
),
}
} else {
Outcome {
ok: true,
line: format!(
"{BOLD}Proxy{RST} {GREEN}enabled, running on port {port}{RST}"
),
}
}
} else if supported {
Outcome {
ok: false,
line: format!(
"{BOLD}Proxy{RST} {RED}enabled but not reachable on port {port}{RST} {YELLOW}fix: lean-ctx proxy start{RST}"
),
}
} else {
Outcome {
ok: true,
line: format!(
"{BOLD}Proxy{RST} {YELLOW}enabled but not running{RST} {DIM}autostart unavailable on this platform — start: lean-ctx proxy start{RST}"
),
}
}
}
Some(false) => Outcome {
ok: true,
line: format!(
"{BOLD}Proxy{RST} {DIM}disabled (optional feature){RST} {DIM}enable: lean-ctx proxy enable{RST}"
),
},
None => Outcome {
ok: true,
line: format!(
"{BOLD}Proxy{RST} {DIM}not configured{RST} {DIM}enable: lean-ctx proxy enable{RST}"
),
},
}
}
pub(crate) fn stale_proxy_env_outcome() -> Option<Outcome> {
use lean_ctx::core::config::Config;
let home = dirs::home_dir()?;
let cfg = Config::load();
if cfg.proxy_enabled == Some(true) {
return None;
}
let settings_dir = lean_ctx::core::editor_registry::claude_state_dir(&home);
let settings_path = settings_dir.join("settings.json");
let content = std::fs::read_to_string(&settings_path).ok()?;
let doc: serde_json::Value = lean_ctx::core::jsonc::parse_jsonc(&content).ok()?;
let base_url = doc
.get("env")
.and_then(|e| e.get("ANTHROPIC_BASE_URL"))
.and_then(|v| v.as_str())
.unwrap_or("");
if base_url.is_empty() {
return None;
}
if !crate::proxy_setup::is_local_lean_ctx_url(base_url) {
return None;
}
let state = if cfg.proxy_enabled == Some(false) {
"disabled"
} else {
"not configured"
};
Some(Outcome {
ok: false,
line: format!(
"{BOLD}Proxy env{RST} {RED}ANTHROPIC_BASE_URL → {base_url} but proxy is {state}{RST}\n\
{DIM} Claude Code routes API traffic to lean-ctx, but lean-ctx proxy is {state}.{RST}\n\
{DIM} This causes 401 auth failures. Fix:{RST}\n\
{YELLOW} lean-ctx proxy cleanup {DIM}(remove stale URL){RST}\n\
{YELLOW} lean-ctx proxy enable {DIM}(enable the proxy){RST}"
),
})
}
pub(crate) fn removed_routing_tiers_outcome() -> Option<Outcome> {
let cfg = lean_ctx::core::config::Config::load();
removed_routing_tiers_line(&cfg.proxy.routing).map(|line| Outcome { ok: false, line })
}
fn removed_routing_tiers_line(rules: &lean_ctx::core::config::RoutingRules) -> Option<String> {
if rules.tiers.is_empty() {
return None;
}
Some(format!(
"{BOLD}Proxy routing{RST} {YELLOW}[proxy.routing.tiers] is ignored{RST}\n\
{DIM} LeanCTX no longer picks models automatically; these {} tier entries route nothing.{RST}\n\
{DIM} Remove the table, or map exact model names under [proxy.routing.aliases].{RST}",
rules.tiers.len()
))
}
pub(crate) fn proxy_subscription_conflict_outcome() -> Option<(Outcome, Advice)> {
use lean_ctx::core::config::Config;
let home = dirs::home_dir()?;
let cfg = Config::load();
if cfg.proxy_enabled != Some(true) {
return None;
}
let settings_path =
lean_ctx::core::editor_registry::claude_state_dir(&home).join("settings.json");
let content = std::fs::read_to_string(&settings_path).ok()?;
let doc: serde_json::Value = lean_ctx::core::jsonc::parse_jsonc(&content).ok()?;
let base_url = doc
.get("env")
.and_then(|e| e.get("ANTHROPIC_BASE_URL"))
.and_then(|v| v.as_str())
.unwrap_or("");
if !crate::proxy_setup::is_local_lean_ctx_url(base_url) {
return None;
}
if crate::proxy_setup::anthropic_api_key_available(&home) {
return None;
}
Some(subscription_conflict())
}
pub(crate) fn subscription_conflict() -> (Outcome, Advice) {
let advice = Advice {
id: "claude_subscription_via_proxy",
title: "Claude subscription goes through the proxy".to_string(),
severity: Severity::Error,
detail: "Claude Code sends its requests to the LeanCTX proxy without an \
ANTHROPIC_API_KEY, so it uses your Pro/Max login, and Anthropic does \
not allow third-party tools to route a subscription login."
.to_string(),
actions: vec![
FixAction {
label: "Turn off the proxy".to_string(),
changes: "The proxy goes off for every client on this computer (Claude Code, \
Codex, shell exports and autostart): Claude Code talks to \
api.anthropic.com again, your subscription keeps working and the \
ctx_* tools still save tokens."
.to_string(),
commands: vec!["lean-ctx proxy disable".to_string()],
},
FixAction {
label: "Use an API key".to_string(),
changes: "Set ANTHROPIC_API_KEY where Claude Code starts (your shell profile \
or Claude Code's settings.json), then turn the proxy on again. \
Requests keep going through the proxy and are billed pay-as-you-go \
to your API key."
.to_string(),
commands: vec!["lean-ctx proxy enable".to_string()],
},
],
};
let outcome = Outcome {
ok: false,
line: format!("{BOLD}Claude auth{RST} {RED}{}{RST}", advice.title),
};
(outcome, advice)
}
pub(crate) fn proxy_upstream_outcome() -> Outcome {
use lean_ctx::core::config::{Config, ProxyProvider, is_local_proxy_url};
let cfg = Config::load();
let checks = [
(
"Anthropic",
"proxy.anthropic_upstream",
cfg.proxy.resolve_upstream(ProxyProvider::Anthropic),
),
(
"OpenAI",
"proxy.openai_upstream",
cfg.proxy.resolve_upstream(ProxyProvider::OpenAi),
),
(
"ChatGPT",
"proxy.chatgpt_upstream",
cfg.proxy.resolve_upstream(ProxyProvider::ChatGpt),
),
(
"Gemini",
"proxy.gemini_upstream",
cfg.proxy.resolve_upstream(ProxyProvider::Gemini),
),
];
let mut custom = Vec::new();
let mut plaintext = Vec::new();
for (label, key, resolved) in &checks {
if is_local_proxy_url(resolved) {
return Outcome {
ok: false,
line: format!(
"{BOLD}Proxy upstream{RST} {RED}{label} upstream points back to local proxy{RST} {YELLOW}run: lean-ctx config set {key} <url>{RST}"
),
};
}
if !resolved.starts_with("http://") && !resolved.starts_with("https://") {
return Outcome {
ok: false,
line: format!(
"{BOLD}Proxy upstream{RST} {RED}invalid {label} upstream{RST} {YELLOW}set {key} to an http(s) URL{RST}"
),
};
}
let is_default = matches!(
*label,
"Anthropic" if resolved == "https://api.anthropic.com"
) || matches!(
*label,
"OpenAI" if resolved == "https://api.openai.com"
) || matches!(
*label,
"ChatGPT" if resolved == "https://chatgpt.com"
) || matches!(
*label,
"Gemini" if resolved == "https://generativelanguage.googleapis.com"
);
if !is_default {
custom.push(format!("{label}={resolved}"));
if resolved.starts_with("http://") {
plaintext.push(*label);
}
}
}
if custom.is_empty() {
Outcome {
ok: true,
line: format!("{BOLD}Proxy upstream{RST} {GREEN}provider defaults{RST}"),
}
} else {
let mut line = format!(
"{BOLD}Proxy upstream{RST} {GREEN}custom: {}{RST}",
custom.join(", ")
);
if !plaintext.is_empty() {
line.push_str(&format!(
" {YELLOW}⚠ plaintext HTTP ({}) — trusted local network only{RST}",
plaintext.join(", ")
));
}
Outcome { ok: true, line }
}
}
pub(crate) fn proxy_upstream_drift_outcome() -> Option<Outcome> {
use lean_ctx::core::config::{
Config, ProxyProvider, UpstreamDrift, diagnose_drift, env_upstream_override,
};
let cfg = Config::load();
if cfg.proxy_enabled != Some(true) {
return None;
}
let port = crate::proxy_setup::default_port();
let (live_anthropic, live_openai, live_chatgpt, live_gemini) = proxy_live_upstreams(port)?;
let disk = cfg.proxy.resolve_all_disk();
let mut env_not_applied = Vec::new();
let mut config_not_applied = Vec::new();
for (label, key, provider, disk_val, live) in [
(
"Anthropic",
"anthropic",
ProxyProvider::Anthropic,
&disk.anthropic,
&live_anthropic,
),
(
"OpenAI",
"openai",
ProxyProvider::OpenAi,
&disk.openai,
&live_openai,
),
(
"ChatGPT",
"chatgpt",
ProxyProvider::ChatGpt,
&disk.chatgpt,
&live_chatgpt,
),
(
"Gemini",
"gemini",
ProxyProvider::Gemini,
&disk.gemini,
&live_gemini,
),
] {
let env = env_upstream_override(provider);
match diagnose_drift(env.as_deref(), disk_val, live) {
Some(UpstreamDrift::EnvNotApplied) => {
env_not_applied.push(format!(
"{label} → `lean-ctx config set proxy.{key}_upstream`"
));
}
Some(UpstreamDrift::ConfigNotApplied) => {
config_not_applied.push(format!("{label} live {live} ≠ config {disk_val}"));
}
None => {}
}
}
if env_not_applied.is_empty() && config_not_applied.is_empty() {
return None;
}
let mut line = format!("{BOLD}Proxy upstream drift{RST}");
if !env_not_applied.is_empty() {
line.push_str(&format!(
" {YELLOW}LEAN_CTX_*_UPSTREAM set in this shell but not reaching the proxy — env never reaches an MCP/service-spawned proxy (#449); persist it (applies live): {}{RST}",
env_not_applied.join(", ")
));
}
if !config_not_applied.is_empty() {
line.push_str(&format!(
" {YELLOW}{} — apply: lean-ctx proxy restart{RST}",
config_not_applied.join("; ")
));
}
Some(Outcome { ok: false, line })
}
#[cfg(test)]
mod removed_routing_tests {
#[test]
fn a_leftover_tier_table_is_reported_and_an_empty_one_is_not() {
let mut rules = lean_ctx::core::config::RoutingRules::default();
assert!(super::removed_routing_tiers_line(&rules).is_none());
rules.tiers.insert("fast".into(), "foundry:phi-4".into());
let line = super::removed_routing_tiers_line(&rules).expect("reported");
assert!(line.contains("[proxy.routing.tiers] is ignored"));
}
}