"""Submit-tab handler for the CADGenBench leaderboard Space. Step 6 (E) chunk 2: the cheap-sync validation pipeline. The handler validates the upload and returns a placeholder message describing the submission it would have queued. No Hub writes, no eval kick-off, no background work. The async write + worker thread land in later chunks of 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 ``/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 json import re import tempfile import zipfile from datetime import datetime, timezone from pathlib import Path from typing import Any from cadgenbench.common.paths import data_inputs_dir from cadgenbench.common.validity import parse_step NOTES_MAX_CHARS = 500 REQUIRED_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/.zip`` and ``reports/.*``.""" 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"