File size: 6,459 Bytes
7045b45
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7045b45
a80eade
7045b45
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7045b45
a80eade
7045b45
 
a80eade
 
7045b45
 
 
 
 
a80eade
 
7045b45
 
 
 
 
 
 
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
 
 
 
 
 
7045b45
 
 
 
 
a80eade
 
7045b45
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
 
 
 
7045b45
 
 
 
 
a80eade
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pre-submission validation for the data-cleaning environment."""

from __future__ import annotations

from pathlib import Path
import sys

from fastapi.testclient import TestClient

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from app import create_app
from baseline.inference import BaselineAgent
from environment import OpenEnv, TASKS, grade_episode


def _check(condition: bool, message: str) -> None:
    if not condition:
        raise AssertionError(message)
    print(f"[ok] {message}")


def validate_manifest() -> None:
    manifest_path = ROOT / "openenv.yaml"
    _check(manifest_path.exists(), "openenv.yaml exists")
    manifest_text = manifest_path.read_text()
    _check("name: data-cleaning-env" in manifest_text, "manifest name is correct")
    _check("tags:" in manifest_text and "openenv" in manifest_text, "manifest includes openenv tag")
    _check("tasks:" in manifest_text and manifest_text.count("- id:") >= 5, "manifest lists the data-cleaning benchmark")


def validate_inference_script() -> None:
    inference_path = ROOT / "inference.py"
    _check(inference_path.exists(), "root inference.py exists")
    contents = inference_path.read_text()
    _check("API_BASE_URL" in contents, "inference.py references API_BASE_URL")
    _check("MODEL_NAME" in contents, "inference.py references MODEL_NAME")
    _check("HF_TOKEN" in contents, "inference.py references HF_TOKEN")
    _check("OPENAI_API_KEY" in contents, "inference.py supports OPENAI_API_KEY fallback")
    _check("OpenAI(" in contents, "inference.py uses the OpenAI client")


def validate_dockerfile() -> None:
    dockerfile = ROOT / "Dockerfile"
    _check(dockerfile.exists(), "Dockerfile exists")
    contents = dockerfile.read_text()
    _check("7860" in contents, "Dockerfile exposes HuggingFace Spaces port 7860")
    _check("uvicorn" in contents, "Dockerfile launches uvicorn")


def validate_graders() -> None:
    for task_id in TASKS:
        score = grade_episode(task_id, [])
        _check(0.0 <= score <= 1.0, f"{task_id} grader returns a valid score on empty history")

    score_levels = {
        grade_episode("duplicate_outlier", []),
        grade_episode(
            "duplicate_outlier",
            [{"action": {"action_type": "drop_row", "row_index": 1, "column": "row_id", "new_value": None, "reason": "duplicate"}}],
        ),
        grade_episode(
            "duplicate_outlier",
            [
                {"action": {"action_type": "drop_row", "row_index": 1, "column": "row_id", "new_value": None, "reason": "duplicate"}},
                {"action": {"action_type": "flag_anomaly", "row_index": 2, "column": "amount", "new_value": None, "reason": "outlier"}},
            ],
        ),
        grade_episode(
            "duplicate_outlier",
            [
                {"action": {"action_type": "drop_row", "row_index": 1, "column": "row_id", "new_value": None, "reason": "duplicate"}},
                {"action": {"action_type": "flag_anomaly", "row_index": 2, "column": "amount", "new_value": None, "reason": "outlier"}},
                {"action": {"action_type": "fix_value", "row_index": 3, "column": "amount", "new_value": 50.0, "reason": "negative amount"}},
                {"action": {"action_type": "standardize_format", "row_index": 5, "column": "status", "new_value": "completed", "reason": "normalize case"}},
            ],
        ),
    }
    _check(len(score_levels) >= 4, "grader exposes multiple distinct score levels")


def validate_api() -> None:
    client = TestClient(create_app(OpenEnv()))

    health = client.get("/health")
    _check(health.status_code == 200, "GET /health succeeds")

    tasks = client.get("/tasks")
    _check(tasks.status_code == 200, "GET /tasks succeeds")
    _check(isinstance(tasks.json(), list), "GET /tasks returns a bare array")
    _check(len(tasks.json()) >= 5, "GET /tasks exposes the data-cleaning benchmark")
    _check(all("answer" not in task for task in tasks.json()), "GET /tasks does not leak answers")

    reset = client.post("/reset")
    _check(reset.status_code == 200, "POST /reset succeeds with no body")
    session_id = reset.json()["session_id"]

    step = client.post(
        f"/step?session_id={session_id}",
        json={
            "action_type": "flag_anomaly",
            "row_index": 0,
            "column": "reading",
            "new_value": None,
            "reason": "This looks suspicious.",
        },
    )
    _check(step.status_code == 200, "POST /step succeeds with raw data-cleaning action JSON")

    state = client.get(f"/state?session_id={session_id}")
    _check(state.status_code == 200, "GET /state succeeds")
    history = state.json()["episode_history"]
    _check(len(history) == 1, "state includes episode history")

    grader = client.post(
        f"/grader?session_id={session_id}",
        json={"task_id": state.json()["task_id"], "episode": history},
    )
    _check(grader.status_code == 200, "POST /grader succeeds")

    with client.websocket_connect("/ws") as websocket:
        websocket.send_json({"type": "tasks"})
        tasks_message = websocket.receive_json()
        _check(tasks_message["type"] == "tasks", "WS /ws tasks message succeeds")

        websocket.send_json({"type": "reset", "payload": {"task_id": "null_filling"}})
        reset_message = websocket.receive_json()
        _check(reset_message["type"] == "reset", "WS /ws reset succeeds")

        websocket.send_json(
            {
                "type": "step",
                "payload": {
                    "action_type": "fill_missing",
                    "row_index": 1,
                    "column": "age",
                    "new_value": "unknown",
                    "reason": "age is missing",
                },
            }
        )
        step_message = websocket.receive_json()
        _check(step_message["type"] == "step", "WS /ws step succeeds")


def validate_baseline() -> None:
    agent = BaselineAgent(env=OpenEnv(), use_llm=False)
    results = agent.run(task_ids=["null_filling"], max_episodes_per_task=1)
    _check(results["summary"]["total_episodes"] == 1, "baseline smoke test completes")


def main() -> None:
    validate_manifest()
    validate_inference_script()
    validate_dockerfile()
    validate_graders()
    validate_api()
    validate_baseline()
    print("Validation complete.")


if __name__ == "__main__":
    main()