zeptoclaw 0.9.0

Ultra-lightweight personal AI assistant
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Doctor — system diagnostics for ZeptoClaw.

use std::path::Path;
use std::process::Command;

use anyhow::Result;
use zeptoclaw::config::Config;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    Ok,
    Warn,
    Err,
}

impl Severity {
    pub fn icon(&self) -> &'static str {
        match self {
            Severity::Ok => "[ok]",
            Severity::Warn => "[warn]",
            Severity::Err => "[ERR]",
        }
    }
}

#[derive(Debug)]
pub struct DiagItem {
    pub severity: Severity,
    pub category: &'static str,
    pub message: String,
}

pub fn run_diagnostics(config: &Config, online: bool) -> Vec<DiagItem> {
    let mut diags = Vec::new();

    check_config(config, &mut diags);
    check_workspace_writable(&config.workspace_path(), &mut diags);
    check_environment(&mut diags);
    check_providers(config, &mut diags);
    check_channels(config, &mut diags);
    check_memory(&mut diags);
    check_coding_tools(config, &mut diags);

    if online {
        check_provider_connectivity(config, &mut diags);
    }

    diags
}

fn check_config(config: &Config, diags: &mut Vec<DiagItem>) {
    diags.push(DiagItem {
        severity: Severity::Ok,
        category: "config",
        message: "Configuration loaded successfully".into(),
    });

    let temp = config.agents.defaults.temperature;
    if !(0.0..=2.0).contains(&temp) {
        diags.push(DiagItem {
            severity: Severity::Warn,
            category: "config",
            message: format!("Temperature {} is outside typical range 0.0-2.0", temp),
        });
    }
}

fn check_workspace_writable(workspace: &Path, diags: &mut Vec<DiagItem>) {
    if !workspace.exists() {
        diags.push(DiagItem {
            severity: Severity::Err,
            category: "workspace",
            message: format!(
                "Workspace directory does not exist: {}",
                workspace.display()
            ),
        });
        return;
    }

    let probe = workspace.join(".zeptoclaw_doctor_probe");
    match std::fs::write(&probe, b"probe") {
        Ok(_) => {
            let _ = std::fs::remove_file(&probe);
            diags.push(DiagItem {
                severity: Severity::Ok,
                category: "workspace",
                message: format!("Workspace writable: {}", workspace.display()),
            });
        }
        Err(e) => {
            diags.push(DiagItem {
                severity: Severity::Err,
                category: "workspace",
                message: format!("Workspace not writable: {} ({})", workspace.display(), e),
            });
        }
    }
}

fn check_environment(diags: &mut Vec<DiagItem>) {
    // sh is required for the shell tool.
    check_binary("sh", diags);
    // git is needed for skill installation from GitHub and the git tool.
    check_binary_with_hint("git", "skill installation from GitHub won't work", diags);
}

pub fn check_binary(name: &str, diags: &mut Vec<DiagItem>) {
    check_binary_with_hint(name, "", diags);
}

fn check_binary_with_hint(name: &str, hint: &str, diags: &mut Vec<DiagItem>) {
    // Probe the binary directly instead of relying on `which`, which may
    // not be installed in minimal containers (e.g. Debian slim).
    let found = Command::new(name)
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok();

    if found {
        diags.push(DiagItem {
            severity: Severity::Ok,
            category: "environment",
            message: format!("{} found", name),
        });
    } else {
        let message = if hint.is_empty() {
            format!("{} not found in PATH", name)
        } else {
            format!("{} not found in PATH — {}", name, hint)
        };
        diags.push(DiagItem {
            severity: Severity::Warn,
            category: "environment",
            message,
        });
    }
}

pub fn check_providers(config: &Config, diags: &mut Vec<DiagItem>) {
    let mut any_configured = false;

    let named_providers = [
        ("Anthropic", &config.providers.anthropic),
        ("OpenAI", &config.providers.openai),
        ("OpenRouter", &config.providers.openrouter),
        ("Groq", &config.providers.groq),
    ];

    for (label, provider) in &named_providers {
        if let Some(ref p) = provider {
            if p.api_key.as_ref().is_some_and(|k| !k.is_empty()) {
                any_configured = true;
                diags.push(DiagItem {
                    severity: Severity::Ok,
                    category: "providers",
                    message: format!("{} API key configured", label),
                });
            }
        }
    }

    if !any_configured {
        diags.push(DiagItem {
            severity: Severity::Warn,
            category: "providers",
            message: "No provider API keys configured — add at least one to use the agent".into(),
        });
    }
}

pub fn check_channels(config: &Config, diags: &mut Vec<DiagItem>) {
    let mut any_enabled = false;

    if let Some(ref tg) = config.channels.telegram {
        if tg.enabled {
            any_enabled = true;
            if tg.token.is_empty() {
                diags.push(DiagItem {
                    severity: Severity::Err,
                    category: "channels",
                    message: "Telegram enabled but bot token is empty".into(),
                });
            } else {
                diags.push(DiagItem {
                    severity: Severity::Ok,
                    category: "channels",
                    message: "Telegram configured".into(),
                });
            }
        }
    }

    if let Some(ref dc) = config.channels.discord {
        if dc.enabled {
            any_enabled = true;
            if dc.token.is_empty() {
                diags.push(DiagItem {
                    severity: Severity::Err,
                    category: "channels",
                    message: "Discord enabled but token is empty".into(),
                });
            } else {
                diags.push(DiagItem {
                    severity: Severity::Ok,
                    category: "channels",
                    message: "Discord configured".into(),
                });
            }
        }
    }

    if !any_enabled {
        diags.push(DiagItem {
            severity: Severity::Warn,
            category: "channels",
            message: "No channels enabled (CLI-only mode)".into(),
        });
    }
}

