vtcode-core 0.177.1

Core library for VT Code - a Rust-based terminal coding agent
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
use super::{
    BoundedRecordRead, CODE_SEARCH_OUTLINE_BYTE_CAP, CODE_SEARCH_OUTLINE_FIXED_ARGS,
    CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE, arg_bytes, next_outline_path_batch_with_cap, read_bounded_record,
    search_declarations_bounded, smart_case_eq,
};
use crate::tools::ast_grep_binary::set_ast_grep_binary_override_for_tests as set_read_only_ast_grep_override_for_tests;
use crate::tools::ast_grep_language::AstGrepLanguage;
use serial_test::serial;
use std::{fs, path::PathBuf};
use tempfile::TempDir;

/// Write a fake `ast-grep` executable that ignores its args and writes the
/// given body to stdout, exiting 0. The body is staged in a sidecar file so
/// real newlines are preserved (embedding it via `printf` mangles NDJSON).
fn write_fake_sg(stdout_body: &str) -> (TempDir, PathBuf) {
    let script_dir = TempDir::new().expect("script tempdir");
    let output_path = script_dir.path().join("output.txt");
    fs::write(&output_path, stdout_body).expect("write output");
    let script_path = script_dir.path().join("sg");
    let script = format!("#!/bin/sh\ncat {:?}\n", output_path.as_os_str());
    fs::write(&script_path, script).expect("write fake sg");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).expect("metadata").permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).expect("chmod");
    }
    (script_dir, script_path)
}

const TWO_FILE_STREAM: &str = r#"{"path":"src/lib.rs","language":"Rust","items":[{"role":"item","symbolType":"function","name":"alpha","range":{"byteOffset":{"start":0,"end":10},"start":{"line":0,"column":0},"end":{"line":0,"column":10}},"signature":"pub fn alpha()","astKind":"function_item","isImport":false,"isExported":true,"members":[]},{"role":"item","symbolType":"struct","name":"Foo","range":{"byteOffset":{"start":11,"end":40},"start":{"line":1,"column":0},"end":{"line":3,"column":1}},"signature":"pub struct Foo","astKind":"struct_item","isImport":false,"isExported":true,"members":[{"role":"member","symbolType":"field","name":"x","range":{"byteOffset":{"start":22,"end":28},"start":{"line":2,"column":4},"end":{"line":2,"column":10}},"signature":"","astKind":"field_definition","isPublic":true}]}]}
{"path":"src/util.rs","language":"Rust","items":[{"role":"item","symbolType":"function","name":"beta","range":{"byteOffset":{"start":0,"end":12},"start":{"line":0,"column":0},"end":{"line":0,"column":12}},"signature":"fn beta()","astKind":"function_item","isImport":false,"isExported":false,"members":[]}]}
"#;

fn workspace_with_source() -> TempDir {
    let temp = TempDir::new().expect("workspace tempdir");
    let src_dir = temp.path().join("src");
    fs::create_dir_all(&src_dir).expect("create src dir");
    fs::write(src_dir.join("lib.rs"), "pub fn alpha() {}\n").expect("write lib.rs");
    fs::write(src_dir.join("util.rs"), "fn beta() {}\n").expect("write util.rs");
    temp
}

fn write_arg_order_fake_sg() -> (TempDir, PathBuf) {
    let script_dir = TempDir::new().expect("script tempdir");
    let script_path = script_dir.path().join("sg");
    let script = r#"#!/bin/sh
if [ "$1" = "--version" ]; then
    printf 'ast-grep 0.44.0\n'
    exit 0
fi
for path in "$@"; do
    case "$path" in
        *.rs) printf '{"path":"%s","language":"Rust","items":[{"name":"alpha","isImport":false,"range":{"byteOffset":{"start":0,"end":17}}}]}\n' "$path" ;;
    esac
done
"#;
    fs::write(&script_path, script).expect("write fake sg");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).expect("metadata").permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).expect("chmod");
    }
    (script_dir, script_path)
}

fn write_failing_second_batch_fake_sg() -> (TempDir, PathBuf) {
    let script_dir = TempDir::new().expect("script tempdir");
    let script_path = script_dir.path().join("sg");
    let failure_name = format!("{CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE:03}.rs");
    let script = format!(
        r#"#!/bin/sh
if [ "$1" = "--version" ]; then
    printf 'ast-grep 0.44.0\n'
    exit 0
fi
for path in "$@"; do
    case "$path" in
        *{failure_name}) exit 1 ;;
        *.rs) printf '{{"path":"%s","language":"Rust","items":[]}}\n' "$path" ;;
    esac
done
"#
    );
    fs::write(&script_path, script).expect("write fake sg");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).expect("metadata").permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).expect("chmod");
    }
    (script_dir, script_path)
}

