vtcode 0.146.0

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
use std::borrow::Cow;
use std::path::Path;

use serde_json::Value;
use vtcode_core::config::constants::tools;
use vtcode_core::tools::result_cache::ToolCacheKey;
use vtcode_core::tools::tool_intent;

use crate::agent::runloop::unified::tool_reads::spool_chunk_read_path;

/// Determine if a tool is cacheable based on tool type and arguments.
pub(super) fn is_tool_cacheable(tool_name: &str, args: &Value) -> bool {
    if is_readonly_repo_browsing_tool(tool_name, args) {
        return true;
    }

    if is_stable_tool_catalog_lookup(tool_name, args) {
        return true;
    }

    // Cache path-scoped git diff command calls to avoid redundant reruns.
    if extract_git_diff_cache_target(tool_name, args).is_some() {
        return true;
    }

    false
}

fn is_readonly_repo_browsing_tool(tool_name: &str, args: &Value) -> bool {
    // PTY and command-session output spools are append-only while the command
    // is running. Reusing a successful result for the same path would hide
    // newly available output from the agent.
    if spool_chunk_read_path(tool_name, args).is_some() {
        return false;
    }

    matches!(tool_name, tools::READ_FILE | tools::LIST_FILES | "grep_search" | "find_files")
        || tool_name == tools::CODE_SEARCH
        || (tool_name == tools::UNIFIED_FILE && tool_intent::file_operation_action_is(args, "read"))
}

fn is_stable_tool_catalog_lookup(tool_name: &str, args: &Value) -> bool {
    let _ = args;
    matches!(tool_name, "search_tools" | "get_errors" | "agent_info")
}

/// Enhanced cache key creation that includes workspace context in the target path
/// This prevents cache collisions between different workspaces
pub(super) fn create_enhanced_cache_key(
    tool_name: &str,
    args: &Value,
    cache_target: &str,
    workspace: &str,
) -> ToolCacheKey {
    // For file-based tools, include workspace in the target path to ensure uniqueness
    // For non-file tools, use a workspace-specific target path
    let enhanced_target = if cache_target == "." {
        workspace.to_string()
    } else if cache_target.starts_with('/') || cache_target.contains(':') {
        // Absolute path or special path - keep as is
        cache_target.to_string()
    } else {
        // Relative path - prefix with workspace to ensure uniqueness
        format!("{workspace}/{cache_target}")
    };

    if tool_name == tools::CODE_SEARCH
        && let Some(identity) = vtcode_core::tools::normalised_code_search_identity(args)
    {
        return ToolCacheKey::new(tool_name, &identity, &enhanced_target);
    }

    ToolCacheKey::from_json(tool_name, args, &enhanced_target)
}

pub(super) fn cache_target_path<'a>(tool_name: &str, args: &'a Value) -> Cow<'a, str> {
    if is_directory_scoped_tool(tool_name) {
        for key in ["path", "root", "target_path", "dir"] {
            if let Some(path) = args
                .get(key)
                .and_then(Value::as_str)
                .map(str::trim)
                .filter(|path| !path.is_empty())
            {
                return Cow::Borrowed(path);
            }
        }
        return Cow::Borrowed(".");
    }
    if let Some(path) = args.get("path").and_then(|v| v.as_str()) {
        return Cow::Borrowed(path);
    }
    if let Some(root) = args.get("root").and_then(|v| v.as_str()) {
        return Cow::Borrowed(root);
    }
    if let Some(target) = args.get("target_path").and_then(|v| v.as_str()) {
        return Cow::Borrowed(target);
    }
    if let Some(dir) = args.get("dir").and_then(|v| v.as_str()) {
        return Cow::Borrowed(dir);
    }
    if let Some(diff_target) = extract_git_diff_cache_target(tool_name, args) {
        return Cow::Owned(diff_target);
    }

    Cow::Owned(tool_name.to_string())
}

fn is_directory_scoped_tool(tool_name: &str) -> bool {
    matches!(tool_name, tools::CODE_SEARCH | tools::LIST_FILES | "grep_search" | "find_files")
}

fn extract_git_diff_cache_target(tool_name: &str, args: &Value) -> Option<String> {
    let parts = command_parts_for_cache(tool_name, args)?;
    if contains_shell_operator(&parts) {
        return None;
    }
    if !is_git_diff_command(&parts) {
        return None;
    }
    extract_git_diff_path_target(&parts)
}

pub(super) fn stream_command_parts(tool_name: &str, args: &Value) -> Option<Vec<String>> {
    if !tool_intent::is_command_run_tool_call(tool_name, args) {
        return None;
    }

    let command_value = args
        .get("command")
        .or_else(|| args.get("cmd"))
        .or_else(|| args.get("raw_command"))?;
    collect_command_parts(command_value, args)
}

