File size: 1,625 Bytes
101858b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

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