Conversation
… file An insertion-only hunk records the original end of the file as its position, while later hunks can change how many lines precede it. Replacements were applied in patch order reversed rather than by position, so when such a hunk came before an edit that added or removed lines, its lines were written into the middle of the file and the tool still reported success. Sort replacements by position before applying them bottom-up, as Codex does. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the built-in
apply_patchtool receives an update whose insertion-only hunk (@@followed only by+lines) comes before a hunk that adds or removes lines, the inserted lines are written into the middle of the file instead of at its end, and the tool still reportsSuccess. For example, insertingomegaand then replacingbetawith two lines inalpha / beta / gamma / deltaproduces... gamma / omega / deltainstead of... delta / omega.An insertion-only hunk records the original end of the file as its position.
_applyHunksthen applied the collected replacements in reverse patch order, so a later hunk higher in the file was spliced first and shifted the end-of-file position the insertion had recorded. Codex sorts replacements by position before applying them bottom-up (codex-rs/apply-patch/src/file_update.rs:replacements.sort_by_key(|(index, _, _)| *index)); this does the same. Hunks that are already in file order are applied exactly as before.This is independent of #174. Both touch
apply-patch.tsin different functions, and the two branches merge cleanly.Validation
bun test packages/runtime/tests/tools/built-in/apply-patch.test.tsBefore the fix (new test added, source unchanged): 7 pass, 1 fail.
After the fix: 8 pass, 0 fail.
appends insertion-only hunks and normalizes a final newline. This shows that only the out-of-order case changes.bun test packages/runtime/tests/tools/built-in/built-in-tools-module.test.ts: 5 pass / 1 fail on bothmainand this branch. The failure (runs bash commands from the workspace root) is pre-existing locally because that test spawnsbash.--max-warnings 0) on both touched files: clean onmainand on this branch.tsc --noEmit -p packages/runtime/tsconfig.json: clean onmainand on this branch.main(16 and 6); this change adds none.🤖 Generated with Claude Code