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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! Rust 开发工具:cargo check/test/clippy/metadata/run/tree/clean/doc/outdated/machete/udeps/publish dry-run、工作区内 `rustc`。
#![allow(clippy::result_large_err)] // `ToolError` 含 legacy 解析快照,与 `run_tool_dispatch` 一致

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

use crate::cm_tools::cargo_metadata::cargo_metadata_command;
use crate::cm_tools::tool_result::ToolError;

use super::ToolContext;
use super::output_util;
use super::test_result_cache::{
    TestCacheKey, TestCacheKind, cargo_test_args_fingerprint, fingerprint_rust_workspace_sources,
    store_cached, try_get_cached, wrap_cache_hit,
};

const MAX_OUTPUT_LINES: usize = 800;

pub fn cargo_check(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_check_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_check_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    run_cargo_subcommand_str_try("check", args_json, workspace_root, max_output_len)
}

const RUST_RUSTC_MAX_ARGS: usize = 64;
const RUST_RUSTC_MAX_ARG_BYTES: usize = 8192;

fn rustc_arg_is_safe(arg: &str) -> bool {
    let a = arg.trim();
    !a.contains("..") && !a.starts_with('/')
}

/// 在工作区根目录执行 **`rustc`**(不经 shell),参数须与 `run_command` 相同:**不得**含 `..` 或以 `/` 开头的参数。
/// 适用于 `rustc --explain E0xxx`、`rustc -vV`、`rustc --print=cfg` 等;**不要求**存在 `Cargo.toml`。
pub fn rust_rustc(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    rust_rustc_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn rust_rustc_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    let args: Vec<String> = match v.get("args") {
        Some(serde_json::Value::Array(arr)) => arr
            .iter()
            .filter_map(|x| x.as_str().map(String::from))
            .collect(),
        Some(_) => {
            return Err(ToolError::invalid_args(
                "错误:args 必须是字符串数组".to_string(),
            ));
        }
        None => Vec::new(),
    };
    if args.len() > RUST_RUSTC_MAX_ARGS {
        return Err(ToolError::invalid_args(format!(
            "错误:rustc 参数个数超过上限 {}",
            RUST_RUSTC_MAX_ARGS
        )));
    }
    for a in &args {
        if a.len() > RUST_RUSTC_MAX_ARG_BYTES {
            return Err(ToolError::invalid_args(format!(
                "错误:单参数长度超过 {} 字节",
                RUST_RUSTC_MAX_ARG_BYTES
            )));
        }
        if !rustc_arg_is_safe(a) {
            return Err(ToolError::invalid_args(
                "错误:参数不允许包含 \"..\" 或绝对路径(以 / 开头)".to_string(),
            ));
        }
    }

    let mut cmd = Command::new("rustc");
    cmd.args(&args).current_dir(workspace_root);
    run_and_format_try(cmd, max_output_len, "rustc", "rust_rustc")
}

pub fn cargo_test(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
    ctx: Option<&ToolContext<'_>>,
) -> String {
    cargo_test_try(args_json, workspace_root, max_output_len, ctx).unwrap_or_else(|e| e.message)
}

pub fn cargo_test_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
    ctx: Option<&ToolContext<'_>>,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    let Some(c) = ctx else {
        return run_cargo_subcommand_value_try("test", &v, workspace_root, max_output_len);
    };
    maybe_cache_cargo_test_try(&v, workspace_root, c, || {
        run_cargo_subcommand_value_try("test", &v, workspace_root, max_output_len)
    })
}

pub fn cargo_clippy(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_clippy_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_clippy_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    run_cargo_subcommand_str_try("clippy", args_json, workspace_root, max_output_len)
}

pub fn cargo_run(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_run_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_run_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    run_cargo_subcommand_str_try("run", args_json, workspace_root, max_output_len)
}

pub fn rust_test_one(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
    ctx: Option<&ToolContext<'_>>,
) -> String {
    rust_test_one_try(args_json, workspace_root, max_output_len, ctx).unwrap_or_else(|e| e.message)
}

