Spaces:
Sleeping
Sleeping
File size: 23,135 Bytes
ac8898f 6326bef ac8898f 6326bef ac8898f 6326bef ac8898f | 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | import asyncio
import json
import os
from typing import Optional, Dict, Any, List
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import uvicorn
from .environment import TrainingEnv
from .scenarios import list_scenarios, detail_scenario, SCENARIO_CATALOG
from .model_registry import get_registry
app = FastAPI(
title="Linux SRE Environment API",
description=(
"OpenEnv-compliant API for the Linux SRE training environment. "
"Supports legacy difficulty-based tasks and composable scenarios "
"with cascading fault injection."
),
version="2.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
backends: Dict[str, TrainingEnv] = {}
counter = 0
# ======================================================================
# REQUEST / RESPONSE MODELS
# ======================================================================
class ResetPayload(BaseModel):
scenario: str = Field(
default="log_analysis", description="Scenario key to load")
seed: Optional[int] = Field(
default=None, description="Random seed for reproducibility")
class StepPayload(BaseModel):
action: str = Field(description="Shell command to execute")
class ResetOut(BaseModel):
env_id: str
observation: Dict[str, Any]
info: Dict[str, Any]
class StepOut(BaseModel):
observation: Dict[str, Any]
reward: float
done: bool
info: Dict[str, Any]
# ======================================================================
# HEALTH + TASKS
# ======================================================================
@app.get("/health")
async def health_check():
return {"status": "ok", "service": "linux-sre-env", "version": "2.0.0"}
@app.get("/api/v1/tasks")
async def list_tasks():
return {
"tasks": TrainingEnv.avail_tasks(),
"details": {
key: TrainingEnv.task_details(key)
for key in TrainingEnv.avail_tasks()
}
}
@app.get("/api/v1/tasks/{key}")
async def get_task(key: str):
info = TrainingEnv.task_details(key)
if not info:
raise HTTPException(status_code=404, detail=f"Task '{key}' not found")
return info
# ======================================================================
# SCENARIOS
# ======================================================================
@app.get("/api/v1/scenarios")
async def get_scenarios():
"""List all available scenarios with metadata."""
return {"scenarios": list_scenarios()}
@app.get("/api/v1/scenarios/{key}")
async def get_scenario(key: str):
"""Get detailed info for a single scenario."""
if key not in SCENARIO_CATALOG:
raise HTTPException(
status_code=404, detail=f"Scenario '{key}' not found")
return detail_scenario(key)
# ======================================================================
# AI MODELS
# ======================================================================
@app.get("/api/v1/models")
async def list_models():
"""List all available AI models with metadata."""
registry = get_registry()
return {"models": registry.list_models()}
@app.get("/api/v1/models/{name}")
async def get_model(name: str):
"""Get info for a specific AI model."""
registry = get_registry()
info = registry.get_model_info(name)
if not info:
raise HTTPException(status_code=404, detail=f"Model '{name}' not found")
return info
# ======================================================================
# ENVIRONMENT LIFECYCLE
# ======================================================================
@app.post("/reset")
@app.post("/api/v1/env/reset")
async def reset(req: Optional[ResetPayload] = None):
global counter
try:
scenario = req.scenario if req else "log_analysis"
env = TrainingEnv(scenario=scenario)
eid = f"env_{counter}"
counter += 1
backends[eid] = env
res = env.reset()
return ResetOut(env_id=eid, observation=res["observation"], info=res["info"])
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/step/{env_id}")
@app.post("/api/v1/env/{env_id}/step")
async def step(env_id: str, req: StepPayload):
if env_id not in backends:
raise HTTPException(
status_code=404, detail=f"Environment '{env_id}' not found")
env = backends[env_id]
res = env.step(req.action)
return StepOut(
observation=res["observation"],
reward=res["reward"],
done=res["done"],
info=res["info"],
)
@app.get("/state/{env_id}")
@app.get("/api/v1/env/{env_id}/state")
async def get_state(env_id: str):
if env_id not in backends:
raise HTTPException(
status_code=404, detail=f"Environment '{env_id}' not found")
return backends[env_id].dump()
@app.delete("/api/v1/env/{env_id}")
async def delete_env(env_id: str):
if env_id not in backends:
raise HTTPException(
status_code=404, detail=f"Environment '{env_id}' not found")
del backends[env_id]
return {"status": "deleted", "env_id": env_id}
@app.get("/api/v1/env")
async def list_envs():
return {
"count": len(backends),
"environments": {
eid: {
"task": env.task.nm,
"difficulty": env.difficulty,
"score": env.score,
"step": env.step_count,
"done": env.finished,
}
for eid, env in backends.items()
},
}
# ======================================================================
# WEBSOCKET — live terminal streaming
# ======================================================================
class ConnectionManager:
"""Manages WebSocket connections for live terminal streaming."""
def __init__(self):
self.active: Dict[str, List[WebSocket]] = {}
async def connect(self, env_id: str, ws: WebSocket):
await ws.accept()
if env_id not in self.active:
self.active[env_id] = []
self.active[env_id].append(ws)
def disconnect(self, env_id: str, ws: WebSocket):
if env_id in self.active:
self.active[env_id] = [w for w in self.active[env_id] if w != ws]
async def broadcast(self, env_id: str, data: dict):
if env_id in self.active:
for ws in self.active[env_id]:
try:
await ws.send_json(data)
except Exception:
pass
ws_manager = ConnectionManager()
async def wait_briefly() -> None:
"""Yield control without adding user-visible latency."""
await asyncio.sleep(0)
@app.websocket("/ws/env/{env_id}")
async def ws_terminal(ws: WebSocket, env_id: str):
"""
WebSocket endpoint for live terminal interaction.
Client sends: {"action": "step", "command": "ps aux"}
Server sends: {"type": "output", "command": "...", "output": "...", "score": 0.5, ...}
Also supports: {"action": "reset"}, {"action": "state"}
"""
if env_id not in backends:
await ws.close(code=4004, reason=f"Environment '{env_id}' not found")
return
await ws_manager.connect(env_id, ws)
try:
while True:
raw = await ws.receive_text()
try:
msg = json.loads(raw)
except json.JSONDecodeError:
await ws.send_json({"type": "error", "message": "Invalid JSON"})
continue
action = msg.get("action", "")
env = backends.get(env_id)
if not env:
await ws.send_json({"type": "error", "message": "Environment not found"})
break
if action == "step":
command = msg.get("command", "")
if not command:
await ws.send_json({"type": "error", "message": "Missing 'command'"})
continue
res = env.step(command)
payload = {
"type": "output",
"command": command,
"output": res["info"].get("command_output", ""),
"score": res["info"]["task_score"],
"reward": res["reward"],
"step": res["info"]["step"],
"max_steps": res["info"]["max_steps"],
"done": res["done"],
"exit_code": res["info"]["exit_code"],
}
await ws_manager.broadcast(env_id, payload)
elif action == "reset":
res = env.reset()
await ws.send_json({
"type": "reset",
"observation": res["observation"],
"info": res["info"],
})
elif action == "state":
await ws.send_json({
"type": "state",
"data": env.dump(),
})
else:
await ws.send_json({"type": "error", "message": f"Unknown action: {action}"})
except WebSocketDisconnect:
ws_manager.disconnect(env_id, ws)
class AgentRunPayload(BaseModel):
agent_type: str = Field(description="Agent type: llm or rl")
model_name: str = Field(default="ppo", description="Model name for RL agent (ppo, a2c, heuristic)")
async def run_agent_in_background(env_id: str, agent_type: str, model_name: str = "ppo"):
"""Background task to run an autonomous agent and stream to websockets."""
env = backends.get(env_id)
if not env:
return
try:
if agent_type == "llm":
from .agent import LLMAgent
# Boot LLM agent with environment-based OpenAI config.
await ws_manager.broadcast(env_id, {"type": "system", "text": "Booting Autonomous LLM via OpenAI client..."})
await wait_briefly()
scenario_key = getattr(env, "_scenario_key", "log_analysis")
try:
agent = LLMAgent(verbose=False)
result = agent.solve(scenario=scenario_key)
except Exception as e:
await ws_manager.broadcast(env_id, {"type": "error", "message": f"LLM Error: {str(e)}"})
return
turns = result.get("turns", [])
if not turns:
await ws_manager.broadcast(env_id, {"type": "system", "text": "LLM returned no executable commands."})
return
for turn in turns:
command = str(turn.get("command", "")).strip()
if not command:
continue
await ws_manager.broadcast(env_id, {"type": "input", "text": command})
await wait_briefly()
res = env.step(command)
payload = {
"type": "output",
"command": command,
"output": res["info"].get("command_output", ""),
"score": res["info"]["task_score"],
"reward": res["reward"],
"step": res["info"]["step"],
"max_steps": res["info"]["max_steps"],
"done": res["done"],
"exit_code": res["info"]["exit_code"],
}
await ws_manager.broadcast(env_id, payload)
if res["done"]:
break
elif agent_type == "rl":
from src.rl_env import ChaosLabEnv
registry = get_registry()
display_name = registry.get_display_name(model_name)
await ws_manager.broadcast(env_id, {"type": "system", "text": f"Booting AI Agent: [{display_name}]..."})
await wait_briefly()
# Hijack the actively running sandbox environment
rl_env = ChaosLabEnv()
rl_env.hijack_env(env)
catalog = rl_env.get_action_catalog()
# Load the selected model from registry
model, model_info = registry.load_model(model_name)
actual_name = model_info.display_name
await ws_manager.broadcast(env_id, {
"type": "system",
"text": f"[Model Loaded: {actual_name}] Algorithm: {model_info.algorithm}"
})
# Reset heuristic state if applicable
if hasattr(model, 'reset'):
model.reset()
done = False
while not done and env.step_count < env.limit:
await wait_briefly()
# Get observation and predict
obs = rl_env.get_current_obs()
# Stream agent reasoning for heuristic
if hasattr(model, 'get_reasoning'):
reasoning = model.get_reasoning(obs)
await ws_manager.broadcast(env_id, {
"type": "system",
"text": f"[{actual_name}] {reasoning}"
})
await wait_briefly()
action_arr, _ = model.predict(obs, deterministic=True)
action = int(action_arr)
command = catalog[action]
# Broadcast the input
await ws_manager.broadcast(env_id, {"type": "input", "text": command})
# Step the actual TrainingEnv
res = env.step(command)
payload = {
"type": "output",
"command": command,
"output": res["info"].get("command_output", ""),
"score": res["info"]["task_score"],
"reward": res["reward"],
"step": res["info"]["step"],
"max_steps": res["info"]["max_steps"],
"done": res["done"],
"exit_code": res["info"]["exit_code"],
"model_name": model_name,
}
await ws_manager.broadcast(env_id, payload)
done = res["done"]
except Exception as e:
await ws_manager.broadcast(env_id, {"type": "error", "message": f"Agent crashed: {str(e)}"})
@app.post("/api/v1/env/{env_id}/agent_run")
async def start_agent_run(env_id: str, req: AgentRunPayload, background_tasks: BackgroundTasks):
if env_id not in backends:
raise HTTPException(status_code=404, detail="Environment not found")
background_tasks.add_task(run_agent_in_background, env_id, req.agent_type, req.model_name)
return {"status": "started", "agent_type": req.agent_type, "model_name": req.model_name}
# ======================================================================
# AGENT ARENA — compare models on same scenario
# ======================================================================
class ChatQueryPayload(BaseModel):
query: str = Field(description="User question or request for LLM assistance")
class ChatResponsePayload(BaseModel):
response: str
suggested_command: Optional[str] = None
reasoning: str
class ArenaPayload(BaseModel):
scenario: str = Field(description="Scenario key to run")
commands_a: List[str] = Field(description="Commands for Agent A")
commands_b: List[str] = Field(description="Commands for Agent B")
label_a: str = Field(default="Agent A")
label_b: str = Field(default="Agent B")
type_a: str = Field(default="script", description="Type of agent: script, llm, rl")
type_b: str = Field(default="script", description="Type of agent: script, llm, rl")
model_a: str = Field(default="ppo", description="Model name for Agent A (if type is rl)")
model_b: str = Field(default="heuristic", description="Model name for Agent B (if type is rl)")
@app.post("/api/v1/arena/run")
async def arena_run(req: ArenaPayload):
"""Run two command sequences or autonomous agents on the same scenario and compare scores."""
results = {}
agent_configs = [
(req.label_a, req.type_a, req.commands_a, req.model_a),
(req.label_b, req.type_b, req.commands_b, req.model_b),
]
for label, agent_type, commands, model_name_for_agent in agent_configs:
history = []
final_score = 0.0
steps_used = 0
completed = False
if agent_type == "llm":
# Run Autonomous LLM (fallback to error if no API key/fails)
try:
from .agent import LLMAgent
import os
agent = LLMAgent(verbose=False)
res = agent.solve(scenario=req.scenario)
for i, turn in enumerate(res.get("turns", [])):
history.append({
"command": turn["command"],
"score": turn["score"],
"exit_code": 0, # Simplify
})
final_score = res["final_score"]
steps_used = res["steps_used"]
completed = res["completed"]
except Exception as e:
history.append({
"command": f"LLM Initialization Failed: {str(e)}",
"score": 0.0,
"exit_code": 1
})
elif agent_type == "rl":
# Run RL Agent using the model registry
try:
import gymnasium as gym
import src.rl_env
# Determine which model to load for this agent slot
agent_model_name = model_name_for_agent
registry = get_registry()
model, model_info = registry.load_model(agent_model_name)
env = gym.make("ChaosLab-v0", scenario=req.scenario)
obs, _ = env.reset()
# Reset heuristic state if applicable
if hasattr(model, 'reset'):
model.reset()
done = False
while not done:
action_arr, _ = model.predict(obs, deterministic=True)
action = int(action_arr)
obs, reward, terminated, truncated, info = env.step(action)
done = terminated or truncated
history.append({
"command": info.get("command", f"Action {action}"),
"score": info.get("task_score", 0.0),
"exit_code": info.get("exit_code", 0),
"model": agent_model_name,
})
summary = env.unwrapped.get_episode_summary()
final_score = summary["final_score"]
steps_used = summary["steps_used"]
completed = summary["completed"]
env.close()
except Exception as e:
history.append({
"command": f"RL Initialization Failed ({agent_model_name}): {str(e)}",
"score": 0.0,
"exit_code": 1
})
else:
# Standard Script looping
env = TrainingEnv(scenario=req.scenario)
env.reset()
for cmd in commands:
res = env.step(cmd)
history.append({
"command": cmd,
"score": res["info"]["task_score"],
"exit_code": res["info"]["exit_code"],
})
if res["done"]:
break
final_score = env.score
steps_used = env.step_count
completed = env.finished
results[label] = {
"final_score": final_score,
"steps_used": steps_used,
"completed": completed,
"history": history,
}
# determine winner
scores = {k: v["final_score"] for k, v in results.items()}
winner = max(scores, key=scores.get)
if len(set(scores.values())) == 1:
winner = "tie"
return {
"scenario": req.scenario,
"results": results,
"winner": winner,
}
# ======================================================================
# LLM CHAT ASSISTANT
# ======================================================================
@app.post("/api/v1/chat/{env_id}")
async def chat_assistant(env_id: str, req: ChatQueryPayload) -> ChatResponsePayload:
"""
Interactive chat endpoint for getting LLM suggestions based on environment context.
"""
if env_id not in backends:
raise HTTPException(status_code=404, detail="Environment not found")
try:
from .agent import LLMAgent
env = backends[env_id]
user_query = req.query.strip()
# Build context safely
context = "You are a Linux SRE assistant helping solve system tasks.\n"
try:
context += f"Current Task: {env.task.nm}\n"
context += f"Task Desc: {env.task.desc}\n"
except:
context += "Task: Unknown\n"
try:
context += f"Score: {env.score} | Steps: {env.step_count}/{env.limit}\n"
except:
pass
# Initialize LLM agent
llm_agent = LLMAgent(verbose=False)
client = llm_agent._ensure_client()
# Build messages
messages = [
{
"role": "system",
"content": (
context +
"\nYou are helpful and concise. Suggest specific shell commands for the current task. "
"Respond in 1-2 sentences."
),
},
{
"role": "user",
"content": user_query,
},
]
# Get LLM response
response = client.chat.completions.create(
model=llm_agent.model,
messages=messages,
temperature=0.2,
max_tokens=256,
)
response_text = (response.choices[0].message.content or "").strip()
# Extract command if applicable
suggested_command = None
query_lower = user_query.lower()
if any(kw in query_lower for kw in ["command", "run", "next", "what", "how", "try"]):
suggested_command = LLMAgent.extract_command(response_text)
return ChatResponsePayload(
response=response_text,
suggested_command=suggested_command,
reasoning="LLM Assistant"
)
except RuntimeError as e:
if "Missing" in str(e):
detail = "LLM not configured: Set API_BASE_URL, MODEL_NAME, HF_TOKEN"
else:
detail = f"LLM Error: {str(e)}"
raise HTTPException(status_code=503, detail=detail)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Chat error: {str(e)}")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|