From fa5894ea5740852c37eba3937c6ad255574ccfd3 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:26:21 +0100 Subject: 0.3.0: --version and --doctor doctor reports key source, writable dirs, provider, model, then checks the key against GET /models and confirms the model id is listed. Non-zero exit on failure, so it can gate a deployment. --- src/doctor.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/doctor.rs (limited to 'src/doctor.rs') diff --git a/src/doctor.rs b/src/doctor.rs new file mode 100644 index 0000000..42d24a8 --- /dev/null +++ b/src/doctor.rs @@ -0,0 +1,99 @@ +use std::path::Path; + +use crate::config::{self, Config, KeySource}; +use crate::provider; +use crate::session; + +/// Prints the state of everything the harness needs, and checks the things that +/// are cheap to check but expensive to discover mid-turn. +pub fn run(cfg: &Config) -> i32 { + let mut failed = false; + + println!( + "agent686 {} · {} · {}", + env!("CARGO_PKG_VERSION"), + std::env::consts::ARCH, + std::env::consts::OS + ); + println!(); + + match config::key_source() { + KeySource::Env => row(true, "key", "present in OPENCODE_API_KEY"), + KeySource::File(path) => row(true, "key", &format!("present in {}", path.display())), + KeySource::Missing => { + row( + false, + "key", + "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key", + ); + failed = true; + } + } + + let sessions = session::sessions_dir(); + match probe_dir(&sessions) { + Ok(()) => row(true, "sessions", &sessions.display().to_string()), + Err(error) => { + row(false, "sessions", &format!("{}: {error}", sessions.display())); + failed = true; + } + } + + let history = session::data_root().join("history"); + match std::fs::read_to_string(&history) { + Ok(text) => row( + true, + "history", + &format!("{} ({} entries)", history.display(), text.lines().count()), + ), + Err(_) => row(true, "history", &format!("{} (new)", history.display())), + } + + row(true, "provider", &cfg.base_url); + row(true, "model", &cfg.model); + + if cfg.api_key.is_empty() { + row(false, "auth", "skipped: no key"); + failed = true; + } else { + match provider::models(cfg) { + Ok(models) => { + row(true, "auth", &format!("ok — {} models visible", models.len())); + if models.iter().any(|id| id == &cfg.model) { + row(true, "model known", "yes"); + } else { + row( + false, + "model known", + "no — the provider does not list this id; pick another with --model", + ); + failed = true; + } + } + Err(error) => { + row(false, "auth", &error.to_string()); + failed = true; + } + } + } + + println!(); + if failed { + println!("some checks failed"); + 1 + } else { + println!("all checks passed"); + 0 + } +} + +fn row(ok: bool, label: &str, detail: &str) { + println!("{:<13} {:<4} {detail}", label, if ok { "ok" } else { "FAIL" }); +} + +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) +} -- cgit v1.2.3