vtcode-core 0.162.3

Core library for VT Code - a Rust-based terminal coding agent
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
//! Diff preview utilities for file operations.

use crate::config::constants::diff;
use crate::utils::diff::{DiffOptions, compute_diff_with_theme};
use serde_json::{Value, json};
use std::time::Instant;
use vtcode_commons::ansi::strip_ansi;
use vtcode_commons::diff_preview::count_diff_changes;

/// Create a diff preview response when content exceeds the size limit.
pub fn diff_preview_size_skip() -> Value {
    json!({
        "skipped": true,
        "reason": "content_exceeds_preview_limit",
        "max_bytes": diff::MAX_PREVIEW_BYTES
    })
}

/// Stable machine-readable reason for suppressed diff previews.
///
/// Kept stable for `ThreadEvent`/API consumers. Renderers must show
/// [`diff_preview_user_message`] instead of this code.
pub const SUPPRESSED_PREVIEW_REASON: &str = "too_many_changes";

/// Create a diff preview response when inline diffs are suppressed due to too many changes.
pub fn diff_preview_suppressed(additions: usize, deletions: usize, line_count: usize) -> Value {
    json!({
        "skipped": true,
        "suppressed": true,
        "reason": SUPPRESSED_PREVIEW_REASON,
        "message": diff::SUPPRESSION_MESSAGE,
        "summary": {
            "additions": additions,
            "deletions": deletions,
            "total_lines": line_count
        }
    })
}

/// User-facing one-line summary for a skipped/suppressed diff entry.
///
/// Prefers the producer-supplied `message`, maps known `reason` codes to
/// friendly text, and humanizes unknown snake_case codes. The stable `reason`
/// code itself is never surfaced for known cases.
pub fn diff_preview_user_message(preview: &Value) -> String {
    if let Some(message) = preview
        .get("message")
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        return message.to_owned();
    }
    let reason = preview.get("reason").and_then(Value::as_str).map(str::trim).unwrap_or_default();
    match reason {
        "too_many_changes" => {
            let additions = preview
                .get("additions")
                .and_then(Value::as_u64)
                .or_else(|| preview.get("summary").and_then(|s| s.get("additions")).and_then(Value::as_u64));
            let deletions = preview
                .get("deletions")
                .and_then(Value::as_u64)
                .or_else(|| preview.get("summary").and_then(|s| s.get("deletions")).and_then(Value::as_u64));
            match (additions, deletions) {
                (Some(a), Some(d)) => {
                    format!("Large change — preview suppressed (+{a} -{d}); use `git diff` for the full view")
                }
                _ => "Large change — preview suppressed; use `git diff` for the full view".to_owned(),
            }
        }
        "content_exceeds_preview_limit" => "Preview skipped — file exceeds the preview size limit".to_owned(),
        "" => "preview skipped".to_owned(),
        other => other.replace('_', " "),
    }
}

/// Create a diff preview response when an error prevents diff generation.
pub fn diff_preview_error_skip(reason: &str, detail: Option<&str>) -> Value {
    match detail {
        Some(value) => json!({
            "skipped": true,
            "reason": reason,
            "detail": value
        }),
        None => json!({
            "skipped": true,
            "reason": reason
        }),
    }
}

/// Return the canonical bounded diff entries from a file-operation response.
///
/// `diff[]` is the shared response contract for multi-file operations. The
/// legacy `diff_preview` object is normalized into a one-entry list so older
/// write/create responses render and persist through the same path.
pub fn canonical_diff_previews(output: &Value) -> Vec<Value> {
    if let Some(diffs) = output.get("diff").and_then(Value::as_array) {
        return diffs.clone();
    }

    let Some(preview) = output.get("diff_preview") else {
        return Vec::new();
    };

    let mut entry = preview.clone();
    if let Some(fields) = entry.as_object_mut() {
        if !fields.contains_key("path")
            && let Some(path) = output.get("path").and_then(Value::as_str)
        {
            fields.insert("path".to_string(), Value::String(path.to_string()));
        }
        if !fields.contains_key("operation") {
            let operation = if output.get("created").and_then(Value::as_bool) == Some(true)
                || output.get("file_existed").and_then(Value::as_bool) == Some(false)
            {
                "created"
            } else {
                "updated"
            };
            fields.insert("operation".to_string(), Value::String(operation.to_string()));
        }
    }

    vec![entry]
}

