"""
FastAPI application for the SQL Migration Environment.
Uses create_app() from the OpenEnv framework to auto-generate all
standard endpoints (/reset, /step, /state, /ws, /health, /schema).
Additionally defines three hackathon-required custom endpoints:
/tasks, /grader, /baseline.
Usage:
uvicorn server.app:app --host 0.0.0.0 --port 7860
"""
import os
import subprocess
import sys
from typing import Any, Dict, List, Optional
from fastapi import Body
# Support both in-repo and standalone imports
import server.environment # Ensuring server is treated as a package
try:
from openenv.core.env_server.http_server import create_app
from models import MigrationAction, MigrationObservation
from server.environment import DbMigrationEnvironment
except ImportError:
from openenv.core.env_server.http_server import create_app
from ..models import MigrationAction, MigrationObservation
from .environment import DbMigrationEnvironment
# Get task name from environment variable (default to column-restructure)
DEFAULT_TASK = os.getenv("MIGRATION_TASK", "column-restructure")
# Factory function for per-session environment creation
def create_migration_environment():
"""Factory function that creates DbMigrationEnvironment instances."""
return DbMigrationEnvironment(task_name=DEFAULT_TASK)
# Create the FastAPI app using OpenEnv's create_app factory
# This auto-generates: /reset, /step, /state, /ws, /health, /schema, /docs
app = create_app(
create_migration_environment,
MigrationAction,
MigrationObservation,
env_name="sql_migration_env",
)
# =============================================================================
# Custom Hackathon Endpoints
# =============================================================================
from fastapi.responses import HTMLResponse
@app.get("/", response_class=HTMLResponse)
async def root():
"""Root endpoint — returns a premium status page for the HF Space UI."""
return """
SQL Migration Agent | OpenEnv Benchmark
SQL Migration Agent
Production-Grade OpenEnv Benchmark Suite
● Online & Compliant
Core Endpoints
POST/resetInitialize task state
POST/stepExecute SQL agent action
GET/stateCurrent episode status
GET/tasksList benchmark tasks
POST/graderRun golden-DB comparison
Benchmark Features
This environment provides high-fidelity SQLite migration tasks designed to pressure-test schema decomposition,
type coercion, and data integrity handling in LLMs.
✔ Dynamic Grader
Seed-independent golden-DB logic.
✔ Efficiency Metrics
Tracks Query Ops & Latency.
✔ ERD Viz
Real-time Mermaid diagrams.
✔ Anti-Exploit
PRAGMA & dialect blacklisting.
Assessment Tasks
EasyColumn Merge
Merge name fields with apostrophe preservation.
MediumNormalization
Decompose god-table into 3NF schema.
HardCascade Sync
Multi-table FK cascade with audit logging.
ExtremeData Poisoning
Quarantine poisoned staging data with strict schema integrity.