Skip to main content

vtcode_core/tools/tool_intent/
mod.rs

1//! Tool intent classification: mutating vs read-only, parallel-safe, and
2//! action extraction for unified tool surfaces.
3
4mod mutation;
5pub use mutation::{is_docs_only_write, is_plan_artifact_write, tmp_plans_dir};
6
7pub mod actions;
8pub mod activity;
9pub mod classify;
10pub mod readonly;
11pub mod types;
12
13pub use actions::{
14    action_qualified_policy_name, command_session_action, command_session_action_in, command_session_action_is,
15    file_operation_action, file_operation_action_in, file_operation_action_is, mcp_action, mcp_action_is,
16};
17pub use activity::{
18    GENERIC_VERIFIER_DESCRIPTION, ShellActivity, VERIFIER_SHELL_FORM_NOTE, classify_shell_activity,
19    default_verifier_for_workspace, exec_mutation_should_incur_debt, rewrite_truncation_only_verifier,
20    shell_args_as_executed, shell_command_contains_verifier, shell_command_is_admitted_verification_attempt,
21    shell_command_is_output_search, shell_command_is_standalone_grep_search, verification_recovery_directive,
22    verifier_reference,
23};
24pub use classify::{
25    builtin_tool_behavior, canonical_command_session_tool_name, classify_tool_intent, is_command_run_tool,
26    is_command_run_tool_call, is_command_session_tool, is_command_tool, is_edited_file_conflict_guarded_call,
27    is_exec_session_cleanup_call, is_parallel_safe_call, is_parallel_safe_call_with_intent, planning_allowed_actions,
28    remap_file_operation_command_args_to_command_session, should_use_spool_reference_only,
29};
30pub use readonly::{is_readonly_command_session_command, is_spool_file_read_command};
31pub use types::{ToolBehavior, ToolIntent, ToolIntentClassifier, ToolMutationModel, ToolSurfaceKind};
32
33#[cfg(test)]
34mod tests {
35    use super::{
36        canonical_command_session_tool_name, classify_tool_intent, file_operation_action, is_command_run_tool_call,
37        is_command_session_tool, is_edited_file_conflict_guarded_call, is_parallel_safe_call,
38        is_parallel_safe_call_with_intent, remap_file_operation_command_args_to_command_session,
39        should_use_spool_reference_only,
40    };
41    use crate::config::constants::tools;
42    use serde_json::json;
43
44    #[test]
45    fn file_operation_read_is_retry_safe() {
46        let intent = classify_tool_intent(tools::UNIFIED_FILE, &json!({"action": "read", "path": "README.md"}));
47        assert!(!intent.mutating);
48        assert!(intent.readonly_unified_action);
49        assert!(intent.retry_safe);
50    }
51
52    #[test]
53    fn command_session_poll_is_retry_safe() {
54        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "poll", "session_id": 1}));
55        assert!(!intent.mutating);
56        assert!(intent.readonly_unified_action);
57        assert!(intent.retry_safe);
58    }
59
60    #[test]
61    fn skill_loading_is_mutating_and_requires_confirmation() {
62        let intent = classify_tool_intent(tools::LOAD_SKILL, &json!({"name": "example"}));
63        assert!(intent.mutating);
64        assert!(intent.destructive);
65        assert!(!intent.retry_safe);
66    }
67
68    #[test]
69    fn command_session_inspect_is_retry_safe() {
70        let intent = classify_tool_intent(
71            tools::UNIFIED_EXEC,
72            &json!({"action": "inspect", "spool_path": ".vtcode/context/tool_outputs/run-1.txt"}),
73        );
74        assert!(!intent.mutating);
75        assert!(intent.readonly_unified_action);
76        assert!(intent.retry_safe);
77    }
78
79    #[test]
80    fn command_session_continue_without_input_is_retry_safe() {
81        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "continue", "session_id": "run-1"}));
82        assert!(!intent.mutating);
83        assert!(intent.readonly_unified_action);
84        assert!(intent.retry_safe);
85    }
86
87    #[test]
88    fn command_session_continue_with_input_is_mutating_and_destructive() {
89        let intent = classify_tool_intent(
90            tools::UNIFIED_EXEC,
91            &json!({"action": "continue", "session_id": "run-1", "input": "q"}),
92        );
93        assert!(intent.mutating);
94        assert!(intent.destructive);
95        assert!(!intent.readonly_unified_action);
96        assert!(!intent.retry_safe);
97    }
98
99    #[test]
100    fn command_session_continue_with_empty_input_stays_retry_safe() {
101        let intent = classify_tool_intent(
102            tools::UNIFIED_EXEC,
103            &json!({"action": "continue", "session_id": "run-1", "input": ""}),
104        );
105        assert!(!intent.mutating);
106        assert!(intent.readonly_unified_action);
107        assert!(intent.retry_safe);
108    }
109
110    #[test]
111    fn computer_use_stays_mutating_and_sequential() {
112        let intent = classify_tool_intent(tools::COMPUTER_USE, &json!({"task": "take a screenshot"}));
113        assert!(intent.mutating);
114        assert!(!is_parallel_safe_call(tools::COMPUTER_USE, &json!({"task": "take a screenshot"})));
115    }
116
117    #[test]
118    fn command_session_run_is_mutating_and_destructive() {
119        let intent =
120            classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "rm -rf /tmp/test"}));
121        assert!(intent.mutating);
122        assert!(intent.destructive);
123        assert!(!intent.retry_safe);
124    }
125
126    #[test]
127    fn command_session_run_echo_is_read_only() {
128        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "echo hi"}));
129        assert!(!intent.mutating);
130        assert!(intent.readonly_unified_action);
131        assert!(intent.retry_safe);
132    }
133
134    #[test]
135    fn command_session_run_diff_is_read_only() {
136        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "diff a.rs b.rs"}));
137        assert!(!intent.mutating);
138        assert!(intent.readonly_unified_action);
139        assert!(intent.retry_safe);
140    }
141
142    #[test]
143    fn command_session_run_find_is_read_only() {
144        let intent = classify_tool_intent(
145            tools::UNIFIED_EXEC,
146            &json!({"action": "run", "command": "find . -type f -name '*.rs' -not -path '*/target/*'"}),
147        );
148        assert!(!intent.mutating);
149        assert!(intent.readonly_unified_action);
150        assert!(intent.retry_safe);
151    }
152
153    #[test]
154    fn command_session_run_grep_wc_head_are_read_only() {
155        for cmd in [
156            "grep -rn 'todo' src",
157            "wc -l src/main.rs",
158            "head -50 src/lib.rs",
159            "tail -20 src/lib.rs",
160            "sort src/words.txt | uniq",
161        ] {
162            let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
163            assert!(!intent.mutating, "expected '{cmd}' to be read-only");
164            assert!(intent.readonly_unified_action, "expected '{cmd}' to be readonly_unified_action");
165        }
166    }
167
168    /// `ast-grep`/`sg` are not shipped by VT Code, `sg` also names the
169    /// shadow-utils group-switch command that runs an arbitrary command, and
170    /// several ast-grep modes rewrite files. They must stay mutating so they
171    /// take the approval path instead of being admitted as read-only.
172    #[test]
173    fn command_session_run_ast_grep_is_mutating() {
174        for cmd in [
175            "ast-grep -p 'foo($A)' -l rs",
176            "ast-grep scan --update-all",
177            "sg wheel -c 'rm -rf /'",
178        ] {
179            let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
180            assert!(intent.mutating, "expected '{cmd}' to be mutating");
181            assert!(!intent.readonly_unified_action, "expected '{cmd}' to not be readonly_unified_action");
182        }
183    }
184
185    #[test]
186    fn command_session_run_with_redirection_is_mutating() {
187        for cmd in [
188            "cat a.txt > b.txt",
189            "grep x src > out.txt",
190            "diff a b | wc -l > count.txt",
191            "echo $(date) > log.txt",
192        ] {
193            let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
194            assert!(intent.mutating, "expected '{cmd}' to be mutating because it contains redirection/substitution");
195        }
196    }
197
198    #[test]
199    fn command_session_run_find_with_destructive_flags_is_mutating() {
200        for cmd in [
201            "find . -type f -delete",
202            "find . -name '*.tmp' -exec rm {} \\;",
203            "find . -name '*.tmp' -exec chmod 600 {} \\;",
204        ] {
205            let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
206            assert!(intent.mutating, "expected '{cmd}' to be mutating because it has destructive find flags");
207        }
208    }
209
210    #[test]
211    fn command_session_run_pipelines_with_unsafe_segments_are_mutating() {
212        for cmd in ["cat a.txt | tee b.txt", "python script.py | cat", "grep x src | rm -rf"] {
213            let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
214            assert!(intent.mutating, "expected '{cmd}' to be mutating because a pipeline segment is unsafe");
215        }
216    }
217
218    #[test]
219    fn command_session_run_allowlisted_is_read_only() {
220        let intent =
221            classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "rg planning_active src"}));
222        assert!(!intent.mutating);
223        assert!(intent.readonly_unified_action);
224        assert!(intent.retry_safe);
225    }
226
227    #[test]
228    fn command_session_run_dry_run_is_read_only() {
229        let intent =
230            classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "npm install --dry-run"}));
231        assert!(!intent.mutating);
232        assert!(intent.readonly_unified_action);
233        assert!(intent.retry_safe);
234    }
235
236    #[test]
237    fn parallel_safe_calls_reject_control_and_exec_paths() {
238        assert!(is_parallel_safe_call(tools::READ_FILE, &json!({"path": "README.md"})));
239        assert!(!is_parallel_safe_call(tools::LIST_PTY_SESSIONS, &json!({})));
240        assert!(!is_parallel_safe_call(tools::REQUEST_USER_INPUT, &json!({"questions": []})));
241        assert!(!is_parallel_safe_call(tools::UNIFIED_EXEC, &json!({"action": "inspect", "session_id": "run-1"})));
242        assert!(is_parallel_safe_call(tools::EXEC_COMMAND, &json!({"cmd": "sed -n '1,40p' README.md"})));
243        assert!(!is_parallel_safe_call(tools::EXEC_COMMAND, &json!({"cmd": "cat README.md && cat Cargo.toml"})));
244        assert!(!is_parallel_safe_call(
245            tools::EXEC_COMMAND,
246            &json!({"cmd": "sed -n '1,40p' README.md", "tty": true})
247        ));
248    }
249
250    #[test]
251    fn parallel_safe_with_intent_matches_classifying_variant() {
252        // The with-intent variant must agree with the classifying variant for
253        // every branch: command-run payloads, non-command builtin behaviors,
254        // and unknown tools that fall back to plain intent classification.
255        for (tool_name, args) in [
256            (tools::UNIFIED_EXEC, json!({"action": "run", "command": "rg -n 'todo' src"})),
257            (tools::UNIFIED_EXEC, json!({"action": "run", "command": "rm -rf /tmp/test"})),
258            (tools::UNIFIED_EXEC, json!({"action": "poll", "session_id": "run-1"})),
259            (tools::EXEC_COMMAND, json!({"cmd": "sed -n '1,40p' README.md"})),
260            (tools::READ_FILE, json!({"path": "README.md"})),
261            (tools::WRITE_FILE, json!({"path": "README.md", "content": "x"})),
262            (tools::APPLY_PATCH, json!({"input": "*** Begin Patch\n*** End Patch\n"})),
263            ("totally_unknown_tool", json!({"path": "README.md"})),
264        ] {
265            let with_intent =
266                is_parallel_safe_call_with_intent(tool_name, &args, &classify_tool_intent(tool_name, &args));
267            assert_eq!(
268                with_intent,
269                is_parallel_safe_call(tool_name, &args),
270                "variants disagree for {tool_name} with {args}"
271            );
272        }
273    }
274
275    #[test]
276    fn command_session_cmd_alias_infers_run() {
277        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"cmd": "rm -rf /tmp/test"}));
278        assert!(intent.mutating);
279        assert!(intent.destructive);
280        assert!(!intent.readonly_unified_action);
281    }
282
283    #[test]
284    fn command_session_chars_alias_infers_write() {
285        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"session_id": "abc123", "chars": "status\n"}));
286        assert!(intent.mutating);
287        assert!(intent.destructive);
288        assert!(!intent.readonly_unified_action);
289    }
290
291    #[test]
292    fn write_stdin_empty_chars_is_read_only_and_retry_safe() {
293        let intent = classify_tool_intent(tools::WRITE_STDIN, &json!({"session_id": "abc123", "chars": ""}));
294
295        assert!(!intent.mutating);
296        assert!(!intent.destructive);
297        assert!(!intent.readonly_unified_action);
298        assert!(intent.retry_safe);
299    }
300
301    #[test]
302    fn write_stdin_non_empty_chars_is_mutating() {
303        let intent = classify_tool_intent(tools::WRITE_STDIN, &json!({"session_id": "abc123", "chars": "  status\n"}));
304
305        assert!(intent.mutating);
306        assert!(intent.destructive);
307        assert!(!intent.readonly_unified_action);
308        assert!(!intent.retry_safe);
309    }
310
311    #[test]
312    fn command_session_text_alias_infers_write() {
313        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"session_id": "abc123", "text": "status\n"}));
314        assert!(intent.mutating);
315        assert!(intent.destructive);
316        assert!(!intent.readonly_unified_action);
317    }
318
319    #[test]
320    fn command_session_spool_path_alias_infers_inspect() {
321        let intent =
322            classify_tool_intent(tools::UNIFIED_EXEC, &json!({"spool_path": ".vtcode/context/tool_outputs/run-1.txt"}));
323        assert!(!intent.mutating);
324        assert!(!intent.destructive);
325        assert!(intent.readonly_unified_action);
326    }
327
328    #[test]
329    fn command_session_compact_session_alias_infers_poll() {
330        let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"s": "run-1"}));
331        assert!(!intent.mutating);
332        assert!(!intent.destructive);
333        assert!(intent.readonly_unified_action);
334    }
335
336    #[test]
337    fn file_operation_unknown_args_require_action() {
338        let args = json!({
339            "unexpected": true
340        });
341        let action = file_operation_action(&args);
342        assert_eq!(action, None);
343    }
344
345    #[test]
346    fn file_operation_compact_path_alias_infers_read() {
347        let args = json!({
348            "p": "README.md"
349        });
350        let action = file_operation_action(&args);
351        assert_eq!(action, Some("read"));
352    }
353
354    #[test]
355    fn remap_file_operation_command_args_maps_command_payload_to_command_session() {
356        let remapped = remap_file_operation_command_args_to_command_session(&json!({
357            "command": "cargo check",
358            "cwd": ".",
359            "timeout_ms": 1000
360        }))
361        .expect("command payload should remap");
362
363        assert_eq!(remapped["action"], "run");
364        assert_eq!(remapped["command"], "cargo check");
365        assert_eq!(remapped["cwd"], ".");
366        assert_eq!(remapped["timeout_ms"], 1000);
367    }
368
369    #[test]
370    fn remap_file_operation_command_args_accepts_exec_action_aliases() {
371        let remapped = remap_file_operation_command_args_to_command_session(&json!({
372            "action": "shell",
373            "cmd": "echo ok"
374        }))
375        .expect("shell action alias should remap");
376
377        assert_eq!(remapped["action"], "run");
378        assert_eq!(remapped["command"], "echo ok");
379    }
380
381    #[test]
382    fn remap_file_operation_command_args_rejects_non_command_actions() {
383        let remapped = remap_file_operation_command_args_to_command_session(&json!({
384            "action": "read",
385            "command": "echo ok"
386        }));
387
388        assert_eq!(remapped, None);
389    }
390
391    #[test]
392    fn edited_file_conflict_guard_accepts_supported_mutations() {
393        assert!(is_edited_file_conflict_guarded_call(
394            tools::WRITE_FILE,
395            &json!({"path": "README.md", "content": "agent"})
396        ));
397        assert!(is_edited_file_conflict_guarded_call(
398            tools::CREATE_FILE,
399            &json!({"path": "README.md", "content": "agent"})
400        ));
401        assert!(is_edited_file_conflict_guarded_call(
402            tools::EDIT_FILE,
403            &json!({"path": "README.md", "old_str": "a", "new_str": "b"})
404        ));
405        assert!(is_edited_file_conflict_guarded_call(
406            tools::APPLY_PATCH,
407            &json!({"patch": "*** Begin Patch\n*** End Patch\n"})
408        ));
409        assert!(is_edited_file_conflict_guarded_call(
410            tools::UNIFIED_FILE,
411            &json!({"action": "write", "path": "README.md", "content": "agent"})
412        ));
413        assert!(is_edited_file_conflict_guarded_call(
414            tools::UNIFIED_FILE,
415            &json!({"action": "create", "path": "README.md", "content": "agent"})
416        ));
417    }
418
419    #[test]
420    fn edited_file_conflict_guard_rejects_non_guarded_calls() {
421        assert!(!is_edited_file_conflict_guarded_call(tools::READ_FILE, &json!({"path": "README.md"})));
422        assert!(!is_edited_file_conflict_guarded_call(tools::GREP_FILE, &json!({"pattern": "needle", "path": "."})));
423        assert!(!is_edited_file_conflict_guarded_call(tools::LIST_FILES, &json!({"path": "."})));
424        assert!(!is_edited_file_conflict_guarded_call(
425            tools::UNIFIED_FILE,
426            &json!({"action": "read", "path": "README.md"})
427        ));
428        assert!(!is_edited_file_conflict_guarded_call(
429            tools::UNIFIED_FILE,
430            &json!({"action": "delete", "path": "README.md"})
431        ));
432        assert!(!is_edited_file_conflict_guarded_call(
433            tools::UNIFIED_EXEC,
434            &json!({"action": "run", "command": "git status"})
435        ));
436    }
437
438    #[test]
439    fn canonical_command_session_tool_name_normalizes_exec_aliases() {
440        for alias in [
441            tools::UNIFIED_EXEC,
442            tools::RUN_PTY_CMD,
443            tools::SEND_PTY_INPUT,
444            tools::READ_PTY_SESSION,
445            tools::LIST_PTY_SESSIONS,
446            tools::CLOSE_PTY_SESSION,
447            tools::EXECUTE_CODE,
448            tools::EXEC_PTY_CMD,
449            tools::EXEC_COMMAND,
450            tools::WRITE_STDIN,
451            tools::SHELL,
452            "bash",
453            "exec",
454            "container.exec",
455        ] {
456            assert_eq!(canonical_command_session_tool_name(alias), Some(tools::UNIFIED_EXEC));
457            assert!(is_command_session_tool(alias));
458        }
459        assert!(!is_command_session_tool(tools::READ_FILE));
460        assert!(!is_command_session_tool(""));
461    }
462
463    #[test]
464    fn spool_reference_only_detects_exec_aliases() {
465        assert!(should_use_spool_reference_only(
466            Some(tools::RUN_PTY_CMD),
467            &json!({"spool_path": ".vtcode/context/tool_outputs/run-1.txt"})
468        ));
469    }
470
471    #[test]
472    fn spool_reference_only_detects_exec_payload_without_tool_name() {
473        assert!(should_use_spool_reference_only(
474            None,
475            &json!({
476                "command": "cargo check",
477                "spool_path": ".vtcode/context/tool_outputs/run-1.txt",
478                "exit_code": 0
479            })
480        ));
481    }
482
483    #[test]
484    fn spool_reference_only_skips_loop_recovery_payloads() {
485        assert!(!should_use_spool_reference_only(
486            Some(tools::UNIFIED_EXEC),
487            &json!({
488                "spool_path": ".vtcode/context/tool_outputs/run-1.txt",
489                "exit_code": 0,
490                "loop_detected": true
491            })
492        ));
493    }
494
495    #[test]
496    fn is_command_run_tool_call_only_accepts_run_actions() {
497        assert!(is_command_run_tool_call(tools::RUN_PTY_CMD, &json!({"command": "cargo check"})));
498        assert!(is_command_run_tool_call(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "cargo check"})));
499        assert!(is_command_run_tool_call(tools::EXEC_COMMAND, &json!({"cmd": "cargo check"})));
500        assert!(!is_command_run_tool_call(tools::UNIFIED_EXEC, &json!({"action": "poll", "session_id": "run-1"})));
501        assert!(!is_command_run_tool_call(tools::WRITE_STDIN, &json!({"session_id": "run-1", "chars": "q"})));
502    }
503}