xchecker-engine 1.2.0

Core orchestration engine for xchecker phase execution
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
use std::path::PathBuf;

use regex::Regex;

use crate::error::FixupError;
use crate::paths::{SandboxConfig, SandboxError, SandboxPath, SandboxRoot};

use super::model::{DiffHunk, FixupMode, UnifiedDiff};

/// Main fixup parser that handles detection and parsing of fixup plans
///
/// # Security
///
/// `FixupParser` uses `SandboxRoot` to validate all target paths before any file operations.
/// This ensures that:
/// - Paths cannot escape the workspace root via `..` traversal
/// - Absolute paths are rejected
/// - Symlinks are rejected by default (configurable via `SandboxConfig`)
/// - Hardlinks are rejected by default (configurable via `SandboxConfig`)
///
/// All path validation happens through `SandboxRoot::join()` which provides
/// comprehensive security checks before any file I/O.
pub struct FixupParser {
    /// Operating mode (preview or apply)
    pub mode: FixupMode,
    /// Sandboxed root directory for resolving and validating relative paths
    pub sandbox_root: SandboxRoot,
}

impl FixupParser {
    /// Create a new fixup parser with a sandboxed root directory.
    ///
    /// # Arguments
    ///
    /// * `mode` - The operating mode (preview or apply)
    /// * `base_dir` - The base directory to use as a sandbox root
    ///
    /// # Errors
    ///
    /// Returns an error if base directory cannot be used as a sandbox root
    /// (e.g., doesn't exist, isn't a directory, or can't be canonicalized).
    pub fn new(mode: FixupMode, base_dir: PathBuf) -> Result<Self, FixupError> {
        let sandbox_root = SandboxRoot::new(&base_dir, SandboxConfig::default()).map_err(|e| {
            FixupError::CanonicalizationError(format!("Failed to create sandbox root: {e}"))
        })?;
        Ok(Self { mode, sandbox_root })
    }

    /// Create a new fixup parser with custom sandbox configuration.
    ///
    /// # Arguments
    ///
    /// * `mode` - The operating mode (preview or apply)
    /// * `base_dir` - The base directory to use as a sandbox root
    /// * `config` - Custom sandbox configuration (e.g., to allow symlinks)
    ///
    /// # Errors
    ///
    /// Returns an error if base directory cannot be used as a sandbox root.
    pub fn with_config(
        mode: FixupMode,
        base_dir: PathBuf,
        config: SandboxConfig,
    ) -> Result<Self, FixupError> {
        let sandbox_root = SandboxRoot::new(&base_dir, config).map_err(|e| {
            FixupError::CanonicalizationError(format!("Failed to create sandbox root: {e}"))
        })?;
        Ok(Self { mode, sandbox_root })
    }

    /// Get the sandbox root path.
    #[must_use]
    pub fn base_dir(&self) -> &std::path::Path {
        self.sandbox_root.as_path()
    }

    /// Validate and resolve a target path within the sandbox.
    ///
    /// This method uses `SandboxRoot::join()` to validate the path, ensuring:
    /// - The path is relative (not absolute)
    /// - The path doesn't contain `..` traversal components
    /// - The path doesn't escape the sandbox root
    /// - The path isn't a symlink (unless configured to allow)
    /// - The path isn't a hardlink (unless configured to allow)
    ///
    /// # Arguments
    ///
    /// * `target_file` - The relative path to validate
    ///
    /// # Returns
    ///
    /// A `SandboxPath` that is guaranteed to be within the sandbox root.
    pub(super) fn validate_target_path(
        &self,
        target_file: &str,
    ) -> Result<SandboxPath, FixupError> {
        self.sandbox_root.join(target_file).map_err(|e| match e {
            SandboxError::AbsolutePath { path } => FixupError::AbsolutePath(PathBuf::from(path)),
            SandboxError::ParentTraversal { path } => {
                FixupError::ParentDirEscape(PathBuf::from(path))
            }
            SandboxError::EscapeAttempt { path, .. } => {
                FixupError::OutsideRepo(PathBuf::from(path))
            }
            SandboxError::SymlinkNotAllowed { path } => {
                FixupError::SymlinkNotAllowed(PathBuf::from(path))
            }
            SandboxError::HardlinkNotAllowed { path } => {
                FixupError::HardlinkNotAllowed(PathBuf::from(path))
            }
            SandboxError::RootNotFound { path } | SandboxError::RootNotDirectory { path } => {
                FixupError::CanonicalizationError(format!("Invalid sandbox root: {path}"))
            }
            SandboxError::RootCanonicalizationFailed { path, reason } => {
                FixupError::CanonicalizationError(format!(
                    "Failed to canonicalize {path}: {reason}"
                ))
            }
            SandboxError::PathCanonicalizationFailed { path, reason } => {
                FixupError::CanonicalizationError(format!(
                    "Failed to canonicalize {path}: {reason}"
                ))
            }
        })
    }

