crabmate 0.5.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
//! 在工作区内应用 **unified diff**(与 `git diff` / `diff -u` 同类)补丁。
//!
//! 使用 `diffy` crate 纯 Rust 实现,不依赖系统 `patch` 命令。
//!
//! # 安全策略
//!
//! - 仅允许相对路径(禁止绝对路径)
//! - 禁止 `..` 路径穿越
//! - 必须落在工作区根目录下

use crate::cm_tools::tools::write_sse_preview::{
    WORKSPACE_WRITE_DIFF_BUDGET_CHARS, WriteDiffFileState,
    format_tool_output_with_write_diff_preview,
};
use crate::cm_tools::workspace::changelist::WorkspaceChangelist;
use crate::cm_tools::workspace::path::{
    absolutize_relative_under_root, ensure_existing_ancestor_within_root,
};
use std::path::Path;
use std::sync::Arc;

pub fn run_with_changelist(
    args_json: &str,
    workspace_root: &Path,
    changelist: Option<&Arc<WorkspaceChangelist>>,
) -> String {
    let args = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };

    let patch_text = match args.get("patch").and_then(|p| p.as_str()) {
        Some(s) if !s.trim().is_empty() => s,
        _ => return "错误:缺少 patch 参数".to_string(),
    };
    let strip = args
        .get("strip")
        .and_then(|v| v.as_u64())
        .unwrap_or_default() as usize;

    let root = match workspace_root.canonicalize() {
        Ok(p) => p,
        Err(e) => return format!("工作区根目录无法解析: {}", e),
    };

    if let Err(e) = validate_patch_paths(patch_text, &root) {
        return format!("补丁路径校验失败: {}", e);
    }

    apply_unified_patch(patch_text, &root, strip, changelist)
}

fn apply_unified_patch(
    patch_text: &str,
    root: &Path,
    strip: usize,
    changelist: Option<&Arc<WorkspaceChangelist>>,
) -> String {
    if let Err(e) = diffy::Patch::from_str(patch_text) {
        return format!("解析 unified diff 失败: {}", e);
    }

    let mut preview_files: Vec<WriteDiffFileState> = Vec::new();
    let mut applied_files = Vec::new();
    let mut errors = Vec::new();

    for hunk_patch in split_patch_by_file(patch_text) {
        apply_single_unified_hunk(
            &hunk_patch,
            root,
            strip,
            changelist,
            &mut preview_files,
            &mut applied_files,
            &mut errors,
        );
    }

    let outcome = unified_patch_format_outcome(&applied_files, &errors);
    format_tool_output_with_write_diff_preview(
        "apply_patch",
        outcome,
        preview_files,
        WORKSPACE_WRITE_DIFF_BUDGET_CHARS,
    )
}

fn unified_patch_format_outcome(applied_files: &[String], errors: &[String]) -> String {
    if errors.is_empty() {
        if applied_files.is_empty() {
            "补丁应用成功(无文件变更)".to_string()
        } else {
            format!("补丁应用成功:\n{}", applied_files.join("\n"))
        }
    } else if applied_files.is_empty() {
        format!("补丁应用失败:\n{}", errors.join("\n"))
    } else {
        format!(
            "补丁部分应用:\n成功:\n{}\n失败:\n{}",
            applied_files.join("\n"),
            errors.join("\n")
        )
    }
}

enum DevNullHunkKind {
    Create,
    Delete,
}

fn classify_dev_null_hunk(file_path: &str, original_path: &str) -> Option<DevNullHunkKind> {
    if original_path == "/dev/null" {
        Some(DevNullHunkKind::Create)
    } else if file_path == "/dev/null" {
        Some(DevNullHunkKind::Delete)
    } else {
        None
    }
}

