Spaces:
Sleeping
Sleeping
Commit ·
2da7963
1
Parent(s): 147b2e1
debugged the script
Browse files- app/__pycache__/grader.cpython-313.pyc +0 -0
- app/__pycache__/main.cpython-313.pyc +0 -0
- app/main.py +51 -34
- graders.json +21 -3
- openenv.yaml +16 -4
- validate_submission.py +34 -13
app/__pycache__/grader.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/grader.cpython-313.pyc and b/app/__pycache__/grader.cpython-313.pyc differ
|
|
|
app/__pycache__/main.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-313.pyc and b/app/__pycache__/main.cpython-313.pyc differ
|
|
|
app/main.py
CHANGED
|
@@ -14,13 +14,13 @@ from app.models import (
|
|
| 14 |
StateResponse,
|
| 15 |
StepRequest,
|
| 16 |
StepResult,
|
|
|
|
| 17 |
)
|
| 18 |
-
from app.tasks import TASKS
|
| 19 |
|
| 20 |
# Repo root (contains openenv.yaml) — /app in Docker, project dir locally.
|
| 21 |
_ENV_ROOT = Path(__file__).resolve().parent.parent
|
| 22 |
_OPENENV_PATH = _ENV_ROOT / "openenv.yaml"
|
| 23 |
-
_GRADERS_JSON_PATH = _ENV_ROOT / "graders.json"
|
| 24 |
|
| 25 |
app = FastAPI(
|
| 26 |
title="Synapse Code Auditor",
|
|
@@ -31,6 +31,44 @@ app = FastAPI(
|
|
| 31 |
env = AICodeReviewEnvironment()
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
@app.get("/health")
|
| 35 |
def health() -> dict[str, str]:
|
| 36 |
return {"status": "healthy", "service": "synapse-code-auditor"}
|
|
@@ -48,25 +86,22 @@ def robots() -> PlainTextResponse:
|
|
| 48 |
|
| 49 |
@app.get("/metadata")
|
| 50 |
def metadata() -> dict[str, Any]:
|
|
|
|
| 51 |
return {
|
| 52 |
"name": app.title,
|
| 53 |
"description": app.description,
|
| 54 |
"version": app.version,
|
| 55 |
"task_count": len(TASKS),
|
| 56 |
-
"tasks":
|
| 57 |
-
|
| 58 |
-
"task_id": task.id,
|
| 59 |
-
"task_type": task.task_type.value,
|
| 60 |
-
"has_grader": task.has_grader,
|
| 61 |
-
"grader_enabled": task.has_grader,
|
| 62 |
-
"criteria_count": len(task.criteria),
|
| 63 |
-
}
|
| 64 |
-
for task in TASKS.values()
|
| 65 |
-
],
|
| 66 |
"grading": {
|
| 67 |
"type": "automated",
|
| 68 |
"deterministic": True,
|
| 69 |
"tasks_with_graders": len(TASKS),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
},
|
| 71 |
}
|
| 72 |
|
|
@@ -76,32 +111,14 @@ def list_tasks() -> dict[str, Any]:
|
|
| 76 |
"""Task catalog with grader flags (used by hub validators and OpenEnv clients)."""
|
| 77 |
return {
|
| 78 |
"task_count": len(TASKS),
|
| 79 |
-
"tasks": [
|
| 80 |
-
{
|
| 81 |
-
"task_id": task.id,
|
| 82 |
-
"task_type": task.task_type.value,
|
| 83 |
-
"title": task.title,
|
| 84 |
-
"has_grader": task.has_grader,
|
| 85 |
-
"grader": {"type": "deterministic", "enabled": True},
|
| 86 |
-
# Use numeric bounds strictly inside (0, 1); some validators reject
|
| 87 |
-
# any 0.0/1.0 floats anywhere under task payloads.
|
| 88 |
-
"score_range": [0.01, 0.99],
|
| 89 |
-
}
|
| 90 |
-
for task in TASKS.values()
|
| 91 |
-
],
|
| 92 |
}
|
| 93 |
|
| 94 |
|
| 95 |
@app.get("/graders.json")
|
| 96 |
-
def graders_manifest() ->
|
| 97 |
-
"""
|
| 98 |
-
|
| 99 |
-
raise HTTPException(status_code=404, detail="graders.json not found in environment root")
|
| 100 |
-
return FileResponse(
|
| 101 |
-
_GRADERS_JSON_PATH,
|
| 102 |
-
media_type="application/json",
|
| 103 |
-
filename="graders.json",
|
| 104 |
-
)
|
| 105 |
|
| 106 |
|
| 107 |
@app.get("/openenv.yaml")
|
|
|
|
| 14 |
StateResponse,
|
| 15 |
StepRequest,
|
| 16 |
StepResult,
|
| 17 |
+
TaskDefinition,
|
| 18 |
)
|
| 19 |
+
from app.tasks import ORDERED_TASK_IDS, TASKS
|
| 20 |
|
| 21 |
# Repo root (contains openenv.yaml) — /app in Docker, project dir locally.
|
| 22 |
_ENV_ROOT = Path(__file__).resolve().parent.parent
|
| 23 |
_OPENENV_PATH = _ENV_ROOT / "openenv.yaml"
|
|
|
|
| 24 |
|
| 25 |
app = FastAPI(
|
| 26 |
title="Synapse Code Auditor",
|
|
|
|
| 31 |
env = AICodeReviewEnvironment()
|
| 32 |
|
| 33 |
|
| 34 |
+
def _task_catalog_entry(task: TaskDefinition) -> dict[str, Any]:
|
| 35 |
+
"""Single shape for /metadata and /tasks (hub Phase-2 often counts graders on metadata)."""
|
| 36 |
+
return {
|
| 37 |
+
"task_id": task.id,
|
| 38 |
+
"task_type": task.task_type.value,
|
| 39 |
+
"title": task.title,
|
| 40 |
+
"has_grader": task.has_grader,
|
| 41 |
+
"grader_enabled": task.has_grader,
|
| 42 |
+
"grader": {"type": "deterministic", "enabled": True},
|
| 43 |
+
"score_range": [0.01, 0.99],
|
| 44 |
+
"criteria_count": len(task.criteria),
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _graders_manifest_body() -> dict[str, Any]:
|
| 49 |
+
"""Always in sync with TASKS; includes aliases some hub scanners expect."""
|
| 50 |
+
return {
|
| 51 |
+
"spec_version": 1,
|
| 52 |
+
"environment": "synapse_code_auditor",
|
| 53 |
+
"tasks_with_graders": len(TASKS),
|
| 54 |
+
"graders": [
|
| 55 |
+
{
|
| 56 |
+
"task_id": tid,
|
| 57 |
+
"task": tid,
|
| 58 |
+
"type": "deterministic",
|
| 59 |
+
"kind": "deterministic",
|
| 60 |
+
"enabled": True,
|
| 61 |
+
}
|
| 62 |
+
for tid in ORDERED_TASK_IDS
|
| 63 |
+
],
|
| 64 |
+
"score_policy": {
|
| 65 |
+
"min": 0.01,
|
| 66 |
+
"max": 0.99,
|
| 67 |
+
"strictly_between_zero_and_one": True,
|
| 68 |
+
},
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
@app.get("/health")
|
| 73 |
def health() -> dict[str, str]:
|
| 74 |
return {"status": "healthy", "service": "synapse-code-auditor"}
|
|
|
|
| 86 |
|
| 87 |
@app.get("/metadata")
|
| 88 |
def metadata() -> dict[str, Any]:
|
| 89 |
+
tasks = [_task_catalog_entry(TASKS[tid]) for tid in ORDERED_TASK_IDS]
|
| 90 |
return {
|
| 91 |
"name": app.title,
|
| 92 |
"description": app.description,
|
| 93 |
"version": app.version,
|
| 94 |
"task_count": len(TASKS),
|
| 95 |
+
"tasks": tasks,
|
| 96 |
+
"graders": _graders_manifest_body()["graders"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
"grading": {
|
| 98 |
"type": "automated",
|
| 99 |
"deterministic": True,
|
| 100 |
"tasks_with_graders": len(TASKS),
|
| 101 |
+
"graders": [
|
| 102 |
+
{"task_id": tid, "type": "deterministic", "enabled": True}
|
| 103 |
+
for tid in ORDERED_TASK_IDS
|
| 104 |
+
],
|
| 105 |
},
|
| 106 |
}
|
| 107 |
|
|
|
|
| 111 |
"""Task catalog with grader flags (used by hub validators and OpenEnv clients)."""
|
| 112 |
return {
|
| 113 |
"task_count": len(TASKS),
|
| 114 |
+
"tasks": [_task_catalog_entry(TASKS[tid]) for tid in ORDERED_TASK_IDS],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
|
| 118 |
@app.get("/graders.json")
|
| 119 |
+
def graders_manifest() -> dict[str, Any]:
|
| 120 |
+
"""Grader manifest derived from TASKS so the running Space cannot drift from code."""
|
| 121 |
+
return _graders_manifest_body()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
@app.get("/openenv.yaml")
|
graders.json
CHANGED
|
@@ -3,9 +3,27 @@
|
|
| 3 |
"environment": "synapse_code_auditor",
|
| 4 |
"tasks_with_graders": 3,
|
| 5 |
"graders": [
|
| 6 |
-
{
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
],
|
| 10 |
"score_policy": {
|
| 11 |
"min": 0.01,
|
|
|
|
| 3 |
"environment": "synapse_code_auditor",
|
| 4 |
"tasks_with_graders": 3,
|
| 5 |
"graders": [
|
| 6 |
+
{
|
| 7 |
+
"task_id": "easy",
|
| 8 |
+
"task": "easy",
|
| 9 |
+
"type": "deterministic",
|
| 10 |
+
"kind": "deterministic",
|
| 11 |
+
"enabled": true
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"task_id": "medium",
|
| 15 |
+
"task": "medium",
|
| 16 |
+
"type": "deterministic",
|
| 17 |
+
"kind": "deterministic",
|
| 18 |
+
"enabled": true
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"task_id": "hard",
|
| 22 |
+
"task": "hard",
|
| 23 |
+
"type": "deterministic",
|
| 24 |
+
"kind": "deterministic",
|
| 25 |
+
"enabled": true
|
| 26 |
+
}
|
| 27 |
],
|
| 28 |
"score_policy": {
|
| 29 |
"min": 0.01,
|
openenv.yaml
CHANGED
|
@@ -12,6 +12,7 @@ description: >
|
|
| 12 |
|
| 13 |
tasks:
|
| 14 |
- id: easy
|
|
|
|
| 15 |
name: Detect syntax errors
|
| 16 |
difficulty: easy
|
| 17 |
max_steps: 1
|
|
@@ -19,6 +20,7 @@ tasks:
|
|
| 19 |
has_grader: true
|
| 20 |
score_range: [0.01, 0.99]
|
| 21 |
- id: medium
|
|
|
|
| 22 |
name: Suggest optimization
|
| 23 |
difficulty: medium
|
| 24 |
max_steps: 1
|
|
@@ -26,6 +28,7 @@ tasks:
|
|
| 26 |
has_grader: true
|
| 27 |
score_range: [0.01, 0.99]
|
| 28 |
- id: hard
|
|
|
|
| 29 |
name: Full code review with scoring
|
| 30 |
difficulty: hard
|
| 31 |
max_steps: 1
|
|
@@ -43,15 +46,18 @@ endpoints:
|
|
| 43 |
step: POST /step
|
| 44 |
state: GET /state
|
| 45 |
|
| 46 |
-
# Top-level graders (
|
| 47 |
graders:
|
| 48 |
-
-
|
|
|
|
| 49 |
enabled: true
|
| 50 |
type: deterministic
|
| 51 |
-
-
|
|
|
|
| 52 |
enabled: true
|
| 53 |
type: deterministic
|
| 54 |
-
-
|
|
|
|
| 55 |
enabled: true
|
| 56 |
type: deterministic
|
| 57 |
|
|
@@ -60,8 +66,14 @@ grading:
|
|
| 60 |
deterministic: true
|
| 61 |
graders:
|
| 62 |
- task_id: easy
|
|
|
|
| 63 |
type: deterministic
|
|
|
|
| 64 |
- task_id: medium
|
|
|
|
| 65 |
type: deterministic
|
|
|
|
| 66 |
- task_id: hard
|
|
|
|
| 67 |
type: deterministic
|
|
|
|
|
|
| 12 |
|
| 13 |
tasks:
|
| 14 |
- id: easy
|
| 15 |
+
task_id: easy
|
| 16 |
name: Detect syntax errors
|
| 17 |
difficulty: easy
|
| 18 |
max_steps: 1
|
|
|
|
| 20 |
has_grader: true
|
| 21 |
score_range: [0.01, 0.99]
|
| 22 |
- id: medium
|
| 23 |
+
task_id: medium
|
| 24 |
name: Suggest optimization
|
| 25 |
difficulty: medium
|
| 26 |
max_steps: 1
|
|
|
|
| 28 |
has_grader: true
|
| 29 |
score_range: [0.01, 0.99]
|
| 30 |
- id: hard
|
| 31 |
+
task_id: hard
|
| 32 |
name: Full code review with scoring
|
| 33 |
difficulty: hard
|
| 34 |
max_steps: 1
|
|
|
|
| 46 |
step: POST /step
|
| 47 |
state: GET /state
|
| 48 |
|
| 49 |
+
# Top-level graders (task_id + task aliases for static hub scanners).
|
| 50 |
graders:
|
| 51 |
+
- task_id: easy
|
| 52 |
+
task: easy
|
| 53 |
enabled: true
|
| 54 |
type: deterministic
|
| 55 |
+
- task_id: medium
|
| 56 |
+
task: medium
|
| 57 |
enabled: true
|
| 58 |
type: deterministic
|
| 59 |
+
- task_id: hard
|
| 60 |
+
task: hard
|
| 61 |
enabled: true
|
| 62 |
type: deterministic
|
| 63 |
|
|
|
|
| 66 |
deterministic: true
|
| 67 |
graders:
|
| 68 |
- task_id: easy
|
| 69 |
+
task: easy
|
| 70 |
type: deterministic
|
| 71 |
+
enabled: true
|
| 72 |
- task_id: medium
|
| 73 |
+
task: medium
|
| 74 |
type: deterministic
|
| 75 |
+
enabled: true
|
| 76 |
- task_id: hard
|
| 77 |
+
task: hard
|
| 78 |
type: deterministic
|
| 79 |
+
enabled: true
|
validate_submission.py
CHANGED
|
@@ -15,6 +15,19 @@ from app.main import app
|
|
| 15 |
from app.tasks import TASKS
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def _check_openenv_yaml(path: Path) -> None:
|
| 19 |
content = path.read_text(encoding="utf-8")
|
| 20 |
required = ["spec_version:", "name:", "type:", "runtime:", "app:", "port:"]
|
|
@@ -102,6 +115,20 @@ def _check_metadata_graders() -> None:
|
|
| 102 |
if isinstance(t, dict) and (t.get("has_grader") is True or t.get("grader_enabled") is True)
|
| 103 |
)
|
| 104 |
assert graded >= 3, "metadata must expose at least 3 tasks with graders enabled"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def _check_tasks_endpoint() -> None:
|
|
@@ -125,18 +152,6 @@ def _check_tasks_endpoint() -> None:
|
|
| 125 |
assert manifest.status_code == 200, f"/openenv.yaml returned {manifest.status_code}"
|
| 126 |
assert b"has_grader:" in manifest.content or b"grader:" in manifest.content
|
| 127 |
|
| 128 |
-
def _floats_bad(obj: object) -> list[float]:
|
| 129 |
-
bad: list[float] = []
|
| 130 |
-
if isinstance(obj, float) and (obj == 0.0 or obj == 1.0):
|
| 131 |
-
bad.append(obj)
|
| 132 |
-
elif isinstance(obj, dict):
|
| 133 |
-
for v in obj.values():
|
| 134 |
-
bad.extend(_floats_bad(v))
|
| 135 |
-
elif isinstance(obj, list):
|
| 136 |
-
for item in obj:
|
| 137 |
-
bad.extend(_floats_bad(item))
|
| 138 |
-
return bad
|
| 139 |
-
|
| 140 |
for t in task_list:
|
| 141 |
if isinstance(t, dict) and _floats_bad(t):
|
| 142 |
raise AssertionError(
|
|
@@ -173,12 +188,18 @@ def _check_api_contracts() -> None:
|
|
| 173 |
},
|
| 174 |
)
|
| 175 |
assert step.status_code == 200, f"/step returned {step.status_code}"
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
| 177 |
score = float(reward.get("score", -1.0))
|
| 178 |
assert 0.0 < score < 1.0, f"/step reward score must be strictly in (0, 1), got {reward.get('score')}"
|
| 179 |
|
| 180 |
state = client.get("/state")
|
| 181 |
assert state.status_code == 200, f"/state returned {state.status_code}"
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
def _check_tasks_and_graders() -> None:
|
|
|
|
| 15 |
from app.tasks import TASKS
|
| 16 |
|
| 17 |
|
| 18 |
+
def _floats_bad(obj: object) -> list[float]:
|
| 19 |
+
bad: list[float] = []
|
| 20 |
+
if isinstance(obj, float) and (obj == 0.0 or obj == 1.0):
|
| 21 |
+
bad.append(obj)
|
| 22 |
+
elif isinstance(obj, dict):
|
| 23 |
+
for v in obj.values():
|
| 24 |
+
bad.extend(_floats_bad(v))
|
| 25 |
+
elif isinstance(obj, list):
|
| 26 |
+
for item in obj:
|
| 27 |
+
bad.extend(_floats_bad(item))
|
| 28 |
+
return bad
|
| 29 |
+
|
| 30 |
+
|
| 31 |
def _check_openenv_yaml(path: Path) -> None:
|
| 32 |
content = path.read_text(encoding="utf-8")
|
| 33 |
required = ["spec_version:", "name:", "type:", "runtime:", "app:", "port:"]
|
|
|
|
| 115 |
if isinstance(t, dict) and (t.get("has_grader") is True or t.get("grader_enabled") is True)
|
| 116 |
)
|
| 117 |
assert graded >= 3, "metadata must expose at least 3 tasks with graders enabled"
|
| 118 |
+
with_grader_obj = sum(
|
| 119 |
+
1
|
| 120 |
+
for t in tasks
|
| 121 |
+
if isinstance(t, dict)
|
| 122 |
+
and isinstance(t.get("grader"), dict)
|
| 123 |
+
and t["grader"].get("enabled") is True
|
| 124 |
+
)
|
| 125 |
+
assert with_grader_obj >= 3, "metadata.tasks must each expose grader.enabled=true (hub Phase-2)"
|
| 126 |
+
meta_graders = payload.get("graders")
|
| 127 |
+
assert isinstance(meta_graders, list) and len(meta_graders) >= 3, "metadata.graders must list >=3 entries"
|
| 128 |
+
assert sum(1 for g in meta_graders if isinstance(g, dict) and g.get("enabled") is True) >= 3
|
| 129 |
+
nested = (payload.get("grading") or {}).get("graders")
|
| 130 |
+
assert isinstance(nested, list) and len(nested) >= 3
|
| 131 |
+
assert sum(1 for g in nested if isinstance(g, dict) and g.get("enabled") is True) >= 3
|
| 132 |
|
| 133 |
|
| 134 |
def _check_tasks_endpoint() -> None:
|
|
|
|
| 152 |
assert manifest.status_code == 200, f"/openenv.yaml returned {manifest.status_code}"
|
| 153 |
assert b"has_grader:" in manifest.content or b"grader:" in manifest.content
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
for t in task_list:
|
| 156 |
if isinstance(t, dict) and _floats_bad(t):
|
| 157 |
raise AssertionError(
|
|
|
|
| 188 |
},
|
| 189 |
)
|
| 190 |
assert step.status_code == 200, f"/step returned {step.status_code}"
|
| 191 |
+
step_body = step.json()
|
| 192 |
+
if _floats_bad(step_body):
|
| 193 |
+
raise AssertionError(f"/step JSON must not contain float 0.0 or 1.0 anywhere: {step_body}")
|
| 194 |
+
reward = step_body.get("reward") or {}
|
| 195 |
score = float(reward.get("score", -1.0))
|
| 196 |
assert 0.0 < score < 1.0, f"/step reward score must be strictly in (0, 1), got {reward.get('score')}"
|
| 197 |
|
| 198 |
state = client.get("/state")
|
| 199 |
assert state.status_code == 200, f"/state returned {state.status_code}"
|
| 200 |
+
state_body = state.json()
|
| 201 |
+
if _floats_bad(state_body):
|
| 202 |
+
raise AssertionError(f"/state JSON must not contain float 0.0 or 1.0 anywhere: {state_body}")
|
| 203 |
|
| 204 |
|
| 205 |
def _check_tasks_and_graders() -> None:
|