vtcode 0.169.4

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
mod runtime;
mod segments;
mod state;

pub(crate) use runtime::PtyStreamRuntime;

#[cfg(test)]
mod tests {
    use anstyle::{AnsiColor, Color as AnsiColorEnum};
    use std::sync::Arc;
    use std::sync::atomic::Ordering;
    use std::time::Duration;
    use tokio::sync::{mpsc, oneshot};
    use tokio::time::timeout;
    use vtcode_core::config::PtyConfig;
    use vtcode_ui::tui::app::{InlineCommand, InlineHandle, InlineSegment};

    use super::runtime::PtyStreamRuntime;
    use super::segments::{PtyLineStyles, line_to_segments, tokenize_preserve_whitespace};
    use super::state::PtyStreamState;
    use crate::agent::runloop::unified::progress::ProgressReporter;

    struct DropNotifier(Option<oneshot::Sender<()>>);

    impl Drop for DropNotifier {
        fn drop(&mut self) {
            if let Some(tx) = self.0.take() {
                let _ = tx.send(());
            }
        }
    }

    fn flatten_text(segments: &[InlineSegment]) -> String {
        segments
            .iter()
            .map(|segment| segment.text.as_str())
            .collect::<Vec<_>>()
            .join("")
    }

    fn test_pty_config() -> PtyConfig {
        PtyConfig::default()
    }