    /// Detect if review output contains fixup markers.
    #[must_use]
    pub fn has_fixup_markers(&self, content: &str) -> bool {
        self.detect_fixup_markers(content).is_some()
    }

    /// Detect fixup markers in review output.
    /// Returns the content after the first marker if found.
    #[must_use]
    pub fn detect_fixup_markers(&self, content: &str) -> Option<String> {
        // Look for "FIXUP PLAN:" or "needs fixups" markers
        let fixup_plan_regex = Regex::new(r"(?i)FIXUP PLAN:").unwrap();
        let needs_fixups_regex = Regex::new(r"(?i)needs fixups").unwrap();

        if let Some(mat) = fixup_plan_regex.find(content) {
            return Some(content[mat.end()..].to_string());
        }

        if let Some(mat) = needs_fixups_regex.find(content) {
            return Some(content[mat.end()..].to_string());
        }

        None
    }

    /// Parse unified diff blocks from fixup content.
    pub fn parse_diffs(&self, content: &str) -> Result<Vec<UnifiedDiff>, FixupError> {
        let fixup_content = self
            .detect_fixup_markers(content)
            .ok_or(FixupError::NoFixupMarkersFound)?;

        let diffs = self.extract_diff_blocks(&fixup_content)?;

        if diffs.is_empty() {
            return Err(FixupError::NoValidDiffBlocks);
        }

        Ok(diffs)
    }

    /// Extract diff blocks from fenced code blocks.
    fn extract_diff_blocks(&self, content: &str) -> Result<Vec<UnifiedDiff>, FixupError> {
        let mut diffs = Vec::new();

        // Regex to match fenced diff blocks: ```diff ... ```
        // Use (?s) flag to make . match newlines
        let diff_block_regex = Regex::new(r"(?s)```diff\n(.*?)\n```").unwrap();

        for (block_index, captures) in diff_block_regex.captures_iter(content).enumerate() {
            let diff_content = captures
                .get(1)
                .ok_or_else(|| FixupError::InvalidDiffFormat {
                    block_index,
                    reason: "No diff content found in block".to_string(),
                })?
                .as_str();

            match self.parse_unified_diff(diff_content, block_index) {
                Ok(diff) => diffs.push(diff),
                Err(e) => {
                    // Log error but continue processing other blocks
                    tracing::warn!("Failed to parse diff block {block_index}: {e}");
                }
            }
        }

        Ok(diffs)
    }

    /// Parse a single unified diff block.
    fn parse_unified_diff(
        &self,
        diff_content: &str,
        block_index: usize,
    ) -> Result<UnifiedDiff, FixupError> {
        let lines: Vec<&str> = diff_content.lines().collect();

        if lines.is_empty() {
            return Err(FixupError::InvalidDiffFormat {
                block_index,
                reason: "Empty diff block".to_string(),
            });
        }

        // Find --- and +++ headers
        let mut old_file = None;
        let mut new_file = None;
        let mut header_end = 0;

        for (i, line) in lines.iter().enumerate() {
            if let Some(rest) = line.strip_prefix("--- ") {
                old_file = Some(rest.trim());
            } else if let Some(rest) = line.strip_prefix("+++ ") {
                new_file = Some(rest.trim());
                header_end = i + 1;
                break;
            }
        }

        let target_file = new_file
            .or(old_file)
            .ok_or_else(|| FixupError::InvalidDiffFormat {
                block_index,
                reason: "No --- or +++ headers found".to_string(),
            })?;

        // Remove a/ and b/ prefixes if present (common in git diffs)
        let target_file = if target_file.starts_with("a/") || target_file.starts_with("b/") {
            &target_file[2..]
        } else {
            target_file
        };

        // Parse hunks
        let hunks = self.parse_hunks(&lines[header_end..], block_index)?;

        Ok(UnifiedDiff {
            path: target_file.to_string(),
            target_file: target_file.to_string(),
            diff_content: diff_content.to_string(),
            hunks,
        })
    }

