File size: 3,147 Bytes
50bf486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Pydantic models for the SQL Debug Environment.

These extend the openenv-core base types so the framework knows
how to serialize/deserialize our action and observation payloads.

  Action      → SQLAction      (just a query string)
  Observation → SQLObservation  (all the feedback the agent sees)
  State       → SQLState        (episode tracking for checkpoints)
"""

from __future__ import annotations

from typing import Any, Optional

from openenv.core.env_server import Action, Observation, State
from pydantic import Field


class SQLAction(Action):
    """What the agent sends us — just a SQL query."""

    query: str = Field(
        ...,
        description="SQL query to run against the database.",
        examples=[
            "SELECT name, salary FROM employees WHERE department = 'Engineering' ORDER BY salary DESC"
        ],
    )


class SQLObservation(Observation):
    """Everything the agent sees after a reset() or step().

    Inherits done, reward, and metadata from the base Observation class.
    """

    task_id: str = Field(
        ...,
        description="Which task we're on (fix_syntax, fix_join, optimize_query).",
    )
    broken_query: str = Field(
        ...,
        description="The original broken query that needs fixing.",
    )
    schema_description: str = Field(
        ...,
        description="Human-readable schema description for the relevant tables.",
    )
    error_message: Optional[str] = Field(
        None,
        description="SQL error from the last query, if it failed.",
    )
    result_preview: Optional[list[dict[str, Any]]] = Field(
        None,
        description="First 10 rows of the last query's output (when it ran successfully).",
    )
    expected_row_count: int = Field(
        ...,
        description="How many rows the correct query returns.",
    )
    current_row_count: Optional[int] = Field(
        None,
        description="How many rows the agent's last query returned.",
    )
    step_number: int = Field(
        0,
        description="Current step in the episode (0 = haven't submitted anything yet).",
    )
    max_steps: int = Field(
        8,
        description="Max steps before the episode auto-terminates.",
    )
    hint: Optional[str] = Field(
        None,
        description="Hint given at certain steps to nudge the agent in the right direction.",
    )


class SQLState(State):
    """Episode-level state — mostly for debugging and checkpointing.

    Inherits episode_id and step_count from the base State class.
    """

    task_id: str = Field(default="", description="Which task is active.")
    max_steps: int = Field(default=8, description="Step limit for the episode.")
    last_reward: float = Field(default=0.0, description="Most recent step's reward.")
    cumulative_max_reward: float = Field(
        default=0.0, description="Best reward seen so far this episode."
    )
    done: bool = Field(default=False, description="Whether the episode is over.")
    history: list[dict[str, Any]] = Field(
        default_factory=list,
        description="Log of (query, reward) pairs from this episode.",
    )