crabmate 0.4.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! 运行状况检查:与 **`GET /health`** JSON 形状一致,供 Axum handler 与 TUI(F10)共用。
//!
//! 默认**不**请求上游 LLM;可选(配置 [`crate::cm_config::AgentConfig::health_llm_models_probe`])对当前 `api_base` 发起 **GET …/models**(与 `crabmate probe` 同源,无 chat/completions 计费),并带进程内缓存以降低探活频率。

use serde::Serialize;
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Mutex;
use std::time::Instant;

use reqwest::Client;

use crate::cm_config::LlmHttpAuthMode;

/// 单项检查结果(与 HTTP JSON 字段一致)。
#[derive(Debug, Clone, Serialize)]
pub struct HealthCheckItem {
    pub ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

/// 与 `GET /health` 响应体一致(`status` + `checks`)。
#[derive(Debug, Clone, Serialize)]
pub struct HealthReport {
    pub status: String,
    pub checks: BTreeMap<String, HealthCheckItem>,
}

/// 进程内缓存的上一次 **GET …/models** 健康探测结果(供 [`append_llm_models_endpoint_probe`] 复用)。
#[derive(Clone)]
pub struct CachedLlmModelsHealthProbe {
    pub checked_at: Instant,
    pub item: HealthCheckItem,
}

/// [`append_llm_models_endpoint_probe`] 的输入(减少参数个数,满足 clippy)。
pub struct LlmModelsEndpointProbeParams<'a> {
    pub enabled: bool,
    pub cache_secs: u64,
    pub cache_cell: &'a Mutex<Option<CachedLlmModelsHealthProbe>>,
    pub client: &'a Client,
    pub api_base: &'a str,
    pub api_key: &'a str,
    pub auth_mode: LlmHttpAuthMode,
}

fn health_report_status(checks: &BTreeMap<String, HealthCheckItem>) -> String {
    // 进程级 API_KEY 不再作为健康必检项:官方 Client 经请求体 `client_llm.api_key` 注入。
    let required_ok = checks
        .get("workspace_writable")
        .map(|c| c.ok)
        .unwrap_or(false);
    if required_ok && checks.values().all(|c| c.ok) {
        "ok".to_string()
    } else {
        "degraded".to_string()
    }
}

/// 在 [`build_health_report`] 之后可选追加 **`llm_models_endpoint`** 检查项并重算 **`status`**。
///
/// 使用与 **`crabmate models` / `crabmate probe`** 相同的 [`crate::cm_llm::fetch_models_report`];**`bearer` 且无 `API_KEY`** 时跳过探测(检查项 `ok: true`,说明中标注跳过)。
pub async fn append_llm_models_endpoint_probe(
    report: &mut HealthReport,
    p: LlmModelsEndpointProbeParams<'_>,
) {
    if !p.enabled {
        return;
    }

    let item = if p.auth_mode == LlmHttpAuthMode::Bearer && p.api_key.trim().is_empty() {
        HealthCheckItem {
            ok: true,
            detail: Some("跳过(bearer 且无 API_KEY)".to_string()),
        }
    } else {
        let cache_ttl = std::time::Duration::from_secs(p.cache_secs.max(1));
        let from_cache = p.cache_cell.lock().ok().and_then(|guard| {
            guard.as_ref().and_then(|c| {
                if c.checked_at.elapsed() < cache_ttl {
                    Some(c.item.clone())
                } else {
                    None
                }
            })
        });

        if let Some(cached) = from_cache {
            cached
        } else {
            let fresh =
                probe_llm_models_endpoint(p.client, p.api_base, p.api_key, p.auth_mode).await;
            if let Ok(mut guard) = p.cache_cell.lock() {
                *guard = Some(CachedLlmModelsHealthProbe {
                    checked_at: Instant::now(),
                    item: fresh.clone(),
                });
            }
            fresh
        }
    };

    report
        .checks
        .insert("llm_models_endpoint".to_string(), item);
    report.status = health_report_status(&report.checks);
}

