Siteshcodes commited on
Commit
6ff231e
Β·
1 Parent(s): 2824eb9

fix: auto-init bug in __init__ for stateless HTTP server

Browse files
Files changed (1) hide show
  1. server/environment.py +15 -45
server/environment.py CHANGED
@@ -4,11 +4,9 @@ sys.path.insert(0, "/app")
4
  sys.path.insert(0, "/app/server")
5
 
6
  import uuid
7
- import random
8
- from typing import Optional
9
  from openenv.core.env_server.interfaces import Environment
10
  from model import TriageAction, TriageObservation, TriageState, BugReport
11
- from task import TASKS, grade_action, sample_bug
12
 
13
 
14
  TASK_PROGRESSION = ["easy", "medium", "hard"]
@@ -23,8 +21,8 @@ class BugTriageEnvironment(Environment):
23
  Step 2 β†’ medium task (priority + labels + team)
24
  Step 3 β†’ hard task (priority + labels + team + milestone)
25
 
26
- Each reset() picks a fresh random bug from each task pool,
27
- so the agent never sees the same sequence twice.
28
  """
29
 
30
  def __init__(self):
@@ -34,24 +32,21 @@ class BugTriageEnvironment(Environment):
34
  step_count=0,
35
  total_score=0.0,
36
  tasks_completed=[],
37
- )
38
- self._current_bug: Optional[BugReport] = None
39
  self._current_task_key: str = "easy"
40
  self._episode_done: bool = False
41
-
42
- # ─────────────────────────────────────────
43
- # reset()
44
- # ─────────────────────────────────────────
45
 
46
  def reset(self) -> TriageObservation:
47
- """Start a fresh episode. Picks a random bug from the easy pool."""
48
  self._state = TriageState(
49
- episode_id=str(uuid.uuid4()),
50
- current_task="easy",
51
- step_count=0,
52
- total_score=0.0,
53
- tasks_completed=[],
54
- )
55
  self._current_task_key = "easy"
56
  self._episode_done = False
57
  self._current_bug = sample_bug("easy")
@@ -65,21 +60,9 @@ class BugTriageEnvironment(Environment):
65
  reward=0.0,
66
  )
67
 
68
- # ─────────────────────────────────────────
69
- # step()
70
- # ─────────────────────────────────────────
71
-
72
  def step(self, action: TriageAction) -> TriageObservation:
73
- """
74
- Process the agent's triage action and return the next observation.
75
-
76
- - Grades the current task
77
- - Advances to next task (easy β†’ medium β†’ hard)
78
- - Returns done=True after the hard task is graded
79
- """
80
- # Guard: prevent stepping after episode is over
81
  if self._episode_done:
82
- assert self._current_bug is not None
83
  return TriageObservation(
84
  bug_report=self._current_bug,
85
  task_id=self._current_task_key,
@@ -89,28 +72,21 @@ class BugTriageEnvironment(Environment):
89
  reward=0.0,
90
  )
91
 
92
- # Guard: step() must be called after reset()
93
- assert self._current_bug is not None, "step() called before reset()"
94
-
95
  self._state.step_count += 1
96
  task_key = self._current_task_key
97
 
98
- # Grade the action for this task
99
  score, feedback = grade_action(task_key, self._current_bug, action)
100
  self._state.total_score += score
101
  self._state.tasks_completed.append(task_key)
102
 
103
- # Determine progression
104
  current_idx = TASK_PROGRESSION.index(task_key)
105
- done = current_idx == len(TASK_PROGRESSION) - 1 # True after hard task
106
 
107
  if done:
108
- # Episode complete β€” keep current bug/task for final observation
109
  self._episode_done = True
110
  next_bug = self._current_bug
111
  next_task = self._current_task_key
112
  else:
113
- # Advance to next task with a fresh random bug
114
  next_task = TASK_PROGRESSION[current_idx + 1]
115
  next_bug = sample_bug(next_task)
116
  self._current_task_key = next_task
@@ -127,15 +103,9 @@ class BugTriageEnvironment(Environment):
127
  reward=round(score, 3),
128
  )
129
 
130
- # ─────────────────────────────────────────
131
- # state() β€” both property and method forms
132
- # ─────────────────────────────────────────
133
-
134
  @property
135
  def state(self) -> TriageState:
136
- """Property form β€” used internally."""
137
  return self._state
138
 
139
  def get_state(self) -> TriageState:
140
- """Method form β€” satisfies OpenEnv spec's state() interface."""
141
  return self._state
 
4
  sys.path.insert(0, "/app/server")
5
 
6
  import uuid
 
 
7
  from openenv.core.env_server.interfaces import Environment
8
  from model import TriageAction, TriageObservation, TriageState, BugReport
9
+ from task import grade_action, sample_bug
10
 
11
 
12
  TASK_PROGRESSION = ["easy", "medium", "hard"]
 
21
  Step 2 β†’ medium task (priority + labels + team)
22
  Step 3 β†’ hard task (priority + labels + team + milestone)
23
 
24
+ NOTE: OpenEnv HTTP server creates a new environment instance per
25
+ request. So __init__ auto-initializes a bug so step() always works.
26
  """
27
 
28
  def __init__(self):
 
32
  step_count=0,
33
  total_score=0.0,
34
  tasks_completed=[],
35
+ )
 
36
  self._current_task_key: str = "easy"
37
  self._episode_done: bool = False
38
+ # Auto-init bug so step() works on stateless HTTP server
39
+ self._current_bug: BugReport = sample_bug("easy")
 
 
40
 
41
  def reset(self) -> TriageObservation:
42
+ """Start a fresh episode."""
43
  self._state = TriageState(
44
+ episode_id=str(uuid.uuid4()),
45
+ current_task="easy",
46
+ step_count=0,
47
+ total_score=0.0,
48
+ tasks_completed=[],
49
+ )
50
  self._current_task_key = "easy"
51
  self._episode_done = False
52
  self._current_bug = sample_bug("easy")
 
60
  reward=0.0,
61
  )
62
 
 
 
 
 
63
  def step(self, action: TriageAction) -> TriageObservation:
64
+ """Process the agent's triage action."""
 
 
 
 
 
 
 
65
  if self._episode_done:
 
66
  return TriageObservation(
67
  bug_report=self._current_bug,
68
  task_id=self._current_task_key,
 
72
  reward=0.0,
73
  )
74
 
 
 
 
75
  self._state.step_count += 1
76
  task_key = self._current_task_key
77
 
 
78
  score, feedback = grade_action(task_key, self._current_bug, action)
79
  self._state.total_score += score
80
  self._state.tasks_completed.append(task_key)
81
 
 
82
  current_idx = TASK_PROGRESSION.index(task_key)
83
+ done = current_idx == len(TASK_PROGRESSION) - 1
84
 
85
  if done:
 
86
  self._episode_done = True
87
  next_bug = self._current_bug
88
  next_task = self._current_task_key
89
  else:
 
90
  next_task = TASK_PROGRESSION[current_idx + 1]
91
  next_bug = sample_bug(next_task)
92
  self._current_task_key = next_task
 
103
  reward=round(score, 3),
104
  )
105
 
 
 
 
 
106
  @property
107
  def state(self) -> TriageState:
 
108
  return self._state
109
 
110
  def get_state(self) -> TriageState:
 
111
  return self._state