Add a feature flag to preserve apply_patch line endings (#37758)

## What changed

- Add the `apply_patch_preserve_line_endings` feature, disabled by default, to
  preserve CRLF, CR, and mixed line endings when `apply_patch` updates files.
- Apply the feature consistently to built-in patch handling and patches invoked
  through shell, user-shell, unified-exec, and app-server command execution.
- Keep the active feature configuration authoritative over inherited, shell
  snapshot, and client-provided environment values.

## Testing

- Cover line-ending behavior with the feature enabled and disabled for custom
  tool calls, shell heredocs, command execution, and the `apply_patch` CLI.

GitOrigin-RevId: 531a7c66761959c650270559f57941929f03e6c4
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-08-10 01:44:30 +00:00
1 parent 21aa552e87
commit c9c6c0daa9
18 files changed
+462 -99

No files matched your search

@@ -16,6 +16,7 @@ use crate::codex_thread::BackgroundTerminalInfo;
use crate::exec_env::CODEX_PERMISSION_PROFILE_ENV_VAR;
use crate::exec_env::CODEX_THREAD_ID_ENV_VAR;
use crate::exec_env::create_env;
use crate::exec_env::inject_apply_patch_env;
use crate::exec_env::inject_permission_profile_env;
use crate::exec_policy::ExecApprovalRequest;
use crate::sandboxing::ExecOptions;
@@ -122,10 +123,18 @@ fn exec_env_policy_from_shell_policy(
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
exclude.push(CODEX_PERMISSION_PROFILE_ENV_VAR.to_string());
exclude.extend([
CODEX_PERMISSION_PROFILE_ENV_VAR.to_string(),
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(),
]);
let mut r#set = policy.r#set.clone();
r#set.retain(|key, _| {
!key.eq_ignore_ascii_case(CODEX_PERMISSION_PROFILE_ENV_VAR)
![
CODEX_PERMISSION_PROFILE_ENV_VAR,
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR,
]
.iter()
.any(|runtime_key| key.eq_ignore_ascii_case(runtime_key))
&& !is_non_inheritable_env_var(key)
});
codex_exec_server::ExecEnvPolicy {
@@ -149,8 +158,11 @@ fn env_overlay_for_exec_server(
.iter()
.filter(|(key, value)| {
!is_non_inheritable_env_var(key)
&& (key.as_str() == CODEX_PERMISSION_PROFILE_ENV_VAR
|| local_policy_env.get(*key) != Some(*value))
&& (matches!(
key.as_str(),
CODEX_PERMISSION_PROFILE_ENV_VAR
| codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR
) || local_policy_env.get(*key) != Some(*value))
})
.map(|(key, value)| (key.clone(), value.clone()))
.collect()
@@ -1148,6 +1160,7 @@ impl UnifiedExecProcessManager {
CODEX_THREAD_ID_ENV_VAR.to_string(),
context.session.thread_id.to_string(),
);
inject_apply_patch_env(&mut env, &turn.config.features);
let active_permission_profile = request.turn_environment.active_permission_profile();
inject_permission_profile_env(&mut env, active_permission_profile.as_ref());
let env = apply_unified_exec_env(env);
@@ -47,6 +47,10 @@ fn env_overlay_for_exec_server_keeps_runtime_changes_only() {
CODEX_PERMISSION_PROFILE_ENV_VAR.to_string(),
"current-profile".to_string(),
),
(
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(),
"1".to_string(),
),
]);
let request_env = HashMap::from([
("HOME".to_string(), "/client-home".to_string()),
@@ -58,6 +62,10 @@ fn env_overlay_for_exec_server_keeps_runtime_changes_only() {
CODEX_PERMISSION_PROFILE_ENV_VAR.to_string(),
"current-profile".to_string(),
),
(
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(),
"1".to_string(),
),
(
"CODEX_SANDBOX_NETWORK_DISABLED".to_string(),
"1".to_string(),
@@ -73,6 +81,10 @@ fn env_overlay_for_exec_server_keeps_runtime_changes_only() {
CODEX_PERMISSION_PROFILE_ENV_VAR.to_string(),
"current-profile".to_string(),
),
(
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(),
"1".to_string(),
),
(
"CODEX_SANDBOX_NETWORK_DISABLED".to_string(),
"1".to_string()
@@ -93,6 +105,10 @@ fn exec_env_policy_excludes_non_inheritable_and_runtime_variables() {
"openai_identity_token_file".to_string(),
"/run/identity-token".to_string(),
),
(
"codex_apply_patch_preserve_line_endings".to_string(),
"1".to_string(),
),
("KEEP".to_string(), "value".to_string()),
]),
..Default::default()
@@ -103,7 +119,10 @@ fn exec_env_policy_excludes_non_inheritable_and_runtime_variables() {
codex_exec_server::ExecEnvPolicy {
inherit: policy.inherit,
ignore_default_excludes: policy.ignore_default_excludes,
exclude: vec![CODEX_PERMISSION_PROFILE_ENV_VAR.to_string()],
exclude: vec![
CODEX_PERMISSION_PROFILE_ENV_VAR.to_string(),
codex_apply_patch::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR.to_string(),
],
r#set: HashMap::from([("KEEP".to_string(), "value".to_string())]),
include_only: Vec::new(),
}