fn command_parts_for_cache(tool_name: &str, args: &Value) -> Option<Vec<String>> {
    match tool_name {
        // Keep cache behavior strict for run_pty/shell: only "command" is accepted
        // (no raw_command fallback) to avoid broadening cacheability semantics.
        tools::RUN_PTY_CMD | tools::SHELL | "bash" => collect_command_parts(args.get("command")?, args),
        _ => stream_command_parts(tool_name, args),
    }
}

fn collect_command_parts(command_value: &Value, args: &Value) -> Option<Vec<String>> {
    let mut parts = command_value_to_parts(command_value)?;
    append_args(&mut parts, args.get("args"));
    if parts.is_empty() { None } else { Some(parts) }
}

fn command_value_to_parts(value: &Value) -> Option<Vec<String>> {
    if let Some(command) = value.as_str() {
        let parts = shell_words::split(command)
            .ok()?
            .into_iter()
            .filter(|segment| !segment.is_empty())
            .collect::<Vec<_>>();
        if parts.is_empty() { None } else { Some(parts) }
    } else if let Some(parts) = value.as_array() {
        let collected = parts
            .iter()
            .filter_map(Value::as_str)
            .map(str::trim)
            .filter(|segment| !segment.is_empty())
            .map(ToString::to_string)
            .collect::<Vec<_>>();
        if collected.is_empty() { None } else { Some(collected) }
    } else {
        None
    }
}

fn append_args(parts: &mut Vec<String>, args_value: Option<&Value>) {
    let Some(args_array) = args_value.and_then(Value::as_array) else {
        return;
    };

    for arg in args_array {
        if let Some(segment) = arg.as_str().map(str::trim).filter(|value| !value.is_empty()) {
            parts.push(segment.to_string());
        }
    }
}

fn is_git_diff_command(parts: &[String]) -> bool {
    let Some(first) = parts.first() else {
        return false;
    };
    let basename = Path::new(first)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or(first.as_str())
        .to_ascii_lowercase();
    if basename != "git" && basename != "git.exe" {
        return false;
    }

    parts.iter().skip(1).any(|part| part == "diff")
}

fn extract_git_diff_path_target(parts: &[String]) -> Option<String> {
    let diff_index = parts.iter().position(|part| part == "diff")?;
    if diff_index + 1 >= parts.len() {
        return None;
    }

    let mut saw_separator = false;
    let mut targets = Vec::new();

    for part in parts.iter().skip(diff_index + 1) {
        if part == "--" {
            saw_separator = true;
            continue;
        }

        if !saw_separator {
            if part.starts_with('-') {
                continue;
            }
            if !is_path_like(part) {
                continue;
            }
        }

        targets.push(part.clone());
    }

    if targets.is_empty() {
        None
    } else {
        Some(targets.join(" "))
    }
}

fn is_path_like(candidate: &str) -> bool {
    candidate.contains('/') || candidate.contains('\\') || candidate.starts_with("./")
}

