Spaces:
Sleeping
Sleeping
File size: 7,139 Bytes
9b47159 0b67021 9b47159 | 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 | """
FastAPI server exposing the Bug Triage OpenEnv endpoints.
Standard OpenEnv endpoints: /health /reset /step /state
Hackathon-required endpoints: /tasks /grader /baseline
"""
from __future__ import annotations
import json
import logging
import os
import subprocess
from typing import Any, Dict
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from ..models import (
DEVELOPERS,
BaselineResponse,
BugTriageObservation,
BugTriageState,
GraderRequest,
GraderResponse,
ResetRequest,
StepRequest,
)
from .environment import BugTriageEnvironment
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# -- Application setup ---------------------------------------------------------
app = FastAPI(
title="Bug Triage OpenEnv",
description="Bug Triage RL Environment following the OpenEnv spec.",
version="0.1.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
env = BugTriageEnvironment()
# -- Standard OpenEnv endpoints ------------------------------------------------
@app.get("/")
def root() -> Dict[str, Any]:
"""Root endpoint showing environment info."""
return {
"name": "Bug Triage OpenEnv",
"version": "0.1.0",
"status": "running",
"endpoints": ["/health", "/reset", "/step", "/state", "/tasks", "/grader", "/baseline", "/docs"],
"tasks": ["task_1 (classify)", "task_2 (prioritize)", "task_3 (full triage)"],
}
@app.get("/health")
def health() -> Dict[str, str]:
"""Liveness check. Returns 200 with status=healthy."""
return {"status": "healthy"}
@app.post("/reset", response_model=BugTriageObservation)
def reset(body: ResetRequest = ResetRequest()) -> BugTriageObservation:
"""Start a new episode. Returns an initial observation with a bug report."""
try:
return env.reset(task_id=body.task_id)
except Exception as exc:
logger.error("reset() error: %s", exc)
raise HTTPException(status_code=500, detail=str(exc))
@app.post("/step", response_model=BugTriageObservation)
def step(body: StepRequest) -> BugTriageObservation:
"""Execute one triage action. Requires episode_id from /reset."""
try:
return env.step(action=body.action, episode_id=body.episode_id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
except Exception as exc:
logger.error("step() error: %s", exc)
raise HTTPException(status_code=500, detail=str(exc))
@app.get("/state", response_model=BugTriageState)
def state() -> BugTriageState:
"""Return current episode metadata."""
return env.state
# -- Hackathon-required endpoints ----------------------------------------------
TASKS_REGISTRY = [
{
"task_id": "task_1",
"name": "Bug Type Classification",
"description": (
"Given a bug report (title, description, logs, environment "
"metadata), classify the bug into one of: crash, ui, "
"performance, security, data_loss, compatibility."
),
"difficulty": "easy",
"action_schema": {
"task_id": "task_1",
"bug_type": (
"one of: crash | ui | performance | security "
"| data_loss | compatibility"
),
"confidence": "optional float in [0.0, 1.0]",
"reasoning": "optional string",
},
},
{
"task_id": "task_2",
"name": "Priority Assignment",
"description": (
"Given a bug report, assign the correct priority level: "
"low, medium, high, or critical. "
"Partial credit awarded for near-miss levels."
),
"difficulty": "medium",
"action_schema": {
"task_id": "task_2",
"priority": "one of: low | medium | high | critical",
"confidence": "optional float in [0.0, 1.0]",
"reasoning": "optional string",
},
},
{
"task_id": "task_3",
"name": "Full Bug Triage",
"description": (
"Complete triage: classify bug type, assign priority, "
"route to the correct developer, and suggest the "
"appropriate action. "
f"Available developers: {', '.join(DEVELOPERS)}."
),
"difficulty": "hard",
"action_schema": {
"task_id": "task_3",
"bug_type": (
"one of: crash | ui | performance | security "
"| data_loss | compatibility"
),
"priority": "one of: low | medium | high | critical",
"assigned_developer": f"one of: {' | '.join(DEVELOPERS)}",
"suggested_action": (
"one of: fix_immediately | schedule_sprint "
"| needs_more_info | wontfix | duplicate"
),
"confidence": "optional float in [0.0, 1.0]",
"reasoning": "optional string",
},
},
]
@app.get("/tasks")
def get_tasks() -> Dict[str, Any]:
"""Return available tasks and action schemas."""
return {"tasks": TASKS_REGISTRY}
@app.post("/grader", response_model=GraderResponse)
def grader(body: GraderRequest) -> GraderResponse:
"""Return grader score [0.0, 1.0] for a completed episode."""
result = env.grade_episode(
episode_id=body.episode_id, task_id=body.task_id
)
if "error" in result:
raise HTTPException(status_code=404, detail=result["error"])
score = result["score"]
return GraderResponse(
task_id=body.task_id,
episode_id=body.episode_id,
score=score,
breakdown=result.get("breakdown", {}),
passed=score >= 0.5,
)
@app.post("/baseline", response_model=BaselineResponse)
def baseline() -> BaselineResponse:
"""Run baseline inference script against all tasks. Returns scores."""
try:
result = subprocess.run(
[
"python", "-m", "bug_triage_env.baseline",
"--all-tasks", "--json", "--episodes", "3",
],
capture_output=True,
text=True,
timeout=300,
env={**os.environ},
)
if result.returncode != 0:
logger.error("baseline script error: %s", result.stderr)
raise HTTPException(
status_code=500,
detail=f"Baseline script failed: {result.stderr[:300]}",
)
data = json.loads(result.stdout)
return BaselineResponse(**data)
except subprocess.TimeoutExpired:
raise HTTPException(
status_code=504, detail="Baseline script timed out (>300s)"
)
except json.JSONDecodeError as exc:
raise HTTPException(
status_code=500, detail=f"Baseline output parse error: {exc}"
)
except HTTPException:
raise
except Exception as exc:
logger.error("/baseline error: %s", exc)
raise HTTPException(status_code=500, detail=str(exc))
|