Spaces:
Sleeping
Sleeping
File size: 4,411 Bytes
80a5932 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | from typing import Dict, List, Any
from .virtual_filesystem import SystemStore
from .terminal_emulator import Shell
from .tasks import (
Objective, ScenarioTask,
get_task, all_task_keys, task_metadata,
)
from .scenarios import list_scenarios
class TrainingEnv:
def __init__(self, scenario: str = "log_analysis"):
"""
Create environment for a given scenario key.
"""
key = scenario
self.task = get_task(key)
self.difficulty = self.task.diff
self.storage = SystemStore()
self.terminal = Shell(self.storage)
self.step_count = 0
self.limit = 50
self.finished = False
self.score = 0.0
self._scenario_key = key
# scenario tasks may override max_steps
if isinstance(self.task, ScenarioTask):
self.limit = self.task.scenario.max_steps
def reset(self) -> Dict[str, Any]:
self.storage.clear()
self.terminal = Shell(self.storage)
self.step_count = 0
self.finished = False
self.score = 0.0
# for scenario tasks, inject faults into the fresh environment
if isinstance(self.task, ScenarioTask):
self.task.setup(self.storage, self.terminal)
view = self._view()
return {
"observation": view,
"info": {
"task_name": self.task.nm,
"difficulty": self.difficulty,
"instructions": self.task.guide(),
"max_steps": self.limit,
"scenario_key": self._scenario_key,
}
}
def step(self, cmd: str) -> Dict[str, Any]:
if self.finished:
return {
"observation": self._view(),
"reward": 0.0,
"done": True,
"info": {"error": "Episode already complete. Call reset() first."}
}
self.step_count += 1
out, code = self.terminal.run(cmd)
penalty = -0.01
done = self.step_count >= self.limit
curr_score, task_info = self.task.eval(self.storage, self.terminal)
if curr_score > self.score:
bonus = (curr_score - self.score) * 0.5
penalty += bonus
self.score = curr_score
if curr_score >= 1.0 and not done:
penalty += 0.5
done = True
self.finished = True
self.score = min(curr_score, 0.99)
if done and not self.finished:
self.finished = True
view = self._view()
return {
"observation": view,
"reward": penalty,
"done": done,
"info": {
"task_score": self.score,
"command": cmd,
"command_output": out,
"exit_code": code,
"output_length": len(out),
"step": self.step_count,
"max_steps": self.limit,
"task_metadata": task_info,
}
}
def _view(self) -> Dict[str, Any]:
pout, _ = self.terminal.run("ps")
lout, _ = self.terminal.run("ls /home/user/scripts")
return {
"current_directory": self.terminal.cwd,
"processes": pout,
"filesystem": lout,
"task_name": self.task.nm,
"task_description": self.task.desc,
"request": "Use commands to complete the task. Type your command below."
}
def dump(self) -> Dict:
snap = self.storage.snapshot()
return {
"filesystem": snap,
"episode_step": self.step_count,
"max_steps": self.limit,
"task_score": self.score,
"task_difficulty": self.difficulty,
"scenario_key": self._scenario_key,
"command_history": self.terminal.history(),
}
# ------------------------------------------------------------------
# Class-level helpers
# ------------------------------------------------------------------
@staticmethod
def avail_tasks() -> List[str]:
return all_task_keys()
@staticmethod
def task_details(key: str) -> Dict[str, Any]:
try:
return task_metadata(key)
except ValueError:
return {}
@staticmethod
def avail_scenarios() -> Dict[str, Dict]:
return list_scenarios()
|