fn workspace_with_numbered_sources(count: usize) -> (TempDir, PathBuf) {
    let temp = TempDir::new().expect("workspace tempdir");
    let src = temp.path().join("src");
    fs::create_dir_all(&src).expect("src directory");
    for index in 0..count {
        fs::write(src.join(format!("{index:03}.rs")), "pub fn alpha() {}\n").expect("source fixture");
    }
    (temp, src)
}

#[tokio::test]
#[serial]
async fn code_search_declaration_stream_reaps_at_candidate_cap() {
    let (_script_dir, script_path) = write_fake_sg(TWO_FILE_STREAM);
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let temp = workspace_with_source();

    let outcome =
        search_declarations_bounded(temp.path(), &temp.path().join("src"), "alpha", &[AstGrepLanguage::Rust], 1)
            .await
            .expect("candidate-bounded declaration stream");

    assert_eq!(outcome.files.len(), 1);
    assert_eq!(outcome.files[0].declarations.len(), 1);
    assert!(outcome.files[0].complete);
    assert!(!outcome.stream_complete);
    assert!(outcome.truncated);
}

#[tokio::test]
#[serial]
async fn code_search_declaration_candidate_cap_selects_stable_path_prefix() {
    let (_script_dir, script_path) = write_arg_order_fake_sg();
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let temp = TempDir::new().expect("workspace tempdir");
    let src = temp.path().join("src");
    fs::create_dir_all(&src).expect("src directory");
    for name in ["z.rs", "a.rs", "m.rs"] {
        fs::write(src.join(name), "pub fn alpha() {}\n").expect("source fixture");
    }

    let first = search_declarations_bounded(temp.path(), &src, "alpha", &[AstGrepLanguage::Rust], 1)
        .await
        .expect("first bounded declaration search");
    let second = search_declarations_bounded(temp.path(), &src, "alpha", &[AstGrepLanguage::Rust], 1)
        .await
        .expect("second bounded declaration search");

    assert_eq!(first, second);
    assert!(first.truncated);
    assert_eq!(first.files.len(), 1);
    assert!(first.files[0].path.ends_with("a.rs"), "{first:?}");
}

#[tokio::test]
#[serial]
async fn code_search_declaration_batches_preserve_stable_global_prefix() {
    let (_script_dir, script_path) = write_arg_order_fake_sg();
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let file_count = CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE + 2;
    let candidate_cap = CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE + 1;
    let (temp, src) = workspace_with_numbered_sources(file_count);

    let outcome = search_declarations_bounded(temp.path(), &src, "alpha", &[AstGrepLanguage::Rust], candidate_cap)
        .await
        .expect("multi-batch declaration search");

    assert_eq!(outcome.files.len(), candidate_cap);
    assert!(outcome.truncated);
    assert!(!outcome.stream_complete);
    for (index, file) in outcome.files.iter().enumerate() {
        assert!(file.path.ends_with(format!("{index:03}.rs")), "unexpected global prefix at {index}: {file:?}");
    }
}

#[tokio::test]
#[serial]
async fn code_search_declaration_batches_keep_complete_file_inventory() {
    let (_script_dir, script_path) = write_arg_order_fake_sg();
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let file_count = CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE + 2;
    let (temp, src) = workspace_with_numbered_sources(file_count);

    let outcome = search_declarations_bounded(temp.path(), &src, "missing", &[AstGrepLanguage::Rust], 1)
        .await
        .expect("complete multi-batch declaration search");

    assert_eq!(outcome.files.len(), file_count);
    assert!(outcome.files.iter().all(|file| file.complete));
    assert!(outcome.stream_complete);
    assert!(!outcome.truncated);
}

#[tokio::test]
#[serial]
async fn code_search_declaration_batch_failure_fails_the_component() {
    let (_script_dir, script_path) = write_failing_second_batch_fake_sg();
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let (temp, src) = workspace_with_numbered_sources(CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE + 1);

    let error = search_declarations_bounded(temp.path(), &src, "missing", &[AstGrepLanguage::Rust], 1)
        .await
        .expect_err("failed batch must fail declaration discovery");

    assert!(error.to_string().contains("definition search failed"));
}

fn fixed_outline_arg_bytes(executable: &std::path::Path) -> usize {
    super::arg_os_bytes(executable.as_os_str())
        + CODE_SEARCH_OUTLINE_FIXED_ARGS.iter().map(|arg| arg_bytes(arg)).sum::<usize>()
}

