File size: 8,653 Bytes
75ca235
54e0639
75ca235
 
 
 
339abf5
75ca235
 
 
 
 
 
 
 
 
 
 
 
339abf5
 
75ca235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9cf02d
 
 
 
 
 
 
75ca235
 
 
 
 
 
f9cf02d
 
 
75ca235
f9cf02d
75ca235
 
339abf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75ca235
339abf5
 
 
 
 
 
 
75ca235
 
339abf5
 
 
 
 
 
 
 
75ca235
 
339abf5
 
 
75ca235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339abf5
75ca235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54e0639
 
 
75ca235
 
 
 
 
 
 
 
 
 
 
54e0639
 
 
 
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
"""FastAPI app wrapper for running Bug Triage OpenEnv in containerized environments."""

from __future__ import annotations

import os
import threading
from functools import lru_cache
from pathlib import Path
from typing import Optional

import uvicorn
from fastapi import Body, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, Response
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

from models import ActionModel
from server.environment import BugTriageEnv
from server.graders import BugTriageGrader
from server.policy import recommend_action
from server.tasks import list_tasks


app = FastAPI(
    title="Bug Triage OpenEnv",
    version="0.1.0",
    description="Real-world OpenEnv environment for bug triage training",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# ---------------------------------------------------------------------------
# Global singleton environment
# NOTE: This server is designed for single-user / validator use.  The
# environment is shared across all HTTP requests and protected by ENV_LOCK.
# Running multiple concurrent sessions will interleave state; deploy separate
# server instances if multi-tenancy is required.
# ---------------------------------------------------------------------------
env = BugTriageEnv()
ENV_LOCK = threading.RLock()
STATIC_DIR = Path(__file__).resolve().parents[1] / "static"
if STATIC_DIR.exists():
    app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")


def _as_bool(value: str | None) -> bool:
    if value is None:
        return False
    return value.strip().lower() in {"1", "true", "yes", "on"}


@lru_cache(maxsize=1)
def _offline_baseline_snapshot(seed: int = 42) -> dict:
    """Run the built-in offline policy against every task and cache the snapshot."""
    task_ids = list(list_tasks().keys())
    results: list[dict[str, object]] = []
    total_score = 0.0

    for task_id in task_ids:
        runner = BugTriageEnv()
        observation = runner.reset(task_id=task_id, seed=seed)
        done = False
        info: dict = {"metrics": {}}

        while not done:
            history: list[str] = []
            if runner.current_task is not None and runner.current_ticket_index < len(runner.ticket_states):
                history = list(runner.ticket_states[runner.current_ticket_index]["actions_taken"])

            action, _ = suggest_action(observation=observation, action_history=history)
            observation, _, done, info = runner.step(action)

        grader = BugTriageGrader(task_id=task_id)
        grader_result = grader.grade_episode(
            episode_actions=[],
            ground_truths=[gt.model_dump(mode="json") for gt in runner.current_task.ground_truths],
            metrics=info.get("metrics", {}),
        )
        total_score += grader_result.score
        results.append(
            {
                "task_id": task_id,
                "score": round(grader_result.score, 4),
                "passed": grader_result.passed,
            }
        )

    mean_score = total_score / len(results) if results else 0.0
    return {
        "model": "offline-heuristic",
        "offline_mode": True,
        "seed": seed,
        "mean_score": round(mean_score, 4),
        "results": results,
    }


def suggest_action(observation, action_history: list[str] | None = None) -> tuple[ActionModel, str]:
    """Return a deterministic next-step suggestion for the active ticket."""
    return recommend_action(observation=observation, action_history=action_history)


class ResetRequest(BaseModel):
    task_id: Optional[str] = None
    seed: Optional[int] = None


class StepRequest(BaseModel):
    action: ActionModel


def _app_base_path(request: Request) -> str:
    """Return proxy-aware base path without a trailing slash."""
    root_path = (request.scope.get("root_path") or "").rstrip("/")
    return root_path


@app.get("/")
def index(request: Request):
    """Serve the frontend with proxy-aware asset URLs."""
    index_file = STATIC_DIR / "index.html"
    if index_file.exists():
        html = index_file.read_text(encoding="utf-8")
        html = html.replace("__APP_BASE__", _app_base_path(request))
        return HTMLResponse(content=html)

    root_path = _app_base_path(request)
    docs_path = f"{root_path}/docs" if root_path else "/docs"
    return {"message": "Bug Triage OpenEnv API", "docs": docs_path}


@app.get("/favicon.ico", include_in_schema=False)
def favicon() -> Response:
    """Serve favicon for browser clients."""
    icon_file = STATIC_DIR / "favicon.svg"
    if icon_file.exists():
        return FileResponse(icon_file, media_type="image/svg+xml")
    return Response(status_code=204)


@app.get("/health")
@app.get("/openenv/health")
def health() -> dict[str, str]:
    return {
        "status": "ok",
        "environment": "bug-triage-openenv",
        "version": app.version,
    }


@app.get("/tasks")
@app.get("/openenv/tasks")
def tasks() -> dict:
    registry = list_tasks()
    return {
        "total": len(registry),
        "tasks": [
            {
                "id": task_id,
                "difficulty": meta["difficulty"],
                "description": meta["description"],
            }
            for task_id, meta in registry.items()
        ],
    }


@app.get("/baseline")
def baseline() -> dict:
    return _offline_baseline_snapshot()


@app.get("/suggest_action")
def suggest_current_action() -> dict:
    with ENV_LOCK:
        observation = env._get_observation()
        history: list[str] = []

        if env.current_task is not None and env.current_ticket_index < len(env.ticket_states):
            history = list(env.ticket_states[env.current_ticket_index]["actions_taken"])

        action, reason = suggest_action(observation=observation, action_history=history)
    return {
        "action": action.model_dump(mode="json", exclude_none=True),
        "reason": reason,
        "history": history,
    }


@app.get("/reset")
@app.get("/openenv/reset")
def reset_get(task_id: Optional[str] = None, seed: Optional[int] = None) -> dict:
    """Validator-friendly reset endpoint (GET)."""
    try:
        with ENV_LOCK:
            observation = env.reset(task_id=task_id, seed=seed)
    except (FileNotFoundError, ValueError) as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    return observation.model_dump(mode="json")


@app.post("/reset")
@app.post("/openenv/reset")
def reset_post(req: Optional[ResetRequest] = Body(default=None)) -> dict:
    """Typed reset endpoint (POST), compatible with empty-body validator calls."""
    task_id = req.task_id if req is not None else None
    seed = req.seed if req is not None else None
    try:
        with ENV_LOCK:
            observation = env.reset(task_id=task_id, seed=seed)
    except (FileNotFoundError, ValueError) as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    return observation.model_dump(mode="json")


@app.post("/step")
@app.post("/openenv/step")
def step(req: StepRequest) -> dict:
    try:
        with ENV_LOCK:
            observation, reward, done, info = env.step(req.action)
    except RuntimeError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc

    return {
        "observation": observation.model_dump(mode="json"),
        "reward": reward.model_dump(mode="json"),
        "done": done,
        "info": info,
    }


@app.get("/state")
@app.get("/openenv/state")
def state() -> dict:
    with ENV_LOCK:
        if env.current_task is None:
            return {
                "initialized": False,
                "current_task_id": None,
                "current_ticket_index": 0,
                "total_tickets": 0,
                "tickets_state": [],
                "steps_used": 0,
                "steps_remaining": 0,
                "cumulative_reward": 0.0,
                "episode_done": False,
            }

        current_state = env.state()
        payload = current_state.model_dump(mode="json")
        payload["initialized"] = True
        return payload


def main() -> None:
    """Run the API server with optional env-driven overrides."""
    host = os.getenv("HOST", "0.0.0.0")
    port = int(os.getenv("PORT", "7860"))
    reload_enabled = _as_bool(os.getenv("RELOAD", "false"))

    uvicorn.run(
        "server.app:app",
        host=host,
        port=port,
        reload=reload_enabled,
    )


if __name__ == "__main__":
    main()