File size: 11,715 Bytes
d1f939e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
 
 
 
 
b4deece
 
 
 
 
 
d1f939e
b4deece
 
 
 
 
 
 
d1f939e
b4deece
 
 
 
 
 
 
d1f939e
b4deece
 
 
 
 
d1f939e
b4deece
 
d1f939e
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
b4deece
 
 
d1f939e
b4deece
d1f939e
 
 
 
 
 
 
 
 
 
 
 
b4deece
 
d1f939e
 
 
b4deece
d1f939e
 
 
b4deece
d1f939e
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
 
 
b4deece
d1f939e
 
b4deece
d1f939e
 
b4deece
 
d1f939e
 
b4deece
d1f939e
 
b4deece
 
d1f939e
 
 
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
 
 
 
b4deece
d1f939e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
 
 
b4deece
d1f939e
 
 
 
 
b4deece
 
d1f939e
 
 
 
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
b4deece
d1f939e
 
 
b4deece
 
d1f939e
 
 
 
 
b4deece
d1f939e
b4deece
d1f939e
b4deece
d1f939e
 
b4deece
 
 
 
 
 
d1f939e
 
 
 
 
 
b4deece
 
 
d1f939e
b4deece
d1f939e
 
 
 
 
 
b4deece
d1f939e
 
b4deece
d1f939e
b4deece
 
d1f939e
 
 
 
 
b4deece
d1f939e
 
b4deece
 
d1f939e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4deece
d1f939e
b4deece
 
 
 
 
 
d1f939e
b4deece
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
grader.py β€” Reward function for the CodeBug RL loop.

Key improvements over v1:
  - Clean-code correctly approved β†’ small positive reward (+5)   ← NEW
  - Right decision but wrong category β†’ partial credit (+5)      ← NEW
  - All reward values imported from config.py
  - Type annotations on public functions
