summaryrefslogtreecommitdiffstats
path: root/src/doctor.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-18 12:39:38 +0100
committersalmonik <salmonik@tuta.io>2026-11-18 12:39:38 +0100
commit8c8f93e598644f6af5538e9ea28463f126f9abcb (patch)
tree47fd5e49724a5e9ba175f0c232360ddda591e7ab /src/doctor.rs
parent0.9.2: doc accuracy and UX fixes from the review (diff)
downloadagent686-8c8f93e598644f6af5538e9ea28463f126f9abcb.tar.gz
agent686-8c8f93e598644f6af5538e9ea28463f126f9abcb.zip
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)
Diffstat (limited to 'src/doctor.rs')
-rw-r--r--src/doctor.rs138
1 files changed, 79 insertions, 59 deletions
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 @@
1use std::path::Path; 1use std::path::Path;
2 2
3use serde_json::json;
4
3use crate::config::{self, Config, KeySource}; 5use crate::config::{self, Config, KeySource};
4use crate::provider; 6use crate::provider;
5use crate::session; 7use crate::session;
6 8
7/// Prints the state of everything the harness needs, and checks the things that 9/// Checks the things that are cheap to check but expensive to discover mid-turn.
8/// are cheap to check but expensive to discover mid-turn. 10/// Returns the process exit code; with `json` it prints one JSON object instead
9pub fn run(cfg: &Config) -> i32 { 11/// of the human-readable rows.
12pub fn run(cfg: &Config, json: bool) -> i32 {
13 let mut checks: Vec<(String, bool, String)> = Vec::new();
10 let mut failed = false; 14 let mut failed = false;
11 15 let mut record = |label: &str, ok: bool, detail: String| {
12 println!( 16 if !ok {
13 "agent686 {} · {} · {}", 17 failed = true;
14 env!("CARGO_PKG_VERSION"), 18 }
15 std::env::consts::ARCH, 19 checks.push((label.to_string(), ok, detail));
16 std::env::consts::OS 20 };
17 );
18 println!();
19 21
20 match config::key_source() { 22 match config::key_source() {
21 KeySource::Env => row(true, "key", "present in OPENCODE_API_KEY"), 23 KeySource::Env => record("key", true, "present in OPENCODE_API_KEY".into()),
22 KeySource::File(path) => { 24 KeySource::File(path) => {
23 row(true, "key", &format!("present in {}", path.display())); 25 let mut detail = format!("present in {}", path.display());
24 use std::os::unix::fs::PermissionsExt; 26 use std::os::unix::fs::PermissionsExt;
25 if let Ok(meta) = std::fs::metadata(&path) { 27 if std::fs::metadata(&path)
26 if meta.permissions().mode() & 0o077 != 0 { 28 .map(|m| m.permissions().mode() & 0o077 != 0)
27 println!( 29 .unwrap_or(false)
28 "{:<13} {:<4} {} is readable by other users; chmod 600 it", 30 {
29 "key perms", 31 detail.push_str(" (readable by other users; chmod 600 it)");
30 "WARN",
31 path.display()
32 );
33 }
34 } 32 }
33 record("key", true, detail);
35 } 34 }
36 KeySource::Missing => { 35 KeySource::Missing => record("key",
37 row( 36 false,
38 false, 37 "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key".into(),
39 "key", 38 ),
40 "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key",
41 );
42 failed = true;
43 }
44 } 39 }
45 40
46 let sessions = session::sessions_dir(); 41 let sessions = session::sessions_dir();
47 match probe_dir(&sessions) { 42 match probe_dir(&sessions) {
48 Ok(()) => row(true, "sessions", &sessions.display().to_string()), 43 Ok(()) => record("sessions", true, sessions.display().to_string()),
49 Err(error) => { 44 Err(error) => record(
50 row(false, "sessions", &format!("{}: {error}", sessions.display())); 45 "sessions",
51 failed = true; 46 false,
52 } 47 format!("{}: {error}", sessions.display()),
48 ),
53 } 49 }
54 50
55 let history = session::data_root().join("history"); 51 let history = session::data_root().join("history");
56 match std::fs::read_to_string(&history) { 52 match std::fs::read_to_string(&history) {
57 Ok(text) => row( 53 Ok(text) => record(
54 "history",
58 true, 55 true,
56 format!("{} ({} entries)", history.display(), text.lines().count()),
57 ),
58 Err(_) => record(
59 "history", 59 "history",
60 &format!("{} ({} entries)", history.display(), text.lines().count()), 60 true,
61 format!("{} (new)", history.display()),
61 ), 62 ),
62 Err(_) => row(true, "history", &format!("{} (new)", history.display())),
63 } 63 }
64 64
65 row(true, "provider", &cfg.base_url); 65 record("provider", true, cfg.base_url.clone());
66 row(true, "model", &cfg.model); 66 record("model", true, cfg.model.clone());
67 67
68 if cfg.api_key.is_empty() { 68 if cfg.api_key.is_empty() {
69 row(false, "auth", "skipped: no key"); 69 record("auth", false, "skipped: no key".into());
70 failed = true;
71 } else { 70 } else {
72 match provider::models(cfg) { 71 match provider::models(cfg) {
73 Ok(models) => { 72 Ok(models) => {
74 row(true, "auth", &format!("ok — {} models visible", models.len())); 73 record("auth", true, format!("ok — {} models visible", models.len()));
75 if models.iter().any(|id| id == &cfg.model) { 74 if models.iter().any(|id| id == &cfg.model) {
76 row(true, "model known", "yes"); 75 record("model known", true, "yes".into());
77 } else { 76 } else {
78 row( 77 record(
79 false,
80 "model known", 78 "model known",
81 "no — the provider does not list this id; pick another with --model", 79 false,
80 "no — the provider does not list this id; pick another with --model".into(),
82 ); 81 );
83 failed = true;
84 } 82 }
85 } 83 }
86 Err(error) => { 84 Err(error) => record("auth", false, error.to_string()),
87 row(false, "auth", &error.to_string()); 85 }
88 failed = true; 86 }
89 } 87
88 if json {
89 let checks_json: serde_json::Map<String, serde_json::Value> = checks
90 .iter()
91 .map(|(label, ok, detail)| (label.clone(), json!({ "ok": ok, "detail": detail })))
92 .collect();
93 let doc = json!({
94 "version": env!("CARGO_PKG_VERSION"),
95 "arch": std::env::consts::ARCH,
96 "os": std::env::consts::OS,
97 "ok": !failed,
98 "checks": checks_json,
99 });
100 println!("{}", serde_json::to_string(&doc).unwrap_or_default());
101 } else {
102 println!(
103 "agent686 {} · {} · {}",
104 env!("CARGO_PKG_VERSION"),
105 std::env::consts::ARCH,
106 std::env::consts::OS
107 );
108 println!();
109 for (label, ok, detail) in &checks {
110 println!("{:<13} {:<4} {detail}", label, if *ok { "ok" } else { "FAIL" });
111 }
112 println!();
113 if failed {
114 println!("some checks failed");
115 } else {
116 println!("all checks passed");
90 } 117 }
91 } 118 }
92 119
93 println!();
94 if failed { 120 if failed {
95 println!("some checks failed");
96 1 121 1
97 } else { 122 } else {
98 println!("all checks passed");
99 0 123 0
100 } 124 }
101} 125}
102 126
103fn row(ok: bool, label: &str, detail: &str) {
104 println!("{:<13} {:<4} {detail}", label, if ok { "ok" } else { "FAIL" });
105}
106
107fn probe_dir(dir: &Path) -> std::io::Result<()> { 127fn probe_dir(dir: &Path) -> std::io::Result<()> {
108 std::fs::create_dir_all(dir)?; 128 std::fs::create_dir_all(dir)?;
109 let probe = dir.join(".doctor-probe"); 129 let probe = dir.join(".doctor-probe");