summaryrefslogtreecommitdiffstats
path: root/src/doctor.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 15:26:21 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 15:26:21 +0100
commitfa5894ea5740852c37eba3937c6ad255574ccfd3 (patch)
tree82d09308bbe8afe3ce92c4df31a03b77d3add110 /src/doctor.rs
parent0.2.0: streaming line-range reads (diff)
downloadagent686-fa5894ea5740852c37eba3937c6ad255574ccfd3.tar.gz
agent686-fa5894ea5740852c37eba3937c6ad255574ccfd3.zip
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.
Diffstat (limited to 'src/doctor.rs')
-rw-r--r--src/doctor.rs99
1 files changed, 99 insertions, 0 deletions
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 @@
1use std::path::Path;
2
3use crate::config::{self, Config, KeySource};
4use crate::provider;
5use crate::session;
6
7/// Prints the state of everything the harness needs, and checks the things that
8/// are cheap to check but expensive to discover mid-turn.
9pub fn run(cfg: &Config) -> i32 {
10 let mut failed = false;
11
12 println!(
13 "agent686 {} · {} · {}",
14 env!("CARGO_PKG_VERSION"),
15 std::env::consts::ARCH,
16 std::env::consts::OS
17 );
18 println!();
19
20 match config::key_source() {
21 KeySource::Env => row(true, "key", "present in OPENCODE_API_KEY"),
22 KeySource::File(path) => row(true, "key", &format!("present in {}", path.display())),
23 KeySource::Missing => {
24 row(
25 false,
26 "key",
27 "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key",
28 );
29 failed = true;
30 }
31 }
32
33 let sessions = session::sessions_dir();
34 match probe_dir(&sessions) {
35 Ok(()) => row(true, "sessions", &sessions.display().to_string()),
36 Err(error) => {
37 row(false, "sessions", &format!("{}: {error}", sessions.display()));
38 failed = true;
39 }
40 }
41
42 let history = session::data_root().join("history");
43 match std::fs::read_to_string(&history) {
44 Ok(text) => row(
45 true,
46 "history",
47 &format!("{} ({} entries)", history.display(), text.lines().count()),
48 ),
49 Err(_) => row(true, "history", &format!("{} (new)", history.display())),
50 }
51
52 row(true, "provider", &cfg.base_url);
53 row(true, "model", &cfg.model);
54
55 if cfg.api_key.is_empty() {
56 row(false, "auth", "skipped: no key");
57 failed = true;
58 } else {
59 match provider::models(cfg) {
60 Ok(models) => {
61 row(true, "auth", &format!("ok — {} models visible", models.len()));
62 if models.iter().any(|id| id == &cfg.model) {
63 row(true, "model known", "yes");
64 } else {
65 row(
66 false,
67 "model known",
68 "no — the provider does not list this id; pick another with --model",
69 );
70 failed = true;
71 }
72 }
73 Err(error) => {
74 row(false, "auth", &error.to_string());
75 failed = true;
76 }
77 }
78 }
79
80 println!();
81 if failed {
82 println!("some checks failed");
83 1
84 } else {
85 println!("all checks passed");
86 0
87 }
88}
89
90fn row(ok: bool, label: &str, detail: &str) {
91 println!("{:<13} {:<4} {detail}", label, if ok { "ok" } else { "FAIL" });
92}
93
94fn probe_dir(dir: &Path) -> std::io::Result<()> {
95 std::fs::create_dir_all(dir)?;
96 let probe = dir.join(".doctor-probe");
97 std::fs::write(&probe, b"ok")?;
98 std::fs::remove_file(probe)
99}