async fn probe_llm_models_endpoint(
    client: &Client,
    api_base: &str,
    api_key: &str,
    auth_mode: LlmHttpAuthMode,
) -> HealthCheckItem {
    match crate::cm_llm::fetch_models_report(client, api_base, api_key, auth_mode).await {
        Ok(rep) => {
            let ok = (200..300).contains(&rep.http_status)
                && rep.note.is_none()
                && !rep.model_ids.is_empty();
            let detail = if ok {
                Some(format!(
                    "HTTP {} · {}ms · {} 个模型 id(仅列表,无 completion)",
                    rep.http_status,
                    rep.elapsed_ms,
                    rep.model_ids.len()
                ))
            } else {
                Some(
                    rep.note
                        .unwrap_or_else(|| format!("HTTP {}(无可用模型 id)", rep.http_status)),
                )
            };
            HealthCheckItem { ok, detail }
        }
        Err(e) => HealthCheckItem {
            ok: false,
            detail: Some(format!("请求失败: {e}")),
        },
    }
}

/// 构建健康报告(阻塞工作放在 `spawn_blocking` 内)。
///
/// `include_frontend_static`:为 true 时检查 UI 静态根(`CM_WEB_STATIC_DIR` / Client `frontend/dist` 等,与挂载 SPA 一致);默认纯 API(未传 `--with-web`)应为 false。
///
/// **不**检查进程级 `API_KEY`(对话密钥由 Client 请求体提供;可选回退仍可用于 `models`/`probe` 与 `health_llm_models_probe`)。
pub async fn build_health_report(
    workspace_dir: &Path,
    include_frontend_static: bool,
) -> HealthReport {
    let mut checks: BTreeMap<String, HealthCheckItem> = BTreeMap::new();

    if include_frontend_static {
        let static_dir = crate::cm_internal::web_static_dir::resolve_web_static_dir();
        let static_ok = static_dir.is_dir();
        checks.insert(
            "frontend_static_dir".to_string(),
            HealthCheckItem {
                ok: static_ok,
                detail: if static_ok {
                    None
                } else {
                    Some(format!("目录不存在:{}", static_dir.display()))
                },
            },
        );
    }

    let work_dir = workspace_dir.to_path_buf();
    let writable = tokio::task::spawn_blocking({
        let work_dir = work_dir.clone();
        move || {
            let ts = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis();
            let pid = std::process::id();
            let p = work_dir.join(format!(".crabmate_healthcheck_{}_{}.tmp", pid, ts));
            match std::fs::write(&p, b"") {
                Ok(()) => {
                    let _ = std::fs::remove_file(&p);
                    Ok(())
                }
                Err(e) => Err(e),
            }
        }
    })
    .await
    .ok()
    .and_then(|r| r.err())
    .map(|e| format!("不可写:{}{}", work_dir.display(), e));

    checks.insert(
        "workspace_writable".to_string(),
        HealthCheckItem {
            ok: writable.is_none(),
            detail: writable,
        },
    );

    let deps = tokio::task::spawn_blocking(|| {
        fn check_cmd(cmd: &str, args: &[&str]) -> Result<String, String> {
            match std::process::Command::new(cmd).args(args).output() {
                Ok(out) => {
                    let status = out.status.code().unwrap_or(-1);
                    if status == 0 {
                        let s = if !out.stdout.is_empty() {
                            String::from_utf8_lossy(&out.stdout).trim().to_string()
                        } else {
                            String::from_utf8_lossy(&out.stderr).trim().to_string()
                        };
                        Ok(if s.is_empty() { "ok".to_string() } else { s })
                    } else {
                        Err(format!("exit={}", status))
                    }
                }
                Err(e) => Err(e.to_string()),
            }
        }

        let mut m = BTreeMap::new();

        m.insert("rustc", check_cmd("rustc", &["-V"]));
        m.insert("cargo", check_cmd("cargo", &["-V"]));

        let bc = check_cmd("bc", &["--version"])
            .or_else(|_| check_cmd("bc", &["-v"]))
            .or_else(|_| check_cmd("bc", &["-V"]));
        m.insert("bc", bc);

        m.insert("rustfmt", check_cmd("rustfmt", &["--version"]));
        m.insert("clang_format", check_cmd("clang-format", &["--version"]));
        m.insert("cmake", check_cmd("cmake", &["--version"]));
        m.insert("ctest", check_cmd("ctest", &["--version"]));
        m.insert("cxxfilt", check_cmd("c++filt", &["--version"]));
        // GNU Binutils(或兼容实现):供 run_command 白名单内 ELF/目标文件只读分析
        m.insert("objdump", check_cmd("objdump", &["--version"]));
        m.insert("nm", check_cmd("nm", &["--version"]));
        m.insert("readelf", check_cmd("readelf", &["--version"]));
        m.insert("strings_binutils", check_cmd("strings", &["--version"]));
        m.insert("size", check_cmd("size", &["--version"]));
        m.insert("ar", check_cmd("ar", &["--version"]));
        m.insert("npm", check_cmd("npm", &["--version"]));
        m.insert("python3", check_cmd("python3", &["--version"]));
        m.insert("mvn", check_cmd("mvn", &["--version"]));
        m.insert("gradle", check_cmd("gradle", &["--version"]));
        m.insert("docker", check_cmd("docker", &["--version"]));
        m.insert("podman", check_cmd("podman", &["--version"]));
        // GitHub CLI:默认 run_command 白名单含 `gh`;未安装时工具调用会失败
        m.insert("gh", check_cmd("gh", &["version"]));

        m.insert("typos", check_cmd("typos", &["--version"]));
        m.insert("codespell", check_cmd("codespell", &["--version"]));
        m.insert("ast_grep", check_cmd("ast-grep", &["--version"]));

        m.insert(
            "cargo_machete",
            check_cmd("cargo", &["machete", "--version"]),
        );
        m.insert("cargo_udeps", check_cmd("cargo", &["udeps", "--version"]));

        m.insert("shellcheck", check_cmd("shellcheck", &["--version"]));
        m.insert("cppcheck", check_cmd("cppcheck", &["--version"]));
        m.insert("semgrep", check_cmd("semgrep", &["--version"]));
        m.insert("hadolint", check_cmd("hadolint", &["--version"]));
        m.insert("bandit", check_cmd("bandit", &["--version"]));
        m.insert("lizard", check_cmd("lizard", &["--version"]));

        m
    })
    .await
    .ok()
    .unwrap_or_default();

    for (k, v) in deps {
        let key = match k {
            "rustc" => "dep_toolchain_rustc",
            "cargo" => "dep_toolchain_cargo",
            "bc" => "dep_bc",
            "rustfmt" => "dep_rustfmt",
            "clang_format" => "dep_clang_format",
            "cmake" => "dep_cmake",
            "ctest" => "dep_ctest",
            "cxxfilt" => "dep_cxxfilt",
            "objdump" => "dep_objdump",
            "nm" => "dep_nm",
            "readelf" => "dep_readelf",
            "strings_binutils" => "dep_strings_binutils",
            "size" => "dep_size",
            "ar" => "dep_ar",
            "npm" => "dep_npm",
            "python3" => "dep_python3",
            "mvn" => "dep_mvn",
            "gradle" => "dep_gradle",
            "docker" => "dep_docker_cli",
            "podman" => "dep_podman",
            "gh" => "dep_gh",
            "typos" => "dep_typos",
            "codespell" => "dep_codespell",
            "ast_grep" => "dep_ast_grep",
            "cargo_machete" => "dep_cargo_machete",
            "cargo_udeps" => "dep_cargo_udeps",
            "shellcheck" => "dep_shellcheck",
            "cppcheck" => "dep_cppcheck",
            "semgrep" => "dep_semgrep",
            "hadolint" => "dep_hadolint",
            "bandit" => "dep_bandit",
            "lizard" => "dep_lizard",
            _ => continue,
        };
        checks.insert(key.to_string(), {
            let raw = crate::cm_internal::health_dep_compat::health_item_from_cmd_result(k, v);
            HealthCheckItem {
                ok: raw.ok,
                detail: raw.detail,
            }
        });
    }

    HealthReport {
        status: health_report_status(&checks),
        checks,
    }
}

