From a95b425f46bd9b9a164873bffd4f6b008762caa3 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 7 Jul 2026 10:24:09 -0400 Subject: [PATCH] Gate EOF matching on update mode --- codex-rs/apply-patch/src/lib.rs | 11 +++++-- codex-rs/apply-patch/src/seek_sequence.rs | 40 ++++++++++++++++++++--- codex-rs/apply-patch/tests/suite/tool.rs | 20 ++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/codex-rs/apply-patch/src/lib.rs b/codex-rs/apply-patch/src/lib.rs index 0271938c54..b1bf5a205a 100644 --- a/codex-rs/apply-patch/src/lib.rs +++ b/codex-rs/apply-patch/src/lib.rs @@ -838,6 +838,7 @@ fn compute_replacements( std::slice::from_ref(ctx_line), line_index, /*eof*/ false, + update_file_mode, ) { line_index = idx + 1; } else { @@ -871,8 +872,13 @@ fn compute_replacements( // touching the end‑of‑file can be located reliably. let mut pattern: &[String] = &chunk.old_lines; - let mut found = - seek_sequence::seek_sequence(original_lines, pattern, line_index, chunk.is_end_of_file); + let mut found = seek_sequence::seek_sequence( + original_lines, + pattern, + line_index, + chunk.is_end_of_file, + update_file_mode, + ); let mut new_slice: &[String] = &chunk.new_lines; @@ -889,6 +895,7 @@ fn compute_replacements( pattern, line_index, chunk.is_end_of_file, + update_file_mode, ); } diff --git a/codex-rs/apply-patch/src/seek_sequence.rs b/codex-rs/apply-patch/src/seek_sequence.rs index 5e3ab000e3..9934fa55e7 100644 --- a/codex-rs/apply-patch/src/seek_sequence.rs +++ b/codex-rs/apply-patch/src/seek_sequence.rs @@ -14,6 +14,7 @@ pub(crate) fn seek_sequence( pattern: &[String], start: usize, eof: bool, + update_file_mode: crate::ApplyPatchFileUpdateMode, ) -> Option { if pattern.is_empty() { return Some(start); @@ -27,7 +28,11 @@ pub(crate) fn seek_sequence( return None; } let search_start = if eof && lines.len() >= pattern.len() { - (lines.len() - pattern.len()).max(start) + let eof_start = lines.len() - pattern.len(); + match update_file_mode { + crate::ApplyPatchFileUpdateMode::NormalizeToLf => eof_start, + crate::ApplyPatchFileUpdateMode::PreserveLineEndings => eof_start.max(start), + } } else { start }; @@ -112,6 +117,7 @@ pub(crate) fn seek_sequence( #[cfg(test)] mod tests { use super::seek_sequence; + use crate::ApplyPatchFileUpdateMode; use std::string::ToString; fn to_vec(strings: &[&str]) -> Vec { @@ -123,7 +129,13 @@ mod tests { let lines = to_vec(&["foo", "bar", "baz"]); let pattern = to_vec(&["bar", "baz"]); assert_eq!( - seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false), + seek_sequence( + &lines, + &pattern, + /*start*/ 0, + /*eof*/ false, + ApplyPatchFileUpdateMode::NormalizeToLf, + ), Some(1) ); } @@ -134,7 +146,13 @@ mod tests { // Pattern omits trailing whitespace. let pattern = to_vec(&["foo", "bar"]); assert_eq!( - seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false), + seek_sequence( + &lines, + &pattern, + /*start*/ 0, + /*eof*/ false, + ApplyPatchFileUpdateMode::NormalizeToLf, + ), Some(0) ); } @@ -145,7 +163,13 @@ mod tests { // Pattern omits any additional whitespace. let pattern = to_vec(&["foo", "bar"]); assert_eq!( - seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false), + seek_sequence( + &lines, + &pattern, + /*start*/ 0, + /*eof*/ false, + ApplyPatchFileUpdateMode::NormalizeToLf, + ), Some(0) ); } @@ -156,7 +180,13 @@ mod tests { let pattern = to_vec(&["too", "many", "lines"]); // Should not panic – must return None when pattern cannot possibly fit. assert_eq!( - seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false), + seek_sequence( + &lines, + &pattern, + /*start*/ 0, + /*eof*/ false, + ApplyPatchFileUpdateMode::NormalizeToLf, + ), None ); } diff --git a/codex-rs/apply-patch/tests/suite/tool.rs b/codex-rs/apply-patch/tests/suite/tool.rs index b285098102..192384285f 100644 --- a/codex-rs/apply-patch/tests/suite/tool.rs +++ b/codex-rs/apply-patch/tests/suite/tool.rs @@ -107,6 +107,26 @@ fn test_apply_patch_cli_rejects_overlapping_end_of_file_chunks() -> anyhow::Resu Ok(()) } +#[test] +fn test_apply_patch_cli_allows_overlapping_eof_chunks_in_legacy_mode() -> anyhow::Result<()> { + let tmp = tempdir()?; + let target_path = tmp.path().join("overlapping.txt"); + fs::write(&target_path, "one\n")?; + + let patch = "*** Begin Patch\n*** Update File: overlapping.txt\n@@\n-one\n+first\n@@\n-one\n+second\n*** End of File\n*** End Patch"; + + Command::new(codex_utils_cargo_bin::cargo_bin("apply_patch")?) + .env_remove(CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR) + .arg(patch) + .current_dir(tmp.path()) + .assert() + .success() + .stdout("Success. Updated the following files:\nM overlapping.txt\n"); + + assert_eq!(fs::read_to_string(target_path)?, "first\n"); + Ok(()) +} + #[test] fn test_apply_patch_cli_preserves_crlf_from_target_file() -> anyhow::Result<()> { let patch = "*** Begin Patch\n*** Update File: crlf.txt\n@@\n-one\n+uno\n@@\n two\n+\n+between\n three\n*** End Patch";