1mod 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 command_session_run_is_mutating_and_destructive() {
112 let intent =
113 classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "rm -rf /tmp/test"}));
114 assert!(intent.mutating);
115 assert!(intent.destructive);
116 assert!(!intent.retry_safe);
117 }
118
119 #[test]
120 fn command_session_run_echo_is_read_only() {
121 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "echo hi"}));
122 assert!(!intent.mutating);
123 assert!(intent.readonly_unified_action);
124 assert!(intent.retry_safe);
125 }
126
127 #[test]
128 fn command_session_run_diff_is_read_only() {
129 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "diff a.rs b.rs"}));
130 assert!(!intent.mutating);
131 assert!(intent.readonly_unified_action);
132 assert!(intent.retry_safe);
133 }
134
135 #[test]
136 fn command_session_run_find_is_read_only() {
137 let intent = classify_tool_intent(
138 tools::UNIFIED_EXEC,
139 &json!({"action": "run", "command": "find . -type f -name '*.rs' -not -path '*/target/*'"}),
140 );
141 assert!(!intent.mutating);
142 assert!(intent.readonly_unified_action);
143 assert!(intent.retry_safe);
144 }
145
146 #[test]
147 fn command_session_run_grep_wc_head_are_read_only() {
148 for cmd in [
149 "grep -rn 'todo' src",
150 "wc -l src/main.rs",
151 "head -50 src/lib.rs",
152 "tail -20 src/lib.rs",
153 "sort src/words.txt | uniq",
154 ] {
155 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
156 assert!(!intent.mutating, "expected '{cmd}' to be read-only");
157 assert!(intent.readonly_unified_action, "expected '{cmd}' to be readonly_unified_action");
158 }
159 }
160
161 #[test]
166 fn command_session_run_ast_grep_is_mutating() {
167 for cmd in [
168 "ast-grep -p 'foo($A)' -l rs",
169 "ast-grep scan --update-all",
170 "sg wheel -c 'rm -rf /'",
171 ] {
172 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
173 assert!(intent.mutating, "expected '{cmd}' to be mutating");
174 assert!(!intent.readonly_unified_action, "expected '{cmd}' to not be readonly_unified_action");
175 }
176 }
177
178 #[test]
179 fn command_session_run_with_redirection_is_mutating() {
180 for cmd in [
181 "cat a.txt > b.txt",
182 "grep x src > out.txt",
183 "diff a b | wc -l > count.txt",
184 "echo $(date) > log.txt",
185 ] {
186 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
187 assert!(intent.mutating, "expected '{cmd}' to be mutating because it contains redirection/substitution");
188 }
189 }
190
191 #[test]
192 fn command_session_run_find_with_destructive_flags_is_mutating() {
193 for cmd in [
194 "find . -type f -delete",
195 "find . -name '*.tmp' -exec rm {} \\;",
196 "find . -name '*.tmp' -exec chmod 600 {} \\;",
197 ] {
198 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
199 assert!(intent.mutating, "expected '{cmd}' to be mutating because it has destructive find flags");
200 }
201 }
202
203 #[test]
204 fn command_session_run_pipelines_with_unsafe_segments_are_mutating() {
205 for cmd in ["cat a.txt | tee b.txt", "python script.py | cat", "grep x src | rm -rf"] {
206 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": cmd}));
207 assert!(intent.mutating, "expected '{cmd}' to be mutating because a pipeline segment is unsafe");
208 }
209 }
210
211 #[test]
212 fn command_session_run_allowlisted_is_read_only() {
213 let intent =
214 classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "rg planning_active src"}));
215 assert!(!intent.mutating);
216 assert!(intent.readonly_unified_action);
217 assert!(intent.retry_safe);
218 }
219
220 #[test]
221 fn command_session_run_dry_run_is_read_only() {
222 let intent =
223 classify_tool_intent(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "npm install --dry-run"}));
224 assert!(!intent.mutating);
225 assert!(intent.readonly_unified_action);
226 assert!(intent.retry_safe);
227 }
228
229 #[test]
230 fn parallel_safe_calls_reject_control_and_exec_paths() {
231 assert!(is_parallel_safe_call(tools::READ_FILE, &json!({"path": "README.md"})));
232 assert!(!is_parallel_safe_call(tools::LIST_PTY_SESSIONS, &json!({})));
233 assert!(!is_parallel_safe_call(tools::REQUEST_USER_INPUT, &json!({"questions": []})));
234 assert!(!is_parallel_safe_call(tools::UNIFIED_EXEC, &json!({"action": "inspect", "session_id": "run-1"})));
235 assert!(is_parallel_safe_call(tools::EXEC_COMMAND, &json!({"cmd": "sed -n '1,40p' README.md"})));
236 assert!(!is_parallel_safe_call(tools::EXEC_COMMAND, &json!({"cmd": "cat README.md && cat Cargo.toml"})));
237 assert!(!is_parallel_safe_call(
238 tools::EXEC_COMMAND,
239 &json!({"cmd": "sed -n '1,40p' README.md", "tty": true})
240 ));
241 }
242
243 #[test]
244 fn parallel_safe_with_intent_matches_classifying_variant() {
245 for (tool_name, args) in [
249 (tools::UNIFIED_EXEC, json!({"action": "run", "command": "rg -n 'todo' src"})),
250 (tools::UNIFIED_EXEC, json!({"action": "run", "command": "rm -rf /tmp/test"})),
251 (tools::UNIFIED_EXEC, json!({"action": "poll", "session_id": "run-1"})),
252 (tools::EXEC_COMMAND, json!({"cmd": "sed -n '1,40p' README.md"})),
253 (tools::READ_FILE, json!({"path": "README.md"})),
254 (tools::WRITE_FILE, json!({"path": "README.md", "content": "x"})),
255 (tools::APPLY_PATCH, json!({"input": "*** Begin Patch\n*** End Patch\n"})),
256 ("totally_unknown_tool", json!({"path": "README.md"})),
257 ] {
258 let with_intent =
259 is_parallel_safe_call_with_intent(tool_name, &args, &classify_tool_intent(tool_name, &args));
260 assert_eq!(
261 with_intent,
262 is_parallel_safe_call(tool_name, &args),
263 "variants disagree for {tool_name} with {args}"
264 );
265 }
266 }
267
268 #[test]
269 fn command_session_cmd_alias_infers_run() {
270 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"cmd": "rm -rf /tmp/test"}));
271 assert!(intent.mutating);
272 assert!(intent.destructive);
273 assert!(!intent.readonly_unified_action);
274 }
275
276 #[test]
277 fn command_session_chars_alias_infers_write() {
278 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"session_id": "abc123", "chars": "status\n"}));
279 assert!(intent.mutating);
280 assert!(intent.destructive);
281 assert!(!intent.readonly_unified_action);
282 }
283
284 #[test]
285 fn write_stdin_empty_chars_is_read_only_and_retry_safe() {
286 let intent = classify_tool_intent(tools::WRITE_STDIN, &json!({"session_id": "abc123", "chars": ""}));
287
288 assert!(!intent.mutating);
289 assert!(!intent.destructive);
290 assert!(!intent.readonly_unified_action);
291 assert!(intent.retry_safe);
292 }
293
294 #[test]
295 fn write_stdin_non_empty_chars_is_mutating() {
296 let intent = classify_tool_intent(tools::WRITE_STDIN, &json!({"session_id": "abc123", "chars": " status\n"}));
297
298 assert!(intent.mutating);
299 assert!(intent.destructive);
300 assert!(!intent.readonly_unified_action);
301 assert!(!intent.retry_safe);
302 }
303
304 #[test]
305 fn command_session_text_alias_infers_write() {
306 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"session_id": "abc123", "text": "status\n"}));
307 assert!(intent.mutating);
308 assert!(intent.destructive);
309 assert!(!intent.readonly_unified_action);
310 }
311
312 #[test]
313 fn command_session_spool_path_alias_infers_inspect() {
314 let intent =
315 classify_tool_intent(tools::UNIFIED_EXEC, &json!({"spool_path": ".vtcode/context/tool_outputs/run-1.txt"}));
316 assert!(!intent.mutating);
317 assert!(!intent.destructive);
318 assert!(intent.readonly_unified_action);
319 }
320
321 #[test]
322 fn command_session_compact_session_alias_infers_poll() {
323 let intent = classify_tool_intent(tools::UNIFIED_EXEC, &json!({"s": "run-1"}));
324 assert!(!intent.mutating);
325 assert!(!intent.destructive);
326 assert!(intent.readonly_unified_action);
327 }
328
329 #[test]
330 fn file_operation_unknown_args_require_action() {
331 let args = json!({
332 "unexpected": true
333 });
334 let action = file_operation_action(&args);
335 assert_eq!(action, None);
336 }
337
338 #[test]
339 fn file_operation_compact_path_alias_infers_read() {
340 let args = json!({
341 "p": "README.md"
342 });
343 let action = file_operation_action(&args);
344 assert_eq!(action, Some("read"));
345 }
346
347 #[test]
348 fn remap_file_operation_command_args_maps_command_payload_to_command_session() {
349 let remapped = remap_file_operation_command_args_to_command_session(&json!({
350 "command": "cargo check",
351 "cwd": ".",
352 "timeout_ms": 1000
353 }))
354 .expect("command payload should remap");
355
356 assert_eq!(remapped["action"], "run");
357 assert_eq!(remapped["command"], "cargo check");
358 assert_eq!(remapped["cwd"], ".");
359 assert_eq!(remapped["timeout_ms"], 1000);
360 }
361
362 #[test]
363 fn remap_file_operation_command_args_accepts_exec_action_aliases() {
364 let remapped = remap_file_operation_command_args_to_command_session(&json!({
365 "action": "shell",
366 "cmd": "echo ok"
367 }))
368 .expect("shell action alias should remap");
369
370 assert_eq!(remapped["action"], "run");
371 assert_eq!(remapped["command"], "echo ok");
372 }
373
374 #[test]
375 fn remap_file_operation_command_args_rejects_non_command_actions() {
376 let remapped = remap_file_operation_command_args_to_command_session(&json!({
377 "action": "read",
378 "command": "echo ok"
379 }));
380
381 assert_eq!(remapped, None);
382 }
383
384 #[test]
385 fn edited_file_conflict_guard_accepts_supported_mutations() {
386 assert!(is_edited_file_conflict_guarded_call(
387 tools::WRITE_FILE,
388 &json!({"path": "README.md", "content": "agent"})
389 ));
390 assert!(is_edited_file_conflict_guarded_call(
391 tools::CREATE_FILE,
392 &json!({"path": "README.md", "content": "agent"})
393 ));
394 assert!(is_edited_file_conflict_guarded_call(
395 tools::EDIT_FILE,
396 &json!({"path": "README.md", "old_str": "a", "new_str": "b"})
397 ));
398 assert!(is_edited_file_conflict_guarded_call(
399 tools::APPLY_PATCH,
400 &json!({"patch": "*** Begin Patch\n*** End Patch\n"})
401 ));
402 assert!(is_edited_file_conflict_guarded_call(
403 tools::UNIFIED_FILE,
404 &json!({"action": "write", "path": "README.md", "content": "agent"})
405 ));
406 assert!(is_edited_file_conflict_guarded_call(
407 tools::UNIFIED_FILE,
408 &json!({"action": "create", "path": "README.md", "content": "agent"})
409 ));
410 }
411
412 #[test]
413 fn edited_file_conflict_guard_rejects_non_guarded_calls() {
414 assert!(!is_edited_file_conflict_guarded_call(tools::READ_FILE, &json!({"path": "README.md"})));
415 assert!(!is_edited_file_conflict_guarded_call(tools::GREP_FILE, &json!({"pattern": "needle", "path": "."})));
416 assert!(!is_edited_file_conflict_guarded_call(tools::LIST_FILES, &json!({"path": "."})));
417 assert!(!is_edited_file_conflict_guarded_call(
418 tools::UNIFIED_FILE,
419 &json!({"action": "read", "path": "README.md"})
420 ));
421 assert!(!is_edited_file_conflict_guarded_call(
422 tools::UNIFIED_FILE,
423 &json!({"action": "delete", "path": "README.md"})
424 ));
425 assert!(!is_edited_file_conflict_guarded_call(
426 tools::UNIFIED_EXEC,
427 &json!({"action": "run", "command": "git status"})
428 ));
429 }
430
431 #[test]
432 fn canonical_command_session_tool_name_normalizes_exec_aliases() {
433 for alias in [
434 tools::UNIFIED_EXEC,
435 tools::RUN_PTY_CMD,
436 tools::SEND_PTY_INPUT,
437 tools::READ_PTY_SESSION,
438 tools::LIST_PTY_SESSIONS,
439 tools::CLOSE_PTY_SESSION,
440 tools::EXECUTE_CODE,
441 tools::EXEC_PTY_CMD,
442 tools::EXEC_COMMAND,
443 tools::WRITE_STDIN,
444 tools::SHELL,
445 "bash",
446 "exec",
447 "container.exec",
448 ] {
449 assert_eq!(canonical_command_session_tool_name(alias), Some(tools::UNIFIED_EXEC));
450 assert!(is_command_session_tool(alias));
451 }
452 assert!(!is_command_session_tool(tools::READ_FILE));
453 assert!(!is_command_session_tool(""));
454 }
455
456 #[test]
457 fn spool_reference_only_detects_exec_aliases() {
458 assert!(should_use_spool_reference_only(
459 Some(tools::RUN_PTY_CMD),
460 &json!({"spool_path": ".vtcode/context/tool_outputs/run-1.txt"})
461 ));
462 }
463
464 #[test]
465 fn spool_reference_only_detects_exec_payload_without_tool_name() {
466 assert!(should_use_spool_reference_only(
467 None,
468 &json!({
469 "command": "cargo check",
470 "spool_path": ".vtcode/context/tool_outputs/run-1.txt",
471 "exit_code": 0
472 })
473 ));
474 }
475
476 #[test]
477 fn spool_reference_only_skips_loop_recovery_payloads() {
478 assert!(!should_use_spool_reference_only(
479 Some(tools::UNIFIED_EXEC),
480 &json!({
481 "spool_path": ".vtcode/context/tool_outputs/run-1.txt",
482 "exit_code": 0,
483 "loop_detected": true
484 })
485 ));
486 }
487
488 #[test]
489 fn is_command_run_tool_call_only_accepts_run_actions() {
490 assert!(is_command_run_tool_call(tools::RUN_PTY_CMD, &json!({"command": "cargo check"})));
491 assert!(is_command_run_tool_call(tools::UNIFIED_EXEC, &json!({"action": "run", "command": "cargo check"})));
492 assert!(is_command_run_tool_call(tools::EXEC_COMMAND, &json!({"cmd": "cargo check"})));
493 assert!(!is_command_run_tool_call(tools::UNIFIED_EXEC, &json!({"action": "poll", "session_id": "run-1"})));
494 assert!(!is_command_run_tool_call(tools::WRITE_STDIN, &json!({"session_id": "run-1", "chars": "q"})));
495 }
496}