vtcode-core 0.181.9

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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use crate::tools::command_args::{
    command_words_after_environment_prefix, has_unsafe_readonly_options, shell_command_text_with_args,
};
use crate::tools::output_spooler::SpooledOutputReference;
use serde_json::Value;
use std::path::Path;

/// Conservative allow-list of read-only inspection commands used by
/// `command_session`. Any command that could write, move, or delete must be
/// rejected so it is not cached or parallelized as read-only.
///
/// `cd` is included because changing the working directory mutates nothing;
/// models habitually prefix exploration with `cd <workspace> && …` and plan
/// mode must not reject that pattern (checkpoint turn_810).
const READONLY_UNIFIED_EXEC_COMMANDS: &[&str] = &[
    "rg", "ls", "cat", "diff", "find", "wc", "grep", "egrep", "fgrep", "head", "tail", "sort", "uniq", "bat", "sed",
    "awk", "cut", "tr", "ast-grep", "sg", "echo", "pwd", "printf", "true", "false", "test", "cd", "fd", "tree",
    "which", "stat", "file", "du", "df", "realpath", "basename", "dirname", "nl", "column", "jq", "date", "whoami",
    "uname",
];

pub fn is_readonly_base_command(command: &str) -> bool {
    READONLY_UNIFIED_EXEC_COMMANDS.contains(&command)
}

/// Read-only subcommand allow-list for multi-word tools whose base command is
/// not inherently safe (`git`, `cargo`, package managers). Only inspection
/// subcommands are listed; anything that can mutate the worktree, index,
/// refs, or lockfiles must stay out.
///
/// Unconditionally read-only: every form of these subcommands is an
/// inspection, so this list is also safe for the flag-skipping parallel-safe
/// extractor. Flag-shaped subcommands whose bare form reads but whose operand
/// forms mutate live in [`is_readonly_flag_shaped_git_subcommand`].
fn is_readonly_subcommand(first: &str, second: Option<&str>, third: Option<&str>) -> bool {
    match first {
        "git" => matches!(
            second,
            Some(
                "status"
                    | "log"
                    | "diff"
                    | "show"
                    | "blame"
                    | "ls-files"
                    | "rev-parse"
                    | "describe"
                    | "shortlog"
                    | "grep"
                    | "rev-list"
                    | "ls-tree"
                    | "cat-file"
                    | "diff-tree"
                    | "merge-base"
                    | "range-diff"
                    | "whatchanged"
                    | "count-objects"
                    | "var"
                    | "version"
                    | "help"
            )
        ),
        "cargo" => match second {
            Some("check" | "test" | "metadata" | "tree" | "clippy") => true,
            Some("nextest") => matches!(third, Some("run" | "list")),
            _ => false,
        },
        "npm" | "pnpm" | "yarn" => match second {
            Some("test") => true,
            Some("run") => matches!(third, Some("test")),
            _ => false,
        },
        _ => false,
    }
}

/// Flag-shaped git subcommands whose bare or flag-only form is a pure read
/// (`git tag`, `git branch -a`, `git remote -v`, `git reflog`, `git stash
/// list`, `git worktree list`), while the same name with an operand mutates
/// (`tag v1`, `branch -D x`, `reflog expire`, `stash pop`, `remote add`,
/// `worktree add`). The mutating operand survives as the third word, so this
/// classification is position-safe only on the read-only path, which keeps
/// operands; the flag-skipping parallel-safe extractor must never consult it
/// (there `git tag -f x` would look identical to `git tag`).
///
/// Two hardening rules keep the bare/flag-only forms honest:
/// - bare `stash` is excluded: bare `git stash` is `git stash push`, which
///   stashes uncommitted work, and flag-only push forms (`-k`, `-u`, `-a`,
///   `-m=msg`) carry no operand to betray them;
/// - `branch` carries a mutating-flag denylist, because inline-value flags
///   (`--set-upstream-to=origin/x`, `--unset-upstream`, `--edit-description`)
///   and glued short mutators (`-m<name>`, `-c<name>`, `-u<upstream>`) start
///   with `-` and are invisible to the operand finder.
fn is_readonly_flag_shaped_git_subcommand(
    subcommand: Option<&str>,
    operand: Option<&str>,
    command_words: &[String],
    sub_index: usize,
) -> bool {
    if subcommand == Some("branch") {
        let mutates = command_words.iter().skip(sub_index + 1).any(|word| {
            matches!(word.as_str(), "--set-upstream" | "--unset-upstream" | "--edit-description")
                || word.starts_with("--set-upstream-to")
                || word.starts_with("-u")
                || word.starts_with("-m")
                || word.starts_with("-M")
                || word.starts_with("-c")
                || word.starts_with("-C")
        });
        if mutates {
            return false;
        }
    }

    matches!(
        (subcommand, operand),
        (Some("tag") | Some("branch"), None)
            | (Some("reflog"), None | Some("show"))
            | (Some("remote"), None | Some("show") | Some("get-url"))
            | (Some("stash"), Some("list") | Some("show"))
            | (Some("worktree"), Some("list"))
    )
}