pub fn rust_test_one_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
    ctx: Option<&ToolContext<'_>>,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    let filter = match v.get("test_name").and_then(|x| x.as_str()).map(str::trim) {
        Some(s) if !s.is_empty() => s.to_string(),
        _ => {
            return Err(ToolError::invalid_args(
                "错误:缺少 test_name 参数".to_string(),
            ));
        }
    };
    let mut merged = v;
    if let Some(obj) = merged.as_object_mut() {
        obj.insert("test_filter".to_string(), serde_json::Value::String(filter));
    }
    let Some(c) = ctx else {
        return run_cargo_subcommand_value_try("test", &merged, workspace_root, max_output_len);
    };
    maybe_cache_cargo_test_try(&merged, workspace_root, c, || {
        run_cargo_subcommand_value_try("test", &merged, workspace_root, max_output_len)
    })
}

fn maybe_cache_cargo_test_try(
    v: &serde_json::Value,
    workspace_root: &Path,
    ctx: &ToolContext<'_>,
    run: impl FnOnce() -> Result<String, ToolError>,
) -> Result<String, ToolError> {
    if ctx.test_result_cache_enabled
        && let Some(inputs_fp) = fingerprint_rust_workspace_sources(workspace_root)
    {
        let args_fp = cargo_test_args_fingerprint(v);
        let root = workspace_root.to_path_buf();
        let key = TestCacheKey {
            workspace_root: root,
            kind: TestCacheKind::CargoTest,
            args_fingerprint: args_fp,
            inputs_fingerprint: inputs_fp.clone(),
        };
        if let Some(hit) = try_get_cached(
            ctx.test_result_cache_enabled,
            ctx.test_result_cache_max_entries,
            &key,
        ) {
            return Ok(wrap_cache_hit(&inputs_fp, &hit));
        }
        let out = run()?;
        store_cached(
            ctx.test_result_cache_enabled,
            ctx.test_result_cache_max_entries,
            key,
            out.clone(),
        );
        return Ok(out);
    }
    run()
}

pub fn cargo_metadata(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_metadata_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_metadata_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    let no_deps = v.get("no_deps").and_then(|x| x.as_bool()).unwrap_or(true);
    let format_version = v
        .get("format_version")
        .and_then(|x| x.as_u64())
        .unwrap_or(1);

    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }

    let cmd = cargo_metadata_command(workspace_root, no_deps, format_version);
    run_and_format_try(cmd, max_output_len, "cargo metadata", "cargo_metadata")
}

pub fn cargo_tree(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_tree_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_tree_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let package = v.get("package").and_then(|x| x.as_str()).map(str::trim);
    let invert = v.get("invert").and_then(|x| x.as_str()).map(str::trim);
    let depth = v.get("depth").and_then(|x| x.as_u64());
    let edges = v.get("edges").and_then(|x| x.as_str()).map(str::trim);

    let mut cmd = Command::new("cargo");
    cmd.arg("tree");
    if let Some(p) = package.filter(|s| !s.is_empty()) {
        cmd.arg("--package").arg(p);
    }
    if let Some(i) = invert.filter(|s| !s.is_empty()) {
        cmd.arg("--invert").arg(i);
    }
    if let Some(d) = depth {
        cmd.arg("--depth").arg(d.to_string());
    }
    if let Some(e) = edges.filter(|s| !s.is_empty()) {
        cmd.arg("--edges").arg(e);
    }
    cmd.current_dir(workspace_root);
    run_and_format_try(cmd, max_output_len, "cargo tree", "cargo_tree")
}

pub fn cargo_clean(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_clean_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_clean_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let package = v.get("package").and_then(|x| x.as_str()).map(str::trim);
    let release = v.get("release").and_then(|x| x.as_bool()).unwrap_or(false);
    let doc = v.get("doc").and_then(|x| x.as_bool()).unwrap_or(false);
    let dry_run = v.get("dry_run").and_then(|x| x.as_bool()).unwrap_or(true);

    let mut cmd = Command::new("cargo");
    cmd.arg("clean");
    if let Some(p) = package.filter(|s| !s.is_empty()) {
        cmd.arg("--package").arg(p);
    }
    if release {
        cmd.arg("--release");
    }
    if doc {
        cmd.arg("--doc");
    }
    if dry_run {
        cmd.arg("--dry-run");
    }
    cmd.current_dir(workspace_root);
    run_and_format_try(cmd, max_output_len, "cargo clean", "cargo_clean")
}

pub fn cargo_doc(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_doc_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_doc_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let package = v.get("package").and_then(|x| x.as_str()).map(str::trim);
    let no_deps = v.get("no_deps").and_then(|x| x.as_bool()).unwrap_or(true);
    let open = v.get("open").and_then(|x| x.as_bool()).unwrap_or(false);

    let mut cmd = Command::new("cargo");
    cmd.arg("doc");
    if let Some(p) = package.filter(|s| !s.is_empty()) {
        cmd.arg("--package").arg(p);
    }
    if no_deps {
        cmd.arg("--no-deps");
    }
    if open {
        cmd.arg("--open");
    }
    cmd.current_dir(workspace_root);
    run_and_format_try(cmd, max_output_len, "cargo doc", "cargo_doc")
}

