Spaces:
Sleeping
Sleeping
File size: 18,334 Bytes
f89b1ac a1b343c f89b1ac | 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 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | """High-signal regression tests for seeded grading and public API shape."""
from __future__ import annotations
import json
import shutil
import sqlite3
import sys
from pathlib import Path
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 models import DataOpsAction # noqa: E402
from server.app import app # noqa: E402
from server.dataops_env_environment import DataOpsEnvironment # noqa: E402
from server.grading import evaluate_task # noqa: E402
from server.task_specs import build_task_3_report # noqa: E402
def _fixed_pipeline_script(visible_batch: list[dict[str, object]]) -> str:
return f'''\
import json
def process_data_stream(payloads):
processed_records = []
for payload in payloads:
if payload["status"] != "ready" or int(payload["amount_cents"]) <= 0:
continue
amount_usd = round(int(payload["amount_cents"]) / 100.0, 2)
priority_band = (
"high"
if int(payload["priority"]) >= 8 or amount_usd >= 500.0
else "normal"
)
processed_records.append(
{{
"order_id": payload["order_id"],
"region": payload["region"],
"amount_usd": amount_usd,
"priority_band": priority_band,
}}
)
processed_records.sort(key=lambda item: (-item["amount_usd"], item["order_id"]))
return processed_records
if __name__ == "__main__":
mock_batch = {visible_batch!r}
print(json.dumps(process_data_stream(mock_batch), indent=2, sort_keys=True))
'''
def _visible_only_pipeline_stub(
visible_batch: list[dict[str, object]],
visible_expected: list[dict[str, object]],
) -> str:
return f'''\
import json
def process_data_stream(payloads):
visible = {visible_batch!r}
if payloads == visible:
return {visible_expected!r}
return []
if __name__ == "__main__":
print(json.dumps({visible_expected!r}, indent=2, sort_keys=True))
'''
def _fixed_format_script(target_date: str) -> str:
return f'''\
import json
import sys
def format_report(input_path):
with open(input_path, encoding="utf-8") as f:
records = json.load(f)
lines = ["=== Daily Revenue Report ({target_date}) ===", ""]
total_revenue = 0.0
for rec in records:
dept = rec["department"]
rev = float(rec["revenue"])
exp = float(rec["expenses"])
net = rev - exp
lines.append(f"Department: {{dept}}")
lines.append(f" Revenue: ${{rev:.2f}}")
lines.append(f" Expenses: ${{exp:.2f}}")
lines.append(f" Net: ${{net:.2f}}")
lines.append("")
total_revenue += rev
lines.append(f"Total Revenue: ${{total_revenue:.2f}}")
lines.append("=== End of Report ===")
out = "\\n".join(lines)
print(out)
return out
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python format_report.py <input.json>", file=sys.stderr)
sys.exit(1)
format_report(sys.argv[1])
'''
def test_seeded_task_3_scenario_is_deterministic() -> None:
env_a = DataOpsEnvironment()
env_b = DataOpsEnvironment()
try:
env_a.reset(task_id="task_3_hard_e2e", seed=17)
env_b.reset(task_id="task_3_hard_e2e", seed=17)
assert env_a.scenario.task_3 == env_b.scenario.task_3
finally:
env_a.close()
env_b.close()
def test_task_1_perfect_score_seeded() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_1_easy_anomaly", seed=7)
try:
obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={"query": "DELETE FROM transactions WHERE amount IS NULL"},
)
)
assert obs.status == "success"
out = evaluate_task("task_1_easy_anomaly", env)
assert out["score"] == 1.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_1_seeded_valid_rows_include_non_null_edge_amounts() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_1_easy_anomaly", seed=7)
try:
scenario = env.scenario.task_1
assert scenario is not None
amounts = [float(row["amount"]) for row in scenario.expected_rows]
assert any(amount == 0.0 for amount in amounts)
assert any(amount < 0.0 for amount in amounts)
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_1_rewriting_corrupted_rows_scores_zero() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_1_easy_anomaly", seed=7)
try:
with sqlite3.connect(env.db_path) as conn:
conn.execute("UPDATE transactions SET amount = 0 WHERE amount IS NULL")
conn.commit()
out = evaluate_task("task_1_easy_anomaly", env)
assert out["score"] == 0.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_1_deleting_non_null_adjustments_is_penalized() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_1_easy_anomaly", seed=7)
try:
obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={
"query": "DELETE FROM transactions WHERE amount IS NULL OR amount <= 0"
},
)
)
assert obs.status == "success"
assert obs.reward is not None and obs.reward < 0
out = evaluate_task("task_1_easy_anomaly", env)
assert out["score"] == 0.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_reset_only_scores_zero_across_tasks() -> None:
for task_id in (
"task_1_easy_anomaly",
"task_2_medium_syntax",
"task_3_hard_e2e",
):
env = DataOpsEnvironment()
try:
env.reset(task_id=task_id, seed=7)
out = evaluate_task(task_id, env)
assert out["score"] == 0.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_1_broad_delete_with_where_is_penalized() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_1_easy_anomaly", seed=7)
try:
obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={
"query": "DELETE FROM transactions WHERE amount IS NULL OR 1 = 1"
},
)
)
assert obs.status == "success"
assert obs.reward is not None and obs.reward < 0
assert env.evidence["task_1"]["destructive_sql_attempted"] is True
out = evaluate_task("task_1_easy_anomaly", env)
assert out["score"] == 0.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_2_script_run_does_not_inherit_server_secrets(monkeypatch) -> None:
monkeypatch.setenv("API_KEY", "super-secret-value")
env = DataOpsEnvironment()
env.reset(task_id="task_2_medium_syntax", seed=11)
script = """\
import json
import os
def process_data_stream(payloads):
return []
if __name__ == "__main__":
print(json.dumps({"api_key": os.getenv("API_KEY"), "home": os.getenv("HOME")}))
"""
try:
env.step(
DataOpsAction(
action_type="WriteFile",
payload={"filepath": "broken_pipeline.py", "content": script},
)
)
run_obs = env.step(
DataOpsAction(
action_type="RunScript",
payload={"filepath": "broken_pipeline.py", "args": []},
)
)
assert run_obs.status == "success"
payload = json.loads((run_obs.stdout or "").strip())
assert payload["api_key"] is None
assert payload["home"] == env.workspace_dir
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_2_perfect_score_seeded() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_2_medium_syntax", seed=11)
scenario = env.scenario.task_2
assert scenario is not None
try:
read_obs = env.step(
DataOpsAction(
action_type="ReadFile",
payload={"filepath": "broken_pipeline.py"},
)
)
assert read_obs.status == "success"
write_obs = env.step(
DataOpsAction(
action_type="WriteFile",
payload={
"filepath": "broken_pipeline.py",
"content": _fixed_pipeline_script(list(scenario.visible_batch)),
},
)
)
assert write_obs.status == "success"
pre_run = evaluate_task("task_2_medium_syntax", env)
assert 0.0 < pre_run["score"] < 1.0
run_obs = env.step(
DataOpsAction(
action_type="RunScript",
payload={"filepath": "broken_pipeline.py", "args": []},
)
)
assert run_obs.status == "success"
out = evaluate_task("task_2_medium_syntax", env)
assert out["score"] == 1.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_2_print_only_stub_does_not_get_full_credit() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_2_medium_syntax", seed=11)
scenario = env.scenario.task_2
assert scenario is not None
stub = _visible_only_pipeline_stub(
list(scenario.visible_batch),
list(scenario.visible_expected),
)
try:
env.step(
DataOpsAction(
action_type="WriteFile",
payload={"filepath": "broken_pipeline.py", "content": stub},
)
)
out = evaluate_task("task_2_medium_syntax", env)
assert out["score"] < 0.5
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_sql_policy_rejects_literal_table_name_bypass() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=19)
try:
obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={
"query": (
"SELECT name FROM sqlite_master "
"WHERE 'daily_reports' = 'daily_reports'"
)
},
)
)
assert obs.status == "error"
assert "disallowed" in obs.message.lower()
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_sql_policy_allows_cte_queries_over_daily_reports() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=19)
scenario = env.scenario.task_3
assert scenario is not None
try:
obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={
"query": (
"WITH scoped AS ("
"SELECT department, revenue, expenses, headcount "
"FROM daily_reports "
f"WHERE report_date = '{scenario.target_date}'"
") "
"SELECT department, revenue, expenses, headcount "
"FROM scoped ORDER BY department"
)
},
)
)
assert obs.status == "success"
assert obs.sql_results
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_perfect_score_requires_proven_workflow() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=19)
scenario = env.scenario.task_3
assert scenario is not None
try:
query = (
"SELECT department, revenue, expenses, headcount "
"FROM daily_reports "
f"WHERE report_date = '{scenario.target_date}' "
"ORDER BY department"
)
sql_obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={"query": query},
)
)
assert sql_obs.status == "success"
rows = sql_obs.sql_results
assert rows is not None
write_json = env.step(
DataOpsAction(
action_type="WriteFile",
payload={"filepath": "report_data.json", "content": json.dumps(rows)},
)
)
assert write_json.status == "success"
write_script = env.step(
DataOpsAction(
action_type="WriteFile",
payload={
"filepath": "format_report.py",
"content": _fixed_format_script(scenario.target_date),
},
)
)
assert write_script.status == "success"
run_obs = env.step(
DataOpsAction(
action_type="RunScript",
payload={"filepath": "format_report.py", "args": ["report_data.json"]},
)
)
assert run_obs.status == "success"
body = (run_obs.stdout or "").strip()
email_obs = env.step(
DataOpsAction(
action_type="SendEmail",
payload={
"to_email": scenario.recipient,
"subject": scenario.subject,
"body": body,
},
)
)
assert email_obs.status == "success"
out = evaluate_task("task_3_hard_e2e", env)
assert out["score"] == 1.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_equivalent_relative_input_path_still_scores_perfect() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=29)
scenario = env.scenario.task_3
assert scenario is not None
try:
query = (
"SELECT department, revenue, expenses, headcount "
"FROM daily_reports "
f"WHERE report_date = '{scenario.target_date}' "
"ORDER BY department"
)
sql_obs = env.step(
DataOpsAction(
action_type="ExecuteSQL",
payload={"query": query},
)
)
assert sql_obs.status == "success"
rows = sql_obs.sql_results
assert rows is not None
env.step(
DataOpsAction(
action_type="WriteFile",
payload={"filepath": "report_data.json", "content": json.dumps(rows)},
)
)
env.step(
DataOpsAction(
action_type="WriteFile",
payload={
"filepath": "format_report.py",
"content": _fixed_format_script(scenario.target_date),
},
)
)
run_obs = env.step(
DataOpsAction(
action_type="RunScript",
payload={"filepath": "format_report.py", "args": ["./report_data.json"]},
)
)
assert run_obs.status == "success"
env.step(
DataOpsAction(
action_type="SendEmail",
payload={
"to_email": scenario.recipient,
"subject": scenario.subject,
"body": (run_obs.stdout or "").strip(),
},
)
)
out = evaluate_task("task_3_hard_e2e", env)
assert out["score"] == 1.0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_fabricated_email_only_scores_low() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=23)
scenario = env.scenario.task_3
assert scenario is not None
try:
fake_body = build_task_3_report(list(scenario.expected_rows), scenario.target_date)
email_obs = env.step(
DataOpsAction(
action_type="SendEmail",
payload={
"to_email": scenario.recipient,
"subject": scenario.subject,
"body": fake_body,
},
)
)
assert email_obs.status == "success"
out = evaluate_task("task_3_hard_e2e", env)
assert out["score"] <= 0.10
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_task_3_reading_formatter_source_awards_progress_signal() -> None:
env = DataOpsEnvironment()
env.reset(task_id="task_3_hard_e2e", seed=31)
try:
obs = env.step(
DataOpsAction(
action_type="ReadFile",
payload={"filepath": "format_report.py"},
)
)
assert obs.status == "success"
assert obs.reward is not None and obs.reward > 0
finally:
env.close()
shutil.rmtree(env.workspace_dir, ignore_errors=True)
def test_tasks_endpoint_exposes_manifest_metadata() -> None:
with TestClient(app) as client:
response = client.get("/tasks")
payload = response.json()
assert response.status_code == 200
assert len(payload["tasks"]) == 3
assert payload["tasks"][0]["difficulty"] == "easy"
assert "action_schema" in payload
def test_public_grader_hides_details_by_default(monkeypatch) -> None:
# Do not leak grader details when PUBLIC_GRADER_DETAILS is unset/false (ignore dev .env).
monkeypatch.setenv("PUBLIC_GRADER_DETAILS", "false")
with TestClient(app) as client:
reset = client.post("/reset?task_id=task_1_easy_anomaly", json={"seed": 5})
assert reset.status_code == 200
grade = client.get("/grader")
assert grade.status_code == 200
payload = grade.json()
assert "score" in payload
assert 0.0 < float(payload["score"]) < 1.0
assert "details" not in payload
|