/// A version or help probe prints to stdout and mutates nothing for any
/// program, including subcommand-based tools (`cargo --version`, `node
/// --version`, `python3 -V`) that the subcommand allow-list cannot classify
/// because every word after the program is a flag. At least one probe flag
/// is required (a bare program name like `tsc` still compiles) and ALL
/// trailing words must be probe flags, so `python3 script.py` or `git commit
/// -v` stay outside. Lowercase `-v` is deliberately excluded: it means
/// verbose rather than version for most tools, and for shell interpreters
/// (`bash -v`) it reads stdin.
fn is_version_or_help_probe(command_words: &[String]) -> bool {
    fn is_probe_flag(word: &str) -> bool {
        matches!(word, "-h" | "--help" | "-V" | "--version")
    }

    let mut flags = command_words.iter().skip(1);
    flags.next().is_some_and(|word| is_probe_flag(word)) && flags.all(|word| is_probe_flag(word))
}

/// Parse a shell script into simple command words after proving that it has no
/// dynamic expansion, file redirection, or unsupported background operator.
///
/// The tree-sitter parser is the single shell-grammar boundary used by the
/// safety subsystem. Callers receive already-tokenized command words and must
/// still apply their own command-policy predicate; an unknown command never
/// becomes read-only merely because parsing succeeded.
pub(crate) fn static_shell_command_words(command: &str) -> Option<Vec<Vec<String>>> {
    let sanitized = sanitize_static_shell_command(command)?;
    parse_shell_command_words(&sanitized)
}

/// Parse a command whose only shell operators outside quotes are output
/// redirections. This is intentionally separate from the read-only parser:
/// writing a build log is still a mutating command for the authoritative tool
/// intent, but it should not hide the verification nature of `cargo check` in
/// progress accounting.
pub(crate) fn static_shell_command_words_with_output_plumbing(command: &str) -> Option<Vec<Vec<String>>> {
    if !crate::command_safety::shell_parser::has_only_output_redirections(command) {
        return None;
    }
    parse_shell_command_words(command)
}

/// Tokenize shell invocations without proving static execution or safety.
/// Permission and activity predicates must sanitize before using these words;
/// diagnostic-only consumers may inspect dynamic shell text without grants.
pub(super) fn parse_shell_command_words(command: &str) -> Option<Vec<Vec<String>>> {
    let commands = crate::command_safety::shell_parser::parse_shell_commands_tree_sitter(command).ok()?;
    if commands.is_empty() {
        return None;
    }

    commands
        .into_iter()
        .map(|command| {
            let mut words = Vec::new();
            for word in command {
                let tokens = shell_words::split(&word).ok()?;
                words.extend(tokens);
            }
            (!words.is_empty()).then_some(words)
        })
        .collect()
}

/// Shared head of both read-only classifiers: strip environment prefixes,
/// resolve the lowercased program name, and apply the option-level guard.
/// Returns the stripped words plus the program name, or `None` when the
/// command cannot be classified (fail closed). The parallel-safe classifier
/// shares this head; its stricter tail (no builds/tests, git allow-list
/// only) stays separate on purpose.
///
/// The guard reads the original `words` (not the stripped view) so
/// environment-injected options stay visible; keep that argument as-is.
fn readonly_command_head(words: &[String]) -> Option<(&[String], String)> {
    let command_words = command_words_after_environment_prefix(words);
    let first = command_words
        .first()
        .and_then(|word| Path::new(word).file_name())
        .and_then(|name| name.to_str())
        .map(str::to_ascii_lowercase)?;
    if has_unsafe_readonly_options(words) {
        return None;
    }
    Some((command_words, first))
}