#[test]
fn code_search_outline_path_batches_roll_over_at_byte_cap() {
    let executable = std::path::Path::new("ast-grep");
    let first = "a.rs".to_owned();
    let second = "longer.rs".to_owned();
    let cap = fixed_outline_arg_bytes(executable) + arg_bytes(&first) + arg_bytes(&second) - 1;
    let mut paths = vec![first.clone(), second.clone()].into_iter().peekable();

    assert_eq!(next_outline_path_batch_with_cap(executable, &mut paths, cap).expect("first batch"), vec![first]);
    assert_eq!(next_outline_path_batch_with_cap(executable, &mut paths, cap).expect("second batch"), vec![second]);
}

#[test]
fn code_search_outline_path_batch_counts_executable_and_nul() {
    let executable = std::path::Path::new("/long/path/to/ast-grep");
    let path = "a.rs".to_owned();
    let cap_without_executable =
        CODE_SEARCH_OUTLINE_FIXED_ARGS.iter().map(|arg| arg_bytes(arg)).sum::<usize>() + arg_bytes(&path);
    let mut paths = vec![path.clone()].into_iter().peekable();

    let error = next_outline_path_batch_with_cap(executable, &mut paths, cap_without_executable)
        .expect_err("executable bytes must count towards the argv cap");

    assert!(error.to_string().contains("command argument byte limit"));
    assert_eq!(paths.next(), Some(path));
}

#[test]
fn code_search_outline_path_batch_preserves_input_order() {
    let executable = std::path::Path::new("ast-grep");
    let expected = vec!["c.rs".to_owned(), "a.rs".to_owned(), "b.rs".to_owned()];
    let cap = fixed_outline_arg_bytes(executable) + expected.iter().map(|path| arg_bytes(path)).sum::<usize>();
    let mut paths = expected.clone().into_iter().peekable();

    let actual = next_outline_path_batch_with_cap(executable, &mut paths, cap).expect("ordered path batch");

    assert_eq!(actual, expected);
}

#[test]
fn code_search_outline_path_batches_do_not_drop_paths() {
    let executable = std::path::Path::new("ast-grep");
    let expected = (0..(CODE_SEARCH_OUTLINE_PATH_BATCH_SIZE + 3))
        .map(|index| format!("{index:03}.rs"))
        .collect::<Vec<_>>();
    let cap = fixed_outline_arg_bytes(executable) + arg_bytes(&expected[0]) * 3;
    let mut paths = expected.clone().into_iter().peekable();
    let mut actual = Vec::new();

    while paths.peek().is_some() {
        actual.extend(next_outline_path_batch_with_cap(executable, &mut paths, cap).expect("bounded path batch"));
    }

    assert_eq!(actual, expected);
}

#[test]
fn code_search_outline_oversized_single_path_fails_without_consuming_it() {
    let executable = std::path::Path::new("ast-grep");
    let oversized = "oversized.rs".to_owned();
    let cap = fixed_outline_arg_bytes(executable) + arg_bytes(&oversized) - 1;
    let mut paths = vec![oversized.clone()].into_iter().peekable();

    let error = next_outline_path_batch_with_cap(executable, &mut paths, cap)
        .expect_err("oversized path must fail the definition component");

    assert!(error.to_string().contains("command argument byte limit"));
    assert_eq!(paths.next(), Some(oversized));
}

#[tokio::test]
#[serial]
async fn code_search_declaration_stream_reaps_at_byte_cap() {
    let oversized_record = format!("{}\n", "x".repeat(CODE_SEARCH_OUTLINE_BYTE_CAP + 1));
    let (_script_dir, script_path) = write_fake_sg(&oversized_record);
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let temp = workspace_with_source();

    let outcome =
        search_declarations_bounded(temp.path(), &temp.path().join("src"), "alpha", &[AstGrepLanguage::Rust], 20)
            .await
            .expect("byte-bounded declaration stream");

    assert!(outcome.files.is_empty(), "{outcome:?}");
    assert!(!outcome.stream_complete);
    assert!(outcome.truncated);
}

#[tokio::test]
async fn code_search_outline_bounded_record_distinguishes_exact_eof_from_exhaustion() {
    use tokio::io::BufReader;

    let mut exact_reader = BufReader::new(&b"{}\n"[..]);
    let mut exact_record = Vec::with_capacity(3);
    let mut exact_bytes_read = 0;
    assert_eq!(
        read_bounded_record(&mut exact_reader, &mut exact_record, &mut exact_bytes_read, 3,)
            .await
            .expect("exact record"),
        BoundedRecordRead::Record
    );
    exact_record.clear();
    assert_eq!(
        read_bounded_record(&mut exact_reader, &mut exact_record, &mut exact_bytes_read, 3,)
            .await
            .expect("exact EOF probe"),
        BoundedRecordRead::Eof
    );

    let mut oversized_reader = BufReader::new(&b"xxxx"[..]);
    let mut oversized_record = Vec::with_capacity(3);
    let mut oversized_bytes_read = 0;
    assert_eq!(
        read_bounded_record(&mut oversized_reader, &mut oversized_record, &mut oversized_bytes_read, 3,)
            .await
            .expect("oversized record"),
        BoundedRecordRead::Exhausted
    );
    assert_eq!(oversized_record.len(), 3);
}