pub fn cargo_nextest(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_nextest_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_nextest_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let test_filter = v
        .get("test_filter")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let package = v.get("package").and_then(|x| x.as_str()).map(str::trim);
    let profile = v
        .get("profile")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let no_capture = v
        .get("nocapture")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);

    let mut cmd = Command::new("cargo");
    cmd.arg("nextest").arg("run");
    if let Some(p) = package.filter(|s| !s.is_empty()) {
        cmd.arg("--package").arg(p);
    }
    if let Some(p) = profile {
        cmd.arg("--profile").arg(p);
    }
    if let Some(f) = test_filter {
        cmd.arg(f);
    }
    if no_capture {
        cmd.arg("--").arg("--nocapture");
    }
    cmd.current_dir(workspace_root);
    let out = run_and_format_try(cmd, max_output_len, "cargo nextest run", "cargo_nextest")?;
    if out.contains("no such command: `nextest`") {
        return Err(ToolError::invalid_args(
            "cargo nextest: 未安装 cargo-nextest,请先运行 `cargo install cargo-nextest`"
                .to_string(),
        ));
    }
    Ok(out)
}

pub fn cargo_outdated(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_outdated_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_outdated_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let workspace = v
        .get("workspace")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let depth = v.get("depth").and_then(|x| x.as_u64());

    let mut cmd = Command::new("cargo");
    cmd.arg("outdated");
    if workspace {
        cmd.arg("--workspace");
    }
    if let Some(d) = depth {
        cmd.arg("--depth").arg(d.to_string());
    }
    cmd.current_dir(workspace_root);
    let out = run_and_format_try(cmd, max_output_len, "cargo outdated", "cargo_outdated")?;
    if out.contains("no such command: `outdated`") || out.contains("no such command: outdated") {
        return Err(ToolError::invalid_args(
            "cargo outdated: 未安装 cargo-outdated,请先运行 `cargo install cargo-outdated`"
                .to_string(),
        ));
    }
    Ok(out)
}

/// 运行 **cargo machete**(需已安装 `cargo-machete`):启发式查找 **Cargo.toml 中声明但未在源码中引用** 的依赖;与 `cargo_outdated`(版本是否可升级)互补。误报可用 `with_metadata` 或 `package.metadata.cargo-machete` 缓解。
pub fn cargo_machete(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_machete_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_machete_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let with_metadata = v
        .get("with_metadata")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let path_rel = v.get("path").and_then(|x| x.as_str()).map(str::trim);

    if let Some(p) = path_rel
        && (p.is_empty() || p.contains(".."))
    {
        return Err(ToolError::invalid_args(
            "错误:path 无效(不可为空或含 ..)".to_string(),
        ));
    }

    let mut cmd = Command::new("cargo");
    cmd.arg("machete");
    if with_metadata {
        cmd.arg("--with-metadata");
    }
    if let Some(p) = path_rel.filter(|s| !s.is_empty()) {
        let root_canon = workspace_root
            .canonicalize()
            .unwrap_or_else(|_| workspace_root.to_path_buf());
        let joined = workspace_root.join(p);
        match joined.canonicalize() {
            Ok(abs) => {
                if !abs.starts_with(&root_canon) {
                    return Err(ToolError::invalid_args(
                        "错误:path 必须位于工作区内".to_string(),
                    ));
                }
                cmd.arg(abs);
            }
            Err(e) => {
                return Err(ToolError::invalid_args(format!(
                    "错误:无法解析 path({}",
                    e
                )));
            }
        }
    }
    cmd.current_dir(workspace_root);
    let out = run_and_format_try(cmd, max_output_len, "cargo machete", "cargo_machete")?;
    if out.contains("no such command: `machete`") || out.contains("no such command: machete") {
        return Err(ToolError::invalid_args(
            "cargo machete: 未安装 cargo-machete,请先运行 `cargo install cargo-machete`"
                .to_string(),
        ));
    }
    Ok(out)
}