fn dep_check_label(check_key: &str) -> String {
    let name = check_key.strip_prefix("dep_").unwrap_or(check_key);
    match name {
        "toolchain_rustc" => "rustc".to_string(),
        "toolchain_cargo" => "cargo".to_string(),
        "clang_format" => "clang-format".to_string(),
        "strings_binutils" => "strings".to_string(),
        "docker_cli" => "docker".to_string(),
        "ast_grep" => "ast-grep".to_string(),
        "cargo_machete" => "cargo machete".to_string(),
        "cargo_udeps" => "cargo udeps".to_string(),
        other => other.replace('_', "-"),
    }
}

fn is_missing_cli_detail(detail: &str) -> bool {
    detail.contains("No such file") || detail.contains("not found") || detail.starts_with("exit=")
}

fn is_version_incompat_detail(detail: &str) -> bool {
    detail.contains("低于建议最低")
}

struct StartupHealthBuckets {
    config: Vec<String>,
    workspace: Vec<String>,
    toolchain: Vec<String>,
    optional_missing: Vec<String>,
    other: Vec<String>,
}

fn classify_startup_health_failures(report: &HealthReport) -> StartupHealthBuckets {
    let mut b = StartupHealthBuckets {
        config: Vec::new(),
        workspace: Vec::new(),
        toolchain: Vec::new(),
        optional_missing: Vec::new(),
        other: Vec::new(),
    };
    for (check_key, item) in &report.checks {
        if item.ok {
            continue;
        }
        let detail = item.detail.as_deref().unwrap_or("未通过");
        if check_key == "frontend_static_dir" {
            b.config.push(format!("{check_key}: {detail}"));
        } else if check_key == "workspace_writable" {
            b.workspace.push(format!("{check_key}: {detail}"));
        } else if check_key.starts_with("dep_toolchain_") {
            b.toolchain
                .push(format!("{}{detail}", dep_check_label(check_key)));
        } else if check_key.starts_with("dep_") {
            if is_version_incompat_detail(detail) {
                b.toolchain
                    .push(format!("{}{detail}", dep_check_label(check_key)));
            } else if is_missing_cli_detail(detail) {
                b.optional_missing.push(dep_check_label(check_key));
            } else {
                b.other
                    .push(format!("{}{detail}", dep_check_label(check_key)));
            }
        } else {
            b.other.push(format!("{check_key}: {detail}"));
        }
    }
    b.optional_missing.sort_unstable();
    b.optional_missing.dedup();
    b
}

