File size: 1,905 Bytes
42d30a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re


class CodeReviewer:
    """
    Simulated actor bot that reviews the agent's code edits 
    for Stage 2 and Stage 3 to detect data leakage and missing scaler.
    """

    def review(self, script: str, stage: int) -> str:
        script = script or ""
        
        has_scaler = "StandardScaler" in script
        # Check if X_scaled = fit_transform(X) happens before train_test_split
        # or if scaler is applied to the raw dataframe before splitting
        leakage_detected = False
        
        if has_scaler:
            if re.search(r"scaler\.fit_transform\(\s*X\s*\)", script):
                leakage_detected = True
            elif re.search(r"scaler\.fit\(\s*X\s*\)", script):
                leakage_detected = True

        if stage == 2:
            if not has_scaler:
                return (
                    "REJECT: Feature scaling is missing. MLPClassifier is "
                    "highly sensitive to unscaled features. Add StandardScaler "
                    "before fitting."
                )
            if leakage_detected:
                return (
                    "REJECT: You're fitting the scaler on the full dataset. "
                    "This causes data leakage. Fit only on X_train."
                )
            return (
                "LGTM: Scaler is correctly fitted on training data only. "
                "This looks good to merge."
            )

        if stage == 3:
            if leakage_detected or not has_scaler:
                return (
                    "REJECT: Critical data leakage detected. Scaler must be "
                    "fit on training data only. This invalidates your evaluation."
                )
            return (
                "LGTM: No data leakage detected. Evaluation pipeline "
                "looks correct."
            )
            
        return "I have no feedback for this stage."