splice 2.6.2

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
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
//! Integration tests for span-safe patching with validation gates.
//!
//! These tests validate the full pipeline:
//! resolve → patch-by-span → tree-sitter reparse gate → cargo check gate → optional rust-analyzer

use splice::graph::CodeGraph;
use splice::ingest::rust::extract_rust_symbols;
use splice::patch::apply_patch_with_validation;
use splice::patch::{apply_batch_with_validation, SpanBatch, SpanReplacement};
use splice::resolve::resolve_symbol;
use splice::symbol::Language;
use splice::validate::AnalyzerMode;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};

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

    /// Test A: Patch succeeds with all gates passing.
    ///
    /// This test creates a temporary Rust workspace, indexes symbols, resolves a function,
    /// applies a valid patch, and verifies:
    /// 1) File content changed exactly in the resolved byte span
    /// 2) Tree-sitter reparse succeeds
    /// 3) cargo check succeeds
    #[test]
    fn test_patch_succeeds_with_all_gates() {
        // Create temporary workspace directory
        let workspace_dir = TempDir::new().expect("Failed to create temp workspace");
        let workspace_path = workspace_dir.path();

        // Create Cargo.toml
        let cargo_toml_path = workspace_path.join("Cargo.toml");
        let mut cargo_toml = NamedTempFile::new().expect("Failed to create Cargo.toml");
        write!(
            cargo_toml,
            r#"[package]
name = "temp-test"
version = "0.1.0"
edition = "2021"

[lib]
name = "temp_test"
path = "src/lib.rs"
"#
        )
        .expect("Failed to write Cargo.toml");
        std::fs::rename(cargo_toml.path(), &cargo_toml_path).expect("Failed to move Cargo.toml");

        // Create src directory
        let src_dir = workspace_path.join("src");
        std::fs::create_dir(&src_dir).expect("Failed to create src directory");

        // Create lib.rs with function to patch
        let lib_rs_path = src_dir.join("lib.rs");
        let source = r#"
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

pub fn farewell(name: &str) -> String {
    format!("Goodbye, {}!", name)
}
"#;

        std::fs::write(&lib_rs_path, source).expect("Failed to write lib.rs");

        // Create temporary graph database
        let graph_db_path = workspace_path.join("graph.db");
        let mut code_graph =
            CodeGraph::open(&graph_db_path).expect("Failed to open graph database");

        // Ingest symbols from lib.rs
        let symbols =
            extract_rust_symbols(&lib_rs_path, source.as_bytes()).expect("Failed to parse lib.rs");

        assert_eq!(symbols.len(), 2, "Expected 2 functions");

        // Store symbols with file association and language
        for symbol in &symbols {
            code_graph
                .store_symbol_with_file_and_language(
                    &lib_rs_path,
                    &symbol.name,
                    symbol.kind.as_str(),
                    Language::Rust,
                    symbol.byte_start,
                    symbol.byte_end,
                    symbol.line_start,
                    symbol.line_end,
                    symbol.col_start,
                    symbol.col_end,
                )
                .expect("Failed to store symbol");
        }

        // Resolve the "greet" function
        let resolved = resolve_symbol(&code_graph, Some(&lib_rs_path), Some("function"), "greet")
            .expect("Failed to resolve greet function");

        // Verify we got the right span
        let greet_symbol = &symbols[0];
        assert_eq!(resolved.name, "greet");
        assert_eq!(resolved.byte_start, greet_symbol.byte_start);
        assert_eq!(resolved.byte_end, greet_symbol.byte_end);

        // Apply patch: replace function body
        let new_body = r#"
pub fn greet(name: &str) -> String {
    format!("Greetings, {}!", name)
}
"#;

        let result = apply_patch_with_validation(
            &lib_rs_path,
            resolved.byte_start,
            resolved.byte_end,
            new_body.trim(),
            workspace_path,    // For compiler check
            Language::Rust,    // Rust file
            AnalyzerMode::Off, // rust-analyzer OFF for this test
            false,             // strict: test mode doesn't need strict validation
            false,             // skip: still run validation for test
        );

        // Should succeed
        assert!(result.is_ok(), "Patch should succeed: {:?}", result);

        // Verify file content changed exactly in the span
        let new_content =
            std::fs::read_to_string(&lib_rs_path).expect("Failed to read patched file");

        assert!(
            new_content.contains("Greetings, "),
            "Patched content should be present"
        );
        assert!(
            !new_content.contains("Hello, "),
            "Old content should be gone"
        );

        // Verify the other function is unchanged
        assert!(
            new_content.contains("Goodbye,"),
            "Other function should be unchanged"
        );
    }

    /// Test B: Patch rejected on syntax gate.
    ///
    /// This test introduces a syntax error and verifies:
    /// 1) SpliceError::ParseValidationFailed is returned
    /// 2) Original file is unchanged (atomic rollback)
    #[test]
    fn test_patch_rejected_on_syntax_gate() {
        // Create temporary workspace
        let workspace_dir = TempDir::new().expect("Failed to create temp workspace");
        let workspace_path = workspace_dir.path();

        // Create Cargo.toml
        let cargo_toml_path = workspace_path.join("Cargo.toml");
        let mut cargo_toml = NamedTempFile::new().expect("Failed to create Cargo.toml");
        write!(
            cargo_toml,
            r#"[package]
name = "temp-test"
version = "0.1.0"
edition = "2021"

[lib]
name = "temp_test"
path = "src/lib.rs"
"#
        )
        .expect("Failed to write Cargo.toml");
        std::fs::rename(cargo_toml.path(), &cargo_toml_path).expect("Failed to move Cargo.toml");

        // Create src directory
        let src_dir = workspace_path.join("src");
        std::fs::create_dir(&src_dir).expect("Failed to create src directory");

        // Create lib.rs
        let lib_rs_path = src_dir.join("lib.rs");
        let source = r#"
pub fn valid_function() -> i32 {
    42
}
"#;

        std::fs::write(&lib_rs_path, source).expect("Failed to write lib.rs");

        // Create temporary graph database
        let graph_db_path = workspace_path.join("graph.db");
        let mut code_graph =
            CodeGraph::open(&graph_db_path).expect("Failed to open graph database");

        // Ingest and store symbols
        let symbols =
            extract_rust_symbols(&lib_rs_path, source.as_bytes()).expect("Failed to parse lib.rs");

        let symbol = &symbols[0];
        code_graph
            .store_symbol_with_file_and_language(
                &lib_rs_path,
                &symbol.name,
                symbol.kind.as_str(),
                Language::Rust,
                symbol.byte_start,
                symbol.byte_end,
                symbol.line_start,
                symbol.line_end,
                symbol.col_start,
                symbol.col_end,
            )
            .expect("Failed to store symbol");

        // Resolve function
        let resolved = resolve_symbol(
            &code_graph,
            Some(&lib_rs_path),
            Some("function"),
            "valid_function",
        )
        .expect("Failed to resolve function");

        // Read original content for comparison
        let replaced_content =
            std::fs::read_to_string(&lib_rs_path).expect("Failed to read replaced file");

        // Apply patch with syntax error (unclosed brace)
        let invalid_patch = r#"
pub fn valid_function() -> i32 {
    42
"#;

        let result = apply_patch_with_validation(
            &lib_rs_path,
            resolved.byte_start,
            resolved.byte_end,
            invalid_patch.trim(),
            workspace_path,
            Language::Rust,    // Rust file
            AnalyzerMode::Off, // rust-analyzer OFF for this test
            false,             // strict: test mode doesn't need strict validation
            false,             // skip: still run validation for test
        );

        // Should fail with parse validation error
        assert!(result.is_err(), "Patch should fail on syntax error");

        match result {
            Err(splice::SpliceError::ParseValidationFailed { .. }) => {
                // Expected error type
            }
            Err(other) => {
                panic!("Expected ParseValidationFailed, got: {:?}", other);
            }
            Ok(_) => {
                panic!("Expected error for syntax error, but patch succeeded");
            }
        }

        // Verify original file is unchanged (atomic rollback)
        let current_content =
            std::fs::read_to_string(&lib_rs_path).expect("Failed to read current file");

        assert_eq!(
            replaced_content, current_content,
            "File should be unchanged after failed patch (atomic rollback)"
        );
    }

    /// Test C: Patch rejected on compiler gate.
    ///
    /// This test introduces a type error and verifies:
    /// 1) SpliceError::CargoCheckFailed is returned
    /// 2) Original file is unchanged (atomic rollback)
    #[test]
    fn test_patch_rejected_on_compiler_gate() {
        // Create temporary workspace
        let workspace_dir = TempDir::new().expect("Failed to create temp workspace");
        let workspace_path = workspace_dir.path();

        // Create Cargo.toml
        let cargo_toml_path = workspace_path.join("Cargo.toml");
        let mut cargo_toml = NamedTempFile::new().expect("Failed to create Cargo.toml");
        write!(
            cargo_toml,
            r#"[package]
name = "temp-test"
version = "0.1.0"
edition = "2021"

[lib]
name = "temp_test"
path = "src/lib.rs"
"#
        )
        .expect("Failed to write Cargo.toml");
        std::fs::rename(cargo_toml.path(), &cargo_toml_path).expect("Failed to move Cargo.toml");

        // Create src directory
        let src_dir = workspace_path.join("src");
        std::fs::create_dir(&src_dir).expect("Failed to create src directory");

        // Create lib.rs with function returning i32
        let lib_rs_path = src_dir.join("lib.rs");
        let source = r#"
pub fn get_number() -> i32 {
    42
}
"#;

        std::fs::write(&lib_rs_path, source).expect("Failed to write lib.rs");

        // Create temporary graph database
        let graph_db_path = workspace_path.join("graph.db");
        let mut code_graph =
            CodeGraph::open(&graph_db_path).expect("Failed to open graph database");

        // Ingest and store symbols
        let symbols =
            extract_rust_symbols(&lib_rs_path, source.as_bytes()).expect("Failed to parse lib.rs");

        let symbol = &symbols[0];
        code_graph
            .store_symbol_with_file_and_language(
                &lib_rs_path,
                &symbol.name,
                symbol.kind.as_str(),
                Language::Rust,
                symbol.byte_start,
                symbol.byte_end,
                symbol.line_start,
                symbol.line_end,
                symbol.col_start,
                symbol.col_end,
            )
            .expect("Failed to store symbol");

        // Resolve function
        let resolved = resolve_symbol(
            &code_graph,
            Some(&lib_rs_path),
            Some("function"),
            "get_number",
        )
        .expect("Failed to resolve function");

        // Read original content for comparison
        let replaced_content =
            std::fs::read_to_string(&lib_rs_path).expect("Failed to read replaced file");

        // Apply patch that breaks the type signature (returns String instead of i32)
        let type_error_patch = r#"
pub fn get_number() -> i32 {
    "this is a string not an i32"
}
"#;

        let result = apply_patch_with_validation(
            &lib_rs_path,
            resolved.byte_start,
            resolved.byte_end,
            type_error_patch.trim(),
            workspace_path,
            Language::Rust,    // Rust file
            AnalyzerMode::Off, // rust-analyzer OFF for this test
            false,             // strict: test mode doesn't need strict validation
            false,             // skip: still run validation for test
        );

        // Should fail with compiler validation error
        assert!(result.is_err(), "Patch should fail on type error");

        match result {
            Err(splice::SpliceError::CargoCheckFailed { .. }) => {
                // Expected error type (Rust-specific cargo check)
            }
            Err(other) => {
                panic!("Expected CargoCheckFailed, got: {:?}", other);
            }
            Ok(_) => {
                panic!("Expected error for type mismatch, but patch succeeded");
            }
        }

        // Verify original file is unchanged (atomic rollback)
        let current_content =
            std::fs::read_to_string(&lib_rs_path).expect("Failed to read current file");

        assert_eq!(
            replaced_content, current_content,
            "File should be unchanged after failed patch (atomic rollback)"
        );
    }

    /// Test D: Batch patch rolls back when a later replacement fails validation.
    ///
    /// This test sets up two files. The first replacement is valid, the second introduces
    /// a type error. The entire batch must fail atomically with both files untouched.
    #[test]
    fn test_apply_batch_rolls_back_on_failure() {
        let workspace_dir = TempDir::new().expect("Failed to create temp workspace");
        let workspace_path = workspace_dir.path();

        // Create Cargo manifest
        let cargo_toml_path = workspace_path.join("Cargo.toml");
        let mut cargo_toml = NamedTempFile::new().expect("Failed to create Cargo.toml");
        write!(
            cargo_toml,
            r#"[package]
name = "temp-test"
version = "0.1.0"
edition = "2021"

[lib]
name = "temp_test"
path = "src/lib.rs"
"#
        )
        .expect("Failed to write Cargo.toml");
        std::fs::rename(cargo_toml.path(), &cargo_toml_path).expect("Failed to move Cargo.toml");

        let src_dir = workspace_path.join("src");
        std::fs::create_dir(&src_dir).expect("Failed to create src directory");

        // lib.rs contains a helper invoked by module a and b
        let lib_rs_path = src_dir.join("lib.rs");
        std::fs::write(
            &lib_rs_path,
            r#"
pub fn helper(x: i32) -> i32 {
    x + 1
}

pub mod a;
pub mod b;
"#,
        )
        .expect("Failed to write lib.rs");

        let file_a = src_dir.join("a.rs");
        std::fs::write(
            &file_a,
            r#"
pub fn value() -> i32 {
    helper(10)
}
"#,
        )
        .expect("Failed to write a.rs");

        let file_b = src_dir.join("b.rs");
        std::fs::write(
            &file_b,
            r#"
pub fn broken() -> i32 {
    helper(5)
}
"#,
        )
        .expect("Failed to write b.rs");

        // Compute spans for helper usage
        let mut replacements = Vec::new();

        let symbols =
            extract_rust_symbols(&file_a, std::fs::read(&file_a).unwrap().as_slice()).unwrap();
        let target = symbols.iter().find(|s| s.name == "value").unwrap();
        replacements.push(SpanReplacement {
            file: file_a.clone(),
            start: target.byte_start,
            end: target.byte_end,
            content: r#"
pub fn value() -> i32 {
    helper(42)
}
"#
            .trim()
            .to_string(),
        });

        let symbols_b =
            extract_rust_symbols(&file_b, std::fs::read(&file_b).unwrap().as_slice()).unwrap();
        let target_b = symbols_b.iter().find(|s| s.name == "broken").unwrap();
        replacements.push(SpanReplacement {
            file: file_b.clone(),
            start: target_b.byte_start,
            end: target_b.byte_end,
            content: r#"
pub fn broken() -> i32 {
    helper("oops")
}
"#
            .trim()
            .to_string(),
        });

        let batches = vec![SpanBatch::new(replacements)];
        let replaced_a = std::fs::read_to_string(&file_a).unwrap();
        let replaced_b = std::fs::read_to_string(&file_b).unwrap();

        let result = apply_batch_with_validation(
            &batches,
            workspace_path,
            Language::Rust,
            AnalyzerMode::Off,
        );

        assert!(
            result.is_err(),
            "Batch should fail due to invalid second patch"
        );
        let err = result.err().unwrap();
        assert!(
            matches!(err, splice::SpliceError::CargoCheckFailed { .. }),
            "Expected CargoCheckFailed, got {:?}",
            err
        );

        assert_eq!(
            replaced_a,
            std::fs::read_to_string(&file_a).unwrap(),
            "File a.rs should remain unchanged after batch failure"
        );
        assert_eq!(
            replaced_b,
            std::fs::read_to_string(&file_b).unwrap(),
            "File b.rs should remain unchanged after batch failure"
        );
    }
}