Veer15/openenv-distributed-systems-debugging
0
1 2 3from contextlib import asynccontextmanager4 5from fastapi import FastAPI, HTTPException6 7from .constants import TaskName8from .env import DistributedDebugEnv9from .models import Action, Observation, StepResult10 11 12@asynccontextmanager13async def lifespan(app: FastAPI):14 env = DistributedDebugEnv()15 env.start()16 app.state.env = env17 try:18 yield19 finally:20 env.close()21 22 23app = FastAPI(title="Distributed Systems Debug Environment", version="1.0.0", lifespan=lifespan)24 25 26@app.post("/reset", response_model=Observation)27async def reset(task_name: str | None = None) -> Observation:28 # Validator and sample inference call /reset without task input.29 # Use a deterministic default task for reproducible episode bootstrapping.30 selected_task_name = task_name or TaskName.CASCADING_TIMEOUT.value31 try:32 task = TaskName.parse(selected_task_name)33 except ValueError as exc:34 raise HTTPException(status_code=400, detail=str(exc)) from exc35 36 try:37 env: DistributedDebugEnv = app.state.env38 return env.reset(task_name=task)39 except Exception as exc:40 raise HTTPException(status_code=500, detail=str(exc)) from exc41 42 43@app.post("/step", response_model=StepResult)44async def step(action: Action) -> StepResult:45 try:46 env: DistributedDebugEnv = app.state.env47 return env.step(action)48 except Exception as exc:49 raise HTTPException(status_code=500, detail=str(exc)) from exc50 51 52@app.get("/state")53async def state() -> dict:54 try:55 env: DistributedDebugEnv = app.state.env56 return env.state()57 except Exception as exc:58 raise HTTPException(status_code=500, detail=str(exc)) from exc59 60 61@app.get("/health")62async def health() -> dict:63 return {"status": "ok", "version": "1.0.0"}64 