/// 运行 **cargo udeps**(需已安装 `cargo-udeps`):基于构建信息查找未使用依赖,通常比 machete 更准但更重;**官方文档要求 nightly 工具链**,可用参数 `nightly: true` 调用 `cargo +nightly udeps`。
pub fn cargo_udeps(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_udeps_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_udeps_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }
    let nightly = v.get("nightly").and_then(|x| x.as_bool()).unwrap_or(false);

    let mut cmd = Command::new("cargo");
    if nightly {
        cmd.arg("+nightly");
    }
    cmd.arg("udeps");
    cmd.current_dir(workspace_root);
    let out = run_and_format_try(cmd, max_output_len, "cargo udeps", "cargo_udeps")?;
    if out.contains("no such command: `udeps`") || out.contains("no such command: udeps") {
        return Err(ToolError::invalid_args(
            "cargo udeps: 未安装 cargo-udeps,请先运行 `cargo install cargo-udeps`(运行期通常需 nightly,可传 nightly: true)"
                .to_string(),
        ));
    }
    Ok(out)
}

/// `cargo publish --dry-run`:仅验证打包与发布检查,**不会**上传 registry。
pub fn cargo_publish_dry_run(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> String {
    cargo_publish_dry_run_try(args_json, workspace_root, max_output_len)
        .unwrap_or_else(|e| e.message)
}

pub fn cargo_publish_dry_run_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }

    let package = v
        .get("package")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let allow_dirty = v
        .get("allow_dirty")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let no_verify = v
        .get("no_verify")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let features = v
        .get("features")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let all_features = v
        .get("all_features")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);

    let mut cmd = Command::new("cargo");
    cmd.arg("publish").arg("--dry-run");
    if let Some(p) = package {
        cmd.arg("--package").arg(p);
    }
    if allow_dirty {
        cmd.arg("--allow-dirty");
    }
    if no_verify {
        cmd.arg("--no-verify");
    }
    if let Some(f) = features {
        cmd.arg("--features").arg(f);
    }
    if all_features {
        cmd.arg("--all-features");
    }
    cmd.current_dir(workspace_root);
    run_and_format_try(
        cmd,
        max_output_len,
        "cargo publish --dry-run",
        "cargo_publish_dry_run",
    )
}

pub fn cargo_fix(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    cargo_fix_try(args_json, workspace_root, max_output_len).unwrap_or_else(|e| e.message)
}

pub fn cargo_fix_try(
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;

    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }

    let confirm = v.get("confirm").and_then(|x| x.as_bool()).unwrap_or(false);
    if !confirm {
        return Err(ToolError::invalid_args(
            "拒绝执行:cargo_fix 需要 confirm=true 才会真正应用修复(避免误改代码)。".to_string(),
        ));
    }

    let broken_code = v
        .get("broken_code")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let all_targets = v
        .get("all_targets")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let package = v
        .get("package")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let features = v
        .get("features")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let all_features = v
        .get("all_features")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let edition = v
        .get("edition")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let edition_idioms = v
        .get("edition_idioms")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let allow_dirty = v
        .get("allow_dirty")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let allow_staged = v
        .get("allow_staged")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let allow_no_vcs = v
        .get("allow_no_vcs")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);

    let mut cmd = Command::new("cargo");
    cmd.arg("fix");

    if broken_code {
        cmd.arg("--broken-code");
    }
    if all_targets {
        cmd.arg("--all-targets");
    }
    if let Some(p) = package {
        cmd.arg("--package").arg(p);
    }
    if let Some(f) = features {
        cmd.arg("--features").arg(f);
    }
    if all_features {
        cmd.arg("--all-features");
    }
    if let Some(e) = edition {
        cmd.arg("--edition").arg(e);
    }
    if edition_idioms {
        cmd.arg("--edition-idioms");
    }
    if allow_dirty {
        cmd.arg("--allow-dirty");
    }
    if allow_staged {
        cmd.arg("--allow-staged");
    }
    if allow_no_vcs {
        cmd.arg("--allow-no-vcs");
    }

    cmd.current_dir(workspace_root);
    run_and_format_try(cmd, max_output_len, "cargo fix", "cargo_fix")
}

struct CargoSubCmdOpts<'a> {
    release: bool,
    all_targets: bool,
    package: Option<&'a str>,
    bin: Option<&'a str>,
    features: Option<&'a str>,
    test_filter: Option<&'a str>,
    no_capture: bool,
    run_args: Vec<serde_json::Value>,
}