/// Returns whether one parsed command is an allow-listed read-only command.
/// Option-level guards are shared with raw argument validation so activity
/// accounting cannot accidentally become less strict than tool intent.
pub(crate) fn command_words_are_readonly(words: &[String]) -> bool {
    let Some((command_words, first)) = readonly_command_head(words) else {
        return false;
    };
    if first == "command" {
        return matches!(command_words.get(1).map(String::as_str), Some("-v" | "-V"))
            && command_words.len() > 2
            && command_words
                .iter()
                .skip(2)
                .all(|word| !word.starts_with('-') && !word.is_empty());
    }

    if is_readonly_base_command(&first)
        || is_version_or_help_probe(command_words)
        || is_known_readonly_dry_run(command_words, &first)
    {
        return true;
    }

    let lowered = command_words
        .iter()
        .filter(|word| !word.starts_with('-') && !word.contains('='))
        .take(3)
        .map(|word| word.to_ascii_lowercase())
        .collect::<Vec<_>>();
    if first != "git" {
        let (second, third) = (lowered.get(1).map(String::as_str), lowered.get(2).map(String::as_str));
        return is_readonly_subcommand(&first, second, third);
    }

    let Some((sub_index, operand)) = git_subcommand_and_operand(command_words) else {
        return false;
    };
    let subcommand = command_words.get(sub_index).map(String::as_str);
    is_readonly_subcommand("git", subcommand, None)
        || is_readonly_flag_shaped_git_subcommand(subcommand, operand, command_words, sub_index)
}

/// Locate the git subcommand and the first operand after it, skipping global
/// options — including their separate values (`-C <dir>`, `-c <config>`,
/// `--git-dir <path>`, `--shallow-file <info>`), whose inline forms
/// (`-C<dir>`, `--git-dir=<path>`) carry no extra word. Without this, the
/// option value occupies the subcommand slot and worktree-style exploration
/// (`git -C <dir> log`) is misread as an unknown subcommand.
///
/// Position-based on purpose: the operand of a flag-shaped subcommand stays
/// visible as the second tuple field, which is what keeps `git tag v1`
/// distinguishable from `git tag -l`. Fails closed on any unknown
/// global-shaped word: an unrecognized option may consume a separate value,
/// which would otherwise smuggle the real (possibly mutating) subcommand past
/// the allow-list (`git --shallow-file log commit -m x`).
///
/// Returns the subcommand's index into `command_words` (callers re-read it —
/// real git subcommands are case-sensitive, so no lowercasing happens here)
/// plus the first non-flag operand after it, or `None` when the command must
/// stay denied.
fn git_subcommand_and_operand(command_words: &[String]) -> Option<(usize, Option<&str>)> {
    const GLOBAL_OPTIONS_WITH_VALUE: &[&str] = &[
        "-C",
        "-c",
        "--git-dir",
        "--work-tree",
        "--namespace",
        "--super-prefix",
        "--exec-path",
        "--config-env",
        "--shallow-file",
        "--attr-source",
    ];
    const GLOBAL_OPTIONS_FLAG_ONLY: &[&str] = &[
        "-p",
        "--paginate",
        "--no-pager",
        "--bare",
        "--literal-pathspecs",
        "--no-replace-objects",
        "--no-optional-locks",
        "--end-of-options",
        "--version",
        "--help",
        "--html-path",
    ];

    let mut index = 1;
    while let Some(word) = command_words.get(index) {
        if !word.starts_with('-') {
            let operand = command_words
                .iter()
                .skip(index + 1)
                .find(|candidate| !candidate.starts_with('-'))
                .map(String::as_str);
            return Some((index, operand));
        }
        if GLOBAL_OPTIONS_WITH_VALUE.contains(&word.as_str()) {
            index += 2; // skip the option and its separate value
            continue;
        }
        if GLOBAL_OPTIONS_FLAG_ONLY.contains(&word.as_str()) {
            index += 1;
            continue;
        }
        // Unknown global-shaped word: an option this allow-list does not know
        // may take a separate value, so the next word could be that value
        // rather than the subcommand. Fail closed.
        return None;
    }
    None
}

/// Return whether one command is a side-effect-free inspection suitable for
/// concurrent execution. Build and test commands remain read-only for policy
/// purposes, but are excluded here because they write caches, lockfiles, or
/// build artifacts and contend on shared package-manager state.
fn command_words_are_parallel_safe(words: &[String]) -> bool {
    let Some((command_words, first)) = readonly_command_head(words) else {
        return false;
    };

    if is_readonly_base_command(&first) {
        return true;
    }
    // Version/help probes are side-effect-free for any program.
    if is_version_or_help_probe(command_words) {
        return true;
    }
    if first != "git" {
        return false;
    }

    let subcommand = command_words
        .iter()
        .skip(1)
        .find(|word| !word.starts_with('-') && !word.contains('='))
        .map(|word| word.to_ascii_lowercase());
    is_readonly_subcommand("git", subcommand.as_deref(), None)
}

fn is_known_readonly_dry_run(words: &[String], program: &str) -> bool {
    if !matches!(program, "npm" | "pnpm" | "yarn") {
        return false;
    }

    let Some(subcommand) = words.iter().skip(1).find(|word| !word.starts_with('-') && !word.contains('=')) else {
        return false;
    };

    subcommand == "install" && words.iter().any(|word| word == "--dry-run")
}

fn matches_at(chars: &[char], index: usize, pattern: &[char]) -> bool {
    chars
        .get(index..index.saturating_add(pattern.len()))
        .is_some_and(|candidate| candidate == pattern)
}

