File size: 12,269 Bytes
29c6586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# environment.py – Final integrated environment (multi-turn, gated, continuous scoring)

import sys
import subprocess
import tempfile
import os
import re
from dataclasses import dataclass, field
from typing import Tuple, Dict, Any, Optional

# Uncomment these imports – ensure the files are in the same directory
from models import (
    AnyAction, WriteComment, ProposeFix, Execute, Inspect,
    RunLinter, RunTests, QueryDocs, Skip, Done, AskQuestion,
    Observation, Reward, State
)
from grader import RigorousGrader
from redteam import RedTeam
from test_runner import TestRunner
from author import PersonaAuthor
from rltool import ToolBox

# ----------------------------------------------------------------------
# Helper: execute arbitrary Python code
# ----------------------------------------------------------------------
def execute_code(code: str, timeout_sec: int = 5) -> Tuple[bool, str, str]:
    if not code.strip():
        return False, "", "Error: Empty code"

    with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as f:
        f.write(code)
        tmp_path = f.name

    try:
        result = subprocess.run(
            [sys.executable, tmp_path],
            capture_output=True,
            text=True,
            timeout=timeout_sec
        )
        success = (result.returncode == 0)
        return success, result.stdout, result.stderr

    except subprocess.TimeoutExpired:
        return False, "", f"Timeout after {timeout_sec}s"

    except Exception as e:
        return False, "", f"Execution error: {str(e)}"

    finally:
        try:
            os.unlink(tmp_path)
        except:
            pass