/// Determine whether a canonical diff contract represents an effective
/// mutation. `None` means the response is not a file-operation response and
/// callers should use the tool's normal success semantics.
pub fn diff_output_has_effective_change(output: &Value) -> Option<bool> {
    if output.get("skipped").and_then(Value::as_bool) == Some(true)
        || output.get("conflict").and_then(Value::as_bool) == Some(true)
        || output.get("success").and_then(Value::as_bool) == Some(false)
    {
        return Some(false);
    }

    if output.get("diff").is_none() && output.get("diff_preview").is_none() {
        return None;
    }

    Some(canonical_diff_previews(output).iter().any(diff_preview_has_effective_change))
}

fn diff_preview_has_effective_change(preview: &Value) -> bool {
    if preview.get("is_empty").and_then(Value::as_bool) == Some(true) {
        return matches!(preview.get("operation").and_then(Value::as_str), Some("created" | "deleted"));
    }

    if preview
        .get("content")
        .and_then(Value::as_str)
        .is_some_and(|content| !content.is_empty())
    {
        return true;
    }

    // A skipped preview still represents a completed mutation when the
    // operation itself succeeded; only an explicit `is_empty` preview is a
    // no-op. This covers bounded/suppressed previews without over-counting
    // writes that were skipped or conflicted at the response level.
    preview.get("skipped").and_then(Value::as_bool) == Some(true)
        || preview
            .get("summary")
            .and_then(|summary| summary.get("additions"))
            .and_then(Value::as_u64)
            .is_some_and(|additions| additions > 0)
        || preview
            .get("summary")
            .and_then(|summary| summary.get("deletions"))
            .and_then(Value::as_u64)
            .is_some_and(|deletions| deletions > 0)
}

