@@ -10,10 +10,8 @@ use crate::exec_policy::create_exec_approval_requirement_for_command;
1010use crate :: features:: Feature ;
1111use crate :: function_tool:: FunctionCallError ;
1212use crate :: is_safe_command:: is_known_safe_command;
13- use crate :: powershell:: prefix_utf8_output;
1413use crate :: protocol:: ExecCommandSource ;
1514use crate :: shell:: Shell ;
16- use crate :: shell:: ShellType ;
1715use crate :: tools:: context:: ToolInvocation ;
1816use crate :: tools:: context:: ToolOutput ;
1917use crate :: tools:: context:: ToolPayload ;
@@ -46,9 +44,14 @@ impl ShellHandler {
4644}
4745
4846impl ShellCommandHandler {
49- fn base_command ( shell : & Shell , command : & str , login : Option < bool > ) -> Vec < String > {
47+ fn base_command (
48+ shell : & Shell ,
49+ command : & str ,
50+ login : Option < bool > ,
51+ powershell_utf8_enabled : bool ,
52+ ) -> Vec < String > {
5053 let use_login_shell = login. unwrap_or ( true ) ;
51- shell. derive_exec_args ( command, use_login_shell)
54+ shell. derive_exec_args ( command, use_login_shell, powershell_utf8_enabled )
5255 }
5356
5457 fn to_exec_params (
@@ -57,14 +60,12 @@ impl ShellCommandHandler {
5760 turn_context : & TurnContext ,
5861 ) -> ExecParams {
5962 let shell = session. user_shell ( ) ;
60- let command_str = if matches ! ( shell. shell_type, ShellType :: PowerShell )
61- && session. features ( ) . enabled ( Feature :: PowershellUtf8 )
62- {
63- prefix_utf8_output ( & params. command )
64- } else {
65- params. command . to_string ( )
66- } ;
67- let command = Self :: base_command ( shell. as_ref ( ) , & command_str, params. login ) ;
63+ let command = Self :: base_command (
64+ shell. as_ref ( ) ,
65+ & params. command ,
66+ params. login ,
67+ session. features ( ) . enabled ( Feature :: PowershellUtf8 ) ,
68+ ) ;
6869
6970 ExecParams {
7071 command,
@@ -171,7 +172,8 @@ impl ToolHandler for ShellCommandHandler {
171172 serde_json:: from_str :: < ShellCommandToolCallParams > ( arguments)
172173 . map ( |params| {
173174 let shell = invocation. session . user_shell ( ) ;
174- let command = Self :: base_command ( shell. as_ref ( ) , & params. command , params. login ) ;
175+ let command =
176+ Self :: base_command ( shell. as_ref ( ) , & params. command , params. login , false ) ;
175177 !is_known_safe_command ( & command)
176178 } )
177179 . unwrap_or ( true )
@@ -360,12 +362,18 @@ mod tests {
360362 }
361363
362364 fn assert_safe ( shell : & Shell , command : & str ) {
363- assert ! ( is_known_safe_command(
364- & shell. derive_exec_args( command, /* use_login_shell */ true )
365- ) ) ;
366- assert ! ( is_known_safe_command(
367- & shell. derive_exec_args( command, /* use_login_shell */ false )
368- ) ) ;
365+ assert ! ( is_known_safe_command( & shell. derive_exec_args(
366+ command, /* use_login_shell */ true , false
367+ ) ) ) ;
368+ assert ! ( is_known_safe_command( & shell. derive_exec_args(
369+ command, /* use_login_shell */ false , false
370+ ) ) ) ;
371+ assert ! ( is_known_safe_command( & shell. derive_exec_args(
372+ command, /* use_login_shell */ true , true
373+ ) ) ) ;
374+ assert ! ( is_known_safe_command( & shell. derive_exec_args(
375+ command, /* use_login_shell */ false , true
376+ ) ) ) ;
369377 }
370378
371379 #[ tokio:: test]
@@ -379,7 +387,7 @@ mod tests {
379387 let sandbox_permissions = SandboxPermissions :: RequireEscalated ;
380388 let justification = Some ( "because tests" . to_string ( ) ) ;
381389
382- let expected_command = session. user_shell ( ) . derive_exec_args ( & command, true ) ;
390+ let expected_command = session. user_shell ( ) . derive_exec_args ( & command, true , false ) ;
383391 let expected_cwd = turn_context. resolve_path ( workdir. clone ( ) ) ;
384392 let expected_env = create_env ( & turn_context. shell_environment_policy ) ;
385393
@@ -415,17 +423,17 @@ mod tests {
415423 } ;
416424
417425 let login_command =
418- ShellCommandHandler :: base_command ( & shell, "echo login shell" , Some ( true ) ) ;
426+ ShellCommandHandler :: base_command ( & shell, "echo login shell" , Some ( true ) , false ) ;
419427 assert_eq ! (
420428 login_command,
421- shell. derive_exec_args( "echo login shell" , true )
429+ shell. derive_exec_args( "echo login shell" , true , false )
422430 ) ;
423431
424432 let non_login_command =
425- ShellCommandHandler :: base_command ( & shell, "echo non login shell" , Some ( false ) ) ;
433+ ShellCommandHandler :: base_command ( & shell, "echo non login shell" , Some ( false ) , false ) ;
426434 assert_eq ! (
427435 non_login_command,
428- shell. derive_exec_args( "echo non login shell" , false )
436+ shell. derive_exec_args( "echo non login shell" , false , false )
429437 ) ;
430438 }
431439}
0 commit comments