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 | "bash" => true,
147 tools::EXEC_COMMAND => crate::tools::command_args::command_text(args).ok().flatten().is_some(),
148 tools::UNIFIED_EXEC | tools::EXEC_PTY_CMD | "exec" | "container.exec" => command_session_action_is(args, "run"),
149 _ => false,
150 }
151}
152
153pub fn is_turn_budget_exempt_call(tool_name: &str, args: &Value) -> bool {
167 if canonical_command_session_tool_name(tool_name).is_none() {
168 return false;
169 }
170 command_session_action_is(args, "wait")
171 || command_session_action_is(args, "inspect")
172 || is_exec_session_cleanup_call(tool_name, args)
173}
174
175pub fn is_exec_session_cleanup_call(tool_name: &str, args: &Value) -> bool {
177 tool_name == tools::WRITE_STDIN
178 && matches!(
179 crate::tools::command_args::write_stdin_dispatch(args),
180 Ok(crate::tools::command_args::WriteStdinDispatch::Terminate
181 | crate::tools::command_args::WriteStdinDispatch::Close)
182 )
183}
184
185pub fn remap_file_operation_command_args_to_command_session(args: &Value) -> Option<Value> {
186 let obj = args.as_object()?;
187 let command = obj
188 .get("command")
189 .or_else(|| obj.get("cmd"))
190 .or_else(|| obj.get("raw_command"))
191 .and_then(Value::as_str)
192 .map(str::trim)
193 .filter(|value| !value.is_empty())?;
194 let action = obj.get("action").and_then(Value::as_str).map(str::trim);
195 if let Some(action) = action
196 && !action.is_empty()
197 && !action_matches_any(Some(action), &["run", "exec", "execute", "shell"])
198 {
199 return None;
200 }
201
202 let mut mapped = serde_json::Map::new();
203 mapped.insert("action".to_string(), Value::String("run".to_string()));
204 mapped.insert("command".to_string(), Value::String(command.to_string()));
205
206 for key in [
207 "args",
208 "cwd",
209 "workdir",
210 "env",
211 "timeout_ms",
212 "yield_time_ms",
213 "login",
214 "shell",
215 "tty",
216 "sandbox_permissions",
217 "justification",
218 "prefix_rule",
219 ] {
220 if let Some(value) = obj.get(key) {
221 mapped.insert(key.to_string(), value.clone());
222 }
223 }
224
225 Some(Value::Object(mapped))
226}
227
228fn file_operation_intent(args: &Value) -> ToolIntent {
229 if file_operation_action_is(args, "read") {
230 ToolIntent::read_only_unified_action()
231 } else {
232 ToolIntent::mutating()
233 }
234}
235
236fn command_session_intent(args: &Value) -> ToolIntent {
237 let has_exec_input = command_session_has_input(args);
238 let readonly_unified_action = if command_session_action_is(args, "run") {
239 is_readonly_command_session_command(args)
240 } else {
241 command_session_action_in(args, &["poll", "wait", "list", "inspect"])
242 || (command_session_action_is(args, "continue") && !has_exec_input)
243 };
244
245 if readonly_unified_action {
246 ToolIntent::read_only_unified_action()
247 } else {
248 ToolIntent::mutating()
249 }
250}
251
252fn exec_command_intent(args: &Value) -> ToolIntent {
253 if is_readonly_command_session_command(args) {
254 ToolIntent::read_only_unified_action()
255 } else {
256 ToolIntent::mutating()
257 }
258}
259
260fn write_stdin_intent(args: &Value) -> ToolIntent {
261 match crate::tools::command_args::write_stdin_dispatch(args) {
262 Ok(crate::tools::command_args::WriteStdinDispatch::Poll) => ToolIntent::read_only(),
263 Ok(
264 crate::tools::command_args::WriteStdinDispatch::Wait
265 | crate::tools::command_args::WriteStdinDispatch::Inspect,
266 ) => ToolIntent::read_only(),
267 Ok(
268 crate::tools::command_args::WriteStdinDispatch::Write
269 | crate::tools::command_args::WriteStdinDispatch::Terminate
270 | crate::tools::command_args::WriteStdinDispatch::Close,
271 )
272 | Err(_) => ToolIntent::mutating(),
273 }
274}
275
276fn memory_tool_intent(args: &Value) -> ToolIntent {
277 let command = args.get("command").and_then(Value::as_str).map(str::trim).unwrap_or_default();
278 if command.eq_ignore_ascii_case("view") {
279 ToolIntent::read_only()
280 } else {
281 ToolIntent::mutating()
282 }
283}
284
285fn builtin_tool_behavior_canonical(tool: &str) -> Option<ToolBehavior> {
286 match tool {
287 tools::CODE_SEARCH => Some(ToolBehavior::function(ToolMutationModel::ReadOnly, true, false)),
288 tools::UNIFIED_EXEC => {
289 Some(ToolBehavior::function(ToolMutationModel::ByArgs(command_session_intent), false, true))
290 }
291 tools::EXEC_COMMAND => {
292 Some(ToolBehavior::function(ToolMutationModel::ByArgs(exec_command_intent), false, true))
293 }
294 tools::WRITE_STDIN => Some(ToolBehavior::function(ToolMutationModel::ByArgs(write_stdin_intent), false, true)),
295 tools::UNIFIED_FILE => {
296 Some(ToolBehavior::function(ToolMutationModel::ByArgs(file_operation_intent), false, false))
297 }
298 tools::APPLY_PATCH => Some(ToolBehavior::apply_patch(ToolMutationModel::Mutating, false, true)),
299 tools::REQUEST_USER_INPUT
300 | tools::MEMORY
301 | tools::START_PLANNING
302 | tools::LIST_SKILLS
303 | tools::LOAD_SKILL_RESOURCE
304 | tools::TASK_TRACKER
305 | tools::RECORD_DECISION
306 | tools::GET_ERRORS
307 | tools::SEARCH_TOOLS
308 | tools::MCP_SEARCH_TOOLS
309 | tools::MCP_GET_TOOL_DETAILS
310 | tools::MCP_LIST_SERVERS
311 | tools::THINK => Some(ToolBehavior::function(
312 if tool == tools::MEMORY {
313 ToolMutationModel::ByArgs(memory_tool_intent)
314 } else {
315 ToolMutationModel::ReadOnly
316 },
317 false,
318 false,
319 )),
320 tools::LOAD_SKILL => Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true)),
321 tools::READ_FILE | tools::GREP_FILE | tools::LIST_FILES => {
322 Some(ToolBehavior::function(ToolMutationModel::ReadOnly, true, false))
323 }
324 tools::WEB_FETCH | tools::FETCH_URL | tools::WEB_SEARCH | tools::DEFUDDLE_FETCH => {
325 Some(ToolBehavior::function(ToolMutationModel::ReadOnly, false, false))
326 }
327 tools::WRITE_FILE | tools::EDIT_FILE | tools::DELETE_FILE | tools::CREATE_FILE => {
328 Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true))
329 }
330 tools::MCP_CONNECT_SERVER | tools::MCP_DISCONNECT_SERVER => {
331 Some(ToolBehavior::function(ToolMutationModel::Mutating, false, false))
332 }
333 tools::RUN_PTY_CMD
334 | tools::SEND_PTY_INPUT
335 | tools::CREATE_PTY_SESSION
336 | tools::READ_PTY_SESSION
337 | tools::LIST_PTY_SESSIONS
338 | tools::CLOSE_PTY_SESSION
339 | tools::EXECUTE_CODE
340 | tools::SHELL => Some(ToolBehavior::function(ToolMutationModel::Mutating, false, true)),
341 _ => None,
342 }
343}
344
345fn command_session_has_input(args: &Value) -> bool {
346 interactive_input_text(args).is_some()
347}