fn apply_single_unified_hunk(
    hunk_patch: &str,
    root: &Path,
    strip: usize,
    changelist: Option<&Arc<WorkspaceChangelist>>,
    preview_files: &mut Vec<WriteDiffFileState>,
    applied_files: &mut Vec<String>,
    errors: &mut Vec<String>,
) {
    let (file_path, original_path) = match extract_target_path(hunk_patch, strip) {
        Some(p) => p,
        None => {
            errors.push("无法提取文件路径".to_string());
            return;
        }
    };

    if let Some(kind) = classify_dev_null_hunk(&file_path, &original_path) {
        match kind {
            DevNullHunkKind::Create => unified_hunk_create_new_file(
                hunk_patch,
                root,
                &file_path,
                changelist,
                preview_files,
                applied_files,
                errors,
            ),
            DevNullHunkKind::Delete => unified_hunk_delete_file(
                root,
                &original_path,
                changelist,
                preview_files,
                applied_files,
                errors,
            ),
        }
        return;
    }

    let target = root.join(&file_path);
    let original = match std::fs::read_to_string(&target) {
        Ok(s) => s,
        Err(e) => {
            errors.push(format!("{}: 读取失败: {}", file_path, e));
            return;
        }
    };

    let single = match diffy::Patch::from_str(hunk_patch) {
        Ok(p) => p,
        Err(e) => {
            errors.push(format!("{}: 解析失败: {}", file_path, e));
            return;
        }
    };

    match diffy::apply(&original, &single) {
        Ok(patched) => {
            if let Err(e) = std::fs::write(&target, patched.as_bytes()) {
                errors.push(format!("{}: 写入失败: {}", file_path, e));
            } else {
                if let Some(cl) = changelist {
                    cl.record_mutation(&file_path, Some(original.clone()), Some(patched.clone()));
                }
                preview_files.push(WriteDiffFileState {
                    rel_path: file_path.clone(),
                    before: Some(original),
                    after: Some(patched),
                });
                applied_files.push(file_path);
            }
        }
        Err(e) => errors.push(format!("{}: 应用失败: {}", file_path, e)),
    }
}

fn unified_hunk_create_new_file(
    hunk_patch: &str,
    root: &Path,
    file_path: &str,
    changelist: Option<&Arc<WorkspaceChangelist>>,
    preview_files: &mut Vec<WriteDiffFileState>,
    applied_files: &mut Vec<String>,
    errors: &mut Vec<String>,
) {
    let target = root.join(file_path);
    let single = match diffy::Patch::from_str(hunk_patch) {
        Ok(p) => p,
        Err(e) => {
            errors.push(format!("{}: 解析失败: {}", file_path, e));
            return;
        }
    };
    let new_content = diffy::apply("", &single);
    match new_content {
        Ok(content) => {
            if let Some(parent) = target.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            if let Err(e) = std::fs::write(&target, content.as_bytes()) {
                errors.push(format!("{}: 创建文件失败: {}", file_path, e));
            } else {
                if let Some(cl) = changelist {
                    cl.record_mutation(file_path, None, Some(content.clone()));
                }
                preview_files.push(WriteDiffFileState {
                    rel_path: file_path.to_string(),
                    before: None,
                    after: Some(content),
                });
                applied_files.push(format!("新建: {}", file_path));
            }
        }
        Err(e) => errors.push(format!("{}: 应用失败: {}", file_path, e)),
    }
}

fn unified_hunk_delete_file(
    root: &Path,
    original_path: &str,
    changelist: Option<&Arc<WorkspaceChangelist>>,
    preview_files: &mut Vec<WriteDiffFileState>,
    applied_files: &mut Vec<String>,
    errors: &mut Vec<String>,
) {
    let source = root.join(original_path);
    if !source.exists() {
        return;
    }
    let before = std::fs::read_to_string(&source).ok();
    if let Err(e) = std::fs::remove_file(&source) {
        errors.push(format!("{}: 删除失败: {}", original_path, e));
    } else {
        if let Some(cl) = changelist {
            cl.record_mutation(original_path, before.clone(), None);
        }
        preview_files.push(WriteDiffFileState {
            rel_path: original_path.to_string(),
            before,
            after: None,
        });
        applied_files.push(format!("删除: {}", original_path));
    }
}

fn split_patch_by_file(patch_text: &str) -> Vec<String> {
    let mut chunks = Vec::new();
    let mut current = String::new();
    let mut in_file = false;

    for line in patch_text.lines() {
        if line.starts_with("--- ") {
            if in_file && !current.is_empty() {
                chunks.push(std::mem::take(&mut current));
            }
            in_file = true;
        }
        if in_file {
            current.push_str(line);
            current.push('\n');
        }
    }
    if !current.is_empty() {
        chunks.push(current);
    }
    if chunks.is_empty() {
        chunks.push(patch_text.to_string());
    }
    chunks
}

fn extract_target_path(patch_chunk: &str, strip: usize) -> Option<(String, String)> {
    let mut original = None;
    let mut target = None;
    for line in patch_chunk.lines() {
        if let Some(rest) = line.strip_prefix("--- ") {
            let path = rest.split_whitespace().next()?;
            original = Some(strip_components(path, strip));
        } else if let Some(rest) = line.strip_prefix("+++ ") {
            let path = rest.split_whitespace().next()?;
            target = Some(strip_components(path, strip));
        }
        if original.is_some() && target.is_some() {
            break;
        }
    }
    Some((target?, original?))
}

