Michael Rabinovich Cursor commited on
Commit ·
b224eee
1
Parent(s): dc8ff2a
submit: retry Hub commits on 429/5xx + persistent status panel
Browse filesWrap every Space-side commit (zip upload, results.jsonl RMW, report +
gallery uploads, shard cleanup) in a backoff-with-jitter retry that
honors Retry-After, so a rate-limited submissions repo delays rather
than fails a submit. Make handle_submit a generator streaming a
durable status panel (validating -> uploading -> queued, or the
rejection reason) instead of relying on transient toasts.
Co-authored-by: Cursor <cursoragent@cursor.com>
Files changed (3)
- app.py+17-5
- submit.py+186-74
- tests/test_submit.py+82
122 unmodified lines123124125126127128655 unmodified lines784785786787788789790791792793794122 unmodified linesFull policy: [`docs/benchmark/validation.md`]({VALIDATION_DOC_URL})."""DETAIL_PLACEHOLDER = "_Click a row above for details._"def _has(value) -> bool:655 unmodified lines# Starts disabled; the `blocks.load` handler below flips it# to interactive when an OAuthProfile is present.submit_btn = gr.Button("Submit", variant="primary", interactive=False)# No static markdown output: handle_submit surfaces every# status update via gr.Info / gr.Error toasts. The handler# also reads `gr.OAuthProfile` implicitly via its parameter# type annotation (Gradio's dependency-injection convention).submit_btn.click(fn=handle_submit, inputs=[zip_in])with gr.Tab("About"):gr.Markdown(ABOUT_MD)122 unmodified lines123124125126127128129130131132655 unmodified lines788789790791792793794795796797798799800801802803804805806122 unmodified linesFull policy: [`docs/benchmark/validation.md`]({VALIDATION_DOC_URL})."""DETAIL_PLACEHOLDER = "_Click a row above for details._"SUBMIT_STATUS_IDLE = ("_Log in, attach a zip, and click **Submit**. Progress and any ""errors appear here._")def _has(value) -> bool:655 unmodified lines# Starts disabled; the `blocks.load` handler below flips it# to interactive when an OAuthProfile is present.submit_btn = gr.Button("Submit", variant="primary", interactive=False)# Persistent status panel. handle_submit is a generator that# streams stage updates (validating -> uploading/queuing -># queued) and any rejection reason here, so the outcome# survives instead of vanishing with a transient toast. The# handler also reads `gr.OAuthProfile` implicitly via its# parameter type annotation (Gradio's dependency-injection# convention).submit_status = gr.Markdown(value=SUBMIT_STATUS_IDLE)submit_btn.click(fn=handle_submit,inputs=[zip_in],outputs=[submit_status],)with gr.Tab("About"):gr.Markdown(ABOUT_MD)
87 unmodified lines88899091929317 unmodified lines11111211311411511611720 unmodified lines13813914014114214370 unmodified lines21421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126213 unmodified lines2762772782792802812821 unmodified line2842852862872882892902912922932942952962972982 unmodified lines301302303304305306307308309310311312313314194 unmodified lines509510511512513514515516517518519520146 unmodified lines66766866967067167267367412 unmodified lines687688689690691692546 unmodified lines12391240124112421243124412451246124712481249125012511252125312541255125612571258125920 unmodified lines128012811282128312841285128612871288128912901291129212931294129510 unmodified lines1306130713081309131013111312131313141315131687 unmodified linesimport jsonimport loggingimport osimport reimport shutilimport tempfile17 unmodified linesrun_job,snapshot_download,)from huggingface_hub.errors import EntryNotFoundErrorfrom leaderboard import HF_DATA_REPO, HF_ORG, HF_SUBMISSIONS_REPO20 unmodified linesDATA_REV_SHORT_LEN = 12FAILURE_REASON_MAX_CHARS = 200SHA256_BLOCK_SIZE = 64 * 1024STUCK_PENDING_THRESHOLD_SECONDS = 30 * 60SUBMITTED_AT_FORMAT = "%Y-%m-%dT%H:%M:%SZ"STUCK_PENDING_REASON = "evaluation interrupted by Space restart"70 unmodified lines"""Raised when a Hub upload fails after validation succeeded."""def handle_submit(zip_file,profile: gr.OAuthProfile | None,) -> None:"""Validate a submission upload; surface progress + outcome via toasts.Requires the user to be logged in via ``gr.LoginButton`` so therow's ``hf_username`` is the canonical HF identity (not afree-text claim). The submit button in :mod:`app` is disableduntil login completes; this function also raises ``gr.Error``defensively if it's called without a profile so a UI mishapcan't write an anonymous row.Side-effect-only (returns ``None``): in-flight status surfaces via``gr.Info`` toasts (validating on click, queued after the workerspawns) and ``gr.Error`` toasts for rejections. Rejection pathsraise ``gr.Error``, which Gradio surfaces as a red toast andaborts the handler; the outer ``try/finally`` still runs to cleanup the temp dir.Toasts on the happy path:1. ``"Validating submission..."`` on click, with a wall-time hintso the user knows the post-queue eval takes ~1 minute.2. ``"Submission <id> queued ..."`` once the row + zip are on theHub and the worker has been spawned.On rejection (login-missing, form-level, validation gate, dedup,or Hub write), a single ``gr.Error`` toast carries the message;no second toast."""if profile is None:raise gr.Error("Please log in via the HF button before submitting.")form_err = _validate_form(zip_file)if form_err is not None:raise gr.Error(form_err)# Immediate ack on click. The eval itself (after queuing) is the# slow part, so hint at the wall time up front so the user knows# what to expect instead of waiting on a silent button.gr.Info("Validating submission... evaluation usually takes ""~1 minute after queuing.")zip_path = Path(zip_file.name)13 unmodified linesfixture_names = _validate_fixture_set(run_dir)_validate_steps_parseable(run_dir, fixture_names)except _ValidationError as e:raise gr.Error(f"Submission rejected: {e}")# Dedup gate: hash the raw zip bytes and reject if an existing# row carries the same hash. Runs after validation so a clearly1 unmodified linezip_sha256 = _compute_sha256(zip_path)existing_id = _find_existing_submission_by_sha256(zip_sha256)if existing_id is not None:raise gr.Error(f"This zip's contents are identical to an existing "f"submission ({existing_id}). Resubmit only after changing "f"at least one byte of the upload.")submission_id = _mint_submission_id(meta["submitter_name"], meta["submission_name"])try:blob_url = _upload_submission_zip(submission_id, zip_path)row = _build_pending_row(2 unmodified lines)_append_pending_row(row)except _HubWriteError as e:raise gr.Error(f"Submission rejected: {e}")_spawn_worker(submission_id, blob_url, sorted(fixture_names))gr.Info(f"Submission {submission_id} queued for evaluation "f"({len(fixture_names)} fixtures). The eval runs on an "f"HF Jobs GPU; the row flips to completed when the job "f"finishes (typically 1-3 minutes).")finally:shutil.rmtree(tmp, ignore_errors=True)194 unmodified lines"""repo_path = f"{SUBMISSIONS_DIR}/{submission_id}.zip"try:_HF_API.upload_file(path_or_fileobj=str(zip_path),path_in_repo=repo_path,repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add submission zip for {submission_id}",)except Exception as e: # noqa: BLE001 - Hub API surface is broadlogger.exception("Failed to upload submission zip %s", submission_id)146 unmodified linesThe lock is held only for the read-modify-write cycle (~1-2s),never for eval time. Concurrent submitters serialise here, notin the eval pipeline. Treats a missing file as the empty list."""with _HUB_LOCK:existing = _download_results_jsonl()rows: list[dict[str, Any]] = [json.loads(line) for line in existing.splitlines() if line.strip()12 unmodified linescommit_message=commit_message,)def _download_results_jsonl() -> str:"""Fetch the current ``results.jsonl`` body as text, or ``""`` if absent."""546 unmodified linesartifacts land at the exact paths the leaderboard + the row-flipexpect. Uses the process ``HfApi`` (Space HF_TOKEN env)."""_HF_API.upload_file(path_or_fileobj=str(html_path),path_in_repo=f"{REPORTS_DIR}/{submission_id}.html",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add merged HTML report for {submission_id}",)_HF_API.upload_file(path_or_fileobj=json.dumps(report_json, ensure_ascii=False, indent=2,).encode("utf-8"),path_in_repo=f"{REPORTS_DIR}/{submission_id}.json",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add merged JSON report for {submission_id}",)logger.info("Uploaded merged reports/%s.{html,json}", submission_id)20 unmodified lineslogger.info("No gallery renders to upload for %s", submission_id)returnfor iso_png, fixture_name in staged:_HF_API.upload_file(path_or_fileobj=str(iso_png),path_in_repo=(f"{RENDERS_DIR}/{submission_id}/{fixture_name}.png"),repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=(f"add gallery render {fixture_name} for {submission_id}"),)logger.info("Uploaded %d gallery render(s) under %s/%s/",10 unmodified linessubmission."""try:_HF_API.delete_folder(path_in_repo=f"{REPORTS_DIR}/{submission_id}/{SHARDS_SUBDIR}",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"clean up eval shards for {submission_id}",)logger.info("Cleaned up shard artifacts for %s", submission_id)except Exception as e: # noqa: BLE001 - cleanup is best-effort87 unmodified lines8889909192939417 unmodified lines11211311411511611711820 unmodified lines13914014114214314414514614714814915015115215315415515670 unmodified lines22722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433513 unmodified lines3493503513523533543553563571 unmodified line3593603613623633643653663673683693703713723733743753763773783793803812 unmodified lines384385386387388389390391392393394395396397398399400194 unmodified lines595596597598599600601602603604605606607608609146 unmodified lines75675775875976076176276376476576676776876912 unmodified lines782783784785786787788789790546 unmodified lines13371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136320 unmodified lines13841385138613871388138913901391139213931394139513961397139813991400140114021403140410 unmodified lines1415141614171418141914201421142214231424142514261427142887 unmodified linesimport jsonimport loggingimport osimport randomimport reimport shutilimport tempfile17 unmodified linesrun_job,snapshot_download,)from huggingface_hub.errors import EntryNotFoundError, HfHubHTTPErrorfrom leaderboard import HF_DATA_REPO, HF_ORG, HF_SUBMISSIONS_REPO20 unmodified linesDATA_REV_SHORT_LEN = 12FAILURE_REASON_MAX_CHARS = 200SHA256_BLOCK_SIZE = 64 * 1024# Hub-write retry policy. HF rate-limits commits per repo, so a burst# of concurrent commits (e.g. bulk baseline seeding writing into this# same dataset) can 429 an otherwise-valid submit at the commit step.# Retry transient statuses with exponential backoff + jitter, honoring# a server Retry-After, up to a wall-clock cap. Applied to every# Space-side commit so a busy repo delays a submit instead of failing# it._HUB_RETRY_STATUSES = frozenset({429, 500, 502, 503, 504})HUB_RETRY_MAX_SECONDS = 120HUB_RETRY_BASE_DELAY_SECONDS = 2.0HUB_RETRY_MAX_DELAY_SECONDS = 20.0STUCK_PENDING_THRESHOLD_SECONDS = 30 * 60SUBMITTED_AT_FORMAT = "%Y-%m-%dT%H:%M:%SZ"STUCK_PENDING_REASON = "evaluation interrupted by Space restart"70 unmodified lines"""Raised when a Hub upload fails after validation succeeded."""def _retry_after_seconds(error: HfHubHTTPError) -> float | None:"""Parse a ``Retry-After`` header (seconds form) off a Hub error, if any."""response = getattr(error, "response", None)if response is None:return Noneraw = response.headers.get("Retry-After")if not raw:return Nonetry:return float(raw)except (TypeError, ValueError):return Nonedef _with_hub_retries(fn, *, what: str):"""Run *fn* (a Hub commit) retrying transient HTTP errors with backoff.Retries only the statuses in :data:`_HUB_RETRY_STATUSES` (ratelimits + transient 5xx); any other error (auth, validation, a``LookupError`` from a mutate closure) propagates immediately.Backoff is exponential with jitter, clamped to:data:`HUB_RETRY_MAX_DELAY_SECONDS`, never sleeps past the:data:`HUB_RETRY_MAX_SECONDS` wall cap, and honors a server``Retry-After`` when present. *fn* must be idempotent across calls-- every caller here re-reads the remote state inside *fn* beforecommitting, so a retried commit can't double-apply."""deadline = time.monotonic() + HUB_RETRY_MAX_SECONDSattempt = 0while True:attempt += 1try:return fn()except HfHubHTTPError as e:status = getattr(getattr(e, "response", None), "status_code", None)now = time.monotonic()if status not in _HUB_RETRY_STATUSES or now >= deadline:raisedelay = min(HUB_RETRY_MAX_DELAY_SECONDS,HUB_RETRY_BASE_DELAY_SECONDS * (2 ** (attempt - 1)),)delay = delay / 2 + random.uniform(0, delay / 2)retry_after = _retry_after_seconds(e)if retry_after is not None:delay = max(delay, retry_after)delay = min(delay, max(0.0, deadline - now))logger.warning("Hub %s got HTTP %s; retry %d in %.1fs (cap %ds)",what, status, attempt, delay, HUB_RETRY_MAX_SECONDS,)time.sleep(delay)def _submit_status(state: str, message: str) -> str:"""Markdown for the persistent submit-status panel.The panel is the durable counterpart to the transient ``gr.Info`` /``gr.Error`` toasts: a submitter always sees the current stage andany rejection reason without having to catch an ephemeral toast.*state* picks the leading glyph (``working`` / ``queued`` /``error``)."""glyph = {"working": "⏳", "queued": "✅", "error": "❌"}.get(state, "•")return f"{glyph} {message}"def handle_submit(zip_file,profile: gr.OAuthProfile | None,):"""Validate + queue a submission, streaming status to a panel.Requires the user to be logged in via ``gr.LoginButton`` so therow's ``hf_username`` is the canonical HF identity (not afree-text claim). The submit button in :mod:`app` is disableduntil login completes; this generator also rejects defensively ifit's called without a profile so a UI mishap can't write ananonymous row.Generator: each ``yield`` is a Markdown string pushed to apersistent status panel, the durable counterpart to the transienttoasts. Happy-path stages: validating -> uploading/queuing ->queued. Every rejection (login-missing, form-level, validationgate, dedup, Hub write) yields a final error panel **and** raises``gr.Error`` for a toast; the outer ``try/finally`` still runs toclean up the temp dir. The Hub writes inside ride out transientrate limits via :func:`_with_hub_retries`, so a busy submissionsrepo delays rather than fails the submit."""if profile is None:msg = "Please log in via the HF button before submitting."yield _submit_status("error", msg)raise gr.Error(msg)form_err = _validate_form(zip_file)if form_err is not None:yield _submit_status("error", form_err)raise gr.Error(form_err)yield _submit_status("working","Validating submission (unpacking the zip, checking the fixture set ""and STEP files)…",)zip_path = Path(zip_file.name)13 unmodified linesfixture_names = _validate_fixture_set(run_dir)_validate_steps_parseable(run_dir, fixture_names)except _ValidationError as e:msg = f"Submission rejected: {e}"yield _submit_status("error", msg)raise gr.Error(msg)# Dedup gate: hash the raw zip bytes and reject if an existing# row carries the same hash. Runs after validation so a clearly1 unmodified linezip_sha256 = _compute_sha256(zip_path)existing_id = _find_existing_submission_by_sha256(zip_sha256)if existing_id is not None:msg = (f"This zip's contents are identical to an existing "f"submission ({existing_id}). Resubmit only after changing "f"at least one byte of the upload.")yield _submit_status("error", msg)raise gr.Error(msg)submission_id = _mint_submission_id(meta["submitter_name"], meta["submission_name"])yield _submit_status("working",f"Uploading `{submission_id}` ({len(fixture_names)} fixtures) and "f"queuing the evaluation… (this can take a moment, and retries "f"automatically if the Hub is busy).",)try:blob_url = _upload_submission_zip(submission_id, zip_path)row = _build_pending_row(2 unmodified lines)_append_pending_row(row)except _HubWriteError as e:msg = f"Submission rejected: {e}"yield _submit_status("error", msg)raise gr.Error(msg)_spawn_worker(submission_id, blob_url, sorted(fixture_names))yield _submit_status("queued",f"Submission `{submission_id}` queued ({len(fixture_names)} "f"fixtures). The eval runs on an HF Jobs GPU; your row appears on "f"the **Unvalidated** leaderboard and flips to completed when the "f"job finishes (typically 1–3 minutes).",)finally:shutil.rmtree(tmp, ignore_errors=True)194 unmodified lines"""repo_path = f"{SUBMISSIONS_DIR}/{submission_id}.zip"try:_with_hub_retries(lambda: _HF_API.upload_file(path_or_fileobj=str(zip_path),path_in_repo=repo_path,repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add submission zip for {submission_id}",),what="submission-zip upload",)except Exception as e: # noqa: BLE001 - Hub API surface is broadlogger.exception("Failed to upload submission zip %s", submission_id)146 unmodified linesThe lock is held only for the read-modify-write cycle (~1-2s),never for eval time. Concurrent submitters serialise here, notin the eval pipeline. Treats a missing file as the empty list.The whole download->mutate->upload cycle is retried as a unit on atransient Hub error (:func:`_with_hub_retries`): re-reading thecurrent file each attempt keeps the mutation idempotent and alsofolds in any concurrent change, so a rate-limited commit waits andretries instead of failing the caller."""def _download_mutate_upload() -> None:existing = _download_results_jsonl()rows: list[dict[str, Any]] = [json.loads(line) for line in existing.splitlines() if line.strip()12 unmodified linescommit_message=commit_message,)with _HUB_LOCK:_with_hub_retries(_download_mutate_upload, what="results.jsonl write")def _download_results_jsonl() -> str:"""Fetch the current ``results.jsonl`` body as text, or ``""`` if absent."""546 unmodified linesartifacts land at the exact paths the leaderboard + the row-flipexpect. Uses the process ``HfApi`` (Space HF_TOKEN env)."""_with_hub_retries(lambda: _HF_API.upload_file(path_or_fileobj=str(html_path),path_in_repo=f"{REPORTS_DIR}/{submission_id}.html",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add merged HTML report for {submission_id}",),what="report-html upload",)_with_hub_retries(lambda: _HF_API.upload_file(path_or_fileobj=json.dumps(report_json, ensure_ascii=False, indent=2,).encode("utf-8"),path_in_repo=f"{REPORTS_DIR}/{submission_id}.json",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"add merged JSON report for {submission_id}",),what="report-json upload",)logger.info("Uploaded merged reports/%s.{html,json}", submission_id)20 unmodified lineslogger.info("No gallery renders to upload for %s", submission_id)returnfor iso_png, fixture_name in staged:_with_hub_retries(lambda iso_png=iso_png, fixture_name=fixture_name: (_HF_API.upload_file(path_or_fileobj=str(iso_png),path_in_repo=(f"{RENDERS_DIR}/{submission_id}/{fixture_name}.png"),repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=(f"add gallery render {fixture_name} for {submission_id}"),)),what="gallery-render upload",)logger.info("Uploaded %d gallery render(s) under %s/%s/",10 unmodified linessubmission."""try:_with_hub_retries(lambda: _HF_API.delete_folder(path_in_repo=f"{REPORTS_DIR}/{submission_id}/{SHARDS_SUBDIR}",repo_id=HF_SUBMISSIONS_REPO,repo_type="dataset",commit_message=f"clean up eval shards for {submission_id}",),what="shard cleanup",)logger.info("Cleaned up shard artifacts for %s", submission_id)except Exception as e: # noqa: BLE001 - cleanup is best-effort
11 unmodified lines1213141516171819202111 unmodified linesfrom __future__ import annotationsfrom pathlib import Pathimport submitdef _stub_meta() -> dict:"""Minimum meta.json shape that survives ``_load_and_validate_meta``."""return {11 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210311 unmodified linesfrom __future__ import annotationsfrom pathlib import Pathfrom types import SimpleNamespaceimport pytestimport submitdef _hub_http_error(status: int, headers: dict | None = None) -> submit.HfHubHTTPError:"""An ``HfHubHTTPError`` with a minimal response carrying *status*.Built without going through the real Hub: construct the exceptionwith no response, then attach a stand-in so ``_with_hub_retries``can read ``response.status_code`` / ``response.headers``."""err = submit.HfHubHTTPError(f"HTTP {status}")err.response = SimpleNamespace(status_code=status, headers=headers or {})return errdef test_with_hub_retries_recovers_after_transient(monkeypatch):"""A 429 (then a 503) is retried; the eventual success is returned."""monkeypatch.setattr(submit.time, "sleep", lambda *_: None)statuses = iter([429, 503])calls = {"n": 0}def flaky():calls["n"] += 1status = next(statuses, None)if status is not None:raise _hub_http_error(status)return "ok"assert submit._with_hub_retries(flaky, what="test") == "ok"assert calls["n"] == 3def test_with_hub_retries_reraises_non_retryable(monkeypatch):"""A 403 is not in the retry set, so it propagates on the first try."""monkeypatch.setattr(submit.time, "sleep", lambda *_: None)calls = {"n": 0}def forbidden():calls["n"] += 1raise _hub_http_error(403)with pytest.raises(submit.HfHubHTTPError):submit._with_hub_retries(forbidden, what="test")assert calls["n"] == 1def test_with_hub_retries_gives_up_after_wall_cap(monkeypatch):"""Past the wall cap, a persistent 429 stops being retried and raises."""monkeypatch.setattr(submit.time, "sleep", lambda *_: None)# Force the deadline check to trip after the first failure: the# second monotonic() read (post-failure) already exceeds the cap.ticks = iter([0.0, submit.HUB_RETRY_MAX_SECONDS + 1])monkeypatch.setattr(submit.time, "monotonic", lambda: next(ticks, submit.HUB_RETRY_MAX_SECONDS + 1))calls = {"n": 0}def always_429():calls["n"] += 1raise _hub_http_error(429)with pytest.raises(submit.HfHubHTTPError):submit._with_hub_retries(always_429, what="test")assert calls["n"] == 1def test_retry_after_header_is_honored(monkeypatch):"""A ``Retry-After`` seconds value sets the floor for the sleep delay."""slept: list[float] = []monkeypatch.setattr(submit.time, "sleep", lambda d: slept.append(d))calls = {"n": 0}def flaky():calls["n"] += 1if calls["n"] == 1:raise _hub_http_error(429, headers={"Retry-After": "7"})return "ok"assert submit._with_hub_retries(flaky, what="test") == "ok"assert slept and slept[0] >= 7.0def _stub_meta() -> dict:"""Minimum meta.json shape that survives ``_load_and_validate_meta``."""return {
app.py+17-5
submit.py+186-74
tests/test_submit.py+82