fix(ext): reject stale hunks in TextCanvas.apply_patch with context validation - #8195
Open
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
Open
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
Conversation
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.
Fixes #8193
Summary
TextCanvas.apply_patch()documented context-line validation, but the hunk loop performed a blindworking_lines[start:end] = replacementslice assignment — so a unified diff prepared against an older revision was silently applied to the latest revision, returningPATCH APPLIEDand making stale content the newest revision (reproduced exactly as in the issue: stale edit landed,region=Tokyo).Changes
ValueError, matching the class's existing error convention and propagating throughApplyPatchToollike its other validation errors. The check runs beforeadd_or_update_file(), so rejected patches leave the canvas untouched.source_start(notsource_start - 1), which fixes silent one-line misplacement ofn=0difflib patches while keeping file-creation patches working.Tests
python/packages/autogen-ext/tests/memory/test_text_canvas_memory.py(+100):test_apply_patch_rejects_stale_hunks— the issue's repro now raisesValueError, revision 2 stays latest, and a control patch built from the real revision-2 content then applies cleanly (revision 3)test_apply_patch_rejects_near_miss_context— one-char context drift is rejected (no fuzzy matching)test_apply_patch_increments_revisionpytest tests/memory/test_text_canvas_memory.py→ 6/6 passed;ruff check+ruff format --checkclean on both files. Multi-hunk, deletions, EOF appends, prepends, create-empty, zero-context insert, and chained applies manually verified unaffected.