1use crate::tools::shell::escape_arg;
4use crate::tools::{
5 agent::{self, AgentBuildOptions},
6 claude::{self, ClaudeBuildOptions},
7 codex::{self, CodexBuildOptions},
8 gemini::{self, GeminiBuildOptions},
9 is_tool_supported,
10 opencode::{self, OpencodeBuildOptions},
11 qwen::{self, QwenBuildOptions},
12};
13
14#[derive(Debug, Clone, Default)]
16pub struct AgentCommandOptions {
17 pub tool: String,
18 pub working_directory: String,
19 pub prompt: Option<String>,
20 pub prompt_file: Option<String>,
21 pub system_prompt: Option<String>,
22 pub append_system_prompt: Option<String>,
23 pub model: Option<String>,
24 pub fallback_model: Option<String>,
25 pub json: bool,
26 pub verbose: bool,
27 pub replay_user_messages: bool,
28 pub resume: Option<String>,
29 pub session_id: Option<String>,
30 pub fork_session: bool,
31 pub read_only: bool,
32 pub plan_only: bool,
33 pub approve_each: bool,
35 pub executable: Option<String>,
36 pub extra_args: Vec<String>,
37 pub extra_env: Vec<(String, String)>,
38 pub skip_default_safety_flags: bool,
39 pub isolation: String,
40 pub screen_name: Option<String>,
41 pub container_name: Option<String>,
42 pub detached: bool,
43}
44
45pub fn supports_read_only(tool: &str) -> bool {
47 matches!(
48 tool,
49 "claude" | "codex" | "opencode" | "gemini" | "qwen" | "agent"
50 )
51}
52
53pub fn read_only_unsupported_error(tool: &str) -> String {
55 format!(
56 "Tool \"{}\" does not support enforceable read-only mode. Choose one of: claude, codex, opencode, gemini, qwen, agent; or run without --read-only.",
57 tool
58 )
59}
60
61fn escape_quotes(s: &str) -> String {
63 s.replace('\'', "'\\''")
64}
65
66fn escape_for_bash_c(s: &str) -> String {
68 s.replace('\\', "\\\\")
69 .replace('"', "\\\"")
70 .replace('$', "\\$")
71 .replace('`', "\\`")
72}
73
74fn build_tool_command(tool: &str, prompt: Option<&str>, system_prompt: Option<&str>) -> String {
76 let mut command = tool.to_string();
77
78 if let Some(p) = prompt {
79 command.push_str(&format!(" --prompt \"{}\"", escape_quotes(p)));
80 }
81
82 if let Some(sp) = system_prompt {
83 command.push_str(&format!(" --system-prompt \"{}\"", escape_quotes(sp)));
84 }
85
86 command
87}
88
89fn build_screen_command(base_command: &str, screen_name: Option<&str>, detached: bool) -> String {
91 let session_name = screen_name.map(|s| s.to_string()).unwrap_or_else(|| {
92 format!(
93 "agent-{}",
94 std::time::SystemTime::now()
95 .duration_since(std::time::UNIX_EPOCH)
96 .unwrap()
97 .as_millis()
98 )
99 });
100
101 if detached {
102 format!(
104 "screen -dmS \"{}\" bash -c '{}'",
105 session_name,
106 escape_quotes(base_command)
107 )
108 } else {
109 format!(
111 "screen -S \"{}\" bash -c '{}'",
112 session_name,
113 escape_quotes(base_command)
114 )
115 }
116}
117
118fn build_docker_command(
120 base_command: &str,
121 container_name: Option<&str>,
122 working_directory: &str,
123 detached: bool,
124) -> String {
125 let name = container_name.map(|s| s.to_string()).unwrap_or_else(|| {
126 format!(
127 "agent-{}",
128 std::time::SystemTime::now()
129 .duration_since(std::time::UNIX_EPOCH)
130 .unwrap()
131 .as_millis()
132 )
133 });
134
135 let mut command = "docker run".to_string();
136
137 if detached {
138 command.push_str(" -d");
139 } else {
140 command.push_str(" -it");
141 }
142
143 command.push_str(&format!(" --name \"{}\"", name));
144 command.push_str(&format!(
145 " -v \"{}:{}\"",
146 working_directory, working_directory
147 ));
148 command.push_str(&format!(" -w \"{}\"", working_directory));
149 command.push_str(" node:18-slim");
150 command.push_str(&format!(" bash -c '{}'", escape_quotes(base_command)));
151
152 command
153}
154
155pub fn build_agent_command(options: &AgentCommandOptions) -> String {
163 let read_only_requested = options.read_only || options.plan_only;
166
167 assert!(
168 !(read_only_requested && !supports_read_only(&options.tool)),
169 "{}",
170 read_only_unsupported_error(&options.tool)
171 );
172
173 assert!(
177 !(options.approve_each && !crate::permissions::supports_ask(&options.tool)),
178 "{}",
179 crate::permissions::ask_unsupported_error(&options.tool)
180 );
181
182 let base_command = if is_tool_supported(&options.tool) {
184 match options.tool.as_str() {
185 "claude" => claude::build_command(&ClaudeBuildOptions {
186 prompt: options.prompt.clone(),
187 prompt_file: options.prompt_file.clone(),
188 system_prompt: options.system_prompt.clone(),
189 append_system_prompt: options.append_system_prompt.clone(),
190 model: options.model.clone(),
191 fallback_model: options.fallback_model.clone(),
192 json: options.json,
193 json_input: false,
194 verbose: options.verbose,
195 replay_user_messages: options.replay_user_messages,
196 resume: options.resume.clone(),
197 session_id: options.session_id.clone(),
198 fork_session: options.fork_session,
199 print: false,
200 read_only: read_only_requested,
201 approve_each: options.approve_each,
202 executable: options.executable.clone(),
203 extra_env: options.extra_env.clone(),
204 extra_args: options.extra_args.clone(),
205 skip_default_safety_flags: options.skip_default_safety_flags,
206 permission_mode: None,
207 stream_input: false,
208 }),
209 "codex" => codex::build_command(&CodexBuildOptions {
210 prompt: options.prompt.clone(),
211 prompt_file: options.prompt_file.clone(),
212 system_prompt: options.system_prompt.clone(),
213 model: options.model.clone(),
214 json: options.json,
215 resume: options.resume.clone(),
216 read_only: read_only_requested,
217 executable: options.executable.clone(),
218 extra_env: options.extra_env.clone(),
219 extra_args: options.extra_args.clone(),
220 skip_default_safety_flags: options.skip_default_safety_flags,
221 sandbox_mode: None,
222 approval_mode: None,
223 }),
224 "opencode" => opencode::build_command(&OpencodeBuildOptions {
225 prompt: options.prompt.clone(),
226 prompt_file: options.prompt_file.clone(),
227 system_prompt: options.system_prompt.clone(),
228 model: options.model.clone(),
229 json: options.json,
230 resume: options.resume.clone(),
231 read_only: read_only_requested,
232 executable: options.executable.clone(),
233 extra_env: options.extra_env.clone(),
234 extra_args: options.extra_args.clone(),
235 }),
236 "agent" => agent::build_command(&AgentBuildOptions {
237 resume: options.resume.clone(),
238 prompt: options.prompt.clone(),
239 prompt_file: options.prompt_file.clone(),
240 system_prompt: options.system_prompt.clone(),
241 model: options.model.clone(),
242 compact_json: false,
243 use_existing_claude_oauth: false,
244 read_only: read_only_requested,
245 plan_only: options.plan_only,
246 approve_each: options.approve_each,
247 permission_mode: None,
248 permission: None,
249 stream_input: false,
250 executable: options.executable.clone(),
251 extra_env: options.extra_env.clone(),
252 extra_args: options.extra_args.clone(),
253 }),
254 "gemini" => {
255 let options = GeminiBuildOptions {
256 resume: options.resume.clone(),
257 prompt: options.prompt.clone(),
258 prompt_file: options.prompt_file.clone(),
259 system_prompt: options.system_prompt.clone(),
260 model: options.model.clone(),
261 json: options.json,
262 read_only: read_only_requested,
263 executable: options.executable.clone(),
264 extra_env: options.extra_env.clone(),
265 extra_args: options.extra_args.clone(),
266 skip_default_safety_flags: options.skip_default_safety_flags,
267 ..GeminiBuildOptions::new()
268 };
269 gemini::build_command(&options)
270 }
271 "qwen" => {
272 let options = QwenBuildOptions {
273 prompt: options.prompt.clone(),
274 prompt_file: options.prompt_file.clone(),
275 system_prompt: options.system_prompt.clone(),
276 model: options.model.clone(),
277 json: options.json,
278 resume: options.resume.clone(),
279 read_only: read_only_requested,
280 executable: options.executable.clone(),
281 extra_env: options.extra_env.clone(),
282 extra_args: options.extra_args.clone(),
283 skip_default_safety_flags: options.skip_default_safety_flags,
284 ..QwenBuildOptions::new()
285 };
286 qwen::build_command(&options)
287 }
288 _ => build_tool_command(
289 &options.tool,
290 options.prompt.as_deref(),
291 options.system_prompt.as_deref(),
292 ),
293 }
294 } else {
295 build_tool_command(
297 &options.tool,
298 options.prompt.as_deref(),
299 options.system_prompt.as_deref(),
300 )
301 };
302
303 let mut full_command = format!(
305 "bash -c \"cd {} && {}\"",
306 escape_for_bash_c(&escape_arg(&options.working_directory)),
307 escape_for_bash_c(&base_command)
308 );
309
310 match options.isolation.as_str() {
312 "screen" => {
313 full_command = build_screen_command(
314 &full_command,
315 options.screen_name.as_deref(),
316 options.detached,
317 );
318 }
319 "docker" => {
320 full_command = build_docker_command(
321 &full_command,
322 options.container_name.as_deref(),
323 &options.working_directory,
324 options.detached,
325 );
326 }
327 _ => {}
328 }
329
330 full_command
331}
332
333pub fn build_screen_stop_command(screen_name: &str) -> String {
341 format!("screen -S \"{}\" -X quit", screen_name)
342}
343
344pub fn build_docker_stop_command(container_name: &str) -> String {
352 format!(
353 "docker stop \"{}\" && docker rm \"{}\"",
354 container_name, container_name
355 )
356}
357
358pub fn build_piped_command(input: &str, command: &str) -> String {
367 let escaped_input = escape_quotes(input);
368 format!("printf '%s' '{}' | {}", escaped_input, command)
369}
370
371#[cfg(test)]
372mod tests {
373 use super::*;
374
375 #[test]
376 fn test_build_agent_command_basic_claude() {
377 let options = AgentCommandOptions {
378 tool: "claude".to_string(),
379 working_directory: "/tmp/test".to_string(),
380 prompt: Some("Hello".to_string()),
381 isolation: "none".to_string(),
382 ..Default::default()
383 };
384
385 let command = build_agent_command(&options);
386 assert!(command.contains("bash -c"));
387 assert!(command.contains("cd"));
388 assert!(command.contains("/tmp/test"));
389 assert!(command.contains("claude"));
390 assert!(command.contains("-p"));
391 assert!(command.contains("Hello"));
392 }
393
394 #[test]
395 fn test_build_agent_command_with_system_prompt() {
396 let options = AgentCommandOptions {
397 tool: "claude".to_string(),
398 working_directory: "/tmp/test".to_string(),
399 prompt: Some("Hello".to_string()),
400 system_prompt: Some("You are helpful".to_string()),
401 isolation: "none".to_string(),
402 ..Default::default()
403 };
404
405 let command = build_agent_command(&options);
406 assert!(command.contains("-p"));
407 assert!(command.contains("--system-prompt"));
408 assert!(command.contains("You are helpful"));
409 }
410
411 #[test]
412 fn test_build_agent_command_claude_with_fallback_model() {
413 let options = AgentCommandOptions {
414 tool: "claude".to_string(),
415 working_directory: "/tmp/test".to_string(),
416 model: Some("opus".to_string()),
417 fallback_model: Some("sonnet".to_string()),
418 isolation: "none".to_string(),
419 ..Default::default()
420 };
421
422 let command = build_agent_command(&options);
423 assert!(command.contains("--model"));
424 assert!(command.contains("--model opus"));
425 assert!(command.contains("--fallback-model"));
426 assert!(command.contains("claude-sonnet-5"));
427 }
428
429 #[test]
430 fn test_build_agent_command_claude_with_append_system_prompt() {
431 let options = AgentCommandOptions {
432 tool: "claude".to_string(),
433 working_directory: "/tmp/test".to_string(),
434 append_system_prompt: Some("Extra instructions".to_string()),
435 isolation: "none".to_string(),
436 ..Default::default()
437 };
438
439 let command = build_agent_command(&options);
440 assert!(command.contains("--append-system-prompt"));
441 assert!(command.contains("Extra instructions"));
442 }
443
444 #[test]
445 fn test_build_agent_command_claude_with_session_management() {
446 let options = AgentCommandOptions {
447 tool: "claude".to_string(),
448 working_directory: "/tmp/test".to_string(),
449 resume: Some("abc123".to_string()),
450 session_id: Some("123e4567-e89b-12d3-a456-426614174000".to_string()),
451 fork_session: true,
452 isolation: "none".to_string(),
453 ..Default::default()
454 };
455
456 let command = build_agent_command(&options);
457 assert!(command.contains("--resume"));
458 assert!(command.contains("abc123"));
459 assert!(command.contains("--session-id"));
460 assert!(command.contains("123e4567-e89b-12d3-a456-426614174000"));
461 assert!(command.contains("--fork-session"));
462 }
463
464 #[test]
465 fn test_build_agent_command_claude_with_verbose_streaming() {
466 let options = AgentCommandOptions {
467 tool: "claude".to_string(),
468 working_directory: "/tmp/test".to_string(),
469 verbose: true,
470 replay_user_messages: true,
471 isolation: "none".to_string(),
472 ..Default::default()
473 };
474
475 let command = build_agent_command(&options);
476 assert!(command.contains("--verbose"));
477 assert!(command.contains("--replay-user-messages"));
478 }
479
480 #[test]
481 fn test_build_agent_command_claude_raw_passthrough() {
482 let options = AgentCommandOptions {
483 tool: "claude".to_string(),
484 working_directory: "/tmp/test".to_string(),
485 prompt: Some("Hello".to_string()),
486 executable: Some("/opt/Claude Code/bin/claude".to_string()),
487 extra_env: vec![
488 (
489 "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC".to_string(),
490 "1".to_string(),
491 ),
492 ("MCP_TIMEOUT".to_string(), "10000".to_string()),
493 ],
494 extra_args: vec![
495 "--mcp-config".to_string(),
496 "/tmp/mcp config.json".to_string(),
497 "--permission-mode".to_string(),
498 "default".to_string(),
499 ],
500 skip_default_safety_flags: true,
501 isolation: "none".to_string(),
502 ..Default::default()
503 };
504
505 let command = build_agent_command(&options);
506 assert!(command.contains("env"));
507 assert!(command.contains("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1"));
508 assert!(command.contains("MCP_TIMEOUT=10000"));
509 assert!(command.contains("/opt/Claude Code/bin/claude"));
510 assert!(command.contains("--mcp-config"));
511 assert!(command.contains("/tmp/mcp config.json"));
512 assert!(command.contains("--permission-mode"));
513 assert!(command.contains("default"));
514 assert!(!command.contains("--dangerously-skip-permissions"));
515 }
516
517 #[test]
518 fn test_build_agent_command_qwen_raw_passthrough() {
519 let options = AgentCommandOptions {
520 tool: "qwen".to_string(),
521 working_directory: "/tmp/test".to_string(),
522 prompt_file: Some("/tmp/prompt.txt".to_string()),
523 executable: Some("/opt/qwen code/qwen".to_string()),
524 extra_env: vec![("QWEN_HOME".to_string(), "/tmp/qwen home".to_string())],
525 extra_args: vec![
526 "--checkpointing".to_string(),
527 "--approval-mode".to_string(),
528 "default".to_string(),
529 ],
530 skip_default_safety_flags: true,
531 isolation: "none".to_string(),
532 ..Default::default()
533 };
534
535 let command = build_agent_command(&options);
536 assert!(command.contains("cat"));
537 assert!(command.contains("/tmp/prompt.txt"));
538 assert!(command.contains("| env QWEN_HOME="));
539 assert!(command.contains("/tmp/qwen home"));
540 assert!(command.contains("/opt/qwen code/qwen"));
541 assert!(command.contains("--checkpointing"));
542 assert!(command.contains("--approval-mode"));
543 assert!(command.contains("default"));
544 assert!(!command.contains("--yolo"));
545 }
546
547 #[test]
548 fn test_build_agent_command_gemini_raw_passthrough() {
549 let options = AgentCommandOptions {
550 tool: "gemini".to_string(),
551 working_directory: "/tmp/test".to_string(),
552 prompt_file: Some("/tmp/prompt.txt".to_string()),
553 executable: Some("/opt/gemini cli/gemini".to_string()),
554 extra_env: vec![("GEMINI_HOME".to_string(), "/tmp/gemini home".to_string())],
555 extra_args: vec!["--telemetry".to_string(), "false".to_string()],
556 skip_default_safety_flags: true,
557 isolation: "none".to_string(),
558 ..Default::default()
559 };
560
561 let command = build_agent_command(&options);
562 assert!(command.contains("cat"));
563 assert!(command.contains("/tmp/prompt.txt"));
564 assert!(command.contains("| env GEMINI_HOME="));
565 assert!(command.contains("/tmp/gemini home"));
566 assert!(command.contains("/opt/gemini cli/gemini"));
567 assert!(command.contains("--telemetry"));
568 assert!(command.contains("false"));
569 assert!(!command.contains("--yolo"));
570 }
571
572 #[test]
573 fn test_build_agent_command_unknown_tool() {
574 let options = AgentCommandOptions {
575 tool: "unknown-tool".to_string(),
576 working_directory: "/tmp/test".to_string(),
577 prompt: Some("Hello".to_string()),
578 isolation: "none".to_string(),
579 ..Default::default()
580 };
581
582 let command = build_agent_command(&options);
583 assert!(command.contains("bash -c"));
584 assert!(command.contains("unknown-tool"));
585 assert!(command.contains("--prompt"));
586 }
587
588 #[test]
589 fn test_build_agent_command_screen_isolation() {
590 let options = AgentCommandOptions {
591 tool: "claude".to_string(),
592 working_directory: "/tmp/test".to_string(),
593 isolation: "screen".to_string(),
594 screen_name: Some("my-session".to_string()),
595 detached: true,
596 ..Default::default()
597 };
598
599 let command = build_agent_command(&options);
600 assert!(command.contains("screen"));
601 assert!(command.contains("-dmS"));
602 assert!(command.contains("my-session"));
603 }
604
605 #[test]
606 fn test_build_agent_command_docker_isolation() {
607 let options = AgentCommandOptions {
608 tool: "claude".to_string(),
609 working_directory: "/tmp/test".to_string(),
610 isolation: "docker".to_string(),
611 container_name: Some("my-container".to_string()),
612 detached: true,
613 ..Default::default()
614 };
615
616 let command = build_agent_command(&options);
617 assert!(command.contains("docker run"));
618 assert!(command.contains("-d"));
619 assert!(command.contains("--name \"my-container\""));
620 assert!(command.contains("-v \"/tmp/test:/tmp/test\""));
621 }
622
623 #[test]
624 fn test_build_agent_command_with_model() {
625 let options = AgentCommandOptions {
626 tool: "claude".to_string(),
627 working_directory: "/tmp/test".to_string(),
628 model: Some("opus".to_string()),
629 isolation: "none".to_string(),
630 ..Default::default()
631 };
632
633 let command = build_agent_command(&options);
634 assert!(command.contains("--model"));
635 assert!(command.contains("--model opus"));
636 }
637
638 #[test]
639 fn test_build_agent_command_codex() {
640 let options = AgentCommandOptions {
641 tool: "codex".to_string(),
642 working_directory: "/tmp/test".to_string(),
643 prompt: Some("Hello".to_string()),
644 json: true,
645 isolation: "none".to_string(),
646 ..Default::default()
647 };
648
649 let command = build_agent_command(&options);
650 assert!(command.contains("codex"));
651 assert!(command.contains("exec"));
652 assert!(command.contains("--json"));
653 }
654
655 #[test]
656 fn test_build_agent_command_codex_prompt_file() {
657 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
658 let options = AgentCommandOptions {
659 tool: "codex".to_string(),
660 working_directory: "/tmp/test".to_string(),
661 prompt: Some(inline_prompt.to_string()),
662 system_prompt: Some("System instructions".to_string()),
663 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
664 json: true,
665 isolation: "none".to_string(),
666 ..Default::default()
667 };
668
669 let command = build_agent_command(&options);
670 assert!(command.contains("cat"));
671 assert!(command.contains("/tmp/agent prompt.txt"));
672 assert!(command.contains("codex"));
673 assert!(!command.contains(inline_prompt));
674 assert!(!command.contains("System instructions"));
675 }
676
677 #[test]
678 fn test_build_agent_command_claude_prompt_file() {
679 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
680 let options = AgentCommandOptions {
681 tool: "claude".to_string(),
682 working_directory: "/tmp/test".to_string(),
683 prompt: Some(inline_prompt.to_string()),
684 system_prompt: Some("You are helpful".to_string()),
685 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
686 isolation: "none".to_string(),
687 ..Default::default()
688 };
689
690 let command = build_agent_command(&options);
691 assert!(command.contains("cat"));
692 assert!(command.contains("/tmp/agent prompt.txt"));
693 assert!(command.contains("claude"));
694 assert!(command.contains("--system-prompt"));
695 assert!(command.contains("You are helpful"));
696 assert!(!command.contains(inline_prompt));
697 }
698
699 #[test]
700 fn test_build_agent_command_qwen_prompt_file() {
701 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
702 let options = AgentCommandOptions {
703 tool: "qwen".to_string(),
704 working_directory: "/tmp/test".to_string(),
705 prompt: Some(inline_prompt.to_string()),
706 system_prompt: Some("System instructions".to_string()),
707 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
708 isolation: "none".to_string(),
709 ..Default::default()
710 };
711
712 let command = build_agent_command(&options);
713 assert!(command.contains("cat"));
714 assert!(command.contains("/tmp/agent prompt.txt"));
715 assert!(command.contains("qwen"));
716 assert!(!command.contains(inline_prompt));
717 assert!(!command.contains("System instructions"));
718 }
719
720 #[test]
721 fn test_build_agent_command_gemini_prompt_file() {
722 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
723 let options = AgentCommandOptions {
724 tool: "gemini".to_string(),
725 working_directory: "/tmp/test".to_string(),
726 prompt: Some(inline_prompt.to_string()),
727 system_prompt: Some("System instructions".to_string()),
728 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
729 isolation: "none".to_string(),
730 ..Default::default()
731 };
732
733 let command = build_agent_command(&options);
734 assert!(command.contains("cat"));
735 assert!(command.contains("/tmp/agent prompt.txt"));
736 assert!(command.contains("gemini"));
737 assert!(!command.contains(inline_prompt));
738 assert!(!command.contains("System instructions"));
739 }
740
741 #[test]
742 fn test_build_agent_command_opencode() {
743 let options = AgentCommandOptions {
744 tool: "opencode".to_string(),
745 working_directory: "/tmp/test".to_string(),
746 prompt: Some("Hello".to_string()),
747 json: true,
748 isolation: "none".to_string(),
749 ..Default::default()
750 };
751
752 let command = build_agent_command(&options);
753 assert!(command.contains("opencode"));
754 assert!(command.contains("run"));
755 assert!(command.contains("--format"));
756 }
757
758 #[test]
759 fn test_build_agent_command_agent_tool() {
760 let options = AgentCommandOptions {
761 tool: "agent".to_string(),
762 working_directory: "/tmp/test".to_string(),
763 model: Some("grok".to_string()),
764 isolation: "none".to_string(),
765 ..Default::default()
766 };
767
768 let command = build_agent_command(&options);
769 assert!(command.contains("agent"));
770 assert!(command.contains("--model"));
771 assert!(command.contains("opencode/grok-code"));
772 }
773
774 #[test]
775 fn test_build_agent_command_claude_read_only() {
776 let options = AgentCommandOptions {
777 tool: "claude".to_string(),
778 working_directory: "/tmp/test".to_string(),
779 prompt: Some("Plan only".to_string()),
780 read_only: true,
781 isolation: "none".to_string(),
782 ..Default::default()
783 };
784
785 let command = build_agent_command(&options);
786 assert!(command.contains("--permission-mode"));
787 assert!(command.contains("plan"));
788 assert!(!command.contains("--dangerously-skip-permissions"));
789 }
790
791 #[test]
792 fn test_build_agent_command_codex_read_only() {
793 let options = AgentCommandOptions {
794 tool: "codex".to_string(),
795 working_directory: "/tmp/test".to_string(),
796 prompt: Some("Plan only".to_string()),
797 read_only: true,
798 isolation: "none".to_string(),
799 ..Default::default()
800 };
801
802 let command = build_agent_command(&options);
803 assert!(command.contains("codex --ask-for-approval never exec"));
804 assert!(command.contains("--sandbox"));
805 assert!(command.contains("read-only"));
806 assert!(!command.contains("--dangerously-bypass-approvals-and-sandbox"));
807 }
808
809 #[test]
810 fn test_build_agent_command_opencode_read_only() {
811 let options = AgentCommandOptions {
812 tool: "opencode".to_string(),
813 working_directory: "/tmp/test".to_string(),
814 prompt: Some("Plan only".to_string()),
815 read_only: true,
816 isolation: "none".to_string(),
817 ..Default::default()
818 };
819
820 let command = build_agent_command(&options);
821 assert!(command.contains("OPENCODE_PERMISSION="));
822 assert!(command.contains("bash"));
823 assert!(command.contains("edit"));
824 assert!(command.contains("deny"));
825 }
826
827 #[test]
828 fn test_build_agent_command_agent_read_only_uses_readonly_mode() {
829 let options = AgentCommandOptions {
830 tool: "agent".to_string(),
831 working_directory: "/tmp/test".to_string(),
832 prompt: Some("Inspect only".to_string()),
833 read_only: true,
834 isolation: "none".to_string(),
835 ..Default::default()
836 };
837
838 let command = build_agent_command(&options);
839 assert!(command.contains("--permission-mode"));
840 assert!(command.contains("readonly"));
841 assert!(!command.contains("plan"));
842 }
843
844 #[test]
845 fn test_build_agent_command_agent_plan_only_uses_plan_mode() {
846 let options = AgentCommandOptions {
847 tool: "agent".to_string(),
848 working_directory: "/tmp/test".to_string(),
849 prompt: Some("Plan only".to_string()),
850 plan_only: true,
851 isolation: "none".to_string(),
852 ..Default::default()
853 };
854
855 let command = build_agent_command(&options);
856 assert!(command.contains("--permission-mode"));
857 assert!(command.contains("plan"));
858 assert!(!command.contains("readonly"));
859 }
860
861 #[test]
862 fn test_build_agent_command_agent_approve_each_uses_ask_mode() {
863 let options = AgentCommandOptions {
864 tool: "agent".to_string(),
865 working_directory: "/tmp/test".to_string(),
866 prompt: Some("Do work".to_string()),
867 approve_each: true,
868 isolation: "none".to_string(),
869 ..Default::default()
870 };
871
872 let command = build_agent_command(&options);
873 assert!(command.contains("--permission-mode"));
874 assert!(command.contains("ask"));
875 assert!(command.contains("--input-format"));
877 assert!(command.contains("stream-json"));
878 assert!(!command.contains("readonly"));
879 }
880
881 #[test]
882 fn test_build_agent_command_claude_approve_each_uses_default_mode() {
883 let options = AgentCommandOptions {
884 tool: "claude".to_string(),
885 working_directory: "/tmp/test".to_string(),
886 prompt: Some("Do work".to_string()),
887 approve_each: true,
888 isolation: "none".to_string(),
889 ..Default::default()
890 };
891
892 let command = build_agent_command(&options);
893 assert!(command.contains("--permission-mode"));
894 assert!(command.contains("default"));
895 assert!(!command.contains("--dangerously-skip-permissions"));
898 assert!(!command.contains("plan"));
899 }
900
901 #[test]
902 #[should_panic(expected = "does not support enforceable per-command approval")]
903 fn test_build_agent_command_approve_each_rejects_codex() {
904 let options = AgentCommandOptions {
905 tool: "codex".to_string(),
906 working_directory: "/tmp/test".to_string(),
907 approve_each: true,
908 isolation: "none".to_string(),
909 ..Default::default()
910 };
911
912 let _command = build_agent_command(&options);
913 }
914
915 #[test]
916 #[should_panic(expected = "does not support enforceable per-command approval")]
917 fn test_build_agent_command_approve_each_rejects_unknown_tool() {
918 let options = AgentCommandOptions {
919 tool: "unknown-tool".to_string(),
920 working_directory: "/tmp/test".to_string(),
921 approve_each: true,
922 isolation: "none".to_string(),
923 ..Default::default()
924 };
925
926 let _command = build_agent_command(&options);
927 }
928
929 #[test]
930 #[should_panic(expected = "does not support enforceable read-only mode")]
931 fn test_build_agent_command_read_only_rejects_unknown_tool() {
932 let options = AgentCommandOptions {
933 tool: "unknown-tool".to_string(),
934 working_directory: "/tmp/test".to_string(),
935 read_only: true,
936 isolation: "none".to_string(),
937 ..Default::default()
938 };
939
940 let _command = build_agent_command(&options);
941 }
942
943 #[test]
944 fn test_build_screen_stop_command() {
945 let command = build_screen_stop_command("my-session");
946 assert!(command.contains("screen"));
947 assert!(command.contains("-S \"my-session\""));
948 assert!(command.contains("-X quit"));
949 }
950
951 #[test]
952 fn test_build_docker_stop_command() {
953 let command = build_docker_stop_command("my-container");
954 assert!(command.contains("docker stop \"my-container\""));
955 assert!(command.contains("docker rm \"my-container\""));
956 }
957
958 #[test]
959 fn test_build_piped_command_basic() {
960 let command = build_piped_command("Hello World", "mycommand --flag");
961 assert!(command.contains("printf '%s'"));
962 assert!(command.contains("Hello World"));
963 assert!(command.contains("mycommand --flag"));
964 }
965
966 #[test]
967 fn test_build_piped_command_escapes_quotes() {
968 let command = build_piped_command("It's working", "mycommand");
969 assert!(command.contains("'\\''"));
970 }
971}