Phillnet-2 / AgenticScaffold /diff_utils.py
ayjays132's picture
Upload 478 files
101858b verified
import re
def validate_diff(diff_text: str) -> bool:
"""
Surgical Patch Validator.
Checks for the presence of standard unified diff headers and basic hunk integrity.
"""
if not diff_text or not diff_text.strip():
return False
# Look for the hunk header: @@ -L,C +L,C @@
hunk_header_regex = r"^@@ -\d+,\d+ \+\d+,\d+ @@"
lines = diff_text.splitlines()
has_header = False
for line in lines:
if re.match(hunk_header_regex, line):
has_header = True
break
# Top-tier AGI validation: Also ensure it doesn't just contain deletions if we expect edits
# But for a general validator, just checking the header is the primary structural requirement.
return has_header
def parse_surgical_diff(diff_text: str):
"""
Parses a surgical patch into manageable hunks.
"""
hunks = []
current_hunk = None
hunk_header_regex = r"^@@ -(\d+),(\d+) \+(\d+),(\d+) @@"
for line in diff_text.splitlines():
match = re.match(hunk_header_regex, line)
if match:
if current_hunk:
hunks.append(current_hunk)
current_hunk = {
"old_start": int(match.group(1)),
"old_count": int(match.group(2)),
"new_start": int(match.group(3)),
"new_count": int(match.group(4)),
"lines": []
}
elif current_hunk:
current_hunk["lines"].append(line)
if current_hunk:
hunks.append(current_hunk)
return hunks