Spaces:
Sleeping
Sleeping
fix: reset POST - correct State fields, reward=0.0 on reset, complete Dockerfile + openenv.yaml
Browse files- Dockerfile +5 -2
- environment.py +70 -42
- models.py +9 -7
- openenv.yaml +6 -3
Dockerfile
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
| 3 |
WORKDIR /app
|
|
|
|
| 4 |
COPY requirements.txt .
|
| 5 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 6 |
-
RUN pip install openenv-core>=0.2.2 fastapi uvicorn pydantic
|
| 7 |
|
| 8 |
COPY . .
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
+
|
| 5 |
COPY requirements.txt .
|
| 6 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
RUN pip install --no-cache-dir "openenv-core>=0.2.2" fastapi uvicorn pydantic anthropic openai
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
+
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
environment.py
CHANGED
|
@@ -2,91 +2,119 @@ import uuid
|
|
| 2 |
from openenv.core.env_server import Environment
|
| 3 |
from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
|
| 4 |
|
|
|
|
| 5 |
class CodeBugEnvironment(Environment):
|
| 6 |
SUPPORTS_CONCURRENT_SESSIONS = True
|
| 7 |
-
|
| 8 |
-
# 3 minimum tasks: easy -> medium -> hard
|
| 9 |
TASKS = [
|
| 10 |
{
|
| 11 |
"difficulty": "easy",
|
| 12 |
-
"snippet":
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"difficulty": "medium",
|
| 17 |
-
"snippet":
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"difficulty": "hard",
|
| 22 |
-
"snippet":
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
|
| 27 |
def __init__(self):
|
| 28 |
self._state = CodeReviewState()
|
| 29 |
|
| 30 |
def reset(self, seed=None, episode_id=None, **kwargs) -> CodeReviewObservation:
|
|
|
|
| 31 |
self._state = CodeReviewState(
|
| 32 |
-
episode_id=episode_id
|
| 33 |
step_count=0,
|
| 34 |
current_task_idx=0,
|
| 35 |
-
total_score=0.0
|
| 36 |
)
|
| 37 |
task = self.TASKS[0]
|
| 38 |
return CodeReviewObservation(
|
| 39 |
done=False,
|
| 40 |
-
reward=
|
| 41 |
code_snippet=task["snippet"],
|
| 42 |
difficulty=task["difficulty"],
|
| 43 |
-
feedback=
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
def step(self, action: CodeReviewAction, timeout_s=None, **kwargs) -> CodeReviewObservation:
|
|
|
|
| 47 |
self._state.step_count += 1
|
| 48 |
-
|
| 49 |
-
idx
|
| 50 |
-
task
|
| 51 |
truth = task["truth"]
|
| 52 |
-
|
| 53 |
-
# Reward
|
| 54 |
reward = 0.0
|
| 55 |
-
|
| 56 |
-
is_correct_category = action.category.strip().lower() == truth["category"]
|
| 57 |
-
if is_correct_category:
|
| 58 |
reward += 0.5
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if severity_diff == 0:
|
| 62 |
reward += 0.3
|
| 63 |
-
elif
|
| 64 |
reward += 0.15
|
| 65 |
-
|
| 66 |
-
if action.line_hint == truth["line_hint"]:
|
| 67 |
reward += 0.2
|
| 68 |
-
|
| 69 |
self._state.total_score += reward
|
| 70 |
self._state.current_task_idx += 1
|
| 71 |
-
|
| 72 |
done = self._state.current_task_idx >= len(self.TASKS)
|
| 73 |
-
|
| 74 |
if done:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
return CodeReviewObservation(
|
| 85 |
-
done=
|
| 86 |
reward=reward,
|
| 87 |
-
code_snippet=
|
| 88 |
-
difficulty=
|
| 89 |
-
feedback=
|
| 90 |
)
|
| 91 |
|
| 92 |
@property
|
|
|
|
| 2 |
from openenv.core.env_server import Environment
|
| 3 |
from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
|
| 4 |
|
| 5 |
+
|
| 6 |
class CodeBugEnvironment(Environment):
|
| 7 |
SUPPORTS_CONCURRENT_SESSIONS = True
|
| 8 |
+
|
|
|
|
| 9 |
TASKS = [
|
| 10 |
{
|
| 11 |
"difficulty": "easy",
|
| 12 |
+
"snippet": (
|
| 13 |
+
"def transfer(from_account, to_account, amount):\n"
|
| 14 |
+
" if from_account.balance > amount:\n"
|
| 15 |
+
" from_account.balance -= amount\n"
|
| 16 |
+
" to_account.balance += amount\n"
|
| 17 |
+
" return True\n"
|
| 18 |
+
" return False"
|
| 19 |
+
),
|
| 20 |
+
"truth": {"category": "logic", "severity": 4, "line_hint": 2},
|
| 21 |
},
|
| 22 |
{
|
| 23 |
"difficulty": "medium",
|
| 24 |
+
"snippet": (
|
| 25 |
+
"def find_max(numbers):\n"
|
| 26 |
+
" if not numbers:\n"
|
| 27 |
+
" return None\n"
|
| 28 |
+
" max_val = numbers[0]\n"
|
| 29 |
+
" for i in range(len(numbers) - 1):\n"
|
| 30 |
+
" if numbers[i] > max_val:\n"
|
| 31 |
+
" max_val = numbers[i]\n"
|
| 32 |
+
" return max_val"
|
| 33 |
+
),
|
| 34 |
+
"truth": {"category": "logic", "severity": 3, "line_hint": 5},
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"difficulty": "hard",
|
| 38 |
+
"snippet": (
|
| 39 |
+
"def login(username, password):\n"
|
| 40 |
+
" query = f\"SELECT * FROM users WHERE username = '{username}'"
|
| 41 |
+
" AND password = '{password}'\"\n"
|
| 42 |
+
" user = db.execute(query)\n"
|
| 43 |
+
" if user:\n"
|
| 44 |
+
" return True\n"
|
| 45 |
+
" return False"
|
| 46 |
+
),
|
| 47 |
+
"truth": {"category": "security", "severity": 5, "line_hint": 2},
|
| 48 |
+
},
|
| 49 |
]
|
| 50 |
|
| 51 |
def __init__(self):
|
| 52 |
self._state = CodeReviewState()
|
| 53 |
|
| 54 |
def reset(self, seed=None, episode_id=None, **kwargs) -> CodeReviewObservation:
|
| 55 |
+
"""Reset environment to initial state. Called by POST /reset."""
|
| 56 |
self._state = CodeReviewState(
|
| 57 |
+
episode_id=str(episode_id) if episode_id else str(uuid.uuid4()),
|
| 58 |
step_count=0,
|
| 59 |
current_task_idx=0,
|
| 60 |
+
total_score=0.0,
|
| 61 |
)
|
| 62 |
task = self.TASKS[0]
|
| 63 |
return CodeReviewObservation(
|
| 64 |
done=False,
|
| 65 |
+
reward=0.0,
|
| 66 |
code_snippet=task["snippet"],
|
| 67 |
difficulty=task["difficulty"],
|
| 68 |
+
feedback=(
|
| 69 |
+
"Episode started. Review the code snippet: "
|
| 70 |
+
"identify the bug category, severity (1-5), and line number."
|
| 71 |
+
),
|
| 72 |
)
|
| 73 |
|
| 74 |
def step(self, action: CodeReviewAction, timeout_s=None, **kwargs) -> CodeReviewObservation:
|
| 75 |
+
"""Grade the agent's action and advance to the next task."""
|
| 76 |
self._state.step_count += 1
|
| 77 |
+
|
| 78 |
+
idx = self._state.current_task_idx
|
| 79 |
+
task = self.TASKS[idx]
|
| 80 |
truth = task["truth"]
|
| 81 |
+
|
| 82 |
+
# Reward: 0.0 → 1.0
|
| 83 |
reward = 0.0
|
| 84 |
+
if action.category.strip().lower() == truth["category"]:
|
|
|
|
|
|
|
| 85 |
reward += 0.5
|
| 86 |
+
sev_diff = abs(action.severity - truth["severity"])
|
| 87 |
+
if sev_diff == 0:
|
|
|
|
| 88 |
reward += 0.3
|
| 89 |
+
elif sev_diff == 1:
|
| 90 |
reward += 0.15
|
| 91 |
+
if action.line_hint is not None and action.line_hint == truth["line_hint"]:
|
|
|
|
| 92 |
reward += 0.2
|
| 93 |
+
|
| 94 |
self._state.total_score += reward
|
| 95 |
self._state.current_task_idx += 1
|
| 96 |
+
|
| 97 |
done = self._state.current_task_idx >= len(self.TASKS)
|
| 98 |
+
|
| 99 |
if done:
|
| 100 |
+
return CodeReviewObservation(
|
| 101 |
+
done=True,
|
| 102 |
+
reward=reward,
|
| 103 |
+
code_snippet="",
|
| 104 |
+
difficulty="",
|
| 105 |
+
feedback=(
|
| 106 |
+
f"Episode complete. "
|
| 107 |
+
f"Total score: {self._state.total_score:.2f} / {len(self.TASKS):.1f}"
|
| 108 |
+
),
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
next_task = self.TASKS[self._state.current_task_idx]
|
| 112 |
return CodeReviewObservation(
|
| 113 |
+
done=False,
|
| 114 |
reward=reward,
|
| 115 |
+
code_snippet=next_task["snippet"],
|
| 116 |
+
difficulty=next_task["difficulty"],
|
| 117 |
+
feedback=f"Step reward: {reward:.2f}. Now review the next snippet.",
|
| 118 |
)
|
| 119 |
|
| 120 |
@property
|
models.py
CHANGED
|
@@ -2,16 +2,18 @@ from typing import Optional
|
|
| 2 |
from openenv.core.env_server import Action, Observation, State
|
| 3 |
|
| 4 |
class CodeReviewAction(Action):
|
| 5 |
-
category: str
|
| 6 |
-
severity: int
|
| 7 |
-
line_hint: Optional[int]
|
| 8 |
-
comment: str
|
| 9 |
|
| 10 |
class CodeReviewObservation(Observation):
|
| 11 |
-
code_snippet: str
|
| 12 |
-
difficulty: str
|
| 13 |
-
feedback: str
|
| 14 |
|
| 15 |
class CodeReviewState(State):
|
|
|
|
|
|
|
| 16 |
current_task_idx: int = 0
|
| 17 |
total_score: float = 0.0
|
|
|
|
| 2 |
from openenv.core.env_server import Action, Observation, State
|
| 3 |
|
| 4 |
class CodeReviewAction(Action):
|
| 5 |
+
category: str # 'security' | 'logic' | 'style' | 'approve'
|
| 6 |
+
severity: int # 1-5 (5 = most severe)
|
| 7 |
+
line_hint: Optional[int] = None
|
| 8 |
+
comment: str = "" # Short explanation
|
| 9 |
|
| 10 |
class CodeReviewObservation(Observation):
|
| 11 |
+
code_snippet: str = ""
|
| 12 |
+
difficulty: str = "" # 'easy' | 'medium' | 'hard'
|
| 13 |
+
feedback: str = ""
|
| 14 |
|
| 15 |
class CodeReviewState(State):
|
| 16 |
+
episode_id: str = ""
|
| 17 |
+
step_count: int = 0
|
| 18 |
current_task_idx: int = 0
|
| 19 |
total_score: float = 0.0
|
openenv.yaml
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
name: "CodeBug Review Environment"
|
| 2 |
version: "1.0.0"
|
| 3 |
-
description: "
|
| 4 |
entry_point: "main:app"
|
| 5 |
-
author: "
|
| 6 |
-
tags: ["rl", "code-review", "agent", "
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
name: "CodeBug Review Environment"
|
| 2 |
version: "1.0.0"
|
| 3 |
+
description: "RL environment where an AI agent reviews code snippets to identify bugs. Three tasks: easy (logic/boundary), medium (off-by-one), hard (SQL injection). Reward 0-1 per step."
|
| 4 |
entry_point: "main:app"
|
| 5 |
+
author: "vinith17"
|
| 6 |
+
tags: ["rl", "code-review", "security", "agent", "pytorch-hackathon"]
|
| 7 |
+
environment_class: "environment.CodeBugEnvironment"
|
| 8 |
+
action_class: "models.CodeReviewAction"
|
| 9 |
+
observation_class: "models.CodeReviewObservation"
|