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

@@ -37,6 +37,7 @@ use crate::tools::runtimes::apply_patch::ApplyPatchRuntime;
use crate::tools::sandboxing::ToolCtx;
use codex_apply_patch::ApplyPatchAction;
use codex_apply_patch::ApplyPatchFileChange;
use codex_apply_patch::ApplyPatchFileUpdateMode;
use codex_apply_patch::Hunk;
use codex_apply_patch::StreamingPatchParser;
use codex_exec_server::ExecutorFileSystem;
@@ -55,6 +56,19 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
const APPLY_PATCH_ARGUMENT_DIFF_BUFFER_INTERVAL: Duration = Duration::from_millis(500);
fn apply_patch_file_update_mode(turn: &TurnContext) -> ApplyPatchFileUpdateMode {
if turn
.config
.features
.enabled(Feature::ApplyPatchPreserveLineEndings)
{
ApplyPatchFileUpdateMode::PreserveLineEndings
} else {
ApplyPatchFileUpdateMode::NormalizeToLf
}
}
/// Handles freeform `apply_patch` requests and routes verified patches to the
/// selected environment filesystem.
#[derive(Default)]
@@ -387,9 +401,10 @@ impl ApplyPatchHandler {
let fs = turn_environment.environment.get_filesystem();
let sandbox = turn
.file_system_sandbox_context(/*additional_permissions*/ None, turn_environment);
match codex_apply_patch::verify_apply_patch_args(
match codex_apply_patch::verify_apply_patch_args_with_mode(
args,
turn_environment.cwd(),
apply_patch_file_update_mode(&turn),
fs.as_ref(),
Some(&sandbox),
)
@@ -496,8 +511,14 @@ pub(crate) async fn intercept_apply_patch(
let turn = &step_context.turn;
let sandbox =
turn.file_system_sandbox_context(/*additional_permissions*/ None, &turn_environment);
match codex_apply_patch::maybe_parse_apply_patch_verified(command, cwd, fs, Some(&sandbox))
.await
match codex_apply_patch::maybe_parse_apply_patch_verified_with_mode(
command,
cwd,
apply_patch_file_update_mode(turn),
fs,
Some(&sandbox),
)
.await
{
codex_apply_patch::MaybeApplyPatchVerified::Body(changes) => {
let tool_ctx = ToolCtx {
@@ -44,6 +44,24 @@ async fn invocation_for_payload(payload: ToolPayload) -> ToolInvocation {
}
}
#[tokio::test]
async fn file_update_mode_follows_preserve_line_endings_feature() {
let (_, mut turn) = make_session_and_context().await;
assert_eq!(
apply_patch_file_update_mode(&turn),
codex_apply_patch::ApplyPatchFileUpdateMode::NormalizeToLf
);
Arc::make_mut(&mut turn.config)
.features
.enable(codex_features::Feature::ApplyPatchPreserveLineEndings)
.expect("feature should be enabled");
assert_eq!(
apply_patch_file_update_mode(&turn),
codex_apply_patch::ApplyPatchFileUpdateMode::PreserveLineEndings
);
}
#[tokio::test]
async fn pre_tool_use_payload_uses_freeform_patch_input() {
let patch = sample_patch();
@@ -6,6 +6,7 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use crate::exec::ExecCapturePolicy;
use crate::exec::ExecParams;
use crate::exec_env::create_env;
use crate::exec_env::inject_apply_patch_env;
use crate::exec_env::inject_permission_profile_env;
use crate::function_tool::FunctionCallError;
use crate::maybe_emit_implicit_skill_invocation;
@@ -105,6 +106,7 @@ impl ShellCommandHandler {
&turn_context.config.permissions.shell_environment_policy,
Some(session.thread_id),
);
inject_apply_patch_env(&mut env, &turn_context.config.features);
let active_permission_profile = turn_environment.active_permission_profile();
inject_permission_profile_env(&mut env, active_permission_profile.as_ref());
let sandbox_permissions = resolve_sandbox_permissions(