Skip to main content

vtcode_core/tools/editing/patch/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5/// Errors produced while parsing or applying agent patches.
6#[derive(Debug, Error)]
7pub enum PatchError {
8    #[error("cannot parse empty patch input")]
9    EmptyInput,
10
11    #[error("patch does not contain any operations")]
12    NoOperations,
13
14    #[error("invalid patch format: {0}")]
15    InvalidFormat(String),
16
17    #[error("invalid patch hunk on line {line}: {message}")]
18    InvalidHunk { line: usize, message: String },
19
20    #[error("invalid patch operation for '{path}': {reason}")]
21    InvalidOperation { path: String, reason: String },
22
23    #[error("invalid path for {operation}: {path} ({reason})")]
24    InvalidPath {
25        operation: &'static str,
26        path: String,
27        reason: String,
28    },
29
30    #[error("file '{path}' not found for update")]
31    MissingFile { path: String },
32
33    #[error(
34        "failed to locate context '{context}' in '{path}'. Fix: Use read_file to get exact content, then copy the context lines precisely."
35    )]
36    ContextNotFound { path: String, context: String },
37
38    #[error(
39        "failed to locate expected lines in '{path}':\n{snippet}\n\nFix: Use complete current context/deletion lines; internal whitespace and full-line content must match. Use read_file first, then use those exact lines in your patch."
40    )]
41    SegmentNotFound { path: String, snippet: String },
42
43    #[error(
44        "patch context in '{path}' matches redacted source; repeated reads show the same redaction. Split the edit around the hidden line when possible. Editing the hidden line requires user assistance."
45    )]
46    RedactedContext { path: String },
47
48    #[error(
49        "semantic patch anchor '{anchor}' for '{path}' could not be resolved safely: {reason}\n\nFix: Use a more specific @@ anchor such as a function/class name, or copy exact context lines from read_file."
50    )]
51    SemanticResolutionFailed {
52        path: String,
53        anchor: String,
54        reason: String,
55    },
56
57    #[error(
58        "semantic patch anchor '{anchor}' for '{path}' matched {candidate_count} possible locations. Use a more specific @@ anchor or smaller hunk."
59    )]
60    SemanticAmbiguous {
61        path: String,
62        anchor: String,
63        candidate_count: usize,
64    },
65
66    #[error("I/O error while {action} '{path}': {source}")]
67    Io {
68        action: &'static str,
69        path: PathBuf,
70        #[source]
71        source: std::io::Error,
72    },
73
74    #[error("failed to generate temporary path for '{path}': {source}")]
75    TempPath {
76        path: PathBuf,
77        #[source]
78        source: std::time::SystemTimeError,
79    },
80
81    #[error("failed to rollback patch after error ({original}): {rollback}")]
82    Recovery {
83        original: Box<PatchError>,
84        #[source]
85        rollback: Box<PatchError>,
86    },
87
88    #[error("{original}")]
89    RolledBack {
90        #[source]
91        original: Box<PatchError>,
92    },
93}
94
95impl PatchError {
96    /// A redaction mismatch never contains the underlying source text.
97    pub fn redacted_context_path(&self) -> Option<&str> {
98        match self {
99            Self::RedactedContext { path } => Some(path),
100            Self::RolledBack { original } => original.redacted_context_path(),
101            _ => None,
102        }
103    }
104
105    /// Only exact text/context failures qualify for a fresh recovery read.
106    pub fn context_mismatch(&self) -> Option<(&str, &str)> {
107        match self {
108            Self::ContextNotFound { path, context } => Some((path, context)),
109            Self::SegmentNotFound { path, snippet } => Some((path, snippet)),
110            Self::RolledBack { original } => original.context_mismatch(),
111            // A failed rollback requires resolving partial state first.
112            _ => None,
113        }
114    }
115}