    #[test]
    fn pty_stream_state_streams_incremental_chunks() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("line1\nline2", 5);
        let rendered = state.render_lines(5);
        assert_eq!(rendered, vec!["  └ line1".to_string(), "    line2".to_string()]);
        assert_eq!(state.last_display_line(5), Some("line2".to_string()));
    }

    #[test]
    fn pty_stream_state_handles_carriage_return_overwrite() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("start\rreplace\n", 5);
        let rendered = state.render_lines(5);
        assert_eq!(rendered, vec!["  └ replace".to_string()]);
        assert_eq!(state.last_display_line(5), Some("replace".to_string()));
    }

    #[test]
    fn pty_stream_state_applies_tail_truncation() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("a\nb\nc\nd\ne\nf\ng\n", 5);
        let rendered = state.render_lines(5);
        assert_eq!(
            rendered,
            vec![
                "  └ a".to_string(),
                "    b".to_string(),
                "    … +3 lines".to_string(),
                "    f".to_string(),
                "    g".to_string(),
            ]
        );
    }

    #[test]
    fn pty_stream_state_formats_hidden_line_summary() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("a\nb\nc\nd\ne\nf\ng\nh\n", 5);
        let rendered = state.render_lines(5);
        assert!(rendered.contains(&"    … +4 lines".to_string()));
    }

    #[test]
    fn pty_stream_state_preserves_consecutive_duplicate_lines() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("same\nsame\nnext\n", 5);
        let rendered = state.render_lines(5);
        assert_eq!(rendered, vec!["  └ same".to_string(), "    same".to_string(), "    next".to_string(),]);
    }

    #[test]
    fn pty_stream_state_preserves_indentation_and_blank_lines() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("  fn main() {\n\n    println!(\"hi\");\n  }\n", 8);
        let rendered = state.render_lines(8);
        assert_eq!(
            rendered,
            vec![
                "  └   fn main() {".to_string(),
                "    ".to_string(),
                "        println!(\"hi\");".to_string(),
                "      }".to_string(),
            ]
        );
    }

    #[test]
    fn pty_stream_state_renders_command_prompt_without_output() {
        let state = PtyStreamState::new(Some("cargo check".to_string()), test_pty_config(), None);
        let rendered = state.render_lines(5);
        assert_eq!(rendered, vec!["• Ran cargo check".to_string()]);
    }

    #[test]
    fn pty_stream_state_uses_bounded_live_preview() {
        let mut state = PtyStreamState::new(Some("cargo check".to_string()), test_pty_config(), None);
        state.apply_chunk("first\nsecond\nthird\n", 2);

        assert_eq!(
            state.render_lines(2),
            vec![
                "• Ran cargo check".to_string(),
                "    … +1 line".to_string(),
                "  └ second".to_string(),
                "    third".to_string(),
            ]
        );
    }

    #[test]
    fn pty_stream_state_keeps_command_prompt_with_truncated_tail() {
        let mut state = PtyStreamState::new(Some("cargo check".to_string()), test_pty_config(), None);
        state.apply_chunk("a\nb\nc\nd\ne\nf\ng\n", 5);
        let rendered = state.render_lines(5);
        assert_eq!(
            rendered,
            vec![
                "• Ran cargo check".to_string(),
                "  └ a".to_string(),
                "    b".to_string(),
                "    … +3 lines".to_string(),
                "    f".to_string(),
                "    g".to_string(),
            ]
        );
    }

    #[test]
    fn normalizes_command_prompt_whitespace() {
        let state = PtyStreamState::new(Some("  cargo   check \n -p  vtcode  ".to_string()), test_pty_config(), None);
        let rendered = state.render_lines(5);
        assert_eq!(rendered, vec!["• Ran cargo check -p vtcode".to_string()]);
    }

    #[test]
    fn wraps_long_command_header() {
        let command = "cargo test -p vtcode run_command_preview_ build_tool_summary_formats_run_command_as_ran";
        let state = PtyStreamState::new(Some(command.to_string()), test_pty_config(), None);
        let rendered = state.render_lines(5);
        assert_eq!(rendered.len(), 2);
        assert!(rendered[0].starts_with("• Ran cargo test -p vtcode run_command_preview_"));
        assert!(rendered[1].starts_with("  │ build_tool_summary_formats_run_command_as_ran"));
    }

    #[test]
    fn screenshot_grep_pipeline_header_renders_in_full_without_truncation() {
        // Screenshot 2026-09-24 16:37: `• Ran grep -rn "@vinhnx/..." docs`
        // wrapped across `│` lines must keep every pipe segment with no `…`.
        // Exact screenshot bytes: `||` inside the quoted pattern and the
        // backslash-escaped `\.backup` arg must both survive (3 pattern pipes
        // + 3 shell pipes = 6).
        let command = "grep -rn \"@vinhnx/vtcode|npm install -g||npx @vinhnx\" docs | grep -v node_modules | grep -v package-lock | grep -v \"\\.backup\"";
        assert!(command.chars().count() > 120, "fixture must overflow the old preview cap");
        let state = PtyStreamState::new(Some(command.to_string()), test_pty_config(), None);
        let rendered = state.render_lines(8);
        let joined = rendered.join("\n");
        assert!(!joined.contains('…'), "command header must not truncate, got: {joined:?}");
        assert!(joined.contains("node_modules"), "got: {joined:?}");
        assert!(joined.contains("package-lock"), "got: {joined:?}");
        assert!(joined.contains("\"\\.backup\""), "final pipe arg must survive, got: {joined:?}");
        assert_eq!(joined.matches('|').count(), 6, "pattern pipes + shell pipes must survive: {joined:?}");
        // Proper shell-aware wrapping: the quoted pattern holds spaces but
        // must stay on the first line, never split mid-quote, and every
        // segment must fit its 62/58 budget plus the `• Ran` / `  │ ` prefix.
        assert!(
            rendered[0].contains("\"@vinhnx/vtcode|npm install -g||npx @vinhnx\""),
            "quoted pattern must stay atomic on the first line, got: {rendered:?}"
        );
        for (index, line) in rendered.iter().enumerate() {
            let (body, budget) = if index == 0 {
                (line.strip_prefix("• Ran ").expect("header prefix"), 62)
            } else {
                (line.strip_prefix("  │ ").expect("continuation prefix"), 58)
            };
            assert!(body.chars().count() <= budget, "line {index} exceeds its {budget}-char budget: {line:?}");
        }
    }

    #[test]
    fn pty_stream_state_uses_terminal_snapshot_for_screen_rewrites() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("before\n\x1b[2J\x1b[Hmenu\nitem\n", 6);

        assert_eq!(state.render_lines(6), vec!["  └ menu".to_string(), "    item".to_string()]);
        assert_eq!(state.last_display_line(6), Some("item".to_string()));
    }

    #[test]
    fn tokenization_preserves_whitespace() {
        let tokens = tokenize_preserve_whitespace("cargo   check -p  vtcode");
        assert_eq!(tokens, vec!["cargo", "   ", "check", " ", "-p", "  ", "vtcode"]);
    }

    #[test]
    fn line_to_segments_preserves_command_text() {
        let styles = PtyLineStyles::new();
        let line = "• Ran echo \"$HOME\" && cargo check";
        let (segments, _) = line_to_segments(line, &styles);
        assert_eq!(flatten_text(&segments), line);
    }

    #[test]
    fn line_to_segments_distinguishes_command_and_args_styles() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("• Ran cargo fmt", &styles);
        assert_eq!(flatten_text(&segments), "• Ran cargo fmt");
        assert!(
            segments
                .iter()
                .any(|segment| !segment.text.trim().is_empty() && segment.style.color.is_some())
        );
    }

    #[test]
    fn line_to_segments_handles_invalid_bash_input_without_dropping_text() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("• Ran )(", &styles);
        assert_eq!(flatten_text(&segments), "• Ran )(");
    }

    #[test]
    fn line_to_segments_preserves_stdout_ansi_styles() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("  └ \u{1b}[31mERR\u{1b}[0m done", &styles);
        assert_eq!(flatten_text(&segments), "  └ ERR done");

        let err_segment = segments
            .iter()
            .find(|segment| segment.text.contains("ERR"))
            .expect("colored text segment should be present");
        assert_eq!(err_segment.style.color, Some(AnsiColorEnum::Ansi(AnsiColor::Red)));
    }

    #[test]
    fn line_to_segments_ignores_non_sgr_ansi_sequences_without_dropping_text() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("  └ \u{1b}[2Kclean", &styles);
        assert_eq!(flatten_text(&segments), "  └ clean");
        let clean_segment = segments
            .iter()
            .find(|segment| segment.text.contains("clean"))
            .expect("text segment should be present");
        assert_eq!(*clean_segment.style, *styles.output);
    }

    #[test]
    fn line_to_segments_stdout_uses_output_style() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("  └ cargo check done", &styles);
        let output_segment = segments
            .iter()
            .find(|segment| segment.text.contains("cargo check done"))
            .expect("stdout segment should be present");
        assert_eq!(*output_segment.style, *styles.output);
    }

    #[test]
    fn line_to_segments_continuation_line_keeps_first_token_as_arg_style() {
        let styles = PtyLineStyles::new();
        let (segments, _) = line_to_segments("  │ --flag value", &styles);
        assert_eq!(flatten_text(&segments), "  │ --flag value");
        assert!(
            segments
                .iter()
                .any(|segment| !segment.text.trim().is_empty() && segment.style.color.is_some())
        );
    }

    #[test]
    fn pty_stream_state_preserves_osc8_links_across_control_only_chunks() {
        let mut state = PtyStreamState::new(None, test_pty_config(), None);
        state.apply_chunk("\u{1b}]8;;https://example.com/docs\u{1b}\\", 5);

        let (_, segments, link_ranges, _) = state.render_segments("docs\u{1b}]8;;\u{1b}\\\n", 5);
        assert_eq!(segments.len(), 1);
        assert_eq!(flatten_text(&segments[0]), "  └ docs");
        assert_eq!(link_ranges.len(), 1);
        assert_eq!(link_ranges[0].len(), 1);
    }

    #[tokio::test]
    async fn compact_pty_runtime_does_not_emit_live_preview() {
        let (sender, mut receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(sender);
        let (runtime, callback) = PtyStreamRuntime::start(
            handle,
            Default::default(),
            8,
            Some("cargo check".to_string()),
            test_pty_config(),
            None,
            false,
        );

        callback("run_pty_cmd", "first\nsecond\n");
        runtime.shutdown(anstyle::Color::Ansi(AnsiColor::Green)).await;

        let commands = std::iter::from_fn(|| receiver.try_recv().ok()).collect::<Vec<_>>();
        assert!(
            !commands
                .iter()
                .any(|command| matches!(command, InlineCommand::ReplaceLast { .. })),
            "compact PTY execution must not emit a transient live row"
        );
    }

    #[tokio::test]
    async fn compact_pty_runtime_streams_status_line_without_transcript() {
        let (sender, mut receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(sender);
        let progress = ProgressReporter::new();
        let (runtime, callback) = PtyStreamRuntime::start(
            handle,
            progress.clone(),
            8,
            Some("cargo test".to_string()),
            test_pty_config(),
            None,
            false,
        );

        // Asymmetric pair: cargo-style progress line vs ANSI-only spinner frame.
        // Only the former must reach the status line; control sequences alone
        // must not overwrite it with blank text.
        callback("exec_command", "   Compiling vtcode-core v0.163.2\n");
        callback("exec_command", "\x1b[2K\x1b[1G");
        runtime.shutdown(anstyle::Color::Ansi(AnsiColor::Green)).await;

        let commands = std::iter::from_fn(|| receiver.try_recv().ok()).collect::<Vec<_>>();
        assert!(
            !commands
                .iter()
                .any(|command| matches!(command, InlineCommand::ReplaceLast { .. })),
            "compact mode must keep the transcript stable while streaming status"
        );

        let info = progress.progress_info().await;
        assert!(
            info.message.contains("Compiling vtcode-core"),
            "compact mode must stream live stdout to status line, got: {:?}",
            info.message
        );
    }

    #[tokio::test]
    async fn enabled_pty_runtime_emits_live_preview() {
        let (sender, mut receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(sender);
        let (runtime, callback) = PtyStreamRuntime::start(
            handle,
            Default::default(),
            8,
            Some("cargo check".to_string()),
            test_pty_config(),
            None,
            true,
        );

        callback("run_pty_cmd", "first\nsecond\n");
        runtime.shutdown(anstyle::Color::Ansi(AnsiColor::Green)).await;

        let commands = std::iter::from_fn(|| receiver.try_recv().ok()).collect::<Vec<_>>();
        assert!(
            commands
                .iter()
                .any(|command| matches!(command, InlineCommand::ReplaceLast { .. })),
            "expanded PTY execution should retain its live preview"
        );
    }

    #[tokio::test]
    async fn expanded_live_preview_never_exceeds_ten_rows() {
        let (sender, mut receiver) = mpsc::unbounded_channel();
        let handle = InlineHandle::new_for_tests(sender);
        let (runtime, callback) =
            PtyStreamRuntime::start(handle, Default::default(), 50, None, test_pty_config(), None, true);

        let chunk = (1..=15).map(|n| format!("line-{n:02}")).collect::<Vec<_>>().join("\n") + "\n";
        callback("run_pty_cmd", &chunk);
        runtime.shutdown(anstyle::Color::Ansi(AnsiColor::Green)).await;

        let commands = std::iter::from_fn(|| receiver.try_recv().ok()).collect::<Vec<_>>();
        let last_preview = commands
            .iter()
            .rev()
            .find_map(|command| match command {
                InlineCommand::ReplaceLast { lines, .. } => Some(lines),
                _ => None,
            })
            .expect("expanded execution should emit a live preview");
        assert!(
            last_preview.len() <= 10,
            "live preview must stay within the 10-row budget, got {} rows",
            last_preview.len()
        );
        let text = last_preview
            .iter()
            .map(|row| row.iter().map(|segment| segment.text.as_str()).collect::<String>())
            .collect::<Vec<_>>()
            .join("\n");
        assert!(text.contains("line-01"), "head row should survive: {text:?}");
        assert!(text.contains("line-15"), "tail row should survive: {text:?}");
        assert!(!text.contains("line-05"), "middle row should be trimmed: {text:?}");
    }

    #[tokio::test]
    async fn pty_stream_runtime_drop_aborts_background_task() {
        let (drop_tx, drop_rx) = oneshot::channel();
        let notifier = DropNotifier(Some(drop_tx));
        let task = tokio::spawn(async move {
            let _notifier = notifier;
            std::future::pending::<()>().await;
        });
        let active = Arc::new(std::sync::atomic::AtomicBool::new(true));
        let runtime = PtyStreamRuntime::for_test(task, Arc::clone(&active));

        drop(runtime);

        assert!(!active.load(Ordering::Relaxed));
        timeout(Duration::from_millis(300), drop_rx)
            .await
            .expect("background task should be aborted on drop")
            .expect("drop notifier should signal when task future is dropped");
    }
}