/// 将未通过的检查项整理为可读摘要(供 `serve` 启动日志与测试复用)。
pub fn format_startup_health_summary(report: &HealthReport) -> String {
    let b = classify_startup_health_failures(report);
    let mut sections: Vec<String> = Vec::new();
    if !b.config.is_empty() {
        sections.push(format!("【配置】{}", b.config.join("")));
    }
    if !b.workspace.is_empty() {
        sections.push(format!("【工作区】{}", b.workspace.join("")));
    }
    if !b.toolchain.is_empty() {
        sections.push(format!("【工具链】{}", b.toolchain.join("")));
    }
    if !b.optional_missing.is_empty() {
        sections.push(format!(
            "【可选 CLI 未安装 {} 项】{}",
            b.optional_missing.len(),
            b.optional_missing.join("")
        ));
    }
    if !b.other.is_empty() {
        sections.push(format!("【其他】{}", b.other.join("")));
    }

    if sections.is_empty() {
        "存在未通过的检查项(详情见 GET /health)".to_string()
    } else {
        sections.join("")
    }
}

/// `serve` 启动时记录依赖与工具链兼容性(与 `GET /health` 同源逻辑)。
pub fn log_startup_dep_compat_summary(report: &HealthReport) {
    if report.status == "ok" {
        tracing::info!(target: "crabmate", "启动健康检查: 全部通过 (status=ok)");
        return;
    }
    let summary = format_startup_health_summary(report);
    tracing::warn!(
        target: "crabmate",
        status = %report.status,
        "启动健康检查未通过: {summary}(完整项见 GET /health)"
    );
}