fn strip_components(path: &str, n: usize) -> String {
    if path == "/dev/null" {
        return path.to_string();
    }
    let parts: Vec<&str> = path.split('/').collect();
    if n >= parts.len() {
        parts.last().unwrap_or(&"").to_string()
    } else {
        parts[n..].join("/")
    }
}

fn validate_patch_paths(patch_text: &str, root: &Path) -> Result<(), String> {
    let mut seen_header = false;
    for line in patch_text.lines() {
        let Some(raw_path) = parse_header_path(line) else {
            continue;
        };
        seen_header = true;
        if raw_path == "/dev/null" {
            continue;
        }
        validate_single_path(raw_path, root)?;
    }
    if !seen_header {
        return Err("未检测到 unified diff 文件头(--- / +++)".to_string());
    }
    Ok(())
}

fn parse_header_path(line: &str) -> Option<&str> {
    let body = line
        .strip_prefix("--- ")
        .or_else(|| line.strip_prefix("+++ "))?;
    body.split_whitespace().next()
}

fn validate_single_path(raw_path: &str, root: &Path) -> Result<(), String> {
    let path_no_prefix = raw_path
        .strip_prefix("a/")
        .or_else(|| raw_path.strip_prefix("b/"))
        .unwrap_or(raw_path);
    let p = Path::new(path_no_prefix);
    if p.is_absolute() {
        return Err(format!("不允许绝对路径: {}", raw_path));
    }
    let normalized = absolutize_relative_under_root(root, path_no_prefix)
        .map_err(|e| format!("路径超出工作区或无效: {} ({})", raw_path, e.user_message()))?;
    ensure_existing_ancestor_within_root(root, &normalized)
        .map_err(|e| format!("路径超出工作区或无效: {} ({})", raw_path, e.user_message()))
}

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

    #[test]
    fn test_parse_header_path() {
        assert_eq!(
            parse_header_path("--- a/src/main.rs"),
            Some("a/src/main.rs")
        );
        assert_eq!(
            parse_header_path("+++ b/src/main.rs\t2026-01-01"),
            Some("b/src/main.rs")
        );
        assert_eq!(parse_header_path("@@ -1,2 +1,2 @@"), None);
    }

    #[test]
    fn test_validate_single_path_rejects_parent() {
        let root = std::env::current_dir().unwrap();
        let err = validate_single_path("../etc/passwd", &root).unwrap_err();
        assert!(
            err.contains("超出") || err.contains("工作目录"),
            "应拒绝越出工作区: {}",
            err
        );
    }

    #[test]
    fn test_validate_patch_paths_ok() {
        let root = std::env::current_dir().unwrap();
        let patch = "\
--- a/src/main.rs
+++ b/src/main.rs
@@ -1 +1 @@
-old
+new
";
        assert!(validate_patch_paths(patch, &root).is_ok());
    }

    #[test]
    fn test_strip_components() {
        assert_eq!(strip_components("a/src/main.rs", 1), "src/main.rs");
        assert_eq!(strip_components("src/main.rs", 0), "src/main.rs");
        assert_eq!(strip_components("/dev/null", 1), "/dev/null");
    }

    #[test]
    fn test_split_patch_by_file() {
        let patch = "\
--- a/foo.rs
+++ b/foo.rs
@@ -1 +1 @@
-old
+new
--- a/bar.rs
+++ b/bar.rs
@@ -1 +1 @@
-x
+y
";
        let chunks = split_patch_by_file(patch);
        assert_eq!(chunks.len(), 2);
        assert!(chunks[0].contains("foo.rs"));
        assert!(chunks[1].contains("bar.rs"));
    }

    #[cfg(unix)]
    #[test]
    fn test_validate_single_path_rejects_symlink_escape() {
        use std::os::unix::fs::symlink;
        use std::time::{SystemTime, UNIX_EPOCH};

        let root = std::env::temp_dir().join(format!(
            "crabmate_patch_tool_test_{}_{}",
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis()
        ));
        let outside = std::env::temp_dir().join(format!(
            "crabmate_patch_outside_{}_{}",
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        ));
        std::fs::create_dir_all(&root).unwrap();
        std::fs::create_dir_all(&outside).unwrap();
        let link = root.join("escape");
        symlink(&outside, &link).unwrap();

        let err = validate_single_path("escape/pwned.txt", &root).unwrap_err();
        assert!(
            err.contains("路径超出工作区"),
            "应拒绝 symlink 绕过: {}",
            err
        );

        let _ = std::fs::remove_dir_all(&root);
        let _ = std::fs::remove_dir_all(&outside);
    }
}