1use crate::tools::shell::escape_arg;
4use crate::tool_overrides::ToolOverrides;
6use crate::tools::launch_options::{
7 check_launch_options_against, launch_capabilities, LaunchOptions,
8};
9use crate::tools::{
10 agent::{self, AgentBuildOptions},
11 claude::{self, ClaudeBuildOptions},
12 codex::{self, CodexBuildOptions},
13 gemini::{self, GeminiBuildOptions},
14 opencode::{self, OpencodeBuildOptions},
15 qwen::{self, QwenBuildOptions},
16};
17use command_stream::quote::{escape_for_double_quotes, escape_for_single_quotes};
18
19#[derive(Debug, Clone, Default)]
21pub struct AgentCommandOptions {
22 pub tool: String,
23 pub working_directory: String,
24 pub prompt: Option<String>,
25 pub prompt_file: Option<String>,
26 pub system_prompt: Option<String>,
27 pub append_system_prompt: Option<String>,
28 pub model: Option<String>,
29 pub fallback_model: Option<String>,
30 pub json: bool,
31 pub verbose: bool,
32 pub replay_user_messages: bool,
33 pub resume: Option<String>,
34 pub session_id: Option<String>,
35 pub fork_session: bool,
36 pub read_only: bool,
37 pub plan_only: bool,
38 pub approve_each: bool,
40 pub stream_input: bool,
43 pub executable: Option<String>,
44 pub extra_args: Vec<String>,
45 pub extra_env: Vec<(String, String)>,
46 pub unset_env: Vec<String>,
48 pub skip_default_safety_flags: bool,
49 pub isolation: String,
50 pub screen_name: Option<String>,
51 pub container_name: Option<String>,
52 pub detached: bool,
53 pub docker: crate::DockerOptions,
55 pub docker_tty: bool,
57 pub docker_cid_file: Option<String>,
59 pub docker_env: Vec<String>,
61 pub launch: LaunchOptions,
63 pub overrides: ToolOverrides,
65}
66
67pub fn supports_read_only(tool: &str) -> bool {
69 matches!(
70 tool,
71 "claude" | "codex" | "opencode" | "gemini" | "qwen" | "agent"
72 )
73}
74
75pub fn read_only_unsupported_error(tool: &str) -> String {
77 format!(
78 "Tool \"{}\" does not support enforceable read-only mode. Choose one of: claude, codex, opencode, gemini, qwen, agent; or run without --read-only.",
79 tool
80 )
81}
82
83fn build_tool_command(tool: &str, prompt: Option<&str>, system_prompt: Option<&str>) -> String {
85 let mut command = tool.to_string();
86
87 if let Some(p) = prompt.filter(|p| !p.is_empty()) {
88 command.push_str(&format!(" --prompt {}", escape_arg(p)));
89 }
90
91 if let Some(sp) = system_prompt.filter(|sp| !sp.is_empty()) {
92 command.push_str(&format!(" --system-prompt {}", escape_arg(sp)));
93 }
94
95 command
96}
97
98fn build_screen_command(base_command: &str, screen_name: Option<&str>, detached: bool) -> String {
100 let session_name = screen_name.map(|s| s.to_string()).unwrap_or_else(|| {
101 format!(
102 "agent-{}",
103 std::time::SystemTime::now()
104 .duration_since(std::time::UNIX_EPOCH)
105 .unwrap()
106 .as_millis()
107 )
108 });
109
110 if detached {
111 format!(
113 "screen -dmS \"{}\" bash -c '{}'",
114 session_name,
115 escape_for_single_quotes(base_command)
116 )
117 } else {
118 format!(
120 "screen -S \"{}\" bash -c '{}'",
121 session_name,
122 escape_for_single_quotes(base_command)
123 )
124 }
125}
126
127fn docker_env_names(options: &AgentCommandOptions) -> Vec<String> {
130 let mut names = options.docker_env.clone();
131 for (name, _) in &options.extra_env {
132 assert!(
133 crate::tools::shell::is_valid_env_name(name),
134 "invalid environment variable name: {name}"
135 );
136 if !names.contains(name) {
137 names.push(name.clone());
138 }
139 }
140 if let Some(gateway) = &options.launch.gateway {
141 if !names.contains(&gateway.token_env) {
142 names.push(gateway.token_env.clone());
143 }
144 }
145 if let Some(variable) = crate::openai_compatible_gateway::config_variable(
146 &options.tool,
147 options.launch.gateway.as_ref(),
148 ) {
149 if !names.iter().any(|name| name == variable) {
150 names.push(variable.to_owned());
151 }
152 }
153 names
154}
155
156pub fn build_agent_command(options: &AgentCommandOptions) -> String {
164 let mut docker_options;
165 let options = if options.isolation == "docker" {
166 docker_options = options.clone();
167 docker_options.working_directory = std::path::absolute(&options.working_directory)
168 .expect("Docker working directory")
169 .to_string_lossy()
170 .into_owned();
171 &docker_options
172 } else {
173 options
174 };
175 let read_only_requested = options.read_only || options.plan_only;
178
179 assert!(
180 !(read_only_requested && !supports_read_only(&options.tool)),
181 "{}",
182 read_only_unsupported_error(&options.tool)
183 );
184
185 assert!(
189 !(options.approve_each && !crate::permissions::supports_ask(&options.tool)),
190 "{}",
191 crate::permissions::ask_unsupported_error(&options.tool)
192 );
193
194 let capabilities = options
195 .overrides
196 .launch_capabilities
197 .unwrap_or_else(|| launch_capabilities(&options.tool));
198 if let Err(error) = check_launch_options_against(&options.tool, &options.launch, &capabilities)
199 {
200 panic!("{error}");
201 }
202
203 let base_command = match &options.overrides.build_command {
205 Some(build) => build(options),
206 None => build_base_command(options),
207 };
208
209 let mut full_command = format!(
211 "bash -c \"cd {} && {}\"",
212 escape_for_double_quotes(&escape_arg(&options.working_directory)),
213 escape_for_double_quotes(&base_command)
214 );
215
216 match options.isolation.as_str() {
218 "screen" => {
219 full_command = build_screen_command(
220 &full_command,
221 options.screen_name.as_deref(),
222 options.detached,
223 );
224 }
225 "docker" => {
226 full_command =
227 crate::docker::build_command(&full_command, options, &docker_env_names(options));
228 }
229 _ => {}
230 }
231
232 full_command
233}
234
235pub fn build_base_command(options: &AgentCommandOptions) -> String {
238 let read_only_requested = options.read_only || options.plan_only;
241 let gateway_model = || {
242 crate::openai_compatible_gateway::model(
243 &options.tool,
244 options.launch.gateway.as_ref(),
245 options.model.as_deref(),
246 )
247 .unwrap_or_else(|error| panic!("{error}"))
248 };
249 match options.tool.as_str() {
250 "claude" => claude::build_command(&ClaudeBuildOptions {
251 prompt: options.prompt.clone(),
252 prompt_file: options.prompt_file.clone(),
253 system_prompt: options.system_prompt.clone(),
254 append_system_prompt: options.append_system_prompt.clone(),
255 model: options.model.clone(),
256 fallback_model: options.fallback_model.clone(),
257 json: options.json,
258 json_input: options.stream_input,
259 verbose: options.verbose,
260 replay_user_messages: options.replay_user_messages,
261 resume: options.resume.clone(),
262 session_id: options.session_id.clone(),
263 fork_session: options.fork_session,
264 print: false,
265 read_only: read_only_requested,
266 approve_each: options.approve_each,
267 executable: options.executable.clone(),
268 extra_env: options.extra_env.clone(),
269 unset_env: options.unset_env.clone(),
270 extra_args: options.extra_args.clone(),
271 skip_default_safety_flags: options.skip_default_safety_flags,
272 permission_mode: None,
273 stream_input: options.stream_input,
274 launch: options.launch.clone(),
275 }),
276 "codex" => codex::build_command(&CodexBuildOptions {
277 prompt: options.prompt.clone(),
278 prompt_file: options.prompt_file.clone(),
279 system_prompt: options.system_prompt.clone(),
280 model: options.model.clone(),
281 json: options.json,
282 resume: options.resume.clone(),
283 read_only: read_only_requested,
284 executable: options.executable.clone(),
285 extra_env: options.extra_env.clone(),
286 unset_env: options.unset_env.clone(),
287 extra_args: options.extra_args.clone(),
288 skip_default_safety_flags: options.skip_default_safety_flags,
289 sandbox_mode: None,
290 approval_mode: None,
291 launch: options.launch.clone(),
292 }),
293 "opencode" => opencode::build_command(&OpencodeBuildOptions {
294 prompt: options.prompt.clone(),
295 prompt_file: options.prompt_file.clone(),
296 system_prompt: options.system_prompt.clone(),
297 model: gateway_model(),
298 json: options.json,
299 resume: options.resume.clone(),
300 read_only: read_only_requested,
301 executable: options.executable.clone(),
302 extra_env: options.extra_env.clone(),
303 unset_env: options.unset_env.clone(),
304 extra_args: options.extra_args.clone(),
305 launch: options.launch.clone(),
306 }),
307 "agent" => agent::build_command(&AgentBuildOptions {
308 resume: options.resume.clone(),
309 prompt: options.prompt.clone(),
310 prompt_file: options.prompt_file.clone(),
311 system_prompt: options.system_prompt.clone(),
312 model: gateway_model(),
313 temperature: options.launch.temperature,
314 compact_json: false,
315 use_existing_claude_oauth: false,
316 read_only: read_only_requested,
317 plan_only: options.plan_only,
318 approve_each: options.approve_each,
319 permission_mode: None,
320 permission: None,
321 stream_input: options.stream_input,
322 json_input: options.stream_input,
323 executable: options.executable.clone(),
324 extra_env: options.extra_env.clone(),
325 unset_env: options.unset_env.clone(),
326 extra_args: options.extra_args.clone(),
327 }),
328 "gemini" => {
329 let options = GeminiBuildOptions {
330 resume: options.resume.clone(),
331 prompt: options.prompt.clone(),
332 prompt_file: options.prompt_file.clone(),
333 system_prompt: options.system_prompt.clone(),
334 model: options.model.clone(),
335 json: options.json,
336 read_only: read_only_requested,
337 executable: options.executable.clone(),
338 extra_env: options.extra_env.clone(),
339 unset_env: options.unset_env.clone(),
340 extra_args: options.extra_args.clone(),
341 skip_default_safety_flags: options.skip_default_safety_flags,
342 launch: options.launch.clone(),
343 ..GeminiBuildOptions::new()
344 };
345 gemini::build_command(&options)
346 }
347 "qwen" => {
348 let options = QwenBuildOptions {
349 prompt: options.prompt.clone(),
350 prompt_file: options.prompt_file.clone(),
351 system_prompt: options.system_prompt.clone(),
352 model: options.model.clone(),
353 json: options.json,
354 resume: options.resume.clone(),
355 read_only: read_only_requested,
356 executable: options.executable.clone(),
357 extra_env: options.extra_env.clone(),
358 unset_env: options.unset_env.clone(),
359 extra_args: options.extra_args.clone(),
360 skip_default_safety_flags: options.skip_default_safety_flags,
361 launch: options.launch.clone(),
362 json_input: options.stream_input,
363 stream_input: options.stream_input,
364 ..QwenBuildOptions::new()
365 };
366 qwen::build_command(&options)
367 }
368 _ => build_tool_command(
370 &options.tool,
371 options.prompt.as_deref(),
372 options.system_prompt.as_deref(),
373 ),
374 }
375}
376
377pub fn build_screen_stop_command(screen_name: &str) -> String {
385 format!("screen -S \"{}\" -X quit", screen_name)
386}
387
388pub fn build_docker_stop_command(container_name: &str) -> String {
396 format!("docker stop -- {}", escape_arg(container_name))
397}
398
399pub fn build_piped_command(input: &str, command: &str) -> String {
408 let escaped_input = escape_for_single_quotes(input);
409 format!("printf '%s' '{}' | {}", escaped_input, command)
410}
411
412#[cfg(test)]
413mod tests {
414 use super::*;
415
416 #[test]
417 fn test_build_agent_command_basic_claude() {
418 let options = AgentCommandOptions {
419 tool: "claude".to_string(),
420 working_directory: "/tmp/test".to_string(),
421 prompt: Some("Hello".to_string()),
422 isolation: "none".to_string(),
423 ..Default::default()
424 };
425
426 let command = build_agent_command(&options);
427 assert!(command.contains("bash -c"));
428 assert!(command.contains("cd"));
429 assert!(command.contains("/tmp/test"));
430 assert!(command.contains("claude"));
431 assert!(command.contains("-p"));
432 assert!(command.contains("Hello"));
433 }
434
435 #[test]
436 fn test_build_agent_command_with_system_prompt() {
437 let options = AgentCommandOptions {
438 tool: "claude".to_string(),
439 working_directory: "/tmp/test".to_string(),
440 prompt: Some("Hello".to_string()),
441 system_prompt: Some("You are helpful".to_string()),
442 isolation: "none".to_string(),
443 ..Default::default()
444 };
445
446 let command = build_agent_command(&options);
447 assert!(command.contains("-p"));
448 assert!(command.contains("--system-prompt"));
449 assert!(command.contains("You are helpful"));
450 }
451
452 #[test]
453 fn test_build_agent_command_claude_with_fallback_model() {
454 let options = AgentCommandOptions {
455 tool: "claude".to_string(),
456 working_directory: "/tmp/test".to_string(),
457 model: Some("opus".to_string()),
458 fallback_model: Some("sonnet".to_string()),
459 isolation: "none".to_string(),
460 ..Default::default()
461 };
462
463 let command = build_agent_command(&options);
464 assert!(command.contains("--model"));
465 assert!(command.contains("--model opus"));
466 assert!(command.contains("--fallback-model"));
467 assert!(command.contains("--fallback-model sonnet"));
468 }
469
470 #[test]
471 fn test_build_agent_command_claude_with_append_system_prompt() {
472 let options = AgentCommandOptions {
473 tool: "claude".to_string(),
474 working_directory: "/tmp/test".to_string(),
475 append_system_prompt: Some("Extra instructions".to_string()),
476 isolation: "none".to_string(),
477 ..Default::default()
478 };
479
480 let command = build_agent_command(&options);
481 assert!(command.contains("--append-system-prompt"));
482 assert!(command.contains("Extra instructions"));
483 }
484
485 #[test]
486 fn test_build_agent_command_claude_with_session_management() {
487 let options = AgentCommandOptions {
488 tool: "claude".to_string(),
489 working_directory: "/tmp/test".to_string(),
490 resume: Some("abc123".to_string()),
491 session_id: Some("123e4567-e89b-12d3-a456-426614174000".to_string()),
492 fork_session: true,
493 isolation: "none".to_string(),
494 ..Default::default()
495 };
496
497 let command = build_agent_command(&options);
498 assert!(command.contains("--resume"));
499 assert!(command.contains("abc123"));
500 assert!(command.contains("--session-id"));
501 assert!(command.contains("123e4567-e89b-12d3-a456-426614174000"));
502 assert!(command.contains("--fork-session"));
503 }
504
505 #[test]
506 fn test_build_agent_command_claude_with_verbose_streaming() {
507 let options = AgentCommandOptions {
508 tool: "claude".to_string(),
509 working_directory: "/tmp/test".to_string(),
510 verbose: true,
511 replay_user_messages: true,
512 isolation: "none".to_string(),
513 ..Default::default()
514 };
515
516 let command = build_agent_command(&options);
517 assert!(command.contains("--verbose"));
518 assert!(command.contains("--replay-user-messages"));
519 }
520
521 #[test]
522 fn test_build_agent_command_claude_raw_passthrough() {
523 let options = AgentCommandOptions {
524 tool: "claude".to_string(),
525 working_directory: "/tmp/test".to_string(),
526 prompt: Some("Hello".to_string()),
527 executable: Some("/opt/Claude Code/bin/claude".to_string()),
528 extra_env: vec![
529 (
530 "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC".to_string(),
531 "1".to_string(),
532 ),
533 ("MCP_TIMEOUT".to_string(), "10000".to_string()),
534 ],
535 extra_args: vec![
536 "--mcp-config".to_string(),
537 "/tmp/mcp config.json".to_string(),
538 "--permission-mode".to_string(),
539 "default".to_string(),
540 ],
541 skip_default_safety_flags: true,
542 isolation: "none".to_string(),
543 ..Default::default()
544 };
545
546 let command = build_agent_command(&options);
547 assert!(!command.contains("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC"));
549 assert!(!command.contains("MCP_TIMEOUT"));
550 assert!(command.contains("/opt/Claude Code/bin/claude"));
551 assert!(command.contains("--mcp-config"));
552 assert!(command.contains("/tmp/mcp config.json"));
553 assert!(command.contains("--permission-mode"));
554 assert!(command.contains("default"));
555 assert!(!command.contains("--dangerously-skip-permissions"));
556 }
557
558 #[test]
559 fn test_docker_forwards_extra_env_by_name_only() {
560 let options = AgentCommandOptions {
561 tool: "claude".to_string(),
562 working_directory: "/tmp/test".to_string(),
563 prompt: Some("Hello".to_string()),
564 extra_env: vec![("API_TOKEN".to_string(), "sk-secret".to_string())],
565 isolation: "docker".to_string(),
566 docker: crate::DockerOptions {
567 image: Some("example/tool:1".into()),
568 ..Default::default()
569 },
570 container_name: Some("c".to_string()),
571 ..Default::default()
572 };
573 let command = build_agent_command(&options);
574 assert!(command.contains(" -e API_TOKEN "));
575 assert!(!command.contains("sk-secret"));
576 }
577
578 #[test]
579 fn test_claude_prompt_and_schema_files_replace_inline_values() {
580 let options = AgentCommandOptions {
581 tool: "claude".to_string(),
582 working_directory: "/tmp/test".to_string(),
583 prompt: Some("Hello".to_string()),
584 system_prompt: Some("PRIVATE-SYSTEM".to_string()),
585 append_system_prompt: Some("PRIVATE-APPEND".to_string()),
586 launch: crate::tools::launch_options::LaunchOptions {
587 json_schema: Some(r#"{"type":"object","secretField":1}"#.to_string()),
588 json_schema_file: Some("/tmp/d/schema.json".to_string()),
589 system_prompt_file: Some("/tmp/d/system-prompt.txt".to_string()),
590 append_system_prompt_file: Some("/tmp/d/append.txt".to_string()),
591 ..Default::default()
592 },
593 ..Default::default()
594 };
595 let command = build_agent_command(&options);
596 assert!(!command.contains("PRIVATE"));
597 assert!(!command.contains("secretField"));
598 assert!(command.contains("--system-prompt-file /tmp/d/system-prompt.txt"));
599 assert!(command.contains("--append-system-prompt-file /tmp/d/append.txt"));
600 assert!(
601 command.contains("--json-schema \\\"\\$(cat /tmp/d/schema.json)\\\""),
602 "{command}"
603 );
604 }
605
606 #[test]
607 fn test_build_agent_command_qwen_raw_passthrough() {
608 let options = AgentCommandOptions {
609 tool: "qwen".to_string(),
610 working_directory: "/tmp/test".to_string(),
611 prompt_file: Some("/tmp/prompt.txt".to_string()),
612 executable: Some("/opt/qwen code/qwen".to_string()),
613 extra_env: vec![("QWEN_HOME".to_string(), "/tmp/qwen home".to_string())],
614 extra_args: vec![
615 "--checkpointing".to_string(),
616 "--approval-mode".to_string(),
617 "default".to_string(),
618 ],
619 skip_default_safety_flags: true,
620 isolation: "none".to_string(),
621 ..Default::default()
622 };
623
624 let command = build_agent_command(&options);
625 assert!(command.contains("cat"));
626 assert!(command.contains("/tmp/prompt.txt"));
627 assert!(!command.contains("QWEN_HOME"));
628 assert!(command.contains("/opt/qwen code/qwen"));
629 assert!(command.contains("--checkpointing"));
630 assert!(command.contains("--approval-mode"));
631 assert!(command.contains("default"));
632 assert!(!command.contains("--yolo"));
633 }
634
635 #[test]
636 fn test_build_agent_command_gemini_raw_passthrough() {
637 let options = AgentCommandOptions {
638 tool: "gemini".to_string(),
639 working_directory: "/tmp/test".to_string(),
640 prompt_file: Some("/tmp/prompt.txt".to_string()),
641 executable: Some("/opt/gemini cli/gemini".to_string()),
642 extra_env: vec![("GEMINI_HOME".to_string(), "/tmp/gemini home".to_string())],
643 extra_args: vec!["--telemetry".to_string(), "false".to_string()],
644 skip_default_safety_flags: true,
645 isolation: "none".to_string(),
646 ..Default::default()
647 };
648
649 let command = build_agent_command(&options);
650 assert!(command.contains("cat"));
651 assert!(command.contains("/tmp/prompt.txt"));
652 assert!(!command.contains("GEMINI_HOME"));
653 assert!(command.contains("/opt/gemini cli/gemini"));
654 assert!(command.contains("--telemetry"));
655 assert!(command.contains("false"));
656 assert!(!command.contains("--yolo"));
657 }
658
659 #[test]
660 fn test_build_agent_command_unknown_tool() {
661 let options = AgentCommandOptions {
662 tool: "unknown-tool".to_string(),
663 working_directory: "/tmp/test".to_string(),
664 prompt: Some("Hello".to_string()),
665 isolation: "none".to_string(),
666 ..Default::default()
667 };
668
669 let command = build_agent_command(&options);
670 assert!(command.contains("bash -c"));
671 assert!(command.contains("unknown-tool"));
672 assert!(command.contains("--prompt"));
673 }
674
675 #[test]
676 fn test_build_agent_command_screen_isolation() {
677 let options = AgentCommandOptions {
678 tool: "claude".to_string(),
679 working_directory: "/tmp/test".to_string(),
680 isolation: "screen".to_string(),
681 screen_name: Some("my-session".to_string()),
682 detached: true,
683 ..Default::default()
684 };
685
686 let command = build_agent_command(&options);
687 assert!(command.contains("screen"));
688 assert!(command.contains("-dmS"));
689 assert!(command.contains("my-session"));
690 }
691
692 #[test]
693 fn test_build_agent_command_docker_isolation() {
694 let options = AgentCommandOptions {
695 tool: "claude".to_string(),
696 working_directory: "/tmp/test".to_string(),
697 isolation: "docker".to_string(),
698 docker: crate::DockerOptions {
699 image: Some("example/tool:1".into()),
700 ..Default::default()
701 },
702 container_name: Some("my-container".to_string()),
703 detached: true,
704 ..Default::default()
705 };
706
707 let command = build_agent_command(&options);
708 assert!(command.contains("docker run"));
709 assert!(command.contains("-d"));
710 assert!(command.contains("--name my-container"));
711 let mount_path = std::path::absolute(&options.working_directory).unwrap();
712 let path = mount_path.display();
713 assert!(command.contains(&format!("-v {}", escape_arg(&format!("{path}:{path}")))));
714 }
715
716 #[test]
717 fn test_build_agent_command_with_model() {
718 let options = AgentCommandOptions {
719 tool: "claude".to_string(),
720 working_directory: "/tmp/test".to_string(),
721 model: Some("opus".to_string()),
722 isolation: "none".to_string(),
723 ..Default::default()
724 };
725
726 let command = build_agent_command(&options);
727 assert!(command.contains("--model"));
728 assert!(command.contains("--model opus"));
729 }
730
731 #[test]
732 fn test_build_agent_command_codex() {
733 let options = AgentCommandOptions {
734 tool: "codex".to_string(),
735 working_directory: "/tmp/test".to_string(),
736 prompt: Some("Hello".to_string()),
737 json: true,
738 isolation: "none".to_string(),
739 ..Default::default()
740 };
741
742 let command = build_agent_command(&options);
743 assert!(command.contains("codex"));
744 assert!(command.contains("exec"));
745 assert!(command.contains("--json"));
746 }
747
748 #[test]
749 fn test_build_agent_command_codex_prompt_file() {
750 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
751 let options = AgentCommandOptions {
752 tool: "codex".to_string(),
753 working_directory: "/tmp/test".to_string(),
754 prompt: Some(inline_prompt.to_string()),
755 system_prompt: Some("System instructions".to_string()),
756 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
757 json: true,
758 isolation: "none".to_string(),
759 ..Default::default()
760 };
761
762 let command = build_agent_command(&options);
763 assert!(command.contains("cat"));
764 assert!(command.contains("/tmp/agent prompt.txt"));
765 assert!(command.contains("codex"));
766 assert!(!command.contains(inline_prompt));
767 assert!(!command.contains("System instructions"));
768 }
769
770 #[test]
771 fn test_build_agent_command_claude_prompt_file() {
772 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
773 let options = AgentCommandOptions {
774 tool: "claude".to_string(),
775 working_directory: "/tmp/test".to_string(),
776 prompt: Some(inline_prompt.to_string()),
777 system_prompt: Some("You are helpful".to_string()),
778 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
779 isolation: "none".to_string(),
780 ..Default::default()
781 };
782
783 let command = build_agent_command(&options);
784 assert!(command.contains("cat"));
785 assert!(command.contains("/tmp/agent prompt.txt"));
786 assert!(command.contains("claude"));
787 assert!(command.contains("--system-prompt"));
788 assert!(command.contains("You are helpful"));
789 assert!(!command.contains(inline_prompt));
790 }
791
792 #[test]
793 fn test_build_agent_command_qwen_prompt_file() {
794 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
795 let options = AgentCommandOptions {
796 tool: "qwen".to_string(),
797 working_directory: "/tmp/test".to_string(),
798 prompt: Some(inline_prompt.to_string()),
799 system_prompt: Some("System instructions".to_string()),
800 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
801 isolation: "none".to_string(),
802 ..Default::default()
803 };
804
805 let command = build_agent_command(&options);
806 assert!(command.contains("cat"));
807 assert!(command.contains("/tmp/agent prompt.txt"));
808 assert!(command.contains("qwen"));
809 assert!(!command.contains(inline_prompt));
810 assert!(!command.contains("System instructions"));
811 }
812
813 #[test]
814 fn test_build_agent_command_gemini_prompt_file() {
815 let inline_prompt = "Secret prompt with 'quotes', $HOME, and `pwd`";
816 let options = AgentCommandOptions {
817 tool: "gemini".to_string(),
818 working_directory: "/tmp/test".to_string(),
819 prompt: Some(inline_prompt.to_string()),
820 system_prompt: Some("System instructions".to_string()),
821 prompt_file: Some("/tmp/agent prompt.txt".to_string()),
822 isolation: "none".to_string(),
823 ..Default::default()
824 };
825
826 let command = build_agent_command(&options);
827 assert!(command.contains("cat"));
828 assert!(command.contains("/tmp/agent prompt.txt"));
829 assert!(command.contains("gemini"));
830 assert!(!command.contains(inline_prompt));
831 assert!(!command.contains("System instructions"));
832 }
833
834 #[test]
835 fn test_build_agent_command_opencode() {
836 let options = AgentCommandOptions {
837 tool: "opencode".to_string(),
838 working_directory: "/tmp/test".to_string(),
839 prompt: Some("Hello".to_string()),
840 json: true,
841 isolation: "none".to_string(),
842 ..Default::default()
843 };
844
845 let command = build_agent_command(&options);
846 assert!(command.contains("opencode"));
847 assert!(command.contains("run"));
848 assert!(command.contains("--format"));
849 }
850
851 #[test]
852 fn test_build_agent_command_agent_tool() {
853 let options = AgentCommandOptions {
854 tool: "agent".to_string(),
855 working_directory: "/tmp/test".to_string(),
856 model: Some("grok".to_string()),
857 isolation: "none".to_string(),
858 ..Default::default()
859 };
860
861 let command = build_agent_command(&options);
862 assert!(command.contains("agent"));
863 assert!(command.contains("--model"));
864 assert!(command.contains("opencode/grok-code"));
865 }
866
867 #[test]
868 fn test_build_screen_stop_command() {
869 let command = build_screen_stop_command("my-session");
870 assert!(command.contains("screen"));
871 assert!(command.contains("-S \"my-session\""));
872 assert!(command.contains("-X quit"));
873 }
874
875 #[test]
876 fn test_build_docker_stop_command() {
877 let command = build_docker_stop_command("my-container");
878 assert_eq!(command, "docker stop -- my-container");
879 }
880
881 #[test]
882 fn test_build_piped_command_basic() {
883 let command = build_piped_command("Hello World", "mycommand --flag");
884 assert!(command.contains("printf '%s'"));
885 assert!(command.contains("Hello World"));
886 assert!(command.contains("mycommand --flag"));
887 }
888
889 #[test]
890 fn test_build_piped_command_escapes_quotes() {
891 let command = build_piped_command("It's working", "mycommand");
892 assert!(command.contains("'\\''"));
893 }
894}