splice 2.8.0

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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Error code registry for Splice diagnostics.
//!
//! Provides stable error codes in SPL-E### format with severity levels,
//! precise locations, and actionable hints for LLM consumption.

/// Error code explanations following the `rustc --explain` pattern.
pub mod explanations;
/// Error code registry: ErrorCode struct, ErrorSeverity enum, SpliceErrorCode enum.
pub mod registry;

pub use explanations::*;
pub use registry::*;

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

    #[test]
    fn test_error_code_format() {
        assert_eq!(SpliceErrorCode::SymbolNotFound.code(), "SPL-E001");
        assert_eq!(SpliceErrorCode::AmbiguousSymbol.code(), "SPL-E002");
        assert_eq!(SpliceErrorCode::ParseError.code(), "SPL-E011");
        assert_eq!(SpliceErrorCode::InvalidSpan.code(), "SPL-E021");
        assert_eq!(SpliceErrorCode::FileReadError.code(), "SPL-E031");
        assert_eq!(SpliceErrorCode::PreVerificationFailed.code(), "SPL-E041");
        assert_eq!(SpliceErrorCode::InvalidPlanSchema.code(), "SPL-E051");
        assert_eq!(SpliceErrorCode::GraphError.code(), "SPL-E061");
        assert_eq!(SpliceErrorCode::ExecutionLogError.code(), "SPL-E071");
        assert_eq!(SpliceErrorCode::AnalyzerNotAvailable.code(), "SPL-E081");
    }

    #[test]
    fn test_error_code_severity() {
        assert_eq!(SpliceErrorCode::SymbolNotFound.severity(), "error");
        assert_eq!(SpliceErrorCode::ParseError.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidSpan.severity(), "error");
    }

    #[test]
    fn test_error_code_has_hint() {
        let code = SpliceErrorCode::SymbolNotFound;
        let hint = code.hint();
        assert!(!hint.is_empty());
        assert!(hint.contains("symbol") || hint.contains("file"));
    }

    #[test]
    fn test_error_code_from_splice_error() {
        use crate::SpliceError;

        let symbol_error = SpliceError::symbol_not_found("foo", None);
        let code = SpliceErrorCode::from_splice_error(&symbol_error);
        assert_eq!(code, Some(SpliceErrorCode::SymbolNotFound));

        let ambiguous_error = SpliceError::AmbiguousSymbol {
            name: "foo".to_string(),
            files: vec!["a.rs".to_string(), "b.rs".to_string()],
        };
        let code = SpliceErrorCode::from_splice_error(&ambiguous_error);
        assert_eq!(code, Some(SpliceErrorCode::AmbiguousSymbol));
    }

    #[test]
    fn test_error_code_coverage() {
        use crate::SpliceError;
        use std::path::PathBuf;

        // Test all error-level SpliceError variants produce error codes
        let mut mapped_count = 0;

        // Symbol resolution errors
        let error = SpliceError::symbol_not_found("test", None);
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::AmbiguousSymbol {
            name: "foo".to_string(),
            files: vec!["a.rs".to_string()],
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::ReferenceFailed {
            name: "foo".to_string(),
            reason: "test".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::AmbiguousReference {
            name: "foo".to_string(),
            file: "test.rs".to_string(),
            line: 1,
            col: 1,
            candidates: vec!["a::foo".to_string()],
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Parse/AST errors
        let error = SpliceError::Parse {
            file: PathBuf::from("test.rs"),
            message: "test error".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // InvalidUtf8 - create by converting invalid bytes
        let invalid_bytes = vec![0xffu8, 0xfe];
        let invalid_utf8 = std::str::from_utf8(&invalid_bytes).unwrap_err();
        let error = SpliceError::InvalidUtf8 {
            file: PathBuf::from("test.rs"),
            source: invalid_utf8,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::CompilerError("syntax error".to_string());
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Span errors
        let error = SpliceError::InvalidSpan {
            file: PathBuf::from("test.rs"),
            start: 0,
            end: 10,
            file_size: 100,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::InvalidLineRange {
            file: PathBuf::from("test.rs"),
            line_start: 1,
            line_end: 10,
            total_lines: 20,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::FileExternallyModified {
            file: "test.rs".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // I/O errors
        let error = SpliceError::Io {
            path: PathBuf::from("test.rs"),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "test"),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::IoContext {
            context: "test context".to_string(),
            source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "test"),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::InsufficientDiskSpace {
            needed: 1000,
            available: 100,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Validation errors
        let error = SpliceError::PreVerificationFailed {
            check: "test check".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::ParseValidationFailed {
            file: PathBuf::from("test.rs"),
            message: "validation failed".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::CompilerValidationFailed {
            file: PathBuf::from("test.rs"),
            language: "rust".to_string(),
            diagnostics: vec![],
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Plan execution errors
        let error = SpliceError::InvalidPlanSchema {
            message: "invalid schema".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::PlanExecutionFailed {
            step: 1,
            error: "failed".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::InvalidBatchSchema {
            message: "invalid batch".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Graph/database errors
        let error = SpliceError::Graph(sqlitegraph::SqliteGraphError::connection("test error"));
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Execution log errors
        let error = SpliceError::ExecutionLogError {
            message: "log error".to_string(),
            source: None,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::ExecutionNotFound {
            execution_id: "test-id".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::ExecutionRecordFailed {
            execution_id: "test-id".to_string(),
            source: None,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Analyzer errors
        let error = SpliceError::AnalyzerNotAvailable {
            mode: "path".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        let error = SpliceError::AnalyzerFailed {
            output: "failed".to_string(),
            diagnostics: vec![],
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Query errors
        let error = SpliceError::QueryError {
            message: "query failed".to_string(),
            source: None,
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Date format errors
        let error = SpliceError::InvalidDateFormat {
            input: "invalid".to_string(),
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Cargo check failed (Rust-specific compiler validation)
        let error = SpliceError::CargoCheckFailed {
            workspace: PathBuf::from("/workspace"),
            output: "check failed".to_string(),
            diagnostics: vec![],
        };
        assert!(SpliceErrorCode::from_splice_error(&error).is_some());
        mapped_count += 1;

        // Intentionally unmapped errors
        let error = SpliceError::BrokenPipe;
        assert!(
            SpliceErrorCode::from_splice_error(&error).is_none(),
            "BrokenPipe should not have an error code (terminal state)"
        );

        // Utf8 variant - intentionally unmapped
        let invalid_bytes = vec![0xffu8, 0xfe];
        let invalid_utf8 = std::str::from_utf8(&invalid_bytes).unwrap_err();
        let error = SpliceError::Utf8(invalid_utf8);
        assert!(
            SpliceErrorCode::from_splice_error(&error).is_none(),
            "Utf8 variant should not have an error code (covered by InvalidUtf8)"
        );

        let error = SpliceError::Other("generic error".to_string());
        assert!(
            SpliceErrorCode::from_splice_error(&error).is_none(),
            "Other variant should not have an error code (generic catchall)"
        );

        // Verify we have at least 22 error-level variants mapped
        assert!(
            mapped_count >= 22,
            "Expected at least 22 error-level variants to be mapped, got {}",
            mapped_count
        );

        println!("Total error-level variants mapped: {}", mapped_count);
    }

    #[test]
    fn test_explain_command_generation() {
        use crate::SpliceError;

        // Test that explain_command is generated for errors with error codes
        let symbol_error = SpliceError::symbol_not_found("foo", None);
        let payload = crate::cli::CliErrorPayload::from_error(&symbol_error);

        // Should have error_code
        assert!(payload.error.error_code.is_some());
        // Should have explain_command
        let Some(explain_cmd) = payload.error.explain_command.as_ref() else {
            panic!("explain_command must be present for coded errors");
        };
        assert_eq!(explain_cmd, "splice explain --code SPL-E001");
        assert!(explain_cmd.contains("splice explain --code"));

        // Test that BrokenPipe (no error code) doesn't have explain_command
        let broken_pipe_error = SpliceError::BrokenPipe;
        let payload = crate::cli::CliErrorPayload::from_error(&broken_pipe_error);

        assert!(payload.error.error_code.is_none());
        assert!(payload.error.explain_command.is_none());

        // Test format for various error codes
        let test_cases = vec![
            (
                SpliceError::Parse {
                    file: std::path::PathBuf::from("test.rs"),
                    message: "parse error".to_string(),
                },
                "SPL-E011",
            ),
            (
                SpliceError::InvalidSpan {
                    file: std::path::PathBuf::from("test.rs"),
                    start: 0,
                    end: 10,
                    file_size: 100,
                },
                "SPL-E021",
            ),
            (
                SpliceError::Io {
                    path: std::path::PathBuf::from("test.rs"),
                    source: std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
                },
                "SPL-E031",
            ),
        ];

        for (error, expected_code) in test_cases {
            let payload = crate::cli::CliErrorPayload::from_error(&error);
            assert!(payload.error.error_code.is_some());
            let Some(explain_cmd) = payload.error.explain_command.as_ref() else {
                panic!("explain_command must be present for coded errors");
            };
            let expected = format!("splice explain --code {}", expected_code);
            assert_eq!(explain_cmd, &expected);
        }
    }

    #[test]
    fn test_error_code_construction() {
        let code = ErrorCode::new(
            "SPL-E001",
            "error",
            "src/main.rs:10:5",
            "Check symbol spelling",
        );

        assert_eq!(code.code, "SPL-E001");
        assert_eq!(code.severity, "error");
        assert_eq!(code.location, Some("src/main.rs:10:5".to_string()));
        assert_eq!(code.hint, "Check symbol spelling");
    }

    #[test]
    fn test_error_code_from_splice_code() {
        let error_code = ErrorCode::from_splice_code(
            SpliceErrorCode::SymbolNotFound,
            Some("src/main.rs"),
            Some(10),
            Some(5),
        );

        assert_eq!(error_code.code, "SPL-E001");
        assert_eq!(error_code.severity, "error");
        assert_eq!(error_code.location, Some("src/main.rs:10:5".to_string()));
        assert!(!error_code.hint.is_empty());
    }

    #[test]
    fn test_error_code_location_formats() {
        // Full location
        let ec = ErrorCode::from_splice_code(
            SpliceErrorCode::InvalidSpan,
            Some("test.rs"),
            Some(5),
            Some(10),
        );
        assert_eq!(ec.location, Some("test.rs:5:10".to_string()));

        // File only
        let ec = ErrorCode::from_splice_code(
            SpliceErrorCode::FileReadError,
            Some("test.rs"),
            None,
            None,
        );
        assert_eq!(ec.location, Some("test.rs".to_string()));

        // Unknown location
        let ec = ErrorCode::from_splice_code(SpliceErrorCode::GraphError, None, None, None);
        assert_eq!(ec.location, None);
    }

    #[test]
    fn test_error_severity_as_str() {
        assert_eq!(ErrorSeverity::Error.as_str(), "error");
        assert_eq!(ErrorSeverity::Warning.as_str(), "warning");
        assert_eq!(ErrorSeverity::Note.as_str(), "note");
    }

    #[test]
    fn test_error_code_severity_error() {
        // Test that error-level codes return "error"
        assert_eq!(SpliceErrorCode::SymbolNotFound.severity(), "error");
        assert_eq!(SpliceErrorCode::ReferenceFailed.severity(), "error");
        assert_eq!(SpliceErrorCode::ParseError.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidUtf8.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidSyntax.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidSpan.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidLineRange.severity(), "error");
        assert_eq!(SpliceErrorCode::SpanOutOfBounds.severity(), "error");
        assert_eq!(SpliceErrorCode::FileReadError.severity(), "error");
        assert_eq!(SpliceErrorCode::FileWriteError.severity(), "error");
        assert_eq!(SpliceErrorCode::FileNotFound.severity(), "error");
        assert_eq!(SpliceErrorCode::PreVerificationFailed.severity(), "error");
        assert_eq!(SpliceErrorCode::ParseValidationFailed.severity(), "error");
        assert_eq!(
            SpliceErrorCode::CompilerValidationFailed.severity(),
            "error"
        );
        assert_eq!(SpliceErrorCode::InvalidPlanSchema.severity(), "error");
        assert_eq!(SpliceErrorCode::PlanExecutionFailed.severity(), "error");
        assert_eq!(SpliceErrorCode::InvalidBatchSchema.severity(), "error");
        assert_eq!(SpliceErrorCode::GraphError.severity(), "error");
        assert_eq!(SpliceErrorCode::DatabaseError.severity(), "error");
        assert_eq!(SpliceErrorCode::ExecutionLogError.severity(), "error");
        assert_eq!(SpliceErrorCode::ExecutionNotFound.severity(), "error");
        assert_eq!(SpliceErrorCode::AnalyzerNotAvailable.severity(), "error");
        assert_eq!(SpliceErrorCode::AnalyzerFailed.severity(), "error");
    }

    #[test]
    fn test_error_code_severity_warning() {
        // Test that warning-level codes return "warning"
        assert_eq!(SpliceErrorCode::AmbiguousSymbol.severity(), "warning");
        assert_eq!(SpliceErrorCode::AmbiguousReference.severity(), "warning");
        assert_eq!(
            SpliceErrorCode::FileExternallyModified.severity(),
            "warning"
        );
        assert_eq!(
            SpliceErrorCode::AmbiguousSymbolAsWarning.severity(),
            "warning"
        );
        assert_eq!(SpliceErrorCode::FileSkipped.severity(), "warning");
        assert_eq!(
            SpliceErrorCode::FileExternallyModifiedWarning.severity(),
            "warning"
        );
    }

    #[test]
    fn test_error_code_all_have_severity() {
        // Test that all SpliceErrorCode variants have valid severity
        let all_codes = [
            SpliceErrorCode::SymbolNotFound,
            SpliceErrorCode::AmbiguousSymbol,
            SpliceErrorCode::ReferenceFailed,
            SpliceErrorCode::AmbiguousReference,
            SpliceErrorCode::ParseError,
            SpliceErrorCode::InvalidUtf8,
            SpliceErrorCode::InvalidSyntax,
            SpliceErrorCode::InvalidSpan,
            SpliceErrorCode::InvalidLineRange,
            SpliceErrorCode::SpanOutOfBounds,
            SpliceErrorCode::FileReadError,
            SpliceErrorCode::FileWriteError,
            SpliceErrorCode::FileNotFound,
            SpliceErrorCode::FileExternallyModified,
            SpliceErrorCode::AmbiguousSymbolAsWarning,
            SpliceErrorCode::FileSkipped,
            SpliceErrorCode::FileExternallyModifiedWarning,
            SpliceErrorCode::PreVerificationFailed,
            SpliceErrorCode::ParseValidationFailed,
            SpliceErrorCode::CompilerValidationFailed,
            SpliceErrorCode::InvalidPlanSchema,
            SpliceErrorCode::PlanExecutionFailed,
            SpliceErrorCode::InvalidBatchSchema,
            SpliceErrorCode::GraphError,
            SpliceErrorCode::DatabaseError,
            SpliceErrorCode::ExecutionLogError,
            SpliceErrorCode::ExecutionNotFound,
            SpliceErrorCode::AnalyzerNotAvailable,
            SpliceErrorCode::AnalyzerFailed,
        ];

        for code in all_codes.iter() {
            let severity = code.severity();
            assert!(
                severity == "error" || severity == "warning" || severity == "note",
                "Code {:?} has invalid severity: {}",
                code,
                severity
            );
        }
    }

    #[test]
    fn test_warning_code_format() {
        // Test that warning codes use SPL-W### format
        assert_eq!(SpliceErrorCode::AmbiguousSymbolAsWarning.code(), "SPL-W001");
        assert_eq!(SpliceErrorCode::FileSkipped.code(), "SPL-W002");
        assert_eq!(
            SpliceErrorCode::FileExternallyModifiedWarning.code(),
            "SPL-W003"
        );
    }

    #[test]
    fn test_warning_code_from_splice_code() {
        // Test that warning codes produce proper ErrorCode with warning severity
        let warning_code = ErrorCode::from_splice_code(
            SpliceErrorCode::AmbiguousSymbolAsWarning,
            Some("src/main.rs"),
            Some(10),
            Some(5),
        );

        assert_eq!(warning_code.code, "SPL-W001");
        assert_eq!(warning_code.severity, "warning");
        assert_eq!(warning_code.location, Some("src/main.rs:10:5".to_string()));
        assert!(!warning_code.hint.is_empty());
    }
}