You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reject missing anchors before applying file patches - #1785
An update_file patch with an explicit Codex-style @@ anchor can modify a different function when that anchor is absent. advanceCursorToAnchor leaves the cursor unchanged after both searches fail, so the hunk context is matched elsewhere and the tool reports success.
For example, a patch anchored to function missing() { changes return false inside function other() { to return true, even though the requested function does not exist. This also reproduces through applyPatchTool with a real temporary file.
Changes
Reject an anchor that cannot be found before applying the hunk. Keep the existing exact/trimmed matching and reuse of an anchor already passed by the cursor. Add one regression asserting an error and unchanged file contents.
Verification
New regression on unmodified main: 0 pass / 1 fail (unexpected success); after the fix: 1 pass / 0 fail.
Real-filesystem probe: original code modifies the wrong function; patched code returns an anchor error and leaves the file unchanged.
The existing suite uses POSIX-only mock paths: the native Windows run has 11 unchanged path-fixture failures. A temporary copy changing only those paths and import locations runs all 13 cases successfully, including the existing positive anchor case. This is supplemental verification, not a Linux CI result.
SDK typecheck passes with the installed native TypeScript 7.0.2. Frozen-lockfile install reports no changes. ESLint: 0 errors / 5 existing resolver/import-order warnings. Prettier still reports existing file differences; comparison against main finds no new formatting differences.
SDK build remains blocked by the existing declaration-generator ts.sys.getCurrentDirectory error. Binary build and smoke were not run because SDK build is a prerequisite.
Scope / Tradeoffs
Source-audit fix; no existing issue claimed. Two SDK files only. Numeric/bare hunk headers and successful anchor matching retain their behavior. The change intentionally refuses a missing explicit anchor rather than guessing another location. No patch-format, path-confinement or write-lifecycle redesign.
Good catch and a tight, well-targeted fix. advanceCursorToAnchor previously fell through silently when neither the exact nor trimmed anchor search matched, leaving the cursor wherever it started — so a hunk context could match unrelated code and applyPatchTool would report success while editing the wrong function. Throwing Anchor not found: ${anchor} when found is still false after both search passes is the right fix: fail loudly and leave the file untouched rather than guess.
The regression test in apply-patch.test.ts is appropriately minimal — it reproduces the exact failure mode described (anchor for function missing() silently patching function other()), asserts both the error message and that file contents are unchanged. That's the kind of test this repo would expect for a patch-matching bug.
Two things I'd want a maintainer to double check before porting, since I can't see the full file from the diff: (1) that found is genuinely still in scope and reflects the outcome of both the exact and trimmed match attempts at the point of the new check, not just the second loop, and (2) that this doesn't affect the already-passed-anchor reuse path or bare/numeric hunk headers, which the PR description claims are unaffected but isn't covered by a dedicated test. A second regression test confirming a valid explicit anchor still succeeds post-fix (beyond the existing positive case) would make this airtight.
Overall: correct diagnosis, minimal footprint, in-scope (sdk/ only), with a real repro test. Worth porting after a quick scope-of-found sanity check.
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
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.
Problem / Why
An
update_filepatch with an explicit Codex-style@@anchor can modify a different function when that anchor is absent.advanceCursorToAnchorleaves the cursor unchanged after both searches fail, so the hunk context is matched elsewhere and the tool reports success.For example, a patch anchored to
function missing() {changesreturn falseinsidefunction other() {toreturn true, even though the requested function does not exist. This also reproduces throughapplyPatchToolwith a real temporary file.Changes
Reject an anchor that cannot be found before applying the hunk. Keep the existing exact/trimmed matching and reuse of an anchor already passed by the cursor. Add one regression asserting an error and unchanged file contents.
Verification
ts.sys.getCurrentDirectoryerror. Binary build and smoke were not run because SDK build is a prerequisite.Scope / Tradeoffs
Source-audit fix; no existing issue claimed. Two SDK files only. Numeric/bare hunk headers and successful anchor matching retain their behavior. The change intentionally refuses a missing explicit anchor rather than guessing another location. No patch-format, path-confinement or write-lifecycle redesign.