fn contains_shell_operator(parts: &[String]) -> bool {
    parts.iter().any(|part| {
        matches!(part.as_str(), "|" | "||" | "&" | "&&" | ";" | ">" | ">>" | "<")
            || part.contains('|')
            || part.contains(';')
            || part.contains("&&")
            || part.contains("||")
    })
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use vtcode_core::config::constants::tools;

    use super::{cache_target_path, create_enhanced_cache_key, is_tool_cacheable, stream_command_parts};

    #[test]
    fn caches_path_scoped_git_diff_run_pty() {
        let args = json!({
            "command": "git diff vtcode-tui/src/core_tui/session/diff_preview.rs"
        });

        assert!(is_tool_cacheable(tools::RUN_PTY_CMD, &args));
        assert_eq!(cache_target_path(tools::RUN_PTY_CMD, &args), "vtcode-tui/src/core_tui/session/diff_preview.rs");
    }

    #[test]
    fn does_not_cache_git_diff_without_path() {
        let args = json!({ "command": "git diff" });

        assert!(!is_tool_cacheable(tools::RUN_PTY_CMD, &args));
        assert_eq!(cache_target_path(tools::RUN_PTY_CMD, &args), tools::RUN_PTY_CMD);
    }

    #[test]
    fn caches_command_session_run_with_git_diff_path() {
        let args = json!({
            "action": "run",
            "command": ["git", "diff", "src/main.rs"]
        });

        assert!(is_tool_cacheable(tools::UNIFIED_EXEC, &args));
        assert_eq!(cache_target_path(tools::UNIFIED_EXEC, &args), "src/main.rs");
    }

    #[test]
    fn does_not_cache_non_run_command_session_action() {
        let args = json!({
            "action": "poll",
            "session_id": "run-123"
        });

        assert!(!is_tool_cacheable(tools::UNIFIED_EXEC, &args));
    }

    #[test]
    fn does_not_cache_compound_shell_command_with_diff() {
        let args = json!({
            "command": "git diff src/main.rs && echo done"
        });

        assert!(!is_tool_cacheable(tools::RUN_PTY_CMD, &args));
    }

    #[test]
    fn caches_quoted_path_with_spaces() {
        let args = json!({
            "command": "git diff \"dir with space/file.rs\""
        });

        assert!(is_tool_cacheable(tools::RUN_PTY_CMD, &args));
        assert_eq!(cache_target_path(tools::RUN_PTY_CMD, &args), "dir with space/file.rs");
    }

    #[test]
    fn stream_command_parts_supports_raw_command_for_run_pty() {
        let args = json!({
            "raw_command": "cargo check -p vtcode-core"
        });

        assert_eq!(
            stream_command_parts(tools::RUN_PTY_CMD, &args),
            Some(vec![
                "cargo".to_string(),
                "check".to_string(),
                "-p".to_string(),
                "vtcode-core".to_string()
            ])
        );
    }

    #[test]
    fn stream_command_parts_skips_non_run_command_session() {
        let args = json!({
            "action": "poll",
            "session_id": "run-123"
        });

        assert_eq!(stream_command_parts(tools::UNIFIED_EXEC, &args), None);
    }

    #[test]
    fn caches_file_operation_read_calls() {
        let args = json!({
            "action": "read",
            "path": "src/main.rs"
        });

        assert!(is_tool_cacheable(tools::UNIFIED_FILE, &args));
        assert_eq!(cache_target_path(tools::UNIFIED_FILE, &args), "src/main.rs");
    }

    #[test]
    fn does_not_cache_live_tool_output_spool_reads() {
        let read_file_args = json!({
            "path": ".vtcode\\context\\tool_outputs\\command_session_123.txt"
        });
        let unified_file_args = json!({
            "action": "read",
            "path": ".vtcode/context/tool_outputs/command_session_123.txt"
        });

        assert!(!is_tool_cacheable(tools::READ_FILE, &read_file_args));
        assert!(!is_tool_cacheable(tools::UNIFIED_FILE, &unified_file_args));
    }

    #[test]
    fn pathless_directory_scoped_reads_use_workspace_target() {
        let args = json!({ "query": "Widget" });

        assert_eq!(cache_target_path(tools::CODE_SEARCH, &args), ".");
        assert_eq!(cache_target_path(tools::LIST_FILES, &args), ".");
        assert_eq!(create_enhanced_cache_key(tools::LIST_FILES, &args, ".", "/workspace").target_path, "/workspace");
    }

    #[test]
    fn caches_code_search_with_normalised_target() {
        let args = json!({
            "query": "Widget",
            "path": " src ",
            "file_types": [".rs"],
            "result_types": ["path", "definition"],
            "max_results": 5
        });

        assert!(is_tool_cacheable(tools::CODE_SEARCH, &args));
        assert_eq!(cache_target_path(tools::CODE_SEARCH, &args), "src");
    }

    #[test]
    fn code_search_cache_identity_includes_effective_limit_and_canonicalises_filters() {
        let first = json!({
            "query": " Widget ",
            "path": "src",
            "file_types": [".rs", "python", "rust"],
            "result_types": ["path", "definition", "path"],
            "max_results": 5
        });
        let second = json!({
            "query": "Widget",
            "path": "src",
            "file_types": ["rust", ".py"],
            "result_types": ["definition", "path"],
            "max_results": 100
        });

        assert_ne!(
            create_enhanced_cache_key(tools::CODE_SEARCH, &first, "src", "/workspace"),
            create_enhanced_cache_key(tools::CODE_SEARCH, &second, "src", "/workspace"),
            "different effective limits must not share cached results"
        );

        let explicit_default = json!({
            "query": "Widget",
            "path": "src",
            "file_types": ["rust", ".py"],
            "result_types": ["definition", "path"],
            "max_results": 20
        });
        let omitted_default = json!({
            "query": " Widget ",
            "path": "src",
            "file_types": [".rs", "python"],
            "result_types": ["path", "definition"]
        });
        assert_eq!(
            create_enhanced_cache_key(tools::CODE_SEARCH, &explicit_default, "src", "/workspace"),
            create_enhanced_cache_key(tools::CODE_SEARCH, &omitted_default, "src", "/workspace"),
            "omitted and explicit default limits must share cached results"
        );
    }
}