    /// Parse hunks from diff lines.
    fn parse_hunks(&self, lines: &[&str], block_index: usize) -> Result<Vec<DiffHunk>, FixupError> {
        let mut hunks = Vec::new();
        let mut current_hunk_lines: Vec<String> = Vec::new();
        let mut current_hunk_header: Option<((usize, usize), (usize, usize))> = None;

        // Regex to match hunk headers: @@ -old_start,old_count +new_start,new_count @@
        // Note: Optional count groups must come after their respective start numbers
        let hunk_header_regex = Regex::new(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@").unwrap();

        for line in lines {
            if let Some(captures) = hunk_header_regex.captures(line) {
                // Save previous hunk if exists
                if let Some((old_range, new_range)) = current_hunk_header {
                    hunks.push(DiffHunk {
                        start: old_range.0,
                        remove_count: old_range.1,
                        add_count: new_range.1,
                        remove_lines: current_hunk_lines
                            .iter()
                            .filter(|line| line.starts_with('-'))
                            .map(|line| line[1..].to_string())
                            .collect(),
                        add_lines: current_hunk_lines
                            .iter()
                            .filter(|line| line.starts_with('+'))
                            .map(|line| line[1..].to_string())
                            .collect(),
                        old_range,
                        new_range,
                        content: current_hunk_lines.join("\n"),
                    });
                }

                // Parse new hunk header
                let old_start: usize = captures.get(1).unwrap().as_str().parse().map_err(|_| {
                    FixupError::InvalidDiffFormat {
                        block_index,
                        reason: "Invalid old start line number".to_string(),
                    }
                })?;

                let old_count: usize = captures
                    .get(2)
                    .map_or(1, |m| m.as_str().parse().unwrap_or(1));

                let new_start: usize = captures.get(3).unwrap().as_str().parse().map_err(|_| {
                    FixupError::InvalidDiffFormat {
                        block_index,
                        reason: "Invalid new start line number".to_string(),
                    }
                })?;

                let new_count: usize = captures
                    .get(4)
                    .map_or(1, |m| m.as_str().parse().unwrap_or(1));

                current_hunk_header = Some(((old_start, old_count), (new_start, new_count)));
                current_hunk_lines = vec![(*line).to_string()];
            } else {
                // Add line to current hunk
                current_hunk_lines.push((*line).to_string());
            }
        }

        // Save last hunk if exists
        if let Some((old_range, new_range)) = current_hunk_header {
            hunks.push(DiffHunk {
                start: old_range.0,
                remove_count: old_range.1,
                add_count: new_range.1,
                remove_lines: current_hunk_lines
                    .iter()
                    .filter(|line| line.starts_with('-'))
                    .map(|line| line[1..].to_string())
                    .collect(),
                add_lines: current_hunk_lines
                    .iter()
                    .filter(|line| line.starts_with('+'))
                    .map(|line| line[1..].to_string())
                    .collect(),
                old_range,
                new_range,
                content: current_hunk_lines.join("\n"),
            });
        }

        Ok(hunks)
    }
}

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

    #[test]
    fn test_detect_fixup_markers() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        // Test FIXUP PLAN: marker
        let content1 = "Some review content\nFIXUP PLAN:\nHere are the fixes needed...";
        assert!(parser.has_fixup_markers(content1));

        // Test needs fixups marker
        let content2 = "The review shows that this needs fixups in several areas...";
        assert!(parser.has_fixup_markers(content2));

        // Test no markers
        let content3 = "This is a clean review with no issues found.";
        assert!(!parser.has_fixup_markers(content3));
    }

    #[test]
    fn test_parse_simple_diff() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:
The following changes are needed:

```diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
fn main() {
+    println!("Hello, world!");
     // TODO: implement
}
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].target_file, "src/main.rs");
        assert_eq!(diffs[0].hunks.len(), 1);
    }

    #[test]
    fn test_parse_multiple_hunks() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:
Multiple changes needed:

```diff
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
pub fn foo() {
+    println!("Starting foo");
     // implementation
}
@@ -10,2 +11,3 @@
pub fn bar() {
+    println!("Starting bar");
     // implementation
}
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].target_file, "src/lib.rs");
        assert_eq!(diffs[0].hunks.len(), 2);

        // Verify first hunk
        let hunk1 = &diffs[0].hunks[0];
        assert_eq!(hunk1.start, 1);
        assert_eq!(hunk1.remove_count, 3);
        assert_eq!(hunk1.add_count, 4);

        // Verify second hunk
        let hunk2 = &diffs[0].hunks[1];
        assert_eq!(hunk2.start, 10);
        assert_eq!(hunk2.remove_count, 2);
        assert_eq!(hunk2.add_count, 3);

        // Verify second hunk
        let hunk2 = &diffs[0].hunks[1];
        assert_eq!(hunk2.start, 10);
        assert_eq!(hunk2.remove_count, 2);
        assert_eq!(hunk2.add_count, 3);
    }

    #[test]
    fn test_parse_multiple_diffs() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:
Changes needed in multiple files:

```diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,2 +1,3 @@
fn main() {
+    println!("Hello");
}
```

```diff
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,2 +1,3 @@
pub fn test() {
+    println!("Test");
}
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 2);
        assert_eq!(diffs[0].target_file, "src/main.rs");
        assert_eq!(diffs[0].hunks.len(), 1);
        assert_eq!(diffs[1].target_file, "src/lib.rs");
        assert_eq!(diffs[1].hunks.len(), 1);
    }

    #[test]
    fn test_parse_diff_without_git_prefix() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:

```diff
--- src/main.rs
+++ src/main.rs
@@ -1,2 +1,3 @@
fn main() {
+    println!("Hello");
}
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].target_file, "src/main.rs");
        assert_eq!(diffs[0].hunks.len(), 1);
    }

    #[test]
    fn test_hunk_range_parsing() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:

```diff
--- a/test.txt
+++ b/test.txt
@@ -5,3 +5,4 @@
line 5
+new line
line 6
line 7
@@ -10 +11,2 @@
line 10
+another new line
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].hunks.len(), 2);

        // First hunk: @@ -5,3 +5,4 @@
        let hunk1 = &diffs[0].hunks[0];
        assert_eq!(hunk1.old_range, (5, 3));
        assert_eq!(hunk1.new_range, (5, 4));

        // Second hunk: @@ -10 +11,2 @@ (implicit count of 1)
        let hunk2 = &diffs[0].hunks[1];
        assert_eq!(hunk2.old_range, (10, 1));
        assert_eq!(hunk2.new_range, (11, 2));

        // Verify first hunk - start should match old_range.0
        let hunk1 = &diffs[0].hunks[0];
        assert_eq!(hunk1.start, 5); // old_range.0
        assert_eq!(hunk1.remove_count, 3);
        assert_eq!(hunk1.add_count, 4);

        // Verify second hunk
        let hunk2 = &diffs[0].hunks[1];
        assert_eq!(hunk2.old_range, (10, 1));
        assert_eq!(hunk2.remove_count, 1); // old_range.1
        assert_eq!(hunk2.new_range, (11, 2));
    }

    #[test]
    fn test_empty_diff_block() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:

```diff
```
"#;

        let result = parser.parse_diffs(content);
        // Should fail with NoValidDiffBlocks since diff block is empty
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), FixupError::NoValidDiffBlocks));
    }

    #[test]
    fn test_malformed_hunk_header() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = r#"
FIXUP PLAN:

```diff
--- a/test.txt
+++ b/test.txt
@@ invalid hunk header @@
some content
```
"#;

        let diffs = parser.parse_diffs(content).unwrap();
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].hunks.len(), 0);
    }

    #[test]
    fn test_no_fixup_markers() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        let content = "This is just regular review content without any fixup markers.";

        let result = parser.parse_diffs(content);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            FixupError::NoFixupMarkersFound
        ));
    }

    #[test]
    fn test_case_insensitive_fixup_markers() {
        let temp_dir = TempDir::new().unwrap();
        let parser = FixupParser::new(FixupMode::Preview, temp_dir.path().to_path_buf()).unwrap();

        // Test various case variations
        let content1 = "fixup plan:\nSome content";
        assert!(parser.has_fixup_markers(content1));

        let content2 = "FIXUP PLAN:\nSome content";
        assert!(parser.has_fixup_markers(content2));

        let content3 = "Fixup Plan:\nSome content";
        assert!(parser.has_fixup_markers(content3));

        let content4 = "needs FIXUPS in several places";
        assert!(parser.has_fixup_markers(content4));
    }
}