Spaces:
Sleeping
Sleeping
Update server/my_env_environment.py
Browse files- server/my_env_environment.py +58 -134
server/my_env_environment.py
CHANGED
|
@@ -1,23 +1,17 @@
|
|
| 1 |
-
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
-
# All rights reserved.
|
| 3 |
-
|
| 4 |
-
"""FixOS environment implementation."""
|
| 5 |
-
|
| 6 |
from dataclasses import dataclass
|
| 7 |
from uuid import uuid4
|
| 8 |
-
import
|
| 9 |
-
from typing import Any, Callable, Dict, List, Tuple
|
| 10 |
|
| 11 |
from openenv.core.env_server.interfaces import Environment
|
| 12 |
from openenv.core.env_server.types import State
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
from ..models import
|
| 16 |
except ImportError:
|
| 17 |
-
from models import
|
| 18 |
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def fix_score(score: float) -> float:
|
| 22 |
return max(0.01, min(0.99, score))
|
| 23 |
|
|
@@ -31,147 +25,74 @@ class TaskDefinition:
|
|
| 31 |
success_check: Callable[[Dict[str, Any]], bool]
|
| 32 |
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
]
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def _task_score_base(state: Dict[str, Any]) -> float:
|
| 40 |
-
score = 0.0
|
| 41 |
-
if state["disk_percent"] <= 95:
|
| 42 |
-
score += 0.2
|
| 43 |
-
if state["nginx_status"] == "running":
|
| 44 |
-
score += 0.2
|
| 45 |
-
if state["mysql_status"] == "running":
|
| 46 |
-
score += 0.2
|
| 47 |
-
if state["nginx_config_valid"]:
|
| 48 |
-
score += 0.2
|
| 49 |
-
if state["mysql_config_valid"]:
|
| 50 |
-
score += 0.2
|
| 51 |
-
return fix_score(score)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
TASKS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
class FixOSEnvironment(Environment):
|
| 59 |
|
| 60 |
-
SUPPORTS_CONCURRENT_SESSIONS
|
| 61 |
-
MAX_STEPS_PER_EPISODE
|
| 62 |
|
| 63 |
def __init__(self):
|
| 64 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 65 |
-
self._current_task = None
|
| 66 |
self._task_pointer = -1
|
|
|
|
| 67 |
self._env_state: Dict[str, Any] = {}
|
| 68 |
self._previous_score = 0.0
|
| 69 |
|
| 70 |
def reset(self):
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
self._task_pointer = (self._task_pointer + 1) % len(TASK_ORDER)
|
| 74 |
-
self._current_task = TASKS[TASK_ORDER[self._task_pointer]]
|
| 75 |
-
|
| 76 |
-
self._env_state = {
|
| 77 |
-
"task_id": self._current_task.task_id,
|
| 78 |
-
"difficulty": self._current_task.difficulty,
|
| 79 |
-
"nginx_status": "running",
|
| 80 |
-
"mysql_status": "running",
|
| 81 |
-
"nginx_config_valid": True,
|
| 82 |
-
"mysql_config_valid": True,
|
| 83 |
-
"disk_percent": 45.0,
|
| 84 |
-
"processes": [],
|
| 85 |
-
"files": {},
|
| 86 |
-
"logs": [],
|
| 87 |
-
"history": [],
|
| 88 |
-
"logs_checked": False,
|
| 89 |
-
"config_checked": {"nginx": False, "mysql": False},
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
self._apply_failures(self._current_task.failures)
|
| 93 |
-
self._refresh_resources()
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
self._state.step_count += 1
|
| 101 |
-
|
| 102 |
-
reward = 0.0
|
| 103 |
-
|
| 104 |
-
current_score = self._compute_task_score()
|
| 105 |
-
delta = max(0.0, current_score - self._previous_score)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
reward += 0.4 * delta
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
reward += 0.5
|
| 116 |
-
|
| 117 |
-
done = is_success or self._state.step_count >= self.MAX_STEPS_PER_EPISODE
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
task_score=current_score,
|
| 127 |
-
)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
(0.7 if ((s["nginx_status"] == "running" and task_id == "easy_1")
|
| 136 |
-
or (s["mysql_status"] == "running" and task_id == "easy_2")) else 0.0)
|
| 137 |
-
+ (0.3 if s["disk_percent"] < 95 else 0.0)
|
| 138 |
-
)
|
| 139 |
-
return fix_score(raw)
|
| 140 |
-
|
| 141 |
-
score = 0.0
|
| 142 |
-
|
| 143 |
-
if task_id == "medium_1":
|
| 144 |
-
score += 0.2 if s["logs_checked"] else 0.0
|
| 145 |
-
score += 0.3 if s["nginx_config_valid"] else 0.0
|
| 146 |
-
score += 0.5 if s["nginx_status"] == "running" else 0.0
|
| 147 |
-
|
| 148 |
-
elif task_id == "medium_2":
|
| 149 |
-
score += 0.2 if s["logs_checked"] else 0.0
|
| 150 |
-
score += 0.3 if s["mysql_config_valid"] else 0.0
|
| 151 |
-
score += 0.5 if s["mysql_status"] == "running" else 0.0
|
| 152 |
-
|
| 153 |
-
else:
|
| 154 |
-
return _task_score_base(s)
|
| 155 |
-
|
| 156 |
-
return fix_score(score)
|
| 157 |
-
|
| 158 |
-
def _apply_failures(self, failures):
|
| 159 |
-
for k, v in failures.items():
|
| 160 |
-
self._env_state[k] = v
|
| 161 |
-
|
| 162 |
-
def _refresh_resources(self):
|
| 163 |
-
self._env_state["cpu_percent"] = 50.0
|
| 164 |
-
self._env_state["memory_mb"] = 500.0
|
| 165 |
-
|
| 166 |
-
def _build_observation(
|
| 167 |
-
self,
|
| 168 |
-
command_output,
|
| 169 |
-
reward=0.0,
|
| 170 |
-
done=False,
|
| 171 |
-
is_success_step=False,
|
| 172 |
-
task_score=None,
|
| 173 |
-
):
|
| 174 |
-
return FixOSObservation(
|
| 175 |
command_output=command_output,
|
| 176 |
processes=[],
|
| 177 |
services=[],
|
|
@@ -179,17 +100,20 @@ class FixOSEnvironment(Environment):
|
|
| 179 |
resources={},
|
| 180 |
logs=[],
|
| 181 |
history=[],
|
| 182 |
-
task_id=self._env_state
|
| 183 |
-
task_difficulty=self._env_state
|
| 184 |
-
task_score=fix_score(
|
| 185 |
-
|
| 186 |
-
),
|
| 187 |
-
is_success_step=is_success_step,
|
| 188 |
remaining_steps=max(0, self.MAX_STEPS_PER_EPISODE - self._state.step_count),
|
| 189 |
done=done,
|
| 190 |
reward=fix_score(reward),
|
| 191 |
)
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
@property
|
| 194 |
def state(self):
|
| 195 |
return self._state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
from uuid import uuid4
|
| 3 |
+
from typing import Any, Callable, Dict, List
|
|
|
|
| 4 |
|
| 5 |
from openenv.core.env_server.interfaces import Environment
|
| 6 |
from openenv.core.env_server.types import State
|
| 7 |
|
| 8 |
try:
|
| 9 |
+
from ..models import FixOSObservation
|
| 10 |
except ImportError:
|
| 11 |
+
from models import FixOSObservation
|
| 12 |
|
| 13 |
|
| 14 |
+
# Score clamp
|
| 15 |
def fix_score(score: float) -> float:
|
| 16 |
return max(0.01, min(0.99, score))
|
| 17 |
|
|
|
|
| 25 |
success_check: Callable[[Dict[str, Any]], bool]
|
| 26 |
|
| 27 |
|
| 28 |
+
# Dummy tasks (safe fallback)
|
| 29 |
+
TASK_ORDER = ["easy_1"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
TASKS = {
|
| 32 |
+
"easy_1": TaskDefinition(
|
| 33 |
+
task_id="easy_1",
|
| 34 |
+
difficulty="easy",
|
| 35 |
+
failures={},
|
| 36 |
+
optimal_steps=3,
|
| 37 |
+
success_check=lambda state: True,
|
| 38 |
+
)
|
| 39 |
+
}
|
| 40 |
|
| 41 |
|
| 42 |
class FixOSEnvironment(Environment):
|
| 43 |
|
| 44 |
+
SUPPORTS_CONCURRENT_SESSIONS = True
|
| 45 |
+
MAX_STEPS_PER_EPISODE = 50
|
| 46 |
|
| 47 |
def __init__(self):
|
| 48 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
|
|
|
| 49 |
self._task_pointer = -1
|
| 50 |
+
self._current_task = None
|
| 51 |
self._env_state: Dict[str, Any] = {}
|
| 52 |
self._previous_score = 0.0
|
| 53 |
|
| 54 |
def reset(self):
|
| 55 |
+
try:
|
| 56 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
self._task_pointer = (self._task_pointer + 1) % len(TASK_ORDER)
|
| 59 |
+
self._current_task = TASKS[TASK_ORDER[self._task_pointer]]
|
| 60 |
|
| 61 |
+
self._env_state = {
|
| 62 |
+
"task_id": self._current_task.task_id,
|
| 63 |
+
"difficulty": self._current_task.difficulty,
|
| 64 |
+
}
|
| 65 |
|
| 66 |
+
self._previous_score = 0.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return self._build_response("[INFO] Environment reset")
|
|
|
|
| 69 |
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return {
|
| 72 |
+
"observation": {"error": str(e)},
|
| 73 |
+
"info": {}
|
| 74 |
+
}
|
| 75 |
|
| 76 |
+
def step(self, action):
|
| 77 |
+
try:
|
| 78 |
+
self._state.step_count += 1
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
done = self._state.step_count >= self.MAX_STEPS_PER_EPISODE
|
| 81 |
|
| 82 |
+
return self._build_response(
|
| 83 |
+
command_output="step executed",
|
| 84 |
+
reward=0.5,
|
| 85 |
+
done=done
|
| 86 |
+
)
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return {
|
| 90 |
+
"observation": {"error": str(e)},
|
| 91 |
+
"info": {}
|
| 92 |
+
}
|
| 93 |
|
| 94 |
+
def _build_response(self, command_output, reward=0.0, done=False):
|
| 95 |
+
obs = FixOSObservation(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
command_output=command_output,
|
| 97 |
processes=[],
|
| 98 |
services=[],
|
|
|
|
| 100 |
resources={},
|
| 101 |
logs=[],
|
| 102 |
history=[],
|
| 103 |
+
task_id=self._env_state.get("task_id", "unknown"),
|
| 104 |
+
task_difficulty=self._env_state.get("difficulty", "easy"),
|
| 105 |
+
task_score=fix_score(self._previous_score),
|
| 106 |
+
is_success_step=False,
|
|
|
|
|
|
|
| 107 |
remaining_steps=max(0, self.MAX_STEPS_PER_EPISODE - self._state.step_count),
|
| 108 |
done=done,
|
| 109 |
reward=fix_score(reward),
|
| 110 |
)
|
| 111 |
|
| 112 |
+
return {
|
| 113 |
+
"observation": obs.model_dump(), # KEY FIX
|
| 114 |
+
"info": {}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
@property
|
| 118 |
def state(self):
|
| 119 |
return self._state
|