use std::path::Path; use serde_json::json; use crate::config::{self, Config, KeySource}; use crate::provider; use crate::session; /// Checks the things that are cheap to check but expensive to discover mid-turn. /// Returns the process exit code; with `json` it prints one JSON object instead /// of the human-readable rows. pub fn run(cfg: &Config, json: bool) -> i32 { let mut checks: Vec<(String, bool, String)> = Vec::new(); let mut failed = false; let mut record = |label: &str, ok: bool, detail: String| { if !ok { failed = true; } checks.push((label.to_string(), ok, detail)); }; match config::key_source() { KeySource::Env => record("key", true, "present in OPENCODE_API_KEY".into()), KeySource::File(path) => { let mut detail = format!("present in {}", path.display()); use std::os::unix::fs::PermissionsExt; if std::fs::metadata(&path) .map(|m| m.permissions().mode() & 0o077 != 0) .unwrap_or(false) { detail.push_str(" (readable by other users; chmod 600 it)"); } record("key", true, detail); } KeySource::Missing => record("key", false, "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key".into(), ), } let sessions = session::sessions_dir(); match probe_dir(&sessions) { Ok(()) => record("sessions", true, sessions.display().to_string()), Err(error) => record( "sessions", false, format!("{}: {error}", sessions.display()), ), } let history = session::data_root().join("history"); match std::fs::read_to_string(&history) { Ok(text) => record( "history", true, format!("{} ({} entries)", history.display(), text.lines().count()), ), Err(_) => record( "history", true, format!("{} (new)", history.display()), ), } record("provider", true, cfg.base_url.clone()); record("model", true, cfg.model.clone()); if cfg.api_key.is_empty() { record("auth", false, "skipped: no key".into()); } else { match provider::models(cfg) { Ok(models) => { record("auth", true, format!("ok — {} models visible", models.len())); if models.iter().any(|id| id == &cfg.model) { record("model known", true, "yes".into()); } else { record( "model known", false, "no — the provider does not list this id; pick another with --model".into(), ); } } Err(error) => record("auth", false, error.to_string()), } } if json { let checks_json: serde_json::Map = checks .iter() .map(|(label, ok, detail)| (label.clone(), json!({ "ok": ok, "detail": detail }))) .collect(); let doc = json!({ "version": env!("CARGO_PKG_VERSION"), "arch": std::env::consts::ARCH, "os": std::env::consts::OS, "ok": !failed, "checks": checks_json, }); println!("{}", serde_json::to_string(&doc).unwrap_or_default()); } else { println!( "agent686 {} · {} · {}", env!("CARGO_PKG_VERSION"), std::env::consts::ARCH, std::env::consts::OS ); println!(); for (label, ok, detail) in &checks { println!("{:<13} {:<4} {detail}", label, if *ok { "ok" } else { "FAIL" }); } println!(); if failed { println!("some checks failed"); } else { println!("all checks passed"); } } if failed { 1 } else { 0 } } fn probe_dir(dir: &Path) -> std::io::Result<()> { std::fs::create_dir_all(dir)?; let probe = dir.join(".doctor-probe"); std::fs::write(&probe, b"ok")?; std::fs::remove_file(probe) }