Spaces:
Runtime error
Runtime error
File size: 3,577 Bytes
877de20 bfb35fc | 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 | import json
from typing import Tuple, Dict, Any
from models import Observation, Action, PullRequest
class RepoReviewerEnv:
def __init__(self, task_path: str):
self.task_path = task_path
self._done = False
self._current_obs = None
with open(self.task_path, 'r') as f:
self.task_data = json.load(f)
self._mock_file_system = self.task_data.get("file_system", {})
self._target_solution = self.task_data.get("target_solution", {})
self._ci_checks = self.task_data.get("ci_checks", {})
def reset(self) -> Observation:
pr_data = self.task_data["pr"]
self._current_obs = Observation(
current_pr=PullRequest(**pr_data),
repo_rules=self.task_data.get("rules", []),
terminal_output="System initialized. Awaiting review. Hint: Run the linter and tests."
)
self._done = False
return self._current_obs
def state(self) -> Observation:
return self._current_obs
def step(self, action: Action) -> Tuple[Observation, float, bool, Dict[str, Any]]:
if self._done:
return self._current_obs, 0.0, True, {"msg": "Environment already finished."}
reward = 0.0
info = {}
if action.action_type == 'view_diff':
if action.target_file in self._mock_file_system:
file_content = self._mock_file_system[action.target_file]
self._current_obs.terminal_output = f"--- {action.target_file} ---\n{file_content}"
reward = 0.1
else:
self._current_obs.terminal_output = f"Error: File '{action.target_file}' not found."
reward = -0.1
elif action.action_type == 'run_linter':
self._current_obs.terminal_output = f"Linter Output:\n{self._ci_checks.get('linter', 'No output.')}"
reward = 0.1
elif action.action_type == 'run_tests':
self._current_obs.terminal_output = f"Test Output:\n{self._ci_checks.get('tests', 'No output.')}"
reward = 0.1
elif action.action_type in ['approve_pr', 'reject_pr', 'request_changes']:
self._done = True
expected_action = self._target_solution.get("action_type")
if action.action_type != expected_action:
reward = -1.0
info['evaluation'] = f"Failure: Agent chose {action.action_type}, expected {expected_action}."
else:
is_correct = True
expected_line = self._target_solution.get("line_number")
if expected_line and action.line_number != expected_line:
is_correct = False
info['evaluation'] = f"Failure: Wrong line number. Expected {expected_line}, got {action.line_number}."
expected_keyword = self._target_solution.get("required_keyword")
comment = (action.comment or "").lower()
if expected_keyword and expected_keyword.lower() not in comment:
is_correct = False
info['evaluation'] = f"Failure: Missing context. Expected comment to mention '{expected_keyword}'."
if is_correct:
reward = 1.0
info['evaluation'] = "Success: Correct action and correctly identified the root cause."
else:
reward = -0.5
return self._current_obs, reward, self._done, info |