forge-engine 0.2.0

Causal edit attribution and structured patch evaluation engine
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
//! C. StructuredPatch validation tests (C1–C6)
//! D. StructuredPatch apply tests (D1–D7)
//! E. Diff rendering tests (E1–E4)

use std::path::PathBuf;

use forge_engine::*;
use tempfile::TempDir;
use uuid::Uuid;

fn default_config() -> ForgeConfig {
    ForgeConfig::default()
}

fn make_patch(edits: Vec<FileEdit>) -> StructuredPatch {
    StructuredPatch {
        patch_id: Uuid::new_v4(),
        summary: "test patch".to_string(),
        edits,
        notes: vec![],
    }
}

// === C. Validation tests ===

/// C1: patch_rejects_forbidden_path_tests
#[test]
fn c1_patch_rejects_forbidden_path_tests() {
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("tests/my_test.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["// test".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = validate_patch(&patch, &default_config());
    assert!(!result.ok);
    assert!(result
        .violations
        .iter()
        .any(|v| v.kind == ViolationKind::ForbiddenPath));
}

/// C2: patch_rejects_forbidden_path_snap
#[test]
fn c2_patch_rejects_forbidden_path_snap() {
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/snapshots/foo.snap"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["snapshot".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = validate_patch(&patch, &default_config());
    assert!(!result.ok);
    assert!(result
        .violations
        .iter()
        .any(|v| v.kind == ViolationKind::ForbiddenPath));
}

/// C3: patch_rejects_cap_files
#[test]
fn c3_patch_rejects_cap_files() {
    let edits: Vec<FileEdit> = (0..9)
        .map(|i| FileEdit {
            path: PathBuf::from(format!("src/file_{i}.rs")),
            ops: vec![EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 1,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines: vec!["// change".to_string()],
            }],
            mode: Some(FileMode::Modify),
        })
        .collect();

    let patch = make_patch(edits);
    let result = validate_patch(&patch, &default_config());
    assert!(!result.ok);
    assert!(result
        .violations
        .iter()
        .any(|v| v.kind == ViolationKind::CapExceeded));
}

/// C4: patch_rejects_cap_total_lines
#[test]
fn c4_patch_rejects_cap_total_lines() {
    // Create a patch with 401 total lines changed
    let lines: Vec<String> = (0..401).map(|i| format!("line {i}")).collect();
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/big.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines,
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = validate_patch(&patch, &default_config());
    assert!(!result.ok);
    assert!(result
        .violations
        .iter()
        .any(|v| { v.kind == ViolationKind::CapExceeded && v.message.contains("total lines") }));
}

/// C5: patch_accepts_well_formed
#[test]
fn c5_patch_accepts_well_formed() {
    let patch = make_patch(vec![
        FileEdit {
            path: PathBuf::from("src/lib.rs"),
            ops: vec![EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 1,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines: vec!["// comment".to_string()],
            }],
            mode: Some(FileMode::Modify),
        },
        FileEdit {
            path: PathBuf::from("src/main.rs"),
            ops: vec![EditOp::Replace {
                range: LineRange {
                    start: 1,
                    end_exclusive: 2,
                },
                lines: vec!["fn main() {}".to_string()],
            }],
            mode: Some(FileMode::Modify),
        },
    ]);

    let result = validate_patch(&patch, &default_config());
    assert!(
        result.ok,
        "Expected valid patch, got: {:?}",
        result.violations
    );
}

/// C6: patch_validation_returns_all_violations (not fail-fast)
#[test]
fn c6_patch_validation_returns_all_violations() {
    // Patch with 3 violations: forbidden path, cap exceeded, degenerate range
    let lines: Vec<String> = (0..201).map(|i| format!("line {i}")).collect();
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("tests/bad.rs"),
        ops: vec![
            EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 1,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines,
            },
            EditOp::Delete {
                range: LineRange {
                    start: 5,
                    end_exclusive: 3, // degenerate!
                },
            },
        ],
        mode: Some(FileMode::Modify),
    }]);

    let result = validate_patch(&patch, &default_config());
    assert!(!result.ok);
    // Should have at least 3 violations (forbidden path, per-file cap, degenerate range)
    assert!(
        result.violations.len() >= 3,
        "Expected at least 3 violations, got {}: {:?}",
        result.violations.len(),
        result.violations
    );
}

