Spaces:
Sleeping
Sleeping
File size: 9,951 Bytes
f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b cb24840 f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b 8cb206e f812d5b ea504bf cb24840 ea504bf f812d5b 8cb206e f812d5b 1a89fae f812d5b f2d88cb cb24840 1a89fae 8cb206e 1a89fae f2d88cb f812d5b 8cb206e | 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 | from pydantic import BaseModel, Field, field_validator
from typing import Optional, Any
from enum import Enum
import time
# βββββββββββββββββββββββββββββββββββββββββββββ
# ENUMS
# βββββββββββββββββββββββββββββββββββββββββββββ
class DifficultyLevel(str, Enum):
EASY = "easy"
MEDIUM = "medium"
HARD = "hard"
class ActionType(str, Enum):
# ββ Round 1 actions (keep β backward compatible) ββ
IDENTIFY_ERROR = "identify_error"
PROPOSE_FIX = "propose_fix"
SUBMIT_ANSWER = "submit_answer"
REQUEST_HINT = "request_hint"
EXPLAIN_ISSUE = "explain_issue"
OPTIMIZE_QUERY = "optimize_query"
# ββ Round 2 new actions ββ
INSPECT_QUERY = "inspect_query"
ANALYZE_INDEXES = "analyze_indexes"
CREATE_INDEX = "create_index"
REWRITE_QUERY = "rewrite_query"
ADD_COLUMN = "add_column"
DROP_INDEX = "drop_index"
PARTITION_TABLE = "partition_table"
ANALYZE_STATS = "analyze_statistics"
SUBMIT_REPORT = "submit_report"
# βββββββββββββββββββββββββββββββββββββββββββββ
# CORE MODELS
# βββββββββββββββββββββββββββββββββββββββββββββ
class Observation(BaseModel):
task_id: str = Field(..., description="Unique task identifier")
task_description: str = Field(..., description="What the agent must do")
current_context: dict = Field(..., description="What the agent currently sees")
step_count: int = Field(default=0, ge=0, description="Steps taken so far")
difficulty: DifficultyLevel = Field(..., description="Task difficulty level")
max_steps: int = Field(default=50, description="Maximum steps allowed")
hints_used: int = Field(default=0, description="Number of hints used")
previous_actions: list[str] = Field(default_factory=list, description="History of action types taken")
metadata: dict = Field(default_factory=dict, description="Extra task metadata")
model_config = {"json_schema_extra": {
"example": {
"task_id": "easy_s001",
"task_description": "Optimize a slow user lookup query on 10K users table.",
"current_context": {
"tables": [{"name": "users", "rows": 10000, "indexes": ["PRIMARY"]}],
"slow_queries": [{"id": "q1", "sql": "SELECT * FROM users WHERE email=?", "avg_ms": 2000}],
"performance_score": 8.0,
"target_score": 80.0
},
"step_count": 0,
"difficulty": "easy",
"max_steps": 50,
"hints_used": 0,
"previous_actions": [],
"metadata": {"scenario_id": "easy_s001", "baseline_score": 8.0}
}
}}
class Action(BaseModel):
action_type: ActionType = Field(..., description="Type of action the agent is taking")
payload: dict = Field(..., description="Action-specific data")
@field_validator("payload")
@classmethod
def payload_must_not_be_empty(cls, v):
if v is None:
raise ValueError("Payload cannot be None")
return v
@field_validator("payload")
@classmethod
def truncate_long_strings(cls, v):
def truncate(obj, max_len=5000):
if isinstance(obj, str) and len(obj) > max_len:
return obj[:max_len] + "...[truncated]"
if isinstance(obj, dict):
return {k: truncate(val, max_len) for k, val in obj.items()}
return obj
return truncate(v)
model_config = {"json_schema_extra": {
"example": {
"action_type": "create_index",
"payload": {
"table": "users",
"columns": ["email"]
}
}
}}
class Reward(BaseModel):
score: float = Field(..., ge=-1.0, le=1.0, description="Reward score between -1.0 and 1.0")
breakdown: dict = Field(..., description="Partial credit details per dimension")
feedback: str = Field(..., description="Human-readable explanation of the reward")
@field_validator("score")
@classmethod
def clamp_score(cls, v):
return max(0.0, min(1.0, round(v, 4)))
model_config = {"json_schema_extra": {
"example": {
"score": 0.75,
"breakdown": {
"step_reward": 0.05,
"delta_reward": 0.40,
"milestone_bonus": 0.15,
"total": 0.60
},
"feedback": "Index created. Performance improved 55%. Milestone bonus earned!"
}
}}
# βββββββββββββββββββββββββββββββββββββββββββββ
# EPISODE STATE (used by state() endpoint)
# βββββββββββββββββββββββββββββββββββββββββββββ
class EpisodeState(BaseModel):
task_id: Optional[str] = Field(default=None)
difficulty: Optional[DifficultyLevel] = Field(default=None)
step_count: int = Field(default=0)
total_reward: float = Field(default=0.0)
done: bool = Field(default=False)
hints_used: int = Field(default=0)
previous_actions: list[str] = Field(default_factory=list)
action_counts: dict[str, Any] = Field(default_factory=dict)
started_at: Optional[float] = Field(default=None)
last_reward: float = Field(default=0.0)
initialized: bool = Field(default=False)
model_config = {"json_schema_extra": {
"example": {
"task_id": "easy_s001",
"difficulty": "easy",
"step_count": 3,
"total_reward": 0.65,
"done": False,
"hints_used": 0,
"previous_actions": ["inspect_query", "analyze_indexes", "create_index"],
"action_counts": {"inspect_query": 1, "analyze_indexes": 1, "create_index": 1},
"started_at": 1700000000.0,
"last_reward": 0.45,
"initialized": True
}
}}
# βββββββββββββββββββββββββββββββββββββββββββββ
# API REQUEST / RESPONSE WRAPPERS
# βββββββββββββββββββββββββββββββββββββββββββββ
class StepResponse(BaseModel):
observation: Observation
reward: Reward
done: bool
info: dict
class ResetResponse(BaseModel):
observation: Observation
class TaskInfo(BaseModel):
id: str
difficulty: DifficultyLevel
description: str
action_schema: dict
class TaskListResponse(BaseModel):
tasks: list[TaskInfo]
total: int
action_types: list[str]
class BaselineResult(BaseModel):
task_id: str
difficulty: DifficultyLevel
score: float
steps: int
feedback: str
@field_validator("score")
@classmethod
def clamp_score(cls, v):
return max(0.0, min(1.0, round(float(v), 4)))
class BaselineResponse(BaseModel):
results: list[BaselineResult]
average_score: float
completed_at: float = Field(default_factory=time.time)
class GraderRequest(BaseModel):
task_id: str
action: Optional[Action] = None
episode: Optional[dict] = None
class GraderResponse(BaseModel):
score: float = Field(..., description="Score strictly between 0 and 1 exclusive")
feedback: str
breakdown: dict
@field_validator("score")
@classmethod
def clamp_score(cls, v):
return max(0.0, min(1.0, round(float(v), 4)))
model_config = {"json_schema_extra": {
"example": {
"score": 0.82,
"feedback": "Performance improved from 12.5 to 85.0. Excellent optimization!",
"breakdown": {"perf_improvement": 0.60, "step_efficiency": 0.12, "index_quality": 0.10}
}
}}
class HealthResponse(BaseModel):
status: str = "ok"
version: str = "2.0.0"
uptime: float = Field(default_factory=time.time)
# βββββββββββββββββββββββββββββββββββββββββββββ
# ROUND 2 β PROGRESS RESPONSE
# βββββββββββββββββββββββββββββββββββββββββββββ
class ProgressResponse(BaseModel):
scenario_id: Optional[str] = Field(default=None)
performance_score: float = Field(default=0.0, description="Current DB performance score 0-100")
baseline_score: float = Field(default=0.0, description="Starting score this episode")
target_score: float = Field(default=85.0, description="Score needed to succeed")
improvement_history: list[float] = Field(default_factory=list)
milestones_earned: list[float] = Field(default_factory=list)
best_score: float = Field(default=0.0)
steps_used: int = Field(default=0)
budget_remaining: int = Field(default=50)
total_reward: float = Field(default=0.0)
|