diff --git a/codex-rs/core/src/tools/runtimes/mod.rs b/codex-rs/core/src/tools/runtimes/mod.rs index 1a83ef5657..d53dc9b1cc 100644 --- a/codex-rs/core/src/tools/runtimes/mod.rs +++ b/codex-rs/core/src/tools/runtimes/mod.rs @@ -10,6 +10,7 @@ use crate::sandboxing::SandboxPermissions; use crate::shell::Shell; use crate::shell::ShellType; use crate::tools::sandboxing::ToolError; +use codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR; #[cfg(unix)] use codex_install_context::InstallContext; #[cfg(target_os = "macos")] @@ -288,14 +289,23 @@ pub(crate) fn maybe_wrap_shell_lc_with_snapshot( .map(|arg| format!(" '{}'", shell_single_quote(arg))) .collect::(); let mut override_env = explicit_env_overrides.clone(); - for key in [CODEX_THREAD_ID_ENV_VAR, CODEX_PERMISSION_PROFILE_ENV_VAR] { + for key in [ + CODEX_THREAD_ID_ENV_VAR, + CODEX_PERMISSION_PROFILE_ENV_VAR, + CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR, + ] { if let Some(value) = env.get(key) { override_env.insert(key.to_string(), value.clone()); } } - // Do not let a snapshot resurrect a stale profile when no named profile is active. - let (override_captures, override_exports) = - build_override_exports(&override_env, &[CODEX_PERMISSION_PROFILE_ENV_VAR]); + // Do not let a snapshot resurrect stale runtime state when it is inactive. + let (override_captures, override_exports) = build_override_exports( + &override_env, + &[ + CODEX_PERMISSION_PROFILE_ENV_VAR, + CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR, + ], + ); let (proxy_captures, proxy_exports) = build_proxy_env_exports(); let runtime_path_prepend_exports = runtime_path_prepends.shell_exports_after_snapshot(explicit_env_overrides); diff --git a/codex-rs/core/src/tools/runtimes/mod_tests.rs b/codex-rs/core/src/tools/runtimes/mod_tests.rs index f6ec38f6c2..539ad60a44 100644 --- a/codex-rs/core/src/tools/runtimes/mod_tests.rs +++ b/codex-rs/core/src/tools/runtimes/mod_tests.rs @@ -607,6 +607,61 @@ fn maybe_wrap_shell_lc_with_snapshot_unsets_absent_permission_profile() { assert_eq!(output.stdout, b""); } +#[test] +fn maybe_wrap_shell_lc_with_snapshot_restores_apply_patch_rollout_state() { + let dir = tempdir().expect("create temp dir"); + let snapshot_path = dir.path().join("snapshot.sh"); + std::fs::write( + &snapshot_path, + "# Snapshot file\nexport CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS='stale'\n", + ) + .expect("write snapshot"); + let (session_shell, shell_snapshot) = + shell_with_snapshot(ShellType::Bash, "/bin/bash", snapshot_path.abs()); + let command = vec![ + "/bin/bash".to_string(), + "-lc".to_string(), + "printenv CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS".to_string(), + ]; + let env = HashMap::from([( + CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(), + "1".to_string(), + )]); + let rewritten = maybe_wrap_shell_lc_with_snapshot( + &command, + &session_shell, + Some(&shell_snapshot), + &HashMap::new(), + &env, + &RuntimePathPrepends::default(), + ); + let output = Command::new(&rewritten[0]) + .args(&rewritten[1..]) + .env(CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR, "1") + .output() + .expect("run rewritten command"); + + assert!(output.status.success(), "command failed: {output:?}"); + assert_eq!(String::from_utf8_lossy(&output.stdout), "1\n"); + + let rewritten = maybe_wrap_shell_lc_with_snapshot( + &command, + &session_shell, + Some(&shell_snapshot), + &HashMap::new(), + &HashMap::new(), + &RuntimePathPrepends::default(), + ); + let output = Command::new(&rewritten[0]) + .args(&rewritten[1..]) + .env_remove(CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR) + .output() + .expect("run rewritten command"); + + assert_eq!(output.status.code(), Some(1)); + assert_eq!(output.stdout, b""); +} + #[test] fn maybe_wrap_shell_lc_with_snapshot_restores_proxy_env_from_process_env() { let dir = tempdir().expect("create temp dir");