/// Remove only stderr plumbing that cannot write workspace state and reject
/// all other redirection/background syntax. Operators inside quoted arguments
/// remain ordinary data and are never rewritten.
fn sanitize_static_shell_command(command: &str) -> Option<String> {
    if crate::command_safety::shell_parser::contains_dynamic_shell_syntax(command) {
        return None;
    }

    let chars = command.chars().collect::<Vec<_>>();
    let mut sanitized = String::with_capacity(command.len());
    let mut index = 0;
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    while index < chars.len() {
        let character = chars[index];
        if character == '\'' && !in_double_quote {
            in_single_quote = !in_single_quote;
            sanitized.push(character);
            index += 1;
            continue;
        }
        if character == '"' && !in_single_quote {
            in_double_quote = !in_double_quote;
            sanitized.push(character);
            index += 1;
            continue;
        }
        if in_single_quote || in_double_quote {
            sanitized.push(character);
            index += 1;
            continue;
        }

        let preceded_by_boundary = index == 0
            || chars
                .get(index.saturating_sub(1))
                .is_some_and(|previous| previous.is_whitespace() || matches!(previous, '|' | '&' | ';'));
        if preceded_by_boundary && matches_at(&chars, index, &['2', '>', '&', '1']) {
            index += 4;
            continue;
        }
        if cfg!(unix)
            && preceded_by_boundary
            && matches_at(&chars, index, &['2', '>', '/', 'd', 'e', 'v', '/', 'n', 'u', 'l', 'l'])
        {
            index += 11;
            continue;
        }

        match character {
            '<' | '>' => return None,
            '&' if matches_at(&chars, index, &['&', '&']) => {
                sanitized.push('&');
                sanitized.push('&');
                index += 2;
            }
            '&' => return None,
            _ => {
                sanitized.push(character);
                index += 1;
            }
        }
    }

    (!sanitized.trim().is_empty()).then_some(sanitized)
}

/// Shared extract + static-parse prologue for command-session classifiers.
///
/// Extracts the full classification candidate (including the quoted argv
/// suffix) and proves a static shape through the tree-sitter boundary.
/// Returns `None` when the args cannot prove one invocation (conflicting raw
/// alias, dynamic syntax, redirection) — every caller maps `None` to
/// fail-closed `false`, in the same order as before.
fn parsed_session_commands(args: &Value) -> Option<Vec<Vec<String>>> {
    let raw = shell_command_text_with_args(args)?;
    static_shell_command_words(&raw)
}

/// Shared read-only gate over already-parsed segments: a `&&` chain is
/// accepted only when every segment independently passes the allow-list.
/// (`sanitize_static_shell_command` already rejected `;`, `||`,
/// redirections, and background `&` up front.)
fn all_commands_readonly(commands: &[Vec<String>]) -> bool {
    commands.iter().all(|words| command_words_are_readonly(words))
}

pub fn is_readonly_command_session_command(args: &Value) -> bool {
    let Some(commands) = parsed_session_commands(args) else {
        return false;
    };

    all_commands_readonly(&commands)
}

/// A stricter read-only command shape that may run concurrently after hooks,
/// command admission, and preflight have all succeeded.
pub(crate) fn is_parallel_safe_command_session_command(args: &Value) -> bool {
    if args.get("session_id").is_some()
        || args.get("chars").is_some()
        || args.get("input").is_some()
        || args.get("tty").and_then(Value::as_bool) == Some(true)
        || args
            .get("sandbox_permissions")
            .and_then(Value::as_str)
            .is_some_and(|value| value != "use_default")
    {
        return false;
    }
    let Some(commands) = parsed_session_commands(args) else {
        return false;
    };
    commands.len() == 1 && command_words_are_parallel_safe(&commands[0])
}

/// Returns `true` when a safe shell inspection command reads the internal tool
/// output spool directory. Such reads must stay inline: spooling their output
/// again would create a recursive chain of spool references.
pub fn is_spool_file_read_command(tool_name: &str, args: &Value) -> bool {
    if super::classify::canonical_command_session_tool_name(tool_name).is_none() {
        return false;
    }

    // Parse once through the shared prologue: the same segments feed both
    // the read-only gate below and the spool-path scan, so a completed
    // command output (see `prepare_execution_output`) pays the extract +
    // sanitize + tree-sitter pipeline a single time.
    let Some(commands) = parsed_session_commands(args) else {
        return false;
    };
    if !all_commands_readonly(&commands) {
        return false;
    }
    commands
        .iter()
        .flatten()
        .any(|word| SpooledOutputReference::recognizes_path(Path::new(word)))
}

#[cfg(test)]
mod tests;