# ----------------------------------------------------------------------
# Main Environment
# ----------------------------------------------------------------------
@dataclass
class CodeReviewEnv:
    task: str = "easy"
    max_steps: int = 10
    step_penalty: float = 0.02

    _red_team: Optional[RedTeam] = field(init=False, default=None)
    _author: Optional[PersonaAuthor] = field(init=False, default=None)

    _current_code: str = field(init=False, default="")
    _current_bug_id: str = field(init=False, default="")
    _bug_description: str = field(init=False, default="")
    _oracle_fix: str = field(init=False, default="")

    _comments: list = field(init=False, default_factory=list)
    _test_results: Optional[str] = field(init=False, default=None)
    _lint_results: Optional[str] = field(init=False, default=None)
    _doc_results: Optional[str] = field(init=False, default=None)

    _step_count: int = field(init=False, default=0)
    _done: bool = field(init=False, default=False)

    # ------------------------------------------------------------------
    def __post_init__(self):
        self.set_task(self.task)

    # ------------------------------------------------------------------
    def set_task(self, task: str):
        if task not in ["easy", "medium", "hard", "harder", "hardest"]:
            raise ValueError(f"Unknown task: {task}")

        self.task = task
        self._red_team = RedTeam(task)
        self._author = PersonaAuthor()  # uses default personality "defensive"

        self._reset_internal()

    # ------------------------------------------------------------------
    def _reset_internal(self):
        self._step_count = 0
        self._comments = []
        self._test_results = None
        self._lint_results = None
        self._doc_results = None
        self._done = False

        self._author.reset()

        # --- Base tasks ---
        if self.task == "easy":
            original = "def get_user(id):\n    if id in users:\n        return users[id]"

        elif self.task == "medium":
            original = "def process_items(items):\n    for item in items:\n        print(item)"

        elif self.task == "hard":
            original = "def average(data):\n    if not data:\n        return 0\n    return sum(data) / len(data)"

        elif self.task == "harder":
            original = "counter = 0\ndef increment():\n    global counter\n    with lock:\n        counter += 1"

        else:
            original = "def safe_work():\n    with lock1:\n        with lock2:\n            do_work()"

        # --- Inject bug ---
        buggy_code, bug_id, desc, oracle = self._red_team.inject_bug(original)

        self._current_code = buggy_code
        self._current_bug_id = bug_id
        self._bug_description = desc
        self._oracle_fix = oracle

        self._comments.append(f"[RedTeam] {desc}")

    # ------------------------------------------------------------------
    def reset(self) -> Observation:
        self._reset_internal()
        return self._get_observation()

    # ------------------------------------------------------------------
    def _get_observation(self) -> Observation:
        # Observation as defined in models.py (no conversation_history)
        return Observation(
            code_snippet=self._current_code,
            last_tool_output=self._test_results or "",
            step=self._step_count,
            done=self._done
        )

    # ------------------------------------------------------------------
    def step(self, action: AnyAction) -> Tuple[Observation, Reward, bool, Dict[str, Any]]:

        if self._done:
            raise RuntimeError("Episode already finished")

        reward_val = 0.0
        info = {}

        # ================================================================
        # TOOL ACTIONS
        # ================================================================
        if isinstance(action, Execute):
            success, stdout, stderr = execute_code(self._current_code)
            self._test_results = (stdout + stderr).strip() or "No output"
            reward_val = -self.step_penalty

        elif isinstance(action, Inspect):
            self._test_results = self._current_code
            reward_val = -self.step_penalty

        elif isinstance(action, RunLinter):
            lint_output = ToolBox.run_linter(self._current_code)
            self._lint_results = lint_output[:500]
            self._test_results = self._lint_results
            reward_val = -self.step_penalty

        elif isinstance(action, RunTests):
            runner = TestRunner(self._current_bug_id)
            score, output = runner.run_tests(self._current_code)

            self._test_results = f"Test score: {score:.2f}\n{output[:500]}"
            reward_val = -self.step_penalty

        elif isinstance(action, QueryDocs):
            doc = ToolBox.query_docs(action.query_topic)
            self._doc_results = doc
            self._test_results = doc
            reward_val = -self.step_penalty

        # ================================================================
        # COMMUNICATION (MULTI-TURN)
        # ================================================================
        elif isinstance(action, WriteComment):
            self._comments.append(f"Agent: {action.comment_text}")

            response = self._author.respond(
                agent_comment=action.comment_text,
                test_results=self._test_results,
                lint_results=self._lint_results,
                doc_results=self._doc_results,
                proposed_fix=None,
                original_code=self._current_code
            )

            self._comments.append(f"Author: {response}")
            reward_val = -self.step_penalty

        elif isinstance(action, AskQuestion):
            self._comments.append(f"Agent: {action.question}")

            response = self._author.respond(
                agent_question=action.question,
                test_results=self._test_results,
                lint_results=self._lint_results,
                doc_results=self._doc_results,
                proposed_fix=None,
                original_code=self._current_code
            )

            self._comments.append(f"Author: {response}")
            reward_val = -self.step_penalty

        # ================================================================
        # FINAL FIX
        # ================================================================
        elif isinstance(action, ProposeFix):

            if not action.fix_code:
                reward_val = -0.5
                self._done = True

            else:
                self._current_code = action.fix_code

                runner = TestRunner(self._current_bug_id)
                test_score, test_output = runner.run_tests(self._current_code)

                lint_score = self._run_linter_score(self._current_code)
                negotiation_score = self._author.get_negotiation_score()

                step_cost = self.step_penalty * self._step_count

                reward_val = (
                    0.6 * test_score +
                    0.2 * lint_score +
                    0.2 * negotiation_score -
                    step_cost
                )

                # -------------------------
                # Cross-signal penalties
                # -------------------------
                if test_score > 0.8 and lint_score < 0.3:
                    reward_val *= 0.8

                if test_score < 0.3 and lint_score > 0.8:
                    reward_val *= 0.7

                if test_score > 0.8 and negotiation_score < 0.3:
                    reward_val *= 0.75

                # -------------------------
                # Author gating (only if not already convinced)
                # -------------------------
                threshold = self._author.thresholds.get(self._author.personality, 0.5)
                if self._author._confidence < threshold:
                    reward_val = max(0.0, reward_val - 0.3)
                    # Allow continuation if steps left
                    if self._step_count < self.max_steps:
                        self._done = False
                    else:
                        self._done = True
                else:
                    self._done = True

                reward_val = max(0.0, min(1.0, reward_val))

                self._test_results = f"Test score: {test_score:.2f}\n{test_output[:300]}"

        # ================================================================
        # TERMINATION
        # ================================================================
        elif isinstance(action, Skip):
            reward_val = -0.2
            self._done = True

        elif isinstance(action, Done):
            reward_val = -0.5
            self._done = True

        else:
            reward_val = -0.2
            self._done = True

        # ================================================================
        # STEP UPDATE
        # ================================================================
        self._step_count += 1

        if self._step_count >= self.max_steps:
            self._done = True

        obs = self._get_observation()

        return obs, Reward(value=reward_val), self._done, info

    # ------------------------------------------------------------------
    def _run_linter_score(self, code: str) -> float:
        try:
            with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
                f.write(code)
                tmp_path = f.name

            result = subprocess.run(
                ['pylint', tmp_path, '--score=y', '--exit-zero'],
                capture_output=True,
                text=True,
                timeout=5
            )

            match = re.search(r"rated at (\d+\.\d+)/10", result.stdout)

            if match:
                return float(match.group(1)) / 10.0

            return 0.0

        except:
            return 0.0

        finally:
            try:
                os.unlink(tmp_path)
            except:
                pass

    # ------------------------------------------------------------------
    def state(self) -> State:
        return State(
            pr_title="Code Review",
            pr_description=self._bug_description,
            code_snippet=self._current_code,
            comments=self._comments.copy(),
            test_results=self._test_results,
            step=self._step_count,
            done=self._done
        )