codescout 0.13.0

High-performance coding agent toolkit MCP server
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
//! Regression tests for tool misbehavior bugs (BUG-029, BUG-031, BUG-032).
//!
//! These tests use real LSP servers — they are skipped when the required LSP
//! is not installed. Run with:
//!
//!     cargo test --test bug_regression -- --ignored
//!
//! All tests follow the sandwich pattern:
//! 1. Setup a scenario that triggers the bug
//! 2. Call the tool
//! 3. Assert the output is correct (would have been wrong before the fix)

use codescout::agent::Agent;
use codescout::lsp::LspManager;
use codescout::tools::markdown::EditMarkdown;
use codescout::tools::output_buffer::OutputBuffer;
use codescout::tools::symbol::EditCode;
use codescout::tools::{Tool, ToolContext};
use serde_json::json;
use std::sync::Arc;
use tempfile::tempdir;

/// Create a project context with files pre-populated and a real LSP manager.
async fn project_with_files(files: &[(&str, &str)]) -> (tempfile::TempDir, ToolContext) {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join(".codescout")).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();
    }
    let agent = Agent::new(Some(dir.path().to_path_buf())).await.unwrap();
    let ctx = ToolContext {
        agent,
        lsp: LspManager::new_arc(),
        output_buffer: Arc::new(OutputBuffer::new(20)),
        progress: None,
        peer: None,
        section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
            codescout::tools::section_coverage::SectionCoverage::new(),
        )),
    };
    (dir, ctx)
}

