Spaces:
Sleeping
Sleeping
Commit ·
1abf21a
1
Parent(s): d360475
debugged again
Browse files- debug_api.py +57 -0
- debug_check.py +56 -0
- debug_phase2.py +152 -0
debug_api.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Deep API endpoint checks to diagnose hub phase 2 failures."""
|
| 2 |
+
import json
|
| 3 |
+
from fastapi.testclient import TestClient
|
| 4 |
+
from app.main import app
|
| 5 |
+
|
| 6 |
+
client = TestClient(app)
|
| 7 |
+
|
| 8 |
+
# /metadata check
|
| 9 |
+
r = client.get("/metadata")
|
| 10 |
+
data = r.json()
|
| 11 |
+
print("=== /metadata ===")
|
| 12 |
+
print(f"task_count: {data['task_count']}")
|
| 13 |
+
print(f"tasks_with_graders: {data['grading']['tasks_with_graders']}")
|
| 14 |
+
print(f"Number of tasks: {len(data['tasks'])}")
|
| 15 |
+
for t in data["tasks"]:
|
| 16 |
+
print(f" {t['task_id']}: has_grader={t['has_grader']}, grader_enabled={t['grader_enabled']}, grader={t['grader']}")
|
| 17 |
+
|
| 18 |
+
# /tasks check
|
| 19 |
+
print()
|
| 20 |
+
r2 = client.get("/tasks")
|
| 21 |
+
data2 = r2.json()
|
| 22 |
+
print("=== /tasks ===")
|
| 23 |
+
for t in data2["tasks"]:
|
| 24 |
+
print(f" {t['task_id']}: has_grader={t['has_grader']}, grader={t['grader']}")
|
| 25 |
+
|
| 26 |
+
# /graders.json check
|
| 27 |
+
print()
|
| 28 |
+
r3 = client.get("/graders.json")
|
| 29 |
+
data3 = r3.json()
|
| 30 |
+
print("=== /graders.json ===")
|
| 31 |
+
print(f"tasks_with_graders: {data3['tasks_with_graders']}")
|
| 32 |
+
print(f"graders count: {len(data3['graders'])}")
|
| 33 |
+
for g in data3["graders"]:
|
| 34 |
+
print(f" {g}")
|
| 35 |
+
|
| 36 |
+
# Full reset + step to see actual score
|
| 37 |
+
print()
|
| 38 |
+
print("=== reset + step (easy task) ===")
|
| 39 |
+
reset = client.post("/reset", json={"task_id": "easy", "seed": 42})
|
| 40 |
+
print(f"reset status: {reset.status_code}")
|
| 41 |
+
|
| 42 |
+
step = client.post("/step", json={"action": {"review": "sql injection parameterized query f-string"}})
|
| 43 |
+
print(f"step status: {step.status_code}")
|
| 44 |
+
step_data = step.json()
|
| 45 |
+
score = step_data["reward"]["score"]
|
| 46 |
+
print(f"score: {score}, type: {type(score)}")
|
| 47 |
+
print(f"score valid (0<x<1): {0.0 < float(score) < 1.0}")
|
| 48 |
+
|
| 49 |
+
# Check for bad floats
|
| 50 |
+
print()
|
| 51 |
+
print("=== checking for 0.0 or 1.0 in step response ===")
|
| 52 |
+
raw = json.dumps(step_data)
|
| 53 |
+
has_zero = '"score": 0.0' in raw or '"score":0.0' in raw
|
| 54 |
+
has_one = '"score": 1.0' in raw or '"score":1.0' in raw
|
| 55 |
+
print(f"Has 0.0 score literal: {has_zero}")
|
| 56 |
+
print(f"Has 1.0 score literal: {has_one}")
|
| 57 |
+
print(f"Raw score field in JSON: '{json.dumps(step_data['reward']['score'])}'")
|
debug_check.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from app.grader import normalize_score, grade_review
|
| 3 |
+
from app.tasks import TASKS
|
| 4 |
+
|
| 5 |
+
# ---- normalize_score edge cases ----
|
| 6 |
+
print("=== normalize_score edge cases ===")
|
| 7 |
+
print(f"normalize_score(nan) = {normalize_score(float('nan'))}")
|
| 8 |
+
print(f"normalize_score(inf) = {normalize_score(float('inf'))}")
|
| 9 |
+
print(f"normalize_score(-inf) = {normalize_score(float('-inf'))}")
|
| 10 |
+
print(f"normalize_score(0.0) = {normalize_score(0.0)}")
|
| 11 |
+
print(f"normalize_score(1.0) = {normalize_score(1.0)}")
|
| 12 |
+
print(f"normalize_score(1.5) = {normalize_score(1.5)}")
|
| 13 |
+
print(f"normalize_score(0.5) = {normalize_score(0.5)}")
|
| 14 |
+
|
| 15 |
+
# ---- coverage=1.0 edge case ----
|
| 16 |
+
print()
|
| 17 |
+
print("=== coverage=1.0 edge case ===")
|
| 18 |
+
task = TASKS["easy"]
|
| 19 |
+
full_review = "sql injection parameterized query f-string interpolation prepared statement"
|
| 20 |
+
result = grade_review(task, full_review)
|
| 21 |
+
raw_coverage = len(result.matched_criteria) / max(len(task.criteria), 1)
|
| 22 |
+
print(f"Easy full-match: score={result.score}, matched={len(result.matched_criteria)}/{len(task.criteria)}, raw_coverage={raw_coverage}")
|
| 23 |
+
|
| 24 |
+
# ---- Empty review ----
|
| 25 |
+
print()
|
| 26 |
+
print("=== Empty / minimal review ===")
|
| 27 |
+
for label, rev in [("empty", ""), ("single char", "a"), ("30 spaces", " " * 30)]:
|
| 28 |
+
try:
|
| 29 |
+
r = grade_review(task, rev)
|
| 30 |
+
print(f" [{label}] score={r.score} finite={math.isfinite(r.score)}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f" [{label}] raised: {e}")
|
| 33 |
+
|
| 34 |
+
# ---- Extreme reviews on all tasks ----
|
| 35 |
+
print()
|
| 36 |
+
print("=== All tasks, extreme inputs ===")
|
| 37 |
+
junk = ("weather recipe movie travel football politics unrelated filler text "
|
| 38 |
+
"with enough tokens to avoid the too_short penalty entirely here")
|
| 39 |
+
emptyish = "a " * 30
|
| 40 |
+
inputs = {"junk": junk, "emptyish": emptyish, "syntax_error_repeat": "syntax error " * 20}
|
| 41 |
+
for task_id, task in TASKS.items():
|
| 42 |
+
for label, rev in inputs.items():
|
| 43 |
+
r = grade_review(task, rev)
|
| 44 |
+
s = float(r.score)
|
| 45 |
+
ok = math.isfinite(s) and 0.0 < s < 1.0
|
| 46 |
+
print(f" task={task_id} input={label} score={s} OK={ok}")
|
| 47 |
+
|
| 48 |
+
# ---- Validator's own check ----
|
| 49 |
+
print()
|
| 50 |
+
print("=== Validator normalize_score assertions ===")
|
| 51 |
+
assert normalize_score(float("nan")) == 0.99, f"FAIL: {normalize_score(float('nan'))}"
|
| 52 |
+
assert normalize_score(float("inf")) == 0.99, f"FAIL: {normalize_score(float('inf'))}"
|
| 53 |
+
assert normalize_score(0.0) == 0.01, f"FAIL: {normalize_score(0.0)}"
|
| 54 |
+
assert normalize_score(1.0) == 0.99, f"FAIL: {normalize_score(1.0)}"
|
| 55 |
+
assert normalize_score(1.5) == 0.99, f"FAIL: {normalize_score(1.5)}"
|
| 56 |
+
print("All assertions passed!")
|
debug_phase2.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Exhaustive Phase 2 simulation - run every check the hub might run.
|
| 3 |
+
"""
|
| 4 |
+
import math
|
| 5 |
+
import json
|
| 6 |
+
from fastapi.testclient import TestClient
|
| 7 |
+
from app.grader import grade_review, grader, normalize_score
|
| 8 |
+
from app.main import app
|
| 9 |
+
from app.tasks import TASKS
|
| 10 |
+
|
| 11 |
+
ALL_PASS = True
|
| 12 |
+
|
| 13 |
+
def check(name, cond, detail=""):
|
| 14 |
+
global ALL_PASS
|
| 15 |
+
status = "PASS" if cond else "FAIL"
|
| 16 |
+
if not cond:
|
| 17 |
+
ALL_PASS = False
|
| 18 |
+
print(f" [{status}] {name}" + (f" | {detail}" if detail else ""))
|
| 19 |
+
return cond
|
| 20 |
+
|
| 21 |
+
print("=== 1. normalize_score edge cases ===")
|
| 22 |
+
check("nan -> 0.99", normalize_score(float("nan")) == 0.99, str(normalize_score(float("nan"))))
|
| 23 |
+
check("inf -> 0.99", normalize_score(float("inf")) == 0.99, str(normalize_score(float("inf"))))
|
| 24 |
+
check("-inf -> 0.01", normalize_score(float("-inf")) == 0.01, str(normalize_score(float("-inf"))))
|
| 25 |
+
check("0.0 -> 0.01", normalize_score(0.0) == 0.01, str(normalize_score(0.0)))
|
| 26 |
+
check("1.0 -> 0.99", normalize_score(1.0) == 0.99, str(normalize_score(1.0)))
|
| 27 |
+
check("1.5 -> 0.99", normalize_score(1.5) == 0.99, str(normalize_score(1.5)))
|
| 28 |
+
check("0.5 -> 0.5", normalize_score(0.5) == 0.5, str(normalize_score(0.5)))
|
| 29 |
+
|
| 30 |
+
print()
|
| 31 |
+
print("=== 2. Task count and grader presence ===")
|
| 32 |
+
check(">=3 tasks", len(TASKS) >= 3, str(len(TASKS)))
|
| 33 |
+
for tid, task in TASKS.items():
|
| 34 |
+
check(f"{tid} has_grader=True", task.has_grader is True)
|
| 35 |
+
|
| 36 |
+
print()
|
| 37 |
+
print("=== 3. Scoring with grader function ===")
|
| 38 |
+
sample_reviews = {
|
| 39 |
+
"easy": "This uses an f-string leading to a sql injection. Use a parameterized query instead.",
|
| 40 |
+
"medium": "Race condition. The await allows interleaving. Use an async lock to make it atomic.",
|
| 41 |
+
"hard": (
|
| 42 |
+
"Blocking requests.post call in an async def function. Handle exceptions properly and "
|
| 43 |
+
"avoid synchronous file i/o. Extract hardcoded urls. Overall score: 0.85 (on a 0-1 scale)."
|
| 44 |
+
),
|
| 45 |
+
}
|
| 46 |
+
for task_id, task in TASKS.items():
|
| 47 |
+
review = sample_reviews.get(task_id, "Provide relevant code review feedback with score.")
|
| 48 |
+
result = grade_review(task, review)
|
| 49 |
+
s = float(result.score)
|
| 50 |
+
check(f"{task_id} score in (0,1)", 0.0 < s < 1.0, str(s))
|
| 51 |
+
check(f"{task_id} score is finite", math.isfinite(s), str(s))
|
| 52 |
+
|
| 53 |
+
print()
|
| 54 |
+
print("=== 4. Grader edge cases (all tasks, all junk inputs) ===")
|
| 55 |
+
junk = (
|
| 56 |
+
"weather recipe movie travel football politics unrelated filler text "
|
| 57 |
+
"with enough tokens to avoid the too_short penalty entirely here"
|
| 58 |
+
)
|
| 59 |
+
emptyish = "a " * 30
|
| 60 |
+
for task_id, task in TASKS.items():
|
| 61 |
+
for label, rev in [("junk", junk), ("emptyish", emptyish), ("syntax_error_repeat", "syntax error " * 20)]:
|
| 62 |
+
result = grade_review(task, rev)
|
| 63 |
+
s = float(result.score)
|
| 64 |
+
check(f"{task_id}/{label} finite", math.isfinite(s), str(s))
|
| 65 |
+
check(f"{task_id}/{label} in (0,1)", 0.0 < s < 1.0, str(s))
|
| 66 |
+
gs = grader(rev, task)
|
| 67 |
+
check(f"{task_id}/{label} grader==grade_review", gs == s, f"grader={gs} grade_review={s}")
|
| 68 |
+
|
| 69 |
+
print()
|
| 70 |
+
print("=== 5. API contract checks ===")
|
| 71 |
+
client = TestClient(app)
|
| 72 |
+
|
| 73 |
+
r = client.get("/health")
|
| 74 |
+
check("/health 200", r.status_code == 200, str(r.status_code))
|
| 75 |
+
|
| 76 |
+
r = client.get("/metadata")
|
| 77 |
+
data = r.json()
|
| 78 |
+
check("/metadata 200", r.status_code == 200)
|
| 79 |
+
check("metadata tasks >= 3", len(data.get("tasks", [])) >= 3, str(len(data.get("tasks", []))))
|
| 80 |
+
graded = sum(1 for t in data["tasks"] if t.get("has_grader") is True or t.get("grader_enabled") is True)
|
| 81 |
+
check("metadata graded >= 3", graded >= 3, str(graded))
|
| 82 |
+
with_grader_obj = sum(1 for t in data["tasks"] if isinstance(t.get("grader"), dict) and t["grader"].get("enabled") is True)
|
| 83 |
+
check("metadata grader.enabled >= 3", with_grader_obj >= 3, str(with_grader_obj))
|
| 84 |
+
meta_graders = data.get("graders", [])
|
| 85 |
+
check("metadata.graders >= 3", len(meta_graders) >= 3, str(len(meta_graders)))
|
| 86 |
+
enabled_meta = sum(1 for g in meta_graders if isinstance(g, dict) and g.get("enabled") is True)
|
| 87 |
+
check("metadata.graders enabled >= 3", enabled_meta >= 3, str(enabled_meta))
|
| 88 |
+
nested = (data.get("grading") or {}).get("graders", [])
|
| 89 |
+
check("metadata.grading.graders >= 3", len(nested) >= 3, str(len(nested)))
|
| 90 |
+
nested_enabled = sum(1 for g in nested if isinstance(g, dict) and g.get("enabled") is True)
|
| 91 |
+
check("metadata.grading.graders enabled >= 3", nested_enabled >= 3, str(nested_enabled))
|
| 92 |
+
|
| 93 |
+
r2 = client.get("/tasks")
|
| 94 |
+
tdata = r2.json()
|
| 95 |
+
check("/tasks 200", r2.status_code == 200)
|
| 96 |
+
tlist = tdata.get("tasks", [])
|
| 97 |
+
check("/tasks count >= 3", len(tlist) >= 3, str(len(tlist)))
|
| 98 |
+
n = sum(1 for t in tlist if t.get("has_grader") is True and isinstance(t.get("grader"), dict) and t["grader"].get("enabled") is True)
|
| 99 |
+
check("/tasks enabled deterministic graders >= 3", n >= 3, str(n))
|
| 100 |
+
for t in tlist:
|
| 101 |
+
if isinstance(t, dict):
|
| 102 |
+
raw = json.dumps(t)
|
| 103 |
+
has_bad_float = '"score":0.0' in raw or '"score": 0.0' in raw or '"score":1.0' in raw or '"score": 1.0' in raw
|
| 104 |
+
check(f"/tasks/{t.get('task_id')} no 0.0/1.0 float", not has_bad_float, raw[:100])
|
| 105 |
+
|
| 106 |
+
r3 = client.get("/graders.json")
|
| 107 |
+
gdata = r3.json()
|
| 108 |
+
check("/graders.json >= 3 graders", len(gdata.get("graders", [])) >= 3, str(len(gdata.get("graders", []))))
|
| 109 |
+
enabled_g = sum(1 for g in gdata.get("graders", []) if isinstance(g, dict) and g.get("enabled") is True)
|
| 110 |
+
check("/graders.json enabled >= 3", enabled_g >= 3, str(enabled_g))
|
| 111 |
+
|
| 112 |
+
print()
|
| 113 |
+
print("=== 6. Reset + Step flow ===")
|
| 114 |
+
reset_r = client.post("/reset", json={})
|
| 115 |
+
check("/reset 200", reset_r.status_code == 200)
|
| 116 |
+
obs = reset_r.json()["observation"]
|
| 117 |
+
check("observation has task_id", "task_id" in obs)
|
| 118 |
+
check("observation has code", "code" in obs)
|
| 119 |
+
check("observation has instructions", "instructions" in obs)
|
| 120 |
+
|
| 121 |
+
step_r = client.post("/step", json={"action": {"review": "There is an sql injection vulnerability. Fix using a parameterized query."}})
|
| 122 |
+
check("/step 200", step_r.status_code == 200)
|
| 123 |
+
sbody = step_r.json()
|
| 124 |
+
raw_step = json.dumps(sbody)
|
| 125 |
+
bad_floats = any(x in raw_step for x in ['"score":0.0', '"score": 0.0', '"score":1.0', '"score": 1.0'])
|
| 126 |
+
check("/step no 0.0/1.0 float", not bad_floats)
|
| 127 |
+
reward = sbody.get("reward", {})
|
| 128 |
+
score_val = float(reward.get("score", -1.0))
|
| 129 |
+
check("/step score in (0,1)", 0.0 < score_val < 1.0, str(score_val))
|
| 130 |
+
|
| 131 |
+
state_r = client.get("/state")
|
| 132 |
+
check("/state 200", state_r.status_code == 200)
|
| 133 |
+
state_body = state_r.json()
|
| 134 |
+
raw_state = json.dumps(state_body)
|
| 135 |
+
bad_state = any(x in raw_state for x in ['"score":0.0', '"score": 0.0', '"score":1.0', '"score": 1.0'])
|
| 136 |
+
check("/state no 0.0/1.0 float", not bad_state)
|
| 137 |
+
|
| 138 |
+
print()
|
| 139 |
+
print("=== 7. OpenAPI schema ===")
|
| 140 |
+
oa_r = client.get("/openapi.json")
|
| 141 |
+
raw_oa = json.dumps(oa_r.json())
|
| 142 |
+
check("no exclusiveMinimum 0.0", '"exclusiveMinimum": 0.0' not in raw_oa)
|
| 143 |
+
check("no exclusiveMaximum 1.0", '"exclusiveMaximum": 1.0' not in raw_oa)
|
| 144 |
+
reward_schema = oa_r.json()["components"]["schemas"]["Reward"]["properties"]["score"]
|
| 145 |
+
check("Reward.score minimum=0.01", reward_schema.get("minimum") == 0.01, str(reward_schema))
|
| 146 |
+
check("Reward.score maximum=0.99", reward_schema.get("maximum") == 0.99, str(reward_schema))
|
| 147 |
+
|
| 148 |
+
print()
|
| 149 |
+
if ALL_PASS:
|
| 150 |
+
print("ALL CHECKS PASSED")
|
| 151 |
+
else:
|
| 152 |
+
print("SOME CHECKS FAILED")
|