From 8c8f93e598644f6af5538e9ea28463f126f9abcb Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Nov 2026 12:39:38 +0100 Subject: 0.10.0: --json, /models /model /forget, git-repo gate for --yolo - --list/--show/--doctor gain --json for scripts - /models lists provider models, /model switches mid-session, /forget prunes sessions - --yolo refused outside a git repo (AGENT686_ALLOW_NOGIT=1 opts out) --- src/doctor.rs | 138 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 59 deletions(-) (limited to 'src/doctor.rs') diff --git a/src/doctor.rs b/src/doctor.rs index 65fca50..8baf1f7 100644 --- a/src/doctor.rs +++ b/src/doctor.rs @@ -1,109 +1,129 @@ use std::path::Path; +use serde_json::json; + 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 { +/// 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; - - println!( - "agent686 {} · {} · {}", - env!("CARGO_PKG_VERSION"), - std::env::consts::ARCH, - std::env::consts::OS - ); - println!(); + 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 => row(true, "key", "present in OPENCODE_API_KEY"), + KeySource::Env => record("key", true, "present in OPENCODE_API_KEY".into()), KeySource::File(path) => { - row(true, "key", &format!("present in {}", path.display())); + let mut detail = format!("present in {}", path.display()); use std::os::unix::fs::PermissionsExt; - if let Ok(meta) = std::fs::metadata(&path) { - if meta.permissions().mode() & 0o077 != 0 { - println!( - "{:<13} {:<4} {} is readable by other users; chmod 600 it", - "key perms", - "WARN", - path.display() - ); - } + 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 => { - row( - false, - "key", - "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key", - ); - failed = true; - } + 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(()) => row(true, "sessions", &sessions.display().to_string()), - Err(error) => { - row(false, "sessions", &format!("{}: {error}", sessions.display())); - failed = true; - } + 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) => row( + Ok(text) => record( + "history", true, + format!("{} ({} entries)", history.display(), text.lines().count()), + ), + Err(_) => record( "history", - &format!("{} ({} entries)", history.display(), text.lines().count()), + true, + format!("{} (new)", history.display()), ), - Err(_) => row(true, "history", &format!("{} (new)", history.display())), } - row(true, "provider", &cfg.base_url); - row(true, "model", &cfg.model); + record("provider", true, cfg.base_url.clone()); + record("model", true, cfg.model.clone()); if cfg.api_key.is_empty() { - row(false, "auth", "skipped: no key"); - failed = true; + record("auth", false, "skipped: no key".into()); } else { match provider::models(cfg) { Ok(models) => { - row(true, "auth", &format!("ok — {} models visible", models.len())); + record("auth", true, format!("ok — {} models visible", models.len())); if models.iter().any(|id| id == &cfg.model) { - row(true, "model known", "yes"); + record("model known", true, "yes".into()); } else { - row( - false, + record( "model known", - "no — the provider does not list this id; pick another with --model", + false, + "no — the provider does not list this id; pick another with --model".into(), ); - failed = true; } } - Err(error) => { - row(false, "auth", &error.to_string()); - failed = true; - } + 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"); } } - 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"); -- cgit v1.2.3