Spaces:
Sleeping
Sleeping
Update environment.py
Browse files- environment.py +78 -78
environment.py
CHANGED
|
@@ -1,79 +1,79 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import uuid
|
| 4 |
-
from typing import Dict, Any, Tuple
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
|
| 7 |
-
# βββ OpenEnv Typed Models βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
-
class EnvState(BaseModel):
|
| 9 |
-
ticket_id: str
|
| 10 |
-
status: str
|
| 11 |
-
last_observation: str
|
| 12 |
-
steps_taken: int
|
| 13 |
-
reward: float
|
| 14 |
-
is_done: bool
|
| 15 |
-
|
| 16 |
-
class MTORServiceEnv:
|
| 17 |
-
def __init__(self):
|
| 18 |
-
self.tasks = {
|
| 19 |
-
"easy": "Check if the system 'spooler' service is running and report status.",
|
| 20 |
-
"medium": "The user cannot find the 'MTOR_LOG' environment variable. Create it with value 'ACTIVE'.",
|
| 21 |
-
"hard": "Diagnostic: The app 'AgentService' is crashing. Find the dummy log file in /data/logs/error.txt and extract the error code."
|
| 22 |
-
}
|
| 23 |
-
self.reset()
|
| 24 |
-
|
| 25 |
-
def reset(self, level: str = "easy") -> str:
|
| 26 |
-
"""Resets the environment to a starting state."""
|
| 27 |
-
self.current_ticket_id = f"TKT-{uuid.uuid4().hex[:6].upper()}"
|
| 28 |
-
self.current_task = self.tasks.get(level, self.tasks["easy"])
|
| 29 |
-
self.steps_taken = 0
|
| 30 |
-
self.current_reward = 0.0
|
| 31 |
-
self.is_done = False
|
| 32 |
-
self.last_obs = f"New Ticket Assigned: {self.current_task}"
|
| 33 |
-
return self.last_obs
|
| 34 |
-
|
| 35 |
-
def step(self, action_script: str) -> Tuple[str, float, bool, Dict]:
|
| 36 |
-
"""
|
| 37 |
-
Executes the agent's action (the generated script).
|
| 38 |
-
In a real OpenEnv, this would run the code. Here, we simulate the output.
|
| 39 |
-
"""
|
| 40 |
-
self.steps_taken += 1
|
| 41 |
-
|
| 42 |
-
# --- Meaningful Reward Function Logic ---
|
| 43 |
-
if not action_script or len(action_script) < 10:
|
| 44 |
-
observation = "Error: Empty or invalid script submitted."
|
| 45 |
-
reward = 0.0
|
| 46 |
-
else:
|
| 47 |
-
# Simulate progress signals
|
| 48 |
-
observation = f"Script executed successfully on {self.current_ticket_id}. Logs: Service status verified."
|
| 49 |
-
reward = 0.5 # Partial progress for writing a runnable script
|
| 50 |
-
|
| 51 |
-
# If the script contains keywords relevant to the task
|
| 52 |
-
if "status" in action_script.lower() or "os.environ" in action_script.lower():
|
| 53 |
-
reward = 1.0
|
| 54 |
-
self.is_done = True
|
| 55 |
-
|
| 56 |
-
self.current_reward = reward
|
| 57 |
-
return observation, reward, self.is_done, self.state().dict()
|
| 58 |
-
|
| 59 |
-
def state(self) -> EnvState:
|
| 60 |
-
"""Returns the current state of the environment."""
|
| 61 |
-
return EnvState(
|
| 62 |
-
ticket_id=self.current_ticket_id,
|
| 63 |
-
status="Resolved" if self.is_done else "In Progress",
|
| 64 |
-
last_observation=self.last_obs,
|
| 65 |
-
steps_taken=self.steps_taken,
|
| 66 |
-
reward=self.current_reward,
|
| 67 |
-
is_done=self.is_done
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
# βββ Example Baseline Inference Usage ββββββββββββββββββββββββββββββββββββββββ
|
| 71 |
-
if __name__ == "__main__":
|
| 72 |
-
env = MTORServiceEnv()
|
| 73 |
-
obs = env.reset(level="easy")
|
| 74 |
-
print(f"Observation: {obs}")
|
| 75 |
-
|
| 76 |
-
# Simulating an agent taking a step
|
| 77 |
-
script = "import os; print(os.name)"
|
| 78 |
-
new_obs, reward, done, info = env.step(script)
|
| 79 |
print(f"Reward: {reward} | Done: {done}")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from typing import Dict, Any, Tuple
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
|
| 7 |
+
# βββ OpenEnv Typed Models βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
+
class EnvState(BaseModel):
|
| 9 |
+
ticket_id: str
|
| 10 |
+
status: str
|
| 11 |
+
last_observation: str
|
| 12 |
+
steps_taken: int
|
| 13 |
+
reward: float
|
| 14 |
+
is_done: bool
|
| 15 |
+
|
| 16 |
+
class MTORServiceEnv:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.tasks = {
|
| 19 |
+
"easy": "Check if the system 'spooler' service is running and report status.",
|
| 20 |
+
"medium": "The user cannot find the 'MTOR_LOG' environment variable. Create it with value 'ACTIVE'.",
|
| 21 |
+
"hard": "Diagnostic: The app 'AgentService' is crashing. Find the dummy log file in /data/logs/error.txt and extract the error code."
|
| 22 |
+
}
|
| 23 |
+
self.reset()
|
| 24 |
+
|
| 25 |
+
def reset(self, level: str = "easy") -> str:
|
| 26 |
+
"""Resets the environment to a starting state."""
|
| 27 |
+
self.current_ticket_id = f"TKT-{uuid.uuid4().hex[:6].upper()}"
|
| 28 |
+
self.current_task = self.tasks.get(level, self.tasks["easy"])
|
| 29 |
+
self.steps_taken = 0
|
| 30 |
+
self.current_reward = 0.0
|
| 31 |
+
self.is_done = False
|
| 32 |
+
self.last_obs = f"New Ticket Assigned: {self.current_task}"
|
| 33 |
+
return self.last_obs
|
| 34 |
+
|
| 35 |
+
def step(self, action_script: str) -> Tuple[str, float, bool, Dict]:
|
| 36 |
+
"""
|
| 37 |
+
Executes the agent's action (the generated script).
|
| 38 |
+
In a real OpenEnv, this would run the code. Here, we simulate the output.
|
| 39 |
+
"""
|
| 40 |
+
self.steps_taken += 1
|
| 41 |
+
|
| 42 |
+
# --- Meaningful Reward Function Logic ---
|
| 43 |
+
if not action_script or len(action_script) < 10:
|
| 44 |
+
observation = "Error: Empty or invalid script submitted."
|
| 45 |
+
reward = 0.0
|
| 46 |
+
else:
|
| 47 |
+
# Simulate progress signals
|
| 48 |
+
observation = f"Script executed successfully on {self.current_ticket_id}. Logs: Service status verified."
|
| 49 |
+
reward = 0.5 # Partial progress for writing a runnable script
|
| 50 |
+
|
| 51 |
+
# If the script contains keywords relevant to the task
|
| 52 |
+
if "status" in action_script.lower() or "os.environ" in action_script.lower():
|
| 53 |
+
reward = 1.0
|
| 54 |
+
self.is_done = True
|
| 55 |
+
|
| 56 |
+
self.current_reward = reward
|
| 57 |
+
return observation, reward, self.is_done, self.state().dict()
|
| 58 |
+
|
| 59 |
+
def state(self) -> EnvState:
|
| 60 |
+
"""Returns the current state of the environment."""
|
| 61 |
+
return EnvState(
|
| 62 |
+
ticket_id=self.current_ticket_id,
|
| 63 |
+
status="Resolved" if self.is_done else "In Progress",
|
| 64 |
+
last_observation=self.last_obs,
|
| 65 |
+
steps_taken=self.steps_taken,
|
| 66 |
+
reward=self.current_reward,
|
| 67 |
+
is_done=self.is_done
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# βββ Example Baseline Inference Usage ββββββββββββββββββββββββββββββββββββββββ
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
env = MTORServiceEnv()
|
| 73 |
+
obs = env.reset(level="easy")
|
| 74 |
+
print(f"Observation: {obs}")
|
| 75 |
+
|
| 76 |
+
# Simulating an agent taking a step
|
| 77 |
+
script = "import os; print(os.name)"
|
| 78 |
+
new_obs, reward, done, info = env.step(script)
|
| 79 |
print(f"Reward: {reward} | Done: {done}")
|