/// C7: patch_rejects_path_traversal
/// Patch with path containing ".." or absolute path → must fail with ForbiddenPath.
#[test]
fn c7_patch_rejects_path_traversal() {
    // Test 1: path with ".." component
    let patch_dotdot = make_patch(vec![FileEdit {
        path: PathBuf::from("../../../etc/passwd"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["pwned".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);
    let result = validate_patch(&patch_dotdot, &default_config());
    assert!(!result.ok);
    assert!(
        result
            .violations
            .iter()
            .any(|v| v.kind == ViolationKind::ForbiddenPath && v.message.contains("..")),
        "Expected ForbiddenPath for '..' path: {:?}",
        result.violations
    );

    // Test 2: absolute path
    let patch_abs = make_patch(vec![FileEdit {
        path: PathBuf::from("/etc/shadow"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["pwned".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);
    let result = validate_patch(&patch_abs, &default_config());
    assert!(!result.ok);
    assert!(
        result
            .violations
            .iter()
            .any(|v| v.kind == ViolationKind::ForbiddenPath && v.message.contains("absolute")),
        "Expected ForbiddenPath for absolute path: {:?}",
        result.violations
    );

    // Test 3: duplicate file paths in same patch
    let patch_dup = make_patch(vec![
        FileEdit {
            path: PathBuf::from("src/lib.rs"),
            ops: vec![EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 1,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines: vec!["// change 1".to_string()],
            }],
            mode: Some(FileMode::Modify),
        },
        FileEdit {
            path: PathBuf::from("src/lib.rs"),
            ops: vec![EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 2,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines: vec!["// change 2".to_string()],
            }],
            mode: Some(FileMode::Modify),
        },
    ]);
    let result = validate_patch(&patch_dup, &default_config());
    assert!(!result.ok);
    assert!(
        result
            .violations
            .iter()
            .any(|v| v.kind == ViolationKind::DuplicatePath),
        "Expected DuplicatePath violation: {:?}",
        result.violations
    );
}

// === D. Apply tests ===

fn setup_workspace(files: &[(&str, &str)]) -> TempDir {
    let dir = TempDir::new().unwrap();
    for (name, content) in files {
        let path = dir.path().join(name);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(path, content).unwrap();
    }
    dir
}

/// D1: apply_insert_after_line
#[test]
fn d1_apply_insert_after_line() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\nline 3\nline 4\n")]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 3,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["inserted line A".to_string(), "inserted line B".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_ok(), "Apply failed: {:?}", result.err());

    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    let lines: Vec<&str> = content.lines().collect();
    assert_eq!(lines[0], "line 1");
    assert_eq!(lines[1], "line 2");
    assert_eq!(lines[2], "line 3");
    assert_eq!(lines[3], "inserted line A");
    assert_eq!(lines[4], "inserted line B");
    assert_eq!(lines[5], "line 4");
}

/// D2: apply_replace_range
#[test]
fn d2_apply_replace_range() {
    let ws = setup_workspace(&[(
        "src/lib.rs",
        "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\n",
    )]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Replace {
            range: LineRange {
                start: 5,
                end_exclusive: 8,
            },
            lines: vec!["replaced A".to_string(), "replaced B".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_ok(), "Apply failed: {:?}", result.err());

    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    let lines: Vec<&str> = content.lines().collect();
    assert_eq!(lines[0], "line 1");
    assert_eq!(lines[3], "line 4");
    assert_eq!(lines[4], "replaced A");
    assert_eq!(lines[5], "replaced B");
    assert_eq!(lines[6], "line 8");
}

/// D3: apply_delete_range
#[test]
fn d3_apply_delete_range() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\nline 3\nline 4\nline 5\n")]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Delete {
            range: LineRange {
                start: 2,
                end_exclusive: 4,
            },
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_ok(), "Apply failed: {:?}", result.err());

    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    let lines: Vec<&str> = content.lines().collect();
    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "line 1");
    assert_eq!(lines[1], "line 4");
    assert_eq!(lines[2], "line 5");
}

/// D4: apply_match_anchor
#[test]
fn d4_apply_match_anchor() {
    let ws = setup_workspace(&[(
        "src/lib.rs",
        "fn compute() {}\nfn helper() {}\nfn compute() {}\nfn done() {}\n",
    )]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterMatch {
                needle: "fn compute".to_string(),
                occurrence: 2,
            },
            lines: vec!["// inserted after 2nd fn compute".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_ok(), "Apply failed: {:?}", result.err());

    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    let lines: Vec<&str> = content.lines().collect();
    // The 2nd "fn compute" is at index 2 (0-indexed), insert after it at index 3
    assert_eq!(lines[3], "// inserted after 2nd fn compute");
}

/// D5: apply_fails_on_ambiguous_context
#[test]
fn d5_apply_fails_on_ambiguous_context() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\nline 3\n")]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 2,
                context_before: vec!["WRONG CONTEXT".to_string()],
                context_after: vec![],
            },
            lines: vec!["should not appear".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_err(), "Expected failure on context mismatch");

    // Workspace should be unchanged
    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "line 1\nline 2\nline 3\n");
}

/// D6: apply_is_atomic
#[test]
fn d6_apply_is_atomic() {
    let ws = setup_workspace(&[
        ("src/lib.rs", "line 1\nline 2\nline 3\n"),
        ("src/other.rs", "other 1\nother 2\n"),
    ]);

    // First edit succeeds, second edit fails (bad anchor on other.rs)
    let patch = make_patch(vec![
        FileEdit {
            path: PathBuf::from("src/lib.rs"),
            ops: vec![EditOp::Insert {
                anchor: Anchor::AfterLine {
                    line: 1,
                    context_before: vec![],
                    context_after: vec![],
                },
                lines: vec!["added".to_string()],
            }],
            mode: Some(FileMode::Modify),
        },
        FileEdit {
            path: PathBuf::from("src/other.rs"),
            ops: vec![EditOp::Delete {
                range: LineRange {
                    start: 10,
                    end_exclusive: 20,
                },
            }],
            mode: Some(FileMode::Modify),
        },
    ]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_err(), "Expected atomic failure");

    // First file should be restored
    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "line 1\nline 2\nline 3\n");
}

/// D7: apply_returns_line_attribution_map
#[test]
fn d7_apply_returns_line_attribution_map() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\nline 3\n")]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["new".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path()).unwrap();
    assert!(result.mappings.contains_key("src/lib.rs"));
    let map = &result.mappings["src/lib.rs"];
    assert!(!map.is_empty());
}

