Spaces:
Sleeping
Sleeping
File size: 10,865 Bytes
ff84a08 147b2e1 ff84a08 6d75c2b 5d46764 6d75c2b 88c262b 6d75c2b 88c262b 6d75c2b 88c262b 6d75c2b 88c262b 6d75c2b ff84a08 b213994 a8a9661 b213994 a8a9661 88c262b 5d46764 3ec25ed a8a9661 5d46764 ff84a08 5d46764 ff84a08 147b2e1 ff84a08 bbd9ae1 ff84a08 147b2e1 ff84a08 88c262b ff84a08 a8a9661 b213994 88c262b 5d46764 ff84a08 147b2e1 ff84a08 | 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 | from __future__ import annotations
import json
import os
import subprocess
import sys
from pathlib import Path
from fastapi.testclient import TestClient
import math
from app.grader import grade_review, grader, normalize_score
from app.main import app
from app.tasks import TASKS
def _check_openenv_yaml(path: Path) -> None:
content = path.read_text(encoding="utf-8")
required = ["spec_version:", "name:", "type:", "runtime:", "app:", "port:"]
missing = [key for key in required if key not in content]
if missing:
raise AssertionError(f"openenv.yaml missing keys: {missing}")
try:
import yaml
except ImportError as exc: # pragma: no cover
raise AssertionError("PyYAML is required to validate openenv.yaml structure") from exc
data = yaml.safe_load(content)
if not isinstance(data, dict):
raise AssertionError("openenv.yaml must parse to a mapping")
tasks = data.get("tasks")
if not isinstance(tasks, list) or len(tasks) < 3:
raise AssertionError("openenv.yaml must include a tasks: list with at least 3 entries")
tasks_with_grader = sum(
1
for t in tasks
if isinstance(t, dict)
and (t.get("grader") is True or t.get("has_grader") is True)
)
grading = data.get("grading")
nested_graders: list = []
if isinstance(grading, dict):
g = grading.get("graders")
if isinstance(g, list):
nested_graders = g
root_graders = data.get("graders")
root_graders_len = len(root_graders) if isinstance(root_graders, list) else 0
grader_slots = max(tasks_with_grader, len(nested_graders), root_graders_len)
if grader_slots < 3:
raise AssertionError(
"openenv.yaml must declare graders for at least 3 tasks "
"(each task: grader: true, top-level graders:, and/or grading.graders)"
)
def _check_openapi_reward_schema() -> None:
"""Synapse-style checks may scan OpenAPI for 0.0/1.0 on score bounds (see Reward model)."""
client = TestClient(app)
r = client.get("/openapi.json")
assert r.status_code == 200
raw = json.dumps(r.json())
# Reject the old Pydantic pattern that embeds 0.0/1.0 as exclusive bounds on score.
assert '"exclusiveMinimum": 0.0' not in raw and '"exclusiveMaximum": 1.0' not in raw, (
"OpenAPI must not expose exclusive bounds 0.0/1.0 on scores (hub static validation)"
)
data = r.json()
reward = data["components"]["schemas"]["Reward"]["properties"]["score"]
assert reward.get("minimum") == 0.01 and reward.get("maximum") == 0.99
def _check_graders_json(path: Path) -> None:
assert path.is_file(), "graders.json is required at repository root for hub scanners"
data = json.loads(path.read_text(encoding="utf-8"))
assert data.get("tasks_with_graders", 0) >= 3
graders = data.get("graders")
assert isinstance(graders, list) and len(graders) >= 3
enabled = sum(1 for g in graders if isinstance(g, dict) and g.get("enabled") is True)
assert enabled >= 3, "graders.json must list at least 3 enabled graders"
def _check_task_definitions_have_grader() -> None:
for task_id, task in TASKS.items():
assert task.has_grader is True, f"task {task_id} must have has_grader=True"
def _check_metadata_graders() -> None:
client = TestClient(app)
response = client.get("/metadata")
assert response.status_code == 200, f"/metadata returned {response.status_code}"
payload = response.json()
tasks = payload.get("tasks")
assert isinstance(tasks, list) and len(tasks) >= 3, "metadata.tasks must list at least 3 tasks"
graded = sum(
1
for t in tasks
if isinstance(t, dict) and (t.get("has_grader") is True or t.get("grader_enabled") is True)
)
assert graded >= 3, "metadata must expose at least 3 tasks with graders enabled"
def _check_tasks_endpoint() -> None:
client = TestClient(app)
response = client.get("/tasks")
assert response.status_code == 200, f"/tasks returned {response.status_code}"
payload = response.json()
task_list = payload.get("tasks")
assert isinstance(task_list, list) and len(task_list) >= 3, "/tasks must list at least 3 tasks"
n = sum(
1
for t in task_list
if isinstance(t, dict)
and t.get("has_grader") is True
and isinstance(t.get("grader"), dict)
and t["grader"].get("enabled") is True
)
assert n >= 3, "/tasks must declare enabled deterministic graders for at least 3 tasks"
manifest = client.get("/openenv.yaml")
assert manifest.status_code == 200, f"/openenv.yaml returned {manifest.status_code}"
assert b"has_grader:" in manifest.content or b"grader:" in manifest.content
def _floats_bad(obj: object) -> list[float]:
bad: list[float] = []
if isinstance(obj, float) and (obj == 0.0 or obj == 1.0):
bad.append(obj)
elif isinstance(obj, dict):
for v in obj.values():
bad.extend(_floats_bad(v))
elif isinstance(obj, list):
for item in obj:
bad.extend(_floats_bad(item))
return bad
for t in task_list:
if isinstance(t, dict) and _floats_bad(t):
raise AssertionError(
f"/tasks task payload must not contain 0.0 or 1.0 floats (strict grader rules): {t}"
)
gm = client.get("/graders.json")
assert gm.status_code == 200, f"/graders.json returned {gm.status_code}"
gdata = gm.json()
assert len(gdata.get("graders", [])) >= 3
def _check_api_contracts() -> None:
client = TestClient(app)
health = client.get("/health")
assert health.status_code == 200, f"/health returned {health.status_code}"
reset = client.post("/reset", json={})
assert reset.status_code == 200, f"/reset returned {reset.status_code}"
obs = reset.json()["observation"]
assert "task_id" in obs and "instructions" in obs and "code" in obs
step = client.post(
"/step",
json={
"action": {
"review": (
"There is a syntax error with a missing colon. "
"Fix to def add_numbers(a, b):"
)
}
},
)
assert step.status_code == 200, f"/step returned {step.status_code}"
reward = step.json().get("reward") or {}
score = float(reward.get("score", -1.0))
assert 0.0 < score < 1.0, f"/step reward score must be strictly in (0, 1), got {reward.get('score')}"
state = client.get("/state")
assert state.status_code == 200, f"/state returned {state.status_code}"
def _check_tasks_and_graders() -> None:
assert len(TASKS) >= 3, "At least 3 tasks are required"
sample_reviews = {
"easy": "Syntax error due to missing colon after function definition. Use def add_numbers(a, b):",
"medium": "Use a list comprehension while preserving behavior for readability and concise code.",
"hard": (
"Handle empty list to avoid division by zero, avoid broad except Exception, "
"iterate directly, add type hints/tests, overall score: 0.85 (on a 0–1 scale)."
),
}
for task_id, task in TASKS.items():
review = sample_reviews.get(task_id, "Provide relevant code review feedback with score.")
result = grade_review(task, review)
assert 0.0 < result.score < 1.0, f"Score out of strict range for {task_id}: {result.score}"
def _check_grader_edge_cases() -> None:
"""Stress grader outputs: no NaN/inf, scores never touch 0.0 or 1.0, grader() matches rubric."""
assert normalize_score(float("nan")) == 0.01
assert normalize_score(float("inf")) == 0.01
assert normalize_score(0.0) == 0.01
assert normalize_score(1.0) == 0.99
assert normalize_score(1.5) == 0.99
junk = (
"weather recipe movie travel football politics unrelated filler text "
"with enough tokens to avoid the too_short penalty entirely here"
)
emptyish = "a " * 30
for _task_id, task in TASKS.items():
for review in (junk, emptyish, "syntax error " * 20):
result = grade_review(task, review)
s = float(result.score)
assert math.isfinite(s), f"non-finite score for {task.id}: {s}"
assert 0.0 < s < 1.0, f"score out of range for {task.id}: {s}"
assert grader(review, task) == s
def _check_inference_script(path: Path) -> None:
assert path.exists(), "Root inference.py is required"
content = path.read_text(encoding="utf-8")
required_tokens = [
"API_BASE_URL",
"MODEL_NAME",
"HF_TOKEN",
"OpenAI(",
'"START"',
'"STEP"',
'"END"',
]
missing = [token for token in required_tokens if token not in content]
if missing:
raise AssertionError(f"inference.py missing required tokens: {missing}")
def _run_inference_dry_run(root: Path) -> None:
env = os.environ.copy()
env.setdefault("API_BASE_URL", "https://api.openai.com/v1")
env.setdefault("MODEL_NAME", "gpt-4o-mini")
env.setdefault("HF_TOKEN", "dummy-token-for-dry-run")
env.setdefault("ENV_BASE_URL", "inprocess")
env["DRY_RUN"] = "1"
proc = subprocess.run(
[sys.executable, "inference.py"],
cwd=str(root),
env=env,
capture_output=True,
text=True,
timeout=120,
check=False,
)
if proc.returncode != 0:
raise AssertionError(f"inference.py dry run failed: {proc.stderr or proc.stdout}")
lines = [line.strip() for line in proc.stdout.splitlines() if line.strip()]
assert any(line.startswith("[START]") for line in lines), "Missing [START] log"
assert sum(1 for line in lines if line.startswith("[STEP]")) >= 3, "Expected >=3 [STEP] logs"
assert any(line.startswith("[END]") for line in lines), "Missing [END] log"
for line in lines:
if not line.startswith("[STEP]"):
continue
payload_text = line.split("]", 1)[1].strip()
step_payload = json.loads(payload_text)
score = float(step_payload["score"])
assert 0.0 < score < 1.0, f"inference STEP score must be strictly in (0, 1), got {score}"
def main() -> None:
root = Path(__file__).resolve().parent
_check_openenv_yaml(root / "openenv.yaml")
_check_graders_json(root / "graders.json")
_check_task_definitions_have_grader()
_check_openapi_reward_schema()
_check_metadata_graders()
_check_tasks_endpoint()
_check_api_contracts()
_check_tasks_and_graders()
_check_grader_edge_cases()
_check_inference_script(root / "inference.py")
_run_inference_dry_run(root)
print(json.dumps({"status": "ok", "message": "Pre-submission validation passed."}))
if __name__ == "__main__":
main()
|