1use std::sync::Arc;
6
7use crate::bandit::{ContextBucket, StartRungBandit};
8use crate::gate::GateHealthRegistry;
9use crate::provider::ProviderRegistry;
10use crate::{AppState, ProxyConfig, app, store};
11
12pub fn init_tracing() {
15 tracing_subscriber::fmt()
16 .with_env_filter(
17 tracing_subscriber::EnvFilter::try_from_default_env()
18 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
19 )
20 .init();
21}
22
23#[must_use]
26pub fn build_gate_health(config: &ProxyConfig) -> GateHealthRegistry {
27 let mut gate_health = GateHealthRegistry::new();
28 if let Some(routing) = config.routing.as_ref() {
29 let mut seen = std::collections::HashSet::new();
30 for route in &routing.routes {
31 for gate in route.gates.iter().chain(&route.deferred_gates) {
32 if seen.insert(gate.clone()) {
33 gate_health = gate_health.with_budget(gate.clone(), 50, 0.25);
34 }
35 }
36 }
37 }
38 gate_health
39}
40
41pub async fn serve(config: ProxyConfig) -> Result<(), Box<dyn std::error::Error>> {
47 let (traces, spill, writer) = store::open_with_receipts(&config.db_path, config.receipts_mode)?;
48 let bind = config.bind.clone();
49 let provider_defs = config
52 .routing
53 .as_ref()
54 .map(|r| r.providers.as_slice())
55 .unwrap_or_default();
56 let providers = ProviderRegistry::from_config(
57 provider_defs,
58 &config.upstream_anthropic,
59 &config.upstream_openai,
60 );
61 let gate_health = build_gate_health(&config);
62 let adaptive = config
65 .routing
66 .as_ref()
67 .and_then(|r| r.escalation.adaptive.as_ref())
68 .map(|a| {
69 let init = config
70 .routing
71 .as_ref()
72 .and_then(|r| r.escalation.serve_threshold)
73 .unwrap_or(0.5);
74 Arc::new(std::sync::Mutex::new(
75 firstpass_core::conformal::AdaptiveConformal::new(a.alpha, a.gamma, init),
76 ))
77 });
78 let tenant_rate_limiter = crate::proxy::build_tenant_rate_limiter(&config);
79
80 let promoter = crate::proxy::build_promoter(&config)?;
85
86 let bandit = config
91 .routing
92 .as_ref()
93 .and_then(|r| r.escalation.bandit.as_ref())
94 .map(|bc| {
95 let algorithm = match bc.algorithm {
96 firstpass_core::BanditAlgorithm::Ucb1 => crate::bandit::Algorithm::Ucb1,
97 firstpass_core::BanditAlgorithm::Thompson => crate::bandit::Algorithm::Thompson,
98 };
99 let seed = uuid::Uuid::now_v7().as_u128() as u64;
101 let mut b = StartRungBandit::with_algorithm(
102 bc.min_observations,
103 bc.exploration,
104 algorithm,
105 bc.discount,
106 seed,
107 );
108 if let Ok(traces_history) = store::load_all_traces(&config.db_path) {
109 for trace in &traces_history {
110 let ctx = ContextBucket::from_features(&trace.request.features);
111 b.feed_trace_attempts(&ctx, &trace.attempts);
112 }
113 tracing::info!(
114 n = traces_history.len(),
115 "bandit warm-started from trace store"
116 );
117 }
118 Arc::new(std::sync::Mutex::new(b))
119 });
120
121 let predictor = config
125 .routing
126 .as_ref()
127 .and_then(|r| r.escalation.predictor.as_ref())
128 .map(|pc| {
129 let mut p = firstpass_core::PassPredictor::new(pc.lr, pc.l2);
130 if let Ok(traces_history) = store::load_all_traces(&config.db_path) {
131 let mut n = 0usize;
132 for trace in &traces_history {
133 for attempt in &trace.attempts {
134 match attempt.verdict {
135 firstpass_core::Verdict::Pass => {
136 p.update(&trace.request.features, attempt.rung, true);
137 n += 1;
138 }
139 firstpass_core::Verdict::Fail => {
140 p.update(&trace.request.features, attempt.rung, false);
141 n += 1;
142 }
143 firstpass_core::Verdict::Abstain => {}
144 }
145 }
146 }
147 tracing::info!(examples = n, "predictor warm-started from trace store");
148 }
149 Arc::new(std::sync::Mutex::new(p))
150 });
151
152 let state = AppState {
153 config: Arc::new(config),
154 http: reqwest::Client::builder()
158 .connect_timeout(std::time::Duration::from_secs(10))
159 .build()?,
160 providers,
161 gate_health: Arc::new(gate_health),
162 shadow_ledger: Arc::new(crate::shadow::ShadowLedger::new()),
163 guardrails: Arc::new(crate::guard::GuardrailRegistry::new()),
164 traces,
165 adaptive,
166 promoter,
167 bandit,
168 predictor,
169 tenant_rate_limiter,
170 spill,
171 };
172
173 let listener = tokio::net::TcpListener::bind(&bind).await?;
174 tracing::info!(%bind, "firstpass listening");
175 tracing::info!("offboard: unset ANTHROPIC_BASE_URL");
176
177 axum::serve(listener, app(state)?)
178 .with_graceful_shutdown(shutdown_signal())
179 .await?;
180
181 drop(writer);
184 Ok(())
185}
186
187async fn shutdown_signal() {
188 if let Err(err) = tokio::signal::ctrl_c().await {
189 tracing::warn!(%err, "failed to install Ctrl-C handler; shutting down anyway");
190 }
191}