/// Build a unified diff preview between before and after content.
pub fn build_diff_preview(path: &str, before: Option<&str>, after: &str) -> Value {
    let started = Instant::now();
    let previous = before.unwrap_or("");
    let old_label = format!("a/{path}");
    let new_label = format!("b/{path}");

    let diff_bundle = compute_diff_with_theme(
        previous,
        after,
        DiffOptions {
            context_lines: diff::CONTEXT_RADIUS,
            old_label: Some(old_label.as_str()),
            new_label: Some(new_label.as_str()),
            missing_newline_hint: true,
        },
    );
    // Tool responses carry a plain unified diff. The terminal/UI renderers
    // apply colors, gutters, and syntax highlighting after parsing the diff;
    // embedding ANSI here would hide hunk markers and line prefixes from that
    // parser and make apply_patch previews fall back to raw text.
    let formatted = strip_ansi(&diff_bundle.formatted);

    if formatted.trim().is_empty() {
        tracing::debug!(
            target: "vtcode.tools.diff",
            path,
            before_bytes = previous.len(),
            after_bytes = after.len(),
            additions = 0,
            deletions = 0,
            line_count = 0,
            truncated = false,
            suppressed = false,
            elapsed_ms = started.elapsed().as_millis(),
            "diff preview generated"
        );

        return json!({
            "content": "",
            "truncated": false,
            "omitted_line_count": 0,
            "skipped": false,
            "is_empty": true,
            "additions": 0,
            "deletions": 0
        });
    }

    let line_count = formatted.lines().count();
    let counts = count_diff_changes(&diff_bundle.hunks);
    let additions = counts.additions;
    let deletions = counts.deletions;
    let total_changes = counts.total();

    if total_changes > diff::MAX_SINGLE_FILE_CHANGES {
        tracing::debug!(
            target: "vtcode.tools.diff",
            path,
            before_bytes = previous.len(),
            after_bytes = after.len(),
            additions,
            deletions,
            line_count,
            truncated = false,
            suppressed = true,
            elapsed_ms = started.elapsed().as_millis(),
            "diff preview suppressed (too many changes)"
        );

        return diff_preview_suppressed(additions, deletions, line_count);
    }

    if line_count > diff::MAX_PREVIEW_LINES {
        let lines: Vec<&str> = formatted.lines().collect();
        let head_count = diff::HEAD_LINE_COUNT.min(lines.len());
        let tail_count = diff::TAIL_LINE_COUNT.min(lines.len().saturating_sub(head_count));
        let omitted = lines.len().saturating_sub(head_count + tail_count);

        let mut condensed = Vec::with_capacity(head_count + tail_count + 1);
        condensed.extend(lines[..head_count].iter().copied());
        if omitted > 0 {
            condensed.push("");
        }
        if tail_count > 0 {
            let tail_start = lines.len().saturating_sub(tail_count);
            condensed.extend(lines[tail_start..].iter().copied());
        }

        let diff_output = if omitted > 0 {
            let mut result = condensed[..head_count].join("\n");
            result.push_str(&format!("\n... {omitted} lines omitted ...\n"));
            result.push_str(&condensed[head_count + 1..].join("\n"));
            result
        } else {
            condensed.join("\n")
        };

        let elapsed = started.elapsed().as_millis();

        tracing::debug!(
            target: "vtcode.tools.diff",
            path,
            before_bytes = previous.len(),
            after_bytes = after.len(),
            additions,
            deletions,
            line_count,
            omitted_lines = omitted,
            truncated = true,
            suppressed = false,
            elapsed_ms = elapsed,
            "diff preview generated"
        );

        json!({
            "content": diff_output,
            "truncated": true,
            "omitted_line_count": omitted,
            "skipped": false,
            "additions": additions,
            "deletions": deletions
        })
    } else {
        let elapsed = started.elapsed().as_millis();

        tracing::debug!(
            target: "vtcode.tools.diff",
            path,
            before_bytes = previous.len(),
            after_bytes = after.len(),
            additions,
            deletions,
            line_count,
            truncated = false,
            suppressed = false,
            elapsed_ms = elapsed,
            "diff preview generated"
        );

        json!({
            "content": formatted,
            "truncated": false,
            "omitted_line_count": 0,
            "skipped": false,
            "additions": additions,
            "deletions": deletions
        })
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use vtcode_commons::ansi::strip_ansi;

    use super::*;

    #[test]
    fn canonical_diff_previews_normalize_legacy_write_output() {
        let previews = canonical_diff_previews(&json!({
            "path": "README.md",
            "file_existed": true,
            "diff_preview": {"content": "diff", "skipped": false}
        }));

        assert_eq!(previews.len(), 1);
        assert_eq!(previews[0]["path"], "README.md");
        assert_eq!(previews[0]["operation"], "updated");
    }

    #[test]
    fn effective_change_distinguishes_noop_skipped_and_empty_file_operations() {
        assert_eq!(
            diff_output_has_effective_change(&json!({
                "success": true,
                "diff": [{"operation": "updated", "is_empty": true}]
            })),
            Some(false)
        );
        assert_eq!(
            diff_output_has_effective_change(&json!({"success": true, "skipped": true, "diff": []})),
            Some(false)
        );
        assert_eq!(
            diff_output_has_effective_change(&json!({
                "success": true,
                "diff": [{"operation": "created", "is_empty": true}]
            })),
            Some(true)
        );
    }

    #[test]
    fn suppressed_preview_user_message_prefers_message_and_maps_reason_codes() {
        let suppressed = diff_preview_suppressed(12, 3, 40);
        assert_eq!(suppressed["reason"], SUPPRESSED_PREVIEW_REASON);
        let rendered = diff_preview_user_message(&suppressed);
        assert_eq!(rendered, diff::SUPPRESSION_MESSAGE);
        assert!(!rendered.contains("too_many_changes"));

        let reason_only = json!({
            "skipped": true,
            "reason": SUPPRESSED_PREVIEW_REASON,
            "summary": {"additions": 12, "deletions": 3}
        });
        let fallback = diff_preview_user_message(&reason_only);
        assert!(fallback.contains("+12 -3"), "fallback should keep counts: {fallback}");
        assert!(!fallback.contains("too_many_changes"));

        let oversized = json!({"skipped": true, "reason": "content_exceeds_preview_limit"});
        assert_eq!(diff_preview_user_message(&oversized), "Preview skipped — file exceeds the preview size limit");

        let unknown = json!({"skipped": true, "reason": "custom_retry_later"});
        assert_eq!(diff_preview_user_message(&unknown), "custom retry later");
    }

    #[test]
    fn build_diff_preview_keeps_serialized_content_plain_for_ui_parsing() {
        let preview = build_diff_preview("README.md", Some("before\n"), "after\n");
        let content = preview
            .get("content")
            .and_then(Value::as_str)
            .expect("changed preview should contain diff content");

        assert_eq!(content, strip_ansi(content));
        assert!(content.contains("-before"));
        assert!(content.contains("+after"));
    }
}