File size: 7,592 Bytes
d814291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

from fastapi.testclient import TestClient

import server
from server import app


client = TestClient(app)


def test_server_health():
    response = client.get("/healthz")
    assert response.status_code == 200
    assert response.json()["status"] == "ok"


def test_server_health_alias():
    response = client.get("/health")
    assert response.status_code == 200
    assert response.json()["status"] == "ok"


def test_server_environment_metadata():
    response = client.get("/api/environment")
    assert response.status_code == 200
    body = response.json()
    assert "action_space" in body
    assert "observation_space" in body
    assert "summary" in body


def test_openenv_spec_and_tasks_endpoints():
    spec = client.get("/openenv.yaml")
    assert spec.status_code == 200
    assert "reset" in spec.text

    tasks = client.get("/openenv/tasks")
    assert tasks.status_code == 200
    body = tasks.json()
    assert len(body) >= 3
    assert {"task_id", "task_type", "question", "difficulty"} <= set(body[0].keys())


def test_openenv_reset_step_and_state_cycle():
    reset = client.post("/openenv/reset", json={"task_index": 0})
    assert reset.status_code == 200
    body = reset.json()
    session_id = body["session_id"]
    assert body["done"] is False
    assert "question" in body["observation"]["task"]

    state = client.get(f"/openenv/state/{session_id}")
    assert state.status_code == 200
    assert state.json()["session_id"] == session_id

    step = client.post(
        "/openenv/step",
        json={
            "session_id": session_id,
            "action_type": "ANSWER",
            "payload": {"answer": "unknown"},
        },
    )
    assert step.status_code == 200
    step_body = step.json()
    assert step_body["session_id"] == session_id
    assert step_body["done"] is True
    assert "task_answer" in step_body["info"]


def test_openenv_reset_accepts_empty_body():
    reset = client.post("/openenv/reset")
    assert reset.status_code == 200
    body = reset.json()
    assert body["done"] is False
    assert "session_id" in body


def test_openenv_reset_accepts_empty_json_body():
    reset = client.post(
        "/openenv/reset",
        data="",
        headers={"Content-Type": "application/json"},
    )
    assert reset.status_code == 200
    body = reset.json()
    assert body["done"] is False
    assert "session_id" in body


def test_openenv_reset_trailing_slash_post_returns_json():
    reset = client.post(
        "/openenv/reset/",
        data="",
        headers={"Content-Type": "application/json"},
    )
    assert reset.status_code == 200
    body = reset.json()
    assert body["done"] is False
    assert "session_id" in body


def test_openenv_step_accepts_nested_action_payload():
    reset = client.post("/openenv/reset", json={"task_index": 0})
    assert reset.status_code == 200
    session_id = reset.json()["session_id"]

    step = client.post(
        "/openenv/step",
        json={
            "session_id": session_id,
            "action": {
                "action_type": "ANSWER",
                "payload": {"answer": "unknown"},
            },
        },
    )
    assert step.status_code == 200
    assert step.json()["done"] is True


def test_step_alias_uses_latest_session_when_session_id_missing():
    reset = client.post("/reset", json={"task_index": 0})
    assert reset.status_code == 200
    session_id = reset.json()["session_id"]

    step = client.post(
        "/step",
        json={
            "action_type": "ANSWER",
            "payload": {"answer": "unknown"},
        },
    )
    assert step.status_code == 200
    body = step.json()
    assert body["session_id"] == session_id
    assert body["done"] is True


def test_state_alias_returns_latest_session():
    reset = client.post("/reset", json={"task_index": 0})
    assert reset.status_code == 200
    session_id = reset.json()["session_id"]

    state = client.get("/state")
    assert state.status_code == 200
    body = state.json()
    assert body["session_id"] == session_id
    assert "task" in body["observation"]


def test_report_inference_updates_latest_evaluation_and_dashboard(tmp_path, monkeypatch):
    latest_evaluation = tmp_path / "latest_evaluation.json"
    space_dashboard = tmp_path / "space_dashboard.html"

    monkeypatch.setattr(server, "LATEST_EVALUATION_OUTPUT", latest_evaluation)
    monkeypatch.setattr(server, "SPACE_DASHBOARD", space_dashboard)
    monkeypatch.setattr(server, "load_leaderboard", lambda path: [])
    monkeypatch.setattr(server, "export_dashboard", lambda env, evaluation, leaderboard_records, output_path: str(space_dashboard))

    response = client.post(
        "/openenv/report_inference",
        json={
            "run": {"name": "inference_py_run"},
            "summary": {"leaderboard_score": 0.75, "task_success_rate": 1.0},
            "episodes": [
                {
                    "task_id": "seed_task_0",
                    "agent_answer": "user_bharat",
                    "graph_f1": 0.5,
                    "reward": 1.2,
                    "steps": 5,
                    "tool_calls": 4,
                    "success": 1,
                }
            ],
        },
    )
    assert response.status_code == 200
    body = response.json()
    assert body["status"] == "ok"
    assert latest_evaluation.exists()
    stored = json.loads(latest_evaluation.read_text(encoding="utf-8"))
    assert stored["summary"]["leaderboard_score"] == 0.75
    assert stored["episodes"][0]["task_id"] == "seed_task_0"
    assert stored["episodes"][0]["truth_edges"]


def test_space_snapshot_prefers_newer_evaluation_payload(tmp_path, monkeypatch):
    baseline_path = tmp_path / "baseline.json"
    evaluation_path = tmp_path / "evaluation.json"
    baseline_dashboard = tmp_path / "baseline_dashboard.html"
    space_dashboard = tmp_path / "space_dashboard.html"

    baseline_path.write_text(
        json.dumps(
            {
                "run": {"dashboard_path": str(baseline_dashboard)},
                "summary": {"leaderboard_score": 0.1, "task_success_rate": 0.1},
            }
        ),
        encoding="utf-8",
    )
    baseline_dashboard.write_text("<html>baseline</html>", encoding="utf-8")
    evaluation_path.write_text(
        json.dumps({"summary": {"leaderboard_score": 0.9, "task_success_rate": 0.9}, "episodes": []}),
        encoding="utf-8",
    )
    space_dashboard.write_text("<html>space</html>", encoding="utf-8")
    os.utime(evaluation_path, (baseline_path.stat().st_atime + 5, baseline_path.stat().st_mtime + 5))

    monkeypatch.setattr(server, "LATEST_BASELINE_OUTPUT", baseline_path)
    monkeypatch.setattr(server, "LATEST_EVALUATION_OUTPUT", evaluation_path)
    monkeypatch.setattr(server, "SPACE_DASHBOARD", space_dashboard)
    monkeypatch.setattr(
        server,
        "_base_environment_snapshot",
        lambda: {
            "task_count": 30,
            "difficulty_counts": {},
            "action_space": ["CALL_TOOL", "ADD_EDGE", "ANSWER"],
            "observation_space": {},
            "task_types": [],
            "config": {},
        },
    )
    monkeypatch.setattr(server, "_build_environment", lambda: object())
    monkeypatch.setattr(server, "export_dashboard", lambda env, evaluation, leaderboard_records, output_path: str(space_dashboard))

    snapshot = server._space_snapshot()
    assert snapshot["source"] == "latest_evaluation"
    assert snapshot["summary"]["leaderboard_score"] == 0.9
    assert snapshot["dashboard_path"] == str(space_dashboard)