vtcode_core/tools/tool_intent/
classify.rs1use serde_json::Value;
2
3use crate::config::constants::tools;
4use crate::tools::command_args::interactive_input_text;
5use crate::tools::names::canonical_tool_name;
6use crate::tools::tool_intent::actions::{
7 action_matches_any, command_session_action_in, command_session_action_is, file_operation_action,
8 file_operation_action_is,
9};
10use crate::tools::tool_intent::readonly::{
11 is_parallel_safe_command_session_command, is_readonly_command_session_command,
12};
13use crate::tools::tool_intent::types::{ToolBehavior, ToolIntent, ToolMutationModel};
14
15pub fn planning_allowed_actions(tool_name: &str) -> Option<&'static [&'static str]> {
19 let canonical = canonical_tool_name(tool_name);
20 match canonical {
21 tools::UNIFIED_FILE => Some(&["read"]),
22 tools::UNIFIED_EXEC => Some(&["run", "poll", "wait", "list", "inspect", "continue"]),
23 _ => None,
25 }
26}
27
28pub fn builtin_tool_behavior(tool_name: &str) -> Option<ToolBehavior> {
29 let canonical = canonical_tool_name(tool_name);
30 builtin_tool_behavior_canonical(canonical)
31}
32
33pub fn is_parallel_safe_call(tool_name: &str, args: &Value) -> bool {
34 is_parallel_safe_call_with_intent(tool_name, args, &classify_tool_intent(tool_name, args))
35}
36
37pub fn is_parallel_safe_call_with_intent(tool_name: &str, args: &Value, intent: &ToolIntent) -> bool {
45 let canonical = canonical_tool_name(tool_name);
46 if matches!(canonical, tools::EXEC_COMMAND | tools::UNIFIED_EXEC) && is_command_run_tool_call(canonical, args) {
47 return is_parallel_safe_command_session_command(args);
48 }
49 if let Some(behavior) = builtin_tool_behavior_canonical(canonical) {
50 return behavior.supports_parallel_calls && !intent.mutating;
51 }
52
53 !intent.mutating
54}
55
56pub fn classify_tool_intent(tool_name: &str, args: &Value) -> ToolIntent {
57 let canonical = canonical_tool_name(tool_name);
58 builtin_tool_behavior_canonical(canonical)
59 .map(|behavior| behavior.classify(args))
60 .unwrap_or_else(ToolIntent::mutating)
61}
62
63pub fn is_edited_file_conflict_guarded_call(tool_name: &str, args: &Value) -> bool {
64 let canonical = canonical_tool_name(tool_name);
65 match canonical {
66 tools::WRITE_FILE | tools::CREATE_FILE | tools::EDIT_FILE | tools::APPLY_PATCH => true,
67 tools::UNIFIED_FILE => file_operation_action(args)
68 .map(is_edited_file_conflict_guarded_file_operation_action)
69 .unwrap_or(false),
70 _ => false,
71 }
72}
73
74fn is_edited_file_conflict_guarded_file_operation_action(action: &str) -> bool {
75 action_matches_any(Some(action), &["write", "create", "edit", "patch"])
76}
77
78pub use tools::{canonical_command_session_tool_name, is_command_session_tool};
79
80pub fn should_use_spool_reference_only(tool_name: Option<&str>, output: &Value) -> bool {
81 let Some(obj) = output.as_object() else {
82 return false;
83 };
84
85 let has_spool_path = obj
86 .get("spool_path")
87 .and_then(Value::as_str)
88 .is_some_and(|path| !path.trim().is_empty());
89 if !has_spool_path {
90 return false;
91 }
92
93 if obj.get("loop_detected").and_then(Value::as_bool) == Some(true) {
94 return false;
95 }
96
97 if tool_name.is_some_and(is_command_session_tool) {
98 return true;
99 }
100
101 if obj
102 .get("content_type")
103 .and_then(Value::as_str)
104 .is_some_and(|content_type| content_type == "exec_inspect")
105 {
106 return true;
107 }
108
109 if tool_name.is_some_and(|name| {
110 name == tools::CODE_SEARCH
111 || name == tools::UNIFIED_SEARCH
112 || name == "code_search"
113 || name == "search_dispatch_internal"
114 }) {
115 return true;
116 }
117
118 ["command", "id", "session_id", "process_id", "is_exited", "exit_code"]
119 .iter()
120 .any(|key| obj.contains_key(*key))
121}
122
123#[must_use]
127pub fn is_command_tool(tool_name: &str) -> bool {
128 tool_name == tools::CREATE_PTY_SESSION || tool_name == tools::SEND_PTY_INPUT || is_command_session_tool(tool_name)
129}
130
131#[must_use]
140pub fn is_command_run_tool(tool_name: &str) -> bool {
141 matches!(tool_name, tools::RUN_PTY_CMD | tools::EXEC_COMMAND)
142}
143
144pub fn is_command_run_tool_call(tool_name: &str, args: &Value) -> bool {
145 match tool_name {
146 tools::RUN_PTY_CMD | tools::CREATE_PTY_SESSION | tools::SHELL | tools::BASH_ALIAS => true,
147 tools::EXEC_COMMAND => crate::tools::command_args::command_text(args).ok().flatten().is_some(),
148 tools::UNIFIED_EXEC | tools::EXEC_PTY_CMD | tools::EXEC_ALIAS | tools::CONTAINER_EXEC_ALIAS => {
149 command_session_action_is(args, "run")
150 }
151 _ => false,
152 }
153}
154
155pub fn is_exec_session_cleanup_call(tool_name: &str, args: &Value) -> bool {
157 tool_name == tools::WRITE_STDIN
158 && matches!(
159 crate::tools::command_args::write_stdin_dispatch(args),
160 Ok(crate::tools::command_args::WriteStdinDispatch::Terminate
161 | crate::tools::command_args::WriteStdinDispatch::Close)
162 )
163}
164
165pub fn remap_file_operation_command_args_to_command_session(args: &Value) -> Option<Value> {
166 let obj = args.as_object()?;
167 let command = obj
168 .get("command")
169 .or_else(|| obj.get("cmd"))
170 .or_else(|| obj.get("raw_command"))
171 .and_then(Value::as_str)
172 .map(str::trim)
173 .filter(|value| !value.is_empty())?;
174 let action = obj.get("action").and_then(Value::as_str).map(str::trim);
175 if let Some(action) = action
176 && !action.is_empty()
177 && !action_matches_any(Some(action), &["run", "exec", "execute", "shell"])
178 {
179 return None;
180 }
181
182 let mut mapped = serde_json::Map::new();
183 mapped.insert("action".to_string(), Value::String("run".to_string()));
184 mapped.insert("command".to_string(), Value::String(command.to_string()));
185
186 for key in [
187 "args",
188 "cwd",
189 "workdir",
190 "env",
191 "timeout_ms",
192 "yield_time_ms",
193 "login",
194 "shell",
195 "tty",
196 "sandbox_permissions",
197 "justification",
198 "prefix_rule",
199 ] {
200 if let Some(value) = obj.get(key) {
201 mapped.insert(key.to_string(), value.clone());
202 }
203 }
204
205 Some(Value::Object(mapped))
206}
207
208fn file_operation_intent(args: &Value) -> ToolIntent {
209 if file_operation_action_is(args, "read") {
210 ToolIntent::read_only_unified_action()
211 } else {
212 ToolIntent::mutating()
213 }
214}
215
216fn command_session_intent(args: &Value) -> ToolIntent {
217 let has_exec_input = command_session_has_input(args);
218 let readonly_unified_action = if command_session_action_is(args, "run") {
219 is_readonly_command_session_command(args)
220 } else {
221 command_session_action_in(args, &["poll", "wait", "list", "inspect"])
222 || (command_session_action_is(args, "continue") && !has_exec_input)
223 };
224
225 if readonly_unified_action {
226 ToolIntent::read_only_unified_action()
227 } else {
228 ToolIntent::mutating()
229 }
230}
231
232fn exec_command_intent(args: &Value) -> ToolIntent {
233 if is_readonly_command_session_command(args) {
234 ToolIntent::read_only_unified_action()
235 } else {
236 ToolIntent::mutating()
237 }
238}
239
240fn write_stdin_intent(args: &Value) -> ToolIntent {
241 match crate::tools::command_args::write_stdin_dispatch(args) {
242 Ok(crate::tools::command_args::WriteStdinDispatch::Poll) => ToolIntent::read_only(),
243 Ok(
244 crate::tools::command_args::WriteStdinDispatch::Wait
245 | crate::tools::command_args::WriteStdinDispatch::Inspect,
246 ) => ToolIntent::read_only(),
247 Ok(
248 crate::tools::command_args::WriteStdinDispatch::Write
249 | crate::tools::command_args::WriteStdinDispatch::Terminate
250 | crate::tools::command_args::WriteStdinDispatch::Close,
251 )
252 | Err(_) => ToolIntent::mutating(),
253 }
254}
255
256fn memory_tool_intent(args: &Value) -> ToolIntent {
257 let command = args.get("command").and_then(Value::as_str).map(str::trim).unwrap_or_default();
258 if command.eq_ignore_ascii_case("view") {
259 ToolIntent::read_only()
260 } else {
261 ToolIntent::mutating()
262 }
263}
264
265fn builtin_tool_behavior_canonical(tool: &str) -> Option<ToolBehavior> {
266 match tool {
267 tools::CODE_SEARCH => Some(ToolBehavior::function(ToolMutationModel::ReadOnly, true, false)),
268 tools::UNIFIED_EXEC => {
269 Some(ToolBehavior::function(ToolMutationModel::ByArgs(command_session_intent), false, true))
270 }
271 tools::EXEC_COMMAND => {
272 Some(ToolBehavior::function(ToolMutationModel::ByArgs(exec_command_intent), false, true))
273 }
274 tools::WRITE_STDIN => Some(ToolBehavior::function(ToolMutationModel::ByArgs(write_stdin_intent), false, true)),
275 tools::UNIFIED_FILE => {
276 Some(ToolBehavior::function(ToolMutationModel::ByArgs(file_operation_intent), false, false))
277 }
278 tools::APPLY_PATCH => Some(ToolBehavior::apply_patch(ToolMutationModel::Mutating, false, true)),
279 tools::REQUEST_USER_INPUT
280 | tools::MEMORY
281 | tools::START_PLANNING
282 | tools::LIST_SKILLS
283 | tools::LOAD_SKILL_RESOURCE
284 | tools::TASK_TRACKER
285 | tools::RECORD_DECISION
286 | tools::GET_ERRORS
287 | tools::SEARCH_TOOLS
288 | tools::MCP_SEARCH_TOOLS
289 | tools::MCP_GET_TOOL_DETAILS
290 | tools::MCP_LIST_SERVERS
291 | tools::THINK => Some(ToolBehavior::function(
292 if tool == tools::MEMORY {
293 ToolMutationModel::ByArgs(memory_tool_intent)
294 } else {
295 ToolMutationModel::ReadOnly
296 },
297 false,
298 false,
299 )),
300 tools::LOAD_SKILL => Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true)),
301 tools::COMPUTER_USE => Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true)),
304 tools::READ_FILE | tools::GREP_FILE | tools::LIST_FILES => {
305 Some(ToolBehavior::function(ToolMutationModel::ReadOnly, true, false))
306 }
307 tools::WEB_FETCH | tools::FETCH_URL | tools::WEB_SEARCH => {
308 Some(ToolBehavior::function(ToolMutationModel::ReadOnly, false, false))
309 }
310 tools::WRITE_FILE | tools::EDIT_FILE | tools::DELETE_FILE | tools::CREATE_FILE => {
311 Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true))
312 }
313 tools::MCP_CONNECT_SERVER | tools::MCP_DISCONNECT_SERVER => {
314 Some(ToolBehavior::function(ToolMutationModel::Mutating, false, false))
315 }
316 tools::RUN_PTY_CMD
317 | tools::SEND_PTY_INPUT
318 | tools::CREATE_PTY_SESSION
319 | tools::READ_PTY_SESSION
320 | tools::LIST_PTY_SESSIONS
321 | tools::CLOSE_PTY_SESSION
322 | tools::EXECUTE_CODE
323 | tools::SHELL => Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true)),
324 _ => None,
325 }
326}
327
328fn command_session_has_input(args: &Value) -> bool {
329 interactive_input_text(args).is_some()
330}