/// D8: apply_rejects_path_traversal
/// Patch with ".." path must fail in apply_patch.
#[test]
fn d8_apply_rejects_path_traversal() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\n")]);

    // Try to apply a patch that escapes the workspace
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("../../../etc/evil.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 0,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["// evil".to_string()],
        }],
        mode: Some(FileMode::Create),
    }]);

    let result = apply_patch(&patch, ws.path());
    assert!(result.is_err(), "apply_patch must reject path with '..'");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains(".."),
        "Error message should mention '..': {err}"
    );

    // Verify workspace is unchanged
    let content = std::fs::read_to_string(ws.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "line 1\nline 2\n");
}

// === E. Diff rendering tests ===

/// E1: diff_render_produces_valid_unified_format
#[tokio::test]
async fn e1_diff_render_produces_valid_unified_format() {
    let original = TempDir::new().unwrap();
    let patched = TempDir::new().unwrap();

    std::fs::write(original.path().join("file.rs"), "line 1\nline 2\n").unwrap();
    std::fs::write(patched.path().join("file.rs"), "line 1\nnew line\nline 2\n").unwrap();

    let diff = render_diff(original.path(), patched.path()).await.unwrap();
    assert!(!diff.is_empty(), "Diff should not be empty");
    // Check for unified diff markers
    assert!(
        diff.contains("---") && diff.contains("+++") && diff.contains("@@"),
        "Diff should be valid unified format: {diff}"
    );
}

/// E2: diff_render_is_stable
#[tokio::test]
async fn e2_diff_render_is_stable() {
    let original = TempDir::new().unwrap();
    let patched = TempDir::new().unwrap();

    std::fs::write(original.path().join("file.rs"), "a\nb\nc\n").unwrap();
    std::fs::write(patched.path().join("file.rs"), "a\nX\nc\n").unwrap();

    let diff1 = render_diff(original.path(), patched.path()).await.unwrap();
    let diff2 = render_diff(original.path(), patched.path()).await.unwrap();
    assert_eq!(diff1, diff2, "Diff should be stable across calls");
}

/// E4: diff_fallback_renders_correctly (test internal diff)
#[tokio::test]
async fn e4_diff_fallback_renders_correctly() {
    // We test the internal diff by using the public render_diff function.
    // Even if git is available, the output should be parseable.
    let original = TempDir::new().unwrap();
    let patched = TempDir::new().unwrap();

    std::fs::write(original.path().join("test.rs"), "fn main() {\n}\n").unwrap();
    std::fs::write(
        patched.path().join("test.rs"),
        "fn main() {\n    println!(\"hello\");\n}\n",
    )
    .unwrap();

    let diff = render_diff(original.path(), patched.path()).await.unwrap();
    assert!(!diff.is_empty());
    // Should contain the added line
    assert!(
        diff.contains("println") || diff.contains("+"),
        "Diff should contain the change"
    );
}

