Spaces:
Sleeping
Sleeping
Update client.py
Browse files
client.py
CHANGED
|
@@ -1,86 +1,309 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""FixOS baseline inference script."""
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import time
|
| 8 |
+
from typing import Any, Dict, Optional
|
| 9 |
+
from urllib import error as urlerror
|
| 10 |
+
from urllib import request as urlrequest
|
| 11 |
+
|
| 12 |
+
from openai import APIConnectionError, APIError, OpenAI, RateLimitError
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _load_local_env_class():
|
| 16 |
+
errors = []
|
| 17 |
+
try:
|
| 18 |
+
from server.my_env_environment import FixOSEnvironment # type: ignore
|
| 19 |
+
|
| 20 |
+
return FixOSEnvironment, ""
|
| 21 |
+
except Exception as exc:
|
| 22 |
+
errors.append(f"server.my_env_environment: {exc}")
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
from my_env.server.my_env_environment import FixOSEnvironment # type: ignore
|
| 26 |
+
|
| 27 |
+
return FixOSEnvironment, ""
|
| 28 |
+
except Exception as exc:
|
| 29 |
+
errors.append(f"my_env.server.my_env_environment: {exc}")
|
| 30 |
+
|
| 31 |
+
return None, " | ".join(errors)
|
| 32 |
+
|
| 33 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
| 34 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _env_required(name: str) -> str:
|
| 38 |
+
value = os.getenv(name)
|
| 39 |
+
if not value:
|
| 40 |
+
raise ValueError(f"Missing required environment variable: {name}")
|
| 41 |
+
return value
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class EnvHTTPClient:
|
| 45 |
+
def __init__(self, base_url: str):
|
| 46 |
+
self.base_url = base_url.rstrip("/")
|
| 47 |
+
|
| 48 |
+
def reset(self) -> Dict[str, Any]:
|
| 49 |
+
return self._post_json("/reset", {})
|
| 50 |
+
|
| 51 |
+
def step(self, action: Dict[str, Any]) -> Dict[str, Any]:
|
| 52 |
+
try:
|
| 53 |
+
return self._post_json("/step", action)
|
| 54 |
+
except RuntimeError as exc:
|
| 55 |
+
if "422" in str(exc):
|
| 56 |
+
return self._post_json("/step", {"action": action})
|
| 57 |
+
raise
|
| 58 |
+
|
| 59 |
+
def _post_json(self, path: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
| 60 |
+
req = urlrequest.Request(
|
| 61 |
+
url=f"{self.base_url}{path}",
|
| 62 |
+
data=json.dumps(payload).encode("utf-8"),
|
| 63 |
+
headers={"Content-Type": "application/json"},
|
| 64 |
+
method="POST",
|
| 65 |
+
)
|
| 66 |
+
try:
|
| 67 |
+
with urlrequest.urlopen(req, timeout=30) as resp:
|
| 68 |
+
return json.loads(resp.read().decode("utf-8"))
|
| 69 |
+
except urlerror.URLError as exc:
|
| 70 |
+
raise RuntimeError(f"HTTP request failed for {path}: {exc}") from exc
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class LocalEnvClient:
|
| 74 |
+
def __init__(self):
|
| 75 |
+
FixOSEnvironment, import_error = _load_local_env_class()
|
| 76 |
+
if FixOSEnvironment is None:
|
| 77 |
+
raise RuntimeError(f"Local FixOS environment is unavailable (import failed): {import_error}")
|
| 78 |
+
self.env = FixOSEnvironment()
|
| 79 |
+
|
| 80 |
+
def reset(self) -> Dict[str, Any]:
|
| 81 |
+
observation = self.env.reset()
|
| 82 |
+
return {"observation": observation.model_dump(), "reward": 0.0, "done": False}
|
| 83 |
+
|
| 84 |
+
def step(self, action: Dict[str, Any]) -> Dict[str, Any]:
|
| 85 |
+
try:
|
| 86 |
+
from models import FixOSAction
|
| 87 |
+
except ImportError:
|
| 88 |
+
from my_env.models import FixOSAction
|
| 89 |
+
|
| 90 |
+
observation = self.env.step(FixOSAction(command=action.get("command", "status"), args=action.get("args", {})))
|
| 91 |
+
return {
|
| 92 |
+
"observation": observation.model_dump(),
|
| 93 |
+
"reward": observation.reward,
|
| 94 |
+
"done": observation.done,
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class FixOSAgent:
|
| 99 |
+
def __init__(self, env_url: str | None = None):
|
| 100 |
+
hf_token = _env_required("HF_TOKEN")
|
| 101 |
+
|
| 102 |
+
self.llm = OpenAI(api_key=hf_token, base_url=API_BASE_URL)
|
| 103 |
+
self.model_name = MODEL_NAME
|
| 104 |
+
self.env = self._create_env_client(env_url)
|
| 105 |
+
|
| 106 |
+
def _create_env_client(self, env_url: str | None):
|
| 107 |
+
if not env_url:
|
| 108 |
+
return LocalEnvClient()
|
| 109 |
+
try:
|
| 110 |
+
client = EnvHTTPClient(env_url)
|
| 111 |
+
client.reset()
|
| 112 |
+
return client
|
| 113 |
+
except Exception:
|
| 114 |
+
return LocalEnvClient()
|
| 115 |
+
|
| 116 |
+
def _emit(self, tag: str, payload: Dict[str, Any]) -> None:
|
| 117 |
+
print(f"[{tag}] {json.dumps(payload, separators=(',', ':'), sort_keys=False)}", flush=True)
|
| 118 |
+
|
| 119 |
+
def _llm_action(self, observation: Dict[str, Any], step: int, max_steps: int) -> Dict[str, Any]:
|
| 120 |
+
prompt = (
|
| 121 |
+
"You are solving a deterministic OS troubleshooting task.\n"
|
| 122 |
+
"Return JSON only with keys: command, args, reasoning.\n"
|
| 123 |
+
f"Step: {step}/{max_steps}\n"
|
| 124 |
+
f"Observation: {json.dumps(observation)}\n"
|
| 125 |
+
"Allowed commands: ps, top, df, status, logs, cat, edit, restart, kill, rm.\n"
|
| 126 |
+
"Prefer concrete remediation over repeated diagnostics."
|
| 127 |
)
|
| 128 |
|
| 129 |
+
try:
|
| 130 |
+
resp = self.llm.chat.completions.create(
|
| 131 |
+
model=self.model_name,
|
| 132 |
+
messages=[{"role": "user", "content": prompt}],
|
| 133 |
+
temperature=0.0,
|
| 134 |
+
max_tokens=250,
|
| 135 |
+
)
|
| 136 |
+
content = (resp.choices[0].message.content or "").strip()
|
| 137 |
+
start = content.find("{")
|
| 138 |
+
end = content.rfind("}")
|
| 139 |
+
if start >= 0 and end >= start:
|
| 140 |
+
candidate = json.loads(content[start : end + 1])
|
| 141 |
+
cmd = str(candidate.get("command", "status")).lower()
|
| 142 |
+
args = candidate.get("args", {})
|
| 143 |
+
if cmd in {"ps", "top", "df", "status", "logs", "cat", "edit", "restart", "kill", "rm"} and isinstance(args, dict):
|
| 144 |
+
return {"command": cmd, "args": args, "reasoning": str(candidate.get("reasoning", ""))}
|
| 145 |
+
except (APIError, APIConnectionError, RateLimitError, ValueError):
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
return self._heuristic_action(observation)
|
| 149 |
+
|
| 150 |
+
def _heuristic_action(self, observation: Dict[str, Any]) -> Dict[str, Any]:
|
| 151 |
+
services = {s.get("name", ""): s for s in observation.get("services", [])}
|
| 152 |
+
resources = observation.get("resources", {})
|
| 153 |
+
processes = observation.get("processes", [])
|
| 154 |
+
history = observation.get("history", [])
|
| 155 |
+
|
| 156 |
+
high_cpu = sorted(processes, key=lambda p: p.get("cpu_percent", 0), reverse=True)
|
| 157 |
+
|
| 158 |
+
for proc in high_cpu:
|
| 159 |
+
if int(proc.get("pid", -1)) == 922:
|
| 160 |
+
return {"command": "kill", "args": {"pid": 922}, "reasoning": "remove port blocker"}
|
| 161 |
+
|
| 162 |
+
if float(resources.get("cpu_percent", 0)) > 80:
|
| 163 |
+
for proc in high_cpu:
|
| 164 |
+
if int(proc.get("pid", -1)) in {920, 921}:
|
| 165 |
+
return {
|
| 166 |
+
"command": "kill",
|
| 167 |
+
"args": {"pid": int(proc.get("pid"))},
|
| 168 |
+
"reasoning": "reduce aggregate cpu pressure",
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
if float(resources.get("disk_percent", 0)) > 95:
|
| 172 |
+
for candidate in ["/var/log/archive.bin", "/var/log/system.log"]:
|
| 173 |
+
if any(f.get("path") == candidate for f in observation.get("filesystem", [])):
|
| 174 |
+
return {"command": "rm", "args": {"path": candidate}, "reasoning": "free disk"}
|
| 175 |
+
|
| 176 |
+
for proc in high_cpu:
|
| 177 |
+
if float(proc.get("cpu_percent", 0)) >= 50:
|
| 178 |
+
return {"command": "kill", "args": {"pid": int(proc.get("pid"))}, "reasoning": "kill high cpu process"}
|
| 179 |
+
|
| 180 |
+
nginx = services.get("nginx", {})
|
| 181 |
+
mysql = services.get("mysql", {})
|
| 182 |
+
|
| 183 |
+
if nginx and not nginx.get("config_valid", True):
|
| 184 |
+
return {"command": "edit", "args": {"path": "/etc/nginx/nginx.conf", "content": "valid nginx config"}, "reasoning": "fix nginx config"}
|
| 185 |
+
|
| 186 |
+
if mysql and not mysql.get("config_valid", True):
|
| 187 |
+
return {"command": "edit", "args": {"path": "/etc/mysql/my.cnf", "content": "valid mysql config"}, "reasoning": "fix mysql config"}
|
| 188 |
+
|
| 189 |
+
if mysql.get("status") != "running":
|
| 190 |
+
return {"command": "restart", "args": {"service": "mysql"}, "reasoning": "restart mysql"}
|
| 191 |
+
|
| 192 |
+
if nginx.get("status") != "running":
|
| 193 |
+
return {"command": "restart", "args": {"service": "nginx"}, "reasoning": "restart nginx"}
|
| 194 |
+
|
| 195 |
+
recent = " ".join(str(item) for item in history[-3:]).lower()
|
| 196 |
+
if "logs" not in recent and "cat" not in recent:
|
| 197 |
+
return {"command": "logs", "args": {}, "reasoning": "check logs"}
|
| 198 |
+
|
| 199 |
+
return {"command": "status", "args": {}, "reasoning": "verify system"}
|
| 200 |
+
|
| 201 |
+
def run_episode(
|
| 202 |
+
self,
|
| 203 |
+
episode_index: int,
|
| 204 |
+
max_steps: int = 50,
|
| 205 |
+
retried_local: bool = False,
|
| 206 |
+
emit_start: bool = True,
|
| 207 |
+
) -> Dict[str, Any]:
|
| 208 |
+
try:
|
| 209 |
+
reset_payload = self.env.reset()
|
| 210 |
+
except Exception:
|
| 211 |
+
if not retried_local:
|
| 212 |
+
self.env = LocalEnvClient()
|
| 213 |
+
return self.run_episode(
|
| 214 |
+
episode_index,
|
| 215 |
+
max_steps=max_steps,
|
| 216 |
+
retried_local=True,
|
| 217 |
+
emit_start=emit_start,
|
| 218 |
+
)
|
| 219 |
+
raise
|
| 220 |
+
|
| 221 |
+
obs = reset_payload.get("observation", {})
|
| 222 |
+
task_id = str(obs.get("task_id", "unknown"))
|
| 223 |
+
episode_id = f"ep-{episode_index:03d}"
|
| 224 |
+
|
| 225 |
+
if emit_start:
|
| 226 |
+
self._emit(
|
| 227 |
+
"START",
|
| 228 |
+
{
|
| 229 |
+
"episode_id": episode_id,
|
| 230 |
+
"task_id": task_id,
|
| 231 |
+
"max_steps": max_steps,
|
| 232 |
+
"timestamp": int(time.time()),
|
| 233 |
+
},
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
total_reward = 0.0
|
| 237 |
+
success = False
|
| 238 |
+
|
| 239 |
+
for step in range(1, max_steps + 1):
|
| 240 |
+
action_obj = self._llm_action(obs, step, max_steps)
|
| 241 |
+
try:
|
| 242 |
+
step_payload = self.env.step({"command": action_obj["command"], "args": action_obj["args"]})
|
| 243 |
+
except Exception:
|
| 244 |
+
if not retried_local:
|
| 245 |
+
self.env = LocalEnvClient()
|
| 246 |
+
return self.run_episode(
|
| 247 |
+
episode_index,
|
| 248 |
+
max_steps=max_steps,
|
| 249 |
+
retried_local=True,
|
| 250 |
+
emit_start=False,
|
| 251 |
+
)
|
| 252 |
+
raise
|
| 253 |
+
obs = step_payload.get("observation", {})
|
| 254 |
+
|
| 255 |
+
reward = float(step_payload.get("reward", obs.get("reward", 0.0) or 0.0))
|
| 256 |
+
done = bool(step_payload.get("done", obs.get("done", False)))
|
| 257 |
+
total_reward += reward
|
| 258 |
+
success = bool(obs.get("is_success_step", False)) or success
|
| 259 |
+
|
| 260 |
+
self._emit(
|
| 261 |
+
"STEP",
|
| 262 |
+
{
|
| 263 |
+
"episode_id": episode_id,
|
| 264 |
+
"task_id": task_id,
|
| 265 |
+
"step": step,
|
| 266 |
+
"command": action_obj["command"],
|
| 267 |
+
"args": action_obj["args"],
|
| 268 |
+
"reward": round(reward, 6),
|
| 269 |
+
"task_score": round(float(obs.get("task_score", 0.0) or 0.0), 6),
|
| 270 |
+
"done": done,
|
| 271 |
+
},
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
if done:
|
| 275 |
+
break
|
| 276 |
+
|
| 277 |
+
final_score = max(0.0, min(1.0, float(obs.get("task_score", 0.0) or 0.0)))
|
| 278 |
+
self._emit(
|
| 279 |
+
"END",
|
| 280 |
+
{
|
| 281 |
+
"episode_id": episode_id,
|
| 282 |
+
"task_id": task_id,
|
| 283 |
+
"success": success,
|
| 284 |
+
"steps_taken": len(obs.get("history", [])),
|
| 285 |
+
"total_reward": round(total_reward, 6),
|
| 286 |
+
"final_score": round(final_score, 6),
|
| 287 |
+
"timestamp": int(time.time()),
|
| 288 |
+
},
|
| 289 |
)
|
| 290 |
|
| 291 |
+
return {"task_id": task_id, "success": success, "score": final_score}
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def main() -> None:
|
| 295 |
+
agent = FixOSAgent(env_url=os.getenv("ENV_BASE_URL"))
|
| 296 |
+
|
| 297 |
+
runs = 7
|
| 298 |
+
scores: Dict[str, list[float]] = {}
|
| 299 |
+
|
| 300 |
+
for i in range(runs):
|
| 301 |
+
result = agent.run_episode(i + 1, max_steps=50)
|
| 302 |
+
scores.setdefault(result["task_id"], []).append(float(result["score"]))
|
| 303 |
+
|
| 304 |
+
summary = {k: round(sum(v) / len(v), 6) for k, v in sorted(scores.items())}
|
| 305 |
+
print(json.dumps({"summary": summary}, separators=(",", ":")), file=sys.stderr)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
if __name__ == "__main__":
|
| 309 |
+
main()
|