#[test]
fn code_search_definition_smart_case_supports_unicode_lowercase_queries() {
    assert!(smart_case_eq("Éclair", "éclair"));
    assert!(!smart_case_eq("éclair", "Éclair"));
}

#[cfg(unix)]
#[tokio::test]
#[serial]
async fn declaration_backend_terminates_owned_child_on_cancel_error_and_truncation() {
    use std::process::{Command, Stdio};
    use std::time::Duration;

    for mode in ["cancel", "malformed", "truncated", "failed"] {
        let output = if mode == "malformed" {
            "invalid json\n"
        } else {
            TWO_FILE_STREAM
        };
        let (_script_dir, script_path) = write_fake_sg(output);
        let script = format!(
            "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then\n    printf 'ast-grep 0.44.0\\n'\n    exit 0\nfi\nprintf '%s\\n' \"$$\" > \"$0.pid\"\n{}{}\n",
            if matches!(mode, "cancel" | "failed") {
                ""
            } else {
                "cat \"${0%/*}/output.txt\"\n"
            },
            if mode == "failed" { "exit 7" } else { "exec sleep 30" },
        );
        fs::write(&script_path, script).unwrap();
        let pid_path = script_path.with_file_name("sg.pid");
        let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
        let workspace = workspace_with_source();
        let source_path = workspace.path().join("src");
        let mut search =
            Box::pin(search_declarations_bounded(workspace.path(), &source_path, "alpha", &[AstGrepLanguage::Rust], 1));
        let read_pid = async {
            tokio::time::timeout(Duration::from_secs(3), async {
                loop {
                    if let Ok(pid) = tokio::fs::read_to_string(&pid_path).await
                        && !pid.trim().is_empty()
                    {
                        break pid.trim().to_owned();
                    }
                    tokio::time::sleep(Duration::from_millis(10)).await;
                }
            })
            .await
            .expect("definition child starts")
        };
        let (pid, result) = if mode == "cancel" {
            let pid = tokio::select! {
                result = &mut search => panic!("search completed before cancellation: {result:?}"),
                pid = read_pid => pid,
            };
            drop(search);
            (pid, None)
        } else {
            let result = tokio::time::timeout(Duration::from_secs(3), &mut search)
                .await
                .expect("completed stream returns promptly");
            drop(search);
            (read_pid.await, Some(result))
        };
        let terminated = tokio::time::timeout(Duration::from_secs(3), async {
            while Command::new("kill")
                .args(["-0", &pid])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .unwrap()
                .success()
            {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await;
        if terminated.is_err() {
            // Clean up the controlled fixture even when the regression fails.
            let _ = Command::new("kill").args(["-KILL", &pid]).status();
        }
        assert!(terminated.is_ok(), "{mode}: the owned declaration child must terminate");
        if let Some(result) = result {
            if mode == "malformed" {
                assert!(result.unwrap_err().to_string().contains("failed to parse definition stream"));
            } else if mode == "failed" {
                let error = result.unwrap_err().to_string();
                assert!(error.contains("definition search failed"));
                assert!(error.contains("exit status: 7"));
            } else {
                let outcome = result.unwrap();
                assert!(outcome.truncated);
                assert!(!outcome.stream_complete);
                assert_eq!(outcome.files[0].declarations.len(), 1);
            }
        }
    }
}

#[cfg(unix)]
#[tokio::test]
#[serial]
async fn declaration_backend_executes_outline_without_a_version_probe() {
    let (_script_dir, script_path) = write_fake_sg(TWO_FILE_STREAM);
    fs::write(
        &script_path,
        r#"#!/bin/sh
if [ "$1" = "--version" ]; then
    printf 'unexpected probe\n' > "$0.probed"
    exit 7
fi
cat "${0%/*}/output.txt"
"#,
    )
    .unwrap();
    let probe_path = script_path.with_file_name("sg.probed");
    let _override = set_read_only_ast_grep_override_for_tests(Some(script_path));
    let workspace = workspace_with_source();
    let outcome = search_declarations_bounded(
        workspace.path(),
        &workspace.path().join("src"),
        "alpha",
        &[AstGrepLanguage::Rust],
        5,
    )
    .await
    .expect("outline execution is the availability check");
    assert!(!probe_path.exists(), "search must not spawn a synchronous metadata probe");
    assert_eq!(outcome.files.len(), 2);
    assert_eq!(outcome.files[0].declarations[0].name, "alpha");
    assert!(outcome.stream_complete);
    assert!(!outcome.truncated);
}