fn lsp_available(cmd: &str) -> bool {
    // Some LSP servers (e.g. pyright-langserver) don't support --version and exit 1.
    // Use `which` to check if the binary exists on PATH instead.
    std::process::Command::new("which")
        .arg(cmd)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

// ===========================================================================
// BUG-031: replace_symbol must not duplicate doc comments
// ===========================================================================

/// Rust: replace a function that has `///` doc comments.
/// Before fix: old doc comments left in place, new body's docs appended → duplication.
#[tokio::test]
#[ignore] // requires rust-analyzer
async fn bug031_replace_symbol_with_doc_comments_rust() {
    if !lsp_available("rust-analyzer") {
        eprintln!("Skipping: rust-analyzer not installed");
        return;
    }

    let src = r#"[package]
name = "test-project"
version = "0.1.0"
edition = "2021"
"#;
    let code = r#"/// Adds two numbers.
/// Returns the sum.
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

/// Subtracts b from a.
pub fn sub(a: i32, b: i32) -> i32 {
    a - b
}
"#;

    let (dir, ctx) = project_with_files(&[("Cargo.toml", src), ("src/lib.rs", code)]).await;

    // Replace `add` with a new implementation (including updated doc comments)
    let new_body = r#"/// Adds two numbers together.
/// Returns their sum.
pub fn add(a: i32, b: i32) -> i32 {
    let result = a + b;
    result
}"#;

    EditCode
        .call(
            json!({
                "path": "src/lib.rs",
                "symbol": "add",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("src/lib.rs")).unwrap();

    // Must not have duplicated doc comments
    assert_eq!(
        result.matches("/// Adds two numbers").count(),
        1,
        "doc comment must appear exactly once (no duplication); got:\n{result}"
    );
    assert!(
        result.contains("let result = a + b"),
        "new body must be present; got:\n{result}"
    );
    assert!(
        !result.contains("a + b\n}"),
        "old body must be gone; got:\n{result}"
    );
    // `sub` must be untouched
    assert!(
        result.contains("pub fn sub"),
        "adjacent function must survive; got:\n{result}"
    );
}

/// Python: replace a function with a docstring.
#[tokio::test]
#[ignore] // requires pyright
async fn bug031_replace_symbol_with_doc_comments_python() {
    if !lsp_available("pyright-langserver") {
        eprintln!("Skipping: pyright-langserver not installed");
        return;
    }

    let code = r#"def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

def sub(a: int, b: int) -> int:
    """Subtract b from a."""
    return a - b
"#;

    let (dir, ctx) = project_with_files(&[("lib.py", code)]).await;

    let new_body = r#"def add(a: int, b: int) -> int:
    """Add two numbers together."""
    result = a + b
    return result"#;

    EditCode
        .call(
            json!({
                "path": "lib.py",
                "symbol": "add",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("lib.py")).unwrap();
    assert_eq!(
        result.matches("def add").count(),
        1,
        "function signature must appear once; got:\n{result}"
    );
    assert!(
        result.contains("result = a + b"),
        "new body; got:\n{result}"
    );
    assert!(
        result.contains("def sub"),
        "adjacent function must survive; got:\n{result}"
    );
}

/// TypeScript: replace a function with JSDoc.
#[tokio::test]
#[ignore] // requires typescript-language-server
async fn bug031_replace_symbol_with_doc_comments_typescript() {
    if !lsp_available("typescript-language-server") {
        eprintln!("Skipping: typescript-language-server not installed");
        return;
    }

    let code = r#"/**
 * Add two numbers.
 * @param a First number
 * @param b Second number
 */
export function add(a: number, b: number): number {
    return a + b;
}

export function sub(a: number, b: number): number {
    return a - b;
}
"#;
    // tsconfig so the LSP doesn't complain
    let tsconfig = r#"{ "compilerOptions": { "strict": true } }"#;

    let (dir, ctx) = project_with_files(&[("src/lib.ts", code), ("tsconfig.json", tsconfig)]).await;

    let new_body = r#"/**
 * Add two numbers together.
 * @param a First number
 * @param b Second number
 */
export function add(a: number, b: number): number {
    const result = a + b;
    return result;
}"#;

    EditCode
        .call(
            json!({
                "path": "src/lib.ts",
                "symbol": "add",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("src/lib.ts")).unwrap();
    assert_eq!(
        result.matches("export function add").count(),
        1,
        "function must appear once; got:\n{result}"
    );
    assert_eq!(
        result.matches("/**").count(),
        1,
        "JSDoc opener must appear once (no duplication); got:\n{result}"
    );
    assert!(
        result.contains("const result = a + b"),
        "new body; got:\n{result}"
    );
    assert!(
        result.contains("export function sub"),
        "adjacent function must survive; got:\n{result}"
    );
}

/// Go: replace a function with a Go doc comment.
#[tokio::test]
#[ignore] // requires gopls
async fn bug031_replace_symbol_with_doc_comments_go() {
    if !lsp_available("gopls") {
        eprintln!("Skipping: gopls not installed");
        return;
    }

    let code = r#"package math

// Add returns the sum of a and b.
func Add(a, b int) int {
	return a + b
}

// Sub returns a minus b.
func Sub(a, b int) int {
	return a - b
}
"#;
    let go_mod = "module example.com/test\n\ngo 1.21\n";

    let (dir, ctx) = project_with_files(&[("math.go", code), ("go.mod", go_mod)]).await;

    let new_body = r#"// Add returns the sum of a and b.
func Add(a, b int) int {
	result := a + b
	return result
}"#;

    EditCode
        .call(
            json!({
                "path": "math.go",
                "symbol": "Add",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("math.go")).unwrap();
    assert_eq!(
        result.matches("func Add").count(),
        1,
        "function must appear once; got:\n{result}"
    );
    assert_eq!(
        result.matches("// Add returns").count(),
        1,
        "doc comment must appear once; got:\n{result}"
    );
    assert!(
        result.contains("result := a + b"),
        "new body; got:\n{result}"
    );
    assert!(
        result.contains("func Sub"),
        "adjacent function must survive; got:\n{result}"
    );
}

// ===========================================================================
// BUG-029: insert_code "after" must place code AFTER the closing brace
// ===========================================================================

/// Rust: insert a new test function after an existing one inside mod tests.
#[tokio::test]
#[ignore] // requires rust-analyzer
async fn bug029_insert_code_after_nested_fn_rust() {
    if !lsp_available("rust-analyzer") {
        eprintln!("Skipping: rust-analyzer not installed");
        return;
    }

    let src = r#"[package]
name = "test-project"
version = "0.1.0"
edition = "2021"
"#;
    let code = r#"pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

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

    #[test]
    fn test_add_positive() {
        assert_eq!(add(2, 3), 5);
    }

    #[test]
    fn test_add_negative() {
        assert_eq!(add(-1, -2), -3);
    }
}
"#;

    let (dir, ctx) = project_with_files(&[("Cargo.toml", src), ("src/lib.rs", code)]).await;

    let new_test = r#"
    #[test]
    fn test_add_zero() {
        assert_eq!(add(0, 0), 0);
    }"#;

    EditCode
        .call(
            json!({
                "path": "src/lib.rs",
                "symbol": "tests/test_add_positive",
                "action": "insert",
                "body": new_test,
                "position": "after"
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("src/lib.rs")).unwrap();

    // The new test must be a sibling, not nested inside test_add_positive
    assert!(
        result.contains("fn test_add_zero"),
        "new test must be present; got:\n{result}"
    );
    // test_add_positive must remain intact — its body must not be split
    assert!(
        result.contains("assert_eq!(add(2, 3), 5);"),
        "test_add_positive body must be intact; got:\n{result}"
    );

    // Verify the file compiles by checking balanced braces
    let opens = result.matches('{').count();
    let closes = result.matches('}').count();
    assert_eq!(
        opens, closes,
        "braces must be balanced (opens={opens}, closes={closes}); got:\n{result}"
    );
}

/// TypeScript: insert a function after another in a module.
#[tokio::test]
#[ignore] // requires typescript-language-server
async fn bug029_insert_code_after_function_typescript() {
    if !lsp_available("typescript-language-server") {
        eprintln!("Skipping: typescript-language-server not installed");
        return;
    }

    let code = r#"export function first(): number {
    const x = 1;
    const y = 2;
    return x + y;
}

export function third(): number {
    return 3;
}
"#;
    let tsconfig = r#"{ "compilerOptions": { "strict": true } }"#;

    let (dir, ctx) = project_with_files(&[("src/lib.ts", code), ("tsconfig.json", tsconfig)]).await;

    let new_fn = r#"
export function second(): number {
    return 2;
}"#;

    EditCode
        .call(
            json!({
                "path": "src/lib.ts",
                "symbol": "first",
                "action": "insert",
                "body": new_fn,
                "position": "after"
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("src/lib.ts")).unwrap();
    assert!(
        result.contains("function second"),
        "new function; got:\n{result}"
    );
    // `first` must be complete — its body must not be split
    assert!(
        result.contains("return x + y;"),
        "first() body intact; got:\n{result}"
    );
    assert!(
        result.contains("function third"),
        "third() must survive; got:\n{result}"
    );
    let opens = result.matches('{').count();
    let closes = result.matches('}').count();
    assert_eq!(opens, closes, "braces balanced; got:\n{result}");
}

// ===========================================================================
// BUG-032: sequential remove_symbol must not corrupt file
// ===========================================================================

/// Rust: remove an enum and its impl block sequentially.
/// Before fix: second removal used stale line numbers, corrupting the next function.
#[tokio::test]
#[ignore] // requires rust-analyzer
async fn bug032_sequential_remove_symbol_rust() {
    if !lsp_available("rust-analyzer") {
        eprintln!("Skipping: rust-analyzer not installed");
        return;
    }

    let src = r#"[package]
name = "test-project"
version = "0.1.0"
edition = "2021"
"#;
    let code = r#"pub enum Filter {
    All,
    Active,
    Inactive,
}

impl Filter {
    pub fn is_all(&self) -> bool {
        matches!(self, Filter::All)
    }
}

pub fn process(items: &[i32]) -> Vec<i32> {
    items.iter().copied().collect()
}
"#;

    let (dir, ctx) = project_with_files(&[("Cargo.toml", src), ("src/lib.rs", code)]).await;

    // First removal: remove the enum
    EditCode
        .call(
            json!({
                "path": "src/lib.rs",
                "symbol": "Filter",
                "action": "remove"
            }),
            &ctx,
        )
        .await
        .unwrap();

    let after_first = std::fs::read_to_string(dir.path().join("src/lib.rs")).unwrap();
    assert!(
        !after_first.contains("pub enum Filter"),
        "enum must be removed; got:\n{after_first}"
    );
    assert!(
        after_first.contains("pub fn process"),
        "process() must survive first removal; got:\n{after_first}"
    );

    // Second removal: remove the impl block
    // This is where BUG-032 would strike — stale line numbers
    let result = EditCode
        .call(
            json!({
                "path": "src/lib.rs",
                "symbol": "impl Filter",
                "action": "remove"
            }),
            &ctx,
        )
        .await;

    match result {
        Ok(_) => {
            let after_second = std::fs::read_to_string(dir.path().join("src/lib.rs")).unwrap();
            assert!(
                !after_second.contains("impl Filter"),
                "impl block must be removed; got:\n{after_second}"
            );
            assert!(
                after_second.contains("pub fn process"),
                "process() must survive both removals; got:\n{after_second}"
            );
            // File must still be valid Rust
            let opens = after_second.matches('{').count();
            let closes = after_second.matches('}').count();
            assert_eq!(
                opens, closes,
                "braces must be balanced; got:\n{after_second}"
            );
        }
        Err(e) => {
            let msg = e.to_string();
            // RecoverableError about stale positions is acceptable — the guard caught it
            assert!(
                msg.contains("stale") || msg.contains("not found"),
                "error should be about stale data or missing symbol, got: {msg}"
            );
        }
    }
}

/// Python: sequential removal of a class and a standalone function.
#[tokio::test]
#[ignore] // requires pyright
async fn bug032_sequential_remove_symbol_python() {
    if !lsp_available("pyright-langserver") {
        eprintln!("Skipping: pyright-langserver not installed");
        return;
    }

    let code = r#"class Filter:
    ALL = "all"
    ACTIVE = "active"

def process(items: list[int]) -> list[int]:
    return list(items)

def helper() -> str:
    return "ok"
"#;

    let (dir, ctx) = project_with_files(&[("lib.py", code)]).await;

    // Remove the class
    EditCode
        .call(json!({ "path": "lib.py", "symbol": "Filter" }), &ctx)
        .await
        .unwrap();

    let after_first = std::fs::read_to_string(dir.path().join("lib.py")).unwrap();
    assert!(
        !after_first.contains("class Filter"),
        "class removed; got:\n{after_first}"
    );
    assert!(
        after_first.contains("def process"),
        "process survives; got:\n{after_first}"
    );

    // Remove process — uses positions that may be stale
    let result = EditCode
        .call(json!({ "path": "lib.py", "symbol": "process" }), &ctx)
        .await;

    match result {
        Ok(_) => {
            let after_second = std::fs::read_to_string(dir.path().join("lib.py")).unwrap();
            assert!(
                !after_second.contains("def process"),
                "process removed; got:\n{after_second}"
            );
            assert!(
                after_second.contains("def helper"),
                "helper survives; got:\n{after_second}"
            );
        }
        Err(e) => {
            let msg = e.to_string();
            assert!(
                msg.contains("stale") || msg.contains("not found"),
                "acceptable error about stale data; got: {msg}"
            );
        }
    }
}

/// BUG-043: `replace` on a heading whose section contains sub-headings used to
/// silently wipe them. Guard rejects unless caller opts in with
/// `include_subsections: true`.
#[tokio::test]
async fn bug043_edit_markdown_replace_rejects_when_section_has_subsections() {
    let plan = "\
# Plan

## File Map
short map body

### Task A
work
### Task B
more work
### Task C
even more
";
    let (dir, ctx) = project_with_files(&[("plan.md", plan)]).await;

    let err = EditMarkdown
        .call(
            json!({
                "path": "plan.md",
                "heading": "## File Map",
                "action": "replace",
                "content": "new short map body\n"
            }),
            &ctx,
        )
        .await
        .unwrap_err();

    let msg = err.to_string();
    assert!(
        msg.contains("### Task A") && msg.contains("### Task B") && msg.contains("### Task C"),
        "error must list the would-be-consumed subsections; got: {msg}"
    );
    assert!(
        msg.contains("include_subsections"),
        "error must point to the opt-in flag; got: {msg}"
    );

    // File must be unchanged on disk.
    let on_disk = std::fs::read_to_string(dir.path().join("plan.md")).unwrap();
    assert_eq!(on_disk, plan, "file must be untouched when guard fires");
}

/// `include_subsections: true` bypasses the BUG-043 guard — user explicitly
/// asked for the consume-children semantics.
#[tokio::test]
async fn bug043_edit_markdown_replace_allows_subsection_consumption_on_opt_in() {
    let plan = "\
# Plan

## File Map
old
### Task A
work
";
    let (dir, ctx) = project_with_files(&[("plan.md", plan)]).await;

    EditMarkdown
        .call(
            json!({
                "path": "plan.md",
                "heading": "## File Map",
                "action": "replace",
                "content": "new body\n",
                "include_subsections": true
            }),
            &ctx,
        )
        .await
        .expect("opt-in must succeed");

    let on_disk = std::fs::read_to_string(dir.path().join("plan.md")).unwrap();
    assert!(on_disk.contains("new body"));
    assert!(
        !on_disk.contains("### Task A"),
        "opt-in truly consumes subsections: {on_disk}"
    );
}

// ===========================================================================
// BUG-044: replace_symbol on a method inside an impl/class must not drop
// sibling methods, even when LSP ranges overshoot.
// ===========================================================================

/// Rust (rust-analyzer): impl block with two sibling methods. Replacing one
/// must leave the other intact.
#[tokio::test]
#[ignore] // requires rust-analyzer
async fn bug044_replace_symbol_preserves_sibling_method_rust() {
    if !lsp_available("rust-analyzer") {
        eprintln!("Skipping: rust-analyzer not installed");
        return;
    }

    let manifest = r#"[package]
name = "test-project"
version = "0.1.0"
edition = "2021"
"#;
    let code = r#"pub struct Foo;

impl Foo {
    pub fn alpha(&self) -> i32 {
        1
    }

    pub fn beta(&self) -> i32 {
        2
    }
}
"#;

    let (dir, ctx) = project_with_files(&[("Cargo.toml", manifest), ("src/lib.rs", code)]).await;

    let new_body = r#"    pub fn alpha(&self) -> i32 {
        99
    }"#;

    EditCode
        .call(
            json!({
                "path": "src/lib.rs",
                "symbol": "impl Foo/alpha",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("src/lib.rs")).unwrap();
    assert!(
        result.contains("99"),
        "alpha must be replaced with new body; got:\n{result}"
    );
    assert!(
        result.contains("pub fn beta"),
        "sibling beta must survive the replacement; got:\n{result}"
    );
    assert!(
        result.contains('2'),
        "beta's body must survive; got:\n{result}"
    );
}

/// Python (pyright): class with two sibling methods. Replacing one must
/// leave the other intact.
#[tokio::test]
#[ignore] // requires pyright-langserver
async fn bug044_replace_symbol_preserves_sibling_method_python() {
    if !lsp_available("pyright-langserver") {
        eprintln!("Skipping: pyright-langserver not installed");
        return;
    }

    let code = "\
class Foo:
    def alpha(self) -> int:
        return 1

    def beta(self) -> int:
        return 2
";

    let (dir, ctx) = project_with_files(&[("main.py", code)]).await;

    let new_body = "    def alpha(self) -> int:\n        return 99";

    EditCode
        .call(
            json!({
                "path": "main.py",
                "symbol": "Foo/alpha",
                "action": "replace",
                "body": new_body
            }),
            &ctx,
        )
        .await
        .unwrap();

    let result = std::fs::read_to_string(dir.path().join("main.py")).unwrap();
    assert!(
        result.contains("return 99"),
        "alpha must be replaced; got:\n{result}"
    );
    assert!(
        result.contains("def beta"),
        "sibling beta must survive; got:\n{result}"
    );
    assert!(
        result.contains("return 2"),
        "beta's body must survive; got:\n{result}"
    );
}