firstpass_proxy/run.rs
1//! Server bootstrap shared by the `firstpass` and `firstpass-proxy` binaries: build state from
2//! config, open the trace store, and serve until Ctrl-C. Keeping this in the lib means both the
3//! unified CLI (`firstpass up`) and the bare proxy binary start the server the exact same way.
4
5use std::sync::Arc;
6
7use crate::bandit::{ContextBucket, StartRungBandit};
8use crate::gate::GateHealthRegistry;
9use crate::provider::ProviderRegistry;
10use crate::{AppState, ProxyConfig, app, store};
11
12/// Initialize the global tracing subscriber from `RUST_LOG` (default `info`). Called by the
13/// binaries, not the library internals.
14pub 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/// Register a default error budget for every gate named across enforce routes: auto-disable a gate
24/// whose abstain rate exceeds 25% over its last 50 runs (SPEC §7.2).
25#[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
41/// Open the trace store, build [`AppState`], and serve the HTTP proxy until Ctrl-C.
42///
43/// # Errors
44/// Returns any error from opening the store, building the HTTP client, binding the listener, or
45/// serving.
46pub 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 // Build the provider registry from any `[[provider]]` entries in the routing config (Groq,
50 // Together, Ollama, …), on top of the built-in anthropic/openai defaults. No config => defaults.
51 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 // Online adaptive conformal (opt-in): seed the live threshold from the fixed one (or 0.5) and
63 // let /v1/feedback track it. Absent config => None => fixed-threshold behavior, byte-identical.
64 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 // Anytime-valid risk control (opt-in, ADR 0011). Absent => None => existing behaviour,
81 // byte-identical. Present, it still contributes nothing until it certifies something, so
82 // enabling it cannot change serving until the evidence exists.
83 let eprocess = config
84 .routing
85 .as_ref()
86 .and_then(|r| r.escalation.eprocess.as_ref())
87 .map(|e| {
88 Arc::new(std::sync::Mutex::new(
89 firstpass_core::eprocess::EProcessRiskControl::new(
90 e.alpha, e.delta, e.bet, &e.grid,
91 ),
92 ))
93 });
94
95 // Session promotion (opt-in): in-memory and per-process by design. A promotion is a statement
96 // about a live conversation, so it should not outlive the process that observed it — and a
97 // stale pin restored from disk would start a fresh session on an expensive rung for reasons
98 // nobody can see. Built via the shared helper so tests cannot wire it differently.
99 let promoter = crate::proxy::build_promoter_async(&config).await?;
100
101 // Verified-response cache (opt-in): in-memory and per-process, like the promoter. A cached
102 // answer is a claim that a gate passed, and that claim should not outlive the process that
103 // witnessed it — restoring one from disk would replay a proof nobody in this run observed.
104 // Fails fast on an unreachable Redis rather than degrading to a per-replica cache: a cache
105 // that quietly stays local is indistinguishable from one that is working.
106 let verified_cache = crate::proxy::build_verified_cache(&config).await?;
107
108 // UCB1 start-rung bandit (opt-in): warm-start from stored traces so learning survives
109 // restarts. Forgiving: an unreadable or absent store simply yields an empty bandit.
110 // ponytail: operator-wide load (load_all_traces) is correct here — the bandit is also
111 // operator-wide, keyed by context bucket, not by tenant.
112 let bandit = config
113 .routing
114 .as_ref()
115 .and_then(|r| r.escalation.bandit.as_ref())
116 .map(|bc| {
117 let algorithm = match bc.algorithm {
118 firstpass_core::BanditAlgorithm::Ucb1 => crate::bandit::Algorithm::Ucb1,
119 firstpass_core::BanditAlgorithm::Thompson => crate::bandit::Algorithm::Thompson,
120 };
121 // Seed from a fresh UUID: per-process randomness, no new deps (tests seed explicitly).
122 let seed = uuid::Uuid::now_v7().as_u128() as u64;
123 let mut b = StartRungBandit::with_algorithm(
124 bc.min_observations,
125 bc.exploration,
126 algorithm,
127 bc.discount,
128 seed,
129 );
130 if let Ok(traces_history) = store::load_all_traces(&config.db_path) {
131 for trace in &traces_history {
132 let ctx = ContextBucket::from_features(&trace.request.features);
133 b.feed_trace_attempts(&ctx, &trace.attempts);
134 }
135 tracing::info!(
136 n = traces_history.len(),
137 "bandit warm-started from trace store"
138 );
139 }
140 Arc::new(std::sync::Mutex::new(b))
141 });
142
143 // Per-query gate-pass predictor (ADR 0008 Phase 2, opt-in): warm-start from stored traces so
144 // the model survives restarts — every attempt with a clear Pass/Fail verdict is a training
145 // example. Forgiving: an unreadable or absent store simply yields a zero-init predictor.
146 let predictor = config
147 .routing
148 .as_ref()
149 .and_then(|r| r.escalation.predictor.as_ref())
150 .map(|pc| {
151 let mut p = firstpass_core::PassPredictor::new(pc.lr, pc.l2);
152 if let Ok(traces_history) = store::load_all_traces(&config.db_path) {
153 let mut n = 0usize;
154 for trace in &traces_history {
155 for attempt in &trace.attempts {
156 match attempt.verdict {
157 firstpass_core::Verdict::Pass => {
158 p.update(&trace.request.features, attempt.rung, true);
159 n += 1;
160 }
161 firstpass_core::Verdict::Fail => {
162 p.update(&trace.request.features, attempt.rung, false);
163 n += 1;
164 }
165 firstpass_core::Verdict::Abstain => {}
166 }
167 }
168 }
169 tracing::info!(examples = n, "predictor warm-started from trace store");
170 }
171 Arc::new(std::sync::Mutex::new(p))
172 });
173
174 let state = AppState {
175 config: Arc::new(config),
176 // Observe passthrough may stream SSE, so only bound the CONNECT phase here — a total or
177 // read timeout would sever a long-lived stream. (The enforce providers, which never stream
178 // through the adapter, carry a full request timeout — see `ProviderRegistry::new`.)
179 http: reqwest::Client::builder()
180 .connect_timeout(std::time::Duration::from_secs(10))
181 .build()?,
182 providers,
183 gate_health: Arc::new(gate_health),
184 shadow_ledger: Arc::new(crate::shadow::ShadowLedger::new()),
185 guardrails: Arc::new(crate::guard::GuardrailRegistry::new()),
186 traces,
187 adaptive,
188 eprocess,
189 verified_cache,
190 promoter,
191 bandit,
192 predictor,
193 tenant_rate_limiter,
194 spill,
195 };
196
197 let listener = tokio::net::TcpListener::bind(&bind).await?;
198 tracing::info!(%bind, "firstpass listening");
199 tracing::info!("offboard: unset ANTHROPIC_BASE_URL");
200
201 axum::serve(listener, app(state)?)
202 .with_graceful_shutdown(shutdown_signal())
203 .await?;
204
205 // Dropping `traces` (via `state`, already out of scope) closes the channel; wait for the
206 // writer to flush and exit before the process ends.
207 drop(writer);
208 Ok(())
209}
210
211async fn shutdown_signal() {
212 if let Err(err) = tokio::signal::ctrl_c().await {
213 tracing::warn!(%err, "failed to install Ctrl-C handler; shutting down anyway");
214 }
215}