use crate::config::ProxyConfig;
use firstpass_core::{Mode, Trace};
#[derive(Debug, PartialEq, Eq)]
pub enum CheckStatus {
Ok,
Warn,
Fail,
}
#[derive(Debug)]
pub struct Check {
pub name: String,
pub status: CheckStatus,
pub detail: String,
}
impl Check {
fn new(name: impl Into<String>, status: CheckStatus, detail: impl Into<String>) -> Self {
Self {
name: name.into(),
status,
detail: detail.into(),
}
}
}
#[derive(Debug)]
pub struct DoctorReport {
pub checks: Vec<Check>,
}
impl DoctorReport {
#[must_use]
pub fn healthy(&self) -> bool {
self.checks.iter().all(|c| c.status != CheckStatus::Fail)
}
#[must_use]
pub fn render(&self) -> String {
let mut out = String::new();
for c in &self.checks {
let mark = match c.status {
CheckStatus::Ok => "✓",
CheckStatus::Warn => "!",
CheckStatus::Fail => "✗",
};
out.push_str(&format!("{mark} {}: {}\n", c.name, c.detail));
}
out.push_str(if self.healthy() {
"\nhealthy — ready to route.\n"
} else {
"\nnot healthy — fix the ✗ items above.\n"
});
out
}
}
#[must_use]
pub fn doctor(config: &ProxyConfig, env: impl Fn(&str) -> Option<String>) -> DoctorReport {
let mut checks = Vec::new();
let route_count = config.routing.as_ref().map_or(0, |c| c.routes.len());
checks.push(Check::new(
"config",
CheckStatus::Ok,
format!("default mode {:?}, {route_count} route(s)", config.mode),
));
let enforce_routes = config.routing.as_ref().map_or(0, |c| {
c.routes.iter().filter(|r| r.mode == Mode::Enforce).count()
});
if config.mode == Mode::Enforce && enforce_routes == 0 {
checks.push(Check::new(
"routing",
CheckStatus::Warn,
"mode is enforce but no enforce route is defined — all traffic will observe",
));
} else {
checks.push(Check::new(
"routing",
CheckStatus::Ok,
format!("{enforce_routes} enforce route(s)"),
));
}
if env("ANTHROPIC_API_KEY").is_some_and(|k| !k.is_empty()) {
checks.push(Check::new("anthropic-key", CheckStatus::Ok, "present"));
} else {
checks.push(Check::new(
"anthropic-key",
CheckStatus::Warn,
"ANTHROPIC_API_KEY not set — observe uses the caller's key; enforce needs one reachable",
));
}
let path = env("PATH");
let gate_defs = config.routing.as_ref().map_or(&[][..], |c| &c.gate_defs);
for def in gate_defs {
match def.cmd.first() {
Some(program) if command_on_path(program, path.as_deref()) => checks.push(Check::new(
format!("gate:{}", def.id),
CheckStatus::Ok,
format!("`{program}` found"),
)),
Some(program) => checks.push(Check::new(
format!("gate:{}", def.id),
CheckStatus::Fail,
format!("`{program}` not found on PATH"),
)),
None => checks.push(Check::new(
format!("gate:{}", def.id),
CheckStatus::Fail,
"empty command",
)),
}
}
if can_write_db(&config.db_path) {
checks.push(Check::new(
"trace-store",
CheckStatus::Ok,
format!("{} is writable", config.db_path),
));
} else {
checks.push(Check::new(
"trace-store",
CheckStatus::Fail,
format!("cannot write near {}", config.db_path),
));
}
DoctorReport { checks }
}
#[must_use]
pub fn command_on_path(program: &str, path_var: Option<&str>) -> bool {
if program.contains('/') || program.contains('\\') {
return std::path::Path::new(program).is_file();
}
let Some(path) = path_var else { return false };
std::env::split_paths(path).any(|dir| dir.join(program).is_file())
}
fn can_write_db(db_path: &str) -> bool {
let path = std::path::Path::new(db_path);
let dir = path
.parent()
.filter(|d| !d.as_os_str().is_empty())
.map_or_else(
|| std::path::PathBuf::from("."),
std::path::Path::to_path_buf,
);
let probe = dir.join(format!(".firstpass-doctor-probe-{}", std::process::id()));
match std::fs::File::create(&probe) {
Ok(_) => {
let _ = std::fs::remove_file(&probe);
true
}
Err(_) => false,
}
}
#[must_use]
pub fn format_traces(traces: &[Trace], limit: usize) -> String {
let mut lines: Vec<String> = traces
.iter()
.rev()
.take(limit)
.filter_map(|t| serde_json::to_string(t).ok())
.collect();
lines.reverse();
if lines.is_empty() {
"no traces recorded yet".to_owned()
} else {
lines.join("\n")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn config_with(toml: Option<&str>, db_path: &str) -> ProxyConfig {
ProxyConfig::from_lookup(|k| match k {
"FIRSTPASS_MODE" if toml.is_some() => Some("enforce".to_owned()),
"FIRSTPASS_CONFIG_TOML" => toml.map(str::to_owned),
"FIRSTPASS_DB" => Some(db_path.to_owned()),
_ => None,
})
.unwrap()
}
#[test]
fn command_on_path_finds_real_and_rejects_fake() {
let path = std::env::var("PATH").ok();
assert!(command_on_path("sh", path.as_deref()), "sh is on PATH");
assert!(!command_on_path(
"firstpass-definitely-not-a-real-binary",
path.as_deref()
));
assert!(!command_on_path("/nonexistent/abs/path", None));
}
#[test]
fn doctor_flags_a_missing_gate_binary() {
let toml = "[[route]]\nmatch = {}\nmode = \"enforce\"\nladder = [\"anthropic/claude-haiku-4-5\"]\ngates = [\"good\", \"bad\"]\n\
[[gate]]\nid = \"good\"\ncmd = [\"sh\"]\n\
[[gate]]\nid = \"bad\"\ncmd = [\"firstpass-nope-not-real\"]\n";
let db = std::env::temp_dir().join("firstpass-doctor-test.db");
let config = config_with(Some(toml), db.to_str().unwrap());
let report = doctor(&config, |k| match k {
"PATH" => std::env::var("PATH").ok(),
_ => None,
});
assert!(
!report.healthy(),
"a missing gate binary must fail the report"
);
let bad = report.checks.iter().find(|c| c.name == "gate:bad").unwrap();
assert_eq!(bad.status, CheckStatus::Fail);
let good = report
.checks
.iter()
.find(|c| c.name == "gate:good")
.unwrap();
assert_eq!(good.status, CheckStatus::Ok);
let key = report
.checks
.iter()
.find(|c| c.name == "anthropic-key")
.unwrap();
assert_eq!(key.status, CheckStatus::Warn);
}
#[test]
fn doctor_is_healthy_for_a_plain_observe_setup() {
let db = std::env::temp_dir().join("firstpass-doctor-ok.db");
let config = config_with(None, db.to_str().unwrap());
let report = doctor(&config, |k| {
(k == "ANTHROPIC_API_KEY").then(|| "sk-test".to_owned())
});
assert!(report.healthy(), "{}", report.render());
}
#[test]
fn format_traces_handles_empty() {
assert_eq!(format_traces(&[], 10), "no traces recorded yet");
}
}