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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
models.py
---------
Pydantic models for the Data Cleaning Pipeline environment.

Three models define the full agent↔environment contract:

  CleanAction      β€” what the agent sends on each step
  CleanObservation β€” what the agent receives back
  CleanState       β€” internal server state (not sent to agent directly)

Inheritance chain (confirmed from OpenEnv source):
  Action      β†’ extra="forbid", has: metadata: Dict[str, Any]
  Observation β†’ extra="forbid", has: done: bool, reward: float|None, metadata: Dict[str, Any]
  State       β†’ extra="allow",  has: episode_id: Optional[str], step_count: int
"""

from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional

from pydantic import Field, field_validator, model_validator

try:
    from openenv.core.env_server.types import Action, Observation, State
except ImportError:
    # Fallback for local development without the full OpenEnv install
    from openenv.core.env_server import Action, Observation, State


# ── Valid values (used by validators + schema hints) ──────────────────────────

VALID_COMMANDS = Literal[
    "SET_VALUE",       # Fix a specific cell: (row_index, column, value)
    "DROP_ROW",        # Remove an entire row: (row_index,)
    "STANDARDIZE_COL", # Normalize an entire column's format: (column,)
    "FILL_MISSING",    # Fill NaN values in a column: (column, fill_strategy)
    "DONE",            # Agent signals episode is complete: ()
]

VALID_FILL_STRATEGIES = Literal["mean", "median", "mode", "drop"]

VALID_TASK_IDS = Literal["easy", "medium", "hard"]


# ─────────────────────────────────────────────────────────────────────────────
# CleanAction
# ─────────────────────────────────────────────────────────────────────────────

class CleanAction(Action):
    """Action sent by the agent each step.

    The ``command`` field selects the operation. Depending on command,
    only a subset of the remaining fields are required:

    +-----------------+------------+--------+-------+---------------+
    | command         | row_index  | column | value | fill_strategy |
    +=================+============+========+=======+===============+
    | SET_VALUE       | required   | req    | req   | β€”             |
    | DROP_ROW        | required   | β€”      | β€”     | β€”             |
    | STANDARDIZE_COL | β€”          | req    | β€”     | β€”             |
    | FILL_MISSING    | β€”          | req    | β€”     | required      |
    | DONE            | β€”          | β€”      | β€”     | β€”             |
    +-----------------+------------+--------+-------+---------------+

    Example (fix a single cell)::

        CleanAction(
            command="SET_VALUE",
            row_index=3,
            column="price",
            value="29.99",
        )

    Example (drop a whole row)::

        CleanAction(command="DROP_ROW", row_index=17)

    Example (fill all NaN in a column with the median)::

        CleanAction(
            command="FILL_MISSING",
            column="quantity",
            fill_strategy="median",
        )
    """

    command: VALID_COMMANDS = Field(
        ...,
        description=(
            "Operation to perform. One of: SET_VALUE, DROP_ROW, "
            "STANDARDIZE_COL, FILL_MISSING, DONE."
        ),
    )

    row_index: Optional[int] = Field(
        default=None,
        ge=0,
        description=(
            "Zero-based row index to target. "
            "Required for SET_VALUE and DROP_ROW."
        ),
    )

    column: Optional[str] = Field(
        default=None,
        min_length=1,
        description=(
            "Name of the column to target. "
            "Required for SET_VALUE, STANDARDIZE_COL, and FILL_MISSING."
        ),
    )

    value: Optional[str] = Field(
        default=None,
        description=(
            "New cell value as a string. "
            "Required for SET_VALUE. The environment casts this to the "
            "column's expected dtype (e.g. '29.99' β†’ float for a price column)."
        ),
    )

    fill_strategy: Optional[VALID_FILL_STRATEGIES] = Field(
        default=None,
        description=(
            "Strategy for FILL_MISSING. One of: mean, median, mode, drop. "
            "'drop' removes rows where the column is NaN."
        ),
    )

    @model_validator(mode="after")
    def _check_required_fields(self) -> "CleanAction":
        """Ensure each command has exactly the fields it needs."""
        cmd = self.command

        if cmd == "SET_VALUE":
            missing = []
            if self.row_index is None:
                missing.append("row_index")
            if self.column is None:
                missing.append("column")
            if self.value is None:
                missing.append("value")
            if missing:
                raise ValueError(
                    f"SET_VALUE requires: {', '.join(missing)}"
                )

        elif cmd == "DROP_ROW":
            if self.row_index is None:
                raise ValueError("DROP_ROW requires row_index")

        elif cmd == "STANDARDIZE_COL":
            if self.column is None:
                raise ValueError("STANDARDIZE_COL requires column")

        elif cmd == "FILL_MISSING":
            missing = []
            if self.column is None:
                missing.append("column")
            if self.fill_strategy is None:
                missing.append("fill_strategy")
            if missing:
                raise ValueError(
                    f"FILL_MISSING requires: {', '.join(missing)}"
                )

        # DONE requires nothing β€” always valid

        return self

    @field_validator("row_index")
    @classmethod
    def _non_negative_row(cls, v: Optional[int]) -> Optional[int]:
        if v is not None and v < 0:
            raise ValueError(f"row_index must be >= 0, got {v}")
        return v


# ─────────────────────────────────────────────────────────────────────────────
# CleanObservation
# ─────────────────────────────────────────────────────────────────────────────

class CleanObservation(Observation):
    """Observation returned to the agent after each step (and at reset).

    The agent sees the full current state of the dirty CSV at every step
    so it can decide what to fix next. This is intentionally verbose β€”
    passing the whole CSV string keeps the environment stateless from the
    agent's perspective (no hidden memory needed).

    Inherited from Observation (do NOT redeclare these):
      done:     bool           β€” True when the episode has ended
      reward:   float | None   β€” per-step reward (None at reset)
      metadata: Dict[str, Any] β€” extra info (unused by core loop)
    """

    # ── Task context (set at reset, constant for the episode) ────────────────

    task_id: VALID_TASK_IDS = Field(
        ...,
        description="Which task is active: 'easy', 'medium', or 'hard'.",
    )

    schema_hint: str = Field(
        ...,
        description=(
            "Plain-English description of the target schema. "
            "Tells the agent what the clean data should look like."
        ),
    )

    initial_dirty_cells: int = Field(
        ...,
        ge=0,
        description=(
            "Total number of cells that differed from ground truth at episode start. "
            "Used to compute a normalised progress score."
        ),
    )

    # ── Per-step state ───────────────────────────────────────────────────────

    dirty_csv: str = Field(
        ...,
        description=(
            "Full current state of the working DataFrame serialised as a CSV string. "
            "This reflects all changes the agent has made so far this episode."
        ),
    )

    current_score: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description=(
            "Grader score after the last action (0.0 = no cells correct, "
            "1.0 = perfect match with ground truth)."
        ),
    )

    issues_remaining: int = Field(
        default=0,
        ge=0,
        description=(
            "Approximate count of cells still differing from ground truth. "
            "Convenience field β€” agents can also derive this from the CSV."
        ),
    )

    step_number: int = Field(
        default=0,
        ge=0,
        description="How many steps have been taken in this episode so far.",
    )

    max_steps: int = Field(
        ...,
        ge=1,
        description="Maximum steps allowed for this task before forced termination.",
    )

    # ── Last-action feedback ────────────────────────────────────────────────

    last_action_success: bool = Field(
        default=True,
        description=(
            "Whether the last action was applied without errors. "
            "False if the column/row didn't exist, value couldn't be cast, etc."
        ),
    )

    last_action_error: Optional[str] = Field(
        default=None,
        description=(
            "Error message if last_action_success is False, else None. "
            "Helps the agent self-correct."
        ),
    )

    @field_validator("current_score")
    @classmethod
    def _round_score(cls, v: float) -> float:
        return round(v, 4)


# ─────────────────────────────────────────────────────────────────────────────
# CleanState
# ─────────────────────────────────────────────────────────────────────────────

class CleanState(State):
    """Internal server-side state. Never sent to the agent directly.

    Holds the live DataFrames, ground truth, and grader metadata.
    Because State uses extra="allow", we can store arbitrary fields
    without listing them in the JSON schema.

    Inherited from State:
      episode_id: Optional[str]   β€” unique episode identifier
      step_count: int             β€” steps taken this episode (ge=0)
    """

    # ── Task identity ────────────────────────────────────────────────────────

    task_id: str = Field(
        default="easy",
        description="Active task: 'easy', 'medium', or 'hard'.",
    )

    # ── DataFrame snapshots (stored as CSV strings for serialisation) ────────
    # NOTE: The environment keeps live pd.DataFrame objects in instance vars.
    # These string fields are the serialised snapshots used by state() calls
    # and for WebSocket state responses.

    dirty_csv_snapshot: str = Field(
        default="",
        description="Current working DataFrame serialised to CSV string.",
    )

    clean_csv_snapshot: str = Field(
        default="",
        description="Ground-truth clean DataFrame serialised to CSV string.",
    )

    # ── Scoring ──────────────────────────────────────────────────────────────

    initial_dirty_cells: int = Field(
        default=0,
        ge=0,
        description="Dirty cell count at episode start (denominator for progress).",
    )

    current_score: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description="Grader score after the last step.",
    )

    previous_score: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description="Grader score before the last step (for reward delta).",
    )

    # ── Task metadata (passed through from TaskDataset.metadata) ─────────────
    # Contains grader-specific ground truth: outlier_rows, canonical_lookup, etc.

    task_metadata: Dict[str, Any] = Field(
        default_factory=dict,
        description=(
            "Task-specific metadata from dataset_factory.TaskDataset.metadata. "
            "Contains grader ground truth (outlier_rows, duplicate_pairs, etc.)."
        ),
    )

    # ── Schema hint (echoed in observations) ────────────────────────────────

    schema_hint: str = Field(
        default="",
        description="Plain-English schema description for this task.",
    )

    # ── Per-task step budget ─────────────────────────────────────────────────

    max_steps: int = Field(
        default=40,
        ge=1,
        description="Maximum steps for this task (40 / 80 / 150 for easy/medium/hard).",
    )

    @field_validator("current_score", "previous_score")
    @classmethod
    def _clamp_score(cls, v: float) -> float:
        return round(max(0.0, min(1.0, v)), 4)


# ── Step budget constants ─────────────────────────────────────────────────────

MAX_STEPS: Dict[str, int] = {
    "easy":   40,
    "medium": 80,
    "hard":   150,
}

# Done threshold: score at which the agent is considered successful
DONE_THRESHOLD: Dict[str, float] = {
    "easy":   0.95,
    "medium": 0.85,
    "hard":   0.80,
}


# ── Smoke test ────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import json

    print("── CleanAction examples ──────────────────────────────────────")

    a1 = CleanAction(command="SET_VALUE", row_index=3, column="price", value="29.99")
    print("SET_VALUE:      ", a1.model_dump())

    a2 = CleanAction(command="DROP_ROW", row_index=17)
    print("DROP_ROW:       ", a2.model_dump())

    a3 = CleanAction(command="FILL_MISSING", column="quantity", fill_strategy="median")
    print("FILL_MISSING:   ", a3.model_dump())

    a4 = CleanAction(command="STANDARDIZE_COL", column="order_date")
    print("STANDARDIZE_COL:", a4.model_dump())

    a5 = CleanAction(command="DONE")
    print("DONE:           ", a5.model_dump())

    # Validation: SET_VALUE without row_index should fail
    print("\n── Validation ────────────────────────────────────────────────")
    try:
        bad = CleanAction(command="SET_VALUE", column="price", value="10.0")
    except Exception as e:
        print(f"Expected error (missing row_index): {e}")

    try:
        bad = CleanAction(command="FILL_MISSING", column="price")
    except Exception as e:
        print(f"Expected error (missing fill_strategy): {e}")

    print("\n── CleanObservation ──────────────────────────────────────────")
    obs = CleanObservation(
        task_id="easy",
        schema_hint="Sales orders dataset. price must be float.",
        initial_dirty_cells=29,
        dirty_csv="order_id,price\n1001,N/A\n1002,19.99",
        current_score=0.0,
        issues_remaining=29,
        step_number=0,
        max_steps=40,
        done=False,
        reward=None,
    )
    print(json.dumps(obs.model_dump(), indent=2))

    print("\n── CleanState ────────────────────────────────────────────────")
    state = CleanState(
        episode_id="ep-001",
        step_count=0,
        task_id="easy",
        dirty_csv_snapshot="order_id,price\n1001,N/A",
        clean_csv_snapshot="order_id,price\n1001,14.99",
        initial_dirty_cells=29,
        current_score=0.0,
        previous_score=0.0,
        task_metadata={"injected_cells": [(0, "price")]},
        schema_hint="Sales orders dataset.",
        max_steps=40,
    )
    print(json.dumps(state.model_dump(), indent=2))

    print("\n── JSON schemas ──────────────────────────────────────────────")
    print("Action schema keys:     ", list(CleanAction.model_json_schema()["properties"].keys()))
    print("Observation schema keys:", list(CleanObservation.model_json_schema()["properties"].keys()))
    print("State schema keys:      ", list(CleanState.model_json_schema()["properties"].keys()))