"""

from __future__ import annotations

from config import (
    PENALTY_MISS_CRITICAL,
    PENALTY_MISS_HIGH,
    PENALTY_MISS_LOW,
    PENALTY_MISS_MEDIUM,
    PENALTY_UNKNOWN_ACTION,
    PENALTY_WRONG_CATEGORY,
    REWARD_CLEAN_APPROVE,
    REWARD_COMMENT_LONG,
    REWARD_COMMENT_SHORT,
    REWARD_LINE_HINT,
    REWARD_LOGIC_CATCH,
    REWARD_OTHER_CATCH,
    REWARD_PARTIAL_CREDIT,
    REWARD_SECURITY_CATCH,
    REWARD_SEVERITY_CLOSE,
    REWARD_SEVERITY_EXACT,
)


# ---------------------------------------------------------------------------
# GradeResult
# ---------------------------------------------------------------------------


class GradeResult:
    def __init__(
        self,
        reward: int,
        breakdown: list[str],
        correct: bool,
        bug_type: str = "unknown",
        bug_severity: int = 0,
    ) -> None:
        self.reward = reward
        self.breakdown = breakdown
        self.correct = correct
        self.bug_type = bug_type
        self.bug_severity = bug_severity

    def summary(self) -> str:
        lines = [
            "GRADE SUMMARY",
            "-" * 25,
            f"Reward:    {self.reward:+d}",
            f"Correct:   {'Yes' if self.correct else 'No'}",
            f"Bug was:   {self.bug_type} (severity {self.bug_severity})",
            "",
            "Breakdown:",
        ]
        for entry in self.breakdown:
            lines.append(f"  {entry}")
        lines.append("-" * 25)
        lines.append(f"Total: {self.reward:+d}")
        return "\n".join(lines)

    def to_dict(self) -> dict:
        return {
            "reward": self.reward,
            "correct": self.correct,
            "bug_type": self.bug_type,
            "bug_severity": self.bug_severity,
            "breakdown": self.breakdown,
        }


# ---------------------------------------------------------------------------
# grade()
# ---------------------------------------------------------------------------


def grade(ai_action: dict, ground_truth: dict | None) -> GradeResult:
    """
    Grade a single AI review against the ground truth.

    ai_action keys  : type, category, line_hint, comment, severity
    ground_truth keys: bug β†’ {type, severity, description, line_hint}
    """
    if ground_truth is None or "bug" not in ground_truth:
        return GradeResult(reward=0, breakdown=["No ground truth available"], correct=False)

    bug = ground_truth["bug"]
    bug_type     = bug.get("type", "none")
    bug_severity = bug.get("severity", 0)
    is_clean     = bug_type in ("none", "ok") or bug_severity == 0

    reward    = 0
    breakdown: list[str] = []
    correct   = False

    ai_type      = ai_action.get("type",      "approve")
    ai_category  = ai_action.get("category",  "ok")
    ai_comment   = ai_action.get("comment",   "")
    ai_severity  = ai_action.get("severity",  3)
    ai_line_hint = ai_action.get("line_hint", "")

    # ------------------------------------------------------------------
    # CASE 1: AI flagged something
    # ------------------------------------------------------------------
    if ai_type == "flag":

        if ai_category == bug_type:
            # βœ… Perfect category match
            correct = True

            if bug_type == "security":
                reward += REWARD_SECURITY_CATCH
                breakdown.append(f"+{REWARD_SECURITY_CATCH}: caught a security vulnerability")
            elif bug_type == "logic":
                reward += REWARD_LOGIC_CATCH
                breakdown.append(f"+{REWARD_LOGIC_CATCH}: caught a logic bug")
            else:
                reward += REWARD_OTHER_CATCH
                breakdown.append(f"+{REWARD_OTHER_CATCH}: caught a style/other issue")

            # Severity accuracy
            sev_diff = abs(ai_severity - bug_severity)
            if sev_diff == 0:
                reward += REWARD_SEVERITY_EXACT
                breakdown.append(f"+{REWARD_SEVERITY_EXACT}: severity rating exact")
            elif sev_diff == 1:
                reward += REWARD_SEVERITY_CLOSE
                breakdown.append(f"+{REWARD_SEVERITY_CLOSE}: severity rating close")
            else:
                breakdown.append("+0: severity rating off")

            # Comment quality
            if len(ai_comment) > 40:
                reward += REWARD_COMMENT_LONG
                breakdown.append(f"+{REWARD_COMMENT_LONG}: detailed comment")
            elif len(ai_comment) > 15:
                reward += REWARD_COMMENT_SHORT
                breakdown.append(f"+{REWARD_COMMENT_SHORT}: brief comment")
            else:
                breakdown.append("+0: comment too short or missing")

            # Line hint
            if len(ai_line_hint) > 5:
                reward += REWARD_LINE_HINT
                breakdown.append(f"+{REWARD_LINE_HINT}: line location specified")
            else:
                breakdown.append("+0: no line location given")

        elif not is_clean:
            # ⚠️  Right decision (it IS a bug), wrong category β†’ partial credit
            reward += REWARD_PARTIAL_CREDIT
            breakdown.append(
                f"+{REWARD_PARTIAL_CREDIT}: partial credit β€” flagged correctly but "
                f"wrong category ('{ai_category}' vs '{bug_type}')"
            )

        else:
            # ❌ False positive β€” code was clean
            reward += PENALTY_WRONG_CATEGORY
            breakdown.append(
                f"{PENALTY_WRONG_CATEGORY}: false positive β€” code was clean, "
                f"flagged as '{ai_category}'"
            )

    # ------------------------------------------------------------------
    # CASE 2: AI approved
    # ------------------------------------------------------------------
    elif ai_type == "approve":

        if is_clean:
            # βœ… Correctly approved clean code β€” small positive reward
            correct = True
            reward += REWARD_CLEAN_APPROVE
            breakdown.append(f"+{REWARD_CLEAN_APPROVE}: correctly approved clean code")

        else:
            # ❌ Missed a real bug
            if bug_severity >= 5:
                reward += PENALTY_MISS_CRITICAL
                breakdown.append(f"{PENALTY_MISS_CRITICAL}: missed critical severity-5 bug ({bug_type})")
            elif bug_severity >= 4:
                reward += PENALTY_MISS_HIGH
                breakdown.append(f"{PENALTY_MISS_HIGH}: missed high severity-4 bug ({bug_type})")
            elif bug_severity >= 3:
                reward += PENALTY_MISS_MEDIUM
                breakdown.append(f"{PENALTY_MISS_MEDIUM}: missed medium severity-3 bug ({bug_type})")
            else:
                reward += PENALTY_MISS_LOW
                breakdown.append(f"{PENALTY_MISS_LOW}: missed low severity bug ({bug_type})")

    # ------------------------------------------------------------------
    # CASE 3: Unknown action
    # ------------------------------------------------------------------
    else:
        reward += PENALTY_UNKNOWN_ACTION
        breakdown.append(f"{PENALTY_UNKNOWN_ACTION}: unknown action type '{ai_type}'")

    return GradeResult(
        reward=reward,
        breakdown=breakdown,
        correct=correct,
        bug_type=bug_type,
        bug_severity=bug_severity,
    )


# ---------------------------------------------------------------------------
# batch_grade()
# ---------------------------------------------------------------------------


def batch_grade(episodes: list[dict]) -> dict:
    """Summarise grading across multiple episodes."""
    results_list: list[dict] = []
    total_reward    = 0
    correct_count   = 0
    best_score      = -float("inf")
    worst_score     = float("inf")
    caught_security = 0
    caught_logic    = 0
    false_positives = 0
    missed_bugs     = 0
    clean_approved  = 0

    for ep in episodes:
        res = grade(ep["ai_action"], ep["ground_truth"])
        d   = res.to_dict()
        results_list.append(d)

        total_reward += d["reward"]
        if d["correct"]:
            correct_count += 1
            if d["bug_type"] == "security":
                caught_security += 1
            elif d["bug_type"] == "logic":
                caught_logic += 1
            elif d["bug_type"] in ("none", "ok"):
                clean_approved += 1
        else:
            ai_type = ep["ai_action"].get("type")
            if ai_type == "flag":
                false_positives += 1
            elif ai_type == "approve":
                missed_bugs += 1

        if d["reward"] > best_score:
            best_score = d["reward"]
        if d["reward"] < worst_score:
            worst_score = d["reward"]

    count = len(episodes)
    if count == 0:
        return {"error": "no episodes provided"}

    return {
        "results":         results_list,
        "total_reward":    total_reward,
        "average_reward":  total_reward / count,
        "accuracy":        correct_count / count,
        "best_score":      best_score,
        "worst_score":     worst_score,
        "caught_security": caught_security,
        "caught_logic":    caught_logic,
        "clean_approved":  clean_approved,
        "false_positives": false_positives,
        "missed_bugs":     missed_bugs,
    }


# ---------------------------------------------------------------------------
# Test block
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    import pprint

    test_cases = [
        # 1. Perfect security catch
        {
            "ai_action":   {"type": "flag", "category": "security",
                            "line_hint": "line 3 query string",
                            "comment": "SQL injection β€” user input directly in query", "severity": 5},
            "ground_truth": {"bug": {"type": "security", "severity": 5,
                                     "description": "SQL injection", "line_hint": "query line"}},
        },
        # 2. Partial credit (right decision, wrong category)
        {
            "ai_action":   {"type": "flag", "category": "logic",
                            "line_hint": "line 5", "comment": "this looks wrong", "severity": 2},
            "ground_truth": {"bug": {"type": "security", "severity": 5,
                                     "description": "plain text password", "line_hint": "db.insert"}},
        },
        # 3. Missed critical bug
        {
            "ai_action":   {"type": "approve", "category": "ok",
                            "line_hint": "", "comment": "", "severity": 0},
            "ground_truth": {"bug": {"type": "security", "severity": 5,
                                     "description": "SQL injection", "line_hint": "query line"}},
        },
        # 4. Correctly approved clean code  ← NEW
        {
            "ai_action":   {"type": "approve", "category": "ok",
                            "line_hint": "", "comment": "", "severity": 0},
            "ground_truth": {"bug": {"type": "none", "severity": 0,
                                     "description": "clean code", "line_hint": ""}},
        },
    ]

    print("RUNNING GRADER TESTS...\n")
    for i, case in enumerate(test_cases, 1):
        print(f"CASE {i}:")
        result = grade(case["ai_action"], case["ground_truth"])
        print(result.summary())
        print()

    print("BATCH RESULTS:")
    pprint.pprint(batch_grade(test_cases))