// === Phase 1 regression tests ===

/// Insert after line 1 should NOT shift line 1's mapping.
#[test]
fn insert_after_line_1_does_not_shift_line_1_mapping() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\nline 3\n")]);

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["new A".to_string(), "new B".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path()).unwrap();
    let map = &result.mappings["src/lib.rs"];

    // Original line 1 (mapping.0 == 1) should still map to patched line 1
    let line1_mapping = map.iter().find(|(orig, _)| *orig == 1).unwrap();
    assert_eq!(
        line1_mapping.1, 1,
        "line 1 should NOT be shifted by insert after line 1, got mapped to {}",
        line1_mapping.1
    );

    // Original lines 2 and 3 should be shifted by +2
    let line2_mapping = map.iter().find(|(orig, _)| *orig == 2).unwrap();
    assert_eq!(
        line2_mapping.1, 4,
        "line 2 should shift to 4 (2 + 2 inserted), got {}",
        line2_mapping.1
    );

    let line3_mapping = map.iter().find(|(orig, _)| *orig == 3).unwrap();
    assert_eq!(
        line3_mapping.1, 5,
        "line 3 should shift to 5 (3 + 2 inserted), got {}",
        line3_mapping.1
    );
}

/// Negative offset from cascading deletes should produce an error, not wrap.
#[test]
fn negative_offset_returns_error_not_wrap() {
    let ws = setup_workspace(&[("src/lib.rs", "line 1\nline 2\n")]);

    // Delete lines 1-2 (removes all content), then try to delete line 1 again.
    // The second delete should fail because offset makes the start negative.
    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/lib.rs"),
        ops: vec![
            EditOp::Delete {
                range: LineRange {
                    start: 1,
                    end_exclusive: 3,
                },
            },
            EditOp::Delete {
                range: LineRange {
                    start: 1,
                    end_exclusive: 2,
                },
            },
        ],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, ws.path());
    // Should either succeed gracefully or return an error — never silently corrupt
    // The second delete on an empty file should produce an out-of-bounds error
    assert!(
        result.is_err(),
        "Second delete on empty file should fail: {:?}",
        result
    );
}

// === Phase 2 regression tests ===

/// Patch must reject symlink files in workspace.
#[cfg(unix)]
#[test]
fn patch_rejects_symlink_file_in_workspace() {
    let dir = tempfile::tempdir().unwrap();
    let workspace = dir.path();
    std::fs::create_dir_all(workspace.join("src")).unwrap();
    // Create a real file at the target so the symlink is "valid"
    let target = dir.path().join("outside.rs");
    std::fs::write(&target, "original content\n").unwrap();
    // Create a symlink: workspace/src/link.rs → outside.rs
    std::os::unix::fs::symlink(&target, workspace.join("src/link.rs")).unwrap();

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("src/link.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["// injected".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, workspace);
    assert!(result.is_err(), "Should reject symlink file");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("symlink"),
        "Error should mention symlink: {err}"
    );
}

/// Patch must reject symlink directories in workspace.
#[cfg(unix)]
#[test]
fn patch_rejects_symlink_directory_in_workspace() {
    let dir = tempfile::tempdir().unwrap();
    let workspace = dir.path();
    std::fs::create_dir_all(workspace.join("real_dir")).unwrap();
    // Create a target directory with a file
    let target_dir = dir.path().join("target_dir");
    std::fs::create_dir_all(&target_dir).unwrap();
    std::fs::write(target_dir.join("file.rs"), "content\n").unwrap();
    // Create a symlink directory: workspace/sneaky_dir → target_dir
    std::os::unix::fs::symlink(&target_dir, workspace.join("sneaky_dir")).unwrap();

    let patch = make_patch(vec![FileEdit {
        path: PathBuf::from("sneaky_dir/file.rs"),
        ops: vec![EditOp::Insert {
            anchor: Anchor::AfterLine {
                line: 1,
                context_before: vec![],
                context_after: vec![],
            },
            lines: vec!["// injected".to_string()],
        }],
        mode: Some(FileMode::Modify),
    }]);

    let result = apply_patch(&patch, workspace);
    assert!(result.is_err(), "Should reject symlink directory");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("symlink") || err.contains("escapes workspace"),
        "Error should mention symlink or escape: {err}"
    );
}