pub fn check_memory(diags: &mut Vec<DiagItem>) {
    let ltm_path = Config::dir().join("memory").join("longterm.json");
    if ltm_path.exists() {
        match std::fs::read_to_string(&ltm_path) {
            Ok(_) => {
                diags.push(DiagItem {
                    severity: Severity::Ok,
                    category: "memory",
                    message: "Long-term memory file readable".into(),
                });
            }
            Err(e) => {
                diags.push(DiagItem {
                    severity: Severity::Err,
                    category: "memory",
                    message: format!("Long-term memory file unreadable: {}", e),
                });
            }
        }
    } else {
        diags.push(DiagItem {
            severity: Severity::Ok,
            category: "memory",
            message: "No long-term memory file yet (created on first use)".into(),
        });
    }
}

fn check_coding_tools(config: &Config, diags: &mut Vec<DiagItem>) {
    // Only relevant if a workspace is configured — no workspace means IoT/portable mode
    // where coding tools aren't expected anyway.
    let workspace = config.workspace_path();
    if !workspace.exists() {
        return;
    }
    if !config.tools.coding_tools {
        diags.push(DiagItem {
            severity: Severity::Warn,
            category: "tools",
            message: "Workspace is set but coding tools (grep, find) are disabled. \
                      Enable with `--template coder` or set `tools.coding_tools: true` in config."
                .into(),
        });
    } else {
        diags.push(DiagItem {
            severity: Severity::Ok,
            category: "tools",
            message: "Coding tools enabled (grep, find)".into(),
        });
    }
}

fn check_provider_connectivity(_config: &Config, diags: &mut Vec<DiagItem>) {
    diags.push(DiagItem {
        severity: Severity::Warn,
        category: "connectivity",
        message: "Online provider connectivity check not yet implemented".into(),
    });
}

/// CLI entry point.
pub(crate) async fn cmd_doctor(online: bool) -> Result<()> {
    let config = match Config::load() {
        Ok(c) => c,
        Err(e) => {
            println!("[ERR] config    Failed to load config: {}", e);
            println!();
            println!("Run `zeptoclaw onboard` to create a configuration.");
            return Ok(());
        }
    };

    let diags = run_diagnostics(&config, online);

    println!("ZeptoClaw Doctor");
    println!("================");
    println!();

    let mut current_category = "";
    for diag in &diags {
        if diag.category != current_category {
            if !current_category.is_empty() {
                println!();
            }
            current_category = diag.category;
        }
        println!(
            "{:<6} {:<14} {}",
            diag.severity.icon(),
            diag.category,
            diag.message
        );
    }

    println!();
    let errors = diags.iter().filter(|d| d.severity == Severity::Err).count();
    let warnings = diags
        .iter()
        .filter(|d| d.severity == Severity::Warn)
        .count();
    let ok = diags.iter().filter(|d| d.severity == Severity::Ok).count();
    println!("{} ok, {} warnings, {} errors", ok, warnings, errors);

    if errors > 0 {
        println!();
        println!("Fix the errors above to ensure ZeptoClaw works correctly.");
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_severity_display() {
        assert_eq!(Severity::Ok.icon(), "[ok]");
        assert_eq!(Severity::Warn.icon(), "[warn]");
        assert_eq!(Severity::Err.icon(), "[ERR]");
    }

    #[test]
    fn test_check_config_exists_ok() {
        let mut diags = Vec::new();
        let config = Config::default();
        check_config(&config, &mut diags);
        assert!(!diags.is_empty());
    }

    #[test]
    fn test_check_workspace_writable() {
        let mut diags = Vec::new();
        let temp = std::env::temp_dir();
        check_workspace_writable(&temp, &mut diags);
        assert!(diags.iter().any(|d| d.severity == Severity::Ok));
    }

    #[test]
    fn test_check_workspace_nonexistent() {
        let mut diags = Vec::new();
        let fake = std::path::PathBuf::from("/nonexistent/path/12345");
        check_workspace_writable(&fake, &mut diags);
        assert!(diags.iter().any(|d| d.severity == Severity::Err));
    }

    #[test]
    fn test_check_binary_present() {
        let mut diags = Vec::new();
        check_binary("sh", &mut diags);
        assert!(diags.iter().any(|d| d.severity == Severity::Ok));
    }

    #[test]
    fn test_check_binary_missing() {
        // Use a long random-looking name to avoid collisions with binaries
        // that might exist in unusual Docker/CI environments.
        let mut diags = Vec::new();
        check_binary("zeptoclaw_nonexistent_a8f3e2d1b9c7", &mut diags);
        assert!(
            diags.iter().any(|d| d.severity == Severity::Warn),
            "expected Warn for missing binary, got: {:?}",
            diags
        );
    }

    #[test]
    fn test_check_provider_no_key() {
        let mut diags = Vec::new();
        let config = Config::default();
        check_providers(&config, &mut diags);
        assert!(diags.iter().any(|d| d.severity == Severity::Warn));
    }

    #[test]
    fn test_check_channels_none_enabled() {
        let mut diags = Vec::new();
        let config = Config::default();
        check_channels(&config, &mut diags);
        assert!(!diags.is_empty());
    }

    #[test]
    fn test_check_memory_accessible() {
        let mut diags = Vec::new();
        check_memory(&mut diags);
        assert!(!diags.is_empty());
    }

    #[test]
    fn test_run_diagnostics_returns_results() {
        let config = Config::default();
        let diags = run_diagnostics(&config, false);
        assert!(!diags.is_empty());
    }
}