Skip to content

fix(apply-diff): reject partially matched stacked anchors - #4431

Merged
seratch merged 2 commits into
openai:mainfrom
Coiggahou2002:fix/reject-partial-stacked-anchors
Aug 15, 2026
Merged

seratch merged 2 commits into
openai:mainfrom
Coiggahou2002:fix/reject-partial-stacked-anchors

Conversation

@Coiggahou2002

Copy link
Copy Markdown
Contributor

Summary

This pull request fixes stacked @@ anchors so every non-empty anchor in a stack must match in sequence. Previously, if one anchor matched and a later anchor did not, apply_diff continued from the partial match and could apply the hunk body to unrelated nearby content.

Single unmatched anchors keep their existing context fallback. Bare anchors, whitespace-fuzzy matches, and fully matched stacks also keep their existing behavior. A partially matched stack now raises ValueError before edited content is returned; the sandbox boundary converts that error to ApplyPatchDiffError before writing the file.

This brings the Python parser in line with the same safety rule added in openai/openai-agents-js#1655.

Test plan

  • Added direct parser coverage for partial stacks, missing first anchors, missing anchors followed by bare markers, and single-anchor compatibility.
  • Added sandbox coverage proving a rejected partial stack leaves the original file unchanged.
  • Ran .agents/skills/code-change-verification/scripts/run.sh with format, lint, typecheck, and the complete test suite passing.

Issue number

N/A. Found while comparing stacked-anchor behavior across the Python and JavaScript SDKs.

Checks

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh
  • I've confirmed all verification steps pass
  • If using Codex, I've run /review before submitting this PR

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merge, please add the released-compatibility regression covered in openai/openai-agents-js#1655: two stacked hunks in the same parent block must be able to reuse a parent anchor that is already before the current cursor, while each child anchor still matches forward. The implementation already appears to preserve this behavior; the missing test is needed because this PR rewrites that exact match-accounting branch.

@Coiggahou2002

Copy link
Copy Markdown
Contributor Author

Added in bec42e8. The new public apply_diff regression mirrors openai/openai-agents-js#1655: the first hunk advances past the parent, then the second stacked hunk reuses that prior parent anchor while its child anchor still matches forward. No implementation change was needed. Focused tests and the repository-wide format/lint/typecheck/test stack all pass.

@seratch seratch added this to the 0.21.x milestone Aug 15, 2026
@seratch
seratch enabled auto-merge (squash) August 15, 2026 07:09
@seratch
seratch merged commit 6b62225 into openai:main Aug 15, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants