Spaces:
Sleeping
Sleeping
File size: 10,700 Bytes
3ef7678 5385772 3ef7678 5385772 3ef7678 5385772 2950a72 5385772 3ef7678 5385772 3ef7678 5385772 3ef7678 55cc9be 3ef7678 55cc9be 8178b78 55cc9be 8178b78 55cc9be 8178b78 55cc9be 8178b78 55cc9be b621598 55cc9be b621598 2950a72 8178b78 55cc9be 3ef7678 5385772 55cc9be 5385772 c086d35 5385772 3ef7678 e6fd5cb 5385772 | 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 | """
FastAPI server for Repair Strategy System V3.
Serves the premium web dashboard + JSON API.
"""
from __future__ import annotations
import sys
import os
import uuid
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from typing import Any, Dict, List, Optional
from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from env_v2 import RepairEnvV2
from models_v2 import ActionType, ComponentTarget, Diagnosis
from tasks_v2 import TASKS
from graders_v2 import GRADERS
import baseline_v2
app = FastAPI(
title="RootCauseArena — Repair Strategy System V3",
description="Interactive web dashboard + API for multi-component system repair with hidden state and root-cause inference.",
version="3.0.0",
)
# Serve static assets (CSS, JS)
_static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
if os.path.isdir(_static_dir):
app.mount("/static", StaticFiles(directory=_static_dir), name="static")
# In-memory session store: session_id -> {env, total_reward}
_sessions: Dict[str, Dict] = {}
# ---------------------------------------------------------------------------
# Request/Response schemas
# ---------------------------------------------------------------------------
class GraderRequestV2(BaseModel):
task_id: str
final_state: Dict[str, Dict[str, Any]]
steps_taken: int
correct_root_fix_ratio: float
class GraderResponseV2(BaseModel):
task_id: str
score: float
steps_taken: int
class BaselineResultV2(BaseModel):
task_id: str
steps_taken: int
total_reward: float
score: float
success: bool
final_health_ratio: float
efficiency: float
correct_root_fix_ratio: float
class BaselineResponseV2(BaseModel):
results: List[BaselineResultV2]
all_scores_in_range: bool
class DiagnoseRequest(BaseModel):
task_id: str
diagnosis: Diagnosis
class DiagnoseResponse(BaseModel):
correct: bool
actual_root_cause: str
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _obs_to_dict(obs) -> Dict:
"""Convert SymptomObservation to a JSON-serialisable dict."""
return {
"metrics": {
comp: {
"latency": m.latency,
"error_rate": m.error_rate,
"queue_load": m.queue_load,
"cpu_usage": m.cpu_usage,
}
for comp, m in obs.metrics.items()
},
"logs": [
{"timestamp": l.timestamp, "message": l.message, "severity": l.severity}
for l in obs.logs
],
"step_count": obs.step_count,
"max_steps": obs.max_steps,
"inspections_remaining":obs.inspections_remaining,
}
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
# Global env for OpenEnv runner — default task: easy
_global_env = RepairEnvV2(seed=42)
_default_task_id = "easy"
_global_env.reset(task_config=TASKS[_default_task_id], task_id=_default_task_id)
@app.post("/reset")
async def reset(request: Request = None):
"""Standard OpenEnv reset endpoint. Accepts optional task_id in JSON body."""
task_id = _default_task_id
if request is not None:
try:
body = await request.json()
task_id = body.get("task_id", _default_task_id)
except Exception:
pass
if task_id not in TASKS:
task_id = _default_task_id
obs = _global_env.reset(task_config=TASKS[task_id], task_id=task_id)
return {
"observation": _obs_to_dict(obs),
"info": {"task_id": task_id}
}
@app.get("/state")
def state():
"""Standard OpenEnv state endpoint."""
try:
obs = _global_env._generate_symptoms()
return {"observation": _obs_to_dict(obs), "info": {}}
except Exception:
return {"observation": {}, "info": {}}
@app.post("/step")
async def step_standard(request: Request):
"""Standard OpenEnv step endpoint."""
data = await request.json()
from models_v2 import Action, ActionType, ComponentTarget
action_type = data.get("action_type")
target = data.get("target")
act = Action(action_type=ActionType(action_type), target=ComponentTarget(target))
obs, reward, done, info = _global_env.step(act)
return {
"observation": _obs_to_dict(obs),
"reward": float(reward.value) if hasattr(reward, "value") else float(reward),
"done": done,
"info": info
}
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "repair-strategy-system-v2"}
@app.get("/tasks")
async def get_tasks():
"""Return all task definitions along with the action schema."""
task_list = []
for task_id, config in TASKS.items():
task_list.append({
"task_id": task_id,
"description": config["description"],
"difficulty": config.get("difficulty", task_id),
"max_steps": config["max_steps"],
"optimal_steps": config["optimal_steps"],
# In V2, initial state is HIDDEN. Only returning structural info.
})
action_schema = {
"action_type": {
"type": "enum",
"values": [e.value for e in ActionType],
},
"target": {
"type": "enum",
"values": [e.value for e in ComponentTarget],
},
}
return {
"tasks": task_list,
"action_schema": action_schema,
"observation_fields": ["metrics", "logs", "step_count", "max_steps", "inspections_remaining"],
"seed": 42,
}
@app.post("/grader", response_model=GraderResponseV2)
async def grade_episode(request: GraderRequestV2):
"""
Grade a completed V2 episode.
"""
if request.task_id not in GRADERS:
raise HTTPException(
status_code=400,
detail=f"Unknown task_id '{request.task_id}'",
)
grader_fn = GRADERS[request.task_id]
# Needs optimal steps from task config
optimal_steps = TASKS.get(request.task_id, {}).get("optimal_steps", 5)
score = grader_fn(
final_state=request.final_state,
steps_taken=request.steps_taken,
task_optimal_steps=optimal_steps,
correct_root_fix_ratio=request.correct_root_fix_ratio
)
return GraderResponseV2(
task_id=request.task_id,
score=score,
steps_taken=request.steps_taken,
)
@app.post("/diagnose", response_model=DiagnoseResponse)
async def submit_diagnosis(request: DiagnoseRequest):
"""
Allow an agent to submit a root cause diagnosis.
Reveals whether they were right or wrong.
"""
if request.task_id not in TASKS:
raise HTTPException(status_code=400, detail="Unknown task_id")
actual = TASKS[request.task_id]["root_cause"]
correct = (request.diagnosis.root_cause == actual)
return DiagnoseResponse(
correct=correct,
actual_root_cause=actual if correct else "HIDDEN (Incorrect Diagnosis)"
)
@app.get("/baseline", response_model=BaselineResponseV2)
async def run_baseline():
"""Run the deterministic inspect-then-fix baseline agent on all tasks."""
results = []
for task_id, config in TASKS.items():
raw = baseline_v2.run_task(task_id, config)
results.append(BaselineResultV2(**raw))
scores = [r.score for r in results]
all_in_range = all(0.0 <= s <= 1.0 for s in scores)
return BaselineResponseV2(
results=results,
all_scores_in_range=all_in_range,
)
# ---------------------------------------------------------------------------
# Dashboard root
# ---------------------------------------------------------------------------
@app.get("/")
async def serve_dashboard():
"""Serve the interactive web dashboard."""
index_path = os.path.join(_static_dir, "index.html")
if os.path.isfile(index_path):
return FileResponse(index_path, media_type="text/html")
return {"message": "RootCauseArena V3 API — static dashboard not found. Check the /static folder."}
# ---------------------------------------------------------------------------
# Interactive Session Endpoints
# ---------------------------------------------------------------------------
class SessionStartRequest(BaseModel):
task_id: str
class SessionStepRequest(BaseModel):
session_id: str
action_type: str
target: str
# _obs_to_dict moved to top of Endpoints section above
@app.post("/session/start")
async def session_start(req: SessionStartRequest):
"""
Start a new interactive episode. Returns a session_id and the initial observation.
"""
if req.task_id not in TASKS:
raise HTTPException(status_code=400, detail=f"Unknown task_id '{req.task_id}'")
config = TASKS[req.task_id]
env = RepairEnvV2(seed=42)
obs = env.reset(task_config=config, task_id=req.task_id)
sid = str(uuid.uuid4())
_sessions[sid] = {"env": env, "total_reward": 0.0, "task_id": req.task_id}
return {
"session_id": sid,
"task_id": req.task_id,
"observation": _obs_to_dict(obs),
}
@app.post("/session/step")
async def session_step(req: SessionStepRequest):
"""
Advance the session by one step. Returns new observation, reward, done, and success.
"""
if req.session_id not in _sessions:
raise HTTPException(status_code=404, detail="Session not found. Start a new session first.")
session = _sessions[req.session_id]
env: RepairEnvV2 = session["env"]
# Validate action & target
try:
action_type = ActionType(req.action_type)
target = ComponentTarget(req.target)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
from models_v2 import Action
action = Action(action_type=action_type, target=target)
obs, reward, done, info = env.step(action)
session["total_reward"] += reward
result = {
"observation": _obs_to_dict(obs),
"reward": reward,
"done": done,
"success": info.get("success", False),
"info": str(info.get("reason", "")),
}
if done:
# Clean up session
del _sessions[req.session_id]
return result
if __name__ == "__main__":
import uvicorn
uvicorn.run("server_v2:app", host="0.0.0.0", port=7860, reload=False)
|