Skip to content

The agent that grows with you

fix(patch): anchor V4A Begin/End Patch markers to full lines

spfcraze8c172726c80c
diff --git a/tests/tools/test_patch_parser.py b/tests/tools/test_patch_parser.py
index 8b60ec4f5e4..8046fc6d86d 100644
--- a/tests/tools/test_patch_parser.py
+++ b/tests/tools/test_patch_parser.py
@@ -106,6 +106,61 @@ class TestParseMoveFile:
assert ops[0].new_path == "new/path.py"
+class TestBoundaryMarkersInContent:
+ """Patch boundary markers inside content lines must not be treated as
+ real boundaries (docs about the patch format, nested patch text, etc.)."""
+
+ def test_end_patch_marker_in_add_content_does_not_truncate(self):
+ patch = """\
+*** Begin Patch
+*** Add File: notes.md
++doc line one
++*** End Patch
++content after the marker mention
+*** End Patch"""
+ ops, err = parse_v4a_patch(patch)
+ assert err is None
+ assert len(ops) == 1
+ contents = [l.content for l in ops[0].hunks[0].lines if l.prefix == "+"]
+ assert contents == [
+ "doc line one",
+ "*** End Patch",
+ "content after the marker mention",
+ ]
+
+ def test_begin_patch_marker_in_content_does_not_discard_operations(self):
+ patch = """\
+*** Begin Patch
+*** Add File: first.md
++first file content
+*** Add File: second.md
++*** Begin Patch
++second file content
+*** End Patch"""
+ ops, err = parse_v4a_patch(patch)
+ assert err is None
+ assert [(op.operation, op.file_path) for op in ops] == [
+ (OperationType.ADD, "first.md"),
+ (OperationType.ADD, "second.md"),
+ ]
+
+ def test_context_line_end_patch_marker_does_not_truncate(self):
+ patch = """\
+*** Begin Patch
+*** Update File: guide.md
+@@ section @@
+ old line
+ *** End Patch
++new line
+*** End Patch"""
+ ops, err = parse_v4a_patch(patch)
+ assert err is None
+ assert len(ops) == 1
+ hunk_lines = [(l.prefix, l.content) for l in ops[0].hunks[0].lines]
+ assert (" ", "*** End Patch") in hunk_lines
+ assert ("+", "new line") in hunk_lines
+
+
class TestParseInvalidPatch:
def test_empty_patch_returns_empty_ops(self):
ops, err = parse_v4a_patch("")
diff --git a/tools/patch_parser.py b/tools/patch_parser.py
index c02037f77ca..5a9cd534e05 100644
--- a/tools/patch_parser.py
+++ b/tools/patch_parser.py
@@ -80,15 +80,19 @@ def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[
"""
lines = patch_content.split('\n')
operations: List[PatchOperation] = []
-
- # Find patch boundaries
+
+ # Find patch boundaries. Markers must occupy the whole line at column 0:
+ # content lines like "+*** End Patch" or " *** End Patch" (e.g. docs
+ # about the patch format) must not truncate the patch or reset the
+ # start boundary.
start_idx = None
end_idx = None
-
+ begin_marker = re.compile(r'^\*\*\*\s*Begin\s+Patch\s*$')
+ end_marker = re.compile(r'^\*\*\*\s*End\s+Patch\s*$')
for i, line in enumerate(lines):
- if '*** Begin Patch' in line or '***Begin Patch' in line:
+ if begin_marker.match(line):
start_idx = i
- elif '*** End Patch' in line or '***End Patch' in line:
+ elif end_marker.match(line):
end_idx = i
break