submit: move handle_submit into its own module + add validation pipeline
Browse filesStep 6 (E) chunk 2 of the async submit work. Lifts the UI-only stub
out of app.py into submit.py and grows it into the real cheap-sync
validation pipeline. Still no Hub writes and no eval kick-off; the
handler returns "would queue here" placeholder text. Async write +
worker thread land in later chunks.
Validation gates, in order:
1. Form: zip attached, agree-checkbox ticked.
2. Zip safety: valid zip, no absolute / parent-traversing entry
names, no symlinks (Python 3.12's extractall normalises unsafe
paths silently; we reject so submitters see a clear error).
3. meta.json schema: required keys, types, agree_to_publish is
literal true, notes within the 500-char cap. meta.json is the
source of truth for submitter / submission identity; the other
form fields are advisory for now and kept only so the gr.click
signature stays stable.
4. Fixture set: top-level dirs in the zip equal the fixture set
reported by cadgenbench.common.paths.data_inputs_dir(), no
missing, no extras.
5. STEP parseability: each <fixture>/output.step loads via the new
cadgenbench parse_step() helper. Per-fixture validity (BRepCheck,
watertight, mesh-manifold) stays the evaluator's job, where
class-3 ("submission accepted, fixture invalid, scored zero")
is the right failure mode.
submit.py is side-effect-free at this point so the validation logic
can be shaken out against real uploads without committing rows to
results.jsonl. Sanity-checked locally against 13 scenarios (happy
path, missing/extra fixtures, corrupted/empty STEPs, missing/bad
meta.json, oversized notes, zip-slip, bad-zip) before pushing.
Dockerfile bumps CADGENBENCH_SHA to d88e39c to pull in the new
cadgenbench.common.validity.parse_step (separate commit on the
benchmark repo). The cadgenbench layer rebuild costs ~3 min cold.
Files changed (3)
- Dockerfile+1-1
- app.py+7-34
- submit.pyAdded+261
43 unmodified lines444546474748495043 unmodified lines
# cadgenbench from the Public GitHub repo, pinned to a commit. Bumping# CADGENBENCH_SHA is the one-line path to picking up a new cadgenbench.ARG CADGENBENCH_SHA=ed31274ARG CADGENBENCH_SHA=d88e39cRUN pip install --no-cache-dir \ "cadgenbench @ git+https://github.com/huggingface/cadgenbench.git@${CADGENBENCH_SHA}"
123453456791089101 unmodified line12131418192021222324252627282930313233343536373839404142431516171855 unmodified lines74757610510610777787980818283"""CADGenBench Leaderboard Space - Gradio UI assembly.
Read path lives in :mod:`leaderboard`. The submit handler is a UI-onlystub here; the real validation + async eval lands in :mod:`submit` aspart of Step 6 (E).Read path lives in :mod:`leaderboard`. Submit-tab validation lives in:mod:`submit`. Both are wired into the Gradio app below."""from __future__ import annotations
from pathlib import Path
import gradio as gr
from leaderboard import (1 unmodified line HF_SUBMISSIONS_REPO, load_leaderboard,)
def handle_submit( zip_file, submission_name: str, submitter: str, agent_url: str, notes: str, agree: bool,) -> str: if zip_file is None: return "**Error:** please attach a submission zip." if not submission_name.strip(): return "**Error:** please fill in the Submission name." if not submitter.strip(): return "**Error:** please fill in your Submitter name." if not agree: return "**Error:** you must agree to publish before submitting."
name = Path(zip_file.name).name return ( f"Received `{name}` - submission `{submission_name}` by `{submitter}`.\n\n" f"_Evaluation is not wired yet (Step 6 of the build plan). Once it " f"is, this submission will run the CPU eval inline and append a row " f"to `{HF_SUBMISSIONS_REPO}`._" )from submit import handle_submit
ABOUT_MD = f"""## About55 unmodified linesand stripped to a single line. Shown in the per-submission detail view,not in the main leaderboard table.
The Space runs the CPU eval inline and appends a row to`{HF_SUBMISSIONS_REPO}`. You can fill the fields below to override`meta.json` for a quick test.`meta.json` inside the zip is the source of truth for all fivesubmission fields. The form below ticks the "agree to publish" gateand lets you smoke-test the UI; the other text fields aren't read bythe handler today.""" ) zip_in = gr.File(label="Submission ZIP", file_types=[".zip"])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261"""Submit-tab handler for the CADGenBench leaderboard Space.
Step 6 (E) chunk 2: the cheap-sync validation pipeline. The handlervalidates the upload and returns a placeholder message describing thesubmission it would have queued. No Hub writes, no eval kick-off, nobackground work. The async write + worker thread land in later chunksof Step 6 (E); see ``space-setup/step-6e-async.md``.
Validation gates, in order:
1. Form-level: a file was attached, the agree checkbox is ticked.2. Zip safety: parseable as a zip, no absolute / parent-traversing entry names, no symlinks.3. ``meta.json`` schema: required keys present, types sane, ``agree_to_publish`` is literally ``true``, ``notes`` is non-empty when present and within the per-submission cap. Single source of truth for submitter / submission identity; the form fields above are advisory for chunk 2 and ignored except for the agree gate.4. Fixture-set match: the set of folders inside the zip equals the set of fixture directories in :func:`cadgenbench.common.paths.data_inputs_dir` (no missing, no extras).5. STEP parseability: each ``<fixture>/output.step`` loads as STEP geometry. Per-fixture validity (watertight, manifold, etc) is *not* checked here, that's the evaluator's job and contributes to the per-fixture score; this gate only rejects "not actually STEP"."""from __future__ import annotations
import jsonimport reimport tempfileimport zipfilefrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Any
from cadgenbench.common.paths import data_inputs_dirfrom cadgenbench.common.validity import parse_step
NOTES_MAX_CHARS = 500REQUIRED_META_KEYS: tuple[str, ...] = ( "submitter_name", "submission_name", "agent_url", "notes", "agree_to_publish",)SUBMISSION_ID_SLUG_MAX = 40
class _ValidationError(Exception): """Internal sentinel that maps to a user-facing rejection message."""
def handle_submit( zip_file, submission_name: str, # noqa: ARG001 - kept for UI compat; meta.json wins submitter: str, # noqa: ARG001 - kept for UI compat; meta.json wins agent_url: str, # noqa: ARG001 - kept for UI compat; meta.json wins notes: str, # noqa: ARG001 - kept for UI compat; meta.json wins agree: bool,) -> str: """Validate a submission upload and return a markdown UI message.
Returns one of: - An error string starting with ``**Error:**`` for form-level rejects (missing zip, unticked agree box). - A rejection string starting with ``**Submission rejected.**`` for any of the deeper validation gates. - A success string starting with ``**Validation OK.**`` carrying the would-be ``submission_id``.
The form fields other than ``agree`` are accepted to keep the Gradio click signature stable, but ``meta.json`` inside the zip is the source of truth for submitter / submission identity. """ form_err = _validate_form(zip_file, agree) if form_err is not None: return form_err
zip_path = Path(zip_file.name) with tempfile.TemporaryDirectory(prefix="cadgenbench-validate-") as tmp: unpacked = Path(tmp) / "unpacked" unpacked.mkdir() try: _extract_zip(zip_path, unpacked) meta = _load_and_validate_meta(unpacked) fixture_names = _validate_fixture_set(unpacked) _validate_steps_parseable(unpacked, fixture_names) except _ValidationError as e: return f"**Submission rejected.** {e}"
submission_id = _mint_submission_id( meta["submitter_name"], meta["submission_name"] ) return ( f"**Validation OK.** Would queue submission `{submission_id}` " f"(submitter: `{meta['submitter_name']}`, system: " f"`{meta['submission_name']}`, {len(fixture_names)} fixtures).\n\n" f"_Chunk 2 of Step 6 (E): validation only. Hub write + eval " f"kick-off land in the next chunk._" )
def _validate_form(zip_file, agree: bool) -> str | None: if zip_file is None: return "**Error:** please attach a submission zip." if not agree: return "**Error:** you must agree to publish before submitting." return None
def _extract_zip(zip_path: Path, target: Path) -> None: """Extract *zip_path* into *target* with zip-slip + symlink rejection.
Python's ``ZipFile.extractall`` since 3.12 normalises away unsafe paths silently; we'd rather reject the upload outright so the submitter sees a clear error instead of getting a "fixture set mismatch" downstream because half their files were dropped. """ try: with zipfile.ZipFile(zip_path) as zf: for info in zf.infolist(): if info.is_dir(): continue name = Path(info.filename) if name.is_absolute() or ".." in name.parts: raise _ValidationError( f"Zip contains an unsafe path: {info.filename!r}." ) # Unix mode lives in the high 16 bits of external_attr; # symlinks are mode 0o120000 (S_IFLNK). mode = info.external_attr >> 16 if mode and (mode & 0o170000) == 0o120000: raise _ValidationError( f"Zip contains a symlink ({info.filename!r}); " f"submissions must be plain files." ) zf.extractall(target) except zipfile.BadZipFile as e: raise _ValidationError(f"Upload is not a valid zip file: {e}") from e
def _load_and_validate_meta(unpacked: Path) -> dict[str, Any]: meta_path = unpacked / "meta.json" if not meta_path.is_file(): raise _ValidationError( "Zip is missing top-level `meta.json` (expected at the root of " "the zip, alongside the per-fixture folders)." ) try: meta = json.loads(meta_path.read_text()) except json.JSONDecodeError as e: raise _ValidationError( f"`meta.json` is not valid JSON: {e.msg} (line {e.lineno})." ) from e if not isinstance(meta, dict): raise _ValidationError( "`meta.json` must be a JSON object at the top level." )
missing = [k for k in REQUIRED_META_KEYS if k not in meta] if missing: raise _ValidationError( f"`meta.json` is missing required key(s): {', '.join(missing)}." )
for k in ("submitter_name", "submission_name"): v = meta[k] if not isinstance(v, str) or not v.strip(): raise _ValidationError( f"`meta.json` field `{k}` must be a non-empty string." )
for k in ("agent_url", "notes"): v = meta[k] if v is not None and not isinstance(v, str): raise _ValidationError( f"`meta.json` field `{k}` must be a string or null." )
if meta["agree_to_publish"] is not True: raise _ValidationError( "`meta.json` field `agree_to_publish` must be the literal boolean " "`true`." )
if meta["notes"] is not None: meta["notes"] = _normalize_notes(meta["notes"])
return meta
def _normalize_notes(raw: str) -> str: """Collapse newlines + tabs to spaces, strip, enforce the char cap.""" one_line = re.sub(r"[\r\n\t]+", " ", raw).strip() if len(one_line) > NOTES_MAX_CHARS: raise _ValidationError( f"`meta.json` field `notes` exceeds the {NOTES_MAX_CHARS}-char " f"cap (got {len(one_line)} after stripping). Trim and resubmit." ) return one_line
def _validate_fixture_set(unpacked: Path) -> set[str]: """Compare unpacked top-level dirs to the inputs dataset's fixture set.""" actual = {p.name for p in unpacked.iterdir() if p.is_dir()}
try: inputs_root = data_inputs_dir() except Exception as e: # noqa: BLE001 - paths.py raises a few types raise _ValidationError( f"Server-side error resolving the fixture set " f"({type(e).__name__}: {e})." ) from e expected = {p.name for p in inputs_root.iterdir() if p.is_dir()}
missing = expected - actual extras = actual - expected if missing or extras: parts: list[str] = [] if missing: parts.append(f"missing fixture(s): {', '.join(sorted(missing))}") if extras: parts.append(f"unexpected folder(s): {', '.join(sorted(extras))}") raise _ValidationError( "Fixture set does not match the dataset. " + "; ".join(parts) + "." ) return expected
def _validate_steps_parseable(unpacked: Path, fixture_names: set[str]) -> None: for name in sorted(fixture_names): step = unpacked / name / "output.step" if not step.is_file(): raise _ValidationError( f"Fixture `{name}` is missing its `output.step` file." ) if step.stat().st_size == 0: raise _ValidationError( f"Fixture `{name}` has an empty `output.step`." ) try: parse_step(step) except RuntimeError as e: raise _ValidationError( f"Fixture `{name}` has an `output.step` that is not loadable " f"as STEP geometry: {e}" ) from e
def _mint_submission_id(submitter_name: str, submission_name: str) -> str: """Build the basename used for ``submissions/<id>.zip`` and ``reports/<id>.*``.""" ts = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S") return f"{_slug(submitter_name)}_{_slug(submission_name)}_{ts}"
def _slug(s: str) -> str: """Filesystem-safe slug. Lowercase, ``[a-z0-9-]``, collapsed dashes.""" cleaned = re.sub(r"[^A-Za-z0-9]+", "-", s).strip("-").lower() return cleaned[:SUBMISSION_ID_SLUG_MAX] or "unnamed"