1use serde::{Deserialize, Serialize};
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct WorkspacePatchSuccess {
15 pub ok: bool,
16 pub tool: String,
17 pub changes: Vec<WorkspacePatchSuccessChange>,
18}
19
20impl WorkspacePatchSuccess {
21 pub(crate) fn model_text(&self) -> String {
22 let mut text = "Applied patch:".to_owned();
23 for change in &self.changes {
24 let operation = match change.operation() {
25 WorkspacePatchOperationKind::Add => "added",
26 WorkspacePatchOperationKind::Update => "updated",
27 WorkspacePatchOperationKind::Delete => "deleted",
28 WorkspacePatchOperationKind::Unknown => "changed",
29 };
30 text.push_str(&format!("\n{operation} {}", change.path));
31 if change.ignored_context_hunks > 0 {
32 text.push_str(&format!(
33 " (ignored {} context-only hunks)",
34 change.ignored_context_hunks
35 ));
36 }
37 }
38 text
39 }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
44pub struct WorkspacePatchSuccessChange {
45 pub path: String,
47 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub op: Option<WorkspacePatchOperationKind>,
51 pub hunks: usize,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub lines_before: Option<usize>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub lines_after: Option<usize>,
57 pub bytes_before: usize,
58 pub bytes_after: usize,
59 #[serde(default, skip_serializing_if = "is_zero")]
61 pub ignored_context_hunks: usize,
62 #[serde(default)]
64 pub lines: Vec<WorkspacePatchSuccessLine>,
65}
66
67impl WorkspacePatchSuccessChange {
68 #[must_use]
73 pub fn operation(&self) -> WorkspacePatchOperationKind {
74 self.op.unwrap_or(WorkspacePatchOperationKind::Update)
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
83#[serde(rename_all = "snake_case")]
84pub enum WorkspacePatchOperationKind {
85 Add,
86 Update,
87 Delete,
88 #[serde(other)]
94 Unknown,
95}
96
97#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
99pub struct WorkspacePatchSuccessLine {
100 pub kind: WorkspacePatchSuccessLineKind,
101 #[serde(default, skip_serializing_if = "Option::is_none")]
102 pub old_line: Option<usize>,
103 #[serde(default, skip_serializing_if = "Option::is_none")]
104 pub new_line: Option<usize>,
105 pub text: String,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
110#[serde(rename_all = "snake_case")]
111pub enum WorkspacePatchSuccessLineKind {
112 Context,
113 Remove,
114 Add,
115 #[serde(other)]
121 Unknown,
122}
123
124fn is_zero(value: &usize) -> bool {
125 *value == 0
126}
127
128#[cfg(test)]
129mod tests {
130 use super::{
131 WorkspacePatchOperationKind, WorkspacePatchSuccess, WorkspacePatchSuccessChange,
132 WorkspacePatchSuccessLine, WorkspacePatchSuccessLineKind,
133 };
134
135 #[test]
136 fn success_envelope_round_trips_every_field() {
137 let envelope = WorkspacePatchSuccess {
138 ok: true,
139 tool: "apply_patch".to_owned(),
140 changes: vec![WorkspacePatchSuccessChange {
141 path: "dir/note.txt".to_owned(),
142 op: Some(WorkspacePatchOperationKind::Update),
143 hunks: 1,
144 lines_before: Some(3),
145 lines_after: Some(3),
146 bytes_before: 22,
147 bytes_after: 24,
148 ignored_context_hunks: 2,
149 lines: vec![WorkspacePatchSuccessLine {
150 kind: WorkspacePatchSuccessLineKind::Add,
151 old_line: None,
152 new_line: Some(2),
153 text: "newer".to_owned(),
154 }],
155 }],
156 };
157
158 let json = serde_json::to_string(&envelope).expect("envelope should serialize");
159 let restored: WorkspacePatchSuccess =
160 serde_json::from_str(&json).expect("serialized envelope should deserialize");
161
162 assert_eq!(restored, envelope);
163 assert_eq!(
164 restored.changes[0].operation(),
165 WorkspacePatchOperationKind::Update
166 );
167 }
168
169 #[test]
170 fn success_envelope_reads_a_legacy_entry_as_an_update() {
171 let json = r#"{"ok":true,"tool":"apply_patch","changes":[
173 {"path":"hello.txt","hunks":1,"bytes_before":0,"bytes_after":12}
174 ]}"#;
175
176 let restored: WorkspacePatchSuccess =
177 serde_json::from_str(json).expect("legacy envelope should deserialize");
178
179 let change = &restored.changes[0];
180 assert_eq!(change.operation(), WorkspacePatchOperationKind::Update);
181 assert_eq!(change.lines_before, None);
182 assert_eq!(change.lines_after, None);
183 assert!(change.lines.is_empty());
184 }
185
186 #[test]
187 fn success_envelope_keeps_an_unknown_line_kind_readable() {
188 let json = r#"{"ok":true,"tool":"apply_patch","changes":[{"path":"note.txt","hunks":1,"bytes_before":1,"bytes_after":2,
189 "lines":[{"kind":"something-new","text":"line"}]}]}"#;
190
191 let restored: WorkspacePatchSuccess =
192 serde_json::from_str(json).expect("future line kinds should not fail the result");
193
194 assert_eq!(
195 restored.changes[0].lines[0].kind,
196 WorkspacePatchSuccessLineKind::Unknown
197 );
198 }
199
200 #[test]
201 fn success_envelope_keeps_an_unknown_operation_readable() {
202 let json = r#"{"ok":true,"tool":"apply_patch","changes":[{"path":"note.txt","op":"something-new","hunks":1,"bytes_before":1,"bytes_after":2}]}"#;
203
204 let restored: WorkspacePatchSuccess =
205 serde_json::from_str(json).expect("future operations should not fail the result");
206
207 assert_eq!(
208 restored.changes[0].operation(),
209 WorkspacePatchOperationKind::Unknown
210 );
211 }
212
213 #[test]
214 fn success_envelope_omits_absent_optional_fields_when_serializing() {
215 let envelope = WorkspacePatchSuccess {
216 ok: true,
217 tool: "apply_patch".to_owned(),
218 changes: vec![WorkspacePatchSuccessChange {
219 path: "gone.txt".to_owned(),
220 op: Some(WorkspacePatchOperationKind::Delete),
221 hunks: 0,
222 lines_before: Some(4),
223 lines_after: Some(0),
224 bytes_before: 30,
225 bytes_after: 0,
226 ignored_context_hunks: 0,
227 lines: Vec::new(),
228 }],
229 };
230
231 let value = serde_json::to_value(&envelope).expect("envelope should serialize");
232
233 assert_eq!(
234 value,
235 serde_json::json!({
236 "ok": true,
237 "tool": "apply_patch",
238 "changes": [{
239 "path": "gone.txt",
240 "op": "delete",
241 "hunks": 0,
242 "lines_before": 4,
243 "lines_after": 0,
244 "bytes_before": 30,
245 "bytes_after": 0,
246 "lines": []
247 }]
248 }),
249 "the wire shape must stay stable for existing consumers"
250 );
251 }
252}