refactor: route zsh-fork through unified exec

This commit is contained in:
Michael Bolin
2026-03-13 10:40:18 -07:00
parent 7e58a83e89
commit 374a9a6bcb
16 changed files with 1442 additions and 173 deletions
+1
View File
@@ -103,6 +103,7 @@ fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec<Stri
&params,
invocation.session.user_shell(),
invocation.turn.tools_config.allow_login_shell,
invocation.turn.tools_config.unified_exec_backend,
)
.ok()?;
Some((command, invocation.turn.resolve_path(params.workdir)))
@@ -19,6 +19,7 @@ use crate::tools::handlers::parse_arguments_with_base_path;
use crate::tools::handlers::resolve_workdir_base_path;
use crate::tools::registry::ToolHandler;
use crate::tools::registry::ToolKind;
use crate::tools::spec::UnifiedExecBackendConfig;
use crate::unified_exec::ExecCommandRequest;
use crate::unified_exec::UnifiedExecContext;
use crate::unified_exec::UnifiedExecProcessManager;
@@ -108,6 +109,7 @@ impl ToolHandler for UnifiedExecHandler {
&params,
invocation.session.user_shell(),
invocation.turn.tools_config.allow_login_shell,
invocation.turn.tools_config.unified_exec_backend,
) {
Ok(command) => command,
Err(_) => return true,
@@ -155,6 +157,7 @@ impl ToolHandler for UnifiedExecHandler {
&args,
session.user_shell(),
turn.tools_config.allow_login_shell,
turn.tools_config.unified_exec_backend,
)
.map_err(FunctionCallError::RespondToModel)?;
@@ -321,12 +324,23 @@ pub(crate) fn get_command(
args: &ExecCommandArgs,
session_shell: Arc<Shell>,
allow_login_shell: bool,
unified_exec_backend: UnifiedExecBackendConfig,
) -> Result<Vec<String>, String> {
let model_shell = args.shell.as_ref().map(|shell_str| {
let mut shell = get_shell_by_model_provided_path(&PathBuf::from(shell_str));
shell.shell_snapshot = crate::shell::empty_shell_snapshot_receiver();
shell
});
if unified_exec_backend == UnifiedExecBackendConfig::ZshFork && args.shell.is_some() {
return Err(
"shell override is not supported when the zsh-fork backend is enabled.".to_string(),
);
}
let model_shell = if unified_exec_backend == UnifiedExecBackendConfig::ZshFork {
None
} else {
args.shell.as_ref().map(|shell_str| {
let mut shell = get_shell_by_model_provided_path(&PathBuf::from(shell_str));
shell.shell_snapshot = crate::shell::empty_shell_snapshot_receiver();
shell
})
};
let shell = model_shell.as_ref().unwrap_or(session_shell.as_ref());
let use_login_shell = match args.login {
@@ -1,12 +1,16 @@
use super::*;
use crate::shell::ShellType;
use crate::shell::default_user_shell;
use crate::shell::empty_shell_snapshot_receiver;
use crate::tools::handlers::parse_arguments_with_base_path;
use crate::tools::handlers::resolve_workdir_base_path;
use crate::tools::spec::UnifiedExecBackendConfig;
use codex_protocol::models::FileSystemPermissions;
use codex_protocol::models::PermissionProfile;
use codex_utils_absolute_path::AbsolutePathBuf;
use pretty_assertions::assert_eq;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::tempdir;
@@ -18,8 +22,13 @@ fn test_get_command_uses_default_shell_when_unspecified() -> anyhow::Result<()>
assert!(args.shell.is_none());
let command =
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
let command = get_command(
&args,
Arc::new(default_user_shell()),
true,
UnifiedExecBackendConfig::Direct,
)
.map_err(anyhow::Error::msg)?;
assert_eq!(command.len(), 3);
assert_eq!(command[2], "echo hello");
@@ -34,8 +43,13 @@ fn test_get_command_respects_explicit_bash_shell() -> anyhow::Result<()> {
assert_eq!(args.shell.as_deref(), Some("/bin/bash"));
let command =
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
let command = get_command(
&args,
Arc::new(default_user_shell()),
true,
UnifiedExecBackendConfig::Direct,
)
.map_err(anyhow::Error::msg)?;
assert_eq!(command.last(), Some(&"echo hello".to_string()));
if command
@@ -55,8 +69,13 @@ fn test_get_command_respects_explicit_powershell_shell() -> anyhow::Result<()> {
assert_eq!(args.shell.as_deref(), Some("powershell"));
let command =
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
let command = get_command(
&args,
Arc::new(default_user_shell()),
true,
UnifiedExecBackendConfig::Direct,
)
.map_err(anyhow::Error::msg)?;
assert_eq!(command[2], "echo hello");
Ok(())
@@ -70,8 +89,13 @@ fn test_get_command_respects_explicit_cmd_shell() -> anyhow::Result<()> {
assert_eq!(args.shell.as_deref(), Some("cmd"));
let command =
get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?;
let command = get_command(
&args,
Arc::new(default_user_shell()),
true,
UnifiedExecBackendConfig::Direct,
)
.map_err(anyhow::Error::msg)?;
assert_eq!(command[2], "echo hello");
Ok(())
@@ -82,8 +106,13 @@ fn test_get_command_rejects_explicit_login_when_disallowed() -> anyhow::Result<(
let json = r#"{"cmd": "echo hello", "login": true}"#;
let args: ExecCommandArgs = parse_arguments(json)?;
let err = get_command(&args, Arc::new(default_user_shell()), false)
.expect_err("explicit login should be rejected");
let err = get_command(
&args,
Arc::new(default_user_shell()),
false,
UnifiedExecBackendConfig::Direct,
)
.expect_err("explicit login should be rejected");
assert!(
err.contains("login shell is disabled by config"),
@@ -92,6 +121,30 @@ fn test_get_command_rejects_explicit_login_when_disallowed() -> anyhow::Result<(
Ok(())
}
#[test]
fn get_command_rejects_model_shell_override_for_zsh_fork_backend() -> anyhow::Result<()> {
let json = r#"{"cmd": "echo hello", "shell": "/bin/bash"}"#;
let args: ExecCommandArgs = parse_arguments(json)?;
let session_shell = Arc::new(Shell {
shell_type: ShellType::Zsh,
shell_path: PathBuf::from("/tmp/configured-zsh-fork-shell"),
shell_snapshot: empty_shell_snapshot_receiver(),
});
let err = get_command(
&args,
session_shell,
true,
UnifiedExecBackendConfig::ZshFork,
)
.expect_err("shell override should be rejected for zsh-fork backend");
assert!(
err.contains("shell override is not supported"),
"unexpected error: {err}"
);
Ok(())
}
#[test]
fn exec_command_args_resolve_relative_additional_permissions_against_workdir() -> anyhow::Result<()>
{
@@ -50,10 +50,10 @@ use codex_shell_escalation::ShellCommandExecutor;
use codex_shell_escalation::Stopwatch;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
@@ -91,7 +91,7 @@ pub(super) async fn try_run_zsh_fork(
req: &ShellRequest,
attempt: &SandboxAttempt<'_>,
ctx: &ToolCtx,
command: &[String],
shell_command: &[String],
) -> Result<Option<ExecToolCallOutput>, ToolError> {
let Some(shell_zsh_path) = ctx.session.services.shell_zsh_path.as_ref() else {
tracing::warn!("ZshFork backend specified, but shell_zsh_path is not configured.");
@@ -106,8 +106,10 @@ pub(super) async fn try_run_zsh_fork(
return Ok(None);
}
let ParsedShellCommand { script, login, .. } = extract_shell_script(shell_command)?;
let spec = build_command_spec(
command,
shell_command,
&req.cwd,
&req.env,
req.timeout_ms.into(),
@@ -119,7 +121,7 @@ pub(super) async fn try_run_zsh_fork(
.env_for(spec, req.network.as_ref())
.map_err(|err| ToolError::Codex(err.into()))?;
let crate::sandboxing::ExecRequest {
command,
command: sandbox_command,
cwd: sandbox_cwd,
env: sandbox_env,
network: sandbox_network,
@@ -133,16 +135,14 @@ pub(super) async fn try_run_zsh_fork(
justification,
arg0,
} = sandbox_exec_request;
let ParsedShellCommand { script, login, .. } = extract_shell_script(&command)?;
let host_zsh_path =
resolve_host_zsh_path(sandbox_env.get("PATH").map(String::as_str), &sandbox_cwd);
let effective_timeout = Duration::from_millis(
req.timeout_ms
.unwrap_or(crate::exec::DEFAULT_EXEC_COMMAND_TIMEOUT_MS),
);
let exec_policy = Arc::new(RwLock::new(
ctx.session.services.exec_policy.current().as_ref().clone(),
));
let command_executor = CoreShellCommandExecutor {
command,
command: sandbox_command,
cwd: sandbox_cwd,
sandbox_policy,
file_system_sandbox_policy,
@@ -163,6 +163,8 @@ pub(super) async fn try_run_zsh_fork(
.clone(),
codex_linux_sandbox_exe: ctx.turn.codex_linux_sandbox_exe.clone(),
use_legacy_landlock: ctx.turn.features.use_legacy_landlock(),
shell_zsh_path: ctx.session.services.shell_zsh_path.clone(),
host_zsh_path: host_zsh_path.clone(),
};
let main_execve_wrapper_exe = ctx
.session
@@ -191,7 +193,6 @@ pub(super) async fn try_run_zsh_fork(
req.additional_permissions_preapproved,
);
let escalation_policy = CoreShellActionProvider {
policy: Arc::clone(&exec_policy),
session: Arc::clone(&ctx.session),
turn: Arc::clone(&ctx.turn),
call_id: ctx.call_id.clone(),
@@ -204,6 +205,7 @@ pub(super) async fn try_run_zsh_fork(
approval_sandbox_permissions,
prompt_permissions: req.additional_permissions.clone(),
stopwatch: stopwatch.clone(),
host_zsh_path,
};
let escalate_server = EscalateServer::new(
@@ -224,6 +226,7 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
req: &crate::tools::runtimes::unified_exec::UnifiedExecRequest,
_attempt: &SandboxAttempt<'_>,
ctx: &ToolCtx,
shell_command: &[String],
exec_request: ExecRequest,
) -> Result<Option<PreparedUnifiedExecZshFork>, ToolError> {
let Some(shell_zsh_path) = ctx.session.services.shell_zsh_path.as_ref() else {
@@ -239,7 +242,7 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
return Ok(None);
}
let parsed = match extract_shell_script(&exec_request.command) {
let parsed = match extract_shell_script(shell_command) {
Ok(parsed) => parsed,
Err(err) => {
tracing::warn!("ZshFork unified exec fallback: {err:?}");
@@ -255,22 +258,35 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
return Ok(None);
}
let exec_policy = Arc::new(RwLock::new(
ctx.session.services.exec_policy.current().as_ref().clone(),
));
let ExecRequest {
command,
cwd,
env,
network,
expiration: _expiration,
sandbox,
windows_sandbox_level,
sandbox_permissions,
sandbox_policy,
file_system_sandbox_policy,
network_sandbox_policy,
justification,
arg0,
} = &exec_request;
let host_zsh_path = resolve_host_zsh_path(env.get("PATH").map(String::as_str), cwd);
let command_executor = CoreShellCommandExecutor {
command: exec_request.command.clone(),
cwd: exec_request.cwd.clone(),
sandbox_policy: exec_request.sandbox_policy.clone(),
file_system_sandbox_policy: exec_request.file_system_sandbox_policy.clone(),
network_sandbox_policy: exec_request.network_sandbox_policy,
sandbox: exec_request.sandbox,
env: exec_request.env.clone(),
network: exec_request.network.clone(),
windows_sandbox_level: exec_request.windows_sandbox_level,
sandbox_permissions: exec_request.sandbox_permissions,
justification: exec_request.justification.clone(),
arg0: exec_request.arg0.clone(),
command: command.clone(),
cwd: cwd.clone(),
sandbox_policy: sandbox_policy.clone(),
file_system_sandbox_policy: file_system_sandbox_policy.clone(),
network_sandbox_policy: *network_sandbox_policy,
sandbox: *sandbox,
env: env.clone(),
network: network.clone(),
windows_sandbox_level: *windows_sandbox_level,
sandbox_permissions: *sandbox_permissions,
justification: justification.clone(),
arg0: arg0.clone(),
sandbox_policy_cwd: ctx.turn.cwd.clone(),
macos_seatbelt_profile_extensions: ctx
.turn
@@ -280,6 +296,8 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
.clone(),
codex_linux_sandbox_exe: ctx.turn.codex_linux_sandbox_exe.clone(),
use_legacy_landlock: ctx.turn.features.use_legacy_landlock(),
shell_zsh_path: ctx.session.services.shell_zsh_path.clone(),
host_zsh_path: host_zsh_path.clone(),
};
let main_execve_wrapper_exe = ctx
.session
@@ -292,7 +310,6 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
)
})?;
let escalation_policy = CoreShellActionProvider {
policy: Arc::clone(&exec_policy),
session: Arc::clone(&ctx.session),
turn: Arc::clone(&ctx.turn),
call_id: ctx.call_id.clone(),
@@ -308,6 +325,7 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
),
prompt_permissions: req.additional_permissions.clone(),
stopwatch: Stopwatch::unlimited(),
host_zsh_path,
};
let escalate_server = EscalateServer::new(
@@ -327,7 +345,6 @@ pub(crate) async fn prepare_unified_exec_zsh_fork(
}
struct CoreShellActionProvider {
policy: Arc<RwLock<Policy>>,
session: Arc<crate::codex::Session>,
turn: Arc<crate::codex::TurnContext>,
call_id: String,
@@ -340,6 +357,7 @@ struct CoreShellActionProvider {
approval_sandbox_permissions: SandboxPermissions,
prompt_permissions: Option<PermissionProfile>,
stopwatch: Stopwatch,
host_zsh_path: Option<PathBuf>,
}
#[allow(clippy::large_enum_variant)]
@@ -377,6 +395,66 @@ fn execve_prompt_is_rejected_by_policy(
}
}
fn paths_match(lhs: &Path, rhs: &Path) -> bool {
lhs == rhs
|| match (lhs.canonicalize(), rhs.canonicalize()) {
(Ok(lhs), Ok(rhs)) => lhs == rhs,
_ => false,
}
}
fn resolve_host_zsh_path(_path_env: Option<&str>, _cwd: &Path) -> Option<PathBuf> {
fn canonicalize_best_effort(path: PathBuf) -> PathBuf {
path.canonicalize().unwrap_or(path)
}
fn is_executable_file(path: &Path) -> bool {
std::fs::metadata(path).is_ok_and(|metadata| {
metadata.is_file() && {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
metadata.permissions().mode() & 0o111 != 0
}
#[cfg(not(unix))]
{
true
}
}
})
}
fn find_zsh_in_dirs(dirs: impl IntoIterator<Item = PathBuf>) -> Option<PathBuf> {
dirs.into_iter().find_map(|dir| {
let candidate = dir.join("zsh");
is_executable_file(&candidate).then(|| canonicalize_best_effort(candidate))
})
}
// Keep nested-zsh rewrites limited to canonical host shell installations.
// PATH shadowing from repos, Nix environments, or tool shims should not be
// treated as the host shell.
find_zsh_in_dirs(
["/bin", "/usr/bin", "/usr/local/bin", "/opt/homebrew/bin"]
.into_iter()
.map(PathBuf::from),
)
}
fn is_unconfigured_zsh_exec(
program: &AbsolutePathBuf,
shell_zsh_path: Option<&Path>,
host_zsh_path: Option<&Path>,
) -> bool {
let Some(shell_zsh_path) = shell_zsh_path else {
return false;
};
let Some(host_zsh_path) = host_zsh_path else {
return false;
};
paths_match(program.as_path(), host_zsh_path) && !paths_match(program.as_path(), shell_zsh_path)
}
impl CoreShellActionProvider {
fn decision_driven_by_policy(matched_rules: &[RuleMatch], decision: Decision) -> bool {
matched_rules.iter().any(|rule_match| {
@@ -488,6 +566,10 @@ impl CoreShellActionProvider {
command,
workdir,
None,
// Intercepted exec prompts happen after the original tool call has
// started, so we do not attach an execpolicy amendment payload here.
// Amendments are currently surfaced only from the top-level tool
// request path.
None,
None,
additional_permissions,
@@ -712,28 +794,35 @@ impl EscalationPolicy for CoreShellActionProvider {
.await;
}
let evaluation = {
let policy = self.policy.read().await;
evaluate_intercepted_exec_policy(
&policy,
program,
argv,
InterceptedExecPolicyContext {
approval_policy: self.approval_policy,
sandbox_policy: &self.sandbox_policy,
file_system_sandbox_policy: &self.file_system_sandbox_policy,
sandbox_permissions: self.approval_sandbox_permissions,
enable_shell_wrapper_parsing:
ENABLE_INTERCEPTED_EXEC_POLICY_SHELL_WRAPPER_PARSING,
},
)
};
let policy = self.session.services.exec_policy.current();
let evaluation = evaluate_intercepted_exec_policy(
policy.as_ref(),
program,
argv,
InterceptedExecPolicyContext {
approval_policy: self.approval_policy,
sandbox_policy: &self.sandbox_policy,
file_system_sandbox_policy: &self.file_system_sandbox_policy,
sandbox_permissions: self.approval_sandbox_permissions,
enable_shell_wrapper_parsing: ENABLE_INTERCEPTED_EXEC_POLICY_SHELL_WRAPPER_PARSING,
},
);
// When true, means the Evaluation was due to *.rules, not the
// fallback function.
let decision_driven_by_policy =
Self::decision_driven_by_policy(&evaluation.matched_rules, evaluation.decision);
let needs_escalation =
self.sandbox_permissions.requires_escalated_permissions() || decision_driven_by_policy;
// Keep zsh-fork interception alive across nested shells: if an
// intercepted exec targets the known host `zsh` path instead of the
// configured zsh-fork binary, force it through escalation so the
// executor can rewrite the program path back to the configured shell.
let force_zsh_fork_reexec = is_unconfigured_zsh_exec(
program,
self.session.services.shell_zsh_path.as_deref(),
self.host_zsh_path.as_deref(),
);
let needs_escalation = self.sandbox_permissions.requires_escalated_permissions()
|| decision_driven_by_policy
|| force_zsh_fork_reexec;
let decision_source = if decision_driven_by_policy {
DecisionSource::PrefixRule
@@ -882,6 +971,8 @@ struct CoreShellCommandExecutor {
macos_seatbelt_profile_extensions: Option<MacOsSeatbeltProfileExtensions>,
codex_linux_sandbox_exe: Option<PathBuf>,
use_legacy_landlock: bool,
shell_zsh_path: Option<PathBuf>,
host_zsh_path: Option<PathBuf>,
}
struct PrepareSandboxedExecParams<'a> {
@@ -954,7 +1045,8 @@ impl ShellCommandExecutor for CoreShellCommandExecutor {
env: HashMap<String, String>,
execution: EscalationExecution,
) -> anyhow::Result<PreparedExec> {
let command = join_program_and_argv(program, argv);
let program = self.rewrite_intercepted_program_for_zsh_fork(program);
let command = join_program_and_argv(&program, argv);
let Some(first_arg) = argv.first() else {
return Err(anyhow::anyhow!(
"intercepted exec request must contain argv[0]"
@@ -1025,7 +1117,33 @@ impl ShellCommandExecutor for CoreShellCommandExecutor {
}
impl CoreShellCommandExecutor {
#[allow(clippy::too_many_arguments)]
fn rewrite_intercepted_program_for_zsh_fork(
&self,
program: &AbsolutePathBuf,
) -> AbsolutePathBuf {
let Some(shell_zsh_path) = self.shell_zsh_path.as_ref() else {
return program.clone();
};
if !is_unconfigured_zsh_exec(
program,
Some(shell_zsh_path.as_path()),
self.host_zsh_path.as_deref(),
) {
return program.clone();
}
match AbsolutePathBuf::from_absolute_path(shell_zsh_path) {
Ok(rewritten) => rewritten,
Err(err) => {
tracing::warn!(
"failed to rewrite intercepted zsh path {} to configured shell {}: {err}",
program.display(),
shell_zsh_path.display(),
);
program.clone()
}
}
}
fn prepare_sandboxed_exec(
&self,
params: PrepareSandboxedExecParams<'_>,
@@ -1102,23 +1220,21 @@ struct ParsedShellCommand {
}
fn extract_shell_script(command: &[String]) -> Result<ParsedShellCommand, ToolError> {
// Commands reaching zsh-fork can be wrapped by environment/sandbox helpers, so
// we search for the first `-c`/`-lc` triple anywhere in the argv rather
// than assuming it is the first positional form.
if let Some((program, script, login)) = command.windows(3).find_map(|parts| match parts {
[program, flag, script] if flag == "-c" => {
Some((program.to_owned(), script.to_owned(), false))
if let [program, flag, script, ..] = command {
if flag == "-c" {
return Ok(ParsedShellCommand {
program: program.to_owned(),
script: script.to_owned(),
login: false,
});
}
[program, flag, script] if flag == "-lc" => {
Some((program.to_owned(), script.to_owned(), true))
if flag == "-lc" {
return Ok(ParsedShellCommand {
program: program.to_owned(),
script: script.to_owned(),
login: true,
});
}
_ => None,
}) {
return Ok(ParsedShellCommand {
program,
script,
login,
});
}
Err(ToolError::Rejected(
@@ -6,8 +6,10 @@ use super::ParsedShellCommand;
use super::commands_for_intercepted_exec_policy;
use super::evaluate_intercepted_exec_policy;
use super::extract_shell_script;
use super::is_unconfigured_zsh_exec;
use super::join_program_and_argv;
use super::map_exec_result;
use super::resolve_host_zsh_path;
#[cfg(target_os = "macos")]
use crate::config::Constrained;
#[cfg(target_os = "macos")]
@@ -50,6 +52,7 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use pretty_assertions::assert_eq;
#[cfg(target_os = "macos")]
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
@@ -203,39 +206,16 @@ fn extract_shell_script_preserves_login_flag() {
}
#[test]
fn extract_shell_script_supports_wrapped_command_prefixes() {
assert_eq!(
extract_shell_script(&[
"/usr/bin/env".into(),
"CODEX_EXECVE_WRAPPER=1".into(),
"/bin/zsh".into(),
"-lc".into(),
"echo hello".into()
])
.unwrap(),
ParsedShellCommand {
program: "/bin/zsh".to_string(),
script: "echo hello".to_string(),
login: true,
}
);
assert_eq!(
extract_shell_script(&[
"sandbox-exec".into(),
"-p".into(),
"sandbox_policy".into(),
"/bin/zsh".into(),
"-c".into(),
"pwd".into(),
])
.unwrap(),
ParsedShellCommand {
program: "/bin/zsh".to_string(),
script: "pwd".to_string(),
login: false,
}
);
fn extract_shell_script_rejects_wrapped_command_prefixes() {
let err = extract_shell_script(&[
"/usr/bin/env".into(),
"CODEX_EXECVE_WRAPPER=1".into(),
"/bin/zsh".into(),
"-lc".into(),
"echo hello".into(),
])
.unwrap_err();
assert!(matches!(err, super::ToolError::Rejected(_)));
}
#[test]
@@ -274,6 +254,80 @@ fn join_program_and_argv_replaces_original_argv_zero() {
);
}
#[test]
fn is_unconfigured_zsh_exec_matches_non_configured_zsh_paths() {
let program = AbsolutePathBuf::try_from(host_absolute_path(&["bin", "zsh"])).unwrap();
let host = PathBuf::from(host_absolute_path(&["bin", "zsh"]));
let configured = PathBuf::from(host_absolute_path(&["tmp", "codex-zsh"]));
assert!(is_unconfigured_zsh_exec(
&program,
Some(configured.as_path()),
Some(host.as_path()),
));
}
#[test]
fn is_unconfigured_zsh_exec_ignores_non_zsh_or_configured_paths() {
let configured = PathBuf::from(host_absolute_path(&["tmp", "codex-zsh"]));
let host = PathBuf::from(host_absolute_path(&["bin", "zsh"]));
let configured_program = AbsolutePathBuf::try_from(configured.clone()).unwrap();
assert!(!is_unconfigured_zsh_exec(
&configured_program,
Some(configured.as_path()),
Some(host.as_path()),
));
let non_zsh =
AbsolutePathBuf::try_from(host_absolute_path(&["usr", "bin", "python3"])).unwrap();
assert!(!is_unconfigured_zsh_exec(
&non_zsh,
Some(configured.as_path()),
Some(host.as_path()),
));
assert!(!is_unconfigured_zsh_exec(
&non_zsh,
None,
Some(host.as_path()),
));
}
#[test]
fn is_unconfigured_zsh_exec_does_not_match_non_host_zsh_named_binaries() {
let program = AbsolutePathBuf::try_from(host_absolute_path(&["tmp", "repo", "zsh"])).unwrap();
let configured = PathBuf::from(host_absolute_path(&["tmp", "codex-zsh"]));
let host = PathBuf::from(host_absolute_path(&["bin", "zsh"]));
assert!(!is_unconfigured_zsh_exec(
&program,
Some(configured.as_path()),
Some(host.as_path()),
));
}
#[test]
fn resolve_host_zsh_path_ignores_repo_local_path_shadowing() {
let shadow_dir = tempfile::tempdir().expect("create shadow dir");
let cwd_dir = tempfile::tempdir().expect("create cwd dir");
let fake_zsh = shadow_dir.path().join("zsh");
std::fs::write(&fake_zsh, "#!/bin/sh\nexit 0\n").expect("write fake zsh");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut permissions = std::fs::metadata(&fake_zsh)
.expect("metadata for fake zsh")
.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&fake_zsh, permissions).expect("chmod fake zsh");
}
let path_env =
std::env::join_paths([shadow_dir.path(), Path::new("/usr/bin"), Path::new("/bin")])
.expect("join PATH")
.into_string()
.expect("PATH should be UTF-8");
let resolved = resolve_host_zsh_path(Some(&path_env), cwd_dir.path());
assert_ne!(resolved.as_deref(), Some(fake_zsh.as_path()));
}
#[test]
fn commands_for_intercepted_exec_policy_parses_plain_shell_wrappers() {
let program = AbsolutePathBuf::try_from(host_absolute_path(&["bin", "bash"])).unwrap();
@@ -670,6 +724,8 @@ async fn prepare_escalated_exec_turn_default_preserves_macos_seatbelt_extensions
}),
codex_linux_sandbox_exe: None,
use_legacy_landlock: false,
shell_zsh_path: None,
host_zsh_path: None,
};
let prepared = executor
@@ -719,6 +775,8 @@ async fn prepare_escalated_exec_permissions_preserve_macos_seatbelt_extensions()
macos_seatbelt_profile_extensions: None,
codex_linux_sandbox_exe: None,
use_legacy_landlock: false,
shell_zsh_path: None,
host_zsh_path: None,
};
let permissions = Permissions {
@@ -796,6 +854,8 @@ async fn prepare_escalated_exec_permission_profile_unions_turn_and_requested_mac
}),
codex_linux_sandbox_exe: None,
use_legacy_landlock: false,
shell_zsh_path: None,
host_zsh_path: None,
};
let prepared = executor
@@ -36,9 +36,10 @@ pub(crate) async fn maybe_prepare_unified_exec(
req: &UnifiedExecRequest,
attempt: &SandboxAttempt<'_>,
ctx: &ToolCtx,
shell_command: &[String],
exec_request: ExecRequest,
) -> Result<Option<PreparedUnifiedExecSpawn>, ToolError> {
imp::maybe_prepare_unified_exec(req, attempt, ctx, exec_request).await
imp::maybe_prepare_unified_exec(req, attempt, ctx, shell_command, exec_request).await
}
#[cfg(unix)]
@@ -51,21 +52,29 @@ mod imp {
#[derive(Debug)]
struct ZshForkSpawnLifecycle {
escalation_session: EscalationSession,
escalation_session: Option<EscalationSession>,
}
impl SpawnLifecycle for ZshForkSpawnLifecycle {
fn inherited_fds(&self) -> Vec<i32> {
self.escalation_session
.env()
.get(ESCALATE_SOCKET_ENV_VAR)
.as_ref()
.and_then(|escalation_session| {
escalation_session.env().get(ESCALATE_SOCKET_ENV_VAR)
})
.and_then(|fd| fd.parse().ok())
.into_iter()
.collect()
}
fn after_spawn(&mut self) {
self.escalation_session.close_client_socket();
if let Some(escalation_session) = self.escalation_session.as_ref() {
escalation_session.close_client_socket();
}
}
fn after_exit(&mut self) {
self.escalation_session = None;
}
}
@@ -82,10 +91,17 @@ mod imp {
req: &UnifiedExecRequest,
attempt: &SandboxAttempt<'_>,
ctx: &ToolCtx,
shell_command: &[String],
exec_request: ExecRequest,
) -> Result<Option<PreparedUnifiedExecSpawn>, ToolError> {
let Some(prepared) =
unix_escalation::prepare_unified_exec_zsh_fork(req, attempt, ctx, exec_request).await?
let Some(prepared) = unix_escalation::prepare_unified_exec_zsh_fork(
req,
attempt,
ctx,
shell_command,
exec_request,
)
.await?
else {
return Ok(None);
};
@@ -93,7 +109,7 @@ mod imp {
Ok(Some(PreparedUnifiedExecSpawn {
exec_request: prepared.exec_request,
spawn_lifecycle: Box::new(ZshForkSpawnLifecycle {
escalation_session: prepared.escalation_session,
escalation_session: Some(prepared.escalation_session),
}),
}))
}
@@ -117,9 +133,10 @@ mod imp {
req: &UnifiedExecRequest,
attempt: &SandboxAttempt<'_>,
ctx: &ToolCtx,
shell_command: &[String],
exec_request: ExecRequest,
) -> Result<Option<PreparedUnifiedExecSpawn>, ToolError> {
let _ = (req, attempt, ctx, exec_request);
let _ = (req, attempt, ctx, shell_command, exec_request);
Ok(None)
}
}
@@ -222,7 +222,11 @@ impl<'a> ToolRuntime<UnifiedExecRequest, UnifiedExecProcess> for UnifiedExecRunt
let exec_env = attempt
.env_for(spec, req.network.as_ref())
.map_err(|err| ToolError::Codex(err.into()))?;
match zsh_fork_backend::maybe_prepare_unified_exec(req, attempt, ctx, exec_env).await? {
match zsh_fork_backend::maybe_prepare_unified_exec(
req, attempt, ctx, &command, exec_env,
)
.await?
{
Some(prepared) => {
return self
.manager
+14 -8
View File
@@ -311,8 +311,6 @@ impl ToolsConfig {
);
let shell_type = if !features.enabled(Feature::ShellTool) {
ConfigShellToolType::Disabled
} else if features.enabled(Feature::ShellZshFork) {
ConfigShellToolType::ShellCommand
} else if features.enabled(Feature::UnifiedExec) && unified_exec_allowed {
// If ConPTY not supported (for old Windows versions), fallback on ShellCommand.
if codex_utils_pty::conpty_supported() {
@@ -323,6 +321,8 @@ impl ToolsConfig {
} else if model_info.shell_type == ConfigShellToolType::UnifiedExec && !unified_exec_allowed
{
ConfigShellToolType::ShellCommand
} else if features.enabled(Feature::ShellZshFork) {
ConfigShellToolType::ShellCommand
} else {
model_info.shell_type
};
@@ -578,6 +578,7 @@ fn create_approval_parameters(
fn create_exec_command_tool(
allow_login_shell: bool,
exec_permission_approvals_enabled: bool,
unified_exec_backend: UnifiedExecBackendConfig,
) -> ToolSpec {
let mut properties = BTreeMap::from([
(
@@ -595,12 +596,6 @@ fn create_exec_command_tool(
),
},
),
(
"shell".to_string(),
JsonSchema::String {
description: Some("Shell binary to launch. Defaults to the user's default shell.".to_string()),
},
),
(
"tty".to_string(),
JsonSchema::Boolean {
@@ -628,6 +623,16 @@ fn create_exec_command_tool(
},
),
]);
if unified_exec_backend != UnifiedExecBackendConfig::ZshFork {
properties.insert(
"shell".to_string(),
JsonSchema::String {
description: Some(
"Shell binary to launch. Defaults to the user's default shell.".to_string(),
),
},
);
}
if allow_login_shell {
properties.insert(
"login".to_string(),
@@ -2522,6 +2527,7 @@ pub(crate) fn build_specs_with_discoverable_tools(
create_exec_command_tool(
config.allow_login_shell,
exec_permission_approvals_enabled,
config.unified_exec_backend,
),
true,
config.code_mode_enabled,
+16 -3
View File
@@ -444,7 +444,7 @@ fn test_full_toolset_specs_for_gpt5_codex_unified_exec_web_search() {
// Build expected from the same helpers used by the builder.
let mut expected: BTreeMap<String, ToolSpec> = BTreeMap::from([]);
for spec in [
create_exec_command_tool(true, false),
create_exec_command_tool(true, false, UnifiedExecBackendConfig::Direct),
create_write_stdin_tool(),
PLAN_TOOL.clone(),
create_request_user_input_tool(CollaborationModesConfig::default()),
@@ -1350,7 +1350,7 @@ fn test_build_specs_default_shell_present() {
}
#[test]
fn shell_zsh_fork_prefers_shell_command_over_unified_exec() {
fn shell_zsh_fork_uses_unified_exec_when_enabled() {
let config = test_config();
let model_info = ModelsManager::construct_model_info_offline_for_tests("o3", &config);
let mut features = Features::with_defaults();
@@ -1368,7 +1368,7 @@ fn shell_zsh_fork_prefers_shell_command_over_unified_exec() {
windows_sandbox_level: WindowsSandboxLevel::Disabled,
});
assert_eq!(tools_config.shell_type, ConfigShellToolType::ShellCommand);
assert_eq!(tools_config.shell_type, ConfigShellToolType::UnifiedExec);
assert_eq!(
tools_config.shell_command_backend,
ShellCommandBackendConfig::ZshFork
@@ -1377,6 +1377,19 @@ fn shell_zsh_fork_prefers_shell_command_over_unified_exec() {
tools_config.unified_exec_backend,
UnifiedExecBackendConfig::ZshFork
);
let (tools, _) = build_specs(&tools_config, Some(HashMap::new()), None, &[]).build();
let exec_spec = find_tool(&tools, "exec_command");
let ToolSpec::Function(exec_tool) = &exec_spec.spec else {
panic!("exec_command should be a function tool spec");
};
let JsonSchema::Object { properties, .. } = &exec_tool.parameters else {
panic!("exec_command parameters should be an object schema");
};
assert!(
!properties.contains_key("shell"),
"exec_command should omit `shell` when zsh-fork backend forces the configured shell",
);
}
#[test]
+41 -3
View File
@@ -1,6 +1,7 @@
#![allow(clippy::module_inception)]
use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use tokio::sync::Mutex;
@@ -36,6 +37,13 @@ pub(crate) trait SpawnLifecycle: std::fmt::Debug + Send + Sync {
}
fn after_spawn(&mut self) {}
/// Releases resources that needed to stay alive until the child process was
/// fully launched or until the session was torn down.
///
/// This hook must tolerate being called during normal process exit as well
/// as early termination paths, and it is guaranteed to run at most once.
fn after_exit(&mut self) {}
}
pub(crate) type SpawnLifecycleHandle = Box<dyn SpawnLifecycle>;
@@ -66,7 +74,8 @@ pub(crate) struct UnifiedExecProcess {
output_drained: Arc<Notify>,
output_task: JoinHandle<()>,
sandbox_type: SandboxType,
_spawn_lifecycle: SpawnLifecycleHandle,
_spawn_lifecycle: Arc<StdMutex<SpawnLifecycleHandle>>,
spawn_lifecycle_released: Arc<AtomicBool>,
}
impl UnifiedExecProcess {
@@ -74,7 +83,7 @@ impl UnifiedExecProcess {
process_handle: ExecCommandSession,
initial_output_rx: tokio::sync::broadcast::Receiver<Vec<u8>>,
sandbox_type: SandboxType,
spawn_lifecycle: SpawnLifecycleHandle,
spawn_lifecycle: Arc<StdMutex<SpawnLifecycleHandle>>,
) -> Self {
let output_buffer = Arc::new(Mutex::new(HeadTailBuffer::default()));
let output_notify = Arc::new(Notify::new());
@@ -119,6 +128,19 @@ impl UnifiedExecProcess {
output_task,
sandbox_type,
_spawn_lifecycle: spawn_lifecycle,
spawn_lifecycle_released: Arc::new(AtomicBool::new(false)),
}
}
fn release_spawn_lifecycle(&self) {
if self
.spawn_lifecycle_released
.swap(true, std::sync::atomic::Ordering::AcqRel)
{
return;
}
if let Ok(mut lifecycle) = self._spawn_lifecycle.lock() {
lifecycle.after_exit();
}
}
@@ -157,6 +179,7 @@ impl UnifiedExecProcess {
}
pub(super) fn terminate(&self) {
self.release_spawn_lifecycle();
self.output_closed.store(true, Ordering::Release);
self.output_closed_notify.notify_waiters();
self.process_handle.terminate();
@@ -232,12 +255,19 @@ impl UnifiedExecProcess {
mut exit_rx,
} = spawned;
let output_rx = codex_utils_pty::combine_output_receivers(stdout_rx, stderr_rx);
let managed = Self::new(process_handle, output_rx, sandbox_type, spawn_lifecycle);
let spawn_lifecycle = Arc::new(StdMutex::new(spawn_lifecycle));
let managed = Self::new(
process_handle,
output_rx,
sandbox_type,
Arc::clone(&spawn_lifecycle),
);
let exit_ready = matches!(exit_rx.try_recv(), Ok(_) | Err(TryRecvError::Closed));
if exit_ready {
managed.signal_exit();
managed.release_spawn_lifecycle();
managed.check_for_sandbox_denial().await?;
return Ok(managed);
}
@@ -247,14 +277,22 @@ impl UnifiedExecProcess {
.is_ok()
{
managed.signal_exit();
managed.release_spawn_lifecycle();
managed.check_for_sandbox_denial().await?;
return Ok(managed);
}
tokio::spawn({
let cancellation_token = managed.cancellation_token.clone();
let spawn_lifecycle = Arc::clone(&spawn_lifecycle);
let spawn_lifecycle_released = Arc::clone(&managed.spawn_lifecycle_released);
async move {
let _ = exit_rx.await;
if !spawn_lifecycle_released.swap(true, Ordering::AcqRel)
&& let Ok(mut lifecycle) = spawn_lifecycle.lock()
{
lifecycle.after_exit();
}
cancellation_token.cancel();
}
});