fn parse_cargo_subcmd_opts(v: &serde_json::Value) -> Result<CargoSubCmdOpts<'_>, ToolError> {
    let package = v.get("package").and_then(|x| x.as_str()).map(str::trim);
    let bin = v.get("bin").and_then(|x| x.as_str()).map(str::trim);
    if let Some(p) = package
        && (p.is_empty() || p.contains(char::is_whitespace))
    {
        return Err(ToolError::invalid_args(
            "错误:package 参数无效".to_string(),
        ));
    }
    if let Some(b) = bin
        && (b.is_empty() || b.contains(char::is_whitespace))
    {
        return Err(ToolError::invalid_args("错误:bin 参数无效".to_string()));
    }
    Ok(CargoSubCmdOpts {
        release: v.get("release").and_then(|x| x.as_bool()).unwrap_or(false),
        all_targets: v
            .get("all_targets")
            .and_then(|x| x.as_bool())
            .unwrap_or(false),
        package,
        bin,
        features: v
            .get("features")
            .and_then(|x| x.as_str())
            .map(str::trim)
            .filter(|s| !s.is_empty()),
        test_filter: v
            .get("test_filter")
            .and_then(|x| x.as_str())
            .map(str::trim)
            .filter(|s| !s.is_empty()),
        no_capture: v
            .get("nocapture")
            .and_then(|x| x.as_bool())
            .unwrap_or(false),
        run_args: v
            .get("args")
            .and_then(|x| x.as_array())
            .cloned()
            .unwrap_or_default(),
    })
}

fn push_cargo_subcmd_cli(cmd: &mut Command, subcmd: &str, o: &CargoSubCmdOpts<'_>) {
    cmd.arg(subcmd);
    if o.release {
        cmd.arg("--release");
    }
    if o.all_targets && matches!(subcmd, "check" | "clippy") {
        cmd.arg("--all-targets");
    }
    if let Some(p) = o.package {
        cmd.arg("--package").arg(p);
    }
    if let Some(b) = o.bin {
        cmd.arg("--bin").arg(b);
    }
    if let Some(f) = o.features {
        cmd.arg("--features").arg(f);
    }
    if subcmd == "test" {
        if let Some(filter) = o.test_filter {
            cmd.arg(filter);
        }
        if o.no_capture {
            cmd.arg("--").arg("--nocapture");
        }
    } else if subcmd == "run" && !o.run_args.is_empty() {
        cmd.arg("--");
        for a in &o.run_args {
            if let Some(s) = a.as_str() {
                cmd.arg(s);
            }
        }
    }
}

fn run_cargo_subcommand_str_try(
    subcmd: &str,
    args_json: &str,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    let v = crate::cm_tools::tools::parse_args_json(args_json).map_err(ToolError::invalid_args)?;
    run_cargo_subcommand_value_try(subcmd, &v, workspace_root, max_output_len)
}

fn run_cargo_subcommand_value_try(
    subcmd: &str,
    v: &serde_json::Value,
    workspace_root: &Path,
    max_output_len: usize,
) -> Result<String, ToolError> {
    if !workspace_root.join("Cargo.toml").is_file() {
        return Err(ToolError::workspace(
            "workspace_no_cargo_toml",
            "错误:当前工作目录未找到 Cargo.toml".to_string(),
        ));
    }

    let o = parse_cargo_subcmd_opts(v)?;
    let mut cmd = Command::new("cargo");
    push_cargo_subcmd_cli(&mut cmd, subcmd, &o);
    cmd.current_dir(workspace_root);
    let tool_code = format!("cargo_{}", subcmd);
    run_and_format_try(
        cmd,
        max_output_len,
        &format!("cargo {}", subcmd),
        &tool_code,
    )
}

fn run_and_format_try(
    mut cmd: Command,
    max_output_len: usize,
    title: &str,
    tool_code: &str,
) -> Result<String, ToolError> {
    match cmd.output() {
        Ok(output) => {
            let exit = output.status.code().unwrap_or(-1);
            let body = output_util::merge_process_output(
                &output,
                output_util::ProcessOutputMerge::ConcatStdoutStderr,
            );
            let message = output_util::format_exited_command_output(
                title,
                exit,
                &body,
                max_output_len,
                MAX_OUTPUT_LINES,
            );
            if output.status.success() {
                Ok(message)
            } else {
                Err(ToolError::cargo_subcommand_failed(tool_code, exit, message))
            }
        }
        Err(e) => Err(ToolError::subprocess_spawn_error(title, e)),
    }
}