#[cfg(test)]
mod health_status_tests {
    use super::{
        HealthCheckItem, HealthReport, format_startup_health_summary, health_report_status,
    };
    use std::collections::BTreeMap;

    #[test]
    fn status_ok_when_required_and_all_checks_ok() {
        let mut checks = BTreeMap::new();
        checks.insert(
            "workspace_writable".into(),
            HealthCheckItem {
                ok: true,
                detail: None,
            },
        );
        checks.insert(
            "dep_bc".into(),
            HealthCheckItem {
                ok: true,
                detail: Some("ok".into()),
            },
        );
        assert_eq!(health_report_status(&checks), "ok");
    }

    #[test]
    fn status_degraded_when_optional_dep_fails() {
        let mut checks = BTreeMap::new();
        checks.insert(
            "workspace_writable".into(),
            HealthCheckItem {
                ok: true,
                detail: None,
            },
        );
        checks.insert(
            "dep_bc".into(),
            HealthCheckItem {
                ok: false,
                detail: Some("missing".into()),
            },
        );
        assert_eq!(health_report_status(&checks), "degraded");
    }

    #[test]
    fn startup_summary_groups_optional_missing_and_config() {
        let mut checks = BTreeMap::new();
        checks.insert(
            "frontend_static_dir".into(),
            HealthCheckItem {
                ok: false,
                detail: Some("目录不存在:/tmp/missing-frontend-dist".into()),
            },
        );
        checks.insert(
            "workspace_writable".into(),
            HealthCheckItem {
                ok: true,
                detail: None,
            },
        );
        checks.insert(
            "dep_bandit".into(),
            HealthCheckItem {
                ok: false,
                detail: Some("No such file or directory (os error 2)".into()),
            },
        );
        checks.insert(
            "dep_shellcheck".into(),
            HealthCheckItem {
                ok: false,
                detail: Some("No such file or directory (os error 2)".into()),
            },
        );
        let summary = format_startup_health_summary(&HealthReport {
            status: "degraded".into(),
            checks,
        });
        assert!(summary.contains("【配置】"));
        assert!(summary.contains("frontend_static_dir"));
        assert!(summary.contains("【可选 CLI 未安装 2 项】"));
        assert!(summary.contains("bandit"));
        assert!(summary.contains("shellcheck"));
    }

    #[test]
    fn startup_summary_shows_toolchain_version_reason() {
        let mut checks = BTreeMap::new();
        checks.insert(
            "workspace_writable".into(),
            HealthCheckItem {
                ok: true,
                detail: None,
            },
        );
        checks.insert(
            "dep_toolchain_rustc".into(),
            HealthCheckItem {
                ok: false,
                detail: Some("rustc 1.84.0 (abc)(版本 1.84.0 低于建议最低 1.85.0)".into()),
            },
        );
        let summary = format_startup_health_summary(&HealthReport {
            status: "degraded".into(),
            checks,
        });
        assert!(summary.contains("【工具链】"));
        assert!(summary.contains("rustc"));
        assert!(summary.contains("1.85.0"));
    }
}